Docsity

Notes on Continuations - Programming Languages and Translators | CS 440, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Continuations March 11, 2009
1 Introduction and Objectives
Last time we saw that integers are unnecessary, because they can be implemented using functions.
Today we will discuss how returning from a function is also unnecessary, because we can pass in
a function that expresses what should happen next. This allows functions to represent the control
flow of a program, and the technique is called continuation passing style (CPS). After today’s
lecture, you should be able to
•explain what CPS is,
•give an example of a programming technique using CPS, and
•transform a simple function from direct style to CPS.
1.1 A Motivating Example
1gcd1 a 0 =a
2gcd1 a b |a<b=gcd1 b a
3|otherwise =gcd1 b $ a ‘mod‘ b
gcd 44 12 ⇒gcd 12 8 ⇒gcd 8 4 ⇒gcd 4 0 ⇒4
The running time of this function is roughly O(lg a).
Now suppose we’d like to take the GCD of an entire list.
1gcdstar [] =0
2gcdstar (x:xs) =gcd x (gcdstar xs)
Question: What will happen if there is a 1 near the beginning of the sequence?
1.2 Bad Solution I — Check and Return
1bad1 [] =0
2bad1 (x:xs) |x== 1=1
3|otherwise =gcd x $ bad1 xs
This stops the computation, but a lot of work has already been done.
1.3 Bad Solution II — Goto Statement
1 bad2 [] = 0
2 bad2 (x:xs) | x == 1 = goto 4
3 | otherwise = gcd x $ bad2 xx
4 return 1
Of course, this is nonsense.
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4

Partial preview of the text

Download Notes on Continuations - Programming Languages and Translators | CS 440 and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

Last time we saw that integers are unnecessary, because they can be implemented using functions. Today we will discuss how returning from a function is also unnecessary, because we can pass in a function that expresses what should happen next. This allows functions to represent the control flow of a program, and the technique is called continuation passing style (CPS). After today’s lecture, you should be able to

  • explain what CPS is,
  • give an example of a programming technique using CPS, and
  • transform a simple function from direct style to CPS.

1.1 A Motivating Example

1 gcd1 a 0 = a 2 gcd1 a b | a < b = gcd1 b a 3 | otherwise = gcd1 b $ a ‘mod‘ b

gcd 44 12 ⇒ gcd 12 8 ⇒ gcd 8 4 ⇒ gcd 4 0 ⇒ 4 The running time of this function is roughly O(lg a). Now suppose we’d like to take the GCD of an entire list.

1 gcdstar [] = 0 2 gcdstar (x:xs) = gcd x (gcdstar xs)

Question: What will happen if there is a 1 near the beginning of the sequence?

1.2 Bad Solution I — Check and Return

1 bad1 [] = 0 2 bad1 (x:xs) | x == 1 = 1 3 | otherwise = gcd x $ bad1 xs

This stops the computation, but a lot of work has already been done.

1.3 Bad Solution II — Goto Statement

1 bad2 [] = 0 2 bad2 (x:xs) | x == 1 = goto 4 3 | otherwise = gcd x $ bad2 xx 4 return 1

Of course, this is nonsense.

1.4 Okay Solution – Prefiltering

1 gcdstar3 xx | all (λ x → not $ x == 1) xx = gcdstar xx 2 | otherwise = 1

2 Continuations

2.1 Terms

  • A function is in Direct Style when it returns its result back to the caller.
  • A Tail Call occurs when a function returns the result of another function call without pro- cessing it first. - This is what is used in accumulator recursion.
  • A function is in Continuation Passing Style when it passes its result to another function.
    • Instead of returning the result to the caller, we pass it forward to another function.
    • Functions in CPS “never return”.

2.2 Definition of a Continuation

  • A continuation is a function into which is passed the result of the current function’s compu- tation.

1 report x = do print x 2 plusk a b k = k $ a + b

*Main> plusk 20 33 report 53 *Main> plusk 20 33 (\ r -> plusk 10 r report) 63

2.3 Continuation Solution

1 gcdstark xx k = aux xx k 2 where aux [] nk = nk 0 3 aux (x:xs) nk | x == 1 = k 1 4 | otherwise = aux xs (λ res → nk (gcd x res))

(gcdstar ’(44 12 80 6) report) 2 (gcdstar ’(44 12 1 80 6) report) 1

  • The function “isn’t going to return,” so we need to tell it where to put the result.

Step 4 A function call not in tail position needs to be built into a new continuation. Be sure your new continuation calls the old one if appropriate!

C[[(op (f arg))]] → f arg (r − > k(C[[op]] r))

2.7 Example

1 bar [] = 10 2 bar (x:xs) | x == 0 = bar xs 3 | otherwise = x + foo xs

1 bark [] k = k 10 -- rule 1, 2 2 bark (x:xs) k | x == 0 = foo xs k -- rule 3 3 | otherwise = foo xs (λ r → k (x + r)) -- rule 4

2.8 Activity

  • Try converting map and foldr.
  • The gcdstar example didn’t go as far as it could have, because the function gcd was left in direct style. Transform gcd into CPS, and then write the gcdstar function in CPS to use it. It will look very similar to the lecture CPS version of gcdstar.