我试图预先计算一些东西和我的节目开始将结果保存为事实:(简化代码)序言:含有断言规则只添加了第一个结果,以事实
:- dynamic cost/2.
%recipe(Id,Cost)
recipe(1,20).
recipe(2,40).
assert_all :- recipe(Id,Cost), assert(cost(Id,Cost)).
但只有第一结果,当我在协商的Prolog SICStus文件成本(1,20)被断言:
| ?- assert_all.
yes
| ?- cost(Id,Cost).
Id = 1,
Cost = 20 ? ;
no
| ?
然而,当我输入直接在SICStus序言控制台assert_all的右手侧,两者的成本/ 2事实就在那里。
| ?- recipe(Id,Cost), assert(cost(Id,Cost)).
Id = 1,
Cost = 20 ? ;
Id = 2,
Cost = 40 ? ;
no
| ?- cost(Id,Cost).
Id = 1,
Cost = 20 ? ;
Id = 2,
Cost = 40 ? ;
no
我觉得这种行为很混乱,发生了什么事?
尝试使用asserta在开头保存您的结果:http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse48 –