## `operator` -- define a new operator symbol

#### Introduction

`operator``(symb, f, T, prio)` defines a new operator symbol `symb` of type `T` with priority `prio`. The function `f` evaluates expressions using the new operator.

`operator``(symb, Delete)` removes the definition of the operator symbol `symb`.

#### Call(s)

`operator(symb, f <, T, prio>)`
`operator(symb, Delete)`

#### Parameters

`symb` | - | the operator symbol: a character string.
`f` | - | the function evaluating expressions using the operator.
`T` | - | the type of the operator: one of the options Prefix, Postfix, Binary or Nary. The default is Nary.
`prio` | - | the priority of the operator: an integer between 1 and 1999. The default is 1300.

#### Options

`Prefix` | - | the operator is a unary operator with prefix notation
`Postfix` | - | the operator is a unary operator with postfix notation
`Binary` | - | the operator is a non-associative binary operator with infix notation
`Nary` | - | the operator is an associative binary operator with infix notation
`Delete` | - | the operator with symbol `symb` is deleted

#### Returns

the void object of type `DOM_NULL`.

#### Side Effects

The new operator symbol `symb` is known by the parser and may be used to enter expressions. The new operator symbol will not be used when reading files using the function `read` with the option Plain.

#### Details

- `operator` is used to define new user-defined operator symbols or to delete them.
- Given the operator symbol `"++"`, say, with evaluating function `f`, the following expressions are built by the parser, depending on the type of the operator:
  Prefix:
    The input `++x` results in `f(x)`.
  Postfix:
    The input `x++` results in `f(x)`.
  Binary:
    The input `x ++ y ++ z` results in `f(f(x, y), z)`.
  Nary:
    The input `x ++ y ++ z` results in `f(x, y, z))`.
- There may exist operator symbols which are prefixes of other operator symbols. The scanner reads as many characters as possible and chooses the longest matching operator symbol. Cf. example 3.
- It is not possible to define two operators with the same symbol. So one may not define a unary `++` and a binary `++` at the same time.
- The following restrictions exist for the operator symbol string `symb`:
  - It may not be longer than 32 characters.
  - It may not start with a white-space.
  - It may not start with a `\` (backslash) character.
  Thus, the strings `" @"` and `"\\/"` are not allowed. Please note that currently `operator` does not check these restrictions.
- Builtin operators may be redefined.
- It is not possible to define out-fix operators like |x| or 3-nary or other types of operators.
- The new operator symbol is also used if files are read, with one exception: if a file is read with the function `read` using the option Plain, the new operator is not taken into account. (This option is used if MuPAD library files are read, because otherwise user-defined operators could change the meaning of the source code in an uncontrolled way.)
- Currently, there is no comfortable way to configure the output of expressions containing user-defined operators. (One may use the function `builtin` to define the text output of expressions. This, however, is not recommended.)
- See the MuPAD 2.0 quick reference for the precedence of the builtin operators.

#### Option: Prefix

- The operator is regarded as a unary operator with prefix notation. Given the operator symbol `"++"` and the evaluation function `f`, the input `++x` is parsed as the expression `f(x)`.

#### Option: Postfix

- The operator is regarded as a unary operator with postfix notation. Given the operator symbol `"++"` and the evaluation function `f`, the input `x++` is parsed as the expression `f(x)`.

#### Option: Binary

- The operator is regarded as a non-associative binary operator with infix notation. Given the operator symbol `"++"` and the evaluation function `f`, the input `x ++ y ++ z` is parsed as the expression `f(f(x, y), z)`, i.e. the operator binds left-to-right.

#### Option: Nary

- The operator is regarded as an associative n-ary operator with infix notation. Given the operator symbol `"++"` and the evaluation function `f`, the input `x ++ y ++ z` is parsed as the expression `f(x, y, z)`.

#### Example 1

This example shows how to define an operator symbol for the logical equivalence:

    >> equiv := (a, b) -> (a and b) or (not a and not b):
       operator("<=>", equiv, Binary, 50):

After this call, the symbol `<=>` can be used to enter expressions:

    >> a <=> FALSE, bool(1 < 0 <=> 1 > 0)

                                   not a, FALSE

    >> operator("<=>", Delete):

#### Example 2

Identifiers may be used as operator symbols:

    >> operator("x", _vector_product, Binary, 1000):

    >> a x b x c

                     _vector_product(_vector_product(a, b), c)

    >> operator("x", Delete):

#### Example 3

This example shows that the scanner tries to match the longest operator symbol:

    >> operator("~", F, Prefix, 1000):
       operator("~>", F1, Prefix, 1000):
       operator("~~>", F2, Prefix, 1000):

    >> ~~ x, ~~> x, ~ ~> x, ~~~> x

                        F(F(x)), F2(x), F(F1(x)), F(F2(x))

    >> operator("~", Delete):
       operator("~>", Delete):
       operator("~~>", Delete):

#### Background

- When the scanner reads a new token, it first discards any whitespace and backslash characters. Then it tries to match user-defined operator symbols. The longest user-defined operator symbol matching the scanned characters is made the next token. If no user-defined operator symbol matches, it scans for the built-in tokens.
- The parser uses both recursive-descend and a operator precedence parsing. Built-in and user-defined operators are parsed using operator precedence.

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000