1. Prolog programming exercises
1.
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).fact
woman(mia). fact
man(jules). fact
person(X) :- man(X); woman(X).Rule
loves(X,Y) :- knows(Y,X). Rule
father(Y,Z) :- man(Y), son(Z,Y). Rule
father(Y,Z) :- man(Y), daughter(Z,Y). Rule
2.
Represent the following in Prolog:
1. Butch is a killer.
Killer(Butch)
2. Mia and Marcellus are married.
Married(Mia, Marcellus).
3. Zed is dead.
Dead(Zed)
4. Marcellus kills everyone who gives Mia a foot massage.
Kills(Marcellus, gives(Mia, foot massage))
5. Mia loves everyone who is a good dancer.
Loves(Mia,X :- good dancer(X ))
6. Jules eats anything that is nutritious or tasty.
Eats(Jules,X):- nutritious(X); tasty(X)
3.
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).
2. How does Prolog respond to the following queries?
1. wizard(ron).true
2. witch(ron).false
3. wizard(hermione). false
4. witch(hermione). false
5. wizard(harry). false
6. wizard(Y). true
7. witch(Y). true
4.
Which of the following pairs of terms match? Where relevant, give the
Variable instantiations that lead to successful matching.
1. bread = bread
yes
2. ’Bread’ = bread
No
3. ’bread’ = bread
4. Bread = bread
Yes
Yes, Bread = bread
5. bread = sausage
No
6. food(bread) = bread
No
7. food(bread) = X
Yes,
8. food(X) = food(bread)
Yes,
X = food(bread)
X = bread
9. food(bread,X) = food(Y,sausage)
Yes, X = sausage, Y = bread
10. food(bread,X,beer) = food(Y,sausage,X) No
11. food(bread,X,beer) = food(Y,kahuna_burger)
12. food(X) = X
No
Yes, X = food(**)
13. meal(food(bread),drink(beer)) = meal(X,Y)
Yes, X = food(bread),
Yes, X = food(bread),
14. meal(food(bread),X) = meal(X,drink(beer)) No
5.
Exercise 2.2 We are working with the following knowledge base:
house_elf(dobby).
3. 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). No, Error: undefined procedure wizard/1
2. ?- wizard(harry). No, Error: undefined procedure wizard/1
3. ?- magic(wizard). No, Error: undefined procedure wizard/1
4. ?- magic(’McGonagall’). No, Error: undefined procedure wizard/1
5. ?- magic(Hermione). Yes, Error: undefined procedure wizard/1
6.
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.
a
a
a
a
a
criminal
criminal
criminal
criminal
criminal
eats a criminal
eats a big kahuna burger
eats every criminal
eats every big kahuna burger
likes a criminal
4. a criminal likes a big kahuna burger
a criminal likes every criminal
a criminal likes every big kahuna burger
a big kahuna burger eats a criminal
a big kahuna burger eats a big kahuna burger
a big kahuna burger eats every criminal
a big kahuna burger eats every big kahuna burger
a big kahuna burger likes a criminal
a big kahuna burger likes a big kahuna burger
a big kahuna burger likes every criminal
a big kahuna burger likes every big kahuna burger
every criminal eats a criminal
every criminal eats a big kahuna burger
every criminal eats every criminal
every criminal eats every big kahuna burger
every criminal likes a criminal
every criminal likes a big kahuna burger
every criminal likes every criminal
every criminal likes every big kahuna burger
every big kahuna burger eats a criminal
every big kahuna burger eats a big kahuna burger
every big kahuna burger eats every criminal
every big kahuna burger eats every big kahuna burger
every big kahuna burger likes a criminal
every big kahuna burger likes a big kahuna burger
every big kahuna burger likes every criminal
every big kahuna burger likes every big kahuna burger
7.
Write natural language sentences that represent what these Prolog facts might convey to a
human reader. (Remember that, to the computer, these facts are simple pieces of information
that can be used for matching answers to questions.)
1.
likes(jeff, painting).
2.
male(john).
3.
building("Empire State Building", new_york).
4.
person(roslin, jeanie, "1429 East Sutter St.",
"Scotts Valley", "CA", 95066).
Write Prolog programming exercises facts that represent the following natural language
statements:
Helen likes pizza.
San Francisco is in California.
Amy's telephone number is 476-0299.
Len's father is Alphonso Grenaldi.
8.
Write natural-language sentences corresponding to the following Prolog programming
exercises rules:
eats(Who, What):- food(What), likes(Who, What).
pass_class(Who):- did_homework(Who), good_attendance(Who).
5. does_not_eat(toby, Stuff):- food(Stuff), greasy(Stuff).
owns(Who, What):- bought(Who, What).
Write Prolog programming exercises rules that convey the meaning of these natural-language
sentences:
a.
A person is hungry if that person's stomach is empty.
b.
Everybody likes a job if it's fun and it pays well.
c.
Sally likes french fries if they're cooked.
d.
Everybody owns a car who buys one, pays for it, and keeps it.
9.
Create an elementary Prolog knowledge base, describing following relations:
1.
2.
3.
4.
5.
John, Fred and Harry are men, Mary, Julie, Susan and Anne are women.
John has blonde hair while Fred and Harry have dark hair.
Julie and Susan are blonde, Mary and Anne are brunette.
Rich is each person who owns the gold - Fred and Julie in our example.
Male like only female and vice versa. Moreover, John and Harry like rich
persons, John likes blonde and Fred likes brunette.
6. Both Mary and Julie like dark hair persons, Julie likes rich persons at the same
time.
7. Anne owns a house and John owns a car.
and ask it:
a. Who is male ?
b. Who does John like ?
c. Who does Mary like ?
d. Does Julie like anyone ?
e. Is there a couple who like each other ?
10.
What is the difference between the following two rules?
!
blah :- a(X) , b(X).
blah :- a(_) , b(_).
11.
Towers of Hanoi
The solution to the Towers of Hanoi puzzle is a classic example of recursion. The
ancient puzzle of the Towers Of Hanoi consists of a number of wooden disks mounted
on three poles, which are in turn attached to a baseboard. The disks each have
6. different diameters and a hole in the middle large enough for the poles to pass
through. In the beginning, all the disks are on the left pole as shown in:
The object of the puzzle is to move all the disks over to the right pole, one at a time,
so that they end up in the original order on that pole. You can use the middle pole as a
temporary resting place for disks, but at no time is a larger disk to be on top of a
smaller one. It's easy to solve the Towers of Hanoi with two or three disks, but the
process becomes more difficult with four or more disks.
7. 12.
Hardware Simulation
Every logical circuit can be described with a Visual Prolog predicate, where the
predicate indicates the relationship between the signals on the input and output
terminals of the circuit. The fundamental circuits are described by giving a table of
corresponding truth values (see the and_, or_, and not_ predicates in Program 4).
Fundamental circuits can be described by indicating the relationships between the
internal connections, as well as the terminals. To see how this works, construct an
exclusive OR circuit from AND, OR, and NOT circuits, and then check its operation
with a Visual Prolog program. The circuit is shown in: