Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog: a person is a sibling of himself?

I'm having some trouble understanding why my code in prolog does something based on the order I put my rules in.

Here is my database:

parent(tom, bob).
parent(tom, liz).
parent(mary, bob).
parent(mary, liz).

male(tom).
male(bob).
female(mary).
female(liz).

And here are the rules:

%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).

The problem is that when I do this,

?- sibling(bob, X).

I get

X = bob ;
X = liz ;
false.

But when I change the ordering (I put difference(X, Y) at the last part)

sibling(X, Y) :-
    mother(M, X), mother(M, Y),
    father(F, X), father(F, Y),
    difference(X, Y).

and I call

?- sibling(bob, X).

I get

X = liz;
false.

which is what I want.

So far, I've only seen that the ordering of the rules matter when doing recursion. So I don't understand how bob is still a sibling of himself just because I put the difference check first.

Thanks for any help!

like image 822
user84399 Avatar asked Nov 26 '13 17:11

user84399


People also ask

How do you write a sibling in Prolog?

sibling(X,Y) :- parent(Z,X),parent(Z,Y). female(X) :- mother(X,Y).

How do you write uncle in Prolog?

uncle(X, Y):- parent(Z, Y), sib(X , Z), male(X). e) niece relationship. niece(X,Y):- parent(Z, X), sib(Y , Z).


2 Answers

The actual reason for your problem is (\==)/2 within different/2. It succeeds simply too often. Replace it with dif/2 and you will get the expected behavior. dif/2 is available in many Prolog systems like SICStus, YAP, B, SWI. You can also define a safe approximation in ISO-Prolog like so:

iso_dif(X, Y) :-
   X \== Y,
   ( X \= Y -> true
   ; throw(error(instantiation_error,iso_dif/2))
   ).

Now, should the arguments be not sufficiently instantiated, you will get an Instantiation Error. Prolog aborts the computation and says: I have no idea! Which is far better than pretending it does have an idea while it has none.

Using iso_dif/2 you will still have to place it at the end of the rule. But this time, Prolog will keep an eye on its correct usage.

?- iso_dif(a,b).
   true.
?- iso_dif([a,_],[b,_]).
   true.
?- iso_dif([a,X],[X,b]).
   true.
?- iso_dif([a,X],[a,X]).
   false.
?- iso_dif([a,X],[X,X]).
   error(instantiation_error,iso_dif/2).
like image 198
false Avatar answered Oct 16 '22 14:10

false


This is because of the way unification works. If you put difference first the values of X and Y are not yet unified to any values. Consider the trace:

 goal list: [sibling(bob, Z)]
 goal: sibling(bob, Z).
 X-> bob, Y -> Z
 goal list: [difference(bob, Y), mother(M, bob), mother(M, Y), father(F, bob), father(F, Y).]
 goal: difference(bob, Y) --SUCCESS
 goal list: [mother(M, bob), mother(M, Y), father(F, bob), father(F, Y).]
 goal: mother(M, bob)
 ...

When you put the difference call last, both X and Y have been unified and difference will fail if they are the same value. Then backtracking will occur.

Use the trace feature of your prolog environment to see what happens step by step during the execution.

like image 42
Vincent Ramdhanie Avatar answered Oct 16 '22 16:10

Vincent Ramdhanie