Create a gist now

Instantly share code, notes, and snippets.

Mapping over errors
{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
import Control.Monad.Error
import Control.Monad.Reader
type family MappedMonad (m :: * -> *) e' :: * -> *
type instance MappedMonad (ErrorT e m) e' = ErrorT e' m
type instance MappedMonad (ReaderT r m) e' = ReaderT r (MappedMonad m e')
class MapError m where
type ErrorType m :: *
mapE :: (ErrorType m -> e') -> m a -> (MappedMonad m e') a
instance Functor m => MapError (ErrorT e m) where
type ErrorType (ErrorT e m) = e
mapE f = mapErrorT (fmap (either (Left . f) Right))
instance MapError m => MapError (ReaderT r m) where
type ErrorType (ReaderT r m) = ErrorType m
mapE f = mapReaderT (mapE f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment