Next: About this document
Up: Global variables and DataBase
Previous: assert
- Delete the first clause that matches the given clause
- Arity of 1
- If clause does note exist - it fails
likes(pooh,honey).
likes(Person, Anything) :-
nice(Anything).
likes(fred,fish).
Q: retract((likes(fred,X) :- B)).
X = _567
B = nice(_0032)
On backtracking, this will delete the third clause, binding the variables
as follows :
X = fish
Y = true
- retract is non-deterministic, in that a set of clauses can be
removed by a single retract clause on backtracking
- Again variations are possible : retractall/1, retractone/1
retractx/2
- retractall : delete ALL clauses that match the given clause head,
e.g. retractall(someone). will delete all clauses someone/0
- retractone : deletes the first clause that matches the given clause head,
e.g. retractone(someone). will delete the first clause someone/0
- retractx : retract a clause at a specified position,
e.g. retractx(someone/2,3). will delete the third clause for the predicate someone/2
Combining retract and assert :
change( Variable, New_value) :-
retract( value( Variable, _)),
assert( value( Variable, New_value)).
and
fast(anne).
slow(tom).
slow(pat).
Q: assert((faster(X,Y) :- fast(X), slow(Y))).
A: yes
Q: faster(A,B).
A: A = anne B = tom
Q: retract(slow(X)).
A: X = tom; X = pat; no
Q: faster(anne,_)
A: no
Omer F Rana
Thu Feb 20 20:05:17 GMT 1997