Previous: Replacement of Operators, Up: Declarations


4.2.4 Operator Reduction

The reduce-operator declaration is provided to inform the compiler that certain names are n-ary versions of binary operators. Here are some examples:

Declaration:

     (declare (reduce-operator (cons* cons)))

Replacements:

     (cons* x y z w) ==> (cons x (cons y (cons z w))),
     (cons* x y) ==> (cons x y)
     (cons* x) ==> x
     (cons*) error--> too few arguments

Declaration:

     (declare (reduce-operator (list cons (null-value '() any))))

Replacements:

     (list x y z w) ==> (cons x (cons y (cons z (cons w '()))))
     (list x y) ==> (cons x (cons y '()))
     (list x) ==> (cons x '())
     (list) ==> '()

Declaration:

     (declare (reduce-operator (- %- (null-value 0 single) (group left))))

Replacements:

     (- x y z w) ==> (%- (%- (%- x y) z) w)
     (- x y) ==> (%- x y)
     (- x) ==> (%- 0 x)
     (-) ==> 0

Declaration:

     (declare (reduce-operator (+ %+ (null-value 0 none) (group right))))

Replacements:

     (+ x y z w) ==> (%+ x (%+ y (%+ z w)))
     (+ x y) ==> (%+ x y)
     (+ x) ==> x
     (+) ==> 0

Note: This declaration does not cause an appropriate definition of %+ (in the last example) to appear in your code. It merely informs the compiler that certain optimizations can be performed on calls to + by replacing them with calls to %+. You should provide a definition of %+ as well, although it is not required.

Declaration:

     (declare (reduce-operator (apply (primitive cons)
                                      (group right)
                                      (wrapper (global apply) 1))))

Replacements:

     (apply f x y z w)
        ==> ((access apply #f) f (cons x (cons y (cons z w))))
     (apply f x y)
        ==> ((access apply #f) f (cons x y))
     (apply f x) ==> (apply f x)
     (apply f) ==> (apply f)
     (apply) ==> (apply)
— declaration: reduce-operator name ...

The general format of the declaration is (brackets denote optional elements):

          (reduce-operator
            (name
              binop
              [(group ordering)]
              [(null-value value null-option)]
              [(singleton unop)]
              [(wrapper wrap [n])]
              [(maximum m)]
            ))
     

where

The meaning of these fields is: