将事实附加到现有的prolog文件中 [英] Appending facts into an existing prolog file

查看:229
本文介绍了将事实附加到现有的prolog文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 : - 动态出生/ 2。 

出生(约翰,伦敦)。
出生(tim,曼彻斯特)。

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

  | ?(assert(born(laura,kent))。 

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

  |? -  tell('test.pl'),列表(born / 2)告诉。 

哪些工作,但test.pl现在只包含事实,而不是: - 动态出生/ 2 :

  born(john,london)。 
出生(tim,曼彻斯特)。
出生(laura,kent)。

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



我读过的地方,我可以做:

  append 'test.pl'),列出(生/ 2),告诉。 

它应该只是附加到文件的末尾,但是我收到以下错误: p>

 !用户出现错误:append / 1 
!程序用户:append / 1不存在
!目标:user:append('test.pl')

Btw,我正在使用Sicstus prolog。这是否有所作为?



谢谢!

解决方案

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

  |? -  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.

这篇关于将事实附加到现有的prolog文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆