------------------------------ MODULE MiniLock ------------------------------ (* Lesson 5: the "lock held until fulfillment" fix from lesson 1. *) (* s1 = order for subscription A, s2 = order for subscription B. *) (* An order may start only while the other order has not started. *) VARIABLES s1, s2 vars == <> Status == {"idle", "open", "done"} TypeOK == s1 \in Status /\ s2 \in Status Init == s1 = "idle" /\ s2 = "idle" Checkout1 == s1 = "idle" /\ s2 = "idle" /\ s1' = "open" /\ UNCHANGED s2 \* place order A Checkout2 == s2 = "idle" /\ s1 = "idle" /\ s2' = "open" /\ UNCHANGED s1 \* place order B Callback1 == s1 = "open" /\ s1' = "done" /\ UNCHANGED s2 \* fulfill order A Callback2 == s2 = "open" /\ s2' = "done" /\ UNCHANGED s1 \* fulfill order B Next == Checkout1 \/ Checkout2 \/ Callback1 \/ Callback2 Spec == Init /\ [][Next]_vars \* The business rule: at most one subscription is active. AtMostOne == ~(s1 = "done" /\ s2 = "done") \* The strengthened, inductive invariant: at most one order has started. Started(s) == s \in {"open", "done"} Inv == TypeOK /\ ~(Started(s1) /\ Started(s2)) \* Starting points for checking inductiveness with TLC. IndInitWeak == TypeOK /\ AtMostOne IndInitStrong == Inv =============================================================================