将事实附加到现有的序言文件中
-
05-10-2019 - |
题
我在不覆盖原始内容的情况下将事实插入现有的序言文件中很难。
假设我有一个文件test.pl:
:- dynamic born/2.
born(john,london).
born(tim,manchester).
如果我将其加载到Prolog中,并且我主张更多事实:
| ?- assert(born(laura,kent)).
yes
我知道我可以通过做到这一点:
|?- tell('test.pl'),listing(born/2),told.
哪个可行,但test.pl现在仅包含事实,而不包含“: - 动态born/2”:
born(john,london).
born(tim,manchester).
born(laura,kent).
这是有问题的,因为如果我重新加载此文件,我将无法将事实插入test.pl。不再存在。
我在某个地方读了我可以做的:
append('test.pl'),listing(born/2),told.
但是,它应该只附加到文件的末尾,但是,我会收到以下错误:
! Existence error in user:append/1
! procedure user:append/1 does not exist
! goal: user:append('test.pl')
顺便说一句,我正在使用sicstus prolog。这有所作为吗?
谢谢!
解决方案
毫不奇怪,它仅包含事实,因为您已经告诉它保存的一切。最简单的方法是使用
|?- tell('test.pl'), write(':- dynamic born/2.'), nl, listing(born/2), told.
或写一个这样做的小过程。根据您打算如何使用此功能,您可能会考虑使用 save_program/1/2
和 restore/1
.
我无能为力 append/1
我耽心。
不隶属于 StackOverflow