0% found this document useful (0 votes)
24 views60 pages

LPN11

The document discusses database manipulation in Prolog, including commands for adding and removing information, as well as collecting solutions to queries using built-in predicates like findall/3, bagof/3, and setof/3. It emphasizes the importance of understanding dynamic and static predicates, and provides examples of how to use these commands effectively. Additionally, it warns about the potential complexity and non-declarative nature of database manipulation in Prolog.

Uploaded by

Rasha Orban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views60 pages

LPN11

The document discusses database manipulation in Prolog, including commands for adding and removing information, as well as collecting solutions to queries using built-in predicates like findall/3, bagof/3, and setof/3. It emphasizes the importance of understanding dynamic and static predicates, and provides examples of how to use these commands effectively. Additionally, it warns about the potential complexity and non-declarative nature of database manipulation in Prolog.

Uploaded by

Rasha Orban
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Lecture 11: Database Manipulation

and Collecting Solutions

• Exercises
– Solutions to exercises LPN chapter 10
• Theory
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– Discuss database manipulation in Prolog


– Discuss built-in predicates that collect all
solutions to a problem into a single list
Solution to Exercise 10.1
p(1). Suppose we have the given database. Write all of
p(2) :- !. Prolog's answers to the following queries.
p(3).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Solution to Exercise 10.2
class(Number,positive) :- Number > 0.
class(0,zero).
class(Number, negative) :- Number < 0.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

First, explain what the program above does.


The predicate class is true if the second argument is positive if the first is
a positive number, negative if the first is negative and zero if the first is 0.

Second, improve it by adding green cuts.


class(Number,positive) :- Number > 0, !.
class(0,zero) :- !.
class(Number, negative) :- Number < 0.
Lecture 11: Database Manipulation
and Collecting Solutions

• Theory
– Discuss database manipulation in Prolog
– Discuss built-in predicates that collect all
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

solutions to a problem into a single list


Database Manipulation

• Prolog has five basic database


manipulation commands:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– assert/1
– asserta/1
– assertz/1

– retract/1
– retractall/1
Database Manipulation

• Prolog has five basic database


manipulation commands:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– assert/1
– asserta/1 Adding information
– assertz/1

– retract/1
Removing information
– retractall/1
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Start with an empty database


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

yes
?- listing.
Start with an empty database
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

yes
Using assert/1

?- assert(happy(mia)).
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

?-
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
?-
Changing meaning of predicates

• The database manipulations have


changed the meaning of the predicate
happy/1
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• More generally:
– database manipulation commands give
us the ability to change the meaning of
predicates during runtime
Dynamic and Static Predicates

• Predicates whose meaning changing


during runtime are called dynamic
predicates
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– happy/1 is a dynamic predicate


– Some Prolog interpreters require a
declaration of dynamic predicates
• Ordinary predicates are sometimes
referred to as static predicates
Asserting rules

happy(mia). ?- assert( (naive(X):- happy(X)).


happy(vincent).
happy(marsellus).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

happy(butch).
happy(vincent).
Asserting rules

happy(mia). ?- assert( (naive(X):- happy(X)).


happy(vincent). yes
?-
happy(marsellus).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

happy(butch).
happy(vincent).

naive(A):- happy(A).
Removing information

• Now we know how to add information


to the Prolog database
– We do this with the assert/1 predicate
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

• How do we remove information?


– We do this with the retract/1 predicate,
this will remove one clause
– We can remove several clauses
simultaneously with the retractall/1
predicate
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).
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).
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).
Using retract/1

naive(A):- happy(A). ?- retract(happy(X)).


X=mia;
X=butch;
X=vincent;
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

no
?-
Using asserta/1 and assertz/1

• If we want more control over where the


asserted material is placed we can use
the variants of assert/1:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– asserta/1
places asserted matieral at the beginning
of the database
– assertz/1
places asserted material at the end of the
database
Memoisation

• Database manipulation is a useful


technique
• It is especially useful for storing the
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

results to computations, in case we


need to recalculate the same query
• This is often called memoisation
or caching
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)).
Example of memoisation

:- dynamic lookup/3. ?- addAndSquare(3,7,X).

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

:- dynamic lookup/3. ?- addAndSquare(3,7,X).


X=100
addAndSquare(X,Y,Res):- yes
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).
Example of memoisation

:- dynamic lookup/3. ?- addAndSquare(3,7,X).


X=100
addAndSquare(X,Y,Res):- yes
lookup(X,Y,Res), !. ?- addAndSquare(3,4,X).
© 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).
Example of memoisation

:- dynamic lookup/3. ?- addAndSquare(3,7,X).


X=100
addAndSquare(X,Y,Res):- yes
lookup(X,Y,Res), !. ?- addAndSquare(3,4,X).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

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).
Using retractall/1

:- dynamic lookup/3. ?- retractall(lookup(_, _, _)).

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

:- dynamic lookup/3. ?- retractall(lookup(_, _, _)).


yes
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)).
Red and Green Cuts

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)).
Red and Green Cuts

Red cut Green cuts


© Patrick Blackburn, Johan Bos & Kristina Striegnitz

:- dynamic lookup/3. :- dynamic lookup/3.

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)).
A word of warning…
• A word of warning on database manipulation:
– Often is a useful technique
– But can lead to dirty, hard to understand code
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

– It is non declarative, non logical


– So should be used cautiously
• Prolog interpreters also differ in the way
assert/1 and retract/1 are implemented with
respect to backtracking
– Either the assert or retract operation is cancelled
over backtracking, or not
Consider this database

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).
Collecting solutions

• There may be many solutions to a


Prolog query
• However, Prolog generates solutions
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

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

• Prolog has three built-in predicates that


do this: findall/3, bagof/3 and setof/3
• In essence, all these predicates collect
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

all the solutions to a query and put


them into a single list
• But there are important differences
between them
findall/3

• The query

?- findall(O,G,L).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

produces a list L of all the objects O


that satisfy the goal G
– Always succeeds
– Unifies L with empty list if G cannot be
satisfied
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).
Other findall/3 examples

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).
Other findall/3 examples

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).
Other findall/3 examples

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).
findall/3 is sometimes rather crude

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).
bagof/3

• The query

?- bagof(O,G,L).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

produces a list L of all the objects O


that satisfy the goal G
– Only succeeds if the goal G succeeds
– Binds free variables in G
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
Using bagof/3 with ^

?- 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

produces a sorted list L of all the


objects O that satisfy the goal G
– Only succeeds if the goal G succeeds
– Binds free variables in G
– Remove duplicates from L
– Sorts the answers in L
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).
?-
Solution to Exercise 11.1
q(foo,blug).
q(a,b).
q(1,2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- assert(q(a,b)), assertz(q(1,2)),
asserta(q(foo,blug)).
Solution to Exercise 11.1
q(foo,blug).
q(a,b).
q(1,2).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- assert(q(a,b)), assertz(q(1,2)),
asserta(q(foo,blug)).
?- retract(q(1,2)), assertz( (p(X) :- h(X)) ).
Solution to Exercise 11.1
q(foo,blug).
q(a,b).
p(X) :- h(X)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- assert(q(a,b)), assertz(q(1,2)),
asserta(q(foo,blug)).
?- retract(q(1,2)), assertz( (p(X) :- h(X)) ).
Solution to Exercise 11.1
q(foo,blug).
q(a,b).
p(X) :- h(X)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- assert(q(a,b)), assertz(q(1,2)),
asserta(q(foo,blug)).
?- retract(q(1,2)), assertz( (p(X) :- h(X)) ).
?- retract(q(_,_)),fail.
Solution to Exercise 11.1
Possibility 1 Possibility 2 (SWI Prolog)
q(foo,blug). p(X) :- h(X)
q(a,b).
p(X) :- h(X)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

?- assert(q(a,b)), assertz(q(1,2)),
asserta(q(foo,blug)).
?- retract(q(1,2)), assertz( (p(X) :- h(X)) ).
?- retract(q(_,_)),fail.
Solution to Exercise 11.2

?- findall(X,q(blob,X),List).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

List = [blug, blag, blig].

?- findall(X,q(X,blug),List).
List = [blob, dang].

?- findall(X,q(X,Y),List).
List = [blob, blob, blob, blaf, dang, dang, flab].
Solution to Exercise 11.2

?- bagof(X,q(X,Y),List).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Y = blag,
List = [blob, blaf] ;
Y = blig,
List = [blob] ;
Y = blob,
List = [flab] ;
Y = blug,
List = [blob, dang] ;
Y = dong,
List = [dang].
Solution to Exercise 11.2

?- setof(X,Y ^ q(X,Y),List).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

List = [blaf, blob, dang, flab].


Written Exam
Datum: Dienstag, den 6. September 2011
Zeit: 10.30-12.00
Ort: Appelstraße 4 (3703), Multimedia Hörsaal (Raum 023)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Art: Schriftlich
Hilfsmittel: Kein erlaubt
Stoff: Beide KI Vorlesung und Einfürung zu Prolog (50%-50%)

Sieh Beispiel Klausuren auf KI Webseite!

Viel Glück!

You might also like