我面临的问题有点微不足道.我想在Prolog中使用逻辑,但似乎not/1不是我想要的东西:
course(ai).
course(pl).
course(os).
have(X,Y) :- course(X),course(Y),not(X = Y).
Run Code Online (Sandbox Code Playgroud)
我查询:
have(X,Y), write(X-Y), nl , fail.
Run Code Online (Sandbox Code Playgroud)
我没有得到我想要的结果:(
它们存在吗?他们是如何实施的?
所述coroutining SWI-Prolog的(的谓词freeze,when,dif等)具有的功能警卫.它们如何适合首选的Prolog编程风格?
我是很新的逻辑编程(有序言和共)和事实,即它不是纯粹的声明,并要求即使在非常简单的情况下程序上的考虑有点困惑(见本关于使用问题\==或dif).我错过了重要的事吗?
我在理解为什么我的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)
这里发生了什么?
这有什么区别:
X \= Y
Run Code Online (Sandbox Code Playgroud)
和这段代码:
dif(X, Y)
Run Code Online (Sandbox Code Playgroud)
我认为他们应该表现得一样,但他们没有。这是示例:
n_puta(L, N, X) :- nputa(L, N, 0, X).
nputa([], N, C, _) :- N = C.
nputa([G|R], N, C, X) :- G = X, nputa(R, N, Y, X), C is Y - 1.
nputa([G|R], N, C, X) :- dif(G,X), nputa(R, N, C, X).
Run Code Online (Sandbox Code Playgroud)
这里有一些电话:
?- n_puta([a,a,b,b,b], 2, X).
X = a ;
false.
?- n_puta([a,a,b,a,b,b], 3, X).
X = a ;
X = b ;
false.
Run Code Online (Sandbox Code Playgroud)
X should be the atom that …
我在Prolog中有两个稍微不同的谓词unique_element/2实现.当给定元素X和列表L时,谓词成功,元素X在列表中仅出现一次.以下是实现和结果:
实施1:
%%% unique_element/2
unique_element(Elem, [Elem|T]) :-
not(member(Elem, T)).
unique_element(Elem, [H|T]) :-
member(Elem, T),
H\==Elem,
unique_element(Elem, T),
!.
Run Code Online (Sandbox Code Playgroud)
结果:
?- unique_element(X, [a, a, b, c, c, b]).
false.
?- unique_element(X, [a, b, c, c, b, d]).
X = a ;
X = d.
Run Code Online (Sandbox Code Playgroud)
实施2:
%%% unique_element/2
unique_element(Elem, [Elem|T]) :-
not(member(Elem, T)).
unique_element(Elem, [H|T]) :-
H\==Elem,
member(Elem, T),
unique_element(Elem, T),
!.
Run Code Online (Sandbox Code Playgroud)
如果你没有第一眼就注意到:"H\== Elem"和"成员(Elem,T)"在第二个impl,规则2上被翻转.
结果:
?- unique_element(X, [a, a, b, c, c, b]).
X = a.
?- unique_element(X, [a, b, c, c, …Run Code Online (Sandbox Code Playgroud) 我正在七周的七种语言中工作,但有一些我对prolog不了解.我有以下程序(基于他们的华莱士和grommit程序):
/* teams.pl */
onTeam(a, aTeam).
onTeam(b, aTeam).
onTeam(b, superTeam).
onTeam(c, superTeam).
teamMate(X, Y) :- \+(X = Y), onTeam(X, Z), onTeam(Y, Z).
Run Code Online (Sandbox Code Playgroud)
并像这样加载它
?- ['teams.pl'].
true.
Run Code Online (Sandbox Code Playgroud)
但它没有给我任何解决方案
?- teamMate(a, X).
false.
Run Code Online (Sandbox Code Playgroud)
它可以解决更简单的东西(在书中显示):
?- onTeam(b, X).
X = aTeam ;
X = superTeam.
Run Code Online (Sandbox Code Playgroud)
有解决方案:
?- teamMate(a, b).
true ;
false.
Run Code Online (Sandbox Code Playgroud)
我错过了什么?我尝试了gnu prolog和swipl.
......还有更多......
当你移动"不能成为你自己的队友"的限制然后结束:
/* teams.pl */
onTeam(a, aTeam).
onTeam(b, aTeam).
onTeam(b, superTeam).
onTeam(c, superTeam).
teamMate(X, Y) :- onTeam(X, Z), onTeam(Y, Z), \+(X = Y).
Run Code Online (Sandbox Code Playgroud)
它给了我期望的解决方案:
?- ['teams.pl'].
true. …Run Code Online (Sandbox Code Playgroud)