1
votes

I am trying to represent a situation in situation calculus with swi-prolog. The situation is the sale of an item, where:

"Person A sells item I to Person B for $10. The item I had a value of $20 before the sale."

What I have so far is the base facts:

person(a).
person(b).
item(i).

owns(a,i,s0).
value(i,20,s0).

What I think I have to do is define the sell predicate. What I have tried so far is:

sell(Seller, Buyer, Item, Price, S0, S1):-
  (
    person(Seller), person(Buyer), item(Item), owns(Seller,Item,S0)
    ->  not(owns(Seller,Item,S1)), 
        owns(Buyer,Item,S1)
  ).

What I would like to do is to say sell(a,b,i,10,s0,s1) and then check owns(b,i,s1) which should return true. The issue is that I don't know how to set the owns(Buyer,Item,S1), since it doesn't seem to be setting there.

1

1 Answers

0
votes

A possible solution would be to declare predicates such as owns/3 and value/3 as dynamic predicates:

:- dynamic([owns/3, value/3]).

And then rewrite your rule as something like:

sell(Seller, Buyer, Item, Price, S0, S1):-
    (   person(Seller),
        person(Buyer),
        item(Item),
        owns(Seller,Item,S0) -> 
        assertz(owns(Buyer, Item, S1)),
        assertz(value(Item, Price, S1))       
    ).

Using dynamic predicates in Prolog is something that should be avoided when possible but this case may warrant their use.

Sample calls:

?- sell(a, b, i, 10, s0, s1).
true.

?- owns(Who, Item, Time).
Who = a,
Item = i,
Time = s0 ;
Who = b,
Item = i,
Time = s1.