@@ -7,11 +7,20 @@ import Control.Comonad.Env.Trans (EnvT(..))
7
7
8
8
import Data.Tuple (Tuple (..), fst )
9
9
10
- -- | The `ComonadEnv` type class represents those monads which support a global environment via
11
- -- | `ask` and `local` .
10
+ -- | The `ComonadEnv` type class represents those comonads which support a
11
+ -- | global environment that can be provided via the `ask` function .
12
12
-- |
13
- -- | - `ask` reads the current environment from the context.
14
- -- | - `local` changes the value of the global environment.
13
+ -- | An implementation is provided for `EnvT`.
14
+ class Comonad w <= ComonadAsk e w | w -> e where
15
+ ask :: forall a . w a -> e
16
+
17
+ -- | Get a value which depends on the environment.
18
+ asks :: forall e1 e2 w . ComonadEnv e1 w => (e1 -> e2 ) -> w e1 -> e2
19
+ asks f x = f (ask x)
20
+
21
+ -- | The `ComonadEnv` type class extends `ComonadAsk` with a function
22
+ -- | `local f x` that allows the value of the local context to be modified for
23
+ -- | the duration of the execution of action `x`.
15
24
-- |
16
25
-- | An implementation is provided for `EnvT`.
17
26
-- |
@@ -20,19 +29,18 @@ import Data.Tuple (Tuple(..), fst)
20
29
-- | - `ask (local f x) = f (ask x)`
21
30
-- | - `extract (local _ x) = extract a`
22
31
-- | - `extend g (local f x) = extend (g <<< local f) x`
23
- class Comonad w <= ComonadEnv e w | w -> e where
24
- ask :: forall a . w a -> e
32
+ class ComonadAsk e w <= ComonadEnv e w | w -> e where
25
33
local :: forall a . (e -> e ) -> w a -> w a
26
34
27
- -- | Get a value which depends on the environment.
28
- asks :: forall e1 e2 w . ComonadEnv e1 w => (e1 -> e2 ) -> w e1 -> e2
29
- asks f x = f (ask x)
35
+ instance comonadAskTuple :: ComonadAsk e (Tuple e ) where
36
+ ask = fst
30
37
31
38
instance comonadEnvTuple :: ComonadEnv e (Tuple e ) where
32
- ask = fst
33
39
local f (Tuple x y) = Tuple (f x) y
34
40
35
- instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w ) where
41
+ instance comonadAskEnvT :: Comonad w => ComonadAsk e (EnvT e w ) where
36
42
ask (EnvT x) = fst x
43
+
44
+ instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w ) where
37
45
local f (EnvT x) = EnvT case x of
38
46
Tuple x y -> Tuple (f x) y
0 commit comments