[Haskell-cafe] Selecting a transformer in a monad transformer stack
Paul Brauner
polux2001 at gmail.com
Tue May 29 16:07:57 UTC 2018
There is a typo in the definition of mplus in my previous message:
"runAbstract" should be "runMyMonad".
On Tue, May 29, 2018 at 6:06 PM Paul Brauner <polux2001 at gmail.com> wrote:
> 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/91837acd/attachment.html>
More information about the Haskell-Cafe
mailing list