assert and retract - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

assert and retract

Description:

disgusting. ?-assert(sunshine). ?-funny. ?-retract(raining). ?-nice. nice:- sunshine, ... disgusting:- raining, fog. raining. fog. Examples of using assert/retract ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 11
Provided by: rong94
Category:

less

Transcript and Presenter's Notes

Title: assert and retract


1
assert and retract
  • Modifying Programs Dynamically
  • Prologs 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 gt 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 F1F2

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 gt 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(XCurrentList)),
  • 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,YPath)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 pathi and linei are lists.
Write a Comment
User Comments (0)
About PowerShow.com