------------------------------ MODULE CouponCut ------------------------------ (* Lesson 7: the same redeem() with a choice of where to cut it into steps. *) (* redeem() = (1) read coupon, (2) RPC applyDiscount, (3) mark USED. *) (* CutAfterRead / CutAfterCall: does a step end after (1) / after (2)? *) (* No cuts = CouponCoarse; both cuts = CouponRace. *) CONSTANTS Orders, CutAfterRead, CutAfterCall VARIABLES status, discounted, pc vars == <> Init == /\ status = "UNUSED" /\ discounted = [o \in Orders |-> FALSE] /\ pc = [o \in Orders |-> "start"] \* Run (2), then either stop (cut after (2)) or run (3) as part of the same step. CallThenMaybeMark(o) == /\ discounted' = [discounted EXCEPT ![o] = TRUE] /\ IF CutAfterCall THEN /\ pc' = [pc EXCEPT ![o] = "called"] /\ UNCHANGED status ELSE /\ status' = "USED" /\ pc' = [pc EXCEPT ![o] = "done"] \* The step that starts with (1). Start(o) == /\ pc[o] = "start" /\ IF status # "UNUSED" THEN /\ pc' = [pc EXCEPT ![o] = "rejected"] /\ UNCHANGED <> ELSE IF CutAfterRead THEN /\ pc' = [pc EXCEPT ![o] = "checked"] /\ UNCHANGED <> ELSE CallThenMaybeMark(o) \* The step that starts with (2); exists only if there is a cut after (1). AfterRead(o) == pc[o] = "checked" /\ CallThenMaybeMark(o) \* The step that is just (3); exists only if there is a cut after (2). AfterCall(o) == /\ pc[o] = "called" /\ status' = "USED" /\ pc' = [pc EXCEPT ![o] = "done"] /\ UNCHANGED discounted Next == \E o \in Orders : Start(o) \/ AfterRead(o) \/ AfterCall(o) Spec == Init /\ [][Next]_vars NoDoubleDiscount == \A a, b \in Orders : discounted[a] /\ discounted[b] => a = b =============================================================================