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
|
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NoImplicitPrelude #-}
-- | Small proof-producing natural-deduction combinators. Every resulting tree
-- is still replayed independently before it can authorize a fact.
module Felix.Checking.Kernel.Proof
( ProofContext
, rootProofContext
, proofContextTypes
, scopedTerm
, BuiltProof
, builtProofStatement
, builtProofDerivation
, importedProof
, foundationProof
, hypothesisProof
, implicationEliminationProof
, implicationIntroductionProof
, forallEliminationProof
, forallIntroductionProof
, falsumEliminationProof
, equalityReflexivityProof
, equalityReverseProof
, equalityCongruenceApplicationProof
, equalityModusPonensProof
, conversionProof
, doubleNegationEliminationProof
, conjunctionTerm
, conjunctionIntroductionProof
, conjunctionLeftProof
, conjunctionRightProof
, disjunctionTerm
, disjunctionLeftProof
, disjunctionRightProof
, disjunctionEliminationProof
, validateCaseAnalysisComposition
, validateDoubleNegationComposition
, validateFalsumEliminationComposition
, validateSetInductionComposition
, existentialTerm
, existentialIntroductionProof
, existentialEliminationProof
, setLfpBoundProof
, setLfpFixedProof
, setLfpInductProof
, KernelProofBuildError(..)
) where
import Base
import Felix.Checking.Core
import Felix.Checking.Foundation
import Felix.Checking.Kernel.Derivation
import Felix.Checking.Kernel.Semantics qualified as Semantics
import Felix.Checking.Kernel.SetLfp qualified as SetLfp
import Control.Monad (unless)
import Data.Bifunctor (first)
import Data.List qualified as List
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as Text
import Numeric.Natural (Natural)
data ProofContext global = ProofContext
!CheckedFoundation
!(global -> Maybe CoreType)
![CoreType]
![ScopedCheckedCore global]
rootProofContext
:: CheckedFoundation
-> (global -> Maybe CoreType)
-> ProofContext global
rootProofContext foundation globalType =
ProofContext foundation globalType [] []
proofContextTypes
:: ProofContext global
-> [CoreType]
proofContextTypes
(ProofContext
_foundation
_globalType
context
_hypotheses) =
context
data BuiltProof global = BuiltProof
!(ScopedCheckedCore global)
!(KernelDerivation global)
builtProofStatement
:: BuiltProof global
-> ScopedCheckedCore global
builtProofStatement
(BuiltProof statement _derivation) =
statement
builtProofDerivation
:: BuiltProof global
-> KernelDerivation global
builtProofDerivation
(BuiltProof _statement derivation) =
derivation
data KernelProofBuildError
= ProofTermIllTyped !CoreCheckError
| ProofSemanticsFailed
!Semantics.KernelSemanticsError
| ProofFoundationArgumentMismatch
!FoundationAxiomTag
| ProofHypothesisNotFound
| ProofExpectedEquality
| ProofExpectedUnaryBinder
| ProofSetLfpRuleFailed !Text
| ProofConversionPlanFailed !Text
| ProofStructuralCompositionMismatch !Text
deriving stock (Show, Eq)
scopedTerm
:: ProofContext global
-> CanonicalTerm global
-> Either
KernelProofBuildError
(ScopedCheckedCore global)
scopedTerm
(ProofContext
_foundation
globalType
context
_hypotheses) =
first ProofTermIllTyped
. checkScopedCanonicalCore
globalType
context
importedProof
:: ProofContext global
-> ImportIx
-> FrozenCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
importedProof context index statement =
pure
(BuiltProof
(embedClosedCore
(proofContextTypes context)
statement)
(importedFactDerivation index))
foundationProof
:: ProofContext global
-> FoundationAxiomTag
-> Either
KernelProofBuildError
(BuiltProof global)
foundationProof
context@(ProofContext
foundation
_globalType
_types
_hypotheses)
tag =
pure
(BuiltProof
(embedClosedCore
(proofContextTypes context)
(mapFrozenGlobals
absurd
(foundationAxiomFrozen
foundation
tag)))
(foundationFactDerivation tag))
hypothesisProof
:: Eq global
=> ProofContext global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
hypothesisProof
(ProofContext
_foundation
_globalType
_context
hypotheses)
statement =
case List.findIndex (== statement) hypotheses of
Nothing ->
Left ProofHypothesisNotFound
Just index ->
pure
(BuiltProof
statement
(localHypothesisDerivation
(hypothesisIx
(fromIntegral index))))
implicationEliminationProof
:: Eq global
=> ProofContext global
-> BuiltProof global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
implicationEliminationProof
(ProofContext
_foundation
globalType
_context
_hypotheses)
implication
premise = do
conclusion <-
first ProofSemanticsFailed
(Semantics.implicationElimination
globalType
(builtProofStatement implication)
(builtProofStatement premise))
pure
(BuiltProof
conclusion
(implicationEliminationDerivation
(builtProofDerivation implication)
(builtProofDerivation premise)))
implicationIntroductionProof
:: ProofContext global
-> ScopedCheckedCore global
-> ( ProofContext global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
)
-> Either
KernelProofBuildError
(BuiltProof global)
implicationIntroductionProof
(ProofContext
foundation
globalType
types
hypotheses)
premise
buildBody = do
let extended =
ProofContext
foundation
globalType
types
(premise : hypotheses)
premiseProof =
BuiltProof
premise
(localHypothesisDerivation
(hypothesisIx 0))
body <-
buildBody extended premiseProof
conclusion <-
first ProofSemanticsFailed
(Semantics.implicationIntroduction
globalType
premise
(builtProofStatement body))
pure
(BuiltProof
conclusion
(implicationIntroductionDerivation
premise
(builtProofDerivation body)))
forallEliminationProof
:: ProofContext global
-> BuiltProof global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
forallEliminationProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
quantified
argument = do
conclusion <-
first ProofSemanticsFailed
(Semantics.forallElimination
globalType
(builtProofStatement quantified)
argument)
pure
(BuiltProof
conclusion
(forallEliminationDerivation
(builtProofDerivation quantified)
argument))
forallIntroductionProof
:: ProofContext global
-> CoreType
-> ( ProofContext global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
)
-> Either
KernelProofBuildError
(BuiltProof global)
forallIntroductionProof
(ProofContext
foundation
globalType
types
hypotheses)
binderType
buildBody = do
weakenedHypotheses <-
traverse
(first ProofTermIllTyped
. weakenScopedCore
globalType
binderType)
hypotheses
let extended =
ProofContext
foundation
globalType
(binderType : types)
weakenedHypotheses
variable <-
scopedTerm extended (CBound 0)
body <-
buildBody extended variable
conclusion <-
first ProofSemanticsFailed
(Semantics.forallIntroduction
globalType
binderType
(builtProofStatement body))
pure
(BuiltProof
conclusion
(forallIntroductionDerivation
binderType
(builtProofDerivation body)))
falsumEliminationProof
:: ProofContext global
-> BuiltProof global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
falsumEliminationProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
falsum
target = do
conclusion <-
first ProofSemanticsFailed
(Semantics.falsumElimination
globalType
(builtProofStatement falsum)
target)
pure
(BuiltProof
conclusion
(falsumEliminationDerivation
(builtProofDerivation falsum)
target))
equalityReflexivityProof
:: ProofContext global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
equalityReflexivityProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
operand = do
equality <-
first ProofSemanticsFailed
(Semantics.equalityReflexivity
globalType
operand)
pure
(BuiltProof
equality
(scopedEqualityReflexivityDerivation
operand))
equalityCongruenceApplicationProof
:: ProofContext global
-> BuiltProof global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
equalityCongruenceApplicationProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
functionEquality
argumentEquality = do
equality <-
first ProofSemanticsFailed
(Semantics.equalityCongruenceApplication
globalType
(builtProofStatement functionEquality)
(builtProofStatement argumentEquality))
pure
(BuiltProof
equality
(equalityCongruenceApplicationDerivation
(builtProofDerivation
functionEquality)
(builtProofDerivation
argumentEquality)))
equalityModusPonensProof
:: Eq global
=> ProofContext global
-> BuiltProof global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
equalityModusPonensProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
equality
premise = do
conclusion <-
first ProofSemanticsFailed
(Semantics.equalityModusPonens
globalType
(builtProofStatement equality)
(builtProofStatement premise))
pure
(BuiltProof
conclusion
(equalityModusPonensDerivation
(builtProofDerivation equality)
(builtProofDerivation premise)))
conversionProof
:: Eq global
=> ProofContext global
-> BuiltProof global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
conversionProof
(ProofContext
_foundation
globalType
_types
_hypotheses)
source
target = do
plan <-
first (ProofConversionPlanFailed . Text.pack . show)
(conversionPlan 100000)
result <-
first ProofSemanticsFailed
(Semantics.convertJudgment
globalType
(conversionPlanBudget plan)
(builtProofStatement source)
target)
pure
(BuiltProof
result
(convertJudgmentDerivation
(builtProofDerivation source)
target
plan))
equalityReverseProof
:: Eq global
=> ProofContext global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
equalityReverseProof context equality = do
(operandType, left, right) <-
equalityParts
(builtProofStatement equality)
leftOperand <-
scopedTerm context left
weakenedLeft <-
first ProofTermIllTyped
(weakenScopedCore
(contextGlobalType context)
operandType
leftOperand)
function <-
scopedTerm context
(CLam operandType
(CEq
operandType
(CBound 0)
(scopedCoreTerm
weakenedLeft)))
functionReflexivity <-
equalityReflexivityProof
context
function
appliedEquality <-
equalityCongruenceApplicationProof
context
functionReflexivity
equality
leftReflexivity <-
equalityReflexivityProof
context
leftOperand
appliedLeft <-
scopedTerm context
(CApp
(scopedCoreTerm function)
left)
appliedLeftReflexivity <-
conversionProof
context
leftReflexivity
appliedLeft
reversedApplication <-
equalityModusPonensProof
context
appliedEquality
appliedLeftReflexivity
expected <-
scopedTerm context
(CEq operandType right left)
conversionProof
context
reversedApplication
expected
doubleNegationEliminationProof
:: Eq global
=> ProofContext global
-> ScopedCheckedCore global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
doubleNegationEliminationProof
context
target
doubleNegation = do
axiom <-
foundationProof
context
DoubleNegationElim
instanceProof <-
forallEliminationProof
context
axiom
target
implicationEliminationProof
context
instanceProof
doubleNegation
conjunctionTerm
:: CanonicalTerm global
-> CanonicalTerm global
-> CanonicalTerm global
conjunctionTerm left right =
CImp
(CImp left
(CImp right CFalsum))
CFalsum
conjunctionIntroductionProof
:: Eq global
=> ProofContext global
-> BuiltProof global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
conjunctionIntroductionProof
context
left
right = do
let leftTerm =
scopedCoreTerm
(builtProofStatement left)
rightTerm =
scopedCoreTerm
(builtProofStatement right)
refuter <-
scopedTerm context
(CImp leftTerm
(CImp rightTerm CFalsum))
implicationIntroductionProof
context
refuter
(\extended refuterProof -> do
firstApplication <-
implicationEliminationProof
extended
refuterProof
(weakenForHypothesis left)
implicationEliminationProof
extended
firstApplication
(weakenForHypothesis right))
conjunctionLeftProof
:: Eq global
=> ProofContext global
-> CanonicalTerm global
-> CanonicalTerm global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
conjunctionLeftProof context left right conjunction = do
leftProposition <-
scopedTerm context left
notLeft <-
scopedTerm context
(CImp left CFalsum)
doubleNegation <-
implicationIntroductionProof
context
notLeft
(\withNotLeft _notLeftProof -> do
leftAtRefuter <-
scopedTerm withNotLeft left
refuterProof <-
implicationIntroductionProof
withNotLeft
leftAtRefuter
(\withLeft leftProof -> do
rightProposition <-
scopedTerm withLeft right
implicationIntroductionProof
withLeft
rightProposition
(\withBoth _rightProof -> do
notLeftCurrent <-
hypothesisProof
withBoth
notLeft
implicationEliminationProof
withBoth
notLeftCurrent
(weakenForHypothesis
leftProof)))
implicationEliminationProof
withNotLeft
(weakenForHypothesis conjunction)
refuterProof)
doubleNegationEliminationProof
context
leftProposition
doubleNegation
conjunctionRightProof
:: Eq global
=> ProofContext global
-> CanonicalTerm global
-> CanonicalTerm global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
conjunctionRightProof context left right conjunction = do
rightProposition <-
scopedTerm context right
notRight <-
scopedTerm context
(CImp right CFalsum)
doubleNegation <-
implicationIntroductionProof
context
notRight
(\withNotRight _notRightProof -> do
leftAtRefuter <-
scopedTerm withNotRight left
refuterProof <-
implicationIntroductionProof
withNotRight
leftAtRefuter
(\withLeft _leftProof -> do
rightAtLeft <-
scopedTerm
withLeft
right
implicationIntroductionProof
withLeft
rightAtLeft
(\withBoth rightProof -> do
notRightCurrent <-
hypothesisProof
withBoth
notRight
implicationEliminationProof
withBoth
notRightCurrent
rightProof))
implicationEliminationProof
withNotRight
(weakenForHypothesis conjunction)
refuterProof)
doubleNegationEliminationProof
context
rightProposition
doubleNegation
disjunctionTerm
:: CanonicalTerm global
-> CanonicalTerm global
-> CanonicalTerm global
disjunctionTerm left right =
CImp
(CImp left CFalsum)
right
disjunctionLeftProof
:: Eq global
=> ProofContext global
-> CanonicalTerm global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
disjunctionLeftProof context right leftProof = do
notLeft <-
scopedTerm context
(CImp
(scopedCoreTerm
(builtProofStatement leftProof))
CFalsum)
rightTarget <-
scopedTerm context right
implicationIntroductionProof
context
notLeft
(\extended notLeftProof -> do
falsum <-
implicationEliminationProof
extended
notLeftProof
(weakenForHypothesis
leftProof)
falsumEliminationProof
extended
falsum
rightTarget)
disjunctionRightProof
:: ProofContext global
-> CanonicalTerm global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
disjunctionRightProof context left rightProof = do
notLeft <-
scopedTerm context
(CImp left CFalsum)
implicationIntroductionProof
context
notLeft
(\_extended _notLeftProof ->
pure
(weakenForHypothesis
rightProof))
disjunctionEliminationProof
:: Eq global
=> ProofContext global
-> CanonicalTerm global
-> CanonicalTerm global
-> BuiltProof global
-> ScopedCheckedCore global
-> ( ProofContext global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
)
-> ( ProofContext global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
)
-> Either
KernelProofBuildError
(BuiltProof global)
disjunctionEliminationProof
context
left
right
disjunction
result
leftCase
rightCase = do
leftProposition <-
scopedTerm context left
rightProposition <-
scopedTerm context right
leftImplication <-
implicationIntroductionProof
context
leftProposition
leftCase
rightImplication <-
implicationIntroductionProof
context
rightProposition
rightCase
notResult <-
scopedTerm context
(CImp
(scopedCoreTerm result)
CFalsum)
doubleNegation <-
implicationIntroductionProof
context
notResult
(\extended notResultProof -> do
leftAtNotResult <-
scopedTerm extended left
notLeftProof <-
implicationIntroductionProof
extended
leftAtNotResult
(\withLeft leftProof -> do
resultProof <-
implicationEliminationProof
withLeft
(weakenForHypothesis
(weakenForHypothesis
leftImplication))
leftProof
implicationEliminationProof
withLeft
(weakenForHypothesis
notResultProof)
resultProof)
rightProof <-
implicationEliminationProof
extended
(weakenForHypothesis
disjunction)
notLeftProof
resultProof <-
implicationEliminationProof
extended
(weakenForHypothesis
rightImplication)
rightProof
implicationEliminationProof
extended
notResultProof
resultProof)
doubleNegationEliminationProof
context
result
doubleNegation
-- | Validate the one structural rule used by exact source case analysis.
-- The branch proofs and the exhaustive disjunction are represented here by
-- exact hypotheses; the kernel combinators must derive the owned goal from
-- precisely those propositions. No derived proof escapes this check.
validateCaseAnalysisComposition
:: Eq global
=> CheckedFoundation
-> (global -> Maybe CoreType)
-> ScopedCheckedCore global
-> NonEmpty (ScopedCheckedCore global)
-> ScopedCheckedCore global
-> Either KernelProofBuildError ()
validateCaseAnalysisComposition
foundation globalType goal cases exhaustive = do
validatePropositionContext "case goal" lexicalContext goal
traverse_
(validatePropositionContext "case assumption" lexicalContext)
cases
validatePropositionContext
"case exhaustiveness target" lexicalContext exhaustive
let expectedExhaustive =
foldl1
(\left right ->
CImp
(CImp left CFalsum)
right)
(scopedCoreTerm <$> cases)
unless (scopedCoreTerm exhaustive == expectedExhaustive)
(Left
(ProofStructuralCompositionMismatch
"case exhaustiveness is not the source-ordered disjunction"))
branchImplications <-
traverse
(checkedImplication lexicalContext goal)
cases
let context =
ProofContext
foundation
globalType
lexicalContext
(exhaustive : toList branchImplications)
exhaustiveProof <- hypothesisProof context exhaustive
result <-
eliminateCases
context goal cases exhaustiveProof
unless (builtProofStatement result == goal)
(Left
(ProofStructuralCompositionMismatch
"case elimination did not derive the owned goal"))
where
lexicalContext = scopedCoreContext goal
checkedImplication expectedContext conclusion antecedent =
case implyScopedCore antecedent conclusion of
Just implication
| scopedCoreContext implication == expectedContext ->
pure implication
_ ->
Left
(ProofStructuralCompositionMismatch
"case branch implication changed context")
eliminateCases context result (only :| []) caseProof = do
branchImplication <-
scopedTerm context
(CImp
(scopedCoreTerm only)
(scopedCoreTerm result))
>>= hypothesisProof context
implicationEliminationProof context branchImplication caseProof
eliminateCases context result (firstCase :| rest) disjunctionProof = do
let allCases = firstCase :| rest
leftCases = NonEmpty.fromList (NonEmpty.init allCases)
rightCase = NonEmpty.last allCases
leftTerm =
foldl1 disjunctionTerm
(scopedCoreTerm <$> leftCases)
disjunctionEliminationProof
context
leftTerm
(scopedCoreTerm rightCase)
disjunctionProof
result
(\extended leftProof ->
eliminateCases extended result leftCases leftProof)
(\extended rightProof -> do
branchImplication <-
scopedTerm extended
(CImp
(scopedCoreTerm rightCase)
(scopedCoreTerm result))
>>= hypothesisProof extended
implicationEliminationProof
extended branchImplication rightProof)
-- | Validate the exact classical closing step for a proof by contradiction.
-- The only classical input is the confined 'DoubleNegationElim' foundation
-- row already consumed by 'doubleNegationEliminationProof'.
validateDoubleNegationComposition
:: Eq global
=> CheckedFoundation
-> (global -> Maybe CoreType)
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> Either KernelProofBuildError ()
validateDoubleNegationComposition
foundation globalType goal negation falsum = do
let lexicalContext = scopedCoreContext goal
validatePropositionContext "contradiction goal" lexicalContext goal
validatePropositionContext
"contradiction negation" lexicalContext negation
validatePropositionContext "contradiction falsum" lexicalContext falsum
unless (scopedCoreTerm falsum == CFalsum)
(Left
(ProofStructuralCompositionMismatch
"proof by contradiction did not target falsum"))
expectedNegation <-
checkedNegation lexicalContext goal
unless (negation == expectedNegation)
(Left
(ProofStructuralCompositionMismatch
"proof by contradiction did not own the exact negated goal"))
doubleNegation <-
checkedNegation lexicalContext negation
let context =
ProofContext
foundation globalType lexicalContext [doubleNegation]
hypothesis <- hypothesisProof context doubleNegation
result <- doubleNegationEliminationProof context goal hypothesis
unless (builtProofStatement result == goal)
(Left
(ProofStructuralCompositionMismatch
"double-negation elimination did not derive the owned goal"))
-- | Validate the exact ex-falso closing step used after a terminal indirect
-- contradiction discharge.
validateFalsumEliminationComposition
:: Eq global
=> CheckedFoundation
-> (global -> Maybe CoreType)
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> Either KernelProofBuildError ()
validateFalsumEliminationComposition foundation globalType goal falsum = do
let lexicalContext = scopedCoreContext goal
validatePropositionContext "contradiction goal" lexicalContext goal
validatePropositionContext "contradiction falsum" lexicalContext falsum
unless (scopedCoreTerm falsum == CFalsum)
(Left
(ProofStructuralCompositionMismatch
"falsum elimination did not receive falsum"))
let context =
ProofContext foundation globalType lexicalContext [falsum]
hypothesis <- hypothesisProof context falsum
result <- falsumEliminationProof context hypothesis goal
unless (builtProofStatement result == goal)
(Left
(ProofStructuralCompositionMismatch
"falsum elimination did not derive the owned goal"))
-- | Validate the exact structural instance used by source set induction.
-- The admitted child is represented by its generalized step proposition;
-- the checked foundation row must specialize to that exact premise and the
-- owned binder-level result. No induction principle becomes an ATP premise.
validateSetInductionComposition
:: Eq global
=> CheckedFoundation
-> (global -> Maybe CoreType)
-> Natural
-> ScopedCheckedCore global
-> [ScopedCheckedCore global]
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> Either KernelProofBuildError ()
validateSetInductionComposition
foundation globalType selected property antecedents childTarget
hypothesis result = do
let lexicalContext = scopedCoreContext property
traverse_
(validatePropositionContext
"set-induction antecedent" lexicalContext)
antecedents
validatePropositionContext
"set-induction property" lexicalContext property
validatePropositionContext
"set-induction child target" lexicalContext childTarget
validatePropositionContext
"set-induction hypothesis" lexicalContext hypothesis
validatePropositionContext
"set-induction result" lexicalContext result
expectedProperty <-
foldrM implyChecked childTarget antecedents
unless (property == expectedProperty)
(Left
(ProofStructuralCompositionMismatch
"set-induction property does not own the child target and guards"))
(predicate, expectedHypothesis, step, expectedResult) <-
maybe
(Left
(ProofStructuralCompositionMismatch
"set-induction focus is not a set-valued ambient binder"))
pure
(scopedSetInductionInstance selected property)
unless (hypothesis == expectedHypothesis)
(Left
(ProofStructuralCompositionMismatch
"set-induction hypothesis does not match the owned property"))
unless (result == expectedResult)
(Left
(ProofStructuralCompositionMismatch
"set-induction result does not close the owned property"))
let context =
ProofContext foundation globalType lexicalContext [step]
stepProof <- hypothesisProof context step
axiom <- foundationProof context SetInduction
instanceProof <- forallEliminationProof context axiom predicate
expectedInstance <-
maybe
(Left
(ProofStructuralCompositionMismatch
"set-induction instance changed lexical context"))
pure
(implyScopedCore step result)
convertedInstance <-
conversionProof context instanceProof expectedInstance
resultProof <-
implicationEliminationProof context convertedInstance stepProof
unless (builtProofStatement resultProof == result)
(Left
(ProofStructuralCompositionMismatch
"set-induction foundation instance did not derive the owned result"))
where
implyChecked antecedent conclusion =
maybe
(Left
(ProofStructuralCompositionMismatch
"set-induction guard changed lexical context"))
pure
(implyScopedCore antecedent conclusion)
validatePropositionContext
:: Text
-> [CoreType]
-> ScopedCheckedCore global
-> Either KernelProofBuildError ()
validatePropositionContext label expected proposition =
unless
( scopedCoreType proposition == TyProp
&& scopedCoreContext proposition == expected
)
(Left
(ProofStructuralCompositionMismatch
(label <> " has the wrong type or lexical context")))
checkedNegation
:: [CoreType]
-> ScopedCheckedCore global
-> Either KernelProofBuildError (ScopedCheckedCore global)
checkedNegation expectedContext proposition =
case negateScopedCore proposition of
Just negation
| scopedCoreContext negation == expectedContext ->
pure negation
_ ->
Left
(ProofStructuralCompositionMismatch
"classical negation changed context")
existentialTerm
:: CoreType
-> CanonicalTerm global
-> CanonicalTerm global
existentialTerm binderType body =
CImp
(CForall binderType
(CImp body CFalsum))
CFalsum
existentialIntroductionProof
:: Eq global
=> ProofContext global
-> CoreType
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
existentialIntroductionProof
context
binderType
bodyUnderBinder
witness
bodyAtWitness = do
unless
(proofContextTypes context
== drop 1
(scopedCoreContext
bodyUnderBinder))
(Left ProofExpectedUnaryBinder)
universalNegation <-
scopedTerm context
(CForall binderType
(CImp
(scopedCoreTerm
bodyUnderBinder)
CFalsum))
implicationIntroductionProof
context
universalNegation
(\extended universalProof -> do
negatedBody <-
forallEliminationProof
extended
universalProof
witness
implicationEliminationProof
extended
negatedBody
(weakenForHypothesis
bodyAtWitness))
existentialEliminationProof
:: Eq global
=> ProofContext global
-> CoreType
-> ScopedCheckedCore global
-> BuiltProof global
-> ScopedCheckedCore global
-> ( ProofContext global
-> ScopedCheckedCore global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
)
-> Either
KernelProofBuildError
(BuiltProof global)
existentialEliminationProof
context
binderType
bodyUnderBinder
existential
result
bodyCase = do
notResult <-
scopedTerm context
(CImp
(scopedCoreTerm result)
CFalsum)
notResultUnderBinder <-
first ProofTermIllTyped
(weakenScopedCore
(contextGlobalType context)
binderType
notResult)
doubleNegation <-
implicationIntroductionProof
context
notResult
(\withNotResult _notResultProof -> do
universalNegation <-
forallIntroductionProof
withNotResult
binderType
(\withBinder variable -> do
let body =
bodyUnderBinder
implicationIntroductionProof
withBinder
body
(\withBody bodyProof -> do
resultProof <-
bodyCase
withBody
variable
bodyProof
notResultCurrent <-
hypothesisProof
withBody
notResultUnderBinder
implicationEliminationProof
withBody
notResultCurrent
resultProof))
implicationEliminationProof
withNotResult
(weakenForHypothesis
existential)
universalNegation)
doubleNegationEliminationProof
context
result
doubleNegation
setLfpBoundProof
:: ProofContext global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> Either
KernelProofBuildError
(BuiltProof global)
setLfpBoundProof
(ProofContext
foundation
globalType
_types
_hypotheses)
domain
operator = do
result <-
first (ProofSetLfpRuleFailed . Text.pack . show)
(SetLfp.setLfpBound
foundation
globalType
domain
operator)
pure
(BuiltProof
result
(setLfpBoundDerivation
domain
operator))
setLfpFixedProof
:: Eq global
=> ProofContext global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
setLfpFixedProof
(ProofContext
foundation
globalType
_types
_hypotheses)
domain
operator
monotone = do
result <-
first (ProofSetLfpRuleFailed . Text.pack . show)
(SetLfp.setLfpFixed
foundation
globalType
domain
operator
(builtProofStatement
monotone))
pure
(BuiltProof
result
(setLfpFixedDerivation
domain
operator
(builtProofDerivation
monotone)))
setLfpInductProof
:: Eq global
=> ProofContext global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> ScopedCheckedCore global
-> BuiltProof global
-> BuiltProof global
-> BuiltProof global
-> Either
KernelProofBuildError
(BuiltProof global)
setLfpInductProof
(ProofContext
foundation
globalType
_types
_hypotheses)
domain
operator
predicate
element
monotone
member
closure = do
result <-
first (ProofSetLfpRuleFailed . Text.pack . show)
(SetLfp.setLfpInduct
foundation
globalType
domain
operator
predicate
element
(builtProofStatement
monotone)
(builtProofStatement
member)
(builtProofStatement
closure))
pure
(BuiltProof
result
(setLfpInductDerivation
domain
operator
predicate
element
(builtProofDerivation
monotone)
(builtProofDerivation
member)
(builtProofDerivation
closure)))
contextGlobalType
:: ProofContext global
-> (global -> Maybe CoreType)
contextGlobalType
(ProofContext
_foundation
globalType
_types
_hypotheses) =
globalType
equalityParts
:: ScopedCheckedCore global
-> Either
KernelProofBuildError
( CoreType
, CanonicalTerm global
, CanonicalTerm global
)
equalityParts equality =
case scopedCoreTerm equality of
CEq operandType left right ->
Right (operandType, left, right)
_ ->
Left ProofExpectedEquality
weakenForHypothesis
:: BuiltProof global
-> BuiltProof global
weakenForHypothesis
(BuiltProof statement derivation) =
BuiltProof
statement
(weakenDerivationHypotheses 1 derivation)
|