-
Notifications
You must be signed in to change notification settings - Fork 2
/
arithmetic.scala
1095 lines (882 loc) · 33.9 KB
/
arithmetic.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
/*
This file is based on a translation of John Harrison's OCaml code from
_Handbook of Practical Logic and Automated Reasoning_, and falls under
the following license:
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree
to this license. If you do not agree to this license, do not
download, install, copy or use the software.
Copyright (c) 2003-2007, John Harrison
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The name of John Harrison may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
package KeYmaeraD
import scala.actors.Actor
import scala.actors.Actor._
//import scala.actors.TIMEOUT
import KeYmaeraD.Util._
abstract class Sign
case class Zero() extends Sign
case class Nonzero() extends Sign
case class Positive() extends Sign
case class Negative() extends Sign
object CV {
var lock = new Object();
var keepGoing = true;
def start() : Unit = {
lock.synchronized{ //does scala do this better?
keepGoing = true;
}
}
def stop() : Unit = {
lock.synchronized{
keepGoing = false;
}
}
}
class CHAbort() extends Exception
class AssocException() extends Exception
class Failure() extends Exception
final object AM {
object Integrate {
def productForm(t: Term): Term = t match {
case Fn("*", List(t1, Fn("+", List(t2, t3)))) =>
productForm(
Fn("+", List(Fn("*", List(t1, t2)),
Fn("*", List(t1, t3)))))
case Fn("*", List(Fn("+", List(t2, t3)), t1)) =>
productForm(
Fn("+", List(Fn("*", List(t2, t1)),
Fn("*", List(t3, t1)))))
case Fn("*", List(t1, Fn("-", List(t2, t3)))) =>
productForm(
Fn("-", List(Fn("*", List(t1, t2)),
Fn("*", List(t1, t3)))))
case Fn("*", List(Fn("-", List(t2, t3)), t1)) =>
productForm(
Fn("-", List(Fn("*", List(t2, t1)),
Fn("*", List(t3, t1)))))
case Fn(f, ts) =>
Fn(f, ts.map(productForm))
case Num(n) => Num(n)
case Var(x) => Var(x)
}
// How many times does tocc appear in t?
def countOccurences(tocc : Term, t : Term): Int = {
if (t == tocc) { 1 } else {
t match {
case Fn(f, ts) => ts.map(x => countOccurences(tocc, x)).sum
case _ => 0
}
}
}
// Compute the symbolic integral of t with respect to v.
def integrate1(v : Term, t : Term) : Term = t match {
case Fn("+", List(t1,t2)) =>
Fn("+", List(integrate(v, t1), integrate(v,t2)))
case Fn("-", List(t1,t2)) =>
Fn("-", List(integrate(v, t1), integrate(v,t2)))
case Fn("*", ts) =>
val n = countOccurences(v, t)
Fn("*", List(Num(Exact.Rational(1, n+1)),
Fn("*", List(v, t))))
case Fn(f, Nil) => Fn("*", List(t, v))
case Num(n) => Fn("*", List(t, v))
case _ =>
println("unimplemented")
throw new Error("unimplemented")
}
def integrate(v : Term, t : Term) : Term = {
tsimplify(integrate1(v, productForm(t)))
}
} // object Integrate
/* Simplification.
*/
def tsimplify1( t: Term): Term = t match {
case Fn("+",List(Num(m), Num(n))) => Num(m + n)
case Fn("-",List(Num(m), Num(n))) => Num(m - n)
case Fn("*",List(Num(m), Num(n))) => Num(m * n)
case Fn("/",List(Num(m), Num(n))) => Num(m / n)
case Fn("+",List(Num(n), x)) if n.is_zero => x
case Fn("+",List(x,Num(n))) if n.is_zero => x
// case Fn("-",List(Num(n), x)) if n.is_zero => Fn("-", List(x))
case Fn("-",List(x, Num(n))) if n.is_zero => x
case Fn("*",List(Num(n), x)) if n.is_zero => zero
case Fn("/",List(Num(n), x)) if n.is_zero => zero
case Fn("*",List(x,Num(n))) if n.is_zero => zero
case Fn("*",List(Num(n), x)) if n.is_one => x
case Fn("*",List(x,Num(n))) if n.is_one => x
case Fn("/",List(x,Num(n))) if n.is_one => x
case _ => t
}
def tsimplify(t: Term): Term = t match {
case Fn("+",List(e1, e2)) => tsimplify1(
Fn("+",List(tsimplify(e1), tsimplify(e2))))
case Fn("*",List(e1, e2)) => tsimplify1(
Fn("*",List(tsimplify(e1), tsimplify(e2))))
// Added this case to help diffsolveT.
case Fn("-",List(tm1, tm2)) if tm1 == tm2 => zero
case Fn("-",List(e1, e2)) => tsimplify1(
Fn("-",List(tsimplify(e1), tsimplify(e2))))
// There should probably be a case for / here.
case _ => tsimplify1(t)
}
def psimplify1(fm: Formula): Formula = fm match {
case Not(False) => True
case Not(True) => False
case Not(Not(p)) => p
case Binop(And, p,False) => False
case Binop(And, False,p) => False
case Binop(And,p,True) => p
case Binop(And,True,p) => p
case Binop(Or,p,False) => p
case Binop(Or,False,p) => p
case Binop(Or,p,True) => True
case Binop(Or,True,p) => True
case Binop(Imp,False,p) => True
case Binop(Imp,p,True) => True
case Binop(Imp,True,p) => p
case Binop(Imp,p, False) => Not(p)
case Binop(Iff,p, True) => p
case Binop(Iff,True,p) => p
case Binop(Iff,False, False) => True
case Binop(Iff,p, False) => Not(p)
case Binop(Iff,False,p) => Not(p)
case _ => fm
}
/* Simplify a propositional formula. */
def psimplify(fm: Formula): Formula = fm match {
case Not(p) => psimplify1(Not(psimplify(p)))
case Binop(And,p,q) => psimplify1(Binop(And,psimplify(p),psimplify(q)))
case Binop(Or,p,q) => psimplify1(Binop(Or,psimplify(p),psimplify(q)))
case Binop(Imp,p,q) => psimplify1(Binop(Imp,psimplify(p),psimplify(q)))
case Binop(Iff,p,q) => psimplify1(Binop(Iff,psimplify(p),psimplify(q)))
case _ => fm
}
def simplify1(fm: Formula): Formula = fm match {
case Quantifier(_,_,x,p) => if( fv(p).contains(x)) fm
else p
case _ => psimplify1(fm)
}
/* Simplify a first order formula. */
def simplify(fm: Formula): Formula = fm match {
case Not(p) => simplify1(Not(simplify(p)))
case Binop(c,p,q) => simplify1(Binop(c,simplify(p),simplify(q)))
case Quantifier(q,x,c,p) => simplify1(Quantifier(q,x,c,simplify(p)))
case _ => fm
}
def distrib[A <% Ordered[A]](s1: List[List[A]], s2: List[List[A]])
: List[List[A]] = {
setify(allpairs(union[A],s1,s2))
}
def purednf(fm: Formula): List[List[Formula]] = fm match {
case Binop(And,p,q) => distrib(purednf(p),purednf(q))
case Binop(Or,p,q) => union(purednf(p),purednf(q))
case _ => List(List(fm))
}
// does this list of formulas have a pair f and Not(f)?
def trivial(lits: List[Formula]): Boolean = {
val (pos,neg) = lits.partition(positive(_));
! intersect(pos, setify(neg.map(negate))).isEmpty
}
def simpdnf(fm: Formula): List[List[Formula]] = {
if(fm == False) Nil else if(fm == True) List(Nil) else {
val djs = purednf(nnf(fm)).filter((x:List[Formula]) => ! trivial(x));
djs.filter(d => !(djs.exists(d_1 => psubset(d_1,d))))
}
}
def dnf(fm: Formula): Formula = {
list_disj(simpdnf(fm).map(list_conj))
}
def separate(x: String, cjs: List[Formula]): Formula = {
val (yes,no) = cjs.partition(c => fv(c).contains(x));
if(yes == Nil) list_conj(no)
else if(no == Nil) Quantifier(Exists, x, Real,list_conj(yes))
else Binop(And, Quantifier(Exists, x, Real, list_conj(yes)), list_conj(no))
}
def pushquant(x: String, p: Formula): Formula = {
// P.print_fol_formula(p);
// println();
if(! fv(p).contains(x)) p else {
val djs = purednf(nnf(p));
list_disj (djs.map(d => separate(x,d)))
}
}
def miniscope(fm: Formula): Formula = {
fm match {
case Not(p) => Not(miniscope(p))
case Binop(And,p,q) => Binop(And,miniscope(p),miniscope(q))
case Binop(Or,p,q) => Binop(Or,miniscope(p),miniscope(q))
case Quantifier(Forall,x,c,p) => Not(pushquant(x,Not(miniscope(p))))
case Quantifier(Exists,x,c,p) => pushquant(x,miniscope(p))
case _ => fm
}
}
def eval(fm: Formula, v: Pred => Boolean): Boolean = fm match {
case False => false
case True => true
case Atom(x) => v(x)
case Not(p) => eval(p,v) unary_!
case Binop(And,p,q) => eval(p,v) && eval(q,v)
case Binop(Or,p,q) => eval(p,v) || eval(q,v)
case Binop(Imp,p,q) => (eval(p,v) unary_! ) || eval(q,v)
case Binop(Iff,p,q) => eval(p,v) == eval(q,v)
case _ =>
throw new Error("nonfirstorder arithmetic")
}
val operations: List[(String, (Exact.Num,Exact.Num) => Boolean)] =
List(("=", (r,s) => r == s),
("/=", (r,s) => r != s),
("<", (r,s) => r < s),
(">", (r,s) => r > s),
("<=", (r,s) => r <= s),
(">=", (r,s) => r >= s))
def evalc(fm: Formula) : Formula = {
onatoms(
at => at match {
case R(p,List(Num(n),Num(m))) =>
try {if(assoc(p,operations)(n,m)) True else False}
catch { case e => Atom(at)}
case _ => Atom(at)
}, fm)
}
def mk_and(p: Formula, q: Formula): Formula = Binop(And,p,q);
def mk_or(p: Formula, q: Formula): Formula = Binop(Or,p,q);
def conjuncts(fm: Formula): List[Formula] = fm match {
case Binop(And,p,q) => conjuncts(p) ++ conjuncts(q)
case _ => List(fm)
}
def disjuncts(fm: Formula): List[Formula] = fm match {
case Binop(Or,p,q) => disjuncts(p) ++ disjuncts(q)
case _ => List(fm)
}
// XXX
def onatoms_HP(f : Pred => Formula, hp : HP) : HP = hp match {
case _ => hp
}
def onatoms(f: Pred => Formula, fm: Formula): Formula = fm match {
case Atom(a) => f(a)
case Not(p) => Not(onatoms(f,p))
case Binop(c,p,q) => Binop(c,onatoms(f, p), onatoms(f,q))
case Quantifier(q,x,c,p ) => Quantifier(q,x,c,onatoms(f,p))
case Modality(m,hp, phi) => Modality(m,onatoms_HP(f,hp), onatoms(f,phi))
case _ => fm
}
def simplify_terms(fm: Formula): Formula = {
onatoms( fol => fol match {
case R(r, List(t1,t2)) => Atom(R(r,List(tsimplify(t1),tsimplify(t2))))
case _ => throw new Error("simplify terms.")
}, fm)
}
def overatoms[B](f: Pred => B => B, fm: Formula, b: B): B = fm match {
case Atom(a) => f(a)(b)
case Not(p) => overatoms(f,p,b)
case Binop(_,p,q) => overatoms(f, p, overatoms(f,q,b))
case Quantifier(_,_,_,p) => overatoms(f, p, b)
case _ => b
}
def atom_union[A <% Ordered[A]](f: Pred => List[A], fm: Formula): List[A] = {
setify(overatoms( (h:Pred) => (t:List[A]) => f(h) ++ t, fm, Nil))
}
def list_conj(l: List[Formula]) : Formula = l match {
case Nil => True
case f::Nil => f
case f::fs => Binop(And,f, list_conj(fs))
}
def list_disj(l: List[Formula]) : Formula = l match {
case Nil => False
case f::Nil => f
case f::fs => Binop(Or,f, list_disj(fs))
}
def negative(fm: Formula) : Boolean = fm match {
case Not(p) => true
case _ => false
}
def positive(fm: Formula) : Boolean = fm match {
case Not(p) => false
case _ => true
}
def negate(fm: Formula) : Formula = fm match {
case Not(p) => p
case p => Not(p)
}
def cnnf(lfn: Formula => Formula ) : Formula => Formula = {
def cnnf_aux(fm: Formula): Formula = fm match {
case Binop(And,p,q) => Binop(And,cnnf_aux(p), cnnf_aux(q))
case Binop(Or,p,q) => Binop(Or,cnnf_aux(p), cnnf_aux(q))
case Binop(Imp,p,q) => Binop(Or,cnnf_aux(Not(p)), cnnf_aux(q))
case Binop(Iff,p,q) => Binop(Or,Binop(And,cnnf_aux(p), cnnf_aux(q)),
Binop(And,cnnf_aux(Not(p)), cnnf_aux(Not(q))))
case Not(Not(p)) => cnnf_aux(p)
case Not(Binop(And,p,q)) => Binop(Or,cnnf_aux(Not(p)), cnnf_aux(Not(q)))
case Not(Binop(Or,Binop(And,p,q),Binop(And,p_1,r))) if p_1 == negate(p) =>
Binop(Or,cnnf_aux(Binop(And,p,Not(q))), cnnf_aux(Binop(And,p_1,Not(r))))
case Not(Binop(Or,p,q)) => Binop(And,cnnf_aux(Not(p)),cnnf_aux(Not(q)))
case Not(Binop(Imp,p,q)) => Binop(And,cnnf_aux(p), cnnf_aux(Not(q)))
case Not(Binop(Iff,p,q)) => Binop(Or,Binop(And,cnnf_aux(p),cnnf_aux(Not(q))),
Binop(And,cnnf_aux(Not(p)),cnnf_aux(q)))
case _ => lfn(fm)
}
fm => simplify(cnnf_aux(simplify(fm)))
}
val rZero = new Exact.Rational(0);
val rOne = new Exact.Rational(1);
val zero = Num(rZero)
val one = Num(rOne)
/* Polynomial utilities.
*/
def poly_add(vars: List[String], pol1: Term, pol2: Term): Term =
(pol1,pol2) match {
case (Fn("+", List(c, Fn("*",List(Var(x),p)))),
Fn("+", List(d, Fn("*",List(Var(y),q))))) =>
if(earlier(vars,x,y)) poly_ladd(vars, pol2, pol1)
else if(earlier(vars,y,x)) poly_ladd(vars, pol1,pol2)
else {
val e = poly_add(vars,c,d);
val r = poly_add(vars,p,q);
if(r == zero) e
else Fn("+", List(e, Fn("*", List(Var(x), r))))
}
case (_,Fn("+",_)) => poly_ladd(vars,pol1,pol2)
case (Fn("+",_),_) => poly_ladd(vars,pol2,pol1)
case (Num(n),Num(m)) => Num(n + m)
case _ => zero
}
def poly_ladd(vars: List[String], pol1: Term, pol2: Term): Term =
pol2 match {
case (Fn("+",List(d,Fn("*",List(Var(y),q))))) =>
Fn("+",List(poly_add(vars, pol1, d), Fn("*", List(Var(y), q))))
case _ => throw new Error("poly_ladd: malformed input")
}
def poly_neg(q: Term): Term = q match {
case Fn("+",List(c,Fn("*",List(Var(x),p)))) =>
Fn("+",List(poly_neg(c), Fn("*",List(Var(x), poly_neg(p)))))
case Num(n) => Num(-n)
case _ => throw new Error("impossible")
}
def poly_sub(vars: List[String], p: Term, q: Term): Term = {
val q1 = poly_neg(q);
val r =poly_add(vars, p, poly_neg(q));
r
}
def poly_mul(vars: List[String], pol1: Term, pol2: Term): Term =
(pol1,pol2) match {
case (Fn("+", List(c, Fn("*",List(Var(x),p)))),
Fn("+", List(d, Fn("*",List(Var(y),q))))) =>
if(earlier(vars,x,y)) poly_lmul(vars, pol2, pol1)
else poly_lmul(vars, pol1, pol2)
case (Num(n), _) if n.is_zero => zero
case (_,Num(n)) if n.is_zero => zero
case (_,Fn("+",_)) => poly_lmul(vars,pol1,pol2)
case (Fn("+",_),_) => poly_lmul(vars,pol2,pol1)
case (Num(n),Num(m)) => Num(n * m)
case _ => zero
}
def poly_lmul(vars: List[String], pol1: Term, pol2: Term): Term =
pol2 match {
case (Fn("+",List(d,Fn("*",List(Var(y),q))))) =>
poly_add(vars, poly_mul(vars, pol1, d),
Fn("+",List(zero,
Fn("*",List(Var(y), poly_mul(vars,pol1,q))))))
case _ => throw new Error("poly_lmul: malformed input")
}
def funpow[A](n: Int, f: A => A, x: A): A = {
if( n < 1 ) x
else funpow(n-1, f, f(x))
}
def poly_pow(vars: List[String], p: Term, n: Int): Term = {
funpow(n, (q:Term) => poly_mul(vars,p,q), one)
}
/* I don't think we need this.
def poly_div(vars: List[String], p: Term, q: Term) = q match {
case Num(n) => poly_mul(vars, p, Num(1.0/n) ... ?
*/
def poly_var(x: String): Term = {
Fn("+",List(zero,Fn("*",List(Var(x), one))))
}
/* Put tm into canonical form.
*/
def polynate(vars: List[String], tm: Term): Term = tm match {
case Var(x) => poly_var(x)
case Fn("-", t::Nil) => poly_neg(polynate(vars,t))
case Fn("+", List(s,t)) => poly_add(vars,polynate(vars,s),
polynate(vars,t))
case Fn("-", List(s,t)) => poly_sub(vars,polynate(vars,s),
polynate(vars,t))
case Fn("*", List(s,t)) => poly_mul(vars,polynate(vars,s),
polynate(vars,t))
case Fn("/", List(Num(n),Num(m))) => Num(n / m)
case Fn("^", List(p,Num(n))) =>
poly_pow(vars,polynate(vars,p),n.intValue) //n is a Rational.
case Num(n) => tm
case _ => throw new Error("Unknown term: " + tm)
}
def polyatom(vars: List[String], fm: Formula): Formula = fm match {
case Atom(R(a,List(s,t))) =>
val r = Atom(R(a,List(polynate(vars,Fn("-",List(s,t))),zero)));
r
case _ => throw new Error("polyatom: not an atom.")
}
def coefficients(vars: List[String], p: Term): List[Term] = p match {
case Fn("+", List(c, Fn("*", List(Var(x), q)))) if x == vars.head =>
c::(coefficients(vars,q))
case _ => List(p)
}
def degree(vars: List[String], p: Term): Int = {
(coefficients(vars,p).length - 1)
}
def is_constant(vars: List[String], p: Term): Boolean = {
degree(vars,p) == 0
}
def head(vars: List[String], p: Term): Term = {
coefficients(vars,p).last
}
def behead(vars: List[String], tm: Term): Term = tm match {
case Fn("+",List(c,Fn("*",List(Var(x),p)))) if x == vars.head =>
val p1 = behead(vars,p);
if(p1 == zero) c else Fn("+",List(c,Fn("*",List(Var(x),p1))))
case _ => zero
}
def poly_cmul(k: Exact.Num, p: Term): Term = p match {
case Fn("+", List(c, Fn("*", List( Var(x), q)))) =>
Fn("+", List(poly_cmul(k,c),
Fn("*",List(Var(x),
poly_cmul(k,q)))))
case Num(n) => Num(n * k)
case _ => throw new Error("poly_cmul: non-canonical term" + p)
}
def headconst(p: Term): Exact.Num = p match {
case Fn("+",List(c,Fn("*",List(Var(x),q)))) => headconst(q)
case Num(n) => n
case _ => throw new Error("headconst: malformed polynomial")
}
def monic(p: Term): (Term,Boolean) = {
val h = headconst(p);
if(h.is_zero) (p,false)
else (poly_cmul(rOne / h, p), h < rZero)
}
val pdivide: List[String] => Term => Term => (Int, Term) = {
def shift1(x: String): Term => Term = p => Fn("+",List(zero,
Fn("*",List(Var(x),
p))));
def pdivide_aux(vars: List[String],
a: Term,
n: Int,
p: Term,
k: Int,
s: Term): (Int, Term) = {
if(s == zero) (k,s) else {
val b = head(vars, s);
val m = degree(vars, s);
if(m < n) (k,s) else {
val p_1 = funpow(m-n, shift1(vars.head), p);
if(a == b) pdivide_aux(vars,a,n,p,k,poly_sub(vars,s,p_1))
else pdivide_aux(vars,a,n,p,k+1,
poly_sub(vars,poly_mul(vars,a,s),
poly_mul(vars,b,p_1)))
}
}
};
vars => s => p => pdivide_aux(vars, head(vars,p), degree(vars,p), p, 0, s)
}
def poly_diffn(x: Term, n: Int, p: Term): Term = p match {
case Fn("+", List(c, Fn("*", List(y,q)))) if y == x =>
Fn("+", List(poly_cmul(new Exact.Rational(n), c),
Fn("*", List(x, poly_diffn(x,n+1,q)))))
case _ => poly_cmul( new Exact.Rational(n), p)
}
def poly_diff(vars: List[String], p: Term): Term = p match {
case Fn("+", List(c, Fn("*", List(Var(x), q)))) if x == vars.head =>
poly_diffn(Var(x), 1, q)
case _ => zero
}
/* End polynomical utilities.
*/
def swap(swf: Boolean, s: Sign): Sign = {
if(!swf) s else s match {
case Positive() => Negative()
case Negative() => Positive()
case _ => s
}
}
def qelim(bfn: Formula => Formula, x: String, p: Formula): Formula = {
val cjs = conjuncts(p);
val (ycjs, ncjs) = cjs.partition(c => fv(c).contains(x));
if(ycjs == Nil) p else {
val q = bfn(Quantifier(Exists, x, Real, list_conj(ycjs)));
val r = ncjs.foldLeft(q)(mk_and)
print("|");
r
}
}
def lift_qelim(afn: (List[String], Formula) => Formula,
nfn: Formula => Formula,
qfn: List[String] => Formula => Formula) :
Formula => Formula = {
def qelift(vars: List[String], fm: Formula): Formula = fm match {
case Atom(R(_,_)) => afn(vars,fm)
case Not(p) => Not(qelift(vars,p))
case Binop(c,p,q) =>
Binop(c,qelift(vars,p), qelift(vars,q))
case Quantifier(Forall, x, Real, p) =>
Not(qelift(vars,Quantifier(Exists, x, Real, Not(p))))
case Quantifier(Exists, x, Real, p) =>
val djs = disjuncts(nfn(qelift(x::vars,p)));
println("In qelift. Number of disjuncts = " + djs.length);
print("[");
for(i <- 0 until djs.length){ print(".");}
print("]\u0008");
for(i <- 0 until djs.length){ print("\u0008");}
val djs2 = djs.map(p1 => qelim(qfn(vars), x, p1));
val r = list_disj(djs2)
println("]");
r
// list_disj(Parallel.pmap(djs, ((p1:Formula) => qelim(qfn(vars), x, p1))))
case _ => fm
}
fm => {
val m = miniscope(fm);
val f = fv(fm);
val q = qelift( f, m);
val r = simplify(q)
r
}
}
class FindSignFailure() extends Exception;
def findsign(sgns: List[(Term,Sign)], p: Term): Sign =
try {
val (p_1,swf) = monic(p);
swap(swf,assoc(p_1,sgns))
} catch {
case e : Throwable => throw new FindSignFailure()
}
def assertsign(sgns: List[(Term,Sign)], pr: (Term,Sign)): List[(Term,Sign)]
= {
val (p,s) = pr;
if( p == zero ) {
if(s == Zero()) sgns
else throw new Error("assertsign") }
else {
val (p_1,swf) = monic(p);
val s_1 = swap(swf,s);
val s_0 = try { assoc(p_1,sgns) } catch { case e : Throwable => s_1};
if(s_1 == s_0 || (s_0 == Nonzero() && (s_1==Positive() || s_1==Negative())))
(p_1,s_1)::(sgns filterNot ( List((p_1,s_0)) contains ))
else throw new Error("assertsign 1")
}
}
final def split_zero(sgns: List[(Term,Sign)], pol: Term,
cont_z: List[(Term,Sign)] => Formula,
cont_n: List[(Term,Sign)] => Formula) : Formula
= try {
val z = findsign(sgns,pol);
(if(z == Zero()) cont_z else cont_n)(sgns)
} catch {
case f: FindSignFailure =>
val eq = Atom(R("=",List(pol,zero)));
Binop(Or,Binop(And,eq, cont_z(assertsign(sgns, (pol,Zero())))),
Binop(And,Not(eq), cont_n(assertsign(sgns,(pol,Nonzero())))))
}
val rel_signs = List(("=", List(Zero())),
("<=", List(Zero(), Negative())),
(">=", List(Zero(), Positive())),
("<", List(Negative())),
(">", List(Positive())) )
def testform(pmat: List[(Term, Sign)], fm: Formula): Boolean = {
// println("in testform. pmat = ");
// pmat.map( x => {print("(");
// P.printert(x._1);
// println(", " + x._2 + ")");});
// println("fm = ");
// P.print_fol_formula(fm);
// println();
def f(r: Pred): Boolean = r match {
case R(a,List(p,z)) =>
mem(assoc(p, pmat), assoc(a, rel_signs))
case _ => throw new Error("testform: bad Pred:" + r)
};
eval(fm, f)
}
def inferpsign(pr: (List[Sign], List[Sign])): List[Sign] = pr match {
case (pd,qd) =>
try {
val i = index(Zero(), pd);
el(i,qd)::pd
} catch {
case e:Failure => Nonzero() :: pd
}
}
def condense(ps: List[List[Sign]]): List[List[Sign]] = ps match {
case int::pt::other =>
val rest = condense(other);
if(mem(Zero(), pt)) int::pt::rest
else rest
case _ => ps
}
def inferisign(ps: List[List[Sign]]): List[List[Sign]] = ps match {
case ((x@(l::ls))::(_::ints)::(pts@((r::rs)::xs))) =>
(l,r) match {
case (Zero(), Zero()) => throw new Error("inferisign: inconsistent")
case (Nonzero() ,_)
| (_, Nonzero()) => throw new Error("inferisign: indeterminate")
case (Zero(),_) => x::(r::ints)::inferisign(pts)
case (_,Zero()) => x::(l::ints)::inferisign(pts)
case (Negative(), Negative())
| (Positive(), Positive()) =>
x::(l::ints)::inferisign(pts)
case _ => x::(l::ints)::(Zero()::ints)::(r::ints)::inferisign(pts)
}
case _ => ps
}
def dedmatrix(cont: List[List[Sign]] => Formula,
mat: List[List[Sign]]) : Formula = {
val l = (mat.head).length / 2;
val mat1 = condense(mat.map((lst:List[Sign])=>inferpsign(lst.splitAt(l))));
// val mat1 = condense(Parallel.pmap(mat,(lst:List[Sign])=>inferpsign(lst.splitAt(l))));
val mat2 = List(swap(true, el(1,mat1.head)))::
(mat1 ++ List(List(el(1,mat1.last))));
val mat3 = inferisign(mat2).tail.init;
cont(condense(mat3.map((l:List[Sign]) => l.head :: l.tail.tail)))
}
def pdivide_pos(vars: List[String], sgns: List[(Term,Sign)],
s: Term, p: Term): Term
= {
val a = head(vars,p);
val (k,r) = pdivide(vars)(s)(p);
val sgn = findsign(sgns,a);
if(sgn == Zero()) throw new Error("pdivide_pos: zero head coefficient.")
else if(sgn == Positive() || (k % 2) == 0) r
else if(sgn == Negative()) poly_neg(r)
else poly_mul(vars,a,r)
}
def split_sign(sgns: List[(Term,Sign)], pol: Term,
cont: List[(Term,Sign)] => Formula) : Formula =
findsign(sgns, pol) match {
case Nonzero() =>
val fm = Atom(R(">",List(pol,zero)));
Binop(Or,Binop(And,fm,cont(assertsign(sgns,(pol,Positive())))),
Binop(And,Not(fm),cont(assertsign(sgns,(pol,Negative())))))
case _ => cont(sgns)
}
final def split_trichotomy(sgns: List[(Term,Sign)],
pol: Term,
cont_z: List[(Term,Sign)] => Formula,
cont_pn: List[(Term,Sign)] => Formula) : Formula =
split_zero(sgns,pol,cont_z,(s_1 => split_sign(s_1,pol,cont_pn)))
/* inlined
final def monicize(vars: List[String],
pols: List[Term],
cont: List[List[Sign]] => Formula,
sgns: List[(Term,Sign)] ): Formula = {
val (mols,swaps) = List.unzip(pols.map(monic));
val sols = setify(mols);
val indices = mols.map(p => index(p, sols));
def transform(m: List[Sign]) : List[Sign] = {
(swaps zip indices).map( pr => swap(pr._1, el(pr._2, m)))}
val (cont_1 : (List[List[Sign]] => Formula)) = mat => cont(mat.map(transform));
matrix(vars,sols,cont_1,sgns)
}
*/
final def casesplit(vars: List[String],
dun: List[Term],
pols: List[Term],
cont: List[List[Sign]] => Formula):
List[(Term,Sign)] => Formula = sgns => pols match {
// case Nil => monicize(vars,dun,cont,sgns)
// case Nil => matrix(vars,dun,cont,sgns)
case Nil => val (mols,swaps) = dun.map(monic).unzip;
val sols = setify(mols);
val indices = mols.map(p => index(p, sols));
def transform(m: List[Sign]) : List[Sign] = {
(swaps zip indices).map( pr => swap(pr._1, el(pr._2, m)))}
val (cont_1 : (List[List[Sign]] => Formula)) = mat => cont(mat.map(transform));
matrix(vars,sols,cont_1,sgns)
case p::ops =>
split_trichotomy(sgns,head(vars,p),
(if(is_constant(vars,p)) delconst(vars,dun,p,ops,cont)
else casesplit(vars,dun,behead(vars,p)::ops,cont)),
(if(is_constant(vars,p)) delconst(vars,dun,p,ops,cont)
else casesplit(vars,dun++List(p),ops,cont)))
}
final def delconst(vars: List[String],
dun: List[Term],
p: Term,
ops: List[Term],
cont: List[List[Sign]] => Formula) :
List[(Term,Sign)] => Formula = sgns => {
def cont_1(m: List[List[Sign]]): Formula =
cont(m.map((rw:List[Sign]) => insertat(dun.length,findsign(sgns,p),rw)));
casesplit(vars,dun,ops,cont_1)(sgns)
}
final def matrix(vars: List[String],
pols: List[Term],
cont: List[List[Sign]] => Formula,
sgns: List[(Term,Sign)]): Formula = {
// CV.lock.synchronized{
if(CV.keepGoing == false) throw new CHAbort();
// }
if(pols == Nil) try { cont(List(Nil)) } catch {case e : Throwable => False} else {
/* find the polynomial of highest degree */
val (p,_) = pols.foldLeft[(Term,Int)](zero,-1)(
(bst:(Term,Int),ths:Term) => {val (p_1,n_1) = bst;
val n_2 = degree(vars, ths);
if(n_2 > n_1) (ths,n_2) else bst});
val p_1 = poly_diff(vars,p);
val i = index(p,pols);
val qs = {val (p1,p2) = pols.splitAt(i);
p_1::p1 ++ p2.tail};
// println("in matrix. number of divisions to perform = " + qs.length);
val gs = qs.map((p_3:Term) => pdivide_pos(vars,sgns,p,p_3));
// val gs = Parallel.pmap(qs,((p_3:Term) => pdivide_pos(vars,sgns,p,p_3)));
def cont_1(m: List[List[Sign]]): Formula =
cont(m.map(l => insertat(i,l.head,l.tail)));
casesplit(vars, Nil, qs ++ gs, ls => dedmatrix(cont_1,ls))(sgns)
}
}
val init_sgns:List[(Term,Sign)] = List((one, Positive()),
(zero, Zero()));
def basic_real_qelim(vars: List[String]): Formula => Formula
= fm => fm match {
case Quantifier(Exists, x, Real, p) =>
val pols = atom_union(
fm1 => fm1 match{case R(a,List(t,Num(n))) if n.is_zero => List(t)
case _ => Nil},
p);
val cont = (mat:List[List[Sign]]) =>
if(mat.exists(m => testform(pols.zip(m),p))) True else False;
casesplit(x::vars, Nil, pols, cont)(init_sgns)
case _ =>
throw new Error("impossible")
}
def real_elim(fm: Formula): Formula = {
simplify(evalc(lift_qelim(polyatom,
fm1 => simplify(evalc(fm1)),
basic_real_qelim)(fm)))
}
/* better version that first converts to dnf */
def real_elim2(fm: Formula): Formula = {
simplify(evalc(lift_qelim(polyatom,
fm1 => dnf(cnnf( (x:Formula)=>x)(evalc(fm1))),
basic_real_qelim)(fm)))
}
def univ_close(fm: Formula): Formula = {
val fvs = fv(fm);
fvs.foldRight(fm) ((v,fm1) => Quantifier(Forall, v, Real, fm1))
}
def unaryFns_Term(tm : Term): List[String] = tm match {
case Fn(f, Nil) => List(f)
case Fn(f, args) =>
args.map(unaryFns_Term).flatten.distinct
case _ => Nil
}
def unaryFns_Pred(fol: Pred) : List[String] = fol match {
case R(r, args) =>
args.map(unaryFns_Term).flatten.distinct
}
def replaceUnaryFns_Term(tm : Term): Term = tm match {
case Fn(f, Nil) => Var(f)
case Fn(f, args) => Fn(f, args.map(replaceUnaryFns_Term))
case _ => tm
}
def replaceUnaryFns_Pred(fol: Pred) : Pred = fol match {
case R(r, args) =>
R(r,args.map(replaceUnaryFns_Term))
}