Pregunta

Just started programming in Prolog. I wrote a couple of clauses.

predicates
parent(symbol,symbol)
male(symbol)
female(symbol)
mother(symbol,symbol)
father(symbol,symbol)
brother(symbol,symbol)
sister(symbol,symbol)
uncle(symbol,symbol)
clauses
parent(arthur,fred).
parent(arthur,george).
parent(arthur,bill).
parent(arthur,percy).
parent(arthur,ginny).
parent(arthur,ron).
parent(arthur,charlie).



parent(molly,fred).
parent(molly,george).
parent(molly,bill).
parent(molly,percy).
parent(molly,ginny).
parent(molly,ron).
parent(molly,charlie).
parent(rowling,arthur).
parent(rowling,james).


parent(james,harry).
parent(lily,harry).

female(molly).
female(ginny).
female(lily).
female(rowling).


male(X) if not(female(X)).


brother(X,Y) if male(Y),X<>Y,brother(Y,X).
brother(X,Y) if X<>Y,parent(Z,X),parent(Z,Y),male(X).

mother(X,Y) if parent(X,Y),female(X).
father(X,Y) if parent(X,Y),male(X).
sister(X,Y) if parent(Z,X),parent(Z,Y),female(X),X<>Y.
uncle(X,Y) if parent(Z,Y),brother(X,Z).

But when i am trying to ask question

brother(X,james)

It is showing Free variable in expression at X<>Y in line

brother(X,Y) if male(Y),X<>Y,brother(Y,X).

I am unable to trace what is the problem with the code

¿Fue útil?

Solución

maybe you could try to simplify the rule

brother(X,Y) if male(Y),X<>Y,parent(Z,X),parent(Z,Y).

but this wouldn't work because male/2 it's defined only in negative terms. To make it work I would add:

person(X) :- parent(X,_) ; parent(_,X).
male(X) :- person(X), not(female(X)).

Now male/1 is able to generate positive information...

(note: untested code)

edit a better way, without adding person/1, could be

/* Y is brother of X */
brother(X,Y) if parent(Z,X), parent(Z,Y), X<>Y, male(Y).
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top