-
Notifications
You must be signed in to change notification settings - Fork 2
/
newtactics.scala
1254 lines (1065 loc) · 36.1 KB
/
newtactics.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 KeYmaeraD
object Tactics {
import Nodes._
import TreeActions._
import RulesUtil._
import Rules._
import Util._
import AM._
// A tactic returns a list of the new open leaves that it spawns.
abstract class Tactic(name: String)
extends ((OrNode) => Option[List[NodeID]]) {
override def toString: String = {
name
}
def * : Tactic = repeatT(this)
def | (alternative : =>Tactic) = eitherT(this, alternative)
def ~ (continued : =>Tactic) = composeT(this, continued)
def & (continued : =>Tactic) = composeT(this, continued)
def < (tcts : Tactic *) = branchT(this, tcts.toList)
}
def trylistofrules(rs: List[ProofRule], nd: OrNode)
: Option[List[NodeID]] = {
val sq = nd.goal;
var res: Option[List[NodeID]] = None;
for (p <- positions(sq)) {
for(r <- rs) {
if(res == None){
val res0 = r.apply(p)(sq);
res0 match {
case Some(_) =>
res = applyrule(nd,p,r)
()
case None =>
()
}
}
}
}
res
}
val trylistofrulesT : List[ProofRule] => Tactic = rls =>
new Tactic("trylist " + rls) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
trylistofrules(rls, nd)
}
}
val tryruleT : ProofRule => Tactic = rl =>
new Tactic("tryrule " + rl) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
trylistofrules(List(rl), nd)
}
}
val tryruleatT : ProofRule => Position => Tactic = rl => pos =>
new Tactic("tryruleat " + rl + " " + pos ) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val res0 = rl.apply(pos)(nd.goal)
res0 match {
case Some(_) =>
applyrule(nd,pos,rl)
case None =>
None
}
}
}
val tryrulematchT : ProofRule => Formula => Tactic = rl => fm =>
new Tactic("tryrulematch " + rl + " " + fm ) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val Sequent(sig, cs, ss) = nd.goal
val pnc = cs.indexOf(fm)
val pns = ss.indexOf(fm)
(pnc,pns) match {
case (-1,-1) => None
case (-1, pn) =>
applyrule(nd,RightP(pn),rl)
case (pn, _) =>
applyrule(nd,LeftP(pn),rl)
}
}
}
val tryruleunifyT : ProofRule => Formula => Tactic = rl => fm =>
new Tactic("tryruleunify " + rl + " " + fm ) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val sq@Sequent(sig, cs, ss) = nd.goal
for(p <- positions(sq)) {
Prover.unify(lookup(p,sq), fm) match {
case None => ()
case Some(_) => return tryruleatT(rl)(p)(nd)
}
}
return None
}
}
val tryrulepredT : ProofRule
=> (Formula => Boolean) => Tactic =
rl => pred =>
new Tactic("tryrulepred " ) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val sq@Sequent(sig, cs, ss) = nd.goal
for(p <- positions(sq)) {
val fm = lookup(p,sq)
if(pred(fm)){
return tryruleatT(rl)(p)(nd)
}
}
return None
}
}
def combinations[A](n: Int, ls: List[A]) : List[List[A]] =
ls match {
case Nil => Nil
case head :: xs =>
if (n <= 0 || n > ls.length) {
Nil
} else if (n == 1) {
ls.map(List(_))
} else {
combinations(n-1, xs).map(head :: _) ::: combinations(n, xs)
}
}
// To check if this goal can be proved (success or failure).
// def proved(sq: Sequent): Option[Sequent]
//To check if this goal can be proved in tm; and we close the node if it cannot be proved in tm.
// def proved(sq: Sequent, tm: Long): Option[Sequent]
// To abort
// def abort: Unit
def usehintsT(pos: Position): Tactic = new Tactic("usehints") {
def apply(nd: OrNode ) = lookup(pos,nd.goal) match {
/* case Modality(Box,Loop(hp, True, inv_hints), phi) =>
val rules = inv_hints.map(loopInduction)
// XXX be smarter about success and failure.
Some(rules.map(r => applyrule(nd, pos, r)).flatten.flatten)*/
case Modality(Box,Loop(hp, True, inv_hints), phi) =>
var fm : Formula = lookup(pos,nd.goal)
val Sequent(sig, cc, ss) = nd.goal
var rfms : List[Formula] = subtract(ss, List(fm))
var nrfms : List[Formula] = Nil
for (rf <- rfms) {
nrfms = nnf(Not(rf))::nrfms
}
var candidates1 : List[Formula] = union(cc, nrfms)
var candidates2 : List[Formula] = union(subFormulas(phi), candidates1)
var candidates : List[Formula] = union(inv_hints, candidates2)
/* var res : List[Formula] = setify(candidates) */
var res : List[Formula] = Nil
for (i <- 1 until candidates.length){
var llfms : List[List[Formula]] = combinations(i, candidates)
for (j <- 0 until llfms.length -1) {
var lfms : List[Formula] = llfms.apply(j)
var cf : Formula = True
for ( ccf <- lfms) {
cf = simplify(Binop(And,ccf,cf)) }
res = union(res, List(cf))
}
}
var res1: List[Formula] = setify(res)
val rules = res1.map(loopInduction)
Some(rules.map(r => applyrule(nd, pos, r)).flatten.flatten)
case Modality(Box,Evolve(derivs,h,inv_hints,sols), phi) =>
var dfm : Formula = lookup(pos,nd.goal)
val Sequent(sig, cc, ss) = nd.goal
var drfms : List[Formula] = subtract(ss, List(dfm))
var dnrfms : List[Formula] = Nil
for (drf <- drfms) {
dnrfms = nnf(Not(drf))::dnrfms
}
var dcandidates1 : List[Formula] = union(cc, dnrfms)
var dcandidates2 : List[Formula] = union(subFormulas(phi), dcandidates1)
var dcandidates : List[Formula] = union(inv_hints, dcandidates2)
var dres : List[Formula] = Nil
for (i <- 1 until dcandidates.length){
var dllfms : List[List[Formula]] = combinations(i, dcandidates)
for (j <- 0 until dllfms.length -1) {
var dlfms : List[Formula] = dllfms.apply(j)
var dcf : Formula = True
for ( dccf <- dlfms) {
dcf = simplify(Binop(And,dccf,dcf)) }
dres = union(dres, List(dcf))
}
}
var dres1: List[Formula] = setify(dres)
val inv_rules = dres1.map(diffStrengthen)
val inv_res = inv_rules.map(r => applyrule(nd, pos, r)).flatten.flatten
val sol_rule1 = diffSolve(Endpoint)(sols)
val sol_rule2 = diffSolve(Standard)(sols)
val sol_res1 = applyrule(nd,pos,sol_rule1) match {
case None => Nil
case Some(lst) => lst
}
val sol_res2 = applyrule(nd,pos,sol_rule2) match {
case None => Nil
case Some(lst) => lst
}
// XXX
Some(sol_res1 ++ sol_res2 ++ inv_res)
case Modality(Box,EvolveQuantified(i,c,vs,h,sols), phi) =>
val sol_rule1 = qDiffSolve(Endpoint)(sols)
val sol_rule2 = qDiffSolve(Standard)(sols)
val sol_res1 = applyrule(nd,pos,sol_rule1) match {
case None => Nil
case Some(lst) => lst
}
val sol_res2 = applyrule(nd,pos,sol_rule2) match {
case None => Nil
case Some(lst) => lst
}
Some(sol_res1 ++ sol_res2)
case _ => None
}
}
/* def usehintsT(pos: Position): Tactic = new Tactic("usehints") {
def apply(nd: OrNode ) = lookup(pos,nd.goal) match {
case Modality(Box,Loop(hp, True, inv_hints), phi) =>
val rules = inv_hints.map(loopInduction)
// XXX be smarter about success and failure.
Some(rules.map(r => applyrule(nd, pos, r)).flatten.flatten)
case Modality(Box,Evolve(derivs,h,inv_hints,sols), phi) =>
val inv_rules = inv_hints.map(diffStrengthen)
val inv_res = inv_rules.map(r => applyrule(nd, pos, r)).flatten.flatten
val sol_rule1 = diffSolve(Endpoint)(sols)
val sol_rule2 = diffSolve(Standard)(sols)
val sol_res1 = applyrule(nd,pos,sol_rule1) match {
case None => Nil
case Some(lst) => lst
}
val sol_res2 = applyrule(nd,pos,sol_rule2) match {
case None => Nil
case Some(lst) => lst
}
// XXX
Some(sol_res1 ++ sol_res2 ++ inv_res)
case Modality(Box,EvolveQuantified(i,c,vs,h,sols), phi) =>
val sol_rule1 = qDiffSolve(Endpoint)(sols)
val sol_rule2 = qDiffSolve(Standard)(sols)
val sol_res1 = applyrule(nd,pos,sol_rule1) match {
case None => Nil
case Some(lst) => lst
}
val sol_res2 = applyrule(nd,pos,sol_rule2) match {
case None => Nil
case Some(lst) => lst
}
Some(sol_res1 ++ sol_res2)
case _ => None
}
} */
object LinearAlgebra {
type Mat = List[List[Term]]
type Vec = List[Term]
val zero = Num(Exact.Integer(0))
val one = Num(Exact.Integer(1))
def transpose(mat : Mat) : Mat = {
// If all rows are nil, we are done.
if (mat.exists(row => row.size == 0)) {
return Nil
} else {
val splt = mat.map(row => row match {
case ent::ents => (ent,ents)
case _ => throw new Error("impossible")
})
val (nr,rest) = splt.unzip
nr::transpose(rest)
}
}
def dot(v1 : Vec, v2 : Vec) : Term = {
val v3 = v1.zip(v2).map({case (t1, t2) => Fn("*", List(t1,t2))})
v3.foldRight[Term](Num(Exact.Integer(0)))((t1,t2) =>
Fn("+", List(t1,t2)))
}
def multMV(m : Mat, v : Vec) : Vec = {
m.map(row => dot(row, v))
}
def mult(m1 : Mat, m2 : Mat) : Mat = {
val m2T = transpose(m2)
m1.map(row =>
m2T.map(col =>
AM.tsimplify(dot(row, col))))
}
def plusV(v1 : Vec, v2: Vec) : Vec = {
v1.zip(v2).map({case(t1,t2) =>
AM.tsimplify(Fn("+", List(t1, t2)))})
}
def plusM(m1 : Mat, m2 : Mat) : Mat = {
m1.zip(m2).map({case(v1,v2) =>
plusV(v1,v2)})
}
def scalarV(v : Vec, c : Term) : Vec = {
v.map(e => AM.tsimplify(Fn("*", List(c, e))))
}
def scalarM(m : Mat, c : Term) : Mat = {
m.map(row => scalarV(row, c))
}
def eye(n: Int) : Mat = {
if (n <= 0) {
Nil
} else {
val eye0 = eye(n-1)
val eye01 = eye0.map(row => zero :: row)
(one :: (Range(0,n-1).map(x => zero)).toList) :: eye01
}
}
def pow(m : Mat, n : Int) : Mat = {
if (n <= 0 ) {
eye(m.length)
} else {
mult(m, pow(m, n-1))
}
}
def factorial(n: Int) : Int = {
if (n <= 0 ) {
1
} else {
n * factorial(n-1)
}
}
def exp(m : Mat) : Mat = {
val n = m.length
val ks = Range(1, n + 1).toList
ks.foldLeft[Mat](eye(n))( (r, k) =>
{
plusM(r, scalarM(pow(m, k),
Num(Exact.Rational(1,factorial(k)))))
})
}
}
def diffsolveT(pos: Position, md: DiffSolveMode): Tactic =
new Tactic("diffsolveT " + md) {
import Prover._
import LinearAlgebra._
def derivsToSols (derivs : List[(Fn,Term)]) : List[Formula] = {
val (vs, thetas) = derivs.unzip
val n = vs.length
// y' = Ay + b
val b = thetas.map(theta =>
AM.tsimplify(
vs.foldRight[Term](theta)(
(v:Fn, th1:Term) => extract_Term(v, th1)(zero))))
println(b)
val homog = thetas.zip(b).map(x => x match {
case (theta, bi) => Fn("-", List(theta, bi))})
val A = homog.map(theta =>
vs.map(v =>
AM.tsimplify(
vs.foldRight[Term](theta)(
(v1:Fn, th1:Term) =>
if(v1 == v)
{extract_Term(v1, th1)(one)}
else
{extract_Term(v1, th1)(zero)}
))))
println(A)
val t1name = uniqify("t")
val t1 = Var(t1name)
val t2name = uniqify("t")
val t2 = Var(t2name)
println(t1)
println(t2)
val A1 = exp(scalarM(A,t1))
println(A1)
// use |vs| as the initial values
val v1 = multMV(A1, vs)
println(v1)
val v2 = multMV(exp(scalarM(A,t2)), b)
println(v2)
val v2I = v2.map(e => AM.Integrate.integrate(t2, e))
println(v2I)
val v2S = v2I.map(e =>
AM.tsimplify(Fn("-",
List(substitute_Term(t2name, zero, e),
substitute_Term(t2name, t1, e)))))
println(v2S)
val vsol = plusV(v1, v2S)
println(vsol)
val sols = vs.zip(vsol).map({case (Fn(v, Nil), tm) =>
Quantifier(Forall, t1name, Real,
Atom(R("=", List(Fn(v, List(Var(t1name))),
tm))))})
sols
}
def apply(nd: OrNode ) = lookup(pos,nd.goal) match {
case Modality(Box,Evolve(derivs, h, inv_hints, Nil), phi) =>
try {
val sols = derivsToSols(derivs)
applyrule(nd, pos, diffSolve(md)(sols))
} catch {
case _ => None
}
case Modality(Box,Evolve(derivs,h,inv_hints,sols), phi) =>
val sol_rule1 = diffSolve(md)(sols)
applyrule(nd,pos,sol_rule1)
case Modality(Box,EvolveQuantified(i,c,vs,h,sols), phi) =>
val sol_rule1 = qDiffSolve(md)(sols)
applyrule(nd,pos,sol_rule1)
case _ => None
}
}
val hpeasy = List(seq, choose, check,
assign, assignAnyRight, qassign,
diffClose
)
val hpalpha = List(seq, check,
assign, assignAnyRight, qassign, choose
)
val needhints = List(loopInduction, diffStrengthen)
def composeT(t1: Tactic, t2: Tactic) : Tactic =
new Tactic("compose " + t1.toString + "," + t2.toString) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val mbe_newnds = t1(nd)
mbe_newnds match {
case None => None
case Some(newnds) =>
val nnds: List[NodeID] =
newnds.map(c =>
{getnodethen(c,gotonode _);
hereNode match {
case ornd@OrNode(_,_) =>
t2(ornd)
case _ => None
}}).flatten.flatten
// XXX think about success and failure
Some(nnds)
}
}
}
val unitT : Tactic =
new Tactic("unit") {
def apply(nd: OrNode) = {
Some(List(nd.nodeID))
}
}
val nilT : Tactic =
new Tactic("nil") {
def apply(nd: OrNode) = {
None
}
}
def composelistT(tcts : Tactic * ) : Tactic =
tcts.toList.foldRight(unitT)( (t1,t2) => composeT(t1,t2) )
// do t1. Then, if no new nodes, do t2.
//@todo it could make sense to optimize for special case t1=nilT or t2=nilT or, instead, optimize eitherlistT to avoid nilT except for empty list
def eitherT(t1: Tactic, t2: Tactic) : Tactic =
new Tactic("either " + t1.toString + "," + t2.toString) {
def apply(nd: OrNode) = {
val nds = t1.apply(nd);
nds match {
case None =>
t2.apply(nd)
case _ =>
nds
}
}
}
def eitherlistT(tcts : Tactic *) : Tactic =
tcts.toList.foldRight(nilT)( (t1,t2) => eitherT(t1,t2) )
val hpeasyT : Tactic = new Tactic("hpeasy") {
def apply(nd: OrNode) = nd.goal match {
case Sequent(sig, c,List(s)) =>
// try all the box hp easy rules
val pos = RightP(0)
eitherT(trylistofrulesT(hpeasy),usehintsT(pos))(nd)
case _ => None
}
}
def repeatT(t: Tactic) : Tactic = new Tactic("repeat " + t.toString) {
def apply(nd: OrNode) = {
t(nd) match {
case None => Some(List(nd.nodeID))
case Some(newnds) =>
Some(newnds.map(
c => {getnodethen(c,gotonode _);
hereNode match {
case ornd@OrNode(_,_) =>
apply(ornd)
case _ => None
}
}).flatten.flatten)
}
}
}
val substT : Tactic = new Tactic("substitute") {
def apply(nd: OrNode): Option[List[NodeID]] = nd.goal match {
case sq@Sequent(sig, c,s) =>
for (i <- c.indices) {
val pos = LeftP(i)
substitute.apply(pos)(sq) match {
case Some(_) =>
return applyrule(nd,pos,substitute);
()
case None =>
()
}
}
return None
case _ => None
}
}
def arithT : Tactic = new Tactic("arithmetic") {
def apply(nd: OrNode): Option[List[NodeID]] = {
if(submitproc(nd, "math")){
Some(Nil)
} else {
None
}
}
}
val alpha = List(andLeft, impRight, allRight, orRight, not)
val alphaT : Tactic = new Tactic("alpha") {
def apply(nd: OrNode) =
trylistofrules(alpha,nd)
}
val hpalphaT : Tactic = new Tactic("hpalpha") {
def apply(nd: OrNode) =
trylistofrules(hpalpha ++ alpha,nd)
}
val beta = List(andRight, orLeft, impLeft)
val betaT : Tactic = new Tactic("beta") {
def apply(nd: OrNode) =
trylistofrules(beta,nd)
}
val nonarithcloseT =
trylistofrulesT(List(close,identity))
val closeOrArithT = eitherT(trylistofrulesT(List(close, identity)),
arithT)
// lazy arithmetic
//@todo could add cheap close earlier.
val alleasyT: Tactic = composelistT(repeatT(eitherT(hpeasyT, alphaT)),
repeatT(substT),
repeatT(eitherT(hpalpha1T,betaT)),
closeOrArithT)
def getOpenLeaves(nd: ProofNode) : List[OrNode] = {
val kds = nd.getChildren.map(getnode)
(kds, nd.getStatus, nd) match {
case (Nil,Open, nd1@OrNode(_,_)) =>
List(nd1)
case _ =>
val lvs = kds.map(getOpenLeaves).flatten
lvs
}
}
val applyToLeavesT : Tactic => Tactic = tct =>
new Tactic("applyToLeavesT " + tct) {
def apply(nd: OrNode): Option[List[NodeID]] = {
val lvs = getOpenLeaves(rootNode)
val rs = lvs.map(tct).flatten.flatten
Some(rs)
}
}
val nullarizeT : Tactic =
new Tactic("nullarize") {
import Prover._
def getunaryfn(tm: Term) : List[Term] = tm match {
case Fn(f, List(arg)) if f != "-" => List(tm)
case Fn(f, args) => args.map(getunaryfn).flatten
case _ => Nil
}
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig, c, s) = nd.goal
val fms = c ++ s
val unaryfns = fms.map(fm =>
overterms_Formula(tm => (b:List[Term]) =>
getunaryfn(tm) ++ b,
fm, Nil)).flatten.distinct
val rls = unaryfns.map(tm => unsubstitute(tm))
trylistofrules(rls, nd)
}
}
val instantiateAuxT : Sort => Term => Formula => Tactic = srt => tm => fm =>
new Tactic("instantiateAux") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,cs,ss) = nd.goal
val pn = cs.indexOf(fm)
if(pn == -1) None
else {
RulesUtil.lookup(LeftP(pn), nd.goal) match {
case Quantifier(Forall, _, srt1, _) if srt == srt1 =>
applyrule(nd,LeftP(pn),allLeft(tm))
case Quantifier(Forall, _, srt1, _) => Some(List(nd.nodeID))
case _ => None
}
}
}
}
def findunivs(srt: Sort, sq: Sequent): List[Formula] = sq match {
case Sequent(sig,cs,ss) =>
var res: List[Formula] = Nil
for(c <- cs) {
c match {
case Quantifier(Forall, _, srt1, _) if srt1 == srt =>
res = c::res
()
case _ =>
()
}
}
res
}
def findSingleUnivs(srt: Sort, sq: Sequent): List[Formula] = sq match {
case Sequent(sig,cs,ss) =>
var res: List[Formula] = Nil
for(c <- cs) {
c match {
case Quantifier(Forall, _, srt1,
Quantifier(Forall, _, _, _)) =>
()
case Quantifier(Forall, _, srt1, _) if srt1 == srt =>
res = c::res
()
case _ =>
()
}
}
res
}
val instantiateT : Sort => List[Term] => Tactic = srt => tms =>
new Tactic("instantiate") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,_,_) = nd.goal
val fms = findunivs(srt,nd.goal)
var tct1 = unitT
for(tm <- tms) {
if(Prover.infersort(sig, tm) == srt){
tct1 =
fms.foldRight(tct1)((fm1,rt) =>
composeT(instantiateAuxT(srt)(tm)(fm1),rt))
}
}
tct1(nd)
}
}
val instantiatesinglesT : Sort => List[Term] => Tactic = srt => tms =>
new Tactic("instantiate") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,_,_) = nd.goal
val fms = findSingleUnivs(srt,nd.goal)
var tct1 = unitT
for(tm <- tms) {
if(Prover.infersort(sig, tm) == srt){
tct1 =
fms.foldRight(tct1)((fm1,rt) =>
composeT(instantiateAuxT(srt)(tm)(fm1),rt))
}
}
tct1(nd)
}
}
val instantiatebyT : Sort => List[(String, List[String])] => Tactic = srt => alist =>
new Tactic("instantiate by") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,_,_) = nd.goal
val sig1 =
sig.filter((kv) => kv._2 match { case (Nil, srt1) if srt == srt1 => true
case _ => false})
val insts = sig1.keys.toList
val fms = findunivs(srt, nd.goal)
var tcts : List[Tactic] = Nil
for (fm <- fms) {
fm match {
case Quantifier(Forall, i, s, fm0) =>
Prover.assoc(Prover.ununiqify(i), alist) match {
case None => ()
case Some(js) =>
for (j <- js) {
tcts =
insts.filter(x => Prover.ununiqify(x) == j).map(x =>
instantiateAuxT(srt)(Fn(x,Nil))(fm)) ++ tcts
}
}
case _ => ()
}
}
if (tcts.length == 0) {
None
} else {
val tct = tcts.foldRight(unitT)(composeT)
val hidetct = fms.map(fm => tryrulematchT(hide)(fm)).foldRight(unitT)(composeT)
composeT(tct, hidetct)(nd)
}
}
}
val instantiatebyselfT : Sort => Tactic = srt =>
new Tactic("instantiate by self") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,_,_) = nd.goal
val sig1 =
sig.filter((kv) => kv._2 match { case (Nil, srt1) if srt == srt1 => true
case _ => false})
val insts =
sig1.keys.toList.map(k => Prover.ununiqify(k)).distinct.map(x => (x,List(x)))
instantiatebyT(srt)(insts)(nd)
}
}
val instantiatebyeverythingT : Sort => Tactic = srt =>
new Tactic("instantiate by everything") {
def apply(nd: OrNode): Option[List[NodeID]] = {
val Sequent(sig,_,_) = nd.goal
val sig1 =
sig.filter((kv) => kv._2 match { case (Nil, srt1) if srt == srt1 => true
case _ => false})
val insts = sig1.keys.toList
val fms = findunivs(srt, nd.goal)
var tcts : List[Tactic] = Nil
for (fm <- fms) {
fm match {
case Quantifier(Forall, i, s, fm0) =>
tcts = insts.map(x =>
instantiateAuxT(srt)(Fn(x,Nil))(fm)) ++ tcts
case _ => ()
}
}
if (tcts.length == 0) {
None
} else {
val tct = tcts.foldRight(unitT)(composeT)
val hidetct = fms.map(fm => tryrulematchT(hide)(fm)).foldRight(unitT)(composeT)
composeT(tct, hidetct)(nd)
}
}
}
def findidx(sq: Sequent): List[(Term,Term)] = sq match {
case Sequent(sig,cs,ss) =>
var res: List[(Term,Term)] = Nil
for(c <- cs) {
c match {
case Not(Atom(R("=", List(t1,t2)))) =>
res = (t1,t2)::res
()
case _ =>
()
}
}
for(s <- ss) {
s match {
case Atom(R("=", List(t1,t2))) =>
res = (t1,t2)::res
()
case _ =>
()
}
}
res
}
val instantiate0T : Sort => Tactic = srt => new Tactic("instantiate0") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val idcs = findidx(nd.goal)
val tct1 =
idcs.foldRight(unitT)((idx,rt) =>
composeT(instantiateT(srt)(List(idx._1,idx._2)),rt))
tct1(nd)
}
}
val instantiate1T : Sort => Tactic = srt => new Tactic("instantiate1") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val idcs = findidx(nd.goal)
val uvs = findunivs(srt,nd.goal)
val tct1 =
idcs.foldRight(unitT)((idx,rt) =>
composeT(instantiateT(srt)(List(idx._1,idx._2)),rt))
val tct2 =
uvs.foldRight(unitT)((fm1,rt) =>
composeT(tryrulematchT(hide)(fm1),rt));
composeT(tct1,tct2)(nd)
}
}
val instantiate2T : Term => Term => Tactic = tm1 => tm2 =>
new Tactic("instantiate2") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val sq = nd.goal
for(p <- positions(sq)){
lookup(p,sq) match {
case fm@Quantifier(Forall, i1, srt1,
fm1@Quantifier(Forall, i2, srt2, fm2)) =>
return composelistT(
tryruleatT(allLeft(tm1))(p),
tryruleunifyT(allLeft(tm2))(fm1),
tryruleunifyT(hide)(fm1)
)(nd)
case _ => ()
}
}
return None
}
}
val instantiate3T : Tactic =new Tactic("instantiate3") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val idcs = findidx(nd.goal)
val tct1 =
idcs.foldRight(unitT)((idx,rt) =>
composeT(instantiate2T(idx._1)(idx._2),rt))
tct1(nd)
}
}
val instantiate4T : Tactic =new Tactic("instantiate4") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val idcs = findidx(nd.goal)
val tct1 =
idcs.foldRight(unitT)((idx,rt) =>
composeT(instantiate2T(idx._2)(idx._1),rt))
tct1(nd)
}
}
val instantiate5T : Sort => Tactic = srt => new Tactic("instantiate5 " + srt) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val Sequent(sig,s,c) = nd.goal
val sig1 = sig.filter((kv) => kv._2 match { case (Nil, srt1) if srt == srt1 => true
case _ => false})
val tms =
sig1.keys.toList.sortWith((s1,s2) =>
s1.compareTo(s2) < 0 ).map( k => (Fn(k,Nil): Term) )
instantiateT(srt)(tms)(nd)
}
}
val instantiatesinglesofT : Sort => Tactic = srt => new Tactic("instantiate5 " + srt) {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val Sequent(sig,s,c) = nd.goal
val sig1 = sig.filter((kv) => kv._2 match { case (Nil, srt1) if srt == srt1 => true
case _ => false})
val tms =
sig1.keys.toList.sortWith((s1,s2) =>
s1.compareTo(s2) < 0 ).map( k => (Fn(k,Nil): Term) )
instantiatesinglesT(srt)(tms)(nd)
}
}
val hideunivsT : Sort => Tactic = srt => new Tactic("hideunivs") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val fms = findunivs(srt,nd.goal)
val tct1 =
fms.foldRight(unitT)((fm1,rt) =>
composeT(tryrulematchT(hide)(fm1),rt));
tct1(nd)
}
}
val hidedoublequantT : Tactic =
new Tactic("hidedoublequant") {
def apply(nd: OrNode) : Option[List[NodeID]] = {
val sq = nd.goal
for(p <- positions(sq)){
lookup(p,sq) match {
case fm@Quantifier(Forall, i1, srt1,
fm1@Quantifier(Forall, i2, srt2, fm2)) =>
return tryruleatT(hide)(p)(nd);
case _ => ()
}
}
return None
}
}
def branchT(tct: Tactic, tcts: List[Tactic]) : Tactic =
new Tactic("branch " + tct + " " + tcts) {