forked from mroman42/vitrea-prototype-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vitrea.hs
278 lines (211 loc) · 8.69 KB
/
Vitrea.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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LiberalTypeSynonyms #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Vitrea where
import Control.Monad.State
import Data.Foldable
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Profunctor
import Data.Void
import GHC.Exts hiding (toList)
-------------------------------------------
-- PROFUNCTOR/EXISTENTIAL REPRESENTATION --
-------------------------------------------
-- Existential optics.
data ExOptic mon a b s t where
ExOptic :: (mon f) => (s -> f a) -> (f b -> t) -> ExOptic mon a b s t
-- A submonoid is unital if we can have some identity optic. It is
-- functorial and multiplicative if we can create a bigger optic from
-- a small one.
class Tensorial mon where
idOptic :: ExOptic mon a b a b
multOptic :: (mon f) => ExOptic mon a b s t -> ExOptic mon a b (f s) (f t)
-- Profunctor optics.
class (Profunctor p) => Tambara mon p where
action :: forall f a b . (mon f) => p a b -> p (f a) (f b)
type ProfOptic mon a b s t = forall p . Tambara mon p => p a b -> p s t
-- Equivalence profunctor to existential.
instance Profunctor (ExOptic mon a b) where
dimap u v (ExOptic l r) = ExOptic (l . u) (v . r)
instance (Tensorial mon) => Tambara mon (ExOptic mon a b) where
action = multOptic
crOptic :: (Tensorial mon) => ProfOptic mon a b s t -> ExOptic mon a b s t
crOptic p = p idOptic
mkOptic :: forall mon a b s t . ExOptic mon a b s t -> ProfOptic mon a b s t
mkOptic (ExOptic l r) = dimap l r . (action @mon)
------------
-- OPTICS --
------------
-- Lenses.
class (f ~ ((,) (Arg f))) => Product f
class (f ~ ((,) (Arg f))) => Lens f
instance Product ((,) a)
instance Tensorial Product where
idOptic = ExOptic ((,) ()) snd
multOptic (ExOptic l r) = ExOptic (\(d,s) -> ((d , fst (l s)), snd (l s))) (\((d,c),b) -> (d,r (c,b)))
type ExLens s t a b = ExOptic Product a b s t
type ProfLens s t a b = ProfOptic Product a b s t
type ExLens' t a = ExOptic Product a a t t
type ProfLens' t a = ProfOptic Product a a t t
mkLens :: forall a t . ExLens' t a -> ProfLens' t a
mkLens = mkOptic
-- Prisms.
class (f ~ (Either (Arg f))) => Sum f
instance Sum (Either a)
instance Tensorial Sum where
idOptic = ExOptic Right (either absurd id)
multOptic (ExOptic l r) = ExOptic
(either (Left . Left) (either (Left . Right) Right . l))
(either (either Left (Right . r . Left)) (Right . r . Right))
type ExPrism s t a b = ExOptic Sum a b s t
type ProfPrism s t a b = ProfOptic Sum a b s t
type ExPrism' t a = ExOptic Sum a a t t
type ProfPrism' t a = ProfOptic Sum a a t t
mkPrism :: forall a t . ExPrism' t a -> ProfPrism' t a
mkPrism = mkOptic
-- Grates.
class (f ~ ((->) (Arg f))) => Exponential f
instance Exponential ((->) a)
instance Tensorial Exponential where
idOptic = ExOptic const ($ ())
multOptic (ExOptic l r) = ExOptic (\f (d , c) -> l (f d) c) ((r .) . curry)
type ExGrate s t a b = ExOptic Exponential a b s t
type ProfGrate s t a b = ProfOptic Exponential a b s t
-- Traversals.
instance Tensorial Traversable where
idOptic = ExOptic Identity runIdentity
multOptic (ExOptic l r) = ExOptic (Compose . fmap l) (fmap r . getCompose)
type ExTraversal s t a b = ExOptic Traversable a b s t
type ProfTraversal s t a b = ProfOptic Traversable a b s t
type ExTraversal' t a = ExOptic Traversable a a t t
type ProfTraversal' t a = ProfOptic Traversable a a t t
mkTraversal :: forall a t . ExTraversal' t a -> ProfTraversal' t a
mkTraversal = mkOptic
instance (Profunctor p , Tambara Traversable p) => Tambara AffineTransf p where
action = dimap (Compose . unAff) (Aff . getCompose) . action @Traversable
-- Setters.
instance Tensorial Functor where
idOptic = ExOptic Identity runIdentity
multOptic (ExOptic l r) = ExOptic (Compose . fmap l) (fmap r . getCompose)
type ExSetter s t a b = ExOptic Functor a b s t
type ProfSetter s t a b = ProfOptic Functor a b s t
-- Affine, which is both Sum and Product.
data Aff a b c = Aff { unAff :: Either a (b , c) }
class (f ~ Aff (Arg1 f) (Arg2 f)) => AffineTransf f
instance AffineTransf (Aff a b)
instance Tensorial AffineTransf where
idOptic = ExOptic (Aff . Right . ((,) ())) (either absurd snd . unAff)
multOptic (ExOptic l r) = ExOptic (u l) (v r)
where
u :: (s -> Aff c d a) -> Aff e f s -> Aff (Either e (f,c)) (f,d) a
u l (Aff (Left e)) = Aff $ Left $ Left e
u l (Aff (Right (f,s))) = Aff $ case unAff (l s) of
(Left c) -> (Left (Right (f,c)))
(Right (d,a)) -> (Right ((f,d),a))
v :: (Aff c d b -> t) -> Aff (Either e (f,c)) (f,d) b -> Aff e f t
v r (Aff (Left (Left e))) = Aff $ Left e
v r (Aff (Left (Right (f,c)))) = Aff $ Right (f,r $ Aff $ Left c)
v r (Aff (Right ((f,d),b))) = Aff $ Right (f , r . Aff $ Right (d,b))
instance (Profunctor p , Tambara AffineTransf p) => Tambara Sum p where
action = dimap
(Aff . either (Left . id) (Right . ((,) ())))
(either Left (Right . snd) . unAff)
. action @AffineTransf
instance (Profunctor p , Tambara AffineTransf p) => Tambara Product p where
action = dimap (Aff . Right) (either absurd id . unAff) . action @AffineTransf
type ExAffine s t a b = ExOptic AffineTransf a b s t
type ProfAffine s t a b = ProfOptic AffineTransf a b s t
type ExAffine' t a = ExOptic AffineTransf a a t t
type ProfAffine' t a = ProfOptic AffineTransf a a t t
-- Glass.
data Gls a b c = Gls (a , b -> c)
class (f ~ Gls (Arg1 f) (Arg2 f)) => GlassTransf f
instance GlassTransf (Gls a b)
-- Achromatic lens.
data Achl a b = Achl (Maybe a , b)
class (f ~ Achl (Arg f)) => Achromatic f
instance Achromatic (Achl a)
-- -- Adapters. (TODO: Composition of identities is not the identity, maybe we could use Yoneda)
-- class (f ~ Identity) => IsIdentity f
-- instance IsIdentity Identity
-- instance Tensorial IsIdentity where
-- idOptic = ExOptic coerce runIdentity
-- multOptic (ExOptic l r) = ExOptic _ _
-----------------
-- COMBINATORS --
-----------------
-- Now some combinators.
data CrAff s t a b = CrAff
{ aoriginal :: s
, aget :: Maybe a
, aset :: Maybe (b -> t)
}
instance (Show s, Show a) => Show (CrAff s t a b) where
show (CrAff o Nothing t) = "Nothing"
show (CrAff o (Just a) t) = show a
(^.) :: s -> ProfAffine s t a b -> CrAff s t a b
s ^. p = CrAff s (getter (crOptic p) s) (setter (crOptic p) s)
where
getter :: ExAffine s t a b -> s -> Maybe a
getter (ExOptic l _) = either (const Nothing) (Just . snd) . unAff . l
setter :: ExAffine s t a b -> s -> Maybe (b -> t)
setter (ExOptic l r) =
either (const Nothing)
(Just . curry (r . Aff . Right) . fst) . unAff . l
(<~) :: CrAff s s a a -> a -> s
(<~) (CrAff s _ Nothing) b = s
(<~) (CrAff _ _ (Just f)) b = f b
(<~~) :: CrAff s s a a -> (a -> a) -> s
(<~~) (CrAff s _ Nothing) g = s
(<~~) (CrAff s Nothing _) g = s
(<~~) (CrAff _ (Just a) (Just f)) g = f $ g a
data TrAff s t a b = TrAff
{ tor :: s
, tget :: [a]
, tset :: [b] -> t
}
(<~~~) :: TrAff s s a a -> (a -> a) -> s
(<~~~) (TrAff _ a f) g = f $ fmap g a
instance (Show s, Show a) => Show (TrAff s t a b) where
show = show . tget
(^..) :: s -> ProfTraversal s t a b -> TrAff s t a b
s ^.. p = TrAff s (getter (crOptic p) s) (setter (crOptic p) s)
where
getter :: ExTraversal s t a b -> s -> [a]
getter (ExOptic l _) = toList . l
setter :: ExTraversal s t a b -> s -> [b] -> t
setter (ExOptic l r) s list = r . fst . ($ 0) . runState . mapM (counter list) . l $ s
where
counter :: [b] -> a -> State Int b
counter list _ = do
n <- get
modify (+1)
return $ list !! n
infixr 8 ^.
infixr 8 ^..
infixl 6 <~
infixl 6 <~~
infixl 6 <~~~
-- Auxiliary functions
type family Arg (x :: * -> *) where Arg (f a) = a
type family Arg1 (x :: * -> *) where Arg1 (f a b) = a
type family Arg2 (x :: * -> *) where Arg2 (f a b) = b