<div dir="ltr"><div>As it stands, working with newtypes around transformers can be kind of a pain. For example,</div><div>consider<br></div><div><br></div><div>newtype FreshT m a = { unFreshT :: StateT Int m a }</div><div>  deriving (Functor, Applicative, Monad, MonadReader r, ...)</div><div><br></div><div>Right now, we can't just GND the instance:</div><div><br></div><div>instance (MonadState s m) => MonadState s (FreshT m)</div><div><br></div><div>However, the instance we end up writing is completely formulaic. Fortunately, we can solve this by using the recently added -XDerivingVia</div><div><br></div><div>newtype Inner (t :: (* -> *) -> * -> *) m a = Inner { getInner :: t m a }<br>  deriving (Functor, Applicative, Monad)</div><div><br></div><div>instance (MonadState s m) => MonadState s (Inner (StateT s') m) where<br>  get = Inner $ lift get<br>  put = Inner . lift . put<br></div><div><br></div><div>This lets us derive the instance that we were looking for</div><div><br></div><div>newtype FreshT m a = FreshT { unFreshT :: StateT Int m a }<br>  deriving newtype (Functor, Applicative, Monad, MonadReader r)<br>  deriving (MonadState s) via Inner (StateT Int) m</div><div><br></div><div>This can be extended to other transformers/classes very easily. It also works well when dealing with newtyped transformer stacks.</div><div><br></div><div>newtype FooT s e m a = FooT { unFooT :: StateT s (WriterT String (ExceptT e m)) a }<br>  deriving newtype (Functor, Applicative, Monad, MonadError e)<br>  deriving (MonadWriter w) via (StateT s (Inner (WriterT String) (ExceptT e m)))<br>  deriving (MonadState s') via Inner (StateT s) (WriterT String (ExceptT e m))<br></div><div><br></div><div>Cheers,</div><div>Reed Mullanix<br></div></div>