[Haskell-cafe] Selecting a transformer in a monad transformer stack
Paul Brauner
polux2001 at gmail.com
Tue May 29 16:06:16 UTC 2018
Hello,
I have a newtype for a monad transformer stack:
newtype MyMonad a = MyMonad { runMyMonad :: ((ReaderT Env (ExceptT String
(StateT Store (ListT Identity)))) a) }
and I'd like to it derive MonadPlus. The default instance for MonadPlus I
get via GeneralizedNewtypeDeriving is the ExceptT one because it is the
outermost transformer implementing MonadPlus. I would like to use the ListT
instance instead. The best solution I have come up with is to define:
instance MonadPlus MyMonad where
mzero = MyMonad $ ReaderT $ \e -> ExceptT $ StateT $ \s -> ListT $
Identity []
mplus a b =
MyMonad $ ReaderT $ \e -> ExceptT $ StateT $ \s -> ListT $ Identity $
let
run x = runIdentity (runListT (runStateT (runExceptT (runReaderT
(runAbstract x) e)) s))
as = run a
bs = run b
in
as ++ bs
My questions are:
- Is there a shorter way to define mplus?
- Is there maybe some variation of monad transformer stacks where the
layers are named and can be accessed by name? (I know of effects handlers
but I'd like to stick to monad transformers for now.)
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180529/fc293ad3/attachment.html>
More information about the Haskell-Cafe
mailing list