© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Exercises –Solutions to exercises LPN chapter.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Logic Programming Lecture 9: Constraint logic programming.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 12: Working with Files This lecture is concerned with various aspects of file handling and.
Prolog Nonmonotonic logic.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
”assert” and ”retract”
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/25.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
LING 388: Language and Computers Sandiway Fong Lecture 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Lecture 7: Definite Clause Grammars
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 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 Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 10.1 Suppose we have the given database. Write all of Prolog's 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, Johan Bos & Kristina Striegnitz Solution to Exercise 10.2 First, explain what the program above does. class(Number,positive) :- Number > 0. class(0,zero). class(Number, negative) :- Number < 0. 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. class(Number,positive) :- Number > 0, !. class(0,zero) :- !. class(Number, negative) :- Number < 0. Second, improve it by adding green cuts.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 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 Striegnitz Database Manipulation Prolog has five basic database manipulation commands: –assert/1 –asserta/1 –assertz/1 –retract/1 –retractall/1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Database Manipulation Prolog has five basic database manipulation commands: –assert/1 –asserta/1 –assertz/1 –retract/1 –retractall/1 Adding information Removing information

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Start with an empty database

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

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using assert/1 ?- assert(happy(mia)). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). ?- assert(happy(vincent)), assert(happy(marsellus)), assert(happy(butch)), assert(happy(vincent)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using assert/1 happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). ?- assert(happy(mia)). yes ?- listing. happy(mia). ?- assert(happy(vincent)), assert(happy(marsellus)), assert(happy(butch)), assert(happy(vincent)). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Changing meaning of predicates The database manipulations have changed the meaning of the predicate happy/1 More generally: –database manipulation commands give us the ability to change the meaning of predicates during runtime

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Dynamic 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 dynamic predicates Ordinary predicates are sometimes referred to as static predicates

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asserting rules happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). ?- assert( (naive(X):- happy(X)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Asserting rules happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A). ?- assert( (naive(X):- happy(X)). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Removing information Now we know how to add information to the Prolog database –We do this with the assert/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 Striegnitz Using retract/1 happy(mia). happy(vincent). happy(marsellus). happy(butch). happy(vincent). naive(A):- happy(A). ?- retract(happy(marsellus)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using retract/1 happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A). ?- retract(happy(marsellus)). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using retract/1 happy(mia). happy(vincent). happy(butch). happy(vincent). naive(A):- happy(A). ?- retract(happy(marsellus)). yes ?- retract(happy(vincent)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using retract/1 happy(mia). happy(butch). happy(vincent). naive(A):- happy(A). ?- retract(happy(marsellus)). yes ?- retract(happy(vincent)). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using retract/1 happy(mia). happy(butch). happy(vincent). naive(A):- happy(A). ?- retract(happy(X)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using retract/1 naive(A):- happy(A). ?- retract(happy(X)). X=mia; X=butch; X=vincent; no ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz 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: –asserta/1 places asserted matieral at the beginning of the database –assertz/1 places asserted material at the end of the database

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Memoisation 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, Johan Bos & Kristina Striegnitz Example 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 Striegnitz Example 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)). ?- addAndSquare(3,7,X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 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). ?- addAndSquare(3,7,X). X=100 yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 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). ?- addAndSquare(3,7,X). X=100 yes ?- addAndSquare(3,4,X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example 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). X=100 yes ?- addAndSquare(3,4,X). X=49 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using 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(_, _, _)).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using 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 Striegnitz Red and Green 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 Striegnitz Red and Green 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)). :- 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 Striegnitz 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 –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

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Consider this database 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). ?- descend(martha,X). X=charlotte; X=caroline; X=laura; X=rose; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Collecting 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, it would be handy to have them in a neat, usable format

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Collecting 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 single list But there are important differences between them

© Patrick Blackburn, Johan Bos & Kristina Striegnitz findall/3 The query 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 ?- findall(O,G,L).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz A findall/3 example 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). ?- findall(X,descend(martha,X),L). L=[charlotte,caroline,laura,rose] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Other findall/3 examples 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). ?- findall(f:X,descend(martha,X),L). L=[f:charlotte,f:caroline,f:laura,f:rose] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Other findall/3 examples 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). ?- findall(X,descend(rose,X),L). L=[ ] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Other findall/3 examples 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). ?- findall(d,descend(martha,X),L). L=[d,d,d,d] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz findall/3 is sometimes rather crude 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). ?- findall(Chi,descend(Mot,Chi),L). L=[charlotte,caroline,laura, rose, caroline,laura,rose,laura,rose,rose] yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz bagof/3 The query 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 ?- bagof(O,G,L).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using bagof/3 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). ?- bagof(Chi,descend(Mot,Chi),L). Mot=caroline L=[laura, rose]; Mot=charlotte L=[caroline,laura,rose]; Mot=laura L=[rose]; Mot=martha L=[charlotte,caroline,laura,rose]; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using bagof/3 with ^ ?- bagof(Chi,Mot^descend(Mot,Chi),L). L=[charlotte, caroline, laura, rose, caroline,laura,rose,laura, rose, rose] 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).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz setof/3 The query 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 ?- setof(O,G,L).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using setof/3 ?- bagof(Chi,Mot^descend(Mot,Chi),L). L=[charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose, rose] yes ?- 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).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using setof/3 ?- bagof(Chi,Mot^descend(Mot,Chi),L). L=[charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose, rose] yes ?- setof(Chi,Mot^descend(Mot,Chi),L). L=[caroline, charlotte, laura, rose] yes ?- 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).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 11.1 q(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 Striegnitz Solution to Exercise 11.1 q(foo,blug). 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 Striegnitz Solution to Exercise 11.1 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 Striegnitz Solution to Exercise 11.1 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)) ). ?- retract(q(_,_)),fail.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 11.1 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)) ). ?- retract(q(_,_)),fail. p(X) :- h(X) Possibility 1Possibility 2 (SWI Prolog)

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 11.2 q(blob,blug). q(blob,blag). q(blob,blig). q(blaf,blag). q(dang,dong). q(dang,blug). q(flab,blob). ?- findall(X,q(blob,X),List). 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].

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 11.2 q(blob,blug). q(blob,blag). q(blob,blig). q(blaf,blag). q(dang,dong). q(dang,blug). q(flab,blob). ?- bagof(X,q(X,Y),List). Y = blag, List = [blob, blaf] ; Y = blig, List = [blob] ; Y = blob, List = [flab] ; Y = blug, List = [blob, dang] ; Y = dong, List = [dang].

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Solution to Exercise 11.2 q(blob,blug). q(blob,blag). q(blob,blig). q(blaf,blag). q(dang,dong). q(dang,blug). q(flab,blob). ?- setof(X,Y ^ q(X,Y),List). List = [blaf, blob, dang, flab].

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Written Exam Datum: Dienstag, den 6. September 2011 Zeit: Ort: Appelstraße 4 (3703), Multimedia Hörsaal (Raum 023) 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!