# monad library

**Iavor Diatchki
**
diatchki@cse.ogi.edu

*Sun, 27 Jul 2003 21:28:57 +0200*

hello,
Andrew J Bromage wrote:
>>*1. there are the map* functions for different transformers (mapReader,
*>>*mapWriter, etc.). does anyone use them for anything? while there is
*>>*something common between them, i am not quite sure exactly what it is.
*>*
*>*
*>* I've used them once, and that was to swap the underlying monad of a
*>* transformer. It's occasionally handy, for example, when you want your
*>* state to be backtrackable for a small part of your code, to stick a
*>* backtracking monad _under_ the top monad transformer.
*>*
*>* I don't think that you can duplicate this behaviour using the other
*>* operations on the monad.
*>*
*>* A simple solution would be to introduce this typeclass:
*>*
*>* class (MonadTrans t) => MonadMapTrans t where
*>* mapTrans :: (m a -> n b) -> (t m a -> t n b)
*
i think this type doesn't quite work as often the underlying computation
returns a more complicated type (e.g. the state monad also returns a new
state). in my other library i had a similar operation, corresponding to
the fact that most monad transformers are functors on the category of
monads. it works for all transformers except for continuations:
class MondaTrans t => MapTrans t where
mapTrans :: (Monad m, Monad n) => (forall a. m a -> n a) -> t m b ->
t n b
intuitively the "forall" constraint says that we are changing just the
monad, and not touching the value. to get continuations to work, one
needs a map (n a -> m a) as well, i think. any comments about that class?
bye
iavor
--
==================================================
|* Iavor S. Diatchki, Ph.D. student |
*|* Department of Computer Science and Engineering |
*|* School of OGI at OHSU |
*|* http://www.cse.ogi.edu/~diatchki |
*==================================================