fx | ~ |
700 |
xfx |
<, =, =.., =@=, =:=, =<, ==, ==, >, >=, @<, @=<, @>, @>=, =, ==, is |
600 |
xfy |
: |
500 |
yfx |
+, -, /, /, xor |
500 |
fx |
+, -, ?, |
400 |
yfx |
*, /, //, rdiv, <<, >>, mod, rem |
200 |
xfx |
** |
200 |
xfy |
^ |
Note that all operators can be redefined by the user, most commonlyaccidentally and with disastrous results.
or, logical disjunction, ;
The logicalconjunction (logical-and, ∧) of goals is achieved in Prolog by separating the goals by commas:
happy(X) :- rich(X), famous(X).
says that X
is happy
if X
is rich and X
is famous. What about logical-or, ∨, also called disjunction? If we want to say that X
is happy
if X
is rich
or if X
is famous
, we have two possibilities:
use two alternative rules
use the ;
operator (pronounced "or").
Option 1:
happy1(X) :- rich(X).
happy1(X) :- famous(X).
Option 2:
happy2(X) :- rich(X) ; famous(X).
Sometimes it is necessary, or at least advisable, to wrap parentheses ( )
around a disjunction in order to make clear what how the conjunctions and disjunctions interact. If you leave out the parentheses, Prolog has its own rules about the precedence of logical-and and logical-or, which might or might not correspond to what you intend. If you use parentheses, the things inside the parentheses are done first, so everything is crystal clear.
Option 3:
happy3(X) :- attractive(X), ( rich(X) ; famous(X) ).
This says X
is happy3
if X
is attractive, and X
is either rich or famous:
happy3(X) ⇐ attractive(X) ∧ (rich(X) ∨ famous(X)).*
* The Prolog Dictionary wishes to acknowledge that a range of world religions and philosophies would not agree that the criteria specified for happy1
, happy2
, and happy3
are either necessary nor sufficient for happiness. The definitions given in this article, are, therefore, purely intended as Prolog programming examples, and should not be taken as a practical guide to life. Thank you.
output in Prolog
Output in Prolog is not always needed, as often the variable bindings return all the information that is required. If explicit output is required, a set of extra-logical built-in predicates is available.
These include:
write(X)
which writes the term X to the current outputstream (which means the window on your workstation unless you have done something fancy).
print(X, …)
which writes a variable number of arguments to the current output stream. If an argument is a string (like 'Hello world!n'
) then the string is printed without quotes, and any 'n'
and 't'
are interpreted as newline and tab, respectively. A newline is printed at the end of the printing operation.
prin(X, …)
is like print
except that it does not append a newline to the end of the output.
tab(N)
prints N spaces to output - N
should be a number.
nl
starts a new line.
put(C)
prints a single character to the current output stream. C might be bound to a number in the range 0 to 255, in which case the number is treated as a character code, and the equivalentcharacter is printed, or it might be bound to a character, e.g. C = 'a'
, in which case obviously the character is printed.
predicate
Used to refer to the functionality afforded by a Prolog procedure. In this dictionary, we tend to talk about procedures when the code is written by the programmer, but predicates when the functionality is built-in, as with member.
procedure
A procedure in Prolog is a group of clauses about the same relation, for example:
is_a_parent(Parent) :- father_of(Parent, _Child).
is_a_parent(Parent) :- mother_of(Parent, _Child).
Here, two clauses together define the condition for someone to be a parent - together they form a procedure.
See also underscore for the reason for putting an underscore (_
) character at the front of the variable_Child
.
query
A query is a list of one or more goals typed to the Prolog interpreter's ?-
prompt, separated by commas, and terminated with a full stop (.
). For example,
?- lectures(john, Subject), studies(Student, Subject).
is a query comprising two goals. Prolog tries to satisfy the goals, and if it manages to do this, the query is said to succeed. If not, the query fails.
If the query fails, Prolog types "false.
". If it succeeds, Prolog either types the list of variable bindings it had to assume in order to make the query succeed, or, if no variable bindings were necessary, it types "true.
".
If several variable bindings allow the query to succeed, then normally (i.e. in the absence of cuts) all the bindings will be typed out, one after the other, with the Prolog user typing a ;
to let the Prolog system know that they are ready to see the next set of bindings.
concept [´kɔnsept] n.概念;观念;思想 (初中英语单词)failure [´feiljə] n.失败;衰竭;破产 (初中英语单词)obvious [´ɔbviəs] a.明显的;显而易见的 (初中英语单词)relative [´relətiv] a.有关系的 n.亲属 (初中英语单词)interpreter [in´tə:pritə] n.译员;解释者;翻译器 (初中英语单词)supposed [sə´pəuzd] a.想象的;假定的 (初中英语单词)standing [´stændiŋ] n.持续 a.直立的 (初中英语单词)operator [´ɔpəreitə] n.操作者;接线员 (初中英语单词)available [ə´veiləbəl] a.可用的;有效的 (初中英语单词)comparison [kəm´pærisən] n.比较;对照;比喻 (初中英语单词)computer [kəm´pju:tə] n.计算机;电子计算器 (初中英语单词)treatment [´tri:tmənt] n.待遇;对待;治疗 (初中英语单词)argument [´ɑ:gjumənt] n.辩论;争论;论证 (初中英语单词)solution [sə´lu:ʃən] n.解答;解决;溶解 (初中英语单词)program [´prəugræm] n.说明v.为…安排节目 (初中英语单词)principal [´prinsəpəl] a.主要的 n.负责人 (初中英语单词)normal [´nɔ:məl] a.正规的 n.正常状态 (初中英语单词)achieve [ə´tʃi:v] vt.完成;达到;获得 (初中英语单词)crystal [´kristəl] n.水晶 a.水晶的 (初中英语单词)attractive [ə´træktiv] a.有吸引力;诱人的 (初中英语单词)acknowledge [ək´nɔlidʒ] vt.(公开)承认;感谢 (初中英语单词)therefore [´ðeəfɔ:] ad.&conj.因此;所以 (初中英语单词)purely [´pjuəli] ad.仅仅;简单地 (初中英语单词)output [´autput] n.产品;产品;计算结果 (初中英语单词)stream [stri:m] n.河 vi.流出;飘扬 (初中英语单词)character [´kæriktə] n.特性;性质;人物;字 (初中英语单词)obviously [´ɔbviəsli] ad.明显地;显而易见地 (初中英语单词)absence [´æbsəns] n.不在,缺席;缺乏 (初中英语单词)system [´sistəm] n.系统,体系,制度 (初中英语单词)symbol [´simbəl] n.符号;象征 (高中英语单词)proposition [,prɔpə´ziʃən] n.提议;主张;陈述 (高中英语单词)critical [´kritikəl] a.批评的;关键性的 (高中英语单词)assumption [ə´sʌmpʃən] n.掌握;担任;假定 (高中英语单词)infinite [´infinit] a.无限的,无穷的 (高中英语单词)dependent [di´pendənt] a.依赖的;从属的 (高中英语单词)emphasis [´emfəsis] n.强调;重点 (高中英语单词)define [di´fain] vt.解释;说明;限定 (高中英语单词)declaration [,deklə´reiʃən] n.宣布;宣言;申报 (高中英语单词)external [ik´stə:nəl] a.外部的;外面的 (高中英语单词)representation [,reprizen´teiʃən] n.描写;表现(法) (高中英语单词)strictly [´striktli] ad.严格地 (高中英语单词)commonly [´kɔmənli] ad.一般地;通常 (高中英语单词)correspond [,kɔri´spɔnd] vi.符合;相当;通信 (高中英语单词)equivalent [i´kwivələnt] a.相等的 n.同等物 (高中英语单词)procedure [prə´si:dʒə] n.过程;手续;方法 (高中英语单词)prompt [prɔmpt] a.敏捷的 vt.促使 (高中英语单词)logical [´lɔdʒikəl] a.逻辑(上)的 (英语四级单词)arithmetic [ə´riθmətik] n.算术 (英语四级单词)disastrous [di´zɑ:strəs] a.招致灾祸的;不幸的 (英语四级单词)conjunction [kən´dʒʌŋkʃən] n.联合;巧合;接近 (英语四级单词)alternative [ɔ:l´tə:nətiv] a.二中选一的 n.选择 (英语四级单词)respectively [ri´spektivli] ad.各自地;分别地 (英语四级单词)normally [´nɔ:məli] ad.正常情况下;通常 (英语六级单词)accidentally [,æksi´dentəli] ad.偶然地 (英语六级单词)advisable [əd´vaizəbl] a.合适的,得当的 (英语六级单词)variable [´veəriəbəl] a.易变的 n.可变量 (英语六级单词)