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

3

10

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.

Somatoplasm answered 7/12, 2011 at 15:5 Comment(0)
S
3

Insert

:- abolish(counter/1).

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

Shastashastra answered 7/12, 2011 at 15:17 Comment(3)
thanks, I tried it but now it gives ERROR: retract/1: No permission to modify static_procedure counter/1. although I have :- dynamic counter/1. at the start.Somatoplasm
did you put the abolish thing on top of the dynamic thing ?Shastashastra
btw if you are a beginner in prolog try to avoid assert/retract and global variables until you know better, it's often an old imperative reflex that won't help you to get the language.Shastashastra
T
6

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

Tapir answered 7/12, 2011 at 15:29 Comment(0)
V
4

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.

Viviennevivify answered 7/12, 2011 at 15:30 Comment(0)
S
3

Insert

:- abolish(counter/1).

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

Shastashastra answered 7/12, 2011 at 15:17 Comment(3)
thanks, I tried it but now it gives ERROR: retract/1: No permission to modify static_procedure counter/1. although I have :- dynamic counter/1. at the start.Somatoplasm
did you put the abolish thing on top of the dynamic thing ?Shastashastra
btw if you are a beginner in prolog try to avoid assert/retract and global variables until you know better, it's often an old imperative reflex that won't help you to get the language.Shastashastra

© 2022 - 2024 — McMap. All rights reserved.