Application example for state and other MTL monks?

Looking at the documents for Control.Applicative , I notice that they have instance declarations for certain monads (like IO , Maybe and especially ST ), but there are no instances for MTAD monads like State and RWS . Instead, there seems to be a certain type of WrappedMonad that I assume covers all other cases.

So here are my questions:

  • Why are there no Applicative instances for monads MTL? The best answer that I could find on my own was a three-year article where someone implemented these instances and were briefly ignored.

  • What is the deal with WrappedMonad ? I found a post on Reddit that explains this briefly, but I think I'm confused about how to use it.

Ultimately, I would like to use State in an applicative style (as I recommended ), but if I have to mutate my code with WrappedMonad data WrappedMonad , then this doesn't seem to be a win. I could also completely ignore WrappedMonad and define the Applicative instance in the same way as I did for IO , ST , etc .: in terms of return and ap ... But this seems to be silly.

+6
source share
2 answers

This documentation is only created based on the contents of the base package, which does not contain State - therefore, it does not contain Applicative instances for it. If you look at the MTL documentation , you will see the Applicative instance listed.

Alternatively, you can open GHCi, import Control.Monad.State and Control.Applicative and use :i Applicative for the list of Applicative instances or :i StateT for the list of classes for which StateT has instances ( State is an alias of type StateT with fixed one from its parameters). Or it will show that there really is such an instance.

+8
source

Applicative instances for monad mtl cannot be specified in the base documents because there is no mtl at this point. The good news is that mtl has applicative StateT instances , for example , since mtl-2.0.0.0.

+2
source

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


All Articles