|  | 
| 1 |  | -{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies ,FlexibleInstances #-} | 
|  | 1 | +{-# LANGUAGE FlexibleInstances #-} | 
|  | 2 | +{-# LANGUAGE FunctionalDependencies #-} | 
|  | 3 | +{-# LANGUAGE MultiParamTypeClasses #-} | 
|  | 4 | + | 
| 2 | 5 | module EitherT where | 
| 3 | 6 | 
 | 
| 4 |  | -import Control.Monad | 
|  | 7 | +import Control.Monad | 
| 5 | 8 | 
 | 
| 6 |  | -newtype MyEitherT l m r = MyEitherT { runMyEitherT :: m (Either l r)} | 
|  | 9 | +newtype MyEitherT l m r = MyEitherT | 
|  | 10 | + { runMyEitherT :: m (Either l r) | 
|  | 11 | + } | 
| 7 | 12 | 
 | 
| 8 | 13 | class MonadTrans t where | 
| 9 |  | - lift :: Monad m => m a -> t m a | 
|  | 14 | + lift :: Monad m => m a -> t m a | 
| 10 | 15 | 
 | 
| 11 |  | -class (Show e, Monad m) => (MonadError e m) where | 
| 12 |  | - eFail :: e -> m a | 
| 13 |  | - eHandle :: m a -> (e -> m a) -> m a | 
|  | 16 | +class (Show e, Monad m) => | 
|  | 17 | + (MonadError e m) where | 
|  | 18 | + eFail :: e -> m a | 
|  | 19 | + eHandle :: m a -> (e -> m a) -> m a | 
| 14 | 20 | 
 | 
| 15 | 21 | instance (Monad m) => Functor (MyEitherT l m) where | 
| 16 |  | - fmap = liftM | 
|  | 22 | + fmap = liftM | 
| 17 | 23 | 
 | 
| 18 | 24 | instance (Monad m) => Applicative (MyEitherT l m) where | 
| 19 |  | - pure = MyEitherT . pure . Right | 
| 20 |  | - (<*>) = ap | 
|  | 25 | + pure = MyEitherT . pure . Right | 
|  | 26 | + (<*>) = ap | 
| 21 | 27 | 
 | 
| 22 | 28 | instance (Monad m) => Monad (MyEitherT l m) where | 
| 23 |  | - return = pure | 
| 24 |  | - a >>= b = MyEitherT $ do | 
| 25 |  | - a' <- runMyEitherT a | 
| 26 |  | - case a' of | 
| 27 |  | - Left err -> return $ Left err | 
| 28 |  | - Right res -> runMyEitherT $ b res | 
|  | 29 | + return = pure | 
|  | 30 | + (MyEitherT a) >>= b = | 
|  | 31 | + MyEitherT $ do | 
|  | 32 | + a' <- a | 
|  | 33 | + case a' of | 
|  | 34 | + Left err -> pure . Left $ err | 
|  | 35 | + Right res -> runMyEitherT $ b res | 
| 29 | 36 | 
 | 
| 30 | 37 | instance MonadTrans (MyEitherT l) where | 
| 31 |  | - lift = MyEitherT . liftM Right | 
|  | 38 | + lift = MyEitherT . fmap Right | 
| 32 | 39 | 
 | 
| 33 | 40 | instance (Show l, Monad m) => MonadError l (MyEitherT l m) where | 
| 34 |  | - eFail l = MyEitherT $ return $ Left l | 
| 35 |  | - eHandle m1 m2 = MyEitherT $ do | 
| 36 |  | - m1' <- runMyEitherT m1 | 
| 37 |  | - case m1' of | 
| 38 |  | - Right res -> return m1' | 
| 39 |  | - Left err -> runMyEitherT $ m2 err | 
|  | 41 | + eFail = MyEitherT . return . Left | 
|  | 42 | + eHandle m1 m2 = | 
|  | 43 | + MyEitherT $ do | 
|  | 44 | + m1' <- runMyEitherT m1 | 
|  | 45 | + case m1' of | 
|  | 46 | + Right res -> return m1' | 
|  | 47 | + Left err -> runMyEitherT $ m2 err | 
0 commit comments