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

What Is An Exception?

  • Upload
    remedy

  • View
    27

  • Download
    1

Embed Size (px)

DESCRIPTION

What Is An Exception?. An event within a computation that causes termination in a non-standard way. Examples:. Division by zero Null pointer. What Is An Interrupt?. An exception that arises from the external environement, e.g. another computation. Examples:. Terminate Any exception. - PowerPoint PPT Presentation

Citation preview

Page 1: What Is An Exception?

What is the Meaning of These Constant Interruptions?

Graham Hutton and Joel WrightUniversity of Nottingham

Page 2: What Is An Exception?

2

What Is An Exception?

Division by zero

Null pointer

Examples:

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

standard way

Page 3: What Is An Exception?

3

What Is An Interrupt?

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

computation

Terminate

Any exception

Examples:

Page 4: What Is An Exception?

4

This Talk

Haskell 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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

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 An Exception?

14

Adding Interrupts

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

x Throw

Evaluation can be interrupted at any time by replacing

the current expression by throw

Page 15: What Is An Exception?

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 An Exception?

16

Controlling Interrupts

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

Syntax:

Semantics:

e i v

e can evaluate to v in interrupt

status i

Page 17: What Is An Exception?

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 An Exception?

18

Finally Revisited

finally x y

=

Seq (Catch x (Seq y Throw)) y

Page 19: What Is An Exception?

19

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

Finally Revisited

finally x y

=

Page 20: What Is An Exception?

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 An Exception?

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 An Exception?

22

Catch (Unblock (2+3)) 4

Example

Code

Page 23: What Is An Exception?

23

Catch (Unblock (2+3)) 4

Example

Code

Page 24: What Is An Exception?

24

Catch (Unblock (2+3)) 4

Example

MARK [ ]

UNMARK

Code

Page 25: What Is An Exception?

25

Catch (Unblock (2+3)) 4

Example

MARK [ ]

UNMARK

Code

Page 26: What Is An Exception?

26

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]

UNMARK

Code

Page 27: What Is An Exception?

27

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]

UNMARK

Code

Page 28: What Is An Exception?

28

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET U

RESETUNMARK

Code

Page 29: What Is An Exception?

29

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET U

RESETUNMARK

Code

Page 30: What Is An Exception?

30

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Page 31: What Is An Exception?

31

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

Status

Page 32: What Is An Exception?

32

Catch (Unblock (2+3)) 4

Example

MARK [PUSH 4]SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

Status

B

Page 33: What Is An Exception?

33

Catch (Unblock (2+3)) 4

Example

SET UPUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

HAN [PUSH 4]

Status

B

Page 34: What Is An Exception?

34

Catch (Unblock (2+3)) 4

Example

PUSH 2PUSH 3ADDRESETUNMARK

Code

Stack

INT BHAN [PUSH 4]

Status

U

Page 35: What Is An Exception?

35

Catch (Unblock (2+3)) 4

Example

PUSH 3ADDRESETUNMARK

Code

Stack

VAL 2INT BHAN [PUSH 4]

Status

U

Page 36: What Is An Exception?

36

Catch (Unblock (2+3)) 4

Example

ADDRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

Page 37: What Is An Exception?

37

Catch (Unblock (2+3)) 4

Example

ADDRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

interrupt!

Page 38: What Is An Exception?

38

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

VAL 3VAL 2INT BHAN [PUSH 4]

Status

U

interrupt!

Page 39: What Is An Exception?

39

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

VAL 2INT BHAN [PUSH 4]

Status

U

Page 40: What Is An Exception?

40

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

INT BHAN [PUSH 4]

Status

U

Page 41: What Is An Exception?

41

Catch (Unblock (2+3)) 4

Example

THROWRESETUNMARK

Code

Stack

HAN [PUSH 4]

Status

B

Page 42: What Is An Exception?

42

Catch (Unblock (2+3)) 4

Example

PUSH 4

Code

Stack

Status

B

Page 43: What Is An Exception?

43

Catch (Unblock (2+3)) 4

Example

Code

Stack

VAL 4

Status

B

Page 44: What Is An Exception?

44

Catch (Unblock (2+3)) 4

Example

Code

Stack

VAL 4

Status

B

Final result

Page 45: What Is An Exception?

45

Compiler Correctness

We 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 An Exception?

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 An Exception?

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 An Exception?

48

Further Work

Mechanical verification

Bisimulation theorem

Generalising the language

Reasoning about programs

Calculating the compiler