Top Banner
Database Manipulation Database Manipulation Section 7.4 Section 7.4
44

Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Database ManipulationDatabase Manipulation

Section 7.4Section 7.4

Page 2: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Prolog Knowledge BaseProlog Knowledge Base

Knowledge base = databaseKnowledge base = database– set of facts/rules in the programset of facts/rules in the program

Can add/subtract facts and rules at run timeCan add/subtract facts and rules at run time Adding facts/rulesAdding facts/rules

– assert, asserta, assertzassert, asserta, assertz Subtracting facts/rulesSubtracting facts/rules

– retract, retractallretract, retractall

Page 3: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Asserting a FactAsserting a Fact

Just tell prolog that it’s soJust tell prolog that it’s so?- ?- raining.raining.ERROR: Undefined procedure: raining/0ERROR: Undefined procedure: raining/0?- ?- assert(raining).assert(raining).YesYes?- ?- raining.raining.YesYes Prolog didn’t know about raining/0Prolog didn’t know about raining/0

Page 4: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Preparing Prolog for a PredicatePreparing Prolog for a Predicate

Need to tell Prolog that the predicate existsNeed to tell Prolog that the predicate exists– helpful also to say that it may have rules addedhelpful also to say that it may have rules added

Give a Give a directivedirective in the program file in the program file– a command to Prologa command to Prolog

:- dynamic raining/0.:- dynamic raining/0. Says that raining/0 is a predicateSays that raining/0 is a predicate

– prevents the error messageprevents the error message

Page 5: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Dynamic PredicatesDynamic Predicates

Can be done during sessionCan be done during session?- ?- dynamic foggy/0.dynamic foggy/0.YesYes?- ?- foggy.foggy.NoNo Note how we call goals in a fileNote how we call goals in a file

:- dynamic foggy/0.:- dynamic foggy/0.– can do that with any querycan do that with any query

Page 6: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Asserting RulesAsserting Rules

Just give rule instead of factJust give rule instead of fact– use parentheses if rule is compound (to prevent use parentheses if rule is compound (to prevent

confusion about the comma)confusion about the comma)

?- ?- assert(raining).assert(raining).

?- ?- assert(bad :- raining ; foggy).assert(bad :- raining ; foggy).

?- ?- assert(nice :- (sunshine, \+ cold)).assert(nice :- (sunshine, \+ cold)).

?- ?- bad.bad.

YesYes

Page 7: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Assertion OrderAssertion Order

assert/1 puts the fact/rule in the databaseassert/1 puts the fact/rule in the database– order is implementation dependentorder is implementation dependent– (SWI-Prolog puts it at the end)(SWI-Prolog puts it at the end)

asserta/1 puts fact/rule in frontasserta/1 puts fact/rule in front assertz/1 puts fact/rule at endassertz/1 puts fact/rule at end

Page 8: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Assertion OrderAssertion Order

?- ?- assert(num(1)).assert(num(1)).?- ?- assertz(num(2)).assertz(num(2)).?- ?- asserta(num(0)).asserta(num(0)).?- ?- assertz(num(3)).assertz(num(3)).?- ?- num(X).num(X).X = 0 X = 0 ;;X = 1 X = 1 ;;X = 2 X = 2 ;;X = 3X = 3

Adds num(1) to database

Adds num(2) to end of DB

Adds num(0) to front of DB

Adds num(3) to end of DB

num(1).num(2).

num(0).

num(3).

Page 9: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Assertion Order ExerciseAssertion Order Exercise

?- ?- asserta(what(1)).asserta(what(1)).?- ?- assertz(what(2)).assertz(what(2)).?- ?- asserta(what(3)).asserta(what(3)).?- ?- assertz(what(4)).assertz(what(4)).?- ?- asserta(what(5)).asserta(what(5)).

?- ?- what(X).what(X).X = ? X = ? ;;X = ? X = ? ;;X = ? X = ? ;;

Page 10: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

ExerciseExercise

Write a predicate that asks the user for a Write a predicate that asks the user for a person’s parents & asserts those factsperson’s parents & asserts those facts?- ?- add_parents(mark).add_parents(mark).Who is mark’s father? Who is mark’s father? bob.bob.Who is mark’s mother? Who is mark’s mother? isabel.isabel.YesYes?- ?- father(mark, Dad), mother(mark, Mom).father(mark, Dad), mother(mark, Mom).Dad = bob, Mom = isabelDad = bob, Mom = isabel

Page 11: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

SolutionSolution

ask_parents(Person) :-ask_parents(Person) :-ask_for_who(Person, father, Dad),ask_for_who(Person, father, Dad),ask_for_who(Person, mother, Mom),ask_for_who(Person, mother, Mom),assert(father(Person, Dad)),assert(father(Person, Dad)),assert(mother(Person, Mom)).assert(mother(Person, Mom)).

ask_for_who(Person, Role, Name) :-ask_for_who(Person, Role, Name) :-write(‘Who is ’), write(Person), write(‘\’s ’),write(‘Who is ’), write(Person), write(‘\’s ’),write(Role), write(‘? ’), read(Name).write(Role), write(‘? ’), read(Name).

Page 12: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

RetractionRetraction

Tell Prolog to remove a fact/ruleTell Prolog to remove a fact/rule?- ?- raining.raining.YesYes?- ?- retract(raining).retract(raining).YesYes?- ?- raining.raining.NoNo

Page 13: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Retracting RulesRetracting Rules

As for asserting rulesAs for asserting rules– use parentheses if body is compounduse parentheses if body is compound– body may be a variable/partly instantiatedbody may be a variable/partly instantiated

?- ?- retract(bad :- raining ; foggy).retract(bad :- raining ; foggy).

?- ?- retract(nice :- Body).retract(nice :- Body).

Body = sunshine, \+ rainingBody = sunshine, \+ raining

YesYes

Page 14: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Retraction OrderRetraction Order

From first to lastFrom first to last?- ?- retract(num(N)), retract(num(M)).retract(num(N)), retract(num(M)).

N = 0N = 0

M = 1M = 1

YesYes retract fails if no clause matchesretract fails if no clause matches

Page 15: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Retracting All ClausesRetracting All Clauses

rectractall/1 retracts multiple clausesrectractall/1 retracts multiple clauses– all clauses with all clauses with headhead matching the argument matching the argument

?- ?- num(N).num(N).N = 0N = 0?- ?- retractall(num(N)).retractall(num(N)).YesYes?- ?- num(N).num(N).NoNo

Note: also gets rules?- assert(bad :- member(1,[1])).Yes?- bad.Yes?- retractall(bad).Yes?- bad.No

Page 16: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Asserting and RetractingAsserting and Retracting

Used for AI programs that learnUsed for AI programs that learn– create a new rule & add it to the databasecreate a new rule & add it to the database– forget an old ruleforget an old rule

Can also be used for efficiencyCan also be used for efficiency– asserta solutions previously foundasserta solutions previously found– found before general code calledfound before general code called

Page 17: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Naïve FibonacciNaïve Fibonacci

fib(1, 1).fib(1, 1).fib(2, 1).fib(2, 1).fib(N, F) :-fib(N, F) :-

N > 2,N > 2,N1 is N – 1, fib(N1, F1),N1 is N – 1, fib(N1, F1),N2 is N – 2, fib(N2, F2),N2 is N – 2, fib(N2, F2),F is F1 + F2.F is F1 + F2.

Page 18: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Trace fib(5,F)Trace fib(5,F)

fib(5, Ffib(5, F00))

fib(4, Ffib(4, F11))

fib(3, Ffib(3, F22))

fib(2, Ffib(2, F33) ) F F33 = 1 = 1

fib(1, Ffib(1, F44) ) F F44 = 1 = 1

fib(2, Ffib(2, F55) ) F F55 = 1 = 1

fib(3, Ffib(3, F22))

fib(2, Ffib(2, F33) ) F F33 = 1 = 1

fib(1, Ffib(1, F44) ) F F44 = 1 = 1

fib(3, F) gets calculated againextra work donemuch worse as #s get bigger

Page 19: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Assertional Fibonacci (8.5.4)Assertional Fibonacci (8.5.4)

fib2(1, 1).fib2(1, 1).fib2(2, 1).fib2(2, 1).fib2(N, F) :-fib2(N, F) :-

N > 2,N > 2,N1 is N – 1, fib2(N1, F1),N1 is N – 1, fib2(N1, F1),N2 is N – 2, fib2(N2, F2),N2 is N – 2, fib2(N2, F2),F is F1 + F2,F is F1 + F2,asserta( fib2(N, F) ).asserta( fib2(N, F) ). % remember the result% remember the result

% % at the beginningat the beginning

Page 20: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Trace fib2(5,F)Trace fib2(5,F)

fib2(5, Ffib2(5, F00))fib2(4, Ffib2(4, F11))

fib2(3, Ffib2(3, F22))fib2(2, Ffib2(2, F33) ) F F33 = 1 = 1fib2(1, Ffib2(1, F44) ) F F44 = 1 = 1asserta( fib2(3, 2) )asserta( fib2(3, 2) )

fib2(2, Ffib2(2, F55) ) F F55 = 1 = 1asserta( fib2(4, 3) )asserta( fib2(4, 3) )

fib2(3, Ffib2(3, F66) ) F F66 = 2 = 2asserta( fib2(5, 5) )asserta( fib2(5, 5) )

Saves work from calculating fib(3)

Matches asserted fact – no need to recalculate

Page 21: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Problems for Asserting SolutionsProblems for Asserting Solutions

Backtracking gives multiple solutionsBacktracking gives multiple solutions– saved solution found firstsaved solution found first– then solution re-calculatedthen solution re-calculated– and and re-asserted: solution there twice, nowre-asserted: solution there twice, now– next time there’ll be three solutionsnext time there’ll be three solutions

Adding a cut to the solution might helpAdding a cut to the solution might help– asserta( fib2(F, N) :- ! ).asserta( fib2(F, N) :- ! ).

Page 22: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Generating FactsGenerating Facts

Multiplication tableMultiplication tablemake_table :-make_table :-

\+ (\+ ( L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],member(X, L),member(X, L),member(Y, L),member(Y, L),Z is X * Y,Z is X * Y,assert(product(X, Y, Z)),assert(product(X, Y, Z)),failfail ).).

Page 23: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Using the Multiplication TableUsing the Multiplication Table

Now have 100 multiplication factsNow have 100 multiplication facts?- ?- product(X, Y, 12).product(X, Y, 12).X = 2, Y = 6 X = 2, Y = 6 ;;X = 3, Y = 4 X = 3, Y = 4 ;;X = 4, Y = 3 X = 4, Y = 3 ;;X = 6, Y = 2 X = 6, Y = 2 ;;NoNo

Page 24: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Asserts on Calls in ProgressAsserts on Calls in Progress

Suppose assert or assertz a clause of a Suppose assert or assertz a clause of a predicate that’s in progresspredicate that’s in progress– is that clause considered for that call?is that clause considered for that call?

strange(N) :-strange(N) :-assertz(strange(N)),assertz(strange(N)),fail.fail.

Does strange(3) succeed or fail?Does strange(3) succeed or fail?– or throw an exceptionor throw an exception

Page 25: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Speed of Dynamic PredicatesSpeed of Dynamic Predicates

Dynamic predicates may be slower than Dynamic predicates may be slower than static (dynamic = can change, static = can’t)static (dynamic = can change, static = can’t)

Prolog may have a better way for Prolog may have a better way for remembering solutionsremembering solutions– rememberremember or or recordrecord predicates to memorize predicates to memorize– recallrecall or or recordedrecorded to recall to recall– forgetforget or or eraseerase to forget to forget

Page 26: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Remembering SolutionsRemembering Solutions

One clause (near front) to look for One clause (near front) to look for remembered solutionsremembered solutions– cut if find onecut if find one

Remember solution after it’s calculatedRemember solution after it’s calculated

Page 27: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Remembering FibonacciRemembering Fibonacci

fib_mem(0, 1).fib_mem(0, 1).fib_mem(1, 1).fib_mem(1, 1).fib_mem(N, F) :- fib_mem(N, F) :- recorded(fibonacci, fib(N, F))recorded(fibonacci, fib(N, F)), !., !.fib_mem(N, F) :-fib_mem(N, F) :-

N > 1,N > 1,succ(N1, N),succ(N1, N),fib_mem(N1, F1),fib_mem(N1, F1),

succ(N2, N1),succ(N2, N1), fib_mem(N2, F2),fib_mem(N2, F2), F is F1 + F2,F is F1 + F2, recorda(fibonacci, fib(N, F))recorda(fibonacci, fib(N, F))..

Page 28: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

RecordingRecording

recorda/2, recordz/2 store 2recorda/2, recordz/2 store 2ndnd argument argument– use 1use 1stst argument as a key argument as a key– you can record same fact under multiple keysyou can record same fact under multiple keys– key is integer or atom (or term – but no good)key is integer or atom (or term – but no good)

recorded/2 retrieves factrecorded/2 retrieves fact– needs key – only retrieves facts under that keyneeds key – only retrieves facts under that key– matches fact – only those that match selectedmatches fact – only those that match selected

Page 29: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Erasing/ForgettingErasing/Forgetting

recorded/3 also gives a memory locationrecorded/3 also gives a memory location Can use the location to erase the recordCan use the location to erase the record?- ?- recorded(fibonacci, fib(500,_), Mem),recorded(fibonacci, fib(500,_), Mem),

erase(Mem).erase(Mem).

YesYes Can only erase one item at a timeCan only erase one item at a time

– don’t erase anything more than oncedon’t erase anything more than once

Page 30: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Versions of FibonacciVersions of Fibonacci

Vanilla version is very slowVanilla version is very slow– and only works on a few numbersand only works on a few numbers

Remembering version quite fastRemembering version quite fast– even faster when asking a second timeeven faster when asking a second time

Single recursion version very fastSingle recursion version very fast– but second time takes as long as firstbut second time takes as long as first?- time(fib(20, F)), time(fib2(20, F)), ?- time(fib(20, F)), time(fib2(20, F)),

time(fib_mem(20, F)).time(fib_mem(20, F)).

Page 31: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Looking at CodeLooking at Code

Program can inspect its own codeProgram can inspect its own code clause/2 returns clauses of programclause/2 returns clauses of program

– 11stst argument is the head of the rule argument is the head of the rule– 22ndnd argument is the body argument is the body– at least one argument must be givenat least one argument must be given

Page 32: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Clause/2Clause/2

sibs(Sib1, Sib2) :-sibs(Sib1, Sib2) :- parent(Sib1, P), parent(Sib2, P),parent(Sib1, P), parent(Sib2, P),different(Sib1, Sib2).different(Sib1, Sib2).

different(X, Y) :- X \= Y.different(X, Y) :- X \= Y.

?- ?- clause(sibs(_, _), Body).clause(sibs(_, _), Body).Body = parent(_1,_2), parent(_3,_2), different(_1,_3)Body = parent(_1,_2), parent(_3,_2), different(_1,_3)?- ?- clause(Head, _A \= _B).clause(Head, _A \= _B).Head = different(_1,_2)Head = different(_1,_2) Note: this last one doesn’t

seem to work in SWI-Prolog

Page 33: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Compound BodiesCompound Bodies

Body is a term – a comma termBody is a term – a comma term?- ?- (a, b, c, d) = (X, Y).(a, b, c, d) = (X, Y).X = aX = aY = b, c, dY = b, c, d Needs to be in parentheses when 2Needs to be in parentheses when 2ndnd arg. arg.?- ?- clause(sibs(_,_), (parent(_,_), _) ).clause(sibs(_,_), (parent(_,_), _) ).YesYes

Is there a clause of sibs/2 with a body like parent(_,_),…

Page 34: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Bodies of FactsBodies of Facts

Puts true/0 in for body of factPuts true/0 in for body of factparent(mark, alex).parent(mark, alex).parent(bob, mark).parent(bob, mark).parent(X, brian) :- parent(X, mark).parent(X, brian) :- parent(X, mark).

?- ?- clause(parent(X, Y), Body).clause(parent(X, Y), Body).X = mark, Y = alex, Body = true X = mark, Y = alex, Body = true ;;X = bob, Y = mark, Body = true X = bob, Y = mark, Body = true ;;X = _1, Y = brian, Body = parent(_1,mark)X = _1, Y = brian, Body = parent(_1,mark)

Page 35: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Looking at Built-in CodeLooking at Built-in Code

Some built-in predicates are availableSome built-in predicates are available?- ?- clause(member(X,Y), Body).clause(member(X,Y), Body).X = _1, Y = [_1|_2], Body = true ;X = _1, Y = [_1|_2], Body = true ;X = _1, Y = [_2|_3], Body = member(_1, _3)X = _1, Y = [_2|_3], Body = member(_1, _3) Others are hiddenOthers are hidden?- ?- clause(clause(A,B), Body).clause(clause(A,B), Body).ERROR: No permission to access private_procedure ERROR: No permission to access private_procedure

`clause/2'`clause/2'

Page 36: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Modifying CodeModifying Code

Replace every rule of the formReplace every rule of the form– H :- …, different(X,Y), …H :- …, different(X,Y), …

withwith– H :- …, X \= Y, …H :- …, X \= Y, …

(where the other bits don’t change)(where the other bits don’t change) Maybe for efficiencyMaybe for efficiency

– H should be specified (else errors ensue)H should be specified (else errors ensue)

Page 37: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Method for Modifying CodeMethod for Modifying Code

Find a rule with matching headFind a rule with matching head– Note: predicate must be Note: predicate must be dynamicdynamic

If it has a different(X,Y) in it,If it has a different(X,Y) in it,– replace it with X \= Yreplace it with X \= Y– retract old ruleretract old rule– assert new ruleassert new rule

Page 38: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Modification PredicateModification Predicate

modify_rule(Head, Old, New) :-modify_rule(Head, Old, New) :-clause(Head, Body),clause(Head, Body),replace_code(Body, Old, New, NewBody),replace_code(Body, Old, New, NewBody),retract(Head :- Body),retract(Head :- Body),assert(Head :- NewBody).assert(Head :- NewBody).

replace_code((Old,More), Old, New, (New,More)).replace_code((Old,More), Old, New, (New,More)).replace_code((H,OldT), Old, New, (H,NewT)) :-replace_code((H,OldT), Old, New, (H,NewT)) :-

replace_code(OldT, Old, New, NewT).replace_code(OldT, Old, New, NewT).replace_code(Old, Old, New, New).replace_code(Old, Old, New, New).

Page 39: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Modifying CodeModifying Code

?- ?- modify_rule(sib(_,_), different(X,Y), X\=Y)modify_rule(sib(_,_), different(X,Y), X\=Y)..

before:before:sib(A, B) :-sib(A, B) :-

parent(A,C),parent(A,C),parent(B,C),parent(B,C),different(A,B).different(A,B).

Problem – only modifies one clause!Problem – only modifies one clause!

after:after:sib(A, B) :-sib(A, B) :-

parent(A,C),parent(A,C),parent(B,C),parent(B,C),A \= B.A \= B.

Page 40: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Modifying All ClausesModifying All Clauses

Prolog prints bindingsProlog prints bindings– ask for ask for anotheranother solution solution– Prolog backs up to clause/2 & gets another ruleProlog backs up to clause/2 & gets another rule– then changes that rulethen changes that rule

Keep asking for answers until all doneKeep asking for answers until all done– it works – will stop when no rule has Old in itit works – will stop when no rule has Old in it– it’s sort of like a loopit’s sort of like a loop

Page 41: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

““Failure Driven Loops”Failure Driven Loops”

Need to make a change, then failNeed to make a change, then fail– Prolog will try another clauseProlog will try another clause

Have predicate make a change, then failHave predicate make a change, then fail– it will change all the rules before saying Noit will change all the rules before saying No

Says No even tho’ it worked!Says No even tho’ it worked!– if we put \+ in front, it’ll say Yes instead!if we put \+ in front, it’ll say Yes instead!– that’s more intuitive for userthat’s more intuitive for user

Page 42: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Modify AllModify All

modify_all(Head, Old, New) :-modify_all(Head, Old, New) :-\+ modify_and_fail(Head, Old, New).\+ modify_and_fail(Head, Old, New).

modify_and_fail(Head, Old, New) :-modify_and_fail(Head, Old, New) :-modify_rule(Head, Old, New),modify_rule(Head, Old, New),fail.fail.

OR JustOR Justmodify_all(Head, Old, New) :-modify_all(Head, Old, New) :-

\+ (modify_rule(Head, Old, New), fail).\+ (modify_rule(Head, Old, New), fail).

Page 43: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Remaining ProblemRemaining Problem

too_different/3 is not fully changedtoo_different/3 is not fully changed– replace_code only changes first Old to Newreplace_code only changes first Old to New– backtracking doesn’t pick up new clausebacktracking doesn’t pick up new clause

Try to fix by making replace_code change Try to fix by making replace_code change allall– unexpected side-effect: all X\=Y made the sameunexpected side-effect: all X\=Y made the same– too_different(A,A,A) :- A\=A, A\=A, A\=A.too_different(A,A,A) :- A\=A, A\=A, A\=A.

Too complicated for us!Too complicated for us!

Page 44: Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.

Next TimeNext Time

Definite Clause GrammarsDefinite Clause Grammars