Prolog减去值不起作用

时间:2016-01-07 09:47:34

标签: prolog

我有这个知识库:

bottle(b1).
bottle(b2).
bottle(b3).
bottle(b4).

full(bottle(b1),100).
full(bottle(b2),150).
full(bottle(b3),300).
full(bottle(b4),400).


consume(bottle(X),Milliliter) :-
   full(bottle(X),Y),
   Milliliter=<Y,
   Y-10.

所以我想使用消费谓词,并且我希望将分配给的值减少到与消耗的值一样多的值。是否允许从静态值中减去,如果还没有使用瓶子,我怎么能解决这个问题才能获得值true。

1 个答案:

答案 0 :(得分:0)

如果你想&#34;更新&#34;当你打电话给&#34;消费&#34;时,你必须收回并断言这个事实,例如......

% Use this to add the initial facts (if you don;t have a clause to do this, prolog complains about modifying static clauses...)
addfacts :-
    asserta(full(bottle(b1),100)),
    asserta(full(bottle(b2),150)),
    asserta(full(bottle(b3),300)),
    asserta(full(bottle(b4),400)).

consume(bottle(X), Millis) :-
    % Retract the current state of the bottle
    retract(full(bottle(X), V)),
    % Calculate the new Millis after consumption
    Y is V - Millis,
    % Check it was possible (there should be 0 or more millis left after)
    Y >= 0,
    % Add the new fact
    asserta(full(bottle(X), Y)).

现在在prolog中,你可以做......

1 ?- addfacts.
true.

2 ?- full(bottle(b1), X).
X = 100.

3 ?- consume(bottle(b1), 10).
true.

4 ?- full(bottle(b1), X).
X = 90 .