2

我正在尝试在 Prolog 中为我的家庭作业编写一个家谱程序。这是代码的一部分。

/** a sample fact **/
parents(someone, someoneelse, child).

/** are M and F parents of the children in the given list? **/
parents(M,F,[]) :- parents(M,F,_).
parents(M,F,[First|Rest]) :- parents(M,F,First), parents(M,F,Rest).

/** are M and F parents? **/
parents(M,F) :- parents(M,F,_).

当我使用与事实匹配的查询时,上面的代码有效。

   [trace] 25 ?- parents(someone,someoneelse).
       Call: (6) parents(someone, someoneelse) ? creep
       Call: (7) parents(someone, someoneelse, _G586) ? creep
       Exit: (7) parents(someone, someoneelse, child) ? creep
       Exit: (6) parents(someone, someoneelse) ? creep
    true 
    .

但是当我尝试这个时:

[trace] 26 ?- parents(aa, bb).
   Call: (6) parents(aa, bb) ? creep
   Call: (7) parents(aa, bb, _G541) ? creep
   Call: (8) parents(aa, bb, _G541) ? creep
   Call: (9) parents(aa, bb, _G541) ? creep
   Call: (10) parents(aa, bb, _G541) ? 
...

它不起作用,它进入一个无限循环。我在这里做错了什么?

编辑:我已经改变了代码:

/** a sample fact **/
parents(someone, someoneelse, child).

/** are M and F parents of the children in the given list? **/
parents(M,F,[]).
parents(M,F,[First|Rest]) :- parents(M,F,First), parents(M,F,Rest).

/** are M and F parents? **/
parents(M,F) :- parents(M,F,A), not(A=[]).

现在我得到的是:

[trace] 3 ?- parents(a,b).
   Call: (6) parents(a, b) ? creep
   Call: (7) parents(a, b, _G514) ? creep
   Exit: (7) parents(a, b, []) ? creep
^  Call: (7) not([]=[]) ? creep
^  Fail: (7) not(user: ([]=[])) ? creep
   Redo: (7) parents(a, b, _G514) ? creep
   Call: (8) parents(a, b, _G508) ? creep
   Exit: (8) parents(a, b, []) ? creep
   Call: (8) parents(a, b, _G509) ? creep
   Exit: (8) parents(a, b, []) ? creep
   Exit: (7) parents(a, b, [[]]) ? creep
^  Call: (7) not([[]]=[]) ? creep
^  Exit: (7) not(user: ([[]]=[])) ? creep
   Exit: (6) parents(a, b) ? creep
true 
.

我不明白为什么它现在失败后要重做。有任何想法吗?

4

2 回答 2

0

我有一种感觉,您将两个不同的谓词混淆为一个:

/** a sample fact **/
parents(someone, someoneelse, child).

/** are M and F parents of any child? **/
parents(M,F) :- parents(M,F,_).

/** are M and F parents of the children in the given list? **/
parents_of_all_children(M,F,[]).
parents_of_all_children(M,F,[First|Rest]) :-
    parents(M,F,First), parents_of_all_children(M,F,Rest).

然后,您的查询是有意义的:

?- parents(someone, someoneelse, C).
C = child.
%-- someone & someoneelse have child "child"

?- parents(A, B).
A = someone,
B = someoneelse.
%-- all possible parent pairs out there

?- parents(aa, bb).
false.
%-- do "aa" & "bb" form a parents pair?
于 2012-05-21T17:56:26.547 回答
0

你必须停止搜索。Prolog 将遍历所有规则并找到匹配项。如果匹配,那么它将通过扩展并重复该过程。

第 2 行是 inf 循环的原因,因为 don't care(来自最后一条规则)映射到 []。

如果您想知道一对是否是任何孩子的父母,您只需删除 2 条中间规则:

parents(M,F,[]) :- parents(M,F,_).
parents(M,F,[First|Rest]) :- parents(M,F,First), parents(M,F,Rest).
于 2012-05-21T16:41:31.017 回答