2016-01-07 54 views
0

我得到了這方面的知識基礎: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. 

所以我想用消耗謂語,我想,以減少分配給完全不亞於這是越來越消耗的值的值。它是否允許從靜態值中減去,我如何才能解決這個問題,只有在沒有消耗瓶子時才能達到真值。

回答

0

如果你想「升級」當你調用「消費」,你將不得不收回並斷言的事實,例如KB ...

% 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)). 

現在,在序言中,你可以做。 ..

1 ?- addfacts. 
true. 

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

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

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