哈斯克尔:电梯vs电梯

问题描述:

在什么情况下应该使用 liftIO ?当我使用 ErrorT String IO 时, lift 函数可以将IO动作提升为 ErrorT ,所以 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.

lift 总是从上一个图层抬起。如果您需要从第二层提升,则需要 lift。电梯等等。

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

另一方面, liftIO 总是从IO层(当存在时,总是在堆栈的底部)。所以,如果你有两层以上的monads,你会明白 liftIO

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.

比较参数在以下lambda中:

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