Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7bd5f66

Browse files
committed
added: MonadError
1 parent a4df047 commit 7bd5f66

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

‎EitherT.hs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies ,FlexibleInstances #-}
12
module EitherT where
23

34
import Control.Monad
4-
import Control.Monad.Trans
55

66
newtype MyEitherT l m r = MyEitherT { runMyEitherT :: m (Either l r)}
77

8+
class MonadTrans t where
9+
lift :: Monad m => m a -> t m a
10+
11+
class (Show e, Monad m) => (MonadError e m) where
12+
eFail :: e -> m a
13+
eHandle :: m a -> (e -> m a) -> m a
14+
815
instance (Monad m) => Functor (MyEitherT l m) where
916
fmap = liftM
1017

1118
instance (Monad m) => Applicative (MyEitherT l m) where
12-
pure = MyEitherT . pure . Right
19+
pure = MyEitherT . pure . Right
20+
(<*>) = ap
1321

1422
instance (Monad m) => Monad (MyEitherT l m) where
1523
return = pure
@@ -23,3 +31,11 @@ instance MonadTrans (MyEitherT l) where
2331
lift m = MyEitherT $ do
2432
m' <- m
2533
return $ Right m'
34+
35+
instance (Show l, Monad m) => MonadError l (MyEitherT l m) where
36+
eFail l = MyEitherT $ return $ Left l
37+
eHandle m1 m2 = MyEitherT $ do
38+
m1' <- runMyEitherT m1
39+
case m1' of
40+
Right res -> return m1'
41+
Left err -> runMyEitherT $ m2 err

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /