DOC PREVIEW
U of I CS 421 - Continuation Passing Style

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineContinuationsContinuation Passing StyleCPS by HandCS421 Lecture 21: Continuation Passing Style1Mark [email protected] of Illinois at Urbana-ChampaignJuly 27, 20061Based on slides by Mattox Beckman, as updated by Vikram Adve, GulAgha, and Elsa GunterMark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuationsContinuation Passing StyleCPS by HandMark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandQuestionCan we use functions to represent the control flow in a program?Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuationsYes,using the concept of a continuation.◮We will augment each procedure with an additi onal argument– a function to which it will pass the current computationalresult.◮The outer procedure “returns” no result – it will be kept inthe function argument◮This function argument, receiving the res ult, will be called thecontinuation.◮Note: We saw the use of continuations briefly in the lastlecture.Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformContinuation Passing StyleWriting procedures so that they take a continuation to which theypass on t he computation result, and which return no result, iscalled continuation passing style (CPS)Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformContinuation Passing StyleCPS provides a programming technique for all forms of “non-local”control flow:◮non-local jumps◮exceptions◮etcCPS turns all non-tail calls into t ail calls.◮Essentially a higher-order functional GOTOMark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformContinuation Passing Style◮CPS also acts as a compilation technique to implementnon-local control flow◮Especially useful in interpreters◮Also acts as a formalization of non-lo cal control flow indenotational semantics (again, mentioned i n last le cture)Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformCPS Terminology◮A function i s in direct style when it returns its result back tothe caller (this is a standard function)◮A tail call occurs when a function returns the result ofanother function call without any more computations (li ke intail recursion, but not restricted to just recursive calls)◮A function is in continuation passing style when it passes itsresult to another function instead of back to its caller –essentially, we pass the result forward, not backwardsMark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformExampleA simple reporting continuation:1 # let report x =2 (print_int x; print_newline( ) );;3 val report : int -> unit = <fun>And a function that use s it:1 # let plusk a b k = k (a + b)2 val plusk : int -> int -> (int -> ’a) -> ’a = <fun>3 # plusk 20 22 report;;4 425 - : unit = ()Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformRecursive FunctionsRecall our defi nition of factorial:1 # let rec factorial n =2 if n = 0 then 1 else n * factorial (n - 1);;3 val factorial : int -> int = <fun>4 # factorial 5;;5 - : int = 120Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformRecursive FunctionsNow for our version with continuations:1 # let rec factorialk n k =2 if n = 03 then k 14 else factorialk (n - 1) (fun m -> k (n * m));;5 val factorialk : int -> (int -> ’a) -> ’a = <fun>6 # factorialk 5 report;;7 1208 - : unit = ()◮Note that our factorial calculation i s now tail recursive◮To make it work, we had to◮take a recursive value, m◮build it to a final result, n * m◮then pass it to the final continuation, k (n * m)Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformAnother Example: Length, Take 11 # let rec lengthk list k =2 match list with3 | [ ] -> k 04 | x :: xs -> lengthk xs (fun r -> k (r + 1));;5 val lengthk : ’a list -> (int -> ’b) -> ’b = <fun>6 # lengthk [2;4;6;8] report;;7 48 - : unit = ()Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformAnother Example: Length, Take 21 # let rec lengthk list k =2 match list with3 | [ ] -> k 04 | x :: xs -> lengthk xs (fun r -> plusk r 1 k);;5 val lengthk : ’a list -> (int -> ’b) -> ’b = <fun>6 # lengthk [2;4;6;8] report;;7 48 - : unit = ()Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing StyleCPS by HandContinuation Passing StyleCPS TerminologyExamplesCPS and ExceptionsMore TerminologyPerforming the TransformExample: Exceptions1 # exception Zero;;2 exception Zero3 # let rec list_mult_aux list =4 match list with [ ] -> 15 | x :: xs -> if x = 0 then raise Zero6 else x * list_mult_aux xs;;7 val list_mult_aux : int list -> int = <fun>8 # let rec list_mult list =9 try list_mult_aux list with Zero -> 0;;10 val list_mult : int list -> int = <fun>11 # list_mult [3;4;2];;12 - : int = 2413 # list_mult [7;4;0];;14 - : int = 015 # list_mult_aux [7;4;0];;16 Exception: Zero.Mark Hills CS421 Lecture 21: Continuation Passing StyleOutlineContinuationsContinuation Passing


View Full Document

U of I CS 421 - Continuation Passing Style

Documents in this Course
Lecture 2

Lecture 2

12 pages

Exams

Exams

20 pages

Lecture

Lecture

32 pages

Lecture

Lecture

21 pages

Lecture

Lecture

15 pages

Lecture

Lecture

4 pages

Lecture

Lecture

68 pages

Lecture

Lecture

68 pages

Lecture

Lecture

84 pages

s

s

32 pages

Parsing

Parsing

52 pages

Lecture 2

Lecture 2

45 pages

Midterm

Midterm

13 pages

LECTURE

LECTURE

10 pages

Lecture

Lecture

5 pages

Lecture

Lecture

39 pages

Load more
Download Continuation Passing Style
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Continuation Passing Style and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Continuation Passing Style 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?