酷兔英语

章节正文
文章总共2页
comparison operators
(Only some of the operators below are relevant to COMP9414 at University of New South Wales - see green colouring below.) Prolog has two main classes of comparison operators - arithmeticcomparison operators (and similar alphabetic comparison operators) and unification-style operators:

 

 

ComparisonDefinitionEvaluates?
X = Ysucceeds if X and Y unify (match) in the Prolog senseNo
X = Ysucceeds if X and Y do not unify; i.e. if not (X = Y)No
T1 == T2succeeds if terms T1 and T2 are identical; e.g. names of variables have to be the sameNo
T1 == T2succeeds if terms T1 and T2 are not identicalNo
E1 =:= E2succeeds if values of expressions E1 and E2 are equalYes
E1 == E2succeeds if values of expressions E1 and E2 are not equalYes
E1 < E2succeeds if numeric value of expression E1 is < numeric value of E2Yes
E1 =< E2succeeds if numeric value of expression E1 is ≤ numeric value of E2Yes
E1 > E2succeeds if numeric value of expression E1 is > numeric value of E2Yes
E1 >= E2succeeds if numeric value of expression E1 is ≤ numeric value of E2Yes
T1 @< T2succeeds if T1 is alphabetically < T2No
T1 @=< T2succeeds if T1 is alphabetically ≤ T2No
T1 @> T2succeeds if T1 is alphabetically > T2No
T1 @>= T2succeeds if T1 is alphabetically ≥ T2No

 

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.
More generally, test for 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
These pre-defined pseudo-predicates allow one to load Prolog code into a running Prolog interpreter. Example:
?- 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.
?- 

 

 

current input stream
At any given time, input to Prolog comes from the "current input stream". When Prolog starts up, the current input stream is the keyboard of the computer/workstation from which Prolog was started.

 


文章总共2页
文章标签:词典  

章节正文