hi,
I was reading lately some Haskell tutorials. The thing that come
and come again was Monads :).
There is so many tutorials on Monads over the internet but
almost none of them I think is describing the Monads well.
In most of the cases they explain just part of it..what I mean
first most of them are Haskell oriented (nothing bad with that but,
generally most of the ppl still learn Haskell or even don't want to learn Haskell at all,
so it is of no bigger use for them),
other tutorials underline the mechanics of Monads these are a
litte better because most ppl coming from imperative background
can relate things.
And finally there is those written from the
perspective of other languages Perl,Python, Ruby, JS... the
worst thing about them is that they will show you how to implement
Maybe and/or State monads, which is good but ..:(
What all of them forget to explain why the hell you will
want to use these monads in imperative languages !?
Is there any use at all and if yes when ?
So as I'm saynig there is no one good tutorial on the net
for Monads. You have to read them all to combine the pieces
to figure something at all.
The best tutorial IMHO is this one :
http://en.wikibooks.org/wiki/Haskell/Understanding_monads
OK. I'm not trying to blame the authors of writing bad tutorials
what I'm tring to say is that they always concentrate on a narrow
view of either Monad mechanics or usage or examples or Haskell ;).
I will try to explain this to myself and to you (please be warned
that I'm not totaly sure that I understand Monads myself ;)
/so bear with me and don't blame me for being totally wrong/,
what I'm trying is to provoke a discussion on the Monads mechanics,
examples in Perl, when to use them, why to use them..etc..
Let start. From what I read the Monads in Haskell are used to bring
ability to add control flow to the language (like the imperative languages).
(Haskell is more like Math, rather than C-like lang where you describe the
steps by step description how to solve a problem)
So if we have to translate this to Perl and similar lang. we should use
Monads (design pattern :), some go nuts when they hear this) when
we have to do "out-of-order" operations, which break the flow of the program.
What are these things , IMO :
- Exceptions
- Debugging
- Logging
- State handling
- etc...fill in
We have one additional benefit to this and it is the ability to interfere to all
this activities from outside of the normal-flow too, what I mean f.e. enabling/disabling
debuging/logging or modifing the handling of them w/o changing our original program
which is a good thing.
So what is the simplest way to implement Monads in Perl (for now w/o using objects).
In general from my understaning it should be something like this (in its simplest possible form) :
("bind" is the focal point of the whole monad thing)
sub bind {
my ($callback, @args) = @_;
$callback->(@args);
}
Then in our main program we do something like this :
bind(\&debug, "Entering xxx()");
...
bind(\&logit, "Username : $user logged in")
... and so on...
By itself bind() it is not that interesting. But now we can
modify it to do millon other things we have not forseen when we wrote our program.
This is the point where I start loosing the grip with the monads.
Normally in the examples instead of passing @args,
they pack them in a container which would look more like this
(if i'm not totaly wrong). :
sub bind {
my $monad = shift;#self
my $callback = shift;
my @args = @_;
my @res = $callback->(@args);
$monad->pack_args(@res);
return $monad
}
my $monad = new Monad;
$monad->bind(\&debug, "Entering xxx()");
...
$monad->bind(\&logit, "Username : $user logged in")
... and so on...
I.e. our callback function will always get the arg as normal args not like monad. And then like result we will get the monad again and so on..
Still don't quite understand the purpose of all this (if i can do it simpler in the first case).
Except that now I can modify the Monad-object to store much more state and info and of course
that OO-way is more scalable.
(Yes they also mention they want to separate/decouple the things in a way so that Monad live in its own universe, so that your original program logic is not modified when you modify the Monad. I can see the benefit of this )
What I'm worried is that it now becomes much more hard to use, I mean alot more typing and
of course it becomes slower.
And finally my last point, this seem alot to me like Aspect-design pattern ?!
So again excuse my total ignorance ;) and please explain and elaborate more on the topic.
Thanx for your attention.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.