Created
September 23, 2012 11:34
-
-
Save hesselink/3769836 to your computer and use it in GitHub Desktop.
Mapping over errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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