public
Created

Mapping over errors

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{-# 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.