
Is there a reason why the lift function in ReaderT's MonadTrans instance is implemented as: instance MonadTrans (ReaderT r) where lift m = ReaderT $ \_ -> m Instead of just using the monad's return function? Could "lift m" be implemented as "return m"? instance (Monad m) => Monad (ReaderT r m) where * return a = ReaderT $ \_ -> return a* m >>= k = ReaderT $ \r -> do a <- runReaderT m r runReaderT (k a) r fail msg = ReaderT $ \_ -> fail msg Thanks, Daryoush

On Mon, 2009-01-19 at 13:03 -0800, Daryoush Mehrtash wrote:
Is there a reason why the lift function in ReaderT's MonadTrans instance is implemented as:
instance MonadTrans (ReaderT r) where
lift m = ReaderT $ \_ -> m
Instead of just using the monad's return function? Could "lift m" be implemented as "return m"?
No: Prelude> :m + Control.Monad.Reader Prelude Control.Monad.Reader> :t \ m -> ReaderT $ \ _ -> m \ m -> ReaderT $ \ _ -> m :: m a -> ReaderT r m a Prelude Control.Monad.Reader> :t \ m -> ReaderT $ \ _ -> return m \ m -> ReaderT $ \ _ -> return m :: (Monad m) => a -> ReaderT r m a Your first clue that something's wrong should be that the types don't work out. (On the other hand, your hunch that lift = return is correct --- so you get a cookie for that; it's just that return here is neither the return of the monad for m nor the return of the monad for ReaderT m. It is, instead, the return of the *applicative functor* --- on the category of monads and monad homomorphisms --- associated to the monad transformer ReaderT.) (By the way, you *do* have the equations lift (return x) = return x and lift (a >>= f) = lift a >>= lift . f ) jcc

On Mon, Jan 19, 2009 at 01:13:37PM -0800, Jonathan Cast wrote:
(On the other hand, your hunch that lift = return is correct --- so you get a cookie for that; it's just that return here is neither the return of the monad for m nor the return of the monad for ReaderT m. It is, instead, the return of the *applicative functor* --- on the category of monads and monad homomorphisms --- associated to the monad transformer ReaderT.)
It's also a monad in the category of monads, as are ErrorT, StateT and WriteT (see Moggi, An Abstract View of Programming Languages, 1989, s4).

On Mon, 2009-01-19 at 21:31 +0000, Ross Paterson wrote:
On Mon, Jan 19, 2009 at 01:13:37PM -0800, Jonathan Cast wrote:
(On the other hand, your hunch that lift = return is correct --- so you get a cookie for that; it's just that return here is neither the return of the monad for m nor the return of the monad for ReaderT m. It is, instead, the return of the *applicative functor* --- on the category of monads and monad homomorphisms --- associated to the monad transformer ReaderT.)
It's also a monad in the category of monads, as are ErrorT, StateT and WriteT (see Moggi, An Abstract View of Programming Languages, 1989, s4).
Nice! I really haven't studied monad transformers nearly as much as I should have. (I also really need to read more Moggi :) jcc

Ross Paterson wrote:
On Mon, Jan 19, 2009 at 01:13:37PM -0800, Jonathan Cast wrote:
(On the other hand, your hunch that lift = return is correct --- so you get a cookie for that; it's just that return here is neither the return of the monad for m nor the return of the monad for ReaderT m. It is, instead, the return of the *applicative functor* --- on the category of monads and monad homomorphisms --- associated to the monad transformer ReaderT.)
It's also a monad in the category of monads, as are ErrorT, StateT and WriteT (see Moggi, An Abstract View of Programming Languages, 1989, s4). _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Monads in the (2-)category of monads correspond to distributive laws (see Formal theory of Monads by Street, 1972). StateT does not arise from a distributive law. In particular, you would need a map tjoin :: Monad m => StateT (StateT m) a -> StateT m a :: Monad m = (s -> s -> m ((a,s),s)) -> s -> m (a,s) subject to the monad laws (where lift :: Monad m => m a -> StateT m a is the unit, i.e. return) All the best - Mauro This message has been checked for viruses but the contents of an attachment may still contain software viruses, which could damage your computer system: you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
participants (6)
-
ajb@spamcop.net
-
Brent Yorgey
-
Daryoush Mehrtash
-
Jonathan Cast
-
Mauro J. Jaskelioff
-
Ross Paterson