Next: Conditional Rewrite Rules, Previous: Entering Rewrite Rules, Up: Rewrite Rules

#### 11.11.2 Basic Rewrite Rules

To match a particular formula ‘`x`’ with a particular rewrite rule ‘`old := new`’, Calc compares the structure of ‘`x`’ with the structure of old. Variables that appear in old are treated as meta-variables; the corresponding positions in ‘`x`’ may contain any sub-formulas. For example, the pattern ‘`f(x,y)`’ would match the expression ‘`f(12, a+1)`’ with the meta-variable ‘`x`’ corresponding to 12 and with ‘`y`’ corresponding to ‘`a+1`’. However, this pattern would not match ‘`f(12)`’ or ‘`g(12, a+1)`’, since there is no assignment of the meta-variables that will make the pattern match these expressions. Notice that if the pattern is a single meta-variable, it will match any expression.

If a given meta-variable appears more than once in old, the corresponding sub-formulas of ‘`x`’ must be identical. Thus the pattern ‘`f(x,x)`’ would match ‘`f(12, 12)`’ and ‘`f(a+1, a+1)`’ but not ‘`f(12, a+1)`’ or ‘`f(a+b, b+a)`’. (See Conditional Rewrite Rules, for a way to match the latter.)

Things other than variables must match exactly between the pattern and the target formula. To match a particular variable exactly, use the pseudo-function ‘`quote(v)`’ in the pattern. For example, the pattern ‘`x+quote(y)`’ matches ‘`x+y`’, ‘`2+y`’, or ‘`sin(a)+y`’.

The special variable names ‘`e`’, ‘`pi`’, ‘`i`’, ‘`phi`’, ‘`gamma`’, ‘`inf`’, ‘`uinf`’, and ‘`nan`’ always match literally. Thus the pattern ‘`sin(d + e + f)`’ acts exactly like ‘`sin(d + quote(e) + f)`’.

If the old pattern is found to match a given formula, that formula is replaced by new, where any occurrences in new of meta-variables from the pattern are replaced with the sub-formulas that they matched. Thus, applying the rule ‘`f(x,y) := g(y+x,x)`’ to ‘`f(12, a+1)`’ would produce ‘`g(a+13, 12)`’.

The normal `a r` command applies rewrite rules over and over throughout the target formula until no further changes are possible (up to a limit of 100 times). Use `C-u 1 a r` to make only one change at a time.