![Page 1: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/1.jpg)
Misc. Announcements
• Assignment available end of the day today– Due back in 11/03 (after break)
• Will also update slides on website– Today
• Midterm next week– Handed out Monday in class – Due back 5:00pm Friday
![Page 2: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/2.jpg)
Exceptions
COS 441
Princeton University
Fall 2004
![Page 3: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/3.jpg)
Outline
• Formalize simplified version of exceptions that do not return values
• “Stack unwinding” model of exception handling
• “Two stack” model of exception handling
• Exceptions that return values
![Page 4: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/4.jpg)
Simple Language with Failure
Numbers n 2 NTypes ::= int
Expr’s e ::= n | +(e1,e2)
| fail | try e1 ow e2
Values v ::= n
![Page 5: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/5.jpg)
Static Semantics
![Page 6: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/6.jpg)
Static Semantics
Arbitrary Type
![Page 7: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/7.jpg)
Dynamic Semantics
• Modify C-machine to encode semantics of failure
• M-machine semantics can also be made but messy– Similar to adding “error” case for division– C-machine formulation allows us to talk about
implementation issues in more detail
Frames f ::= +(v1,¤) | +(¤,e2)
| try ¤ ow e2
![Page 8: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/8.jpg)
Dynamic Semantics
unwind
catch
pop handler
push handler
![Page 9: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/9.jpg)
Type Soundness
![Page 10: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/10.jpg)
Example: Normal Evaluation
(²,try +(+(1,2),3) ow 0)
(try ¤ ow 0 B²,+(+(1,2),3)) push hndl.
(+(¤,3)Btry ¤ ow 0 B²,+(1,2)) push
(+(¤,2)B+(¤,3)Btry ¤ ow 0 B²,1) push
(+(1,¤)B+(¤,3)Btry ¤ ow 0 B²,2) continue
(+(¤,3)Btry ¤ ow 0 B²,3) pop
(+(3,¤)Btry ¤ ow 0 B²,3) continue
(try ¤ ow 0 B²,6) pop
(²,6) pop hndl.
![Page 11: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/11.jpg)
Example: Failure Evaluation
(²,try +(+(1,fail),3) ow 0)
(try ¤ ow 0 B²,+(+(1,fail),3)) push hndl.
(+(¤,3)Btry ¤ ow 0 B²,+(1,fail)) push
(+(¤,fail)B+(¤,3)Btry ¤ ow 0 B²,1) push
(+(1,¤)B+(¤,3)Btry ¤ ow 0 B²,fail) continue
(+(¤,3)Btry ¤ ow 0 B²,fail) unwind
(try ¤ ow 0 B²,fail) unwind
(²,0) catch
![Page 12: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/12.jpg)
Stack Unwinding
• Notice we raise the nearest dynamically enclosing handler– Handlers have “dynamic scope”
• Stack unwinding takes a linear number of steps depending on stack depth– Not a problem if exceptions are truly
exceptional!– Bigger problem when we use exceptions to
encode other control structures
![Page 13: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/13.jpg)
Non-Exceptional Exceptions
• Common programming trick is to use exceptions to encode non-determinism– More on this later
• Used to short circuit deep nested evaluations– When checking for list equality fail on first
mismatch
![Page 14: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/14.jpg)
List Equality
fun lsteq([],[])= true
| lsteq(x::xs,y::ys) =
(x = y) andalso lsteq(xs,ys)
| lsteq (_,_) = false
Consider two 1000 element list where the last element of the list is different
Code above must pop off 1000 frames to return false
![Page 15: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/15.jpg)
List Equality with Exceptions
exception Falsefun lsteq(xs,ys) = let fun f([],[]) = () | f(x::xs,y::ys) =
if (x = y) then f(xs,ys)else raise False
| f(_,_) = raise Falsein (f(xs,ys);true) handle
False => falseend
![Page 16: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/16.jpg)
List Equality with Exceptions
• If raising exception is a constant time operation version that uses exceptions detects failure faster
• Doesn’t have to unwind or pop stack frames just return with false immediately
• We can design abstract machine which raises exceptions in constant time
![Page 17: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/17.jpg)
Two Stack Approach
Modify C-machine to have two stacks one for pending exception handlers as well as normal control flow
Frames f ::= +(v1,¤) | +(¤,e2)
| try ¤ ow e2
Control Stack K ::= ² | f B K Handler Stack H ::= ² | (K,e) B H
![Page 18: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/18.jpg)
Dynamic Semanticspush handler
pop handler
raise handler
exit
![Page 19: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/19.jpg)
Example: Normal Evaluation
(²,²,try +(+(1,2),3) ow 0) ((²,0) B²,try ¤ ow 0 B²,+(+(1,2),3))
((²,0) B²,+(¤,3)Btry ¤ ow 0 B²,+(1,2)) ((²,0) B²,+(¤,2)B+(¤,3)Btry ¤ ow 0 B²,1) ((²,0) B²,+(1,¤)B+(¤,3)Btry ¤ ow 0 B²,2) ((²,0) B²,+(¤,3)Btry ¤ ow 0 B²,3) ((²,0) B²,+(3,¤)Btry ¤ ow 0 B²,3) ((²,0) B²,try ¤ ow 0 B²,6) (²,6)
![Page 20: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/20.jpg)
Example: Failure Evaluation
(²,²,try +(+(1,fail),3) ow 0)
((²,0) B²,try ¤ ow 0 B²,+(+(1,fail),3))
((²,0) B²,+(¤,3)Btry ¤ ow 0 B²,+(1,fail))
((²,0) B²,+(¤,fail)B+(¤,3)Btry ¤ ow 0 B²,1)
((²,0) B²,+(1,¤)B+(¤,3)Btry ¤ ow 0 B²,fail)
(²,²,0)
![Page 21: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/21.jpg)
“Prefix” Property
• Handler stack captures control stack by saving value of stack pointer
• Prefix property guaranteesFor every state of the form ((K’,e’) B H’,K,e)
K = f0B f1B … B fn BK’
![Page 22: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/22.jpg)
Low-Level Machine View
control stack top
handler stack top
![Page 23: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/23.jpg)
Low-Level Machine View
control stack top
handler stack top
![Page 24: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/24.jpg)
Exceptions with Values
Modify our syntax so that exception return values to carry information back
Numbers n 2 NTypes ::= int | ??
Expr’s e ::= n | +(e1,e2) | ??
| raise(e) | try e1 ow e2
Values v ::= n | ??
![Page 25: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/25.jpg)
Type Checking Rules
Handler is a function accepting exception value
![Page 26: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/26.jpg)
Exceptions Values
Tags t 2 …Numbers n 2 NTypes ::= int | exn
Expr’s e ::= … | etag(e)
| declexn (x.e)
| iftag[x]e then(x.e) else e
Values v ::= n | t(v)
exception name
exception tags
![Page 27: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/27.jpg)
Extensible Datatypes
• Exceptions are like Java Object act as universal container for any values
• Exception tags are generate at runtime• Generative exceptions
– Arguably a bad idea! – Harder to implement not that much extra
expressiveness
• Typing rules must be carefully designed to avoid soundness bugs
![Page 28: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/28.jpg)
Puzzle One
let exception E val x = E val y = E
in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4end
![Page 29: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/29.jpg)
Puzzle Two
let exception E val x = Eexception E
val y = Ein case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4end
![Page 30: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/30.jpg)
Puzzle Three
let exception E val x = E val y = Eexception E
in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4end
![Page 31: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/31.jpg)
Puzzle One
let exception E val x = E val y = E
in case (x,y) of (E,E) => 1 (* Answer *) | (E,_) => 2 | (_,E) => 3 | _ => 4end
![Page 32: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/32.jpg)
Puzzle Two
let exception E val x = Eexception E
val y = Ein case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 (* Answer *) | _ => 4end
![Page 33: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/33.jpg)
Puzzle Three
let exception E val x = E val y = Eexception E
in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4 (* Answer *)end
![Page 34: Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week](https://reader035.vdocuments.site/reader035/viewer/2022062516/56649d4c5503460f94a298d3/html5/thumbnails/34.jpg)
Summary
• Exceptions allow for non-local exits
• Can be used by programmer for non-exceptional exists not just infrequent errors
• Efficient implementation can be achieved using two stack approach
• Extensible datatypes allow exceptions to carry values