|
Boost : |
From: Miroslav Silovic (miro_at_[hidden])
Date: 2003-10-21 06:19:41
David Abrahams wrote:
>>In general, given a monad "m",
>>
>>
>
>e.g. []
>
>
>
>
>>the type "m a"
>>
>>
>
>e.g. [] a or a list of a's
>
>
>
>>represents a "computation which will yield a value of type a".
>>
>>
>
>
>Huh? A list of a's is a computation which yields a value of type a??
>
>I must be off track here.
>
>
OK, I think what's missing here is why monads are absolutely necessary
in a language like Haskell. While Haskell has different (non-monadic)
ways to handle List and Maybe monads, the real issue here is the IO monad.
Haskell is a lazy language. In fact, there is no way to turn or override
laziness in Haskell - basically every operation in Haskell is understood
to take some arguments and return a result. Side-effects are non only
not allowed, they can't be expressed.
That's where comes IO monad. IO monad is a 'snapshot of the universe'. A
function that takes IO a and returns IO b basically returns the
'universe and a' and returns 'new universe and b'. In particular, a and
b may be the same type, and in fact the same instance (as in, refer to
the same spot in memory). IO can't be copied or stored (so you can't
'rewind the universe') and so it allows you to modify data in place or
do useful i/o (which by necessity has side-effects) by pretending that
you're still taking a value in and returning the result. What you need
for this to make sense is a way to explicitely express sequencing. And
this is exactly what bind does.
Hope this helps.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk