Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm developing an algorithm in a .pl file, and examining it with queries on the command window. I use dynamic variables and retract/assert predicates. And when I modify the pl file and click on "reload modified files", I have extra facts that I don't want.

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

and I do something, retract&assert this counter, it becomes counter(7). Then, when I reload the modified pl file, I have both counter(0). and counter(7).

How can I prevent this and only have counter(0). in the beginning?

Thanks in advance.

like image 597
void Avatar asked Dec 07 '11 15:12

void


People also ask

How do you clear the console in Prolog?

On UNIX terminals, you can simply use the built-in tty_clear predicate to clear the console. tty_clear.

How do I get out of Prolog?

If you want to exit SWI-Prolog, issue the command halt., or simply type CTRL-d at the SWI-Prolog prompt.

What does exit mean in Prolog?

In lines starting with Call Prolog is telling which is the goal that it is at the moment attempting to prove. Fail: ... means that the specified goal failed, and Exit: ... means that it succeeded.

What does Prolog procedure execute?

A Prolog program consists of a collection of procedures. Each procedure defines a particular predicate, being a certain relationship between its arguments. A procedure consists of one or more assertions, or clauses. It is convenient to think of two kinds of clauses: facts and rules.


3 Answers

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).

like image 64
twinterer Avatar answered Nov 03 '22 11:11

twinterer


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.

like image 30
false Avatar answered Nov 03 '22 10:11

false


Insert

:- abolish(counter/1).

at the start of your file. When you'll be done testing, remove it.

like image 42
m09 Avatar answered Nov 03 '22 09:11

m09