Skip to content

Commit 6337064

Browse files
committed
Merge pull request #19 from joneshf/comonad-trans
Comonad trans
2 parents 8de108b + 99e7df7 commit 6337064

File tree

12 files changed

+416
-8
lines changed

12 files changed

+416
-8
lines changed

.travis.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
language: node_js
22
node_js:
33
- "0.10"
4+
env:
5+
- PATH=$HOME/bin:$PATH purescript_datadir=$HOME/.local/share/purescript
6+
before_install:
7+
- mkdir -p $HOME/bin
8+
- mkdir -p $HOME/.local/share/purescript/prelude
49
install:
5-
- "sudo apt-get install cabal-install"
6-
- "cabal update"
7-
- "cabal install Cabal cabal-install"
8-
- "export PATH=~/.cabal/bin:$PATH"
9-
- "cabal install purescript --force-reinstalls"
10+
- wget -O $HOME/bin/psc https://github.com/purescript/purescript/releases/download/0.5.6.2/psc
11+
- chmod a+x $HOME/bin/psc
12+
- wget -O $purescript_datadir/prelude/prelude.purs https://github.com/purescript/purescript/releases/download/0.5.6.2/prelude.purs
1013
- "npm install bower grunt-cli -g"
1114
- "npm install"
1215
- "bower install"

docs/Module.md

Lines changed: 203 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,205 @@
11
# Module Documentation
22

3+
## Module Control.Comonad.Env
4+
5+
### Types
6+
7+
type Env e = EnvT e Identity
8+
9+
10+
### Values
11+
12+
env :: forall e a. e -> a -> Env e a
13+
14+
mapEnv :: forall e a b. (a -> b) -> Env e a -> Env e b
15+
16+
runEnv :: forall e a. Env e a -> Tuple e a
17+
18+
withEnv :: forall e1 e2 a. (e1 -> e2) -> Env e1 a -> Env e2 a
19+
20+
21+
## Module Control.Comonad.Env.Class
22+
23+
### Type Classes
24+
25+
class (Comonad w) <= ComonadEnv e w where
26+
ask :: forall a. w a -> e
27+
local :: forall a. (e -> e) -> w a -> w a
28+
29+
30+
### Type Class Instances
31+
32+
instance comonadEnvEnvT :: (Comonad w) => ComonadEnv e (EnvT e w)
33+
34+
instance comonadEnvTuple :: ComonadEnv e (Tuple e)
35+
36+
37+
### Values
38+
39+
asks :: forall e1 e2 w a. (ComonadEnv e1 w) => (e1 -> e2) -> w e1 -> e2
40+
41+
42+
## Module Control.Comonad.Env.Trans
43+
44+
### Types
45+
46+
newtype EnvT e w a where
47+
EnvT :: Tuple e (w a) -> EnvT e w a
48+
49+
50+
### Type Class Instances
51+
52+
instance comonadEnvT :: (Comonad w) => Comonad (EnvT e w)
53+
54+
instance comonadTransEnvT :: ComonadTrans (EnvT e)
55+
56+
instance extendEnvT :: (Extend w) => Extend (EnvT e w)
57+
58+
instance functorEnvT :: (Functor w) => Functor (EnvT e w)
59+
60+
61+
### Values
62+
63+
mapEnvT :: forall e w1 w2 a b. (w1 a -> w2 b) -> EnvT e w1 a -> EnvT e w2 b
64+
65+
runEnvT :: forall e w a. EnvT e w a -> Tuple e (w a)
66+
67+
withEnvT :: forall e1 e2 w a. (e1 -> e2) -> EnvT e1 w a -> EnvT e2 w a
68+
69+
70+
## Module Control.Comonad.Store
71+
72+
### Types
73+
74+
type Store s a = StoreT s Identity a
75+
76+
77+
### Values
78+
79+
runStore :: forall s a. Store s a -> Tuple (s -> a) s
80+
81+
store :: forall s a. (s -> a) -> s -> Store s a
82+
83+
84+
## Module Control.Comonad.Store.Class
85+
86+
### Type Classes
87+
88+
class (Comonad w) <= ComonadStore s w where
89+
pos :: forall a. w a -> s
90+
peek :: forall a. s -> w a -> a
91+
92+
93+
### Type Class Instances
94+
95+
instance comonadStoreStoreT :: (Comonad w) => ComonadStore s (StoreT s w)
96+
97+
98+
### Values
99+
100+
experiment :: forall f a w s. (ComonadStore s w, Functor f) => (s -> f s) -> w a -> f a
101+
102+
peeks :: forall s a w. (ComonadStore s w) => (s -> s) -> w a -> a
103+
104+
seek :: forall s a w. (ComonadStore s w, Extend w) => s -> w a -> w a
105+
106+
seeks :: forall s a w. (ComonadStore s w, Extend w) => (s -> s) -> w a -> w a
107+
108+
109+
## Module Control.Comonad.Store.Trans
110+
111+
### Types
112+
113+
newtype StoreT s w a where
114+
StoreT :: Tuple (w (s -> a)) s -> StoreT s w a
115+
116+
117+
### Type Class Instances
118+
119+
instance comonadStoreT :: (Comonad w) => Comonad (StoreT s w)
120+
121+
instance comonadTransStoreT :: ComonadTrans (StoreT s)
122+
123+
instance extendStoreT :: (Extend w) => Extend (StoreT s w)
124+
125+
instance functorStoreT :: (Functor w) => Functor (StoreT s w)
126+
127+
128+
### Values
129+
130+
runStoreT :: forall s w a. StoreT s w a -> Tuple (w (s -> a)) s
131+
132+
133+
## Module Control.Comonad.Traced
134+
135+
### Types
136+
137+
type Traced m = TracedT m Identity
138+
139+
140+
### Values
141+
142+
runTraced :: forall m a. Traced m a -> m -> a
143+
144+
traced :: forall m a. (m -> a) -> Traced m a
145+
146+
147+
## Module Control.Comonad.Traced.Class
148+
149+
### Type Classes
150+
151+
class (Comonad w) <= ComonadTraced t w where
152+
track :: forall a. t -> w a -> a
153+
154+
155+
### Type Class Instances
156+
157+
instance comonadTracedTracedT :: (Comonad w, Monoid t) => ComonadTraced t (TracedT t w)
158+
159+
160+
### Values
161+
162+
censor :: forall w a t b. (Functor w) => (t -> t) -> TracedT t w a -> TracedT t w a
163+
164+
listen :: forall w a t. (Functor w) => TracedT t w a -> TracedT t w (Tuple a t)
165+
166+
listens :: forall w a t b. (Functor w) => (t -> b) -> TracedT t w a -> TracedT t w (Tuple a b)
167+
168+
tracks :: forall w a t. (Comonad w, ComonadTraced t w) => (a -> t) -> w a -> a
169+
170+
171+
## Module Control.Comonad.Traced.Trans
172+
173+
### Types
174+
175+
newtype TracedT t w a where
176+
TracedT :: w (t -> a) -> TracedT t w a
177+
178+
179+
### Type Class Instances
180+
181+
instance comonadTracedT :: (Comonad w, Monoid t) => Comonad (TracedT t w)
182+
183+
instance comonadTransTracedT :: (Monoid t) => ComonadTrans (TracedT t)
184+
185+
instance extendTracedT :: (Extend w, Semigroup t) => Extend (TracedT t w)
186+
187+
instance functorTracedT :: (Functor w) => Functor (TracedT t w)
188+
189+
190+
### Values
191+
192+
runTracedT :: forall w a t. TracedT t w a -> w (t -> a)
193+
194+
195+
## Module Control.Comonad.Trans
196+
197+
### Type Classes
198+
199+
class ComonadTrans f where
200+
lower :: forall w a. (Comonad w) => f w a -> w a
201+
202+
3203
## Module Control.Monad.Cont.Class
4204

5205
### Type Classes
@@ -35,7 +235,7 @@
35235

36236
instance applicativeContT :: (Functor m, Monad m) => Applicative (ContT r m)
37237

38-
instance appluContT :: (Functor m, Monad m) => Apply (ContT r m)
238+
instance applyContT :: (Functor m, Monad m) => Apply (ContT r m)
39239

40240
instance bindContT :: (Monad m) => Bind (ContT r m)
41241

@@ -109,9 +309,9 @@
109309

110310
instance alternativeErrorT :: (Monad m, Error e) => Alternative (ErrorT e m)
111311

112-
instance applicativeErrorT :: (Functor m, Monad m) => Applicative (ErrorT e m)
312+
instance applicativeErrorT :: (Applicative m) => Applicative (ErrorT e m)
113313

114-
instance applyErrorT :: (Functor m, Monad m) => Apply (ErrorT e m)
314+
instance applyErrorT :: (Apply m) => Apply (ErrorT e m)
115315

116316
instance bindErrorT :: (Monad m, Error e) => Bind (ErrorT e m)
117317

src/Control/Comonad/Env.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Control.Comonad.Env where
2+
3+
import Control.Comonad.Env.Trans
4+
import Control.Monad.Identity
5+
6+
import Data.Tuple
7+
8+
type Env e = EnvT e Identity
9+
10+
runEnv :: forall e a. Env e a -> Tuple e a
11+
runEnv x = runIdentity <$> runEnvT x
12+
13+
withEnv :: forall e1 e2 a. (e1 -> e2) -> Env e1 a -> Env e2 a
14+
withEnv = withEnvT
15+
16+
mapEnv :: forall e a b. (a -> b) -> Env e a -> Env e b
17+
mapEnv = (<$>)
18+
19+
env :: forall e a. e -> a -> Env e a
20+
env e a = EnvT $ Tuple e $ Identity a

src/Control/Comonad/Env/Class.purs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Control.Comonad.Env.Class where
2+
3+
import Control.Comonad
4+
import Control.Comonad.Env
5+
import Control.Comonad.Env.Trans
6+
7+
import Data.Tuple
8+
9+
class (Comonad w) <= ComonadEnv e w where
10+
ask :: forall a. w a -> e
11+
local :: forall a. (e -> e) -> w a -> w a
12+
13+
instance comonadEnvTuple :: ComonadEnv e (Tuple e) where
14+
ask = fst
15+
local f (Tuple x y) = Tuple (f x) y
16+
17+
instance comonadEnvEnvT :: (Comonad w) => ComonadEnv e (EnvT e w) where
18+
ask x = fst $ runEnvT x
19+
local f x = EnvT $ case runEnvT x of
20+
Tuple x y -> Tuple (f x) y
21+
22+
asks :: forall e1 e2 w a. (ComonadEnv e1 w) => (e1 -> e2) -> w e1 -> e2
23+
asks f x = f $ ask x

src/Control/Comonad/Env/Trans.purs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Control.Comonad.Env.Trans where
2+
3+
import Control.Comonad
4+
import Control.Comonad.Trans
5+
import Control.Extend
6+
import Data.Tuple
7+
8+
newtype EnvT e w a = EnvT (Tuple e (w a))
9+
10+
runEnvT :: forall e w a. EnvT e w a -> Tuple e (w a)
11+
runEnvT (EnvT x) = x
12+
13+
withEnvT :: forall e1 e2 w a. (e1 -> e2) -> EnvT e1 w a -> EnvT e2 w a
14+
withEnvT f (EnvT (Tuple e x)) = EnvT $ Tuple (f e) x
15+
16+
mapEnvT :: forall e w1 w2 a b. (w1 a -> w2 b) -> EnvT e w1 a -> EnvT e w2 b
17+
mapEnvT f (EnvT (Tuple e x)) = EnvT $ Tuple e (f x)
18+
19+
instance functorEnvT :: (Functor w) => Functor (EnvT e w) where
20+
(<$>) f (EnvT (Tuple e x)) = EnvT $ Tuple e (f <$> x)
21+
22+
instance extendEnvT :: (Extend w) => Extend (EnvT e w) where
23+
(<<=) f (EnvT (Tuple e x)) = EnvT $ Tuple e (f <$> ((Tuple e >>> EnvT) <<= x))
24+
25+
instance comonadEnvT :: (Comonad w) => Comonad (EnvT e w) where
26+
extract (EnvT (Tuple e x)) = extract x
27+
28+
instance comonadTransEnvT :: ComonadTrans (EnvT e) where
29+
lower (EnvT (Tuple e x)) = x

src/Control/Comonad/Store.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Control.Comonad.Store where
2+
3+
import Control.Comonad.Store.Trans
4+
import Control.Monad.Identity
5+
6+
import Data.Tuple
7+
8+
type Store s a = StoreT s Identity a
9+
10+
runStore :: forall s a. Store s a -> Tuple (s -> a) s
11+
runStore s = swap (runIdentity <$> (swap $ runStoreT s))
12+
13+
store :: forall s a. (s -> a) -> s -> Store s a
14+
store f x = StoreT $ Tuple (Identity f) x

src/Control/Comonad/Store/Class.purs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Control.Comonad.Store.Class where
2+
3+
import Control.Comonad
4+
import Control.Comonad.Store.Trans
5+
import Control.Extend
6+
7+
import Data.Tuple
8+
9+
class (Comonad w) <= ComonadStore s w where
10+
pos :: forall a. w a -> s
11+
peek :: forall a. s -> w a -> a
12+
13+
instance comonadStoreStoreT :: (Comonad w) => ComonadStore s (StoreT s w) where
14+
pos (StoreT (Tuple f s)) = s
15+
peek s (StoreT (Tuple f _)) = extract f s
16+
17+
experiment :: forall f a w s. (ComonadStore s w, Functor f) => (s -> f s) -> w a -> f a
18+
experiment f x = flip peek x <$> f (pos x)
19+
20+
peeks :: forall s a w. (ComonadStore s w) => (s -> s) -> w a -> a
21+
peeks f x = peek (f $ pos x) x
22+
23+
seek :: forall s a w. (ComonadStore s w, Extend w) => s -> w a -> w a
24+
seek s x = peek s $ duplicate x
25+
26+
seeks :: forall s a w. (ComonadStore s w, Extend w) => (s -> s) -> w a -> w a
27+
seeks f x = peeks f $ duplicate x

src/Control/Comonad/Store/Trans.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Control.Comonad.Store.Trans where
2+
3+
import Control.Extend
4+
import Control.Comonad
5+
import Control.Comonad.Trans
6+
7+
import Data.Tuple
8+
9+
newtype StoreT s w a = StoreT (Tuple (w (s -> a)) s)
10+
11+
runStoreT :: forall s w a. StoreT s w a -> Tuple (w (s -> a)) s
12+
runStoreT (StoreT s) = s
13+
14+
instance functorStoreT :: (Functor w) => Functor (StoreT s w) where
15+
(<$>) f (StoreT (Tuple w s)) = StoreT $ Tuple ((\h -> h >>> f) <$> w) s
16+
17+
instance extendStoreT :: (Extend w) => Extend (StoreT s w) where
18+
(<<=) f (StoreT (Tuple w s)) = StoreT $ Tuple ((\w' s' -> f $ StoreT $ Tuple w' s') <<= w) s
19+
20+
instance comonadStoreT :: (Comonad w) => Comonad (StoreT s w) where
21+
extract (StoreT (Tuple w s)) = extract w s
22+
23+
instance comonadTransStoreT :: ComonadTrans (StoreT s) where
24+
lower (StoreT (Tuple w s)) = (\f -> f s) <$> w

0 commit comments

Comments
 (0)