Information Technology Reference
In-Depth Information
?- age(donna,N).
N=23
Yes
?- age(X,N), \+ N=23.
X = andy,N=22
Yes
But what makes numbers especially useful is that Prolog provides arithmetic oper-
ations over them. Specifically, going back to figure 3.10, the notion of a literal (the
building block of queries and bodies of clauses) can be generalized to include the
following arithmetic relations , optionally preceded by the negation sign:
less than: E 1 < E 2
greater than: E 1 > E 2
less than or equal: E 1 =< E 2
greater than or equal: E 1 >= E 2
equal: E 1 =:= E 2
equal: Variable is E
The E i mentioned here are arithmetic expressions , that is, formulas built out of numbers
and variables using parentheses and the operations + (addition), - (subtraction), *
(multiplication), / (division), ** (exponentiation), and others. So here is an arithmetic
expression in Prolog:
(X-Y)**2 + 3*(X/Y)
Note that this is not a Prolog term, atom, or literal; the grammar is clearly all wrong.
Arithmetic expressions like these can only be used within Prolog programs and
queries in very specific contexts. In particular, they can only be used as an E in one
of the arithmetic relations just listed.
Here then are some legal Prolog queries that use arithmetic expressions and the
answers they produce:
?- X=3, (X+4) > 2*X.
X=3
Yes
?- X=3, (X+2) >= 2*X + 1.
No
 
Search WWH ::




Custom Search