在Prolog中收回并断言到另一个文件
我试图收回并在另一个文件中声明一个事实。一个(fruit1.pl)包含两个事实,另一个(fruit.pl)包含谓词在Prolog中收回并断言到另一个文件,prolog,prolog-directive-dynamic,Prolog,Prolog Directive Dynamic,我试图收回并在另一个文件中声明一个事实。一个(fruit1.pl)包含两个事实,另一个(fruit.pl)包含谓词start,它指定另一个谓词insert\u fruit将更新的事实: 水果1.pl fruit(apple, [[2, yellow], [1, brown]]). fruit(orange, [[3, orange], [2, orange]]). :- dynamic fruit/2. start :- consult('fruit1.pl'), File
start
,它指定另一个谓词insert\u fruit
将更新的事实:
水果1.pl
fruit(apple, [[2, yellow], [1, brown]]).
fruit(orange, [[3, orange], [2, orange]]).
:- dynamic fruit/2.
start :-
consult('fruit1.pl'),
File = 'fruit1.pl',
Name = apple,
Price = 2,
Color = red,
insert_fruit(File, Name, Price, Color).
insert_fruit(File, Name, Price, Color) :-
open(File, update, Stream),
retract(fruit(Name, Information)),
assert(fruit(Name, [[Price, Color]|Information])),
close(Stream).
水果。pl
fruit(apple, [[2, yellow], [1, brown]]).
fruit(orange, [[3, orange], [2, orange]]).
:- dynamic fruit/2.
start :-
consult('fruit1.pl'),
File = 'fruit1.pl',
Name = apple,
Price = 2,
Color = red,
insert_fruit(File, Name, Price, Color).
insert_fruit(File, Name, Price, Color) :-
open(File, update, Stream),
retract(fruit(Name, Information)),
assert(fruit(Name, [[Price, Color]|Information])),
close(Stream).
但是,
insert\u fruit
没有按预期工作,因为我认为它需要包含流来修改另一个文件,尽管我不知道如何(retract(Stream,…)
不工作)。是否有一些我可以让retract和assert谓词在另一个文件中运行?在SWI Prolog中,您可以使用以下命令从用作持久事实存储的文件中断言/收回事实:
水果/3
声明为持久性。(可选):使用类型注释参数以进行自动类型检查fruit
模块(在本例中为fruit1.pl
)时,附加一个文件作为持久事实存储add_-fruit/3
)和查询(即,current_-fruit/3
)水果事实。回缩的处理方式与此类似与\u mutex/2
一起使用(在您开始收回事实时尤其有用)fruit.pl
,执行:
?- add_fruit(apple, 1.10, red).
?- current_fruit(X, Y, Z).
X = apple,
Y = 1.1,
Z = red
关闭Prolog,启动Prolog(再次),执行:
?- add_fruit(apple, 1.10, red).
?- current_fruit(X, Y, Z).
X = apple,
Y = 1.1,
Z = red
您现在正在阅读fruit1.pl
中的事实
自动类型检查示意图
如前所述,库还会为您执行类型检查,例如:
?- add_fruit(pear, expensive, green).
ERROR: Type error: `float' expected, found `expensive' (an atom)
在SWI Prolog中,您可以使用以下命令从用作持久事实存储的文件中断言/收回事实:
水果/3
声明为持久性。(可选):使用类型注释参数以进行自动类型检查fruit
模块(在本例中为fruit1.pl
)时,附加一个文件作为持久事实存储add_-fruit/3
)和查询(即,current_-fruit/3
)水果事实。回缩的处理方式与此类似与\u mutex/2
一起使用(在您开始收回事实时尤其有用)fruit.pl
,执行:
?- add_fruit(apple, 1.10, red).
?- current_fruit(X, Y, Z).
X = apple,
Y = 1.1,
Z = red
关闭Prolog,启动Prolog(再次),执行:
?- add_fruit(apple, 1.10, red).
?- current_fruit(X, Y, Z).
X = apple,
Y = 1.1,
Z = red
您现在正在阅读fruit1.pl
中的事实
自动类型检查示意图
如前所述,库还会为您执行类型检查,例如:
?- add_fruit(pear, expensive, green).
ERROR: Type error: `float' expected, found `expensive' (an atom)
你所尝试的不是这样的<代码>断言和
收回
操作谓词“数据库”,而不是当前打开的文件。你以前问过类似的问题,得到了评论和回答;读一下,你看到了吗?我在那里给出了答案。还有,鲍里斯说的,你所尝试的不是这样的<代码>断言和收回
操作谓词“数据库”,而不是当前打开的文件。你以前问过类似的问题,得到了评论和回答;读一下,你看到了吗?我在那里给出了答案。还有,鲍里斯说的。嗨,沃特,回答得好。你知道QLF是否受支持吗?你是说准逻辑形式吗?我不熟悉这一点,也不确定它将如何应用于这里。。。你能举一个QLF w.r.t.持久存储的例子吗?不,级别低得多。。。谢谢你的富有洞察力的回复!你好,沃特,回答得好。你知道QLF是否受支持吗?你是说准逻辑形式吗?我不熟悉这一点,也不确定它将如何应用于这里。。。你能举一个QLF w.r.t.持久存储的例子吗?不,级别低得多。。。谢谢你的富有洞察力的回复!