Continuations COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Advertisements

Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Compiler Construction Intermediate Code Generation.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
A small part of what you should know about continuations, but were too afraid to ask Nick Benton.
Denotational vs Operational Approaches COS 441 Princeton University Fall 2004.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
Subtyping COS 441 Princeton University Fall 2004.
C SC 520 Principles of Programming Languages Principles of Programming Languages Lecture 08 Control Semantics & Continuations.
Data Abstraction COS 441 Princeton University Fall 2004.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Programming Languages CS 152 Avi Shinnar Lecture 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.
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.
Syntax With Binders COS 441 Princeton University Fall 2004.
Dynamic Semantics COS 441 Princeton University Fall 2004.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
7/2/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Closure and Environment Compiler Baojian Hua
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Basic Semantics Associating meaning with language entities.
Data Structures & Algorithms
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
CS April 2002 Lazy Evaluation, Thunks, and Streams.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
2 Desiderata Delimited continuations (shift/reset) for Standard ML of New Jersey Pure ML library (no compiler hacking) Correct behavior in the presence.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
Evolution of programming languages
Syntax-Directed Translation
First-class continuations
FP Foundations, Scheme In Text: Chapter 14.
Closure Representations in Higher-Order Programming Languages
Continuations and Compilation
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
Continuations in Scheme
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.
Final Review In Text: Chapters 1-3, 5-12,
Continuations in Scheme
Continuations in Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Final Review In Text: Chapters 1-3, 5-16.
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).
Announcements Quiz 5 HW6 due October 23
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
Continuations in Scheme
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
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:

Continuations COS 441 Princeton University Fall 2004

Webster Says… Main Entry: re·ify Pronunciation: 'rA-&-"fI, 'rE- Function: transitive verb Inflected Form(s): re·ified; re·ify·ing Etymology: Latin res thing -- to regard (something abstract) as a material or concrete thing

First-Class Continuations Form of structured GOTO Can be used to implement exception Can be used to build co-routines or threads Available in Scheme, Ruby, and SML/NJ but not Standard ML Also useful as a programming abstraction for web-services!

Some Informal Examples ( throw e 1 to e 2 ):  ’ Invoke continuation (e 2 :  cont ) with the value of e 1 :  ( letcc x in e):  Evaluate e with x bound to a value of type  cont. Expression evaluates to the value of e or result thrown to x. Can throw to x multiple times and “rewind” to this control point

Example Compute product of list elements with short circuit when 0 is encountered

Example Compute product of list elements with short circuit when 0 is encountered

Trickier Example Compose a function with a continuation

Continuation Passing Style Can apply a global translation of a program and remove all uses of throw and letcc Won’t study in detail in class –CPS transform used in compilation of functional languages to machine code Understanding how continuations can be used is harder than understanding their semantics!

First-Class Continuations Numbers n 2 N Types  ::= int |  cont | … Expr’s e::=n | +(e 1,e 2 ) | throw e 1 to e 2 | letcc x in e |K (* not user visible *) Values v::=n | K

Frames and Stacks Frames f::= +(v 1, ¤ ) | +( ¤,e 2 ) | throw v 1 to ¤ | throw ¤ to e 2 Stack K::= ² | f B K

Dynamic Semantics

Capturing a continuation can be a linear time operation if not implemented cleverly Clever implementations make continuation capture constant time Compare this to two stack exception handler approach

Type Safety Must define when a machine state is well- formed Introduced the notion of types for stacks, frames, and also choose one arbitrary but fixed type for the empty stack  ans

Programmer Visible Rules

Auxiliary Types Types  ::= int |  cont |  stack | (  1,  2 ) frame

Internal Type-Checking Rules

Rules For Frames ( ,  ’) frame Frame which expects a value of type  that can be used to fill the hole to produce an expression of type  ’

Frame Rules (cont.) Harper’s text has a small bug rules above are correct ` throw v 1 to ¤ : (  cont,  ’) frame v 1 value ` v 1 :  ` throw ¤ to e 2 : ( ,  ’) frame ` e 2 :  cont

Example: Continuation Capture ( ²,+(( letcc x in 1),2))  (+( ¤,2) B ², letcc x in 1)  (+( ¤,2) B ²,1)  (+(1, ¤ ) B ²,2)  ( ²,3)

Example: Thrown Continuation ( ²,+(( letcc x in +(1, throw 2 to x)),3))  (+( ¤,3) B ², letcc x in +(1, throw 2 to x))  (+( ¤,3) B ²,+(1, throw 2 to +( ¤,3) B ² ))  (+( ¤, throw 2 to +( ¤,3) B ² ) B +( ¤,3) B ²,1)  (+(1, ¤ ) B +( ¤,3) B ², throw 2 to +( ¤,3) B ² )  (( throw ¤ to +( ¤,3) B ² ) B +(1, ¤ ) B +( ¤,3) B ²,2)  (( throw 2 to ¤ ) B +(1, ¤ ) B +( ¤,3) B ²,+( ¤,3) B ² )  (+( ¤,3) B ²,2)  (+(2, ¤ ) B ²,3)  ( ²,5)

Stacks and Frames as HOF One big insight of the CPS transform is that you can represent stacks as HOF rep( ² )  x.x rep(+(v 1, ¤ ) B K)  x.(rep(K) +(v 1,x)) rep(+( ¤,e 2 ) B K)  x.(rep(K) +(x,e 2 )) rep( throw v 1 to ¤B K)  x.(x v) rep( throw ¤ to e 2 B K)  x.(e 2 x)

Producer Consumer fun p(n) = (put(n);p(n+1)) fun c() = let val n = get() in printInt(n); c() end

Push Model fun p(n) = (c(n);p(n+1)) fun c(n) = printInt(n)

Pull Model fun p(n,s) = (n,n+1) fun c(n,s) = let val (n,s) = p(s) in printInt(n); c(s) end

Coroutine Model fun p(n,s) = let val s = put(n,s) in p(n+1,s) end fun c(s) = let val (n,s) = get(s) in printInt(n); c(s) end

Implementation datatype state = S of state cont val buf = ref 0 fun resume(S k) = callcc(fn k' => throw k (S k')) fun put(n,s) = (buf := n;resume s) fun get(s) = (!buf,resume s)

Threads signature THREADS = sig exception NoMoreThreads val fork : (unit -> unit) -> unit val yield : unit -> unit val exit : unit -> ‘a end User-level threads implemented with callcc

Summary First-Class continuations very powerful construct “Simple” semantics but allows powerful control constructs –Non-local exits –Coroutines –User-level threads