monad library

Ross Paterson ross@soi.city.ac.uk
Tue, 29 Jul 2003 23:51:48 +0100


On Mon, Jul 28, 2003 at 10:51:47PM +0200, Iavor Diatchki wrote:
> >Another issue: typically one works with a stack of transformers sitting
> >on a base monad.  You have functions for adding or removing a transformer
> >from the top of the stack, and for manipulating the base monad.  It might
> >also be useful to have something to remove a State transformer from
> >anywhere in the stack, etc.
> i've tried to do things like that, but i can't figure out a way to 
> achieve them. what would be the type of such a function?

class (MonadState s m, Monad m') => MonadWithState s m m' | m -> m' where
	liftState :: m' a -> m a
	runState :: s -> m a -> m' (a,s)

instance Monad m => MonadWithState s (StateT s m) m
instance MonadWithState s m m' => MonadWithState s (ErrorT e m) (ErrorT e m')
...

and similarly for the rest.  (Instances that use the same type constructor
twice are an embarrassment for Haddock, though.)