Why MonadReader r (StateT sm) uses an instance of the main monad

As far as I can see, we could implement an instance MonadReader s (StateT s m):

instance MonadReader s (StateT s m) where
    ask = get
    local f m = do
        s <- get
        put (f s)
        m
        put s   

i.e. why is it not

class MonadReader s m => MonadState s m | s -> m where ...

Similarly, we could have an instance Monoid s => MonadWriter s (StateT s m).

Is there any deep reason between the choices?


This question is motivated by the fact there should be MonadErrorand MonadWriter superclassesMonadChronicle

+4
source share
2 answers

, , mtl API. mtl , . StateT ReaderT . StateT MonadReader, . , "". MonadChronicle , , . / , .

+7

, , / MonadReader StateT, , .

MonadReader MonadState . ask get . ask get , . StateT / MonadReader MonadState.

, mappend <|> Maybe . :)

, StateT ask get, , / .

0

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


All Articles