Is it possible to make lazy combinators for IO?

If f :: a -> IO a for some a, and I want to use mfix f then f must not inspect its argument in any way, or the computation will get stuck. In some cases, this seems a bit harsh. For example, mfix (\x -> fmap (3 :) (x `seq` readLn)) looks perfectly reasonable. There is no need to inspect the return [] action to know that the final result of the computation will begin with 3:. Is there a lazy IO mapping function somewhere that can work such magic?

Urk.. I mean there's no need to execute the readLn action.
On Apr 18, 2016 4:18 PM, "David Feuer"
If
f :: a -> IO a
for some a, and I want to use
mfix f
then f must not inspect its argument in any way, or the computation will get stuck. In some cases, this seems a bit harsh. For example,
mfix (\x -> fmap (3 :) (x `seq` readLn))
looks perfectly reasonable. There is no need to inspect the return [] action to know that the final result of the computation will begin with 3:. Is there a lazy IO mapping function somewhere that can work such magic?

You can't know that the final result of the computation (x `seq` (3:...))
will begin with 3, because sometimes it doesn't! More specifically, it
diverges (without yielding the 3) if x diverges.
I don't think this is anything special about mfix: (let x = x `seq` 3:...
in x) also diverges for the same reason.
Hope that helps,
David
On 18 Apr 2016 21:19, "David Feuer"
If
f :: a -> IO a
for some a, and I want to use
mfix f
then f must not inspect its argument in any way, or the computation will get stuck. In some cases, this seems a bit harsh. For example,
mfix (\x -> fmap (3 :) (x `seq` readLn))
looks perfectly reasonable. There is no need to inspect the return [] action to know that the final result of the computation will begin with 3:. Is there a lazy IO mapping function somewhere that can work such magic? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Consider the implementation of `second` for pairs:
second f ~(a,b) = (a, f b)
Now
fix $ second (3 :)
Will be (undefined, [3,3,....])
Translating this to IO, I'd want
lazyMap f undefined
to produce as much as possible of the result, although it cannot produce
the final State# RealWorld token.
On Apr 18, 2016 4:47 PM, "David Turner"
You can't know that the final result of the computation (x `seq` (3:...)) will begin with 3, because sometimes it doesn't! More specifically, it diverges (without yielding the 3) if x diverges.
I don't think this is anything special about mfix: (let x = x `seq` 3:... in x) also diverges for the same reason.
Hope that helps,
David On 18 Apr 2016 21:19, "David Feuer"
wrote: If
f :: a -> IO a
for some a, and I want to use
mfix f
then f must not inspect its argument in any way, or the computation will get stuck. In some cases, this seems a bit harsh. For example,
mfix (\x -> fmap (3 :) (x `seq` readLn))
looks perfectly reasonable. There is no need to inspect the return [] action to know that the final result of the computation will begin with 3:. Is there a lazy IO mapping function somewhere that can work such magic? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Sorry, you've lost me. I suspect you're trying to give a minimal example of
the problem you're having, but you've stripped away too much context. What
are lazyMap and f? At least, what are their types?
On 18 Apr 2016 22:03, "David Feuer"
Consider the implementation of `second` for pairs:
second f ~(a,b) = (a, f b)
Now
fix $ second (3 :)
Will be (undefined, [3,3,....])
Translating this to IO, I'd want
lazyMap f undefined
to produce as much as possible of the result, although it cannot produce the final State# RealWorld token. On Apr 18, 2016 4:47 PM, "David Turner"
wrote: You can't know that the final result of the computation (x `seq` (3:...)) will begin with 3, because sometimes it doesn't! More specifically, it diverges (without yielding the 3) if x diverges.
I don't think this is anything special about mfix: (let x = x `seq` 3:... in x) also diverges for the same reason.
Hope that helps,
David On 18 Apr 2016 21:19, "David Feuer"
wrote: If
f :: a -> IO a
for some a, and I want to use
mfix f
then f must not inspect its argument in any way, or the computation will get stuck. In some cases, this seems a bit harsh. For example,
mfix (\x -> fmap (3 :) (x `seq` readLn))
looks perfectly reasonable. There is no need to inspect the return [] action to know that the final result of the computation will begin with 3:. Is there a lazy IO mapping function somewhere that can work such magic? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (2)
-
David Feuer
-
David Turner