@@ -69,14 +69,59 @@ import Data.Monoid
69
69
-- class MonadReader
70
70
-- asks for the internal (non-mutable) state.
71
71
72
- -- | See examples in "Control.Monad.Reader".
72
+ -- | Monads with a notion of readable environment.
73
+ --
74
+ -- See examples in "Control.Monad.Reader".
73
75
-- Note, the partially applied function type @(->) r@ is a simple reader monad.
74
76
-- See the @instance@ declaration below.
77
+ --
78
+ -- === Laws
79
+ --
80
+ -- 'ask' has no side effects, and produces the same result at any time.
81
+ --
82
+ -- @
83
+ -- 'ask' '>>' m = m
84
+ -- 'ask' '>>=' \\s1 -> 'ask' '>>=' \\s2 -> k s1 s2 = 'ask' '>>=' \\s -> k s s
85
+ --
86
+ -- m '<*>' 'ask' = 'ask' 'Control.Applicative.<**>' m
87
+ -- @
88
+ --
89
+ -- @'local' f@ applies @f@ to the environment produced by 'ask'.
90
+ --
91
+ -- @
92
+ -- 'local' f 'ask' = f '<$>' 'ask'
93
+ -- 'local' f u = 'ask' '>>=' \\s -> 'local' (\\_ -> f s) u
94
+ -- @
95
+ --
96
+ -- 'local' is a monoid morphism from @(r -> r)@ to (reversed) @(m a -> m a)@
97
+ -- (i.e., @('Data.Monoid.Endo' r -> 'Data.Monoid.Dual' ('Data.Monoid.Endo' (m a)))@).
98
+ --
99
+ -- @
100
+ -- 'local' 'id' = 'id'
101
+ -- 'local' g '.' 'local' f = 'local' (f '.' g)
102
+ -- @
103
+ --
104
+ -- 'local' is a monad morphism from 'm' to 'm'.
105
+ --
106
+ -- @
107
+ -- 'local' f ('pure' x) = 'pure' x
108
+ -- 'local' f (a '>>=' k) = 'local' f a '>>=' \\x -> 'local' f (k x)
109
+ -- @
110
+ --
111
+ -- 'reader' must be equivalent to its default definition in terms of 'ask',
112
+ -- and conversely.
113
+ --
114
+ -- Under that last condition, a property which is equivalent to the first two
115
+ -- laws is that 'reader' must be a monad morphism from @'Reader' r@ to 'm'.
116
+ --
117
+ -- Another property equivalent to the first three laws is that there
118
+ -- is a monad morphism @phi :: forall a. 'ReaderT' r m a -> m a@ such that
119
+ -- @phi 'ask' = 'ask'@ and @phi . 'lift' = 'id'@.
75
120
class Monad m => MonadReader r m | m -> r where
76
121
#if __GLASGOW_HASKELL__ >= 707
77
122
{-# MINIMAL (ask | reader), local #-}
78
123
#endif
79
- -- | Retrieves the monad environment.
124
+ -- | Retrieves the environment.
80
125
ask :: m r
81
126
ask = reader id
82
127
0 commit comments