Programming Languages CS 152 Avi Shinnar Lecture 13.

Slides:



Advertisements
Similar presentations
Improving Rotor for Dynamically Typed Languages Fabio Mascarenhas and Roberto Ierusalimschy.
Advertisements

Compiling Web Scripts for Apache Jacob Matthews Luke Hoban Robby Findler Rice University.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
A small part of what you should know about continuations, but were too afraid to ask Nick Benton.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
Prof. Fateman CS164 Lecture 241 Other Control Flow ideas: Throw, Catch, Continuations and Call/CC Lecture 24.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
CS2135 Lectures on Script Position. Recap: CGI scripts In the CGI protocol, there is no way to send the result of a script back to the page that invoked.
Continuations COS 441 Princeton University Fall 2004.
CS2135 Lectures on Script Position (aka: the “web compiler”)
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
7/2/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
ISBN Chapter 15 Functional Programming Languages.
A Revolutionary Programming Pattern that Will Clean up your Code : Coroutines in C++ David Sackstein ACCU 2015.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
1/21 4/6/2004 OOPS – One more example Goal: See an example that distinguishes between –“is-a” or inheritance relationships –“has-a” or local variable relationships.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Programming Languages: Design, Specification, and Implementation G Rob Strom September 28, 2006.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Additional Scheme examples
Programming Languages and Compilers (CS 421)
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closure conversion Compiling λ.
Programming Languages and Compilers (CS 421)
Class 19: Think Globally, Mutate Locally CS150: Computer Science
First-class continuations
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
Closure Representations in Higher-Order Programming Languages
Continuations and Compilation
Dynamic Scoping Lazy Evaluation
Continuations in Scheme
Lecture 12: Message passing The Environment Model
Madhusudan Parthasarathy
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Programming Languages and Compilers (CS 421)
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Programming Languages CS 152 Avi Shinnar Lecture 13

Continuations: What’s Next (+ 3 4)  ( (x) (* x 5)) [ ] (* (+ 3 4) 5)

Making the continuation explicit (+ 3 4)  ( (x) (* x 5)) (( (x) (* x 5)) (+ 3 4)) (+ 3 4 ( (x) (* x 5))) oops. back where we started. (+ 3 4 ( (x) (* x 5 k))) but it still returns 35! k represents what to do next

Exception continuations (throw ‘a ( (x) (* x 5 )) catcher) (try (* (throw ‘a) 5) catcher) k catcher

What’s next? (try (* (if b 4 (throw ‘a)) 5) catcher) [ ] catcher ( (x) (* x 5)) (if b 4 (throw ‘a)) normal exceptional

Exception continuations (if b (( (x) (* x 5 k catcher) 4) (throw ‘a ( (x) (* x 5 k catcher)) catcher)) catcher) (try (* (if b 4 (throw ‘a)) 5) catcher) or (4 … catcher) where 4 = ( (k err) (k 4))

Continuation Passing Style whole program everything goes forward –every (non-primitive) is a tail-call! –and they never return CPS conversion can be done automatically Primitives are normally left alone –so + and * would behave normally…

The point of no return Control –With explicit continuations, we can do cooler things --- coming soon simplicity one lambda to rule them all… Naming: all intermediate values have a name All sequencing is explicit –functional compilers stack frame is explicit CPS conversion –theory

map (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))

(map f ‘()) (map f ‘(3)) (map f ‘(2 3)) (map f ‘(1 2 3)) (f 2) (f 3) What’s going on (direct) (f 1) (map f ‘(1 2 3)) ‘() ‘(6) ‘(4 6) ‘(2 4 6) (define (f x) (+ x x)) f returnmap return (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))

CPS: map (define (map-cps f l k) (if (null? l) (k ‘()) (f (car l) (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))))))))

What’s going on (CPS) (map-cps f ‘(1 2 3) k) (define (f x k) (k (+ x x))) (f 1 (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))))) (map-cps f (cdr l) (λ (rest-cdr) (k (cons 2 rest-cdr)))) (f 2 (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) ((λ (rest-cdr) (k (cons 2 rest-cdr))) (cons fc rest-cdr))))) (define (map-cps f l k) (if (null? l) (k ‘()) (f (car l) (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))))))))

CPS: map w/exceptions (define (map-cps-exn f l k err) (if (null? l) (k ‘()) (f (car l) (λ (fc) (map-cps-exn f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))) err)) err)))

Web

Ajax xmlHttp.onreadystatechange

CPS: naming the continuation d’s stack frame c’s stack frame b’s stack frame a’s stack frame d’s stack frame kc’s stack frame kb’s stack frame ka’s stack frame k direct (normal) CPS

call-with-current-continuation (or call/cc to avoid RSI) Gives us a way to name and use the implicit continuation (call/cc (λ (k) …)) –k is a name for the implicit continuation like setjmp/longjmp –but cooler –and safer

[ ]  ( (x) (* x 5)) (* 5) [ ] “hole”-y Computations (+ 3 4) (* [(call/cc (λ (k) (+ 3 4)))] 5) 35

“hole”-y Computations (* [(call/cc (λ (k) (k 11)))] 5) 55 (* [(call/cc (λ (k) ( + 2 (if b (+ 3 4) (k 11)))))] 5) 45 b=true 55 b=false

“hole”-y Computations (define !hole #f) (* [(call/cc (λ (k) (set! !hole k) (+ 3 4)))] 5) 35 (!hole 11) 55 (!hole 20) 100

simple yield (define (map-yield f l !do-other-stuff) (map (λ (x) (display x) (space) (set! !do-other-stuff (call/cc !do-other- stuff)) (f x)) l) '()) !do-other-stuff, when it invokes the continuation picks the next place to yield to.

co-routines (define (map-yield2 f l1 l2) (map-yield f l1 (λ (k) (map-yield f l2 k)))) (map-yield2 (λ (x) (+ x x)) '( ) '( ))

And more… (continuable) exceptions generators (yield, resume…) –can turn an arbitrary traversal pattern into an iterator automatically general co-routines co-operative (non-preemptive) multitasking –threads explicitly yield backtracking –non-determinism –undo

Other Fun Stuff

Exception Handling ;; initialization (define !exn_handlers '()) (let ((is_exn (call/cc (λ (c) (set! !exn_handlers (cons c !exn_handlers)) #f)))) (if is_exn (display is_exn))) ;; throw: goto most recent exception handle continuation (define (throw v) ((car !exn_handlers) (cons 'exn v)))

Exception Handling (define (try-def e exn) (let ((v (call/cc (λ (c) ;; add ourselves (set! !exn_handlers (cons c !exn_handlers)) (cons 'ret (force e)))))) ;; remove ourselves (no matter what) (set! !exn_handlers (cdr !exn_handlers)) (if (eq? (car v) 'ret) (cdr v) ;; normal return (exn (cdr v))))) ;; exception: call handler (define-syntax try (syntax-rules () ((_ e exn) (try-def (delay e) exn))))

Design notes in-band v. out of band data –prevent-effects “-no-effects-for-me” –exceptional return from thread (cons ‘this-is-an-exception exn) prevent-effects –should it be observable? –is it?

effects-prevented? (define (effects-prevented?) (try (let ((!x 3)) (set! !x 5)) (λ (x) (eq? x 'prevent-effects))))

Data continuations Prof. Morrisett: Refining first-class stores #Morrisett93

CPS map: It could be worse (define (map-cps-full f l k) (if (null? l) (k ‘()) (car l (λ (carl) (f carl (λ (fc) (cdr l (λ (cdrl) (map-cps-full f cdrl (λ (rest-cdr) (cons fc rest-cdr k)))))))))))