[Haskell-cafe] Re: Generalizing IO

Heinrich Apfelmus apfelmus at quantentunnel.de
Wed Oct 7 07:24:44 EDT 2009

David Menendez wrote:
> Floptical Logic wrote:
>> The code below is a little interactive program that uses some state.
>> It uses StateT with IO to keep state.  My question is: what is the
>> best way to generalize this program to work with any IO-like
>> monad/medium?  For example, I would like the program to function as it
>> does now using stdin but I would also like it to function over IRC
>> using the Net monad from
>> <http://haskell.org/haskellwiki/Roll_your_own_IRC_bot>.  Thanks for
>> any suggestions.
> Instead of specifying the monad implementation, specify the interface.
> That is, you are using state operations (from MonadState) and IO
> operations (from MonadIO). Try removing all the type signatures that
> mention PDState and see what you get.
> E.g., loop :: (MonadState PD m, MonadIO m) => m a

Alternatively, you can use algebraic data types instead of type classes
to generalize one program to different implementations. For monads, this
can be achieved with


In particular, the idea is to turn every effect like


into a constructor


and have different implementations pattern match on that.



More information about the Haskell-Cafe mailing list