Home > Archive > Functional > March 2007 > Maybe monad (was: mathementical/formal foundations of computing ?)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Maybe monad (was: mathementical/formal foundations of computing ?)
|
|
| Dirk Thierbach 2007-02-24, 4:16 am |
| Paul Rubin <http://phr.cx@nospam.invalid> wrote:
> Phil Armstrong <phil-news@kantaka.co.uk> writes:
[color=darkred]
> Well why does it even support the >>= operator?
For some Monads, one can think about (>>=) as some sort of "sequencing".
Then in an expression like "F >>= \x -> G[x]" (by G[x] I mean that x is
a free variable in the expression G), it means "first do F, bind the
result of F to x, if possible, and then do G".
For the Maybe monad, the result can be either "Just v" or "Nothing".
So the obvious thing to is to bind v to x in the first case, and then
do G, and in the second case just "stop" and return "Nothing" for the
whole thing. (Exercise: What should return do?)
This is like error handling: If an error happens in F, you stop, otherwise
you continue with G. That's the reason the Maybe monad is sometimes also
called the error monad. (Exercise: What do you have to do if not only
want to flag an error, but also have an extra argument (say, a string)
indicting what sort of error happened? What well-known datatype from
the library corresponds to that, and what new monad do you get this
way?)
It's not always so simple, the list monad for example does something
a bit more complex.
> It could be that I'm simply about how typeclasses work, but
> I thought something like that couldn't happen by accident.
Of course it doesn't happen by accident :-) You must prove to the
compiler that Maybe is indeed an instance of the Monad typeclass,
which is what the library does in the corresponding instance
declaration. Just look it up, and see if you can match the
implementation to the effect described above.
- Dirk
[Unnecessary NGs removed from f'up]
| |
| Paul Rubin 2007-03-03, 4:05 am |
| Dirk Thierbach <dthierbach@usenet.arcornews.de> writes:
> For some Monads, one can think about (>>=) as some sort of "sequencing".
> Then in an expression like "F >>= \x -> G[x]" (by G[x] I mean that x is
> a free variable in the expression G), it means "first do F, bind the
> result of F to x, if possible, and then do G".
It is starting to glimmer on me that the \x->G[x] in the above means
something other than function application in the usual sense. Maybe this
is how the mysterious operation of lifting manages to work. Am I on the
right track?
Thanks.
|
|
|
|
|