LPN11
LPN11
• Theory
– Discuss database manipulation in Prolog
– Discuss built-in predicates that collect all
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• Exercises
– Exercises of LPN: 11.1, 11.2, 11.3
– Practical session
Database Manipulation
– assert/1
– asserta/1
– assertz/1
– retract/1
– retractall/1
• • 1
• •
Database Manipulation
– assert/1
– asserta/1 Adding information
– assertz/1
– retract/1
Removing information
– retractall/1
• • 2
•
•
© Patrick Blackburn, Johan Bos & Kristina Striegnitz © Patrick Blackburn, Johan Bos & Kristina Striegnitz
yes
yes
?- listing.
Using assert/1
?- assert(happy(mia)).
Start with an empty database
•
•
3
• •
Using assert/1
happy(mia). ?- assert(happy(mia)).
yes
?-
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Using assert/1
happy(mia). ?- assert(happy(mia)).
yes
?- listing.
happy(mia).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?-
• • 4
• •
Using assert/1
happy(mia). ?- assert(happy(mia)).
yes
?- listing.
happy(mia).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
?- assert(happy(vincent)),
assert(happy(marsellus)),
assert(happy(butch)),
assert(happy(vincent)).
Using assert/1
happy(mia). ?- assert(happy(mia)).
happy(vincent). yes
?- listing.
happy(marsellus).
happy(mia).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
happy(butch). ?- assert(happy(vincent)),
happy(vincent). assert(happy(marsellus)),
assert(happy(butch)),
assert(happy(vincent)).
yes
?-
• • 5
• •
• More generally:
– database manipulation commands give
us the ability to change the meaning of
predicates during runtime
• • 6
• •
Asserting rules
happy(butch).
happy(vincent).
Asserting rules
happy(butch).
happy(vincent).
naive(A):- happy(A).
• • 7
• •
Removing information
Using retract/1
happy(mia). ?- retract(happy(marsellus)).
happy(vincent).
happy(marsellus).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
happy(butch).
happy(vincent).
naive(A):- happy(A).
• • 8
• •
Using retract/1
happy(mia). ?- retract(happy(marsellus)).
happy(vincent). yes
?-
happy(butch).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
happy(vincent).
naive(A):- happy(A).
Using retract/1
happy(mia). ?- retract(happy(marsellus)).
happy(vincent). yes
?- retract(happy(vincent)).
happy(butch).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
happy(vincent).
naive(A):- happy(A).
• • 9
• •
Using retract/1
happy(mia). ?- retract(happy(marsellus)).
happy(butch). yes
?- retract(happy(vincent)).
happy(vincent).
yes
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
naive(A):- happy(A).
Using retract/1
happy(mia). ?- retract(happy(X)).
happy(butch).
happy(vincent).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
naive(A):- happy(A).
• • 10
• •
Using retract/1
no
?-
– asserta/1
places asserted matieral at the beginning
of the database
– assertz/1
places asserted material at the end of the
database
• • 11
• •
Memoisation
Example of memoisation
:- dynamic lookup/3.
addAndSquare(X,Y,Res):-
lookup(X,Y,Res), !.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
• • 12
• •
Example of memoisation
addAndSquare(X,Y,Res):-
lookup(X,Y,Res), !.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
Example of memoisation
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
lookup(3,7,100).
• • 13
• •
Example of memoisation
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
lookup(3,7,100).
Example of memoisation
X=49
addAndSquare(X,Y,Res):- yes
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
lookup(3,7,100).
lookup(3,4,49).
• • 14
• •
Using retractall/1
addAndSquare(X,Y,Res):-
lookup(X,Y,Res), !.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
lookup(3,7,100).
lookup(3,4,49).
Using retractall/1
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
• • 15
• •
Red cut
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
:- dynamic lookup/3.
addAndSquare(X,Y,Res):-
lookup(X,Y,Res), !.
addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
addAndSquare(X,Y,Res):- addAndSquare(X,Y,Res):-
lookup(X,Y,Res), !. lookup(X,Y,Res), !.
addAndSquare(X,Y,Res):- addAndSquare(X,Y,Res):-
Res is (X+Y) * (X+Y), \+ lookup(X,Y,Res), !,
assert(lookup(X,Y,Res)). Res is (X+Y) * (X+Y),
assert(lookup(X,Y,Res)).
• • 16
• •
A word of warning…
child(martha,charlotte). ?- descend(martha,X).
child(charlotte,caroline). X=charlotte;
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). X=caroline;
child(laura,rose). X=laura;
X=rose;
descend(X,Y):- child(X,Y). no
descend(X,Y):- child(X,Z),
descend(Z,Y).
• • 17
• •
Collecting solutions
one by one
• Sometimes we would like to have all
the solutions to a query in one go
• Needless to say, it would be handy to
have them in a neat, usable format
Collecting solutions
• • 18
• •
findall/3
• The query
?- findall(O,G,L).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
A findall/3 example
child(martha,charlotte). ?- findall(X,descend(martha,X),L).
child(charlotte,caroline). L=[charlotte,caroline,laura,rose]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). yes
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
• • 19
• •
child(martha,charlotte). ?- findall(f:X,descend(martha,X),L).
child(charlotte,caroline). L=[f:charlotte,f:caroline,f:laura,f:rose]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). yes
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
child(martha,charlotte). ?- findall(X,descend(rose,X),L).
child(charlotte,caroline). L=[ ]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). yes
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
• • 20
• •
child(martha,charlotte). ?- findall(d,descend(martha,X),L).
child(charlotte,caroline). L=[d,d,d,d]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). yes
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
child(martha,charlotte). ?- findall(Chi,descend(Mot,Chi),L).
child(charlotte,caroline). L=[charlotte,caroline,laura, rose,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). caroline,laura,rose,laura,rose,rose]
child(laura,rose). yes
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
• • 21
• •
bagof/3
• The query
?- bagof(O,G,L).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Using bagof/3
?- bagof(Chi,descend(Mot,Chi),L).
child(martha,charlotte). Mot=caroline
child(charlotte,caroline). L=[laura, rose];
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura).
Mot=charlotte
child(laura,rose).
L=[caroline,laura,rose];
descend(X,Y):- Mot=laura
child(X,Y). L=[rose];
descend(X,Y):- Mot=martha
child(X,Z), L=[charlotte,caroline,laura,rose];
descend(Z,Y). no
• • 22
• •
?- bagof(Chi,Mot^descend(Mot,Chi),L).
child(martha,charlotte). L=[charlotte, caroline, laura, rose,
child(charlotte,caroline). caroline,laura,rose,laura, rose, rose]
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura).
child(laura,rose).
descend(X,Y):-
child(X,Y).
descend(X,Y):-
child(X,Z),
descend(Z,Y).
setof/3
• The query
?- setof(O,G,L).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
• • 23
• •
Using setof/3
?- bagof(Chi,Mot^descend(Mot,Chi),L).
child(martha,charlotte). L=[charlotte, caroline, laura, rose,
child(charlotte,caroline). caroline, laura, rose, laura, rose,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). rose]
child(laura,rose). yes
descend(X,Y):- ?-
child(X,Y).
descend(X,Y):-
child(X,Z),
descend(Z,Y).
Using setof/3
?- bagof(Chi,Mot^descend(Mot,Chi),L).
child(martha,charlotte). L=[charlotte, caroline, laura, rose,
child(charlotte,caroline). caroline, laura, rose, laura, rose,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
child(caroline,laura). rose]
child(laura,rose). yes
descend(X,Y):- ?- setof(Chi,Mot^descend(Mot,Chi),L).
child(X,Y).
L=[caroline, charlotte, laura, rose]
descend(X,Y):-
yes
child(X,Z),
descend(Z,Y).
?-
• • 24
• •
Next lecture
• • 25