oxilean-std 0.1.2

OxiLean standard library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use oxilean_kernel::{BinderInfo, Declaration, Environment, Expr, Level, Name};

use super::types::LogicalRelation;

pub fn app(f: Expr, a: Expr) -> Expr {
    Expr::App(Box::new(f), Box::new(a))
}
pub fn app2(f: Expr, a: Expr, b: Expr) -> Expr {
    app(app(f, a), b)
}
pub fn app3(f: Expr, a: Expr, b: Expr, c: Expr) -> Expr {
    app(app2(f, a, b), c)
}
pub fn app4(f: Expr, a: Expr, b: Expr, c: Expr, d: Expr) -> Expr {
    app(app3(f, a, b, c), d)
}
pub fn cst(s: &str) -> Expr {
    Expr::Const(Name::str(s), vec![])
}
pub fn prop() -> Expr {
    Expr::Sort(Level::zero())
}
pub fn type0() -> Expr {
    Expr::Sort(Level::succ(Level::zero()))
}
pub fn type1() -> Expr {
    Expr::Sort(Level::succ(Level::succ(Level::zero())))
}
pub fn pi(bi: BinderInfo, name: &str, dom: Expr, body: Expr) -> Expr {
    Expr::Pi(bi, Name::str(name), Box::new(dom), Box::new(body))
}
pub(super) fn arrow(a: Expr, b: Expr) -> Expr {
    pi(BinderInfo::Default, "_", a, b)
}
pub fn impl_pi(name: &str, dom: Expr, body: Expr) -> Expr {
    pi(BinderInfo::Implicit, name, dom, body)
}
pub fn bvar(n: u32) -> Expr {
    Expr::BVar(n)
}
pub fn nat_ty() -> Expr {
    cst("Nat")
}
/// `Rel : Type → Type → Type`
///
/// The type of binary relations between two types.
/// `Rel A B = A → B → Prop`.
pub fn rel_ty() -> Expr {
    arrow(type0(), arrow(type0(), type0()))
}
/// `RelId : ∀ (A : Type), Rel A A`
///
/// The identity relation on A: `RelId A a a' ↔ a = a'`.
pub fn rel_id_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        app2(cst("Rel"), bvar(0), bvar(0)),
    )
}
/// `RelComp : ∀ {A B C : Type}, Rel A B → Rel B C → Rel A C`
///
/// Relational composition: (R ∘ S)(a, c) ↔ ∃ b, R(a,b) ∧ S(b,c).
pub fn rel_comp_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            impl_pi(
                "C",
                type0(),
                arrow(
                    app2(cst("Rel"), bvar(2), bvar(1)),
                    arrow(
                        app2(cst("Rel"), bvar(2), bvar(1)),
                        app2(cst("Rel"), bvar(4), bvar(2)),
                    ),
                ),
            ),
        ),
    )
}
/// `RelConverse : ∀ {A B : Type}, Rel A B → Rel B A`
///
/// The converse relation: R^op(b, a) ↔ R(a, b).
pub fn rel_converse_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            arrow(
                app2(cst("Rel"), bvar(1), bvar(0)),
                app2(cst("Rel"), bvar(1), bvar(2)),
            ),
        ),
    )
}
/// `RelFun : ∀ {A B C D : Type}, Rel A B → Rel C D → Rel (A → C) (B → D)`
///
/// Lifting of relations to function types: the "function relation" between A→C and B→D
/// induced by relations R : Rel A B and S : Rel C D.
/// `RelFun R S f g ↔ ∀ a b, R a b → S (f a) (g b)`.
pub fn rel_fun_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            impl_pi(
                "C",
                type0(),
                impl_pi(
                    "D",
                    type0(),
                    arrow(
                        app2(cst("Rel"), bvar(3), bvar(2)),
                        arrow(
                            app2(cst("Rel"), bvar(3), bvar(2)),
                            app2(cst("Rel"), arrow(bvar(5), bvar(3)), arrow(bvar(5), bvar(3))),
                        ),
                    ),
                ),
            ),
        ),
    )
}
/// `ReynoldsParametricity : Prop`
///
/// The fundamental parametricity theorem (Reynolds 1983):
/// Every polymorphic term f : ∀ α. F(α) satisfies the relation F(R) for every
/// relation R between types A and B.
/// Stated as an axiom at the metatheory level.
pub fn reynolds_parametricity_ty() -> Expr {
    prop()
}
/// `ParametricFunction : ∀ {F : Type → Type}, (∀ α, F α) → Prop`
///
/// Predicate: a polymorphic function is parametric if it preserves all relations.
/// `ParametricFunction f ↔ ∀ A B (R : Rel A B), Lift_F R (f A) (f B)`.
pub fn parametric_function_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        arrow(
            pi(BinderInfo::Default, "α", type0(), app(bvar(1), bvar(0))),
            prop(),
        ),
    )
}
/// `ParametricityCondition : ∀ {A B : Type} (R : Rel A B) {F : Type → Type},
///     (∀ α, F α) → Prop`
///
/// The parametricity condition for a specific relation R and functor F:
/// the polymorphic function maps R-related inputs to F(R)-related outputs.
pub fn parametricity_condition_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            pi(
                BinderInfo::Default,
                "R",
                app2(cst("Rel"), bvar(1), bvar(0)),
                impl_pi(
                    "F",
                    arrow(type0(), type0()),
                    arrow(
                        pi(BinderInfo::Default, "α", type0(), app(bvar(1), bvar(0))),
                        prop(),
                    ),
                ),
            ),
        ),
    )
}
/// `RelTypeConstructor : (Type → Type) → (Type → Type → Type)`
///
/// Lifting of type constructors to relational type constructors.
/// `RelTypeConstructor F A B = Rel (F A) (F B)`.
pub fn rel_type_constructor_ty() -> Expr {
    arrow(
        arrow(type0(), type0()),
        arrow(type0(), arrow(type0(), type0())),
    )
}
/// `FreeTheorem : ∀ (τ : Type), Prop`
///
/// The free theorem associated to a polymorphic type τ.
/// By Wadler's theorem-for-free, every inhabitant of ∀ α. τ(α) satisfies a
/// non-trivial equational property determined by τ.
pub fn free_theorem_ty() -> Expr {
    arrow(type0(), prop())
}
/// `FreeTheoremId : ∀ {A B : Type} (f : ∀ α, α → α),
///     ∀ (x : A), f A x = x`
///
/// Free theorem for the identity type ∀ α, α → α:
/// Any parametric function of this type must be the identity.
pub fn free_theorem_id_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            pi(
                BinderInfo::Default,
                "f",
                pi(BinderInfo::Default, "α", type0(), arrow(bvar(0), bvar(0))),
                pi(
                    BinderInfo::Default,
                    "x",
                    bvar(2),
                    app3(cst("Eq"), bvar(3), app(bvar(1), bvar(3)), bvar(0)),
                ),
            ),
        ),
    )
}
/// `FreeTheoremList : ∀ {A B : Type} (f : ∀ α, List α → List α) (g : A → B),
///     map g (f A xs) = f B (map g xs)`
///
/// Free theorem for ∀ α, List α → List α:
/// Every parametric such function commutes with map.
pub fn free_theorem_list_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            pi(
                BinderInfo::Default,
                "f",
                pi(
                    BinderInfo::Default,
                    "α",
                    type0(),
                    arrow(app(cst("List"), bvar(0)), app(cst("List"), bvar(0))),
                ),
                pi(BinderInfo::Default, "g", arrow(bvar(2), bvar(1)), prop()),
            ),
        ),
    )
}
/// `FreeTheoremFold : ∀ {A B : Type}
///     (f : ∀ α β, (α → β → β) → β → List α → β),
///     ∀ (h : A → B) (op : A → A → A) (z : A),
///     h (f A A op z xs) = f B B (fun a b -> h (op a ...)) (h z) (map h xs)`
///
/// A free theorem for fold-like polymorphic functions.
pub fn free_theorem_fold_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            pi(
                BinderInfo::Default,
                "f",
                pi(
                    BinderInfo::Default,
                    "α",
                    type0(),
                    pi(
                        BinderInfo::Default,
                        "β",
                        type0(),
                        arrow(
                            arrow(bvar(1), arrow(bvar(0), bvar(0))),
                            arrow(bvar(1), arrow(app(cst("List"), bvar(2)), bvar(2))),
                        ),
                    ),
                ),
                prop(),
            ),
        ),
    )
}
/// `Naturality : ∀ {F G : Type → Type} (η : ∀ α, F α → G α),
///     ∀ {A B : Type} (f : A → B), G f ∘ η A = η B ∘ F f`
///
/// A parametric polymorphic function η is a natural transformation.
pub fn naturality_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        impl_pi(
            "G",
            arrow(type0(), type0()),
            pi(
                BinderInfo::Default,
                "η",
                pi(
                    BinderInfo::Default,
                    "α",
                    type0(),
                    arrow(app(bvar(2), bvar(0)), app(bvar(2), bvar(0))),
                ),
                impl_pi(
                    "A",
                    type0(),
                    impl_pi("B", type0(), arrow(arrow(bvar(1), bvar(0)), prop())),
                ),
            ),
        ),
    )
}
/// `DiNaturality : ∀ {F : Type → Type → Type} (α : ∀ A, F A A),
///     ∀ {A B : Type} (f : A → B), ...`
///
/// Dinaturality: a parametric function of mixed-variance type is a dinatural
/// transformation, meaning it satisfies a hexagon identity.
pub fn dinaturality_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), arrow(type0(), type0())),
        pi(
            BinderInfo::Default,
            "α",
            pi(
                BinderInfo::Default,
                "A",
                type0(),
                app2(bvar(1), bvar(0), bvar(0)),
            ),
            impl_pi(
                "A",
                type0(),
                impl_pi("B", type0(), arrow(arrow(bvar(1), bvar(0)), prop())),
            ),
        ),
    )
}
/// `NaturalTransformation : (Type → Type) → (Type → Type) → Type`
///
/// The type of natural transformations between two functors F and G.
/// `NaturalTransformation F G = { η : ∀ α, F α → G α | Naturality η }`.
pub fn natural_transformation_ty() -> Expr {
    arrow(
        arrow(type0(), type0()),
        arrow(arrow(type0(), type0()), type0()),
    )
}
/// `NaturalIso : (Type → Type) → (Type → Type) → Type`
///
/// A natural isomorphism between functors.
pub fn natural_iso_ty() -> Expr {
    arrow(
        arrow(type0(), type0()),
        arrow(arrow(type0(), type0()), type0()),
    )
}
/// `AbstractType : Type`
///
/// An abstract type is an existential package: `∃ (α : Type), Interface α`.
/// This models information hiding and encapsulation.
pub fn abstract_type_ty() -> Expr {
    type0()
}
/// `AbstractPackage : ∀ (Interface : Type → Prop), Type`
///
/// A package hiding the concrete representation type:
/// `AbstractPackage I = ∃ (α : Type), I α × α`.
pub fn abstract_package_ty() -> Expr {
    arrow(arrow(type0(), prop()), type0())
}
/// `RepresentationIndependence : ∀ {I : Type → Prop}
///     (pkg1 pkg2 : AbstractPackage I), Prop`
///
/// Two abstract packages with the same interface are observationally equivalent:
/// no client program can distinguish them.
pub fn representation_independence_ty() -> Expr {
    impl_pi(
        "I",
        arrow(type0(), prop()),
        arrow(
            app(cst("AbstractPackage"), bvar(0)),
            arrow(app(cst("AbstractPackage"), bvar(1)), prop()),
        ),
    )
}
/// `AbstractionTheorem : ∀ {I : Type → Prop}
///     (pkg1 pkg2 : AbstractPackage I),
///     RepresentationIndependence pkg1 pkg2`
///
/// Reynolds' abstraction theorem: any two implementations of an interface
/// are representationally independent.
pub fn abstraction_theorem_ty() -> Expr {
    impl_pi(
        "I",
        arrow(type0(), prop()),
        pi(
            BinderInfo::Default,
            "pkg1",
            app(cst("AbstractPackage"), bvar(0)),
            pi(
                BinderInfo::Default,
                "pkg2",
                app(cst("AbstractPackage"), bvar(1)),
                app2(cst("RepresentationIndependence"), bvar(1), bvar(0)),
            ),
        ),
    )
}
/// `ExistentialType : (Type → Type) → Type`
///
/// Existential quantification over types: `∃ α. F α`.
/// Models abstract data types in System F.
pub fn existential_type_ty() -> Expr {
    arrow(arrow(type0(), type0()), type0())
}
/// `Pack : ∀ {F : Type → Type} (A : Type), F A → ExistentialType F`
///
/// Packing a concrete implementation into an abstract package.
pub fn pack_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        pi(
            BinderInfo::Default,
            "A",
            type0(),
            arrow(app(bvar(1), bvar(0)), app(cst("ExistentialType"), bvar(2))),
        ),
    )
}
/// `Unpack : ∀ {F : Type → Type} {R : Prop},
///     ExistentialType F → (∀ A, F A → R) → R`
///
/// Unpacking (elimination) for existential types.
pub fn unpack_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        impl_pi(
            "R",
            prop(),
            arrow(
                app(cst("ExistentialType"), bvar(1)),
                arrow(
                    pi(
                        BinderInfo::Default,
                        "A",
                        type0(),
                        arrow(app(bvar(3), bvar(0)), bvar(2)),
                    ),
                    bvar(1),
                ),
            ),
        ),
    )
}
/// `LogicalRelation : ∀ (F : Type → Type), Type`
///
/// A logical relation over a type constructor F:
/// `LogicalRelation F = ∀ A B, Rel A B → Rel (F A) (F B)`.
/// This is the relational interpretation of F in a parametric model.
pub fn logical_relation_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "F",
        arrow(type0(), type0()),
        pi(
            BinderInfo::Default,
            "A",
            type0(),
            pi(
                BinderInfo::Default,
                "B",
                type0(),
                arrow(
                    app2(cst("Rel"), bvar(1), bvar(0)),
                    app2(cst("Rel"), app(bvar(3), bvar(2)), app(bvar(3), bvar(1))),
                ),
            ),
        ),
    )
}
/// `FundamentalLemma : ∀ (τ : Type) (f : τ),
///     LogicalRelation_at τ (f, f)`
///
/// The fundamental lemma of logical relations: every typeable term is related
/// to itself by the logical relation of its type.
pub fn fundamental_lemma_ty() -> Expr {
    pi(BinderInfo::Default, "τ", type0(), arrow(bvar(0), prop()))
}
/// `ClosedRelation : ∀ {F : Type → Type}, LogicalRelation F → Prop`
///
/// A logical relation is closed (a.k.a. admissible) if it is preserved by
/// least fixed points (needed for recursive types).
pub fn closed_relation_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        arrow(app(cst("LogicalRelation"), bvar(0)), prop()),
    )
}
/// `RelationalModel : Type`
///
/// A relational model of System F: assigns to each type a carrier set and
/// a logical relation, satisfying the parametricity condition for all terms.
pub fn relational_model_ty() -> Expr {
    type0()
}
/// `ParametricModel : RelationalModel → Prop`
///
/// A relational model is parametric if every definable function in System F
/// is interpreted by a parametric element (one that lives in all logical relations
/// connecting related inputs).
pub fn parametric_model_ty() -> Expr {
    arrow(cst("RelationalModel"), prop())
}
/// `SystemFType : Type`
///
/// Types of System F (second-order polymorphic lambda calculus):
/// ∀ α. τ, τ₁ → τ₂, base types.
pub fn system_f_type_ty() -> Expr {
    type0()
}
/// `SystemFTerm : SystemFType → Type`
///
/// Terms of System F of a given type.
pub fn system_f_term_ty() -> Expr {
    arrow(cst("SystemFType"), type0())
}
/// `PolymorphicId : ∀ (α : Type), α → α`
///
/// The polymorphic identity function: the unique inhabitant of ∀ α, α → α
/// (up to parametricity).
pub fn polymorphic_id_ty() -> Expr {
    pi(BinderInfo::Default, "α", type0(), arrow(bvar(0), bvar(0)))
}
/// `PolymorphicConst : ∀ (α β : Type), α → β → α`
///
/// The polymorphic constant (K combinator): the unique inhabitant of ∀ α β, α → β → α.
pub fn polymorphic_const_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "α",
        type0(),
        pi(
            BinderInfo::Default,
            "β",
            type0(),
            arrow(bvar(1), arrow(bvar(1), bvar(2))),
        ),
    )
}
/// `PolymorphicFlip : ∀ (α β γ : Type), (α → β → γ) → β → α → γ`
///
/// The flip combinator.
pub fn polymorphic_flip_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "α",
        type0(),
        pi(
            BinderInfo::Default,
            "β",
            type0(),
            pi(
                BinderInfo::Default,
                "γ",
                type0(),
                arrow(
                    arrow(bvar(2), arrow(bvar(1), bvar(0))),
                    arrow(bvar(2), arrow(bvar(3), bvar(3))),
                ),
            ),
        ),
    )
}
/// `ChurchNumeral : (∀ α, (α → α) → α → α) → Nat`
///
/// Church numerals in System F: ∀ α. (α → α) → α → α.
/// Parametricity implies these are exactly the natural numbers.
pub fn church_numeral_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "α",
        type0(),
        arrow(arrow(bvar(0), bvar(0)), arrow(bvar(1), bvar(1))),
    )
}
/// `ChurchBool : (∀ α, α → α → α) → Bool`
///
/// Church booleans in System F: ∀ α. α → α → α.
/// Parametricity implies these are exactly true and false.
pub fn church_bool_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "α",
        type0(),
        arrow(bvar(0), arrow(bvar(1), bvar(1))),
    )
}
/// `CoherenceTheorem : ∀ {F : Type → Type}
///     (f g : ∀ α, F α), f = g`
///
/// The coherence theorem via parametricity:
/// In a polymorphic type with sufficient constraints (e.g., functor laws),
/// all parallel morphisms are equal. Parametricity gives coherence for free.
pub fn coherence_theorem_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        pi(
            BinderInfo::Default,
            "f",
            pi(BinderInfo::Default, "α", type0(), app(bvar(1), bvar(0))),
            pi(
                BinderInfo::Default,
                "g",
                pi(BinderInfo::Default, "α", type0(), app(bvar(2), bvar(0))),
                app3(
                    cst("Eq"),
                    pi(BinderInfo::Default, "α", type0(), app(bvar(3), bvar(0))),
                    bvar(1),
                    bvar(0),
                ),
            ),
        ),
    )
}
/// `ParametricityCoherence : ∀ {A B : Type} {F : Type → Type → Type}
///     (f g : ∀ α β, F α β), f = g`
///
/// Coherence for bifunctors: parametricity forces all polymorphic terms
/// of the same type to be equal (up to the relevant equations).
pub fn parametricity_coherence_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            impl_pi(
                "F",
                arrow(type0(), arrow(type0(), type0())),
                pi(
                    BinderInfo::Default,
                    "f",
                    pi(
                        BinderInfo::Default,
                        "α",
                        type0(),
                        pi(
                            BinderInfo::Default,
                            "β",
                            type0(),
                            app2(bvar(3), bvar(1), bvar(0)),
                        ),
                    ),
                    pi(
                        BinderInfo::Default,
                        "g",
                        pi(
                            BinderInfo::Default,
                            "α",
                            type0(),
                            pi(
                                BinderInfo::Default,
                                "β",
                                type0(),
                                app2(bvar(4), bvar(1), bvar(0)),
                            ),
                        ),
                        prop(),
                    ),
                ),
            ),
        ),
    )
}
/// `UniqueInhabitant : ∀ (τ : ∀ α, F α), Prop`
///
/// Parametricity can imply uniqueness: some polymorphic types have a unique
/// (up to propositional equality) inhabitant by the free theorem.
pub fn unique_inhabitant_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        arrow(
            pi(BinderInfo::Default, "α", type0(), app(bvar(1), bvar(0))),
            prop(),
        ),
    )
}
/// `UniversalProperty : ∀ {F : Type → Type}, (∀ α, F α) → Prop`
///
/// A polymorphic function satisfies a universal property if it is the unique
/// natural transformation of its type — which parametricity guarantees for
/// many common functors.
pub fn universal_property_ty() -> Expr {
    impl_pi(
        "F",
        arrow(type0(), type0()),
        arrow(
            pi(BinderInfo::Default, "α", type0(), app(bvar(1), bvar(0))),
            prop(),
        ),
    )
}
/// `KripkeRelation : ∀ (W : Type) (A B : Type), Type`
///
/// A Kripke relation over a world type W between A and B:
/// `W → A → B → Prop`. Used in step-indexed logical relations.
pub fn kripke_relation_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "W",
        type0(),
        pi(
            BinderInfo::Default,
            "A",
            type0(),
            pi(
                BinderInfo::Default,
                "B",
                type0(),
                arrow(bvar(2), arrow(bvar(2), arrow(bvar(2), prop()))),
            ),
        ),
    )
}
/// `StepIndexedRelation : ∀ (A B : Type), Type`
///
/// A step-indexed (Appel-McAllester) logical relation between A and B,
/// indexed by a step count n : Nat.
/// Used to prove parametricity for recursive types (iso-recursive, equi-recursive).
pub fn step_indexed_relation_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "B",
            type0(),
            arrow(nat_ty(), arrow(bvar(1), arrow(bvar(2), prop()))),
        ),
    )
}
/// `BiorthogonalityRelation : ∀ (A : Type), Type`
///
/// The biorthogonality (⊥⊥) construction used in orthogonality-based
/// logical relations (e.g., Pitts, Bierman-Pitts).
/// `Biorthogonal A = { S : Set A | S = S^⊥⊥ }`.
pub fn biorthogonality_relation_ty() -> Expr {
    arrow(type0(), type0())
}
/// `AdmissibleRelation : ∀ (A B : Type), Rel A B → Prop`
///
/// An admissible relation is one closed under directed limits (needed for
/// domain-theoretic models of parametricity).
pub fn admissible_relation_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "B",
            type0(),
            arrow(app2(cst("Rel"), bvar(1), bvar(0)), prop()),
        ),
    )
}
/// `PiType : ∀ (A : Type) (B : A → Type), Type`
///
/// Dependent function type (Pi-type): Π (x : A), B x.
/// The fundamental dependent type construct.
pub fn pi_type_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        arrow(arrow(bvar(0), type0()), type0()),
    )
}
/// `SigmaType : ∀ (A : Type) (B : A → Type), Type`
///
/// Dependent pair type (Sigma-type): Σ (x : A), B x.
/// The existential type at the propositions-as-types level.
pub fn sigma_type_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        arrow(arrow(bvar(0), type0()), type0()),
    )
}
/// `SigmaFst : ∀ {A : Type} {B : A → Type}, Sigma A B → A`
///
/// First projection of a dependent pair.
pub fn sigma_fst_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            arrow(bvar(0), type0()),
            arrow(app2(cst("SigmaType"), bvar(1), bvar(0)), bvar(2)),
        ),
    )
}
/// `SigmaSnd : ∀ {A : Type} {B : A → Type} (p : Sigma A B), B (fst p)`
///
/// Second projection of a dependent pair.
pub fn sigma_snd_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            arrow(bvar(0), type0()),
            pi(
                BinderInfo::Default,
                "p",
                app2(cst("SigmaType"), bvar(1), bvar(0)),
                app(bvar(1), app(cst("SigmaFst"), bvar(0))),
            ),
        ),
    )
}
/// `PiExt : ∀ {A : Type} {B : A → Type} (f g : Π x, B x),
///     (∀ x, f x = g x) → f = g`
///
/// Eta-extensionality for Pi-types: pointwise equal functions are equal.
pub fn pi_ext_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            arrow(bvar(0), type0()),
            pi(
                BinderInfo::Default,
                "f",
                app2(cst("PiType"), bvar(1), bvar(0)),
                pi(
                    BinderInfo::Default,
                    "g",
                    app2(cst("PiType"), bvar(2), bvar(1)),
                    arrow(
                        pi(
                            BinderInfo::Default,
                            "x",
                            bvar(3),
                            app3(
                                cst("Eq"),
                                app(bvar(3), bvar(0)),
                                app(bvar(2), bvar(0)),
                                app(bvar(1), bvar(0)),
                            ),
                        ),
                        app3(
                            cst("Eq"),
                            app2(cst("PiType"), bvar(3), bvar(2)),
                            bvar(1),
                            bvar(0),
                        ),
                    ),
                ),
            ),
        ),
    )
}
/// `IdType : ∀ {A : Type}, A → A → Type`
///
/// Martin-Löf identity type: `Id A a b` is the type of proofs that `a = b`.
pub fn id_type_ty() -> Expr {
    impl_pi("A", type0(), arrow(bvar(0), arrow(bvar(1), type0())))
}
/// `IdRefl : ∀ {A : Type} (a : A), Id A a a`
///
/// Reflexivity: the canonical element of the identity type.
pub fn id_refl_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "a",
            bvar(0),
            app2(cst("IdType"), bvar(1), bvar(0)),
        ),
    )
}
/// `IdElim : ∀ {A : Type} (C : ∀ a b, Id A a b → Type)
///     (refl_case : ∀ a, C a a (IdRefl a))
///     {a b : A} (p : Id A a b), C a b p`
///
/// The J eliminator (path induction).
pub fn id_elim_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "C",
            pi(
                BinderInfo::Default,
                "a",
                bvar(0),
                pi(
                    BinderInfo::Default,
                    "b",
                    bvar(1),
                    arrow(app2(cst("IdType"), bvar(2), bvar(0)), type0()),
                ),
            ),
            pi(
                BinderInfo::Default,
                "refl_case",
                pi(
                    BinderInfo::Default,
                    "a",
                    bvar(1),
                    app3(bvar(2), bvar(0), bvar(0), app(cst("IdRefl"), bvar(0))),
                ),
                impl_pi(
                    "a",
                    bvar(2),
                    impl_pi(
                        "b",
                        bvar(3),
                        pi(
                            BinderInfo::Default,
                            "p",
                            app2(cst("IdType"), bvar(4), bvar(0)),
                            app3(bvar(4), bvar(2), bvar(1), bvar(0)),
                        ),
                    ),
                ),
            ),
        ),
    )
}
/// `IdSymm : ∀ {A : Type} {a b : A}, Id A a b → Id A b a`
///
/// Symmetry of the identity type.
pub fn id_symm_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "a",
            bvar(0),
            impl_pi(
                "b",
                bvar(1),
                arrow(
                    app2(cst("IdType"), bvar(2), bvar(0)),
                    app2(cst("IdType"), bvar(1), bvar(3)),
                ),
            ),
        ),
    )
}
/// `IdTrans : ∀ {A : Type} {a b c : A},
///     Id A a b → Id A b c → Id A a c`
///
/// Transitivity (composition) of identity types.
pub fn id_trans_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "a",
            bvar(0),
            impl_pi(
                "b",
                bvar(1),
                impl_pi(
                    "c",
                    bvar(2),
                    arrow(
                        app2(cst("IdType"), bvar(3), bvar(1)),
                        arrow(
                            app2(cst("IdType"), bvar(3), bvar(0)),
                            app2(cst("IdType"), bvar(4), bvar(2)),
                        ),
                    ),
                ),
            ),
        ),
    )
}
/// `TypeEquiv : ∀ (A B : Type), Type`
///
/// Type equivalence: A ≃ B. A pair (f : A → B, g : B → A) with
/// g ∘ f ~ id and f ∘ g ~ id.
pub fn type_equiv_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        pi(BinderInfo::Default, "B", type0(), type0()),
    )
}
/// `UnivalenceAxiom : ∀ (A B : Type), (A ≃ B) ≃ (Id Type A B)`
///
/// The univalence axiom (Voevodsky): equivalence of types is equivalent
/// to identity of types.  Implies function extensionality.
pub fn univalence_axiom_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "B",
            type0(),
            app2(
                cst("TypeEquiv"),
                app2(cst("TypeEquiv"), bvar(1), bvar(0)),
                app2(cst("IdType"), type0(), bvar(1)),
            ),
        ),
    )
}
/// `FunExt : ∀ {A : Type} {B : A → Type} (f g : Π x, B x),
///     (∀ x, f x = g x) → f = g`
///
/// Function extensionality: pointwise equal functions are propositionally equal.
/// A consequence of univalence.
pub fn funext_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            arrow(bvar(0), type0()),
            pi(
                BinderInfo::Default,
                "f",
                app2(cst("PiType"), bvar(1), bvar(0)),
                pi(
                    BinderInfo::Default,
                    "g",
                    app2(cst("PiType"), bvar(2), bvar(1)),
                    arrow(
                        pi(
                            BinderInfo::Default,
                            "x",
                            bvar(3),
                            app2(cst("IdType"), app(bvar(3), bvar(0)), app(bvar(2), bvar(0))),
                        ),
                        app2(cst("IdType"), bvar(1), bvar(0)),
                    ),
                ),
            ),
        ),
    )
}
/// `PropExt : ∀ (P Q : Prop), (P ↔ Q) → P = Q`
///
/// Propositional extensionality: logically equivalent propositions are equal.
pub fn prop_ext_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "P",
        prop(),
        pi(
            BinderInfo::Default,
            "Q",
            prop(),
            arrow(
                app2(cst("Iff"), bvar(1), bvar(0)),
                app2(cst("IdType"), bvar(1), bvar(0)),
            ),
        ),
    )
}
/// `Trunc : Type → Prop`
///
/// Propositional truncation (squash type): |A| is the proposition that A
/// is inhabited.  The mere existence quantifier.
pub fn trunc_ty() -> Expr {
    arrow(type0(), prop())
}
/// `TruncIn : ∀ {A : Type}, A → Trunc A`
///
/// Introduction rule for propositional truncation.
pub fn trunc_in_ty() -> Expr {
    impl_pi("A", type0(), arrow(bvar(0), app(cst("Trunc"), bvar(1))))
}
/// `TruncElim : ∀ {A : Type} {P : Prop},
///     (A → P) → Trunc A → P`
///
/// Elimination into propositions.
pub fn trunc_elim_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "P",
            prop(),
            arrow(
                arrow(bvar(1), bvar(0)),
                arrow(app(cst("Trunc"), bvar(2)), bvar(1)),
            ),
        ),
    )
}
/// `CircleHIT : Type`
///
/// The circle S¹ as a higher inductive type with one point and one path.
/// `base : S¹` and `loop : Id S¹ base base`.
pub fn circle_hit_ty() -> Expr {
    type0()
}
/// `SuspensionHIT : Type → Type`
///
/// Suspension ΣA of a type A, a HIT with North, South poles and a meridian
/// path for each `a : A`.
pub fn suspension_hit_ty() -> Expr {
    arrow(type0(), type0())
}
/// `IntervalHIT : Type`
///
/// The interval \[0,1\] as a HIT with two endpoints and a path between them.
pub fn interval_hit_ty() -> Expr {
    type0()
}
/// `PushoutHIT : ∀ {A B C : Type}, (A → B) → (A → C) → Type`
///
/// The pushout of two functions, a fundamental HIT for homotopy type theory.
pub fn pushout_hit_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            impl_pi(
                "C",
                type0(),
                arrow(
                    arrow(bvar(2), bvar(1)),
                    arrow(arrow(bvar(3), bvar(1)), type0()),
                ),
            ),
        ),
    )
}
/// `ObsEq : ∀ (A : Type) (a b : A), Prop`
///
/// Observational equality: a =_A b.  In OTT this is defined by recursion on
/// the type A, making it definitionally proof-irrelevant.
pub fn obs_eq_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        arrow(bvar(0), arrow(bvar(1), prop())),
    )
}
/// `ObsEqRefl : ∀ {A : Type} (a : A), ObsEq A a a`
///
/// Reflexivity of observational equality.
pub fn obs_eq_refl_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        pi(
            BinderInfo::Default,
            "a",
            bvar(0),
            app3(cst("ObsEq"), bvar(1), bvar(0), bvar(0)),
        ),
    )
}
/// `Coerce : ∀ {A B : Type}, A = B → A → B`
///
/// Coercion / transport along a type equality.
pub fn coerce_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            arrow(
                app2(cst("IdType"), bvar(1), bvar(0)),
                arrow(bvar(2), bvar(2)),
            ),
        ),
    )
}
/// `Coherence : ∀ {A B : Type} (p q : A = B), p = q`
///
/// Proof irrelevance for type equalities in OTT: all coercions from A to B
/// are propositionally equal.
pub fn coherence_ott_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            pi(
                BinderInfo::Default,
                "p",
                app2(cst("IdType"), bvar(1), bvar(0)),
                pi(
                    BinderInfo::Default,
                    "q",
                    app2(cst("IdType"), bvar(2), bvar(1)),
                    app2(cst("IdType"), bvar(1), bvar(0)),
                ),
            ),
        ),
    )
}
/// `FibrantType : Type → Prop`
///
/// Fibrant types are those that satisfy the Kan filling condition.
/// In two-level type theory, the inner level consists of fibrant types.
pub fn fibrant_type_ty() -> Expr {
    arrow(type0(), prop())
}
/// `StrictEquality : ∀ (A : Type) (a b : A), Prop`
///
/// Strict (definitional) equality in the outer level of 2LTT.
/// This is a decidable, trivial propositional equality.
pub fn strict_equality_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        arrow(bvar(0), arrow(bvar(1), prop())),
    )
}
/// `InnerToOuter : ∀ {A : Type}, FibrantType A → A → A`
///
/// Embedding from the inner fibrant level to the outer strict level.
pub fn inner_to_outer_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        arrow(app(cst("FibrantType"), bvar(0)), arrow(bvar(1), bvar(2))),
    )
}
/// `Setoid : Type`
///
/// A setoid is a type equipped with an equivalence relation.
/// `Setoid = Σ (A : Type), (A → A → Prop) × IsEquiv`.
pub fn setoid_ty() -> Expr {
    type0()
}
/// `SetoidMap : Setoid → Setoid → Type`
///
/// A setoid morphism respects the equivalence relations.
pub fn setoid_map_ty() -> Expr {
    arrow(cst("Setoid"), arrow(cst("Setoid"), type0()))
}
/// `SetoidEquiv : Setoid → Setoid → Prop`
///
/// Two setoids are equivalent if there is a setoid isomorphism between them.
pub fn setoid_equiv_ty() -> Expr {
    arrow(cst("Setoid"), arrow(cst("Setoid"), prop()))
}
/// `SetoidQuotient : ∀ (S : Setoid), Type`
///
/// The quotient type of a setoid: the type of equivalence classes.
pub fn setoid_quotient_ty() -> Expr {
    arrow(cst("Setoid"), type0())
}
/// `PCA : Type`
///
/// A Partial Combinatory Algebra (PCA): the base structure for realizability.
/// Equipped with an application operation that may be partial.
pub fn pca_ty() -> Expr {
    type0()
}
/// `Realizer : ∀ (A : Type), PCA → Prop`
///
/// A realizer for an element of A: a PCA element that witnesses the
/// computational content of an element of A.
pub fn realizer_ty() -> Expr {
    arrow(type0(), arrow(cst("PCA"), prop()))
}
/// `RealizabilityTripos : Type`
///
/// A tripos (a higher-order fibration) arising from realizability.
/// The Kleene realizability tripos is the canonical example.
pub fn realizability_tripos_ty() -> Expr {
    type0()
}
/// `EffectiveTopos : Type`
///
/// The effective topos (Hyland 1982): the realizability topos over
/// Kleene's first algebra. Models constructive mathematics with
/// all functions being computable.
pub fn effective_topos_ty() -> Expr {
    type0()
}
/// `PER : Type → Type`
///
/// A partial equivalence relation on a type A: a symmetric and transitive
/// (but not necessarily reflexive) relation.  The domain of a PER is the
/// set of elements related to themselves.
pub fn per_ty() -> Expr {
    arrow(type0(), type0())
}
/// `PERDomain : ∀ {A : Type}, PER A → A → Prop`
///
/// The domain of a PER: elements a such that R(a, a).
pub fn per_domain_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        arrow(app(cst("PER"), bvar(0)), arrow(bvar(1), prop())),
    )
}
/// `PERMap : ∀ {A B : Type}, PER A → PER B → (A → B) → Prop`
///
/// A function respects two PERs: if R(a, a') then S(f a, f a').
pub fn per_map_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            arrow(
                app(cst("PER"), bvar(1)),
                arrow(
                    app(cst("PER"), bvar(1)),
                    arrow(arrow(bvar(3), bvar(2)), prop()),
                ),
            ),
        ),
    )
}
/// `PERModel : Type`
///
/// The PER model of a type theory: each type is interpreted as a PER,
/// and terms are interpreted as functions respecting the PERs.
pub fn per_model_ty() -> Expr {
    type0()
}
/// `Orthogonal : ∀ {A B C D : Type}, (A → B) → (C → D) → Prop`
///
/// Two morphisms are orthogonal if every lifting problem has a unique solution:
/// `∀ (u : A → C) (v : B → D), ∃! h, h ∘ f = u ∧ g ∘ h = v`.
pub fn orthogonal_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            impl_pi(
                "C",
                type0(),
                impl_pi(
                    "D",
                    type0(),
                    arrow(
                        arrow(bvar(3), bvar(2)),
                        arrow(arrow(bvar(3), bvar(2)), prop()),
                    ),
                ),
            ),
        ),
    )
}
/// `WFS : Type`
///
/// A weak factorization system (WFS) on a category: a pair (L, R) of
/// morphism classes such that L ⊥ R and every morphism factors as
/// an L-map followed by an R-map.
pub fn wfs_ty() -> Expr {
    type0()
}
/// `SmallObjectArgument : ∀ (I : Type), WFS`
///
/// The small object argument: given a set of generating cofibrations I,
/// produces a WFS (cof(I), inj(I)).
pub fn small_object_argument_ty() -> Expr {
    arrow(type0(), cst("WFS"))
}
/// `DynType : Type`
///
/// The dynamic type: the type of dynamically-typed values.
/// Every type embeds into DynType, and extraction may fail at runtime.
pub fn dyn_type_ty() -> Expr {
    type0()
}
/// `Inject : ∀ {A : Type}, A → DynType`
///
/// Injection of a statically-typed value into the dynamic type.
pub fn inject_ty() -> Expr {
    impl_pi("A", type0(), arrow(bvar(0), cst("DynType")))
}
/// `Project : ∀ {A : Type}, DynType → Option A`
///
/// Projection (cast) from the dynamic type to a static type.
/// May fail if the runtime type does not match.
pub fn project_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        arrow(cst("DynType"), app(cst("Option"), bvar(1))),
    )
}
/// `GradualConsistency : ∀ (A B : Type), Prop`
///
/// Type consistency in gradual typing: A ~ B holds when A and B are
/// compatible under the gradualization, i.e., one could be a specialization
/// of the other via the dynamic type.
pub fn gradual_consistency_ty() -> Expr {
    pi(
        BinderInfo::Default,
        "A",
        type0(),
        pi(BinderInfo::Default, "B", type0(), prop()),
    )
}
/// `CastEvidence : ∀ {A B : Type}, GradualConsistency A B → A → B`
///
/// A cast with explicit evidence of type consistency.
pub fn cast_evidence_ty() -> Expr {
    impl_pi(
        "A",
        type0(),
        impl_pi(
            "B",
            type0(),
            arrow(
                app2(cst("GradualConsistency"), bvar(1), bvar(0)),
                arrow(bvar(2), bvar(2)),
            ),
        ),
    )
}
/// `EffectSig : Type`
///
/// An algebraic effect signature: a set of operations with arities.
/// Example: State sig = { get : Unit → S, put : S → Unit }.
pub fn effect_sig_ty() -> Expr {
    type0()
}
/// `EffectTree : EffectSig → Type → Type`
///
/// The free monad over an effect signature: computation trees.
/// `EffectTree Σ A = Pure A | Op (Σ.op) (Σ.arity op → EffectTree Σ A)`.
pub fn effect_tree_ty() -> Expr {
    arrow(cst("EffectSig"), arrow(type0(), type0()))
}
/// `Handler : ∀ {Σ : EffectSig} {A B : Type},
///     EffectTree Σ A → (A → B) → (∀ op, Σ.arity op → (Σ.result op → B) → B) → B`
///
/// Effect handler: interprets an effect tree using a pure return case
/// and operation cases.
pub fn handler_ty() -> Expr {
    impl_pi(
        "Σ",
        cst("EffectSig"),
        impl_pi(
            "A",
            type0(),
            impl_pi(
                "B",
                type0(),
                arrow(
                    app2(cst("EffectTree"), bvar(2), bvar(1)),
                    arrow(arrow(bvar(2), bvar(1)), bvar(2)),
                ),
            ),
        ),
    )
}
/// `MonadLaw : ∀ (M : Type → Type), Prop`
///
/// The monad laws for M: left unit, right unit, and associativity of bind.
pub fn monad_law_ty() -> Expr {
    pi(BinderInfo::Default, "M", arrow(type0(), type0()), prop())
}
/// `MonadMorphism : ∀ {M N : Type → Type}, (∀ A, M A → N A) → Prop`
///
/// A monad morphism: a natural transformation between monads that commutes
/// with return and bind.
pub fn monad_morphism_ty() -> Expr {
    impl_pi(
        "M",
        arrow(type0(), type0()),
        impl_pi(
            "N",
            arrow(type0(), type0()),
            arrow(
                pi(
                    BinderInfo::Default,
                    "A",
                    type0(),
                    arrow(app(bvar(2), bvar(0)), app(bvar(2), bvar(0))),
                ),
                prop(),
            ),
        ),
    )
}
/// Register all parametricity and free theorem axioms into the kernel environment.
#[allow(clippy::too_many_arguments)]
pub fn register_parametricity(env: &mut Environment) {
    let axioms: &[(&str, Expr)] = &[
        ("Rel", rel_ty()),
        ("RelId", rel_id_ty()),
        ("RelComp", rel_comp_ty()),
        ("RelConverse", rel_converse_ty()),
        ("RelFun", rel_fun_ty()),
        ("ReynoldsParametricity", reynolds_parametricity_ty()),
        ("ParametricFunction", parametric_function_ty()),
        ("ParametricityCondition", parametricity_condition_ty()),
        ("RelTypeConstructor", rel_type_constructor_ty()),
        ("FreeTheorem", free_theorem_ty()),
        ("FreeTheoremId", free_theorem_id_ty()),
        ("FreeTheoremList", free_theorem_list_ty()),
        ("FreeTheoremFold", free_theorem_fold_ty()),
        ("Naturality", naturality_ty()),
        ("DiNaturality", dinaturality_ty()),
        ("NaturalTransformation", natural_transformation_ty()),
        ("NaturalIso", natural_iso_ty()),
        ("AbstractType", abstract_type_ty()),
        ("AbstractPackage", abstract_package_ty()),
        (
            "RepresentationIndependence",
            representation_independence_ty(),
        ),
        ("AbstractionTheorem", abstraction_theorem_ty()),
        ("ExistentialType", existential_type_ty()),
        ("Pack", pack_ty()),
        ("Unpack", unpack_ty()),
        ("LogicalRelation", logical_relation_ty()),
        ("FundamentalLemma", fundamental_lemma_ty()),
        ("ClosedRelation", closed_relation_ty()),
        ("RelationalModel", relational_model_ty()),
        ("ParametricModel", parametric_model_ty()),
        ("SystemFType", system_f_type_ty()),
        ("SystemFTerm", system_f_term_ty()),
        ("PolymorphicId", polymorphic_id_ty()),
        ("PolymorphicConst", polymorphic_const_ty()),
        ("PolymorphicFlip", polymorphic_flip_ty()),
        ("ChurchNumeral", church_numeral_ty()),
        ("ChurchBool", church_bool_ty()),
        ("CoherenceTheorem", coherence_theorem_ty()),
        ("ParametricityCoherence", parametricity_coherence_ty()),
        ("UniqueInhabitant", unique_inhabitant_ty()),
        ("UniversalProperty", universal_property_ty()),
        ("KripkeRelation", kripke_relation_ty()),
        ("StepIndexedRelation", step_indexed_relation_ty()),
        ("BiorthogonalityRelation", biorthogonality_relation_ty()),
        ("AdmissibleRelation", admissible_relation_ty()),
        ("PiType", pi_type_ty()),
        ("SigmaType", sigma_type_ty()),
        ("SigmaFst", sigma_fst_ty()),
        ("SigmaSnd", sigma_snd_ty()),
        ("PiExt", pi_ext_ty()),
        ("IdType", id_type_ty()),
        ("IdRefl", id_refl_ty()),
        ("IdElim", id_elim_ty()),
        ("IdSymm", id_symm_ty()),
        ("IdTrans", id_trans_ty()),
        ("TypeEquiv", type_equiv_ty()),
        ("UnivalenceAxiom", univalence_axiom_ty()),
        ("FunExt", funext_ty()),
        ("PropExt", prop_ext_ty()),
        ("Trunc", trunc_ty()),
        ("TruncIn", trunc_in_ty()),
        ("TruncElim", trunc_elim_ty()),
        ("CircleHIT", circle_hit_ty()),
        ("SuspensionHIT", suspension_hit_ty()),
        ("IntervalHIT", interval_hit_ty()),
        ("PushoutHIT", pushout_hit_ty()),
        ("ObsEq", obs_eq_ty()),
        ("ObsEqRefl", obs_eq_refl_ty()),
        ("Coerce", coerce_ty()),
        ("CoherenceOTT", coherence_ott_ty()),
        ("FibrantType", fibrant_type_ty()),
        ("StrictEquality", strict_equality_ty()),
        ("InnerToOuter", inner_to_outer_ty()),
        ("Setoid", setoid_ty()),
        ("SetoidMap", setoid_map_ty()),
        ("SetoidEquiv", setoid_equiv_ty()),
        ("SetoidQuotient", setoid_quotient_ty()),
        ("PCA", pca_ty()),
        ("Realizer", realizer_ty()),
        ("RealizabilityTripos", realizability_tripos_ty()),
        ("EffectiveTopos", effective_topos_ty()),
        ("PER", per_ty()),
        ("PERDomain", per_domain_ty()),
        ("PERMap", per_map_ty()),
        ("PERModel", per_model_ty()),
        ("Orthogonal", orthogonal_ty()),
        ("WFS", wfs_ty()),
        ("SmallObjectArgument", small_object_argument_ty()),
        ("DynType", dyn_type_ty()),
        ("Inject", inject_ty()),
        ("Project", project_ty()),
        ("GradualConsistency", gradual_consistency_ty()),
        ("CastEvidence", cast_evidence_ty()),
        ("EffectSig", effect_sig_ty()),
        ("EffectTree", effect_tree_ty()),
        ("Handler", handler_ty()),
        ("MonadLaw", monad_law_ty()),
        ("MonadMorphism", monad_morphism_ty()),
    ];
    for (name, ty) in axioms {
        env.add(Declaration::Axiom {
            name: Name::str(*name),
            univ_params: vec![],
            ty: ty.clone(),
        })
        .ok();
    }
}