【问题标题】:Appending facts into an existing prolog file将事实附加到现有的 prolog 文件中
【发布时间】:2010-05-27 14:19:56
【问题描述】:

我在将事实插入现有 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。这有什么不同吗?

谢谢!

【问题讨论】:

标签: dynamic prolog sicstus-prolog prolog-assert


【解决方案1】:

它只包含事实并不奇怪,因为这就是你告诉它要保存的全部内容。解决这个问题的最简单方法是使用

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

或者写一个小程序来做这件事。根据您打算如何使用它,您可以考虑使用save_program/1/2restore/1

恐怕我无法为您提供append/1 的帮助。

【讨论】:

  • 对于 SWP-Prolog,不需要直接声明 writeborn/2。由listing(born/2)完成。
  • 非常感谢,这确实有效。我来看看如何使用 save_program 和 restore。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 2014-07-04
  • 2014-08-01
  • 1970-01-01
相关资源
最近更新 更多