-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeducer.class.php
More file actions
1170 lines (930 loc) · 64.1 KB
/
deducer.class.php
File metadata and controls
1170 lines (930 loc) · 64.1 KB
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
<?php
require_once 'sum.class.php';
require_once 'equation.class.php';
/*
* deducer class
*/
class Deducer {
// deductions from equations
public $deductions;
// removed deductions
public $removed_deductions;
/*
* constructor - initialize deductions
*/
public function __construct() {
$this->deductions = array();
$this->removed_deductions = array();
}
/*
* returns the deductions as a string to be printed
*/
public function toString() {
$str = "";
foreach ($this->deductions as $deduction) $str .= $deduction->toString() . "\n";
// if ($this->removed_deductions) { $str .= "Removed Deductions: \n"; foreach ($this->removed_deductions as $deduction) $str .= $deduction->toString() . "\n"; }
return $str;
}
/*
* add a new deduction to the deductions and do self reductions
*/
public function add(Equation $deduction) {
// add the new equation to the set of deductions if it was not already added before
if (!$this->add_if_not_deduced($deduction)) return false;
// now do self deduction until there is no more to deduce
$this->self_deductions();
// remove unnecessary equations
$this->prune_totalities();
echo "Deductions after merge: " . $this->toString();
}
/*
* add a new deduction to the deduction set if it was not already there
*/
public function add_if_not_deduced(Equation $new_deduction) {
// go through the deductions and check if we already have this deduction already - return false if we already have it
foreach ($this->deductions as $old_deduction) if ($new_deduction->equals($old_deduction)) return false;
// go through the deductions that were added before and removed later and check if we already removed this deduction - return false if we already have it
foreach ($this->removed_deductions as $old_deduction) if ($new_deduction->equals($old_deduction)) return false;
echo "Adding new equation " . $new_deduction->toString() . " to deductions: \n" . $this->toString();
// add the new equation to the set of deductions
$this->deductions[] = $new_deduction;
// return true to indicate that we added a new deduction
return true;
}
/*
* returns if a deduction is a duplicate or not
*/
protected function duplicate_deduction($deduction_index) {
for ($i = 0; $i < count($this->deductions); $i++) if ($i != $deduction_index && $this->deductions[$deduction_index]->equals($this->deductions[$i])) return true;
return false;
}
/*
* removes a deduction from the deduction set
*/
protected function remove_deduction($deduction_index) {
echo "Removing deduction: " . $this->deductions[$deduction_index]->toString() . "\n";
$this->removed_deductions[] = $this->deductions[$deduction_index];
unset($this->deductions[$deduction_index]);
$this->deductions = array_values($this->deductions);
}
/*
* do self deduction - apply each deduction to the other deduction until deductions cannot get reduced anymore
*/
protected function self_deductions() {
// we have to have at least 2 deductions to be able to start self deductions
if (count($this->deductions) < 2) return;
// do self deduction until there is no more to deduce
while ($this->self_deduction());
}
/*
* do self deduction - apply each deduction to the other deduction until deductions
*/
protected function self_deduction() {
// debug:
echo "Executing self deduction\n";
// loop through the deductions and reduce when possible
for ($i = 0; $i < count($this->deductions); $i++) {
// get the deduction we have and remove it from the rest of the deductions
// echo "Self deduction for " . $this->deductions[$i]->toString() . "\n";
// now reduce the other deductions from this deduction - except itself
for ($j = 0; $j < count($this->deductions); $j++) {
// cannot apply the deduction to itself
if ($i == $j) continue;
// if the pair of deductions help reduce the system, try again until we reach the stable state where there are no more deductions
if ($this->reduce_deduction($i, $j)) return true;
}
}
// we checked all deductions with one another - no more reductions - return false to stop the loop
// echo "Deductions after self deduction: " . $this->toString();
return false;
}
/*
* reduce a single deduction from a new deduction when possible
*/
protected function reduce_deduction($from_deduction, $deduction_to_reduce) {
// debug: echo "Applying deduction " . $this->deductions[$from_deduction]->toString() . " to deduction: " . $this->deductions[$deduction_to_reduce]->toString() . "\n";
// variable reductions - direct replacements
if (is_a($this->deductions[$from_deduction]->left, 'Variable')) return $this->reduce_deduction_from_var($from_deduction, $deduction_to_reduce);
// term reductions - zero products
if (is_a($this->deductions[$from_deduction]->left, 'Term')) return $this->reduce_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// if we get a deduction that is something other than term or direct variable, something's wrong - error out
throw new Exception('Unknown deduction type: ' . print_r($this->deductions[$from_deduction]->left, true));
}
/*
* reduce a single deduction from a new zero product deduction
*/
protected function reduce_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// if the value is not zero, something's wrong
if ($this->deductions[$from_deduction]->right == '1') throw new Exception('Zero product valued at one: ' . $this->deductions[$from_deduction]->toString());
// deduction is a zero product type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Term')) return $this->reduce_zero_product_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// deduction is a variable = binary expression type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'BinaryExpression')) return $this->reduce_var_expr_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// deduction is a variable = boolean type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Boolean')) return $this->reduce_var_bool_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// deduction is a variable = another variable type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Variable')) return $this->reduce_var_var_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// deduction is a variable = constant value type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') && !is_object($this->deductions[$deduction_to_reduce]->right)) return $this->reduce_var_value_deduction_from_zero_product($from_deduction, $deduction_to_reduce);
// unknown deduction type
throw new Exception('Unknown deduction type: ' . $this->deductions[$deduction_to_reduce]->toString());
}
/*
* reduce a single variable in a var = constant type deduction from a new var = constant type deduction
*/
protected function reduce_var_value_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// nothing to change here
return false;
}
/*
* reduce a single variable in a var = variable type deduction from a new var = constant type deduction
*/
protected function reduce_var_var_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// if the zero product has only a single variable, it may be applicable but the way our deductions work, that should never happen - error out if that is the case
if (count($this->deductions[$from_deduction]->left->vars) == 1) throw new Exception('Encountered zero product with single variable: ' . $this->toString());
// normal zero product deductions cannot be used in reducing var = var type deductions - return false
return false;
}
/*
* reduce a single variable in a var = expr type deduction from a zero product deduction
*/
protected function reduce_var_expr_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// get the variable and expression of the deduction
$var = $this->deductions[$deduction_to_reduce]->left->copy();
$expr = $this->deductions[$deduction_to_reduce]->right->copy();
// apply the zero product in the expression - if the deduction has not changed, return false - no new deductions
if (!$expr->apply_zero_product($this->deductions[$from_deduction]->left)) return false;
// remove the old deduction
echo "Var = Expr Deduction reduced from zero product " . $this->deductions[$from_deduction]->toString() . "\n";
$this->remove_deduction($deduction_to_reduce);
// if the expression turned into a nothing, set the value as zero
if (count($expr->terms) == 0) {
$new_deduction = new Equation($var, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from zero product as Var = Zero " . $new_deduction->toString() . "\n";
return true;
}
// if the expression turned into a value, set the value as that
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 0) {
$new_deduction = new Equation($var, $expr->terms[0]->val);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from zero product as Var = Value " . $new_deduction->toString() . "\n";
exit;
return true;
}
// if the expression turned into a single variable, convert the deduction as such - if it's not negated, just set the variable itself
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 1) {
$new_deduction = new Equation($var, ($expr->terms[0]->vars[0]->negated ? $expr->terms[0]->vars[0] : $expr->terms[0]->vars[0]->var));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from zero product as Var = Var " . $new_deduction->toString() . "\n";
exit;
return true;
}
// regular reduction of var = expr to var = expr
$new_deduction = new Equation($var, $expr);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from zero product as Var = Expr " . $new_deduction->toString() . "\n";
return true;
}
/*
* reduce a single variable in a var = boolean variable type deduction from a zero product deduction
*/
protected function reduce_var_bool_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// if the zero product has only a single variable, it may be applicable but the way our deductions work, that should never happen - error out if that is the case
if (count($this->deductions[$from_deduction]->left->vars) == 1) throw new Exception('Encountered zero product with single variable: ' . $this->toString());
// normal zero product deductions cannot be used in reducing var = boolean type deductions - return false
return false;
}
/*
* reduce a zero product deduction from another zero product deduction
*/
protected function reduce_zero_product_deduction_from_zero_product($from_deduction, $deduction_to_reduce) {
// ignore totalities
if (count($this->deductions[$deduction_to_reduce]->left->vars) == 0) return false;
if (count($this->deductions[$from_deduction]->left->vars) == 0) return false;
// existing and new zero products
$old_zero_product = $this->deductions[$deduction_to_reduce]->left;
$new_zero_product = $this->deductions[$from_deduction]->left;
$old_zero_product->sort();
$new_zero_product->sort();
// different zero products with the same 2 variables
// e.g. x1x2 = 0 and x1'x2' = 0 => x1 = x2' or x1x2' = 0 and x1'x2 = 0 => x1 = x2 or x1'x2' = 0 and x1x2 = 0 => x1 = x2' or x1'x2 = 0 and x1x2' = 0 => x1 = x2
if (count($old_zero_product->vars) == 2 &&
count($new_zero_product->vars) == 2 &&
$old_zero_product->vars[0]->toString() == $new_zero_product->vars[0]->negate()->toString() &&
$old_zero_product->vars[1]->toString() == $new_zero_product->vars[1]->negate()->toString()) {
echo "Deducing from " . $old_zero_product->toString() . " = 0 and " . $new_zero_product->toString() . " = 0\n";
if ($old_zero_product->vars[0]->negated) echo $new_zero_product->vars[0]->var->toString() . " = " . $old_zero_product->vars[1]->toString() . "\n";
else echo $old_zero_product->vars[0]->var->toString() . " = " . $new_zero_product->vars[1]->toString() . "\n";
// add new deduction and remove old deductions
if ($old_zero_product->vars[0]->negated) $new_deduction = new Equation($new_zero_product->vars[0]->var->copy(), $old_zero_product->vars[1]->copy());
else $new_deduction = new Equation($old_zero_product->vars[0]->var->copy(), $new_zero_product->vars[1]->copy());
$this->remove_deduction($deduction_to_reduce);
$this->remove_deduction($from_deduction);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// identical deductions - remove one of them to de-dupe
if ($new_zero_product->toString() == $old_zero_product->toString()) {
echo "Identical deductions " . $old_zero_product->toString() . " = 0 and " . $new_zero_product->toString() . " = 0 - de-dupe\n";
$this->remove_deduction($deduction_to_reduce);
return true;
}
// loop through the term variables and see if they can be merged as a new deduction - there needs to be a negated variable and all other variables should appear identical or not appear at all
// e.g. x1x2x3 = 0 and x1x2' = 0 => x1x3 = 0 or x1x2 = 0 and x1x2' = 0 => x1 = 0
$new_product = new Term();
$negated_var = false;
$subset_product = true;
foreach ($new_zero_product->vars as $new_var) {
// search for the variable in the other expression
$variable_status = 'missing';
foreach ($old_zero_product->vars as $old_var) {
// variable appears identical
if ($old_var->toString() == $new_var->toString()) { $variable_status = 'identical'; break; }
// variable appears negated
if ($old_var->var->toString() == $new_var->var->toString()) { $variable_status = 'negated'; break; }
}
// if the variable does not appear identically, it cannot be a subset product
if ($variable_status != 'identical') $subset_product = false;
// if the variable appears identical in the other expression or does not appear at all, just add it to the new product as-is
if ($variable_status == 'identical' || $variable_status == 'missing') { $new_product->add($new_var); continue; }
// if the variable appears negated in the other expression
if ($variable_status == 'negated') {
// if we already encountered another negated variable, it means the expressions have more than one variable that appears negated - cannot be used for new deductions - return false
if ($negated_var !== false) return false;
// if we have not encountered a negated variable so far, set the negated variable
$negated_var = $new_var; continue;
}
}
// if all variables of the new zero product appears in the old zero product, old zero product is obsolete - remove
if ($subset_product) {
echo $new_zero_product->toString() . " = 0 makes " . $old_zero_product->toString() . " = 0 obsolete - remove\n";
$this->remove_deduction($deduction_to_reduce);
return true;
}
// if we could not find a negated variable, we cannot use these expressions for new deductions - return false
if ($negated_var === false) return false;
// now go through the other expression to include the variables we may have missed
foreach ($old_zero_product->vars as $old_var) {
// search for the variable in the other expression
$variable_status = 'missing';
foreach ($new_zero_product->vars as $new_var) {
// variable appears identical
if ($old_var->toString() == $new_var->toString()) { $variable_status = 'identical'; break; }
// variable appears negated
if ($old_var->var->toString() == $new_var->var->toString()) { $variable_status = 'negated'; break; }
}
// if the variable is identical or negated, we already processed it in the previous loop - nothing to do
if ($variable_status == 'identical' || $variable_status == 'negated') continue;
// if the variable does not appear in the other expression, just add it to the new product as-is
if ($variable_status == 'missing') { $new_product->add($old_var); continue; }
}
// if the term is down to a single variable, convert the deduction to that form - if the variable is negated, it means it's one - otherwise it's zero
if (count($new_product->vars) == 1) {
echo "Deducing new value from " . $old_zero_product->toString() . " = 0 and " . $new_zero_product->toString() . " = 0\n";
$new_deduction = new Equation($new_product->vars[0]->var->copy(), ($new_product->vars[0]->negated ? 1 : 0));
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// new zero product deduction
$new_deduction = new Equation($new_product, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Deduced new zero product from " . $old_zero_product->toString() . " = 0 and " . $new_zero_product->toString() . " = 0\n";
return true;
}
/*
* reduce a single deduction from a new var = something deduction
*/
protected function reduce_deduction_from_var($from_deduction, $deduction_to_reduce) {
// single variable = binary expression
if (is_object($this->deductions[$from_deduction]->right) && is_a($this->deductions[$from_deduction]->right, 'BinaryExpression')) return $this->reduce_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// single variable = boolean variable
if (is_object($this->deductions[$from_deduction]->right) && is_a($this->deductions[$from_deduction]->right, 'Boolean')) return $this->reduce_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// single variable = another variable
if (is_object($this->deductions[$from_deduction]->right) && is_a($this->deductions[$from_deduction]->right, 'Variable')) return $this->reduce_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// single variable = value
if (!is_object($this->deductions[$from_deduction]->right)) return $this->reduce_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// otherwise unknown deduction
throw new Exception('Unknown single variable deduction: ' . print_r($this->deductions[$from_deduction]->right, true));
}
/*
* reduce a single deduction from a new var = constant value type deduction
*/
protected function reduce_deduction_from_var_value($from_deduction, $deduction_to_reduce) {
// deduction is a zero product type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Term')) return $this->reduce_zero_product_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// deduction is a variable = binary expression type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'BinaryExpression')) return $this->reduce_var_expr_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// deduction is a variable = boolean type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Boolean')) return $this->reduce_var_bool_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// deduction is a variable = another variable type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Variable')) return $this->reduce_var_var_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// deduction is a variable = constant value type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') && !is_object($this->deductions[$deduction_to_reduce]->right)) return $this->reduce_var_value_deduction_from_var_value($from_deduction, $deduction_to_reduce);
// unknown deduction type
throw new Exception('Unknown deduction type: ' . $this->deductions[$deduction_to_reduce]->toString());
}
/*
* reduce a single variable in a var = boolean variable type deduction from a new var = constant type deduction
*/
protected function reduce_var_bool_deduction_from_var_value($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = value deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right);
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// if the variable appears negated, reduce the deduction with the negated value
if ($this->deductions[$deduction_to_reduce]->right->negate()->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = value deduction " . $this->deductions[$from_deduction]->toString() . " (negated)\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), ($this->deductions[$from_deduction]->right == '1' ? 0 : 1));
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// no new deductions - return false
return false;
}
/*
* reduce a single variable in a var = variable type deduction from a new var = constant type deduction
*/
protected function reduce_var_var_deduction_from_var_value($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = var deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = value deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right);
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// no new deductions - return false
return false;
}
/*
* reduce a single variable in a zero product deduction from a new variable deduction that equals a value
*/
protected function reduce_zero_product_deduction_from_var_value($from_deduction, $deduction_to_reduce) {
// if the term does not contain our variable, nothing to do
if (!$this->deductions[$deduction_to_reduce]->left->has_variable($this->deductions[$from_deduction]->left)) return false;
// get the zero product and apply the new value in the term
$zero_product = $this->deductions[$deduction_to_reduce]->left->copy();
$zero_product->apply_var($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right);
echo "Reducing zero product: " . $this->deductions[$deduction_to_reduce]->toString() . " from " . $this->deductions[$from_deduction]->toString() . "\n";
// get rid of the old deduction
$this->remove_deduction($deduction_to_reduce);
// if the term is has no variables and has a value, it must be a totality now - cannot be a contradiction - if it's a totality it will be pruned - otherwise error out
if (count($zero_product->vars) == 0) {
// if the deduction became a contradiction, error out
if ($zero_product->val == '1') throw new Exception('Unexpected zero product reduction contradiction');
// show that the deduction got reduced to totality
echo "Reduced deduction from zero product to totality\n";
return true;
}
// if the term is down to a single variable, convert the deduction to that form - if the variable is negated, it means it's one - otherwise it's zero
if (count($zero_product->vars) == 1) {
$new_deduction = new Equation($zero_product->vars[0]->var, ($zero_product->vars[0]->negated ? 1 : 0));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction from zero product to var = value: " . $new_deduction->toString() . "\n";
return true;
}
// regular zero product deduction
$new_deduction = new Equation($zero_product, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction: " . $new_deduction->toString() . "\n";
exit;
return true;
}
/*
* reduce a single variable in a var = expr type deduction from a new var = value type deduction
*/
protected function reduce_var_expr_deduction_from_var_value($from_deduction, $deduction_to_reduce) {
// get the variable and the expression
$var = $this->deductions[$deduction_to_reduce]->left->copy();
$expr = $this->deductions[$deduction_to_reduce]->right->copy();
// apply the variable replacement in the expression - if the expression has not changed, nothing to deduce
if (!$expr->apply_var($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right)) return false;
// remove the old deduction
echo "Var = Expr Deduction reduced from Var = Value Deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$this->remove_deduction($deduction_to_reduce);
// if the expression turned into a nothing, set the value as zero
if (count($expr->terms) == 0) {
$new_deduction = new Equation($var, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Value Deduction as Var = Zero " . $new_deduction->toString() . "\n";
return true;
}
// if the expression turned into a value, set the value as that
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 0) {
$new_deduction = new Equation($var, $expr->terms[0]->val);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Value Deduction as Var = Value " . $new_deduction->toString() . "\n";
exit;
return true;
}
// if the expression turned into a single variable, convert the deduction as such - if it's not negated, just set the variable itself
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 1) {
$new_deduction = new Equation($var, ($expr->terms[0]->vars[0]->negated ? $expr->terms[0]->vars[0] : $expr->terms[0]->vars[0]->var));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Value Deduction as Var = Var " . $new_deduction->toString() . "\n";
return true;
}
// regular reduction of var = expr to var = expr
$new_deduction = new Equation($var, $expr);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Value Deduction as Var = Expr " . $new_deduction->toString() . "\n";
return true;
}
/*
* reduce a single deduction from a new variable deduction that equals another variable
*/
protected function reduce_deduction_from_var_var($from_deduction, $deduction_to_reduce) {
// deduction is a zero product type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Term')) return $this->reduce_zero_product_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// deduction is a variable = binary expression type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'BinaryExpression')) return $this->reduce_var_expr_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// deduction is a variable = boolean type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Boolean')) return $this->reduce_var_bool_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// deduction is a variable = another variable type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Variable')) return $this->reduce_var_var_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// deduction is a variable = constant value type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') && !is_object($this->deductions[$deduction_to_reduce]->right)) return $this->reduce_var_value_deduction_from_var_var($from_deduction, $deduction_to_reduce);
// unknown deduction type
throw new Exception('Unknown deduction type: ' . $this->deductions[$deduction_to_reduce]->toString());
}
/*
* reduce a single variable in a var = expr type deduction from a new var = var type deduction
*/
protected function reduce_var_expr_deduction_from_var_var($from_deduction, $deduction_to_reduce) {
// get the variable and the expression
$var = $this->deductions[$deduction_to_reduce]->left->copy();
$expr = $this->deductions[$deduction_to_reduce]->right->copy();
// apply the variable replacement in the expression - if the expression has not changed, nothing to deduce
if (!$expr->apply_var_replace($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right)) return false;
// remove the old deduction
echo "Var = Expr Deduction reduced from Var = Var Deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$this->remove_deduction($this->deductions[$deduction_to_reduce]);
// if the expression turned into a nothing, set the value as zero
if (count($expr->terms) == 0) {
$new_deduction = new Equation($var, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Var Deduction as Var = Zero " . $new_deduction->toString() . "\n";
exit;
return true;
}
// if the expression turned into a value, set the value as that
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 0) {
$new_deduction = new Equation($var, $expr->terms[0]->val);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Var Deduction as Var = Value " . $this->deductions[$deduction_to_reduce]->toString() . "\n";
exit;
return true;
}
// if the expression turned into a single variable, convert the deduction as such - if it's not negated, just set the variable itself
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 1) {
$new_deduction = new Equation($var, ($expr->terms[0]->vars[0]->negated ? $expr->terms[0]->vars[0] : $expr->terms[0]->vars[0]->var));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Var Deduction as Var = Var " . $new_deduction->toString() . "\n";
exit;
return true;
}
// regular reduction of var = expr to var = expr
$new_deduction = new Equation($var, $expr);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Var Deduction as Var = Expr " . $new_deduction->toString() . "\n";
exit;
return true;
}
/*
* reduce a single variable in a var = boolean variable type deduction from a new var = var type deduction
*/
protected function reduce_var_bool_deduction_from_var_var($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = var deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right->copy());
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
exit;
return true;
}
// if the variable appears negated, reduce the deduction with the negated value
if ($this->deductions[$deduction_to_reduce]->right->negate()->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = var deduction " . $this->deductions[$from_deduction]->toString() . " (negated)\n";
$newval = new Boolean($this->deductions[$from_deduction]->right->copy());
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $newval->negate());
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
exit;
return true;
}
// no new deductions - return false
return false;
}
/*
* reduce a single variable in a zero product deduction from a new variable deduction that equals a value
*/
protected function reduce_zero_product_deduction_from_var_var($from_deduction, $deduction_to_reduce) {
// if the term does not contain our variable, no new deductions can be found
if (!$this->deductions[$deduction_to_reduce]->left->has_variable($this->deductions[$from_deduction]->left)) return false;
// term does contain our variable - apply deduction
echo "Reducing zero product deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from " . $this->deductions[$from_deduction]->toString() . "\n";
// get the zero product and apply the new value in the term
$zero_product = $this->deductions[$deduction_to_reduce]->left->copy();
$zero_product->apply_var_replace($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right);
// get rid of the old deduction
$this->remove_deduction($deduction_to_reduce);
// if the term is has no variables and has a value, it must be a totality now - cannot be a contradiction - if it's a totality it will be pruned - otherwise error out
if (count($zero_product->vars) == 0) {
// if the deduction became a contradiction, error out
if ($zero_product->val == '1') throw new Exception('Unexpected zero product reduction contradiction');
// show that the deduction got reduced to totality
echo "Reduced deduction from zero product to totality\n";
return true;
}
// if the term is down to a single variable, convert the deduction to that form - if the variable is negated, it means it's one - otherwise it's zero
if (count($zero_product->vars) == 1) {
$new_deduction = new Equation($zero_product->vars[0]->var, ($zero_product->vars[0]->negated ? 1 : 0));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction from zero product to var = var: " . $new_deduction->toString() . "\n";
exit;
return true;
}
// regular zero product deduction
$new_deduction = new Equation($zero_product, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction: " . $new_deduction->toString() . "\n";
exit;
return true;
}
/*
* reduce a single deduction from a new variable deduction that equals another boolean variable
*/
protected function reduce_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// deduction is a zero product type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Term')) return $this->reduce_zero_product_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// deduction is a variable = binary expression type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'BinaryExpression')) return $this->reduce_var_expr_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// deduction is a variable = boolean type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Boolean')) return $this->reduce_var_bool_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// deduction is a variable = another variable type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Variable')) return $this->reduce_var_var_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// deduction is a variable = constant value type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') && !is_object($this->deductions[$deduction_to_reduce]->right)) return $this->reduce_var_value_deduction_from_var_bool($from_deduction, $deduction_to_reduce);
// unknown deduction type
throw new Exception('Unknown deduction type: ' . $this->deductions[$deduction_to_reduce]->toString());
}
/*
* reduce a single variable in a var = constant type deduction from a new var = boolean variable type deduction
*/
protected function reduce_var_value_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// nothing to change here
return false;
}
/*
* reduce a single variable in a var = constant type deduction from a new var = variable type deduction
*/
protected function reduce_var_value_deduction_from_var_var($from_deduction, $deduction_to_reduce) {
// nothing to change here
return false;
}
/*
* reduce a single variable in a var = variable type deduction from a new var = boolean variable type deduction
*/
protected function reduce_var_var_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = var deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = bool deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right->copy());
if (!$new_deduction->right->negated) $new_deduction->right = $new_deduction->right->var;
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// if the variable does not appear, no new deductions
return false;
}
/*
* reduce a single variable in a var = expr type deduction from a new var = boolean variable type deduction
*/
protected function reduce_var_expr_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// get the variable and the expression
$var = $this->deductions[$deduction_to_reduce]->left->copy();
$expr = $this->deductions[$deduction_to_reduce]->right->copy();
// apply the variable replacement in the expression - if the expression has not changed, nothing to deduce
if (!$expr->apply_var_replace($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right)) return false;
// remove the old deduction
echo "Var = Expr Deduction reduced from Var = Bool Deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$this->remove_deduction($deduction_to_reduce);
// if the expression turned into a nothing, set the value as zero
if (count($expr->terms) == 0) {
$new_deduction = new Equation($var, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Bool Deduction as Var = Zero " . $new_deduction->toString() . "\n";
return true;
}
// if the expression turned into a value, set the value as that
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 0) {
$new_deduction = new Equation($var, $expr->terms[0]->val);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Bool Deduction as Var = Value " . $this->deductions[$deduction_to_reduce]->toString() . "\n";
exit;
return true;
}
// if the expression turned into a single variable, convert the deduction as such - if it's not negated, just set the variable itself
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 1) {
$new_deduction = new Equation($var, ($expr->terms[0]->vars[0]->negated ? $expr->terms[0]->vars[0] : $expr->terms[0]->vars[0]->var));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Bool Deduction as Var = Var/Bool " . $new_deduction->toString() . "\n";
return true;
}
// regular reduction of var = expr to var = expr
$new_deduction = new Equation($var, $expr);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Bool Deduction as Var = Expr " . $new_deduction->toString() . "\n";
return true;
}
/*
* reduce a single variable in a var = boolean variable type deduction from a new var = boolean variable type deduction
*/
protected function reduce_var_bool_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = bool deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right->copy());
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// if the variable appears negated, reduce the deduction with the negated value
if ($this->deductions[$deduction_to_reduce]->right->negate()->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = bool deduction " . $this->deductions[$from_deduction]->toString() . " (negated)\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right->negate());
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// if the variable does not appear, no new deductions
return false;
}
/*
* reduce a single variable in a zero product deduction from a new var = boolean variable deduction
*/
protected function reduce_zero_product_deduction_from_var_bool($from_deduction, $deduction_to_reduce) {
// if the term does not contain our variable, no new deductions can be found
if (!$this->deductions[$deduction_to_reduce]->left->has_variable($this->deductions[$from_deduction]->left)) return false;
// term does contain our variable - apply deduction
echo "Reducing zero product deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from " . $this->deductions[$from_deduction]->toString() . "\n";
// get the zero product and apply the new value in the term
$zero_product = $this->deductions[$deduction_to_reduce]->left->copy();
$zero_product->apply_var_replace($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right);
// get rid of the old deduction
$this->remove_deduction($deduction_to_reduce);
// if the term is has no variables and has a value, it must be a totality now - cannot be a contradiction - if it's a totality it will be pruned - otherwise error out
if (count($zero_product->vars) == 0) {
// if the deduction became a contradiction, error out
if ($zero_product->val == '1') throw new Exception('Unexpected zero product reduction contradiction');
// show that the deduction got reduced to totality
echo "Reduced deduction from zero product to totality\n";
return true;
}
// if the term is down to a single variable, convert the deduction to that form - if the variable is negated, it means it's one - otherwise it's zero
if (count($zero_product->vars) == 1) {
$new_deduction = new Equation($zero_product->vars[0]->var, ($zero_product->vars[0]->negated ? 1 : 0));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction from zero product to var = bool: " . $new_deduction->toString() . "\n";
return true;
}
// regular zero product deduction
$new_deduction = new Equation($zero_product, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Reduced deduction: " . $new_deduction->toString() . "\n";
return true;
}
/*
* reduce a single deduction from a new variable deduction that equals a binary expression
*/
protected function reduce_deduction_from_var_expr($from_deduction, $deduction_to_reduce) {
// deduction to reduce is a zero product type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Term')) return $this->reduce_zero_product_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// deduction is a variable = binary expression type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'BinaryExpression')) return $this->reduce_var_expr_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// deduction is a variable = boolean type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Boolean')) return $this->reduce_var_bool_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// deduction is a variable = another variable type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') &&
is_object($this->deductions[$deduction_to_reduce]->right) && is_a($this->deductions[$deduction_to_reduce]->right, 'Variable')) return $this->reduce_var_var_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// deduction is a variable = constant value type deduction
if (is_object($this->deductions[$deduction_to_reduce]->left) && is_a($this->deductions[$deduction_to_reduce]->left, 'Variable') && !is_object($this->deductions[$deduction_to_reduce]->right)) return $this->reduce_var_value_deduction_from_var_expr($from_deduction, $deduction_to_reduce);
// unknown deduction type
throw new Exception('Unknown deduction type: ' . $this->deductions[$deduction_to_reduce]->toString());
}
/*
* reduce a single variable in a var = expr type deduction from a new var = expr variable type deduction
*/
protected function reduce_var_expr_deduction_from_var_expr($from_deduction, $deduction_to_reduce) {
// get the variable and the expression
$var = $this->deductions[$deduction_to_reduce]->left->copy();
$expr = $this->deductions[$deduction_to_reduce]->right->copy();
// apply the variable replacement in the expression - if the expression has not changed, nothing to deduce
if (!$expr->apply_var_expr($this->deductions[$from_deduction]->left, $this->deductions[$from_deduction]->right)) return false;
// remove the old deduction
echo "Var = Expr Deduction reduced from Var = Expr Deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$this->remove_deduction($this->deductions[$deduction_to_reduce]);
// if the expression turned into a nothing, set the value as zero
if (count($expr->terms) == 0) {
$new_deduction = new Equation($var, 0);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Expr Deduction as Var = Zero " . $new_deduction->toString() . "\n";
exit;
return true;
}
// if the expression turned into a value, set the value as that
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 0) {
$new_deduction = new Equation($var, $expr->terms[0]->val);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Expr Deduction as Var = Value " . $this->deductions[$deduction_to_reduce]->toString() . "\n";
exit;
return true;
}
// if the expression turned into a single variable, convert the deduction as such - if it's not negated, just set the variable itself
if (count($expr->terms) == 1 && count($expr->terms[0]->vars) == 1) {
$new_deduction = new Equation($var, ($expr->terms[0]->vars[0]->negated ? $expr->terms[0]->vars[0] : $expr->terms[0]->vars[0]->var));
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Expr Deduction as Var = Var " . $new_deduction->toString() . "\n";
exit;
return true;
}
// regular reduction of var = expr to var = expr
$new_deduction = new Equation($var, $expr);
if (!$this->add_if_not_deduced($new_deduction)) return false;
echo "Var = Expr Deduction reduced from Var = Expr Deduction as Var = Expr " . $new_deduction->toString() . "\n";
exit;
return true;
}
/*
* reduce a single variable in a var = boolean variable type deduction from a new var = expr variable type deduction
*/
protected function reduce_var_bool_deduction_from_var_expr($from_deduction, $deduction_to_reduce) {
// if the variable is the same, reduce the deduction
if ($this->deductions[$deduction_to_reduce]->right->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = expr deduction " . $this->deductions[$from_deduction]->toString() . "\n";
$new_deduction = new Equation($this->deductions[$deduction_to_reduce]->left->copy(), $this->deductions[$from_deduction]->right->copy());
$this->remove_deduction($deduction_to_reduce);
if (!$this->add_if_not_deduced($new_deduction)) return false;
return true;
}
// if the variable appears negated, reduce the deduction with the negated value
if ($this->deductions[$deduction_to_reduce]->right->negate()->toString() == $this->deductions[$from_deduction]->left->toString()) {
echo "Reducing var = bool deduction " . $this->deductions[$deduction_to_reduce]->toString() . " from var = expr deduction " . $this->deductions[$from_deduction]->toString() . " (negated)\n";