introduction to Prolog – sheet – answer

advertisement
El-Shorouk Academy
Higher Institute for Computer &
Information Technology
Dr. Mohamed Mostafa
Acad. Year : 2014 / 2015
Term
: Second
Year
: Thrid
Department of CS
Logic Programming
Introduction to Prolog – Sheet
1. What exactly are facts, rules and queries built out of?
Prolog terms
2- Which of the following sequences of characters are atoms, which are
variables, and which are neither?
1. vINCENT
2. Footmassage
3. variable23
4. Variable2000
5. big_kahuna_burger
6. ’big kahuna burger’
7. big kahuna burger
8. ’Jules’
9. _Jules
10. ’_Jules’
3- Which of the following sequences of characters are atoms, which are
variables, which are complex terms, and which are not terms at all? Give the
functor and arity of each complex term:
1. loves(Vincent,mia)
2. ’loves(Vincent,mia)’
3. Butch(boxer)
4. boxer(Butch)
5. and(big(burger),kahuna(burger))
6. and(big(X),kahuna(X))
7. _and(big(X),kahuna(X))
8. (Butch kills Vincent)
9. kills(Butch Vincent)
10. kills(Butch,Vincent
4- How many facts, rules, clauses, and predicates are there in the following
knowledge base? What are the heads of the rules, and what are the goals they
contain?
woman(vincent).
woman(mia).
man(jules).
person(X) :- man(X); woman(X).
loves(X,Y) :- knows(Y,X).
father(Y,Z) :- man(Y), son(Z,Y).
father(Y,Z) :- man(Y), daughter(Z,Y).
facts: 3
rules: 4
clauses: 7
predicates: 5
5- Represent the following in Prolog:
1. Butch is a killer.
2. Mia and Marcellus are married.
3. Zed is dead.
4. Marcellus kills everyone who gives Mia a footmassage.
5. Mia loves everyone who is a good dancer.
6. Jules eats anything that is nutritious or tasty.
6- Suppose we are working with the following knowledge base:
wizard(ron).
hasWand(harry).
quidditchPlayer(harry).
wizard(X) :- hasBroom(X),hasWand(X).
hasBroom(X) :- quidditchPlayer(X).
How does Prolog respond to the following queries?
1. wizard(ron).
2. witch(ron).
3. wizard(hermione).
4. witch(hermione).
5. wizard(harry).
6. wizard(Y).
7. witch(Y).
7- Which of the following pairs of terms match? Where relevant, give the
variable instantiations that lead to successful matching:
1. bread = bread
-> true
2. ’Bread’ = bread
-> false
3. ’bread’ = bread
-> true
4. Bread = bread
bread -> Bread = bread.
5. bread = sausage
-> false
6. food(bread) = bread
-> false
7. food(bread) = X
-> X = food(bread).
8. food(X) = food(bread)
-> X = bread.
9. food(bread,X) = food(Y,sausage) -> X = sausage, Y = bread.
10. food(bread,X,beer) = food(Y,sausage,X)
-> false
11. food(bread,X,beer) = food(Y,kahuna_burger) -> false
12. food(X) = X
-> X = food(X).
13. meal(food(bread),drink(beer)) = meal(X,Y)
-
> X = food(bread), Y = drink(beer).
14. meal(food(bread),X) = meal(X,drink(beer)) -> false
8- We are working with the following knowledge base:
house_elf(dobby).
witch(hermione).
witch(’McGonagall’).
witch(rita_skeeter).
magic(X):-house_elf(X).
magic(X):-wizard(X).
magic(X):-witch(X).
Which of the following queries are satisfied? Where relevant, give all the
variable instantiations that lead to success.
1. ?- magic(house_elf).
2. ?- wizard(harry).
3. ?- magic(wizard).
4. ?- magic(’McGonagall’).
5. ?- magic(Hermione).
?- magic(house_elf). -> false
?- wizard(harry). -> false
?- magic(wizard). -> false
?- magic('McGonagall'). -> true
?- magic(Hermione). ->
Hermione = dobby;
Hermione = hermione;
Hermione = 'McGonagall';
Hermione = rita_skeeter.
9- Here is a tiny lexicon and mini grammar with only one rule which defines a
sentence as consisting of five words: an article, a noun, a verb, and again an
article and a noun.
word(article,a).
word(article,every).
word(noun,criminal).
word(noun,’big kahuna burger’).
word(verb,eats).
word(verb,likes).
sentence(Word1,Word2,Word3,Word4,Word5) :word(article,Word1),
word(noun,Word2),
word(verb,Word3),
word(article,Word4),
word(noun,Word5).
What query do you have to pose in order to find out which sentences the
grammar can generate? List all sentences that this grammar can generate in the
order Prolog will generate them. Make sure that you understand why Prolog
generates them in this order.
sentence(A, B, C, D, E). -> generates all possibilities:
The following are sample of possibilities:
A=a
A = every
B = criminal
B = criminal
C = eats
C = eats
D=a
D=a
E = criminal
E = big kahuna burger
A=a
B = criminal
C = eats
D = every
E = big kahuna burger
10- Given the following prolog statements, give answers for the following
queries?
has(jack,apples).
has(ann,plums).
has(dan,money).
fruit(apples).
fruit(plums).
What has Jack?
has(jack,apples).
Does Jack have something?
?- has(jack,_).
Who has apples and who has plums?
?- has(X,apples),has(Y,plums).
Does someone have apples and plums?
?- has(X,apples),has(X,plums).
Has Dan fruits? Does someone have something else?
?- has(dan,X),fruit(X).
?- has(X,Y),not fruit(Y).
11- Here are six English words:
abalone, abandon, anagram, connect, elegant, enhance.
They are to be arranged in a crossword puzzle like fashion
in the given grid given below.
The following knowledge base represents a lexicon
containing these words.
word(abalone,a,b,a,l,o,n,e).
word(abandon,a,b,a,n,d,o,n).
word(enhance,e,n,h,a,n,c,e).
word(anagram,a,n,a,g,r,a,m).
word(connect,c,o,n,n,e,c,t).
word(elegant,e,l,e,g,a,n,t).
Write a predicate crosswd/6 that tells us how to fill the grid, i.e. the first three
arguments should be the vertical words from left to right and the following
three arguments the horizontal words from top to bottom.
crossword(V1,V2,V3,H1,H2,H3) :word(H1,_,H12V12,_,H14V22,_,H16V32,_),
word(H2,_,H22V14,_,H24V24,_,H26V34,_),
word(H3,_,H32V16,_,H34V26,_,H36V36,_),
word(V1,_,H12V12,_,H22V14,_,H32V16,_),
word(V2,_,H14V22,_,H24V24,_,H34V26,_),
word(V3,_,H16V32,_,H26V34,_,H36V36,_)
?- crosswd(H1,H2,H3,V1,V2,V3). ->
H1 = abandon
H2 = elegant
H3 = enhance
V1 = abalone
V2 = anagram
V3 = connect ?
12- Given the following prolog statements, give answers for the following
queries?
parent(abraham,ismael).
parent(abraham,isaac).
parent(isaac,esau).
parent(isaac,iacob).
grandfather(B,N):- parent(B,P),parent(P,N).
brother(F1,F2):- parent(P,F1),parent(P,F2), \F1=F2.
descendent(X,Y):- parent(X,Y).
descendent(X,Y):- parent(X,Z),descendent(Z,Y).
List of Abraham’s Children?
?- parent(abraham,X).
Did Abraham have children?
?- parent(abraham,_).
Who is the father of Esau?
?- parent(Father,esau).
All the pairs father-son from the data base?
?- parent(F,S).
Does Abraham grandfather?
?- parent(abraham,X),parent(X,Grandson).
List of Abraham’s Grandsons?
?- grandfather(abraham,iacob).
Does Esau descent of Abraham?
?- descendent(abraham,esau).
13- Write a prolog program that encodes the relationships in the Simpson’s
family and can answer the questions:
Is X the [father | mother | parent] of Y?
Is X the [son | daughter |child] of Y?
Extend your program to handle the following queries:
Is X the grandparent of Y?
Is X an ancestor of Y?
List the [ancestors | parents | children] of X.
List all the grandparent-grandchild pairs in the domain.
% Facts
homer.
marge.
bart.
lisa.
maggie.
mona.
jacqueline.
patty.
abraham.
clancy.
hugo.
louise.
herb.
woman(marge).
woman(maggie).
woman(lisa).
woman(mona).
woman(clancy).
woman(jacqueline).
woman(selma).
woman(patty).
woman(louise).
man(homer).
man(bart).
man(abraham).
man(clancy).
man(hugo).
man(herb).
ancestor(homer,bart).
ancestor(homer,lisa).
ancestor(homer,maggie).
ancestor(marge,bart).
ancestor(marge,lisa).
ancestor(marge,maggie).
ancestor(abraham, homer).
ancestor(mona, homer).
ancestor(clancy, marge).
ancestor(clancy, patty).
ancestor(clancy, selma).
ancestor(jacqueline, marge).
ancestor(jacqueline, patty).
ancestor(jacqueline, selma).
ancestor(abraham, herb).
ancestor(herb, hugo).
ancestor(louise, hugo).
% Rules
father(A,B) :- man(A), ancestor(A,B).
mother(A,B) :- woman(A), ancestor(A,B).
is_father(A) :- father(A,_).
is_mother(A) :- mother(A,_).
son(A,B):- man(A), ancestor(B,A).
son(A,B):- woman(A), ancestor(B,A).
brother(X,Y) :ancestor(Z,X),
ancestor(Z,Y),
X\=Y.
brother_complete(A,B) :father(P,A), father(P,B),
mother(M,A), mother(M,B),
A\=B.
uncle(T,A) :man(T),
brother(T,X), ancestor(X,A).
uncle(T,A) :woman(T),
brother(T,X), ancestor(X,A).
cousin(A,B) :man(A),
ancestor(X,A),
ancestor(Y,B),
cousin(X,Y).
cousin(A,B) :woman(A),
ancestor(X,A),
ancestor(Y,B),
brother(X,Y).
grandfather(A,B) :- father(A,X), father(X,B).
gransfather(A,B) :- father(A,X), father(X,B).
Download