Prolog order of query rules

1k views Asked by At

Prolog Question: Just started learning prolog and this was on one of the practice quizzes we were given.

Given:

avenger(thor).
avenger(captainAmerica).
sibling(thor,loki).
asgardian(thor).
asgardian(X) :- sibling(Y,X),asgardian(Y).
train1(X,Y) :- avenger(X),!,avenger(Y).
train2(X,Y) :- avenger(X),X\=Y,avenger(Y). 

List all answers returned by the following queries.

train2(A, captainAmerica). %returns A=thor.
train2(captainAmerica, A). %returns false.

My question is about the second query. Why wouldn't this return A=thor. ? I messed around a bit and if i change train2 to

train2(X,Y) :- avenger(X),avenger(Y),X\=Y.

when i run the second query I get

A=thor. 

A quick explanation of why the order of the rules in the query matters here would be awesome. Thanks.

2

There are 2 answers

0
AudioBubble On BEST ANSWER

\= is a weird predicate... It says, "if the unification of the two arguments succeeds, fail; if the unification fails, succeed". So, as the unification of a free variable with an atom will always succeed, it fails.

Once the Y has been unified with thor, the unification of captainAmerica with thor fails, so the X \= Y succeeds.

Anyway, you should not use \= in this context. Instead, use dif/2. Try messing around with a predicate defined as:

train3(X, Y) :-
    dif(X, Y),
    avenger(X),
    avenger(Y).

Better than the other two in several ways. You can search SO for other questions with dif/2.

3
Paulo Moura On

The \=/2 standard predicate is true when its arguments do not unify. Thus, it requires both arguments to be bound (i.e. not variables) to be meaningful. Your solution:

train2(X,Y) :- avenger(X), avenger(Y), X \= Y.

is correct as the two proceeding calls to the avenger/1 predicate ensure that both X and Y will be sufficiently instantiated before the call to the \=/2 predicate.

As Boris explained in his answer, the alternative of using the dif/2 predicate, have the virtue of making goal order in the clause above irrelevant. There's a caveat, however. The dif/2 predicate is not a standard predicate and not all Prolog implementations provide it.