[Haskell] Lift Info
dmhouse at gmail.com
Tue Mar 27 13:33:11 EDT 2007
On 27/03/07, Dave at haskell.org <Dave at haskell.org> wrote:
> I've been looking for an explanation of the Haskell lift function which
> I don't yet get. There seem to be two kinds of lifting, one of which is
> making normal functions usable inside monads. I don't understand either
> type. I've googled for lift but what I've found has not helped me much.
> Is there a tutorial on what problem using lift solves?
There are indeed two meanings of the word 'lift', which are actually
related, but the relationship is difficult to comprehend if you don't
understand the surrounding concepts.
1) liftM :: Monad m => (a -> b) -> (m a -> m b).
This function 'lifts' a pure function into a monadic one, which is
useful when you want to apply a pure function to a monadic value. I.e.
if you have an (a -> b) and an m a, then you can use liftM (or fmap,
which is equivalent, doesn't require you to import Control.Monad, and
is one character shorter) to change that into an m b.
2) lift :: (MonadTrans t, Monad m) => m a -> t m a
Used when working with monad transformers to include a monadic
computation in the inner monad within a monadic computation in the
composite monad. E.g. if you're working with the monad ReaderT e IO,
for some environment type e, and wanted to do some IO within a ReaderT
e IO do-block, you'd use lift (or liftIO, which is like lift, but
specialised for when the inner monad is IO) to include the IO
computation within the ReaderT e IO computation.
If you're interested in reading further about case (2), check out
section III of All About Monads .
-David House, dmhouse at gmail.com
More information about the Haskell