質問

After many years of abstinence of the PROLOG programming language, I'm trying to get into it again. And promptly there, something confused me.

(I am using SWI prolog 6.4.1. on windows)

Consider the following defined:

father(jack, clara).
father(jack, sophie).

mother(angela,clara).
mother(angela,sophie).

parent(A, B) :- father(A, B).
parent(A, B) :- mother(A, B).

sibling( A, B ) :-
    A \= B,
    parent(P, A),
    parent(P, B).

Now, if I "ask" the interpreter: sibling(clara, sophie).

true is the answer.

But if I try to get the siblings of e.g. clara: sibling(clara, X).

The answer is just false. Just as findall( X, sibling(clara, X ), L ).

returns an empty list.

Why?

役に立ちましたか?

解決

To prove sibling(clara, X), you first need to prove clara \= x. But that doesn't work because it reduces to \+ clara = X, were \+ is the infamous negation as failure: Prolog tries to prove clara = X, which succeeds, and concludes that clara \= X must therefore be false.

You should either reorder your program to do the \= check last instead of first, or use dif(clara, X).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top