[Haskell-cafe] Generalizing IO

Gregory Crosswhite gcross at phys.washington.edu
Tue Oct 6 12:23:29 EDT 2009


Putting a constraint on the MonadIO class actually makes your code  
less general rather than more since it prevents your code from ever  
being used in a purely functional setting.  If you leave out this  
constraint, you can actually turn your code into a pure function by  
doing things like feeding it a wrapper monad that supplies a list of  
strings as input and returns the list of written strings at the end.

The only reason why you would add a MonadIO constraint would be if you  
wanted to do other things that require the IO monad (e.g., connecting  
to a database, working with files, etc.) and so you wanted to require  
that the IO monad be ultimately present somewhere in the type so that  
you could use it.

Cheers,
Greg


On Oct 6, 2009, at 8:22 AM, Floptical Logic wrote:

>> class StreamMonad m where
>>        fetchLine = m
>>        sendLine = String -> m ()
>>
>> instance StreamMonad IO where
>>        fetchLine = getLine
>>        sendLine = putLine
>>
>> fetchLineFromStream = lift fetchLine
>> sendLineToStream = lift . sendLine
>
> This approach makes more sense to me.  The equivalent to printing
> something to the screen in the Net (IRC) monad is privmsg and it is
> easy to make that connection using type classes.  I don't see how to
> make this connection using MonadIO.



More information about the Haskell-Cafe mailing list