|
| 1 | +Arithmetic normalization |
| 2 | +======================== |
| 3 | + |
| 4 | +The normal form of an arithmetic expression is less well defined. |
| 5 | + |
| 6 | +* there are no '-' operations, `A +Int -1 *Int B` is used instead of |
| 7 | + `A -Int B |
| 8 | +* Addition is grouped to the left, i.e., |
| 9 | + `((..((A +Int B) +Int C) +Int ...) +Int Z)` |
| 10 | +* Only the last element of an addition is concrete, i.e. `(A +Int B) +Int 7` |
| 11 | + is normal, `(A +Int 7) +Int 8` and `(A +Int 7) +Int C` are not. |
| 12 | +* Multiplication is grouped to the right, i.e., |
| 13 | + `(Z *Int (... *Int (C *Int (A *Int B)) ...))` |
| 14 | +* Only the fist element of multiplication is concrete, i.e., `7 *Int (A *Int B)` |
| 15 | + is normal, `8 *Int (7 *Int A)` and `C *Int (7*Int B)` are not. |
| 16 | +* Multiplication with constants is always distributed, i.e., |
| 17 | + `7 *Int A +Int 7 *Int B` is normal, `7 *Int (A +Int B)` is not. |
| 18 | +* (Not fully enforced): Constants multiplied with the same symbolic term, then |
| 19 | + added, are merged, i.e., `(7 *Int A +Int B) +Int 8 *Int A` is not normal and |
| 20 | + will be transformed to `15 *Int A +Int B` |
| 21 | +```k |
| 22 | +module WASM-INT-ARITHMETIC-NORMALIZATION |
| 23 | + imports INT |
| 24 | +
|
| 25 | + // The rules below complement the ones in INT-KORE |
| 26 | +
|
| 27 | +``` |
| 28 | +-Int elimination |
| 29 | +---------------- |
| 30 | +```k |
| 31 | + |
| 32 | + rule X -Int Y => X +Int (-1 *Int Y) [simplification] |
| 33 | +
|
| 34 | +``` |
| 35 | ++Int |
| 36 | +---- |
| 37 | + |
| 38 | +* associativity |
| 39 | +```k |
| 40 | + rule X +Int (Y +Int Z) => (X +Int Y) +Int Z [simplification] |
| 41 | +
|
| 42 | +``` |
| 43 | +* constants |
| 44 | +```k |
| 45 | + rule I +Int X => X +Int I [simplification, symbolic(X), concrete(I)] |
| 46 | + rule (X +Int I) +Int Y => (X +Int Y) +Int I [simplification, symbolic(X, Y), concrete(I)] |
| 47 | +``` |
| 48 | +*Int |
| 49 | +---- |
| 50 | + |
| 51 | +* associativity |
| 52 | +```k |
| 53 | + rule (X *Int Y) *Int Z => X *Int (Y *Int Z) [simplification] |
| 54 | +
|
| 55 | +``` |
| 56 | +* constants |
| 57 | +```k |
| 58 | + rule X *Int I => I *Int X [simplification, symbolic(X), concrete(I)] |
| 59 | + rule X *Int (I *Int Y) => I *Int (X *Int Y) [simplification, symbolic(X, Y), concrete(I)] |
| 60 | + rule I1 *Int (I2 *Int X) => (I1 *Int I2) *Int X [simplification, symbolic(X), concrete(I1, I2)] |
| 61 | +
|
| 62 | + rule 1 *Int X => X [simplification] |
| 63 | + rule 0 *Int _ => 0 [simplification] |
| 64 | +
|
| 65 | +``` |
| 66 | +Distributivity for *Int and +Int |
| 67 | +-------------------------------- |
| 68 | + |
| 69 | +* Multiplication with constants |
| 70 | +```k |
| 71 | + rule I *Int (X +Int Y) => I *Int X +Int I *Int Y |
| 72 | + [simplification, symbolic(X), concrete(I)] |
| 73 | + rule I *Int (X +Int Y) => I *Int X +Int I *Int Y |
| 74 | + [simplification, symbolic(Y), concrete(I)] |
| 75 | +``` |
| 76 | +* Reverse distributivity for different-constants * same-term. |
| 77 | +```k |
| 78 | + // It would be really nice if the backend would check the equality for the |
| 79 | + // two occurrences of X by matching. The next best option is to make these |
| 80 | + // rules low-priority, so they are applied only after the formula has already |
| 81 | + // stabilized (including normalization). |
| 82 | + rule X +Int X => 2 *Int X [simplification(200)] |
| 83 | + rule X +Int I *Int X => (1 +Int I) *Int X [simplification(200), concrete(I)] |
| 84 | + rule I *Int X +Int X => (1 +Int I) *Int X [simplification(200), concrete(I)] |
| 85 | + rule I1 *Int X +Int I2 *Int X => (I1 +Int I2) *Int X [simplification(200), concrete(I1, I2)] |
| 86 | +``` |
| 87 | +* Generalized reverse distributivity |
| 88 | +```k |
| 89 | + // The backends do not allow to do meta-manipulation of terms (e.g. to sort |
| 90 | + // them by some criteria), so this is an attempt to catch the most common |
| 91 | + // non-basic cases for reverse distributivity: |
| 92 | +
|
| 93 | + // Distance 1: |
| 94 | + rule (X +Int Y) +Int X => 2 *Int X +Int Y |
| 95 | + [simplification(200)] |
| 96 | + rule (X +Int Y) +Int I *Int X => (1 +Int I) *Int X +Int Y |
| 97 | + [simplification(200), concrete(I)] |
| 98 | + rule (I *Int X +Int Y) +Int X => (1 +Int I) *Int X +Int Y |
| 99 | + [simplification(200), concrete(I)] |
| 100 | + rule (I1 *Int X +Int Y) +Int I2 *Int X => (I1 +Int I2) *Int X +Int Y |
| 101 | + [simplification(200), symbolic(X), concrete(I1, I2)] |
| 102 | +
|
| 103 | + rule (Y +Int X) +Int X => Y +Int 2 *Int X |
| 104 | + [simplification(200)] |
| 105 | + rule (Y +Int X) +Int I *Int X => Y +Int (1 +Int I) *Int X |
| 106 | + [simplification(200), concrete(I)] |
| 107 | + rule (Y +Int I *Int X) +Int X => Y +Int (1 +Int I) *Int X |
| 108 | + [simplification(200), concrete(I)] |
| 109 | + rule (Y +Int I1 *Int X) +Int I2 *Int X => Y +Int (I1 +Int I2) *Int X |
| 110 | + [simplification(200), concrete(I1, I2)] |
| 111 | +
|
| 112 | + // Distance 2: |
| 113 | + rule ((X +Int Y) +Int Z) +Int X => (2 *Int X +Int Y) +Int Z |
| 114 | + [simplification(200)] |
| 115 | + rule ((X +Int Y) +Int Z) +Int I *Int X => ((1 +Int I) *Int X +Int Y) +Int Z |
| 116 | + [simplification(200), concrete(I)] |
| 117 | + rule ((I *Int X +Int Y) +Int Z) +Int X => ((1 +Int I) *Int X +Int Y) +Int Z |
| 118 | + [simplification(200), concrete(I)] |
| 119 | + rule ((I1 *Int X +Int Y) +Int Z) +Int I2 *Int X => ((I1 +Int I2) *Int X +Int Y) +Int Z |
| 120 | + [simplification(200), symbolic(X), concrete(I1, I2)] |
| 121 | +
|
| 122 | + rule ((Y +Int X) +Int Z) +Int X => (Y +Int 2 *Int X) +Int Z |
| 123 | + [simplification(200)] |
| 124 | + rule ((Y +Int X) +Int Z) +Int I *Int X => (Y +Int (1 +Int I) *Int X) +Int Z |
| 125 | + [simplification(200), concrete(I)] |
| 126 | + rule ((Y +Int I *Int X) +Int Z) +Int X => (Y +Int (1 +Int I) *Int X) +Int Z |
| 127 | + [simplification(200), concrete(I)] |
| 128 | + rule ((Y +Int I1 *Int X) +Int Z) +Int I2 *Int X => (Y +Int (I1 +Int I2) *Int X) +Int Z |
| 129 | + [simplification(200), concrete(I1, I2)] |
| 130 | +
|
| 131 | + // Distance 3: |
| 132 | + rule (((X +Int Y) +Int Z) +Int T) +Int X => ((2 *Int X +Int Y) +Int Z) +Int T |
| 133 | + [simplification(200)] |
| 134 | + rule (((X +Int Y) +Int Z) +Int T) +Int I *Int X => (((1 +Int I) *Int X +Int Y) +Int Z) +Int T |
| 135 | + [simplification(200), concrete(I)] |
| 136 | + rule (((I *Int X +Int Y) +Int Z) +Int T) +Int X => (((1 +Int I) *Int X +Int Y) +Int T) +Int Z |
| 137 | + [simplification(200), concrete(I)] |
| 138 | + rule (((I1 *Int X +Int Y) +Int Z) +Int T) +Int I2 *Int X => (((I1 +Int I2) *Int X +Int Y) +Int T) +Int Z |
| 139 | + [simplification(200), symbolic(X), concrete(I1, I2)] |
| 140 | +
|
| 141 | + rule (((Y +Int X) +Int Z) +Int T) +Int X => ((Y +Int 2 *Int X) +Int Z) +Int T |
| 142 | + [simplification(200)] |
| 143 | + rule (((Y +Int X) +Int Z) +Int T) +Int I *Int X => ((Y +Int (1 +Int I) *Int X) +Int Z) +Int T |
| 144 | + [simplification(200), concrete(I)] |
| 145 | + rule (((Y +Int I *Int X) +Int Z) +Int T) +Int X => ((Y +Int (1 +Int I) *Int X) +Int Z) +Int T |
| 146 | + [simplification(200), concrete(I)] |
| 147 | + rule (((Y +Int I1 *Int X) +Int Z) +Int T) +Int I2 *Int X => ((Y +Int (I1 +Int I2) *Int X) +Int Z) +Int T |
| 148 | + [simplification(200), concrete(I1, I2)] |
| 149 | +
|
| 150 | + // Distance 4: |
| 151 | + rule ((((X +Int Y) +Int Z) +Int T) +Int S) +Int X => (((2 *Int X +Int Y) +Int Z) +Int T) +Int S |
| 152 | + [simplification(200)] |
| 153 | + rule ((((X +Int Y) +Int Z) +Int T) +Int S) +Int I *Int X => ((((1 +Int I) *Int X +Int Y) +Int Z) +Int T) +Int S |
| 154 | + [simplification(200), concrete(I)] |
| 155 | + rule ((((I *Int X +Int Y) +Int Z) +Int T) +Int S) +Int X => ((((1 +Int I) *Int X +Int Y) +Int T) +Int S) +Int Z |
| 156 | + [simplification(200), concrete(I)] |
| 157 | + rule ((((I1 *Int X +Int Y) +Int Z) +Int T) +Int S) +Int I2 *Int X => ((((I1 +Int I2) *Int X +Int Y) +Int T) +Int S) +Int Z |
| 158 | + [simplification(200), symbolic(X), concrete(I1, I2)] |
| 159 | +
|
| 160 | + rule ((((Y +Int X) +Int Z) +Int T) +Int S) +Int X => (((Y +Int 2 *Int X) +Int Z) +Int S) +Int T |
| 161 | + [simplification(200)] |
| 162 | + rule ((((Y +Int X) +Int Z) +Int T) +Int S) +Int I *Int X => (((Y +Int (1 +Int I) *Int X) +Int Z) +Int T) +Int S |
| 163 | + [simplification(200), concrete(I)] |
| 164 | + rule ((((Y +Int I *Int X) +Int Z) +Int T) +Int S) +Int X => (((Y +Int (1 +Int I) *Int X) +Int Z) +Int T) +Int S |
| 165 | + [simplification(200), concrete(I)] |
| 166 | + rule ((((Y +Int I1 *Int X) +Int Z) +Int T) +Int S) +Int I2 *Int X => (((Y +Int (I1 +Int I2) *Int X) +Int Z) +Int T) +Int S |
| 167 | + [simplification(200), concrete(I1, I2)] |
| 168 | +endmodule |
| 169 | +
|
| 170 | +``` |
0 commit comments