Presentation is loading. Please wait.

Presentation is loading. Please wait.

”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.

Similar presentations


Presentation on theme: "”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree."— Presentation transcript:

1 ”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree

2 Built-ins to Modify Prolog Programs The following built-ins allow modification of the loaded program as it is actually running. assert(C) - adds a clause C to program asserta(C) - C is added to front assertz(C) = assert(C) - C is added to end They always succeed. retract(C) - deletes a clause that matches C It fails if there is nothing to delete

3 Examples of using assert/retract ?-nice. ?-disgusting. ?-retract(fog). ?-disgusting. ?-assert(sunshine). ?-funny. ?-retract(raining). ?-nice. nice:- sunshine, \+ raining. % “\+ “ means not funny:- sunshine, raining. disgusting:- raining, fog. raining. fog.

4 Examples of using assert/retract Fibonacci numbers: 1,1,2,3,5,8,13,21,34,… fib(N,F) - F is the N'th Fibonacci number. fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1 + F2.

5 Execution Tree to Compute fib(6,F) fib(6,F) fib(5, F1)fib(4,F2) F is F1+F2

6 To improve the above program, we can use “assert” to save intermediate results :- dynamic myfib/2. myfib(1,1). myfib(2,1). myfib(N,F) :- N > 2, N1 is N-1, N2 is N-2, myfib(N1,F1), myfib(N2,F2), F is F1 + F2, % remember n-th Fibonacci number asserta( myfib(N,F) ).

7 assert/retract plays two roles Adding new information to the database (ref. previous examples) Communicating between or-branches An example: ?- b(X),b(Y). b(0). b(1).

8 the following program collects a list of boy names make_name_list(NameList):- % initialise the name list as an empty list assert(tmp([])), boy_names(X, _, _), retract(tmp(CurrentList)), assert(tmp([X|CurrentList])), fail. make_name_list(NameList):- retract(tmp(NameList)).

9 program collects all solutions of cango(X, Y, Path) cango_all(X, Y, _AllRoutes):- % initialise assert(tmp([])), cango(X, Y, Path), retract(tmp(CurrentList)), assert(tmp([(X,Y,Path)|CurrentList])), fail. cango_all(_, _, AllRoutes):- retract(tmp(AllRoutes)).

10 How to use “findall” Prolog has provided a built-in predicate findall(Vars, Goal, S) which collects all solutions of Goal into S. Here is an example: test(X,Y, AllRoutes):- findall( (P,L), cango(X,Y,P,L), AllRoutes). This returns AllRoutes = [(path1,line1), (path2,line2),…] where path i and line i are lists.


Download ppt "”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree."

Similar presentations


Ads by Google