[Haskell-cafe] Monadic tunnelling: the art of threading one monad through another

Jules Bean jules at jellybean.co.uk
Thu Jul 12 09:40:48 EDT 2007

Henning Thielemann wrote:
> On Wed, 11 Jul 2007, Jules Bean wrote:
>> Now, supposing we have a library function which takes a callback. As
>> an example, suppose we have a library function which reads in a file
>> and calls our callback once on each line. A typical type might look
>> like:
>> forEachLine :: Handle -> (String -> IO ()) -> IO ()
>> We have to provide a callback in the IO monad. But what if we don't
>> want to work in the IO monad? What if we are working in, for example,
>> a custom state monad of our own?
> What about writing a function with signature
> forEachLine :: MonadIO io => Handle -> (String -> io ()) -> IO ()
> ?

I think the best you can do is:

MonadIO io => (String -> io()) -> io ()

'MonadIO' lets you lift one way but not the other. Correct me if I'm wrong.

As for that: yes, that would be great. It would be great if more 
libraries made their callbacks had signatures like that.

But FFI libraries don't have that signature.

(And it wouldn't be easy to make them; if you lift some IO into a ListT, 
then you'll cause the IO parts to be run more than once! This is 
expected behaviour but would probably confuse many C libraries).


More information about the Haskell-Cafe mailing list