Programming Languages and Compilers (CS 421)

Similar documents
Functions Over Lists. Programming Languages and Compilers (CS 421) Structural Recursion. Functions Over Lists. Structural Recursion : List Example

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Languages and Compilers (CS 421)

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

CS 321 Programming Languages

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

FUNCTIONAL PROGRAMMING

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

MP 2 Continuation-Passing Style

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Programming Languages and Compilers (CS 421)

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

Programming Languages and Compilers (CS 421)

CSCI-GA Scripting Languages

School of Computer Science

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

Programming Languages and Compilers (CS 421)

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Topic 5: Examples and higher-order functions

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

List Functions, and Higher-Order Functions

Continuations and Continuation-Passing Style

n Takes abstract syntax trees as input n In simple cases could be just strings n One procedure for each syntactic category

Functional Programming. Pure Functional Programming

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

Recap: Functions as first-class values

CSE 341 : Programming Languages Midterm, Spring 2015

Gain familiarity and practice with folding and mapping. The following supplementary materials may be helpful in completing this assignment:

Continuation Passing Style. Continuation Passing Style

Factorial. Next. Factorial. How does it execute? Tail recursion. How does it execute? More on recursion. Higher-order functions

Introduction to Objective Caml

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Programming Languages and Compilers (CS 421)

Next. Tail Recursion: Factorial. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial.

Konzepte von Programmiersprachen

Compilers: The goal. Safe. e : ASM

Coq Summer School, Session 2 : Basic programming with numbers and lists. Pierre Letouzey

CS 421, Fall Sample Final Questions & Solutions

FOFL and FOBS: First-Order Functions

SNU Programming Language Theory

Playing with Fun. Walter Cazzola. Playing with Fun currying partial evaluation map&reduce iteration var args. References.

CSE 130 [Winter 2014] Programming Languages

CS3110 Spring 2016 Lecture 6: Modules

Accurate Step Counting

Verification of an ML compiler. Lecture 3: Closures, closure conversion and call optimisations

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Introduction to OCaml

CS558 Programming Languages

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CPS Conversion. Di Zhao.

Some Advanced ML Features

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CSci 4223 Principles of Programming Languages

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CS3110 Spring 2017 Lecture 7 Specifications using types continued

CS4410 : Spring 2013

CS422 - Programming Language Design

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

Lecture 4: Higher Order Functions

CSCI 2041: First Class Functions

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CS 11 Ocaml track: lecture 2

Programming Languages and Compilers (CS 421)

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

CSE 307: Principles of Programming Languages

The Art of Recursion: Problem Set 10

CSE 307: Principles of Programming Languages

Functional Programming. Pure Functional Programming

Parsing Scheme (+ (* 2 3) 1) * 1

Begin at the beginning

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Scope, Functions, and Storage Management

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Recursion & Iteration

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Data Types The ML Type System

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

Shell CSCE 314 TAMU. Higher Order Functions

Handout 2 August 25, 2008

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Transcription:

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 9/11/17 1

Functions Over Lists # let rec double_up list = match list with [ ] -> [ ] (* pattern before ->, expression after *) (x :: xs) -> (x :: x :: double_up xs);; val double_up : 'a list -> 'a list = <fun> # let fib5_2 = double_up fib5;; val fib5_2 : int list = [8; 8; 5; 5; 3; 3; 2; 2; 1; 1; 1; 1] 9/11/17 2

Functions Over Lists # let silly = double_up ["hi"; "there"];; val silly : string list = ["hi"; "hi"; "there"; "there"] # let rec poor_rev list = match list with [] -> [] (x::xs) -> poor_rev xs @ [x];; val poor_rev : 'a list -> 'a list = <fun> # poor_rev silly;; - : string list = ["there"; "there"; "hi"; "hi"] 9/11/17 3

Structural Recursion n Functions on recursive datatypes (eg lists) tend to be recursive n Recursion over recursive datatypes generally by structural recursion n Recursive calls made to components of structure of the same recursive type n Base cases of recursive types stop the recursion of the function 9/11/17 4

Structural Recursion : List Example # let rec length list = match list with [ ] -> 0 (* Nil case *) x :: xs -> 1 + length xs;; (* Cons case *) val length : 'a list -> int = <fun> # length [5; 4; 3; 2];; - : int = 4 n Nil case [ ] is base case n Cons case recurses on component list xs 9/11/17 5

Forward Recursion n In Structural Recursion, split input into components and (eventually) recurse on components n Forward Recursion form of Structural Recursion n In forward recursion, first call the function recursively on all recursive components, and then build final result from partial results n Wait until whole structure has been traversed to start building answer 9/11/17 6

Forward Recursion: Examples # let rec double_up list = match list with [ ] -> [ ] (x :: xs) -> (x :: x :: double_up xs);; val double_up : 'a list -> 'a list = <fun> # let rec poor_rev list = match list with [] -> [] (x::xs) -> let pr = poor_rev xs in pr @ [x];; val poor_rev : 'a list -> 'a list = <fun> 9/11/17 7

Question n How do you write length with forward recursion? let rec length l = 9/11/17 8

Question n How do you write length with forward recursion? let rec length l = match l with [] -> (a :: bs) -> 9/11/17 9

Question n How do you write length with forward recursion? let rec length l = match l with [] -> (a :: bs) -> 1 + length bs 9/11/17 10

Question n How do you write length with forward recursion? let rec length l = match l with [] -> 0 (a :: bs) -> 1 + length bs 9/11/17 11

Your turn now Try Problem 2 on ML2 9/11/17 12

An Important Optimization Normal call h g f n When a function call is made, the return address needs to be saved to the stack so we know to where to return when the call is finished n What if f calls g and g calls h, but calling h is the last thing g does (a tail call)? 9/11/17 13

An Important Optimization Tail call h f n When a function call is made, the return address needs to be saved to the stack so we know to where to return when the call is finished n What if f calls g and g calls h, but calling h is the last thing g does (a tail call)? n Then h can return directly to f instead of g 9/11/17 14

Tail Recursion n A recursive program is tail recursive if all recursive calls are tail calls n Tail recursive programs may be optimized to be implemented as loops, thus removing the function call overhead for the recursive calls n Tail recursion generally requires extra accumulator arguments to pass partial results n May require an auxiliary function 9/11/17 15

Example of Tail Recursion n Forward recursive: # let rec prod l = match l with [] -> 1 (x :: rem) -> x * prod rem;; val prod : int list -> int = <fun> n Tail recursive: # let prod list = let rec prod_aux l acc = match l with [] -> acc (y :: rest) -> prod_aux rest (acc * y) (* Uses associativity of multiplication *) in prod_aux list 1;; val prod : int list -> int = <fun> 9/11/17 16

Question n How do you write length with tail recursion? let length l = 9/11/17 17

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = in 9/11/17 18

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> (a :: bs) -> in 9/11/17 19

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> in 9/11/17 20

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux in 9/11/17 21

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux bs in 9/11/17 22

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux bs (n + 1) in 9/11/17 23

Question n How do you write length with tail recursion? let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux bs (n + 1) in length_aux l 0 9/11/17 24

Your turn now Try Problem 4 on MP2 9/11/17 25

Mapping Recursion n One common form of structural recursion applies a function to each element in the structure # let rec doublelist list = match list with [ ] -> [ ] x::xs -> 2 * x :: doublelist xs;; val doublelist : int list -> int list = <fun> # doublelist [2;3;4];; - : int list = [4; 6; 8] 9/11/17 26

Mapping Functions Over Lists # let rec map f list = match list with [] -> [] (h::t) -> (f h) :: (map f t);; val map : ('a -> 'b) -> 'a list -> 'b list = <fun> # map plus_two fib5;; - : int list = [10; 7; 5; 4; 3; 3] # map (fun x -> x - 1) fib6;; : int list = [12; 7; 4; 2; 1; 0; 0] 9/11/17 27

Mapping Recursion n Can use the higher-order recursive map function instead of direct recursion # let doublelist list = List.map (fun x -> 2 * x) list;; val doublelist : int list -> int list = <fun> # doublelist [2;3;4];; - : int list = [4; 6; 8] n Same function, but no rec 9/11/17 28

Your turn now Write a function make_app : (( a -> b) * a) list -> b list that takes a list of function input pairs and gives the result of applying each function to its argument. Use map, no explicit recursion. let make_app l = 9/11/17 29

Folding Recursion n Another common form folds an operation over the elements of the structure # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist xs;; val multlist : int list -> int = <fun> # multlist [2;4;6];; - : int = 48 n Computes (2 * (4 * (6 * 1))) 9/11/17 30

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; - : int = 9 # let rec prodlist list = match list with [ ] -> 1 x::xs -> x * prodlist xs;; val prodlist : int list -> int = <fun> # prodlist [2;3;4];; - : int = 24 9/11/17 31

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; - : int = 9 Base Case # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist xs;; val multlist : int list -> int = <fun> # multlist [2;3;4];; - : int = 24 9/11/17 32

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; Recursive Call - : int = 9 # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist xs;; val multlist : int list -> int = <fun> # multlist [2;3;4];; - : int = 24 9/11/17 33

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; Head Element - : int = 9 # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist xs;; val multlist : int list -> int = <fun> # multlist [2;3;4];; - : int = 24 9/11/17 34

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; Combining Operation - : int = 9 # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist xs;; val multlist : int list -> int = <fun> # multlist [2;3;4];; - : int = 24 9/11/17 35

Folding Functions over Lists How are the following functions similar? # let rec sumlist list = match list with [ ] -> 0 x::xs -> x + sumlist Rec value xs;; val sumlist : int list -> int = <fun> # sumlist [2;3;4];; Combining Operation - : int = 9 # let rec multlist list = match list with [ ] -> 1 x::xs -> x * multlist Rec value xs;; val multlist : int list -> int = <fun> # multlist [2;3;4];; - : int = 24R 9/11/17 36

Recursing over lists # let rec fold_right f list b = match list with [] -> b (x :: xs) -> f x (fold_right f xs b);; The Primitive Recursion Fairy val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = <fun> # fold_right (fun s -> fun () -> print_string s) ["hi"; "there"] ();; therehi- : unit = () 9/11/17 37

Folding Recursion n multlist folds to the right n Same as: # let multlist list = List.fold_right (fun x -> fun p -> x * p) list 1;; val multlist : int list -> int = <fun> # multlist [2;4;6];; - : int = 48 9/11/17 38

Encoding Recursion with Fold # let rec append list1 list2 = match list1 with [ ] -> list2 x::xs -> x :: append xs list2;; val append : 'a list -> 'a list -> 'a list = <fun> Base Case Operation Recursive Call # let append list1 list2 = fold_right (fun x y -> x :: y) list1 list2;; val append : 'a list -> 'a list -> 'a list = <fun> # append [1;2;3] [4;5;6];; - : int list = [1; 2; 3; 4; 5; 6] 9/11/17 39

Question let rec length l = match l with [] -> 0 (a :: bs) -> 1 + length bs n How do you write length with fold_right, but no explicit recursion? 9/11/17 40

Question let rec length l = match l with [] -> 0 (a :: bs) -> 1 + length bs n How do you write length with fold_right, but no explicit recursion? let length list = List.fold_right (fun x -> fun n -> n + 1) list 0 9/11/17 41

Map from Fold # let map f list = fold_right (fun x -> fun y -> f x :: y) list [ ];; val map : ('a -> 'b) -> 'a list -> 'b list = <fun> # map ((+)1) [1;2;3];; - : int list = [2; 3; 4] n Can you write fold_right (or fold_left) with just map? How, or why not? 9/11/17 42

Iterating over lists # let rec fold_left f a list = match list with [] -> a (x :: xs) -> fold_left f (f a x) xs;; val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun> # fold_left (fun () -> print_string) () ["hi"; "there"];; hithere- : unit = () 9/11/17 43

Encoding Tail Recursion with fold_left # let prod list = let rec prod_aux l acc = match l with [] -> acc (y :: rest) -> prod_aux rest (acc * y) in prod_aux list 1;; val prod : int list -> int = <fun> Init Acc Value Recursive Call Operation # let prod list = List.fold_left (fun acc y -> acc * y) 1 list;; val prod: int list -> int = <fun> # prod [4;5;6];; - : int =120 9/11/17 44

Question let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux bs (n + 1) in length_aux l 0 n How do you write length with fold_left, but no explicit recursion? 9/11/17 45

Question let length l = let rec length_aux list n = match list with [] -> n (a :: bs) -> length_aux bs (n + 1) in length_aux l 0 n How do you write length with fold_left, but no explicit recursion? let length list = List.fold_left (fun n -> fun x -> n + 1) 0 list 9/11/17 46

Folding # let rec fold_left f a list = match list with [] -> a (x :: xs) -> fold_left f (f a x) xs;; val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun> fold_left f a [x 1 ; x 2 ; ;x n ] = f( (f (f a x 1 ) x 2 ) )x n # let rec fold_right f list b = match list with [ ] -> b (x :: xs) -> f x (fold_right f xs b);; val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b = <fun> fold_right f [x 1 ; x 2 ; ;x n ] b = f x 1 (f x 2 ( (f x n b) )) 9/11/17 47

Recall # let rec poor_rev list = match list with [] -> [] (x::xs) -> poor_rev xs @ [x];; val poor_rev : 'a list -> 'a list = <fun> n What is its running time? 9/11/17 48

Quadratic Time n Each step of the recursion takes time proportional to input n Each step of the recursion makes only one recursive call. n List example: # let rec poor_rev list = match list with [] -> [] (x::xs) -> poor_rev xs @ [x];; val poor_rev : 'a list -> 'a list = <fun> 9/11/17 49

Tail Recursion - Example # let rec rev_aux list revlist = match list with [ ] -> revlist x :: xs -> rev_aux xs (x::revlist);; val rev_aux : 'a list -> 'a list -> 'a list = <fun> # let rev list = rev_aux list [ ];; val rev : 'a list -> 'a list = <fun> n What is its running time? 9/11/17 50

Comparison n poor_rev [1,2,3] = n (poor_rev [2,3]) @ [1] = n ((poor_rev [3]) @ [2]) @ [1] = n (((poor_rev [ ]) @ [3]) @ [2]) @ [1] = n (([ ] @ [3]) @ [2]) @ [1]) = n ([3] @ [2]) @ [1] = n (3:: ([ ] @ [2])) @ [1] = n [3,2] @ [1] = n 3 :: ([2] @ [1]) = n 3 :: (2:: ([ ] @ [1])) = [3, 2, 1] 9/11/17 51

Comparison n rev [1,2,3] = n rev_aux [1,2,3] [ ] = n rev_aux [2,3] [1] = n rev_aux [3] [2,1] = n rev_aux [ ] [3,2,1] = [3,2,1] 9/11/17 52

Folding - Tail Recursion - # let rev list = - fold_left - (fun l -> fun x -> x :: l) //comb op [] //accumulator cell list 9/11/17 53

Folding n Can replace recursion by fold_right in any forward primitive recursive definition n Primitive recursive means it only recurses on immediate subcomponents of recursive data structure n Can replace recursion by fold_left in any tail primitive recursive definition 9/11/17 54

Continuation Passing Style n A programming technique for all forms of non-local control flow: n non-local jumps n exceptions n general conversion of non-tail calls to tail calls n Essentially it s a higher-order function version of GOTO 9/11/17 55

Continuations n Idea: Use functions to represent the control flow of a program n Method: Each procedure takes a function as an argument to which to pass its result; outer procedure returns no result n Function receiving the result called a continuation n Continuation acts as accumulator for work still to be done 9/11/17 56

Example of Tail Recursion # let rec app fl x = match fl with [] -> x (f :: rem_fs) -> f (app rem_fs x);; val app : ('a -> 'a) list -> 'a -> 'a = <fun> # let app fs x = let rec app_aux fl acc= match fl with [] -> acc (f :: rem_fs) -> app_aux rem_fs (fun z -> acc (f z)) in app_aux fs (fun y -> y) x;; val app : ('a -> 'a) list -> 'a -> 'a = <fun> 9/11/17 57

Continuation Passing Style n Writing procedures so that they take a continuation to which to give (pass) the result, and return no result, is called continuation passing style (CPS) 9/11/17 58

Example of Tail Recursion & CSP # let app fs x = let rec app_aux fl acc= match fl with [] -> acc (f :: rem_fs) -> app_aux rem_fs (fun z -> acc (f z)) in app_aux fs (fun y -> y) x;; val app : ('a -> 'a) list -> 'a -> 'a = <fun> # let rec appk fl x k = match fl with [] -> k x (f :: rem_fs) -> appk rem_fs x (fun z -> k (f z));; val appk : ('a -> 'a) list -> 'a -> ('a -> 'b) -> 'b 9/11/17 59

Continuation Passing Style n A compilation technique to implement nonlocal control flow, especially useful in interpreters. n A formalization of non-local control flow in denotational semantics n Possible intermediate state in compiling functional code 9/11/17 60

Terms n A function is in Direct Style when it returns its result back to the caller. n A Tail Call occurs when a function returns the result of another function call without any more computations (eg tail recursion) n A function is in Continuation Passing Style when it passes its result to another function. n Instead of returning the result to the caller, we pass it forward to another function. 9/11/17 61

Continuation Passing Style n A compilation technique to implement nonlocal control flow, especially useful in interpreters. n A formalization of non-local control flow in denotational semantics n Possible intermediate state in compiling functional code 9/11/17 62

Example n Simple reporting continuation: # let report x = (print_int x; print_newline( ) );; val report : int -> unit = <fun> n Simple function using a continuation: # let plusk a b k = k (a + b) val plusk : int -> int -> (int -> a) -> a = <fun> # plusk 20 22 report;; 42 - : unit = () 9/11/17 63

Simple Functions Taking Continuations n Given a primitive operation, can convert it to pass its result forward to a continuation n Examples: # let subk x y k = k(x + y);; val subk : int -> int -> (int -> 'a) -> 'a = <fun> # let eqk x y k = k(x = y);; val eqk : 'a -> 'a -> (bool -> 'b) -> 'b = <fun> # let timesk x y k = k(x * y);; val timesk : int -> int -> (int -> 'a) -> 'a = <fun> 9/11/17 64

Nesting Continuations # let add_three x y z = x + y + z;; val add_three : int -> int -> int -> int = <fun> # let add_three x y z= let p = x + y in p + z;; val add_three : int -> int -> int -> int = <fun> # let add_three_k x y z k = addk x y (fun p -> addk p z k );; val add_three_k : int -> int -> int -> (int -> 'a) -> 'a = <fun> 9/11/17 65