Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Laziness of sequence

0 views
Skip to first unread message

Lutz Donnerhacke

unread,
Sep 18, 2009, 12:03:25 PM9/18/09
to
During an interesting discussion in de.alt.sysadmin.recovery we touched
laziness issues of mapM.

> liftM (length) $ mapM Just [1,2,3,undefined]
Just 4

> liftM (take 3) $ mapM Just [1,2,3,undefined]
Just [1,2,3]

> liftM (take 3) $ mapM Just [1,2 ..]
_|_


This can be stripped down to the laziness behavior of sequence and from its
definition the strictness issues of (>>=).

Now the question arises, how to define sequence, to allow indefinite long
inputs?

Making it more complex did not help:
sequence xs = foldr my (return []) xs
where my = curry $ runKleisli $ Kleisli id *** Kleisli id >>^ uncurry (:)

Dirk Thierbach

unread,
Sep 18, 2009, 5:46:40 PM9/18/09
to
Lutz Donnerhacke <lu...@iks-jena.de> wrote:
>> liftM (take 3) $ mapM Just [1,2,3,undefined]
> Just [1,2,3]
>
>> liftM (take 3) $ mapM Just [1,2 ..]
> _|_

> Now the question arises, how to define sequence, to allow indefinite long
> inputs?

If I understand you correctly, you would like the result Just [1,2,3]
instead of _|_ in the last example.

But that's unlikely to work. If you look at it in terms of sequence,

liftM (take 3) $ sequence [Just 42, Just 13, Just 7, ....]

the result depends on the whole list, not just the initial segment: If
there's a Nothing *anywhere* in the list, the result is Nothing, otherwise
it's Just [42,13,7]. So the result can only be determined after
inspecting *all* of the remaining list. If the list is infinite, this
cannot happen in finite time.

- Dirk

Lutz Donnerhacke

unread,
Sep 20, 2009, 8:23:07 PM9/20/09
to
* Dirk Thierbach wrote:
> the result depends on the whole list, not just the initial segment: If
> there's a Nothing *anywhere* in the list, the result is Nothing, otherwise
> it's Just [42,13,7].

That was the point, I missed. And that is the reason why (bind) is strict in
both arguments for the Monad Maybe. Thank you.

0 new messages