@@ -7,14 +7,12 @@ the argument to the predicate satisfies `istree` and `operation(x) == f`
77is_operation (f) = @nospecialize (x) -> istree (x) && (operation (x) == f)
88
99let
10- PLUS_RULES = [
10+ CANONICALIZE_PLUS = [
1111 @rule (~ x:: isnotflat (+ ) => flatten_term (+ , ~ x))
1212 @rule (~ x:: needs_sorting (+ ) => sort_args (+ , ~ x))
1313 @ordered_acrule (~ a:: is_literal_number + ~ b:: is_literal_number => ~ a + ~ b)
1414
1515 @acrule (* (~~ x) + * (~ β, ~~ x) => * (1 + ~ β, (~~ x). .. ))
16- @acrule (* (~ α, ~~ x) + * (~ β, ~~ x) => * (~ α + ~ β, (~~ x). .. ))
17- @acrule (* (~~ x, ~ α) + * (~~ x, ~ β) => * (~ α + ~ β, (~~ x). .. ))
1816
1917 @acrule (~ x + * (~ β, ~ x) => * (1 + ~ β, ~ x))
2018 @acrule (* (~ α:: is_literal_number , ~ x) + ~ x => * (~ α + 1 , ~ x))
2422 @rule (+ (~ x) => ~ x)
2523 ]
2624
27- TIMES_RULES = [
25+ PLUS_DISTRIBUTE = [
26+ @acrule (* (~ α, ~~ x) + * (~ β, ~~ x) => * (~ α + ~ β, (~~ x). .. ))
27+ @acrule (* (~~ x, ~ α) + * (~~ x, ~ β) => * (~ α + ~ β, (~~ x). .. ))
28+ ]
29+
30+ CANONICALIZE_TIMES = [
2831 @rule (~ x:: isnotflat (* ) => flatten_term (* , ~ x))
2932 @rule (~ x:: needs_sorting (* ) => sort_args (* , ~ x))
3033
3134 @ordered_acrule (~ a:: is_literal_number * ~ b:: is_literal_number => ~ a * ~ b)
3235 @rule (* (~~ x:: hasrepeats ) => * (merge_repeats (^ , ~~ x)... ))
3336
3437 @acrule ((~ y)^ (~ n) * ~ y => (~ y)^ (~ n+ 1 ))
35- @ordered_acrule ((~ x)^ (~ n) * (~ x)^ (~ m) => (~ x)^ (~ n + ~ m))
3638
3739 @ordered_acrule ((~ z:: _isone * ~ x) => ~ x)
3840 @ordered_acrule ((~ z:: _iszero * ~ x) => ~ z)
3941 @rule (* (~ x) => ~ x)
4042 ]
4143
44+ MUL_DISTRIBUTE = @ordered_acrule ((~ x)^ (~ n) * (~ x)^ (~ m) => (~ x)^ (~ n + ~ m))
4245
43- POW_RULES = [
46+
47+ CANONICALIZE_POW = [
4448 @rule (^ (* (~~ x), ~ y:: _isinteger ) => * (map (a-> pow (a, ~ y), ~~ x)... ))
4549 @rule ((((~ x)^ (~ p:: _isinteger ))^ (~ q:: _isinteger )) => (~ x)^ ((~ p)* (~ q)))
4650 @rule (^ (~ x, ~ z:: _iszero ) => 1 )
4751 @rule (^ (~ x, ~ z:: _isone ) => ~ x)
4852 @rule (inv (~ x) => 1 / (~ x))
53+ ]
54+
55+ POW_RULES = [
4956 @rule (^ (~ x:: _isone , ~ z) => 1 )
5057 ]
5158
@@ -120,12 +127,16 @@ let
120127
121128 function number_simplifier ()
122129 rule_tree = [If (istree, Chain (ASSORTED_RULES)),
123- If (is_operation (+ ),
124- Chain (PLUS_RULES)),
125- If (is_operation (* ),
126- Chain (TIMES_RULES)),
127- If (is_operation (^ ),
128- Chain (POW_RULES))] |> RestartedChain
130+ If (x -> ! isadd (x) && is_operation (+ )(x),
131+ Chain (CANONICALIZE_PLUS)),
132+ If (is_operation (+ ), Chain (PLUS_DISTRIBUTE)), # This would be useful even if isadd
133+ If (x -> ! ismul (x) && is_operation (* )(x),
134+ Chain (CANONICALIZE_TIMES)),
135+ If (is_operation (* ), MUL_DISTRIBUTE),
136+ If (x -> ! ispow (x) && is_operation (^ )(x),
137+ Chain (CANONICALIZE_POW)),
138+ If (is_operation (^ ), Chain (POW_RULES)),
139+ ] |> RestartedChain
129140
130141 rule_tree
131142 end
0 commit comments