Rules and Patterns



2009/4/1 Martin Sch?necker <ms_usenet at gmx.de>:

> /*Declare a rule that tests for a product of an atom and f.
> Return the factors as well as the pattern match results.*/

In the interest of brevity I'll just offer this.

(%i1) matchdeclare (xx, foop, yy, barp);
(%o1)                                done
(%i2) foop (e) := e = 'foo;
(%o2)                         foop(e) := e = 'foo
(%i3) barp (e) := atom (e) and not foop (e) and e # 1;
(%o3)            barp(e) := atom(e) and not foop(e) and e # 1
(%i4) defrule (r1, xx*yy, HEY_LOOK (xx, yy));
yy xx partitions `product'
(%o4)                   r1 : xx yy -> HEY_LOOK(xx, yy)
(%i5) r1 (2*foo);
(%o5)                          HEY_LOOK(foo, 2)
(%i6) r1 (x*foo);
(%o6)                          HEY_LOOK(foo, x)
(%i7) r1 (x*y*z*foo);
(%o7)                        HEY_LOOK(foo, x y z)
(%i8) r1 (x + foo);
(%o8)                                false
(%i9) r1 (sin (a * b * foo));
(%o9)                                false
(%i10) apply1 (sin (a * b * foo), r1);
(%o10)                      sin(HEY_LOOK(foo, a b))

There 's a lot to say about pattern matching but probably the
important points at the moment are these.
(1) pattern matcher imputes a factor = 1 in every "*" expression
(2) pattern matcher sweeps through arguments of "*" and
gathers up all arguments matching a pattern variable
(3) you should try to arrange for exclusive predicates;
if an argument matches two predicates it's just an accident
which is chosen

The reference manual documentation for matchdeclare and
tellsimpafter is dump of what I was able to figure out.

best

Robert Dodier