3For now, consider the following monad transformers together with base Identity
7class MonadTrans t where
10newtype ReaderT e m a = ReaderT { runReaderT :: e -> m a }
12instance Functor f => Functor (ReaderT e f)
14instance Applicative f => Applicative (ReaderT e f)
16instance Monad m => Monad (ReaderT e m)
18instance MonadTrans (ReaderT e)
20newtype WriterT w m a = WriterT { runWriterT :: m (w, a) }
22instance Functor f => Functor (WriterT w f)
24instance (Monoid w, Applicative f) => Applicative (WriterT w f)
26instance (Monoid w, Monad m) => Monad (WriterT w m)
28instance Monoid w => MonadTrans (WriterT w)
30newtype Identity a = Identity { runIdentity :: a }
32instance Functor Identity
34instance Applicative Identity
36instance Monad Identity
38type MyMonad = ReaderT Int (WriterT String Identity)
41Using transformers, we can compose several effects together, but if you try to
42write a simple program with them, it will look like the following:
45myProgram :: MyMonad Int
47 env <- ReaderT (\env -> return env)
48 lift (WriterT (return (show env, ())))
52See how littered this is with different kinds of piping: `return`s, `lift`s,
53monad constructors etc. Using some auxiliary functions, program becomes a lot
57ask0 :: Monad m => ReaderT e m e
60write0 :: (Monoid w, Monad m) => w -> WriterT w m ()
61write0 w = WriterT (return (w, ()))
63myProgram' :: MyMonad Int
66 lift $ write0 (show env)
71newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) }
73instance Functor f => Functor (MaybeT f)
75instance Applicative f => Applicative (MaybeT f)
77instance Monad f => Monad (MaybeT f)
79instance MonadTrans MaybeT
81type ExtMonad a = MaybeT MyMonad a
83exit0 :: Monad m => MaybeT m a
84exit0 = MaybeT (return Nothing)
86divCtx :: Int -> ExtMonad Int
89 if y == 0 then exit0 else return (x `div` y)
91divRed :: Int -> MaybeT (ReaderT Int Identity) Int
94 if y == 0 then exit0 else return (x `div` y)
101class Monad m => MonadWriterProto w m where
102 writeProto :: (a, w) -> m a
103 -- effectively same as w -> m ()
104 unfoldWriteProto :: m a -> m (a, w) -- ??
105 -- effectively same as m a -> m w
107class Monad m => MonadReaderProto e m where
108 readProto :: (e -> a) -> m a
109 readProto f = fmap f askProto
112 unfold :: m a -> (e -> m a)
114 localProto :: (e -> e) -> m a -> m a
121class Monad m => MonadWriter w m where
124instance (Monad m, Monoid w) => MonadWriter w (WriterT w m) where
127instance MonadWriter w m => MonadWriter w (ReaderT e m) where
130instance MonadWriter w m => MonadWriter w (MaybeT m) where
133class Monad m => MonadReader e m where
135 local :: (e -> e) -> m a -> m a
137instance MonadReader e ((->) e) where
139 local f g e = g (f e)
141instance Monad m => MonadReader e (ReaderT e m) where
145class Monad m => MonadMaybe m where
148instance MonadMaybe Maybe where
151instance Monad m => MonadMaybe (MaybeT m) where
156myProgram'' :: (MonadReader Int m, MonadWriter String m) => m Int
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list