A small part of what you should know about continuations, but were too afraid to ask Nick Benton.

Slides:



Advertisements
Similar presentations
A complete citation, notecard, and outlining tool
Advertisements

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
 Suppose for a moment that you were asked to perform a task and were given the following list of instructions to perform:
“What do you want me to do now?”
101.  Take a look at this code and tell me what generation of programming language is used here. It is important that you can EXPLAIN how you came up.
What is good thinking? What is involved in good problem solving?
Best-First Search: Agendas
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Programming Languages CS 152 Avi Shinnar Lecture 13.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Computer Science 162 Section 1 CS162 Teaching Staff.
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.
Register Allocation (via graph coloring)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
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.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Addition and Subtraction Fact Families By: Sandra Harris
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Nachos Phase 1 Code -Hints and Comments
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
What does a computer program look like: a general overview.
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
In charge of: Making official decisions Reading materials to the rest of the group Coordinating presentations Filling in for absent group members.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS April 2002 Lazy Evaluation, Thunks, and Streams.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
The UNA University Writing Center Writing & Research Process Workshop Series Dr. Robert T. Koch Jr. Director, Center for Writing Excellence University.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Test Taking Skills Make sure you prove what you know!
R Some of these slides are from Prof Frank Lin SJSU. r Minor modifications are made. 1.
TechKnowlogy Conference August 2, 2011 Using GoogleDocs for Collaboration.
Recursion.
Sharing stories about our market and extract key observations
Introduction to Recursion
The compilation process
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Common Subexpression Elimination
Chapter 9 :: Subroutines and Control Abstraction
Functions Inputs Output
Stacks.
Closure Representations in Higher-Order Programming Languages
Continuations and Compilation
CSCE 489- Problem Solving Programming Strategies Spring 2018
Background In his classic 1972 paper on definitional interpreters, John Reynolds introduced two key techniques: Continuation-passing style - Makes.
Stacks.
Madhusudan Parthasarathy
Remote Procedure Call Hank Levy 1.
Remote Procedure Call Hank Levy 1.
Remote Procedure Call Hank Levy 1.
Presentation transcript:

A small part of what you should know about continuations, but were too afraid to ask Nick Benton

Continuations… One of the best ideas in CS Rediscovered in different forms/domains many times since 1964 Simple Mind-bogglingly complicated Very useful A good example of the interaction between theory and practice Profound: e.g. connections with classical logic Something which should be part of every computer scientist’s mental toolkit

They said I was crazy, but… Interest not entirely from programming language theoreticians:

Overstretched metaphors ahead…

Warm up. Get total sales figure. Insert it into report …

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it To get report for europe: 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it To get report for europe: 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it To get report for europe: 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it To get report for europe: 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it To get report for europe: 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it E E

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it E E

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it E E

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for europe: 1.Get report for europe 2.Look up the sales figure 3.Return it E E

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it A A

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it A A

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it A A

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it A A

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result

Warm up. Get total sales figure. Insert it into report …

Warm up. Get total sales figure. Insert it into report …

Warm up. Get total sales figure. Insert it into report … And so on… At any stage there is a list of workers All but the last are blocked waiting for a response When the active one completes her task, she returns a result to her immediate predecessor So she has to know who that is Various ways of arranging this: 1.Everybody sits in a line, passes to the right 2.Shared pile of names in the middle of the room 3.Make it part of the request

Pile of names. Get total sales figure. Insert it into report … To compute total sales: 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result To get sales for america: 1.Get report for america 2.Look up the sales figure 3.Return it To get report for america: 1.Search for america file 2.Take it out 3.Copy it 4.Replace original 5.Return copy A A Before delegating, put your name on top To return, take the top name off and send to that person

Part of request 1. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result to Whenever you delegate, send your name as part of the request Tasks end with “send the result to the person named in the request”

Part of request 2. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Return the result to To get europe sales for 1.Get report for europe 2.Look up the sales figure 3.Return it to

An optimization 1. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Return the result to To get europe sales for 1.Get report for europe 2.Look up the sales figure 3.Return it to If there were only one region, things would look like this…

An optimization 2. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Return the result to

An optimization 3. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Return the result to

An optimization 4. Get total sales figure. Insert it into report …

An optimization 5. Get total sales figure. Insert it into report …

An optimization 6. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Return the result to But that’s rather inefficient: Betty (in the middle) is just a postperson

An optimization 7. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Return the result to Betty tells Clare to give her an answer, which she passes directly to Angela

An optimization 8. Get total sales figure. Insert it into report … To compute total sales for 1.Get sales for europe 2.Err... that’s it! Much better: Betty tells Clare to return her answer directly to Angela:

An optimization 9. Get total sales figure. Insert it into report … When Clare’s finished…

Betty got to go home early But Angela’s hanging around for ages…. Get total sales figure. Insert it into report and then Err.. That’s it!

Now Betty’s instructions will have to take account of the fact that she’ll get another sheet of instructions To compute total sales and then do x 1.Get sales for europe 2.Get sales for america 3.Add them together 4.Do x with the result 5.That’s it Insert it into report Insert it into report

But Betty delegates too To compute total sales and then do x 1.Get sales for europe 1a. Get sales for america 2a. Add 1. and 1a. together 3a. Do x with the sum and then do (and that’s it)

But… To compute total sales and then do x 1.Get sales for europe 1a. Get sales for america and then do and then do 1b. Add 1. and 1a. together 2b. Do x with the sum

Clare and Daphne do… To get sales for c and then do y 1. get report for c and then do 1a. look up sales figure from 1 2a. do y with 1a. To get report for c and then do z : 1.Search for c file 2.Take it out 3.Copy it 4.Replace original 5.do z with 3.

So what happens? Get total sales and then insert it into report

Betty does 1.Get sales for europe 1a. Get sales for america and then do and then do 1b. Add 1. and 1a. together 2b. Do insert 1b into the report

Clare does 1. get report for europe and then do 1a. look up sales figure from 1 2a. do 1b. Get sales for america and then do 1c. Add 1a. and 1b. together 2c. Do insert 1c into the report

Daphne 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.do 1a. look up sales figure from 3 2a. do 1b. Get sales for america and then do 1c. Add 1a. and 1b. together 2c. Do insert 1c into the report

Daphne 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.do 1a. look up sales figure from 3 2a. do 1b. Get sales for america and then do 1c. Add 1a. and 1b. together 2c. Do insert 1c into the report E E

Daphne 1.Search for europe file 2.Take it out 3.Copy it 4.Replace original 5.do 1a. look up sales figure from 3 2a. do 1b. Get sales for america and then do 1c. Add 1a. and 1b. together 2c. Do insert 1c into the report E E E E

Clare-2 (??) 1. get report for america and then do 1a. look up sales figure from 1 2a. do 1b. Add 1a. and 1a. together 2b. Do insert 1b into the report

And so on… This is continuation-passing style (CPS) –Every function gets an extra argument saying what do do with the result –i.e. “the rest of the computation” –Every call becomes a tail-call! –So call-and-return replaced by “jump with arguments” –We’ve replaced a long line of workers with a single worker manipulating a large pile of bits of paper (frames on the heap rather than the stack)

How does that help? once you make continuations explicit you can manipulate them in interesting ways this gives complete control of control flow –you can ignore the continuation you were given and use a different one –you can pass it as an argument to someone else –you can duplicate it As a programmer, you can do all these things just with functions and semantically, you can explain all this just using proper mathematical functions

What sort of thing can you do with continuations? Early exit (abort) Exceptions Backtracking (Prolog-type stuff) Generators, iterators, whatever Implement tail calls on VMs that don’t have them Lightweight threads Security stuff Cool stuff in distributed setting –stateless servers –migratory applications … Reason

History Invented to give semantics to jumps (Algol 60) Studied and generalised Becomes a programming pattern Which is added as a language feature (Scheme, SML/NJ, Python) And used in compiler intermediate languages Still an active topic of research

But it looks horrid You don’t have to do it all explicitly Translation into CPS can be done by compiler Or leave most compilation alone and then add special implementations for continuation-manipulating primitives (copy stack to heap and vice-versa)

e.g. stateless servers

Questions?

Monadic reflection using call/cc Add nondeterminism (for example) to already- compiled code val pick : 'a list -> 'a val fail : unit -> 'a val results : (unit->'a)->'a list - results (fn () => let val x = pick [1,2] val y = pick [2,5] in if x = y then fail() else x+y end) > [3,6,7] : int list

Types