Document 14115700

advertisement
'
CS2LAP: Logi and Prolog 2000/2001
An Introdution to Prolog Programming
$
An Introdution to Prolog Programming
Ulle Endriss
King's College London
&
Ulle Endriss, King's College London
'
1
CS2LAP: Logi and Prolog 2000/2001
An Introdution to Prolog Programming
%
$
Contents
Introdution to Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
List Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Arithmeti Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Working with Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Baktraking and Cuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Negation as Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Prolog Programs as Logi Formulas . . . . . . . . . . . . . . . . . . . . . . . 86
&
Ulle Endriss, King's College London
2
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
What is Prolog?
Prolog (pro gramming in log i) is a logial programming
language: programs orrespond to sets of logial formulas and
the Prolog interpreter uses logial methods to resolve queries.
Prolog is a delarative language: you speify what problem you
want to solve rather than how to solve it.
Prolog is very useful in some problem areas, like artiial
intelligene, natural language proessing, databases, . . . , but
pretty useless in others, like graphis or numerial algorithms.
&
Ulle Endriss, King's College London
3
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
%
$
Literature
Prolog leture notes
I. Bratko. Prolog Programming for Artiial Intelligene.
2nd edition, Addison-Wesley Publishing Company, 1990.
F. W. Cloksin and C. S. Mellish. Programming in Prolog.
4th edition, Springer-Verlag, 1994.
&
L. Sterling and E. Shapiro. The Art of Prolog. MIT Press, 1986.
any other textbook on Prolog
Ulle Endriss, King's College London
4
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Fats
A little Prolog program onsisting of four fats :
bigger(
bigger(
bigger(
bigger(
&
elephant, horse).
horse, donkey).
donkey, dog).
donkey, monkey).
Ulle Endriss, King's College London
5
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
%
$
Queries
After ompilation we an query the Prolog system:
?- bigger( donkey, dog).
Yes
?- bigger( monkey, elephant).
No
&
Ulle Endriss, King's College London
6
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
A Problem
The following query does not sueed!
?- bigger( elephant, monkey).
No
The prediate bigger/2 apparently is not quite what we want.
What we'd really like is the transitive losure of bigger/2. In other
words: a prediate that sueeds whenever it is possible to go from
the rst animal to the seond by iterating the previously dened
fats.
&
Ulle Endriss, King's College London
7
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
%
$
Rules
The following two rules dene is bigger/2 as the transitive losure
of bigger/2 (via reursion):
is_bigger( X, Y) :- bigger( X, Y).
is_bigger( X, Y) :- bigger( X, Z), is_bigger( Z, Y).
&
"
\if"
Ulle Endriss, King's College London
"
\and"
8
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
Now it works
$
?- is_bigger( elephant, monkey).
Yes
Even better, we an use the variable X:
?- is_bigger( X, donkey).
X = horse ;
X = elephant ;
No
&
Press ; to nd alternative solutions. No at the end indiates there are
no further solutions.
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
9
Introdution to Prolog
%
$
Another Example
Are there any animals whih are both smaller than a donkey and
bigger than a monkey?
?- is_bigger( donkey, X), is_bigger( X, monkey).
No
&
Ulle Endriss, King's College London
10
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Terms
Prolog terms are either numbers, atoms, variables, or ompound
terms.
Atoms
start with a lowerase letter or are enlosed in single quotes:
elephant, xYZ, a_123, 'Another pint please'
&
Variables
start with a apital letter or the undersore:
X, Elephant, _G177, MyVariable, _
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
11
Introdution to Prolog
%
$
Terms (ontinued)
Compound terms
arguments (terms):
have a funtor (an atom) and a number of
is_bigger( horse, X), f( g( Alpha, _), 7),
'My Funtor'( dog)
Atoms and numbers are alled atomi terms.
&
Atoms and ompound terms are alled prediates.
Terms without variables are alled ground terms.
Ulle Endriss, King's College London
12
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
Fats and Rules
$
are prediates followed by a dot. Fats are used to dene
something as being unonditionally true.
Fats
bigger( elephant, horse).
parent( john, mary).
onsist of a head and a body separated by :-. The head of a
rule is true if all prediates in the body an be proved to be true.
Rules
&
grandfather( X, Y) :father( X, Z),
parent( Z, Y).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
13
Introdution to Prolog
%
$
Programs and Queries
Fats and rules are alled lauses. A Prolog program is a
list of lauses.
Programs.
are prediates (or sequenes of prediates) followed by a
dot. They are typed in at the Prolog prompt and ause the system to
reply.
Queries
?- is_bigger( horse, X), is_bigger( X, dog).
X = donkey
Yes
&
Ulle Endriss, King's College London
14
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Built-in Prediates
Compiling a program le:
?- onsult( 'big-animals.pl').
Yes
Writing terms on the sreen:
&
?- write( 'Hello World!'), nl.
Hello World!
Yes
Ulle Endriss, King's College London
15
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
%
$
Mathing
Two terms math if they are either idential or if they an be made
idential by substituting their variables with suitable ground terms.
We an expliitly ask Prolog whether two given terms math by using
the equality-prediate = (written as an inx operator).
?- born( mary, yorkshire) = born( mary, X).
X = yorkshire
Yes
&
The variable instantiations are reported in Prolog's answer.
Ulle Endriss, King's College London
16
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Mathing (ontinued)
?- f( a, g( X, Y)) = f( X, Z), Z = g( W, h( X)).
X = a
Y = h(a)
Z = g(a, h(a))
W = a
Yes
&
?- p( X, 2, 2) = p( 1, Y, X).
No
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
17
Introdution to Prolog
%
$
The Anonymous Variable
The variable (undersore) is alled the anonymous variable. Every
ourrene of represents a dierent variable (whih is why
instantiations are not reported).
?- p( _, 2, 2) = p( 1, Y, _).
Y = 2
Yes
&
Ulle Endriss, King's College London
18
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Answering Queries
Answering a query means proving that the goal represented by that
query an be satised (aording to the programs urrently in
memory).
Programs are lists of fats and rules. A fat delares
something as being true. A rule states onditions for a statement
being true.
Reall:
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
19
Introdution to Prolog
%
$
Answering Queries (ontinued)
If a goal mathes with a fat, it is satised.
If a goal mathes the head of a rule, then it is satised if the goal
represented by the rule's body is satised.
If a goal onsists of several subgoals separated by ommas, then
it is satised if all its subgoals are satised.
&
When trying to satisfy goals with built-in prediates like write/1
Prolog also performs the assoiated ation (e.g. writing on the
sreen).
Ulle Endriss, King's College London
20
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
$
Example: Mortal Philosophers
Consider the following argument:
&
All men are mortal.
Sorates is a man.
Hene, Sorates is mortal.
It has two premises and a onlusion.
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
21
Introdution to Prolog
%
$
Translating it into Prolog
The two premises an be expressed as a little Prolog program:
mortal( X) :- man( X).
man( sorates).
The onlusion an then be formulated as a query:
&
?- mortal( sorates).
Yes
Ulle Endriss, King's College London
22
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
Goal Exeution
$
1. The query mortal( sorates) is made the initial goal.
2. Prolog looks for the rst mathing fat or head of rule and nds
mortal( X). Variable instantiation: X = sorates.
3. This variable instantiation is extended to the rule's body, i.e.
man( X) beomes man( sorates).
4. New goal: man( sorates).
&
5. Suess, beause man( sorates) is a fat itself.
6. Therefore, also the initial goal sueeds.
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
23
Introdution to Prolog
%
$
Summary: Syntax
All Prolog expression are made up from terms (numbers, atoms,
variables, or ompound terms).
Atoms start with lowerase letters or are enlosed in single
quotes; variables start with apital letters or undersore.
Prolog programs are lists of fats and rules (lauses ).
&
Queries are submitted to the system to initiate a omputation.
Some built-in prediates have speial meaning.
Ulle Endriss, King's College London
24
%
'
CS2LAP: Logi and Prolog 2000/2001
Introdution to Prolog
Summary: Answering Queries
When answering a query Prolog tries to prove the orresponding
goal's satisability. This is done using the rules and fats given
in a program.
A goal is exeuted by mathing it with the rst possible fat or
head of a rule. In the latter ase the rule's body beomes the
new goal.
The variable instantiations made during mathing are arried
along throughout the omputation and reported at the end.
&
$
Only the anonymous variable an be instantiated dierently
whenever it ours.
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
25
List Manipulation
%
$
Lists in Prolog
An example for a Prolog list:
[elephant, horse, donkey, dog℄
are enlosed in square brakets. Their elements ould be any
Prolog terms (inluding other lists). The empty list is [℄.
Lists
Another example:
&
[a, X, [℄, f(X,y), 47, [a,b,℄, bigger(ow,dog)℄
Ulle Endriss, King's College London
26
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
$
Internal Representation
Internally, the list
[a, b, ℄
orresponds to the term
.( a, .( b, .( , [℄)))
That means, this is just a new notation. Internally, lists are just
ompound terms with the funtor . and the speial atom [℄ as an
argument on the innermost level.
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
27
List Manipulation
%
$
The Bar Notation
If a bar | is put just before the last term in a list, it means that this
last term denotes a sub-list. Inserting the elements before the bar at
the beginning of the sub-list yields the entire list.
For example, [a, b, , d℄ is the same as [a, b | [, d℄℄.
&
Ulle Endriss, King's College London
28
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
Examples
$
Extrat the seond element from a given list:
?- [a, b, , d, e℄ = [_, X | _℄.
X = b
Yes
Make sure the rst element is a 1 and get the sub-list after the
seond element:
&
?- MyList = [1, 2, 3, 4, 5℄, MyList = [1, _ | Rest℄.
MyList = [1, 2, 3, 4, 5℄
Rest = [3, 4, 5℄
Yes
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
29
List Manipulation
%
$
Head and Tail
The rst element of a list is alled its head. The rest of the list is
alled its tail. (The empty list doesn't have a head.)
A speial ase of the bar notation { with exatly one element before
the bar { is alled the head/tail-pattern. It an be used to extrat
head and/or tail from a list. Example:
?- [elephant, horse, tiger, dog℄ = [Head | Tail℄.
Head = elephant
Tail = [horse, tiger, dog℄
Yes
&
Ulle Endriss, King's College London
30
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
$
Head and Tail (ontinued)
Another example:
?- [elephant℄ = [X | Y℄.
X = elephant
Y = [℄
Yes
&
The tail of a list is always a list itself. The head of a list is an
element of that list. It doesn't have to be a list itself, but it ould be.
Note:
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
31
List Manipulation
%
$
Appending Lists
We want to write a prediate onat lists/3 to onatenate two
given lists.
It should work like this:
?- onat_lists( [1, 2, 3, 4℄, [dog, ow, tiger℄, L).
L = [1, 2, 3, 4, dog, ow, tiger℄
Yes
&
Ulle Endriss, King's College London
32
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
$
Solution
The prediate onat lists/3 is implemented reursively. The base
ase is when one of the lists is empty. In every reursion step we take
o the head and use the same prediate again, with the (shorter) tail,
until we reah the base ase.
onat_lists( [℄, List, List).
&
onat_lists( [Elem|List1℄, List2, [Elem|List3℄) :onat_lists( List1, List2, List3).
Ulle Endriss, King's College London
'
33
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
Do More
%
$
Among other things, onat lists/3 an also be used for
deomposing lists:
?- onat_lists( Begin, End, [1, 2, 3℄).
Begin = [℄
End = [1, 2, 3℄ ;
Begin = [1℄
End = [2, 3℄ ;
Begin = [1, 2℄
End = [3℄ ;
Begin = [1, 2, 3℄
End = [℄ ;
No
&
Ulle Endriss, King's College London
34
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
$
Built-in Prediates for List Manipulation
append/3: Append two lists (same as onat lists/3).
?- append( [1, 2, 3℄, List, [1, 2, 3, 4, 5℄).
List = [4, 5℄
Yes
length/2: Get the length of a list.
&
?- length( [tiger, donkey, ow, tiger℄, N).
N = 4
Yes
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
35
List Manipulation
Membership
%
$
member/2: Test for membership.
?- member( tiger, [dog, tiger, elephant, horse℄).
Yes
Baktraking into member/2:
?- member( X, [dog, tiger, elephant℄).
X = dog ;
X = tiger ;
X = elephant ;
No
&
Ulle Endriss, King's College London
36
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
Example
$
Consider the following program:
show( List) :member( Element, List),
write( Element),
nl,
fail.
&
Note:
fail is a built-in prediate that always fails.
What happens when you submit a query like the following one?
?- show( [elephant, horse, donkey, dog℄).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
37
List Manipulation
Example (ontinued)
%
$
?- show( [elephant, horse, donkey, dog℄).
elephant
horse
donkey
dog
No
The fail at the end of the rule auses Prolog to baktrak. The
subgoal member( Element, List) is the only hoiepoint. In every
baktraking-yle a new element of List is mathed with the
variable Element. Eventually, the query fails (No).
&
Ulle Endriss, King's College London
38
%
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
$
More Built-in Prediates
reverse/2: Reverse the order of elements in a list.
?- reverse( [1, 2, 3, 4, 5℄, X).
X = [5, 4, 3, 2, 1℄
Yes
&
More built-in prediates an be found in the referene manual.
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
List Manipulation
Summary: List Manipulation
$
List notation:
{ normal: [Element1, Element2, Element3℄ (empty list: [℄)
{ internal: .( Element1, .( Element2, .( Element3, [℄)))
{ bar notation: [Element1, Element2 | Rest℄
{ head/tail-pattern: [Head | Tail℄
Many prediates an be implemented reursively, exploiting the
head/tail-pattern.
&
39
%
Built-in prediates: append/3, member/2, length/2, . . .
Ulle Endriss, King's College London
40
%
'
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
$
Arithmeti Expressions in Prolog
Prolog omes with a range of predened arithmeti funtions and
operators. Something like 3 + 5, for example, is a valid Prolog term.
So, what's happening here?
?- 3 + 5 = 8.
No
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
41
Arithmeti Expressions
%
$
Mathing v Arithmeti Evaluation
The terms 3 + 5 and 8 do not math. In fat, when we are interested
in the sum of the numbers 3 and 5, we an't get it through mathing,
but through arithmeti evaluation.
We have to use the is-operator:
?- X is 3 + 5.
X = 8
Yes
&
Ulle Endriss, King's College London
42
%
'
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
The is-Operator
$
The is-operator auses the term to its right to be evaluated as an
arithmeti expressions and mathes the result of that evaluation with
the term on the operator's left. (The term on the left should usually
be a variable.)
Example:
?- Value is 3 * 4 + 5 * 6, OtherValue is Value / 11.
Value = 42
OtherValue = 3.81818
Yes
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
43
Arithmeti Expressions
%
$
The is-Operator (ontinued)
Note that the term to the right will be evaluated to an integer (i.e.
not a oat) whenever possible:
?- X is 3.5 + 4.5.
X = 8
Yes
&
That means, a further subgoal like X = 8.0 would not sueed.
Ulle Endriss, King's College London
44
%
'
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
$
Example: Length of a List
Instead of using length/2 we an now write our own prediate to
ompute the length of a list:
len( [℄, 0).
len( [_ | Tail℄, N) :len( Tail, N1),
N is N1 + 1.
&
Ulle Endriss, King's College London
'
45
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
Funtions
%
$
Prolog provides a number of built-in arithmeti funtions that an be
used with the is-operator. See manual for details.
Examples:
?- X is max( 8, 6) - sqrt( 2.25) * 2.
X = 5
Yes
&
?- X is (47 mod 7) ** 3.
X = 125
Yes
Ulle Endriss, King's College London
46
%
'
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
Relations
$
Arithmeti relations are used to ompare two arithmeti values.
Example:
?- 2 * 3 > sqrt( 30).
Yes
The following relations are available:
&
=:=
>
<
arithmeti equality
greater
lower
=n=
>=
=<
arithmeti inequality
greater or equal
lower or equal
Ulle Endriss, King's College London
'
47
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
Examples
%
$
Reall the dierene between mathing and arithmeti evaluation :
?- 3 + 5 = 5 + 3.
No
?- 3 + 5 =:= 5 + 3.
Yes
Reall the operator preedene of arithmetis:
&
?- 2 + 3 * 4 =:= (2 + 3) * 4.
No
?- 2 + 3 * 4 =:= 2 + (3 * 4).
Yes
Ulle Endriss, King's College London
48
%
'
CS2LAP: Logi and Prolog 2000/2001
Arithmeti Expressions
$
Summary: Arithmetis in Prolog
For logial pattern mathing use =, for arithmeti evaluation use
the is-operator.
A range of built-in arithmeti funtions is available (some are
written as operators, e.g. +).
Arithmeti expressions an be ompared using arithmeti
relations like < or =:= (without is-operator).
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
49
Working with Operators
%
$
Operators in Prolog
Operators provide a more onvenient way of writing ertain terms in
Prolog. For example, we an write 3 * 155 instead of *( 3, 155) or
N is M + 1 instead of is( N, +( M, 1)).
Both notations are onsidered to be equivalent, i.e. mathing works:
?- +( 1000, 1) = 1000 + 1.
Yes
&
The objetive of this leture is to show you how you an dene your
own operators in Prolog.
Ulle Endriss, King's College London
50
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
Operator Preedene
$
Some operators bind stronger than others. In mathematis, for
example, * binds stronger than +. We also say, * has a lower
preedene than +.
In Prolog, operator preedenes are numbers (in SWI-Prolog between
0 and 1200). The arithmetial operator *, for example, has
preedene 400, + has preedene 500.
This is why Prolog is able to ompute the orret result in the
following example (i.e. not 25):
&
?- X is 2 + 3 * 5.
X = 17
Yes
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
51
Working with Operators
Preedene of Terms
%
$
The preedene of a term is dened as the preedene of its prinipal
operator. If the prinipal funtor isn't (written as) an operator or the
term is enlosed in parentheses then the preedene is dened as 0.
Examples:
The preedene of 3 + 5 is 500.
The preedene of 3 * 3 + 5 * 5 is also 500.
The preedene of sqrt( 3 + 5) is 0.
The preedene of elephant is 0.
The preedene of (3 + 5) is 0.
The preedene of 3 * +( 5, 6) is 400.
&
Ulle Endriss, King's College London
52
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
$
Operator Types
Operators an be divided into three groups:
inx operators, like + in Prolog
prex operators, like : in logi
postx operators, like ! in math
Is giving the type of an operator and its preedene already enough
for Prolog to fully `understand' the struture of a term ontaining
that operator?
&
Ulle Endriss, King's College London
53
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
%
$
Example
Consider the following example:
?- X is 25 - 10 - 3.
X = 12
Yes
Why not 18?
&
Obviously, preedene and type alone are not enough to fully speify
the strutural properties of an operator.
Ulle Endriss, King's College London
54
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
Operator Assoiativity
$
We also have to speify the assoiativity of an operator: -, for
example, is left-assoiative. This is why 20 - 10 - 3 is interpreted
as (20 - 10) - 3.
In Prolog, assoiativity is represented by atoms like yfx. Here f
indiates the position of the operator (i.e. yfx denotes an inx
operator) and x and y indiate the positions of the arguments. A y
should be read as on this position a term with a preedene lower or
equal to that of the operator has to our, whereas x means that on
this position a term with a preedene stritly lower to that of the
operator has to our.
&
Ulle Endriss, King's College London
'
55
CS2LAP: Logi and Prolog 2000/2001
Pattern
yfx
xfy
xfx
yfy
fy
&
fx
yf
xf
Working with Operators
Assoiativity Patterns
Assoiativity
non-assoiative
assoiative
non-assoiative
Ulle Endriss, King's College London
$
Examples
inx
left-assoiative
+, -, *
inx
right-assoiative , (for subgoals)
inx
non-assoiative =, is, < (i.e. no nesting)
makes no sense, struturing would be impossible
prex assoiative
prex
postx
postx
%
- (i.e. --5 not possible)
56
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
Cheking Preedene and Assoiativity
$
You an use the built-in prediate urrent op/3 to hek preedene
and assoiativity of urrently dened operators.
?- urrent_op( Pre, Asso, *).
Pre = 400
Asso = yfx
Yes
&
?- urrent_op( Pre, Asso, is).
Pre = 700
Asso = xfx
Yes
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
57
Working with Operators
Dening Operators
%
$
New operators are dened using the op/3-prediate. This an be
done by submitting the operator denition as a query. Terms using
the new operator will then be equivalent to terms using the operator
as a normal funtor, i.e. prediate denitions will work.
For the following example assume the big animals program has
previously been ompiled:
?- op( 400, xfx, is_bigger).
Yes
&
?- elephant is_bigger dog.
Yes
Ulle Endriss, King's College London
58
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
Query Exeution at Compilation Time
$
It is possible to write queries into a program le (using :- as a prex
operator). They will be exeuted whenever the program is ompiled.
If for example the le my-file.pl ontains the line
:- write( 'Hello, have a beautiful day!').
this will have the following eet:
?- onsult( 'my-file.pl').
Hello, have a beautiful day!
my-file.pl ompiled, 0.00 se, 224 bytes.
Yes
?-
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
59
Working with Operators
%
$
Operator Denition at Compilation Time
You an do the same for operator denitions. For example, the line
:- op( 200, fy, small).
inside a program le will ause a prex operator alled small to be
delared whenever the le is ompiled. It an be used inside the
program itself, in other programs, and in user queries.
&
Ulle Endriss, King's College London
60
%
'
CS2LAP: Logi and Prolog 2000/2001
Working with Operators
$
Summary: Operators
The strutural properties of an operator are determined by its
preedene (a number) and its assoiativity pattern (like e.g.
yfx).
Use urrent op/3 to hek operator denitions.
Use op/3 to make your own operator denitions.
&
Operator denitions are usually inluded inside a program le as
queries (using :-, i.e. like a rule without a head).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
61
Baktraking and Cuts
Baktraking
%
$
Subgoals that an be satised in more than one way
provide hoiepoints. Example:
Choiepoints.
..., member( X, [a, b, ℄), ...
This is a hoiepoint, beause the variable X ould be mathed with
either a, b, or .
During goal exeution Prolog keeps trak of
hoiepoints. If a partiular path turns out to be a failure, it jumps
bak to the most reent hoiepoint and tries the next alternative.
This proess is known as baktraking.
Baktraking.
&
Ulle Endriss, King's College London
62
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
$
Example
Given a list in the rst argument, the prediate permutation/2
generates all possible permutations of that list in the seond
argument through baktraking (if the user presses ; after every
solution):
permutation( [℄, [℄).
&
permutation( List, [Element | Permutation℄) :selet( List, Element, Rest),
permutation( Rest, Permutation).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
63
Baktraking and Cuts
%
$
Example (ontinued)
?- permutation( [1, 2, 3℄, X).
X = [1, 2, 3℄ ;
X = [1, 3, 2℄ ;
X = [2, 1, 3℄ ;
X = [2, 3, 1℄ ;
X = [3, 1, 2℄ ;
X = [3, 2, 1℄ ;
No
&
Ulle Endriss, King's College London
64
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
Problems with Baktraking
$
Asking for alternative solutions generates wrong answers for this
prediate denition:
remove_dupliates( [℄, [℄).
remove_dupliates( [Head | Tail℄, Result) :member( Head, Tail),
remove_dupliates( Tail, Result).
&
remove_dupliates( [Head | Tail℄, [Head | Result℄) :remove_dupliates( Tail, Result).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
65
Baktraking and Cuts
Problems with Baktraking (ontinued)
%
$
Example:
?- remove_dupliates( [a, b, b, , a℄, List).
List = [b, , a℄ ;
List = [b, b, , a℄ ;
List = [a, b, , a℄ ;
&
List = [a, b, b, , a℄ ;
No
Ulle Endriss, King's College London
66
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
$
Introduing Cuts
Sometimes we want to prevent Prolog from baktraking into ertain
hoiepoints, either beause the alternatives would yield wrong
solutions (like in the previous example) or for eÆieny reasons.
This is possible by using a ut, written as !. This predened
prediate always sueeds and prevents Prolog from baktraking into
subgoals plaed before the ut inside the same rule body.
&
Ulle Endriss, King's College London
67
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
%
$
Example
The orret program for removing dupliates from a list:
remove_dupliates( [℄, [℄).
remove_dupliates( [Head | Tail℄, Result) :member( Head, Tail), !,
remove_dupliates( Tail, Result).
&
remove_dupliates( [Head | Tail℄, [Head | Result℄) :remove_dupliates( Tail, Result).
Ulle Endriss, King's College London
68
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
$
Cuts
When exeuting the subgoals in a rule's body the term
parent goal refers to the goal that aused the mathing of the head of
the urrent rule.
Parent goal.
Whenever a ut is enountered in a rule's body, all hoies
made between the time that rule's head has been mathed
with the parent goal and the time the ut is passed are nal,
i.e. any hoiepoints are being disarded.
&
Ulle Endriss, King's College London
'
69
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
Exerise
%
$
Using uts (but without using negation), implement a prediate
add/3 to insert an element into a list, if that element isn't already a
member of the list. Make sure there are no wrong alternative
solutions. Examples:
?- add( elephant, [dog, donkey, rabbit℄, List).
List = [elephant, dog, donkey, rabbit℄ ;
No
&
?- add( donkey, [dog, donkey, rabbit℄, List).
List = [dog, donkey, rabbit℄ ;
No
Ulle Endriss, King's College London
70
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
$
Solution
add( Element, List, List) :member( Element, List), !.
add( Element, List, [Element | List℄).
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
71
Baktraking and Cuts
%
$
Problems with Cuts
The prediate add/3 does not work as intended when the last
argument is already instantiated! Example:
?- add( dog, [dog, at, bird℄, [dog, dog, at, bird℄).
Yes
&
Ulle Endriss, King's College London
72
%
'
CS2LAP: Logi and Prolog 2000/2001
Baktraking and Cuts
$
Summary: Baktraking and Cuts
Baktraking allows Prolog to nd all alternative solutions to a
given query.
That is: Prolog provides the searh strategy, not the
programmer! This is why Prolog is alled a delarative language.
Carefully plaed uts (!) an be used to prevent Prolog from
baktraking into ertain subgoals. This may make a program
more eÆient and/or avoid the generation of (wrong) alternative.
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
73
Negation as Failure
Prolog's Answers
%
$
Consider the following Prolog program:
animal( elephant).
animal( donkey).
animal( tiger).
. . . and the system's reation to the following queries:
?- animal( donkey).
Yes
&
?- animal( dukbill).
No
Ulle Endriss, King's College London
74
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
The Closed World Assumption
In Prolog, Yes means a statement is provably true. Consequently, No
means a statement is not provably true. This only means that suh a
statement is false, if we assume that all relevant information is
present in the respetive Prolog program.
For the semantis of Prolog programs we usually do make this
assumption. It is alled the Closed World Assumption : we assume
that nothing outside the world desribed by a partiular Prolog
program exists (is true).
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
75
Negation as Failure
%
$
The n+-Operator
If we are not interested whether a ertain goal sueeds, but rather
whether it fails, we an use the n+-operator (negation). n+ Goal
sueeds, if Goal fails (and vie versa). Example:
?- \+ member( 17, [1, 2, 3, 4, 5℄).
Yes
&
This is known as negation as failure : Prolog's negation is dened as
the failure to provide a proof.
Ulle Endriss, King's College London
76
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
Negation as Failure: Example
Consider the following program:
married(
married(
married(
married(
peter, luy).
paul, mary).
bob, juliet).
harry, geraldine).
&
single( Person) :\+ married( Person, _),
\+ married( _, Person).
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
77
Negation as Failure
%
$
Example (ontinued)
After ompilation Prolog reats as follows:
?- single( mary).
No
?- single( laudia).
Yes
&
In the losed world desribed by our Prolog program Claudia has to
be single, beause she is not known to be married.
Ulle Endriss, King's College London
78
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
Where to use n+
Note that the n+-operator an only be used to negate goals. These
are either (sub)goals in the body of a rule or (sub)goals of a query.
We annot negate fats or the heads of rules, beause this would
atually onstitute a redenition of the n+-operator (in other words
an expliit denition of Prolog's negation, whih wouldn't be
ompatible with the losed world assumption).
&
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
79
Negation as Failure
Disjuntion
%
$
We already now know onjuntion (omma) and negation (n+). We
also know disjuntion, beause several rules with the same head
orrespond to a disjuntion.
Disjuntion an also be implemented diretly within one rule by
using ; (semiolon). Example:
parent( X, Y) :- father( X, Y); mother( X, Y).
This is equivalent to the following program:
&
parent( X, Y) :- father( X, Y).
parent( X, Y) :- mother( X, Y).
Ulle Endriss, King's College London
80
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
Example
Write a Prolog program to evaluate a row of a truth table. (Assume
appropriate operator denitions have been made before.)
Examples:
?- true and false.
No
&
?- true and (true and false implies true) and neg false.
Yes
Ulle Endriss, King's College London
81
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
%
$
Solution
% Falsity
false :- fail.
% Conjuntion
and( A, B) :- A, B.
&
% Disjuntion
or( A, B) :- A; B.
Ulle Endriss, King's College London
82
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
Solution (ontinued)
% Negation
neg( A) :- \+ A.
% Impliation
implies( A, B) :- A, !, B.
implies( _, _).
&
Ulle Endriss, King's College London
83
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
%
$
Note
We know that in lassial logi :A is equivalent to A)?. Similarly,
instead of using n+ in Prolog we ould dene our own negation
operator as follows:
neg( A) :- A, !, fail.
neg( _).
&
Ulle Endriss, King's College London
84
%
'
CS2LAP: Logi and Prolog 2000/2001
Negation as Failure
$
Summary: Negation and Disjuntion
Closed World Assumption : In Prolog everything that annot be
proven from the given fats and rules is onsidered false.
Negation as Failure : Prolog's negation is implemented as the
failure to provide a proof for a statement.
Goals an be negated using the n+-operator.
&
A disjuntion of goals an be written using ; (semiolon).
(The omma between two subgoals denotes a onjuntion.)
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
85
Prolog Programs as Logi Formulas
%
$
Logi and Prolog
Today we shall see how Prolog programs an be interpreted as sets of
logi formulas. In fat, when proessing a query, Prolog is atually
applying the rules of a logial dedution system similar to the
goal-direted alulus.
&
Ulle Endriss, King's College London
86
%
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
$
Correspondene
Prolog
First-order Logi
prediate
argument
variable
atom
prediate
term
universally quantied variable
onstant/funtion/prediate symbol
sequene of subgoals
onjuntion
impliation (other way round)
&
:-
Ulle Endriss, King's College London
87
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
%
$
Question
What is the logial meaning of this program?
bigger( elephant, horse).
bigger( horse, donkey).
is_bigger( X, Y) :- bigger( X, Y).
is_bigger( X, Y) :- bigger( X, Z), is_bigger( Z, Y).
&
Ulle Endriss, King's College London
88
%
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
$
Answer
f
(
)
bigger (horse; donkey );
8x:8y:(bigger (x; y ) ) is bigger (x; y ));
8x:8y:8z:(bigger (x; z ) ^ is bigger (z; y ) ) is
bigger elephant; horse ;
&
(
bigger x; y
))
Ulle Endriss, King's College London
'
CS2LAP: Logi and Prolog 2000/2001
g
89
Prolog Programs as Logi Formulas
%
$
Translation of Programs
Prediates remain the same (syntatially).
Commas separating subgoals beome ^.
:- beomes
&
)
and the order of head and body is hanged.
Every variable is bound to a universal quantier (8).
Ulle Endriss, King's College London
90
%
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
$
Translation of Queries
Queries are translated like rules; the `empty head' is translated as ?.
This orresponds to the negation of the goal whose provability we try
to test when submitting a query to Prolog.
Logially speaking, instead of deriving the goal itself, we try to prove
that adding the negation of the goal to the program would make it
inonsistent:
P ; A)? ` ?
i P ` A
&
Ulle Endriss, King's College London
91
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
%
$
Example
The query
?- is_bigger( elephant, X), is_bigger( X, donkey).
orresponds to the following rst-order formula:
&
(
(
) ^ is
8x: is bigger elephant; x
Ulle Endriss, King's College London
(
bigger x; donkey
) ) ?)
92
%
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
Horn Formulas
$
The formulas we get when translating all have the same struture:
A1 ^ A2 ^ ^ A
n
) B
Suh a formula an be rewritten as follows:
A1 ^ A2 ^ ^ A
(
n
) B
: A1 ^ A2 ^ ^ A
&
n)
:A1 _ :A2 _ _ :A
_ B
n_B
Hene, formulas obtained from translating Prolog lauses an always
be rewritten as equivalent Horn formulas (disjuntions of literals
with at most one positive literal).
Ulle Endriss, King's College London
93
'
CS2LAP: Logi and Prolog 2000/2001
Prolog Programs as Logi Formulas
%
$
Resolution
The searh tree built up by Prolog when trying to answer a query
orresponds to a logi proof using resolution, whih is a very eÆient
dedution system for Horn formulas. A short introdution an be
found in the notes; for more details refer to theoretially oriented
books on logi programming.
It is also possible to think of a Prolog goal exeution as a
goal-direted proof in rst-order logi. The data formulas in suh a
proof would represent a list of lauses and fats, and a goal formula
would orrespond to a Prolog query.
&
Ulle Endriss, King's College London
94
%
'
CS2LAP: Logi and Prolog 2000/2001
Summary: Logi Foundations
$
Prolog programs orrespond to sets of rst-order logi (Horn)
formulas.
During translation, :- beomes an impliation (from right to
left), ommas between subgoals orrespond to onjuntions, and
all variables need to be universally quantied. Queries beome
(universally quantied) impliations with ? in the onsequent.
Prolog's searh to satisfy a query orresponds to a logial proof.
In priniple, any dedution alulus ould be used. Historially,
Prolog is based on resolution, whih is partiularly suited as it is
tailored for Horn formulas.
&
Prolog Programs as Logi Formulas
Ulle Endriss, King's College London
95
%
Download