Title: Functional Programming
1(No Transcript)
2What Is An Exception?
An event within a computation that causes
termination in a non-standard way
Examples
- Division by zero
- Null pointer
3What Is An Interrupt?
An exception that arises from the external
environement, e.g. another computation
Examples
4This 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.
5An Exceptional Language
Syntax
data Expr Val Int Throw
Add Expr Expr Seq Expr Expr
Catch Expr Expr
Semantics
e can evaluate to v
e ? v
6Sequencing
Catch
7Finally, An Example
Problem how can we ensure that evaluation of x
is always succeeded by evaluation of y?
finally x y
8Finally, An Example
Problem how can we ensure that evaluation of x
is always succeeded by evaluation of y?
finally x y
Seq x y
9Finally, 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 is not evaluated
Seq x y
10Finally, An Example
Problem how can we ensure that evaluation of x
is always succeeded by evaluation of y?
finally x y
Seq (Catch x y) y
11Finally, An Example
Problem how can we ensure that evaluation of x
is always succeeded by evaluation of y?
If x produces an exception, y may be evaluated
twice
finally x y
Seq (Catch x y) y
12Finally, An Example
Problem how can we ensure that evaluation of x
is always succeeded by evaluation of y?
finally x y
Seq (Catch x (Seq y Throw)) y
13Finally, 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
Seq (Catch x (Seq y Throw)) y
14Adding Interrupts
To avoid the need for concurrency, we adopt the
following worst-case rule for interrupts
Evaluation can be interrupted at any time by
replacing the current expression by throw
15Note
- Evaluation is now non-deterministic.
- Finally no longer behaves as expected.
Seq (Catch x (Seq y Throw)) y
could be interrupted as y is about to be evaluated
16Controlling Interrupts
Syntax
data Expr Block Expr
Unblock Expr
Semantics
e can evaluate to v in interrupt status i
e ?i v
17Key rules
The other rules are simply modified to propogate
the current interrupt status to their arguments.
18Finally Revisited
finally x y
Seq (Catch x (Seq y Throw)) y
19Finally Revisited
finally x y
Block (Seq (Catch (Unblock x) (Seq y Throw)) y)
20Finally Revisited
finally x y
Block (Seq (Catch (Unblock x) (Seq y Throw)) y)
Modulo syntax, finally in Haskell is defined in
precisely the same way
21Is 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.
22Example
Catch (Unblock (23)) 4
Code
23Example
Catch (Unblock (23)) 4
Code
24Example
Catch (Unblock (23)) 4
Code
MARK UNMARK
25Example
Catch (Unblock (23)) 4
Code
MARK UNMARK
26Example
Catch (Unblock (23)) 4
Code
MARK PUSH 4 UNMARK
27Example
Catch (Unblock (23)) 4
Code
MARK PUSH 4 UNMARK
28Example
Catch (Unblock (23)) 4
Code
MARK PUSH 4 SET U RESET UNMARK
29Example
Catch (Unblock (23)) 4
Code
MARK PUSH 4 SET U RESET UNMARK
30Example
Catch (Unblock (23)) 4
Code
MARK PUSH 4 SET U PUSH 2 PUSH 3 ADD RESET UNMARK
31Example
Catch (Unblock (23)) 4
Code
Stack
Status
MARK PUSH 4 SET U PUSH 2 PUSH 3 ADD RESET UNMARK
32Example
Catch (Unblock (23)) 4
Code
Stack
Status
MARK PUSH 4 SET U PUSH 2 PUSH 3 ADD RESET UNMARK
B
33Example
Catch (Unblock (23)) 4
Code
Stack
Status
SET U PUSH 2 PUSH 3 ADD RESET UNMARK
HAN PUSH 4
B
34Example
Catch (Unblock (23)) 4
Code
Stack
Status
PUSH 2 PUSH 3 ADD RESET UNMARK
INT B HAN PUSH 4
U
35Example
Catch (Unblock (23)) 4
Code
Stack
Status
PUSH 3 ADD RESET UNMARK
VAL 2 INT B HAN PUSH 4
U
36Example
Catch (Unblock (23)) 4
Code
Stack
Status
ADD RESET UNMARK
VAL 3 VAL 2 INT B HAN PUSH 4
U
37Example
Catch (Unblock (23)) 4
Code
Stack
Status
ADD RESET UNMARK
VAL 3 VAL 2 INT B HAN PUSH 4
U
interrupt!
38Example
Catch (Unblock (23)) 4
Code
Stack
Status
THROW RESET UNMARK
VAL 3 VAL 2 INT B HAN PUSH 4
U
interrupt!
39Example
Catch (Unblock (23)) 4
Code
Stack
Status
THROW RESET UNMARK
VAL 2 INT B HAN PUSH 4
U
40Example
Catch (Unblock (23)) 4
Code
Stack
Status
THROW RESET UNMARK
INT B HAN PUSH 4
U
41Example
Catch (Unblock (23)) 4
Code
Stack
Status
THROW RESET UNMARK
HAN PUSH 4
B
42Example
Catch (Unblock (23)) 4
Code
Stack
Status
PUSH 4
B
43Example
Catch (Unblock (23)) 4
Code
Stack
Status
VAL 4
B
44Example
Catch (Unblock (23)) 4
Code
Stack
Status
VAL 4
B
Final result
45Compiler Correctness
We will exploit two basic notions of reachability
for configurations of our virtual machine.
x Y
x can reach everything in Y
x Y
x will reach something in Y
46Theorem
comp e c i s
U
e ?i Val n
c i VAL n s
e ?i Throw
i s
Proof approximately 10 pages of calculation,
much of which requires considerable care.
47Summary
- 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/interrupt
s.
48Further Work
- Mechanical verification
- Bisimulation theorem
- Generalising the language
- Reasoning about programs
- Calculating the compiler