Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.

Slides:



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

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Peter Fritzson 1 MetaModelica for Meta-Modeling and Model Transformations Peter Fritzson, Adrian Pop, Peter Aronsson OpenModelica Course at INRIA, 2006.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
CALCULATING AN EXCEPTIONAL MACHINE Graham Hutton and Joel Wright University of Nottingham.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
THE WORKER / WRAPPER TRANSFORMATION Graham Hutton and Andy Gill.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
The Formalisation of Haskell Refactorings Huiqing Li Simon Thompson Computing Lab, University of Kent
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Operational Semantics.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Do Now Pass out calculators. Solve the following system by graphing: Graph paper is in the back. 5x + 2y = 9 x + y = -3 Solve the following system by using.
On Roles of Models in Information Systems (Arne Sølvberg) Gustavo Carvalho 26 de Agosto de 2010.
Solving Linear Systems using Linear Combinations (Addition Method) Goal: To solve a system of linear equations using linear combinations.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
Unit 11, Part 2: Introduction To Logarithms. Logarithms were originally developed to simplify complex arithmetic calculations. They were designed to transform.
Unit 11, Part 2: Introduction To Logarithms. Logarithms were originally developed to simplify complex arithmetic calculations. They were designed to transform.
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
Evaluating Python as an Introductory Programming Language A. Thomas and H.L. Liang UNISA.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
THE COUNTDOWN PROBLEM Graham Hutton University of Nottingham.
Notes – 2/13 Addition Method of solving a System of Equations Also called the Elimination Method.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
WORK IT, WRAP IT, FIX IT, FOLD IT Graham Hutton and Neil Sculthorpe.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
First Order Haskell Neil Mitchell York University λ.
Types and Programming Languages
Properties as Processes : FORTE slide Properties as Processes: their Specification and Verification Joel Kelso and George Milne School of Computer.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
What is the Meaning of These Constant Interruptions? Graham Hutton and Joel Wright University of Nottingham.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
CSE Winter 2008 Introduction to Program Verification February 5 calculating with simplify.
CSE-321 Programming Languages Abstract Machine E POSTECH May 1, 2006 박성우.
Elimination Method - Systems. Elimination Method  With the elimination method, you create like terms that add to zero.
Programming what is C++
Lambda Calculus CSE 340 – Principles of Programming Languages
Names and Attributes Names are a key programming language feature
Koen Lindström Claessen
Context-Free Grammars: an overview
Introduction to Parsing (adapted from CS 164 at Berkeley)
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
Programming Languages and Compilers (CS 421)
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
PROGRAMMING IN HASKELL
Corky Cartwright January 18, 2017
Programming Languages and Compilers (CS 421)
Is everyone signed up on piazza?
Mini Language Interpreter Programming Languages (CS 550)
6-3 Solving Systems Using Elimination
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
CHAPTER 2 Context-Free Languages
Continuations and Compilation
The Metacircular Evaluator
Madhusudan Parthasarathy
6.001 SICP Variations on a Scheme
Verifying a compiler for a simple language with exceptions (MPC 04).
PROGRAMMING IN HASKELL
Lecture 2 מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes control flow explicit Defunctionalisation - Makes programs first-order

Background These techniques are often used together: language compiler CPS DEFUN Can the two steps be fused together !? Introduce continuations Remove continuations

This Talk A new approach to transforming high-level semantics into low-level implementations; Only requires simple calculation techniques, and avoids the use of continuations; Scales to exceptions, state, variable binding, loops, non-determinism, interrupts, etc.

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

Step 1 – Add Continuations Make the flow of control explicit by transforming the semantics into continuation-passing style. Definition: A continuation is a function that is applied to the result of another computation.

Specification Aim: define a new semantics such that eval’ :: Expr  Cont  Int Cont = Int  Int such that eval’ e c = c (eval e)

Control flow now explicit. New semantics: eval’ :: Expr  Cont  Int eval’ (Val n) c = c n eval’ (Add x y) c = eval’ x (n  eval’ y (m  c (n + m))) Original semantics: Control flow now explicit. eval e = eval’ e (n  n)

Step 2 - Defunctionalise Make the semantics first-order again by applying the technique of defunctionalisation. Basic idea: Represent the continuations that we actually need using a datatype.

Specification Aim: define a new semantics such that eval’’ :: Expr  CONT  Int such that exec :: CONT  Cont eval’’ e c = eval’ e (exec c)

An abstract machine for evaluating expressions. New semantics: eval’’ :: Expr  CONT  Int eval’’ (Val n) c = exec c n eval’’ (Add x y) c = eval’’ x (NEXT y c) exec :: CONT  Int  Int exec (NEXT y c) n = eval’’ y (ADD n c) exec (ADD n c) m = exec c (n + m) exec HALT n = n An abstract machine for evaluating expressions.

Reflection We now have a two step process for calculating an abstract machine from a high-level semantics: 1 – Add a continuation 2 – Remove the continuations Can the steps be combined?

The Trick Start directly with the combined specification: = eval’’ e c exec c (eval e) = Aim to calculate definitions for eval’’, exec and CONT that satisfy these equations.

In Practice Calculating three interlinked definitions at the same time seems like an impossible task; But... with experience gained from our stepwise approach, it turns out to be straightforward; New calculation is simpler, more direct, and eliminates the use of continuations.

Now we appear to be stuck, as no further definitions can be applied. Case: e = Add x y eval’’ (Add x y) c exec c (eval (Add x y)) = exec c (eval x + eval y) = Now we appear to be stuck, as no further definitions can be applied. But we are using induction, so we have induction hypotheses!

We start by generalising to: To use the hypothesis for y, we need a term of form exec c’ (eval y) for some c’, i.e. we must solve: exec c (eval x + eval y) exec c’ (eval y) = We start by generalising to: exec c (n + m) exec c’ m = But we can’t simply use this as a definition for exec, because c and n are unbound.

Solution Package the free variables up in the argument c’ by adding a new constructor to the CONT type, ADD :: Int  CONT  CONT and a new equation for the exec function: exec (ADD n c) m = exec c (n + m)

Now we can continue: = = = exec c (eval x + eval y) exec (ADD (eval x) c) (eval y) = eval’’ y (ADD (eval x) c) = . =

Summary The rest of the calculation of the abstract machine proceeds in a similar manner; The driving force for the calculation is the desire to apply induction hypotheses; We solve the resulting equations by adding new constructors to CONT on demand.

Final Result data CONT where NEXT :: Expr  CONT  CONT ADD :: Int  CONT  CONT HALT :: CONT eval’’ (Val n) c = exec c n eval’’ (Add x y) c = eval’’ x (NEXT y c) exec (NEXT y c) n = eval’’ y (ADD n c) exec (ADD n c) m = exec c (n + m) exec HALT n = n

Conclusion Purely calculational approach to cutting out the intermediate use of continuations; Only requires simple techniques, and scales to a wide variety of language features; Can also be used to calculate compilers, and everything has been formalised in Coq.