Prolog - 断言和撤回 [英] Prolog - ASSERT and RETRACT
问题描述
我想知道,我知道您可以使用 assert
来添加事实或规则,或者如果您已将谓词声明为 -:dynamic
则可以添加任何内容,但这只是允许所做的更改仅保留在该会话中,例如如果关闭 Prolog 窗口,则数据库更改将丢失.
I was wondering, I am aware you can use assert
to add facts or rules or whatever if you have declared the predicate to be -:dynamic
, but this only allows the changes that are made to be kept in that session only, e.g. if you close the Prolog window then the database changes are lost.
所以我想知道,有没有办法让 assert
和 retract
谓词可以对 Prolog .pl 文件进行永久更改?
So I was wondering, is there any way of making it so that the assert
and retract
predicates can make permanent changes to the Prolog .pl file?
谢谢
推荐答案
我可以建议你一个非常简单的方法.
I can suggest you a very simple way of doing this.
1 ?- assert(a(1)).
true.
2 ?- assert(a(2)).
true.
3 ?- assert(a(3)).
true.
4 ?- a(A).
A = 1 ;
A = 2 ;
A = 3.
5 ?- tell('a_db.txt'), listing(a), told.
true.
然后关闭会话,重新打开.
Then close session, reopen.
1 ?- a(A).
ERROR: toplevel: Undefined procedure: a/1 (DWIM could not correct goal)
2 ?- ['a_db.txt'].
% a_db.txt compiled 0.00 sec, 516 bytes
true.
3 ?- a(A).
A = 1 ;
A = 2 ;
A = 3.
4 ?- listing(a).
:- dynamic a/1.
a(1).
a(2).
a(3).
true.
这篇关于Prolog - 断言和撤回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!