purescript/purescript-lazy

Lazy IO

paf31 opened this issue · 5 comments

paf31 commented

We could provide unsafeLazyEff :: forall eff a. Eff eff a -> Eff eff (Lazy a) which will allow us to support lazy IO.

For example, random numbers can be generated as lazy infinite lists. Right now this is a bit of a pain.

Note this is not a substitute for things like chunked IO via Aff where we want to receive data asynchronously, but it might still be useful for synchronous effects.

Do we want this in purescript-lazy or in purescript-eff? I think the latter is a bigger package.

I think the implementation could just be this, right?

unsafeLazyEff :: forall eff a. Eff eff a -> Eff eff (Lazy a)
unsafeLazyEff a = pure $ defer \_ -> unsafePerformEff a

Is this the only way to support lazy IO? Is there anything stopping one from creating a monad implementing MonadEff that allows for a deferred bind?

(I ask because of the wording "which will allow us to support lazy IO." I recognize that even if the above is a possible/preferred approach, "cheating" can be very handy.)

paf31 commented

I think that implementation is good, assuming we want to support lazy IO at all. It can be handy for modularity in certain cases, although obviously you need to be careful.

I think this needs further discussion on whether we would want to do this and then where to put it.