Lecture 2 Recall A state is an assignment of values to all - - PowerPoint PPT Presentation
Lecture 2 Recall A state is an assignment of values to all - - PowerPoint PPT Presentation
Lecture 2 Recall A state is an assignment of values to all variables A step is a pair of states A stu'ering step wrt some variable leaves the variable unchanged An ac.on is a predicate over a pair of states If x is a variable in
Recall
- A state is an assignment of values to all variables
- A step is a pair of states
- A stu'ering step wrt some variable leaves the variable unchanged
- An ac.on is a predicate over a pair of states
- If x is a variable in the old state, then xβ is the same variable in the new state
- A behavior is an infinite sequence of states (with an ini=al state)
- A specifica.on characterizes the ini=al state and ac=ons
Spec that generates all prime numbers
Spec that generates all prime numbers
- ------------------------------ MODULE prime -------------------------------
EXTENDS Naturals VARIABLE p isPrime(q) == q > 1 /\ \A r \in 2..(q-1): q%r /= 0 TypeInvariant == isPrime(p) Init == p = 2 Next == p' > p /\ isPrime(p') /\ \A q \in (p+1)..(p'-1): ~isPrime(q) Spec == Init /\ [] [Next]_p THEOREM Spec => []TypeInvariant
Some more terms
- A state func.on is a first-order logic expression
- A state predicate is a Boolean state func=on
- A temporal formula is an asser=on about behaviors
- A theorem of a specifica=on is a temporal formula that holds over
every behavior of the specifica=on
- If π is a specifica=on and π½ is a predicate and π β βπ½ is a theorem
then we call π½ an invariant of π.
Temporal Formula
Based on Chapter 8 of Specifying Systems
- A temporal formula πΊ assigns a Boolean value to a behavior π
- π β¨ πΊ means that πΊ holds over π
- If π is a state predicate, then π β¨ π means that π holds over the first
state in π
- If π΅ is an ac=on, then π β¨ π΅ means that π΅ holds over the first
two states in π
- i.e., the first step in π is an π΅ step
- note that a state predicate is simply an ac=on without primed variables
- If π΅ is an ac=on, then π β¨ π΅ 2 means that the first step in π is an π΅
step or a stubering step with respect to π€
βAlways
- π β¨ βπΊ means that πΊ holds over every suffix of π
- More formally
- Let π45 be π with the first π states removed
- Then π β¨ βπΊ β βπ β β: π45 β¨ πΊ
Boolean combina@ons of temporal formulas
- π β¨ (πΊ β§ π») β π β¨ πΊ β§ π β¨ π»
- π β¨ (πΊ β¨ π») β π β¨ πΊ β¨ π β¨ π»
- π β¨ Β¬πΊ β Β¬ π β¨ πΊ
- π β¨ πΊ β π» β (π β¨ πΊ) β π β¨ π»
- π β¨ (βπ : πΊ) β βπ : π β¨ πΊ
- π β¨ βπ π π: πΊ β βπ π π: π β¨ πΊ
// if π is a constant set
Example
What is the meaning of π β¨ β( π¦ = 1 β β π§ > 0 ) ? π β¨ β( π¦ = 1 β β π§ > 0 ) β‘ βπ β β: π45 β¨ ( π¦ = 1 β β π§ > 0 ) β‘ βπ β β: (π45 β¨ π¦ = 1) β (π45 β¨ β π§ > 0 ) β‘ βπ β β: (π45 β¨ π¦ = 1) β (βπ β β: π45 4M β¨ π§ > 0 ) If x = 1 in some state, then henceforth y > 0 in all subsequent states Not: once x = 1, x will always be 1. That would be π β¨ β( π¦ = 1 β β π¦ = 1 )
Not every temporal formula is a TLA+ formula
- TLA+ formulas are temporal formulas that are invariant under stu'ering
- They hold even if you add or remove stubering steps
- Examples
- π if π is a state predicate
- βπ if π is a state predicate
- β π΅ 2 if π΅ is an ac=on and π€ is a state variable (or even state func=on)
- But not
- π¦N = π¦ + 1
not sa=sfied by π¦ = 1 βΆ π¦ = 1 βΆ π¦ = 2
- π¦N = π¦ + 1 R
sa=sfied by π¦ = 1 βΆ π¦ = 1 βΆ π¦ = 3 but not by π¦ = 1 βΆ π¦ = 3
- Yet β π¦N = π¦ + 1
R is a TLA+ formula!
HourClock revisi@ed
Module HourClock
- Variable ππ
- HCini β βπ β 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- HCnxt β βπ β² = βπ mod 12 + 1
- HC β HCini β β»[HCnxt]bc
βπ is a parameter of the specifica=on HourClock
Eventually F
βπΊ β Β¬βΒ¬πΊ π β¨ βπΊ β‘ π β¨ Β¬βΒ¬πΊ β‘ Β¬(π β¨ βΒ¬πΊ) β‘ Β¬(βπ β β: π45 β¨ Β¬πΊ) β‘ Β¬(βπ β β: Β¬(π45 β¨ πΊ)) β‘ βπ β β: (π45 β¨ πΊ)
Eventually an π΅ step occursβ¦
β π΅ 2 β Β¬β Β¬π΅ 2 π β¨ β π΅ 2 β‘ π β¨ Β¬β Β¬π΅ 2 β‘ Β¬(π β¨ β Β¬π΅ 2) β‘ Β¬(βπ β β: π45 β¨ Β¬π΅ 2) β‘ Β¬(βπ β β: π45 β¨ (Β¬π΅ β¨ π€N = π€)) β‘ βπ β β: π45 β¨ π΅ β§ π€N β π€
HourClock with liveness
clock that never stops
Module HourClock
- Variable βπ
- HCini β βπ β 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
- HCnxt β βπ β² = βπ mod 12 + 1
- HC β HCini β β»[HCnxt]bc
- LiveHC β HC β β(β HCnxt bc)
Module Channel with Liveness
Send(d) β πβππ.rdy = πβππ.ack β§ chanβ = [ π€ππ βΌ π, π ππ§ βΌ 1 β πβππ. π ππ§, πππ βΌ πβππ. πππ ] Init β πβππ.val β πΈππ’π β§ πβππ.rdy β 0, 1 β§ πβππ.ack = πβππ.rdy TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} Next β βπ β πΈππ’π: ππππ π β¨ Recv Spec β Initβ β»[Next]vbw5 Rπππ€ β πβππ.rdy β πβππ.ack β§ chanβ = [ π€πl βΌ πβππ. π€ππ, π ππ§ βΌ πβππ. π ππ§, πππ βΌ 1 β πβππ. πππ ] Variable πβππ Constant πΈππ’π πππ€πππππ β ππππ β β(ββ¨πππ¦π’β©vbw5) ???
Module Channel with Liveness
Send(d) β πβππ.rdy = πβππ.ack β§ chanβ = [ π€ππ βΌ π, π ππ§ βΌ 1 β πβππ. π ππ§, πππ βΌ πβππ. πππ ] Init β πβππ.val β πΈππ’π β§ πβππ.rdy β 0, 1 β§ πβππ.ack = πβππ.rdy TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} Next β βπ β πΈππ’π: ππππ π β¨ Recv Spec β Initβ β»[Next]vbw5 Rπππ€ β πβππ.rdy β πβππ.ack β§ chanβ = [ π€πl βΌ πβππ. π€ππ, π ππ§ βΌ πβππ. π ππ§, πππ βΌ 1 β πβππ. πππ ] Variable πβππ Constant πΈππ’π πππ€πππππ β ππππ β β(ββ¨πππ¦π’β©vbw5) ??? Too Strong --- If nothing to send that should be ok
Module Channel with Liveness
Send(d) β πβππ.rdy = πβππ.ack β§ chanβ = [ π€ππ βΌ π, π ππ§ βΌ 1 β πβππ. π ππ§, πππ βΌ πβππ. πππ ] Init β πβππ.val β πΈππ’π β§ πβππ.rdy β 0, 1 β§ πβππ.ack = πβππ.rdy TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} Next β βπ β πΈππ’π: ππππ π β¨ Recv Spec β Initβ β»[Next]vbw5 Rπππ€ β πβππ.rdy β πβππ.ack β§ chanβ = [ π€πl βΌ πβππ. π€ππ, π ππ§ βΌ πβππ. π ππ§, πππ βΌ 1 β πβππ. πππ ] Variable πβππ Constant πΈππ’π πππ€πππππ β ππππ β β(πβππ. rdy β πβππ. ack β ββ¨πecvβ©vbw5)
Weak Fairness as a liveness condi@on
- ENABLED β¨π΅β©2 means ac=on A is possible in some state
- State predicate conjuncts all hold
- ππΊ
2 π΅ β β(βENABLED π΅ 2 β ββ¨π΅β©2)
- HourClock:
ππΊ
bc(πΌπ·ππ¦π’)
- Channel:
ππΊ
bc(ππππ€)
(surprising) Weak Fairness equivalence
- ππΊ
2 π΅ β β(βENABLED π΅ 2 β β π΅ 2
β‘ ββ Β¬ENABLED π΅ 2 β¨ βββ¨π΅β©2 β‘ ββ ENABLED π΅ 2 β ββ π΅ 2
- Always, if π΅ is enabled forever, then an π΅ step eventually occurs
- π΅ if infinitely oken disabled or infinitely many π΅ steps occur
- If π΅ is eventually enabled forever then infinitely many π΅ steps occur
Strong Fairness
- ππΊ
2 π΅ β ββ Β¬ENABLED π΅ 2 β¨ βββ¨π΅β©2
β‘ ββ ENABLED π΅ 2 β ββ π΅ 2
- π΅ is eventually disabled forever or infinitely many π΅ steps occur
- If π΅ is infinitely oken enabled then infinitely many π΅ steps occur
ππΊ
2 π΅ : an π΅ step must occur if π΅ is con=nually enabled
ππΊ
2 π΅ : an π΅ step must occur if π΅ is con=nuously enabled
As always, be'er to make the weaker assump.on if you can
How important is liveness?
- Liveness rules out behaviors that have only stubering steps
- Add non-triviality of a specifica=on
- In prac=ce, βeventualβ is oken not good enough
- Instead, need to specify performance requirements
- Service Level Objec=ves (SLOs)
- Usually done quite informally
A βFIFOβ (async buffered FIFO channel)
Chapter 4 from Specifying Systems
Sender buffer Receiver in
- ut
channels environment
Module Channel
Send(d) β πβππ.rdy = πβππ.ack β§ chanβ = [ π€ππ βΌ π, π ππ§ βΌ 1 β πβππ. π ππ§, πππ βΌ πβππ. πππ ] Init β πβππ.val β πΈππ’π β§ πβππ.rdy β 0, 1 β§ πβππ.ack = πβππ.rdy TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} Next β βπ β πΈππ’π: ππππ π β¨ Recv Spec β Initβ β»[Next]vbw5 Rπππ€ β πβππ.rdy β πβππ.ack β§ chanβ = [ π€πl βΌ πβππ. π€ππ, π ππ§ βΌ πβππ. π ππ§, πππ βΌ 1 β πβππ. πππ ] Vππ¬π£πππ¦π π πππ Constant π¬πππ
Instan@a@ng a Channel
π½ππ·βππ β INSTANCE π·βπππππ WITH πΈππ’π β πππ‘π‘πππ, πβππ β ππ TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} InChan!TypeInvariant β‘ ππ β π€ππ: πππ‘π‘πππ, π ππ§: 0,1 , πππ: {0,1}
Instan.a.on is Subs.tu.on!
Parametrized Instan@a@on
(not parameterized instan@a@on J)
π½ππ·βππ β INSTANCE π·βπππππ WITH πΈππ’π β πππ‘π‘πππ, πβππ β ππ TypeInvariant β πβππ β π€ππ: πΈππ’π, π ππ§: 0,1 , πππ: {0,1} Chan(in)!TypeInvariant β‘ ππ β π€ππ: πππ‘π‘πππ, π ππ§: 0,1 , πππ: {0,1} π·βππ(πβ) β INSTANCE π·βπππππ WITH πΈππ’π β πππ‘π‘πππ, πβππ β πβ
Internal (= Non-Interface) Variables
buffer in
- ut
There is no q here But there is a q here Not incorrect, but donβt really want q to be a specifica=on parameter
Hiding Internal Variables
Hiding Internal Variables
Not the normal existen=al quan=fier!!! In temporal logic, this means that for every state in a behavior, there is a value for q that makes Inner(q)!Spec true
- PreYy. Now for something cool!
- Suppose we wanted to implemented a bounded buffer
- That is, β»πππ π β€ π for some constant π > 0
- The only place where q is extended is in BufRcv
- PreYy. Now for something cool!
- Suppose we wanted to implemented a bounded buffer
- That is, β»πππ π β€ π for some constant π > 0
- The only place where q is extended is in BufRcv
β§ πππ π < π
Even cooler (but tricky)
If it is a BufRcv step, then πππ π < π