haskell - MonadTransControl instance for a custom monad -


the docs monad-control provide example on how create instance of monadtranscontrol using defaultliftwith , defaultrestoret. example following newtype:

newtype countert m = countert {uncountert :: statet int m a} 

this example can adjusted work newtype defined using 1 "elementary" monad transformer (such ones transformers or mtl). case stack contains 2 "elementary" transformers? example, how can define monadtranscontrol instance this:

newtype t m = t {unt :: maybet (statet int m) a} 

my problem don't know how adjust following line

newtype stt countert = stcounter {unstcounter :: stt (statet int) a} 

from countert make work t transformer. in particular, don't know put in last parenthesis. expects has kind (* -> *) -> * -> *, cannot form that.

any ideas?

i haven't been able reuse defaultliftwith , defaultrestoret, looking @ source code , tweaking slightly, arrived @ following:

newtype countert m = countert {uncountert :: maybet (statet int m) a} deriving (monad)  instance monadtrans countert     lift = countert .  lift . lift  instance monadtranscontrol countert      newtype stt countert = stcounter {unstcounter :: stt (statet int) (stt maybet a)}      liftwith = \f ->          countert $ liftwith $ \run ->                     liftwith $ \run' ->                     f $ liftm stcounter . run' . run . uncountert                  restoret = countert . restoret . restoret . liftm unstcounter 

Comments