Operators
Date: 2020-10-07 18:10:00

Up | Prev | PrevTail | Tail

### 2.7 Operators

Operators in REDUCE are speciﬁed by name and type. There are two types, inﬁx and preﬁx. Operators can be purely abstract, just symbols with no properties; they can have values assigned (using := or simple LET declarations) for speciﬁc arguments; they can have properties declared for some collection of arguments (using more general LET declarations); or they can be fully deﬁned (usually by a procedure declaration).

Inﬁx operators have a deﬁnite precedence with respect to one another, and normally occur between their arguments. For example:

a + b - c | (spaces optional)
x<y and y=z | (spaces required where shown)

Spaces can be freely inserted between operators and variables or operators and operators. They are required only where operator names are spelled out with letters (such as the AND in the example) and must be unambiguously separated from another such or from a variable (like Y). Wherever one space can be used, so can any larger number.

Preﬁx operators occur to the left of their arguments, which are written as a list enclosed in parentheses and separated by commas, as with normal mathematical functions, e.g.,

cos(u)
df(x^2,x)
q(v+w)

Unmatched parentheses, incorrect groupings of inﬁx operators and the like, naturally lead to syntax errors. The parentheses can be omitted (replaced by a space following the operator name) if the operator is unary and the argument is a single symbol or begins with a preﬁx operator name:

cos y | means cos(y)
cos (-y) | – parentheses necessary
log cos y | means log(cos(y))
log cos (a+b) | means log(cos(a+b))

but

cos a*b | means (cos a)*b
cos -y | is erroneous (treated as a variable
| “cos” minus the variable y)

A unary preﬁx operator has a precedence higher than any inﬁx operator, including unary inﬁx operators. In other words, REDUCE will always interpret cos y + 3 as (cos y) + 3 rather than as cos(y + 3).

Inﬁx operators may also be used in a preﬁx format on input, e.g., +(a,b,c). On output, however, such expressions will always be printed in inﬁx form (i.e., a + b + c for this example).

A number of preﬁx operators are built into the system with predeﬁned properties. Users may also add new operators and deﬁne their rules for simpliﬁcation. The built in operators are described in another section.

#### Built-In Inﬁx Operators

The following inﬁx operators are built into the system. They are all deﬁned internally as procedures.

⟨inﬁx operator⟩ | ::= | where∣:=∣or∣and∣member∣memq∣
|  | =∣neq∣eq∣>=∣>∣<=∣<∣
|  | +∣-∣*∣/∣^∣**∣.

These operators may be further divided into the following subclasses:

⟨assignment operator⟩ | ::= | :=
⟨logical operator⟩ | ::= | or∣and∣member∣memq
⟨relational operator⟩ | ::= | =∣neq∣eq∣>=∣>∣<=∣<
⟨substitution operator⟩ | ::= | where
⟨arithmetic operator⟩ | ::= | +∣-∣*∣/∣^∣**
⟨construction operator⟩ | ::= | .

MEMQ and EQ are not used in the algebraic mode of REDUCE. They are explained in the section on symbolic mode. WHERE is described in the section on substitutions.

In previous versions of REDUCE, not was also deﬁned as an inﬁx operator. In the present version it is a regular preﬁx operator, and interchangeable with null.

For compatibility with the intermediate language used by REDUCE, each special character inﬁx operator has an alternative alphanumeric identiﬁer associated with it. These identiﬁers may be used interchangeably with the corresponding special character names on input. This correspondence is as follows:

:= | setq | (the assignment operator)
= | equal
>= | geq
> | greaterp
<= | leq
< | lessp
+ | plus
- | difference | (if unary, minus)
* | times
/ | quotient | (if unary, recip)
^ or ** | expt | (raising to a power)
. | cons

Note: NEQ is used to mean not equal. There is no special symbol provided for it.

The above operators are binary, except NOT which is unary and + and * which are nary (i.e., taking an arbitrary number of arguments). In addition, - and / may be used as unary operators, e.g., /2 means the same as 1/2. Any other operator is parsed as a binary operator using a left association rule. Thus a/b/c is interpreted as (a/b)/c. There are two exceptions to this rule: := and . are right associative. Example: a:=b:=c is interpreted as a:=(b:=c). Unlike ALGOL and PASCAL, ^ is left associative. In other words, a^b^c is interpreted as (a^b)^c.

The operators <, <=, >, >= can only be used for making comparisons between numbers. No meaning is currently assigned to this kind of comparison between general expressions.

Parentheses may be used to specify the order of combination. If parentheses are omitted then this order is by the ordering of the precedence list deﬁned by the right-hand side of the ⟨inﬁx operator⟩ table at the beginning of this section, from lowest to highest. In other words, WHERE has the lowest precedence, and . (the dot operator) the highest.

Up | Prev | PrevTail | Front