Top Banner
Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel Vienna July 2014
87

Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Dec 31, 2015

Download

Documents

Geoffrey Palmer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Charts and nets:States, Messages, and Events

Tony Hoare

In honour of David HarelVienna July 2014

Page 2: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Eventsbasic actions and method calls

that occur during execution of a program

Page 3: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Event labelsbasic actions and method calls

that occur in the text of the program

x := 9

c ! 9

c ? 9

Page 4: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Space and Time

time

space

where and when an event occurs

Page 5: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Spatial Boundaries

time

space

horizontal, separating concurrent activities

Page 6: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Temporal Boundaries

time

space

vertical, separating sequential activities

Page 7: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Dependenciesan event at the head of an arrowdepends on the event at the tail

Page 8: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Rules for arrows

• do not cross a time-boundary backwards• Local arrows are drawn horizontal• They must not cross space boundaries• Non-local arrows are not horizontal• They must not close a cycle.

Page 9: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Object values

x

y

39

7 4

9 70

7

Page 10: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Object identifiers

x

y

Page 11: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Object values

x

y

39

7 4

9 70

7

Page 12: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Message identifiers

c[26]c[25]

c!

c?

Page 13: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

The State of Memory

• is a map from the identifier of each linewhich crosses a given temporal boundaryto the value which labels the crossing arrow.

Page 14: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

State of memory

x

y

9

0

{ x = 9, y = 0}

Page 15: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

OBJECTS

Page 16: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Concurrent object behaviour.

• A trace of behaviour of an object is an event chart, containing all the events in which it has engaged, connected by dependency arrows.

• A class is the set of all possible traces of objects of the class (and related objects.

Page 17: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A local object

boxes represent actions of the objectarrows connect successive actions

Page 18: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A local object

the red arrows connect actions in the same thread protected from interference by other threads

Page 19: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A shared object

blue arrows connect actionswhich may occur in a different threads(or they may occur in the same thread).

Page 20: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

An exclusion semaphore

is released by the same thread that most recently acquired it. A released semaphoremay be acquired by any thread.

Page 21: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

with labels

The boxes are labelled to indicate the nature of the actions: enter or exit

exit exitenter enter

Page 22: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Object allocation

has one red output arrowand no red input arrow

alloc

Page 23: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Object disposal

has one red input arrow and no red output arrow

disp

Page 24: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A Communication channelsingle input port and single output port

Page 25: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A shared output portallows multiple outputting threads

Page 26: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

with labels

ad

to indicate that the value communicated is the same at both ends of the channel

dispose

disp

alloc

alloc

!3 !7

?3 ?7

=7=3

Page 27: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

with a single buffer

ad

which is passed back to outputter for refilling

disp

disp

alloc

alloc

!3 !7

?3 ?7

=7=3

Page 28: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

a synchronous channel

ad

no buffering at all. input and output are simultaneous

disp

disp

alloc

alloc

!3 !7

?3 ?7

=7=3

Page 29: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

a re-ordering channel…

ad

allows crossing of arrows

disp

disp

alloc

alloc

!3 !7

?7 ?3

=7

=3

Page 30: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

…with loss of message

ad disp

disp

alloc

alloc

!7 !3

?7

=7

Page 31: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Diagrams in Debugging

• display a trace of concurrent execution• with slices for selected objects• keyed to the text of the program• with dependencies to show sources of error• and consequences of its correction.

Page 32: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

PROGRAMS

A program (or an object class) denotes the set of all possible diagrams of execution, on any computer system running in any environment.

Page 33: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Spatial Boundaries

time

space

horizontal, separating concurrent activities

Page 34: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Temporal Boundaries

time

space

vertical, separating sequential activities

Page 35: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Concurrent Compositionof event charts p, q, r

p

q

r

(p ‖ q)‖ r = p ‖ (q ‖ r)

Page 36: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Sequential composition

(p ; q) ; r = p ; (q ; r)

p q r

Page 37: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Rules for arrows

• do not cross a time-boundary backwards• Local arrows are drawn horizontal• They must not cross space boundaries• Non-local arrows are not horizontal• They must not close a cycle.

Page 38: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A Program Error (race)

Page 39: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Another Program Error (deadlock)

p

Page 40: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Top T

• stands for a trace with programming error• T;p = p;T = p||T

otherwise…..

Page 41: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Operators

• p;q splits the trace across time, into parts that can be executed at different instants.

• p q splits it across space, into parts that can ∥be executed in different places.

• I describes doing nothing

Page 42: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Sequential Composition

• p;q = p+q if q x p contained in neg(dep*)

• p||q = p+q if p x q contained in neg(red u conv(red))n neg(dep* n conv(dep*)) }

= T otherwise• I = { }

Page 43: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Trace algebra

Page 44: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Refinement ≼

• ≼ is a partial order• Covariance: p q≼ implies

– p;r q;r≼– r;p r;q≼– p r q r∥ ≼ ∥

Page 45: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Exchange Axiom

(p q) ; (p’ q’) (p;p’) (q;q’)∥ ∥ ≼ ∥

p’

q’

p

q; ≼

p’

q’

p

q ;

;

note the self-duality of the law

Page 46: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

• p q means that p and q perform all the ≼same actions, but p performs more pairs of them sequentially, and q performs more pairs concurrently.

As a result,…• p is more determinate, q is more abstract

Page 47: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Interleaving

• (p q) ; (p’ q’)∥ ∥ ≼ (p;p’) (q;q’)∥

• LHS is an interleaving implementation of the more general concurrency of RHS

• It is the special case when the two RHS ‘;’s happen to be simultaneous

Page 48: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Frame Laws

• (p q) ; (p’ q’)∥ ∥ ≼ (p;p’) (q;q’)∥

• Theorems (frame): 1. (p q) ; q’ p (q;q’)∥ ≼ ∥2. p;(p’ q’) (p;p’) q’∥ ≼ ∥3. p;q’ p q’≼ ∥ and q;p’ p’ q≼ ∥

Proof: substitute for variables of the axiom that are omitted in the theorem

Page 49: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Example

abcd xyzw∥(a;bcd) (xy;zw)∥(a xy) ; (∥ bcd zw)∥

(a x;y) ; (∥ b;cd zw)∥(a x);y ; (∥ b zw) ; ∥ cd

xayzbwcd

Page 50: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A Program

• is the set of all traces that it can evokewhen executed on any computer systemand in any interacting environment.

P;Q = {p;q | p e P & q e Q }P||Q = {p||q | p e P & q e Q }P < Q = all p e P . exists q e Q . p < q

All our laws for ; and || are preserved for sets

Page 51: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

The Hoare triple

• Definition: {p} q {r} = p;q r≼– If p describes what has happened so far,– and q is then executed to completion,– the trace of overall execution will be r.

Page 52: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

The Milner transition

• Definition: r >- q -> p = q;p r≼– i.e., the dual of {p} q {r}– r may be executed by first executing q , with p as continuation for later execution.– (maybe there are other ways of executing r)

• Tautology: (q ; p) >- q -> p– (CCS prefix rule)Proof: from reflexivity: q;p q;p ≼

Page 53: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Modularity rules

• {p} q {r} {p’} q’ {r’} Logic {p p’} q q’∥ ∥ {r r’}∥

• r >–q-> p r >-q’-> p’ CCS(r r’) >-(q q’)-> (p p’)∥ ∥ ∥

• In CCS, the rule is restricted – by requiring synchronisation of p and p’ , – e.g. input and output on the same channel.

Page 54: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

O’Hearn Frame Rule

{p} q {r}{p f} q {r f}∥ ∥

– adapts a triple to a concurrent environment f– much better than the Hoare rule of adaptation:

{p} q {r} with side-condition

{p&f} q {r&f} – that no variable of f is assigned by q

Page 55: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Milner Frame Rule

r >–q-> p(r f) >–q-> (p f)∥ ∥

– a step q that is possible for a single thread r is still possible when r is executed concurrently with f , which does not change when q happens

Page 56: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Sequential composition

{p} q {s} {s} q’ {r} {p} q;q’ {r}

is equivalent to

p;(q;q’) (p;q);q’≼

Page 57: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Sequential composition

r >–q’-> s s >–q-> p r >–(q’;q)-> pis equivalent to

(q’;q);p q’;(q;p),≼

which is dual to p;(q;q’) (p;q);q’ ≼

Page 58: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Unifying Theories

• The Algebraic Laws of Programmingare strong enough to derive

– A verification logic for program correctness– An operational semantics for programming

language implementation.

• Many laws can be derived from the other two forms of semantics

Page 59: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Summary so far…

Process Calculus

Verification Logic

&

Algebraic Laws

Denotational Model

Page 60: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Assignments and fetches

:= :=

=:

=:

=:

Page 61: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

labelled with values

ack= 3

:= 3 := 7

=: 3

=: 3

=: 3

:=k assigns a constant k=:k fetches a value k=k points to a value kack sync signal

Page 62: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

and by reference to object

ack= 3

[37] := 3 [37]:= 7

[37]=:3

[37]=:3

[37]=:3

[v]:=k assigns to location v =k communication of k[v]=:k fetches value from v ack prevents further reads of previous assignment

Page 63: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

or to a variable named x

ack=3

x:= 3 x:= 7

x=:3

x=:3

x=:3

:=k assigns a constant k=:k fetches a value k=k communicates kack sync signal

Page 64: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

fetching by another thread

ack=3

x:= 3 x:= 7

x=:3

x=:3

x=:3

Page 65: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

A variable

alloc disp:= := :=

=:

=:

=: =:

leaving out colours & some labels

Page 66: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Execution as Petri net

begin end:= := :=

=:

=:

=: =:

Page 67: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Token move

begin end:= := :=

=:

=:

=: =:

Page 68: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Token split

begin end:=3 := :=

=:

=:

=: =:

Page 69: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Concurrency

begin end:= := :=

=:

=:

=: =:

Page 70: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Synchronisation

begin end:= := :=

=:

=:

=: =:

Page 71: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Token reconstitution

begin end:= := :=

=:

=:

=: =:

Page 72: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Concurrent object behaviour

• is modelled by an acyclic directed graph• with boxes representing event occurrences• and arrows recording dependency• and labels on both boxes and arrows.

Page 73: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Examples

• Objects: allocation, ownership, disposal.• Semaphores: exclusion, signalling.• Channels: buffering, synchrony, overtaking.• Variables: locality, sharing.

Page 74: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Weakly consistent memory

as implemented in multi-core architecture,is more complicated to define… and even more complicated to use!

Page 75: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Weak memory (no ack)

:=:= 3

=:3

=:3

=: 3=: 7

Any past value may be delivered at any later time

:= 7

Page 76: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Weak memory (no ack)

:=:= 3

=:3

=:3

=: 3=: 7

a fence needs ack signals from earlier assignments

:= 7fence

Page 77: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Weak memory (with fence)

:=:= 3

=:3

=:3

=: 3=: 7

this cycle is impossible,

:= 7fence

Page 78: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Weak memory (with fence)

:=:= 3

=:3

=:3

=: 3 =: 7

so the 3 and the 7 must be fetched in the right order

:= 7fence

Page 79: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

x =:3 y=:4 x := 7

An atomic event: <x := x + y>

Page 80: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

x = :3 y=: 4

x := 7

<x := x + y> (atomic)

x := 3 x := 17

ackack

y := 4 y := 18

Page 81: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Threads

forkalloc disp

alloc

joinalloc

disp

fork

join

Page 82: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Events and atomic actions

• Each occurrence of an event in the trace of program execution belongs to the trace of exactly one resource (thread, variable, channel,…)

• Atomic actions are groups of synchronised events, including exactly one from the thread which invoked the action, and one (or more) from every resource used by it.

Page 83: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Summary

• occurrence nets are adequate to describe the dynamic behaviour

of many kinds of concurrent object

Page 84: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Fundamental Theorem

• Boxed Petri nets are a model of Concurrent Kleene Algebra

Tony Hoare, Bernhard Moeller, Georg Struth, Ian Wehrman, Concurrent Kleene Algebra and its Foundations, J. Log. Algebr. Program. 80(6): 266-296(2011).

Page 85: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Hoare Logic

• Let P{Q}R = (P;Q) => R

• Theorem: The structural rules of separation logic are valid in the net model– Ian Wehrman, C.A.R.Hoare, Peter O’Hearn:

Graphical Models of Separation Logic. Inf Process. Lett. (IPL) 109(17):1001-1004 (2009)

• Proof: by a short algebraic calculation

Page 86: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Process Algebra

• Let P –Q-> R = R => Q;P= P{Q}R !

• Theorem: The transition rules of operational semantics are valid in the net model.– C.A.R.Hoare, A.Hussain, B.Moeller, P.W. O’Hearn,

R.L. Petersen, G. Struth. On Locality and the Exchange Law for Concurrent Processes. CONCUR 2011:250-264.

• Proof: by a short algebraic calculation.

Page 87: Charts and nets: States, Messages, and Events Tony Hoare In honour of David Harel ViennaJuly 2014.

Acknowledgements

Lucia Pomello, Matthew Parkinson, Philippa Gardiner, Hongseok Yang,John Wickerson, Thomas Dinsdale-YoungGeorg Struth, Bernhard Moeller, Rasmus Petersen, Peter O’Hearn