| - Up - | Next >> |
The arithmetic predicates introduced earlier are a good example of this. As was mentioned in Chapter 5, /, -, *, and \ are functors, and arithmetic expressions such as 2+3 are terms. And this is not an analogy. Apart from the fact that we can evaluate them with the help of is, for Prolog strings of symbols such as 2+3 really are identical with ordinary complex terms:
?- 2+3 == +(2,3).
yes
?- +(2,3) == 2+3.
yes
?- 2-3 == -(2,3).
yes
?- *(2,3) == 2*3.
yes
?- 2*(7+2) == *(2,+(7,2)).
yesIn short, the familiar arithmetic notation is there for our convenience. Prolog doesn't regard it as different from the usual term notation.
Similar remarks to the arithmetic comparison predicates <, =<, =:=, =\=, > and >=:
?- (2 < 3) == <(2,3).
yes
?- (2 =< 3) == =<(2,3).
yes
?- (2 =:= 3) == =:=(2,3).
yes
?- (2 =\= 3) == =\=(2,3).
yes
?- (2 > 3) == >(2,3).
yes
?- (2 >= 3) == >=(2,3).
yes Two remarks. First these example show why it's nice to have the user friendly notation (would you want to have to work with expressions like =:=(2,3)?). Second, note that we enclosed the left hand argument in brackets. For example, we didn't ask
2 =:= 3 == =:=(2,3).we asked
(2 =:= 3) == =:=(2,3). Why? Well, Prolog finds the query 2 =:= 3 == =:=(2,3) confusing (and can you blame it?). It's not sure whether to bracket the expressions as (2 =:= 3) == =:=(2,3) (which is what we want), or 2 =:= (3 == =:=(2,3)). So we need to indicate the grouping explicitly.
One final remark. We have now introduced three rather similar looking symbols, namely =, ==, and =:= (and indeed, there's also \=, \==, and =\=). Here's a summary:
| The unification predicate. |
| Succeeds if it can unify its arguments, fails otherwise. |
| The negation of the unification predicate. |
| Succeeds if |
| The identity predicate. |
| Succeeds if its arguments are identical, fails otherwise. |
| The negation of the identity predicate. |
| Succeeds if |
| The arithmetic equality predicate. |
| Succeeds if its arguments evaluate to the same integer. |
| The arithmetic inequality predicate. |
| Succeeds if its arguments evaluate to different integers. |
| - Up - | Next >> |