酷兔英语

章节正文
文章总共2页
neck
the symbol:-, used in a Prolog rule to separate the head from the body. Usually read as if. Thus

 

a :- b, c.

is read as

a (is true) ifbandc (are true).

 

negation, not, +
The concept of logical negation in Prolog is problematical, in the sense that the only method that Prolog can use to tell if a proposition is false is to try to prove it (from the facts and rules that it has been told about), and then if this attempt fails, it concludes that the proposition is false. This is referred to as negation as failure. An obvious problem is that Prolog may not have been told some critical fact or rule, so that it will not be able to prove the proposition. In such a case, the falsity of the proposition is only relative to the "mini-world-model" defined by the facts and rules known to the Prolog interpreter. This is sometimes referred to as the closed-world assumption.

 

A less obvious problem is that, depending again on the rules and facts known to the Prolog interpreter, it may take a very long time to determine that the proposition cannot be proven. In certain cases, it might "take" infinite time.

Because of the problems of negation-as-failure, negation in Prolog is represented in modern Prolog interpreters using the symbol+, which is supposed to be a mnemonic for not provable with the standing for not and the + for provable. In practice, current Prolog interpreters tend to support the older operatornot as well, as it is present in lots of older Prolog code, which would break if not were not available.

Examples:

?- + (2 = 4).
true.
?- not(2 = 4).
true.

Arithmetic comparison operators in Prolog each come equipped with a negation which does not have a "negation as failure" problem, because it is always possible to determine, for example, if two numbers are equal, though there may be approximation issues if the comparison is between fractional (floating-point) numbers. So it is probably best to use the arithmeticcomparison operators if numeric quantities are being compared. Thus, a better way to do the comparisons shown above would be:

?- 2 == 4.
true.

 

number, number(–)
Numbers in Prolog can be whole numbers, like:
1  1313  0  -97  9311

or fractional numbers, like:
3.14  -0.0035  100.2
The range of numbers that can be used is likely to be dependent on the number of bits (amount of computer memory) used to represent the number. The treatment of real numbers in Prolog is likely to vary from implementation to implementation - real numbers are not all that heavily used in Prolog programs, as the emphasis in Prolog is on symbol manipulation.

 

number is also the name of a built-in predicate which tests its single argument and succeeds if that argument is a number. Examples:

?- number(2.1).
true.
?- number(-8).
true.
?- X = 7, number(X).
X = 7.
?- number(1+1).
false.
The last query fails because 1+1 is an unevaluated expression, not a number. See is, for more on evaluation.

 

 

once
The built-in Prolog extra-logical predicate once takes a single argument, which must be a "callable term" - one that makes sense as a goal - e.g. happy(X) makes sense as a goal, but 23 does not - and calls the term in such a way as to produces just one solution. It is defined as:

 

once(P) :- P, !.

See also !, call, backtracking.

 

op, infix, prefix, and postfix operators, precedence in Prolog
Syntax:

:- op(+Precedence, +Type, :Name).

The Prolog built-in predicate op serves to define the Type and Precedence of infix and postfix, and prefix operators in Prolog terms. Prolog terms normally begin with the functor (e.g. likes, in likes(mary, pizza)) but exceptions exist - for example, arithmetic expressions are written in the usual infix way (i.e. as X + 1, rather than +(X, 1)), and negation can be written without parentheses, as a prefix operator: not P.

The table below lists the predefined infix operators in SWI-Prolog. You may wish to add infix operators of your own. For example, you might wish to define an infix and. This can be done as follows:

:- op(700, xfy, and).

This declares and to be an operator with precedence 700 and type xfy.

Note two things: (1) the fact that these things are referred two as operators does not mean that an operation is performed when they are encountered. This is not usually the case; and (2) the declaration of an operator only affects the external appearance of the operator in your program - internally, the standard representation is used - for example X + 1 really is internally represented by Prolog as though it was +(X, 1).

Precedence is an integer between 0 and 1200. Precedence 0 removes the declaration. Type is one of: xf, yf, xfx, xfy, yfx, yfy, fy or fx. The f indicates the position of the functor, while x and y indicate the position of the arguments. Thus xfx, xfy, and yfx all indicate an infix operator. y should be interpreted as "in this position a term with precedence equal to or less than the precedence of the functor should occur". For x the precedence of the argument must be strictly less than that of the functor. The precedence of a term is 0, unless its principal functor is an operator, in which case the precedence is the precedence of this operator. A term enclosed in brackets (...) has precedence 0.

To see how this works, consider the arithmetic expression a - b - c. In normal maths, this is interpreted as (a - b) - c, rather than a - (b - c). To achieve this, the binary infix operator- must be declared as type yfx so that the first argument has precedence over the second. Then, internally, a - b - c will be represented as -(-(a, b), c) rather than -(a, -(b, c)).

Built-in Operators (SWI-Prolog)

Prec.TypeOperator
1200xfx-->, :-
1200fx:-, ?-
1150fxdynamic, discontiguous, initialization, module_transparent, multifile, thread_local, volatile
1100xfy;, |
1050xfy->, op*->
1000xfy,
954xfy
900fy+
900

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

章节正文