Download Prolog Programming A First Course

Transcript
115
Operator
:-->
,
\+
is
<
=
=<
>
>=
+
*
/
Precedence
1200
1200
1000
900
700
700
700
700
700
700
500
500
400
400
We still need to decide what to do with two operators of the same precedence.
Should we regard
3-2-1
as one or the other of:
-©©
3
-
©H
©HH
©©
H
HH
1
3
2
-
©H
HH©HH
©©
H
©©
2
1
and, remember, that we are not yet talking about arithmetic evaluation!
We can use brackets to distinguish
(3 - 2) -1
from
3 - (2 - 1)
but we have a special way of distinguishing which interpretation we wish
Prolog to make. In the above arithmetic example, the left hand tree has
two subtrees hanging from the root “-”. The left hand one has “-” as its root
while the right hand one is not so allowed. We say that this interpretation
of “-” is left associative.
The normal interpretation of “-” is left associative. The common left associative operators are:
*
/
+
-
div
1
Are there any right associative operators? Yes —consider how we are to
disambiguate
1
div/2 is integer division. It is a synonym for ///2 —read this as an infix operator of
arity 2 written //.