Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

With the question Listing all the contents of a directory by breadth-first order results in low efficiencyI learned that the low efficiency is due to a strange behavior of the recursive monad functions.

Try

sequence $ map return [1..]::[[Int]]
sequence $ map return [1..]::Maybe [Int]

and ghci will fall into an endless calculation.

If we rewrite the sequence function in a more readable form like follows:

sequence' []     = return []
sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)}

and try:

sequence' $ map return [1..]::[[Int]]
sequence' $ map return [1..]::Maybe [Int]

we get the same situation, an endless loop.

Try a finite list

sequence' $ map return [1..]::Maybe [Int]

it will spring out the expected result Just [1,2,3,4..] after a long time waiting.

From what we tried,we can come to the conclusion that although the definition of sequence' seems to be lazy, it is strict and has to make out all the numbers before the result of sequence' can be printed.

Not only just sequence', if we define a function

iterateM:: Monad m => (a -> m a) -> a -> m [a]
iterateM f x = (f x) >>= iterateM0 f >>= return.(x:)

and try

iterateM (>>=(+1)) 0

then endless calculation occurs.

As we all know,the non-monadic iterate is defined just like the above iterateM, but why the iterate is lazy and iterateM is strict. As we can see from above, both iterateM and sequence' are recursive monadic functions.Is there some thing strange with recursive monadic functions

like image 787
TorosFanny Avatar asked Jan 24 '13 05:01

TorosFanny


3 Answers

The problem isn't the definition of sequence, it's the operation of the underlying monad. In particular, it's the strictness of the monad's >>= operation that determines the strictness of sequence.

For a sufficiently lazy monad, it's entirely possible to run sequence on an infinite list and consume the result incrementally. Consider:

Prelude>  :m + Control.Monad.Identity
Prelude Control.Monad.Identity> runIdentity (sequence $ map return [1..] :: Identity [Int])

and the list will be printed (consumed) incrementally as desired.

It may be enlightening to try this with Control.Monad.State.Strict and Control.Monad.State.Lazy:

-- will print the list
Prelude Control.Monad.State.Lazy> evalState (sequence $ map return [1..] :: State () [Int]) ()
-- loops
Prelude Control.Monad.State.Strict> evalState (sequence $ map return [1..] :: State () [Int]) ()

In the IO monad, >>= is by definition strict, since this strictness is exactly the property necessary to enable reasoning about effect sequencing. I think @jberryman's answer is a good demonstration of what is meant by a "strict >>=". For IO and other monads with a strict >>=, each expression in the list must be evaluated before sequence can return. With an infinite list of expressions, this isn't possible.

like image 176
John L Avatar answered Oct 26 '22 14:10

John L


You're not quite grokking the mechanics of bind:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

Here's an implementation of sequence that only works on 3-length lists:

sequence3 (ma:mb:mc:[]) = ma >>= (\a-> mb >>= (\b-> mc >>= (\c-> return [a,b,c] )))

You see how we have to "run" each "monadic action" in the list before we can return the outer constructor (i.e. the outermost cons, or (:))? Try implementing it differently if you don't believe.

This is one reason monads are useful for IO: there is an implicit sequencing of effects when you bind two actions.

You also have to be careful about using the terms "lazy" and "strict". It's true with sequence that you must traverse the whole list before the final result can be wrapped, but the following works perfectly well:

Prelude Control.Monad> sequence3 [Just undefined, Just undefined, Nothing]
Nothing
like image 36
jberryman Avatar answered Oct 26 '22 14:10

jberryman


Monadic sequence cannot in general work lazily on infinite lists. Consider its signature:

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

It combines all monadic effects in its argument into a single effect. If you apply it to an infinite list, you'd need to combine an infinite number of effect into one. For some monads, it is possible, for some monads, it is not.

As an example, consider sequence specialized to Maybe, as you did in your example:

sequence :: [Maybe a] -> Maybe [a]

The result is Just ... iff all elements in the array are Just .... If any of the elements is Nothing then the result is Nothing. This means that unless you examine all elements of the input, you cannot tell if the result is Nothing or Just ....

The same applies for sequence specialized to []: sequence :: [[a]] -> [[a]]. If any of the elements of the argument is an empty list, the whole result is an empty list, like in sequence [[1],[2,3],[],[4]]. So in order to evaluate sequence on a list of lists, you have to examine all the elements to see what the result will look like.


On the other hand, sequence specialized to the Reader monad can process its argument lazily, because there is no real "effect" on Reader's monadic computation. If you define

inf :: Reader Int [Int]
inf = sequence $ map return [1..]

or perhaps

inf = sequence $ map (\x -> reader (* x)) [1..]

it will work lazily, as you can see by calling take 10 (runReader inf 3).

like image 11
Petr Avatar answered Oct 26 '22 15:10

Petr