Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: lift vs liftIO

In what situations should liftIO be used? When I'm using ErrorT String IO, the lift function works to lift IO actions into ErrorT, so liftIO seems superfluous.

like image 975
Lachlan Avatar asked Oct 13 '10 06:10

Lachlan


People also ask

What does liftIO do?

liftIO allows us to lift an IO action into a transformer stack that is built on top of IO and it works no matter how deeply nested the stack is.

What is lifting in Haskell?

From HaskellWiki. Lifting is a concept which allows you to transform a function into a corresponding function within another (usually more general) setting.


1 Answers

lift always lifts from the "previous" layer. If you need to lift from the second layer, you would need lift . lift and so on.

On the other hand, liftIO always lifts from the IO layer (which, when present, is always on the bottom of the stack). So, if you have more than 2 layers of monads, you will appreciate liftIO.

Compare the type of the argument in the following lambdas:

type T = ReaderT Int (WriterT String IO) Bool  > :t \x -> (lift x :: T) \x -> (lift x :: T) :: WriterT String IO Bool -> T  > :t \x -> (liftIO x :: T) \x -> (liftIO x :: T) :: IO Bool -> T 
like image 175
Roman Cheplyaka Avatar answered Nov 10 '22 06:11

Roman Cheplyaka