on master help sign in
Merge pull request #1485 from gavlegoat/master

Added documentation to files in libs/base/Control/Monad

commitf2f050aeb15bfb419b2f7f8841fcccad02224cb9 authorEdwin Brady <edwin.brady@gmail.com> dateFri 22 Aug 2014, 21:34 (12 years ago) parent727b6a9bdf04ad
4 files changed+25
libs/base/Control/Monad/RWS.idr+3 libs/base/Control/Monad/Reader.idr+7 libs/base/Control/Monad/State.idr+7 libs/base/Control/Monad/Writer.idr+8
libs/base/Control/Monad/RWS.idr+3
@@ -7,8 +7,10 @@ import Control.Monad.Reader
77
88%access public
99
10+||| A combination of the Reader, Writer, and State monads
1011class (Monoid w, MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s (m : Type -> Type) where {}
1112
13+||| The transformer on which the RWS monad is based
1214record RWST : Type -> Type -> Type -> (Type -> Type) -> Type -> Type where
1315 MkRWST : {m : Type -> Type} ->
1416 (runRWST : r -> s -> m (a, s, w)) -> RWST r w s m a
@@ -46,5 +48,6 @@ instance (Monoid w, Monad m) => MonadState s (RWST r w s m) where
4648
4749instance (Monoid w, Monad m) => MonadRWS r w s (RWST r w s m) where {}
4850
51+||| The RWS monad. See the MonadRWS class
4952RWS : Type -> Type -> Type -> Type -> Type
5053RWS r w s a = RWST r w s Identity a
libs/base/Control/Monad/Reader.idr+7
@@ -5,14 +5,19 @@ import Control.Monad.Identity
55
66%access public
77
8+||| A monad representing a computation that runs in an immutable context
89class Monad m => MonadReader r (m : Type -> Type) where
10+ ||| Return the context
911 ask : m r
12+ ||| Temprorarily modify the input and run an action in the new context
1013 local : (r -> r) -> m a -> m a
1114
15+||| The transformer on which the Reader monad is based
1216record ReaderT : Type -> (Type -> Type) -> Type -> Type where
1317 RD : {m : Type -> Type} ->
1418 (runReaderT : r -> m a) -> ReaderT r m a
1519
20+||| Create a ReaderT with an empty context
1621liftReaderT : {m : Type -> Type} -> m a -> ReaderT r m a
1722liftReaderT m = RD $ const m
1823
@@ -36,9 +41,11 @@ instance Monad m => MonadReader r (ReaderT r m) where
3641 ask = RD return
3742 local f (RD m) = RD $ m . f
3843
44+||| Evaluate a function in the context of a Reader monad
3945asks : MonadReader r m => (r -> a) -> m a
4046asks f = do r <- ask
4147 return (f r)
4248
49+||| The reader monad. See MonadReader
4350Reader : Type -> Type -> Type
4451Reader r a = ReaderT r Identity a
libs/base/Control/Monad/State.idr+7
@@ -4,10 +4,14 @@ import Control.Monad.Identity
44
55%access public
66
7+||| A computation which runs in a context and produces an output
78class Monad m => MonadState s (m : Type -> Type) where
9+ ||| Get the context
810 get : m s
11+ ||| Write a new context/output
912 put : s -> m ()
1013
14+||| The transformer on which the State monad is based
1115record StateT : Type -> (Type -> Type) -> Type -> Type where
1216 ST : {m : Type -> Type} ->
1317 (runStateT : s -> m (a, s)) -> StateT s m a
@@ -33,13 +37,16 @@ instance Monad m => MonadState s (StateT s m) where
3337 get = ST (\x => return (x, x))
3438 put x = ST (\y => return ((), x))
3539
40+||| Apply a function to modify the context of this computation
3641modify : MonadState s m => (s -> s) -> m ()
3742modify f = do s <- get
3843 put (f s)
3944
45+||| Evaluate a function in the context held by this computation
4046gets : MonadState s m => (s -> a) -> m a
4147gets f = do s <- get
4248 return (f s)
4349
50+||| The State monad. See the MonadState class
4451State : Type -> Type -> Type
4552State s a = StateT s Identity a
libs/base/Control/Monad/Writer.idr+8
@@ -5,11 +5,16 @@ import Control.Monad.Identity
55
66%access public
77
8+||| A monad representing a computation that produces a stream of output
89class (Monoid w, Monad m) => MonadWriter w (m : Type -> Type) where
10+ ||| tell w produces the output w
911 tell : w -> m ()
12+ ||| Execute an action and add it's output to the value of the computation
1013 listen : m a -> m (a, w)
14+ ||| Execute an action and apply the returned function to the output
1115 pass : m (a, w -> w) -> m a
1216
17+||| The transformer base of the Writer monad
1318record WriterT : Type -> (Type -> Type) -> Type -> Type where
1419 WR : {m : Type -> Type} ->
1520 (runWriterT : m (a, w)) -> WriterT w m a
@@ -39,14 +44,17 @@ instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
3944 pass (WR m) = WR $ do ((a, f), w) <- m
4045 return (a, f w)
4146
47+||| Run an action in the Writer monad and transform the written output
4248listens : MonadWriter w m => (w -> b) -> m a -> m (a, b)
4349listens f m = listens' f (listen m) where
4450 listens' f' ma = do (a, w) <- ma
4551 return (a, f' w)
4652
53+||| Run an action, apply a function to it's output, and return it's value
4754censor : MonadWriter w m => (w -> w) -> m a -> m a
4855censor f m = pass $ do a <- m
4956 return (a, f)
5057
58+||| The Writer monad itself. See the MonadWriter class
5159Writer : Type -> Type -> Type
5260Writer w a = WriterT w Identity a