Skip to content

Commit 8f7004c

Browse files
committed
Add laws to MonadReader
1 parent f801014 commit 8f7004c

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Control/Monad/Reader/Class.hs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,38 @@ import Data.Monoid
6969
-- class MonadReader
7070
-- asks for the internal (non-mutable) state.
7171

72-
-- | See examples in "Control.Monad.Reader".
72+
-- | Monads with a notion of readable environment.
73+
--
74+
-- See examples in "Control.Monad.Reader".
7375
-- Note, the partially applied function type @(->) r@ is a simple reader monad.
7476
-- See the @instance@ declaration below.
77+
--
78+
-- === Laws
79+
--
80+
-- @
81+
-- m '<*>' 'ask' = 'ask' 'Control.Applicative.<**>' m
82+
--
83+
-- 'ask' '>>' m = m
84+
-- 'ask' '>>=' \\s1 -> 'ask' '>>=' \\s2 -> k s1 s2 = 'ask' '>>=' \\s -> k s s
85+
--
86+
-- 'local' f 'ask' = f '<$>' 'ask'
87+
-- 'local' g '.' 'local' f = 'local' (g '.' f)
88+
--
89+
-- -- 'local' is a monad morphism from 'm' to 'm'
90+
-- 'local' f ('pure' x) = 'pure' x
91+
-- 'local' f (a '>>=' k) = 'local' f a '>>=' \\x -> 'local' f (k x)
92+
--
93+
-- 'ask' = 'reader' 'id'
94+
-- @
95+
--
96+
-- An equivalent requirement to the first two laws is that 'reader' (as given
97+
-- by the default implementation) must be a monad morphism from @'Reader' r@
98+
-- to 'm'.
7599
class Monad m => MonadReader r m | m -> r where
76100
#if __GLASGOW_HASKELL__ >= 707
77101
{-# MINIMAL (ask | reader), local #-}
78102
#endif
79-
-- | Retrieves the monad environment.
103+
-- | Retrieves the environment.
80104
ask :: m r
81105
ask = reader id
82106

0 commit comments

Comments
 (0)