-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHierarchy.hs
1231 lines (1190 loc) · 48.7 KB
/
Hierarchy.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 ApplicativeDo #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TupleSections #-}
-- | Defines various mappings between categorical representations and the plugin, allowing us to
-- support transformations against different type class hierarchies.
module Categorifier.Hierarchy
( First (..),
Last,
pattern Last,
getLast,
HaskOps (..),
Hierarchy (..),
-- * concrete hierarchies
baseHierarchy,
emptyHierarchy,
concatOps,
-- * building hierarchies
findDataCon,
findId,
findTHName,
findTyCon,
identifier,
nameFromText,
mkFunctionApps,
mkMethodApps,
mkMethodApps',
-- * lookup of identifier info from @base@
--
-- Many things are not built into the compiler, but sometimes (especially in connection with
-- @baseMakers@ / `Categorifier.Core.PrimOp.replace`), we need to recognize these things. The
-- `Lookup` monad run outside the call to `Categorifier.Core.Categorify.categorify` is where we
-- can query the compiler for this information.
BaseIdentifiers (..),
getBaseIdentifiers,
-- ** Fixed-size integer constructors
IntConstructor (..),
getIntegerConstructors,
intConstructorToOpTyPair,
intConstructorToBoxer,
-- ** `GHC.Base.getTag`
GetTagInfo (..),
getGetTagInfo,
)
where
import qualified Categorifier.Category
import qualified Categorifier.Client
import qualified Categorifier.Core.Base
import qualified Categorifier.Core.Functions
import Categorifier.Core.Types (CategoryStack, Lookup, MissingSymbol (..))
import Categorifier.Duoidal (Parallel (..))
import qualified Categorifier.GHC.Builtin as Plugins
import Categorifier.GHC.Core (CoreExpr, CoreM, Type)
import qualified Categorifier.GHC.Core as Plugins
import qualified Categorifier.GHC.Data as Plugins
import qualified Categorifier.GHC.Plugins as Plugins (thNameToGhcName)
import qualified Categorifier.GHC.Runtime as Plugins
import qualified Categorifier.GHC.Types as Plugins
import qualified Categorifier.GHC.Unit as Plugins
import Control.Applicative (Alternative (..))
import qualified Control.Applicative
import qualified Control.Arrow
import qualified Control.Category
import Control.Monad ((<=<))
import qualified Control.Monad
import Control.Monad.Trans.Except (ExceptT (..))
import Data.Bifunctor (bimap)
import Data.Bits (FiniteBits (..))
import qualified Data.Bool
import qualified Data.Either
import qualified Data.Foldable
import qualified Data.Function
import Data.Functor.Identity (Identity (..))
import Data.Maybe (fromMaybe)
import Data.Monoid (Dual (..))
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Tuple
import qualified GHC.Base
import qualified GHC.Classes
import qualified GHC.Err
import qualified GHC.Float
import qualified GHC.Int
import qualified GHC.Real
import qualified GHC.Word
import qualified Language.Haskell.TH as TH
import qualified Numeric
import qualified Unsafe.Coerce
import Prelude hiding (mod)
-- | These are operations that we need to use in __Hask__, rather than in the target category.
data HaskOps f = HaskOps
{ -- | @forall a. Rep a -> a@
abstH :: (CoreExpr -> f CoreExpr) -> Type -> f CoreExpr,
-- | @forall a b c. (a -> c) -> (b -> c) -> Either a b -> c@
eitherH :: Type -> Type -> Type -> CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr,
-- | @forall cat i a b. i -> (a -> b) -> cat a b@
ffcallH ::
(CoreExpr -> f CoreExpr) ->
Type ->
Type ->
Type ->
Type ->
CoreExpr ->
CoreExpr ->
f CoreExpr,
-- | @forall a. (a -> a) -> a@
fixH :: Type -> CoreExpr -> CoreExpr,
-- | @forall a b. (a, b) -> a@
fstH :: Type -> Type -> CoreExpr -> CoreExpr,
-- | @forall cat a b. String -> cat a b -> cat a b@
indirectionH ::
(CoreExpr -> f CoreExpr) ->
Type ->
Type ->
Type ->
CoreExpr ->
f CoreExpr,
-- | @forall a. a -> Rep a@
reprH :: (CoreExpr -> f CoreExpr) -> Type -> f CoreExpr,
-- | @forall a b. (a, b) -> b@
sndH :: Type -> Type -> CoreExpr -> CoreExpr,
-- | @forall a b c. ((a, b) -> c) -> a -> b -> c@
curryH :: Type -> Type -> Type -> CoreExpr -> CoreExpr
}
concatOps :: (Monad f) => Lookup (HaskOps f)
concatOps = do
abstH <- repOp 'Categorifier.Core.Functions.abst
eitherH <- do
op <- identifier 'Data.Either.either
pure (\a b c f g e -> runIdentity $ mkFunctionApps Identity op [a, c, b] [f, g, e])
ffcallH <- do
op <- identifier 'Categorifier.Category.ffcall
pure $ \onDict cat ity a b i f ->
mkMethodApps onDict op [cat, ity, a, b] [] [i, f]
fixH <- do
op <- identifier 'Data.Function.fix
pure (\t e -> runIdentity $ mkFunctionApps Identity op [t] [e])
fstH <- do
op <- identifier 'Data.Tuple.fst
pure (\a b e -> runIdentity $ mkFunctionApps Identity op [a, b] [e])
indirectionH <- do
op <- identifier 'Categorifier.Category.indirection
pure $ \onDict cat a b s ->
mkMethodApps onDict op [Plugins.typeKind a, Plugins.typeKind b, cat, a, b] [] [s]
reprH <- repOp 'Categorifier.Core.Functions.repr
sndH <- do
op <- identifier 'Data.Tuple.snd
pure (\a b e -> runIdentity $ mkFunctionApps Identity op [a, b] [e])
curryH <- do
op <- identifier 'Data.Tuple.curry
pure (\a b c e -> runIdentity $ mkFunctionApps Identity op [a, b, c] [e])
pure HaskOps {..}
where
repOp name = do
op <- identifier name
pure $ \onDict a -> mkFunctionApps onDict op [a] []
-- | This structure relates categorical concepts to operations in a particular library. Each field
-- can be either `Nothing` (if there is no way to model that operation in the library) or some
-- function that expects the correct set of type parameters. The resulting expression should match
-- the type of the operation in the comment for that field (with the type arguments fully
-- applied).
--
-- __TODO__: We should perhaps have the fields return @`Categorifier.Core.Makers.CategoryStack`
-- `CoreExpr`@ so implementations can fail to handle some cases.
data Hierarchy f = Hierarchy
{ -- | @forall cat a. cat a a@
absV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Rep a) a@
abstCV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
acosV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
acoshV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat. cat (Prod Bool Bool) Bool@
andV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> f CoreExpr),
-- | @forall cat f a b. cat (Prod (f (Exp a b)) (f a)) (f b)@
apV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Semigroup a. cat (Prod a a) a@
appendV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat (Prod (Exp a b) a) b@
applyV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat x a b -> cat x (a -> b) -> cat x a -> cat x b@
apply2V ::
Maybe
( (CoreExpr -> f CoreExpr) ->
Type ->
Type ->
Type ->
Type ->
CoreExpr ->
CoreExpr ->
f CoreExpr
),
-- | @forall cat a. cat (Prod a a) a@
arctan2V :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
asinV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
asinhV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
atanV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
atanhV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat m a b. cat (Prod (m a) (Exp a (m b)) (m b)@
bindV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat a b@
bottomV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat from to. cat from to@
coerceV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Ordering@
compareV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b c. cat b c -> cat a b -> cat a c@
composeV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat x b c a. cat x (b -> c) -> cat x (a -> b) -> cat x (a -> c)@
compose2V ::
Maybe
( (CoreExpr -> f CoreExpr) ->
Type ->
Type ->
Type ->
Type ->
Type ->
CoreExpr ->
CoreExpr ->
f CoreExpr
),
-- | @forall cat a b. b -> cat a b@
constV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. b -> cat a b@
constraintV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
cosV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
coshV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a1 a2 b. cat (Prod a1 a2) b -> cat a1 (Exp a2 b)@
curryV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b c. cat (Prod a (Coprod b c)) (Coprod (Prod a b) (Prod a c)) @
distlV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
divV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
divideV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat. cat Double Float@
doubleToFloatV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
equalV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
evenV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat (Prod a b) a@
exlV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
expV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat (Prod a b) b@
exrV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a x. cat (Prod a x) x -> cat a x@
fixV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat. cat Float Double@
floatToDoubleV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
fmodV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b1 b2. cat a b1 -> cat a b2 -> cat a (Prod b1 b2)@
forkV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
fpIsNegativeZeroV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
fpIsInfiniteV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
fpIsFiniteV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
fpIsNaNV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
fpIsDenormalV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat Integer a@
--
-- __TODO__: This is simply a specialization of `fromIntegralV`, but it's difficult to look up
-- type constructors outside of this context, so we make it part of the hierarchy,
-- but we shouldn't have to.
fromIntegerV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat a b@
fromIntegralV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
geV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
gtV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
idV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod Bool (Prod a a)) a@
ifV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat a (Coprod a b)@
inlV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat b (Coprod a b)@
inrV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a1 a2 b. cat a1 b -> cat a2 b -> cat (Coprod a1 a2) c@
joinV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b c. cat (Prod a (Prod b c)) (Prod (Prod a b) c)@
lassocV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
leV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a b c. cat (Prod a b) c -> cat (Prod (f a) (f b)) (f c)@
liftA2V ::
Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
logV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
ltV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat cat' f a b. cat a b -> cat' (f a) (f b)@
--
-- __NB__: This is not necessarily an endofunctor. It expects /two/ categories, which may
-- differ.
mapV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Ord a. cat (Prod a a) a@
maxV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat (f a) a@
maximumV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Ord a. cat (Prod a a) a@
minV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat (f a) a@
minimumV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Semiring a. cat (Prod a a) a@
minusV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
modV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat tag a b. cat a b@
nativeV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Semiring a. cat a a@
negateV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat (f a) (Exp (Rep f) a)@
indexV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat (Exp (Rep f) a) (f a)@
tabulateV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat. cat Bool Bool@
notV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) Bool@
notEqualV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a Bool@
oddV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat. cat (Prod Bool Bool) Bool@
orV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> f CoreExpr),
-- | @forall cat a. Semiring a. cat (Prod a a) a@
plusV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat a (f a)
pointV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
powV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a i. i -> cat a a@
powIV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> CoreExpr -> f CoreExpr),
-- | @forall cat a. cat (a, Int) a@
powIntV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
quotV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a b c. cat (Prod (Prod a b) c) (Prod a (Prod b c))@
rassocV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat a b@
realToFracV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
recipV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat (Prod a a) a@
remV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a (Rep a) @
reprCV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat t f a. cat (t (f a)) (f (t a))
sequenceAV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
signumV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
sinV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
sinhV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
sqrtV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat f a b. cat (Prod a (f b)) (f (Prod a b))@
strengthV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat f a. cat (f a) a@
sumV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a b. cat (Prod a b) (Prod b a)@
swapV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
tanV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. cat a a@
tanhV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat a. Semiring a. cat (Prod a a) a@
timesV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> f CoreExpr),
-- | @forall cat t f a b. cat a (f b) -> cat (t a) (f (t b))@
traverseV ::
Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> Type -> f CoreExpr),
-- | @forall cat a1 a2 b. cat a1 (Exp a2 b) -> cat (Prod a1 a2) b@
uncurryV :: Maybe ((CoreExpr -> f CoreExpr) -> Type -> Type -> Type -> Type -> f CoreExpr)
}
-- | Like `Data.Monoid.First`, but more general, as it isn't restricted to `Maybe`.
newtype First a = First {getFirst :: a}
-- | This default instance /always/ chooses the first argument. If your @a@ is a monoid (see
-- @`First` (`Maybe` a)@), or is somehow mergable (see @`First` (`Hierarchy` f)@), then it's
-- better to create an @overlapping@ instance.
instance {-# OVERLAPPABLE #-} Semigroup (First a) where
(<>) = const
instance Semigroup (First (Maybe a)) where
First a <> First b = First $ a <|> b
type Last a = Dual (First a)
pattern Last :: a -> Last a
pattern Last a = Dual (First a)
getLast :: Last a -> a
getLast = getFirst . getDual
instance Semigroup (First (Hierarchy f)) where
First a <> First b =
First $
Hierarchy
{ absV = absV a <|> absV b,
abstCV = abstCV a <|> abstCV b,
acosV = acosV a <|> acosV b,
acoshV = acoshV a <|> acoshV b,
andV = andV a <|> andV b,
apV = apV a <|> apV b,
appendV = appendV a <|> appendV b,
applyV = applyV a <|> applyV b,
apply2V = apply2V a <|> apply2V b,
arctan2V = arctan2V a <|> arctan2V b,
asinV = asinV a <|> asinV b,
asinhV = asinhV a <|> asinhV b,
atanV = atanV a <|> atanV b,
atanhV = atanhV a <|> atanhV b,
bindV = bindV a <|> bindV b,
bottomV = bottomV a <|> bottomV b,
coerceV = coerceV a <|> coerceV b,
compareV = compareV a <|> compareV b,
composeV = composeV a <|> composeV b,
compose2V = compose2V a <|> compose2V b,
constV = constV a <|> constV b,
constraintV = constraintV a <|> constraintV b,
cosV = cosV a <|> cosV b,
coshV = coshV a <|> coshV b,
curryV = curryV a <|> curryV b,
distlV = distlV a <|> distlV b,
divV = divV a <|> divV b,
divideV = divideV a <|> divideV b,
doubleToFloatV = doubleToFloatV a <|> doubleToFloatV b,
equalV = equalV a <|> equalV b,
evenV = evenV a <|> evenV b,
exlV = exlV a <|> exlV b,
expV = expV a <|> expV b,
exrV = exrV a <|> exrV b,
fixV = fixV a <|> fixV b,
floatToDoubleV = floatToDoubleV a <|> floatToDoubleV b,
fmodV = fmodV a <|> fmodV b,
forkV = forkV a <|> forkV b,
fpIsNegativeZeroV = fpIsNegativeZeroV a <|> fpIsNegativeZeroV b,
fpIsInfiniteV = fpIsInfiniteV a <|> fpIsInfiniteV b,
fpIsFiniteV = fpIsFiniteV a <|> fpIsFiniteV b,
fpIsNaNV = fpIsNaNV a <|> fpIsNaNV b,
fpIsDenormalV = fpIsDenormalV a <|> fpIsDenormalV b,
fromIntegerV = fromIntegerV a <|> fromIntegerV b,
fromIntegralV = fromIntegralV a <|> fromIntegralV b,
geV = geV a <|> geV b,
gtV = gtV a <|> gtV b,
idV = idV a <|> idV b,
ifV = ifV a <|> ifV b,
inlV = inlV a <|> inlV b,
inrV = inrV a <|> inrV b,
joinV = joinV a <|> joinV b,
lassocV = lassocV a <|> lassocV b,
leV = leV a <|> leV b,
liftA2V = liftA2V a <|> liftA2V b,
logV = logV a <|> logV b,
ltV = ltV a <|> ltV b,
mapV = mapV a <|> mapV b,
maxV = maxV a <|> maxV b,
maximumV = maximumV a <|> maximumV b,
minV = minV a <|> minV b,
minimumV = minimumV a <|> minimumV b,
minusV = minusV a <|> minusV b,
modV = modV a <|> modV b,
nativeV = nativeV a <|> nativeV b,
negateV = negateV a <|> negateV b,
indexV = indexV a <|> indexV b,
tabulateV = tabulateV a <|> tabulateV b,
notV = notV a <|> notV b,
notEqualV = notEqualV a <|> notEqualV b,
oddV = oddV a <|> oddV b,
orV = orV a <|> orV b,
plusV = plusV a <|> plusV b,
pointV = pointV a <|> pointV b,
powV = powV a <|> powV b,
powIV = powIV a <|> powIV b,
powIntV = powIntV a <|> powIntV b,
quotV = quotV a <|> quotV b,
rassocV = rassocV a <|> rassocV b,
realToFracV = realToFracV a <|> realToFracV b,
recipV = recipV a <|> recipV b,
remV = remV a <|> remV b,
reprCV = reprCV a <|> reprCV b,
sequenceAV = sequenceAV a <|> sequenceAV b,
signumV = signumV a <|> signumV b,
sinV = sinV a <|> sinV b,
sinhV = sinhV a <|> sinhV b,
sqrtV = sqrtV a <|> sqrtV b,
strengthV = strengthV a <|> strengthV b,
sumV = sumV a <|> sumV b,
swapV = swapV a <|> swapV b,
tanV = tanV a <|> tanV b,
tanhV = tanhV a <|> tanhV b,
timesV = timesV a <|> timesV b,
traverseV = traverseV a <|> traverseV b,
uncurryV = uncurryV a <|> uncurryV b
}
instance Monoid (First (Hierarchy f)) where
mempty = First emptyHierarchy
-- | If you are building a custom hierarchy, it is worth considering whether you want to populate
-- the record from `Hierarchy` or from `emptyHierarchy`. Most of the ones provided in this library
-- are built from `Hierarchy`, because it is easier to keep in sync with changes that way and we
-- can update them in lock-step. If you are defining a hierarchy within a specific application,
-- you probably want to do the same to ensure that changes in this library are caught. However, if
-- you are publishing a hierarchy in a separate library, building from `emptyHierarchy` is more
-- future-proof. As we add more operations, your library will continue to work, it just won't
-- support the new operations.
emptyHierarchy :: Hierarchy f
emptyHierarchy =
Hierarchy
{ absV = Nothing,
abstCV = Nothing,
acosV = Nothing,
acoshV = Nothing,
andV = Nothing,
apV = Nothing,
appendV = Nothing,
applyV = Nothing,
apply2V = Nothing,
arctan2V = Nothing,
asinV = Nothing,
asinhV = Nothing,
atanV = Nothing,
atanhV = Nothing,
bindV = Nothing,
bottomV = Nothing,
coerceV = Nothing,
compareV = Nothing,
composeV = Nothing,
compose2V = Nothing,
constV = Nothing,
constraintV = Nothing,
cosV = Nothing,
coshV = Nothing,
curryV = Nothing,
distlV = Nothing,
divV = Nothing,
divideV = Nothing,
doubleToFloatV = Nothing,
equalV = Nothing,
evenV = Nothing,
exlV = Nothing,
expV = Nothing,
exrV = Nothing,
fixV = Nothing,
floatToDoubleV = Nothing,
fmodV = Nothing,
forkV = Nothing,
fpIsNegativeZeroV = Nothing,
fpIsInfiniteV = Nothing,
fpIsFiniteV = Nothing,
fpIsNaNV = Nothing,
fpIsDenormalV = Nothing,
fromIntegerV = Nothing,
fromIntegralV = Nothing,
geV = Nothing,
gtV = Nothing,
idV = Nothing,
ifV = Nothing,
inlV = Nothing,
inrV = Nothing,
joinV = Nothing,
lassocV = Nothing,
leV = Nothing,
liftA2V = Nothing,
logV = Nothing,
ltV = Nothing,
mapV = Nothing,
maxV = Nothing,
maximumV = Nothing,
minV = Nothing,
minimumV = Nothing,
minusV = Nothing,
modV = Nothing,
negateV = Nothing,
indexV = Nothing,
tabulateV = Nothing,
nativeV = Nothing,
notV = Nothing,
notEqualV = Nothing,
oddV = Nothing,
orV = Nothing,
plusV = Nothing,
pointV = Nothing,
powV = Nothing,
powIV = Nothing,
powIntV = Nothing,
quotV = Nothing,
rassocV = Nothing,
realToFracV = Nothing,
recipV = Nothing,
remV = Nothing,
reprCV = Nothing,
sequenceAV = Nothing,
signumV = Nothing,
sinV = Nothing,
sinhV = Nothing,
sqrtV = Nothing,
strengthV = Nothing,
sumV = Nothing,
swapV = Nothing,
tanV = Nothing,
tanhV = Nothing,
timesV = Nothing,
traverseV = Nothing,
uncurryV = Nothing
}
lookupName ::
Plugins.ModuleName -> (String -> Plugins.OccName) -> String -> CoreM (Maybe Plugins.Name)
lookupName modu mkOcc str = do
hscEnv <- Plugins.getHscEnv
Plugins.liftIO
. fmap (fmap fst)
. Plugins.lookupRdrNameInModuleForPlugins hscEnv modu
. Plugins.Unqual
$ mkOcc str
nameFromStrings :: String -> String -> Lookup Plugins.Name
nameFromStrings modu str =
let mod = Plugins.mkModuleName modu
thName = TH.mkName $ modu <> "." <> str
in Parallel
( ExceptT
(maybe (Left . pure $ MissingName thName) pure <$> lookupName mod Plugins.mkVarOcc str)
)
-- | This is still needed for @Categorifier.Core.categorifyRules@. Apparently @thNameToGhcName@
-- can't load a @TH.Name@ made by @TH.mkName@, unless we specify the package when loading the name.
nameFromText :: Text -> Lookup Plugins.Name
nameFromText =
uncurry nameFromStrings
. bimap (Text.unpack . Text.dropWhileEnd (== '.')) Text.unpack
. Text.breakOnEnd "."
lookupRdr :: (Plugins.Name -> CoreM a) -> TH.Name -> CoreM (Maybe a)
lookupRdr mkThing = traverse mkThing <=< Plugins.thNameToGhcName
findTHName :: TH.Name -> Lookup Plugins.Name
findTHName tn =
Parallel
( ExceptT
(maybe (Left . pure $ MissingName tn) pure <$> Plugins.thNameToGhcName tn)
)
findDataCon :: TH.Name -> Lookup Plugins.DataCon
findDataCon tn =
Parallel
( ExceptT
( maybe (Left . pure $ MissingDataCon tn) pure
<$> lookupRdr Plugins.lookupDataCon tn
)
)
-- | A helper for building `Hierarchy`s, also useful for looking up other `Plugins.Id`s in the
-- appropriate context.
findId :: TH.Name -> Lookup Plugins.Id
findId tn =
Parallel
( ExceptT
( maybe (Left . pure $ MissingId tn) pure
<$> lookupRdr Plugins.lookupId tn
)
)
findTyCon :: TH.Name -> Lookup Plugins.TyCon
findTyCon tn =
Parallel
( ExceptT
( maybe (Left . pure $ MissingTyCon tn) pure
<$> lookupRdr Plugins.lookupTyCon tn
)
)
identifier :: TH.Name -> Lookup CoreExpr
identifier = fmap Plugins.Var . findId
-- | Very much like `mkMethodApps`, but as a function is not a member of a type class, there are no
-- class parameters to apply (or class dictionary to resolve).
mkFunctionApps ::
(Functor f) =>
-- | The dictionary applicator
(CoreExpr -> f CoreExpr) ->
-- | The function
CoreExpr ->
-- | The type arguments
[Type] ->
-- | The term arguments
[CoreExpr] ->
f CoreExpr
mkFunctionApps onDict fn tys terms =
fmap (`Plugins.mkCoreApps` terms) . onDict $ Plugins.mkTyApps fn tys
-- | Applies all of the arguments (types and terms) to a type class method. It's structured in a
-- particular way. E.g., given a class like
--
-- >>> class Foldable t where
-- >>> foldMap :: Monoid m => (a -> m) -> t a -> m
--
-- we need to do a few things in the right order:
-- 1. apply the type parameters in the class declaration (@t@),
-- 2. resolve the dictionary for the type class itself (@Foldable t@),
-- 3. apply the type parameters on the method (@m@ and @a@),
-- 4. resolve any constraint dictionaries on the method (@`Monoid` m@), and finally
-- 5. apply the term parameters on the method (values with types @(a -> m)@ and @t a@).
--
-- This function automatically handles all the dictionary resolution (via the @onDict@ argument),
-- but uses the split in the `Type` lists to know when to do the resolution. So, a call to
-- @foldMap@ would look something like @`mkMethodApps` onDict [t] [m, a] [fn, ta]@.
--
-- __NB__: The types as printed in places like [Haddock](https://www.haskell.org/haddock/) and by
-- `Plugins.Outputable` are misleading. They will show you things like
-- @forall t m a. (Foldable t, `Monoid` m) => (a -> m) -> t a -> m@, which make
-- it /look/ like all the dictionary resolution happens after @t@, @m@, and @a@ are
-- applied. However, doing that can cause the plugin to segfault. Something like
-- @forall t. Foldable t => forall m a. `Monoid` m => (a -> m) -> t a -> m@ would be more
-- honest, but not what we currently get.
mkMethodApps ::
(Monad f) =>
-- | The dictionary applicator
(CoreExpr -> f CoreExpr) ->
-- | The type class method
CoreExpr ->
-- | The type arguments for the type class itself
[Type] ->
-- | The type arguments on the method
[Type] ->
-- | The term arguments
[CoreExpr] ->
f CoreExpr
mkMethodApps onDict = mkMethodApps' onDict onDict
-- | Like `mkMethodApps`, but takes separate dictionary applicators for class constraints
-- and method constraints.
mkMethodApps' ::
(Monad f) =>
-- | The dictionary applicator for class constraints
(CoreExpr -> f CoreExpr) ->
-- | The dictionary applicator for method constraints
(CoreExpr -> f CoreExpr) ->
-- | The type class method
CoreExpr ->
-- | The type arguments for the type class itself
[Type] ->
-- | The type arguments on the method
[Type] ->
-- | The term arguments
[CoreExpr] ->
f CoreExpr
mkMethodApps' onDictCls onDictMeth fn tc tys terms = do
method <- onDictCls $ Plugins.mkTyApps fn tc
mkFunctionApps onDictMeth method tys terms
-- | A hierarchy using only type classes available in @base@.
--
-- __TODO__: This could generalize `CategoryStack` to @`Monad` f => f@, but can't currently
-- dynamically load a parameterized type successfully.
baseHierarchy :: Lookup (Hierarchy CategoryStack)
baseHierarchy = do
absV <- closedUnaryOp 'Prelude.abs
abstCV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
rep <- findTyCon ''Categorifier.Client.Rep
op <- identifier 'Categorifier.Client.abst
pure $ \onDict cat a ->
mkMethodApps onDict arr [cat] [Plugins.mkTyConApp rep [a], a] . pure
=<< mkMethodApps onDict op [a] [] []
acosV <- closedUnaryOp 'Numeric.acos
acoshV <- closedUnaryOp 'Numeric.acosh
andV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
uncur <- identifier 'Data.Tuple.uncurry
op <- identifier '(GHC.Classes.&&)
pure
( \onDict cat -> do
op' <- mkFunctionApps onDict op [] []
uncur' <-
mkFunctionApps onDict uncur [Plugins.boolTy, Plugins.boolTy, Plugins.boolTy] [op']
mkMethodApps
onDict
arr
[cat]
[Plugins.mkBoxedTupleTy [Plugins.boolTy, Plugins.boolTy], Plugins.boolTy]
[uncur']
)
apV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
uncur <- identifier 'Data.Tuple.uncurry
op <- identifier '(Control.Applicative.<*>)
pure
( \onDict cat f a b -> do
let fa = Plugins.mkAppTy f a
fb = Plugins.mkAppTy f b
ffun = Plugins.mkAppTy f (Plugins.mkAppTys Plugins.properFunTy [a, b])
op' <- mkMethodApps onDict op [f] [a, b] []
uncur' <- mkFunctionApps onDict uncur [ffun, fa, fb] [op']
mkMethodApps onDict arr [cat] [Plugins.mkBoxedTupleTy [ffun, fa], fb] [uncur']
)
appendV <- closedBinaryOp '(GHC.Base.<>)
applyV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier '(Data.Function.$)
uncur <- identifier 'Data.Tuple.uncurry
pure $
\onDict cat a b -> do
let fun = Plugins.mkAppTys Plugins.properFunTy [a, b]
op' <- mkFunctionApps onDict op [Plugins.liftedRepTy, a, b] []
uncur' <- mkFunctionApps onDict uncur [fun, a, b] [op']
mkMethodApps onDict arr [cat] [Plugins.mkBoxedTupleTy [fun, a], b] [uncur']
let apply2V = Nothing
arctan2V <- closedBinaryOp 'GHC.Float.atan2
asinV <- closedUnaryOp 'Numeric.asin
asinhV <- closedUnaryOp 'Numeric.asinh
atanV <- closedUnaryOp 'Numeric.atan
atanhV <- closedUnaryOp 'Numeric.atanh
bindV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier '(Control.Monad.>>=)
uncur <- identifier 'Data.Tuple.uncurry
pure $
\onDict cat m a b -> do
let ma = Plugins.mkAppTy m a
mb = Plugins.mkAppTy m b
fun = Plugins.mkAppTys Plugins.properFunTy [a, mb]
op' <- mkMethodApps onDict op [m] [a, b] []
uncur' <- mkFunctionApps onDict uncur [ma, fun, mb] [op']
mkMethodApps onDict arr [cat] [Plugins.mkBoxedTupleTy [ma, fun], mb] [uncur']
bottomV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'GHC.Err.undefined
pure $ \onDict cat a b ->
mkMethodApps onDict arr [cat] [a, b] . pure
=<< mkFunctionApps onDict op [Plugins.liftedRepTy, Plugins.funTy a b] []
coerceV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Unsafe.Coerce.unsafeCoerce
pure $ \onDict cat from to ->
mkMethodApps onDict arr [cat] [from, to] . pure =<< mkFunctionApps onDict op [from, to] []
compareV <- binaryOp (Just $ Plugins.mkTyConTy Plugins.orderingTyCon) 'GHC.Classes.compare
composeV <-
pure <$> do
fn <- identifier '(Control.Category..)
pure $ \onDict cat a b c -> mkMethodApps onDict fn [Plugins.typeKind a, cat] [b, c, a] []
let compose2V = Nothing
constV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Data.Function.const
pure $
\onDict cat a b ->
-- Builds a lambda expecting the expression we want to return before applying `arr`. I.e.,
-- @\bang -> arr (const bang) :: b -> cat a b@.
let v = Plugins.mkSysLocal (Plugins.fsLit "bang") (Plugins.mkBuiltinUnique 17) b
in Plugins.Lam v
<$> ( mkMethodApps onDict arr [cat] [a, b] . pure
=<< mkFunctionApps onDict op [b, a] [Plugins.Var v]
)
let constraintV = Nothing
cosV <- closedUnaryOp 'Numeric.cos
coshV <- closedUnaryOp 'Numeric.cosh
let curryV = Nothing
distlV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Categorifier.Core.Base.distlB
eith <- findTyCon ''Data.Either.Either
pure $ \onDict cat a b c ->
mkMethodApps
onDict
arr
[cat]
[ Plugins.mkBoxedTupleTy [a, Plugins.mkTyConApp eith [b, c]],
Plugins.mkTyConApp eith [Plugins.mkBoxedTupleTy [a, b], Plugins.mkBoxedTupleTy [a, c]]
]
. pure
=<< mkFunctionApps onDict op [Plugins.mkTyConTy eith, a, b, c] []
divV <- closedBinaryOp 'GHC.Real.div
divideV <- closedBinaryOp '(Prelude./)
doubleToFloatV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'GHC.Float.double2Float
pure (\onDict cat -> mkMethodApps onDict arr [cat] [Plugins.doubleTy, Plugins.floatTy] [op])
equalV <- binaryRel '(GHC.Classes.==)
evenV <- unaryRel 'GHC.Real.even
exlV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Data.Tuple.fst
pure $ \onDict cat a b ->
mkMethodApps onDict arr [cat] [Plugins.mkBoxedTupleTy [a, b], a] . pure
=<< mkFunctionApps onDict op [a, b] []
expV <- closedUnaryOp 'Numeric.exp
exrV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Data.Tuple.snd
pure $ \onDict cat a b ->
mkMethodApps onDict arr [cat] [Plugins.mkBoxedTupleTy [a, b], b] . pure
=<< mkFunctionApps onDict op [a, b] []
fixV <-
pure <$> do
fn <- identifier 'Categorifier.Core.Base.fixB
pure (\onDict cat a x -> mkFunctionApps onDict fn [cat, a, x] [])
floatToDoubleV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'GHC.Float.float2Double
pure (\onDict cat -> mkMethodApps onDict arr [cat] [Plugins.floatTy, Plugins.doubleTy] [op])
let fmodV = Nothing
forkV <-
pure <$> do
fn <- identifier '(Control.Arrow.&&&)
pure (\onDict cat a b c -> mkMethodApps onDict fn [cat] [a, b, c] [])
fpIsNegativeZeroV <- unaryRel 'GHC.Float.isNegativeZero
fpIsInfiniteV <- unaryRel 'GHC.Float.isInfinite
let fpIsFiniteV = Nothing
fpIsNaNV <- unaryRel 'GHC.Float.isNaN
fpIsDenormalV <- unaryRel 'GHC.Float.isDenormalized
fromIntegerV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Prelude.fromInteger
int <- findTyCon ''Prelude.Integer
pure $ \onDict cat a ->
mkMethodApps onDict arr [cat] [Plugins.mkTyConTy int, a] . pure
=<< mkFunctionApps onDict op [a] []
fromIntegralV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'GHC.Real.fromIntegral
pure $ \onDict cat a b ->
mkMethodApps onDict arr [cat] [a, b] . pure =<< mkFunctionApps onDict op [a, b] []
geV <- binaryRel '(GHC.Classes.>=)
gtV <- binaryRel '(GHC.Classes.>)
idV <-
pure <$> do
fn <- identifier 'Control.Category.id
pure (\onDict cat a -> mkMethodApps onDict fn [Plugins.typeKind a, cat] [a] [])
ifV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Categorifier.Core.Base.ifThenElseB
pure $ \onDict cat a ->
mkMethodApps
onDict
arr
[cat]
[Plugins.mkBoxedTupleTy [Plugins.boolTy, Plugins.mkBoxedTupleTy [a, a]], a]
. pure
=<< mkFunctionApps onDict op [a] []
inlV <- eitherOp 'Data.Either.Left const
inrV <- eitherOp 'Data.Either.Right (\_ x -> x)
joinV <-
pure <$> do
fn <- identifier '(Control.Arrow.|||)
pure (\onDict cat a1 a2 b -> mkMethodApps onDict fn [cat] [a1, b, a2] [])
lassocV <-
pure <$> do
arr <- identifier 'Control.Arrow.arr
op <- identifier 'Categorifier.Core.Base.lassocB
pure $ \onDict cat a b c ->
mkMethodApps