1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): 
20% 
21% END LICENSE BLOCK
22
23\chapter{Prolog Introduction}
24%HEVEA\cutdef[1]{section}
25
26
27\section{Terms and their data types}
28\index{term} \index{types}
29Prolog data ({\bf terms}) and programs are built from a small set of
30simple data-types.  In this section, we introduce these data types
31together with their syntax (their textual representations).  For the
32full syntax see the User Manual appendix on Syntax.
33
34%Syntactically, even the program code itself is made from valid
35%Prolog data-structures, which makes so-called meta-programming
36%(which means to treat programs as data) easy.
37
38%We shall first introduce
39%the various data types, and then describe how Prolog programs can be built.
40
41
42\subsection{Numbers}
43\index{types!integer} \index{integer numbers}
44\index{number}
45Numbers come in several flavours. The ones that are familiar from
46other programming languages are integers and floating point numbers.
47Integers in {\eclipse} can be as large as fits into the machine's
48memory:
49\begin{quote}\begin{verbatim}
50123  0   -27   3492374892749289174
51\end{verbatim}\end{quote}
52\index{types!float} \index{floating point numbers}
53Floating point numbers (represented as IEEE double floats) are written
54as
55\begin{quote}\begin{verbatim}
560.0 3.141592653589793 6.02e23 -35e-12 -1.0Inf
57\end{verbatim}\end{quote}
58\index{types!rational} \index{rational numbers} \index{types!bounded
59real} \index{bounded reals}
60{\eclipse} provides two additional numeric types, rationals and
61bounded reals.  {\eclipse} can do arithmetic with all these numeric
62types.
63
64Note that performing arithmetic requires the use of the \index{is/2}
65\bipref{is/2}{../bips/kernel/arithmetic/is-2.html} predicate:
66
67\begin{quote}\begin{verbatim}
68?- X is 3 + 4.
69X = 7
70Yes
71\end{verbatim}\end{quote}
72
73\index{=/2}
74If one just uses \bipref{=/2}{../bips/kernel/termcomp/E-2.html},
75\eclipse{} will simply construct a term corresponding to the
76arithmetic expression, and will not evaluate it:
77
78\begin{quote}\begin{verbatim}
79?- X = 3 + 4.
80X = 3 + 4
81Yes
82\end{verbatim}\end{quote}
83
84\See{For more details on numeric types and arithmetic in general see the
85User Manual chapter on Arithmetic.}
86
87\See{For more information on the bounded real numeric type, see
88Chapter~\ref{chapreal}.}
89
90%Rational numbers implement the corresponding mathematical
91%notion, i.e.\ the ratio of two integers, and are written like
92%\begin{quote}\begin{verbatim}
93%1_3  -30517578125_32768  0_1
94%\end{verbatim}\end{quote}
95%Bounded reals are a representation for a real number that lies between
96%two floating-point bounds, e.g.
97%\begin{quote}\begin{verbatim}
98%3.141592653__3.141592654
99%\end{verbatim}\end{quote}
100%{\eclipse} can do arithmetic with all these numeric types.
101%For more details see the User Manual chapter on Arithmetic.
102
103
104\subsection{Strings}
105
106\index{string} \index{types!string}
107Strings are a representation for arbitrary sequences of bytes and are
108written with double quotes:
109\begin{quote}\begin{verbatim}
110"hello"
111"I am a string!"
112"string with a newline \n and a null \000 character"
113\end{verbatim}\end{quote}
114Strings can be constructed and partitioned in various ways using
115{\eclipse} primitives.
116
117
118\subsection{Atoms}
119
120\index{atom} \index{types!atom}
121Atoms are simple symbolic constants, similar to enumeration type
122constants in other languages. No special meaning is attached to them
123by the language.  Syntactically, all words starting with a lower case
124letter are atoms, sequences of symbols are atoms, and anything in
125single quotes is an atom:
126\begin{quote}\begin{verbatim}
127atom  quark  i486  -*-  ???  'Atom'   'an atom'
128\end{verbatim}\end{quote}
129
130
131\subsection{Lists}
132
133\index{list} \index{types!list}
134A list is an ordered sequence of (any number of) elements, each of
135which is itself a term. Lists are delimited by square brackets ({\tt [
136]}), and elements are separated by a comma. Thus, the following are
137lists:
138\begin{quote}\begin{verbatim}
139[1,2,3]
140[london, cardiff, edinburgh, belfast]
141["hello", 23, [1,2,3], london]
142\end{verbatim}\end{quote}
143\index{empty list} \index{nil@\textit{nil}}
144A special case is the empty list (sometimes called {\em nil}), which
145is written as
146\begin{quote}\begin{verbatim}
147[]
148\end{verbatim}\end{quote}
149A list is actually composed of head-and-tail pairs, where the head contains one
150list element, and the tail is itself a list (possibly the empty list).
151Lists can be written as a {\tt [Head|Tail]} pair, with the head separated from
152the tail by the vertical bar. Thus the list {\tt [1,2,3]} can
153be written in any of the following equivalent ways:
154\begin{quote}\begin{verbatim}
155[1,2,3]
156[1|[2,3]]
157[1|[2|[3]]]
158[1|[2|[3|[]]]]
159\end{verbatim}\end{quote}
160The last line shows that the list actually consists of 3 {\tt [Head|Tail]}
161pairs, where the tail of the last pair is the empty list.
162The usefulness of this notation is
163that the tail can be a variable (introduced below):
164{\tt [1|Tail]}, which leaves the tail unspecified for the moment. 
165
166
167\subsection{Structures} 
168
169\index{structure} \index{types!structures}
170Structures correspond to structs or records in other languages.  A
171structure is an aggregate of a fixed number of components, called its
172{\em arguments}. Each argument is itself a term.  Moreover, a
173structure always has a name (which looks like an atom).  The canonical
174syntax for structures is
175\begin{quotation}
176{\it $<$name$>$($<$arg$>_{1}$,...$<$arg$>_{n}$)}
177\end{quotation}
178Valid examples of structures are:
179\begin{quote}\begin{verbatim}
180date(december, 25, "Christmas")
181element(hydrogen, composition(1,0))
182flight(london, new_york, 12.05, 17.55)
183\end{verbatim}\end{quote}
184\index{arity} The number of arguments of a structure is called its {\em
185arity}.  \index{functor} The name and arity of a structure are
186together called its {\em functor} and is often written as {\em
187name/arity}.  The last example above therefore has the functor {\tt
188flight/4}.
189
190\See{See section \ref{structures} for information about defining structures
191with named fields.}
192
193\subsubsection{Operator Syntax} 
194\index{operator syntax}
195\index{infix}
196\index{prefix}
197\index{postfix}
198As a syntactic convenience, unary (1-argument) structures can also be written
199in prefix or postfix notation, and binary (2-argument) structures can be
200written in prefix or infix notation, if the programmer has made an
201appropriate declaration (called an {\em operator declaration})
202about its functor.  For example if {\tt plus/2} were declared to
203be an infix operator, we could write:
204\begin{quote}\begin{verbatim}
2051 plus 100
206\end{verbatim}\end{quote}
207instead of
208\begin{quote}\begin{verbatim}
209plus(1,100)
210\end{verbatim}\end{quote}
211It is worth keeping in mind that the data term represented by the
212two notations is the same, we have just two ways of writing the same thing.
213Various logical and arithmetic functors are automatically declared to
214allow operator syntax, for example {\tt +/2, not/1} etc.
215
216\subsubsection{Parentheses} 
217When prefix, infix and postfix notation is used, it is sometimes necessary to
218write extra parentheses to make clear what the structure of the written
219term is meant to be. For example to write the following nested structure
220\begin{quote}\begin{verbatim}
221+(*(3,4), 5)
222\end{verbatim}\end{quote}
223we can alternatively write
224\begin{quote}\begin{verbatim}
2253 * 4 + 5
226\end{verbatim}\end{quote}
227because the star binds stronger than the plus sign.
228But to write the following differently nested structure
229\begin{quote}\begin{verbatim}
230*(3, +(4, 5))
231\end{verbatim}\end{quote}
232in infix-notation, we need extra parentheses:
233\begin{quote}\begin{verbatim}
2343 * (4 + 5)
235\end{verbatim}\end{quote}
236A full table of the predefined prefix, infix and postfix operators
237with their relative precedences can be found in the appendix of the
238User Manual.
239
240\quickref{Summary of Data Types}{
241\begin{description}
242\item[Numbers]
243        \eclipse has {\em integers}, {\em floats}, {\em rationals} and {\em bounded reals}.
244\item[Strings]
245        Character sequences in double quotes.
246\item[Atoms]
247        Symbolic constants, usually lower case or in single quotes.
248\item[Lists]
249        Lists are constructed from cells that have an arbitrary head and
250        a tail which is again a list.
251\item[Structures]
252        Structures have a name and a certain number ({\em arity}) of arbitrary arguments.
253        This characteristic is called the {\em functor}, and written name/arity.
254\end{description}
255}
256
257
258\section{Predicates, Goals and Queries}
259
260\index{predicate}
261\index{goal}
262\index{query}
263Where other programming languages have procedures and functions,
264Prolog and {\eclipse} have {\em predicates}.  A predicate is something
265that has a truth value, so it is similar to a function with a boolean result.
266A predicate {\em definition} simply defines what is true.
267A predicate {\em invocation} (or {\em call}) checks whether something is true or false.
268\index{integer/1}
269A simple example is the predicate {\em integer/1}, which has a built-in
270definition. It can be called to check whether something is an integer:
271\begin{quote}\begin{verbatim}
272integer(123)           is true
273integer(atom)          is false
274integer([1,2])         is false
275\end{verbatim}\end{quote}
276A predicate call like the above is also called a {\em goal}.
277A starting goal that the user of a program provides is called a {\em query}.
278To show queries and their results, we will from now on
279use the following notation:
280\begin{quote}\begin{verbatim}
281?- integer(123).
282Yes.
283?- integer(atom).
284No.
285?- integer([1,2]).
286No.
287\end{verbatim}\end{quote}
288A query can simply be typed at the eclipse prompt, or entered into the
289query field in a tkeclipse window. Note that it is not necessary to enter
290the {\tt ?-} prefix.
291On a console input, is however necessary to terminate the query with a
292full-stop (a dot followed by a newline).
293After executing the query, the system will print one of the
294answers {\bf Yes} or {\bf No}.
295
296
297\subsection{Conjunction and Disjunction}
298\index{conjunction}
299\index{disjunction}
300
301\index{conjunction} \index{disjunction}
302Goals can be combined to form conjunctions (AND) or disjunctions (OR).
303Because this is so common, Prolog uses the comma for AND and the
304semicolon for OR. The following shows two examples of conjunction,
305the first one is true because both conjuncts are true, the second is false:
306\begin{quote}\begin{verbatim}
307?- integer(5), integer(7).
308Yes.
309?- integer(5), integer(hello).
310No.
311\end{verbatim}\end{quote}
312In contrast, a disjunction is only false if both disjuncts are false:
313\begin{quote}\begin{verbatim}
314?- ( integer(hello) ; integer(5) ).
315Yes.
316?- ( integer(hello) ; integer(world) ).
317No.
318\end{verbatim}\end{quote}
319As in this example, it is advisable to always surround disjunctions with
320parentheses. While not strictly necessary in this example, they are often
321required to clarify the structure.
322
323In practice, when answering queries with disjunctions, the system will
324actually give a separate {\bf Yes} answer for every way in which the
325query can be satisfied (i.e.\ proven to be true).
326For example, the following disjunction can be satisfied in two ways,
327therefore system will give two {\bf Yes} answers:
328\begin{quote}\begin{verbatim}
329?- ( integer(5) ; integer(7) ).
330Yes (0.00s cpu, solution 1, maybe more)
331Yes (0.02s cpu, solution 2)
332\end{verbatim}\end{quote}
333The second answer will only be given after the user has explicitely
334asked for more solutions.
335Sometimes the system cannot decide whether an answer is the last one.
336In that case, asking for more solutions may lead to an alternative
337{\bf No} answer, like in the following example:
338\begin{quote}\begin{verbatim}
339?- ( integer(5) ; integer(hello) ).
340Yes (0.00s cpu, solution 1, maybe more)
341No (0.02s cpu)
342\end{verbatim}\end{quote}
343Of course, as long as there was at least one {\bf Yes} answer, the query
344as a whole was true.
345
346
347\section{Unification and Logical Variables}
348
349\subsection{Symbolic Equality}
350\index{equality!symbolic}
351Prolog has a particularly simple idea of {\bf equality}, namely
352structural equality by pattern matching.  This means that two terms
353are equal if and only if they have exactly the same structure.  No
354evaluation of any kind is perfomed on them:
355\begin{quote}\begin{verbatim}
356?- 3 = 3.
357Yes.
358?- 3 = 4.
359No.
360?- hello = hello.
361Yes.
362?- hello = 3.
363No.
364?- foo(a,2) = foo(a,2).
365Yes.
366?- foo(a,2) = foo(b,2).
367No.
368?- foo(a,2) = foo(a,2,c).
369No.
370?- foo(3,4) = 7.
371No.
372?- +(3,4) = 7.
373No.
374?- 3 + 4 = 7.
375No.
376\end{verbatim}\end{quote}
377Note in particular the last two examples (which are equivalent):
378there is no automatic arithmetic evaluation. The term +(3,4) is simply
379a data structure with two arguments, and therefore of course different from
380any number.
381
382Note also that we have used the built-in predicate =/2, which exactly
383implements this idea of equality.
384
385
386\subsection{Logical Variables}
387\index{logical variable}
388
389\index{variables} \index{logical variables}
390So far we have only performed tests, giving only Yes/No results.
391How can we compute more interesting results? 
392The solution is to introduce Logical Variables. 
393It is very important to understand that Logical Variables are
394variables in the mathematical sense, not in the usual programming
395language sense. Logical Variables are simply placeholders for
396values which are not yet known, like in mathematics.
397In conventional programming languages on the other hand, variables
398are labels for storage locations.
399The important difference is that the value of a logical variables is
400typically unknown at the beginning, and only becomes
401known in the course of the computation. Once it is known, the variable is just
402an alias for the value, i.e. it refers to a term.
403Once a value has been assigned to a logical variable, it remains fixed
404and cannot be assigned a different value. 
405
406Logical Variables are written beginning with an upper-case letter or
407an underscore, for example
408\begin{quote}\begin{verbatim}
409X   Var   Quark   _123   R2D2
410\end{verbatim}\end{quote}
411If the same name occurs repeatedly in the same input term (e.g. the same
412query or clause), it denotes the same variable.
413
414
415\subsection{Unification}
416\index{unification}
417
418\index{unification} \index{instantiation} \index{aliasing} \index{binding}
419With logical variables, the above equality tests become much more interesting,
420resulting in the concept of {\em Unification}.
421Unification is an extension of the idea of pattern matching of two terms.
422In addition to matching, unification also causes the binding (instantiation,
423aliasing) of variables in the two terms.
424Unification instantiates variables such that the two unified terms become
425equal. For example
426\begin{quote}\begin{verbatim}
427X = 7                is true with X instantiated to 7
428X = Y                is true with X aliased to Y (or vice versa)
429foo(X) = foo(7)      is true with X instantiated to 7
430foo(X,Y) = foo(3,4)  is true with X instantiated to 3 and Y to 4
431foo(X,4) = foo(3,Y)  is true with X instantiated to 3 and Y to 4
432foo(X) = foo(Y)      is true with X aliased to Y (or vice versa)
433foo(X,X) = foo(3,4)  is false because there is no possible value for X
434foo(X,4) = foo(3,X)  is false because there is no possible value for X
435\end{verbatim}\end{quote}
436
437
438
439\quickref{Basic Terminology}{
440\begin{description}
441\item[Predicate] Something that is true or false, depending on its definition
442    and its arguments. Defines a relationship between its arguments.
443\item[Goal] A logical formula whose truth value we want to know.
444    A goal can be a conjunction or disjunction of other (sub-)goals.
445\item[Query] The initial Goal given to a computation.
446\item[Unification] An extension of pattern matching which can bind logical
447    variables (placeholders) in the matched terms to make them equal.
448\item[Clause] One alternative definition for when a predicate is true.
449    A clause is logically an implication rule.
450\end{description}
451}
452
453
454
455
456
457\section{Defining Your Own Predicates}
458
459\subsection{Comments}
460\index{comment}
461       Since we will annotate some of our programs, we first introduce
462       the syntax for comments. There are two types:
463       \begin{description}
464        \item[Block comment] The comment is enclosed between the delimiters {\tt /*} and {\tt */}.
465         Such comments can span multiple lines, and may be conveniently used
466         to comment out unused code.
467       \item[Line comment] Anything following and including '{\tt \%}' in a line is taken as a
468       comment (unless 
469        the '{\tt \%}' character is part of a quoted atom or string).
470       \end{description}
471
472\subsection{Clauses and Predicates}
473\label{syntax}
474\index{clause}
475
476     \index{clause} \index{predicate}
477     Prolog programs are built from valid Prolog data-structures.
478     A program is a collection of {\it predicates}, and a
479     predicate is a collection of {\it clauses}.
480
481     The idea of a clause is to define that something is true.
482     The simplest form of a clause is the {\em fact}.
483     For example, the following two are facts:
484\begin{code}
485capital(london, england).
486brother(fred, jane).
487\end{code}
488     Syntactically, a fact is just a structure (or an atom)
489     terminated by a full stop.
490
491     Generally, a clause has the form
492     \begin{quote}
493     Head :- Body.
494     \end{quote}
495     where {\em Head} is a structure (or atom) and {\em Body} is a {\em Goal},
496     possibly with conjunctions and disjunctions like in the queries discussed above.
497     The following is a clause
498\begin{code}
499uncle(X,Z) :- brother(X,Y), parent(Y,Z).
500\end{code}
501     Logically, this can be read as a reverse implication
502     \begin{quote}
503     $uncle(X,Z) \longleftarrow brother(X,Y) \wedge parent(Y,Z)$
504     \end{quote}
505     or, more precisely
506     \begin{quote}
507     $\forall X \forall Z: uncle(X,Z) \longleftarrow \exists Y: brother(X,Y) \wedge parent(Y,Z)$
508     \end{quote}
509     stating that uncle(X,Z) is true if brother(X,Y) and parent(Y,Z) are true.
510     Note that a fact is equivalent to a clause where the body is {\tt true}:
511\begin{code}
512brother(fred, jane) :- true.
513\end{code}
514
515     One or multiple clauses with the same head functor (same name and number
516     of arguments) together form the {\em definition}
517     of a predicate. Logically, multiple clauses are read as a disjunction,
518    i.e.\ they define alternative ways in which the predicate can be true.
519     The simplest case is a collection of alternative facts:
520\begin{code}
521parent(abe, homer).
522parent(abe, herbert).
523parent(homer, bart).
524parent(marge, bart).
525\end{code}
526The following defines the ancestor/2 predicate by giving two alternative
527clauses (rules):
528\begin{code}
529ancestor(X,Y) :- parent(X,Y).
530ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).
531\end{code}
532       Remember that a clause can be read logically, with the {\tt :-}
533       taking the meaning of implication, and the comma
534       separating goals read as a conjunction. The logical
535       reading for several clauses of the same predicate is disjunction
536       between the clauses. So the first
537       ancestor rule above states that if X is a parent of Y, then this
538       implies that X is an ancestor of Y. The second rule, which specifies
539       another way X can be an ancestor of Y states that if some other
540       person, Z, is the parent of Y, {\it and\/} X is an ancestor of Z,
541       then this implies that X is also an ancestor of Y.
542
543%       In fact, syntactically, the {\tt ':-'} and {\tt ','} used in 
544%       constructing clauses are operators ({\tt :-/2} and {\tt ,/2}
545%       if a clause contains more than two body goals, this is achieved by
546%       nesting of {\tt ,/2}).
547
548\Note{
549\index{variables!scope}
550It is also important to remember that the scope of a variable
551       name only extends over the clause in which it is in, so any
552       variables with the same name in the same clause refer to the
553       same variable, but variables which occur in different clauses
554       are different even if they have been written with the same name.
555       }
556
557
558\section{Execution Scheme}
559
560%       Before we describe the execution of Prolog programs in more detail, we
561%       shall first discuss the basic mechanism for computation in Prolog --
562%       unification.  
563%
564%\subsection{Unification}
565%
566%  Unification is the pattern matching of two terms. In addition to
567%  matching, unification also causes the binding (aliasing) of
568%  variables in the two terms. For example,
569%  \begin{quotation}
570%  unifying {\tt capital(london,Country) } with {\tt capital(london,england)} will
571%  cause {\tt Country} to bind to 
572%  {\tt england}.
573%  \end{quotation}
574%
575%   Note that variables from the two terms being unified can both supply
576%   variables that are bound, e.g. unifying
577%
578%\begin{verbatim}
579%   capital(City,england)      capital(london, Country)
580%\end{verbatim}
581%
582%   would bind {\tt City} to {\tt london}, and {\tt Country} to {\tt england}.
583%
584%   If the two terms being unified do not match with each other, then the 
585%   unification {\bf fails}. Here are examples of pairs of terms that fail
586%   to unify:
587%\begin{verbatim}
588%
589%       london                    france
590%       capital(City,Country)     ancestor(X,Y)
591%       capital(london, Country)  capital(paris, Country)
592%
593%\end{verbatim}
594%
595
596\subsection{Resolution}
597\index{resolution}
598
599\index{resolution} \index{resolvent}
600Resolution is the computation rule used by Prolog. Given a set of
601facts and rules as a program, execution begins with a query, which is
602an initial goal that is to be resolved.
603The set of goals that still have to be resolved is called the
604{\em resolvent}.
605
606Consider again the {\tt ancestor/2}
607and {\tt parent/2} predicate shown above.
608\begin{code}
609ancestor(X,Y) :- parent(X,Y).                 % clause 1
610ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).  % clause 2 
611
612parent(abe, homer).                           % clause 3
613parent(abe, herbert).                         % clause 4
614parent(homer, bart).                          % clause 5
615parent(marge, bart).                          % clause 6
616\end{code}
617Program execution is started by issuing a query, for example
618\begin{quote}\begin{verbatim}
619?- ancestor(X, bart).
620\end{verbatim}\end{quote}
621This is our initial resolvent.
622The execution mechanism is now as follows:
623\exercises{Execution Algorithm}{
624\begin{enumerate}
625\item Pick one (usually the leftmost) goal from the resolvent.
626        If the resolvent is empty, stop.
627\item Find all clauses whose head successfully unifies with this goal.
628        If there is no such clause, go to step 6.
629\item Select the first of these clause. If there are more, remember the
630        remaining ones. This is called a {\em choice point}.
631\item Unify the goal with the head of the selected clause.
632        (this may instantiate variables both in the goal and in the clause's body).
633\item Prefix this clause body to the resolvent and go to 1.
634\item \index{backtracking}
635      Backtrack: Reset the whole computation state to
636        how it was when the most recent choice point was created.
637        Take the clauses remembered in this choice point and go to 3.
638\end{enumerate}
639}
640In our example, the Prolog system would attempt to unify
641{\tt ancestor(X, bart)} with the program's
642clause heads. Both clauses of the {\tt ancestor/2} predicate can
643unify with the goal, but the textually first clause, clause 1, is
644selected first, and successfully unified with the goal:
645\begin{quote}\begin{verbatim}
646Goal (Query):   ancestor(X,bart)
647Selected:       clause 1
648Unifying:       ancestor(X,bart) = ancestor(X1,Y1)
649results in:     X=X1, Y1=bart
650New resolvent:  parent(X, bart)
651More choices:   clause 2
652\end{verbatim}\end{quote}
653The body goal of clause 1 \verb'parent(X, bart)' is added to the
654resolvent, and the system remembers that there is an untried 
655alternative -- this is referred to as a {\it choice-point}.
656
657In the same way, \verb'parent(X, bart)' is next selected for 
658unification. Clauses 5 and 6 are possible matches for this goal,
659with clause 5 selected first. There are no body goals to add, and
660the resolvent is now empty:
661\begin{quote}\begin{verbatim}
662Goal:           parent(X, bart)
663Selected:       clause 5
664Unifying:       parent(X,bart) = parent(homer,bart)
665results in:     X = homer
666New resolvent:  
667More choices:   clause 6, then clause 2
668\end{verbatim}\end{quote}
669The execution of a program completes successfully when there is an
670empty resolvent. The program has thus found the first solution 
671to the query, in the form of instantiations to the original Query's
672variables, in this case {\tt X = homer}. \eclipse\ returns this
673solution, and also asks if we want more solutions:
674\begin{quote}\begin{verbatim}
675?- ancestor(X,bart).
676X = homer     More? (;) 
677\end{verbatim}\end{quote}
678Responding with ';' will cause \eclipse\ to try to find alternative
679solutions by {\bf backtracking} to
680the most recent choice-point, i.e. to seek an alternative to 
681{\tt parent/2}. Any bindings done during and after the selection of 
682clause 5 are undone, i.e. the binding of  X to {\tt 
683homer} is undone. Clause 6 is now unified with
684the goal {\tt parent(X,Y)}, which again produces a solution:
685\begin{quote}\begin{verbatim}
686Goal:           parent(X, bart)
687Selected:       clause 6
688Unifying:       parent(X,bart) = parent(marge,bart)
689results in:     X = marge
690New resolvent:  
691More choices:   clause 2
692\end{verbatim}\end{quote}
693If yet further solutions are needed, then \eclipse\ would again backtrack.
694This time, {\tt parent/2} no longer has any alternatives left to unify,
695so the next older choice-point, the one made for {\tt ancestor/2},
696is the one that would be considered. The computation is returned
697to the state it was in just before clause 1 was selected, and clause 2
698is unified with the query goal:
699\begin{quote}\begin{verbatim}
700Goal:           ancestor(X,bart)
701Selected:       clause 2
702Unifying:       ancestor(X,bart) = ancestor(X1,Y1)
703results in:     Y1 = bart, X1 = X
704New resolvent:  parent(Z1, bart), ancestor(X1, Z1)
705More choices:   
706\end{verbatim}\end{quote}
707For the first time, there are more than one goal in the resolvent, the
708leftmost one, {\tt parent(Z1,bart)} is then
709selected for unification. Again, clauses 5 and 6 are candidates, and
710a new choice-point is created, and clause 5 tried first.
711\begin{quote}\begin{verbatim}
712Goal:           parent(Z1, bart)
713Selected:       clause 5
714Unifying:       parent(Z1, bart) = parent(homer, bart)
715results in:     Z1 = homer
716New resolvent:  ancestor(X1, homer)
717More choices:   clause 6
718\end{verbatim}\end{quote}
719Eventually, after a few more steps
720(via finding the ancestor of {\tt homer}), this leads to a new solution, 
721with {\tt abe} returned as an ancestor of {\tt bart}:
722\begin{quote}\begin{verbatim}
723?- ancestor(X,bart).
724X = abe     More? (;) 
725\end{verbatim}\end{quote}
726If yet more solutions are requested, then because only one parent for
727{\tt homer} is given by the program, \eclipse\ would backtrack to the only 
728remaining choice-point, unifying clause 6 is unified with the goal, 
729binding \verb'Z1' to \verb'marge'. However, no ancestor for {\tt marge}
730can be found, because no parent of
731{\tt marge} is specified in the program. No more choice-points remains to
732be tried, so the execution terminates.
733
734
735
736\section{Partial data structures}
737\label{tail}
738\index{tail}
739Logical variables can occur anywhere, not only as arguments of clause
740heads and goals, but also within data structures.
741A data structure which contains variables is called a partial data
742structure, because it will eventually be completed by substituting
743the variable with an actual data term.
744The most common case of a partial data structure is a list whose
745tail is not yet instantiated.
746
747Consider first an example where no partial lists occur.
748In the following query, a list is built incrementally,
749starting from its end:
750\begin{quote}\begin{verbatim}
751?- L1 = [], L2 = [c|L1], L3 = [b|L2], L4 = [a|L3].
752L1 = []
753L2 = [c]
754L3 = [b, c]
755L4 = [a, b, c]
756\end{verbatim}\end{quote}
757Whenever a new head/tail cell is created,
758the tail is already instantiated to a complete list.
759
760But it is also possible to build the list from the front.
761The following code, in which the goals have been reordered,
762gives the same final result as the code above:
763\begin{quote}\begin{verbatim}
764?- L4 = [a|L3], L3 = [b|L2], L2 = [c|L1], L1 = [].
765L1 = []
766L2 = [c]
767L3 = [b, c]
768L4 = [a, b, c]
769\end{verbatim}\end{quote}
770However, in the course of the computation, variables get instantiated to
771''partial lists'', i.e.\ lists whose head is known, but whose tail is not.
772This is perfectly legal: due to the nature of the logical variable, the
773tail can be filled in later by instantiating the variable.
774
775%Partial data structures are in fact a very 
776%important feature of Prolog. Variables can occur anywhere inside
777%a data structure, and this allows data to be built incrementally and dynamically:
778% the partially built data structure is passed
779%around, and its ``holes'' (the variables) are filled in by further partial
780%data structures.
781
782%THIS IS NOT AN EXAMPLE OF PARTIAL DATA STRUCTURES!
783%An example use of partial data structure is in the use of accumulator pairs,
784%which was demonstrated in section~\ref{tail} with numbers. However, the
785%`value' being accumulated can be a partial data structure, as shown with
786%this example of reversing a list:
787%\begin{code}
788%reverse(List,Reverse) :- rev(List, [], Reverse).
789%
790%rev([], R, R).
791%rev([E|L], R0, R) :- rev(L, [E|R0], R).
792%\end{code}
793%
794%The partially reversed list is being constructed in the second argument of
795%\verb'rev/3', and finally passed out when the end of the list is reached.
796
797
798%\subsection{Difference Lists}
799%An important technique using partial data structure is the difference list.
800%In difference lists, a list is represented by a pair of (perhaps partial)
801%lists, e.g.
802%the list \verb'[1,2,3|T]' can be represented as the pair \verb'[1,2,3|T]' and
803%\verb'T'. This gives direct access to the tail of a list, and the list does
804%not need to be traversed to reach the tail. This can result in much improved
805%efficiency when, for example, appending two lists together. With a normal
806%list representation, the {\tt append} predicate is written as:
807%\begin{code}
808%% append(L1, L2, L) L is the list L2 appended to the end of L1
809%append([], L, L).
810%append([E|L1], L2, [E|L]) :-
811%    append(L1, L2, L).
812%\end{code}
813%
814%With this encoding, the first list has to be traversed fully to find
815%the end, and then the 
816%second list appended to that.
817%
818%With the difference list representation, the end of the lists are known,
819%and the append can be encoded with a simple fact:
820%\begin{code}
821%/* append_dl(L1,E1, L2,E2, L,E) 
822%   L,E is the appended list of L1,E1 and L2,E2 
823%*/
824%append_dl(L1,E1, E1,E2, L1,E2).
825%\end{code}
826%
827%\begin{figure}
828%%\epsfbox{appenddiff.ps}
829%\includegraphics{appenddiff.ps}
830%\caption{Appending difference lists}
831%\end{figure}
832%The situation is as shown in the figure. By setting E1 to L2, the second 
833%list is appended to the end of the first list, and \verb'L1',\verb'E2' now
834%represents the combined list. With this difference list representation,
835%no traversal of the first list is needed, and the operation is done in
836%constant time. It can thus be much more efficient, especially if the first
837%list is long.
838
839\section{More control structures}
840    \subsection{Disjunction}
841\index{disjunction} \index{;/2}
842Disjunction is normally specified in Prolog by different clauses of
843a predicate, but it can also be specified within a single clause by
844the use of \verb';/2'. For example,
845
846\begin{code}
847atomic_particle(X) :- (X = proton ; X = neutron ; X = electron).
848\end{code}
849
850This is logically equivalent to: 
851
852\begin{code}
853atomic_particle(proton).
854atomic_particle(neutron).
855atomic_particle(electron).
856\end{code}
857
858    \subsection{Conditional}
859
860\index{conditional} \index{->/2}
861Conditionals can be specified using the \verb'->/2' operator.
862In combination with \verb';/2', a conditional similar to `if-then-else' 
863constructs of conventional language can be constructed:
864\verb'X->Y;Z', where \verb'X', \verb'Y' and \verb'Z' can be one or more
865goals,  means that if \verb'X' is true, then \verb'Y' will be
866executed, otherwise \verb'Z'. Only the first solution of \verb'X' is
867explored, so that on backtracking, no new solutions for \verb'X' will be
868tried. In addition, if \verb'X' succeeds, then the `else' part, \verb'Z'
869will never be tried. If \verb'X' fails, then the `then' part, \verb'Y',
870will never be tried. An example of `if-then-else' is:
871\begin{code}
872max(X,Y, Max) :- 
873   number(X), number(Y),
874   (X > Y -> Max = X ; Max = Y).
875\end{code}
876where \verb'Max' is the bigger of the numbers \verb'X' or \verb'Y'.
877Note the use of the brackets to make the scope of the if-then-else
878clear and correct.
879
880
881    \subsection{Call} 
882\index{call}
883\index{metacall}
884One feature of Prolog is the equivalence of  programs and data -- both are
885represented as terms. The predicate \verb'call' allows 
886program terms (i.e. data) to be treated as goals: \verb'call(X)' will cause
887\verb'X' to be treated as a goal and executed. Although at the time when 
888the predicate is executed, \verb'X' has to be instantiated, it does not 
889need to be instantiated (or even known) at compile time. For example, it
890would in principle be
891possible to define disjunction (\verb';') as follows:
892
893\begin{code}
894X ; Y :- call(X).
895X ; Y :- call(Y).
896\end{code}
897
898%In addition, a Prolog program can construct a term at run-time, which is then 
899%called as a goal.  This is sometimes used to ensure goals are compiled
900%after earlier goals have already been executed, for
901%example\footnote{The 'foreach' construct is described in the next chapter.}:
902%\begin{verbatim}
903%Y=A, call(foreach(A,[a,b,c]) do writeln(Y)).
904%\end{verbatim}
905%As required, this means the same as
906%\begin{verbatim}
907%foreach(A,[a,b,c]) do writeln(A).
908%\end{verbatim}
909
910
911\subsection{All Solutions}
912\label{all-solutions}
913
914In the pure computational model of Prolog, alternative solutions are
915computed one-by-one on backtracking. Only one solution is available
916at any time, while previous solutions disappear on backtracking:
917\begin{quote}\begin{verbatim}
918?- weekday(X).
919X = mo
920More
921X = tu
922More
923X = we
924More
925...
926\end{verbatim}\end{quote}
927Sometimes it is useful to have all solution together in a list.
928This can be achieved by using one of the all-solutions predicates
929\index{findall/3}\index{setof/3}\index{bagof/3}findall/3, setof/3 or bagof/3:
930\begin{quote}\begin{verbatim}
931?- findall(X, weekday(X), List).
932X = X
933List = [mo, tu, we, th, fr, sa, su]
934Yes
935\end{verbatim}\end{quote}
936\See{For the differences between findall/3, setof/3 and bagof/3
937see the \eclipse{} Reference Manual.}
938
939
940\section{Using Cut}
941\label{cut}
942\index{cut}
943Cut (written as \verb'!') prunes away part of the Prolog search-space. This
944can be a very powerful mechanism for improving the performance of programs,
945and even the suppression of unwanted solutions. However, it can also be
946easily misused and over-used. 
947
948Cut does two things:
949
950\begin{description}
951\item[commit] Disregard any later clauses for the predicate.
952\item[prune] Throw away all alternative solutions to the goals to the left of
953 the cut.
954\end{description}
955
956\subsection{Commit to current clause}
957\index{commit}
958
959Consider the following encoding of the ``minimum'' predicate:
960\begin{code}
961min(X,Y, Min) :- X <Y, Min = X.
962min(X,Y, Min) :- Y=<X, Min = Y.
963\end{code}
964Whilst logically correct, the behaviour of this encoding is
965non-optimal for two reasons.  Consider the goal {\tt :- min(2,3,M)}.
966Although the first clause succeeds, correctly instantiating $M$ to
967$2$, Prolog leaves an open choice point.  If these clauses and goal
968occur as part of a larger program and goal, a failure might occur
969later, causing backtracking to this open choice point. 
970Prolog would then, in vain, try to find another minimum using the
971second clause for {\tt min}.  So there is a double drawback:
972firstly, an open choice point consumes memory, and secondly the
973unsuccessful evaluation of the second clause costs execution time.
974
975To achieve the same logic, but more efficient behaviour, the
976programmer can introduce a {\it cut}.
977For example {\tt min} is typically encoded as follows:
978\begin{code}
979min(X,Y, Min) :- X<Y, !, Min = X.
980min(X,Y, Y).
981\end{code}
982The cut removes the unnecessary choice point, which means that the
983second clause will never be executed if the first clause passed the
984cut.  This effectively makes the test in the second clause redundant,
985and it can therefore be removed.
986
987  \subsection{Prune alternative solutions}
988\index{prune}
989A cut may occur anywhere where a goal may occur, consider the following:
990\begin{code}
991first_prime(X, P) :-
992    prime(X,P), !.
993\end{code}
994where \verb'first_prime' returns the first prime number smaller than \verb'X'.
995In this case, it calls a predicate \verb'prime/2', which generates prime
996numbers smaller than \verb'X', starting from the largest one. The effect of
997the cut here is to prune away all the remaining solutions to \verb'prime(X,P)'
998once the first one is generated, so that on backtracking, \verb'prime(X,P)'
999is not tried for alternative solutions. The cut will also commit the execution
1000to this clause for \verb'first_prime/2', but as there is only one clause,
1001this has no visible effect.
1002
1003
1004%%----------------------------------------------------------------------
1005%\section{Prolog vs Imperative Languages}
1006%%----------------------------------------------------------------------
1007%\quickref{Comparison Prolog vs Imperative Programming}{
1008%\begin{tabular}{p{7cm}p{7cm}}
1009%{\bf Prolog} & {\bf Imperative Language} \\
1010%\hline
1011%\hline
1012%set of clauses & program \\
1013%\hline
1014%predicate (set of clauses with same name and arity) & procedure \\
1015%\hline
1016%clause (rule or fact) & if statement / one arm of nondeterministic case
1017%        statement / sequence of procedure calls \\
1018%\hline
1019%goal invocation & procedure call \\
1020%\hline
1021%unification & parameter passing / assignment / dynamic memory allocation /
1022%        conditional branching \\
1023%\hline
1024%backtracking & continuation passing / exception handling /
1025%        execution state manipulation \\
1026%\hline
1027%logical variable & pointer manipulation \\
1028%\hline
1029%tail recursion & iteration \\
1030%\end{tabular}
1031%}
1032
1033%----------------------------------------------------------------------
1034\section{Common Pitfalls}
1035%----------------------------------------------------------------------
1036Prolog is different from conventional programming languages, and a common
1037problem is to program Prolog like a conventional language. Here are some
1038points to note:
1039
1040\begin{itemize}
1041\item Unification is more powerful than normal case discrimination (see 
1042  section~\ref{unif}); 
1043\item Prolog procedure calls are more powerful than conventional procedure
1044calls. In particular, backtracking is possible (see section~\ref{back});
1045\end{itemize}
1046
1047    \subsection{Unification works both ways}
1048\label{unif}
1049One common problem is to write a predicate expecting certain instantiation
1050patterns for the arguments, and then get unexpected results when the 
1051arguments do not conform to the expected pattern. An example is the
1052member relation, intended to check if an item \verb'Item' is a member of
1053a list or not. This might be written as:
1054
1055
1056\begin{code}
1057member(Item, [Item|_]).
1058member(Item, [_|List]) :- member(Item, List).
1059\end{code}
1060
1061The expected usage assumes both \verb'Item' and the list are ground. In 
1062such cases, the above predicate does indeed check if \verb'Item' occurs in
1063the list given as a second argument. However, if either of the arguments are
1064not ground, then potentially unexpected behaviour might occur. Consider
1065the case where \verb'Item' is a variable, then the above predicate will 
1066enumerate the elements of the list successively through backtracking. On
1067the other hand, if any of the list elements of the list is a variable, they
1068would be unified with \verb'Item'. Other instantiation patterns for either
1069arguments can produce even more complex results. 
1070
1071If the intended meaning is simply to check if \verb'Item' is a member of 
1072a list, this can be done by:
1073
1074\begin{code}
1075  \% is_member(+Element, +List)
1076  \% check if Element is an element that occurs in a List of
1077  \% ground elements
1078is_member(Item, [Element|_]) :- Item == Element.
1079is_member(Item, [_|List]) :- nonvar(List), is_member(Item, List).
1080\end{code}
1081
1082Note the use of comments to make clear the intention of the use of the
1083predicate. The convention used is that `+' indicates that an argument should
1084be instantiated (i.e. not a variable), `-' for an argument that should be
1085an uninstantiated variable, and '?' indicates that there is no restrictions
1086on the mode of the argument.
1087
1088    \subsection{Unexpected backtracking}
1089\label{back}
1090Remember that when coding in Prolog, any predicate {\it may\/} be backtracked 
1091into. So correctness in Prolog requires:
1092
1093\begin{itemize}
1094\item Predicate returns the correct answer when first called.
1095\item Predicate behaves correctly when backtracked into.
1096\end{itemize}
1097
1098Recall that backtracking causes alternative choices to be explored, if
1099there are any.  Typically another choice corresponds to another clause
1100in the poredicate definition, but alternative choices may come from
1101disjunction (see above) or built-in predicates with multiple
1102(alternative) solutions. 
1103The programmer should make sure that a predicate will only produce
1104those solutions that are wanted. Excess alternatives can be removed by
1105coding the program not to produce them, or by the cut, or the conditional.
1106
1107For example, to return only the {\it first\/} member, in the
1108\verb'is_member/2' example, 
1109the predicate can be coded using the cut, as follows:
1110
1111\begin{code}
1112is_member(Item, [Element|_]) :- Item == Element, !.
1113is_member(Item, [_|List]) :- nonvar(List), is_member(Item, List).
1114\end{code}
1115
1116\subsubsection{Using conditional}
1117
1118Another way to remove excess choice points is the conditional:
1119
1120\begin{code}
1121is_member(Item, [Element|List]) :- 
1122    ( Item == Element ->
1123        true 
1124    ;
1125        nonvar(List), is_member(Item, List)
1126    ).
1127\end{code}
1128
1129
1130\section{Exercises}
1131
1132\begin{enumerate}
1133
1134\item
1135
1136Consider again the ``family tree'' example (see Section~\ref{syntax}).
1137As well as the \texttt{parent/2} predicate, suppose we have a
1138\texttt{male/1} predicate as follows:
1139
1140\begin{code}
1141male(abe).
1142male(homer).
1143male(herbert).
1144male(bart).
1145\end{code}
1146
1147Define a \texttt{brother/2} predicate, expressed just in terms of
1148\texttt{parent/2} and \texttt{male/1}.  Make sure Homer is not considered
1149his own brother.
1150
1151
1152\item
1153
1154Consider the following alternative definition of \texttt{ancestor/2}:
1155
1156\begin{code}
1157ancestor(X, Y) :- parent(X, Y).
1158ancestor(X, Y) :- ancestor(X, Z), parent(Z, Y).
1159\end{code}
1160
1161What is wrong with this code?  What happens if you use it to find out
1162who Bart is an ancestor of?
1163
1164\end{enumerate}
1165
1166%HEVEA\cutend
1167