0

I am learning Prolog and what i want is to do a simple "calculation"(i dont know how its called in Prolog) of the members of a simle family.

For example i have:

 1)father of steve is petter
 2)brother steve is john
 3)A person is a son to a Father when the brother of the person has  as father the Father

(it seems funny and completely out of logic when its out of my mind :) )

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).

and my question is who is the father of john(the correct awnser would be petter)

?-father(john,X).

But it always giving me false.

SteveL
  • 3,331
  • 4
  • 32
  • 57

2 Answers2

0

When you enter father(john, X)., it starts by trying to find a Z such that brother(john, Z) is true. No such Z exists, so it returns false.

Note that brother(steve, john) does not imply brother(john, steve) unless you tell Prolog that it should.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Well.in the tutorial i was reading the something(1,2) was saying could be interpreted and as something(2,1) , but i guess it was meaning only by the programmer not by Prolog ...my fault... But even if i change them i am getting false again , even when i put both of the cases .Whats the correct way? – SteveL Nov 03 '12 at 00:18
  • @SteveL I think that what you read meant that you can encode phrases like `Mike plays with a dog` either as `plays(mike, dog)` or `plays(dog,mike)`. But once you choose an interpretation you have to stick with it. – Thanos Tintinidis Nov 03 '12 at 09:44
0

SOLVED:

father(steve,petter).
brother(john,steve).
whoisfather(X,Y):-brother(X,Z),father(Z,Y).
?- whoisfather(john,X).
X = petter.

instead of

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
?- father(john,X).
false.

See comments

SteveL
  • 3,331
  • 4
  • 32
  • 57
  • 1
    no, actually, your original code works. You just have one extra parenthesis there, a typo. When I run it, I get `2 ?- father(john,X).` ==> `X = petter ;` (here I press `;`) and then `No`. – Will Ness Nov 06 '12 at 17:08
  • lol . how did i miss that omg.This *** litle mistakes that gets you crazy. Thanks – SteveL Nov 06 '12 at 22:25