简体   繁体   中英

Newbie: Unable to trace Free variable in expression error. Prolog Programming

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 <>Y in line <> Y行的表达式中显示Free变量

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM