[Haskell-cafe] IO, sequence, lazyness, takeWhile

Daniel Fischer daniel.is.fischer at googlemail.com
Sun Dec 19 22:58:09 CET 2010


On Sunday 19 December 2010 22:18:59, Jacek Generowicz wrote:
> >
> > The reason this doesn't stop where you expect it to is that sequence
> > is
> > effectively strict
>
> That would explain it. Thank you.
>
> Where is this fact documented? I mostly rely on Hoogle, which gets me to
>
>     
> http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude
>.html#v
>
> :sequence
>
> which says nothing about strictness.
>
> How could I have known this without having to bother anyone else?
>

Well, you can deduce it from sequence's type. That's of course not 
something you immediately see, but in hindsight, it's pretty easy to 
understand.

sequence :: Monad m => [m a] -> m [a]

So, basically all sequence can do is use (>>=) and return.
Reasonably,

sequence [] = return []

is the only thing that's possible. For nonempty lists,

sequence (x:xs) = ?

Well, what can sequence do? It has to do something with x and something 
with xs, the only reasonable thing is to call sequence on the tail and run 
x, combining x's result and the result of sequence xs.

One can choose the order, but

sequence (x:xs) = do
   a <- x
   as <- sequence xs
   return (a:as)

is the most sensible thing.

Now, that means before sequence can deliver anything, it has to run all 
actions (because if any action fails, sequence fails and that can't be 
known before all actions have been run).




More information about the Haskell-Cafe mailing list