2

I have predicate

superclass('Horde', 'Blood Elf').
superclass('Horde', 'Orc').

element('Blood Elf', ['Paladin', 'Priest','Mage','Warlock','Death Knight','Rogue']).
element('Orc', ['Warrior', 'Shaman','Warlock','Death Knight','Hunter','Rogue']).

find(A):-
  (  element(_,B),member(A,B)
  -> forall(
        ( element(_,B), member(A,B) ),
        ( element(C,B), superclass(D,C), format('~w -> ~w -> ~w\n',[D,C,A])))
  ;  superclass(A, _)
  -> format('~w\n',A), forall(superclass(A,B),format('\t~w\n',B))
  ).  

and two of results for find('Rogue').. After all, the predicate prints only 1 of them. However, when i copypaste forall(..) to console, it gives me all 2 results. Why?

4

1 回答 1

2

本质上,你有一个单曲(->)/2就是If -> Then. 在If你的情况下

( element(_,B), member(A,B) )

接受第一个答案并承诺,因此不会考虑其他答案。这Then是一个forall/2要么成功要么失败。因此,永远不应该出现您得到多个答案的情况。


因此,您将原始程序与查询进行比较:

?- forall(
      (  element(_,B), member('Rogue',B) ),
      (  element(C,B),
         superclass(D,C),
         format('~w -> ~w -> ~w\n',[D,C,'Rogue'])
      )).

但原来的程序不同!它本质上是:

?- element(_,B),member(A,B)
    -> forall(
          (  element(_,B), member(A,B) ),
          (  element(C,B),
             superclass(D,C),
             format('~w -> ~w -> ~w\n',[D,C,A]))).

所以在原始程序中A 并且 B是固定的,而在您的查询中,只有A是固定的。

请注意,以这种方式通过写东西进行编程,通常会导致与 Prolog 本身无关的各种错误。我宁愿建议您首先坚持使用 Prolog 的纯单调子集。

于 2014-10-28T17:19:35.567 回答