Deep confusion about monads

Mark Carroll mark@chaos.x-philes.com
Fri, 17 Aug 2001 16:56:37 -0400 (EDT)


Gosh, thank you everybody! Let me try to summarise where I am, given all
the help.

haskell-cafe is an okay place to ask this stuff. (-: I should also check
out the wiki too; it looked a bit scary to use last time I looked, but it
seems to have relevant stuff. It's a pity that the mailing list archives
aren't more easily searched; maybe applying google to them would work.

Part of my confusion was due to a usual problem with teaching. People come
in to a lesson with preconceived ideas X, the teacher tells them stuff Y,
and instead of coming out of the lesson thinking Y, they come out thinking
f(X,Y), because the teacher didn't tell them not-X. It's instructional to
ask children where they think trees' mass comes from as they grow, after a
lesson on the subject. I originally thought that "IO a" meant "an a whose
value may depend on some IO that was done", as if that data were tainted
by other data it touched which stemmed from IO actions. Instead, "IO a" it
appears to mean "some IO action you can do that will give an a", and you
can only do stuff with the "a" within an "IO-aware" context. A Haskell
program is an IO action.

I got the idea of tainted data from Perl, which is a security aid to
help you ensure that any data drawn from outside your program has to
be approved by you before your program can use it to affect other
things outside your program.

Try to keep most code functional, even if this means that your monadic
code uses very complex referentially-transparent helper functions.

I hadn't bumped into Theo Norvell's monad tutorial yet. I'm guessing that
it's the one at http://www.engr.mun.ca/~theo/Misc/haskell_and_monads.htm ?
I'll take a look.

There is a concept of being in the IO monad. My current concept of "A >>=
B" is that it helps let B into the monad that A's in. This is how 'head'
is let in by

	readFile "/etc/hosts" >>= (return . head)

I find fmap deeply strange, though, because I don't really understand
functors. People pointed me towards fmap, and I figured from IO's instance
of Functor that

	fmap head $ readFile "/etc/hosts"

also works, so I plan to learn a little category theory to help find out
why this makes sense. (Someone also taught me how to use $. (-:)

>>= actually has the type (Monad m) => m a -> (a -> m b) -> m b
That makes some sense.

I get >> and the syntactic sugar of 'do' looks fairly simple so I'll
probably start using that more once I'm happy with the translation
between that and >>=

All the best,
              Mark