0

我无法将事实插入现有的 Prolog 文件,而不会覆盖原始内容。

假设我有一个文件 test.pl:

:- dynamic born/2. 

born(john,london).
born(tim,manchester).

如果我在序言中加载它,并且我断言更多事实:

| ?- assert(born(laura,kent)).
yes

我知道我可以通过以下方式保存它:

|?- tell('test.pl'),listing(born/2),told.

哪个有效,但 test.pl 现在只包含事实,而不是“:-动态出生/2”:

born(john,london).
born(tim,manchester).
born(laura,kent).

这是有问题的,因为如果我重新加载此文件,我将无法再将事实插入 test.pl,因为“:-动态出生/2”。不存在了。

我在某处读到,我可以这样做:

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。这有什么不同吗?

谢谢!

4

1 回答 1

2

毫不奇怪,它只包含事实,因为这就是您告诉它要保存的全部内容。最简单的方法是使用

|?- tell('test.pl'), write(':- dynamic born/2.'), nl, listing(born/2), told.

或编写一个小程序来执行此操作。根据您打算如何使用它,您可以考虑使用save_program/1/2and restore/1

append/1恐怕我帮不了你。

于 2010-05-29T15:08:21.583 回答