Week 1: Exercises

1. Mortal Men

  1. Write Prolog clauses expressing the following facts:

  2. Write Prolog queries asking the following questions:

2. Single Clause

  1. Write a single clause that is equivalent to all the of the following facts:

      parent(charles, wenceslaus).
      parent(charles, margaret).
      parent(charles, sigismund).
  2. Write a single clause that is equivalent to all the of the following facts:

parent(charles, wenceslaus).
parent(charles, margaret).
parent(charles, sigismund).
parent(sigismind, elizabeth).

3. Dancing Pairs

Consider this Prolog program:

male(hans).
male(charles).

female(elizabeth).
female(kate).

dance(P, Q) :- male(P), female(Q).

What answers will these queries produce, and in what order?

  1. dance(charles, X).
  2. dance(jacob, radka).
  3. dance(kate, X).
  4. dance(X, elizabeth).
  5. dance(X, X).
  6. dance(hans, elizabeth).
  7. dance(X, john).
  8. dance(X, Y).

4. Drinking Pairs

Consider this Prolog program:

drinks(thomas, whiskey).
drinks(sonya, beer).
drinks(lucy, 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?

  1. pair(P, thomas, whiskey).
  2. pair(sonya, jarda, beer).
  3. pair(thomas, sonya, beer).
  4. pair(radek, radek, beer).
  5. pair(X, Y, beer).
  6. pair(X, Y, Z).

5. Termination

Consider this predicate:

  1. Consider the following queries. What answer(s) will they produce? Will they terminate?

  1. Suppose that we move the rule to the top:

    foo(X) :- foo(X).
    foo(a).
    foo(b).


    Now what answer(s) will be produced by the query 'foo(Y)'?

6. Directed Graph

Consider this acyclic directed graph:

We can write a Prolog predicate representing adjacency between vertices:

edge(a, b).
edge(a, e).
edge(b, c).
edge(b, f).
edge(e, d).
edge(e, f).
edge(f, c).
edge(g, d).
edge(g, h).
edge(h, f).
  1. Write a predicate path(V, W) that is true if there is some path from V to W in the directed graph.

  2. Given your predicate, what answers will each of these queries produce?

7. Map of Europe

Consider a map that shows 7 countries in central Europe:

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.

8. Occupations and Instruments

Write a Prolog program that can solve the following puzzle.

Kate, 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:

  1. Maria lives next to the doctor.

  2. The lawyer plays the piano.

  3. Maria is not the teacher.

  4. Kate is a patient of the violinist.

Who plays the flute?