copy; Patrick Blackburn, Johan Bosamp; Kristina Striegnitz_第1页
copy; Patrick Blackburn, Johan Bosamp; Kristina Striegnitz_第2页
copy; Patrick Blackburn, Johan Bosamp; Kristina Striegnitz_第3页
copy; Patrick Blackburn, Johan Bosamp; Kristina Striegnitz_第4页
copy; Patrick Blackburn, Johan Bosamp; Kristina Striegnitz_第5页
已阅读5页,还剩55页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 Patrick Blackburn, Johan Bos & Kristina StriegnitzLecture 11: Database Manipulation and Collecting Solutions Exercises Solutions to exercises LPN chapter 10 Theory Discuss database manipulation in Prolog Discuss built-in predicates that collect all solutions to a problem into a single list Patrick

2、Blackburn, Johan Bos & Kristina StriegnitzSuppose we have the given database. Write all of Prologs answers to the following queries. p(1).p(2):-!.p(3).?- p(X).X = 1 ;X = 2.?- p(X),p(Y).X = Y, Y = 1 ;X = 1,Y = 2 ;X = 2,Y = 1 ;X = Y, Y = 2.?- p(X),!,p(Y).X = Y, Y = 1 ;X = 1,Y = 2. Patrick Blackburn, J

3、ohan Bos & Kristina StriegnitzFirst, explain what the program above does.class(Number,positive) :- Number 0.class(0,zero).class(Number, negative) :- Number 0, !.class(0,zero) :- !.class(Number, negative) :- Number 0.Second, improve it by adding green cuts. Patrick Blackburn, Johan Bos & Kristina Str

4、iegnitzLecture 11: Database Manipulation and Collecting Solutions Theory Discuss database manipulation in Prolog Discuss built-in predicates that collect all solutions to a problem into a single list Patrick Blackburn, Johan Bos & Kristina StriegnitzDatabase Manipulation Prolog has five basic databa

5、se manipulation commands: assert/1 asserta/1 assertz/1 retract/1 retractall/1 Patrick Blackburn, Johan Bos & Kristina StriegnitzDatabase Manipulation Prolog has five basic database manipulation commands: assert/1 asserta/1 assertz/1 retract/1 retractall/1Adding informationRemoving information Patric

6、k Blackburn, Johan Bos & Kristina StriegnitzStart with an empty database Patrick Blackburn, Johan Bos & Kristina StriegnitzStart with an empty database?- listing.yes Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing assert/1?- assert(happy(mia).yes Patrick Blackburn, Johan Bos & Kristina Strie

7、gnitzUsing assert/1happy(mia).?- assert(happy(mia).yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing assert/1happy(mia).?- assert(happy(mia).yes?- listing. happy(mia).?- Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing assert/1happy(mia).?- assert(happy(mia).yes?- listing. happy(m

8、ia).?- assert(happy(vincent), assert(happy(marsellus), assert(happy(butch), assert(happy(vincent). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing assert/1happy(mia).happy(vincent).happy(marsellus).happy(butch).happy(vincent).?- assert(happy(mia).yes?- listing. happy(mia).?- assert(happy(vin

9、cent), assert(happy(marsellus), assert(happy(butch), assert(happy(vincent).yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzChanging meaning of predicates The database manipulations have changed the meaning of the predicate happy/1 More generally: database manipulation commands give us the ab

10、ility to change the meaning of predicates during runtime Patrick Blackburn, Johan Bos & Kristina StriegnitzDynamic and Static Predicates Predicates whose meaning changing during runtime are called dynamic predicates happy/1 is a dynamic predicate Some Prolog interpreters require a declaration of dyn

11、amic predicates Ordinary predicates are sometimes referred to as static predicates Patrick Blackburn, Johan Bos & Kristina StriegnitzAsserting ruleshappy(mia).happy(vincent).happy(marsellus).happy(butch).happy(vincent).?- assert( (naive(X):- happy(X). Patrick Blackburn, Johan Bos & Kristina Striegni

12、tzAsserting ruleshappy(mia).happy(vincent).happy(marsellus).happy(butch).happy(vincent).naive(A):- happy(A).?- assert( (naive(X):- happy(X).yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzRemoving information Now we know how to add information to the Prolog database We do this with the asser

13、t/1 predicate 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 Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retract/1happy(mia).happy(vincent).happy(marsellus).ha

14、ppy(butch).happy(vincent).naive(A):- happy(A).?- retract(happy(marsellus). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retract/1happy(mia).happy(vincent).happy(butch).happy(vincent).naive(A):- happy(A).?- retract(happy(marsellus).yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzUs

15、ing retract/1happy(mia).happy(vincent).happy(butch).happy(vincent).naive(A):- happy(A).?- retract(happy(marsellus).yes?- retract(happy(vincent). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retract/1happy(mia).happy(butch).happy(vincent).naive(A):- happy(A).?- retract(happy(marsellus).yes

16、?- retract(happy(vincent).yes Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retract/1happy(mia).happy(butch).happy(vincent).naive(A):- happy(A).?- retract(happy(X). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retract/1naive(A):- happy(A).?- retract(happy(X).X=mia;X=butch;X=vinc

17、ent;no?- Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing 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: asserta/1places asserted matieral at the beginning of the database assertz/1places asserted material at the

18、 end of the database Patrick Blackburn, Johan Bos & Kristina StriegnitzMemoisation Database manipulation is a useful technique It is especially useful for storing the results to computations, in case we need to recalculate the same query This is often called memoisation or caching Patrick Blackburn,

19、 Johan Bos & Kristina StriegnitzExample of memoisation:- 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). Patrick Blackburn, Johan Bos & Kristina StriegnitzExample of memoisation:- dynamic lookup/3.addAndSquare(X,Y,Res)

20、:- lookup(X,Y,Res), !.addAndSquare(X,Y,Res):- Res is (X+Y) * (X+Y), assert(lookup(X,Y,Res).?- addAndSquare(3,7,X). Patrick Blackburn, Johan Bos & Kristina StriegnitzExample of memoisation:- dynamic lookup/3.addAndSquare(X,Y,Res):- lookup(X,Y,Res), !.addAndSquare(X,Y,Res):- Res is (X+Y) * (X+Y), asse

21、rt(lookup(X,Y,Res).lookup(3,7,100).?- addAndSquare(3,7,X).X=100yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzExample of memoisation:- 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).lookup(3,7,100).?- addAndSq

22、uare(3,7,X).X=100yes?- addAndSquare(3,4,X). Patrick Blackburn, Johan Bos & Kristina StriegnitzExample of memoisation:- 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).lookup(3,7,100).lookup(3,4,49).?- addAndSquare(3,7,X

23、).X=100yes?- addAndSquare(3,4,X).X=49yes Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retractall/1:- 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).lookup(3,7,100).lookup(3,4,49).?- retractall(lookup(_, _, _

24、). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing retractall/1:- 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).?- retractall(lookup(_, _, _).yes?- Patrick Blackburn, Johan Bos & Kristina StriegnitzRed and Gre

25、en Cuts:- 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 cut Patrick Blackburn, Johan Bos & Kristina StriegnitzRed and Green Cuts:- dynamic lookup/3.addAndSquare(X,Y,Res):- lookup(X,Y,Res), !.addAndSquare(X,Y,Res):

26、- Res is (X+Y) * (X+Y), assert(lookup(X,Y,Res).:- dynamic lookup/3.addAndSquare(X,Y,Res):- lookup(X,Y,Res), !.addAndSquare(X,Y,Res):- + lookup(X,Y,Res), !, Res is (X+Y) * (X+Y), assert(lookup(X,Y,Res).Red cutGreen cuts Patrick Blackburn, Johan Bos & Kristina StriegnitzA word of warning A word of war

27、ning on database manipulation: Often is a useful technique But can lead to dirty, hard to understand code 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 o

28、r retract operation is cancelled over backtracking, or not Patrick Blackburn, Johan Bos & Kristina StriegnitzConsider this databasechild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- descend(ma

29、rtha,X).X=charlotte;X=caroline;X=laura;X=rose;no Patrick Blackburn, Johan Bos & Kristina StriegnitzCollecting solutions There may be many solutions to a Prolog query However, Prolog generates solutions one by one Sometimes we would like to have all the solutions to a query in one go Needless to say,

30、 it would be handy to have them in a neat, usable format Patrick Blackburn, Johan Bos & Kristina StriegnitzCollecting solutions Prolog has three built-in predicates that do this: findall/3, bagof/3 and setof/3 In essence, all these predicates collect all the solutions to a query and put them into a

31、single list But there are important differences between them Patrick Blackburn, Johan Bos & Kristina Striegnitzfindall/3 The queryproduces 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?- findall(O,G,L). Patrick Blackburn, Joh

32、an Bos & Kristina StriegnitzA findall/3 examplechild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- findall(X,descend(martha,X),L).L=charlotte,caroline,laura,roseyes Patrick Blackburn, Johan Bos

33、 & Kristina StriegnitzOther findall/3 exampleschild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- findall(f:X,descend(martha,X),L).L=f:charlotte,f:caroline,f:laura,f:roseyes Patrick Blackburn,

34、Johan Bos & Kristina StriegnitzOther findall/3 exampleschild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- findall(X,descend(rose,X),L).L= yes Patrick Blackburn, Johan Bos & Kristina Striegnitz

35、Other findall/3 exampleschild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- findall(d,descend(martha,X),L).L=d,d,d,dyes Patrick Blackburn, Johan Bos & Kristina Striegnitzfindall/3 is sometimes

36、rather crudechild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- findall(Chi,descend(Mot,Chi),L).L=charlotte,caroline,laura, rose, caroline,laura,rose,laura,rose,roseyes Patrick Blackburn, Johan

37、 Bos & Kristina Striegnitzbagof/3 The queryproduces 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?- bagof(O,G,L). Patrick Blackburn, Johan Bos & Kristina StriegnitzUsing bagof/3child(martha,charlotte).child(charlotte,caroline).ch

38、ild(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).?- bagof(Chi,descend(Mot,Chi),L).Mot=carolineL=laura, rose;Mot=charlotteL=caroline,laura,rose;Mot=lauraL=rose;Mot=marthaL=charlotte,caroline,laura,rose;no Patrick Blackburn, Johan Bos & Kristina S

39、triegnitzUsing bagof/3 with ?- bagof(Chi,Motdescend(Mot,Chi),L). L=charlotte, caroline, laura, rose, caroline,laura,rose,laura, rose, rosechild(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y). Patri

40、ck Blackburn, Johan Bos & Kristina Striegnitzsetof/3 The queryproduces 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?- setof(O,G,L). Patrick Blackburn, Johan Bos & Kristina S

41、triegnitzUsing setof/3?- bagof(Chi,Motdescend(Mot,Chi),L). L=charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose, roseyes?-child(martha,charlotte).child(charlotte,caroline).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y). Pat

42、rick Blackburn, Johan Bos & Kristina StriegnitzUsing setof/3?- bagof(Chi,Motdescend(Mot,Chi),L). L=charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose, roseyes?- setof(Chi,Motdescend(Mot,Chi),L). L=caroline, charlotte, laura, roseyes?-child(martha,charlotte).child(charlotte,caroline

43、).child(caroline,laura).child(laura,rose).descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y). Patrick Blackburn, Johan Bos & Kristina Striegnitzq(foo,blug).q(a,b).q(1,2).?- assert(q(a,b), assertz(q(1,2),asserta(q(foo,blug). Patrick Blackburn, Johan Bos & Kristina Striegnitzq(foo,blug)

44、.q(a,b).q(1,2).?- assert(q(a,b), assertz(q(1,2),asserta(q(foo,blug).?- retract(q(1,2),assertz(p(X):-h(X). Patrick Blackburn, Johan Bos & Kristina StriegnitzSolution to q(foo,blug).q(a,b).p(X):- h(X)?- assert(q(a,b), assertz(q(1,2),asserta(q(foo,blug).?- retract(q(1,2),assertz(p(X):-h(X). Patrick Blackburn, Johan Bos & Kristina StriegnitzSolution to q(foo,blug).q(a,b).p(X):- h(X)?-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论