Learn Prolog Now, lecture 1

Download Report

Transcript Learn Prolog Now, lecture 1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Lecture 1
Introduction to Prolog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Learn Prolog Now!
Important: be aware that there are different versions of the LPN
book: the online html version, the hard copy book, and online pdf.
They differ in particular wrt exercises and their numbering. When
referring to exercises from the LPN book, I refer to the online
html version as can be found on www.learnprolognow.org.
http://www.learnprolognow.org/
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
SWI Prolog
• Freely available Prolog interpreter
• Works with
– Linux,
– Windows, or
– Mac OS
• There are many more Prolog
interpreters
• Not all are ISO compliant
Aim of this lecture
LPN Ch. 1-3
• After this lecture, you should be able to
explain the meaning of the following key
notions and their relation to Prolog:
– logic programming
– facts, rules and queries (horn clauses)
– unification
– (most general) unifier (mgu)
– backtracking, depth-first search, forward &
Important:
I will not
discuss everything from the book in
backward
chaining
the lectures, but it is to be studied for the exam (to the
extent indicated on blackboard)! I strongly advise you
to read the relevant parts of the book before the
practical sessions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Introduction
Prolog
Colmerauer & Roussel, 1972
• Kowalski, Predicate Logic as Programming Language, In
Proceedings IFIP Congress, pp. 569-574. 1974.
Grace Hopper
1906-1992
• “Mother of COBOL” (1959)
• programs should be written in a language that is
close to English rather than in machine code
Prolog (2)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
removeThrees in Java
static void removeThrees
(ArrayList<Integer> list)
{
Iterator<Integer> iterator =
list.iterator();
while (iterator.hasNext())
{
if (iterator.next() == 3)
{ iterator.remove(); }
}
Procedural language: step by step instructions describing
}what should be computed
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
removeThrees in Prolog
removeThrees([],[]).
removeThrees([3|T],L) :- removeThrees(T,L).
removeThrees([H|T],[H|T2]) :- not(H = 3),
removeThrees(T,T2).
What are the differences with Java?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Prolog
• "Programming with Logic"
• Declarative: separation of knowledge
base and inference engine
• Very different from other (procedural)
programming languages
• Good for knowledge-rich tasks, logical
reasoning
Initially, small programming tasks to practice specific
skills. More elaborate programs as part of GOAL in the
2nd half of the course, and in Q4.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Why Learn Prolog?
Why Learn Prolog?
• It is the knowledge representation language of
GOAL, which will be used to program UT2004
agents in Q4
• Different programming paradigm
• It is a challenge!
• One of the most well-known AI languages
• Learn fundamental programming concepts, in
particular recursion and recursive data structures
WARNING: it seems easy at first
Prolog Development Center
• advanced scheduling systems and speech based
applications for manufacturing, retail and service
businesses, larger public institutions, airlines and
airports
http://www.pdc.dk/
Visual Prolog
• support industrial strength programming of complex
knowledge emphasized problems.
• combining the very best features of logical,
functional and object-oriented programming
paradigms in a consistent and elegant way.
http://www.visual-prolog.com/
Clarissa at ISS
• fully voice-operated procedure browser, enabling
astronauts to be more efficient with their hands and
eyes and to give full attention to the task while they
navigate through the procedure using spoken
commands
http://ti.arc.nasa.gov/project/clarissa/
OntoDLV
• OntoDLV is an intelligent ally for quickly and easily developing
powerful knowledge-based applications and decision support
systems (e.g. Planning, Customer Profiling or Workforce
Scheduling) that otherwise require complex and labourious
programming.
http://www.exeura.com/
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Facts, Rules and Queries
Alle mensen zijn sterfelijk
horn clauses
M(socrates), ∀x(M(x) → S(x))
m(socrates).
s(X) :- m(X).
•
•
•
•
fact
rule
Variables capitalized, predicates lower case
Implication ( :- ) from right to left
Each clause is terminated with a full stop.
Clauses are implicitly universally quantified.
Head and Body of Rule
m(socrates).
s(X) :- m(X).
head
•
•
•
•
fact
rule
body
Variables capitalized, predicates lower case
Implication ( :- ) from right to left
Each clause is terminated with a full stop.
Clauses are implicitly universally quantified.
Is socrates sterfelijk?
M(socrates), ∀x(M(x) → S(x)) |= S(socrates)
m(socrates).
s(X) :- m(X).
?- s(socrates).
query
• “?-” is the SWI-Prolog prompt
• asking whether a goal formula logically follows from
the knowledge base
Is socrates sterfelijk?

m(socrates).
s(X) :- m(X).
?- s(socrates).
yes
?-
• “?-” is the SWI-Prolog prompt
• asking whether a goal formula logically follows from
the knowledge base
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Prolog Variables
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Variable Instantiation
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
X=mia
returning variable instantiations
instead of “only” whether a
goal follows from the KB or
not, is what gives Prolog its
programming power!
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
X=mia;
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
X=mia;
X=jody
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
X=mia;
X=jody;
X=yolanda
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
?- woman(X).
X=mia;
X=jody;
X=yolanda;
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X,Y):- loves(X,Z), loves(Y,Z).
The comma “," expresses conjunction in Prolog
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X,Y):- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W).
how many variable
instantiations does Prolog
return for W?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X,Y):- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W).
W=vincent
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X,Y):- loves(X,Z), loves(Y,Z).
?- jealous(marsellus,W).
W=vincent;
W=marsellus;
no
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Syntax
A Few Remarks
• Be careful: terminology in Learn Prolog Now
differs (somewhat) from standard terminology in
predicate logic (in particular atom and term)!
• Prolog atoms (constants) start with lower case,
but can also be an arbitrary sequence of
characters enclosed in single quotes
• Examples:
'Vincent', 'Five dollar shake', '@$%’
• Variables start with upper case or underscore
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Arity is important
• In Prolog you can define two predicates
with the same functor but with different
arity
• Prolog would treat this as two different
predicates
• In Prolog documentation arity of a
predicate is usually indicated with the
suffix "/" followed by a number to
indicate the arity
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Arity
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
• This knowledge base defines
– happy/1
– listens2music/1
– playsAirGuitar/1
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Unification
Unification
• From Redeneren en Logica:
A subsitution that makes two literals equal is called a
unifier. Searching such a substitution is unification.
A most general unifier (mgu) is a unifier θ for which it
holds that every unifier is an “extension” of θ.
• Unification is a fundamental computation mechanism in
Prolog! The unifier resulting from unification is what
gives Prolog its programming power.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Unification
1. If T1 and T2 are constants, then
T1 and T2 unify if they are the same atom,
or the same number.
2. If T1 is a variable and T2 is any type of term,
then T1 and T2 unify, and T1 is instantiated
to T2. (and vice versa)
3. If T1 and T2 are complex terms then they
unify if:
a) They have the same functor and arity, and
b) all their corresponding arguments unify, and
c) the variable instantiations are compatible.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise: unification
• Which of the following pairs of terms
unify? Which variable instantiations
does unification give rise to?
1. ‘Bread’ = bread.
2. ‘bread’ = bread.
3. food(X) = food(bread).
4. meal(food(X),Y) = meal(food(bread),Z).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise: unification
?- ’Bread’ = bread.
no
?- ’bread’ = bread.
yes
?- food(X) = food(bread).
X = bread.
?- meal(food(X),Y) = meal(food(bread),Z).
X = bread,
Y = Z. <= most general unifier!
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Prolog unification: =/2
?- k(s(g),Y) = k(X,t(k)).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Prolog unification: =/2
?- k(s(g),Y) = k(X,t(k)).
X=s(g)
Y=t(k)
yes
?Important: The equals sign (=) is Prolog’s unification
operator. Do not confuse it with operators for
comparing integers which look or sound similar, to
be introduced later!
Unification is not Assignment
Assignment
int myInt;
myInt = 42;
variable
value
The result of assignment
is the assignment of a
value to a variable.
Unification
myInt(X) = myInt(42).
term
term
The result of unification is
an mgu or failure if no
mgu exists.
Unification is not Assignment
Assignment
int myInt;
myInt = 42;
variable
value
The result of assignment
is the assignment of a
value to a variable.
Unification
MyInt = 42.
term
term
The result of unification is
an mgu or failure if no
mgu exists.
Unification is not Assignment
Assignment
int myInt;
myInt = 3;
myInt = 42;
The result of assignment
is the assignment of a
value to a variable.
Unification
MyInt = 3,
MyInt = 42.
The result of unification is
an mgu or failure if no
mgu exists.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with
Unification
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
?- vertical(line(point(1,1),point(1,3))).
yes
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
?- vertical(line(point(1,1),point(1,3))).
yes
?- vertical(line(point(1,1),point(3,2))).
no
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
?- horizontal(line(point(1,1),point(2,Y))).
Y = 1;
Prolog unifies the query with the
no
second clause in the program and
?-
returns a unifier as the answer.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Programming with Unification
vertical( line(point(X,Y),
point(X,Z))).
horizontal( line(point(X,Y),
point(Z,Y))).
?- horizontal(line(point(2,3),Point)).
Point = point(_554,3);
no
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Proof Search Intro
Logic & Prolog
• Kowalski, Predicate Logic as Programming Language, In
Proceedings IFIP Congress, pp. 569-574. 1974.
Logic
Prolog
Sentences in predicate logic
(horn clauses)
Program
Derivation (using resolution
and unification)
Computation
Proof procedures
Executors of logic programs
Resolution & Proof Search (1)
Predicate logic:
¬B ∨ ¬C ∨ A
¬D ∨ B
¬E ∨ C
D
E
Prolog program:
a :- b, c.
b :- d.
c :- e.
d.
e.
¬A (¬ conclusion)
¬B ∨ ¬C ∨ A & ¬A = ¬B ∨ ¬C
¬B ∨ ¬C & ¬D ∨ B = ¬C ∨ ¬D
¬C ∨ ¬D & D = ¬C
¬C & ¬E ∨ C = ¬E
¬E & E = ☐
?- a. ➟ [a] (list of query goals)
[b,c]
(a :- b, c.)
[d,c]
(b :- d.)
[c]
(d.)
[e] (c :- e.)
true.
(e.)
Resolution & Proof Search (2)
Resolution
Proof search in Prolog
find two clauses that can be
resolved
find a rule with head that
unifies with current goal
calculate resolvent
update goal list by replacing
unified goal with body of rule
repeat
repeat
But: Prolog fixes order in which resolvents are computed.
This can influence the result of proof search! That is,
procedural meaning can differ from declarative (logical)
meaning !
Why learn about proof
search?
Prolog does it for me, so why should I
know how it works?
• declarative vs. procedural meaning of
programs
• writing correct programs
• debugging
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Proof Search
Key Notions
Prolog uses
• backward chaining, i.e., it only
computes if a query is made!
• linear (top-to-bottom) search for
unifying facts or heads of rules
• depth-first search strategy
• backtracking if goal fails
Important: you should be able to explain using these
notions how Prolog searches for a proof of a query!
Backward Chaining
• Prolog uses backward
chaining: only computes if a
query is made!
• Alternative is forward
chaining, i.e., take the facts
and keep on firing rules until
no rule can be fired anymore.
a :- b, c.
b :- d.
c :- e.
d.
e.
Backward chaining:
Forward chaining:
?- a.
true
b.
c.
a.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Depth-First Search
Depth-first traversal: A→B→D→E→C→F→G
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Breadth-First Search
Breadth-first traversal: A→B→C→D→E→F→G
Depth-first Search
• If [q1, …, qn] is the list of query goals, then select the head
of the list, i.e., q1 to search for a matching clause.
• If q1 :- p1, p2, …,pm is the selected unified clause, then
update list of query goals to [p1, p2, …, pm, q2, …, qn]
and find a matching clause for p1
a :- b, c.
b :- d.
c :- e.
d.
e.
a.
b.
c.
d.
e.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
most general unifier
f(a).
?- k(Y).
f(b).
Variables of rules are
Y = _G34
g(a).
renamed (_G34) at
?- f(_G34), g(_G34), h(_G34).
g(b).
each unification with
a goal query.
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
Linear search: f(_G34)
k(X):- f(X), g(X),
h(X).unified with
is first
?- k(Y).
f(a) (the top-most
clause that unifies).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
?- g(a), h(a).
Resulting unifier is applied
to remaining goals.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
?- g(a), h(a).
?- k(Y).
?- h(a).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
?- g(a), h(a).
?- k(Y).
?- h(a).
†
h(a) cannot be derived.
Prolog backtracks to
previous choice point.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
_G34 = b
?- g(a), h(a).
?- g(b), h(b).
?- k(Y).
?- h(a).
†
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
_G34 = b
?- g(a), h(a).
?- g(b), h(b).
?- h(a).
?- h(b).
?- k(Y).
†
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
_G34 = b
?- g(a), h(a).
?- g(b), h(b).
?- h(a).
?- h(b).
?- k(Y).
Y=b
†
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Linear Search & Backtracking
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
Y=b;
no using semicolon: ask
?Prolog to backtrack.
?- k(Y).
Y = _G34
?- f(_G34), g(_G34), h(_G34).
_G34 = a
_G34 = b
?- g(a), h(a).
?- g(b), h(b).
?- h(a).
?- h(b).
†
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Trace Demo
loves(vincent,mia).
loves(marsellus,mia).
jealous(A,B):loves(A,C),
loves(B,C).
?- jealous(X,Y).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise 1
f(1).
f(2).
f(3).
p(3,4).
q(4).
r(3).
r(4).
r(5).
g(X) :- p(X,Y), q(Y), r(X).
h(X) :- f(X).
h(X) :- g(X).
?- h(X).
X=1;
X=2;
X=3;
no.
Output does not
correspond to program.
What’s wrong?
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise 1
f(1).
f(2).
f(3).
p(3,4).
q(4).
r(3).
r(4).
r(5).
g(X) :- p(X,Y), q(Y), r(X).
h(X) :- f(X).
h(X) :- g(X).
?- h(X).
X=1;
X=2;
X=3;
X = 3.
Asking Prolog to
backtrack gives
another substitution
X = 3, resulting from
unification with the
second rule for h(X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise 2
f(3).
p(3,4).
q(4).
r(3).
r(4).
r(5).
Output does not
correspond to
program. What’s
wrong?
g(X) :- p(X,Y), q(Y), r(X).
h(X) :- f(X).
h(X) :- g(X).
[trace] ?- h(X).
Call: (7) h(_G335) ? creep
Call: (8) g(_G335) ? creep
Call: (9) p(_G335, _L191) ?
creep
Exit: (9) p(3, 4) ? creep
Call: (9) q(4) ? creep
Exit: (9) q(4) ? creep
Call: (9) r(3) ? creep
Exit: (9) r(3) ? creep
Exit: (8) g(3) ? creep
Exit: (7) h(3) ? creep
X=3.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercise 2
f(3).
p(3,4).
q(4).
r(3).
r(4).
r(5).
Linear search, so first
unify h(X) with the
head of the first rule
for h(X), resulting in
the goal f(_G335).
g(X) :- p(X,Y), q(Y), r(X).
h(X) :- f(X).
h(X) :- g(X).
[trace] ?- h(X).
Call: (7) h(_G335) ? creep
Call: (8) f(_G335) ? creep
Exit: (8) f(3) ? creep
Exit: (7) h(3) ? creep
X=3;
Redo: (7) h(_G335) ? creep
Call: (8) g(_G335) ? creep
Call: (9) p(_G335, _L191) ?
creep
Exit: (9) p(3, 4) ? creep
.....
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Summary of this lecture
• Introduced the following key notions and their
relation to Prolog:
–
–
–
–
–
logic programming
facts, rules and queries (horn clauses)
unification
(most general) unifier (mgu)
backtracking, depth-first search, forward &
backward chaining
Tips Practical Session
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• add comments to your code!!
• think before you ask the Prolog interpreter!
• remember to add a dot (‘.’) after each clause (fact or
rule)
• you need to create and load a knowledge base
(Prolog program) before you can query Prolog
• if you change the program, you need to reload it in
Prolog. Use the command ‘make.’ for this.
• Prolog gives warning if clauses for a single predicate
are not grouped in the program. Best practice: group
clauses defining the same predicate.
Organization
• Reading: LPN Ch. 1-2
• Tomorrow practical session
- work on LPN Sect. 1.4, LPN Sect. 1.3,
LPN Sect. 2.3, 2.4
• Next lecture
– Recursion
– Lists
– Arithmetic