Objective Explain basic concepts of TLA + modeling systems: static - - PowerPoint PPT Presentation

objective
SMART_READER_LITE
LIVE PREVIEW

Objective Explain basic concepts of TLA + modeling systems: static - - PowerPoint PPT Presentation

A Tutorial Introduction to TLA + Stephan Merz http://www.loria.fr/merz/ INRIA Nancy & LORIA Nancy, France TLA + Community Event, ABZ 2014 Toulouse, June 3, 2014 TLA + Tutorial Stephan Merz (INRIA Nancy) Toulouse, June 2014 1 / 39


slide-1
SLIDE 1

A Tutorial Introduction to TLA+

Stephan Merz

http://www.loria.fr/˜merz/ INRIA Nancy & LORIA Nancy, France

TLA+ Community Event, ABZ 2014 Toulouse, June 3, 2014

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 1 / 39

slide-2
SLIDE 2

Objective

Explain basic concepts of TLA+

◮ modeling systems: static and dynamic aspects ◮ existing tool support for modeling and analysis

PlusCal translator, TLC model checker, TLAPS proof platform

◮ elementary aspects of system refinement

Example-driven presentation, not trying to be exhaustive

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 2 / 39

slide-3
SLIDE 3

Outline

1

Modeling Systems in TLA+

2

System Verification

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 3 / 39

slide-4
SLIDE 4

Example: Distributed Termination Detection

3 2 1

Nodes arranged on a ring perform some computation

◮ nodes can be active (double circle) or inactive ◮ how can node 0 (master node) detect when all nodes are inactive? Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 4 / 39

slide-5
SLIDE 5

Example: Distributed Termination Detection

3 2 1

  • 3

2 1

Nodes arranged on a ring perform some computation

◮ nodes can be active (double circle) or inactive ◮ how can node 0 (master node) detect when all nodes are inactive?

Token-based algorithm

◮ initially: token at master node, who may pass it to its neighbor Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 4 / 39

slide-6
SLIDE 6

Example: Distributed Termination Detection

3 2 1

  • 3

2 1

  • 3

2 1

Nodes arranged on a ring perform some computation

◮ nodes can be active (double circle) or inactive ◮ how can node 0 (master node) detect when all nodes are inactive?

Token-based algorithm

◮ initially: token at master node, who may pass it to its neighbor ◮ when a node is inactive, it passes on the token Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 4 / 39

slide-7
SLIDE 7

Example: Distributed Termination Detection

3 2 1

  • 3

2 1

  • 3

2 1 · · · 3 2 1

Nodes arranged on a ring perform some computation

◮ nodes can be active (double circle) or inactive ◮ how can node 0 (master node) detect when all nodes are inactive?

Token-based algorithm

◮ initially: token at master node, who may pass it to its neighbor ◮ when a node is inactive, it passes on the token ◮ termination detected when token returns to inactive master node Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 4 / 39

slide-8
SLIDE 8

Example: Distributed Termination Detection

3 2 1

  • 3

2 1

  • 3

2 1 · · · 3 2 1

Nodes arranged on a ring perform some computation

◮ nodes can be active (double circle) or inactive ◮ how can node 0 (master node) detect when all nodes are inactive?

Token-based algorithm

◮ initially: token at master node, who may pass it to its neighbor ◮ when a node is inactive, it passes on the token ◮ termination detected when token returns to inactive master node

Complication: nodes may send messages, activating receiver

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 4 / 39

slide-9
SLIDE 9

Dijkstra’s Algorithm (EWD 840, 1983)

3 2 1

Nodes and token colored black or white

◮ master node initiates probe by sending white token Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 5 / 39

slide-10
SLIDE 10

Dijkstra’s Algorithm (EWD 840, 1983)

3 2 1

  • 3

2 1

Nodes and token colored black or white

◮ master node initiates probe by sending white token ◮ message to higher-numbered node stains sending node Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 5 / 39

slide-11
SLIDE 11

Dijkstra’s Algorithm (EWD 840, 1983)

3 2 1

  • 3

2 1

  • 3

2 1

Nodes and token colored black or white

◮ master node initiates probe by sending white token ◮ message to higher-numbered node stains sending node ◮ when passing the token, a black node stains the token Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 5 / 39

slide-12
SLIDE 12

Dijkstra’s Algorithm (EWD 840, 1983)

3 2 1

  • 3

2 1

  • 3

2 1

Nodes and token colored black or white

◮ master node initiates probe by sending white token ◮ message to higher-numbered node stains sending node ◮ when passing the token, a black node stains the token

Termination detection by master node

◮ white token at inactive, white master node Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 5 / 39

slide-13
SLIDE 13

Dijkstra’s Algorithm (EWD 840, 1983)

3 2 1

  • 3

2 1

  • 3

2 1

Nodes and token colored black or white

◮ master node initiates probe by sending white token ◮ message to higher-numbered node stains sending node ◮ when passing the token, a black node stains the token

Termination detection by master node

◮ white token at inactive, white master node

Required correctness properties

◮ safety:

termination detected only if all nodes inactive

◮ liveness: when all nodes inactive, termination will be detected Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 5 / 39

slide-14
SLIDE 14

TLA+ Specification of EWD 840: Data Model

MODULE EWD840 EXTENDS Naturals CONSTANT N ASSUME NAssumption

= N ∈ Nat \ {0} Nodes

= 0 .. N − 1 Color

= {“white”, “black” }

VARIABLES tpos, tcolor, active, color

TypeOK

= ∧ tpos ∈ Nodes ∧ tcolor ∈ Color ∧ active ∈ [Nodes → BOOLEAN] ∧ color ∈ [Nodes → Color]

Declaration of parameters Definition of operators

◮ sets Nodes and Color ◮ TypeOK documents expected values of variables ◮ active and color are arrays, i.e. functions Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 6 / 39

slide-15
SLIDE 15

TLA+ Specification of EWD 840: Behavior (1)

Init

= ∧ tpos ∈ Nodes ∧ tcolor = “black” ∧ active ∈ [Nodes → BOOLEAN] ∧ color ∈ [Nodes → Color]

Initial condition: any “type-correct” values; token should be black

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 7 / 39

slide-16
SLIDE 16

TLA+ Specification of EWD 840: Behavior (1)

Init

= ∧ tpos ∈ Nodes ∧ tcolor = “black” ∧ active ∈ [Nodes → BOOLEAN] ∧ color ∈ [Nodes → Color] InitiateProbe

= ∧ tpos = 0 ∧ (tcolor = “black” ∨ color[0] = “black”) ∧ tpos′ = N − 1 ∧ tcolor′ = “white” ∧ color′ = [color EXCEPT ![0] = “white”] ∧ active′ = active PassToken(i)

= ∧ tpos = i ∧ ¬active[i] ∧ tpos′ = i − 1 ∧ tcolor′ = IF color[i] = “black” THEN “black” ELSE tcolor ∧ color′ = [color EXCEPT ![i] = “white”] ∧ active′ = active

Initial condition: any “type-correct” values; token should be black Action definitions: describe transitions of the algorithm

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 7 / 39

slide-17
SLIDE 17

TLA+ Specification of EWD 840: Behavior (2)

SendMsg(i)

= ∧ active[i] ∧ ∃j ∈ Nodes \ {i} : ∧ active′ = [active EXCEPT ![j] = TRUE] ∧ color′ = [color EXCEPT ![i] = IF j > i THEN “black” ELSE @] ∧ UNCHANGED tpos, tcolor Deactivate(i)

= ∧ active[i] ∧ active′ = [active EXCEPT ![i] = FALSE] ∧ UNCHANGED color, tpos, tcolor

Definition of remaining actions

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 8 / 39

slide-18
SLIDE 18

TLA+ Specification of EWD 840: Behavior (2)

SendMsg(i)

= ∧ active[i] ∧ ∃j ∈ Nodes \ {i} : ∧ active′ = [active EXCEPT ![j] = TRUE] ∧ color′ = [color EXCEPT ![i] = IF j > i THEN “black” ELSE @] ∧ UNCHANGED tpos, tcolor Deactivate(i)

= ∧ active[i] ∧ active′ = [active EXCEPT ![i] = FALSE] ∧ UNCHANGED color, tpos, tcolor Next

= ∨ InitiateProbe ∨ ∃i ∈ Nodes \ {0} : PassToken(i) ∨ ∃i ∈ Nodes : SendMsg(i) ∨ Deactivate(i) vars

= tpos, tcolor, active, color Spec

= Init ∧ [Next]vars

Definition of remaining actions Possible executions: initial condition, interleaving of transitions

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 8 / 39

slide-19
SLIDE 19

Modeling a System in TLA+

1

Describe the system configurations

◮ represent the state of the system by state variables ◮ mathematical abstractions: numbers, sets, functions, tuples, . . . Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 9 / 39

slide-20
SLIDE 20

Modeling a System in TLA+

1

Describe the system configurations

◮ represent the state of the system by state variables ◮ mathematical abstractions: numbers, sets, functions, tuples, . . . 2

Specify system behavior as a state machine Init ∧ [Next]v

◮ initial condition: state formula

identifies initial states

◮ next-state relation: action formula constrains allowed transitions ◮ overall spec: temporal formula

defines system executions

◮ [Next]v

every transition satisfies Next or leaves v unchanged

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 9 / 39

slide-21
SLIDE 21

Modeling a System in TLA+

1

Describe the system configurations

◮ represent the state of the system by state variables ◮ mathematical abstractions: numbers, sets, functions, tuples, . . . 2

Specify system behavior as a state machine Init ∧ [Next]v

◮ initial condition: state formula

identifies initial states

◮ next-state relation: action formula constrains allowed transitions ◮ overall spec: temporal formula

defines system executions

◮ [Next]v

every transition satisfies Next or leaves v unchanged

Specifications (and properties) expressed in mathematical logic

◮ formally, specify a universe that contains the modeled system ◮ use the power of mathematical logic to decompose the specification Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 9 / 39

slide-22
SLIDE 22

Outline

1

Modeling Systems in TLA+

2

System Verification Safety Properties Liveness Properties

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 10 / 39

slide-23
SLIDE 23

Outline

1

Modeling Systems in TLA+

2

System Verification Safety Properties Liveness Properties

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 11 / 39

slide-24
SLIDE 24

Safety Properties in TLA+

1

Prove type correctness

◮ invariant of the specification:

THEOREM Spec ⇒ TypeOK

◮ asserts that TypeOK is always true during any execution of Spec Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 12 / 39

slide-25
SLIDE 25

Safety Properties in TLA+

1

Prove type correctness

◮ invariant of the specification:

THEOREM Spec ⇒ TypeOK

◮ asserts that TypeOK is always true during any execution of Spec 2

Termination detection implies that all nodes are inactive

◮ termination detected when white token at inactive, white node 0

terminationDetected

= tpos = 0 ∧ tcolor = “white” ∧ ¬active[0] ∧ color[0] = “white” TerminationDetection

= terminationDetected ⇒ ∀i ∈ Nodes : ¬active[i]

THEOREM Spec ⇒ TerminationDetection

◮ formally again expressed as an invariant Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 12 / 39

slide-26
SLIDE 26

Model Checking Using TLC

Define a model: finite instance of TLA+ specification

◮ instantiate system parameters by concrete values

for example, create instance for N = 5

◮ indicate operator corresponding to system specification

in our example, Spec

◮ indicate invariants to verify

formulas TypeOK and TerminationDetection

◮ run TLC on this model and for these properties

Fully integrated into the TLA+ Toolbox

◮ Eclipse IDE for developing and analyzing TLA+ specifications

Use TLC also for validating the specification

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 13 / 39

slide-27
SLIDE 27
slide-28
SLIDE 28
slide-29
SLIDE 29

Using TLAPS to Prove Safety of EWD 840

TLAPS: proof assistant for verifying TLA+ specifications

◮ interesting specifications cannot be verified fully automatically

(for arbitrary instances)

◮ user interaction guides verification ◮ automatic back-end provers discharge leaf obligations Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 16 / 39

slide-30
SLIDE 30

Using TLAPS to Prove Safety of EWD 840

TLAPS: proof assistant for verifying TLA+ specifications

◮ interesting specifications cannot be verified fully automatically

(for arbitrary instances)

◮ user interaction guides verification ◮ automatic back-end provers discharge leaf obligations

Proving a simple invariant in TLAPS

THEOREM TypeOK inv

= Spec ⇒ TypeOK

  • 11. Init ⇒ TypeOK
  • 12. TypeOK ∧ [Next]vars ⇒ TypeOK′
  • 13. QED

BY11, 12, PTL DEF Spec

◮ hierarchical proof language represents proof tree ◮ steps can be proved in any order: usually start with QED step ◮ invariant follows from steps 11 and 12 by temporal logic Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 16 / 39

slide-31
SLIDE 31

Simple Proofs

Prove that Init implies TypeOK

  • 11. Init ⇒ TypeOK

BY NAssumption DEFS Init, TypeOK, Node, Color

◮ definitions and facts must be cited explicitly ◮ this helps manage the size of the search space for backend provers Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 17 / 39

slide-32
SLIDE 32

Simple Proofs

Prove that Init implies TypeOK

  • 11. Init ⇒ TypeOK

BY NAssumption DEFS Init, TypeOK, Node, Color

◮ definitions and facts must be cited explicitly ◮ this helps manage the size of the search space for backend provers

Attempt similar proof for step 12

  • 12. TypeOK ∧ [Next]vars ⇒ TypeOK′

BY NAssumption DEFS TypeOK, Next, vars, InitiateProbe, . . .

◮ back-end provers don’t prove this automatically ◮ use TLC to ensure that TypeOK is an inductive invariant

TypeOK ∧ [Next]vars ⇒ TypeOK

◮ decompose proof obligation into simpler steps Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 17 / 39

slide-33
SLIDE 33

Hierarchical Proofs

  • 12. TypeOK ∧ [Next]vars ⇒ TypeOK′

2 USE DEF TypeOK, Node, Color 2 SUFFICES ASSUME TypeOK, Next

PROVE

TypeOK′

BY DEFS TypeOK, vars

  • 21. CASE InitiateProbe

BY 21 DEF InitiateProbe

  • 22. ASSUME NEW i ∈ Node \ {0}, PassToken(i)

PROVE

TypeOK′

BY 22, NAssumption DEF PassToken

. . . similar for remaining actions . . . 2 QED

BY 21, 22, . . . DEF Next

SUFFICES steps represent backward chaining

trivial case UNCHANGED vars handled during decomposition Toolbox IDE helps with hierarchical decomposition

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 18 / 39

slide-34
SLIDE 34

Verifying Safety: Summing Up

Model checking for finite instances

◮ TLC computes reachable state graph, checking invariants on the fly ◮ for termination, the set of reachable states must be finite ◮ updates of state variables: v′ = e or v′ ∈ E

for “computable” expressions e, E (E must evaluate to finite set)

◮ finite bounds for quantifiers, set comprehensions, function domains Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 19 / 39

slide-35
SLIDE 35

Verifying Safety: Summing Up

Model checking for finite instances

◮ TLC computes reachable state graph, checking invariants on the fly ◮ for termination, the set of reachable states must be finite ◮ updates of state variables: v′ = e or v′ ∈ E

for “computable” expressions e, E (E must evaluate to finite set)

◮ finite bounds for quantifiers, set comprehensions, function domains

Theorem proving for arbitrary instances

◮ explicit, hierarchical proofs ◮ inductive invariant must be provided by the user ◮ main proof effort at action level: supported by automatic backends ◮ PTL decision procedure for simple temporal reasoning Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 19 / 39

slide-36
SLIDE 36

Outline

1

Modeling Systems in TLA+

2

System Verification Safety Properties Liveness Properties

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 20 / 39

slide-37
SLIDE 37

Verifying Liveness (1)

When all nodes are inactive, termination will be detected

◮ expressed in TLA+ using a leadsto-formula F G

= (F ⇒ G) Liveness

= (∀i ∈ Nodes : ¬active[i]) terminationDetected

THEOREM Spec ⇒ Liveness

◮ verification using TLC, for 5 nodes Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 21 / 39

slide-38
SLIDE 38

Verifying Liveness (1)

When all nodes are inactive, termination will be detected

◮ expressed in TLA+ using a leadsto-formula F G

= (F ⇒ G) Liveness

= (∀i ∈ Nodes : ¬active[i]) terminationDetected

THEOREM Spec ⇒ Liveness

◮ verification using TLC, for 5 nodes ◮ TLC produces a counter-example that ends in infinite stuttering ◮ [Next]vars allows for steps that do not change vars

Use fairness constraint to ensure progress

◮ fairness: action will be taken, provided it is long enough enabled

Spec

= Init ∧ [Next]vars ∧ WFvars(Next)

◮ don’t stutter indefinitely as long as some transition is possible Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 21 / 39

slide-39
SLIDE 39

Verifying Liveness (2)

Verify liveness property for redefined specification

◮ TLC verifies that the property is now satisfied Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 22 / 39

slide-40
SLIDE 40

Verifying Liveness (2)

Verify liveness property for redefined specification

◮ TLC verifies that the property is now satisfied

TLC also verifies an undesired liveness property AllNodesTerminateIfNoMessages

= [¬∃i ∈ Nodes : SendMsg(i)]vars ⇒ (∀i ∈ Nodes : ¬active[i])

◮ our fairness requirement is too strong! ◮ weaken fairness constraint: only ensure termination detection

System

= InitiateProbe ∨ ∃i ∈ Nodes \ {0} : PassToken(i) Spec

= Init ∧ [Next]vars ∧ WFvars(System)

◮ TLC now verifies liveness, but no longer termination

Too strong fairness constraints are a frequent specification error!

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 22 / 39

slide-41
SLIDE 41

Verifying Liveness: Summing Up

Specification of fair state machine Init ∧ [Next]v ∧ F

◮ F can be a conjunction of weak or strong fairness conditions

ENABLED A

= ∃var′ : A

(var′ contains all primed variables in A)

WFv(A)

= ( ENABLED Av ⇒ Av) SFv(A)

= ( ENABLED Av ⇒ Av)

◮ typically: Next is a disjunction, fairness assumed for some disjuncts ◮ don’t assume too strong fairness conditions: use TLC for validation Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 23 / 39

slide-42
SLIDE 42

Verifying Liveness: Summing Up

Specification of fair state machine Init ∧ [Next]v ∧ F

◮ F can be a conjunction of weak or strong fairness conditions

ENABLED A

= ∃var′ : A

(var′ contains all primed variables in A)

WFv(A)

= ( ENABLED Av ⇒ Av) SFv(A)

= ( ENABLED Av ⇒ Av)

◮ typically: Next is a disjunction, fairness assumed for some disjuncts ◮ don’t assume too strong fairness conditions: use TLC for validation

Verifying liveness through model checking

◮ temporal properties such as P, P, P Q and combinations ◮ verification using TLC is more complex, but still automatic

Verification through theorem proving

◮ not yet supported by TLAPS: needs quantified temporal logic Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 23 / 39

slide-43
SLIDE 43

Outline

1

Modeling Systems in TLA+

2

System Verification

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 24 / 39

slide-44
SLIDE 44

Languages for Describing Algorithms

TLA+: algorithms specified by logical formulas

◮ set-theoretical language for modeling data ◮ fair state machine specified in temporal logic Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 25 / 39

slide-45
SLIDE 45

Languages for Describing Algorithms

TLA+: algorithms specified by logical formulas

◮ set-theoretical language for modeling data ◮ fair state machine specified in temporal logic

Conventional descriptions of algorithms by pseudo-code

◮ familiar presentations, using imperative-style language ◮ (obviously) effective for conveying algorithmic ideas ◮ neither executable nor mathematically precise

PlusCal: pseudo-code flavor, but precise and more expressive

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 25 / 39

slide-46
SLIDE 46

PlusCal: Elements of an Algorithm Language

Language for modeling, not programming concurrent algorithms High-level abstractions, precise semantics Familiar control structure + non-determinism Concurrency: indicate grain of atomicity

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 26 / 39

slide-47
SLIDE 47

PlusCal: Elements of an Algorithm Language

Language for modeling, not programming concurrent algorithms High-level abstractions, precise semantics

◮ use TLA+ expressions for modeling data ◮ simple translation of PlusCal to TLA+ specification

Familiar control structure + non-determinism Concurrency: indicate grain of atomicity

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 26 / 39

slide-48
SLIDE 48

PlusCal: Elements of an Algorithm Language

Language for modeling, not programming concurrent algorithms High-level abstractions, precise semantics

◮ use TLA+ expressions for modeling data ◮ simple translation of PlusCal to TLA+ specification

Familiar control structure + non-determinism

◮ flavor of imperative language: assignment, loop, conditional, . . . ◮ special constructs for non-deterministic choice

either { A } or { B } with x ∈ S { A }

Concurrency: indicate grain of atomicity

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 26 / 39

slide-49
SLIDE 49

PlusCal: Elements of an Algorithm Language

Language for modeling, not programming concurrent algorithms High-level abstractions, precise semantics

◮ use TLA+ expressions for modeling data ◮ simple translation of PlusCal to TLA+ specification

Familiar control structure + non-determinism

◮ flavor of imperative language: assignment, loop, conditional, . . . ◮ special constructs for non-deterministic choice

either { A } or { B } with x ∈ S { A }

Concurrency: indicate grain of atomicity

◮ statements may be labeled

req: try[self] := TRUE;

◮ statements from one label to the next one are executed atomically Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 26 / 39

slide-50
SLIDE 50

Example: Alternating-Bit Protocol in PlusCal

MODULE AlternatingBit EXTENDS Naturals, Sequences CONSTANT Data

noData

= CHOOSE x : x / ∈ Data (****

  • -algorithm AlternatingBit {

variables sndC = , ackC = ; process (send = “sender”) . . . process (rcv = “receiver”) . . . process (err = “error”) . . . } ****)

\* BEGIN TRANSLATION \* END TRANSLATION

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 27 / 39

slide-51
SLIDE 51

Example: Alternating-Bit Protocol in PlusCal

MODULE AlternatingBit EXTENDS Naturals, Sequences

PlusCal algorithm embedded within TLA+ module

CONSTANT Data

noData

= CHOOSE x : x / ∈ Data (****

  • -algorithm AlternatingBit {

variables sndC = , ackC = ; process (send = “sender”) . . . process (rcv = “receiver”) . . . process (err = “error”) . . . } ****)

\* BEGIN TRANSLATION \* END TRANSLATION

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 27 / 39

slide-52
SLIDE 52

Example: Alternating-Bit Protocol in PlusCal

MODULE AlternatingBit EXTENDS Naturals, Sequences

PlusCal algorithm embedded within TLA+ module

CONSTANT Data

noData

= CHOOSE x : x / ∈ Data (****

  • -algorithm AlternatingBit {

variables sndC = , ackC = ;

global variable declarations

process (send = “sender”) . . . process (rcv = “receiver”) . . . process (err = “error”) . . . } ****)

\* BEGIN TRANSLATION \* END TRANSLATION

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 27 / 39

slide-53
SLIDE 53

Example: Alternating-Bit Protocol in PlusCal

MODULE AlternatingBit EXTENDS Naturals, Sequences

PlusCal algorithm embedded within TLA+ module

CONSTANT Data

noData

= CHOOSE x : x / ∈ Data (****

  • -algorithm AlternatingBit {

variables sndC = , ackC = ;

global variable declarations

process (send = “sender”) . . . process (rcv = “receiver”)

three parallel processes code to be filled in

. . . process (err = “error”) . . . } ****)

\* BEGIN TRANSLATION \* END TRANSLATION

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 27 / 39

slide-54
SLIDE 54

Example: Alternating-Bit Protocol in PlusCal

MODULE AlternatingBit EXTENDS Naturals, Sequences

PlusCal algorithm embedded within TLA+ module

CONSTANT Data

noData

= CHOOSE x : x / ∈ Data (****

  • -algorithm AlternatingBit {

variables sndC = , ackC = ;

global variable declarations

process (send = “sender”) . . . process (rcv = “receiver”)

three parallel processes code to be filled in

. . . process (err = “error”) . . . } ****)

\* BEGIN TRANSLATION PlusCal translator inserts TLA+ between these lines \* END TRANSLATION

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 27 / 39

slide-55
SLIDE 55

PlusCal Code of Processes

process (send = “sender”) variables sending = noData, sBit = 0, lastAck = 0; { s0: while (TRUE) { with (d ∈ Data) { sending := d; sBit := 1 − sBit }; s1: while (lastAck = sBit) { either { sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } } } }

\* end process send

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 28 / 39

slide-56
SLIDE 56

PlusCal Code of Processes

process (send = “sender”) variables sending = noData, sBit = 0, lastAck = 0; {

initialize local variables

s0: while (TRUE) { with (d ∈ Data) { sending := d; sBit := 1 − sBit }; s1: while (lastAck = sBit) { either { sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } } } }

\* end process send

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 28 / 39

slide-57
SLIDE 57

PlusCal Code of Processes

process (send = “sender”) variables sending = noData, sBit = 0, lastAck = 0; {

initialize local variables

s0: while (TRUE) { with (d ∈ Data) { sending := d; sBit := 1 − sBit };

prepare new data

s1: while (lastAck = sBit) { either { sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } } } }

\* end process send

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 28 / 39

slide-58
SLIDE 58

PlusCal Code of Processes

process (send = “sender”) variables sending = noData, sBit = 0, lastAck = 0; {

initialize local variables

s0: while (TRUE) { with (d ∈ Data) { sending := d; sBit := 1 − sBit };

prepare new data

s1: while (lastAck = sBit) { either {

while not acknowledged, either (re)send data or receive acknowledgement

sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } } } }

\* end process send

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 28 / 39

slide-59
SLIDE 59

PlusCal Code of Processes

process (send = “sender”) variables sending = noData, sBit = 0, lastAck = 0; {

initialize local variables

s0: while (TRUE) { with (d ∈ Data) { sending := d; sBit := 1 − sBit };

prepare new data

s1: while (lastAck = sBit) { either {

while not acknowledged, either (re)send data or receive acknowledgement

sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } } } }

\* end process send

Code of the two other processes is similar Familiar “look and feel” of imperative code

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 28 / 39

slide-60
SLIDE 60

Translation to TLA+: System State

TLA+ variables

◮ variables corresponding to those declared in PlusCal algorithm ◮ “program counter” stores current point of program execution

VARIABLES sndC, ackC, pc, sending, sBit, lastAck, rcvd, rBit

ProcSet

= {“sender”} ∪ {“receiver”} ∪ {“error”} Init

= ∧ sndC = ∧ ackC = ∧ sending = noData ∧ sBit = 0 ∧ lastAck = 0 ∧ rcvd = noData ∧ rBit = 0 ∧ pc = [self ∈ ProcSet → CASE self = “sender” → “s0” self = “receiver” → “r0” self = “error” → “e0”]

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 29 / 39

slide-61
SLIDE 61

Translation to TLA+: Transitions

s1

=

s1: while (lastAck = sBit) { either { sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } }

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 30 / 39

slide-62
SLIDE 62

Translation to TLA+: Transitions

s1

=

s1: while (lastAck = sBit) { either { sndC := Append(sndC, sending, sBit); } or { await (Len(ackC) > 0); lastAck := Head(ackC); ackC := Tail(ackC); } }

∧ pc[“sender”] = “s1” ∧ IF lastAck = sBit

THEN ∧ ∨ ∧ sndC′ = Append(sndC, sending, sBit)

∧ UNCHANGED ackC, lastAck ∨ ∧ Len(ackC) > 0 ∧ lastAck′ = Head(ackC) ∧ ackC′ = Tail(ackC) ∧ sndC′ = sndC ∧ pc′ = [pc EXCEPT ![“sender”] = “s1”]

ELSE ∧ pc′ = [pc EXCEPT ![“sender”] = “s0”]

∧ UNCHANGED sndC, ackC, lastAck ∧ UNCHANGED sending, sBit, rcvd, rBit

Fairly direct translation from PlusCal block to TLA+ action

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 30 / 39

slide-63
SLIDE 63

Translation to TLA+: Tying It All Together

Define the transition relation of the algorithm

◮ transition relation of process: disjunction of individual transitions ◮ overall next-state relation: disjunction of processes ◮ generalizes to multiple instances of same process type

send

= s0 ∨ s1 rcv

= r0 ∨ r1 err

= e0 Next

= send ∨ rcv ∨ err

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 31 / 39

slide-64
SLIDE 64

Translation to TLA+: Tying It All Together

Define the transition relation of the algorithm

◮ transition relation of process: disjunction of individual transitions ◮ overall next-state relation: disjunction of processes ◮ generalizes to multiple instances of same process type

send

= s0 ∨ s1 rcv

= r0 ∨ r1 err

= e0 Next

= send ∨ rcv ∨ err

Define the overall TLA+ specification

Spec

= Init ∧ [Next]vars

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 31 / 39

slide-65
SLIDE 65

Translation to TLA+: Tying It All Together

Define the transition relation of the algorithm

◮ transition relation of process: disjunction of individual transitions ◮ overall next-state relation: disjunction of processes ◮ generalizes to multiple instances of same process type

send

= s0 ∨ s1 rcv

= r0 ∨ r1 err

= e0 Next

= send ∨ rcv ∨ err

Define the overall TLA+ specification

Spec

= Init ∧ [Next]vars

Extension: fairness conditions per process or label

fair process (send = “sender”) Spec

= . . . ∧ WFvars(send) s1:+ while (lastAck = sBit) . . . Spec

= . . . ∧ SFvars(s1)

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 31 / 39

slide-66
SLIDE 66

PlusCal: Summing Up

A gateway drug for programmers

(C. Newcombe, Amazon)

◮ retain familiar look and feel of pseudo-code ◮ abstractness and expressiveness through embedded TLA+ ◮ precision through simple translation to TLA+ ◮ formal verification via standard TLA+ tool set Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 32 / 39

slide-67
SLIDE 67

PlusCal: Summing Up

A gateway drug for programmers

(C. Newcombe, Amazon)

◮ retain familiar look and feel of pseudo-code ◮ abstractness and expressiveness through embedded TLA+ ◮ precision through simple translation to TLA+ ◮ formal verification via standard TLA+ tool set

Simplicity of translation induces some limitations

◮ single level of processes can make modeling unnatural ◮ translation dictates rules on where labels must and cannot go ◮ properties must be written in TLA+ (probably a feature) Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 32 / 39

slide-68
SLIDE 68

PlusCal: Summing Up

A gateway drug for programmers

(C. Newcombe, Amazon)

◮ retain familiar look and feel of pseudo-code ◮ abstractness and expressiveness through embedded TLA+ ◮ precision through simple translation to TLA+ ◮ formal verification via standard TLA+ tool set

Simplicity of translation induces some limitations

◮ single level of processes can make modeling unnatural ◮ translation dictates rules on where labels must and cannot go ◮ properties must be written in TLA+ (probably a feature)

Algorithm language: much better than pseudo-code

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 32 / 39

slide-69
SLIDE 69

Outline

1

Modeling Systems in TLA+

2

System Verification

3

The PlusCal Algorithm Language

4

Refinement in TLA+

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 33 / 39

slide-70
SLIDE 70

Refinement of System Specifications

Refining (implementing) specification Spec by Impl

◮ every behavior allowed by Impl is a possible execution of Spec ◮ TLA+ formalization

Impl ⇒ Spec

◮ systems and properties represented as formulas Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 34 / 39

slide-71
SLIDE 71

Refinement of System Specifications

Refining (implementing) specification Spec by Impl

◮ every behavior allowed by Impl is a possible execution of Spec ◮ TLA+ formalization

Impl ⇒ Spec

◮ systems and properties represented as formulas

Problem: Impl will include detail not present in Spec

◮ additional variables + extra transitions ◮ many steps of Impl will be meaningless for Spec Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 34 / 39

slide-72
SLIDE 72

Refinement of System Specifications

Refining (implementing) specification Spec by Impl

◮ every behavior allowed by Impl is a possible execution of Spec ◮ TLA+ formalization

Impl ⇒ Spec

◮ systems and properties represented as formulas

Problem: Impl will include detail not present in Spec

◮ additional variables + extra transitions ◮ many steps of Impl will be meaningless for Spec

Stuttering invariance to the rescue!

◮ extra transitions of Impl stutter w.r.t. the variables in Spec ◮ stuttering steps are allowed by next-state relation [Next]vars ◮ infinite stuttering ruled out by fairness conditions Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 34 / 39

slide-73
SLIDE 73

Example: Specifying Data Transmission

MODULE Transmission CONSTANT Data

noData

= CHOOSE x : x / ∈ Data

VARIABLES sending, rcvd

Init

= sending = noData ∧ rcvd = noData Send

= ∧ rcvd = sending ∧ sending′ ∈ Data ∧ rcvd′ = rcvd Receive

= ∧ rcvd = sending ∧ rcvd′ = sending ∧ sending′ = sending vars

= sending, rcvd Spec

= Init ∧ [Send ∨ Receive]vars

Simple handshake protocol specified as a state machine

◮ new data may be sent when previous one has been received ◮ no explicit mechanism for transferring data Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 35 / 39

slide-74
SLIDE 74

Implementation Through Alternating Bit Protocol

MODULE AlternatingBit

. . .

INSTANCE Transmission THEOREM Spec ⇒ Transmission!Spec

Implementation checked by TLC

◮ for fixed instances of Data and constraints on channel size ◮ exercise: extend this to fair data transmission

hint: ensure that the channels do not lose all data or acknowledgements

Stuttering invariance is essential here

◮ most transitions of alternating bit protocol stutter on sending, rcvd Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 36 / 39

slide-75
SLIDE 75

Information Hiding

TLA+ specifications describe state machines

◮ often introduce “implementation detail” for controlling transitions ◮ example: program counter generated by PlusCal translator ◮ internal detail should be hidden from “interface” Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 37 / 39

slide-76
SLIDE 76

Information Hiding

TLA+ specifications describe state machines

◮ often introduce “implementation detail” for controlling transitions ◮ example: program counter generated by PlusCal translator ◮ internal detail should be hidden from “interface”

In logic, hiding corresponds to existential quantification

Inner

= Init ∧ [Next]vars ∧ F Spec

= ∃ ∃ ∃ ∃ ∃ ∃ x : Inner

◮ behaves like inner specification, but with variables x hidden Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 37 / 39

slide-77
SLIDE 77

Information Hiding

TLA+ specifications describe state machines

◮ often introduce “implementation detail” for controlling transitions ◮ example: program counter generated by PlusCal translator ◮ internal detail should be hidden from “interface”

In logic, hiding corresponds to existential quantification

Inner

= Init ∧ [Next]vars ∧ F Spec

= ∃ ∃ ∃ ∃ ∃ ∃ x : Inner

◮ behaves like inner specification, but with variables x hidden

Refinement under information hiding

◮ prove Impl ⇒ Inner[t/x] for showing Impl ⇒ Spec ◮ refinement mapping t : computed from implementation variables Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 37 / 39

slide-78
SLIDE 78

Summing Up

TLA+: Specify systems in logic, from first principles

◮ describe system behavior at appropriate level of abstraction ◮ mathematical logic is flexible and expressive ◮ set theory plus state machine plus temporal logic ◮ no formal distinction between systems and properties ◮ experience shows that this approach scales to practical systems

Support tools

◮ TLA+ Toolbox: editor, syntax/semantic analysis, pretty printer ◮ TLC: explicit-state model checker, checkpointing, parallelization ◮ TLAPS: interactive proof platform with powerful theorem provers ◮ PlusCal translator for generating TLA+ specification

Community: Google group, this workshop!

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 38 / 39

slide-79
SLIDE 79

Going Further

The TLA+ Web page

http://research.microsoft.com/en-us/um/people/lamport/tla/tla.html

Detailed presentations

The Hyperbook Specifying Systems

Stephan Merz (INRIA Nancy) TLA+ Tutorial Toulouse, June 2014 39 / 39