CALCULATING AN EXCEPTIONAL MACHINE Graham Hutton and Joel Wright University of Nottingham.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
The Semantic Soundness of a Type System for Interprocedural Register Allocation and Constructor Registration Torben Amtoft Kansas State University joint.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
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.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill.
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Continuations COS 441 Princeton University Fall 2004.
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.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
Dec Formal Semantics1 Programming Language Theory Formal Semantics Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
0 PROGRAMMING IN HASKELL Chapter 11 - The Countdown Problem.
Chapter 2 A Simple Compiler
HOW TO BE MORE PRODUCTIVE Graham Hutton and Mauro Jaskelioff.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Syntax-Directed Translation
Lab 3: Using ML-Yacc Zhong Zhuang
Adding Mixed Numbers © Math As A Second Language All Rights Reserved next #7 Taking the Fear out of Math
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
Model construction and verification for dynamic programming languages Radu Iosif
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
CPS 506 Comparative Programming Languages Syntax Specification.
1 CS161 Introduction to Computer Science Topic #9.
THE COUNTDOWN PROBLEM Graham Hutton University of Nottingham.
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe.
Top-Down Parsing.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Intermediate code generation Simplifying initial assumptions a 3-address code will be used for instructions  choice of instruction mnemonics and layouts.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
CSE Winter 2008 Introduction to Program Verification February 5 calculating with simplify.
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
Fusion Catherine Hope and Graham Hutton University of Nottingham in Less Space.
Programming Languages Dan Grossman 2013
String is a synonym for the type [Char].
6.001 SICP Compilation Context: special purpose vs. universal machines
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
Optimization Code Optimization ©SoftMoore Consulting.
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
PROGRAMMING IN HASKELL
Chapter 4 Action Routines.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
Verifying a compiler for a simple language with exceptions (MPC 04).
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

CALCULATING AN EXCEPTIONAL MACHINE Graham Hutton and Joel Wright University of Nottingham

1 Exception zAn event that causes a computation to terminate in a non-standard way. Abstract Machine zA term-rewriting system for executing programs in a particular language.

2 This Talk zWe show how to calculate an abstract machine for a small language with exceptions; zThe key technique is defunctionalization, first introduced by John Reynolds in 1972; zSomewhat neglected in recent years, but now re-popularised by Olivier Danvy et al.

3 Arithmetic Expressions data Expr = Val Int | Add Expr Expr eval :: Expr  Int eval (Val n) = n eval (Add x y) = eval x + eval y Syntax: Semantics:

4 Step 1 - Add Continuations Make the evaluation order explicit, by rewriting the semantics in continuation-passing style. Definition: A continuation is a function that is applied to the result of another computation.

5 Example: Basic idea: Generalise the semantics to make the use of continuations explicit. eval x+ eval y continuationcomputation

6 Aim: define a new semantics and hence eval’ :: Expr  (Int  Int)  Int eval e = eval’ e ( n  n) eval’ e c = c (eval e) such that

7 eval’ (Add x y) c Case: e = Add x y c (eval (Add x y)) = c (eval x + eval y) = eval’ x ( n  eval’ y ( m  c (n+m)) = eval’ x ( n  ( m  c (n+m)) (eval y)) = eval’ x ( n  c (n + eval y)) = ( n  c (n + eval y)) (eval x) = c (eval (Add x y)) c (eval x + eval y) eval’ x ( n  ( m  c (n+m)) (eval y)) eval’ x ( n  c (n + eval y)) ( n  c (n + eval y)) (eval x)

8 eval’ :: Expr  Cont  Int eval’ (Val n) c = c n eval’ (Add x y) c = eval’ x ( n  eval’ y ( m  c (n+m))) New semantics: The evaluation order is now explicit.

9 Step 2 - Defunctionalize Make the semantics first-order again, by rewriting eval’ using the defunctionalization technique. Basic idea: Represent the continuations we actually need using a datatype.

10 Continuations: eval’ :: Expr  Cont  Int eval’ (Val n) c = c n eval’ (Add x y) c = eval’ x ( n  eval’ y ( m  c (n+m))) eval :: Expr  Int eval e = eval’ e ( n  n) ( n  n) ( m  c (n+m))( n  eval’ y ( m  c (n+m)))

11 Combinators: c2 :: Expr  Cont  Cont c2 y c = n  eval’ y (c3 n c) c1 :: Cont c1 = n  n c3 :: Int  Cont  Cont c3 n c = m  c (n+m)

12 Datatype: apply :: CONT  Cont apply C1 = c1 apply (C2 y c) = c2 y (apply c) apply (C3 n c) = c3 n (apply c) data CONT = C1 | C2 Expr CONT | C3 Int CONT Semantics:

13 Aim: define a function and hence eval’’ e c = eval’ e (apply c) eval e = eval’’ e C1 such that eval’’ :: Expr  CONT  Int

14 eval’’ (Val n) c = apply c n eval’’ (Add x y) c = eval’’ x (C2 y c) By calculation, we obtain: The semantics is now first-order again. apply C1 n = n apply (C2 y c) n = eval’’ y (C3 n c) apply (C3 n c) m = apply c (n+m)

15 Step 3 - Refactor zWhat have we actually produced? Question: zAn abstract machine, but this only becomes clear after we refactor the components. Answer:

16 data Cont = STOP | EVAL Expr Cont | ADD Int Cont run e = eval e STOP eval (Val n) c = exec c n eval (Add x y) c = eval x (EVAL y c) exec STOP n = n exec (EVAL y c) n = eval y (ADD n c) exec (ADD n c) m = exec c (n+m) Abstract machine:

17 Example: run (Add (Val 1) (Val 2)) eval (Add (Val 1) (Val 2)) STOP = eval (Val 1) (EVAL (Val 2) STOP) = exec 3 STOP = exec (ADD 1 STOP) 2 = eval (Val 2) (ADD 1 STOP) = exec (EVAL (Val 2) STOP) 1 = 3 =

18 eval :: Expr  Maybe Int eval (Val n) = Just n eval (Throw) = Nothing eval (Add x y) = eval x  eval y eval (Catch x y) = eval x  eval y Adding Exceptions data Expr = | Throw | Catch Expr Expr Syntax: Semantics:

19 Step 1 - Add Continuations Step 2 - Defunctionalize Step 3 - Refactor Make evaluation order explicit. Make first-order once again. Reveal the abstract machine.

20 Control stack: Evaluating an expression: data Cont = STOP | EVAL Expr Cont | ADD Int Cont | HAND Expr Cont eval :: Expr  Cont  Maybe Int eval (Val n) c = exec c n eval (Throw) c = unwind c eval (Add x y) c = eval x (EVAL y c) eval (Catch x y) c = eval x (HAND y c)

21 Executing the control stack: Unwinding the control stack: unwind :: Cont  Maybe Int unwind STOP = Nothing unwind (EVAL _ c) = unwind c unwind (ADD _ c) = unwind c unwind (HAND y c) = eval y c exec :: Cont  Int  Maybe Int exec STOP n = Just n exec (EVAL y c) n = eval y (ADD n c) exec (ADD n c) m = exec c (n+m) exec (HAND _ c) n = exec c n

22 Summary zPurely calculational development of an abstract machine for a language with exceptions; zKey ideas of marking/unmarking and unwinding the stack arise directly from the calculations; zTechniques have been used to systematically design many other machines - Danvy et al.

23 Further Work zExploiting monads and folds; zReasoning about efficiency; zGeneralising the language; zCalculating a compiler.