-
Notifications
You must be signed in to change notification settings - Fork 49
/
Category.hs
2682 lines (2100 loc) · 77.9 KB
/
Category.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE NoStarIsType #-}
{-# OPTIONS_GHC -Wall #-}
-- {-# OPTIONS_GHC -fno-warn-unused-imports #-} -- TEMP
{-# OPTIONS_GHC -fconstraint-solver-iterations=10 #-} -- for Oks
-- For ConCat.Inline.ClassOp
{-# OPTIONS_GHC -fplugin=ConCat.Inline.Plugin #-}
-- {-# OPTIONS_GHC -ddump-simpl #-}
-- | Another go at constrained categories. This time without Prod, Coprod, Exp.
module ConCat.Category where
import Prelude hiding (id,(.),curry,uncurry,const,zip,unzip,zipWith,minimum,maximum)
import qualified Prelude as P
import Control.Arrow (Kleisli(..),arr)
import qualified Control.Arrow as A
import Control.Monad ((<=<))
import Data.Typeable (Typeable)
import GHC.Exts (Coercible,coerce)
import qualified GHC.Exts as X
import Data.Type.Equality ((:~:)(..))
import qualified Data.Type.Equality as Eq
import Data.Type.Coercion (Coercion(..))
import qualified Data.Type.Coercion as Co
import GHC.Types (Type)
import Data.Constraint hiding ((&&&),(***),(:=>))
-- import Debug.Trace
import Data.Monoid
import GHC.Generics (U1(..),Par1(..),(:*:)(..),(:.:)(..))
import GHC.TypeLits
import Control.Monad.Fix (MonadFix)
-- import Data.Proxy (Proxy)
import Data.Pointed
import Data.Key (Zip(..))
import Data.Distributive (Distributive(..))
import Data.Functor.Rep (Representable(..))
import Control.Newtype.Generics (Newtype(..))
import Data.Vector.Sized (Vector)
import Data.Finite.Internal (Finite(..))
import ConCat.Misc hiding ((<~),(~>),type (&&))
import ConCat.Rep hiding (Rep)
import ConCat.MinMax
import qualified ConCat.Rep as R
import ConCat.Additive
import qualified ConCat.Inline.ClassOp as IC
#define PINLINER(nm) {-# INLINE nm #-}
-- #define PINLINER(nm)
-- Prevents some subtle non-termination errors. See 2017-12-27 journal notes.
-- #define OPINLINE INLINE [0]
-- Changed to NOINLINE [0]. See 2017-12-29 journal notes.
#define OPINLINE NOINLINE [0]
{--------------------------------------------------------------------
Unit and pairing for binary type constructors
--------------------------------------------------------------------}
-- Unit for binary type constructors
data U2 a b = U2 deriving (Show)
infixr 7 :**:
-- | Product for binary type constructors
data (p :**: q) a b = p a b :**: q a b
prod :: p a b :* q a b -> (p :**: q) a b
prod (p,q) = (p :**: q)
unProd :: (p :**: q) a b -> p a b :* q a b
unProd (p :**: q) = (p,q)
exl2 :: (p :**: q) a b -> p a b
exl2 = exl . unProd
exr2 :: (p :**: q) a b -> q a b
exr2 = exr . unProd
instance HasRep ((k :**: k') a b) where
type Rep ((k :**: k') a b) = k a b :* k' a b
abst (f,g) = f :**: g
repr (f :**: g) = (f,g)
{--------------------------------------------------------------------
Monoid wrapper
--------------------------------------------------------------------}
newtype Monoid2 m a b = Monoid2 m
{--------------------------------------------------------------------
Constraints
--------------------------------------------------------------------}
class HasCon a where
type Con a :: Constraint
toDict :: a -> Dict (Con a)
unDict :: Con a => a
newtype Sat kon a = Sat (Dict (kon a))
instance HasCon (Sat kon a) where
type Con (Sat kon a) = kon a
toDict (Sat d) = d
unDict = Sat Dict
instance HasCon () where
type Con () = ()
toDict () = Dict
unDict = ()
instance (HasCon a, HasCon b) => HasCon (a :* b) where
type Con (a :* b) = (Con a,Con b)
toDict (toDict -> Dict, toDict -> Dict) = Dict
unDict = (unDict,unDict)
infixr 1 |-
newtype a |- b = Entail (Con a :- Con b)
instance Newtype (a |- b) where
type O (a |- b) = Con a :- Con b
pack e = Entail e
unpack (Entail e) = e
instance Category (|-) where
-- type Ok (|-) = HasCon
id = pack refl
(.) = inNew2 (\ g f -> Sub $ Dict \\ g \\ f)
instance OpCon (:*) (Sat HasCon) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance AssociativePCat (|-) where
lassocP = pack (Sub Dict)
rassocP = pack (Sub Dict)
instance MonoidalPCat (|-) where
(***) = inNew2 $ \ f g -> Sub $ Dict \\ f \\ g
instance BraidedPCat (|-) where
swapP = pack (Sub Dict)
instance ProductCat (|-) where
-- type Prod (|-) = (:*)
exl = pack (Sub Dict)
exr = pack (Sub Dict)
dup = pack (Sub Dict)
-- (&&&) = inNew2 $ \ f g -> Sub $ Dict \\ f \\ g
infixl 1 <+
-- | Wrapper
(<+) :: Con a => (Con b => r) -> (a |- b) -> r
r <+ Entail (Sub Dict) = r
-- f <+ Entail e = f \\ e
{-# INLINE (<+) #-}
infixr 3 &&
-- (&&) = Prod (|-)
type (&&) = (:*)
class OpCon op con where
inOp :: con a && con b |- con (a `op` b)
-- default inOp :: (forall a b. (Con (con a), Con (con b)) => Con (con (a `op` b)))
-- => con a && con b |- con (a `op` b)
-- inOp = Entail (Sub Dict)
-- TODO: Look for a working type for this default inOp definition
-- class OpCon op (Dict (kon a)) => OpCon' op kon a
-- instance OpCon op (Dict (kon a)) => OpCon' op kon a
-- class kon a => Sat kon a
-- instance kon a => Sat kon a
yes1 :: c |- Sat Yes1 a
yes1 = Entail (Sub Dict) -- Move to AltCat
forkCon :: forall con con' a. Sat (con &+& con') a |- Sat con a :* Sat con' a
forkCon = Entail (Sub Dict)
joinCon :: forall con con' a. Sat con a :* Sat con' a |- Sat (con &+& con') a
joinCon = Entail (Sub Dict)
inForkCon :: (Sat con1 a :* Sat con2 a |- Sat con1' b :* Sat con2' b)
-> (Sat (con1 &+& con2) a |- Sat (con1' &+& con2') b)
inForkCon h = joinCon . h . forkCon
-- We might want forkCon, joinCon, and inForkCon elsewhere as well.
-- Consider renaming.
type Yes1' = Sat Yes1
type Ok' k = Sat (Ok k)
type OpSat op kon = OpCon op (Sat kon)
inSat :: OpCon op (Sat con) => Sat con a && Sat con b |- Sat con (a `op` b)
inSat = inOp
inOpL :: OpCon op con => (con a && con b) && con c |- con ((a `op` b) `op` c)
inOpL = inOp . first inOp
inOpR :: OpCon op con => con a && (con b && con c) |- con (a `op` (b `op` c))
inOpR = inOp . second inOp
inOpL' :: OpCon op con
=> (con a && con b) && con c |- con (a `op` b) && con ((a `op` b) `op` c)
inOpL' = inOp . exl &&& inOpL
-- inOpL' = second inOp . rassocP . first (dup . inOp)
-- (con a && con b) && con c
-- con (a `op` b) && con c
-- (con (a `op` b) && con (a `op` b)) && con c
-- con (a `op` b) && (con (a `op` b) && con c)
-- con (a `op` b) && con ((a `op` b) `op` c)
inOpR' :: OpCon op con => con a && (con b && con c) |- con (a `op` (b `op` c)) && con (b `op` c)
inOpR' = inOpR &&& inOp . exr
-- inOpR' = first inOp . lassocP . second (dup . inOp)
-- There were mutual recursions between (a) inOpL' & rassocP, and (b) inOpR' & lassocP
inOpLR :: forall op con a b c. OpCon op con =>
((con a && con b) && con c) && (con a && (con b && con c))
|- con ((a `op` b) `op` c) && con (a `op` (b `op` c))
inOpLR = inOpL *** inOpR
instance OpCon op Yes1' where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance Typeable op => OpCon op (Sat Typeable) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance OpCon (:*) (Sat Eq) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance OpCon (:*) (Sat Ord) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
-- TODO: more OpCon instances for standard type classes
instance OpCon (:*) (Sat Additive) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance OpCon (:+) (Sat Eq) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance OpCon (:+) (Sat Ord) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
instance OpCon (->) (Sat Additive) where
inOp = Entail (Sub Dict)
{-# INLINE inOp #-}
class OkAdd k where okAdd :: Ok' k a |- Sat Additive a
type Ok2 k a b = C2 (Ok k) a b
type Ok3 k a b c = C3 (Ok k) a b c
type Ok4 k a b c d = C4 (Ok k) a b c d
type Ok5 k a b c d e = C5 (Ok k) a b c d e
type Ok6 k a b c d e f = C6 (Ok k) a b c d e f
type Oks k as = AllC (Ok k) as
-- I like the elegance of Oks, but it leads to complex dictionary expressions.
-- For now, use Okn for the operations introduced by lambda-to-ccc conversion.
class Show2 k where show2 :: a `k` b -> String
{--------------------------------------------------------------------
Categories
--------------------------------------------------------------------}
class Category k where
type Ok k :: Type -> Constraint
type Ok k = Yes1
id :: Ok k a => a `k` a
infixr 9 .
(.) :: forall b c a. Ok3 k a b c => (b `k` c) -> (a `k` b) -> (a `k` c)
infixl 1 <~
infixr 1 ~>
-- | Add post- and pre-processing
(<~) :: (Category k, Oks k [a,b,a',b'])
=> (b `k` b') -> (a' `k` a) -> ((a `k` b) -> (a' `k` b'))
(h <~ f) g = h . g . f
-- | Add pre- and post-processing
(~>) :: (Category k, Oks k [a,b,a',b'])
=> (a' `k` a) -> (b `k` b') -> ((a `k` b) -> (a' `k` b'))
f ~> h = h <~ f
instance Category (->) where
id = P.id
(.) = (P..)
instance Monad m => Category (Kleisli m) where
id = pack return
(.) = inNew2 (<=<)
instance Category (:~:) where
id = Refl
(.) = flip Eq.trans
instance Category Coercion where
id = Coercion
(.) = flip Co.trans
instance Category U2 where
id = U2
U2 . U2 = U2
instance Monoid m => Category (Monoid2 m) where
id = Monoid2 mempty
Monoid2 m . Monoid2 n = Monoid2 (m `mappend` n)
instance (Category k, Category k') => Category (k :**: k') where
type Ok (k :**: k') = Ok k &+& Ok k'
id = id :**: id
(g :**: g') . (f :**: f') = g.f :**: g'.f'
PINLINER(id)
PINLINER((.))
{--------------------------------------------------------------------
Products
--------------------------------------------------------------------}
type Prod k = (:*)
infixr 3 ***, &&&
type OkProd k = OpCon (Prod k) (Ok' k)
okProd :: forall k a b. OkProd k
=> Ok' k a && Ok' k b |- Ok' k (Prod k a b)
okProd = inOp
{-# INLINE okProd #-}
class (Category k, OkProd k) => AssociativePCat k where
lassocP :: forall a b c. Ok3 k a b c
=> Prod k a (Prod k b c) `k` Prod k (Prod k a b) c
default lassocP :: forall a b c. (MProductCat k, Ok3 k a b c)
=> Prod k a (Prod k b c) `k` Prod k (Prod k a b) c
lassocP = second exl &&& (exr . exr)
<+ okProd @k @a @b
<+ inOpR' @(Prod k) @(Ok' k) @a @b @c
{-# INLINE lassocP #-}
rassocP :: forall a b c. Ok3 k a b c
=> Prod k (Prod k a b) c `k` Prod k a (Prod k b c)
default rassocP :: forall a b c. (MProductCat k, Ok3 k a b c)
=> Prod k (Prod k a b) c `k` Prod k a (Prod k b c)
rassocP = (exl . exl) &&& first exr
<+ okProd @k @b @c
<+ inOpL' @(Prod k) @(Ok' k) @a @b @c
{-# INLINE rassocP #-}
-- | Category with monoidal product.
class (Category k, OkProd k) => MonoidalPCat k where
(***) :: forall a b c d. Ok4 k a b c d
=> (a `k` c) -> (b `k` d) -> (Prod k a b `k` Prod k c d)
first :: forall a a' b. Ok3 k a b a'
=> (a `k` a') -> (Prod k a b `k` Prod k a' b)
first = (*** id)
{-# INLINE first #-}
second :: forall a b b'. Ok3 k a b b'
=> (b `k` b') -> (Prod k a b `k` Prod k a b')
second = (id ***)
{-# INLINE second #-}
-- | Braided monoidal category
class (Category k, OkProd k {- , MonoidalPCat k -}) => BraidedPCat k where
swapP :: forall a b. Ok2 k a b => Prod k a b `k` Prod k b a
default swapP :: forall a b. (ProductCat k, MonoidalPCat k, Ok2 k a b)
=> Prod k a b `k` Prod k b a
swapP = exr &&& exl
<+ okProd @k @a @b
{-# INLINE swapP #-}
type MBraidedPCat k = (BraidedPCat k, MonoidalPCat k)
-- | Category with product.
class (Category k, OkProd k) => ProductCat k where
exl :: Ok2 k a b => Prod k a b `k` a
exr :: Ok2 k a b => Prod k a b `k` b
dup :: Ok k a => a `k` Prod k a a
(&&&) :: forall k a c d. (MProductCat k, Ok3 k a c d)
=> (a `k` c) -> (a `k` d) -> (a `k` Prod k c d)
f &&& g = (f *** g) . dup
<+ okProd @k @a @a
<+ okProd @k @c @d
{-# INLINE (&&&) #-}
type MProductCat k = (ProductCat k, MonoidalPCat k)
instance AssociativePCat (->) where
lassocP = \ (a,(b,c)) -> ((a,b),c)
rassocP = \ ((a,b),c) -> (a,(b,c))
instance MonoidalPCat (->) where
(***) = (A.***)
first = A.first
second = A.second
instance BraidedPCat (->) where
swapP = \ (a,b) -> (b,a)
instance ProductCat (->) where
-- type Prod (->) = (:*)
exl = fst
exr = snd
dup = \ a -> (a,a)
-- TODO: do we want inline for (&&&), (***), first, and second?
instance MonoidalPCat U2 where
U2 *** U2 = U2
PINLINER((***))
instance BraidedPCat U2 where
swapP = U2
PINLINER(swapP)
instance ProductCat U2 where
exl = U2
exr = U2
dup = U2
-- U2 &&& U2 = U2
PINLINER(exl)
PINLINER(exr)
PINLINER(dup)
-- PINLINER((&&&))
instance (AssociativePCat k, AssociativePCat k') => AssociativePCat (k :**: k') where
lassocP = lassocP :**: lassocP
rassocP = rassocP :**: rassocP
PINLINER(lassocP)
PINLINER(rassocP)
instance (MonoidalPCat k, MonoidalPCat k') => MonoidalPCat (k :**: k') where
(f :**: f') *** (g :**: g') = (f *** g) :**: (f' *** g')
first (f :**: f') = first f :**: first f'
second (f :**: f') = second f :**: second f'
PINLINER((***))
PINLINER(first)
PINLINER(second)
instance (BraidedPCat k, BraidedPCat k') => BraidedPCat (k :**: k') where
swapP = swapP :**: swapP
PINLINER(swapP)
instance (ProductCat k, ProductCat k') => ProductCat (k :**: k') where
exl = exl :**: exl
exr = exr :**: exr
dup = dup :**: dup
PINLINER(exl)
PINLINER(exr)
PINLINER(dup)
instance Monad m => MonoidalPCat (Kleisli m) where
(***) = (A.***)
PINLINER((***))
instance Monad m => BraidedPCat (Kleisli m) where
swapP = arr swapP
instance Monad m => ProductCat (Kleisli m) where
-- type Prod (Kleisli m) = (:*)
exl = arr exl
exr = arr exr
dup = arr dup
PINLINER(exl)
PINLINER(exr)
PINLINER(dup)
{--------------------------------------------------------------------
Coproducts
--------------------------------------------------------------------}
type Coprod k = (:+)
type OkCoprod k = OpCon (Coprod k) (Ok' k)
okCoprod :: forall k a b. OkCoprod k
=> Ok' k a && Ok' k b |- Ok' k (Coprod k a b)
okCoprod = inOp
{-# INLINE okCoprod #-}
infixr 2 +++, |||
class (Category k, OkCoprod k) => AssociativeSCat k where
lassocS :: forall a b c. Oks k [a,b,c]
=> Coprod k a (Coprod k b c) `k` Coprod k (Coprod k a b) c
default lassocS :: forall a b c. (MCoproductCat k, Oks k [a,b,c])
=> Coprod k a (Coprod k b c) `k` Coprod k (Coprod k a b) c
lassocS = inl.inl ||| (inl.inr ||| inr)
<+ inOpL' @(Coprod k) @(Ok' k) @a @b @c
<+ okCoprod @k @b @c
{-# INLINE lassocS #-}
rassocS :: forall a b c. Oks k [a,b,c]
=> Coprod k (Coprod k a b) c `k` Coprod k a (Coprod k b c)
default rassocS :: forall a b c. (MCoproductCat k, Oks k [a,b,c])
=> Coprod k (Coprod k a b) c `k` Coprod k a (Coprod k b c)
rassocS = (inl ||| inr.inl) ||| inr.inr
<+ inOpR' @(Coprod k) @(Ok' k) @a @b @c
<+ okCoprod @k @a @b
{-# INLINE rassocS #-}
class (Category k, OkCoprod k) => BraidedSCat k where
swapS :: forall a b. Ok2 k a b => Coprod k a b `k` Coprod k b a
default swapS :: forall a b. (MCoproductCat k, Ok2 k a b) => Coprod k a b `k` Coprod k b a
swapS = inr ||| inl <+ okCoprod @k @b @a
{-# INLINE swapS #-}
-- Monoidal category over sums
class (OkCoprod k, Category k) => MonoidalSCat k where
(+++) :: forall a b c d. Ok4 k a b c d
=> (c `k` a) -> (d `k` b) -> (Coprod k c d `k` Coprod k a b)
left :: forall a a' b. Oks k [a,b,a']
=> (a `k` a') -> (Coprod k a b `k` Coprod k a' b)
left = (+++ id)
{-# INLINE left #-}
right :: forall a b b'. Oks k [a,b,b']
=> (b `k` b') -> (Coprod k a b `k` Coprod k a b')
right = (id +++)
{-# INLINE right #-}
-- | Category with coproduct.
class (Category k, OkCoprod k) => CoproductCat k where
-- type Coprod k :: u -> u -> u
-- type Coprod k = (:+)
inl :: Ok2 k a b => a `k` Coprod k a b
inr :: Ok2 k a b => b `k` Coprod k a b
jam :: Ok k a => Coprod k a a `k` a
type MCoproductCat k = (CoproductCat k, MonoidalSCat k)
(|||) :: forall k a c d. (MCoproductCat k, Ok3 k a c d)
=> (c `k` a) -> (d `k` a) -> (Coprod k c d `k` a)
f ||| g = jam . (f +++ g)
<+ okCoprod @k @a @a
<+ okCoprod @k @c @d
{-# INLINE (|||) #-}
instance AssociativeSCat (->)
instance MonoidalSCat (->) where
(+++) = (A.+++)
left = A.left
right = A.right
instance BraidedSCat (->) where
swapS = Right ||| Left
instance CoproductCat (->) where
-- type Coprod (->) = (:+)
inl = Left
inr = Right
jam = id `either` id
-- TODO: do we want inline for (|||), (+++), left, and right?
instance Monad m => MonoidalSCat (Kleisli m) where
(+++) = inNew2 (\ f g -> (fmap Left ||| fmap Right) . (f +++ g))
-- f :: a -> m c
-- g :: b -> m d
-- f +++ g :: a :+ b -> m c :+ m d
-- fmap Left ||| fmap Right :: m c :+ m d -> m (c :+ d)
instance Monad m => BraidedSCat (Kleisli m) where
swapS = arr swapS
instance Monad m => CoproductCat (Kleisli m) where
inl = arr inl
inr = arr inr
jam = arr jam
-- f :: a -> m c
-- g :: b -> m c
-- h :: a :+ b -> m c
-- want :: a -> m (a :+ b)
instance MonoidalSCat U2 where
U2 +++ U2 = U2
instance BraidedSCat U2 where swapS = U2
instance (BraidedSCat k, BraidedSCat k') => BraidedSCat (k :**: k') where
swapS = swapS :**: swapS
PINLINER(swapS)
instance CoproductCat U2 where
inl = U2
inr = U2
jam = U2
instance (AssociativeSCat k, AssociativeSCat k') => AssociativeSCat (k :**: k') where
lassocS = lassocS :**: lassocS
rassocS = rassocS :**: rassocS
PINLINER(lassocS)
PINLINER(rassocS)
instance (MonoidalSCat k, MonoidalSCat k') => MonoidalSCat (k :**: k') where
(f :**: f') +++ (g :**: g') = (f +++ g) :**: (f' +++ g')
left (f :**: f') = left f :**: left f'
right (f :**: f') = right f :**: right f'
PINLINER((+++))
PINLINER(left)
PINLINER(right)
instance (CoproductCat k, CoproductCat k') => CoproductCat (k :**: k') where
inl = inl :**: inl
inr = inr :**: inr
jam = jam :**: jam
PINLINER(inl)
PINLINER(inr)
PINLINER(jam)
{--------------------------------------------------------------------
Abelian categories
--------------------------------------------------------------------}
#if 1
type AbelianCat k =
(MProductCat k, CoproductPCat k, TerminalCat k, CoterminalCat k, OkAdd k)
zeroC :: (AbelianCat k, Ok2 k a b) => a `k` b
zeroC = ti . it
plusC :: forall k a b. (AbelianCat k, Ok2 k a b) => Binop (a `k` b)
f `plusC` g = jamP . (f *** g) . dup
<+ okProd @k @b @b
<+ okProd @k @a @a
<+ okAdd @k @b
#else
class AbelianCat k where
zeroC :: forall a b. (Ok2 k a b, Additive b) => a `k` b
plusC :: forall a b. (Ok2 k a b, Additive b) => Binop (a `k` b)
default plusC :: forall a b. (Ok2 k a b, Additive b, ProductCat k, CoproductPCat k)
=> Binop (a `k` b)
-- Two reasonable defaults
#if 1
f `plusC` g = (f |||| g) . dup <+ okProd @k @a @a
#else
f `plusC` g = jamP . (f &&& g) <+ okProd @k @b @b
#endif
-- TODO: probably remove the Additive constraints here, but use OkAdd k in the
-- plusC default.
instance AbelianCat U2 where
zeroC = U2
U2 `plusC` U2 = U2
instance (AbelianCat k, AbelianCat k') => AbelianCat (k :**: k') where
zeroC = zeroC :**: zeroC
(f :**: f') `plusC` (g :**: g') = (f `plusC` g) :**: (f' `plusC` g')
PINLINER(zeroC)
PINLINER(plusC)
-- TODO: relate AbelianCat to ProductCat and CoproductPCat.
-- Also to IxProductCat and IxCoproductPCat.
#endif
{--------------------------------------------------------------------
A dual to ProductCat. Temporary workaround.
--------------------------------------------------------------------}
-- TODO: eliminate CoproductPCat in favor of when we have associated products,
-- coproducts, etc.
type CoprodP k = Prod k
type OkCoprodP k = OkProd k
okCoprodP :: forall k a b. OkCoprodP k
=> Ok' k a && Ok' k b |- Ok' k (CoprodP k a b)
okCoprodP = inOp
{-# INLINE okCoprodP #-}
-- | Category with coproduct as Cartesian product.
class BraidedPCat k => CoproductPCat k where
inlP :: Ok2 k a b => a `k` CoprodP k a b
inrP :: Ok2 k a b => b `k` CoprodP k a b
jamP :: Ok k a => CoprodP k a a `k` a
type MCoproductPCat k = (CoproductPCat k, MonoidalPCat k)
infixr 2 ||||
(||||) :: forall k a c d. (MCoproductPCat k, Ok3 k a c d)
=> (c `k` a) -> (d `k` a) -> (CoprodP k c d `k` a)
f |||| g = jamP . (f *** g)
<+ okCoprodP @k @a @a
<+ okCoprodP @k @c @d
{-# INLINE (||||) #-}
-- Don't bother with left, right, lassocS, rassocS, and misc helpers.
instance CoproductPCat U2 where
inlP = U2
inrP = U2
jamP = U2
instance (CoproductPCat k, CoproductPCat k') => CoproductPCat (k :**: k') where
inlP = inlP :**: inlP
inrP = inrP :**: inrP
jamP = jamP :**: jamP
PINLINER(inlP)
PINLINER(inrP)
PINLINER(jamP)
-- Scalar multiplication
class ScalarCat k a where
scale :: a -> (a `k` a)
instance Num a => ScalarCat (->) a where
scale = (*) -- I don't think I want to inline (*)
PINLINER(scale)
instance ScalarCat U2 a where
scale = const U2
instance (ScalarCat k a, ScalarCat k' a) => ScalarCat (k :**: k') a where
scale s = scale s :**: scale s
PINLINER(scale)
type LinearCat k a = (MProductCat k, CoproductPCat k, ScalarCat k a, Ok k a)
{--------------------------------------------------------------------
Distributive
--------------------------------------------------------------------}
class DistribCat k where
distl :: forall a u v. Ok3 k a u v
=> Prod k a (Coprod k u v) `k` Coprod k (Prod k a u) (Prod k a v)
distr :: forall u v b. Ok3 k u v b
=> Prod k (Coprod k u v) b `k` Coprod k (Prod k u b) (Prod k v b)
default distl :: forall a u v. (MonoidalSCat k, BraidedPCat k, Ok3 k a u v)
=> Prod k a (Coprod k u v) `k` Coprod k (Prod k a u) (Prod k a v)
distl = (swapP +++ swapP) . distr . swapP
<+ okProd @k @(Coprod k u v) @a
<+ okCoprod @k @(Prod k u a) @(Prod k v a)
<+ okProd @k @u @a
<+ okProd @k @v @a
<+ okCoprod @k @(Prod k a u) @(Prod k a v)
<+ okProd @k @a @u
<+ okProd @k @a @v
<+ okProd @k @a @(Coprod k u v)
<+ okCoprod @k @u @v
{-# INLINE distl #-}
default distr :: forall u v b. (MonoidalSCat k, BraidedPCat k, Ok3 k u v b)
=> Prod k (Coprod k u v) b `k` Coprod k (Prod k u b) (Prod k v b)
distr = (swapP +++ swapP) . distl . swapP
<+ okProd @k @b @(Coprod k u v)
<+ okCoprod @k @(Prod k b u) @(Prod k b v)
<+ okProd @k @b @u
<+ okProd @k @b @v
<+ okCoprod @k @(Prod k u b) @(Prod k v b)
<+ okProd @k @u @b
<+ okProd @k @v @b
<+ okProd @k @(Coprod k u v) @b
<+ okCoprod @k @u @v
{-# INLINE distr #-}
{-# MINIMAL distl | distr #-}
-- instance DistribCat (->) where
-- distl (a,uv) = ((a,) +++ (a,)) uv
-- distr (uv,b) = ((,b) +++ (,b)) uv
instance DistribCat (->) where
distl (a,Left u) = Left (a,u)
distl (a,Right v) = Right (a,v)
distr (Left u,b) = Left (u,b)
distr (Right v,b) = Right (v,b)
instance DistribCat U2 where
distl = U2
distr = U2
instance (DistribCat k, DistribCat k') => DistribCat (k :**: k') where
distl = distl :**: distl
distr = distr :**: distr
PINLINER(distl)
PINLINER(distr)
{--------------------------------------------------------------------
Exponentials
--------------------------------------------------------------------}
type OkExp k = OpCon (Exp k) (Ok' k)
okExp :: forall k a b. OkExp k
=> Ok' k a && Ok' k b |- Ok' k (Exp k a b)
okExp = inOp
{-# INLINE okExp #-}
-- #define ExpAsCat
#ifdef ExpAsCat
type Exp k = k
#else
type Exp k = (->)
#endif
class (OkExp k, ProductCat k) => ClosedCat k where
-- type Exp k :: u -> u -> u
apply :: forall a b. Ok2 k a b => Prod k (Exp k a b) a `k` b
apply = uncurry id
<+ okExp @k @a @b
{-# INLINE apply #-}
curry :: Ok3 k a b c => (Prod k a b `k` c) -> (a `k` Exp k b c)
uncurry :: forall a b c. Ok3 k a b c
=> (a `k` Exp k b c) -> (Prod k a b `k` c)
default uncurry :: forall a b c. (MonoidalPCat k, Ok3 k a b c)
=> (a `k` Exp k b c) -> (Prod k a b `k` c)
uncurry g = apply . first g
<+ okProd @k @(Exp k b c) @b
<+ okProd @k @a @b
<+ okExp @k @b @c
{-# INLINE uncurry #-}
{-# MINIMAL curry, (apply | uncurry) #-}
-- apply :: (Ok2 k a b, p ~ Prod k, e ~ Exp k) => ((a `e` b) `p` a) `k` b
instance ClosedCat (->) where
-- type Exp (->) = (->)
apply = P.uncurry ($)
curry = P.curry
uncurry = P.uncurry
-- TODO: do we want inline for apply, curry, and uncurry?
applyK :: Kleisli m (Kleisli m a b :* a) b
curryK :: Monad m => Kleisli m (a :* b) c -> Kleisli m a (Kleisli m b c)
uncurryK :: Monad m => Kleisli m a (Kleisli m b c) -> Kleisli m (a :* b) c
applyK = pack (apply . first unpack)
curryK = inNew $ \ h -> return . pack . curry h
uncurryK = inNew $ \ f -> \ (a,b) -> f a >>= ($ b) . unpack
#if 0
instance Monad m => ClosedCat (Kleisli m) where
-- type Exp (Kleisli m) = Kleisli m
apply = applyK
curry = curryK
uncurry = uncurryK
#endif
instance ClosedCat U2 where
apply = U2
curry U2 = U2
uncurry U2 = U2
#ifdef ExpAsCat
instance (ClosedCat k, ClosedCat k') => ClosedCat (k :**: k') where
apply = (apply . first exl) :**: undefined
-- apply = (apply . first exl) :**: (apply . first exr)
-- apply = (apply . exl) :**: (apply . exr)
-- apply :: forall a b. (Ok2 k a b, Ok2 k' a b)
-- => (k :**: k') ((k :**: k') a b :* a) b
-- apply = undefined -- (apply . exl) :**: _
curry (f :**: f') = curry f :**: curry f'
uncurry (g :**: g') = uncurry g :**: uncurry g'
PINLINER(apply)
PINLINER(curry)
PINLINER(uncurry)
#else
instance (ClosedCat k, ClosedCat k') => ClosedCat (k :**: k') where
apply = apply :**: apply
-- apply = (apply . exl) :**: (apply . exr)
-- apply :: forall a b. (Ok2 k a b, Ok2 k' a b)
-- => (k :**: k') ((k :**: k') a b :* a) b
-- apply = undefined -- (apply . exl) :**: _
curry (f :**: f') = curry f :**: curry f'
uncurry (g :**: g') = uncurry g :**: uncurry g'
PINLINER(apply)
PINLINER(curry)
PINLINER(uncurry)
#endif
-- An alternative to ClosedCat
class OkExp k => FlipCat k where
flipC :: Ok3 k a b c => (a `k` (b -> c)) -> (b -> (a `k` c))
flipC' :: Ok3 k a b c => (b -> (a `k` c)) -> (a `k` (b -> c))
instance FlipCat (->) where
flipC = flip
flipC' = flip
-- TODO: inline?
instance FlipCat U2 where
flipC U2 = const U2
flipC' _ = U2
instance (FlipCat k, FlipCat k') => FlipCat (k :**: k') where
flipC (f :**: f') b = flipC f b :**: flipC f' b
flipC' h = flipC' (exl2 . h) :**: flipC' (exr2 . h)
-- Hm. The use of exl2 and exr2 here suggest replication of effort
-- h :: b -> (k :**: k') a c
-- exl2 . h :: b -> a `k` c
-- flipC' (exl2 . h) :: b -> a `k` c
type Unit k = ()
type OkUnit k = Ok k (Unit k)
class OkUnit k => TerminalCat k where
-- type Unit k :: u
it :: Ok k a => a `k` Unit k
default it :: (ConstCat k (Unit k), Ok k a) => a `k` Unit k
it = const ()
{-# INLINE it #-}
-- TODO: add default it = const () when ConstCat k, and then remove instances
-- that were using this definition explicitly.
instance TerminalCat (->) where
-- type Unit (->) = ()
it = const ()
instance Monad m => TerminalCat (Kleisli m) where
-- type Unit (Kleisli m) = ()
it = arr it
instance TerminalCat U2 where
it = U2
instance (TerminalCat k, TerminalCat k') => TerminalCat (k :**: k') where
it = it :**: it
PINLINER(it)
class OkUnit k => UnitCat k where
lunit :: Ok k a => a `k` Prod k (Unit k) a
default lunit :: (MProductCat k, TerminalCat k, Ok k a) => a `k` Prod k (Unit k) a
lunit = it &&& id
lcounit :: Ok k a => Prod k (Unit k) a `k` a
default lcounit :: (ProductCat k, Ok k a) => Prod k (Unit k) a `k` a
lcounit = exr
runit :: Ok k a => a `k` Prod k a (Unit k)
default runit :: (MProductCat k, TerminalCat k, Ok k a) => a `k` Prod k a (Unit k)
runit = id &&& it
rcounit :: Ok k a => Prod k a (Unit k) `k` a
default rcounit :: (ProductCat k, TerminalCat k, Ok k a) => Prod k a (Unit k) `k` a
rcounit = exl
PINLINER(lunit)
PINLINER(runit)