Haskell có thể lấy được cá thể cho MonadState s
trong T1
dưới đây nhưng không phải trong T2
mà là một loại rất giống nhau. Tôi nên sửa đổi mã nào cho T2
để phiên bản cho MonadState s
có thể được tự động bắt nguồn?Generalized Newtype Deriving
{-# 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)