what is the meaning of these constant interruptions? graham hutton and joel wright university of...

48
What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

Upload: leonard-porter

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

2 What Is An Interrupt? An exception that arises from the external environement, e.g. another computation zTerminate zAny exception Examples:

TRANSCRIPT

Page 1: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

What is the Meaning of These Constant Interruptions?

Graham Hutton and Joel WrightUniversity of Nottingham

Page 2: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

2

What Is An Exception?

Division by zeroNull pointer

Examples:

An event within a computation that causes termination in a non-

standard way

Page 3: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

3

What Is An Interrupt?

An exception that arises from the external environement, e.g. another

computation

TerminateAny exception

Examples:

Page 4: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

4

This TalkHaskell is unique in providing both full support

for interrupts and a semantics for this.

But the semantics is subtle, and relies on quite considerable technical machinery.

We give a simple, formally justified, semantics for interrupts in a small language.

Page 5: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

5

An Exceptional Language

data Expr = Val Int | Throw | Add Expr Expr | Seq Expr Expr | Catch Expr Expr

Syntax:

Semantics:

e ve can evaluate to

v

Page 6: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

6

Sequencing:

Seq x y v

x Val n y v

Seq x y Throw

x Throw

Catch x y Val n

x Val n

Catch x y v

x Throw y v

Catch:

Page 7: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

7

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Page 8: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

8

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Seq x y

Page 9: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

9

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Seq x y

If x produces an exception,

y is not evaluated

Page 10: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

10

Seq (Catch x y) y

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Page 11: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

11

Seq (Catch x y) y

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

If x produces an exception, y

may be evaluated twice

Page 12: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

12

Seq (Catch x (Seq y Throw)) y

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Page 13: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

13

Seq (Catch x (Seq y Throw)) y

Finally, An Example

Problem: how can we ensure that evaluation of x is always succeeded by evaluation of y?

finally x y

=

Now has the correct

behaviour

Page 14: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

14

Adding Interrupts

To avoid the need for concurrency, we adopt the following worst-case rule for interrupts:

x ThrowEvaluation can be interrupted at any time by replacing

the current expression by throw

Page 15: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

15

Seq (Catch x (Seq y Throw)) y

Note:

Evaluation is now non-deterministic.

Finally no longer behaves as expected.

could be interrupted as y is about to be

evaluated

Page 16: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

16

Controlling Interrupts

data Expr = ••• | Block Expr | Unblock Expr

Syntax:

Semantics:

e i ve can evaluate to

v in interrupt status i

Page 17: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

17

Key rules:

Block x i v

x B v

Unblock x i v

x U v

x U Throw

The other rules are simply modified to propogate the current interrupt status to their arguments.

Page 18: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

18

Finally Revisited

finally x y

=

Seq (Catch x (Seq y Throw)) y

Page 19: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

19

Block (Seq (Catch (Unblock x) (Seq y Throw)) y)

Finally Revisited

finally x y

=

Page 20: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

20

Block (Seq (Catch (Unblock x) (Seq y Throw)) y)

Finally Revisited

finally x y

=

Modulo syntax, finally in Haskell is defined in precisely

the same way

Page 21: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

21

Is Our Semantics Correct?

How does our high-level semantics reflect our low-level intuition about interrupts?

To address this issue, we first define a virtual machine, its semantics, and a compiler.

We explain the basic ideas informally using an example - the paper gives full details.

Page 22: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

22

Catch (Unblock (2+3)) 4

Example

Code

Page 23: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

23

Catch (Unblock (2+3)) 4

Example

Code

Page 24: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

24

Catch (Unblock (2+3)) 4

Example

MARK [ ]

UNMARK

Code

Page 25: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

25

Catch (Unblock (2+3)) 4

Example

MARK [ ]

UNMARK

Code

Page 26: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

26

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]

UNMARK

Code

Page 27: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

27

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]

UNMARK

Code

Page 28: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

28

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET U

RESETUNMARK

Code

Page 29: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

29

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET U

RESETUNMARK

Code

Page 30: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

30

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Page 31: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

31

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

Status

Page 32: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

32

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

Status

B

Page 33: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

33

Catch (Unblock (2+3)) 4

Example

SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

HAN [PUSH 4]

Status

B

Page 34: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

34

Catch (Unblock (2+3)) 4

Example

PUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

INT BHAN [PUSH 4]

Status

U

Page 35: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

35

Catch (Unblock (2+3)) 4

Example

PUSH 3ADDRESETUNMARK

Code

Stack

VAL 2INT BHAN [PUSH 4]

Status

U

Page 36: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

36

Catch (Unblock (2+3)) 4

Example

ADDRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

Page 37: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

37

Catch (Unblock (2+3)) 4

Example

ADDRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

interrupt!

Page 38: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

38

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

interrupt!

Page 39: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

39

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

VAL 2INT BHAN [PUSH 4]

Status

U

Page 40: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

40

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

INT BHAN [PUSH 4]

Status

U

Page 41: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

41

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

HAN [PUSH 4]

Status

B

Page 42: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

42

Catch (Unblock (2+3)) 4

Example

PUSH 4

Code

Stack

Status

B

Page 43: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

43

Catch (Unblock (2+3)) 4

Example

Code

Stack

VAL 4

Status

B

Page 44: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

44

Catch (Unblock (2+3)) 4

Example

Code

Stack

VAL 4

Status

B

Final result

Page 45: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

45

Compiler CorrectnessWe will exploit two basic notions of reachability for configurations of our virtual machine.

x can reach everything in

Y

x will reach something in

Y

x * Y

x Y

Page 46: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

46

Theorem

{ | e i Val n }

{ | e i Throw }

*

U

Proof: approximately 10 pages of calculation, much of which requires considerable care.

comp e c i s

c i VAL n : s

i s

Page 47: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

47

Summary

Simple semantics for interrupts, formally justified by a compiler correctness theorem.

Discovery of an error in the semantics for Haskell, concerning the delivery of interrupts.

Verification of finally, a useful high-level operator for programming with exceptions/interrupts.

Page 48: What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham

48

Further Work

Mechanical verification

Bisimulation theorem

Generalising the language

Reasoning about programs

Calculating the compiler