You cannot have type two instances for MonadState . This is because MonadState is defined as
class Monad m => MonadState sm | m -> s where get :: ms set :: s -> m () state :: (s -> (a, s)) -> ma
The key part is | m -> s | m -> s . This requires the extension FunctionalDependencies and it is indicated that for any m we automatically recognize the associated s . This means that for any given m , there can only be one choice for s . So you cannot use it for MonadState rm and MonadState sm , unless r ~ s . If r ~ s , then how does the compiler know which base monad it applies to? In this case, I think you will also find that it will be much easier to understand and work with the code if you create get and put functions that have suffixes to indicate which ones, for example getInner , setInner and getOuter , setOuter .
source share