且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将事实附加到现有的prolog文件中

更新时间:2022-10-14 23:01:25

这并不奇怪,它只包含事实,因为你已经告诉它保存。最简单的方法是使用

  |? -  tell('test.pl'),写(': - 动态出生/ 2。'),nl,上市(出生/ 2),告诉。 

或者写一个这样做的小程序。根据您打算使用的方式,您可以考虑使用 save_program / 1/2 restore / 1 。 p>

我无法帮助您使用 append / 1 我害怕。


I'm having trouble inserting facts into an existing Prolog file, without overwriting the original contents.

Suppose I have a file test.pl:

:- dynamic born/2. 

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

If I load this in prolog, and I assert more facts:

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

I'm aware I can save this by doing:

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

Which works but test.pl now only contains the facts, not the ":- dynamic born/2":

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

This is problematic because if I reload this file, I won't be able to insert anymore facts into test.pl because ":- dynamic born/2." doesn't exist anymore.

I read somewhere that, I could do:

append('test.pl'),listing(born/2),told.

which should just append to the end of the file, however, I get the following error:

! Existence error in user:append/1
! procedure user:append/1 does not exist
! goal:  user:append('test.pl')

Btw, I'm using Sicstus prolog. Does this make a difference?

Thanks!

It is not surprising it only contains the facts as that is all you have told it to save. The easiest way around this is to use

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

or write a small procedure which does this. Depending on how you intend to use this you may consider using save_program/1/2 and restore/1.

I can't help you with append/1 I'm afraid.