On Tue, 30 Nov 2010, Edwin Woollett wrote:
< In your commutators.mac you start with
< defining a bilinear property (which I have copied
< into bilinear.mac).
<
< -------------------------------
< declare (bilinear, feature);
< bilinearp (e) := featurep (e, bilinear);
< declare (comm, bilinear);
<
< matchdeclare (xx, bilinearp);
< matchdeclare ([aa, bb, cc], all);
<
< defrule (rbilineara1, xx (aa + bb, cc), map (lambda ([u], xx (u, cc)), bb));
< defrule (rbilineara2, xx (cc, aa + bb), map (lambda ([u], xx (cc, u)), bb));
< -----------------------
< and with these rules in place we get:
< -----------------------
< (%i1) load(bilinear);
< (%o1) "c:/work5/bilinear.mac"
<
< (%i2) apply1(comm(a + b,c),rbilineara1);
< (%o2) comm(b,c)+comm(a,c)
<
< (%i3) apply1(comm(a ,b + c),rbilineara2);
< (%o3) comm(a,c)+comm(a,b)
<
< (%i4) map (lambda ([u], yy (u, c)), b);
< Improper argument to map: b
< -- an error. To debug this try: debugmode(true);
<
< (%i5) map (lambda ([u], yy (u, c)),a + b);
< (%o5) yy(b,c)+yy(a,c)
<
< (%i6) declare (zz,bilinear)$
<
< (%i7) map (lambda ([u], zz (u, c)), b);
< Improper argument to map: b
< -- an error. To debug this try: debugmode(true);
<
< (%i8) map (lambda ([u], zz (u, c)), a + b);
< (%o8) zz(b,c)+zz(a,c)
< -------------------------------------------------
< I don't understand why rule rbilineara1 definition in terms
< of the lambda function doesn't refer to aa as well as bb.
< How is your definition able to get the job done?
Ted, you don't want the rule to be applied to a mapatom.
(Trying to do that is causing your error messages.)
Robert has written a clever rule that prevents this from
happening by requiring there be a '+' op in the
slot 1 of xx or slot 2.
You can trace how the patterns are matched by doing
something like:
matchdeclare (aa, lambda([t],print("aa:",t),true));
matchdeclare (bb, lambda([t],print("bb:",t),true));
matchdeclare (cc, lambda([t],print("cc:",t),true));
You will see that bb matches greedily, and aa is
matched as a 0 on the end of the sum. This does not
occur when the expr in the slot has no '+' op.
I must admit to feeling like there is some black
magic going on here.
Leo
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.