Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is...

95
Event-B Course 4. File Transfer Protocol Jean-Raymond Abrial September-October-November 2011

Transcript of Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is...

Page 1: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Event-B Course

4. File Transfer Protocol

Jean-Raymond Abrial

September-October-November 2011

Page 2: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Purpose of this Lecture 1

- To introduce another example: the file transfer protocol

- To present a number of additional mathematical conventions

- To slighly enlarge the usage of the Proof Obligation Rules

- Example studied in many places, in particular in the following book

- L. Lamport Specifying Systems: The TLA+ Language and Tools

for Hardware and Software Engineers Addison-Wesley 1999

1

Page 3: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Requirement Document (1) 2

- A file is to be transfered from a Sender to a Receiver

- On the Sender’s side the file is called f

- On the Receiver’s side the file is called g

- At the beginning of the protocol, g is supposed to be empty

- At the end of the protocol, g should be equal to f

2

Page 4: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Requirement Document (2) 3

The protocol ensures the copy of a file fromone site to another one FUN-1

The file is supposed to be made of a sequenceof items FUN-2

The file is sent piece by piece betweenthe two sites FUN-3

3

Page 5: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Modeling Approach 4

- Our approach at modeling is one of an external observer

- The observer “sees” the state space first from very far away

- He then approaches the future system and sees more details

- As he approaches he also sees more things happening

4

Page 6: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Refinement Strategy 5

- Initial model: The file is transmitted in one shot (FUN1 and FUN2)

- First refinement: The file is transmitted gradually (FUN3)

- Second refinement: The two agents are separated

- Third refinement: Towards an implementation

5

Page 7: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Sender and the Receiver: a First View 6

a

b

c

RECEIVER

g

a

b

c

f

SENDER

a

b

c

RECEIVER

g

f

SENDER

INITIAL SITUATION FINAL SITUATION

6

Page 8: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

File transfer. The constant part of the state: n and f 7

1

n

a

f

b

c

7

Page 9: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Initial State: Constant Part 8

sets: D

constants: nf

axm0 1: 0 < n

axm0 2: f ∈ 1 .. n→D

- The carrier set D makes this development generic

8

Page 10: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Initial State: Dynamic Part 9

variables: gb

inv0 1: g ∈ 1 .. n 7→D

inv0 2: b = FALSE ⇒ g = ∅

inv0 3: b = TRUE ⇒ g = f

b = TRUE means protocol is finished

9

Page 11: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Reminder of Mathematical Conventions (1) 10

x ∈ S set membership operator

N set of natural numbers: {0, 1, 2, 3, . . .}

a .. binterval from a to b: {a, a + 1, . . . , b}

(empty when b < a)

a 7→ b pair constructing operator

S × T Cartesian product operator

S ⊆ T set inclusion operator

P(S) power set operator

10

Page 12: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Reminder of Mathematical Conventions (2) 11

S↔ T set of binary relations from S to T

S→ T set of total functions from S to T

S 7→ T set of partial functions from S to T

dom(r) domain of a relation r

ran(r) range of a relation r

11

Page 13: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Binary Relation r from a Set A to a Set B 12

A B

a3a2

a6a7

b1

b3

b4

b5

b6

b2

a5

a1

a4

r

12

Page 14: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Partial Function F from a Set A to a Set B 13

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F = {a1 7→ b2, a3 7→ b4, a5 7→ b2, a7 7→ b6}

dom (F ) = {a1, a3, a5, a7}

ran (F ) = {b2, b4, b6}

13

Page 15: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Total Function F from a Set A to a Set B 14

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

dom (F ) = A

14

Page 16: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

File Transfer Protocol Events: init and final 15

initg := ∅b := FALSE

finalwhenb = FALSE

theng := fb := TRUE

end

15

Page 17: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Summary of the File Transfer Protocol Initial Model 16

sets: D constants: nf

axm0 1: 0 < n

axm0 2: f ∈ 1 .. n→D

variables: gb

inv0 1: g ∈ 1 .. n 7→D

inv0 2: b = FALSE ⇒ g = ∅

inv0 3: b = TRUE ⇒ g = f

initg := ∅b := FALSE

finalwhenb = FALSE

theng := fb := TRUE

end

16

Page 18: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What is to be Proved 17

- Event init establishes invariants inv0 1 to inv0 3 (Rule INV)

- Event final preserves invariants inv0 1 to inv0 3 (Rule INV)

inv0 1: g ∈ 1 .. n 7→D

inv0 2: b = FALSE ⇒ g = ∅

inv0 3: b = TRUE ⇒ g = f

initg := ∅b := FALSE

finalwhen

b = FALSEthen

g := fb := TRUE

end

17

Page 19: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Invariant Establishment Proof Rule 18

- For the init event in the initial model

Properties` INV

Modified Invariant

18

Page 20: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event init (1) 19

- Applying Rule INV to invariant inv0 1

initg := ∅b := FALSE

inv0 1: g ∈ 1 .. n 7→D

axm0 1axm0 2`

modified inv0 1

0 < nf ∈ 1 .. n→D`∅ ∈ 1 .. n 7→D

inv0 1 / INV

19

Page 21: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event init (2) 20

- Applying Rule INV to invariant inv0 2

initg := ∅b := FALSE

inv0 2: b = FALSE ⇒ g = ∅

axm0 1axm0 2`

modified inv0 2

0 < nf ∈ 1 .. n→D`FALSE = FALSE ⇒ ∅ = ∅

inv0 2 / INV

20

Page 22: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event init (3) 21

- Applying Rule INV to invariant inv0 3

initg := ∅b := FALSE

inv0 3: b = TRUE ⇒ g = f

axm0 1axm0 2`

modified inv0 3

0 < nf ∈ 1 .. n→D`FALSE = TRUE ⇒ ∅ = f

inv0 3 / INV

21

Page 23: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Invariant Preservation Proof Rule 22

- For other events in the initial model

PropertiesInvariantsGuards of the event INV`

Modified Invariant

22

Page 24: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event final (1) 23

- Applying Rule INV

finalwhen

b = FALSEthen

g := fb := TRUE

end

axm0 1axm0 2inv0 1. . .grd`

modified inv0 1

0 < nf ∈ 1 .. n→Dg ∈ 1 .. n 7→D. . .b = FALSE`f ∈ 1 .. n 7→D

final / inv0 1 / INV

23

Page 25: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event final (2) 24

- Applying Rule INV

finalwhenb = FALSE

theng := fb := TRUE

end

axm0 1axm0 2inv0 2. . .grd`

modified inv0 2

0 < nf ∈ 1 .. n→Db = FALSE ⇒ g = ∅. . .b = FALSE`TRUE = FALSE ⇒ f = ∅

final / inv0 2 / INV

24

Page 26: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Proof of Event final (3) 25

- Applying Rule INV

finalwhenb = FALSE

theng := fb := TRUE

end

axm0 1axm0 2inv0 3. . .grd`

modified inv0 3

0 < nf ∈ 1 .. n→Db = TRUE ⇒ g = f. . .b = FALSE`TRUE = TRUE ⇒ f = f

final / inv0 3 / INV

25

Page 27: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Refinement Strategy 26

- Initial model: The file is transmitted in one shot (FUN1 and FUN2)

- First refinement: The file is transmitted gradually (FUN3)

- Second refinement: The two agents are separated

- Third refinement: Towards an implementation

26

Page 28: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

First Refinement 27

- The observer comes closer to the future system

- So far he was just seeing the beginning and the end

- Now the observer will see some intermediate moves

- He sees the file being gradually transfered from Sender to Receiver

- But he still has a partial view

27

Page 29: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What the Observer has Seen so far 28

init

final_copy

28

Page 30: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What the Observer will now See 29

init

receive

final_copy

receive

receive

init

final_copy

A new event is introduced: receive

29

Page 31: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

File transfer. Event receive 30

b

a

b

c

f

n

1r

a

b

c

b

a

f

n

1

rc

b

a

f

a

r

n

1

c

b

a

f

a

r

c

n

1

h h h h

receive receive receive

- The new variable r lies within the interval 1 .. n+1

- The new variable h is equal to f restricted to its r − 1 first values

30

Page 32: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

First Refinement of the Protocol Model: the State 31

- Introducing additional variables h and r

- Variable g disappears

variables: bhr

inv1 1: r ∈ 1 .. n + 1

inv1 2: h = (1 .. r − 1) � f

inv1 3: b = TRUE ⇒ r = n + 1

- h is defined to be the domain restriction of f to 1 .. r − 1

31

Page 33: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

More Mathematical Conventions: Restrictions 32

s � r domain restriction operator

s �− r domain subtraction operator

r � t range restriction operator

r �− t range subtraction operator

32

Page 34: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Domain Restriction Operator 33

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

{a3, a7} � F

33

Page 35: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Domain Subtraction Operator 34

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

{a3, a7} �− F

34

Page 36: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Range Restriction Operator 35

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F � {b2, b4}

35

Page 37: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Range Subtraction Operator 36

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F �− {b2}

36

Page 38: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Events 37

inith := ∅r := 1b := FALSE

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

finalwhenr = n + 1b = FALSE

thenb := TRUE

end

37

Page 39: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What is to be Proved 38

- Event init refines its abstraction

- Event final refines its abstraction

- Event receive refines skip

- Event receive does not diverge

- Relative deadlock freeness

38

Page 40: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

First Pathology: Divergence 39

init

receive

receive

init

final

receive receive receive . . .

final

39

Page 41: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

More to be Proved: No Divergence 40

- No divergence of new event receive (rules NAT and VAR)

variant1: n + 1− r

- This variant must be decreased by the new event:

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

40

Page 42: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Refinement Rules: Non-divergence of New Events 41

- For new events only

Properties of the constantsAbstract invariantsConcrete invariantsConcrete guards of a new event NAT`

Variant ∈ N

41

Page 43: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

No Divergence 42

- Applying rule NAT

. . .inv1 1. . .`variant belongs to N

. . .r ∈ 1 .. n + 1. . .`n + 1− r ∈ N

42

Page 44: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Non-divergence of New Events 43

- For new events only

Properties of the constantsAbstract invariantsConcrete invariantsConcrete guards of a new event VAR`

Modified variant < Variant

43

Page 45: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

No Divergence 44

- Applying rule VAR

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

. . .`variant is decreased

. . .`n + 1− (r + 1) < n + 1− r

44

Page 46: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Second Pathology: Early Deadlock 45

init

receive

receive

init

final

final

45

Page 47: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Refinement Rules: Relative Deadlock Freeness 46

- Global proof rule

Properties of the constantsAbstract invariantsConcrete invariantsDisjunction of abstract guards DLF`

Disjunction of concrete guards

46

Page 48: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Abstract and Concrete Events 47

- Abstract Events

finalwhenb = FALSE

theng := fb := TRUE

end

- Concrete Events

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

finalwhenb = FALSEr = n + 1

thenb := TRUE

end

47

Page 49: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

More to be Proved: No Early Deadlock 48

- Applying rule DLF

. . .inv1 1disj. of abs. guards`disj. of conc. guards

. . .r ∈ 1 .. n + 1b = FALSE`r ≤ n ∨ (b = FALSE ∧ r = n + 1)

48

Page 50: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Summary of First Refinement: the State and Events 49

variables: bhr

inv1 1: r ∈ 1 .. n + 1

inv1 2: h = (1 .. r − 1)� f

inv1 3: b = TRUE ⇒ r = n + 1

variant1: n + 1− r

initb := FALSEh := ∅r := 1

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

finalwhenb = FALSEr = n + 1

thenb := TRUE

end

- This model is not satisfactory: event receive accesses file f

49

Page 51: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Development Approach 50

- Initial model: The file is transmitted in one shot (FUN1 and FUN2)

- First refinement: The file is transmitted gradually (FUN3)

- Second refinement: The two agents are separated

- Third refinement: Towards an implementation

50

Page 52: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What the Observer could see of the Initial Model 51

init

final_copy

51

Page 53: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What the Observer could see of first Refinement 52

init

receive

final_copy

receive

receive

init

final_copy

52

Page 54: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What the Observer will now See 53

init

receive

final_copy

receive

receive

init

final_copy

send

send

send

receive

receive

receive

init

final_copy

53

Page 55: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A More Accurate Version (1) 54

a

b

c

RECEIVER

a

r

SENDER RECEIVER

s

r

send

s a

54

Page 56: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A More Accurate Version (2) 55

a

b

c

a

b

c

SENDER RECEIVER

a

s

SENDER RECEIVER

receive

s

r

r

55

Page 57: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Initial Situation 56

a

b

c

f

d

r

h

n

s

56

Page 58: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Send 57

a

b

c c

b

a

f f

s

n

d d

a

r

hh

r

n

s

57

Page 59: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Receive 58

a

b

c c c

b b

aa

f f f

s

n

s

n

d d d

a a

h

r

r

a

hh

r

n

s

58

Page 60: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Send 59

a

b

c c c

b b

aa

f f f

s

n

s

n

d d d

a a

h

r

r

a

f

a

b

csn

d

b

h

a

r

hh

r

n

s

59

Page 61: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Receive 60

a

b

c c

b

a

f f

d d

hh

b

a

r

a

b

b

r

n sn s

60

Page 62: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Send 61

a

b

c c c

b b

aa

f f f

n

d d d

c

h

a

hh

b

a

r

a

b

b

r

n s

r

b

s

n s

61

Page 63: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Receive 62

a

b

c c c

b b

aa

f f f

n

d d d

c

h

a

f

a

b

c

d

h

a

hh

b

a

r

a

b

b

r

n s

r

b

n

s

b

c

r

c

s

n s

62

Page 64: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Sender and Receiver 63

Data Channel

Ack. Channel

ReceiverSender

s, d

r

63

Page 65: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Second Refinement: the State 64

- We introduce an additional variable s, and a data item d

variables: bhrsd

inv2 1: s ≤ n + 1

inv2 2: s ∈ r .. r + 1

inv2 3: s = r + 1 ⇒ d = f(r)

64

Page 66: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Second Refinement: the Events 65

initb := FALSEh := ∅r := 1s := 1d :∈ D

sendwhens = rs 6= n + 1

thend := f(s)s := s + 1

end

receivewhens = r + 1

thenh := h ∪ {r 7→ d}r := r + 1

end

finalwhenb = FALSEr = n + 1

thenb := TRUE

end

65

Page 67: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What is to be Proved 66

- Event init refines its abstraction

- Event final refines its abstraction

- Event receive refines its abstraction

- Event send refines skip

- Event send does not diverge

- Relative deadlock freeness

66

Page 68: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Refinement Strategy 67

- Initial model: The file is transmitted in one shot (FUN1 and FUN2)

- First refinement: The file is transmitted gradually (FUN3)

- Second refinement: The two agents are separated

- Third refinement: Towards an implementation

67

Page 69: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Some Ideas for a Third Refinement? 68

sendwhens = rs 6= n + 1

thend := f(s)s := s + 1

end

receivewhens = r + 1

thenh := h ∪ {r 7→ d}r := r + 1

end

inv2 2: s ∈ r .. r + 1

68

Page 70: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Some Ideas for a Third Refinement? 69

sendwhens = rs 6= n + 1

thend := f(s)s := s + 1

end

receivewhens = r + 1

thenh := h ∪ {r 7→ d}r := r + 1

end

inv2 2: s ∈ r .. r + 1

- In order to compare r and s, it is sufficient to compare their parities

69

Page 71: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Third Refinement: Sending Parities (1) 70

a

b

c

RECEIVER

a

r

SENDER RECEIVER

s

r

send

parity(s) a

70

Page 72: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Third Refinement: Sending Parities (2) 71

a

b

c

a

b

c

SENDER RECEIVER

a

s

SENDER RECEIVER

receive

s

r

parity(r)

71

Page 73: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

About Parities 72

axm3 1: parity ∈ N→{0, 1}

axm3 2: parity(0) = 0

axm3 3: ∀x · x ∈ N ⇒ parity(x + 1) = 1− parity(x)

thm3 1: ∀x, y · x ∈ Ny ∈ Nx ∈ y .. y + 1parity(x) = parity(y)⇒x = y

72

Page 74: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Third Refinement: the State 73

variables: bhrsdpq

inv3 1: p = parity(s)

inv3 2: q = parity(r)

73

Page 75: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Third Refinement: the Events 74

initb := FALSEh := ∅r := 1s := 1d :∈ Dp := 1q := 1

finalwhenb = FALSEr = n + 1

thenb := TRUE

end

sendwhenp = qs 6= n + 1

thend := f(s)s := s + 1p := 1− p

end

receivewhenp 6= q

thenh := h ∪ {r 7→ d}r := r + 1q := 1− q

end

74

Page 76: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Third Refinement: Proofs 75

- The proofs are left as exercises

75

Page 77: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Summary of Proofs with the Tool 76

Total Interactive

Initial Model 6 0

1st Refinement 13 0

2nd Refinement 15 0

3rd Refinement 8 5

Total 42 5

76

Page 78: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

What we Have Learned in this Lecture 77

- More mathematical conventions

- How to write a model

- What kind of things we have to prove

- How the proof can help finding invariants

- Many things can be done by tools

- A small theory of parities

77

Page 79: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Gradual Observation of the Intended System 78

init

receive

final_copy

receive

receive

init

final_copy

send

send

send

receive

receive

receive

init

final_copy

78

Page 80: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Reminder of Mathematical Conventions (1) 79

x ∈ S Set membership operator

N set of Natural Numbers: {0, 1, 2, 3, . . .}

a .. bInterval from a to b: {a, a + 1, . . . , b}

(empty when b < a)

a 7→ b pair constructing operator

S × T Cartesian product operator

S ⊆ T set inclusion operator

P(S) power set operator

79

Page 81: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Reminder of Mathematical Conventions (2) 80

S↔ T Set of binary relations from S to T

S→ T Set of total functions from S to T

S 7→ T Set of partial functions from S to T

dom(r) Domain of a relation r

ran(r) Range of a relation r

80

Page 82: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Reminder of Mathematical Conventions (3) 81

s � r domain restriction operator

s �− r domain subtraction operator

r � t range restriction operator

r �− t range subtraction operator

81

Page 83: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Binary Relation r from a Set A to a Set B 82

A B

a3a2

a6a7

b1

b3

b4

b5

b6

b2

a5

a1

a4

r

82

Page 84: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Partial Function F from a Set A to a Set B 83

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F = {a1 7→ b2, a3 7→ b4, a5 7→ b2, a7 7→ b6}

dom (F ) = {a1, a3, a5, a7}

ran (F ) = {b2, b4, b6}

83

Page 85: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

A Total Function F from a Set A to a Set B 84

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

dom (F ) = A

84

Page 86: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Domain Restriction Operator 85

A B

a3a2

a6

a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

{a3, a7} � F

85

Page 87: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Domain Subtraction Operator 86

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

{a3, a7} �− F

86

Page 88: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Range Restriction Operator 87

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F � {b2, b4}

87

Page 89: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

The Range Subtraction Operator 88

A B

a3a2

a6a7

b1

F

b3

b4

b5

b6

b2

a5

a1

a4

F �− {b2}

88

Page 90: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Structure of a Model 89

- List of Sets (identifiers)

- List of Constants (identifiers)

- List of Axioms (predicates built on sets and constants)

- List of Variables (identifiers)

- List of Invariants (predicates built on sets, constants, and variables)

- List of Events

89

Page 91: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Summary of the File Transfer Protocol Initial Model 90

sets: D constants: nf

axm0 1: 0 < n

axm0 2: f ∈ 1 .. n→D

variables: gb

inv0 1: g ∈ 1 .. n 7→D

inv0 2: b = FALSE ⇒ g = ∅

inv0 3: b = TRUE ⇒ g = f

initg := ∅b := FALSE

finalwhenb = FALSE

theng := fb := TRUE

end

90

Page 92: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Summary of First Refinement: the State and Events 91

variables: bhr

inv1 1: r ∈ 1 .. n + 1

inv1 2: h = (1 .. r − 1) � f

inv1 3: b = TRUE ⇒ r = n + 1

thm1 1: b = TRUE ⇒ h = g

variant1: n + 1− r

initb := FALSEh := ∅r := 1

receivewhenr ≤ n

thenh := h ∪ {r 7→ f(r)}r := r + 1

end

finalwhenb = FALSEr = n + 1

thenb := TRUE

end

- Variable g has disappeared: it is not satisfactory

91

Page 93: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Introducing Anticipated Events 92

- We want to keep the variable g in the first refinement

- That seems impossible since event receive has to refine skip

- For this, we introduce the notion of anticipated event

- Such an event will be later proved to be conveergent

92

Page 94: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

Initial Model with an anticipated event 93

sets: D constants: nf

axm0 1: 0 < n

axm0 2: f ∈ 1 .. n→D

variables: g inv0 1: g ∈ 1 .. n 7→D

initg := ∅

receivestatus

anticipatedwheng 6= f

theng :∈ 1 .. n 7→D

end

finalwheng = f

thenskip

end

- Event receive is highly non-deterministic.

- Notice the event final

- Variable b is not useful anymore

93

Page 95: Event-B Course 4. File Transfer Protocol · Refinement Strategy 5 - Initial model: The file is transmitted in one shot (FUN1 and FUN2) ... A Binary Relation r from a Set A to a

First Refinement 94

variables: gr

inv1 1: r ∈ 1 .. n + 1

inv1 2: g = (1 .. r − 1)� f

variant1: n + 1− r

initg := ∅r := 1

receivestatus

convergentwhen

r 6= n + 1then

g(r) := f(r)r := r + 1

end

finalwhen

r = n + 1then

skipend

Note: the event receive works now with variable g

94