We will now look at the structure of some ** Prolog** expressions:

We assume that it is always possible to represent ahappy(jim):-healthy(jim),

wealthy(jim).

which corresponds to ** happy(jim):- (healthy(jim),wealthy(jim))**
or

which corresponds to ** (happy(jim):- healthy(jim)),wealthy(jim)**.
We can see that the first version is the one we have taken for granted.
We describe this situation by saying that ** ,/2** binds * tighter*
than ** :-/2**.

This relates to the way we are taught to calculate arithmetical expressions in that we are told that we do multiplication before addition (unless brackets are used to override this). But there is another way to think of things: how to construct the expression tree. In this case, we choose the root to be the operator that is `loosest' (in opposition to `tightest' for computational purposes).

The issue is decided by
* operator precedence*.

To construct a tree which describes a ** Prolog** expression
we first look for the operator with the highest precedence
(this is in some sense the opposite of the way we compute a function).
If this operator is an infix one, we can divide the expression into
a left hand one and a right hand one.
The process is then repeated, generating left and right subtrees.

We still need to decide what to do with two operators of the same precedence. Should we regard

as one or the other of:3 - 2 - 1

and, remember, that we are not yet talking about arithmetic evaluation!

We can use brackets to distinguish

from(3 - 2) -1

but we have a special way of distinguishing which interpretation we wish3 - (2 - 1)

The normal interpretation of ``-'' is left associative. The common left associative operators are:

Are there any
* right associative*
operators?
Yes ---consider how we are to disambiguate

where ``a'', ``b'' and ``c'' are all legala,b,c

The answer is that ** ,/2** is right associative. Usually,
we do not have to concern ourselves with the details of this.

In all the previous cases we have allowed exactly one subtree to have, as its root, the same operator as the ``principal'' root. We can extend this to permit operators of the same precedence. Thus, since ``+'' and ``-'' have the same precedence, we know that both operators in

are left associative (and legal) and therefore the expression represents3 - 2 + 1

(3 - 2) +1.

Sometimes, we do not wish to permit left or right associativity. For example, obvious interpretations of:

do not readily spring to mind. Therefore we make it possible to forbid the building of expressions of this sort.a:- b :- cY is Z+1 is 3

a

`-->`

b`-->`

c

Mon May 24 20:14:48 BST 1999