☰

A "Hands-on" Introduction to
Pure Logic Programming (Pure Prolog)

Course Notes


These are the notes for the Pure Logic Programming part of the Computational Logic course. The slides and notes go essentially in sync, with these notes providing the explanations, reasoning, and some side comments.

How to use this interactive document:

The code boxes can be edited and run in place. Feel free to edit any program and make all the queries you want!

  • Programs are loaded when the page opens: a loaded program has a check mark (✔) in its top right corner. If you edit a program, the mark turns into a right-pointing triangle (▶): press it to load your version.
  • Queries are the boxes that start with ?-. Press the ▶ of a query box to run it against the programs above it. While an answer ends in ? there may be more: press Next (or type ; and Enter) for another one, or Stop.
  • Expected answers are printed in plain text underneath each query box, so you can read the notes straight through without running anything.
  • Exercises are opened by the corresponding link. In an exercise, the code to complete is marked with a question mark (?), which checks your solution when pressed.
  • Overall, the northeast pointing arrow (↗) of a program cell opens it in a separate Prolog playground window.

Syntax

This is a hands-on introduction to pure logic programming. There are many ways to start; we take the most direct one — give the syntax, explain the operational semantics in the shortest way possible, and get to programming as soon as we can.

Terms: variables, constants and structures (Slide 2)

We use the Prolog notation conventions, which are used by essentially all logic programming systems.

Variables start with an uppercase character or an underscore, and may contain underscores and digits: X, Im4u, A_little_garden, _, _x, _22.

The main thing to remember: if it starts with a capital letter, it is a variable. If it starts with a lowercase letter, it is a constant.

A variable whose name would otherwise start with a lowercase letter can be written with an underscore in front, as in _x. And _ on its own is special: it is the anonymous variable. Many other languages have it too, and it came originally from Prolog. Each occurrence of it is a variable that is different from every other variable. It is used where a variable is needed in some position but is not shared with anything else, so that there is no need to give it a name.

Constants have a lowercase first character and may contain underscores: a, dog, a_big_cat. Numbers are constants too: 23, and also floating point numbers, which we will see later. A constant that starts with a capital letter cannot be written directly, because it would be a variable: it has to be quoted, as in 'Hungry man'.

Many symbols are constants as well: pretty much anything that does not start with an underscore or a capital letter. For example [], the empty list, is a constant, no different from a: it just has two characters.

Structures consist of a functor (the name of the structure, which looks like a constant) followed by a fixed number of arguments in parentheses, separated by commas:

date(monday, Month, 1994)
The functor is date and there are three arguments: monday, Month and 1994. We can think of it as a piece of memory that starts with date and then holds three things. The arguments can in turn be variables, constants, or again structures, so structures can be nested to any depth.

Arity is the number of arguments. date(monday, Month, 1994) has arity 3. Functors are usually written name/arity, so this one is date/3; the arity matters in many contexts.

A nice consequence is that constants can be seen as functors of arity zero, with no arguments. This blends the two ideas elegantly: a constant is just a structure that happens to have no arguments. So the constant a is the functor a/0.

More on terms (Slide 3)

Variables, constants and structures are, taken together, called terms. They are the terms of a first-order language, exactly as in logic. But they are also the data structures of a logic program: every data structure used in a logic program is a term, and there is nothing else. Everything is built with variables, constants and structures: the numbers, the lists, absolutely everything.

Some examples, and one that is not allowed:

term what it is
dada constant, i.e., the functor dad/0
time(min, sec)a structure, functor time/2
tiger(Hobbes)a structure, functor tiger/1
Tee(Alf, rob)ILLEGAL
A_good_timea variable

Why is Tee(Alf, rob) illegal? Because the functor starts with a capital letter, so it would be a variable, and a variable in functor position takes us out of first-order logic and into higher-order logic. (In Ciao this is actually possible, because Ciao supports higher order, but that comes much later.)

Operators

A functor can be declared to be a prefix, postfix or infix operator (we will see how later), which allows writing it without the parentheses.

Take +. It is a constant, so it can be a functor, +/2. If it is declared as an infix operator, we can write

a + b        which is exactly the term        '+'(a,b)
The quotes may or may not be needed, depending on the context. Likewise, if - is declared prefix then - b is '-'(b), and if < is declared infix then a < b is '<'(a,b). And if father were declared infix, we could write john father mary for father(john, mary).

The operator only changes how the term is written and read. This is easy to check at the top level:

?- X = a+b.
Expected answer:

X = a+b ?
Here a is a constant, and a+b is simply a structure. The same structure can also be written in its canonical form:

?- X = +(a,b).
Expected answer:

X = a+b ?
The answer is the same: we typed the canonical form and the system printed the operator form, because + is declared infix, and printing uses the operator declarations to make the output easier to read.

The same thing with write/1, which is perhaps clearer because no variable is involved:

?- write(+(a,b)), nl.
Expected answer:

a+b
?- write(foo(a,b)), nl.
Expected answer:

foo(a,b)
Since foo is not declared as an operator, it is printed in the ordinary way. Reading works in the same way: write(a+b) is fine, but:

?- write(a foo b).
Expected answer:

{SYNTAX ERROR: Malformed query}
This is a syntax error, because foo is not an infix operator. The Ciao top level in a terminal says where the problem is, exactly where foo appears:

{SYNTAX ERROR: (lns 2-1) , or ) expected in arguments
write( a
** here **
foo b ) .
}

From here on we assume that the usual operator definitions (+, -, and the rest of the standard ones) are always preloaded.

This is the whole syntax of data: the data structures of the language, and how they are written. It is simple, but it is worth understanding it perfectly.

Rules and facts (clauses) (Slide 4)

Now for the programs, which are made of rules and facts.

A rule has this shape:

p₀(t₁, ..., tₙ) :- p₁(...), p₂(...), ..., pₘ(...).
The :- is an arrow written backwards. Everything in a rule looks like a term: p₀ is a functor (although with a special name, predicate symbol) and the ts are its arguments, which may be variables, constants or structures. The predicate symbols themselves cannot be variables: they have to be constants.

The vocabulary:

  • p₀(t₁, ..., tₙ) is the head of the rule.
  • Each pᵢ(...) to the right of the arrow is a literal.
  • The set of all those literals is the body.
  • The :- is the neck.
This is where the connection with programming starts: the literals are also called procedure calls. Literal is the name used in logic and procedure call the name used in programming. We will use both throughout, because the whole subject lives in the overlap between the two.

A fact is a special kind of rule: one with an empty body. When the body is empty the arrow is not written either.

meal(soup, beef, coffee).                    % a fact

meal(First, Second, Third) :-                % a rule
    appetizer(First),
    main_dish(Second),
    dessert(Third).
Facts are things that are true. Note that each of these looks like a structure, that is, like a term.

Facts and rules together are called clauses. The piece of program above has two clauses: a fact and a rule.

Predicates, programs and queries (Slide 5)

A predicate (a procedure definition, in programming terms) is a set of clauses whose heads have the same name and arity.

pet(X) :- animal(X), barks(X).
pet(X) :- animal(X), meows(X).
pet(X) :- animal(X), roars(X), small(X).

animal(tim).
animal(spot).
animal(hobbes).
How many predicates are there here? The tempting answer is three, and it is wrong: there are two. The first three clauses all have the predicate name pet and arity one, so they are one predicate. The last three are facts (and facts are clauses too) with the predicate name animal and arity one, so they are a second one.

Two predicates means two procedures, like f and g in an imperative language. pet/1 is a procedure with three cases. It is a bit like a case statement, but it is a procedure, and it is called by its name with arguments, exactly as in other languages.

Note that a different number of arguments means a different procedure: foo/1 and foo/2 are two unrelated predicates that happen to share a name.

A program is a set of predicates. The one above is a program composed of two predicates, each of them with three clauses: pet/1 has three rules, and animal/1 has three facts.

This terminology is worth learning well, because it is used constantly from here on.

Queries

The last element is the query: a clause with no head, just the arrow and a body:

:- pet(X).
It is a question put to the program. Most systems write it with a question mark, ?-, to make this even clearer, and that is what the Ciao prompt is:

?- pet(X).
This prompt is not arbitrary. It is not a $ or a > chosen because it looked good: it has a logical meaning. A question is asked by writing a body with no head, and a body without a head is something to be proved.

For readers who have seen resolution theorem proving: to prove something we first negate it and then derive a contradiction. The negation is why the goal is on that side of the arrow. The question mark is the negated conclusion.

The declarative meaning of facts and rules (Slide 6)

What does all this mean? What does a program like the one above say?

It means two things, and this is something that we will meet all the time: there is a logical view and a procedural, or program, view. A program is at the same time a piece of logic and something that runs. From the point of view of logic, what it says is called its declarative meaning.

Facts state things that are true. Recall that a fact has an empty body, and an empty body is the same as having true there, which needs no proof. So a fact says p if nothing: it is true by itself, with no conditions. When we write animal(spot) we are saying "spot is an animal".

In rules, the commas are conjunction, and :- is classical implication. So

p :- p₁, ..., pₘ.        means        p ← p₁ ∧ ... ∧ pₘ
If p₁ and p₂ and ... and pₘ are true, then p is true. Or, reading it backwards, p is true if all of them are.

So pet(X) :- animal(X), barks(X). is read as "X is a pet if it is an animal and it barks". This is exactly what it means from the logical point of view.

Variable scope is important, and easy to get wrong. Variables are local to clauses, exactly as variables in an imperative language are local to procedures. In

pet(X) :- animal(X), barks(X).
pet(X) :- animal(X), meows(X).
the X in the first clause is not the same X as in the second. They are two different variables that happen to be written the same way.

And every time a clause is used, we can imagine that we get fresh copies of its variables, just as calling a procedure gives a new activation record with fresh local variables. Readers who have seen resolution will recognize this as the renaming that is applied to a clause before using it, so that the variables of one application are not confused with those of another.

The declarative meaning of predicates and queries (Slide 7)

A predicate is a set of facts and rules, and its different clauses provide different ways of defining p, that is, different alternatives. So the clauses of a predicate are read as a disjunction: p holds if the body of the first clause holds, or that of the second, or that of the third.

X is a pet if it is an animal and it barks, or X is a pet if it is an animal and it meows: two alternative ways of being a pet.

The same reading applies to a table of facts. animal(tim). animal(spot). animal(hobbes). says that there are three ways of being an animal or, equivalently, that there are three individuals who are animals. Again, these are alternatives.

A query asks whether something follows from the program and, if it does, for which values of its variables.

"Execution" and semantics (Slide 8)

Here is a complete logic program, the pets database, with two ways of being a pet, three animals, and three noises:

:- module(_,_,[]).

pet(X) :-
    animal(X),
    barks(X).
pet(X) :-
    animal(X),
    meows(X).

animal(tim).
animal(spot).
animal(hobbes).

barks(spot).

meows(tim).

roars(hobbes).
Executing a logic program, given a program and a query, is attempting to find an answer to the query, and giving values for the variables in it.

So if we ask pet(X) (is there an X that is a pet? who is a pet?), the system tries to find a substitution for X that makes pet(X) true.

Just by looking at the program we can see the answers. An animal that barks is a pet, and we have animal(spot) and barks(spot), so spot is certainly an answer. An animal that meows is also a pet, and we have animal(tim) and meows(tim), so tim is another one.

Note that we have found the solutions without running the program: we only had to look at what the logic implies (at the model of the program). This is the declarative semantics, which specifies what should be computed: all the possible answers.

The operational semantics is the other question: how are those answers computed? How does the program run? (This is also what allows us to determine how many steps the computation takes.) The distinction between the logical meaning and the mechanism that computes it is one that we will keep returning to.

The program running

?- barks(X).
Expected answer:

X = spot ? ;
no
spot barks, and nobody else does: the no means that there are no more answers.

?- animal(X).
Expected answer:

X = tim ? ;
X = spot ? ;
X = hobbes ? ;
no
There are three animals, and no more. We can also ask about a particular individual, rather than for all of them:

?- pet(tim).
Expected answer:

yes
?- pet(hobbes).
Expected answer:

no
?- pet(X).
Expected answer:

X = spot ? ;
X = tim ? ;
no
spot is a pet and tim is a pet, and furthermore nobody else is: exactly what we worked out by reading the logic.

The declarative semantics already told us the answers, without running anything. The operational semantics answers a different question: how does the system compute them? From here on, every topic has both readings.

hobbes roars, and there is no clause saying that an animal that roars is a pet, so hobbes is not one. Try adding pet(X) :- animal(X), roars(X). to the program above, loading it again, and running the last query: a third answer appears.

Running programs, and the top-down operational meaning (Slides 9–10)

Programs are run by interacting with the query evaluator of the system, the top level: we load the program and then type queries at the ?- prompt. For the pets program, saved in a file pets.pl:

Ciao X.Y-...
?- use_module(pets).
yes
?- pet(spot).
yes
?- pet(X).
X = spot ? ;
X = tim ? ;
no
?-
In these notes the programs are loaded when the page opens, and the query boxes play the role of the prompt. The part on "Developing Programs with a Prolog System" explains in more detail how to work with the particular system used in the course, Ciao.

The programs of this part start with :- module(_,_,[]). or with :- module(_,_,[sr/bfall]).. The third argument is the list of packages that the module loads. With an empty list none of the Prolog built-ins is available, so the program is pure logic programming, which is what this part of the course is about. The sr/bfall package selects in addition a different search rule, breadth-first, which is explained later.

Operationally, a logic program is a set of procedure definitions (the predicates), and a query ?- p. is an initial procedure call.

A procedure definition with one clause, p :- p₁, ..., pₘ., means: to execute a call to p, call p₁ and ... and pₘ. In principle, the order in which these are called does not matter, but in practical systems it is fixed.

If the procedure has several clauses, that is, several definitions,

p :- p₁, ..., pₙ.
p :- q₁, ..., qₘ.
...
it means: to execute a call to p, call p₁ and ... and pₙ or, alternatively, q₁ and ... and qₘ, or ... This is unique to logic programming: it is like having several alternative procedure definitions. It means that there may be several possible paths to a solution, and that they should be explored. The system usually stops when it finds the first solution, and the user can then ask for more. Again, in principle the order in which these paths are explored does not matter (if certain conditions are met), but in a given system it is typically fixed too.

A little more precisely: given a query, the system takes its leftmost literal (a procedure call) and looks for a clause whose head matches it. Matching here is not equality: it is unification, the subject of the next section. If a matching clause is found, the call is replaced by the body of that clause, with the matching substitution applied, and execution carries on. When there is nothing left to prove, the query has succeeded, and the accumulated substitution is the answer.

If a call has several matching clauses there is a choice point: the alternatives are tried in turn. If a call has no matching clause, the system backtracks to the most recent choice point and takes the next alternative there.

This is the whole mechanism. In the following sections we define it more precisely.

Unification

Unification: one operation with many uses (Slide 11)

We need to know exactly what unification is, because it does far more work than it seems.

Unification is the mechanism used to pass parameters, return values, access parts of data structures, give values to variables, create data structures and "modify" them.

This is a single operation that does what most languages need half a dozen mechanisms for. It is also a procedure for solving equations on data structures.

Take two equations, X = f(Y) and Y = a. They say that X must be equal to f(Y) and that Y must be equal to a. Unification solves this system: Y is a, which we knew, and for the whole system to hold X has to be f(a).

?- X = f(Y), Y = a.
Expected answer:

X = f(a),
Y = a ?
Another example: X = f(Y) and X = f(b) can only be solved by making Y be b:

?- X = f(Y), X = f(b).
Expected answer:

X = f(b),
Y = b ?
That = is unification. It is what does all the work.

Like many equation-solving procedures, it works by isolating variables and instantiating them with their values.

Unification, more formally (Slide 12)

The objective of unifying two terms or literals A and B is to make them syntactically identical by giving minimal values to their variables.

More formally, we look for a variable substitution θ (X = a is a variable substitution, and a substitution in general is a set of them) such that applying θ to A and applying θ to B gives the same term. If no such substitution exists, the unification fails.

There are two rules in the process:

  1. Only variables can be given values. A constant cannot be changed: a is a. But X, being a variable, can become b or c or whatever is needed.
  2. Structures can only be made identical by making their arguments identical.
Some examples, with the substitution θ that unification finds:

A B θ Aθ = Bθ
dogdog{}dog
Xa{X = a}a
XY{X = Y}Y
f(X, g(t))f(m(h), g(M)){X = m(h), M = t}f(m(h), g(t))

The first pair is already identical, so nothing has to be done: the substitution is empty. The second one gives X the value a: B was already a and stays as it was, and A, which was X, becomes a. The third one makes one variable be the other.

The fourth one is the first non-trivial case, and it is worth going through it slowly. Starting on the left, f against f is fine. The first arguments are X and m(h): these will never be identical unless X is m(h), so this is the first substitution. Continuing, g against g is fine. Inside them we have t against M and, since a variable may be replaced by a constant or a structure (and nothing else may be replaced), we set M = t.

Applying the substitution to both sides, A becomes f(m(h), g(t)) and B becomes f(m(h), g(t)): they are identical.

?- f(X, g(t)) = f(m(h), g(M)).
Expected answer:

M = t,
X = m(h) ?

When it does not work

Different functors. Let us change one letter in the example, taking f(m(h), t(M)) instead of f(m(h), g(M)), and follow the same steps. f against f is fine, and so is X against m(h). But then we have g against t, and there is nothing that we can do: there is no way to make a t be a g, or a g be a t, because only variables can be substituted. So the two terms cannot be unified.

?- f(X, g(t)) = f(m(h), t(M)).
Expected answer:

no
Structures with a different name or a different arity cannot be unified. This is the first way in which unification fails.

A second, quite different way. Let us try to unify f(X,X) with f(Y,l(Y)). The first arguments are X and Y, so X = Y. The second arguments are X and l(Y), but X is already Y, so what is really being asked for is

Y = l(Y)
which means Y = l(Y) = l(l(Y)) = l(l(l(Y))) = ..., an infinite term.

The test that detects this case is called the occurs check. In classical first-order logic such a unification is not allowed. In modern logic programming systems it often is allowed, and the result is a cyclic term: a data structure that contains a pointer back into itself. Ciao allows it, and so do many current Prolog systems, but it is a question to ask of any system, because classical logic says no. We come back to this in the worked examples below.

The most general unifier (Slide 13)

When a unifier exists, there may be many. X = Y can be solved by {X = Y}, but also by {X = a, Y = a}, or by {X = f(b), Y = f(b)}, and so on.

Take the pair f(X, g(t)) and f(m(H), g(M)). One solution is {X = m(a), H = a, M = t}: applying it, both sides become f(m(a), g(t)). They are identical, so it is a correct unifier. But it has invented a value for H that nothing in the problem required.

Another solution is {X = m(H), M = t}, which leaves H alone. Both make the terms equal, but they are quite different substitutions: the result of the second one, f(m(H), g(t)), is more general than f(m(a), g(t)).

All these unifiers are correct, but we want the most general unifier: the one that binds the variables minimally. Where there are two variables, one is bound to the other; where there is a term, the variable is bound to that term; and nothing is invented.

This is what "minimal values" meant above, and it matters, because committing early to a value that was not forced would lose solutions. The most general unifier always exists (unless unification fails), and it is unique, up to the renaming of variables. It is the solution that the unification algorithm finds.

The unification algorithm (Slide 14)

The algorithm takes a set of one or more equations, A₁ = B₁, A₂ = B₂, ..., and works as follows:

  • Initialize the solution θ to empty.
  • Until there are no more equations left, select an equation E, delete it, and act depending on its form:

form of the equation Ewhat to do
X = Xignore it
X = f(..., X, ...)fail (occurs check)
X = termadd X = term to the solution, and replace X by term everywhere else
a = aignore it
a = bfail
a = f(...)fail
g(...) = f(...)fail
f(s₁, ..., sₘ) = f(t₁, ..., tₙ), with m ≠ nfail
f(s₁, ..., sₙ) = f(t₁, ..., tₙ)add to the system the equations s₁ = t₁, ..., sₙ = tₙ

In words: the two terms are traversed in parallel. Constants must be identical. A variable is bound to whatever it meets. Two structures must have the same functor and arity, and are then unified argument by argument. Anything else fails.

The unification algorithm: worked examples (Slide 15)

Here are five unifications. It is a good exercise to do them by hand first, following the algorithm, and then check the results at the top level.

?- p(X,f(b)) = p(a,Y).
Expected answer:

X = a,
Y = f(b) ?
This one is straightforward: X meets a, and Y meets f(b).

?- p(X,f(Y)) = p(a,g(b)).
Expected answer:

no
X = a is fine, but then f(Y) meets g(b): the functors are different, so nothing can make them identical, and the unification fails.

?- p(X,X) = p(f(Z),f(W)).
Expected answer:

X = f(W),
Z = W ?
Here X occurs twice. Unifying the first arguments makes X be f(Z), and unifying the second ones then forces f(Z) and f(W) to be identical, hence Z = W. The answer is a constraint between Z and W: neither of them has a value.

?- p(X,f(Y)) = p(Z,X).
Expected answer:

X = f(Y),
Z = f(Y) ?
The first arguments give X = Z. Then f(Y) meets X, which is Z, so both become f(Y).

?- p(X,f(X)) = p(Z,Z).
Expected answer:

X = f(X),
Z = f(X) ?
The last one is the most interesting. The first arguments give X = Z. Then f(X) meets Z, that is, f(Z) meets Z. There is no finite term that satisfies X = f(X): it would have to be f(f(f(...))) forever. What the system has built is a cyclic term (really, a constraint).

The mathematically correct answer here is fail, and the test that detects it is the occurs check: before binding a variable to a term, check that the variable does not occur inside that term.

Standard Prolog omits this check, because it costs time in every single unification and it almost never matters. But it is available when it is wanted, as unify_with_occurs_check/2 (from the iso_misc library):

?- use_module(library(iso_misc)), unify_with_occurs_check( p(X,f(X)), p(Z,Z) ).
Expected answer:

Note: module iso_misc already in executable, just made visible

no
This is the answer that logic wants. Can you explain the difference between the two answers?

Execution, search, and control

A schematic interpreter (SLD-resolution) (Slide 16)

SLD resolution is the operational semantics of logic programs: it is how they run, and it is necessary to know how it works.

Input and output. The input is a logic program P and a query Q. The output is Qμ, where μ is an answer substitution: values for the variables that appear in the query, which Prolog prints as A = something, B = something. This is the output if Q is provable from P, that is, if Q is a logical consequence of P. Otherwise the output is failure, which may be a finite failure or an infinite one.

The algorithm. There is one intermediate data structure, called the resolvent, R. It is initialized to the query Q. Then, while R is not empty:

  1. Take a literal in R, and call it A.
  2. Take a clause of the program whose head unifies with A. This is where the unification algorithm is used: we need a substitution θ that makes the head and A identical.
  3. The clause is renamed before it is used, with fresh names for its variables each time. If the clause has a variable X, it becomes X1 the first time that the clause is used, X2 the second time, and so on. In this way repeated applications, especially of recursive clauses, are not confused with each other.
  4. Replace A in R by the body of the chosen clause, and apply θ to the whole resolvent, and to the query.
If in step 2 no clause unifies, that branch fails, and we have to go back and try some other pending choice. If there are no pending choices left, the output is failure.

When R becomes empty we have a proof, and the composition of all the θs, restricted to the variables of the query, is the answer. On request, other pending branches can then be explored for more solutions.

Two choices are left open in this algorithm, and they have names:

  • which literal is taken from the resolvent: the computation rule;
  • which matching clause is tried: the search rule.
In the run by hand below we always take the leftmost literal, as Prolog does. The search rule is the subject of most of this section, and it is where the interesting differences are.

Note how simple this deduction procedure is. It has exactly one rule, the resolution rule: take a clause, put its body in the resolvent, and apply the substitution. This one step is all that is needed to prove any theorem.

The algorithm, run by hand

The best way to make this concrete is to do it by hand. Take the pets program, with its clauses numbered:

C1: pet(X) :- animal(X), barks(X).
C2: pet(X) :- animal(X), meows(X).
C3: animal(tim).       C6: barks(spot).
C4: animal(spot).      C7: meows(tim).
C5: animal(hobbes).    C8: roars(hobbes).
and the query ?- pet(P).

Start. R = pet(P), Q = pet(P).

Step 1. There is only one literal in R, so there is no choice about which one to take. We look for a clause whose head matches pet(P): both C1 and C2 do, so this is a choice point, which we mark with a star. Say that we take C2. We rename it first, since this is its first use: pet(X1) :- animal(X1), meows(X1). Unifying pet(P) with pet(X1) gives θ = {P = X1}. We apply θ to the query, which gives pet(X1), and replace the selected literal by the body of the clause:

Q = pet(X1)        R = animal(X1), meows(X1)        C2*   {P = X1}
Step 2. The leftmost literal is animal(X1). Three clauses match, so this is another choice point. We take C3, animal(tim). It has no variables, so no renaming is needed, and θ = {X1 = tim}. It is a fact, so its body is empty and nothing is added: the literal simply disappears:

Q = pet(tim)       R = meows(tim)                   C3*   {X1 = tim}
This is how the resolvent changes size. Matching a rule makes it larger, because the body goes in. Matching a fact makes it smaller, because the literal disappears and nothing replaces it. A proof finishes when the shrinking wins.

Step 3. meows(tim) matches only C7, so there is no choice point here, and no star. The two are already identical, so θ is empty, and C7 is a fact:

Q = pet(tim)       R = (empty)                      C7    {}
R is empty: we have proved the query, and the answer is P = tim.

This is resolution, done by hand, and it is exactly what the system does with the pets program above:

?- pet(P).
Expected answer:

P = spot ? ;
P = tim ? ;
no
The first answer of the system is spot, not tim: it made different choices at the starred points. Typing ; sends it back to the most recent of them to take the next alternative, and the second answer is the one that we found by hand.

The choices made in standard Prolog (Slide 17)

The schematic interpreter leaves two things open: which literal is taken from the resolvent, and which matching clause is used. A real system has to decide, and standard Prolog makes the simplest possible choice in each case: always the first one. Its execution model is the algorithm that we have just seen, with each of those open choices fixed. While the resolvent R is not empty:

  1. Take the leftmost literal in R, and call it A.
  2. Take the first clause of the program, in the order in which the clauses are written, whose head unifies with A. If there are more clauses for that predicate further down, they are remembered as a pending choice: a choice point.
  3. The clause is renamed, as before.
  4. Replace A in R by the body of the clause, and apply θ to the whole resolvent, and to the query. Since the body is put in the place of A, its literals are the next ones to be taken, also from left to right.
If no clause unifies, that branch fails, and the system goes back to the most recent pending choice and takes the next clause there. This is called backtracking. If there are no pending choices left, the output is failure.

When R becomes empty, the answer is output. If we then ask for more solutions, the system does the same as after a failure: it goes back to the most recent pending choice and continues from there.

Three words carry all the weight: leftmost, first, and most recent. The first one fixes the computation rule, and the other two fix the search rule to depth-first with backtracking. Changing any of them gives a different system, which is exactly what sr/bfall does.

These choices are also the reason why the first answer of the system to ?- pet(P). was spot, while our run by hand found tim: we started with C2, and Prolog starts with C1, the first clause. We will follow what it does step by step below.

Alternative execution paths, and the "normal programming" view (Slides 19–21)

The same query can be executed in more than one way, because for each call there may be several clauses whose heads unify with it. Following different choices gives different execution paths, some of which reach a solution and some of which fail.

Let us trace two of them for ?- pet(X). (top-down, and left to right), showing in each step the query, the resolvent, the clause that is chosen, and the substitution. In the first one the clause animal(tim) is taken:

Q R clause θ
pet(X)pet(X)C1*{X = X1}
pet(X1)animal(X1), barks(X1)C3*{X1 = tim}
pet(tim)barks(tim)??? failure

The starred entries are the choice points: calls for which other clauses were still applicable. barks(tim) has no matching clause, so this path fails. But failure is not the end of the story, because there are solutions in other paths: we go back to the last choice point (C3*) and try the next alternative, animal(spot):

Q R clause θ
pet(X)pet(X)C1*{X = X1}
pet(X1)animal(X1), barks(X1)C4*{X1 = spot}
pet(spot)barks(spot)C6 {}

This time barks(spot) does match, and the query succeeds: the system answers X = spot ?. If we type ; after the ? prompt, that is, if we ask for another solution, the system goes back and executes a different branch (a different choice at C4*, or at C1*).

The "normal programming" view. The resolution interpreter is the formal basis of the execution. In practice, however, we can simply follow the standard Prolog execution (depth-first, left to right) as it goes step by step through the code, as in any other language. This is easy to see with the debugger:

?- pet(X).
   1  1  Call: pets:pet(_1491) ?
   2  2  Call: pets:animal(_1491) ?
   2  2  Exit: pets:animal(tim) ?
   3  2  Call: pets:barks(tim) ?
   3  2  Fail: pets:barks(tim) ?
   2  2  Redo: pets:animal(tim) ?
   2  2  Exit: pets:animal(spot) ?
   3  2  Call: pets:barks(spot) ?
   3  2  Exit: pets:barks(spot) ?
   1  1  Exit: pets:pet(spot) ?
X = spot ? ;
   1  1  Redo: pets:pet(spot) ?
   2  2  Redo: pets:animal(spot) ?
   2  2  Exit: pets:animal(hobbes) ?
   3  2  Call: pets:barks(hobbes) ?
   3  2  Fail: pets:barks(hobbes) ?
   2  2  Call: pets:animal(_1491) ?
   2  2  Exit: pets:animal(tim) ?
   3  2  Call: pets:meows(tim) ?
   3  2  Exit: pets:meows(tim) ?
   1  1  Exit: pets:pet(tim) ?
X = tim ?
Procedure calls (Call:) and returns (Exit:) are as in any other language. The major difference is that sometimes a step fails (Fail:), and then execution can backtrack (Redo:) to the last choice point. This is what allows search, and having multiple answers. In the development environments each execution step is also marked on the source code.

We can follow this execution right here. The box below is a small playground of its own: it has a copy of the pets program on the left and a top level on the right (the programs and the queries of the rest of this page do not reach it). To use the debugger in it:

  • Press the debug button (🐞), above the program.
  • Type the query pet(X). at the ?- prompt on the right, and press Enter.
  • Press Creep (or Enter) to advance one step at a time. Each step prints a line like the ones above, and marks in the program the goal that is being executed.
  • When an answer appears, type ; and Enter to ask for another one, and go on stepping.
:- module(pets,_,[]).
%! \begin{miniplayground}
pet(X) :-
    animal(X),
    barks(X).
pet(X) :-
    animal(X),
    meows(X).

animal(tim).
animal(spot).
animal(hobbes).

barks(spot).
meows(tim).
roars(hobbes).
%! \end{miniplayground}
The other buttons that appear while stepping are Skip (run the current call to its end without showing its steps), Leap (continue without stopping), Retry (go back to the call of the current goal) and Fail (make the current goal fail). Pressing the debug button again turns the debugger off.

Comparison with imperative and functional languages: continuations (Slide 22)

This comparison is worth dwelling on, because it explains why a logic programming system can be as fast as a conventional one.

Conventional languages and Prolog both implement forward continuations: the place to go to after a procedure call succeeds. When a procedure is called, an activation record is pushed, which has at least a return address: when I finish, where do I go? In

p(X,Y) :- q(X,Z), r(Z,Y).
q(X,Z) :- ...
when the call to q/2 finishes (Exit:), execution continues in p/2 just after that call, that is, at the call to r/2. This is the forward continuation of the call to q/2.

In Prolog, when there are procedures with several definitions, there is also a backward continuation: the place to go to if there is a failure (Redo:). In

p(X,Y) :- q(X,Z), r(Z,Y).
p(X,Y) :- ...
q(X,Z) :- ...
if q/2 succeeds everything is as above, but if it fails, execution continues at (backtracks to) the previous alternative, the second clause of p/2. We say that p/2 has a choice point. So, on entering a procedure that has alternatives, Prolog also pushes a backward continuation, on a different stack, the choice point stack. This, and a few details, is all that backtracking is.

So an imperative program transliterated into Prolog pushes only on the return stack, exactly like its imperative original. We pay for search only when we actually do search.

This was David Warren's insight when he built the first Prolog compiler. Until then, implementations represented proof trees and resolution trees explicitly, which was faithful to the theory of theorem proving, and enormously slow. Warren saw that execution is no different from that of C, except that every now and then it backtracks, and he reused the machinery of imperative language implementation, adding only what backtracking needs. This is why Prolog is a programming language and not a theorem prover.

Again, the debugger is very useful to observe how execution proceeds.

Depth-first, breadth-first, and choosing between them (Slides 26–28)

Depth-first search goes down the leftmost branch as far as it can, and backtracks on failure. It is very efficient: it can be implemented with a call stack, very much as in a traditional programming language, and it is the standard search rule of Prolog. But if an infinite branch lies to the left of a solution, depth-first search goes down that branch and never comes back. So depth-first search is incomplete: it may fall through an infinite branch before finding all the solutions.

Breadth-first search explores the tree level by level: one step down this branch, one step down that one, then the next level everywhere, and so on. Since every solution is at finite depth, it reaches all of them before falling through an infinite branch: breadth-first search is complete.

The price is paid in time and, above all, in memory: at any point of the exploration the whole tree above has to be stored somehow, however compressed. Depth-first search needs one branch; breadth-first search needs the whole frontier.

Since breadth-first search is complete, it always gives the right answers. In this first part of the course we run most examples in breadth-first mode, so that we can write programs as Green would have wanted: we write the specification and, if there are answers, they are found. It may be inefficient, but it will not be wrong.

Depth-first search is what Prolog does by default, and for a good reason: it is efficient, and Prolog is meant to be a programming language. The clauses can be reordered so that the infinite branches are moved to the right and depth-first search finds the solutions first, but this requires understanding what is going on and thinking about it. That is the subject of the next part of the course; here we can ignore it.

In Ciao the search rule is selected, module by module, with the packages mechanism. The file starts with one of these lines:

:- module(_, _, [sr/bfall]).     % breadth-first: complete, slower, more memory
:- module(_, _, []).             % default: Prolog's depth-first
What this declaration does. Ciao code is divided into modules, and a module declaration has three arguments:

  • the name of the module, where _ means "take it from the file name", so that a file foo.pl is the module foo;
  • the exported predicates, where _ means "export everything", which is the easiest thing while learning;
  • a list of packages.
Packages are the extension mechanism of Ciao. At its core Ciao is a Prolog system, and around that core there are many extensions, both syntactic (so that more things can be written) and semantic (so that more things happen when the program runs). sr/bfall is a semantic one: it says run every predicate of this module breadth-first.

In practice one often wants only some predicates to run breadth-first, and there are finer-grained ways of saying so. bfall is the simplest possible choice: everything, with no exceptions.

Ciao has other search rules too, among them iterative deepening: go down to a fixed depth in one branch, then to the same depth in the next one, and increase the bound in each round. It is essentially breadth-first search, but much cheaper in memory. For simplicity, here we just use sr/bfall.

This is the first time that we see algorithm = logic + control as something that we can actually operate. The program does not change; the control does and, with it, what the program can find. The sr/bfall in the squares program of the introduction was exactly this.

Try it with the pets program: change its declaration between [sr/bfall] and [], load it again, and ask pet(X). The answers are exactly the same, because that program is finite and has no infinite branches. An infinite branch needs recursion, and data structures that can grow without bound. The choice matters in those more complicated cases.

Control of search: clause and goal order (Slides 29–30)

Conventional programs, without search, execute conventionally. In programs with search the programmer has at least three ways of controlling the search. In what follows we assume depth-first search, that is, backtracking.

1. The order of the literals in the body of a clause

This order has a profound effect on the size of the computation and, at the limit, on termination. Consider the following program, in which each clause of q/2 does a lot of computing (simulated here with a pause of one second):

:- module(_,_).

p(4).
p(5).

q(1, a) :- lots_of_computing.
q(2, b) :- lots_of_computing.
q(4, c) :- lots_of_computing.
q(4, d) :- lots_of_computing.

lots_of_computing :- pause(1).
and these two predicates:

%! \begin{focus}
% Consider: 
a(X,Y) :- 
    p(X),   % We execute p first, 
    q(X,Y). % then q
% versus: 
b(X,Y) :-
    q(X,Y), % We execute q first,
    p(X).   % then p
%! \end{focus}
:- use_module(library(odd),[undo/1]).
:- meta_predicate(walltime(goal)).
walltime(G) :-
    statistics(walltime,_),
    call(G),
    undo(statistics(walltime,_)),
    statistics(walltime,[_,T]),
    write('Executed in '), NT is T/1000, write(NT), write(' seconds.').
walltime(_) :-
    statistics(walltime,[_,T]),
    write('Executed in '), NT is T/1000, write(NT), write(' seconds.').
a/2 and b/2 are logically identical: the conjunction is the same. But a/2 calls p(X) first, which binds X to 4, so that the call to q is q(4,Y) and only the clauses that match it are tried. b/2 calls q first, with X unbound, so it goes through q(1,a) and q(2,b), each of them costing one second of lots_of_computing, before reaching a clause that p accepts.

We can compare the times with walltime/1, a small predicate defined in the same file (not shown) that runs a goal and prints the time that it took:

?- walltime(a(X,Y)).
Expected answer (the exact figures vary a little from run to run):

Executed in 1.003397 seconds.
X = 4,
Y = c ?
?- walltime(b(X,Y)).
Expected answer:

Executed in 3.009617 seconds.
X = 4,
Y = c ?
The logic is the same and the answers are the same, but the amount of work is very different: executing p/1 first reduces the choices of q/2. This is the distinction between generate and test and constrain and generate, to which the part on constraint logic programming devotes a whole section.

Following the execution by hand. Take ?- p(X), q(X,Y). first. p gives X = 4. Now, for q(4,Y), the first clause wants X = 1 and fails immediately, before any computing, and the second one wants X = 2 and fails as well. So execution goes straight to the third clause and does the work once. If we ask for a second solution, the fourth clause gives it. The first solution costs one expensive computation.

Now take ?- q(X,Y), p(X). We call q with X free. The first clause gives X = 1, Y = a, and does the work; then it returns, p(1) is called, and it fails: the whole computation was wasted. The second clause gives X = 2, does the work, and fails again. The third clause gives X = 4, does the work, and p(4) finally succeeds. That is three expensive computations instead of one.

Following it in the debugger. These are exactly the steps that the debugger shows. The box below is a small playground with a copy of the program, like the one that we used for the pets example. Press its debug button (🐞), type a(X,Y). at the ?- prompt on the right, and step with Creep (or Enter) until the answer appears; press Enter to accept it, and then do the same with b(X,Y).:

:- module(search,_).
%! \begin{miniplayground}
p(4).
p(5).

q(1, a) :- lots_of_computing.
q(2, b) :- lots_of_computing.
q(4, c) :- lots_of_computing.
q(4, d) :- lots_of_computing.

lots_of_computing :- pause(1).

a(X,Y) :-
    p(X),
    q(X,Y).

b(X,Y) :-
    q(X,Y),
    p(X).
%! \end{miniplayground}
With a/2 the trace has ten lines, and lots_of_computing is called only once, from the third clause of q/2: the mark in the program goes straight to that clause. With b/2 the trace has twenty-six lines: lots_of_computing is called from the first clause of q/2, and then p(1) fails; it is called from the second clause, and p(2) fails; and it is called from the third one, after which p(4) succeeds. (The calls to pause/1 are in the trace too, as system:pause(1): system is the library module that defines it.)

The same logic and the same answers, with three times the work. At the limit, this is the difference between terminating and not terminating.

The best order depends on the mode. If the second argument is given, as in q(X,d), p(X), this is the better order: q produces the only candidate for X directly (Y = d selects its last clause), and p just checks it, whereas p(X), q(X,d) calls q once for each value that p generates. There is no order that is best in general: it depends on what is bound when the goals are reached.

2. The order of the clauses in a predicate

The order of the clauses affects the order in which the solutions are generated. In the following program, qr/2 is q/2 with its clauses in the reverse order:

:- module(_,_).

p(4).
p(5).

q(1, a) :- lots_of_computing.
q(2, b) :- lots_of_computing.
q(4, c) :- lots_of_computing.
q(4, d) :- lots_of_computing.

lots_of_computing :- pause(1).
%! \begin{focus}
% Consider calling q:
a(X,Y) :- 
    q(X,Y),
    p(X).
% versus calling qr: a reordered version of q:
b(X,Y) :-
    qr(X,Y),
    p(X).

qr(4, d) :- lots_of_computing.
qr(4, c) :- lots_of_computing.
qr(2, b) :- lots_of_computing.
qr(1, a) :- lots_of_computing.
%! \end{focus}
:- use_module(library(odd),[undo/1]).
:- meta_predicate(walltime(goal)).
walltime(G) :-
    statistics(walltime,_),
    call(G),
    undo(statistics(walltime,_)),
    statistics(walltime,[_,T]),
    write('Executed in '), NT is T/1000, write(NT), write(' seconds.').
walltime(_) :-
    statistics(walltime,[_,T]),
    write('Executed in '), NT is T/1000, write(NT), write(' seconds.').
With q/2, the solution with c comes out first, and with qr/2 the one with d:

?- a(X,Y).
Expected answer:

X = 4,
Y = c ? ;
X = 4,
Y = d ? ;
no
?- b(X,Y).
Expected answer:

X = 4,
Y = d ? ;
X = 4,
Y = c ? ;
no
The order of the clauses can also affect the size of the computation, and termination:

?- walltime(a(X,Y)).
Expected answer:

Executed in 3.011117 seconds.
X = 4,
Y = c ?
?- walltime(b(X,Y)).
Expected answer:

Executed in 1.001547 seconds.
X = 4,
Y = d ?
If we want all the solutions, the order of the clauses only changes the order of the answers: the whole tree has to be explored in any case. But if we want one solution it can matter enormously. Imagine that the solution is on a short branch: if that branch is on the left, it is found at once, and the search stops; if it is on the right, the whole useless part of the tree is explored first.

3. The pruning operators

The third mechanism, which pure logic programming does not have, is that of the pruning operators, such as the "cut", which remove parts of the tree dynamically. We will see them in the part on the Prolog language.

The role of unification in execution (Slide 31)

Unification does three jobs at once during execution, and it is worth telling them apart:

  • passing arguments in: the terms of the caller meet the terms of the head;
  • returning results: a variable of the call is bound by the head;
  • selecting a clause: a head that does not unify is simply not applicable.
In a conventional language these are three different mechanisms (parameter passing, return values, and dispatch). Here they are a single operation, which works in whichever direction the data happens to flow.

A worked example

:- module(_,_,[]).

animal(dog(tim)).
named(dog(Name), Name).
?- animal(A), named(A, Name).
Expected answer:

A = dog(tim),
Name = tim ?
In programming terms, the call to animal(A) creates a data structure, dog(tim) (an object with tim inside it), and makes A point to it. Then named(A,Name) is called with A pointing to dog(tim), and the pattern in the head, dog(Name), is matched against it: it reaches inside the structure and binds Name to tim. This Name is the same variable as the second argument, so the value is passed back, and comes out as part of the answer. (We could also have written simply ?- animal(A), A = dog(Name).)

There is no get, no put, no accessor, no index, and no navigation code. Building the structure, passing it to a procedure, reaching inside it, and returning a part of it: all four are done by unification.

Lists, trees, and any other data structure can be built on this basis: a whole course on data structures can be done with unification and first-order terms.

"Modes" (Slide 32)

An interesting consequence of all this is that argument positions are not fixed a priori to be inputs or outputs.

In a functional language the arguments are all inputs, and the return value is the output. Here it is different. Is the argument of pet/1 an input or an output? It depends on the call. In pet(spot) it is an input: we give spot to the procedure, and ask it to check:

?- pet(spot).
Expected answer:

yes
In pet(X) it is an output: we ask the procedure to produce values:

?- pet(X).
Expected answer:

X = spot ? ;
X = tim ? ;
no
The argument is the same, and so is the procedure.

So arguments are bidirectional, and the role that they play depends on their instantiation state at the moment of the call. The same happens in the Peano arithmetic example of the introduction:

:- module(_,_,[sr/bfall]).

nat(0).
nat(s(X)) :- nat(X).

le(0,X) :- nat(X).
le(s(X),s(Y)) :- le(X,Y).

add(0,Y,Y) :- nat(Y).
add(s(X),Y,s(Z)) :- add(X,Y,Z).

mult(0,Y,0) :- nat(Y).
mult(s(X),Y,Z) :- mult(X,Y,W), add(W,Y,Z).

nat_square(X,Y) :- nat(X), nat(Y), mult(X,X,Y).

output(X) :- nat(Y), le(Y,s(s(s(s(s(0)))))), nat_square(Y,X).
?- add( s(0), s(s(0)), Z).
Expected answer:

Z = s(s(s(0))) ?
?- add( s(0), Y,       s(s(s(0)))).
Expected answer:

Y = s(s(0)) ?
The same clauses have run forwards and backwards: first as addition, and then as subtraction. Nothing in the program declares which argument is the input, because in a relation there is no such thing.

This combination of inputs and outputs is called the mode of a call: procedures can be used in different modes, in which different sets of arguments are inputs or outputs. A predicate with three arguments can be called in mode input-input-output, or input-output-input, and so on. We sometimes write modes with + and -:

add(+X, +Y, -Z)     we call add with X and Y instantiated, and expect Z to be bound if it succeeds
We will use this notation from time to time. Note that it describes a call, not the predicate: the same predicate can be used in several modes.

Pure logic programs: an overview (Slide 34)

Everything so far has been pure: programs that make use only of unification, with no cut, no assert, no arithmetic evaluation, and no negation. Pure logic programs have three important properties:

  • They are fully "logical": the set of computed answers (the answers to all the queries that terminate successfully) coincides with the set of logical consequences of the program.
  • They allow programming declaratively, that is, describing the problem, making queries, and obtaining correct answers: specifications as programs, as in the introduction to the course.
  • They have full computational power (they are Turing complete).
Horn clauses, unification and a search rule are enough to program with, as the rest of this part shows.

Programming with data

Database programming (Slides 35–36)

The simplest use of logic programming is as a database. A logic database is a set of facts and rules, that is, a logic program: the facts are the data, and the rules derive new facts from them.

:- module(_,_,[sr/bfall]).

father_of(john, peter).
father_of(john, mary).
father_of(peter, michael). 

mother_of(mary, david).
mother_of(jill, john).

grandfather_of(L,M) :-
    father_of(L,N),
    father_of(N,M).
grandfather_of(X,Y) :-
    father_of(X,Z),
    mother_of(Z,Y).  
The facts are the tables, and grandfather_of/2 is a view derived from them. Note that it has two clauses, because a grandfather can be on the father's side or on the mother's side, and the two clauses say exactly that.

Existential variables. The variable N in the first rule appears only in the body, and never in the head. Such a variable is much easier to read as an existential one:

L is the grandfather of M if there exists an N such that L is the father of N and N is the father of M.

Or, more plainly: L is the grandfather of M if L is the father of some N, and that N is the father of M.

Given such a logic database, the system can answer queries. Facts that are in the program are easy:

?- father_of(john,peter).
Expected answer:

yes
And what is not in the program is answered with no:

?- father_of(john,david).
Expected answer:

no
This no deserves a comment. The system does not know that John is not the father of David: it only knows that it cannot prove that he is, and it answers no on that basis. This is the closed world assumption, well known in artificial intelligence: what cannot be derived is taken to be false.

?- father_of(john,X).
Expected answer:

X = peter ? ;
X = mary ? ;
no
?- grandfather_of(X,Y).
Expected answer:

X = john, Y = michael ? ;
X = john, Y = david ? ;
no
There are two grandfather relationships in the database, and they are found without giving either argument.

Both directions work equally well. If we ask who is the grandfather of michael, the answer is john (through peter), and nobody else:

?- grandfather_of(X,michael).
Expected answer:

X = john ? ;
no
If we ask instead whose grandfather john is, there are two answers, one from each rule: michael (through peter) and david (through mary).

?- grandfather_of(john,X).
Expected answer:

X = michael ? ;
X = david ? ;
no
And nobody is their own grandfather:

?- grandfather_of(X,X).
Expected answer:

no
A small exercise, worth doing before reading on: write the rules for grandmother_of(X,Y). There are two of them, for the same reason as above. Then do the same for parent/2, ancestor/2, and related/2 (which holds for two people who have a common ancestor). The exercises at the end of this section include them.

A second example shows that "database" need not mean people and dates. Here the facts are the topology of a circuit (which resistors and transistors are connected to which nodes) and the rules are a little circuit theory, which says what an inverter, a NAND gate and an AND gate look like in terms of that topology:

The circuit is represented by these facts:

:- module(_,_,[sr/bfall]).

resistor(power,n1).
resistor(power,n2). 

transistor(n2,ground,n1).
transistor(n3,n4,n2).
transistor(n5,ground,n4).
and this is the circuit theory:

inverter(Input,Output) :- 
   transistor(Input,ground,Output), 
   resistor(power,Output).

nand_gate(Input1,Input2,Output) :-
   transistor(Input1,X,Output),
   transistor(Input2,ground,X), 
   resistor(power,Output).

and_gate(Input1,Input2,Output) :-
   nand_gate(Input1,Input2,X), 
   inverter(X, Output).
We can now make queries that find gates in the circuit:

?- inverter(In,Out).
Expected answer:

In = n2,
Out = n1 ? ;

no
?- nand_gate(In1,In2,Out).
Expected answer:

In1 = n3,
In2 = n5,
Out = n2 ? ;

no
?- and_gate(In1,In2,Out).
Expected answer:

In1 = n3,
In2 = n5,
Out = n1 ?
The program has found a gate inside the circuit: it has recognized that this particular arrangement of five components is an AND gate, and it has told us which nodes are its inputs and its output. Nobody wrote a search for gates: the rules say what a gate is, and the search comes for free.

Exercises

Put it into practice! Some exercises on the classic family database — opens in a separate notebook.

Structured data, data abstraction, and = (Slides 37–40)

Data structures are created using (complex) terms, and structuring the data is important. Suppose that we want to record that the Computational Logic course is taught on Wednesdays from 18:30 to 20:30, by a given lecturer, in a given room, and the same for other courses. We could just use ten arguments:

course(complog, wed, 18, 30, 20, 30, 'M.', 'Hermenegildo', new, 5102).
course(ai,      thu, 15, 00, 17, 00, 'J.', 'Smith',        old, 3102).
course(os,      wed, 18, 30, 20, 30, 'L.', 'Hubbard',      new, 6201).
and then ask when the Computational Logic course is with

?- course(complog,Day,StartH,StartM,FinishH,FinishM,C,D,E,F).
This works, but it has no structure at all.

The alternative is to structure the data:

:- module(_,_,[]).

course(complog, Time, Lecturer, Location) :-
    Time     = t(wed, 18:30, 20:30),
    Lecturer = lect('M.','Hermenegildo'),
    Location = loc(new, 5102).
The time is now a data structure with three fields (day, start and finish). Note also that : is used as an infix operator: 18:30 is really the term :(18,30), written in the way in which we would want to write it.

These = goals are pure unification, and writing them is exactly equivalent to putting the structures directly in the head:

course(complog, t(wed,18:30,20:30), lect('M.','Hermenegildo'), loc(new,5102)).
X = Y is the same as =(X,Y), where the predicate =/2 is defined by the single fact =(X,X).: one clause, whose two arguments are the same variable. This is the whole definition of equality, that is, of unification.

?- course(complog, Time, A, B).
Expected answer:

A = lect('M.','Hermenegildo'),
B = loc(new,5102),
Time = t(wed,18:30,20:30) ?
A small trick: a variable whose name starts with an underscore is not printed by the top level. Better still, we can use the anonymous variable, and not name it at all:

?- course(complog, Time, _, _).
Expected answer:

Time = t(wed,18:30,20:30) ?
Note what building loc(new,5102) involved: no type declaration, no new, and no allocation. The structure is created on the fly, by unification.

In general, terms are the only data structure, and they are enough. A structure groups related things: date(monday, Month, 1994) is a record with three fields, and person(name(john,smith), age(23)) has records nested inside records.

Two habits are worth acquiring early.

Use the anonymous variable for the fields that are not needed. To get the year of a date, we write date(_, _, Year) rather than inventing names that are never used. Each _ is a different fresh variable, which is exactly what is wanted here.

Access fields by unification, not by taking things apart. To get the year we do not call an accessor: we unify the whole term with a pattern, and the year comes out. This is what "data abstraction" means in this setting: the pattern is the accessor.

As for the = predicate, X = Y is neither an assignment nor a test: it is a call to the unification algorithm, exactly as described above. It succeeds if the two terms can be made identical, and binds whatever is needed to achieve this.

Terms as data structures with pointers. A term in memory is a functor cell followed by its arguments, and an argument that is a structure is a pointer to it. So f(g(a),X) is a cell for f/2, a pointer to a g/1 cell, and an unbound cell for X. Unifying it with f(Y,b) makes Y point to the g(a) structure, with no copying, and fills the X cell with b.

This is why unification is cheap, and why we put "modifying" a data structure in quotes: nothing is ever overwritten; we bind variables that were left unbound.

As an example, main below is a procedure that creates some data structures, with pointers and aliasing, and then calls other procedures, passing them pointers to these structures. The figure shows the heap memory at the point marked in the code:

main :-
    X = f(K,g(K)),
    Y = a,
    Z = g(L),
    W = h(b,L),
    % heap memory at this point --->
    p(X,Y),
    q(Y,Z),
    r(W).

Note the aliasing: K occurs twice in X, so both places point to the same cell, and L is shared between Z and W. The call p(X,Y) passes pointers to these structures, not copies. In summary, terms are data structures with pointers, and logical variables are declarative pointers: they can only be assigned once.

The circuit example revisited. Data structures can also be used to give names to the components of the circuit:

:- module(_,_,[sr/bfall]).

resistor(r1,power,n1).
resistor(r2,power,n2). 

transistor(t1,n2,ground,n1).
transistor(t2,n3,n4,n2).
transistor(t3,n5,ground,n4).

inverter(inv(T,R),Input,Output) :-
    transistor(T,Input,ground,Output),
    resistor(R,power,Output). 

nand_gate(nand(T1,T2,R),Input1,Input2,Output) :-
    transistor(T1,Input1,X,Output),
    transistor(T2,Input2,ground,X),
    resistor(R,power,Output). 

and_gate(and(N,I),Input1,Input2,Output) :-
    nand_gate(N,Input1,Input2,X),
    inverter(I,X,Output).
Each gate is now itself a structure, built from the names of its components:

?- inverter(Inv,I,O).
Expected answer:

I = n2,
Inv = inv(t1,r1),
O = n1 ? ;

no
?- and_gate(G,In1,In2,Out).
Expected answer:

G = and(nand(t2,t3,r2),inv(t1,r1)),
In1 = n3,
In2 = n5,
Out = n1 ? ;

no
The answer now says not only that there is an AND gate in the circuit, but also which components it is made of. The facts themselves can be queried in the same way, for example with ?- resistor(R1,P1,P2). or ?- transistor(T1,P1,P2,P3).

Logic programs and the relational database model (Slides 41–42)

The correspondence with relational databases is close enough to be worth stating precisely:

relational database logic programming
relation name predicate symbol
relation (table) procedure consisting of ground facts (facts without variables)
tuple (row) ground fact
attribute (column) argument of the predicate

Take, for example, these two tables:

Person: Name AgeSex
Brown 20M
Jones 21F
Smith 36M

Lived in: Name Town Years
Brown London 15
Brown York 5
Jones Paris 21
Smith Brussels 15
Smith Santander 5

Tables are predicates composed of facts. (The argnames package, used below, allows giving names to the arguments. A second table of persons, person2, is added in order to try the operations between tables.)

:- module(_,_,[]).

% The argnames package allows giving names to arguments.
:- use_package(argnames).

:- argnames
person(   name, age,   sex).
%     ======================
person(  brown, 20,   male).                 
person(  jones, 21, female).           
person(  smith, 36,   male).           

:- argnames
person2(   name, age,   sex).
%      ======================
person2( cabeza, 33,   male).    
person2(  bueno, 39,   male).     
person2(  jones, 21, female).   

:- argnames
lived_in( name,       town, years).
%       ===========================
lived_in( brown,    london,    15).
lived_in( brown,      york,     5).
lived_in( jones,     paris,    21).
lived_in( smith,  brussels,    15).
lived_in( smith, santander,     5).
Operations on tables are predicates composed of rules. For example, a projection keeps only some of the columns:

city(C) :- 
    lived_in(_,C,_).
?- city(C).
Expected answer:

C = london ? ;
C = york ? ;
C = paris ? ;
C = brussels ?
The same projection can be written using the names of the arguments:

city_(C) :- 
    lived_in${town => C}.
?- city_(C).
Expected answer:

C = london ? ;
C = york ? ;
C = paris ? ;
C = brussels ?
The operations of the relational model are easily implemented as rules. The union of two tables is a predicate with one clause for each of them:

all_persons(Name,Age,Sex) :- 
    person(Name,Age,Sex).
all_persons(Name,Age,Sex) :- 
    person2(Name,Age,Sex).
?- all_persons(Name,Age,Sex).
Expected answer:

Age = 20,
Name = brown,
Sex = male ? ;

Age = 21,
Name = jones,
Sex = female ? ;

Age = 36,
Name = smith,
Sex = male ? ;

Age = 33,
Name = cabeza,
Sex = male ?
The set difference needs negation. The \+ used here is not pure; we will come back to negation later in the course:

difference(Name,Age,Sex) :- 
    person(Name,Age,Sex), 
    \+ person2(Name,Age,Sex).
difference(Name,Age,Sex) :- 
    person2(Name,Age,Sex),
    \+ person(Name,Age,Sex).
?- difference(Name,Age,Sex).
Expected answer:

Age = 20,
Name = brown,
Sex = male ? ;

Age = 36,
Name = smith,
Sex = male ? ;

Age = 33,
Name = cabeza,
Sex = male ? ;

Age = 39,
Name = bueno,
Sex = male ?
The Cartesian product is simply a conjunction with no shared variables:

person_X_lived_in(Name1,Age,Sex,Name2,Town,Years) :- 
    person(Name1,Age,Sex), 
    lived_in(Name2,Town,Years).
?- person_X_lived_in(Name1,Age,Sex,Name2,Town,Years).
Expected answer:

Age = 20,
Name1 = brown,
Name2 = brown,
Sex = male,
Town = london,
Years = 15 ? ;

Age = 20,
Name1 = brown,
Name2 = brown,
Sex = male,
Town = york,
Years = 5 ? ;

Age = 20,
Name1 = brown,
Name2 = jones,
Sex = male,
Town = paris,
Years = 21 ? ;

Age = 20,
Name1 = brown,
Name2 = smith,
Sex = male,
Town = brussels,
Years = 15 ?
A selection adds a condition on the values. (The condition should also be written with Peano arithmetic to be pure; here the Prolog built-in </2 is used.)

underage_person(Name,Age,Sex) :- 
    person(Name,Age,Sex), 
    Age < 21. % We should use Peano here also of course.
?- underage_person(Name,Age,Sex).
Expected answer:

Age = 20,
Name = brown,
Sex = male ? ;

no
Some derived operations can be expressed more directly. In the intersection and the join, the tables are connected just by using the same variable in both:

person_lived_in(Name,Age,Sex,Town,Years) :-
    person(Name,Age,Sex), 
    lived_in(Name,Town,Years).
?- person_lived_in(Name,Age,Sex,Town,Years).
Expected answer:

Age = 20,
Name = brown,
Sex = male,
Town = london,
Years = 15 ? ;

Age = 20,
Name = brown,
Sex = male,
Town = york,
Years = 5 ? ;

Age = 21,
Name = jones,
Sex = female,
Town = paris,
Years = 21 ? ;

Age = 36,
Name = smith,
Sex = male,
Town = brussels,
Years = 15 ?
person_joinName_person2(Name,Age,Sex) :-
    person(Name,Age,Sex), 
    person2(Name,_Age2,_Sex2).
?- person_joinName_person2(Name,Age,Sex).
Expected answer:

Age = 21,
Name = jones,
Sex = female ? ;

no
This is the most elegant point of the correspondence. In grandfather_of(L,M) :- father_of(L,N), father_of(N,M). the shared variable N is the join: the two tables are joined on the column in which the variable is repeated. No join operator is written: the same variable is written twice.

In general, for relations r and s:

Union:              r_union_s(X1,...,Xn) ← r(X1,...,Xn).
                    r_union_s(X1,...,Xn) ← s(X1,...,Xn).

Cartesian product:  r_X_s(X1,...,Xm,Xm+1,...,Xm+n) ← r(X1,...,Xm), s(Xm+1,...,Xm+n).

Projection:         r13(X1,X3) ← r(X1,X2,X3).

Selection:          r_selected(X1,X2,X3) ← r(X1,X2,X3), ≤(X2,X3).

Set difference:     r_diff_s(X1,...,Xn) ← r(X1,...,Xn), not s(X1,...,Xn).
                    r_diff_s(X1,...,Xn) ← s(X1,...,Xn), not r(X1,...,Xn).

Intersection:       r_meet_s(X1,...,Xn) ← r(X1,...,Xn), s(X1,...,Xn).

Join:               r_joinX2_s(X1,...,Xn) ← r(X1,X2,X3,...,Xn), s(X1',X2,X3',...,Xn').
(In the selection, ≤/2 can be the Peano one, a Prolog built-in, a constraint, and so on.) Duplicate answers are an issue in these operations: setof/3 and related predicates, which we will see in the part on Prolog, take care of them.

Deductive databases, Datalog and ASP (Slide 43)

These ideas give the field of deductive databases its name: a deductive database is one in which, in addition to the stored facts, there are rules that derive further facts. Logic programming is a deductive database language in which the rules may also be recursive, which relational algebra cannot express; recursion is the subject of the next section.

The field has developed languages of its own, which are deliberately less powerful than Prolog. They take a subset of definite programs, with syntactic restrictions:

  • Datalog allows no functors, and therefore no data structures: the only terms are the constants that are already in the database. It does not allow existential variables either: a rule only concludes things about individuals that its body mentions.
  • Execution is bottom-up, instead of the top-down search of Prolog: it starts from the facts and applies the rules, with the T_P operator that is introduced in the theory part of the course, until nothing new is derived, and then keeps the part of that model that the query asks about.
The restrictions have a purpose. With no functors, no rule can build a term that was not there to begin with, so there are finitely many facts to derive, and the bottom-up computation always stops. The system is decidable, as a database is: both the answers and the failures come in finite time, where Prolog may loop. The price is that it is not Turing complete: it is a query language, not a programming language.

What is gained with the restrictions is reasoning. These systems support much more powerful notions of negation, under the stable model semantics, which is the basis of Answer Set Programming (ASP): we state what holds and what cannot hold, and the system returns the models that satisfy all of it. For knowledge representation and reasoning this is a very effective way of writing a problem down.

The two worlds are closer than this may suggest. The s(CASP) system executes ASP programs in the way in which Prolog executes a query (top-down and goal-directed, with no grounding step), so it answers one query at a time, works on non-ground programs, and can show why an answer holds.

Recursion

Recursive programming (Slide 45)

What makes all this a programming language rather than a query language is that a predicate may call itself. Suppose that we want to define ancestor/2. Without recursion we would have to write:

parent(X,Y) :- father_of(X,Y).
parent(X,Y) :- mother_of(X,Y).

ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), parent(Z,Y).
ancestor(X,Y) :- parent(X,Z), parent(Z,W), parent(W,Y).
ancestor(X,Y) :- parent(X,Z), parent(Z,W), parent(W,K), parent(K,Y).
...
and so on, forever. Defining it recursively, two clauses are enough:

:- module(_,_,[]).

parent(X,Y) :- father_of(X,Y).
parent(X,Y) :- mother_of(X,Y).

ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

father_of(john, peter).
father_of(john, mary).
father_of(peter, michael). 

mother_of(mary, david).
mother_of(jill, john).
The definition reads: X is an ancestor of Y if X is a parent of Y, or if X is a parent of someone who is an ancestor of Y. There are two clauses: the base case and the recursive case. This shape (one clause that stops, and one that reduces the problem) appears again in every recursive predicate that follows.

?- ancestor(john,michael).
Expected answer:

yes
?- ancestor(john,X).
Expected answer:

X = peter ? ;
X = mary ? ;
X = michael ? ;
X = david ? ;
no
These are the four descendants of john, at any depth, which is exactly what relational algebra cannot express. And the same definition gives the ancestors of somebody:

?- ancestor(X,michael).
Expected answer:

X = peter ? ;
X = john ? ;
X = jill ? ;
no

Exercises

Put it into practice! Exercises: related, cousin, and same generation — opens in a separate notebook.

What about "not equal"? (Slides 46–47)

In the previous exercises, two people are siblings if they share a parent... and are not the same person:

sibling(X,Y) :- parent(P,X), parent(P,Y), ???
Sooner or later we need to say that something does not hold; here, that two things are different. As we will see later, Prolog does provide built-ins for this: \=/2, "not unifiable", and also \==/2, "not identical". But we are in the pure part of the language, and these built-ins are not relations: they are not pure, because they test how much their arguments happen to be instantiated at the moment when they are called. With constants everything seems fine:

?- john \= mary.
Expected answer:

yes
But with variables the built-in just says no, although X and Y could be different, and it does not enumerate anything:

?- X \= Y.
Expected answer:

no
whereas the same test, made a little later, succeeds:

?- X = john, Y = mary, X \= Y.
Expected answer:

X = john,
Y = mary ? ;

no
So the answer depends on when the goal is run, and not only on what is true: this is what "impure" means. The program can then no longer be read declaratively, because its meaning is no longer given by the clauses alone.

The pure alternative is the one that we have been using all along: if we need a (negative) relation, we define it. In our case the domain is small and known (the people in the database), so we can give each person a number:

:- module(_,_,[]).

person_num(john,    0).
person_num(peter,   s(0)).
person_num(mary,    s(s(0))).
person_num(michael, s(s(s(0)))).
person_num(david,   s(s(s(s(0))))).
person_num(jill,    s(s(s(s(s(0)))))).
and define disequality on (Peano) numbers, recursively:

diffnat(0,s(_)).                    % 0 differs from any s(...)
diffnat(s(_),0).                    % any s(...) differs from 0
diffnat(s(X),s(Y)) :- diffnat(X,Y). % same depth: look further in
diffnat/2 is a definition of difference, rather than a test: two Peano numbers are different if one of them is zero and the other one is not, or if both are successors and their predecessors are different. Three clauses, and no negation anywhere.

?- diffnat(0,s(0)).
Expected answer:

yes
?- diffnat(s(s(0)),s(s(0))).
Expected answer:

no
And, being a real relation, it also enumerates:

?- diffnat(s(s(0)),X).
Expected answer:

X = 0 ? ;
X = s(0) ? ;
X = s(s(s(_))) ? ;
no
The third answer stands for all the numbers greater than 2: an infinite set of solutions, in a single answer. We can now define disequality of people:

different_person(X,Y) :-
    person_num(X,NX),
    person_num(Y,NY),
    diffnat(NX,NY).
?- different_person(john,mary).
Expected answer:

yes
?- different_person(john,john).
Expected answer:

no
which, again, also enumerates:

?- different_person(john,X).
Expected answer:

X = peter ? ;
X = mary ? ;
X = michael ? ;
X = david ?
Going back to the family database, we can use different_person/2 to write sibling/2:

father_of(john, peter).
father_of(john, mary).
father_of(peter, michael).

mother_of(mary, david).
mother_of(jill, john).

parent(X,Y) :- father_of(X,Y).
parent(X,Y) :- mother_of(X,Y).

sibling(X,Y) :- parent(P,X), parent(P,Y), different_person(X,Y).
?- sibling(X,Y).
Expected answer:

X = peter,
Y = mary ? ;

X = mary,
Y = peter ? ;

no
?- sibling(peter,peter).
Expected answer:

no
The general lesson: in pure logic programming, define the complement as a type, rather than negating a test. nonzero/1, notnat/1, different/2: each of them is a positive definition of what it means not to be something.

For a recursive type the same idea works structurally: two terms are different if they have different constructors, or if they have the same one and some argument is different. This is more work than using \=/2, and it only works for closed types (we have to know all the functors), but what we get is a genuine relation, which works in any mode, and enumerates. General negation is harder: there are several approaches, and we will return to it later.

Note also the other half: to say that two things are equal, no test is needed at all. We use the same variable twice, and unification does it.

Types, and recursive types (Slides 48–49)

A type is a possibly infinite set of terms. A type definition is a program that defines that set.

It is important to keep the two apart: the type is the set, and the definition is the program.

Take weekday. The set of terms is 'Monday', 'Tuesday', ... (in quotes, because they start with a capital letter and they are constants, not variables). The definition is seven facts:

:- module(_,_,[]).

weekday('Monday').
weekday('Tuesday').
weekday('Wednesday').
weekday('Thursday').
weekday('Friday').
weekday('Saturday').
weekday('Sunday').
Similarly, the days of the month are the terms 1, 2, ..., 31 (abbreviated here):

day_of_month(1).
day_of_month(2).
% ...
day_of_month(31).
Now a structured type. A date is a weekday together with a day of the month, so the set of terms that we want is date('Monday',23), date('Tuesday',24), and so on, and the definition is one clause that uses the two other types:

date(date(W,D)) :- weekday(W), day_of_month(D).
We can check whether something is a date:

?- date(date('Monday',31)).
Expected answer:

yes
?- date(date('Monday',23)).
Expected answer:

no
The answer is no because in this abbreviated version only 1, 2 and 31 are days of the month. Something that is not a date at all is of course rejected too:

?- date(date(a,31)).
Expected answer:

no
And with a variable the definition generates dates, that is, the terms of the type:

?- date(D).
Expected answer:

D = date('Monday',1) ? ;
D = date('Monday',2) ? ;
D = date('Monday',31) ? ;
D = date('Tuesday',1) ?
This gives us "property-based" testing for free.

These types are part of the program. There is no separate type language, and no separate declaration syntax. They are written in the same language, and therefore they run: a type definition is at the same time a definition, a checker, and a generator.

Recursive types

date/1 is not recursive: it is a structure with two fields. But a type definition may call itself, and this is what allows a finite program to describe infinitely many terms. The first example is that of the natural numbers: the terms 0, s(0), s(s(0)), ...

A frequent question: what is s, and does it have to be defined? Nothing has to be done at all. s is just a functor: writing it makes it one, and it behaves as one, building terms. s(0) is a data structure whose only field contains 0, and s(s(0)) is a data structure whose only field contains that.

These terms are not functions: they are not called, they are not interpreted, and they do not run. nat/1, on the other hand, is a predicate: it is called, it runs, and it does things.

:- module(_,_,[sr/bfall]).

nat(0).
nat(s(X)) :- nat(X).

% Integers: a natural, possibly with a minus in front
% (integer/1 is the name of a Prolog built-in, so we call it pint/1).
pint( X) :- nat(X).
pint(-X) :- nat(X).
nat/1 has two clauses: one unit clause, and one recursive clause with a single body literal. It is about the minimal recursive predicate that there is. The second type, the integers, represents the terms 0, s(0), -s(0), s(s(0)), ... (Zero is represented twice, as 0 and as -0. This duplication is not a problem, but can you eliminate it?)

?- nat(s(s(s(0)))).
Expected answer:

yes
Types are runnable, and they can be used to check values or to produce them: the same definition generates the natural numbers if it is called with a variable:

?- nat(X).
Expected answer:

X = 0 ? ;
X = s(0) ? ;
X = s(s(0)) ? ;
X = s(s(s(0))) ?

What does it cost?

We can also reason about the complexity of a predicate, for a given class of queries (a mode). It is worth doing it once, carefully, because the reasoning applies to every recursive predicate that follows.

Take the call nat(s(s(s(0)))). It does not match nat(0). It matches nat(s(X)) with X = s(s(0)), and calls nat(s(s(0))). This matches again with X = s(0), then again with X = 0, and finally nat(0) matches the base case.

How many steps are there? As many as there are ss: one for each level, plus the base case. So in this mode, with the argument fully instantiated, the predicate is linear in the size of the number, and it is a loop: it goes down the structure checking that everything is an s and that there is a 0 at the bottom. It could not be done in fewer steps: to be sure that the term really is a natural number, all of it has to be looked at.

The other direction is quite different. Take nat(X), with X free. How many steps does the first answer take? One: the call matches the base case and returns X = 0.

If we then type ;, the system backtracks to the choice point that it left behind, takes the second clause instead (with its variable renamed to X1), binds X to s(X1), and calls nat(X1), which matches the base case, so that the answer is s(0). Again one step.

So generating solutions costs one step per solution, and not n steps for the n-th one. A different mode has a different cost, and in both cases the cost can be worked out, exactly as in an ordinary program.

One base case, and one recursive case: the same two-clause shape as ancestor/2, and as everything that follows.

Arithmetic, the pure way (Slides 50–53)

With nat/1 we can define arithmetic itself, with no built-ins and no evaluation: just relations.

% :- module(_,_,[sr/bfall]).
:- module(_,_,[]).

nat(0).
nat(s(X)) :- nat(X).

The order relation

less_or_equal(0,X) :- nat(X).
less_or_equal(s(X),s(Y)) :- less_or_equal(X,Y).
Again two clauses. Zero is less than or equal to any natural number; and if X is less than or equal to Y, then adding one to both does not change the relation. This is enough.

Note that less_or_equal/2 is a predicate, a relation, and not a function, which is why it can be used in several ways:

?- less_or_equal(s(0),s(s(0))).
Expected answer:

yes
?- less_or_equal(X,0).
Expected answer:

X = 0 ? ;
no
Zero, and nothing else, which is correct. Used in this way it is a generator, and it can have several solutions:

?- less_or_equal(X,s(0)).
Expected answer:

X = 0 ? ;
X = s(0) ? ;
no
And in the other direction there are infinitely many solutions, which are enumerated one by one:

?- less_or_equal(s(s(0)),Y).
Expected answer:

Y = s(s(0)) ? ;
Y = s(s(s(0))) ? ;
Y = s(s(s(s(0)))) ? ;
Y = s(s(s(s(s(0))))) ?
Both arguments can even be left free, asking for the relation itself with less_or_equal(X,Y). This set is infinite, so the enumeration never finishes, and how good it is depends on the search rule. This program runs depth-first (its first line is :- module(_,_,[]).), so the answers are (0,0), (0,1), (0,2), ..., always with X = 0: the search stays in one infinite branch. With the breadth-first rule (the commented-out line, sr/bfall) the enumeration reaches every pair. The logic is complete; the default control is not.

Addition

plus(0,Y,Y) :- nat(Y).
plus(s(X),Y,s(Z)) :- plus(X,Y,Z).
plus/3 is a relation between three numbers, and not a function of two. Forwards, it adds:

?- plus(s(s(0)),s(0),Z).
Expected answer:

Z = s(s(s(0))) ? ;
no
With the result given, it subtracts:

?- plus(s(0),Y,s(s(s(0)))).
Expected answer:

Y = s(s(0)) ? ;
no
A question that has no answer gets a no, correctly, since there is no natural number that added to 2 gives 1:

?- plus(s(s(0)),Y,s(0)).
Expected answer:

no
And with only the result given, it enumerates all the ways of reaching it:

?- plus(X,Y,s(s(s(0)))).
Expected answer:

X = 0,
Y = s(s(s(0))) ? ;

X = s(0),
Y = s(s(0)) ? ;

X = s(s(0)),
Y = s(0) ? ;

X = s(s(s(0))),
Y = 0 ?
One definition of addition is at the same time addition, subtraction, and the enumeration of the ways of splitting a number.

The same thing can often be defined in more than one way. Addition could equally well be defined by recursion on the second argument instead of the first:

plus(X,0,X) :- nat(X).
plus(X,s(Y),s(Z)) :- plus(X,Y,Z).
The meaning of plus/3 is the same, and it would still be the same if both definitions were put together in one program. But this is not recommended: there would be several proof trees for the same query, and each solution would be computed more than once, which is neither efficient nor concise. We look for minimal axiomatizations.

This is the beginning of a theme: the art of logic programming is finding formulations that are both compact and computationally efficient. A logic program is beautiful when it corresponds almost directly to the definition of what we are trying to construct and is an efficient program. Sometimes we cannot have both; often we can, and it is very satisfying when it happens.

A good exercise at this point is to define times(X,Y,Z) (multiplication is repeated addition), exp(N,X,Y) (exponentiation is repeated multiplication), factorial(N,F) and minimum(N1,N2,Min). They are in the exercises at the end of this section.

The remainder: writing the specification

Let us define mod(X,Y,Z): "Z is the remainder of dividing X by Y", i.e., there exists a Q such that X = Y*Q + Z, and Z < Y. For the program we can then simply write the specification:

:- module(_,_,[sr/bfall]).

mod(X,Y,Z) :- 
    less(Z, Y), 
    times(Y,_Q,W), 
    plus(W,Z,X).
To try it with larger numbers, we first define s as a prefix operator at the top level, which saves us from writing the parentheses. From this point on, and for the rest of the session, the top level also uses the operator when it prints the answers, so that s(s(0)) is shown as s s 0. (The expected answers below are written in this way. If the declaration has not been run, the same answers are printed with parentheses.)

?- op(100,fy,s).
Expected answer:

yes
?- mod(s s s s s s s s s s s s s 0, s s s 0, Z).
Expected answer:

Z = s 0 ?
That is, 13 mod 3 is 1. (Do not ask for another solution here: there is none, but the search for it does not terminate.) Running it backwards, which numbers Y leave a remainder of 1 when 13 is divided by them?

?- mod(s s s s s s s s s s s s s 0, Y, s 0).
Expected answer:

Y = s s 0 ? ;
Y = s s s 0 ? ;
Y = s s s s 0 ? ;
Y = s s s s s s 0 ?
These are 2, 3, 4 and 6. The last such number, 12, takes much longer to appear with this definition (it had not appeared after 25 seconds). Here is another possible definition, which subtracts Y repeatedly:

mod2(X,Y,X) :- 
    less(X, Y).
mod2(X,Y,Z) :- 
    plus(X1,Y,X), 
    mod2(X1,Y,Z).
?- mod2(s s s s s s s s s s s s s 0, s s s 0, Z).
Expected answer:

Z = s 0 ? ;
no
?- mod2(s s s s s s s s s s s s s 0, Y, s 0).
Expected answer:

Y = s s s s s s s s s s s s 0 ? ;
Y = s s s s s s 0 ? ;
Y = s s s s 0 ? ;
Y = s s s 0 ?
This second definition is much more efficient than the first one in several query modes: note that in the last query it finds 12 immediately, and that it terminates when there are no more solutions. Both definitions use these auxiliary predicates:

less(0,s(X)) :- nat(X).
less(s(X),s(Y)) :- less(X,Y).

plus(0,Y,Y) :- nat(Y).
plus(s(X),Y,s(Z)) :- plus(X,Y,Z).

times(0,Y,0) :- nat(Y).
times(s(X),Y,Z) :- plus(W,Y,Z), times(X,Y,W).

nat(0).
nat(s(X)) :- nat(X).

Functions as predicates: Ackermann's function

Functions can be coded as predicates with one more argument, which represents the "output". Ackermann's function is the standard example of something that is easy to define and expensive to compute. In a functional language it is three equations:

fun ack 0 n = n + 1
  | ack m 0 = ack (m - 1) 1
  | ack m n = ack (m - 1) (ack m (n - 1))
or, using Peano arithmetic:

datatype nat = Zero | S of nat

fun ack 0 n = S n
  | ack (S m1) 0 = ack m1 (S 0)
  | ack (S m1) (S n1) = ack m1 (ack (S m1) n1)
As plain clauses, a function of two arguments becomes a predicate of three:

:- module(_,_,[rtchecks,modes]).

ack(0,N,s(N)).
ack(s(M),0,Val)    :- ack(M,s(0),Val).
ack(s(M),s(N),Val) :- ack(s(M),N,Val1), ack(M,Val1,Val).
?- ack(s s 0, s s s 0, X).
Expected answer:

X = s s s s s s s s s 0 ? ;
no
There are nine ss: A(2,3) = 9, which is the answer that the equations give. But, being a predicate rather than a function, it of course also runs "backwards", which the functional definition cannot do at all. It can check a result:

?- ack(s s 0, s s s 0, s s s s s s s s s 0).
Expected answer:

yes
and it can answer which second argument gives 9?, or which first argument. (In these two queries there is only one solution, and asking for another one starts a search that does not terminate.)

?- ack(s s 0, Y, s s s s s s s s s 0).
Expected answer:

Y = s s s 0 ?
?- ack(X, s s s 0, s s s s s s s s s 0).
Expected answer:

X = s s 0 ?
If we want to enforce that the predicate can only be called as a function, that is, with all the arguments as inputs and only the last one as output, we can use an assertion, for example :- pred ack(+,+,-). Placing this assertion above the predicate and loading the program again, a call to ack/3 in any other mode gives an error. The check is done by the rtchecks package, which this program loads.

Exercises

Put it into practice! Exercises: times, exp, factorial, and minimum — opens in a separate notebook.

Functional syntax (Slides 54–57)

Writing plus(X, Y, Z) everywhere is faithful to the relational reading, but it is verbose when we only want a value. There is much syntactic sugar available for functional notation, which allows writing the same predicates as functions, without changing what they are.

The fsyntax package (:- use_package(fsyntax).) provides:

  • ~, "eval", which makes the last argument implicit. It allows writing, for example, p(X,Y) :- q(X,Z), r(Z,Y). as p(X,Y) :- r(~q(X),Y). or even as p(X,~r(~q(X))).
  • := for definitions, which allows writing the same clause as p(X) := Y :- r(~q(X),Y). or as p(X) := ~r(~q(X)).
  • | for or, and some other notations.
Thus, for example, this clause of the Ackermann predicate:

ack(s(M),s(N),Val) :-
    ack(s(M),N,Val1), ack(M,Val1,Val).
can be rewritten (using also :- op(100,fy,s).) as:

ack(s M, s N) := ~ack(M, ~ack(s M, N) ).
The ~ can also be avoided. To evaluate automatically the functors that are defined as functions:

:- fun_eval ack/2.
ack(s M, s N) := ack(M, ack(s M, N) ).
To enable this for all the functions defined in a file, we write :- fun_eval defined(true). And to evaluate the arithmetic functors automatically:

:- fun_eval arith(true).
add_one(X,X+1).
The functional package (:- use_package(functional).) includes fsyntax and both of these fun_eval declarations.

Two things to know before using these declarations. :- fun_eval is positional: it takes effect from the place where it appears downwards, so any clause that uses the functors as functions has to come after it. And since :- use_package(functional) turns on arith(true), it takes over +, -, * and /: a program that defines its own versions of these has to add :- fun_eval arith(false).

Ackermann's function (with Peano numbers) in functional syntax needs the package:

:- module(_,_,[]).

:- use_package(functional).
the declaration of s as a prefix operator, so that the Peano numbers need no parentheses:

:- op(100,fy,s).
and the function itself:

ack(  0,   N) := s N.
ack(s M,   0) := ack(M, s 0).
ack(s M, s N) := ack(M, ack(s M, N) ).
This is the same predicate ack/3 as before (:= only says that the last argument is the result), so it gives the same answers, and it also runs in the other modes (with the same caution about asking for more solutions in the second query):

?- ack(s s 0, s s s 0, X).
Expected answer:

X = s s s s s s s s s 0 ? ;
no
?- ack(s s 0, Y, s s s s s s s s s 0).
Expected answer:

Y = s s s 0 ?
op/3 in a file is local to its module, so the queries typed at the top level need their own ?- op(100,fy,s). (which we ran above) before s s 0 can be read. Written with parentheses, as s(s(0)), the numbers need nothing.

Functional syntax is also convenient for defining types. Starting from

nat(0).
nat(s(X)) :- nat(X).
and using the := notation for the "return" (last) argument, we have:

nat := 0.
nat := s(X) :- nat(X).
The body call can be moved to the head with the ~ notation ("evaluate, and replace with the result"), and with the functional package even the ~ is not needed inside the definition of the function itself:

nat := 0.
nat := s(~nat).
nat := 0.
nat := s(nat).

With the declaration of s as a prefix operator the parentheses go away too, and | (disjunction) writes the two clauses as one:

nat := 0.
nat := s nat.
nat := 0 | s nat.

This last line is a type definition as compact as the datatype of a functional language, and it is exactly equivalent to the two clauses that we started with:

:- module(_,_,[functional]).

:- op(100,fy,s).

nat := 0 | s nat.
?- nat(s s 0).
Expected answer:

yes
?- nat(a).
Expected answer:

no
?- nat(X).
Expected answer:

X = 0 ? ;
X = s 0 ? ;
X = s s 0 ? ;
X = s s s 0 ?

Exercises

Put it into practice! Exercise: arithmetic operators as functions — opens in a separate notebook.

Lists

Lists as a recursive type (Slides 58–59)

Lists are not built in. They are ordinary terms, and it is worth building one by hand once, so that the syntactic sugar is never a mystery.

A list is a binary structure: a pair whose first argument is an element and whose second argument is the rest of the list. To build it we need two things:

  • a constant for the empty list, so that we know where to stop, exactly as with 0 in nat/1. It is written [], and it is a constant, no more mysterious than a;
  • a functor of arity 2 for the pair. Traditionally this is the dot, ./2 (which is overloaded, and best avoided in our own programs, but this is the convention).
So a list of one element is a pair with a on the left and [] on the right, and a list of two elements has a second pair nested inside the first one:

?- X = '.'(a,'.'(b,[])).
Expected answer:

X = [a,b] ?
We have built a nested structure of pairs, and the system has printed it as [a,b], because it recognizes the dot and applies the syntactic sugar.

The two layers of syntactic sugar

The first layer is that .(H,T) can be written as [H|T], where H is called the head and T the tail of the list. The bar always separates the left part of the pair from the right one. The second layer is that a right part that is itself a list can be flattened, using commas:

formal object "cons pair" syntax "element" syntax
.(a,[])[a|[]][a]
.(a,.(b,[]))[a|[b|[]]][a,b]
.(a,.(b,.(c,[])))[a|[b|[c|[]]]][a,b,c]
.(a,X)[a|X][a|X]
.(a,.(b,X))[a|[b|X]][a,b|X]

The last two rows are the important ones. [a|X] is a list whose first element is a and whose tail is unknown: it cannot be written in pure element syntax, and it does not have to be. [a,b|X] starts with a, then b, and then it is open. Lists with a variable as tail are extremely useful, and we will see much more of them in the part on Prolog, under the name of difference lists.

Unification on lists

There is nothing new here, since it is the same unification, but it is worth doing a few cases by hand, because this is how every list predicate works.

?- [a,b,c] = [X|Y].
Expected answer:

X = a,
Y = [b,c] ?
If this is not obvious, write it with dots: .(a,.(b,.(c,[]))) against .(X,Y). The dot matches the dot, a matches X, and Y matches everything on the right of the first pair, which is the list [b,c]. In the same way, [a,b] and [a|X] unify with X = [b], and [a] and [a|X] unify with X = [].

?- [a] = [a,b|X].
Expected answer:

no
[a] has one pair, and [a,b|X] needs at least two. For the same reason, [] and [X] do not unify. And now one that is easy to get wrong. What is X here?

?- [a,b] = [X|_].
Expected answer:

X = a ?
It is a, and not [a,b]: [X|_] is a pair, so X is the head. And a longer one, which mixes both notations:

?- [a,b,c,d,e,f,g] = [X,Y|Z].
Expected answer:

X = a,
Y = b,
Z = [c,d,e,f,g] ?
Everything that we do is resolution and unification, and nothing else. Every operation on lists, trees, arrays and any other data structure is this one =, the unification of first-order logic.

The type definition

The type is written exactly like nat/1. Without syntactic sugar it would be

list([]).
list(.(X,Y)) :- list(Y).
and, with the list notation:

:- module(_,_,[]).

list([]).
list([_|Y]) :-
     list(Y).
There is one base case and one recursive case: the same shape as everything so far. Once this is seen, every list predicate almost writes itself: we say what holds for [], and then what holds for [H|T], given that it holds for T.

?- list([a,b,c]).
Expected answer:

yes
?- list(a).
Expected answer:

no
And, as with every type, we can "explore" it, generating its terms:

?- list(L).
Expected answer:

L = [] ? ;
L = [_] ? ;
L = [_,_] ? ;
L = [_,_,_] ?
The same type can be written with the functional notation:

:- use_package(functional).

lst := [] | [_|lst].
?- lst([a,b,c]).
Expected answer:

yes

Membership in a list (Slide 60)

We want to define when X is a member of the list Y. As in the introduction, we can write down some of the facts that we want to hold, and generalize. Facts such as member(a,[a]), member(b,[b,d]) or member(a,[a,c,d]), in which the element is the first one of the list, are all covered by

member(X,[X|Y]) :- list(Y).

and facts such as member(a,[c,a]) or member(b,[s,t,b]), in which the element is further inside, are covered by

member(X,[Y|Z]) :- member(X,Z).

This is the resulting definition (called list_member/2 here, so as not to clash with the library predicate member/2):

:- module(_,_,[]).
% :- use_package(sr/bfall).

% list_member(X,L): X is a member of list L
list_member(X,[X|L]) :-
    list(L).
list_member(X,[_|T]) :- 
    list_member(X,T).

list([]).
list([_|T]) :-
     list(T).
That is: X is a member of a list whose head is X, or of a list of whose tail it is a member. It can be used to check whether an element is in a list:

?- list_member(b,[a,b,c]).
Expected answer:

yes
to find the elements of a list:

?- list_member(X,[a,b,c]).
Expected answer:

X = a ? ;
X = b ? ;
X = c ? ;
no
and even to generate lists that contain a given element:

?- list_member(a,Y).
Expected answer:

Y = [a] ? ;
Y = [a,_] ? ;
Y = [a,_,_] ? ;
Y = [a,_,_,_] ?

Length, sum, and typed lists (Slide 61)

Lists and natural numbers can be combined. list_length(L,N) holds if N is the length of the list L. It counts in Peano numbers, because we are still being pure:

:- module(_,_,[]).
% :- use_package(sr/bfall).

list_length([],0).
list_length([_|T],s(N)) :-
    list_length(T,N).
?- list_length([a, b, c], N).
Expected answer:

N = s s s 0 ? ;
no
Backwards, it builds a list of a given length:

?- list_length(L, s(s(s(0)))).
Expected answer:

L = [_,_,_] ? ;
no
but only for Peano numbers, since those are the numbers that it knows about:

?- list_length(L, 3).
Expected answer:

no
sumlist(L,N) holds if N is the sum of all the elements of L:

sumlist([],0).
sumlist([H|T],S) :-
    sumlist(T,ST),
    add(ST,H,S).
?- sumlist([s(0),s(s(s(0))),s(s(0))],N).
Expected answer:

N = s s s s s s 0 ? ;
no
And natlist(L) is a new type, the lists of natural numbers:

natlist([]).
natlist([X|Y]) :- 
    nat(X),
    natlist(Y).
or, using functional notation:

:- use_package(fsyntax).
natlst := [] | [~nat|~natlst].
These predicates can also be run in other modes, for example ?- list_length(L, N)., ?- sumlist(L,s(s(s(0)))). or ?- sumlist(L,S). It is interesting to try them, and to try them again after uncommenting the line :- use_package(sr/bfall). at the top of the program, which selects the breadth-first rule. The auxiliary predicates used above are:

list([]).
list([_|T]) :-
     list(T).

add(0,X,X) :- 
     nat(X).
add(s(X),Y,s(Z)) :- 
     add(X,Y,Z).

nat(0).
nat(s(X)) :- 
     nat(X).

One way to understand recursive programs (Slide 62)

When writing a recursive predicate over a list, we ask two questions, and write one clause for each:

  1. What holds for the empty list? This is the base case.
  2. Assuming that it holds for the tail, what holds for [H|T]? This is the recursive case.
There is no need to think about how the recursion unfolds: we assume that the recursive call works, and say what to do with its result. All the predicates of this section were written in this way.

To understand how such a program runs, on the other hand, one way to visualize recursion is as the creation of "copies" of the procedure, each with its own variables:

?- sumlist([s(0), s(s(0)), s(0)], R).
         \
          sumlist([],0).       % fails
          sumlist([H|T],S) :-  % H=s(0), T=[s(s(0)), s(0)]
              sumlist(T,ST),   % T=[s(s(0)), s(0)]
              |      \
              |       sumlist([],0).       % fails
              |       sumlist([H1|T1],S1) :-  % H1=s(s(0)), T1=[s(0)]
              |          sumlist(T1,ST1),     % T1=[s(0)]
              |          |       \
              |          |        sumlist([],0).          % fails
              |          |        sumlist([H2|T2],S2) :-  % H2=s(0), T2=[]
              |          |           sumlist(T2,ST2),     % T2=[]
              |          |           |      \
              |          |           |       sumlist([],0).      % ST2=0
              |          |           |      /
              |          |           % H2=s(0), ST2=0
              |          |           add(ST2,H2,S2). % S2=s(0)
              |          |          /
              |          % H1=s(s(0)), ST1=s(0)
              |          add(ST1,H1,S1). % S1=s(s(s(0)))
              |         /
              % H=s(0), ST=s(s(s(0)))
              add(ST,H,S). % S=s(s(s(s(0))))
             /
            % R=s(s(s(s(0))))

Append (Slides 63–65)

This is the central list predicate, and the best illustration of what a relation gives us. Let us build it by generalization again. The base case covers facts such as append([], [a], [a]) and append([], [a,b], [a,b]):

append([], Ys, Ys) :- list(Ys).

For the rest of the cases, a first step is to generalize facts such as append([a], [b], [a,b]) to append([X], Ys, [X|Ys]) :- list(Ys)., and facts such as append([a,b], [c], [a,b,c]) to append([X,Z], Ys, [X,Z|Ys]) :- list(Ys)., and so on. But this is still an infinite set of clauses, one for each length of the first list, so we need to generalize more:

append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).

Another way of reaching the same clause is to think inductively: the base case is append([],Ys,Ys) :- list(Ys). and, if we assume that append(Xs,Ys,Zs) holds, then with one more element at the front append([X|Xs],Ys,[X|Zs]) should hold. This is the resulting program (called list_append/3 here):

:- module(_,_,[]).

% `append(X,Y,Z)`: list `Z` is `X` and `Y` concatenated.
list_append([],L,L) :- 
    list(L).
list_append([X|Xs],Ys,[X|Zs]) :- 
    list_append(Xs,Ys,Zs).

list([]).
list([_|Y]) :-
     list(Y).
There are two clauses. Appending the empty list and L gives L; and appending [X|Xs] and Ys gives a list whose head is X and whose tail is the result of appending Xs and Ys.

?- list_append([a,b], [c], X).
Expected answer:

X = [a,b,c] ?
This is the expected use. But nothing in the program says which arguments are the inputs:

?- list_append(X, Y, [a,b,c]).
Expected answer:

X = [],
Y = [a,b,c] ? ;

X = [a],
Y = [b,c] ? ;

X = [a,b],
Y = [c] ? ;

X = [a,b,c],
Y = [] ?
All the ways of splitting [a,b,c] into two lists, and then no. The same two clauses are concatenation, splitting, testing for a prefix and testing for a suffix, depending on which arguments are given.

Between these two extremes there are the intermediate modes, which are worth trying by hand. Checking a result:

?- list_append([a,b], [1,2], [a,b,1,2]).
Expected answer:

yes
And list subtraction, on either side. What has to be appended to [a,b] to obtain [a,b,1,2]?

?- list_append([a,b], Y, [a,b,1,2]).
Expected answer:

Y = [1,2] ?
And, the other way around, to what does [c] have to be appended to obtain [a,b,c]?

?- list_append(X, [c], [a,b,c]).
Expected answer:

X = [a,b] ?
This is relational programming, not functional programming. list_append/3 is not a function that takes inputs and gives an output: it is a relation, more like a table, of which we can give any of the columns and ask for the rest.

Reverse, and the cost of getting it wrong (Slides 66–67)

Reversing a list is the classic demonstration that two correct programs can have very different costs. The objective is reverse(Xs,Ys), where Ys is the list Xs reversed.

The first version is basically the definition, thinking recursively. The empty list reversed is the empty list (the base case):

:- module(_,_,[]).

reverse_naive([],[]).
And, for the inductive step, if we assume that Xs has already been reversed as Zs, then to reverse [X|Xs], which has one more element at the beginning, X has to go at the end of Zs:

reverse_naive([X|Xs],Ys) :- 
    reverse_naive(Xs,Zs),
    list_append(Zs,[X],Ys).
Let us follow one step by hand, assuming that the recursion works (which is the way to think about recursive programs). Given [1,2,3], the head unification gives X = 1 and Xs = [2,3]. We assume that reverse_naive([2,3],Zs) works and gives Zs = [3,2]. Then the singleton list [1] is appended to it, and Ys = [3,2,1].

?- reverse_naive([1,2,3], Y).
Expected answer:

Y = [3,2,1] ?
It also runs backwards, which is not so obvious (but only for the first solution: there is no other one, and the search for it does not terminate):

?- reverse_naive(X, [3,2,1]).
Expected answer:

X = [1,2,3] ?
If we ask for the relation itself, with ?- reverse_naive(X,Y)., something rather nice happens. The answers come out as patterns:

X = [],       Y = []       ? ;
X = [_A],     Y = [_A]     ? ;
X = [_B,_A],  Y = [_A,_B]  ? ;
...
They contain variables, so each answer stands for infinitely many cases. The third one says that any list of two elements is reversed by swapping them, which includes reverse_naive([f(a),g(b)], Y), with Y = [g(b),f(a)]. The program is enumerating the shape of the relation, and not just instances of it.

As defined, this predicate is very inefficient. Another possible definition uses an accumulating parameter:

reverse_acc(Xs,Ys) :- 
    reverse_(Xs,[],Ys).

reverse_([],Ys,Ys).
reverse_([X|Xs],Acc,Ys) :-
    NewAcc=[X|Acc],
    reverse_(Xs,NewAcc,Ys).
?- reverse_acc([1,2,3], Y).
Expected answer:

Y = [3,2,1] ?
(Both definitions use list_append/3 and list/1, which are in the same file:)

% append(X,Y,Z): list `Z` is `X` and `Y` concatenated.
list_append([],L,L) :- 
    list(L).
list_append([X|Xs],Ys,[X|Zs]) :- 
    list_append(Xs,Ys,Zs).

list([]).
list([_|Y]) :-
     list(Y).

Why the naive version is slow

reverse_naive/2 calls list_append/3 once for each element, and each of those calls traverses the list that it is given, so the cost is quadratic in the length of the list. reverse_acc/2 visits each element once, so it is linear.

There is a second, related problem, which has to do with the shape of the recursion rather than with the number of steps. In reverse_naive/2 the recursive call is not the last goal: after it returns, the list_append/3 still has to be done. This means that the system has to remember to come back, which means pushing a frame on the stack for every level of the recursion.

A predicate is tail recursive when the recursive call is the very last goal, with nothing after it. In that case the local variables are no longer needed when the call is made, the frame can be discarded, and the recursion runs as a loop, in constant stack space. Prolog systems implement tail recursion well, so this is a real difference, and not just a theoretical one.

The accumulating parameter

The trick for turning a predicate that is not tail recursive into one that is, is a standard technique, in functional programming as well as in logic programming: add an extra argument, and build the answer in it on the way down.

In reverse_/3 the goal NewAcc = [X|Acc] is written separately for clarity; putting [X|Acc] directly in the recursive call is exactly equivalent.

Let us follow [1,2,3] down. The accumulator starts as [].

XsAccNewAcc
call 1 [2,3][][1]
call 2 [3][1][2,1]
call 3 [][2,1][3,2,1]
call 4 [][3,2,1](matches reverse_([],Ys,Ys))

Each step puts the current head at the front of what has been accumulated. It may seem that putting elements at the front is not reversing, but note that the accumulator is passed downwards, and not returned upwards: the element that is added last ends up at the front, which is exactly the reversal.

The base case is where the result comes out: reverse_([],Ys,Ys) unifies the finished accumulator with the output argument, and this answer is then passed back, unchanged, through all the pending calls.

The accumulator is worth recognizing as a pattern, because it appears everywhere: instead of combining the result after the recursive call, a partial result is passed into it, and it is handed out in the base case. The same device appears again in the part on Prolog, as the tail recursive compute_length_/3, and in the N-queens program of the part on constraints.

Measuring the difference

That one version is quadratic and the other linear is a claim, so let us measure. The harness uses two things from Prolog rather than from pure logic programming, which is fine, since we are measuring and not specifying:

:- use_module(engine(runtime_control),[statistics/2]).
:- use_module(library(lists),[length/2]).

rev_naive(N,T) :-
    length(L,N),
    statistics(walltime, [_,_]),
    reverse_naive(L,_RL),
    statistics(walltime, [_,T]).
statistics(walltime,[_,T]) gives, in its second element, the time since the previous call to statistics/2. So calling it once to reset, and then again after the work, measures the interval.

The other trick is length/2 run backwards. length([1,2,3],N) gives N = 3, as expected; but length(L,3) creates a list of three fresh variables, [_A,_B,_C]. This is a very convenient way of producing a list of an arbitrary length for benchmarking, and another example of a relation that is useful in a direction that its name does not suggest.

For naive reverse, doubling the length of the list each time (times in milliseconds, measured with Ciao 1.26 running natively on a laptop, and averaged over many runs):

length of the listtime (ms)
1000.034
2000.13
4000.74
8003.2
160013

When the length doubles, the time is multiplied by roughly four: this is quadratic.

For the version with the accumulator, over a much wider range:

length of the listtime (ms)
10000.010
100000.10
1000001.2

With ten times the length, the time is roughly ten times longer: this is linear. Note also the scale: with 800 elements the naive version already takes longer than the version with the accumulator takes with 100,000. This is not a small constant factor: it is a different algorithm.

Both programs are correct, and both are short. Logic tells us what a program computes; it does not tell us what it costs. That is the other half of the subject.

Exercises

Put it into practice! Exercise: comparing the efficiency of the two reverse/2 definitions — opens in a separate notebook.

Some additional exercises on lists: define prefix(X,Y) (the list X is a prefix of the list Y), suffix(X,Y), sublist(X,Y) (in various ways), select(L,X,NL) (NL is L minus one occurrence of X), delete(L,X,NL) (NL is L minus all the occurrences of X), and permutation(X,Y).

Put it into practice! Some exercises on lists: prefix, suffix, sublist, ... — opens in a separate notebook.

Trees, symbols and graphs

Binary trees (Slides 68–69)

The recipe is the same as for lists, with one more dimension.

For lists we used a pair, a functor of arity 2. For a binary tree we need a functor of arity 3, tree(Element,Left,Right): the contents of the node, the left subtree, and the right subtree. We also need a constant for the empty tree, as [] was for the empty list. We call it void, although the name is arbitrary.

A binary tree is the empty tree, or a node with an element and two subtrees:

:- module(_,_,[doccomments]).
% :- use_package(sr/bfall).

%! binary_tree(T): `T` is a binary tree.
binary_tree(void).
binary_tree(tree(_Element,Left,Right)) :- 
    binary_tree(Left),
    binary_tree(Right).
Note what the type definition does not say: it says nothing at all about the element. A node may contain anything. What the definition constrains is the shape: the two branches must themselves be binary trees.

The following tree, which is in the same file, is used in the examples:

%! tree_example(T): `T` is a tree example.
tree_example(  tree( a,
                   tree( b,
                         void,
                         void
                       ),
                   tree( c,
                         tree( b,
                               void,
                               void
                               ),
                         void
                       )
                   )).
We can check that it is indeed a tree, that is, that it is of the type:

?- tree_example(_T), binary_tree(_T).
Expected answer:

yes
And we can check things that are not trees:

?- binary_tree(tree(a, b, c)).
Expected answer:

no
b and c are not trees: this is the same mistake as writing [a|b] and expecting a list.

?- binary_tree(tree(a, [], [])).
Expected answer:

no
The answer is no because the empty tree is void here, and not []. The type is exactly what we defined it to be.

As always, the type also generates:

?- binary_tree(T).
Expected answer:

T = void ? ;
T = tree(_,void,void) ? ;
T = tree(_,void,tree(_,void,void)) ? ;
T = tree(_,void,tree(_,void,tree(_,void,void))) ?
The _ in the answers says that the contents of the nodes do not matter: anything of that shape is a binary tree. This program runs depth-first, so the enumeration only grows the tree along one branch. After uncommenting the line :- use_package(sr/bfall). at the top of the program, it is an orderly enumeration of all the binary trees, by size.

The same type, in functional notation:

:- use_package(functional).

%! binary_t(T): `T` is a binary tree.
binary_t := void | tree(_Element, ~binary_t, ~binary_t). 
Membership. tree_member/2 needs three clauses where the list version needed two, because there are two subtrees to look in, instead of one tail. Otherwise the idea is the same: X is in the tree if it is the contents of this node, or if it is somewhere in the left branch, or if it is somewhere in the right branch.

%! tree_member(X,T): `X` is a node in binary tree `T`.
tree_member(X,tree(X,Left,Right)) :- 
    binary_tree(Left),
    binary_tree(Right).
tree_member(X,tree(_,Left,_Right)) :- 
    tree_member(X,Left). 
tree_member(X,tree(_,_Left,Right)) :- 
    tree_member(X,Right).
?- tree_example(_T), tree_member(X, _T).
Expected answer:

X = a ? ;
X = b ? ;
X = c ? ;
X = b ?
The members of the example tree are a, b, c and b again: the tree contains b twice, and the program finds both occurrences. Logically the two answers are the same answer; operationally it is quite useful to be told that there are two.

?- tree_example(_T), tree_member(e, _T).
Expected answer:

no
And, backwards, it generates trees that contain a given element:

?- tree_member(e,T).
Expected answer:

T = tree(e,void,void) ? ;
T = tree(e,void,tree(_,void,void)) ? ;
T = tree(e,void,tree(_,void,tree(_,void,void))) ? ;
T = tree(e,void,tree(_,void,tree(_,void,tree(_,void,void)))) ?
Again, there is a version in functional notation:

tree_mem_f(X) := tree( X, ~binary_tree,   ~binary_tree)
               | tree(_Y, ~tree_mem_f(X), _Right)
               | tree(_Y, _Left,          ~tree_mem_f(X)).
?- tree_example(_T), tree_mem_f(X, _T).
Expected answer:

X = a ? ;
X = b ? ;
X = c ? ;
X = b ?
Traversal turns a tree into a list. pre_order(Tree,Elements) holds if Elements is a list with the elements of Tree traversed in preorder:

:- module(_,_,[doccomments]).

%! \title Binary tree example: preorder

%! pre_order(T,L): `L` is the list of elements of tree `T` in preorder. 
pre_order(void, []).
pre_order(tree(X,Left,Right), Elements) :-
    pre_order(Left, ElementsLeft),
    pre_order(Right, ElementsRight),
    list_append([X|ElementsLeft], ElementsRight, Elements).
?- tree_example(_T),pre_order(_T,List).
Expected answer:

List = [a,b,c,b] ?
The shape of the clause decides which traversal we get. If the X is moved from the front of the append to between the two lists we have the in-order traversal, and if it is moved to the end, the post-order one: the traversal is the position of one symbol. Defining in_order/2 and post_order/2 is the exercise below. The auxiliary predicates of this program are:

list_append([],L,L) :- 
    list(L).
list_append([X|Xs],Ys,[X|Zs]) :- 
    list_append(Xs,Ys,Zs).

tree_example(  tree( a,
                   tree( b,
                         void,
                         void
                       ),
                   tree( c,
                         tree( b,
                               void,
                               void
                               ),
                         void
                       )
                   )).

Exercises

Put it into practice! Exercises: in_order and post_order — opens in a separate notebook.

Polymorphism (Slide 70)

list_member/2 and tree_member/2 do the same job on different types. Their two definitions can be used simultaneously, as a single polymorphic predicate whose clauses cover both shapes:

:- module(_,_,[doccomments,sr/bfall]).

%! \title Polymorphic member

%! \module We can write a 'polymorphic' member that works 
% with both lists and trees. 

%! lt_member(X,LT): X is a member of the list or tree LT.

lt_member(X, [X|Y]) :- list(Y).
lt_member(X, [_|T]) :- lt_member(X, T).

lt_member(X, tree(X, L, R)) :- binary_tree(L), binary_tree(R).
lt_member(X, tree(_Y, L, _R)) :- lt_member(X, L). 
lt_member(X, tree(_Y, _L, R)) :- lt_member(X, R).
Nothing had to be declared. Lists only unify with the heads of the first two clauses, and trees with those of clauses 3 to 5: the clause heads dispatch on the shape of the term, which is what unification does anyway, so polymorphism costs nothing here. The predicate works on a list:

?- lt_member(M,[1,2,3]).
Expected answer:

M = 1 ? ;
M = 2 ? ;
M = 3 ? ;
no
and it works on a tree:

?- tree_example(_T), lt_member(M,_T).
Expected answer:

M = b ? ;
M = b ? ;
M = c ? ;
M = a ?
But with both arguments free the answers are surprising. We might expect lists with their members, and trees with their members. What we actually get is:

?- lt_member(M,T).
Expected answer:

T = [M] ? ;
T = [M,_] ? ;
T = [_,M] ? ;
T = tree(M,void,void) ? ;
T = tree(_,[M],_) ? ;
T = tree(_,_,[M]) ? ;
T = [M,_,_] ?
Among the answers there are trees with a list inside them. And this is correct, given what we wrote: by mixing the clauses of the two definitions in one predicate, we have allowed the recursion to go from one type into the other. What we have actually defined is a new type, the "list-tree", in which lists and trees can be nested inside each other arbitrarily.

Can the types be kept apart? Certainly, but we have to write it: we keep the two original definitions, each of which recurses only into itself, and put a wrapper with two clauses on top:

%! lt_member_separate(X,LT): X is a member of the list or tree LT.
lt_member_separate(X, Y) :- list_member(X, Y).
lt_member_separate(X, Y) :- tree_member(X, Y).
?- lt_member_separate(M,T).
Expected answer:

T = [M] ? ;
T = [M,_] ? ;
T = [_,M] ? ;
T = tree(M,void,void) ? ;
T = [M,_,_] ? ;
T = [_,M,_] ? ;
T = [_,_,M] ? ;
T = tree(_,tree(M,void,void),_) ?
Now, inside tree_member/2 we can only be in a tree, and inside list_member/2 we can only be in a list: generating with both arguments free gives lists and trees, and never a mixture. These are two designs, and two different types, and we choose between them by choosing what to write. The auxiliary predicates are the ones that we already know:

:- redefining(list/1).

list([]).
list([_|Y]) :-
     list(Y).

list_member(X,[X|L]) :-
    list(L).
list_member(X,[_|T]) :- 
    list_member(X,T).

tree_member(X,tree(X,Left,Right)) :- 
    binary_tree(Left),
    binary_tree(Right).
tree_member(X,tree(_,Left,_Right)) :- 
    tree_member(X,Left). 
tree_member(X,tree(_,_Left,Right)) :- 
    tree_member(X,Right).

binary_tree(void).
binary_tree(tree(_Element,Left,Right)) :- 
    binary_tree(Left),
    binary_tree(Right).

tree_example(  tree( a,
                   tree( b,
                         void,
                         void
                       ),
                   tree( c,
                         tree( b,
                               void,
                               void
                               ),
                         void
                       )
                   )).

Recognizing (and generating) polynomials (Slide 71)

This is an application of trees, because a polynomial is a tree: an expression tree. The definition is a type, written as every type so far:

  • X is a polynomial in X;
  • a constant is a polynomial in X;
  • sums, differences and products of polynomials in X are polynomials in X;
  • so is a polynomial raised to the power of a natural number, and the quotient of a polynomial by a constant.
polynomial(T,X) holds if T is a polynomial in X:

:- module(_,_,[sr/bfall]).

polynomial(X,X).
polynomial(Term,_X)  :- 
    pconstant(Term).
polynomial(Term1+Term2,X)  :- 
    polynomial(Term1,X),
    polynomial(Term2,X). 
polynomial(Term1-Term2,X)  :- 
    polynomial(Term1,X), 
    polynomial(Term2,X).
polynomial(Term1*Term2,X)  :- 
    polynomial(Term1,X), 
    polynomial(Term2,X).
polynomial(Term1/Term2,X)  :- 
    polynomial(Term1,X), 
    pconstant(Term2).
polynomial(Term1^N,X)  :- 
    polynomial(Term1,X), 
    nat_num(N).

pconstant(X) :- 
    nat_num(X).
pconstant(a).
pconstant(b).
pconstant(c).
?- polynomial( a * x ^ s(s(0)) + b, x).
Expected answer:

yes
a is a constant here because we have declared it to be one, so a*x² + b is a polynomial in x. The definition is also precise enough to reject things:

?- polynomial( a * x ^ x + b, x).
Expected answer:

no
x^x is not a polynomial: the exponent has to be a natural number, and the clause says so. And, as always, the same definition enumerates:

?- polynomial(P, x).
Expected answer:

P = x ? ;
P = a ? ;
P = b ? ;
P = c ?
This is a case in which functional notation pays off. The whole type fits in a few lines (note that the arguments are in the opposite order: the functional definition is called as poly_f(x, Expression)):

:- use_package(fsyntax).
poly_f(X) := X
    | ~pconst_f
    | ~poly_f(X) + ~poly_f(X)
    | ~poly_f(X) - ~poly_f(X)
    | ~poly_f(X) * ~poly_f(X)
    | ~poly_f(X) / ~pconst_f
    | ~poly_f(X) ^ ~nat_num. 

pconst_f := ~nat_num | a | b | c.
?- poly_f(x, a * x ^ s(s(0)) + b).
Expected answer:

yes
This is about the shortest way of writing down what a polynomial is, and it is still, at the same time, a checker, a generator, and a part of a working program. Both versions use:

nat_num(0).
nat_num(s(X)) :- 
    nat_num(X).

Symbolic differentiation (Slide 72)

Terms are symbolic expressions, so a program that manipulates expressions is just a program over terms. Symbolic differentiation is the classic example. deriv(Expression, X, Derivative) holds if Derivative is the derivative of Expression with respect to X:

:- module(_,_,[sr/bfall]).

%! deriv(E,X,D): `D` is the derivative of expression `E` w.r.t. `X`.

deriv(X,     X, s(0)                 ).
deriv(C,    _X, 0                    ) :- pconstant(C).
deriv(U+V,   X, DU+DV                ) :- deriv(U,X,DU), deriv(V,X,DV). 
deriv(U-V,   X, DU-DV                ) :- deriv(U,X,DU), deriv(V,X,DV).
deriv(U*V,   X, DU*V+U*DV            ) :- deriv(U,X,DU), deriv(V,X,DV).
deriv(U/V,   X, (DU*V-U*DV)/V^s(s(0))) :- deriv(U,X,DU), deriv(V,X,DV).
deriv(U^s(N),X, s(N)*U^N*DU          ) :- deriv(U,X,DU), nat_num(N).
deriv(log(U),X, DU/U                 ) :- deriv(U,X,DU).

pconstant(X) :- nat_num(X).
pconstant(a).
pconstant(b).
pconstant(c).
Each clause is a differentiation rule copied from a textbook, and the pattern in the head decides which rule applies. There is no dispatch code and no expression parser: the term is the parse tree.

?- deriv(x*x, x, D).
Expected answer:

D = s 0*x+x*s 0 ?
This is the product rule, applied correctly, and left unsimplified, which is the honest output of these rules. (Recall that s 0 is s(0), that is, 1: the answer is 1*x + x*1.)

?- deriv(s(s(s(0)))*x+s(s(0)),x,Y).
Expected answer:

Y = 0*x+s s s 0*s 0+0 ? ;
no
That is, the derivative of 3x + 2, which is 0*x + 3*1 + 0. A simplification step can be added to eliminate superfluous terms such as 0*x, or the s 0 in s 0*x. It is not totally trivial, but it can be fun: the exercise below gives some guidance for writing a simplifier.

The program also runs in the other direction: given a derivative, it finds expressions that have it (several solutions can be asked for):

?- deriv(E,x,0*x+s(s(s(0)))*s(0)+0).
Expected answer:

E = s s s 0*x+a ? ;
E = s s s 0*x+b ? ;
E = s s s 0*x+c ? ;
E = s s s 0*x+0 ?
The solutions are 3x plus any constant. And this is the same program in functional notation:

:- use_package(fsyntax).
derivf(X,          X) := s(0).
derivf(~pconstant, _) := 0.
derivf(U+V,        X) := ~derivf(U,X) + ~derivf(V,X).
derivf(U-V,        X) := ~derivf(U,X) - ~derivf(V,X).
derivf(U*V,        X) := ~derivf(U,X) * V  +  U * ~derivf(V,X).
derivf(U/V,        X) := ( ~derivf(U,X) * V - U * ~derivf(V,X) ) / V^s(s(0)).
derivf(U^s(N),     X) := s(N) * U^N * ~derivf(U,X) :- nat_num(N).
derivf(log(U),     X) := ~derivf(U,X) / U.
?- derivf(s(s(s(0)))*x+s(s(0)),x,Y).
Expected answer:

Y = 0*x+s s s 0*s 0+0 ? ;
no
nat_num(0).
nat_num(s(X)) :- 
    nat_num(X).

Exercises

Put it into practice! Exercise: simplifying the result of deriv/3 — opens in a separate notebook.

Graphs (Slides 73–74)

Directed, acyclic graphs, represented with facts. Take this graph:

a ----> b             f
|       |             |
v       v             v
c ----> d ----> e     g
        |
        v
        h
It can be represented as a set of facts, one for each edge:

edge_da(a,b).
edge_da(a,c).
edge_da(b,d).
edge_da(c,d).
edge_da(d,e).
edge_da(d,h).
edge_da(f,g).
path_da(X,Y) holds if there is a path from X to Y. It is the transitive closure of the edge relation, and it is the recursion with two clauses that we have now seen many times:

path_da(X,Y) :-
        edge_da(X,Y).
path_da(X,Y) :-
        edge_da(X,Z),
        path_da(Z,Y).

circuit_da :- path_da(A,A).
?- path_da(a,e).
Expected answer:

yes
?- path_da(a,g).
Expected answer:

no
The nodes that can be reached from c:

?- path_da(c,X).
Expected answer:

X = d ? ;
X = e ? ;
X = h ? ;
no
All the pairs of connected nodes can be obtained with ?- path_da(X,Y). (with duplicate solutions when there are different paths between two nodes).

The same graph, represented as a list. The graph can also be a data structure, a list of edges, which is passed to the predicate as an argument:

graph_da([
    edge(a,b),
    edge(a,c),
    edge(b,d),
    edge(c,d),
    edge(d,e),
    edge(d,h),
    edge(f,g) ]).

path_l(X,Y,G) :-
    member(edge(X,Y),G).
path_l(X,Y,G) :-
    member(edge(X,Z),G),
    path_l(Z,Y,G).
?- graph_da(_G), path_l(a,e,_G).
Expected answer:

yes
?- graph_da(_G), path_l(c,X,_G).
Expected answer:

X = d ? ;
X = e ? ;
X = h ? ;
no
Undirected graphs. More is needed if the graph is not directed. A first attempt is to say that two nodes are connected if there is an edge between them in either direction:

path_a(X,Y) :-
        connected_a(X,Y).
path_a(X,Y) :-
        connected_a(X,Z),
        path_a(Z,Y).

connected_a(X,Y) :- edge_da(X,Y).
connected_a(Y,X) :- edge_da(X,Y).

circuit_a :- path_a(A,A).
?- path_a(a,e).
Expected answer:

yes
But now we can go back and forth along an edge, so the same node is reached in infinitely many ways:

?- path_a(c,X).
Expected answer:

X = d ? ;
X = a ? ;
X = e ? ;
X = h ?
and a query that should fail, such as ?- path_a(a,g)., does not terminate: the search goes around and round. The same happens with directed graphs that have cycles. We need to deal with cycles.

This is where the search rule stops being an academic matter. If the graph has a cycle, depth-first search can follow it forever. With sr/bfall every existing path is still found, because solutions are at finite depth, which is exactly the completeness result that we saw when characterizing the search tree, now in a program that we would actually write. But a query with no solution still does not terminate: for that, the program itself has to avoid the cycles.

Dealing with cycles. Take a directed graph with a cycle:

a <---- b             f
|       ^             |
v       |             v
c ----> d ----> e     g
        |
        v
        h
again as a list of edges:

graph([
    edge(a,c),
    edge(c,d),
    edge(d,b),
    edge(b,a),
    edge(d,e),
    edge(d,h),
    edge(f,g) ]).
The idea is to remove each edge from the graph when it is used, so that no edge can be used twice and every path is finite:

path(X,Y,G) :-
    member(edge(X,Y),G).
path(X,Y,G) :-
    list_select(edge(X,Z),G,GRest),
    path(Z,Y,GRest).

% `list_select(X,L,NL)`: selects an element `X` out of list `L`
% and `NL` is `L` without `X`.
list_select(X, [X|Xs], Xs).
list_select(X, [Y|Ys], [Y|Zs]) :-
     list_select(X, Ys, Zs).
?- graph(_G), path(a,e,_G).
Expected answer:

yes
?- graph(_G), path(a,g,_G).
Expected answer:

no
?- graph(_G), path(c,X,_G).
Expected answer:

X = d ? ;
X = b ? ;
X = e ? ;
X = h ?
Detecting whether a graph has a cycle (circuit_da :- path_da(A,A). in the first program) is the natural next question, and the exercises take it up, together with other variations: making the predicates return the list of nodes of the path, using other representations, unconnected graphs, and undirected graphs.

Exercises

Put it into practice! Exercises: extending path/2 and circuit/0 — opens in a separate notebook.

Automata as graphs (Slide 75)

A finite automaton is a graph whose edges are labeled, so it is the same kind of program, with one more argument. We want to recognize the sequences of characters accepted by this non-deterministic finite automaton (NDFA), in which q0 is both the initial and the final state:

Deterministic finite automata are no trouble in any language. Non-deterministic ones are more awkward, because from some state two transitions leave with the same input, and it is not known which one to take. Except here, because in logic programming non-determinism is native.

Two things have to be represented, and it is worth keeping them apart:

  • The strings are lists of constants: a b b is [a,b,b].
  • The automaton is a graph, and it is most easily represented by a set of facts.
This is the definition of a (generic) non-deterministic finite automaton:

:- module(_,_,[sr/bfall]).

accept(S) :- initial(Q), accept_from(S, Q).

accept_from([],Q)     :- final(Q).
accept_from([C|Cs],Q) :- delta(Q, C, NewQ), accept_from(Cs,NewQ).
accept_from/2 goes through the string and the automaton together: the empty string is accepted only in a final state (when the input runs out, we have to be where we are supposed to end), and [C|Cs] is accepted from Q if there is a transition with C to some NewQ from which Cs is accepted.

These three clauses are not this automaton. They are every non-deterministic finite automaton. The facts that follow are this particular one.

A concrete automaton is defined by its initial and final states:

initial(q0).      final(q0).
and by its transitions, which are the arrows of the figure, with their directions:

delta(q0, a, q1). 
delta(q1, b, q0). 
delta(q1, b, q1).
Where exactly is the non-determinism? In the call to delta/3. Running forwards, Q and C are given, and NewQ is returned; but in state q1, reading b, there are two transitions: one stays in q1, and the other one goes back to q0. The first one is taken and, if something fails later, execution backtracks and takes the other one. Nothing had to be programmed for this: it is what having several clauses for a predicate means. The search that Prolog does anyway is exactly the search that a non-deterministic automaton needs.

?- accept([a,b,b]).
Expected answer:

yes
?- accept([a,b,b,a]).
Expected answer:

no
Generation is where it gets interesting. Calling accept/1 with variables asks a different question: which strings of four symbols does this automaton accept?

?- accept([A, B, C, D]).
Expected answer:

A = a,
B = b,
C = a,
D = b ? ;

A = a,
B = b,
C = b,
D = b ? ;

no
One of the answers may be surprising: a b a b. Nothing said that we could not go through the initial state twice, so the language is really (a b+)*, and not a b*. And all the accepted strings can be listed with ?- accept(X).: [], [a,b], [a,b,b], [a,b,a,b], and so on.

Running a specification backwards often reveals that we wrote more than we thought. That is a good enough reason to do it.

Adding a stack: pushdown automata (Slide 76)

By adding one argument, a stack, the same three lines become a non-deterministic pushdown automaton (NPDA), which can recognize more than a finite automaton can:

This is the definition of a (generic) NPDA:

:- module(_,_,[sr/bfall]).

npda_accept(S) :- 
    npda_initial(Q),
    npda_accept_from(S, Q, []).

npda_accept_from([], Q, [])  :-
    npda_final(Q).
npda_accept_from([C|Cs], Q, Stack) :-  
    npda_delta(Q, C, Stack, NewQ, NStack),
    npda_accept_from(Cs, NewQ, NStack).
The stack starts empty, and the base case now requires that we finish in a final state and with the stack empty: everything that was pushed has to have been popped. A concrete automaton is defined by its initial and final states:

npda_initial(q0).     
npda_final(q1).
and by its transitions:

npda_delta(q0, C,     Stack, q0, [C|Stack]).
npda_delta(q0, C,     Stack, q1, [C|Stack]).
npda_delta(q0,_C,     Stack, q1,    Stack ).
npda_delta(q1, C, [C|Stack], q1,    Stack ).
What sequences does it recognize? Rather than being told, let us run it.

?- npda_accept([a,b,b,a]).
Expected answer:

yes
?- npda_accept([a,b,a,b]).
Expected answer:

no
?- npda_accept([a,b,c,b,a]).
Expected answer:

yes
?- npda_accept([a,b,X,a]).
Expected answer:

X = b ? ;
no
It recognizes palindromes. In q0 it pushes each symbol. The second and third transitions are the two ways of crossing the middle of the string: the third one skips a symbol, which is what allows palindromes of odd length. And in q1 it pops, but only if the symbol that is read is the same as the one on top of the stack. This last clause, npda_delta(q1, C, [C|Stack], q1, Stack), uses the same variable C twice: reading the symbol and comparing it with the stack are a single unification.

And asking for the accepted sequences of four symbols gives a single, general answer: the third symbol has to be the same as the second one, and the fourth the same as the first one.

?- npda_accept([A, B, C, D]).
Expected answer:

C = B,
D = A ? ;

no
An alternative version accepts only words formed with the symbols of a particular alphabet, by calling symbol/1 in each transition:

npda_accept_alt(S) :- 
    npda_initial_alt(Q),
    npda_accept_from_alt(S, Q, []).

npda_accept_from_alt([], Q, [])  :-
    npda_final_alt(Q).
npda_accept_from_alt([C|Cs], Q, Stack) :-  
    npda_delta_alt(Q, C, Stack, NewQ, NStack),
    npda_accept_from_alt(Cs, NewQ, NStack).
npda_initial_alt(q0).     
npda_final_alt(q1).

npda_delta_alt(q0, C,     Stack, q0, [C|Stack]) :- symbol(C).
npda_delta_alt(q0, C,     Stack, q1, [C|Stack]) :- symbol(C).
npda_delta_alt(q0, C,     Stack, q1,    Stack ) :- symbol(C).
npda_delta_alt(q1, C, [C|Stack], q1,    Stack ) :- symbol(C).

symbol(a).
symbol(b).
symbol(c).
Asking it for the accepted sequences of four symbols now enumerates them. There are exactly nine, the palindromes of that length over the alphabet a, b, c:

?- npda_accept_alt([A, B, C, D]).
Expected answer:

A = a,
B = a,
C = a,
D = a ? ;

A = a,
B = b,
C = b,
D = a ? ;

A = a,
B = c,
C = c,
D = a ? ;

A = b,
B = a,
C = a,
D = b ?

The Towers of Hanoi (Slides 77–79)

There are three pegs, with a tower of disks on the first one. The objective is to move the tower of N disks from peg a to peg b, with the help of peg c, and the rules are that only one disk can be moved at a time, and that a larger disk can never be placed on top of a smaller one.

With one disk, we just move it. With two, the large one cannot be moved first, so the small one goes to c, the large one to b, and the small one on top of it. With three disks it takes seven moves. The legend says that when the monks of Hanoi finish moving their tower the world will end; apparently they are still at it.

What we want from the program is not just to know that it can be done, but the list of moves. The main predicate is hanoi_moves(N,Moves), where N is the number of disks and Moves the corresponding list of moves. Each move is a term move(A,B), which means move the top disk of peg A to peg B. For example, the moves for three disks

are represented by:

hanoi_moves( s(s(s(0))),
             [ move(a,b), move(a,c), move(b,c), move(a,b),
               move(c,a), move(c,b), move(a,b) ])
The general predicate carries more than the number of disks: hanoi(N,Orig,Dest,Help,Moves) holds if Moves contains the moves needed to move a tower of N disks from peg Orig to peg Dest, with the help of peg Help. It needs Help because the pegs change roles: sometimes we are moving from a to b using c, and sometimes from a to c using b. The reasoning is:

  • With one disk in Orig, which we want in Dest, we just move it. No help is needed, and no rule can forbid it.
  • With s(N) disks, the bottom one has to get to Dest, so we first move the N disks above it out of the way, to Help, using Dest as the spare peg. Then we move the bottom disk. And then we move the N disks from Help to Dest, now using Orig, which is empty, as the spare peg.

The list of moves is then the first group of moves, then the single move, and then the second group, which is exactly what the list_append/3 in the clause builds:

:- module(_,_,[]).

hanoi_moves(N,Moves) :-
        hanoi(N,a,b,c,Moves).

hanoi(s(0),A,B,_,[move(A, B)]).
hanoi(s(N),A,B,C,Moves) :- 
    hanoi(N,A,C,B,Moves1),
    hanoi(N,C,B,A,Moves2), 
    list_append(Moves1,[move(A, B)|Moves2],Moves).
The program is the statement of the solution. Note how the three pegs simply rotate between the recursive calls.

?- hanoi_moves(s(s(s(0))),M).
Expected answer:

M = [move(a,b),move(a,c),move(b,c),move(a,b),move(c,a),move(c,b),move(a,b)] ?
Seven moves, as in the figure, and as the formula 2ⁿ−1 says.

The program returns the moves as a list, instead of printing them, so the plan is data: it can be inspected, counted, checked, or passed to something else. This is worth contrasting with the usual imperative version, which prints as it goes and leaves nothing. And, since it is a relation, it can even be asked how many disks a given list of moves is for:

?- hanoi_moves(s(s(s(0))),M), hanoi_moves(N,M).
Expected answer:

M = [move(a,b),move(a,c),move(b,c),move(a,b),move(c,a),move(c,b),move(a,b)],
N = s s s 0 ?
(Again, this is the only solution, and asking for another one does not terminate.) The program can also enumerate the solutions for one disk, two disks, three disks, and so on, with ?- hanoi_moves(N,M).

Execution time. The following query solves the problem for increasing numbers of disks, printing the time taken by each one (press Next for the next number of disks):

?- hanoi_test(NDisks).
Expected answer (the times vary):

1 disks = 1 moves in 0.001 mS
NDisks = 1 ? ;
2 disks = 3 moves in 0.0 mS
NDisks = 2 ? ;
3 disks = 7 moves in 0.001 mS
NDisks = 3 ? ;
4 disks = 15 moves in 0.001 mS
NDisks = 4 ?
The execution time is exponential in the number of disks, as it has to be, since the number of moves is. Each additional disk doubles the time: measured natively (Ciao 1.26, on a laptop), 16 disks take 22 milliseconds, 18 take 92, 20 take 381, and 22 take about a second and a half. This is the auxiliary code used for these measurements:

hanoi_test(D) :- 
    between(1,1000,D), 
    decimal_peano(D,N), 
    statistics(walltime, [_,_]),
    hanoi_moves(N,M),
    statistics(walltime, [_,T]),
    length(M,L),
    write(D), write(' disks = '),
    write(L), write(' moves in '),
    write(T), write(' mS').

:- use_module(engine(runtime_control),[statistics/2]).
:- use_module(library(write),[write/1]).
:- use_module(library(lists),[length/2]).
:- use_module(library(between)).

list_append([],L,L) :- 
    list(L).
list_append([X|Xs],Ys,[X|Zs]) :- 
    list_append(Xs,Ys,Zs).

list([]).
list([_|Y]) :-
    list(Y).

decimal_peano(0,0).
decimal_peano(N,s(X)) :-
    N>0,
    NN is N-1,
    decimal_peano(NN,X).

Learning to compose recursive programs (Slide 80)

How does one learn to write programs such as these? To some extent it is simply a question of practice, but some approaches help:

  • Generalizing from examples, as we did for member/2 and append/3. It is elegant, but sometimes difficult, and it is not the way most people do it.
  • Thinking inductively: stating first the base case (or cases), and then thinking about the general, recursive case (or cases). The type of the data tells us the shape of the program.
  • Sometimes it helps to compose the program with a given use in mind (for example, "forwards" execution), making sure that it is declaratively correct, and then to consider whether the alternative uses also make sense.
  • Sometimes it helps to look at well-written examples, and to use the same "schemas".
  • A global top-down design approach can help too (in general, and not just for recursive programs): state the general problem, break it down into subproblems, and solve the pieces.
And, again, the best approach is practice, practice, practice.

Everything here has been pure: no cut, no assert, no arithmetic evaluation, and no negation. The next part of the course adds those, and much of what it has to say is about what they cost.