-
Notifications
You must be signed in to change notification settings - Fork 25
/
DontFearTheProfunctorOptics.hs
301 lines (205 loc) · 8.59 KB
/
DontFearTheProfunctorOptics.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeSynonymInstances #-}
module DontFearTheProfunctorOptics where
import Data.Functor
import Data.Functor.Constant
import Data.List
--------------------------------
-- Part I: Optics, Concretely --
--------------------------------
-- Adapter
data Adapter s t a b = Adapter { from :: s -> a
, to :: b -> t }
fromTo :: Eq s => Adapter s s a a -> s -> Bool
fromTo (Adapter f t) s = (t . f) s == s
toFrom :: Eq a => Adapter s s a a -> a -> Bool
toFrom (Adapter f t) a = (f . t) a == a
shift :: Adapter ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
shift = Adapter f t where
f ((a, b), c) = (a, (b, c))
t (a', (b', c')) = ((a', b'), c')
-- Lens
data Lens s t a b = Lens { view :: s -> a
, update :: (b, s) -> t }
viewUpdate :: Eq s => Lens s s a a -> s -> Bool
viewUpdate (Lens v u) s = u ((v s), s) == s
updateView :: Eq a => Lens s s a a -> a -> s -> Bool
updateView (Lens v u) a s = v (u (a, s)) == a
updateUpdate :: Eq s => Lens s s a a -> a -> a -> s -> Bool
updateUpdate (Lens v u) a1 a2 s = u (a2, (u (a1, s))) == u (a2, s)
pi1 :: Lens (a, c) (b, c) a b
pi1 = Lens v u where
v = fst
u (b, (_, c)) = (b, c)
(|.|) :: Lens s t a b -> Lens a b c d -> Lens s t c d
(Lens v1 u1) |.| (Lens v2 u2) = Lens v u where
v = v2 . v1
u (d, s) = u1 ((u2 (d, (v1 s))), s)
-- Prism
data Prism s t a b = Prism { match :: s -> Either a t
, build :: b -> t }
matchBuild :: Eq s => Prism s s a a -> s -> Bool
matchBuild (Prism m b) s = either b id (m s) == s
buildMatch :: (Eq a, Eq s) => Prism s s a a -> a -> Bool
buildMatch (Prism m b) a = m (b a) == Left a
the :: Prism (Maybe a) (Maybe b) a b
the = Prism m b where
m = maybe (Right Nothing) Left
b = Just
-- Affine
data Affine s t a b = Affine { preview :: s -> Either a t
, set :: (b, s) -> t }
previewSet :: Eq s => Affine s s a a -> s -> Bool
previewSet (Affine p st) s = either (\a -> st (a, s)) id (p s) == s
setPreview :: (Eq a, Eq s) => Affine s s a a -> a -> s -> Bool
setPreview (Affine p st) a s = p (st (a, s)) == either (Left . const a) Right (p s)
setSet :: Eq s => Affine s s a a -> a -> a -> s -> Bool
setSet (Affine p st) a1 a2 s = st (a2, (st (a1, s))) == st (a2, s)
maybeFirst :: Affine (Maybe a, c) (Maybe b, c) a b
maybeFirst = Affine p st where
p (ma, c) = maybe (Right (Nothing, c)) Left ma
st (b, (ma, c)) = (ma $> b, c)
-- Traversal
data Traversal s t a b = Traversal { contents :: s -> [a]
, fill :: ([b], s) -> t }
firstNSecond :: Traversal (a, a, c) (b, b, c) a b
firstNSecond = Traversal c f where
c (a1, a2, _) = [a1, a2]
f (bs, (_, _, x)) = (head bs, (head . tail) bs, x)
-- Right Traversal
data FunList a b t = Done t | More a (FunList a b (b -> t))
newtype Traversal' s t a b = Traversal' { extract :: s -> FunList a b t }
firstNSecond'' :: Traversal' (a, a, c) (b, b, c) a b
firstNSecond'' = Traversal' (\(a1, a2, c) -> More a1 (More a2 (Done (,,c))))
---------------------------------------------------
-- Part II: Profunctors as Generalized Functions --
---------------------------------------------------
-- Functor
-- class Functor f where
-- fmap :: (a -> b) -> f a -> f b
-- instance Functor ((->) r) where
-- fmap f g = g . f
-- Contravariant
class Contravariant f where
cmap :: (b -> a) -> f a -> f b
newtype CReader r a = CReader (a -> r)
instance Contravariant (CReader r) where
cmap f (CReader g) = CReader (g . f)
-- Profunctor
class Profunctor p where
dimap :: (a' -> a) -> (b -> b') -> p a b -> p a' b'
lmap :: (a' -> a) -> p a b -> p a' b
lmap f = dimap f id
rmap :: (b -> b') -> p a b -> p a b'
rmap f = dimap id f
instance Profunctor (->) where
dimap f g h = g . h . f
-- Cartesian
class Profunctor p => Cartesian p where
first :: p a b -> p (a, c) (b, c)
second :: p a b -> p (c, a) (c, b)
instance Cartesian (->) where
first f (a, c) = (f a, c)
second f (c, a) = (c, f a)
-- Cocartesian
class Profunctor p => Cocartesian p where
left :: p a b -> p (Either a c) (Either b c)
right :: p a b -> p (Either c a) (Either c b)
instance Cocartesian (->) where
left f = either (Left . f) Right
right f = either Left (Right . f)
-- Monoidal
class Profunctor p => Monoidal p where
par :: p a b -> p c d -> p (a, c) (b, d)
empty :: p () ()
instance Monoidal (->) where
par f g (a, c) = (f a, g c)
empty = id
-- Beyond Functions
newtype UpStar f a b = UpStar { runUpStar :: a -> f b }
instance Functor f => Profunctor (UpStar f) where
dimap f g (UpStar h) = UpStar (fmap g . h . f)
instance Functor f => Cartesian (UpStar f) where
first (UpStar f) = UpStar (\(a, c) -> fmap (,c) (f a))
second (UpStar f) = UpStar (\(c, a) -> fmap (c,) (f a))
instance Applicative f => Cocartesian (UpStar f) where
left (UpStar f) = UpStar (either (fmap Left . f) (fmap Right . pure))
right (UpStar f) = UpStar (either (fmap Left . pure) (fmap Right . f))
instance Applicative f => Monoidal (UpStar f) where
par (UpStar f) (UpStar g) = UpStar (\(a, b) -> (,) <$> f a <*> g b)
empty = UpStar pure
newtype Tagged a b = Tagged { unTagged :: b }
instance Profunctor Tagged where
dimap _ g (Tagged b) = Tagged (g b)
instance Cocartesian Tagged where
left (Tagged b) = Tagged (Left b)
right (Tagged b) = Tagged (Right b)
instance Monoidal Tagged where
par (Tagged b) (Tagged d) = Tagged (b, d)
empty = Tagged ()
---------------------------------
-- Part III: Profunctor Optics --
---------------------------------
type Optic p s t a b = p a b -> p s t
-- Profunctor Adapter
type AdapterP s t a b = forall p . Profunctor p => Optic p s t a b
adapterC2P :: Adapter s t a b -> AdapterP s t a b
adapterC2P (Adapter f t) = dimap f t
from' :: AdapterP s t a b -> s -> a
from' ad = getConstant . runUpStar (ad (UpStar Constant))
to' :: AdapterP s t a b -> b -> t
to' ad = unTagged . ad . Tagged
shift' :: AdapterP ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
shift' = dimap assoc assoc' where
assoc ((x, y), z) = (x, (y, z))
assoc' (x, (y, z)) = ((x, y), z)
-- Profunctor Lens
type LensP s t a b = forall p . Cartesian p => Optic p s t a b
lensC2P :: Lens s t a b -> LensP s t a b
lensC2P (Lens v u) = dimap dup u . first . lmap v where
dup a = (a, a)
view' :: LensP s t a b -> s -> a
view' ln = getConstant . runUpStar (ln (UpStar Constant))
update' :: LensP s t a b -> (b, s) -> t
update' ln (b, s) = ln (const b) s
pi1' :: LensP (a, c) (b, c) a b
pi1' = first
-- Profunctor Prism
type PrismP s t a b = forall p . Cocartesian p => Optic p s t a b
prismC2P :: Prism s t a b -> PrismP s t a b
prismC2P (Prism m b) = dimap m (either id id) . left . rmap b
match' :: PrismP s t a b -> s -> Either a t
match' pr = runUpStar (pr (UpStar Left))
build' :: PrismP s t a b -> b -> t
build' pr = unTagged . pr . Tagged
the' :: PrismP (Maybe a) (Maybe b) a b
the' = dimap (maybe (Right Nothing) Left) (either Just id) . left
-- Profunctor Affine
type AffineP s t a b = forall p . (Cartesian p, Cocartesian p) => Optic p s t a b
affineC2P :: Affine s t a b -> AffineP s t a b
affineC2P (Affine p st) = dimap preview' merge . left . rmap st . first where
preview' s = either (\a -> Left (a, s)) Right (p s)
merge = either id id
preview' :: AffineP s t a b -> s -> Either a t
preview' af = runUpStar (af (UpStar Left))
set' :: AffineP s t a b -> (b, s) -> t
set' af (b, s) = af (const b) s
maybeFirst' :: AffineP (Maybe a, c) (Maybe b, c) a b
maybeFirst' = first . dimap (maybe (Right Nothing) Left) (either Just id) . left
maybeFirst'' :: AffineP (Maybe a, c) (Maybe b, c) a b
maybeFirst'' = pi1' . the'
-- Profunctor Traversal
type TraversalP s t a b = forall p . (Cartesian p, Cocartesian p, Monoidal p) => Optic p s t a b
traversalC2P :: Traversal s t a b -> TraversalP s t a b
traversalC2P (Traversal c f) = dimap dup f . first . lmap c . ylw where
ylw h = dimap (maybe (Right []) Left . uncons) merge $ left $ rmap cons $ par h (ylw h)
cons = uncurry (:)
dup a = (a, a)
merge = either id id
contents' :: TraversalP s t a b -> s -> [a]
contents' tr = getConstant . runUpStar (tr (UpStar (\a -> Constant [a])))
firstNSecond' :: TraversalP (a, a, c) (b, b, c) a b
firstNSecond' pab = dimap group group' (first (pab `par` pab)) where
group (x, y, z) = ((x, y), z)
group' ((x, y), z) = (x, y, z)