(Clocksin, Clause and Effect, worksheet 1)
Consider this Prolog program:
male(honza). male(karel). female(eliska). female(katka). dance(P, Q) :- male(P), female(Q).
What answers will these queries produce, and in what order?
dance(karel, X).
dance(jakub, radka).
dance(katka, X).
dance(X, eliska).
dance(X, X).
dance(honza, eliska).
dance(X, jana).
dance(X, Y).
(Clocksin, worksheet 2)
Consider this Prolog program:
drinks(tomas, whiskey). drinks(sonya, beer). drinks(lucie, wine). drinks(radek, beer). drinks(jarda, beer). pair(P, Q, D) :- drinks(P, D), drinks(Q, D).
What answers will these queries produce, and in what order?
pair(P, tomas, whiskey).
pair(sonya, jarda, beer).
pair(tomas, sonya, beer).
pair(radek, radek, beer).
pair(X, Y, beer).
pair(X, Y, Z).
(Clocksin, worksheet 4)
Consider an acyclic directed graph defined as follows:
edge(g, h). edge(g, d). edge(e, d). edge(h, f). edge(e, f). edge(a, e). edge(a, b). edge(b, f). edge(b, c). edge(f, c).
Write a predicate path(V, W)
that is true if there is some path from V to W in the directed
graph.
Given your predicate, what answers will each of these queries produce?
path(f, f).
path(a, c).
path(g, e).
path(g, X).
path(X, h).
path(X, X).
path(X, Y).
Suppose that we define a family tree using
predicates male
, female
and parent
:
male(jan). male(karel). male(vaclav). female(anna). female(eliska). parent(vaclav, anna). parent(vaclav, karel). parent(karel, jan). parent(karel, eliska). ...
Write Prolog predicates expressing the following relationships:
grandmother
sibling
Two people are siblings if they have at least one parent in common.
full_sibling
Full siblings share both parents.
first_cousin
,
second_cousin
First cousins have parents who are full siblings. Second cousins have parents who are first cousins.
cousin
Two people are cousins if they are Nth cousins for any N ≥ 1.
Consider a map that shows 7 countries: the Czech Republic, Slovakia, Germany, Poland, Austria, Hungary, and Ukraine. Is it possible to color this map with 3 colors so that no bordering countries have the same color? Write a Prolog program that can answer this question.
Suppose that we'd like to fill in a 3 x 3 grid with letters so that every row and column contains one of these words:
AGE, AGO, CAN, CAR, NEW, RAN, ROW, WON
Write a Prolog program that can find all possible solutions.
Consider the following tiny Minesweeper board of dimensions 5 x 2:
1? 2? 2? 2? 1?
A number N means that there are N mines in adjacent squares (which may be adjacent horizontally or diagonally).
Write a Prolog program that can find all possible positions for the mines.
(Levesque, Thinking as Computation, 5.5)
Write a Prolog program that can solve the following puzzle.
Katka, Maria and Roman have distinct occupations and play distinct musical instruments. Their occupations are doctor, lawyer, and teacher, and the instruments they play are piano, flute, and violin. Also:
Maria lives next to the doctor.
The lawyer plays the piano.
Maria is not the teacher.
Katka is a patient of the violinist.
Who plays the flute?