Prolog - how to clear the memory and start from scratch?

If you only use these dynamic facts to implement counters, you should think about whether this is the best way to do it. Using assert/1 and retract/1 makes rather slow code.

You could either make the counter variable another predicate argument that you pass along in your code (you might need to distinguish between input and output, so have two extra arguments), or use global variables (which are non-logical features, though, which sometimes is a no-go).


It depends what system you are using. In YAP, B, GNU, SICStus, the directive :- dynamic(counter/1). has this effect. That is, only the facts from the file are present after reloading.

In SWI, the dynamic predicates are retained as you describe. You need to remove them directly with retractall/1 which retains the information that the predicate is dynamic.