Replacement of Operators - MIT/GNU Scheme 9.0
Description: MIT/GNU Scheme 9.0

Next: Reduction of Operators, Previous: In-line Coding, Up: Declarations

#### 4.2.3 Operator Replacement

The `replace-operator` declaration is provided to inform the compiler that certain operators may be replaced by other operators depending on the number of arguments. For example:

Declaration:

         (declare (replace-operator (map (2 map-2) (3 map-3))))

Replacements:

         (map f x y z) ==> (map f x y z)
         (map f x y) ==> (map-3 f x y)
         (map f x) ==> (map-2 f x)
         (map f) ==> (map f)
         (map) ==> (map)

Presumably `map-2` and `map-3` are efficient versions of `map` that are written for exactly two and three arguments respectively. All the other cases are not expanded but are handled by the original, general `map` procedure, which is less efficient because it must handle a variable number of arguments.

— declaration: replace-operator name ...

> The syntax of this declaration is

>               (replace-operator
>                 (name
>                   (nargs1 value1)
>                   (nargs2 value2)
>                   ...))

> where

> - name is a symbol.
> - nargs1, nargs2 etc. are non-negative integers, or one of the following symbols: `any`, `else` or `otherwise`.
> - value1, value2 etc. are simple expressions in one of these forms:
>   `'`constant
>     A constant.
>   variable
>     A variable.
>   `(primitive` primitive-name [arity]`)`
>     The primitive procedure named primitive-name. The optional element arity, a non-negative integer, specifies the number of arguments that the primitive accepts.
>   `(global` var`)`
>     A global variable.

> The meanings of these fields are:

> - name is the name of the operator to be reduced. If is is not shadowed (for example, by a let) then it may be replaced according to the following rules.
> - If the operator has nargsN arguments then it is replaced with a call to valueN with the same arguments.
> - If the number of arguments is not listed, and one of the nargsN is `any`, `else` or `otherwise`, then the operation is replaced with a call to the corresponding valueN. Only one of the nargsN may be of this form.
> - If the number of arguments is not listed and none of the nargsN is `any`, `else` or `otherwise`, then the operation is not replaced.