Haskell/Solutions/Monad transformers

Lifting
1.

can be defined in terms of the  methods,   and. These are not enough for defining, as how a value in the inner monad should be with the base monad and the transformer depends on what the transformer is like. In particular, the differences between the types wrapped by the transformers, as illustrated in the A plethora of transformers section, make a generic implementation impossible.

2.

Implementing transformers
1.

2.

They are not equivalent. Specialising the type of  to work with , we get:

Doing the same for  and , we obtain:

In the first case, we get a  computation that returns a. In the second, we get a function which, from an initial state of type, might give back a result and a new state or not, as the result type is. A  amidst a   destroys merely the returned result, while in a   it destroys the final state as well. This comparison can illustrate some general remarks:


 * In general, the order in which monads are stacked matters.
 * When a composed monad is unwrapped, the effect of the inner monad has priority over the one of the base monad.
 * Failure-handling layers such as  and   usually go on the top of transformer stacks, so that the effects of the other involved monads are preserved in case of failure.