Skip to content

Commit 78ff659

Browse files
committed
Add ComonadAsk
1 parent 30421c2 commit 78ff659

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Control/Comonad/Env/Class.purs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ import Control.Comonad.Env.Trans (EnvT(..))
77

88
import Data.Tuple (Tuple(..), fst)
99

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.
1212
-- |
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`.
1524
-- |
1625
-- | An implementation is provided for `EnvT`.
1726
-- |
@@ -20,19 +29,18 @@ import Data.Tuple (Tuple(..), fst)
2029
-- | - `ask (local f x) = f (ask x)`
2130
-- | - `extract (local _ x) = extract a`
2231
-- | - `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
2533
local :: forall a. (e -> e) -> w a -> w a
2634

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
3037

3138
instance comonadEnvTuple :: ComonadEnv e (Tuple e) where
32-
ask = fst
3339
local f (Tuple x y) = Tuple (f x) y
3440

35-
instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w) where
41+
instance comonadAskEnvT :: Comonad w => ComonadAsk e (EnvT e w) where
3642
ask (EnvT x) = fst x
43+
44+
instance comonadEnvEnvT :: Comonad w => ComonadEnv e (EnvT e w) where
3745
local f (EnvT x) = EnvT case x of
3846
Tuple x y -> Tuple (f x) y

0 commit comments

Comments
 (0)