Write a Prolog program by creating a file that contains the following facts: fem
ID: 3692341 • Letter: W
Question
Write a Prolog program by creating a file that contains the following facts:
female(pam).
female(liz).
female(ann).
female(pat).
male(tom).
male(bob).
male(jim).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
• here the predicate parent(X,Y) means X is the parent of Y
(a) Load this file into Prolog, usually this is done with the consult file predicate:
?- consult(‘<filename>’).
On Windows you can load the fact database with the menu point File Consult. Once you have loaded the program pose the following queries:
?- female(ann).
?- female(jim).
?- parent(X,bob).
?- parent(tom,X).
?- parent(X,ann),parent(X,pat).
What are the answers to these queries? Beware, for some queries here might be more than one answer. To get all the answers type a ';' at each prompt until you reach the next query prompt ?-.
(b) Now, using the parent predicate formulate the following Prolog queries:
1. Who is Pat's parent?
2. Does Liz have a child?
3. Who is Pat's grandparent?
(c) Given the above facts, extend the program by writing rules defining the following predicates:
sister(X,Y) % X is the sister of Y.
son(X,Y) % X is the son of Y.
father(X,Y) % X is the father of Y.
grandmother(X,Y) %X is the grandmother of Y.
ancestor(X,Y) % X is an ancestor of Y. See textbook p.392.
(Hint: this predicate might come in handy: different(X,Y):- not(X=Y). Some predicate definitions might be recursive.)
Demonstrate that your program works by posing the following queries:
4. ?- sister(X,pat).
5. ?- sister(X,Y).
6. ?- son(jim,X).
7. ?- father(X,bob).
8. ?- grandmother(X,ann).
9. ?- ancestor(X,jim).
Explanation / Answer
female(pam). % pam is female
female(liz).
female(ann).
female(pat).
male(tom). %tom is male
male(bob).
male(jim).
parent(pam,bob). %pam is parent of bob
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
sister(X,Y) % X is the sister of Y.
son(X,Y) % X is the son of Y.
father(X,Y) % X is the father of Y.
grandmother(X,Y) %X is the grandmother of Y.
ancestor(X,Y) % X is an ancestor of Y.
?- female(ann). /* ann is female? */
yes
?- female(jim). /* jim is female? */
no
?- parent(X,bob).
X=tom
X=pam
yes
?- parent(tom,X).
X=bob
X=liz
yes
?- parent(X,ann).
X=bob
yes
?- parent(X,pat).
X=bob
yes
/*Who is Pat's parent*/
?-parent(X,pat).
/* Does Liz have a child */
?-parent(liz,X).
/*Who is Pat's grandparent*/
?-parent(X,pat), parent(Y,X).
?- sister(X,pat). % X is siter of pat
?- sister(X,Y). % X is siter of Y ?
?- son(jim,X). %jim is son of X?
?- father(X,bob). %X father of bob
?- grandmother(X,ann). %X grand mother of ann
?- ancestor(X,jim).female(pam). % pam is female
female(liz).
female(ann).
female(pat).
male(tom). %tom is male
male(bob).
male(jim).
parent(pam,bob). %pam is parent of bob
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
sister(X,Y) % X is the sister of Y.
son(X,Y) % X is the son of Y.
father(X,Y) % X is the father of Y.
grandmother(X,Y) %X is the grandmother of Y.
ancestor(X,Y) % X is an ancestor of Y.
?- female(ann). /* ann is female? */
yes
?- female(jim). /* jim is female? */
no
?- parent(X,bob).
X=tom
X=pam
yes
?- parent(tom,X).
X=bob
X=liz
yes
?- parent(X,ann).
X=bob
yes
?- parent(X,pat).
X=bob
yes
/*Who is Pat's parent*/
?-parent(X,pat).
/* Does Liz have a child */
?-parent(liz,X).
/*Who is Pat's grandparent*/
?-parent(X,pat), parent(Y,X).
?- sister(X,pat). % X is siter of pat
?- sister(X,Y). % X is siter of Y ?
?- son(jim,X). %jim is son of X?
?- father(X,bob). %X father of bob
?- grandmother(X,ann). %X grand mother of ann
?- ancestor(X,jim). %X is ancestor of jim
note-the above example can help to answer the given question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.