Non-exclusive pattern matching? e.g. distributiveness of a function, was: Predictable pattern matching?



On 12/23/08, Martin Sch?necker <ms_usenet at gmx.de> wrote:

>  (1) int[a_ + b_, x] := int[a, x] + int[b, x]
>  (2) int[c_ f_, x] := c int[f, x] /; FreeQ[c, x]
>  (3) int[x^n_, x] := 1/(n + 1) * x^(n + 1) /; n != -1
>  (4) int[c_, x] := c x /; FreeQ[c, x]

matchdeclare (aa, all, xx, symbolp);
defrule (r1, foo (aa, xx), block ([xx% : xx], apply1 (foo (aa, xx),
r2, r4, r5)));
matchdeclare (cc, freeof (xx), yy, lambda ([e], not freeof (xx, e)));
defrule (r2, foo (cc * yy, xx%), cc * foo (yy, xx));
matchdeclare (zz, lambda ([e], e = 0), nz, lambda ([e], e # 0));
defrule (r3, foo (zz + nz, xx), map (lambda ([e], foo (e, xx)), nz));
matchdeclare (nn, integerp);
defrule (r4, foo (xx%^nn, xx%), if nn = -1 then log(xx%) else xx^(nn +
1)/(nn + 1));
defrule (r5, foo (cc, xx%), cc * xx);

apply_foo_rules (e) := apply1 (e, r3, r1);

Examples:

apply_foo_rules (foo (1, u))
 => u
apply_foo_rules (foo (u + 1, u))
 => u^2/2+u
apply_foo_rules (foo (10 * u, u))
 => 5*u^2
apply_foo_rules (foo (10 * %pi * u^2 * sin(u), u))
 => 10*%pi*foo(u^2*sin(u),u)
apply_foo_rules (foo (1/u - %pi * u^3 * v^3, u))
 => log(u)-%pi*u^4*v^3/4
apply_foo_rules (foo (- %pi - 10 * cos(u) + sin(u), u))
 => foo(sin(u),u)-10*foo(cos(u),u)-%pi*u

Note that r1 captures the variable of integration for the benefit of
other rules. That's clumsy; it would be better to make the rule
processing smarter.

Could have written these as tellsimp or tellsimpafter rules so that
they would be applied automatically.

The numbers r1, r2, ... don't correspond to your (1), (2), ....

Hope this helps,

Robert