Prolog - 如何清除内存并从头开始?
我正在 .pl 文件中开发一种算法,并通过命令窗口上的查询来检查它。 我使用动态变量和撤消/断言谓词。当我修改 pl 文件并单击“重新加载修改的文件”时,我有我不想要的额外事实。
例如,一开始我有 计数器(0)。
我做了一些事情,收回并断言这个计数器,它变成了计数器(7)。然后,当我重新加载修改后的 pl 文件时,我有两个 计数器(0)。和 计数器(7)。
我怎样才能防止这种情况并且只有计数器(0)。一开始?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
插入
到文件的开头。完成测试后,将其删除。
Insert
at the start of your file. When you'll be done testing, remove it.
如果您只使用这些动态事实来实现计数器,您应该考虑这是否是最好的方法。使用
assert/1
和retract/1
会使代码变得相当慢。您可以将计数器变量设置为在代码中传递的另一个谓词参数(您可能需要区分输入和输出,因此有两个额外的参数),或者使用全局变量(不过,它们是非逻辑功能,有时是不行的)。
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
andretract/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).
这取决于您使用的是什么系统。在 YAP、B、GNU、SICStus 中,
指令
:-dynamic(counter/1).
具有此效果。也就是说,只有重新加载后,文件中的事实仍然存在。
在 SWI 中,动态谓词将按照您的描述保留。您需要直接使用
retractall/1
删除它们,它保留谓词是动态的信息。It depends what system you are using. In YAP, B, GNU, SICStus, the
directive
:- dynamic(counter/1).
has this effect. That is, only thefacts 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.