☰

A Motivational Introduction

Course Notes


These are the notes for the A Motivational Introduction 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.

What the whole area is about

We start with a motivational introduction. The idea is to explain why one would want to do logic programming and, more generally, how logic and programming are related.

The area of computer science (or of mathematical logic) that studies this relation is called computational logic. Many things that you may have heard of live there: programming, algorithms, logic, lambda calculus, logic and AI, logic programming, knowledge representation, functional programming, constraints, the logic of programming, declarative programming. All of these are places where logic and programming, or logic and computer science, intertwine.

Logic and AI, for instance, has a lot to do with knowledge representation: representing knowledge and reasoning about it. Logic and programming — logic programming, functional programming, constraints — is about writing programs using logic. And verification is about proving that properties hold of programs, using logic.

Two halves. All of these topics divide roughly into two classes, and it matters which one this course is about.

The first is the logic of computation: applying logic to computational systems. An example is applying Boolean logic to logic circuits. If you come from the hardware world you know that digital circuits are modeled with propositional logic, which gives us a mathematics in which to talk about the circuit. This is reasoning about computational objects using logic, and its classical application in computer science is verification: we have a program and a property, and we want to prove that the program has that property.

The second is different: instead of using logic to model computation or to reason about it, we use logic to write computations, as a programming language. This is the half that this course is about: the direct use of logic as a programming tool.

The code correctness problem (Slides 2–5)

Let us first look at the whole picture. The basic motivation behind all of this is what is called the program correctness problem.

Consider a simple programming task:

"Compute the squares of the natural numbers that are less than or equal to 5"
In our age, AI is capable of programming anything. So we ask it, and we get a program:

Now consider this alternative, written by a human programmer:

#include <stdio.h>
main() {
  int number, square;
  number = 0;
  while (number <= 5) {
    square = number * number;
    printf("%d\n", square);
    number = number + 1;
  }
}
This program sets number to zero and then, while number is at most five, it multiplies number by itself, puts the result in square, prints square, increments number, and goes around again.

The two programs are semantically identical except for a small detail. The squares that the first one prints are

1 4 9 16 25
while the second one prints

0 1 4 9 16 25
Which one is right? Keep this question in mind: we will be able to answer it precisely by the end of this part, and the answer will not come from staring harder at either program.

Is this program correct? We cannot say, because the immediate reply is: correct with respect to what? What is the specification?

So the first thing that we need, in order to know whether a program is correct, is some kind of specification: a description of the problem, written in some formalism. What were we trying to solve? The program certainly does things, but there is no description of what is expected of it. There might have been a comment; here there is none.

Two things are needed, then. First, we have to know what we want the program to do: this is the specification. Second, we want to be able to reason about whether the program corresponds to it: this is the correctness problem, the proof of correctness.

It is not easy to determine correctness. We can test the program, but how many tests are enough? And what are the correct outputs anyway: was 0 an expected output? When we give a program to a computer and it produces an answer, it is not easy to know whether that answer is the expected one.

From a minor worry to a major one. At the beginning this was a small concern in computer science, and it has slowly become a very large one: nobody wants programs to fail. Initially the effort was easy to justify only for critical applications such as medicine, aerospace, transport, or energy. You do not want the program controlling the plane you are flying in to fail, and you certainly do not want the program in a pacemaker, giving impulses to your heart, to fail. Once deployed, there is no debugging and there are no second chances.

But nowadays people expect programs to work well in completely ordinary applications too. Nobody likes the computer freezing, or the "screens of death" of the past. Fortunately this happens less and less, and it does so thanks to the application of the techniques that we are going to discuss, now that correctness is recognized as an important problem.

In addition, because of society's digital transformation, practically all software is critical in practice, even when it is not safety-critical: a failure means loss of prestige or economic loss in e-commerce, blockchain, banking, online administration, social media...

Software should carry a warranty, like anything else. Programs are not different from other artifacts that humans develop, such as a screw or a car: anything for which we want a warranty, some certification that the thing works and does not do what it is not expected to do. This is also expected of software, among other reasons because most objects that we buy nowadays, from a cell phone to a car, are full of software. In fact, most of the functionality of such devices comes from the software, not from the hardware. So the device will not work properly if the software does not, and the software has to be certified too.

Consider what certification means elsewhere. Something as simple as a screw is certified to many standards: the metal it is made from is made to standards, it is certified to have certain tensile properties and resistance to heat, and the thread conforms to a standard. Software also has to conform to standards.

This is the problem being addressed. How do we manage to have software components with assured quality, such that we are able to give a warranty about them? Can we get software with some kind of warranty so that, even if we do not trust whoever is sending us the software, we can still trust the software itself, because there is a warranty or a proof attached to it? These are very important questions, and they are all part of the program correctness problem.

Today the question takes one more form:

AI is here to transform the world, but, how to communicate in an unambiguous form?

Natural language (Slide 6)

Let us start with the obvious candidate for writing the specification, natural language:

"Compute the squares of the natural numbers which are less than or equal to 5."
This would seem ideal at first sight. But natural language is verbose, vague, and ambiguous, and it needs context: a great deal of assumed information that the reader is expected to supply. Philosophers and mathematicians pointed all this out a long time ago.

In fact, our sentence has already failed us once: it is exactly this sentence that leaves open whether 0 belongs in the output.

So we need a more suitable formalism, and we need it for two distinct jobs: to provide specifications, that is, to describe problems; and to reason about the correctness of programs, that is, of their implementations.

Logic (Slide 7)

Logic is a means of clarifying and formalizing the human thought process.

Classical logic:

Aristotle likes cookies, and Plato is a friend of anyone who likes cookies

implies that

Plato is a friend of Aristotle

Symbolic logic is a shorthand for classical logic — plus a great many useful results:

a₁: likes(aristotle, cookies)
a₂: ∀X likes(X, cookies) → friend(plato, X)
t₁: friend(plato, aristotle)
T[a₁, a₂] ⊢ t₁
This raises the two questions that drive the rest of this introduction to the course:

  • Can logic be used to represent problems — to write specifications?
  • And even, perhaps, to solve them?

Using logic (Slide 8)

For expressing specifications and reasoning about program correctness we need three things:

  • a specification language — for example assertions about input and output, modeling, and so on;
  • a semantics for the programming language — models, axiomatic, denotational, fixpoint;
  • proofs: verification of program correctness — for all possible inputs.
That last phrase is the whole difference between verification and testing. Compare the following two pictures.

Trial and error?

We run the program on some inputs and look at what comes out. The question mark at the bottom is the point: we learn about the inputs that we tried, and nothing about the ones that we did not.

Formal proof!

Here the specification and the semantics of the language both feed a proof, and the proof answers yes or no about all inputs at once. This is why logic is worth the trouble.

Note that this is not our objective in this course: defining the semantics of programming languages and proving programs correct with them is a subject of its own (and great fun), with courses of its own. What follows concentrates on the specification part.

Building a specification

Generating squares: building a specification (Slides 9–12)

"Compute the squares of the natural numbers that are less than or equal to 5"
If we want to say that we generate the squares of the natural numbers, we first have to say what the natural numbers are. So let us formalize the problem starting from scratch, as an exercise in specifying things fully.

Peano numbers

Peano notation is a way to represent numbers axiomatically: to define mathematically what numbers are or, in a sense, to invent the numbers.

We start from the most basic numbering system in the world, the one where one is |, two is ||, and three is |||: sticks, or marks put one after another. This is really Peano arithmetic already.

In logic we first have to introduce a symbol for zero. Then one is represented by a zero followed by a stick, written s(0); two by two sticks, s(s(0)); three by three, s(s(s(0))).

0 ≡ 0     1 ≡ s(0)     2 ≡ s(s(0))     3 ≡ s(s(s(0)))     ...
0 ≡ 0     1 ≡ |        2 ≡ ||          3 ≡ |||            ...
Each of these is just a structure: a term of first-order logic. For example, s(s(s(0))) is the term that represents the number three. To build such terms we need only one constructor, s, and a symbol for zero: with s and 0 we can construct all the numbers. It is not the most efficient representation, but it is the most natural one.

Defining the naturals, and the problem with "et cetera"

How do we state, in logic, which terms are natural numbers? We can say that nat is true of zero, and also (∧ is conjunction) that one, s(0), is a natural number, and also two, and so on:

nat(0) ∧ nat(s(0)) ∧ nat(s(s(0))) ∧ ...
But at some point we have to write "et cetera", because we cannot write down an infinite list.

This is where Peano's great idea comes in. Instead, we write:

nat(0) ∧ ∀X ( nat(X) → nat(s(X)) )

That is: zero is a natural number and, for all X, if X is a natural number then s(X) is also a natural number. These are the axioms of the natural numbers. The infinity of the natural numbers is contained in these two formulas: all of them are defined there.

Facts, rules, and the model

The two formulas are of different kinds, and the distinction is worth naming because it recurs throughout the course. The first one, nat(0), is a fact: a statement without an implication. The second one is a rule.

A fact says only what it says: zero is a natural number. A rule says much more, because that one rule contains an infinite number of facts. Every fact that we would have written in the infinite conjunction (that zero is a natural number, that one is, that two is...) follows from the fact and the rule together.

The set of all those facts is the model of the two axioms: the infinite set of facts that they imply.

Less or equal

Now for the ordering. The technique is the same, and it is the technique for everything that follows: write out the table of facts that we want to be true, and then find the generalizations.

le(0,0)              le(0,s(0))              le(0,s(s(0)))              ...
le(s(0),s(0))        le(s(0),s(s(0)))        le(s(0),s(s(s(0))))        ...
le(s(s(0)),s(s(0)))  le(s(s(0)),s(s(s(0))))  le(s(s(0)),s(s(s(s(0)))))  ...
...
The whole first row says "0 is less than or equal to any natural number":

∀X ( nat(X) → le(0, X) )

To generalize vertically, look down any column: if x ≤ y then x+1 ≤ y+1, that is

∀X ∀Y ( le(X,Y) → le(s(X), s(Y)) )

With these two formulas the infinite table is captured: one formula for the first row, and one for the step that generates each row from the one above.

Squares, which need multiplication, which needs addition

"The square of X is X times X":

∀X ∀Y ( nat(X) ∧ nat(Y) ∧ mult(X,X,Y) → nat_square(X,Y) )

This needs times and, since "X times Y is adding Y to itself X times", it needs add as well.

Addition is defined by exactly the same recipe. We write the table:

add(0,0,0)        add(0,s(0),s(0))        add(0,s(s(0)),s(s(0)))        ...
add(s(0),0,s(0))  add(s(0),s(0),s(s(0)))  add(s(0),s(s(0)),s(s(s(0))))  ...
...
Then we generalize the first row to 0 + x = x, and the columns to if x + y = z then (x+1) + y = z+1:

∀X ( nat(X) → add(0, X, X) ) ∧ ∀X ∀Y ∀Z ( add(X,Y,Z) → add(s(X), Y, s(Z)) )

Multiplication goes the same way, with addition doing the work in the step: 0 × x = 0, and if x × y = w and w + y = z then (x+1) × y = z:

∀X ( nat(X) → mult(0, X, 0) ) ∧ ∀X ∀Y ∀Z ∀W ( mult(X,Y,W) ∧ add(W,Y,Z) → mult(s(X), Y, Z) )

For example (writing 3 for s(s(s(0))), and so on), mult(3,2,6) says that 2+2+2 = 6, and from mult(3,2,6) and add(6,2,8) the rule gives mult(4,2,8): 2+2+2 (+2) = 8.

There is no need to despair at the length of all this: in practice everything so far will be in libraries. What matters is that it can be written down completely, in logic, with no appeal to anyone's intuition about what a natural number is.

The specification itself

With this vocabulary defined we can write a formal specification of the imperative program, that is, the conditions that we want its outputs to meet.

  • Precondition (empty): true
  • Postcondition:
∀X ( output(X) ← ( ∃Y nat(Y) ∧ le(Y, s(s(s(s(s(0)))))) ∧ nat_square(Y,X) ) )

In words: X is an output for the program if there exists a natural number Y such that X is the square of Y and Y is less than or equal to 5.

Now recall the question that we left open at the beginning: is 0 an output? Since nat(0), le(0,5) and nat_square(0,0) all hold, the answer is yes: according to this specification 0 is an output, and the C program is the correct one. More important than which answer is right is that the question now has an answer, and one that can be checked mechanically. This is what a specification gives us.

An alternative use of logic? (Slide 13)

So logic lets us represent and specify problems. But this course is not about that process, although we will do some of it along the way.

Recall the two halves of computational logic: applying logic to computation, and using logic to program. What we have been doing so far belongs to the first: using logic to specify computations, so that their correctness can be proved. What we actually want to do in this course is different: to see whether we can improve the process of writing programs, using logic. Can we use logic not only to specify (to represent the problem) but also to implement the solution?

This question is not an idle one. All real programming languages are Turing complete, and so they have the same theoretical expressive power. But this only means that a program can be written in any of them, not that it is equally compact or equally easy to write in one as in another. So, if we want to improve the process of writing programs, the programming language matters a great deal.

Some relevant context: in the 1960s, when this question was being asked, great techniques and results were being obtained in automated theorem proving.

From specification to computation: Green's dream (Slide 14)

To answer the question we have to go back in time, to the advances of the 1950s and 60s, and to what is known as Green's dream, after Cordell Green, who came up with the idea.

Suppose that we had a mechanical proof method, a deduction procedure: something that can make proofs automatically. This was the heyday of automatic theorem proving and resolution, when these methods were being invented and starting to become practical. Green said: if we had that, then we could look at problem solving, computing, and programming in a completely different way.

  1. Program the deduction procedure into the computer, once and for all. We put a single program in the machine: a deduction system, something that can prove theorems. This is the program that runs.
  2. Then, given a problem:
    • (a) find a representation of it, the specification (for our squares, the logic that we just wrote), and give it to the computer;
    • (b) ask questions, and always get correct answers.
At that point the computer is no longer really a computer: it is an automated deduction system. That one program has to be written, once. But once it is there and works properly (in the sense that if something is a theorem it will say so, and give answers), the consequence for any particular problem is remarkable:

We never write the program.

We only write the specification, and we make the specification run: we get results directly from it. So no correctness proof is needed. The results are correct because they come from the specification: there is no program in between that could be wrong.

Of course, this was a dream: we have a problem, we write its specification, we give it to the computer, we ask questions, and all the answers are correct. To what extent can the dream be realized? At the time only the tip of the iceberg was visible: that automated theorem provers were possible, and that this could be done at least in some cases. So the real question is when we can do it, and that is what the rest of this introduction is about.

Computing with our specification (Slide 15)

Suppose that we have written all of the above in logic (naturals, less-or-equal, addition, multiplication, squares) and that we have an automatic theorem prover.

What a theorem prover is, for our purposes: we give it a specification, a piece of logic, and then we ask a question (is this true? is this false? for which values is it true?) and it answers correctly. If it is a complete deduction procedure, it always answers. This is what we are assuming.

Then the following becomes possible.

If we ask We should obtain
nat(s(0)) ?yes
∃X add(s(0),s(s(0)),X) ?X = s(s(s(0)))
∃X add(s(0),X,s(s(s(0)))) ?X = s(s(0))
∃X nat(X) ?X = 0 ∨ X = s(0) ∨ X = s(s(0)) ∨ ...
∃X ∃Y add(X,Y,s(0)) ?(X = 0 ∧ Y = s(0)) ∨ (X = s(0) ∧ Y = 0)
∃X nat_square(s(s(0)),X) ?X = s(s(s(s(0))))
∃X nat_square(X,s(s(s(s(0))))) ?X = s(s(0))
∃X ∃Y nat_square(X,Y) ?(X = 0 ∧ Y = 0) ∨ (X = s(0) ∧ Y = s(0)) ∨ (X = s(s(0)) ∧ Y = s(s(s(s(0))))) ∨ ...
∃X output(X) ?X = 0 ∨ X = s(0) ∨ X = s⁴(0) ∨ X = s⁹(0) ∨ X = s¹⁶(0) ∨ X = s²⁵(0)

It is worth going down this table slowly, because each row shows something different, and together the rows make the whole argument.

Row 1: answering something we never wrote. Is one a natural number? Yes. But note what has happened: we never wrote that one is a natural number. We wrote that zero is, and then we wrote a rule. So what we are asking about is a consequence: something in the model, deducible from the two axioms. In this case the deduction is easy. Does s(0) match nat(0)? No. Does it match the rule? Yes, if X is 0, and nat(0) is a fact. So one is a natural number. The system should be able to say yes to something that we did not write, because it is a logical consequence of what we did write.

Rows 2 and 3: addition, and why the prover must be constructive. We defined addition with a base case and a general case. Neither of them says that 1 + 2 is 3. But this is a logical consequence: it is in the model, in the table from which we abstracted the two formulas. So the system should deduce it and produce it as output.

This tells us something about the kind of prover that we want: a constructive one. Not a prover that merely says yes or no, but one that says yes, and here are the values. When we write ∃X in the query, the query is a theorem that we want proven (there exists an X which is the result of adding 1 and 2), and we do not just want to be told that such an X exists: we want to be told that an example of such an X is 3.

Row 3 is where this starts to pay off. Is there an X which, added to 1, gives 3? Yes, and it is 2. So:

If we write addition, we do not have to write subtraction.

Subtraction is derived from addition: it is just a different way of using the same arguments. It need not be in our axioms at all.

Rows 4 and 5: several answers, and infinitely many. If we ask whether there exists a natural number, the system says yes, and in fact there are infinitely many: X = 0, X = s(0), X = s(s(0)), and so on, producing them until we stop it. All of them are in the model; all are deducible from the axioms.

Row 5 asks which numbers add up to 1, and there are exactly two answers: X = 0, Y = s(0) and X = s(0), Y = 0. Each answer gives values for several variables at once.

Rows 6, 7 and 8: squares, and square roots for free. What is the square of 2? Multiplying it by itself, the deduction gives 4. And then, exactly as with subtraction, we can also ask: is there a number which, squared, gives 4? Yes: 2. The system is computing square roots, and nobody wrote a square root. In row 8, with both arguments left open, it enumerates all the squares.

Row 9: the problem we started with. ∃X output(X) is the postcondition that we wrote above, asked as a question, and it yields 0, 1, 4, 9, 16, 25.

What we have achieved

If we had an automatic deduction procedure, we would have managed to avoid programming.

Compare with the traditional model, in which we write the specification, write a program, and then prove that the program implements the specification. Here we only represent the problem, that is, we write the specification; we give it to the deduction system, and we run it: we ask questions and get the answers that we want. In this model, writing the specification is sufficient.

This would be great. But, although all of this was invented and logic programming exists, people are still writing Java and C, so obviously it is not as simple as that.

It can get very close, though, and getting close is exactly what this course is about.

Which logic, and what it costs

Which logic should we use, and the tradeoffs (Slides 16–17)

We have argued for representing the specification in logic, and we have glimpsed a new view of computing. This raises new questions, which are not independent of each other.

Does such a proof procedure actually exist? There are many candidates: truth tables, natural deduction, resolution, Prawitz/Bibel, tableaux, bottom-up fixpoint, rewriting, narrowing.

And which logic can we use? The more expressive the logic, the harder it is to have a mechanical proof procedure for it. This tension is the subject of the rest of this section and of the next.

We would like to maximize expressive power. Compare propositions with first-order formulas. Propositional logic is useful:

"spot is a dog"     p
"dogs have tails"   q
but limited: we can say that p ∧ q is true and still not conclude that Spot has a tail, because nothing in p and q connects the dog in the first with the dogs in the second: they are opaque symbols. Predicate logic extends the expressive power:

dog(spot)
∀X dog(X) → has_tail(X)
and now deduction gives us has_tail(spot).

But expressive power is only half of what we need. We also need an effective mechanical proof procedure for whatever logic we choose. So let us compare.

Comparison of logics (Slides 18–19)

Propositional logic — p, "spot is a dog":

  • + decidable — roughly, "we can prove everything to be true or false"
  • + practical deduction mechanisms exist, for example truth tables
  • − limited expressive power — not Turing complete; it cannot express all programs
  • → very useful for circuit design, answer-set programming, and similar
First-order predicate logic — ∀X dog(X) → has_tail(X):

  • + good expressive power — Turing complete, we can express all programs
  • + a practical deduction mechanism — resolution
  • +/− semi-decidable — "we can always prove valid things in finite steps, but there is no guarantee of termination when something is not valid"
This last point is the crux: it is the price to pay for the expressive power, and it is why programs can loop.

Higher-order predicate logic — X(spot), "X is some relationship for spot":

  • + great expressive power
  • − undecidable
  • − no general deduction mechanisms
  • → but there are interesting subsets: higher-order logic programming, functional-logic programming
Another interesting case is the λ-calculus: similar to predicate logic in its results, and it allows higher order.

So first-order predicate logic is the sweet spot: enough power to express any program, and a proof procedure that works in practice. This is the choice that this course rests on.

Generating squares by SLD-resolution (Slides 20–21)

(This section is for readers who have already seen resolution; it can be skipped otherwise.)

We code the problem as definite (Horn) clauses:

nat(0)
¬nat(X) ∨ nat(s(X))
¬nat(X) ∨ add(0, X, X)
¬add(X,Y,Z) ∨ add(s(X),Y,s(Z))
¬nat(X) ∨ mult(0, X, 0)
¬mult(X,Y,W) ∨ ¬add(W,Y,Z) ∨ mult(s(X),Y,Z)
¬nat(X) ∨ ¬nat(Y) ∨ ¬mult(X,X,Y) ∨ nat_square(X,Y)
Query nat(s(0)). To refute, add ¬nat(s(0)):

resolve with unifier giving
¬nat(s(0)) ¬nat(X) ∨ nat(s(X)) {X/0}¬nat(0)
¬nat(0) nat(0) {}□

Answer: yes.

Query ∃X ∃Y add(X,Y,s(0)). To refute, add ¬add(X,Y,s(0)):

resolve with unifier giving
¬add(X,Y,s(0)) ¬nat(X) ∨ add(0,X,X) {X=0, Y=s(0)}¬nat(s(0))

and ¬nat(s(0)) resolves as before. Answer: X = 0, Y = s(0). Taking the other clause instead gives the alternative solution, X = s(0), Y = 0.

This is the mechanical proof method that Green's dream assumed, made concrete, and it works in practice.

Green's dream in a real system

Generating squares in a practical system (Slides 22–23)

With resolution, all of this actually works: the questions that we were asking about our example (is one a natural number? what are the natural numbers?) can be answered by a real system.

Translating the logic into Prolog

We take the same specification and write it in Prolog or, to be precise, in pure logic programming: the Ciao system can be put in a pure logic programming mode, which is what we want for the first part of the course.

The translation is almost nothing: we write the arrows backwards. Instead of writing "∀X, nat(X) implies nat(s(X))", we write it the other way around: the :- is the arrow, pointing from right to left. So nat(s(X)) :- nat(X), reading :- as "if", says that s(X) is a natural number if X is a natural number.

The same goes for the rest. le(0,X) :- nat(X) says that 0 is less than or equal to any X provided that X is a natural number. le(s(X),s(Y)) :- le(X,Y) can be read either way: X+1 ≤ Y+1 if X ≤ Y, or if X ≤ Y then X+1 ≤ Y+1. The commas are the ∧s, :- is the arrow written backwards, and the ∀s are left implicit.

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

% A natural is 0 or s(X) where X is a natural
nat(0).
nat(s(X)) :- nat(X).

% We define less or equal
le(0,X) :- nat(X).
le(s(X),s(Y)) :- le(X,Y).

% Definition of addition
add(0,Y,Y) :- nat(Y).
add(s(X),Y,s(Z)) :- add(X,Y,Z).

% Definition of multiplication
mult(0,Y,0) :- nat(Y).
mult(s(X),Y,Z) :- mult(X,Y,W), add(W,Y,Z).

% Definition of the square of a natural
nat_square(X,Y) :- nat(X), nat(Y), mult(X,X,Y).

% What we want the output of our program to be
output(X) :- le(Y,s(s(s(s(s(0)))))), nat(Y), nat_square(Y,X).
sr/bfall selects a breadth-first search rule instead of Prolog's usual depth-first one. It is needed here because nat_square/2 proposes candidates with nat/1, which has infinitely many answers. Under depth-first search output(X) answers 0 and then never comes back: it is still trying every natural number as the square of 0. Breadth-first search is slower but complete, which makes it the honest way to run a specification that was written with no thought for execution order.

Search rules are explained in the next part of the course. For now, note this as the first appearance of a theme that will occupy us for much of the course: the logic is fine, and it is the control that needs attention.

Where Green's dream is in this picture

Before running anything, note the correspondence with Green's dream:

  • the Ciao system running in the computer is the deduction procedure, programmed once and for all;
  • the program above is the specification, the logic that says what the natural numbers, addition, multiplication, and squares are;
  • the queries below are the questions.

Loading the program

One detail first, because it is the most common beginner's confusion: a program has to be loaded before it can be asked anything. If we ask nat(0) in a top level in which nothing has been loaded, the system does not answer about zero at all: it reports that nat/1 has not been defined:

{ERROR: user:nat/1 - existence error: procedure:user:nat/1 does not exist}
In this page the program above is loaded when the page opens, so the queries below can be run right away.

Asking questions

Is zero a natural number? This is written down explicitly in the program, so the answer is of course yes:

?- nat(0).
Expected answer:

yes
Now a more interesting one: is one a natural number? This we did not write. We wrote that zero is a natural number, and then a rule from which all the others can be deduced:

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

yes
To answer this, the system performs a logical deduction.

Addition, forwards:

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

X = s(s(s(0))) ?
And backwards: what is 3 − 1? Or, put as a question about addition, is there an X which, added to 1, gives 3?

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

X = s(s(0)) ?
This is subtraction, obtained from a definition of addition.

Several answers. We can ask what the natural numbers are. Typing ; (or pressing Next) asks for another answer:

?- nat(X).
Expected answer:

X = 0 ? ;
X = s(0) ? ;
X = s(s(0)) ? ;
X = s(s(s(0))) ? ;
This is an infinite set of solutions, and the system computes them incrementally, so that we can stop at any point by pressing Enter instead of ; (or by pressing Stop).

All the ways of adding up to 1:

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

X = 0,    Y = s(0) ? ;
X = s(0), Y = 0    ? ;
no
We get both answers and then no, meaning that there are no more: these are the only two possibilities.

Squares:

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

X = s(s(s(s(0)))) ?
And square roots: is there a number which, squared, gives 4?

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

X = s(s(0)) ?
Why does this work? Because what we wrote was the definition of squares, on top of the definition of multiplication, on top of the logical definition of addition. Everything that we are asking here is the question is this a theorem in the theory that we wrote?, and it is. And since the deduction procedure is constructive, it does not merely say yes: it gives values for the variables.

With both arguments unbound we get the squares, as pairs:

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

X = 0,          Y = 0 ? ;
X = s(0),       Y = s(0) ? ;
X = s(s(0)),    Y = s(s(s(s(0)))) ? ;
X = s(s(s(0))), Y = s(s(s(s(s(s(s(s(s(0))))))))) ? ;
And finally, the specification itself, run as a program:

?- output(X).
Expected answer:

X = 0 ? ;
X = s(0) ? ;
X = s(s(s(s(0)))) ? ;
X = s(s(s(s(s(s(s(s(s(0))))))))) ? ;
X = s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0)))))))))))))))) ? ;
X = s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(s(0))))))))))))))))))))))))) ?
These are 0, 1, 4, 9, 16 and 25: the squares of the natural numbers up to 5. Nobody wrote a program to compute squares: what we wrote is the postcondition of our specification, transcribed, and it runs.

This is a real, working materialization of Green's dream.

Note also the first answer: 0. The specification says le(Y,5), nat(Y), and 0 is a natural number not greater than 5, so 0 is an output. This settles the question that we left open at the beginning: the program written by the human programmer was right and the one written by the AI was wrong, and we can now say why, by reference to the specification rather than to taste.

Two things to note about all these runs. We always get all the correct answers. What varies is what happens after the last one. Sometimes the system can tell that there are no more and says no, as for add(X, Y, s(0)). Sometimes it cannot, and it goes on searching: if we ask for another answer after X = s(s(0)) in the square-root query, or after the sixth answer of output(X), the system does not come back, because it is trying larger and larger numbers. The theoretical results for resolution and first-order logic are the formal basis for both facts.

It is worth being clear about how far this goes. It works because the specification happens to be in a form, Horn clauses, for which a good proof procedure exists, and because we accepted breadth-first search. Getting acceptable performance out of this idea, while keeping as much of the declarative reading as possible, is the subject of the rest of the course. And note that what we are doing now is programming: this is not just a demonstration of logic.

Exercises

Put it into practice! A real logic program! — opens in a separate notebook.

Applications and history

Applications, and why (constraint) logic programming (Slides 24–25)

Logic programming has many applications, often in combination with other languages:

  • natural language processing
  • scheduling and optimization problems
  • many AI-related problems; (multi-)agent programming
  • heterogeneous data integration
  • program analyzers and verifiers
  • LP as the logical, reasoning and explanation component of LLMs
Some concrete examples:

  • the IBM Watson system has important parts written in Prolog;
  • Clarissa, a voice user interface by NASA for browsing ISS procedures;
  • the first Erlang interpreter was developed in Prolog, by Joe Armstrong;
  • the Microsoft Windows NT networking installation and configuration system;
  • the Ericsson Network Resource Manager (NRM);
  • a "flight booking system handling nearly 1/3 of all airline tickets in the world" (SICStus);
  • the Java abstract machine specification is written in Prolog — look at the specification and you will find Prolog rules, because the people who designed Java were Prolog people;
  • Mangle and the Logica language at Google, and the "Verse" logic/functional language at Epic Games (Fortnite);
  • the ProB verification system.
Why (constraint) logic programming? A quote from the Watson developers is more persuasive than any argument from elegance, because this team had already built the alternative:

"Prior to our decision to use Prolog for this task, we had implemented custom pattern-matching frameworks over parsers. These frameworks ended up replicating some of the features of Prolog but lacked the full feature set of Prolog or the efficiency of a good Prolog implementation. Using Prolog for this task has significantly improved our productivity in developing new pattern-matching rules and has delivered the execution efficiency necessary to be competitive in a Jeopardy! game."

— Adam Lally et al., Question analysis: How Watson reads a clue, IBM J. Res. Dev. 56(3)

Watson is the question-answering system from IBM's DeepQA project. It won first place, and $1 million, playing against the champions Brad Rutter and Ken Jennings at Jeopardy!, a game that, unlike chess, involves common knowledge and wordplay.

Note both halves of the quote: they had reinvented Prolog, badly, and then found that the real thing was also faster. The productivity argument and the efficiency argument point the same way, which is not what people usually expect of a declarative language.

The ProB developers make the same two points in their notes on Why Prolog?:

"The short answer is that Prolog allows us to flexibly implement various specification formalisms as well as the analysis and verification tools that go along with it. At the same time, Prolog is very fast..."
ProB is an animator, constraint solver and model checker for the B-Method for program verification, used by Alstom, ClearSy, Siemens, Thales and several other companies.

A very brief history (Slides 26–27)

The 1960s: the origins.

  • Green: programming as problem solving (the dream described above).
  • Robinson: resolution, the deduction procedure.
These were the basis: logic programming was only possible because these ideas were already there.

The 1970s: the procedural interpretation.

Two people, Bob Kowalski and Alain Colmerauer, came up with the idea of giving a procedural interpretation to Horn clause logic. (Kowalski is also behind SLD resolution, the very efficient form of resolution that we used above to generate the squares.)

This means looking at something like the definition of multiplication, two lines of logic, and saying: I see a procedure. I see something that runs. I see an algorithm in there. We will learn to do exactly that: to look at such a definition, make it run, and watch how it runs. In the end we just run a debugger, and it is not that different from programming in C. But there is a big mental step from looking at two sentences of logic to seeing in them a set of instructions.

The trick, on seeing a clause A :- B₁, ..., Bₙ, is to read it as: to solve (to execute) A, execute B₁, and then B₂, ..., and then Bₙ. That is, we look at sentences of logic as procedures.

This gives Kowalski's equation:

Algorithm = logic + control.

A clause is a piece of logic that states a relation: A is true if B₁, ..., Bₙ are true. But if we give it an order, reading it from left to right, the same clause is also a set of instructions: this is the control. This duality is what makes logic programming interesting, and beautiful.

Much of what will look like performance tuning later in the course is really a change of control that leaves the logic alone. The sr/bfall in our squares program was the first example.

And the rest is history. Colmerauer, in France, built the first practical system and gave it the name Prolog, from Programmation et Logique. The interpreter was written in Fortran.

Then David Warren wrote the first Prolog compiler. Up to that point this was a great idea that ran extremely slowly. Warren's compiler was itself written in Prolog, which was astonishing (nobody thought that a compiler could be written in such a language), and it turned out to be as fast as Lisp, at the time the fastest and most mature symbolic language. This was a small revolution, because it made the whole thing practical. The system, DEC-10 Prolog, came with a top level, a debugger, and very useful built-ins, and it became the standard.

The 1980s and 1990s.

  • Major research in the basic paradigms and in advanced implementation techniques: Japan (the Fifth Generation Project), the US (MCC), Europe (ECRC, the ESPRIT projects), leading to the current EU "framework research programs".
  • Numerous commercial Prolog implementations and programming books, using the de facto standard, the Edinburgh Prolog family.
  • Leading in 1995 to the ISO Prolog standard.
  • Parallel and concurrent logic programming systems.
  • Constraint Logic Programming (CLP) — a major extension that opened new areas and new communities: commercial CLP systems with fielded applications, and concurrent constraint programming systems.
2000 to today.

  • Many further extensions: full higher order, support for types and modes, concurrency, distribution, objects, functional syntax.
  • Highly optimizing compilers, automatic parallelism, automatic verification and debugging, advanced environments.
  • Variations: Datalog, and Answer Set Programming (ASP: negation with stable models).
  • Many applications, as listed above.
  • Increased current interest: explainable AI, and combination with ML and LLMs.
This closes the circle. We started by asking an LLM to write the squares program, and got something whose correctness we could not judge. We end with logic programming as the reasoning and explanation component of such systems. The technology that could not tell us whether 0 belongs in the output is the one that now needs a component that can.