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!
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.
Variables start with an uppercase character or an underscore, and may contain underscores and digits: X, Im4u, A_little_garden, _, _x, _22.
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.
Some examples, and one that is not allowed:
| term | what it is |
|---|---|
| dad | a 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_time | a 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.)
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).
?- 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.
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:
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.
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.
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.
:- 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).
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.
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.
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.
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.
:- 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.
?- barks(X).Expected answer:
X = spot ? ; nospot barks, and nobody else does: the no means that there are no more answers.
?- animal(X).Expected answer:
X = tim ? ; X = spot ? ; X = hobbes ? ; noThere 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 ? ; nospot is a pet and tim is a pet, and furthermore nobody else is: exactly what we worked out by reading the logic.
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.
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.
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 ?
Like many equation-solving procedures, it works by isolating variables and instantiating them with their values.
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:
| A | B | θ | Aθ = Bθ |
|---|---|---|---|
| dog | dog | {} | dog |
| X | a | {X = a} | a |
| X | Y | {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) ?
?- f(X, g(t)) = f(m(h), t(M)).Expected answer:
no
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.
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)).
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.
| form of the equation E | what to do |
|---|---|
| X = X | ignore it |
| X = f(..., X, ...) | fail (occurs check) |
| X = term | add X = term to the solution, and replace X by term everywhere else |
| a = a | ignore it |
| a = b | fail |
| a = f(...) | fail |
| g(...) = f(...) | fail |
| f(s₁, ..., sₘ) = f(t₁, ..., tₙ), with m ≠ n | fail |
| 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.
?- 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:
noX = 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).
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 noThis is the answer that logic wants. Can you explain the difference between the two answers?
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:
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.
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}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 ? ; noThe 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.
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.
The choice of clause defines the alternative paths to be explored in order to find answers, and execution explores this tree (depth-first, breadth-first, or in some other way). Since the schematic interpreter leaves the choice open, any logic programming system has to specify it:
Choosing a different clause can lead to finding the solutions in a different order. Both of the following are valid executions of the same program and the same query:
?- pet(X). ?- pet(X). X = spot ? ; X = tim ? ; X = tim ? ; X = spot ? ; no noNeither is more correct than the other: the set of solutions is the same, which is what the logic determines. Only the order differs, and the order is a matter of control.
There is also some freedom in the computation rule. Any literal of the resolvent can be taken, and even several at once, which is where parallelism comes from.
The search rule is the one that matters, because it decides how the tree of possibilities is explored and, as we will see, whether the answers are found at all.
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:
:- 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.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.
Again, the debugger is very useful to observe how execution proceeds.
The distinction can be made precise, and it is worth having the vocabulary.
Programs without search are those that do not perform "deep" backtracking. In general this means programs whose procedures have either a single clause, or several clauses of which only one is selected for any given call, so that the clauses act as a case statement rather than as alternatives.
Programs with search are those that have at least one procedure with several clauses of which more than one is selected for some calls. These programs perform search (by backtracking, or with another search rule), and they have no direct counterpart in imperative or functional programming.
Take the pets program and the query pet(X). The boxes in the figure are the resolvents (the variable renamings are omitted for brevity). First there is a choice of clause for pet: the one with barks or the one with meows. Then animal(X) has three possibilities in each branch. Then barks(X) has one, and meows(X) has one. The points where the tree splits are the choice points; where a call has only one matching clause there is no real choice.
A different query gives a different tree. Let us ask pet(spot) instead. For the call to pet there are still two choices, but for animal(spot) there is now only one, because the argument is instantiated: animal(tim) and animal(hobbes) do not match. So the tree depends on the query and, for the same query, on the bindings of its variables.
Different execution strategies explore the tree in different ways, determined by the search rule and the computation rule. A search rule that takes the first clause, and then the second, explores the leftmost branch first, and then the next one, which is depth-first search. The question now is whether we can achieve completeness, that is, guarantee that all the solutions will be found.
Recall semi-decidability, the general limitation that we met in the introduction: if a question has an answer, it will be found in finite time; if the answer is no, we may find this out in finite time, or we may not.
In terms of the search tree this means:
The figure shows the cases: solutions and failures at finite depth, and a branch that never ends, to the right of which there may still be a solution.
Everything else in this section follows from this fact.
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.
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
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.
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.
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.
:- 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.):- 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 ? ; noThe 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.
:- 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).)
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.
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:
yesIn pet(X) it is an output: we ask the procedure to produce values:
?- pet(X).Expected answer:
X = spot ? ; X = tim ? ; noThe 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 succeedsWe 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.
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:
:- 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.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:
yesAnd what is not in the program is answered with no:
?- father_of(john,david).Expected answer:
no
?- 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 ? ; noThere 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 ? ; noIf 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 ? ; noAnd nobody is their own grandfather:
?- grandfather_of(X,X).Expected answer:
no
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.
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.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.
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 ? ; noThe 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).
| 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 | Age | Sex |
|---|---|---|
| Brown | 20 | M |
| Jones | 21 | F |
| Smith | 36 | M |
| 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 ? ; noSome 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 ? ; noThis 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.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:
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.
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 ? ; noThese 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
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:
yesBut 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:
nowhereas the same test, made a little later, succeeds:
?- X = john, Y = mary, X \= Y.Expected answer:
X = john, Y = mary ? ; noSo 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 indiffnat/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:
noAnd, being a real relation, it also enumerates:
?- diffnat(s(s(0)),X).Expected answer:
X = 0 ? ; X = s(0) ? ; X = s(s(s(_))) ? ; noThe 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:
nowhich, 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
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.
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:noThe 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:
noAnd 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 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:
yesTypes 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))) ?
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.
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.
% :- module(_,_,[sr/bfall]). :- module(_,_,[]). nat(0). nat(s(X)) :- nat(X).
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 ? ; noZero, 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) ? ; noAnd 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))))) ?
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))) ? ; noWith the result given, it subtracts:
?- plus(s(0),Y,s(s(s(0)))).Expected answer:
Y = s(s(0)) ? ; noA 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:
noAnd 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.
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.
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.
:- 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).
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 ? ; noThere 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:
yesand 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 ?
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:
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.
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 ?
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 ?
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:
?- 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.
| 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.
?- [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] ?
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:
noAnd, 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
and facts such as member(a,[c,a]) or member(b,[s,t,b]), in which the element is further inside, are covered by
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:
yesto find the elements of a list:
?- list_member(X,[a,b,c]).Expected answer:
X = a ? ; X = b ? ; X = c ? ; noand even to generate lists that contain a given element:
?- list_member(a,Y).Expected answer:
Y = [a] ? ; Y = [a,_] ? ; Y = [a,_,_] ? ; Y = [a,_,_,_] ?
:- 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 ? ; noBackwards, it builds a list of a given length:
?- list_length(L, s(s(s(0)))).Expected answer:
L = [_,_,_] ? ; nobut only for Peano numbers, since those are the numbers that it knows about:
?- list_length(L, 3).Expected answer:
nosumlist(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 ? ; noAnd 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). When writing a recursive predicate over a list, we ask two questions, and write one clause for each:
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)))) 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:
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:
yesAnd 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] ?
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] ?
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). 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.
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 [].
| Xs | Acc | NewAcc | |
|---|---|---|---|
| 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.
:- 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.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 list | time (ms) |
|---|---|
| 100 | 0.034 |
| 200 | 0.13 |
| 400 | 0.74 |
| 800 | 3.2 |
| 1600 | 13 |
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 list | time (ms) |
|---|---|
| 1000 | 0.010 |
| 10000 | 0.10 |
| 100000 | 1.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.
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).
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:
yesAnd we can check things that are not trees:
?- binary_tree(tree(a, b, c)).Expected answer:
nob and c are not trees: this is the same mistake as writing [a|b] and expecting a list.
?- binary_tree(tree(a, [], [])).Expected answer:
noThe 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:
noAnd, 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
)
)). :- 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 ? ; noand it works on a tree:
?- tree_example(_T), lt_member(M,_T).Expected answer:
M = b ? ; M = b ? ; M = c ? ; M = a ?
?- 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
)
)). :- 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:
yesa 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:
nox^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:
yesThis 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). :- 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 ? ; noThat 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). a ----> b f
| | |
v v v
c ----> d ----> e g
|
v
hIt 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:
noThe nodes that can be reached from c:
?- path_da(c,X).Expected answer:
X = d ? ; X = e ? ; X = h ? ; noAll 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 ? ; noUndirected 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:
yesBut 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.
Dealing with cycles. Take a directed graph with a cycle:
a <---- b f
| ^ |
v | v
c ----> d ----> e g
|
v
hagain 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.
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:
:- 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.
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
?- accept([A, B, C, D]).Expected answer:
A = a, B = b, C = a, D = b ? ; A = a, B = b, C = b, D = b ? ; noOne 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.
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 ? ; noIt 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 ? ; noAn 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 ?
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:
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). 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.