[Haskell-cafe] >> and sequencing [newbie]
stefanor at cox.net
Sun Apr 15 20:17:37 EDT 2007
On Sun, Apr 15, 2007 at 08:04:41PM -0400, David Powers wrote:
> so... this is likely a question based on serious misunderstandings, but can
> anyone help me understand the exact mechanism by which monads enforce
> sequencing? Specifically, I'm confused by the >> operator. If I understand
> things properly f a >> g expands to something like:
> f >>= \_ -> g
> What I'm missing is how the expansion of f is ever forced under lazy
> evaluation. Since the result is never used, doesn't it just stay as a
> completely unevaluated thunk? Come to think of it... how is it that some IO
> actions sequence completely, while others manage to work in a lazy manner?
> My suspicion is that somehow the order of evaluation in Haskell gives the
> outermost expression a "first crack" at evaluation in all circumstances
> http://users.aber.ac.uk/afc/stricthaskell.html#cps) , but that it somehow
> stops short of a forced deep sequencing... Which is all to say, I have no
> idea how the magic happens.
The values that are passed around monadically aren't the whole story.
EG, in the GHC implementation, we have: (slightly simplified)
newtype IO a = IO (Thread -> ( Thread, a ))
So even though you throw away the a, the Thread *is* demanded.
More information about the Haskell-Cafe