Ich versuche MaybeT
im Geiste der mtl
Bibliothek zu implementieren. Mit dieser nicht-Kompilierung Lösung:Kann GHC Functor- und Applicative-Instanzen für einen Monad-Transformator ableiten?
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}
import Control.Monad
import Control.Monad.Trans
import Control.Monad.State
newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) }
instance (Monad m) => Monad (MaybeT m) where
x >>= f = MaybeT $ runMaybeT x >>= maybe (return Nothing) (runMaybeT . f)
return a = MaybeT $ return (Just a)
fail _ = MaybeT $ return Nothing
instance MonadTrans MaybeT where
lift m = MaybeT (liftM Just m)
instance (MonadIO m) => MonadIO (MaybeT m) where
liftIO m = lift (liftIO m)
instance (MonadState s m) => MonadState s (MaybeT m) where
get = lift get
put = lift . put
...
bekomme ich den Fehler:
Could not deduce (Applicative (MaybeT m)) arising from the superclasses of an instance declaration from the context (Monad m)
Wenn ich folgendes implementieren, es kompiliert:
instance (Monad m) => Applicative (MaybeT m) where
pure = return
(<*>) = ap
instance (Monad m) => Functor (MaybeT m) where
fmap = liftM
Kann GHC tun dies für mich?
In Verbindung stehend: http://stackoverflow.com/questions/18861231/why-is-there-no-xderiveapplicative-extension – dfeuer