-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPureScript.lean
More file actions
1017 lines (942 loc) · 51.5 KB
/
Copy pathPureScript.lean
File metadata and controls
1017 lines (942 loc) · 51.5 KB
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
/-
PureScript Prelude AST in Lean 4
A complete representation of the PureScript Prelude:
- Full AST for types, expressions, declarations, modules
- Pretty printer producing valid PureScript syntax
- Structural proofs verified by rfl
-/
-- ═══════════════════════════════════════════════════════════════════════════════
-- AST
-- ═══════════════════════════════════════════════════════════════════════════════
structure QName where
path : List String
name : String
deriving Repr, BEq, Inhabited
namespace QName
def unqual (name : String) : QName := { path := [], name }
def qual (path : List String) (name : String) : QName := { path, name }
def pretty : QName → String
| { path := [], name } => name
| { path, name } => String.intercalate "." path ++ "." ++ name
end QName
inductive PSType where
| var : String → PSType
| con : QName → PSType
| app : PSType → PSType → PSType
| arr : PSType → PSType → PSType
| forallTy : String → PSType → PSType
| constraint : QName → List PSType → PSType → PSType
| row : List (String × PSType) → Option PSType → PSType
| kind : String → PSType
deriving Repr, BEq, Inhabited
mutual
inductive PSExpr where
| var : QName → PSExpr
| litInt : Int → PSExpr
| litNum : Float → PSExpr
| litStr : String → PSExpr
| litChar : Char → PSExpr
| litBool : Bool → PSExpr
| litArray : List PSExpr → PSExpr
| app : PSExpr → PSExpr → PSExpr
| lam : List String → PSExpr → PSExpr
| letIn : String → PSExpr → PSExpr → PSExpr
| ifThenElse : PSExpr → PSExpr → PSExpr → PSExpr
| caseOf : PSExpr → List (String × PSExpr) → PSExpr
| op : String → PSExpr → PSExpr → PSExpr
| accessor : PSExpr → String → PSExpr
| record : List (String × PSExpr) → PSExpr
| doBlock : List PSDoStmt → PSExpr
inductive PSDoStmt where
| expr : PSExpr → PSDoStmt
| bind : String → PSExpr → PSDoStmt
| letStmt : String → PSExpr → PSDoStmt
end
mutual
def PSExpr.default : PSExpr := .var { path := [], name := "" }
def PSDoStmt.default : PSDoStmt := .expr PSExpr.default
end
instance : Inhabited PSExpr := ⟨PSExpr.default⟩
instance : Inhabited PSDoStmt := ⟨PSDoStmt.default⟩
structure PSImport where
modulePath : List String
qualified : Bool := false
asName : Option String := none
items : Option (List String) := none
isHiding : Bool := false
deriving Inhabited
inductive PSDecl where
| importDecl : PSImport → PSDecl
| typeSig : String → PSType → PSDecl
| valueDef : String → List String → PSExpr → PSDecl
| dataDecl : String → List String → List (String × List PSType) → PSDecl
| newtypeDecl : String → List String → String → PSType → PSDecl
| typeDecl : String → List String → PSType → PSDecl
| classDecl : List (QName × List PSType) → String → List String → List (String × PSType) → PSDecl
| instanceDecl : Option String → List (QName × List PSType) → QName → List PSType → List (String × List String × PSExpr) → PSDecl
| infixDecl : String → Nat → String → String → PSDecl
| foreignImport : String → PSType → PSDecl
| foreignData : String → PSType → PSDecl
| deriveDecl : String → QName → List PSType → PSDecl
deriving Inhabited
structure PSModule where
name : List String
exports : Option (List String) := none
decls : List PSDecl := []
deriving Inhabited
-- ═══════════════════════════════════════════════════════════════════════════════
-- PRETTY PRINTING
-- ═══════════════════════════════════════════════════════════════════════════════
namespace PSType
partial def collectForalls : PSType → (List String × PSType)
| forallTy v body => let (vs, b) := collectForalls body; (v :: vs, b)
| t => ([], t)
partial def pretty (prec : Nat := 0) : PSType → String
| var n => n
| con qn => qn.pretty
| app f a =>
let s := s!"{f.pretty 10} {a.pretty 11}"
if prec > 10 then s!"({s})" else s
| arr a b =>
let s := s!"{a.pretty 5} -> {b.pretty 4}"
if prec > 4 then s!"({s})" else s
| t@(forallTy _ _) =>
let (vars, body) := collectForalls t
let s := s!"forall {String.intercalate " " vars}. {body.pretty 0}"
if prec > 0 then s!"({s})" else s
| constraint cls args body =>
let argsStr := if args.isEmpty then "" else " " ++ String.intercalate " " (args.map (pretty 11))
let s := s!"{cls.pretty}{argsStr} => {body.pretty 0}"
if prec > 0 then s!"({s})" else s
| row fields tail =>
let fieldsStr := String.intercalate ", " (fields.map fun (n, t) => s!"{n} :: {t.pretty 0}")
let tailStr := match tail with | some t => s!" | {t.pretty 0}" | none => ""
s!"( {fieldsStr}{tailStr} )"
| kind k => k
end PSType
namespace PSExpr
partial def pretty (prec : Nat := 0) : PSExpr → String
| var qn => qn.pretty
| litInt n => toString n
| litNum n => toString n
| litStr s => s!"\"{s}\""
| litChar c => s!"'{c}'"
| litBool true => "true"
| litBool false => "false"
| litArray items => s!"[{String.intercalate ", " (items.map (pretty 0))}]"
| app f a =>
let s := s!"{f.pretty 10} {a.pretty 11}"
if prec > 10 then s!"({s})" else s
| lam args body =>
let s := s!"\\{String.intercalate " " args} -> {body.pretty 0}"
if prec > 0 then s!"({s})" else s
| letIn name val body => s!"let {name} = {val.pretty 0} in {body.pretty 0}"
| ifThenElse c t f => s!"if {c.pretty 0} then {t.pretty 0} else {f.pretty 0}"
| caseOf scrut alts =>
let altsStr := String.intercalate "\n " (alts.map fun (p, e) => s!"{p} -> {e.pretty 0}")
s!"case {scrut.pretty 0} of\n {altsStr}"
| op opName l r =>
let s := s!"{l.pretty 6} {opName} {r.pretty 6}"
if prec > 5 then s!"({s})" else s
| accessor e field => s!"{e.pretty 11}.{field}"
| record fields =>
let fieldsStr := String.intercalate ", " (fields.map fun (n, e) => s!"{n}: {e.pretty 0}")
"{ " ++ fieldsStr ++ " }"
| doBlock stmts =>
let stmtsStr := String.intercalate "\n " (stmts.map prettyDoStmt)
s!"do\n {stmtsStr}"
where
prettyDoStmt : PSDoStmt → String
| .expr e => e.pretty 0
| .bind n e => s!"{n} <- {e.pretty 0}"
| .letStmt n e => s!"let {n} = {e.pretty 0}"
def v (name : String) : PSExpr := var (QName.unqual name)
def qv (path : List String) (name : String) : PSExpr := var (QName.qual path name)
end PSExpr
namespace PSImport
def pretty : PSImport → String
| { modulePath, qualified, asName, items, isHiding } =>
let modStr := String.intercalate "." modulePath
let qualStr := if qualified then " qualified" else ""
let asStr := match asName with | some n => s!" as {n}" | none => ""
let hidingStr := if isHiding then " hiding" else ""
let itemsStr := match items with | none => "" | some is => s!" ({String.intercalate ", " is})"
s!"import{qualStr} {modStr}{hidingStr}{itemsStr}{asStr}"
end PSImport
namespace PSDecl
def pretty : PSDecl → String
| importDecl imp => imp.pretty
| typeSig name ty => s!"{name} :: {ty.pretty}"
| valueDef name [] body => s!"{name} = {body.pretty}"
| valueDef name args body => s!"{name} {String.intercalate " " args} = {body.pretty}"
| dataDecl name params ctors =>
let paramsStr := if params.isEmpty then "" else " " ++ String.intercalate " " params
let ctorsStr := String.intercalate "\n | " (ctors.map fun (n, ts) =>
if ts.isEmpty then n else s!"{n} {String.intercalate " " (ts.map (PSType.pretty 11))}")
s!"data {name}{paramsStr} = {ctorsStr}"
| newtypeDecl name params ctor ty =>
let paramsStr := if params.isEmpty then "" else " " ++ String.intercalate " " params
s!"newtype {name}{paramsStr} = {ctor} {ty.pretty}"
| typeDecl name params ty =>
let paramsStr := if params.isEmpty then "" else " " ++ String.intercalate " " params
s!"type {name}{paramsStr} = {ty.pretty}"
| classDecl supers name params members =>
let supersStr := if supers.isEmpty then ""
else String.intercalate ", " (supers.map fun (qn, ts) =>
let argsStr := if ts.isEmpty then "" else " " ++ String.intercalate " " (ts.map (PSType.pretty 11))
s!"{qn.pretty}{argsStr}") ++ " <= "
let paramsStr := if params.isEmpty then "" else " " ++ String.intercalate " " params
let membersStr := if members.isEmpty then ""
else "\n" ++ String.intercalate "\n" (members.map fun (n, t) => s!" {n} :: {t.pretty}")
s!"class {supersStr}{name}{paramsStr} where{membersStr}"
| instanceDecl name constraints cls args members =>
let nameStr := match name with | some n => s!"{n} :: " | none => ""
let constraintsStr := if constraints.isEmpty then ""
else String.intercalate ", " (constraints.map fun (qn, ts) =>
let argsStr := if ts.isEmpty then "" else " " ++ String.intercalate " " (ts.map (PSType.pretty 11))
s!"{qn.pretty}{argsStr}") ++ " => "
let argsStr := String.intercalate " " (args.map (PSType.pretty 11))
let membersStr := if members.isEmpty then ""
else " where\n" ++ String.intercalate "\n" (members.map fun (n, ps, e) =>
let psStr := if ps.isEmpty then "" else " " ++ String.intercalate " " ps
s!" {n}{psStr} = {e.pretty}")
s!"instance {nameStr}{constraintsStr}{cls.pretty} {argsStr}{membersStr}"
| infixDecl assoc prec op name => s!"{assoc} {prec} {name} as {op}"
| foreignImport name ty => s!"foreign import {name} :: {ty.pretty}"
| foreignData name ty => s!"foreign import data {name} :: {ty.pretty}"
| deriveDecl name cls args =>
let argsStr := String.intercalate " " (args.map (PSType.pretty 11))
s!"derive instance {name} :: {cls.pretty} {argsStr}"
end PSDecl
namespace PSModule
def pretty : PSModule → String
| { name, exports, decls } =>
let nameStr := String.intercalate "." name
let exportsStr := match exports with
| none => ""
| some items => s!"\n ( {String.intercalate "\n , " items}\n )"
let declsStr := String.intercalate "\n\n" (decls.map PSDecl.pretty)
s!"module {nameStr}{exportsStr} where\n\n{declsStr}"
end PSModule
-- ═══════════════════════════════════════════════════════════════════════════════
-- COMBINATORS
-- ═══════════════════════════════════════════════════════════════════════════════
-- Type constructors
def tyVar := PSType.var
def tyCon (name : String) := PSType.con (QName.unqual name)
def tyQCon (path : List String) (name : String) := PSType.con (QName.qual path name)
def tyApp := PSType.app
def tyArr := PSType.arr
def tyForall := PSType.forallTy
def tyApp2 (f a b : PSType) := tyApp (tyApp f a) b
def tyApp3 (f a b c : PSType) := tyApp (tyApp (tyApp f a) b) c
def fnTy := tyCon "(->)"
-- Forall helpers
def forallN (vars : List String) (body : PSType) : PSType := vars.foldr PSType.forallTy body
def forallA (body : PSType) : PSType := forallN ["a"] body
def forallAB (body : PSType) : PSType := forallN ["a", "b"] body
def forallABC (body : PSType) : PSType := forallN ["a", "b", "c"] body
def forallABCD (body : PSType) : PSType := forallN ["a", "b", "c", "d"] body
def forallF (body : PSType) : PSType := forallN ["f"] body
def forallFA (body : PSType) : PSType := forallN ["f", "a"] body
def forallFAB (body : PSType) : PSType := forallN ["f", "a", "b"] body
def forallM (body : PSType) : PSType := forallN ["m"] body
def forallMA (body : PSType) : PSType := forallN ["m", "a"] body
def forallMAB (body : PSType) : PSType := forallN ["m", "a", "b"] body
-- Constraint helper
def withConstraint (cls : String) (args : List PSType) (body : PSType) : PSType :=
PSType.constraint (QName.unqual cls) args body
-- Import helper
def imp (path : List String) (items : Option (List String) := none) : PSDecl :=
.importDecl { modulePath := path, items }
-- Class/instance helpers
def cls := QName.unqual
-- ═══════════════════════════════════════════════════════════════════════════════
-- PRELUDE MODULES
-- ═══════════════════════════════════════════════════════════════════════════════
def dataUnitModule : PSModule := {
name := ["Data", "Unit"]
exports := some ["Unit", "unit"]
decls := [
.foreignData "Unit" (.kind "Type"),
.foreignImport "unit" (tyCon "Unit")
]
}
def dataVoidModule : PSModule := {
name := ["Data", "Void"]
exports := some ["Void", "absurd"]
decls := [
.newtypeDecl "Void" [] "Void" (tyCon "Void"),
.typeSig "absurd" (forallA (tyArr (tyCon "Void") (tyVar "a"))),
.valueDef "absurd" ["a"] (.app (.v "spin") (.v "a"))
]
}
def dataBooleanModule : PSModule := {
name := ["Data", "Boolean"]
exports := some ["otherwise"]
decls := [
.typeSig "otherwise" (tyCon "Boolean"),
.valueDef "otherwise" [] (.litBool true)
]
}
def dataOrderingModule : PSModule := {
name := ["Data", "Ordering"]
exports := some ["Ordering(..)", "invert"]
decls := [
.dataDecl "Ordering" [] [("LT", []), ("GT", []), ("EQ", [])],
.typeSig "invert" (tyArr (tyCon "Ordering") (tyCon "Ordering")),
.valueDef "invert" [] (.lam ["x"]
(.caseOf (.v "x") [
("LT", .v "GT"), ("EQ", .v "EQ"), ("GT", .v "LT")
]))
]
}
def dataEqModule : PSModule := {
name := ["Data", "Eq"]
exports := some ["class Eq", "eq", "(==)", "notEq", "(/=)"]
decls := [
.classDecl [] "Eq" ["a"] [
("eq", tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean")))
],
.infixDecl "infix" 4 "==" "eq",
.typeSig "notEq" (forallA (withConstraint "Eq" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean"))))),
.valueDef "notEq" ["x", "y"] (.op "==" (.op "==" (.v "x") (.v "y")) (.litBool false)),
.infixDecl "infix" 4 "/=" "notEq",
.instanceDecl (some "eqBoolean") [] (cls "Eq") [tyCon "Boolean"] [("eq", [], .v "eqBooleanImpl")],
.instanceDecl (some "eqInt") [] (cls "Eq") [tyCon "Int"] [("eq", [], .v "eqIntImpl")],
.instanceDecl (some "eqNumber") [] (cls "Eq") [tyCon "Number"] [("eq", [], .v "eqNumberImpl")],
.instanceDecl (some "eqChar") [] (cls "Eq") [tyCon "Char"] [("eq", [], .v "eqCharImpl")],
.instanceDecl (some "eqString") [] (cls "Eq") [tyCon "String"] [("eq", [], .v "eqStringImpl")],
.instanceDecl (some "eqUnit") [] (cls "Eq") [tyCon "Unit"] [("eq", ["_", "_"], .litBool true)],
.instanceDecl (some "eqVoid") [] (cls "Eq") [tyCon "Void"] [("eq", ["_", "_"], .litBool true)]
]
}
def dataOrdModule : PSModule := {
name := ["Data", "Ord"]
exports := some ["class Ord", "compare", "(<)", "(<=)", "(>)", "(>=)", "min", "max", "clamp", "comparing"]
decls := [
imp ["Data", "Eq"] (some ["class Eq"]),
imp ["Data", "Ordering"] (some ["Ordering(..)"]),
.classDecl [(cls "Eq", [tyVar "a"])] "Ord" ["a"] [
("compare", tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Ordering")))
],
.typeSig "lessThan" (forallA (withConstraint "Ord" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean"))))),
.valueDef "lessThan" ["a1", "a2"]
(.caseOf (.op "`compare`" (.v "a1") (.v "a2")) [("LT", .litBool true), ("_", .litBool false)]),
.infixDecl "infixl" 4 "<" "lessThan",
.typeSig "greaterThan" (forallA (withConstraint "Ord" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean"))))),
.valueDef "greaterThan" ["a1", "a2"]
(.caseOf (.op "`compare`" (.v "a1") (.v "a2")) [("GT", .litBool true), ("_", .litBool false)]),
.infixDecl "infixl" 4 ">" "greaterThan",
.typeSig "lessThanOrEq" (forallA (withConstraint "Ord" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean"))))),
.valueDef "lessThanOrEq" ["a1", "a2"]
(.caseOf (.op "`compare`" (.v "a1") (.v "a2")) [("GT", .litBool false), ("_", .litBool true)]),
.infixDecl "infixl" 4 "<=" "lessThanOrEq",
.typeSig "greaterThanOrEq" (forallA (withConstraint "Ord" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Boolean"))))),
.valueDef "greaterThanOrEq" ["a1", "a2"]
(.caseOf (.op "`compare`" (.v "a1") (.v "a2")) [("LT", .litBool false), ("_", .litBool true)]),
.infixDecl "infixl" 4 ">=" "greaterThanOrEq",
.typeSig "min" (forallA (withConstraint "Ord" [tyVar "a"] (tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))))),
.valueDef "min" ["x", "y"]
(.caseOf (.app (.app (.v "compare") (.v "x")) (.v "y")) [("GT", .v "y"), ("_", .v "x")]),
.typeSig "max" (forallA (withConstraint "Ord" [tyVar "a"] (tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))))),
.valueDef "max" ["x", "y"]
(.caseOf (.app (.app (.v "compare") (.v "x")) (.v "y")) [("LT", .v "y"), ("_", .v "x")]),
.typeSig "comparing" (forallAB (withConstraint "Ord" [tyVar "b"]
(tyArr (tyArr (tyVar "a") (tyVar "b")) (tyArr (tyVar "a") (tyArr (tyVar "a") (tyCon "Ordering")))))),
.valueDef "comparing" ["f", "x", "y"]
(.app (.app (.v "compare") (.app (.v "f") (.v "x"))) (.app (.v "f") (.v "y"))),
.typeSig "clamp" (forallA (withConstraint "Ord" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))))),
.valueDef "clamp" ["low", "hi", "x"]
(.app (.app (.v "min") (.v "hi")) (.app (.app (.v "max") (.v "low")) (.v "x"))),
.instanceDecl (some "ordBoolean") [] (cls "Ord") [tyCon "Boolean"] [
("compare", [], .app (.app (.app (.v "ordBooleanImpl") (.v "LT")) (.v "EQ")) (.v "GT"))],
.instanceDecl (some "ordInt") [] (cls "Ord") [tyCon "Int"] [
("compare", [], .app (.app (.app (.v "ordIntImpl") (.v "LT")) (.v "EQ")) (.v "GT"))],
.instanceDecl (some "ordNumber") [] (cls "Ord") [tyCon "Number"] [
("compare", [], .app (.app (.app (.v "ordNumberImpl") (.v "LT")) (.v "EQ")) (.v "GT"))],
.instanceDecl (some "ordString") [] (cls "Ord") [tyCon "String"] [
("compare", [], .app (.app (.app (.v "ordStringImpl") (.v "LT")) (.v "EQ")) (.v "GT"))],
.instanceDecl (some "ordChar") [] (cls "Ord") [tyCon "Char"] [
("compare", [], .app (.app (.app (.v "ordCharImpl") (.v "LT")) (.v "EQ")) (.v "GT"))],
.instanceDecl (some "ordUnit") [] (cls "Ord") [tyCon "Unit"] [("compare", ["_", "_"], .v "EQ")],
.instanceDecl (some "ordOrdering") [] (cls "Ord") [tyCon "Ordering"] [
("compare", [], .lam ["x", "y"] (.caseOf (.v "x") [
("LT", .caseOf (.v "y") [("LT", .v "EQ"), ("_", .v "LT")]),
("EQ", .caseOf (.v "y") [("LT", .v "GT"), ("EQ", .v "EQ"), ("GT", .v "LT")]),
("GT", .caseOf (.v "y") [("GT", .v "EQ"), ("_", .v "GT")])
]))]
]
}
def dataBoundedModule : PSModule := {
name := ["Data", "Bounded"]
exports := some ["class Bounded", "top", "bottom"]
decls := [
imp ["Data", "Ord"] (some ["class Ord"]),
imp ["Data", "Unit"] (some ["Unit", "unit"]),
.classDecl [(cls "Ord", [tyVar "a"])] "Bounded" ["a"] [
("top", tyVar "a"),
("bottom", tyVar "a")
],
.instanceDecl (some "boundedBoolean") [] (cls "Bounded") [tyCon "Boolean"] [
("top", [], .litBool true), ("bottom", [], .litBool false)],
.instanceDecl (some "boundedInt") [] (cls "Bounded") [tyCon "Int"] [
("top", [], .v "topInt"), ("bottom", [], .v "bottomInt")],
.instanceDecl (some "boundedChar") [] (cls "Bounded") [tyCon "Char"] [
("top", [], .v "topChar"), ("bottom", [], .v "bottomChar")],
.instanceDecl (some "boundedUnit") [] (cls "Bounded") [tyCon "Unit"] [
("top", [], .v "unit"), ("bottom", [], .v "unit")],
.instanceDecl (some "boundedOrdering") [] (cls "Bounded") [tyCon "Ordering"] [
("top", [], .v "GT"), ("bottom", [], .v "LT")]
]
}
def dataShowModule : PSModule := {
name := ["Data", "Show"]
exports := some ["class Show", "show"]
decls := [
.classDecl [] "Show" ["a"] [("show", tyArr (tyVar "a") (tyCon "String"))],
.instanceDecl (some "showUnit") [] (cls "Show") [tyCon "Unit"] [("show", ["_"], .litStr "unit")],
.instanceDecl (some "showBoolean") [] (cls "Show") [tyCon "Boolean"] [
("show", [], .lam ["b"] (.ifThenElse (.v "b") (.litStr "true") (.litStr "false")))],
.instanceDecl (some "showInt") [] (cls "Show") [tyCon "Int"] [("show", [], .v "showIntImpl")],
.instanceDecl (some "showNumber") [] (cls "Show") [tyCon "Number"] [("show", [], .v "showNumberImpl")],
.instanceDecl (some "showChar") [] (cls "Show") [tyCon "Char"] [("show", [], .v "showCharImpl")],
.instanceDecl (some "showString") [] (cls "Show") [tyCon "String"] [("show", [], .v "showStringImpl")]
]
}
def dataSemigroupModule : PSModule := {
name := ["Data", "Semigroup"]
exports := some ["class Semigroup", "append", "(<>)"]
decls := [
.classDecl [] "Semigroup" ["a"] [
("append", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))],
.infixDecl "infixr" 5 "<>" "append",
.instanceDecl (some "semigroupString") [] (cls "Semigroup") [tyCon "String"] [("append", [], .v "concatString")],
.instanceDecl (some "semigroupUnit") [] (cls "Semigroup") [tyCon "Unit"] [("append", ["_", "_"], .v "unit")],
.instanceDecl (some "semigroupArray") [] (cls "Semigroup") [tyApp (tyCon "Array") (tyVar "a")] [
("append", [], .v "concatArray")],
.instanceDecl (some "semigroupFn") [(cls "Semigroup", [tyVar "s"])] (cls "Semigroup") [tyArr (tyVar "a") (tyVar "s")] [
("append", ["f", "g", "x"], .op "<>" (.app (.v "f") (.v "x")) (.app (.v "g") (.v "x")))]
]
}
def dataMonoidModule : PSModule := {
name := ["Data", "Monoid"]
exports := some ["class Monoid", "mempty", "power", "guard"]
decls := [
imp ["Data", "Semigroup"] (some ["class Semigroup"]),
.classDecl [(cls "Semigroup", [tyVar "m"])] "Monoid" ["m"] [("mempty", tyVar "m")],
.instanceDecl (some "monoidString") [] (cls "Monoid") [tyCon "String"] [("mempty", [], .litStr "")],
.instanceDecl (some "monoidUnit") [] (cls "Monoid") [tyCon "Unit"] [("mempty", [], .v "unit")],
.instanceDecl (some "monoidArray") [] (cls "Monoid") [tyApp (tyCon "Array") (tyVar "a")] [
("mempty", [], .litArray [])],
.typeSig "power" (forallM (withConstraint "Monoid" [tyVar "m"]
(tyArr (tyVar "m") (tyArr (tyCon "Int") (tyVar "m"))))),
.valueDef "power" ["x", "n"]
(.ifThenElse (.op "<=" (.v "n") (.litInt 0))
(.v "mempty")
(.op "<>" (.v "x") (.app (.app (.v "power") (.v "x")) (.op "-" (.v "n") (.litInt 1)))))
]
}
def dataSemiringModule : PSModule := {
name := ["Data", "Semiring"]
exports := some ["class Semiring", "add", "(+)", "mul", "(*)", "zero", "one"]
decls := [
.classDecl [] "Semiring" ["a"] [
("add", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("zero", tyVar "a"),
("mul", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("one", tyVar "a")
],
.infixDecl "infixl" 6 "+" "add",
.infixDecl "infixl" 7 "*" "mul",
.instanceDecl (some "semiringInt") [] (cls "Semiring") [tyCon "Int"] [
("add", [], .v "intAdd"), ("zero", [], .litInt 0), ("mul", [], .v "intMul"), ("one", [], .litInt 1)],
.instanceDecl (some "semiringNumber") [] (cls "Semiring") [tyCon "Number"] [
("add", [], .v "numAdd"), ("zero", [], .litNum 0.0), ("mul", [], .v "numMul"), ("one", [], .litNum 1.0)],
.instanceDecl (some "semiringUnit") [] (cls "Semiring") [tyCon "Unit"] [
("add", ["_", "_"], .v "unit"), ("zero", [], .v "unit"), ("mul", ["_", "_"], .v "unit"), ("one", [], .v "unit")]
]
}
def dataRingModule : PSModule := {
name := ["Data", "Ring"]
exports := some ["class Ring", "sub", "(-)", "negate"]
decls := [
imp ["Data", "Semiring"] (some ["class Semiring", "zero"]),
.classDecl [(cls "Semiring", [tyVar "a"])] "Ring" ["a"] [
("sub", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))],
.infixDecl "infixl" 6 "-" "sub",
.typeSig "negate" (forallA (withConstraint "Ring" [tyVar "a"] (tyArr (tyVar "a") (tyVar "a")))),
.valueDef "negate" ["a"] (.op "-" (.v "zero") (.v "a")),
.instanceDecl (some "ringInt") [] (cls "Ring") [tyCon "Int"] [("sub", [], .v "intSub")],
.instanceDecl (some "ringNumber") [] (cls "Ring") [tyCon "Number"] [("sub", [], .v "numSub")],
.instanceDecl (some "ringUnit") [] (cls "Ring") [tyCon "Unit"] [("sub", ["_", "_"], .v "unit")]
]
}
def dataCommutativeRingModule : PSModule := {
name := ["Data", "CommutativeRing"]
exports := some ["class CommutativeRing"]
decls := [
imp ["Data", "Ring"] (some ["class Ring"]),
.classDecl [(cls "Ring", [tyVar "a"])] "CommutativeRing" ["a"] [],
.instanceDecl (some "commutativeRingInt") [] (cls "CommutativeRing") [tyCon "Int"] [],
.instanceDecl (some "commutativeRingNumber") [] (cls "CommutativeRing") [tyCon "Number"] [],
.instanceDecl (some "commutativeRingUnit") [] (cls "CommutativeRing") [tyCon "Unit"] []
]
}
def dataEuclideanRingModule : PSModule := {
name := ["Data", "EuclideanRing"]
exports := some ["class EuclideanRing", "degree", "div", "mod", "(/)", "gcd", "lcm"]
decls := [
imp ["Data", "CommutativeRing"] (some ["class CommutativeRing"]),
imp ["Data", "Eq"] (some ["class Eq", "(==)"]),
imp ["Data", "Semiring"] (some ["zero"]),
.classDecl [(cls "CommutativeRing", [tyVar "a"])] "EuclideanRing" ["a"] [
("degree", tyArr (tyVar "a") (tyCon "Int")),
("div", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("mod", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))
],
.infixDecl "infixl" 7 "/" "div",
.instanceDecl (some "euclideanRingInt") [] (cls "EuclideanRing") [tyCon "Int"] [
("degree", [], .v "intDegree"), ("div", [], .v "intDiv"), ("mod", [], .v "intMod")],
.instanceDecl (some "euclideanRingNumber") [] (cls "EuclideanRing") [tyCon "Number"] [
("degree", ["_"], .litInt 1), ("div", [], .v "numDiv"), ("mod", ["_", "_"], .litNum 0.0)],
.typeSig "gcd" (forallA (withConstraint "Eq" [tyVar "a"]
(withConstraint "EuclideanRing" [tyVar "a"] (tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))))),
.valueDef "gcd" ["a", "b"]
(.ifThenElse (.op "==" (.v "b") (.v "zero"))
(.v "a")
(.app (.app (.v "gcd") (.v "b")) (.app (.app (.v "mod") (.v "a")) (.v "b")))),
.typeSig "lcm" (forallA (withConstraint "Eq" [tyVar "a"]
(withConstraint "EuclideanRing" [tyVar "a"] (tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a")))))),
.valueDef "lcm" ["a", "b"]
(.ifThenElse (.op "==" (.v "a") (.v "zero"))
(.v "zero")
(.ifThenElse (.op "==" (.v "b") (.v "zero"))
(.v "zero")
(.op "/" (.op "*" (.v "a") (.v "b")) (.app (.app (.v "gcd") (.v "a")) (.v "b")))))
]
}
def dataDivisionRingModule : PSModule := {
name := ["Data", "DivisionRing"]
exports := some ["class DivisionRing", "recip"]
decls := [
imp ["Data", "Ring"] (some ["class Ring"]),
imp ["Data", "Semiring"] (some ["one"]),
.classDecl [(cls "Ring", [tyVar "a"])] "DivisionRing" ["a"] [
("recip", tyArr (tyVar "a") (tyVar "a"))
],
.typeSig "leftDiv" (forallA (withConstraint "DivisionRing" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))))),
.valueDef "leftDiv" ["a", "b"] (.op "*" (.v "a") (.app (.v "recip") (.v "b"))),
.typeSig "rightDiv" (forallA (withConstraint "DivisionRing" [tyVar "a"]
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))))),
.valueDef "rightDiv" ["a", "b"] (.op "*" (.app (.v "recip") (.v "b")) (.v "a")),
.instanceDecl (some "divisionRingNumber") [] (cls "DivisionRing") [tyCon "Number"] [
("recip", ["x"], .op "/" (.v "one") (.v "x"))]
]
}
def dataFieldModule : PSModule := {
name := ["Data", "Field"]
exports := some ["class Field"]
decls := [
imp ["Data", "EuclideanRing"] (some ["class EuclideanRing"]),
imp ["Data", "DivisionRing"] (some ["class DivisionRing"]),
.classDecl [(cls "EuclideanRing", [tyVar "a"]), (cls "DivisionRing", [tyVar "a"])] "Field" ["a"] [],
.instanceDecl (some "fieldNumber") [] (cls "Field") [tyCon "Number"] []
]
}
def dataHeytingAlgebraModule : PSModule := {
name := ["Data", "HeytingAlgebra"]
exports := some ["class HeytingAlgebra", "tt", "ff", "implies", "conj", "(&&)", "disj", "(||)", "not"]
decls := [
.classDecl [] "HeytingAlgebra" ["a"] [
("tt", tyVar "a"), ("ff", tyVar "a"),
("implies", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("conj", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("disj", tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "a"))),
("not", tyArr (tyVar "a") (tyVar "a"))
],
.infixDecl "infixr" 3 "&&" "conj",
.infixDecl "infixr" 2 "||" "disj",
.instanceDecl (some "heytingAlgebraBoolean") [] (cls "HeytingAlgebra") [tyCon "Boolean"] [
("tt", [], .litBool true), ("ff", [], .litBool false),
("implies", ["a", "b"], .op "||" (.app (.v "not") (.v "a")) (.v "b")),
("conj", [], .v "boolConj"), ("disj", [], .v "boolDisj"), ("not", [], .v "boolNot")],
.instanceDecl (some "heytingAlgebraUnit") [] (cls "HeytingAlgebra") [tyCon "Unit"] [
("tt", [], .v "unit"), ("ff", [], .v "unit"),
("implies", ["_", "_"], .v "unit"), ("conj", ["_", "_"], .v "unit"),
("disj", ["_", "_"], .v "unit"), ("not", ["_"], .v "unit")],
.instanceDecl (some "heytingAlgebraFn") [(cls "HeytingAlgebra", [tyVar "b"])]
(cls "HeytingAlgebra") [tyArr (tyVar "a") (tyVar "b")] [
("tt", ["_"], .v "tt"), ("ff", ["_"], .v "ff"),
("implies", ["f", "g", "x"], .app (.app (.v "implies") (.app (.v "f") (.v "x"))) (.app (.v "g") (.v "x"))),
("conj", ["f", "g", "x"], .op "&&" (.app (.v "f") (.v "x")) (.app (.v "g") (.v "x"))),
("disj", ["f", "g", "x"], .op "||" (.app (.v "f") (.v "x")) (.app (.v "g") (.v "x"))),
("not", ["f", "x"], .app (.v "not") (.app (.v "f") (.v "x")))]
]
}
def dataBooleanAlgebraModule : PSModule := {
name := ["Data", "BooleanAlgebra"]
exports := some ["class BooleanAlgebra"]
decls := [
imp ["Data", "HeytingAlgebra"] (some ["class HeytingAlgebra"]),
.classDecl [(cls "HeytingAlgebra", [tyVar "a"])] "BooleanAlgebra" ["a"] [],
.instanceDecl (some "booleanAlgebraBoolean") [] (cls "BooleanAlgebra") [tyCon "Boolean"] [],
.instanceDecl (some "booleanAlgebraUnit") [] (cls "BooleanAlgebra") [tyCon "Unit"] [],
.instanceDecl (some "booleanAlgebraFn") [(cls "BooleanAlgebra", [tyVar "b"])]
(cls "BooleanAlgebra") [tyArr (tyVar "a") (tyVar "b")] []
]
}
def dataFunctionModule : PSModule := {
name := ["Data", "Function"]
exports := some ["flip", "const", "apply", "($)", "applyFlipped", "(#)", "on"]
decls := [
imp ["Control", "Category"] (some ["identity", "compose"]),
.typeSig "flip" (forallABC (tyArr (tyArr (tyVar "a") (tyArr (tyVar "b") (tyVar "c")))
(tyArr (tyVar "b") (tyArr (tyVar "a") (tyVar "c"))))),
.valueDef "flip" ["f", "b", "a"] (.app (.app (.v "f") (.v "a")) (.v "b")),
.typeSig "const" (forallAB (tyArr (tyVar "a") (tyArr (tyVar "b") (tyVar "a")))),
.valueDef "const" ["a", "_"] (.v "a"),
.typeSig "apply" (forallAB (tyArr (tyArr (tyVar "a") (tyVar "b")) (tyArr (tyVar "a") (tyVar "b")))),
.valueDef "apply" ["f", "x"] (.app (.v "f") (.v "x")),
.infixDecl "infixr" 0 "$" "apply",
.typeSig "applyFlipped" (forallAB (tyArr (tyVar "a") (tyArr (tyArr (tyVar "a") (tyVar "b")) (tyVar "b")))),
.valueDef "applyFlipped" ["x", "f"] (.app (.v "f") (.v "x")),
.infixDecl "infixl" 1 "#" "applyFlipped",
.typeSig "on" (forallABC (tyArr (tyArr (tyVar "b") (tyArr (tyVar "b") (tyVar "c")))
(tyArr (tyArr (tyVar "a") (tyVar "b"))
(tyArr (tyVar "a") (tyArr (tyVar "a") (tyVar "c")))))),
.valueDef "on" ["f", "g", "x", "y"]
(.app (.app (.v "f") (.app (.v "g") (.v "x"))) (.app (.v "g") (.v "y")))
]
}
def controlSemigroupoidModule : PSModule := {
name := ["Control", "Semigroupoid"]
exports := some ["class Semigroupoid", "compose", "(<<<)", "composeFlipped", "(>>>)"]
decls := [
.classDecl [] "Semigroupoid" ["a"] [
("compose", forallN ["b", "c", "d"] (tyArr (tyApp2 (tyVar "a") (tyVar "c") (tyVar "d"))
(tyArr (tyApp2 (tyVar "a") (tyVar "b") (tyVar "c"))
(tyApp2 (tyVar "a") (tyVar "b") (tyVar "d")))))],
.infixDecl "infixr" 9 "<<<" "compose",
.instanceDecl (some "semigroupoidFn") [] (cls "Semigroupoid") [tyCon "(->)"] [
("compose", ["f", "g", "x"], .app (.v "f") (.app (.v "g") (.v "x")))],
.typeSig "composeFlipped" (forallABCD (withConstraint "Semigroupoid" [tyVar "a"]
(tyArr (tyApp2 (tyVar "a") (tyVar "b") (tyVar "c"))
(tyArr (tyApp2 (tyVar "a") (tyVar "c") (tyVar "d"))
(tyApp2 (tyVar "a") (tyVar "b") (tyVar "d")))))),
.valueDef "composeFlipped" ["f", "g"] (.app (.app (.v "compose") (.v "g")) (.v "f")),
.infixDecl "infixr" 9 ">>>" "composeFlipped"
]
}
def controlCategoryModule : PSModule := {
name := ["Control", "Category"]
exports := some ["class Category", "identity"]
decls := [
imp ["Control", "Semigroupoid"] (some ["class Semigroupoid"]),
.classDecl [(cls "Semigroupoid", [tyVar "a"])] "Category" ["a"] [
("identity", forallN ["t"] (tyApp2 (tyVar "a") (tyVar "t") (tyVar "t")))],
.instanceDecl (some "categoryFn") [] (cls "Category") [tyCon "(->)"] [
("identity", ["x"], .v "x")]
]
}
def dataFunctorModule : PSModule := {
name := ["Data", "Functor"]
exports := some ["class Functor", "map", "(<$>)", "mapFlipped", "(<#>)", "void", "voidRight", "(<$)", "voidLeft", "($>)", "flap"]
decls := [
imp ["Data", "Function"] (some ["const"]),
imp ["Data", "Unit"] (some ["Unit", "unit"]),
imp ["Control", "Semigroupoid"] (some ["compose"]),
.classDecl [] "Functor" ["f"] [
("map", forallAB (tyArr (tyArr (tyVar "a") (tyVar "b"))
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyApp (tyVar "f") (tyVar "b")))))],
.infixDecl "infixl" 4 "<$>" "map",
.typeSig "mapFlipped" (forallFAB (withConstraint "Functor" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyArr (tyArr (tyVar "a") (tyVar "b")) (tyApp (tyVar "f") (tyVar "b")))))),
.valueDef "mapFlipped" ["fa", "f"] (.op "<$>" (.v "f") (.v "fa")),
.infixDecl "infixl" 1 "<#>" "mapFlipped",
.instanceDecl (some "functorFn") [] (cls "Functor") [tyApp fnTy (tyVar "r")] [("map", [], .v "compose")],
.instanceDecl (some "functorArray") [] (cls "Functor") [tyCon "Array"] [("map", [], .v "arrayMap")],
.typeSig "void" (forallFA (withConstraint "Functor" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyApp (tyVar "f") (tyCon "Unit"))))),
.valueDef "void" [] (.app (.v "map") (.app (.v "const") (.v "unit"))),
.typeSig "voidRight" (forallFAB (withConstraint "Functor" [tyVar "f"]
(tyArr (tyVar "a") (tyArr (tyApp (tyVar "f") (tyVar "b")) (tyApp (tyVar "f") (tyVar "a")))))),
.valueDef "voidRight" ["x"] (.app (.v "map") (.app (.v "const") (.v "x"))),
.infixDecl "infixl" 4 "<$" "voidRight",
.typeSig "voidLeft" (forallFAB (withConstraint "Functor" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyArr (tyVar "b") (tyApp (tyVar "f") (tyVar "b")))))),
.valueDef "voidLeft" ["f", "x"] (.op "<$>" (.app (.v "const") (.v "x")) (.v "f")),
.infixDecl "infixl" 4 "$>" "voidLeft",
.typeSig "flap" (forallFAB (withConstraint "Functor" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyArr (tyVar "a") (tyVar "b"))) (tyArr (tyVar "a") (tyApp (tyVar "f") (tyVar "b")))))),
.valueDef "flap" ["ff", "x"] (.app (.app (.v "map") (.lam ["f"] (.app (.v "f") (.v "x")))) (.v "ff"))
]
}
def controlApplyModule : PSModule := {
name := ["Control", "Apply"]
exports := some ["class Apply", "apply", "(<*>)", "applyFirst", "(<*)", "applySecond", "(*>)", "lift2", "lift3"]
decls := [
imp ["Data", "Functor"] (some ["class Functor", "map", "(<$>)"]),
imp ["Data", "Function"] (some ["const"]),
imp ["Control", "Category"] (some ["identity"]),
.classDecl [(cls "Functor", [tyVar "f"])] "Apply" ["f"] [
("apply", forallAB (tyArr (tyApp (tyVar "f") (tyArr (tyVar "a") (tyVar "b")))
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyApp (tyVar "f") (tyVar "b")))))],
.infixDecl "infixl" 4 "<*>" "apply",
.instanceDecl (some "applyFn") [] (cls "Apply") [tyApp fnTy (tyVar "r")] [
("apply", ["f", "g", "x"], .app (.app (.v "f") (.v "x")) (.app (.v "g") (.v "x")))],
.instanceDecl (some "applyArray") [] (cls "Apply") [tyCon "Array"] [("apply", [], .v "arrayApply")],
.typeSig "applyFirst" (forallFAB (withConstraint "Apply" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyArr (tyApp (tyVar "f") (tyVar "b")) (tyApp (tyVar "f") (tyVar "a")))))),
.valueDef "applyFirst" ["a", "b"] (.op "<*>" (.op "<$>" (.v "const") (.v "a")) (.v "b")),
.infixDecl "infixl" 4 "<*" "applyFirst",
.typeSig "applySecond" (forallFAB (withConstraint "Apply" [tyVar "f"]
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyArr (tyApp (tyVar "f") (tyVar "b")) (tyApp (tyVar "f") (tyVar "b")))))),
.valueDef "applySecond" ["a", "b"] (.op "<*>" (.op "<$>" (.app (.v "const") (.v "identity")) (.v "a")) (.v "b")),
.infixDecl "infixl" 4 "*>" "applySecond",
.typeSig "lift2" (forallN ["f", "a", "b", "c"] (withConstraint "Apply" [tyVar "f"]
(tyArr (tyArr (tyVar "a") (tyArr (tyVar "b") (tyVar "c")))
(tyArr (tyApp (tyVar "f") (tyVar "a")) (tyArr (tyApp (tyVar "f") (tyVar "b")) (tyApp (tyVar "f") (tyVar "c"))))))),
.valueDef "lift2" ["f", "a", "b"] (.op "<*>" (.op "<$>" (.v "f") (.v "a")) (.v "b")),
.typeSig "lift3" (forallN ["f", "a", "b", "c", "d"] (withConstraint "Apply" [tyVar "f"]
(tyArr (tyArr (tyVar "a") (tyArr (tyVar "b") (tyArr (tyVar "c") (tyVar "d"))))
(tyArr (tyApp (tyVar "f") (tyVar "a"))
(tyArr (tyApp (tyVar "f") (tyVar "b"))
(tyArr (tyApp (tyVar "f") (tyVar "c")) (tyApp (tyVar "f") (tyVar "d")))))))),
.valueDef "lift3" ["f", "a", "b", "c"] (.op "<*>" (.op "<*>" (.op "<$>" (.v "f") (.v "a")) (.v "b")) (.v "c"))
]
}
def controlApplicativeModule : PSModule := {
name := ["Control", "Applicative"]
exports := some ["class Applicative", "pure", "liftA1", "when", "unless"]
decls := [
imp ["Control", "Apply"] (some ["class Apply", "(<*>)"]),
imp ["Data", "Unit"] (some ["Unit", "unit"]),
.classDecl [(cls "Apply", [tyVar "f"])] "Applicative" ["f"] [
("pure", forallA (tyArr (tyVar "a") (tyApp (tyVar "f") (tyVar "a"))))],
.instanceDecl (some "applicativeFn") [] (cls "Applicative") [tyApp fnTy (tyVar "r")] [
("pure", ["x", "_"], .v "x")],
.instanceDecl (some "applicativeArray") [] (cls "Applicative") [tyCon "Array"] [
("pure", ["x"], .litArray [.v "x"])],
.typeSig "liftA1" (forallFAB (withConstraint "Applicative" [tyVar "f"]
(tyArr (tyArr (tyVar "a") (tyVar "b")) (tyArr (tyApp (tyVar "f") (tyVar "a")) (tyApp (tyVar "f") (tyVar "b")))))),
.valueDef "liftA1" ["f", "a"] (.op "<*>" (.app (.v "pure") (.v "f")) (.v "a")),
.typeSig "when" (forallM (withConstraint "Applicative" [tyVar "m"]
(tyArr (tyCon "Boolean") (tyArr (tyApp (tyVar "m") (tyCon "Unit")) (tyApp (tyVar "m") (tyCon "Unit")))))),
.valueDef "when" ["cond", "m"] (.ifThenElse (.v "cond") (.v "m") (.app (.v "pure") (.v "unit"))),
.typeSig "unless" (forallM (withConstraint "Applicative" [tyVar "m"]
(tyArr (tyCon "Boolean") (tyArr (tyApp (tyVar "m") (tyCon "Unit")) (tyApp (tyVar "m") (tyCon "Unit")))))),
.valueDef "unless" ["cond", "m"] (.ifThenElse (.v "cond") (.app (.v "pure") (.v "unit")) (.v "m"))
]
}
def controlBindModule : PSModule := {
name := ["Control", "Bind"]
exports := some ["class Bind", "bind", "(>>=)", "bindFlipped", "(=<<)", "join", "composeKleisli", "(>=>)", "composeKleisliFlipped", "(<=<)"]
decls := [
imp ["Control", "Apply"] (some ["class Apply"]),
imp ["Control", "Category"] (some ["identity"]),
imp ["Data", "Function"] (some ["flip"]),
.classDecl [(cls "Apply", [tyVar "m"])] "Bind" ["m"] [
("bind", forallAB (tyArr (tyApp (tyVar "m") (tyVar "a"))
(tyArr (tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "b")))
(tyApp (tyVar "m") (tyVar "b")))))],
.infixDecl "infixl" 1 ">>=" "bind",
.typeSig "bindFlipped" (forallMAB (withConstraint "Bind" [tyVar "m"]
(tyArr (tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "b")))
(tyArr (tyApp (tyVar "m") (tyVar "a")) (tyApp (tyVar "m") (tyVar "b")))))),
.valueDef "bindFlipped" [] (.app (.v "flip") (.v "bind")),
.infixDecl "infixr" 1 "=<<" "bindFlipped",
.instanceDecl (some "bindFn") [] (cls "Bind") [tyApp fnTy (tyVar "r")] [
("bind", ["m", "f", "x"], .app (.app (.v "f") (.app (.v "m") (.v "x"))) (.v "x"))],
.instanceDecl (some "bindArray") [] (cls "Bind") [tyCon "Array"] [("bind", [], .v "arrayBind")],
.typeSig "join" (forallMA (withConstraint "Bind" [tyVar "m"]
(tyArr (tyApp (tyVar "m") (tyApp (tyVar "m") (tyVar "a"))) (tyApp (tyVar "m") (tyVar "a"))))),
.valueDef "join" ["m"] (.op ">>=" (.v "m") (.v "identity")),
.typeSig "composeKleisli" (forallN ["m", "a", "b", "c"] (withConstraint "Bind" [tyVar "m"]
(tyArr (tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "b")))
(tyArr (tyArr (tyVar "b") (tyApp (tyVar "m") (tyVar "c")))
(tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "c"))))))),
.valueDef "composeKleisli" ["f", "g", "a"] (.op ">>=" (.app (.v "f") (.v "a")) (.v "g")),
.infixDecl "infixr" 1 ">=>" "composeKleisli",
.typeSig "composeKleisliFlipped" (forallN ["m", "a", "b", "c"] (withConstraint "Bind" [tyVar "m"]
(tyArr (tyArr (tyVar "b") (tyApp (tyVar "m") (tyVar "c")))
(tyArr (tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "b")))
(tyArr (tyVar "a") (tyApp (tyVar "m") (tyVar "c"))))))),
.valueDef "composeKleisliFlipped" ["f", "g", "a"] (.op "=<<" (.v "f") (.app (.v "g") (.v "a"))),
.infixDecl "infixr" 1 "<=<" "composeKleisliFlipped"
]
}
def controlMonadModule : PSModule := {
name := ["Control", "Monad"]
exports := some ["class Monad", "liftM1", "whenM", "unlessM", "ap"]
decls := [
imp ["Control", "Applicative"] (some ["class Applicative", "pure", "when", "unless"]),
imp ["Control", "Bind"] (some ["class Bind", "(>>=)"]),
imp ["Data", "Unit"] (some ["Unit"]),
.classDecl [(cls "Applicative", [tyVar "m"]), (cls "Bind", [tyVar "m"])] "Monad" ["m"] [],
.instanceDecl (some "monadFn") [] (cls "Monad") [tyApp fnTy (tyVar "r")] [],
.instanceDecl (some "monadArray") [] (cls "Monad") [tyCon "Array"] [],
.typeSig "liftM1" (forallMAB (withConstraint "Monad" [tyVar "m"]
(tyArr (tyArr (tyVar "a") (tyVar "b")) (tyArr (tyApp (tyVar "m") (tyVar "a")) (tyApp (tyVar "m") (tyVar "b")))))),
.valueDef "liftM1" ["f", "a"]
(.doBlock [.bind "a'" (.v "a"), .expr (.app (.v "pure") (.app (.v "f") (.v "a'")))]),
.typeSig "whenM" (forallM (withConstraint "Monad" [tyVar "m"]
(tyArr (tyApp (tyVar "m") (tyCon "Boolean")) (tyArr (tyApp (tyVar "m") (tyCon "Unit")) (tyApp (tyVar "m") (tyCon "Unit")))))),
.valueDef "whenM" ["mb", "m"]
(.doBlock [.bind "b" (.v "mb"), .expr (.app (.app (.v "when") (.v "b")) (.v "m"))]),
.typeSig "unlessM" (forallM (withConstraint "Monad" [tyVar "m"]
(tyArr (tyApp (tyVar "m") (tyCon "Boolean")) (tyArr (tyApp (tyVar "m") (tyCon "Unit")) (tyApp (tyVar "m") (tyCon "Unit")))))),
.valueDef "unlessM" ["mb", "m"]
(.doBlock [.bind "b" (.v "mb"), .expr (.app (.app (.v "unless") (.v "b")) (.v "m"))]),
.typeSig "ap" (forallMAB (withConstraint "Monad" [tyVar "m"]
(tyArr (tyApp (tyVar "m") (tyArr (tyVar "a") (tyVar "b")))
(tyArr (tyApp (tyVar "m") (tyVar "a")) (tyApp (tyVar "m") (tyVar "b")))))),
.valueDef "ap" ["f", "a"]
(.doBlock [.bind "f'" (.v "f"), .bind "a'" (.v "a"), .expr (.app (.v "pure") (.app (.v "f'") (.v "a'")))])
]
}
def typeProxyModule : PSModule := {
name := ["Type", "Proxy"]
exports := some ["Proxy(..)", "Proxy2", "Proxy3"]
decls := [
.dataDecl "Proxy" ["a"] [("Proxy", [])],
.typeDecl "Proxy2" ["a"] (tyCon "Proxy"),
.typeDecl "Proxy3" ["a", "b", "c"] (tyCon "Proxy"),
.instanceDecl (some "eqProxy") [] (cls "Eq") [tyApp (tyCon "Proxy") (tyVar "a")] [
("eq", ["_", "_"], .litBool true)],
.instanceDecl (some "ordProxy") [] (cls "Ord") [tyApp (tyCon "Proxy") (tyVar "a")] [
("compare", ["_", "_"], .v "EQ")],
.instanceDecl (some "showProxy") [] (cls "Show") [tyApp (tyCon "Proxy") (tyVar "a")] [
("show", ["_"], .litStr "Proxy")],
.instanceDecl (some "boundedProxy") [] (cls "Bounded") [tyApp (tyCon "Proxy") (tyVar "a")] [
("top", [], .v "Proxy"), ("bottom", [], .v "Proxy")],
.instanceDecl (some "semigroupProxy") [] (cls "Semigroup") [tyApp (tyCon "Proxy") (tyVar "a")] [
("append", ["_", "_"], .v "Proxy")],
.instanceDecl (some "monoidProxy") [] (cls "Monoid") [tyApp (tyCon "Proxy") (tyVar "a")] [
("mempty", [], .v "Proxy")]
]
}
def dataSymbolModule : PSModule := {
name := ["Data", "Symbol"]
exports := some ["class IsSymbol", "reflectSymbol", "reifySymbol"]
decls := [
imp ["Type", "Proxy"] (some ["Proxy"]),
.classDecl [] "IsSymbol" ["sym"] [
("reflectSymbol", tyArr (tyApp (tyCon "Proxy") (tyVar "sym")) (tyCon "String"))
],
.typeSig "reifySymbol" (forallN ["r"]
(tyArr (tyCon "String")
(tyArr (forallN ["sym"] (withConstraint "IsSymbol" [tyVar "sym"]
(tyArr (tyApp (tyCon "Proxy") (tyVar "sym")) (tyVar "r"))))
(tyVar "r")))),
.valueDef "reifySymbol" ["s", "f"] (.app (.app (.v "unsafeCoerce") (.v "f")) (.v "s"))
]
}
def dataNaturalTransformationModule : PSModule := {
name := ["Data", "NaturalTransformation"]
exports := some ["NaturalTransformation", "(~>)"]
decls := [
.typeDecl "NaturalTransformation" ["f", "g"]
(forallA (tyArr (tyApp (tyVar "f") (tyVar "a")) (tyApp (tyVar "g") (tyVar "a")))),
.infixDecl "infixr" 4 "~>" "NaturalTransformation"
]
}
-- ═══════════════════════════════════════════════════════════════════════════════
-- MODULE LIST
-- ═══════════════════════════════════════════════════════════════════════════════
def allModules : List PSModule := [
-- Core data types
dataUnitModule,
dataVoidModule,
dataBooleanModule,
dataOrderingModule,
-- Eq/Ord/Show/Bounded
dataEqModule,
dataOrdModule,
dataBoundedModule,
dataShowModule,
-- Semigroup/Monoid
dataSemigroupModule,
dataMonoidModule,
-- Numeric hierarchy
dataSemiringModule,
dataRingModule,
dataCommutativeRingModule,
dataEuclideanRingModule,
dataDivisionRingModule,
dataFieldModule,
-- Boolean algebra
dataHeytingAlgebraModule,
dataBooleanAlgebraModule,
-- Function
dataFunctionModule,
-- Category
controlSemigroupoidModule,
controlCategoryModule,
-- Functor/Apply/Applicative/Bind/Monad
dataFunctorModule,
controlApplyModule,
controlApplicativeModule,
controlBindModule,
controlMonadModule,
-- Type-level
typeProxyModule,
dataSymbolModule,
dataNaturalTransformationModule
]
-- ═══════════════════════════════════════════════════════════════════════════════
-- STRUCTURAL PROOFS
-- ═══════════════════════════════════════════════════════════════════════════════
theorem flip_body_swaps_args :
dataFunctionModule.decls.get? 2 =
some (.valueDef "flip" ["f", "b", "a"] (.app (.app (.v "f") (.v "a")) (.v "b"))) := rfl
theorem const_ignores_second :
dataFunctionModule.decls.get? 4 =
some (.valueDef "const" ["a", "_"] (.v "a")) := rfl
theorem identity_returns_input :
controlCategoryModule.decls.get? 2 =
some (.instanceDecl (some "categoryFn") [] (cls "Category") [tyCon "(->)"] [
("identity", ["x"], .v "x")]) := rfl
theorem compose_applies_g_then_f :
controlSemigroupoidModule.decls.get? 2 =
some (.instanceDecl (some "semigroupoidFn") [] (cls "Semigroupoid") [tyCon "(->)"] [
("compose", ["f", "g", "x"], .app (.v "f") (.app (.v "g") (.v "x")))]) := rfl
theorem void_maps_const_unit :
dataFunctorModule.decls.get? 11 =
some (.valueDef "void" [] (.app (.v "map") (.app (.v "const") (.v "unit")))) := rfl
theorem join_binds_identity :
controlBindModule.decls.get? 11 =
some (.valueDef "join" ["m"] (.op ">>=" (.v "m") (.v "identity"))) := rfl
-- ═══════════════════════════════════════════════════════════════════════════════
-- MAIN
-- ═══════════════════════════════════════════════════════════════════════════════
def main : IO Unit := do
IO.println "═══════════════════════════════════════════════════════════════════════"
IO.println " PureScript Prelude in Lean 4"
IO.println "═══════════════════════════════════════════════════════════════════════"
IO.println ""
IO.println s!"Modules: {allModules.length}"
IO.println ""
for m in allModules do