Next: Unification Up: Prolog Terms Previous: Lists

## Operators

Operators in Prolog are simply a notational convenience. We could represent the English statement ``2 plus 3'' in Prolog as `+(2, 3)` but this isn't the way we are accustomed to seeing the expression. A more familiar notation involves making the `+` an INFIX operator so that we can write `2 + 3`. When an operator is defined it is also allocated two descriptive attributes:

• Its precedence. This is a number between 1 and 1200 which allows the Prolog system to decide the principal functor of any nested term. For example, the precedence of `+` is 500 while the precedence of `*` (the multiplication operator) is 400. The rule is that in a nested term the operator with highest precedence value is the principal functor. For instance, the term `2 + 3 * 5` has principal functor `+`. If we were to write it in standard Prolog syntax this term would look like this: `+(2,*(3,5))`. NOTE that terms representing numerical expressions are NOT automatically evaluated, like in a functional language. There is a special built--in predicate for doing this, which we shall meet later on.
• Its type. This defines whether the operator is:
• Prefix, in which case it is allocated type `fx` or `fy`.
• Postfix, in which case it is allocated type `xf` or `yf`.
• Infix, in which case it is allocated type `xfx`, `xfy` or `yfx`.
Some operators may have more than one type. For example the `+` operator is both prefix (e.g. `+ 2`) and infix (e.g. `2 + 3`).

You can check what the current operators in your Prolog system are by using the `current_op/3` built-in predicate. You can also define your own operators using the built--in `op/3` predicate but we won't worry about this yet. To display a term containing operators in standard Prolog syntax, use the built-in predicate `display/1`.

```| ?- current_op(Precedence, Type, +).

Precedence=500
Type=fx ;

Precedence=500
Type=yfx
yes
| ?- current_op(Precedence, Type, *).

Precedence=400
Type=yfx
yes
| ?- display(2 + 3 + 4).
+(+(2,3),4)
yes
```

Next: Unification Up: Prolog Terms Previous: Lists

Dave Stuart Robertson
Tue Jul 7 10:44:26 BST 1998