-
Notifications
You must be signed in to change notification settings - Fork 18
/
nd.scala
1509 lines (1227 loc) · 43.6 KB
/
nd.scala
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
package gapt.proofs.nd
import gapt.expr._
import gapt.expr.formula.All
import gapt.expr.formula.And
import gapt.expr.formula.Bottom
import gapt.expr.formula.Eq
import gapt.expr.formula.Ex
import gapt.expr.formula.Formula
import gapt.expr.formula.Imp
import gapt.expr.formula.Neg
import gapt.expr.formula.Or
import gapt.expr.formula.Top
import gapt.expr.subst.Substitution
import gapt.expr.ty.FunctionType
import gapt.expr.util.freeVariables
import gapt.expr.util.replacementContext
import gapt.proofs.IndexOrFormula.{ IsFormula, IsIndex }
import gapt.proofs._
import scala.collection.mutable
abstract class NDProof extends SequentProof[Formula, NDProof] {
protected def NDRuleCreationException( message: String ): NDRuleCreationException =
new NDRuleCreationException( longName, message )
/**
* The end-sequent of the rule.
*/
final def endSequent = conclusion
/** The unique formula in the succedent of the end-sequent. */
final def target: Formula = conclusion.succedent.head
/**
* Checks whether indices are in the right place and premise is defined at all of them.
*
* @param premise The sequent to be checked.
* @param antecedentIndices Indices that should be in the antecedent.
*/
protected def validateIndices( premise: HOLSequent, antecedentIndices: Seq[SequentIndex] ): Unit = {
val antSet = mutable.HashSet[SequentIndex]()
for ( i <- antecedentIndices ) i match {
case Ant( _ ) =>
if ( !premise.isDefinedAt( i ) )
throw NDRuleCreationException( s"Sequent $premise is not defined at index $i." )
if ( antSet contains i )
throw NDRuleCreationException( s"Duplicate index $i for sequent $premise." )
antSet += i
case Suc( _ ) => throw NDRuleCreationException( s"Index $i should be in the antecedent." )
}
}
}
/**
* An NDProof deriving a sequent from another sequent:
* <pre>
* (π)
* Γ :- A
* ----------
* Γ' :- A'
* </pre>
*/
abstract class UnaryNDProof extends NDProof {
/**
* The immediate subproof of the rule.
*
* @return
*/
def subProof: NDProof
/**
* The object connecting the lower and upper sequents.auxFormulas.
*
* @return
*/
def getSequentConnector: SequentConnector = occConnectors.head
/**
* The upper sequent of the rule.
*
* @return
*/
def premise = subProof.endSequent
override def immediateSubProofs = Seq( subProof )
}
object UnaryNDProof {
def unapply( p: UnaryNDProof ) = Some( p.endSequent, p.subProof )
}
/**
* An NDProof deriving a sequent from two other sequents:
* <pre>
* (π1) (π2)
* Γ :- A Γ' :- A'
* ------------------
* Π :- B
* </pre>
*/
abstract class BinaryNDProof extends NDProof {
/**
* The immediate left subproof of the rule.
*
* @return
*/
def leftSubProof: NDProof
/**
* The immediate right subproof of the rule.
*
* @return
*/
def rightSubProof: NDProof
/**
* The object connecting the lower and left upper sequents.
*
* @return
*/
def getLeftSequentConnector: SequentConnector = occConnectors.head
/**
* The object connecting the lower and right upper sequents.
*
* @return
*/
def getRightSequentConnector: SequentConnector = occConnectors.tail.head
/**
* The left upper sequent of the rule.
*
* @return
*/
def leftPremise = leftSubProof.endSequent
/**
* The right upper sequent of the rule.
*
* @return
*/
def rightPremise = rightSubProof.endSequent
override def immediateSubProofs = Seq( leftSubProof, rightSubProof )
}
object BinaryNDProof {
def unapply( p: BinaryNDProof ) = Some( p.endSequent, p.leftSubProof, p.rightSubProof )
}
/**
* An NDProof deriving a sequent from three other sequents:
* <pre>
* (π1) (π2) (π3)
* Γ1 :- A1 Γ2 :- A2 Γ3 :- A3
* --------------------------------
* Π :- B
* </pre>
*/
abstract class TernaryNDProof extends NDProof {
/**
* The immediate left subproof of the rule.
*
* @return
*/
def leftSubProof: NDProof
/**
* The immediate middle subproof of the rule.
*
* @return
*/
def middleSubProof: NDProof
/**
* The immediate right subproof of the rule.
*
* @return
*/
def rightSubProof: NDProof
/**
* The object connecting the lower and left upper sequents.
*
* @return
*/
def getLeftSequentConnector: SequentConnector = occConnectors( 0 )
/**
* The object connecting the lower and middle upper sequents.
*
* @return
*/
def getMiddleSequentConnector: SequentConnector = occConnectors( 1 )
/**
* The object connecting the lower and right upper sequents.
*
* @return
*/
def getRightSequentConnector: SequentConnector = occConnectors( 2 )
/**
* The left upper sequent of the rule.
*
* @return
*/
def leftPremise = leftSubProof.endSequent
/**
* The middle upper sequent of the rule.
*
* @return
*/
def middlePremise = middleSubProof.endSequent
/**
* The right upper sequent of the rule.
*
* @return
*/
def rightPremise = rightSubProof.endSequent
override def immediateSubProofs = Seq( leftSubProof, middleSubProof, rightSubProof )
}
object TernaryNDProof {
def unapply( p: TernaryNDProof ) = Some( p.endSequent, p.leftSubProof, p.middleSubProof, p.rightSubProof )
}
trait CommonRule extends NDProof with ContextRule[Formula, NDProof]
/**
* Use this trait for rules that use eigenvariables.
*
*/
trait Eigenvariable {
def eigenVariable: Var
}
/**
* An NDProof consisting of a single sequent:
* <pre>
* --------ax
* Γ :- A
* </pre>
*/
abstract class InitialSequent extends NDProof {
override def mainIndices = endSequent.indices
override def auxIndices = Seq()
override def immediateSubProofs = Seq()
override def occConnectors = Seq()
}
object InitialSequent {
def unapply( proof: InitialSequent ) = Some( proof.endSequent )
}
/**
* An NDProof ending with weakening:
* <pre>
* (π)
* Γ :- B
* ---------wkn
* A, Γ :- B
* </pre>
*
* @param subProof The subproof π.
* @param formula The formula A.
*/
case class WeakeningRule( subProof: NDProof, formula: Formula )
extends UnaryNDProof with CommonRule {
override def auxIndices = Seq( Seq() )
override def name = "wkn"
def mainFormula = formula
override def mainFormulaSequent = mainFormula +: Sequent()
}
object WeakeningRule extends ConvenienceConstructor( "WeakeningRule" ) {
/**
* Convenience constructor for ax, taking a context.
* Applies the axiom rule followed by 0 or more weakenings.
* <pre>
* (π)
* Γ :- B
* ---------------------wkn*
* A1, ..., An, Γ :- B
* </pre>
*
* @param subProof The subproof π.
* @param formulas The formulas A1, ..., An
* @return
*/
def apply( subProof: NDProof, formulas: Seq[Formula] ): NDProof = {
formulas.foldLeft[NDProof]( subProof ) { ( ant, c ) =>
WeakeningRule( ant, c )
}
}
}
/**
* An NDProof ending with a contraction:
* <pre>
* (π)
* A, A, Γ :- B
* --------------ctr
* A, Γ :- B
* </pre>
*
* @param subProof The subproof π.
* @param aux1 The index of one occurrence of A.
* @param aux2 The index of the other occurrence of A.
*/
case class ContractionRule( subProof: NDProof, aux1: SequentIndex, aux2: SequentIndex )
extends UnaryNDProof with CommonRule {
validateIndices( premise, Seq( aux1, aux2 ) )
if ( premise( aux1 ) != premise( aux2 ) )
throw NDRuleCreationException( s"Auxiliary formulas ${premise( aux1 )} and ${premise( aux2 )} are not equal." )
val mainFormula = premise( aux1 )
override def auxIndices = Seq( Seq( aux1, aux2 ) )
override def name = "ctr"
override def mainFormulaSequent = mainFormula +: Sequent()
}
object ContractionRule extends ConvenienceConstructor( "ContractionRule" ) {
/**
* Convenience constructor for ctr that, given a formula to contract, will automatically pick the
* first two occurrences of that formula.
*
* @param subProof The subproof π.
* @param f The formula to contract.
*/
def apply( subProof: NDProof, f: Formula ): ContractionRule = {
val premise = subProof.endSequent
val ( indices, _ ) = findAndValidate( premise )( Seq( f, f ), Suc( 0 ) )
val p = ContractionRule( subProof, Ant( indices( 0 ) ), Ant( indices( 1 ) ) )
assert( p.mainFormula == f )
p
}
}
/**
* An NDProof consisting of a logical axiom:
* <pre>
* --------ax
* A :- A
* </pre>
*
* @param A The formula A.
*/
case class LogicalAxiom( A: Formula ) extends InitialSequent {
override def name = "ax"
override def conclusion = NDSequent( Seq( A ), A )
def mainFormula = A
}
object LogicalAxiom extends ConvenienceConstructor( "LogicalAxiom" ) {
/**
* Convenience constructor for ax, taking a context.
* Applies the axiom rule followed by 0 or more weakenings.
* <pre>
* --------ax
* A :- A
* -----------wkn*
* Γ, A :- A
* </pre>
*
* @param A The atom a.
* @param context The context Γ.
* @return
*/
def apply( A: Formula, context: Seq[Formula] ): NDProof = {
context.foldLeft[NDProof]( LogicalAxiom( A ) ) { ( ant, c ) =>
WeakeningRule( ant, c )
}
}
}
/**
* An NDProof ending with elimination of the right conjunct:
* <pre>
* (π)
* Γ :- A ∧ B
* --------------∧:e1
* Γ :- A
* </pre>
*
* @param subProof The subproof π.
*/
case class AndElim1Rule( subProof: NDProof )
extends UnaryNDProof with CommonRule {
val conjunction = premise( Suc( 0 ) )
val mainFormula = conjunction match {
case And( leftConjunct, _ ) => leftConjunct
case _ =>
throw NDRuleCreationException( s"Proposed main formula $conjunction is not a conjunction." )
}
override def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def name = "∧:e1"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with elimination of the left conjunct:
* <pre>
* (π)
* Γ :- A ∧ B
* --------------∧:e2
* Γ :- B
* </pre>
*
* @param subProof The subproof π.
*/
case class AndElim2Rule( subProof: NDProof )
extends UnaryNDProof with CommonRule {
val conjunction = premise( Suc( 0 ) )
val mainFormula = conjunction match {
case And( _, rightConjunct ) => rightConjunct
case _ =>
throw NDRuleCreationException( s"Proposed main formula $conjunction is not a conjunction." )
}
override def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def name = "∧:e2"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with a conjunction on the right:
* <pre>
* (π1) (π2)
* Γ :- A Π :- B
* --------------------∧:i
* Γ, Π :- A∧B
* </pre>
*
* @param leftSubProof The proof π,,1,,.
* @param rightSubProof The proof π,,2,,.
*/
case class AndIntroRule( leftSubProof: NDProof, rightSubProof: NDProof )
extends BinaryNDProof with CommonRule {
val leftConjunct = leftPremise( Suc( 0 ) )
val rightConjunct = rightPremise( Suc( 0 ) )
val mainFormula = And( leftConjunct, rightConjunct )
def auxIndices = Seq( Seq( Suc( 0 ) ), Seq( Suc( 0 ) ) )
override def name = "∧:i"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with elimination of a disjunction:
* <pre>
* (π1) (π2) (π3)
* Γ :- A∨B Π, A :- C Δ, B :- C
* ------------------------------------∨:e
* Γ, Π, Δ :- C
* </pre>
*
* @param leftSubProof The proof π,,1,,.
* @param middleSubProof The proof π,,2,,.
* @param aux1 The index of A.
* @param rightSubProof The proof π,,3,,.
* @param aux2 The index of B.
*/
case class OrElimRule(
leftSubProof: NDProof,
middleSubProof: NDProof, aux1: SequentIndex,
rightSubProof: NDProof, aux2: SequentIndex )
extends TernaryNDProof with CommonRule {
validateIndices( middlePremise, Seq( aux1 ) )
validateIndices( rightPremise, Seq( aux2 ) )
val leftDisjunct = middlePremise( aux1 )
val rightDisjunct = rightPremise( aux2 )
val disjunction = leftPremise( Suc( 0 ) )
require(
disjunction == Or( leftDisjunct, rightDisjunct ),
throw NDRuleCreationException( s"Formula $disjunction is not a disjunction of $leftDisjunct and $rightDisjunct." ) )
val middleC = middlePremise( Suc( 0 ) )
val rightC = rightPremise( Suc( 0 ) )
val mainFormula = if ( middleC == rightC ) middleC else
throw NDRuleCreationException( s"Formulas $middleC an $rightC are not the same." )
def auxIndices = Seq( Seq( Suc( 0 ) ), Seq( aux1, Suc( 0 ) ), Seq( aux2, Suc( 0 ) ) )
override def name = "∨:e"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
object OrElimRule extends ConvenienceConstructor( "OrElimRule" ) {
/**
* Convenience constructor for ∨:e.
* Given only the subproofs, it will attempt to create an inference with this.
*
* @param leftSubProof The left subproof.
* @param middleSubProof The middle subproof.
* @param rightSubProof The right subproof.
* @return
*/
def apply( leftSubProof: NDProof, middleSubProof: NDProof, rightSubProof: NDProof ): OrElimRule = {
val disjunction = leftSubProof.endSequent( Suc( 0 ) )
val ( leftDisjunct, rightDisjunct ) = disjunction match {
case Or( f, g ) => ( f, g )
case _ => throw NDRuleCreationException( s"Formula $disjunction is not a disjunction." )
}
val ( middlePremise, rightPremise ) = ( middleSubProof.endSequent, rightSubProof.endSequent )
val ( middleIndices, _ ) = findAndValidate( middlePremise )( Seq( leftDisjunct ), Suc( 0 ) )
val ( rightIndices, _ ) = findAndValidate( rightPremise )( Seq( rightDisjunct ), Suc( 0 ) )
new OrElimRule( leftSubProof, middleSubProof, Ant( middleIndices( 0 ) ), rightSubProof, Ant( rightIndices( 0 ) ) )
}
}
/**
* An NDProof ending with introduction of a disjunction, with a new formula as the right disjunct:
* <pre>
* (π)
* Γ :- A
* ------------∨:i1
* Γ :- A ∨ B
* </pre>
*
* @param subProof The subproof π.
* @param rightDisjunct The formula B.
*/
case class OrIntro1Rule( subProof: NDProof, rightDisjunct: Formula )
extends UnaryNDProof with CommonRule {
val leftDisjunct = premise( Suc( 0 ) )
val mainFormula = Or( leftDisjunct, rightDisjunct )
override def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def name = "∨:i1"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with introduction of a disjunction, with a new formula as the left disjunct:
* <pre>
* (π)
* Γ :- A
* ------------∨:i2
* Γ :- B ∨ A
* </pre>
*
* @param subProof The subproof π.
* @param leftDisjunct The formula B.
*/
case class OrIntro2Rule( subProof: NDProof, leftDisjunct: Formula )
extends UnaryNDProof with CommonRule {
val rightDisjunct = premise( Suc( 0 ) )
val mainFormula = Or( leftDisjunct, rightDisjunct )
override def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def name = "∨:i2"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with elimination of an implication:
* <pre>
* (π1) (π2)
* Γ :- A→B Π :- A
* --------------------→:e
* Γ, Π :- B
* </pre>
*
* @param leftSubProof The proof π,,1,,.
* @param rightSubProof The proof π,,2,,.
*/
case class ImpElimRule( leftSubProof: NDProof, rightSubProof: NDProof )
extends BinaryNDProof with CommonRule {
val implication = leftPremise( Suc( 0 ) )
val antecedent = rightPremise( Suc( 0 ) )
val mainFormula = implication match {
case Imp( `antecedent`, consequent ) => consequent
case Imp( _, _ ) =>
throw NDRuleCreationException( s"Proposed main formula $antecedent is not the antecedent of $implication." )
case _ =>
throw NDRuleCreationException( s"Proposed main formula $implication is not an implication." )
}
def auxIndices = Seq( Seq( Suc( 0 ) ), Seq( Suc( 0 ) ) )
override def name = "→:e"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with introduction of an implication:
* <pre>
* (π)
* A, Γ :- B
* ------------→:i
* Γ :- A → B
* </pre>
*
* @param subProof The subproof π.
* @param aux The index of A.
*/
case class ImpIntroRule( subProof: NDProof, aux: SequentIndex )
extends UnaryNDProof with CommonRule {
validateIndices( premise, Seq( aux ) )
val impPremise = premise( aux )
val impConclusion = premise( Suc( 0 ) )
val mainFormula = Imp( impPremise, impConclusion )
override def auxIndices = Seq( Seq( aux, Suc( 0 ) ) )
override def name = "→:i"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
object ImpIntroRule extends ConvenienceConstructor( "ImpIntroRule" ) {
/**
* Convenience constructor for →:i.
* The aux formula can be given as an index or a formula. If it is given as a formula, the constructor
* will attempt to find an appropriate index on its own.
*
* @param subProof The subproof.
* @param impPremise Index of the premise of the implication or the premise itself.
* @return
*/
def apply( subProof: NDProof, impPremise: IndexOrFormula ): ImpIntroRule = {
val premise = subProof.endSequent
val ( antIndices, sucIndices ) = findAndValidate( premise )( Seq( impPremise ), Suc( 0 ) )
new ImpIntroRule( subProof, Ant( antIndices( 0 ) ) )
}
/**
* Convenience constructor for →:i
* If the subproof has precisely one element in the antecedent of its premise, this element will be the aux index.
*
* @param subProof The subproof.
* @return
*/
def apply( subProof: NDProof ): ImpIntroRule = {
val premise = subProof.endSequent
if ( premise.antecedent.size == 1 ) apply( subProof, Ant( 0 ) )
else if ( premise.antecedent.size == 0 )
throw NDRuleCreationException( s"Antecedent of $premise doesn't contain any elements." )
else throw NDRuleCreationException( s"Antecedent of $premise has more than one element, " +
s"the formula serving as antecedent of the implication should be specified." )
}
}
/**
* An NDProof ending with elimination of a negation:
* <pre>
* (π1) (π2)
* Γ :- ¬A Π :- A
* -------------------¬:e
* Γ, Π :- ⊥
* </pre>
*
* @param leftSubProof The proof π,,1,,.
* @param rightSubProof The proof π,,2,,.
*/
case class NegElimRule( leftSubProof: NDProof, rightSubProof: NDProof )
extends BinaryNDProof with CommonRule {
val negatedFormula = leftPremise( Suc( 0 ) )
val formula = rightPremise( Suc( 0 ) )
val mainFormula =
if ( ( negatedFormula == Neg( formula ) ) )
Bottom()
else
throw NDRuleCreationException(
s"""Formula $negatedFormula is not the negation of $formula.
""".stripMargin )
def auxIndices = Seq( Seq( Suc( 0 ) ), Seq( Suc( 0 ) ) )
override def name = "¬:e"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with introduction of a negation:
* <pre>
* (π)
* A, Γ :- ⊥
* -----------¬:i
* Γ :- ¬A
* </pre>
*
* @param subProof The subproof π.
* @param aux The index of A.
*/
case class NegIntroRule( subProof: NDProof, aux: SequentIndex )
extends UnaryNDProof with CommonRule {
validateIndices( premise, Seq( aux ) )
val bottom = premise( Suc( 0 ) )
require( bottom == Bottom(), s"Formula $bottom is not ⊥." )
val formula = premise( aux )
val mainFormula = Neg( formula )
override def auxIndices = Seq( Seq( aux, Suc( 0 ) ) )
override def name = "¬:i"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
object NegIntroRule extends ConvenienceConstructor( "NegIntroRule" ) {
/**
* Convenience constructor for ¬:i.
* The aux formula can be given as an index or a formula. If it is given as a formula, the constructor
* will attempt to find an appropriate index on its own.
*
* @param subProof The subproof.
* @param negation Index of the negation or the negation itself.
* @return
*/
def apply( subProof: NDProof, negation: IndexOrFormula ): NegIntroRule = {
val premise = subProof.endSequent
val ( antIndices, sucIndices ) = findAndValidate( premise )( Seq( negation ), Suc( 0 ) )
new NegIntroRule( subProof, Ant( antIndices( 0 ) ) )
}
/**
* Convenience constructor for ¬:i.
* If the subproof has precisely one element in the antecedent of its premise, this element will be the aux index.
*
* @param subProof The subproof.
* @return
*/
def apply( subProof: NDProof ): NegIntroRule = {
val premise = subProof.endSequent
if ( premise.antecedent.size == 1 ) apply( subProof, Ant( 0 ) )
else if ( premise.antecedent.size == 0 )
throw NDRuleCreationException( s"Antecedent of $premise doesn't contain any elements." )
else throw NDRuleCreationException(
s"Antecedent of $premise has more than one element, the formula to be negated should be specified." )
}
}
/**
* An NDProof that is the introduction of ⊤:
* <pre>
* ------⊤:i
* :- ⊤
* </pre>
*/
case object TopIntroRule extends InitialSequent {
def mainFormula = Top()
def conclusion = NDSequent( Seq(), mainFormula )
override def name = "⊤:i"
}
/**
* An NDProof eliminating ⊥:
* <pre>
* (π)
* Γ :- ⊥
* --------⊥:e
* Γ :- A
* </pre>
*
* @param subProof The subproof π.
* @param mainFormula The formula A.
*/
case class BottomElimRule( subProof: NDProof, mainFormula: Formula )
extends UnaryNDProof with CommonRule {
val bottom = premise( Suc( 0 ) )
require( bottom == Bottom(), s"Formula $bottom is not ⊥." )
override def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def name = "⊥:e"
override def mainFormulaSequent = Sequent() :+ mainFormula
}
/**
* An NDProof ending with a universal quantifier introduction:
* <pre>
* (π)
* Γ :- A[x\α]
* -------------∀:i
* Γ :- ∀x.A
* </pre>
* This rule is only applicable if the eigenvariable condition is satisfied: α must not occur freely in Γ.
*
* @param subProof The proof π.
* @param eigenVariable The variable α.
* @param quantifiedVariable The variable x.
*/
case class ForallIntroRule( subProof: NDProof, eigenVariable: Var, quantifiedVariable: Var )
extends UnaryNDProof with CommonRule with Eigenvariable {
val ( auxFormula, context ) = premise focus Suc( 0 )
//eigenvariable condition
if ( freeVariables( context ) contains eigenVariable )
throw NDRuleCreationException( s"Eigenvariable condition is violated: $context contains $eigenVariable" )
def subFormula = BetaReduction.betaNormalize( Substitution( eigenVariable, quantifiedVariable )( auxFormula ) )
if ( BetaReduction.betaNormalize( Substitution( quantifiedVariable, eigenVariable )( subFormula ) ) != auxFormula )
throw NDRuleCreationException( s"Aux formula should be $subFormula[$quantifiedVariable\\$eigenVariable] = " +
BetaReduction.betaNormalize( Substitution( quantifiedVariable, eigenVariable )( subFormula ) )
+ s", but is $auxFormula." )
def mainFormula = BetaReduction.betaNormalize( All( quantifiedVariable, subFormula ) )
override def name = "∀:i"
def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def mainFormulaSequent = Sequent() :+ mainFormula
}
object ForallIntroRule extends ConvenienceConstructor( "ForallIntroRule" ) {
/**
* Convenience constructor for ∀:i that, given a main formula and an eigenvariable, will try to
* construct an inference with that instantiation.
*
* @param subProof The subproof.
* @param mainFormula The formula to be inferred. Must be of the form ∀x.A.
* @param eigenVariable A variable α such that A[α] occurs in the premise.
* @return
*/
def apply( subProof: NDProof, mainFormula: Formula, eigenVariable: Var ): ForallIntroRule = {
if ( freeVariables( mainFormula ) contains eigenVariable ) {
throw NDRuleCreationException( s"Illegal main formula: Eigenvariable $eigenVariable is free in $mainFormula." )
} else mainFormula match {
case All( v, subFormula ) =>
val auxFormula = Substitution( v, eigenVariable )( subFormula )
val premise = subProof.endSequent
val ( _, indices ) = findAndValidate( premise )( Seq(), auxFormula )
val p = ForallIntroRule( subProof, eigenVariable, v )
assert( p.mainFormula == mainFormula )
p
case _ => throw NDRuleCreationException( s"Proposed main formula $mainFormula is not universally quantified." )
}
}
def apply( subProof: NDProof, eigenVariable: Var ): ForallIntroRule =
ForallIntroRule( subProof, eigenVariable, eigenVariable )
}
/**
* An NDProof ending with a universal quantifier elimination:
* <pre>
* (π)
* Γ :- ∀x.A
* -------------∀:e
* Γ :- A[x\t]
* </pre>
*
* @param subProof The proof π.
* @param term The term t.
*/
case class ForallElimRule( subProof: NDProof, term: Expr )
extends UnaryNDProof with CommonRule {
val universal = premise( Suc( 0 ) )
val mainFormula = universal match {
case All( v, subFormula ) => Substitution( v, term )( subFormula )
case _ =>
throw NDRuleCreationException( s"Proposed main formula $universal is not universally quantified." )
}
override def name = "∀:e"
def auxIndices = Seq( Seq( Suc( 0 ) ) )
override def mainFormulaSequent = Sequent() :+ mainFormula
}
object ForallElimBlock {
/**
* Applies the ForallElim-rule n times.
*
* The rule:
* <pre>
* (π)
* Γ :- ∀x1,..,xN.A
* ---------------------------------- (∀_e x n)
* Γ :- A[x1\t1,...,xN\tN]
*
* where t1,...,tN are terms.
* </pre>
*
* @param subProof The proof π with (Γ :- ∀x1,..,xN.A) as the bottommost sequent.
* @param terms The list of terms with which to instantiate main. The caller of this
* method has to ensure the correctness of these terms, and, specifically, that
* ∀x1,..,xN.A indeed occurs at the bottom of the proof π.
*/
def apply( subProof: NDProof, terms: Seq[Expr] ): NDProof =
terms.foldLeft( subProof )( ( acc, t ) => nd.ForallElimRule( acc, t ) )
}
/**
* An NDProof ending with an existential quantifier introduction:
* <pre>
* (π)
* Γ :- A[x\t]
* ------------∃:i
* Γ :- ∃x.A
* </pre>
*
* @param subProof The proof π.
* @param A The formula A.
* @param term The term t.
* @param v The variable x.
*/
case class ExistsIntroRule( subProof: NDProof, A: Formula, term: Expr, v: Var )
extends UnaryNDProof with CommonRule {