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.

Slides:



Advertisements
Similar presentations
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Advertisements

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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
PPL Pairs, lists and data abstraction. Compound Data Until now: atomic, unrelated entities Now: organized into structures Why? – Better conceptual level.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Functional Programming: Lisp MacLennan Chapter 10.
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.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Functional Programming
Edited by Original material by Eric Grimson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Lecture 7: List Recursion CS200: Computer Science
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
COP4020 Programming Languages
Binomial Priority Queues
Nondeterministic Evaluation
Proving Properties of Recursive Functions and Data Structures
6.001 SICP Data abstractions
The Metacircular Evaluator
PPL Sequence Interface.
The Metacircular Evaluator
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
Chapter 4 Data and Behavior Abstraction
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Data Mutation Primitive and compound data mutators set! for names
topics mutable data structures
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Binomial Priority Queues
Presentation transcript:

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 factor))) tree))) (scale-tree '(((1 4) 2)) 5) (scale-tree '( ) 2)

Continuation Passing Style

Main idea: instead of returning a value, you pass it as a parameter to another function More specific: every user defined procedure f$ gets another parameter called continuation. When f$ ends we apply the continuation Distinction between creating the continuation and applying it All user defined function are in tail-position

Simple Examples: Normal (define square (lambda (x) (* x x))) (define add1 (lambda (x) (+ x 1))) CPS (define square$ (lambda (x cont) (cont (* x x))) (define add1$ (lambda (x cont) (cont (+ x 1))))

> (add1$ 5 (λ(x) x)) ( (λ(x) x) (+ 5 1) ) 6 > (square$ 5 (λ(x) x)) ( (λ(x) x) (* 5 5) ) 25 > (add1$ 5 (λ(x) (square$ x (λ(x) x)))) (define square$ (lambda (x cont) (cont (* x x))) (define add1$ (lambda (x cont) (cont (+ x 1)))) ( (λ(x) (square$ x (λ(x) x)))) (+ 5 1) ) ( (λ (x) x) 36 ) 36

Evaluation Order Order unknown (define h2 (λ (x y) (mult (square x) (add1 y)))) (define mult (λ (x y) (* x y))) We set the order (define h2$ (λ (x y cont) (square$ x (λ (square-res) (add1$ y (λ (add1-res) (mult$ square-res add1-res cont))))))) (define square$ (lambda (x cont) (cont (* x x))) (define add1$ (lambda (x cont) (cont (+ x 1))))

Does h2 equal to h2$ ? Claim: for every numbers x, y and continuation c : (h2$ x y c) = (c (h2 x y))

Proof a-e[ (h2$ x y cont) ] ==>* a-e[ (square$ x (λ (square-res) (add1$ y (λ (add1-res) (mult$ square-res add1-res cont))))) ] ==>* a-e[ ((λ (square-res) (add1$ y (λ (add1-res) (mult$ square-res add1-res cont)))) (* x x)) ] ==>* a-e[ (add1$ y (λ (add1-res) (mult$ x*x add1-res cont))) ] ==>* a-e[ ((λ (add1-res) (mult$ x*x add1-res cont)) (+ y 1)) ] ==>* a-e[ (mult$ x*x y+1 cont) ] ==>* a-e[ (cont (* x*x y+1)) ] = since a-e[ (* x*x y+1) ] = a-e[ (h2 x y) ] a-e[ (cont (h2 x y)) ]

In General A procedure f$ is CPS equivalent to a procedure f, if for every input value x 1, x 2,…, x n, n≥ 0: (f$ x 1 … x n ) = (cont (f x 1 … x n ))

So What’s the Big Deal? So we pass in a continuation. Doesn’t seem like a big deal, right? Things get more interesting when we work with recursions…

CPS is Good For: Order of computation (just seen) Turning recursion into iteration (seen in the past, see more now) The true power of CPS: Controlling multiple future computations

Recursion Into Iteration (define fact (λ (n) (if (= n 0) 1 (* n (fact (- n 1)))))) (fact 3)

(define fact$ (λ (n cont) (if (= n 0) (cont 1) (fact$ (- n 1) (λ (res) (cont (* n res)))))))

(fact$ 3 (λ (x) x)) ; ==> (fact$ 2 (λ (res 1 ) ((λ (x) x) (* 3 res 1 )))) ; ==> (fact$ 1 (λ (res 2 ) ((λ (res 1 ) ((λ (x) x) (* 3 res 1 ))) (* 2 res 2 )))) ; ==> (fact$ 0 (λ (res 3 ) ((λ (res 2 ) ((λ (res 1 ) ((λ (x) x) (* 3 res 1 ))) (* 2 res 2 ))) (* 1 res 3 )))) ( (λ (res 3 ) ( (λ (res 2 ) ( (λ (res 1 ) ( (λ (x) x) (* 3 res 1 ))) (* 2 res 2 ))) (* 1 res 3 ))) 1) ; ==> ( (λ (res 2 ) ( (λ (res 1 ) ( (λ (x) x) (* 3 res 1 ))) (* 2 res 2 ))) 1) ; ==> ( (λ (res 1 ) ( (λ (x) x) (* 3 res 1 ))) 2) ; ==> ( (λ (x) x) 6) 6

Proof that (fact$ n cont) = (cont (fact n)) Since fact is a recursive function, proof will be inductive.

בסיס האינדוקציה : a-e[ (fact$ 0 c) ] ==>* a-e[ (c 1) ] = a-e[ (c (fact 0)) ] הנחת האינדוקציה : עבור הטענה n=k מתקיימת לכל k≥i צעד האינדוקציה : יהא n=k+1, אזי : a-e[ (fact$ n c) ] ==>* a-e[ (fact$ (- n 1) (λ (res) (c (* n res)))) ] ==>* מהנחת האינדוקציה, נקבל : a-e [ ((λ (res) (c (* n res))) (fact (- n 1))) ] ==>* a-e [ (c (* n (fact (- n 1)))) ] = a-e [ (c (fact n)) ]

CPS Map (define map (λ (f lst) (if (null? lst) lst (cons (f (car lst)) (map f (cdr lst)))))) (define map$ (λ (f$ list c) (if (null? list) (c list) (f$ (car list) (λ (f-res) (map$ f$ (cdr list) (λ (map-cdr) (c (cons f-res map-cdr)))))))))

> (map$ (λ (x c) (c (* x x))) ‘(1 3) (λ (x) x)) ((λ (x c) (c (* x x))) 1 (λ (f-res) (map$ (λ (x c) (c (* x x)) ‘(3) (λ (map-res) ( (λ (x) x) (cons f-res map-res)) )))))) (define map$ (λ (f$ list c) (if (null? list) (c list) (f$ (car list) (λ (f-res) (map$ f$ (cdr list) (λ (map-res) (c (cons f-res map-res)))))))))

Map$ Another Version (define map$ (λ (f$ list cont) (if (null? list) (cont list) (map$ f$ (cdr list) (λ (map-res) (f$ (car list) (λ (f-res) (cont (cons f-res map-res)))))))))

Multiple Future Computation The true power of CPS Most useful example: errors – Errors are unplanned future – The primitive error breaks the calculation and returns void We want more control, and we can do it with CPS

Sum List with Error (define sumlist (lambda (li) (cond ((null? li) 0) ((not (number? (car li))) (error "non numeric value!")) (else (+ (car li) (sumlist (cdr li))))))) (sumlist '(1 2 a))

Try 1 (define sumlist (lambda (li) (cond ((null? li) 0) ((not (number? (car li))) (error "non numeric value!") 0) (else (+ (car li) (sumlist (cdr li)))))))

Try 2 (define sumlist (lambda (li) (cond ((null? li) 0) ((not (number? (car li))) #f) (else (let ((sum-cdr (sumlist (cdr li)))) (if sum-cdr (+ (car li) sum-cdr) #f))))))

Why is it so Complicated We are deep inside the recursion: the stack is full with frames and we need to “close” every one of them If only there was a way NOT to open frames on the stack…

Sum List with CPS (define sumlist$ (λ (l succ fail) (cond ( (null? l) (succ l) ) ( (number? (car l)) (sumlist$ (cdr l) (λ (sum-cdr-l) (succ (+ (car l) sum-cdr-l))) fail)) (else (fail l)))))

Run Example (sumlist$ '(1 2 3 a) (lambda (x) x) (lambda (x) (display x) (display " ") (display 'not-a-num)))

Fail Continuation For Backtracking ;; Purpose: Find the left most even leaf of a binary ;; tree whose leaves are labeled by numbers. ;; Type: [LIST -> Number union Boolean] ;; Examples: (leftmost-even ’((1 2) (3 4))) ==> 2 ;; (leftmost-even ’((1 1) (3 3))) ==> #f (define leftmost-even (λ (tree) (letrec ((iter (λ (tree) (cond ((null? tree) #f) ((not (list? tree)) (if (even? tree) tree #f)) (else (let ((res-car (iter (car tree)))) (if res-car res-car (iter (cdr tree))))))))) (iter tree)))) No CPS

Fail Continuation For Backtracking (define leftmost-even$ (λ (tree succ-cont fail-cont) (cond ((null? tree) (fail-cont)) ; Empty tree ((not (list? tree)) ; Leaf tree (if (even? tree) (succ-cont tree) (fail-cont))) (else ; Composite tree (leftmost-even$ (car tree) succ-cont (λ () (leftmost-even$ (cdr tree) succ-cont fail-cont)))))))

(leftmost-even$ ((1 2) (3 4)) (λ (x) x) (λ () #f)) ==> (leftmost-even$ (1 2) (λ (x) x) (λ () (leftmost-even$ ((3 4)) (λ (x) x) (λ () #f)))) ;==> (leftmost-even$ 1 (λ (x) x) (λ () (leftmost-even$ (2) (λ (x) x) (λ () (leftmost-even$ ((3 4)) (λ (x) x) (λ () #f)))))) ;==>* (leftmost-even$ (2) (λ (x) x) (λ () (leftmost-even$ ((3 4)) (λ (x) x) (λ () #f)))) ;==>* ( (λ (x) x) 2) ;==> 2

Construct Tree with CPS (define replace-leftmost (λ (tree old new succ-cont fail-cont) (cond ((null? tree) (fail-cont)) ; Empty tree ((not (list? tree)) ; Leaf tree (if (eq? tree old) (succ-cont new) (fail-cont))) (else ; Composite tree (replace-leftmost$ (car tree) (λ (car-res) (succ-cont (cons car-res (cdr tree)))) (λ () (replace-leftmost$ (cdr tree) (λ (cdr-res) (succ-cont (cons (car tree) cdr-res))) fail-cont)))))))

Moed A 2007

Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree factor))) tree))) (scale-tree '(((1 4) 2)) 5) (scale-tree '( ) 2)

Solution (define scale-tree$ (λ (tree factor c) (map$ (λ (sub-tree c) (if (list? sub-tree) (scale-tree$ sub-tree factor (λ (scale-sub-tree) (c scale-sub-tree))) (c (* sub-tree factor)))) tree c)))