如果我想确保两个变量不实例化到同一个术语,那么首选方法是什么?
假设我需要在图中找到有向边,并且节点不能有自己的边:
node(a, x, y). node(b, z, x). node(c, y, y).
Run Code Online (Sandbox Code Playgroud)
(这里的边是 - > c,b - > a,但不是 c - > c)
以下作品:
edge(A, B) :- node(A, _, X), node(B, X, _), A \== B.
Run Code Online (Sandbox Code Playgroud)
这也有效[swi-prolog]:
edge(A, B) :- dif(A, B), node(A, _, X), node(B, X, _).
Run Code Online (Sandbox Code Playgroud)
这显然不起作用(因为A和B都没有被实例化?):
edge(A, B) :- A \== B, node(A, _, X), node(B, X, _).
Run Code Online (Sandbox Code Playgroud)
我想我的第一个解决方案的问题是,使用更复杂的node谓词,在edge失败之前可能会发生许多不必要的统一.在dif另一方面,是在图书馆,这表明它并不意味着在这种简单的情况下使用(虽然它,我似乎在寻找精确的功能).
Prolog中A\= B与否(A == B)之间有什么区别?
我发现这个http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse5 和这个wiki页面 http://en.wikibooks.org/wiki/Prolog/Built-in_predicates 但它没有帮助我,因为没有澄清差异,也没有简短的意义\ =.
谢谢.
我是PROLOG的新手,并且正处于本页练习的最开始阶段.给定父(X,Y)和男(X)的规则,我试图将规则母(X,Y)定义为
mother(X, Y) :-
not(male(X)),
parent(X, Y).
Run Code Online (Sandbox Code Playgroud)
但是,在GNU Prolog中我收到以下错误:
| ?- mother(lina, julia).
uncaught exception: error(existence_error(procedure,not/1),mother/2)
| ?-
Run Code Online (Sandbox Code Playgroud) 说我有以下理论:
a(X) :- \+ b(X).
b(X) :- \+ c(X).
c(a).
Run Code Online (Sandbox Code Playgroud)
它只是说真的,这当然是正确的,a(X)因为没有b(X)(因为有限的失败否定).因为只有一个b(X)如果没有c(X),我们就有c(a),可以说这是真的.我想知道为什么Prolog没有提供答案X = a?比如说我介绍一些语义:
noOrphan(X) :- \+ orphan(X).
orphan(X) :- \+ parent(_,X).
parent(david,michael).
Run Code Online (Sandbox Code Playgroud)
当然,如果我查询noOrphan(michael),这将导致true与noOrphan(david)在false(因为我没有定义父david),但我不知道为什么会有的检测没有积极主动地哪些人(michael,david,...)属于noOrphan/1关系?
这可能是Prolog的回溯机制的结果,但是Prolog可以维持一个状态,该状态验证一个人是否以积极方式(0,2,4,...)否定深度或负面方式(1,3) ,5,...)否定深刻.
我在理解为什么我的prolog代码根据我的规则的顺序做了一些事情时遇到了一些麻烦.
这是我的数据库:
parent(tom, bob).
parent(tom, liz).
parent(mary, bob).
parent(mary, liz).
male(tom).
male(bob).
female(mary).
female(liz).
Run Code Online (Sandbox Code Playgroud)
以下是规则:
%difference(X, Y) ==> Predicate to check if two people X and Y are not the same person.
difference(X, Y) :- \==(X, Y).
father(X, Y) :- male(X), parent(X, Y), difference(X, Y).
mother(X, Y) :- female(X), parent(X, Y), difference(X, Y).
sibling(X, Y) :-
difference(X, Y),
mother(M, X), mother(M, Y),
father(F, X), father(F, Y).
Run Code Online (Sandbox Code Playgroud)
问题是,当我这样做时,
?- sibling(bob, X).
Run Code Online (Sandbox Code Playgroud)
我明白了
X = bob ;
X = liz ;
false. …Run Code Online (Sandbox Code Playgroud) 我正在使用SICStus Prolog并拥有一系列事实:
student('John Henry', 'Maths').
student('Jim Henry', 'Maths').
student('John Alan', 'Maths').
student('Alan Smith', 'Computing').
student('Gary Henry', 'Maths').
Run Code Online (Sandbox Code Playgroud)
我想得到两个学生的共同主题,两个学生都不同,所以我得到了:
sharedSubject(S1, S2, Sub) :- S1 \== S2, student(S1, Sub), student(S2, Sub).
Run Code Online (Sandbox Code Playgroud)
但是,当我输入:
sharedSubject('John Henry', F, E).
Run Code Online (Sandbox Code Playgroud)
我得到F = 'John Henry'.有人可以指出我出错的地方以及我需要做什么吗?谢谢.
我试图了解Prolog的工作原理.我正在使用SWI-Prolog.这是一些代码:
forall(C1,C2) :- \+ (C1, \+ C2).
foo(N) :- N < 10.
bar(N) :- N > 5.
foobar(N) :- forall(foo(N),bar(N)).
Run Code Online (Sandbox Code Playgroud)
如果我执行以下操作,它会产生所需的输出:
?- foobar(5).
false.
Run Code Online (Sandbox Code Playgroud)
但是当我试图看到所有可能的值时,我得到一个错误:
?- foobar(N).
ERROR: </2: Arguments are not sufficiently instantiated
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
TL;DR: sibling(a,X)成功回答X = a,但sibling(a,a)失败。
我有以下 Prolog 文件:
children(a, c).
children(a, d).
children(b, c).
children(b, d).
sibling(X, Y) :-
X \== Y, A \== B,
children(X, A), children(X, B),
children(Y, A), children(Y, B).
Run Code Online (Sandbox Code Playgroud)
对我来说似乎很清楚,如果他们的父母相同,那么两个人就是兄弟姐妹。此外,一个人不是他们自己的兄弟姐妹。
但是当我尝试在 GNU Prolog 上运行一些查询时,我得到了一些奇怪的结果:
| ?- sibling(a, b).
true ? a
true
true
yes
Run Code Online (Sandbox Code Playgroud)
这是预期的行为。a并且b是兄弟姐妹。有三个结果,这有点奇怪,但我认为 Prolog 是绑定A = c, B = d和A = d, B = c.
| ?- sibling(a, a).
no
Run Code Online (Sandbox Code Playgroud)
我认为这意味着a …