*)
------------------------------------------------------------------------------
(* Proof of the algorithm. *)
(* Let's start by showing type correctness. *)
LEMMA Typing == Spec => []TypeOk
<1>1. Init => TypeOk
BY NAssumption DEF Init, TypeOk
<1>2. TypeOk /\ [Next]_vars => TypeOk'
BY DEF TypeOk, Next, vars
<1>. QED BY <1>1, <1>2, PTL DEF Spec
(* Now let's rewrite the enabledness condition that occurs as part
of the fairness hypothesis to a simple state predicate. *)
LEMMA EnabledNext ==
ASSUME TypeOk
PROVE (ENABLED <<Next>>_vars) <=> i > 0
\* any Next step changes vars: here type correctness is relevant
<1>1. <<Next>>_vars <=> Next
BY DEF TypeOk, Next, vars
\* therefore the ENABLED conditions are equivalent
<1>2. (ENABLED <<Next>>_vars) <=> ENABLED Next
BY <1>1, ENABLEDrules
\* The method ExpandENABLED replaces ENABLED by explicit quantification.
<1>. QED
BY <1>2, ExpandENABLED DEF Next, vars
(* We can now prove termination by induction. *)
THEOREM Terminate == Spec => <>Termination
<1>. DEFINE BSpec == []TypeOk /\ [][Next]_vars /\ WF_vars(Next)
P(n) == [](i=n => <>Termination)
<1>1. SUFFICES BSpec => <>Termination
BY Typing, PTL DEF Spec
<1>2. BSpec => \A n \in Nat : P(n)
<2>1. BSpec => P(0)
<3>1. i=0 => Termination BY DEF Termination
<3>. QED BY <3>1, PTL
<2>2. ASSUME NEW n \in Nat
PROVE (BSpec => P(n)) => (BSpec => P(n+1))
<3>1. i=n+1 /\ [Next]_vars => (i=n+1)' \/ (i=n)'
BY DEF Next, vars
<3>2. i=n+1 /\ <<Next>>_vars => (i=n)'
BY DEF Next, vars
<3>3. TypeOk /\ i=n+1 => ENABLED <<Next>>_vars
BY EnabledNext
<3>4. BSpec => [](i=n+1 => <>(i=n))
BY <3>1, <3>2, <3>3, PTL DEF BSpec
<3>. QED BY <3>4, PTL
<2>. HIDE DEF BSpec, P
<2>. QED BY <2>1, <2>2, NatInduction
<1>3. BSpec => \A n \in Nat : i=n => <>Termination
<2>. SUFFICES ASSUME NEW n \in Nat PROVE BSpec => (i=n => <>Termination)
OBVIOUS
<2>1. BSpec => P(n) BY <1>2
<2>. QED BY <2>1, PTL
<1>4. BSpec => \E n \in Nat : i=n
<2>1. TypeOk => \E n \in Nat : i=n BY DEF TypeOk
<2>. QED BY <2>1, PTL DEF BSpec
<1>. QED BY <1>3, <1>4
=============================================================================