Prolog is ... a tool for thinking. – David H.D. Warren, Foreward to The Art of Prolog, 1986

This week, we continue our work with Prolog using Learn Prolog Now! and SWI Prolog.

More Prolog

As one would imagine, Prolog supports recursion, lists and arithmetic.

Exercise 12.1

Do the following exercises.

  1. Do these exercises from LPN!.
    1. Exercise 3.2
    2. Exercise 4.5
    3. Exercise 5.3
  2. Does Prolog implement a version of generalized modus ponens? If so, demonstrate how it’s done; if not, explain why not. If it doesn’t, can you implement one? Why or why not?

  3. Have you ever seen anything similar to the “successor” rule discussed in LPN!’s Chapter 3.1, Example 3? Where? What was it used for?

Be sure that you can explain how you built your system and how Prolog does recursion.

As time allows, try the following extensions to the first exercises.

Exercise 12.2

Extend the LPN! exercises above as follows.

  1. Exercise 3.2 - Modify your code to print the containment hierarchy as shown here:
    ?- in(katarina, olga, L).
    L = contains(katarina, olga)					
    
    ?- in(katarina, irina, L).
    L = contains(katarina, olga, contains(olga, natasha, contains(natasha, irina)))					
    					

    These examples produce a “stack trace” of the recursive executation. The first example is a base case, the second a recursive case.

  2. Exercise 4.5 - Modify your code to translate into either English or Chinese. The Chinese numbers are yi, er, san, si, wu, liu, qi, ba, jiu. Your output should look something like this:
    ?- listtran3([eins,zwei],2,X).
    X = [one, two]
    
    ?- listtran3([eins,zwei],3,X).
    X = [yi, er].
    
    ?- listtran3(X,3,[yi, er]).
    X = [eins, zwei].
    
    ?- listtran3(X,2,[one, two]).
    X = [eins, zwei].
    					

    Simple extensions of your original code will likely only support these language pairs; you don’t need to support Chinese-English translations.

  3. Exercise 5.3 - Add a new addN/3 predicate that adds an arbitrary value to each list element as specified by a numeric parameter.
    ?- addN([1,2,3],2,X).
    X = [3, 4, 5].				
    					

Checking in

Submit the files specified above in Moodle under lab 12.

Back to the top