Reputation: 43
Hi I'm new to prolog and I was trying to create a basic family tree so fair i have this:
/* Database for family. It consists of facts and rules. */
/* Facts */
female(ella).
female(jodi).
female(sonya).
male(arnold).
male(chris).
male(louis).
male(mark).
father_of(arnold, chris). /* arnold is the father of chris */
father_of(louis, mark).
father_of(mark, arnold).
mother_of(ella, sonya).
mother_of(jodi, ella).
mother_of(jodi, mark).
/* Rules */
grandmother_of(X, Z) :-
mother_of(X, Y),
(mother_of(Y, Z); father_of(Y, Z)).
familyquestions :-
grandmother_of(X, arnold),
write('The grandmother of Arnold is '), write(X), nl,
father_of(Y, mark),
write(Y), write(' is the father of Mark'), nl, nl.
However, I was trying to do this:
Define (Add into the database) a rule called is_male(X) that returns "yes" (or "true") if X is the father of somebody.
Note, the system will return a "yes", if it finds a "true" answer and there exist no more true answers. The system will return "true ?" if it finds a "true" answer and there are still possibly further matches. In
this case, if you type "enter", it will return "yes" and stop. If you type ";", it will continue to search for further answers.
1.2 Define a rule called is_female(X) that returns "yes" or "true" if X is the mother of somebody.
1.3 Remove (comment out) the facts that are not essential (have been covered by the new rules is_male/1 and is_female/1) you have added to the database.
So I added this
/* Database for family. It consists of facts and rules. */
/* Facts */
female(ella).
female(jodi).
female(sonya).
male(arnold).
male(chris).
male(louis).
male(mark).
father_of(arnold, chris). /* arnold is the father of chris */
father_of(louis, mark).
father_of(mark, arnold).
mother_of(ella, sonya).
mother_of(jodi, ella).
mother_of(jodi, mark).
/* Rules */
grandmother_of(X, Z) :-
mother_of(X, Y),
(mother_of(Y, Z); father_of(Y, Z)).
is_male(X, Y) :-
father_of(X, Y).
is_female(X, Y) :-
mother_of(X, Y).
familyquestions :-
grandmother_of(X, arnold),
write('The grandmother of Arnold is '), write(X), nl,
father_of(Y, mark),
write(Y), write(' is the father of Mark'), nl, nl.
but when i ran it, it gave me this code: uncaught exception: error(existence_error(procedure,is_male/1),top_level/0)
Upvotes: 1
Views: 5975
Reputation: 18940
The error error(existence_error(procedure,is_male/1),top_level/0)
says that you are calling a unary predicate is_male
, i.e. that you are calling is_male(X)
or something like that. But you defined only a binary predicate is_male
(the arity of predicates is indicated by appending /N
to the name of the N
-ary predicate).
Perhaps you want to define is_male
as:
is_male(X) :- father_of(X, _).
Beware that this rule is not sufficient. You may want to extend it, because there can be persons in your KB which are male, but are not father of someone else.
Once you completed the definition of is_male/1
, you can determine the sex of a person p
by calling is_male(p)
instead of checking the fact male(p)
.
There are some persons for which the fact male(p)
is redundant, because they are also fathers of someone else. You should remove those redundant facts as asked.
Upvotes: 3