sums in defrule (partitions sum)



Hello,

If you wonder what I am trying to do, it is not
more or less than just understanding how the
rules in maxima work.  I tried maxima for deriving
some expression for my work in sound synthesis and
it was really usefull, so I decided to learn more about
it.

I just tried how to write a rule for rewriting expressions
and I picked an arbitrary trigonometric identity.
So there is no bigger goal, behind that.

I just want to apply this trigonometric identity:
        sin(a+b)*sin(a-b) = cos^2(b) - cos^2(a)

My try so far led to this (which is probably a totally wrong approach
  but how would I do it right?):

----------------------------------------------------------------
/* convert a sum to a list vice verse (quick and dirty) */
:lisp (defmfun $sumtolist (a) (cons '(mlist) (cdr a)))
:lisp (defmfun $listtosum (a) (cons '(mplus) (cdr a)))

findAandB(x,y, unchanged):=
  if (length(x) = length(y))
  then
     block([a,b],
       catch (
         a:[],  b:[],
         x:sumtolist(x),
         y:sumtolist(y),
         for elt in x do (
           if member(elt, y)
           then (a:cons(elt, a), y:delete(elt, y))
           else (if member(-elt, y)
                 then (b:cons(elt, b), y:delete(-elt, y))
                 else throw (unchanged))
               ),
         if (a = [] or b = [])
         then unchanged
         else cos(listtosum(a))^2 - cos(listtosum(b))^2))
  else unchanged$

sum2ormore(x):=is (not(atom(x)) and  inpart(x,0)= "+" and 2 <= length(x))$
matchdeclare([A,B], sum2ormore)$
matchdeclare(C, true)$
defrule(myr1, -sin(A)*sin(B)*C, findAandB(A,B,-sin(A)*sin(B)) * C);


/* now test it */

apply1(sin(a+b)*sin(a-b), myr1);

          2         2
    => cos (b) - cos (a)


apply1(a*sin(a+b+3)*sin(a+3-b), myr1);

             2         2
    => a (cos (b) - cos (a+3))


apply1(sin(a+b-c)*sin(a+b), myr1);

    => - sin(b + a) sin(c - b - a)



all those results are as intended.

thanks,
Anton