Comparison | Definition | Evaluates? |
X = Y | succeeds if X and Y unify (match) in the Prolog sense | No |
X = Y | succeeds if X and Y do not unify; i.e. if not (X = Y) | No |
T1 == T2 | succeeds if terms T1 and T2 are identical; e.g. names of variables have to be the same | No |
T1 == T2 | succeeds if terms T1 and T2 are not identical | No |
E1 =:= E2 | succeeds if values of expressions E1 and E2 are equal | Yes |
E1 == E2 | succeeds if values of expressions E1 and E2 are not equal | Yes |
E1 < E2 | succeeds if numeric value of expression E1 is < numeric value of E2 | Yes |
E1 =< E2 | succeeds if numeric value of expression E1 is ≤ numeric value of E2 | Yes |
E1 > E2 | succeeds if numeric value of expression E1 is > numeric value of E2 | Yes |
E1 >= E2 | succeeds if numeric value of expression E1 is ≤ numeric value of E2 | Yes |
T1 @< T2 | succeeds if T1 is alphabetically < T2 | No |
T1 @=< T2 | succeeds if T1 is alphabetically ≤ T2 | No |
T1 @> T2 | succeeds if T1 is alphabetically > T2 | No |
T1 @>= T2 | succeeds if T1 is alphabetically ≥ T2 | No |
See also is
. is
is not a comparison operator, but is frequently confused with = by novice Prolog programmers. Briefly, you use X is Exp
to evaluate an arithmetic expression, like Y + 2
, that contains an arithmetic operator, like +
, and bind the resulting value to the variableX
to the left of the the operatoris
.
As an example of @<
and its relatives,
?- likes(mary, pizza) @< likes(mary, plums). true.This succeeds because
likes
and mary
are the same in both terms, and pizza
alphabetically precedes plums
.
Comparison of fractional numbers: When comparing two fractional numbers, problems can arise from the approximate nature of representations of fractional numbers in computers. Sometimes it will work as expected, and sometimes not. For example, the query 1.21 =:= 1.1 * 1.1.
fails with SWI Prolog on the computer where this article was written. You can and should work around this problem by, instead of testing fractional numbers for equality, doing something like the following:
?- abs(1.21 - 1.1 * 1.1) < 0.000001. true.
abs
signifies "absolute value": abs(X) = X
if X >= 0
and abs(X) = -X
if X =< 0
.abs(X - Y) < Tiny
, where Tiny
is bound to small number (or is a small number, as in the example). How small to make Tiny
above depends on the particular case - you might need to use a smaller number if X
and Y
need to be very close together to make your algorithm work correctly.
consult
?- consult('myprogram.pl').This loads the contents of the Prolog program in the file
myprogram.pl
into the running Prolog's database. Note that in SWI Prolog, at least, before loading the new facts and rules into the database, Prolog first removes all facts and rules that relate to procedures in myprogram.pl
. So if myprogram.pl
contains, for example, the fact likes(jane, pizza)
then all facts and rules about likes
that have two arguments will be removed from the Prolog database before the new facts in myprogram.pl
are loaded up. This is convenient when you are using consult
to re-load a program after editing it (e.g. in another window, with the Prolog interpreter left running), but could be a little surprising if you were trying to load extra facts from a file.
It is possible to consult more than one file at a time, by replacing the single file name with a list of files:
?- consult(['file1.pl', 'file2.pl', 'file3.pl']). % file1.pl compiled 0.00 sec, 524 bytes % file2.pl compiled 0.01 sec, 528 bytes % file3.pl compiled 0.00 sec, 524 bytes true.It is also possible to abbreviate a consult call, simply typing the list of (one or more) files as a goal for Prolog:
?- ['file1.pl', 'file2.pl', 'file3.pl'].In some Prolog implementations, consulting
user
causes the Prolog interpreter to read facts and rules from the user's terminal:?- [user]. |: likes(jane, pizza). |: bad_dog(Dog) :- |: bites(Dog, Person), |: is_human(Person), |: is_dog(Dog). |: <control-D> % user://1 compiled 0.00 sec, 1,156 bytes true. ?-