semantic-math / math-rules

Manipulate math-ast ASTs based on algebraic rules
MIT License
4 stars 1 forks source link

PRODUCT_RULE should handle factors that don't have the same base #16

Closed kevinbarabash closed 7 years ago

kevinbarabash commented 7 years ago

e.g. the follow should work:

5 * 10^2 * 10^5 * 10^3 --> 5 * 10^(2 + 5 + 3)

The rule is defined as:

#a^#b_0 * ... --> #a^(#b_0 + ...)

The problem is that we try to match so called variable length patterns against all args to a mul node. Really we should be trying to match against any sub range of the args that is two or more args.

tkosan commented 7 years ago

@kevinbarabash this topic brings us back to our discussion about whether it is better (for the purpose of step-by-step equation solving) to represent expressions as binary trees or as more complex data structures. Since we last discussed this topic, I have researched it heavily and just recently arrived at some conclusions about it. It turns out that this is a standard topic in the logic-based AI research, and this research indicates that there are significant advantages to using representations that use binary relations over those that use more complex relations.

After you have gained some experience with MathPiper we can discuss this AI research in more depth. Until then, this is what I came up with for accomplishing the above transformation with rules that use standard pattern matching:

%mathpiper

exp := '(5 * (10^2 * 10^5 * 10^3));

exp /::
[
    a_^b_ * a_^c_ <- ` ' (@a^(@b + @c)),
];

%/mathpiper

    %output,mpversion=".231",preserve="false"
      Result: 5*10^((2 + 5) + 3)
.   %/output
kevinbarabash commented 7 years ago

@tkosan binary trees can make some things easier. Being able to apply a binary transform like the one described above is definitely easier to both describe and implement. In this case the binary tree that's constructed will work with the strategy of repeatedly apply the transform:

(5 * (10^2 * (10^5 * 10^3)))

If the 5 was at the end of the expression, the resulting tree would be:

(10^2 * (10^5 * (10^3 * 5))

And then the rule couldn't be apply afaict. I'm curious how systems using binary trees deal with the variety of ways in which equivalent expressions can be defined by different binary trees.

One of requirements I have for this work which I should write down https://semantic-math.github.io/ is that I'd like it to be useful for grade school math and I the predominant form of instruction at these levels does not make use of binary trees. Your average high school teacher would show:

5 * 10^2 * 10^3 * 10^3 --> 5 * 10^(2 + 3 + 5)

without any additional parentheses.

tkosan commented 7 years ago

@kevinbarabash

If the 5 was at the end of the expression, the resulting tree would be: (10^2 * (10^5 * (10^3 * 5)) And then the rule couldn't be apply afaict. I'm curious how systems using binary trees deal with the variety of ways in which equivalent expressions can be defined by different binary trees.

Some additional rules that use the commutative and associative laws can be used to transform the tree into the form that the last rule needs (these rules are not perfect, but they should be adequate to illustrate the point):

%mathpiper

exp := '(10^2 * (10^5 * (10^3 * 5)));

exp /::
[
    a_ * b_Integer? :: !? Integer?(a) <- ` '(@b * @a),
    (a_Integer? * b_) * c_ :: !? Integer?(b) <- ` '(@a * (@b * @c) ),
    a_ * (b_Integer? * c_) <- ` '((@a * @b) * @c),
    a_^b_ * a_^c_ <- ` ' (@a^(@b + @c)),
];

%/mathpiper

    %output,mpversion=".231",preserve="false"
      Result: 5*10^(2 + (5 + 3))
.   %/output

One of the requirements I have for this work which I should write down https://semantic-math.github.io/ is that I'd like it to be useful for grade school math and I the predominant form of instruction at these levels does not make use of binary trees. Your average high school teacher would show: 5 * 10^2 * 10^3 * 10^3 --> 5 * 10^(2 + 3 + 5) without any additional parentheses.

The steps produced by rules that use the commutative and associative laws don't need to be shown to the user. However, they can be shown if desired. A binary tree representation combined with carefully chosen rewrite rules should enable the full range of possible detail levels to be shown from highly abbreviated to highly detailed.

A step-by-step math program needs go beyond the limitations of current math teachers. It is important to show students these more detailed steps at some point because otherwise they will not understand how math works well enough to be successful in more advanced math classes :-)