00001
00002
00003 #define _CVCL_TRUSTED_
00004
00005 #include <iostream>
00006 #include "refined_arith_theorem_producer.h"
00007 #include "arith_theorem_producer.h"
00008 #include "theory_arith.h"
00009 #include "vcl.h"
00010 #include "common_proof_rules.h"
00011 #include "theory_core.h"
00012
00013 using namespace std;
00014 using namespace CVCL;
00015
00016
00017
00018
00019
00020
00021
00022 Theorem RefinedArithTheoremProducer::canonPlus(const Expr& e){
00023 cout << "In Sean's canonPlus!!!" << endl;
00024 return ArithTheoremProducer::canonPlus(e);
00025 }
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 Expr RefinedArithTheoremProducer::neg(const Expr& x){
00036 return Expr(d_em,UMINUS,x);
00037 }
00038
00039 Expr RefinedArithTheoremProducer::inv(const Expr& x){
00040 return Expr(d_em,DIVIDE,one,x);
00041 }
00042
00043 Expr RefinedArithTheoremProducer::plus(const Expr& x,const Expr& y){
00044 return Expr(d_em,PLUS,x,y);
00045 }
00046
00047 Expr RefinedArithTheoremProducer::frac(const Expr& x,const Expr& y){
00048 return Expr(d_em,DIVIDE,x,y);
00049 }
00050
00051
00052 Theorem RefinedArithTheoremProducer::trans(const Theorem& t1,const Theorem& t2){
00053 return core->transitivityRule(t1,t2);
00054 }
00055
00056 Theorem RefinedArithTheoremProducer::trans(const Expr& e){
00057 return core->reflexivityRule(e);
00058 }
00059
00060 Theorem RefinedArithTheoremProducer::symm(const Theorem& t){
00061 return core->symmetryRule(t);
00062 }
00063
00064 Theorem RefinedArithTheoremProducer::subst(Op op,const Expr& x,const Theorem& t){
00065 vector<Theorem> thms;
00066 thms.push_back(core->reflexivityRule(x));
00067 thms.push_back(t);
00068 return core->substitutivityRule(op,thms);
00069 }
00070
00071 Theorem RefinedArithTheoremProducer::subst(Op op,const Theorem& t,const Expr& x){
00072 vector<Theorem> thms;
00073 thms.push_back(t);
00074 thms.push_back(core->reflexivityRule(x));
00075 return core->substitutivityRule(op,thms);
00076 }
00077
00078 Theorem RefinedArithTheoremProducer::subst(Op op,const Theorem& t1,const Theorem& t2){
00079 vector<Theorem> thms;
00080 thms.push_back(t1);
00081 thms.push_back(t2);
00082 return core->substitutivityRule(op,thms);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 Theorem RefinedArithTheoremProducer::addAssoc(const Expr& x,const Expr& y,const Expr& z){
00103 Assumptions assum;
00104 Proof pf;
00105 Expr xPy = Expr(d_tm->getEM(),PLUS,x,y);
00106 Expr lhs = Expr(d_tm->getEM(),PLUS,xPy,z);
00107 Expr yPz = Expr(d_tm->getEM(),PLUS,y,z);
00108 Expr rhs = Expr(d_tm->getEM(),PLUS,x,yPz);
00109 if(withProof()) pf = newPf("add_assoc",x,y,z);
00110 return newRWTheorem(lhs,rhs,assum,pf);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 Theorem RefinedArithTheoremProducer::multAssoc(const Expr& x,const Expr& y,const Expr& z){
00125 Assumptions assum;
00126 Proof pf;
00127 Expr xy = Expr(d_tm->getEM(),MULT,x,y);
00128 Expr lhs = Expr(d_tm->getEM(),MULT,xy,z);
00129 Expr yz = Expr(d_tm->getEM(),MULT,y,z);
00130 Expr rhs = Expr(d_tm->getEM(),MULT,x,yz);
00131 if(withProof()) pf = newPf("mult_assoc",x,y,z);
00132 return newRWTheorem(lhs,rhs,assum,pf);
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 Theorem RefinedArithTheoremProducer::addSymm(const Expr& x,const Expr& y){
00146 Assumptions assum;
00147 Proof pf;
00148 Expr lhs = Expr(d_tm->getEM(),PLUS,x,y);
00149 Expr rhs = Expr(d_tm->getEM(),PLUS,y,x);
00150 if(withProof()) pf = newPf("add_symm",x,y);
00151 return newRWTheorem(lhs,rhs,assum,pf);
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 Theorem RefinedArithTheoremProducer::multSymm(const Expr& x,const Expr& y){
00165 Assumptions assum;
00166 Proof pf;
00167 Expr lhs = Expr(d_tm->getEM(),MULT,x,y);
00168 Expr rhs = Expr(d_tm->getEM(),MULT,y,x);
00169 if(withProof()) pf = newPf("mult_symm",x,y);
00170 return newRWTheorem(lhs,rhs,assum,pf);
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 Theorem RefinedArithTheoremProducer::distribL(const Expr& x,const Expr& y,const Expr& z){
00185 Assumptions assum;
00186 Proof pf;
00187 Expr yPz = Expr(d_tm->getEM(),PLUS,y,z);
00188 Expr lhs = Expr(d_tm->getEM(),MULT,x,yPz);
00189 Expr xy = Expr(d_tm->getEM(),MULT,x,y);
00190 Expr xz = Expr(d_tm->getEM(),MULT,x,z);
00191 Expr rhs = Expr(d_tm->getEM(),PLUS,xy,xz);
00192 if(withProof()) pf = newPf("distrib_l",x,y,z);
00193 return newRWTheorem(lhs,rhs,assum,pf);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 Theorem RefinedArithTheoremProducer::addLID(const Expr& x){
00206 Assumptions assum;
00207 Proof pf;
00208 Expr lhs = Expr(d_tm->getEM(),PLUS,zero,x);
00209 Expr rhs = x;
00210 if(withProof()) pf = newPf("add_lid",x);
00211 return newRWTheorem(lhs,rhs,assum,pf);
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 Theorem RefinedArithTheoremProducer::multLID(const Expr& x){
00224 Assumptions assum;
00225 Proof pf;
00226 Expr lhs = Expr(d_tm->getEM(),MULT,one,x);
00227 Expr rhs = x;
00228 if(withProof()) pf = newPf("mult_lid",x);
00229 return newRWTheorem(lhs,rhs,assum,pf);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 Theorem RefinedArithTheoremProducer::addInvR(const Expr& x){
00242 Assumptions assum;
00243 Proof pf;
00244 Expr ix = Expr(d_tm->getEM(),UMINUS,x);
00245 Expr lhs = Expr(d_tm->getEM(),PLUS,x,ix);
00246 Expr rhs = zero;
00247 if(withProof()) pf = newPf("add_inv_r",x);
00248 return newRWTheorem(lhs,rhs,assum,pf);
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 Theorem RefinedArithTheoremProducer::multInvR(const Expr& x){
00261 Assumptions assum;
00262 Proof pf;
00263 Expr ix = Expr(d_tm->getEM(),DIVIDE,one,x);
00264 Expr lhs = Expr(d_tm->getEM(),MULT,x,ix);
00265 Expr rhs = one;
00266 if(withProof()) pf = newPf("mult_inv_r",x);
00267 return newRWTheorem(lhs,rhs,assum,pf);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 Theorem RefinedArithTheoremProducer::ltAddR(const Expr& x,const Expr& y,const Expr& z){
00282 Assumptions assum;
00283 Proof pf;
00284 Expr ant = Expr(d_tm->getEM(),LT,x,y);
00285 Expr lhs = Expr(d_tm->getEM(),PLUS,x,z);
00286 Expr rhs = Expr(d_tm->getEM(),PLUS,y,z);
00287 Expr cons = Expr(d_tm->getEM(),LT,lhs,rhs);
00288 Expr ret = Expr(d_tm->getEM(),IMPLIES,ant,cons);
00289 if(withProof()) pf = newPf("lt_add_r",x,y,z);
00290 return newTheorem(ret,assum,pf);
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 Theorem RefinedArithTheoremProducer::ltTrans(const Expr& x,const Expr& y,const Expr& z){
00305 Assumptions assum;
00306 Proof pf;
00307 Expr ant1 = Expr(d_tm->getEM(),LT,x,y);
00308 Expr ant2 = Expr(d_tm->getEM(),LT,y,z);
00309 Expr cons = Expr(d_tm->getEM(),LT,x,z);
00310 Expr rhs = Expr(d_tm->getEM(),IMPLIES,ant2,cons);
00311 Expr ret = Expr(d_tm->getEM(),IMPLIES,ant1,rhs);
00312 if(withProof()) pf = newPf("lt_trans",x,y,z);
00313 return newTheorem(ret,assum,pf);
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 Theorem RefinedArithTheoremProducer::ltScale(const Expr& x,const Expr& y,const Expr& z){
00328 Assumptions assum;
00329 Proof pf;
00330 Expr ant1 = Expr(d_tm->getEM(),LT,x,y);
00331 Expr ant2 = Expr(d_tm->getEM(),LT,zero,z);
00332 Expr xz = Expr(d_tm->getEM(),MULT,x,z);
00333 Expr yz = Expr(d_tm->getEM(),MULT,y,z);
00334 Expr cons = Expr(d_tm->getEM(),LT,xz,yz);
00335 Expr rhs = Expr(d_tm->getEM(),IMPLIES,ant2,cons);
00336 Expr ret = Expr(d_tm->getEM(),IMPLIES,ant1,rhs);
00337 if(withProof()) pf = newPf("lt_trans",x,y,z);
00338 return newTheorem(ret,assum,pf);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 Theorem RefinedArithTheoremProducer::arithmetic(const Expr& lhs,const Expr& rhs){
00353 Assumptions assum;
00354 Proof pf;
00355 Rational l = eval(lhs);
00356 Rational r = eval(rhs);
00357 if(l == r){
00358 if(withProof()) pf = newPf("arithmetic",lhs,rhs);
00359 return newRWTheorem(lhs,rhs,assum,pf);
00360 } else {
00361 CHECK_SOUND(false,"lhs and rhs not equal:\n lhs: " + lhs.toString()
00362 + "\n rhs: " + rhs.toString());
00363 }
00364 }
00365
00366 Rational RefinedArithTheoremProducer::eval(const Expr& e){
00367 vector<Rational> children;
00368 Rational val = 0;
00369 for (int i = 0; i < e.arity(); i++){
00370 Rational child = eval(e[i]);
00371 children.push_back(child);
00372 }
00373 switch (e.getKind()){
00374 case RATIONAL_EXPR:
00375 return e.getRational();
00376 break;
00377 case MULT:
00378 for(unsigned i = 0; i< children.size();i++)
00379 val *= children[i];
00380 return val;
00381 break;
00382 case UMINUS:
00383 return -(e[0].getRational());
00384 break;
00385 case PLUS:
00386 for(unsigned i = 0; i< children.size();i++)
00387 val += children[i];
00388 return val;
00389 break;
00390 case DIVIDE:
00391 return children[0]/childrent[1];
00392 break;
00393 default:
00394 CHECK_SOUND(false,"Can't evaluate rational expression: " + e.toString());
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 Theorem RefinedArithTheoremProducer::minusDef(const Expr& x,const Expr& y){
00413 Assumptions assum;
00414 Proof pf;
00415 Expr xMy = Expr(d_tm->getEM(),MINUS,x,y);
00416 Expr xPMy = plus(x,neg(y));
00417 if(withProof()) pf = newPf("minus_def",x,y);
00418 return newRWTheorem(xMy,xPMy,assum,pf);
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 Theorem RefinedArithTheoremProducer::divideDef(const Expr& a,const Expr& b){
00431 Assumptions assum;
00432 Proof pf;
00433 Expr aDb = Expr(d_tm->getEM(),DIVIDE,a,b);
00434 Expr xPMy = plus(x,neg(y));
00435 if(withProof()) pf = newPf("minus_def",x,y);
00436 return newRWTheorem(xMy,xPMy,assum,pf);
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 Theorem RefinedArithTheoremProducer::exOp(const Expr& e){
00448 Assumptions assum;
00449 Proof pf;
00450 int arity = p.arity();
00451 CHECK_SOUND(arity>=2,"op expressions have at least 2 children");
00452 int kind = e.getKind();
00453 Expr ex = Expr(d_tm->getEM(),kind,e[0],e[1]);
00454 for(int i=2;i<arity;i++){
00455 ex = Expr(d_tm->getEM(),kind,ex,e[i]);
00456 }
00457 if(withProof()) pf = newPf("exOp",e,ex);
00458 return newRWTheorem(e,ex,assum,pf);
00459 }
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 Theorem RefinedArithTheoremProducer::distribR(const Expr& x,const Expr& y,const Expr& z){
00489 Expr xPy = Expr(d_em,PLUS,x,y);
00490 Theorem t1 = multSymm(xPy,z);
00491 Theorem t2 = distribL(z,x,y);
00492 Theorem t3 = multSymm(z,x);
00493 Theorem t4 = multSymm(z,y);
00494 Theorem t5 = trans(t1,t2);
00495 Theorem t6 = subst(Op(PLUS),t3,t4);
00496 Theorem t7 = trans(t5,t6);
00497 return t7;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 Theorem RefinedArithTheoremProducer::addR(const Theorem& xEy,const Expr& z){
00510 Theorem ret = subst(Op(PLUS),xEy,z);
00511 return ret;
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 Theorem RefinedArithTheoremProducer::addRID(const Expr& x){
00531 Theorem t1 = addSymm(x,zero);
00532 Theorem t2 = addLID(x);
00533 Theorem ret = trans(t1,t2);
00534 return ret;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 Theorem RefinedArithTheoremProducer::cancelR(const Expr& x,const Expr& y){
00558 Expr yi = neg(y);
00559 Theorem t1 = addAssoc(x,y,yi);
00560 Theorem t2 = addInvR(y);
00561 Theorem t3 = subst(Op(PLUS),x,t2);
00562 Theorem t4 = addRID(x);
00563 Theorem t5 = trans(t1,t3);
00564 Theorem t6 = trans(t5,t4);
00565 return t6;
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 Theorem RefinedArithTheoremProducer::addRCancel(const Theorem& t){
00589 Expr x = t.getLHS()[0];
00590 Expr y = t.getRHS()[0];
00591 Expr z = t.getLHS()[1];
00592 Theorem t1 = addR(t,neg(z));
00593 Theorem t2 = cancelR(x,z);
00594 Theorem t3 = cancelR(y,z);
00595 Theorem t4 = symm(t2);
00596 Theorem t5 = trans(t4,t1);
00597 Theorem t6 = trans(t5,t3);
00598 return t6;
00599 }
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629 Theorem RefinedArithTheoremProducer::multZeroL(const Expr& e){
00630 Expr zz = Expr(d_tm->getEM(),PLUS,zero,zero);
00631 Theorem t1 = arithmetic(zero,zz);
00632 Theorem t2 = subst(Op(MULT),t1,e);
00633 Theorem t2a = distribR(zero,zero,e);
00634 Theorem t3 = trans(t2,t2a);
00635 Expr ze = t2.getLHS();
00636 Expr mze = neg(ze);
00637 Theorem t4 = addR(t3,mze);
00638 Theorem t5 = addAssoc(ze,ze,mze);
00639 Theorem t6 = trans(t4,t5);
00640 Theorem t7 = addInvR(ze);
00641 Theorem t8 = symm(t7);
00642 Theorem t9 = trans(t8,t6);
00643 Theorem t10 = subst(Op(PLUS),ze,t7);
00644 Theorem t11 = trans(t9,t10);
00645 Theorem t12 = addRID(ze);
00646 Theorem t13 = trans(t11,t12);
00647 Theorem ret = symm(t13);
00648 return ret;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 Theorem RefinedArithTheoremProducer::uMinusToMult(const Expr& y){
00689 Theorem t1 = multZeroL(y);
00690 Theorem t2 = symm(t1);
00691 Theorem t3 = arithmetic(zero,plus(neg(one),one));
00692 Theorem t4 = subst(Op(MULT),t3,y);
00693 Theorem t5 = trans(t2,t4);
00694 Theorem t6 = distribR(neg(one),one,y);
00695 Theorem t7 = trans(t5,t6);
00696 Theorem t8 = multLID(y);
00697 Expr m1y = t7.getRHS()[0];
00698 Theorem t9 = subst(Op(PLUS),m1y,t8);
00699 Theorem t10 = trans(t7,t9);
00700 Theorem t11 = addR(t10,neg(y));
00701 Theorem t12 = addLID(neg(y));
00702 Theorem t13 = symm(t12);
00703 Theorem t14 = trans(t13,t11);
00704 Theorem t15 = addAssoc(m1y,y,neg(y));
00705 Theorem t16 = trans(t14,t15);
00706 Theorem t17 = addInvR(y);
00707 Theorem t18 = subst(Op(PLUS),m1y,t17);
00708 Theorem t19 = trans(t16,t18);
00709 Theorem t20 = addRID(m1y);
00710 Theorem t21 = trans(t19,t20);
00711 return t21;
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 Theorem RefinedArithTheoremProducer::minusToPlus(const Expr& x,const Expr& y){
00732 Theorem t1 = minusDef(x,y);
00733 Theorem t2 = uMinusToMult(y);
00734 Theorem t3 = subst(Op(PLUS),x,t2);
00735 Theorem t4 = trans(t1,t3);
00736 return t4;
00737 }
00738