Prolog - how to clear memory and start from scratch?

I am developing an algorithm in a .pl file and reviewing it with requests in a command window. I use dynamic variables and retract / assert predicates. And when I modify the pl file and click "reload modified files", I have additional facts that I do not need.

for example, at the beginning I have a counter (0).

and I do something, remove and approve this counter, it becomes a counter (7). Then, when I reload the modified pl file, I have both counter (0). as well as a counter (7).

How can I prevent this and have a counter (0). at the beginning?

Thanks in advance.

+6
source share
3 answers

Insert

:- abolish(counter/1). 

at the beginning of your file. When you are finished testing, delete it.

+3
source

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

You can either make the counter variable another predicate argument that you pass in your code (you may need to distinguish between input and output, so you have two additional arguments) or use global variables (which are illogical functions, although sometimes this is not good).

+6
source

It depends on which 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 the reboot.

In SWI, dynamic predicates are stored in the description. You need to delete them directly with retractall/1 , which stores information about the dynamic predicate.

+4
source

Source: https://habr.com/ru/post/903300/


All Articles