-
Notifications
You must be signed in to change notification settings - Fork 1
/
Class.hs
517 lines (395 loc) · 14.6 KB
/
Class.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Kind ( Constraint )
import Data.Function ( on )
import Data.Functor.Compose ( Compose(Compose)
, getCompose
)
import Control.Arrow ((&&&))
import Control.Monad.Identity (Identity(Identity), runIdentity)
import Control.Applicative (liftA2, Const(Const), getConst)
import Data.Monoid (All(All), getAll)
import Data.Functor.Product (Product(Pair))
import Data.Functor.Sum (Sum(InL, InR))
import Data.Semigroup (Semigroup, (<>))
import qualified Data.Reflection as R
import Data.Proxy (Proxy(Proxy))
-- { Classes
type instance Subclasses SemigroupD a = ()
data SemigroupD a = SemigroupD {
(.<>) :: a -> a -> a
}
type instance Subclasses MonoidD a = Class SemigroupD a
data MonoidD a = MonoidD {
memptyD :: a
}
type instance Subclasses EqD a = ()
data EqD a = EqD {
(.==) :: a -> a -> Bool
}
type instance Subclasses OrdD a = Class EqD a
data OrdD a = OrdD {
compareD :: a -> a -> Ordering
, (.<) :: a -> a -> Bool
--- vv bit of a wart that we have to have this. We need it to
--- implement Preserves.
, (..==) :: a -> a -> Bool
}
type instance Subclasses FunctorD f = ()
data FunctorD f = FunctorD {
fmapD :: forall a b. (a -> b) -> f a -> f b
}
type instance Subclasses ApplicativeD f = Class FunctorD f
data ApplicativeD f = ApplicativeD {
pureD :: forall a. a -> f a
, (.<*>) :: forall a b. f (a -> b) -> f a -> f b
, liftA2D :: forall a b c. (a -> b -> c) -> f a -> f b -> f c
}
-- }
-- { User-defined data types
data Foo a = Foo { foo1 :: Maybe a, foo2 :: [Int] }
data Bar a = Bar1 (Maybe a)
| Bar2 [Int]
data Baz a = Baz { unBaz :: Maybe [a] }
-- "Derived by compiler"
deriveForFoo :: ( Class f [Int]
, Class f (Maybe a)
, Preserves (,) f
, Invariant (->) (->) f )
=> f (Foo a)
deriveForFoo =
mapInvariant (uncurry Foo) (foo1 &&& foo2) (preserve methods methods)
-- "Derived by compiler"
deriveForBar :: ( Class f [Int]
, Class f (Maybe a)
, Preserves Either f
, Invariant (->) (->) f)
=> f (Bar a)
deriveForBar =
mapInvariant
(either Bar1 Bar2)
(\case { Bar1 a -> Left a; Bar2 a -> Right a})
(preserve methods methods)
-- "Derived by compiler"
deriveForBaz :: forall (f :: (* -> *) -> *).
( Preserves Compose f
, Class f Maybe
, Class f []
, Invariant NatTrans (->) f)
=> f Baz
deriveForBaz =
mapInvariant
(NatTrans (Baz . getCompose))
(NatTrans (Compose . unBaz))
(preserve methods methods)
instance Class EqD a => Class EqD (Foo a) where
methods = deriveForFoo
instance Class OrdD a => Class OrdD (Foo a) where
methods = deriveForFoo
instance Class SemigroupD a => Class SemigroupD (Foo a) where
methods = deriveForFoo
instance Class MonoidD a => Class MonoidD (Foo a) where
methods = deriveForFoo
instance Class EqD a => Class EqD (Bar a) where
methods = deriveForBar
instance Class OrdD a => Class OrdD (Bar a) where
methods = deriveForBar
instance Class FunctorD Baz where
methods = deriveForBaz
instance Class ApplicativeD Baz where
methods = deriveForBaz
-- }
-- { Library
preserveClass :: (Preserves f d, Class d a, Class d b) => d (f a b)
preserveClass = preserve methods methods
type family Subclasses (f :: k -> *) (a :: k) :: Constraint
class Subclasses f a => Class (f :: k -> *) (a :: k) where
methods :: f a
class Invariant c c' f where
mapInvariant :: c a b -> c b a -> c' (f a) (f b)
class Preserves p f where
preserve :: f a -> f b -> f (p a b)
(.:) :: (a -> b) -> (c1 -> c2 -> a) -> c1 -> c2 -> b
(.:) f g a b = f (g a b)
-- }
-- { Standard library instances
instance (Class EqD a, Class EqD b) => Class EqD (Either a b) where
methods = preserveClass
instance (Class EqD a, Class EqD b) => Class EqD (a, b) where
methods = preserveClass
instance (Class SemigroupD a, Class SemigroupD b) => Class SemigroupD (a, b) where
methods = preserveClass
instance (Class MonoidD a, Class MonoidD b) => Class MonoidD (a, b) where
methods = preserveClass
instance (Class FunctorD f, Class FunctorD g)
=> Class FunctorD (Compose f g) where
methods = preserveClass
instance (Class ApplicativeD f, Class ApplicativeD g)
=> Class ApplicativeD (Compose f g) where
methods = preserveClass
instance (Class OrdD a, Class OrdD b) => Class OrdD (a, b) where
methods = preserveClass
instance (Class OrdD a, Class OrdD b) => Class OrdD (Either a b) where
methods = preserveClass
instance Class EqD Int where
methods = fromOldClass
instance Class OrdD Int where
methods = fromOldClass
instance Class EqD a => Class EqD [a] where
methods = deriveListViaMaybe
instance Class FunctorD (Const a) where
methods = fromOldClass
instance (Class FunctorD f, Class FunctorD g)
=> Class FunctorD (Sum f g) where
methods = preserveClass
instance (Class FunctorD f, Class FunctorD g)
=> Class FunctorD (Product f g) where
methods = preserveClass
instance Monoid a => Class ApplicativeD (Const a) where
methods = fromOldClass
instance Class ApplicativeD [] where
methods = fromOldClass
instance Class FunctorD Identity where
methods = fromOldClass
instance Class FunctorD [] where
methods = deriveListViaPair1
instance Class FunctorD (Either e) where
methods = fromOldClass
instance Class ApplicativeD (Either e) where
methods = fromOldClass
instance Class FunctorD Maybe where
methods = deriveMaybeViaEither1
instance Class ApplicativeD Maybe where
methods = deriveMaybeViaEither1
instance Class EqD () where
methods = fromOldClass
instance Class OrdD () where
methods = fromOldClass
instance Class OrdD a => Class OrdD [a] where
methods = deriveListViaMaybe
instance Class OrdD a => Class OrdD (Maybe a) where
methods = deriveMaybeViaEither
instance Class EqD a => Class EqD (Maybe a) where
methods = deriveMaybeViaEither
instance Class SemigroupD [a] where
methods = fromOldClass
instance Class MonoidD [a] where
methods = fromOldClass
instance Class SemigroupD a => Class MonoidD (Maybe a) where
methods = MonoidD { memptyD = Nothing }
instance Class SemigroupD a => Class SemigroupD (Maybe a) where
methods = SemigroupD { (.<>) = \a b -> case (a, b) of
(Nothing, Nothing) -> Nothing
(Nothing, Just b') -> Just b'
(Just a', Nothing) -> Just a'
(Just a', Just b') -> Just ((.<>) methods a' b')
}
-- }
-- { Preservations
eitherPreserve :: Class f Int
=> (forall z. f z -> z -> z -> t)
-> f a
-> f b
-> Either a b
-> Either a b
-> t
eitherPreserve op e1 e2 a b =
case (a, b) of
(Left l1, Left l2) ->
op e1 l1 l2
(Right r1, Right r2) ->
op e2 r1 r2
(Left _, Right _) ->
op methods (1 :: Int) 2
(Right _, Left _) ->
op methods (2 :: Int) 1
productPreserve :: Applicative g
=> (forall z. f z -> z -> z -> g z)
-> (g (a, b) -> r)
-> f a
-> f b
-> (a, b)
-> (a, b)
-> r
productPreserve op r e1 e2 (a1, b1) (a2, b2) =
r ((,) <$> op e1 a1 a2 <*> op e2 b1 b2)
using :: (t2 -> t3 -> t4 -> t)
-> (t -> t1) -> t2 -> t3 -> t4 -> t1
(op `using` g) d a b = g (op d a b)
instance Preserves Either OrdD where
preserve e1 e2 = OrdD { compareD = eitherPreserve compareD e1 e2
, (.<) = eitherPreserve (.<) e1 e2
, (..==) = eitherPreserve (..==) e1 e2
}
instance Preserves (,) OrdD where
preserve e1 e2 = OrdD {
compareD = productPreserve
(compareD `using` Const)
getConst
e1
e2
, (.<) = \(a1,a2) (b1,b2) ->
(.<) e1 a1 b1
|| ((..==) e1 a1 b1 && (.<) e2 a2 b2)
, (..==) = productPreserve
((..==) `using` (Const . All))
(getAll . getConst)
e1
e2
}
instance Preserves (,) SemigroupD where
preserve m1 m2 = SemigroupD {
(.<>) = productPreserve ((.<>) `using` Identity) runIdentity m1 m2
}
instance Preserves (,) MonoidD where
preserve s1 s2 = MonoidD { memptyD = (memptyD s1, memptyD s2) }
instance Preserves Compose FunctorD where
preserve f1 f2 =
FunctorD { fmapD = \f c -> Compose ((fmapD f1 . fmapD f2) f (getCompose c)) }
instance Preserves Compose ApplicativeD where
preserve f1 f2 =
ApplicativeD { pureD = Compose . pureD f1 . pureD f2
, (.<*>) = \(Compose f) (Compose x) ->
Compose (liftA2D f1 ((.<*>) f2) f x)
, liftA2D = \f (Compose x) (Compose y) ->
Compose (liftA2D f1 (liftA2D f2 f) x y)
}
instance Preserves Either EqD where
preserve e1 e2 = EqD { (.==) = eitherPreserve (.==) e1 e2 }
instance Preserves Sum FunctorD where
preserve f1 f2 = FunctorD { fmapD = \f -> \case
InL l -> InL (fmapD f1 f l)
InR r -> InR (fmapD f2 f r)
}
instance Preserves Product FunctorD where
preserve f1 f2 = FunctorD {
fmapD = \f (Pair l r) -> Pair (fmapD f1 f l) (fmapD f2 f r)
}
instance Preserves (,) EqD where
preserve e1 e2 = EqD {
(.==) = productPreserve
((.==) `using` (Const . All))
(getAll . getConst)
e1
e2
}
-- }
-- { Invariances
data NatTrans f g = NatTrans { runNatTrans :: forall a. f a -> g a }
instance Invariant (->) (->) EqD where
mapInvariant _ g e = EqD { (.==) = (.==) e `on` g }
instance Invariant (->) (->) OrdD where
mapInvariant _ g e = OrdD { compareD = compareD e `on` g
, (.<) = (.<) e `on` g
, (..==) = (..==) e `on` g
}
instance Invariant (->) (->) SemigroupD where
mapInvariant f g s =
SemigroupD { (.<>) = f .: ((.<>) s `on` g) }
instance Invariant (->) (->) MonoidD where
mapInvariant f _ m = MonoidD { memptyD = f (memptyD m) }
instance Invariant NatTrans (->) FunctorD where
mapInvariant g h f =
FunctorD { fmapD = \i -> runNatTrans g . fmapD f i . runNatTrans h }
instance Invariant NatTrans (->) ApplicativeD where
mapInvariant g h f =
ApplicativeD { pureD = runNatTrans g . pureD f
, (.<*>) = \a b -> runNatTrans g ((.<*>) f (runNatTrans h a)
(runNatTrans h b))
, liftA2D = \i a b ->
runNatTrans g (liftA2D f i (runNatTrans h a) (runNatTrans h b))
}
-- }
-- { Deriving strategies
deriveListViaMaybe :: (Invariant (->) (->) f, Class f (Maybe (a, [a])))
=> f [a]
deriveListViaMaybe = mapInvariant (\case Nothing -> []
Just as -> uncurry (:) as)
(\case [] -> Nothing
(a:as) -> Just (a, as))
methods
deriveMaybeViaEither :: (Invariant (->) (->) f, Class f (Either () a))
=> f (Maybe a)
deriveMaybeViaEither = mapInvariant (\case Left () -> Nothing
Right a -> Just a)
(\case Nothing -> Left ()
Just a -> Right a)
methods
deriveMaybeViaEither1 :: ( Invariant NatTrans (->) f
, Class f (Either ()) )
=> f Maybe
deriveMaybeViaEither1 =
mapInvariant (NatTrans (\case Left () -> Nothing
Right a -> Just a))
(NatTrans (\case Nothing -> Left ()
Just a -> Right a))
methods
type MaybeF = Sum (Const ()) (Product Identity [])
deriveListViaPair1 :: ( Invariant NatTrans (->) f
, Class f MaybeF )
=> f []
deriveListViaPair1 =
mapInvariant (NatTrans (\case InL _ -> []
InR (Pair (Identity a) as) -> a:as))
(NatTrans (\case [] -> InL (Const ())
(a:as) -> InR (Pair (Identity a) as)))
methods
-- }
-- { FromOldClass
class FromOldClass c f | f -> c where
fromOldClass :: forall a. c a => f a
instance FromOldClass Eq EqD where
fromOldClass = EqD { (.==) = (==) }
instance FromOldClass Ord OrdD where
fromOldClass = OrdD {
compareD = compare
, (.<) = (<)
, (..==) = (==)
}
instance FromOldClass Semigroup SemigroupD where
fromOldClass = SemigroupD { (.<>) = (<>) }
instance FromOldClass Monoid MonoidD where
fromOldClass = MonoidD { memptyD = mempty }
instance FromOldClass Functor FunctorD where
fromOldClass = FunctorD { fmapD = fmap }
instance FromOldClass Applicative ApplicativeD where
fromOldClass = ApplicativeD { pureD = pure, (.<*>) = (<*>), liftA2D = liftA2 }
-- }
-- { Reflection
newtype Reflected f a s = Reflected { unReflect :: a }
unreflected :: Reflected f a s -> proxy s -> a
unreflected (Reflected a) _ = a
instance ( Subclasses f (Reflected f a s)
, Invariant (->) (->) f
, R.Reifies s (f a) )
=> Class f (Reflected f a s) where
methods = reflectedMethods
reify :: f a
-> (forall (s :: *). R.Reifies s (f a) => t -> Reflected f a s)
-> t
-> a
reify d m xs = R.reify d (unreflected (m xs))
reflectedMethods :: (Invariant (->) (->) f, R.Reifies s (f a))
=> f (Reflected t a s)
reflectedMethods = getCompose (reflectResult (Compose . mapReflected))
mapReflected :: Invariant (->) (->) g => g a -> g (Reflected f a s)
mapReflected = mapInvariant Reflected unReflect
reflectResult :: forall f s a. R.Reifies s a => (a -> f s) -> f s
reflectResult f = f (R.reflect (Proxy :: Proxy s))
instance (R.Reifies s (SemigroupD a), Class SemigroupD a)
=> Semigroup (Reflected SemigroupD a s) where
(<>) = (.<>) methods
-- }