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.

Slides:



Advertisements
Similar presentations
Copyright 2008 Sun Microsystems, Inc Better Expressiveness for HTM using Split Hardware Transactions Yossi Lev Brown University & Sun Microsystems Laboratories.
Advertisements

CPSC 388 – Compiler Design and Construction
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Compiler Construction Intermediate Code Generation.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Control Flow C and Data Structures Baojian Hua
Data Abstraction COS 441 Princeton University Fall 2004.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Continuations COS 441 Princeton University Fall 2004.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Run time vs. Compile time
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Dec Formal Semantics1 Programming Language Theory Formal Semantics Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Semantics for MinML COS 441 Princeton University Fall 2004.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Imperative Programming
CIS 199 Test 01 Review. Computer Hardware  Central Processing Unit (CPU)  Brains  Operations performed here  Main Memory (RAM)  Scratchpad  Work.
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Data Structures & Algorithms
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Design issues for Object-Oriented Languages
Operational Semantics of Scheme
Programming Languages and Compilers (CS 421)
Edited by Original material by Eric Grimson
Reasoning About Exceptions Using Model Checking
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
Syntax-Directed Translation
Throwing and catching exceptions
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
Data Mutation Primitive and compound data mutators set! for names
6.001 SICP Data Mutation Primitive and Compound Data Mutators
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
Announcements Quiz 5 HW6 due October 23
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
topics interpreters meta-linguistic abstraction eval and apply
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
Programming Languages Dan Grossman 2013
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

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

Exceptions COS 441 Princeton University Fall 2004

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

Simple Language with Failure Numbers n 2 N Types  ::= int Expr’s e::=n | +(e 1,e 2 ) | fail | try e 1 ow e 2 Values v::=n

Static Semantics

Arbitrary Type

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::= +(v 1, ¤ ) | +( ¤,e 2 ) | try ¤ ow e 2

Dynamic Semantics unwind catch pop handler push handler

Type Soundness

Example: Normal Evaluation ( ², try +(+(1,2),3) ow 0)  ( try ¤ ow 0 B ²,+(+(1,2),3))push hndl.  (+( ¤,3) B try ¤ ow 0 B ²,+(1,2)) push  (+( ¤,2) B +( ¤,3) B try ¤ ow 0 B ²,1) push  (+(1, ¤ ) B +( ¤,3) B try ¤ ow 0 B ²,2) continue  (+( ¤,3) B try ¤ ow 0 B ²,3) pop  (+(3, ¤ ) B try ¤ ow 0 B ²,3) continue  ( try ¤ ow 0 B ²,6) pop  ( ²,6) pop hndl.

Example: Failure Evaluation ( ², try +(+(1, fail ),3) ow 0)  ( try ¤ ow 0 B ²,+(+(1, fail ),3))push hndl.  (+( ¤,3) B try ¤ ow 0 B ²,+(1, fail )) push  (+( ¤, fail ) B +( ¤,3) B try ¤ ow 0 B ²,1) push  (+(1, ¤ ) B +( ¤,3) B try ¤ ow 0 B ², fail ) continue  (+( ¤,3) B try ¤ ow 0 B ², fail ) unwind  ( try ¤ ow 0 B ², fail ) unwind  ( ²,0) catch

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

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

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

List Equality with Exceptions exception False fun lsteq(xs,ys) = let fun f([],[]) = () | f(x::xs,y::ys) = if (x = y) then f(xs,ys) else raise False | f(_,_) = raise False in (f(xs,ys);true) handle False => false end

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

Two Stack Approach Modify C-machine to have two stacks one for pending exception handlers as well as normal control flow Frames f::= +(v 1, ¤ ) | +( ¤,e 2 ) | try ¤ ow e 2 Control Stack K::= ² | f B K Handler Stack H::= ² | (K,e) B H

Dynamic Semantics push handler pop handler raise handler exit

Example: Normal Evaluation ( ², ², try +(+(1,2),3) ow 0)  (( ²,0) B ², try ¤ ow 0 B ²,+(+(1,2),3))  (( ²,0) B ²,+( ¤,3) B try ¤ ow 0 B ²,+(1,2))  (( ²,0) B ²,+( ¤,2) B +( ¤,3) B try ¤ ow 0 B ²,1)  (( ²,0) B ²,+(1, ¤ ) B +( ¤,3) B try ¤ ow 0 B ²,2)  (( ²,0) B ²,+( ¤,3) B try ¤ ow 0 B ²,3)  (( ²,0) B ²,+(3, ¤ ) B try ¤ ow 0 B ²,3)  (( ²,0) B ², try ¤ ow 0 B ²,6)  ( ²,6)

Example: Failure Evaluation ( ², ², try +(+(1, fail ),3) ow 0)  (( ²,0) B ², try ¤ ow 0 B ²,+(+(1, fail ),3))  (( ²,0) B ²,+( ¤,3) B try ¤ ow 0 B ²,+(1, fail ))  (( ²,0) B ²,+( ¤, fail ) B +( ¤,3) B try ¤ ow 0 B ²,1)  (( ²,0) B ²,+(1, ¤ ) B +( ¤,3) B try ¤ ow 0 B ², fail )  ( ², ²,0)

“Prefix” Property Handler stack captures control stack by saving value of stack pointer Prefix property guarantees For every state of the form ((K’,e’) B H’,K,e) K = f 0 B f 1 B … B f n B K’

Low-Level Machine View control stack top handler stack top

Low-Level Machine View control stack top handler stack top

Exceptions with Values Modify our syntax so that exception return values to carry information back Numbers n 2 N Types  ::= int | ?? Expr’s e::=n | +(e 1,e 2 ) | ?? | raise( e ) | try e 1 ow e 2 Values v::=n | ??

Type Checking Rules Handler is a function accepting exception value

Exceptions Values Tags t 2 … Numbers n 2 N Types  ::= int | exn Expr’s e::= … | e tag ( e ) | declexn  ( x.e ) | iftag[ x ] e then( x.e ) else e Values v::=n | t(v) exception name exception tags

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

Puzzle One let exception E val x = E val y = E in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4 end

Puzzle Two let exception E val x = E exception E val y = E in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4 end

Puzzle Three let exception E val x = E val y = E exception E in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4 end

Puzzle One let exception E val x = E val y = E in case (x,y) of (E,E) => 1(* Answer *) | (E,_) => 2 | (_,E) => 3 | _ => 4 end

Puzzle Two let exception E val x = E exception E val y = E in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 (* Answer *) | _ => 4 end

Puzzle Three let exception E val x = E val y = E exception E in case (x,y) of (E,E) => 1 | (E,_) => 2 | (_,E) => 3 | _ => 4 (* Answer *) end

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