Monad Transformers and Lift Function

Why is it not necessary to use an elevator to perform a function in the internal environment of a monad transformer other than IO? I mean, if I have StateT over WriterT and WriterT over ReaderT, why can I do this?

tell $ {- any code here for the Writer -} foo <- asks {- This for the reader -} and so on... 

instead

 lift $ tell $ {- code ... -} ... 

Is there a special explanation or is it just how Monad Transformers were written?

+6
source share
1 answer

This is because the Monad Transformer Library (MTL) recognizes that it is enough for you to mount monads in the usual way so that they do not define tell as some function (Mondoid w) => w -> Writer () .

Instead, they have a MonadWriter , which is defined as the type of the class, indicating how to function in it. Then they determine the ton of MonadWriter instances: ReaderT , IO , Writer (duh), etc. And that way you avoid the annoying repetition of lift. .

It is quite common that any monad transformer (in MTL) will have a Control.Monad.***.Class that has this typeclass type.

+7
source

Source: https://habr.com/ru/post/947702/


All Articles