Reputation: 1129
:-dynamic listofQuestions/2.
myrule:-
write('P = '), write(Percent), write('-'),write(X),
( listofQuestions(Percent,X) -> true ; assert(listofQuestions(Percent,X)) ),
The code snippet might not be required to answer my question.
I want to assert to a blank 'listofQuestions' everytime I call my rule. This only happens if I close my prolog window and restart it.
Any suggestions?
Upvotes: 1
Views: 1523
Reputation: 3577
abolish/1
removes all clauses of a given predicate from the database. Hence, just add a call to abolish(PredName/Arity)
whenever you need to remove the information about this predicate. Beware that after abolishing the call to the dynamic predicate does not fail but reports an error.
12 ?- f(X,Y).
false.
13 ?- assert(f(a,b)).
true.
14 ?- f(X,Y).
X = a,
Y = b.
15 ?- abolish(f/2).
true.
16 ?- f(X,Y).
ERROR: user://2:67:
toplevel: Undefined procedure: f/2 (DWIM could not correct goal)
In SWI-Prolog, abolish
works on static procedures, unless the prolog flag iso
is set to true. If you intend to remove only dynamic predicates, you should better try retractall
. Observe that in this case removal does not lead to an error being reported but to a failure.
17 ?- [user].
:- dynamic f/2.
|:
% user://3 compiled 0.00 sec, 264 bytes
true.
18 ?- f(X,Y).
false.
19 ?- assert(f(a,b)).
true.
20 ?- f(X,Y).
X = a,
Y = b.
21 ?- retractall(f(X,Y)).
true.
22 ?- f(X,Y).
false.
Upvotes: 4