chac 已经铺平了道路。Prolog 规则具有以下形式:
Head :- Body.
对于头部,您可以选择化合物或原子。正文可以是 Prolog 查询。查询基本上是从以下内容构建的:
- Invocations: Call some other rules with bound or unbound arguments
- Conditions: Unification =, Arithmetic =:=, <, etc.. Lexical @<, ==, etc..
- Connectives: And ,, Or ;, Not \+ etc..
- Everything else that is found in the handbook of your Prolog system.
如果您有规则的口头规范。首先查找主调用,然后查找条件,最后查找连接词。这是一个例子:
Holidays (book should be less than 400 pages and not be a study or reference book).
我得到:
Main invocation: book(Title, Author, Genre, Pages)
Condition_1: Pages < 400
Condition_2: Genre = study
Condition_3: Genre = reference
Connectives: Condition_1, \+ (Condition_2 ; Condition_3)
如果我将所有这些放在一起,我会得到以下主体,您可以轻松地首先在顶层作为查询对其进行测试:
?- book(Title, Author, Genere, Pages), Pages < 400, \+ (Genre = study; Genre = reference).
现在你可以把它变成一个规则。注意使用下划线 (_) 表示未使用的调用变量,否则 Prolog 系统会向您发出单例警告:
holidays(Title) :-
book(Title, _, Genre, Pages),
Pages < 400,
\+ (Genre = study; Genre = reference).
这是一个很好的作业,你有一个好老师。玩得开心。
再见