Skip to content

Instantly share code, notes, and snippets.

@hesselink
Created September 23, 2012 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hesselink/3769836 to your computer and use it in GitHub Desktop.
Save hesselink/3769836 to your computer and use it in GitHub Desktop.
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