prog prng: a yourf course for nners - ulisboa · prog prng: a yourf course for nners day 1 a z...
TRANSCRIPT
Prolog programming: a do-it-yourselfcourse for beginners
Day 1
Kristina StriegnitzDepartment of Computational Linguistics
Saarland University, Saarbrucken, [email protected]
http://www.coli.uni-sb.de/˜kris
Day 1: Facts, Rules, and Queries – p.1
kb1: A knowledge base of facts
wizard(harry).wizard(ron).wizard(hermione).
muggle(uncle_vernon).muggle(aunt_petunia).
chases(crookshanks, scabbars).
Day 1: Facts, Rules, and Queries – p.7
kb1: queries we can askwizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
?- wizard(harry).
yes
?- chases(crookshanks,scabbars).
yes
?- muggle(harry).
no
?- muggle(dumbledore).
no
?- wizard(dumbledore).
no
?- witch(hermione).
ERROR: Undefined procedure: witch/1
Day 1: Facts, Rules, and Queries – p.8
kb1: more queries we can askwizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
?- muggle(X).
X = uncle vernon ;
X = aunt petunia ;
no
?- chases(X,Y).
X = crookshanks
Y = scabbars ;
no
?- chases(X,X).
no
Day 1: Facts, Rules, and Queries – p.9
A bit of syntax: atoms and variablesAtoms:
• All terms that consist of letters, numbers, and the underscore andstart with a non-capital letter are atoms: harry, uncle vernon,ritaSkeeter, nimbus2000, . . . .
• All terms that are enclosed in single quotes are atoms:’Professor Dumbledore’, ’(@ *+ ’, . . . .
• Certain special symbols are also atoms: +, ,, . . . .
Variables:
• All terms that consist of letters, numbers, and the underscore andstart with a capital letter or an underscore are variables: X,Hermione, ron, . . . .
• is an anonymous variable: two occurrences of are differentvariables. Day 1: Facts, Rules, and Queries – p.10
A bit of syntax: complex terms
Complex terms:
• Complex terms are of the form: functor (argument, ..., argument).
• Functors have to be atoms.
• Arguments can be any kind of Prolog term, e.g., complex terms.likes(ron,hermione), likes(harry,X),f(a,b,g(h(a)),c), . . . .
Day 1: Facts, Rules, and Queries – p.11
A bit of syntax: facts and queries
• Facts are complext terms which are followed by a full stop.wizard(hermione).
muggle(uncle vernon).
chases(crookshanks,scabbars).
• Queries are also complext terms which are followed by a full stop.?- wizard(hermione).
Query
Prompt provided by the Prolog interpreter.
Day 1: Facts, Rules, and Queries – p.12
kb2: a knowledge base of facts and rules
eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley), unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
Day 1: Facts, Rules, and Queries – p.13
kb2: a knowledge base of facts and rules
eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley), unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
if ... then ...: If happy(dudley) is true, then happy(aunt petunia)
is true.
Day 1: Facts, Rules, and Queries – p.14
kb2: a knowledge base of facts and rules
eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley) , unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
and: If happy(dudley) is true and unhappy(harry) is true, thenhappy(uncle vernon) is true.
Day 1: Facts, Rules, and Queries – p.15
kb2: a knowledge base of facts and rules
eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley), unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
or: If kicking(dudley,harry) is true or if eating(dudley) is true,then happy(dudley) is true.
Day 1: Facts, Rules, and Queries – p.16
Querying kb2eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley),
unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
?- happy(dudley).
yes
?- happy(aunt petunia).
yes
?- happy(uncle vernon).
no
?- happy(X).
X = aunt petunia ;
X = dudley ;
no
Day 1: Facts, Rules, and Queries – p.17
A bit of syntax: rules
• Rules are of the form Head :- Body.
• Like facts and queries, they have to be followed by a full stop.
• Head is a complex term.
• Body is complex term or a sequence of complex terms separatedby commas.happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley),
unhappy(harry).
Day 1: Facts, Rules, and Queries – p.18
kb3: facts and rules containing variables
This knowledge base defi nes 3 predi-
cates: father/2, mother/2, and
wizard/1.
father(albert,james).
father(james,harry).
mother(ruth,james).
mother(lili,harry).
wizard(lili).
wizard(ruth).
wizard(albert).
wizard(X) :- father(Y,X),
wizard(Y),
mother(Z,X),
wizard(Z).
For all X, Y, Z, if father(Y,X)
is true and wizard(Y) is true
and mother(Z,X) is true
and wizard(Z) is true, then
wizard(X) is true. I.e., for all X,
if X’s father and mother are wizards,
then X is a wizard.
Day 1: Facts, Rules, and Queries – p.19
Querying kb3 father(albert,james).
father(james,harry).
mother(ruth,james).
mother(lili,harry).
wizard(lili).
wizard(ruth).
wizard(albert).
wizard(X) :- father(Y,X),
wizard(Y),
mother(Z,X),
wizard(Z).
?- wizard(james).yes
?- wizard(harry).yes
?- wizard(X).X = lili ;X = ruth ;X = albert ;X = james ;X = harry ;no
?- wizard(X), mother(Y,X), wizard(Y).X = jamesY = ruth ;X = harryY = lili ;no
Day 1: Facts, Rules, and Queries – p.20
Prolog terms (overview)
atoms: Start with non-capital letters or are enclosed in single quotes.harry, nimbus2000, ’Professor Dumbledore’,aunt petunia
numbers 3, 6, 2957, 8.34, ...
variables Start with a capital letter or an underscore.Harry, harry
complex terms An atom (the functor) is followed by a commaseparated sequence of Prolog terms enclosed in parenthesis (thearguments).like(harry, X), np(det(the),n(potion))
Day 1: Facts, Rules, and Queries – p.21
Ancestors
Paul Helen Albert Ruth
JamesLiliPetuniaVernon
Dudley Harry
parent of(paul,petunia).
parent of(helen,petunia).
parent of(paul,lili).
parent of(helen,lili).
parent of(albert,james).
parent of(ruth,james).
parent of(petunia,dudley).
parent of(vernon,dudley).
parent of(lili,harry).
parent of(james,harry).
Task: Define a predicate ancestor of(X,Y) which is true if Xis an ancestor of Y.
Day 2: Matching and Proof Search – p.3
Ancestors (cont.)grandparent of(X,Y) :- parent of(X,Z), parent of(Z,Y).
greatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A), parent of(A,Y).
greatgreatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A),
parent of(A,B), parent of(B,Y).
! Doesn’t work for ancestor of; don’t know “how many parents wehave to go back”.
ancestor of(X,Y) :- parent of(X,Y).
People are ancestors of their children,
ancestor of(X,Y) :- parent of(X,Z), ancestor of(Z,Y).
and they are ancestors of anybody that their children may be an-cestors of (i.e., of all the descendants of their children).
Day 2: Matching and Proof Search – p.4
Ancestors (cont.)grandparent of(X,Y) :- parent of(X,Z), parent of(Z,Y).
greatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A), parent of(A,Y).
greatgreatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A),
parent of(A,B), parent of(B,Y).
! Doesn’t work for ancestor of; don’t know “how many parents wehave to go back”.
ancestor of(X,Y) :- parent of(X,Y).
People are ancestors of their children,
ancestor of(X,Y) :- parent of(X,Z), ancestor of(Z,Y).
and they are ancestors of anybody that their children may be an-cestors of (i.e., of all the descendants of their children).
recursion
Day 2: Matching and Proof Search – p.5
Example 1
KB: wizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
Query: ?- wizard(hermione).
yes
Easy: wizard(hermione) is a fact in the knowledge base.
Day 2: Matching and Proof Search – p.6
Example 2
KB: wizard(harry).wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
Query: ?- wizard(X).
X = harry ;
X = ron ;
X = hermione ;
no
• The query wizard(X) matches the fact wizard(harry). Thisinstantiates the variable X with harry.
• It also matches the facts wizard(ron) and wizard(hermione).
Day 2: Matching and Proof Search – p.7
Matching• Two atoms match if they are the same atom.Ex.: harry = harry, but harry \= ’Harry’.
• A variable matches any other Prolog term. The variable getsinstantiated with the other term.Ex.: X = wizard(harry)Ex.: X = Y
• Two complex terms match if they have the same functor and thesame number of arguments and if all pairs of parallel argumentsmatch.Ex.: like(harry,hargrid) = like(harry,X)Ex.: like(harry,hargrid) = like(harry,X,Y)Ex.: like(harry,hargrid) \= like(X,X)
Day 2: Matching and Proof Search – p.8
Back to Example 2KB: wizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
Query: ?- wizard(X).
X = harry ;
X = ron ;
X = hermione ;
no
• Prolog checks for facts that match the query. (There are three.)
• Prolog starts from the top of the knowledge base and, therefore,finds wizard(harry) first.
• Typing ; forces Prolog to check whether there are other possibilities.
Day 2: Matching and Proof Search – p.9
Example 3
KB: eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :- happy(dudley),unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).
Query: ?- happy(aunt petunia).
yes
• Check for a fact or a rule’s head that match the query.
• If you find a fact, you’re done.
• If you find a rule, prove all goals specified in the body of the rule.
Day 2: Matching and Proof Search – p.10
Example 4
KB: eating(dudley).
happy(aunt petunia):-happy(dudley).
happy(uncle vernon):-happy(dudley),unhappy(harry).
happy(dudley):-kicking(dudley,harry).
happy(dudley):-eating(dudley).
Query: ?- happy(X).
eating(h)eating(d),unhappy(h)
happy(X)
eating(d)happy(d) happy(d),unhappy(h)
kicking(d,h) kicking(d,h),unhappy(h)
unhappy(d)
kicking(d,h)
X=ap
X=d
X=ap X=uv X=d X=d
Day 2: Matching and Proof Search – p.11
Example 5
father(albert,james).
father(james,harry).
mother(ruth,james).
mother(lili,harry).
wizard(lili).
wizard(ruth).
wizard(albert).
wizard(X) :-
father(Y,X),
wizard(Y),
mother(Z,X),
wizard(Z).
wizard(I3)
wizard(albert),
mother(I3,james),
wizard(I3)
wizard(james),
mother(I3,harry),
father(I2,I1),
wizard(I2),
mother(I3,I1),
wizard(I3)
wizard(I3)
mother(I3,james),
wizard(ruth)
I4=albert
I5=ruth
wizard(X)
wizard(I3)
mother(I3,harry),
father(I4,james),
wizard(I4),
mother(I5,james),
wizard(I5)
wizard(I3)
mother(I3,harry),
wizard(lili)
I3=ruth
I3=lili
X=lili
I2=albert I2=james
X=I1
I1=james I1=harry
X=ruthX=albert
Day 2: Matching and Proof Search – p.12
Ancestors (cont.)
parent of(paul,petunia).
parent of(helen,petunia).
parent of(paul,lili).
parent of(helen,lili).
parent of(albert,james).
parent of(ruth,james).
parent of(petunia,dudley).
parent of(vernon,dudley).
parent of(lili,harry).
parent of(james,harry).
ancestor of(X,Y) :-
parent of(X,Y).
ancestor of(X,Y) :-
parent of(X,Z),
ancestor of(Z,Y).
ancestor_of(albert,harry)
parent_of(albert,I1)
ancestor_of(I1,harry)
ancestor_of(james,harry)
parent_of(james,harry)
I1=james
parent_of(albert,harry)
Day 2: Matching and Proof Search – p.13
Lists
• Intuitively: sequences or enumerations of things
• In Prolog: a special kind of data structure, i.e., special kinds ofProlog terms
Day 3: Lists – p.6
Another way of writing lists in Prolog
• .(a,Tail) = [a|Tail]
• .(a,.(b,Tail)) = [a,b|Tail]
• .(a,.(b,.(c,[]))) = [a,b,c]
Day 3: Lists – p.9
Working with lists (1)
trans a b/2: a predicate for “translating” a list of as into a list of bs.
trans a b(X,Y) should be true if X, the ‘input’, is a list of as and Y, the‘output’, is a list of bs which has just as many bs as the input has as.
trans a b([],[]).If the input is empty, then the output is empty as well.
trans a b([a|InputTail], [b|OutputTail]) :-trans a b(InputTail, OutputTail).
Otherwise the first a in the input list has to correspond to a b in theoutput list. The tail of the output list has to be the “translation” ofthe input list.
Day 3: Lists – p.11
Working with lists (2)
element of/2: a predicate for testing whether a list contains a givenProlog term.
element of(X,Y): should be true if X is an element of Y.
element of(X, [X|Tail]).
If the first element of the list is the one that we are looking for, weare done.
element of(X, [ |Tail]) :- element of(X,Tail).
Otherwise, check whether the term we are looking for is in the tailof the list.
In SWI-Prolog element of/2 is predefined under the name member.
Day 3: Lists – p.12
Working with lists (3)concatenate/3: a predicate for concatenating two lists.
concatenate(X,Y,Z) should be true if Z is the concatenation of Xand Y; for example, concatenating [a] with [b,c] yields [a,b,c].
concatenate([],L,L).Concatenating the empty list with any other list L yields L.
concatenate([Head|Tail],L,[Head|NewTail]) :-concatenate(Tail,L,NewTail).
Otherwise, the first element of the output list has to be the same asthe first element of the first input list. And the tail of the output listis the concatenation of the tail of the first input list with the secondinput list.
Day 3: Lists – p.13
Concatenating lists
concatenate(.(Head,Tail),L,.(Head,NewTail)) :-concatenate(Tail,L,NewTail).
H Tail L
Tail L
Input 1 Input 2
concatenate
H
In SWI-Prolog element of/2 is predefined under the name member.
Day 3: Lists – p.14
Prolog predicates can be used in many ways
?- trans a b([a,a,a],L).
L = [b,b,b] ;
no
?- trans a b([a,a,a],[b]).
no
?- trans a b(L,[b,b]).
L=[a,a] ;
no
?- member(a,[a,b,c]).
yes
?- member(X,[a,b,c]).
X = a ;
X = b ;
X = c ;
no
?- member(a,L).
L = [a| G280] ;
L = [ G279, a| G283] ;
L = [ G279, G282, a| G286] ;
L = [ G279, G282, G285, a| G289]
Yes
Day 3: Lists – p.15