Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generalized Newtype Deriving

Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived?

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Control.Monad.Reader
import Control.Monad.State

newtype T1 r s a = 
  T1 { runT1 :: ReaderT r (State s) a }
  deriving (Monad, MonadReader r, MonadState s)

newtype T2 r s a = 
  T2 { runT2 :: StateT r (State s) a }
  deriving (Monad, MonadState r, MonadState s)
like image 459
Bob Avatar asked Sep 05 '14 20:09

Bob


1 Answers

You can't have a type have two instances for MonadState. This is because MonadState is defined as

class Monad m => MonadState s m | m -> s where
    get :: m s
    set :: s -> m ()
    state :: (s -> (a, s)) -> m a

The key part is the | m -> s. This requires the extension FunctionalDependencies, and states that for any m, we automatically know the associated s. This means that for any given m, there can only be one choice for s that is valid. So you can't have it work for both MonadState r m and MonadState s m unless r ~ s. If r ~ s, then how would the compiler know which underlying monad for it to apply to? In this case, I think you'll also find that it'll be much easier to understand and work with the code if you create get and put functions that have suffixes to indicate which, like getInner, setInner and getOuter, setOuter.

like image 72
bheklilr Avatar answered Oct 15 '22 23:10

bheklilr