71
Recursion in C What does the following program compute? int f(int n, int v) { if (n == 0) return v; else return f(n - 1, v + n); } int main() { return f(1000000, 0); } Answer: stack overflow 1-2

Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Embed Size (px)

Citation preview

Page 1: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in C

What does the following program compute?

int f(int n, int v) { if (n == 0) return v; else return f(n - 1, v + n);} int main() { return f(1000000, 0); }

Answer: stack overflow

1-2

Page 2: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Loops in C

What does the following program compute?

int main() { int n = 1000000; int v = 0; while (n > 0) { v = v + n; n = n - 1; } return v;}

Answer: the sum of 0 to 1000000 (in 32-bit two's complement)

3-4

Page 3: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in the Book Language

What does the following program compute?

letrec f = proc(n, v) if n then (f -(n,1) +(n, v)) else v in (f 1000000 0)

Answer: the sum of 0 to 1000000

5-6

Page 4: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in the Book Language

Why don't we get a stack overflow in the book language?

• This is actually a question about Scheme:

We implement recursion for the book language by usingScheme's recursion

Similarly, we use Scheme numbers to implement numbers forthe book language

• Such an explanation/implementation is called meta-circular

• We don't care so much about numbers, but we don't want to explainaway recursion; we want to understand recursion (in Scheme andthe book language)

7-8

Page 5: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

What does the following program compute?

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 1000000 0)

Answer: the sum of 0 to 1000000

How?

9-10

Page 6: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 1000000 0)

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 1000000) 0 (f (- 1000000 1) (+ 1000000 0))))

11

Page 7: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 1000000) 0 (f (- 1000000 1) (+ 1000000 0))))

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f (- 1000000 1) (+ 1000000 0))12

Page 8: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f (- 1000000 1) (+ 1000000 0))

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 999999 1000000)

13

Page 9: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 999999 1000000)

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 999999) 1000000 (f (- 999999 1) (+ 999999 1000000))))

14

Page 10: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 999999) 1000000 (f (- 999999 1) (+ 999999 1000000))))

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f (- 999999 1) (+ 999999 1000000))15

Page 11: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f (- 999999 1) (+ 999999 1000000))

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 999998 1999999)

16

Page 12: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(f 999998 1999999)

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

...

17

Page 13: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

...

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 0) 500000500000 (f (- 0 1) (+ 0 500000500000))))

18

Page 14: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

(if (zero? 0) 500000500000 (f (- 0 1) (+ 0 500000500000))))

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

50000050000019

Page 15: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

• Our definition of Scheme says nothing about a stack

• Each step from a small program produces a small program...

• We can forget the old small program after each step...

• So there's no reason to run out of anything!

• Does that mean that we can write anything, without worrying aboutrunning out of space?

20-21

Page 16: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

What does the following program compute?

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(f2 1000000)

Answer: out of memory, maybe

22-23

Page 17: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(f2 1000000)

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(if (zero? 1000000) 0 (+ 1000000 (f2 (- 1000000 1))))

24

Page 18: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(if (zero? 1000000) 0 (+ 1000000 (f2 (- 1000000 1))))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (f2 (- 1000000 1)))

25

Page 19: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (f2 (- 1000000 1)))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (f2 999999))

26

Page 20: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (f2 999999))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (if (zero? 999999) 0 (+ 999999 (f2 (- 999999 1)))))

27

Page 21: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (if (zero? 999999) 0 (+ 999999 (f2 (- 999999 1)))))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (+ 999999 (f2 (- 999999 1)))) 28

Page 22: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (+ 999999 (f2 (- 999999 1))))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (+ 999999 (f2 999998)))

29

Page 23: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (+ 999999 (f2 999998)))

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

...

30

Page 24: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Recursion in Scheme

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

...

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

(+ 1000000 (+ 999999 (+ 999998 ... (+ 1 0))))

31

Page 25: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

The Cost of Recursion

• Computing (f n) takes O(1) space

• Computing (f2 n) takes O(n) space

• In Scheme, we write loops and more general forms of recursion inthe same way, but there's still a difference in costs

• How does a Scheme programmer write a loop?

32-33

Page 26: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Loops in Scheme

• A function (or set of functions) acts like a loop if every self-call is atail call

• A tail call is a function call whose value is the final result for thecaller

(define (f n v) (if (zero? n) v (f (- n 1) (+ n v))))

a tail call

34

Page 27: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Loops in Scheme

• A function (or set of functions) acts like a loop if every self-call is atail call

• A tail call is a function call whose value is the final result for thecaller

(define (f2 n) (if (zero? n) 0 (+ n (f2 (- n 1)))))

not a tail call

35

Page 28: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Loops in Scheme

• A function (or set of functions) acts like a loop if every self-call is atail call

• A tail call is a function call whose value is the final result for thecaller

(define (odd n) (if (zero? n) #f (even (- n 1)))(define (even n) (if (zero? n) #t (odd (- n 1)))

tail calls

36

Page 29: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Implementing Control Explicitly

• Ok, so how is it done?

• We'll change our interpreter to make control explicit

• Let's first see what a trace of evaluation should look like

37-38

Page 30: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation

1

exp= 1env= {}

Done!

39-41

Page 31: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation

+(1, 2)

exp= +(1, 2)env= {}

exp= 1env= {}

Done?

42-45

Page 32: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation

+(1, 2)

exp= +(1, 2)env= {}

exp= 1env= {}

exp= 2env= {}

How do we know when we're done?

How do we know what's left to do?

46-47

Page 33: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

1

exp= 1env= {}todo= [done]

• Keep a to-do list, passed to evaluator

48-49

Page 34: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

1

exp= 1env= {}todo= [done]

val= 1todo= [done]

• When we get a value, go into to-do-checking mode

50

Page 35: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

1

exp= 1env= {}todo= [done]

val= 1todo= [done]

Done!

51

Page 36: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, 2)

exp= +(1, 2)env= {}todo= [done]

exp= 1env= {}todo= [addexp 2 in {} then [done]]

• When evaluating sub-expressions, extend the to-do list

• addexp is an abbreviation for:remember the result, evaluate another expression,then add the two results

52-54

Page 37: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, 2)

exp= +(1, 2)env= {}todo= [done]

exp= 1env= {}todo= [addexp 2 in {} then [done]]

val= 1todo= [addexp 2 in {} then [done]]

55

Page 38: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

val= 1todo= [addexp 2 in {} then [done]]

exp= 2env= {}todo= [addval 1 then [done]]

• To do addexp, we start evaluating the remembered expression inthe remembered environment

• Extend to-do list to remember the value we already have, andremember to do an addition later

• addval is an abbreviation for:add the result with a remembered result

56-57

Page 39: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

val= 1todo= [addexp 2 in {} then [done]]

exp= 2env= {}todo= [addval 1 then [done]]

val= 2todo= [addval 1 then [done]]

val= 3todo= [done]

Done!

58-60

Page 40: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= +(1, +(2, 3))env= {}todo= [done]

61

Page 41: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= +(1, +(2, 3))env= {}todo= [done]

exp= 1env= {}todo= [addexp +(2, 3) in {} then [done]]

62

Page 42: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= 1env= {}todo= [addexp +(2, 3) in {} then [done]]

val= 1todo= [addexp +(2, 3) in {} then [done]]

63

Page 43: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

val= 1todo= [addexp +(2, 3) in {} then [done]]

exp= +(2, 3)env= {}todo= [addval 1 then [done]]

64

Page 44: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= +(2, 3)env= {}todo= [addval 1 then [done]]

exp= 2env= {}todo= [addexp 3 in {} then [addval 1 then [done]]]

65

Page 45: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= 2env= {}todo= [addexp 3 in {} then [addval 1 then [done]]]

val= 2todo= [addexp 3 in {} then [addval 1 then [done]]]

66

Page 46: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

val= 2todo= [addexp 3 in {} then [addval 1 then [done]]]

exp= 3env= {}todo= [addval 2 then [addval 1 then [done]]]

67

Page 47: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

exp= 3env= {}todo= [addval 2 then [addval 1 then [done]]]

val= 3todo= [addval 2 then [addval 1 then [done]]]

68

Page 48: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

val= 3todo= [addval 2 then [addval 1 then [done]]]

val= 5todo= [addval 1 then [done]]

69

Page 49: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

+(1, +(2, 3))

val= 5todo= [addval 1 then [done]]

val= 6todo= [done]

70

Page 50: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= let f = proc(y)y in (f 10)env= {}todo= [done]

71

Page 51: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= let f = proc(y)y in (f 10)env= {}todo= [done]

exp= proc(y)yenv= {}todo= [let f in (f 10) {} then [done]]

72

Page 52: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= proc(y)yenv= {}todo= [let f in (f 10) {} then [done]]

val= <y,y,{}>todo= [let f in (f 10) {} then [done]]

73

Page 53: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

val= <y,y,{}>todo= [let f in (f 10) {} then [done]]

exp= (f 10)env= {f=<y,y,{}>}todo= [done]

74

Page 54: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= (f 10)env= {f=<y,y,{}>}todo= [done]

exp= fenv= {f=<y,y,{}>}todo= [apparg 10 in {f=<y,y,{}>} then [done]]

75

Page 55: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= fenv= {f=<y,y,{}>}todo= [apparg 10 in {f=<y,y,{}>} then [done]]

val= <y,y,{}>todo= [apparg 10 in {f=<y,y,{}>} then [done]]

76

Page 56: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

val= <y,y,{}>todo= [apparg 10 in {f=<y,y,{}>} then [done]]

exp= 10env= {f=<y,y,{}>}todo= [app <y,y,{}> then [done]]

77

Page 57: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= 10env= {f=<y,y,{}>}todo= [app <y,y,{}> then [done]]

val= 10todo= [app <y,y,{}> then [done]]

78

Page 58: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

val= 10todo= [app <y,y,{}> then [done]]

exp= yenv= {y=10}todo= [done]

79

Page 59: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Evaluation with To-Do List

let f = proc(y)y in (f 10)

exp= yenv= {y=10}todo= [done]

val= 10todo= [done]

80

Page 60: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

To-Do Lists

• To-do list is called the continuation

• It makes the Scheme context in our interpreter explicit

The interpreter now consists of two main functions:

• eval-expression : expr env cont -> expval

exp= 1env= {}todo= [done]

• apply-cont : value cont -> expval

val= 1todo= [done]

81-82

Page 61: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Continuation Datatype

(define-datatype continuation continuation? (done-cont) (app-arg-cont (rand expression?) (env environment?) (cont continuation?)) (app-cont (rator value?) (cont continuation?)) ...)

83

Page 62: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Continuation Datatype

[done]=

(done-cont)

[addval 1 then [done]]=

(prim-cont (add-prim) 1 (done-cont))

[addexp y in {y=10} then [done]]=

(prim-other-cont (add-prim) (var-exp 'y) (extend-env '(y) '(10) (empty-env)) (done-cont))

84-86

Page 63: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Continuation Datatype

[let f in (f 10) {} then [done]]=

(let-cont 'f (app-exp (var-exp 'f) (list-exp 10)) (empty-env) (done-cont))

87

Page 64: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter

(define eval-program (lambda (pgm) (cases program pgm (a-program (body) (eval-expression body (init-env) (done-cont))))))

88

Page 65: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter

(define (eval-expression exp env cont) (cases expression exp (lit-exp (datum) (apply-cont cont datum)) (var-exp (id) (apply-cont cont (apply-env env id))) (proc-exp (id body-exp) (apply-cont cont (closure id body-exp env))) ...))) (define (apply-cont cont val) (cases continuation cont (done-cont () val) ...))

89

Page 66: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter: Let

... ; in eval-expression:(let-exp (id exp body-exp) (eval-expression exp env (let-cont id body-exp env cont)))...... ; in apply-cont:(let-cont (id body env cont) (eval-expression body (extend-env (list id) (list val) env) cont))...

90

Page 67: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter: Primitives

... ; in eval-expression:(primapp-exp (prim rand1 rand2) (eval-expression rand1 env (prim-other-cont prim rand2 env cont)))...... ; in apply-cont:(prim-other-cont (prim arg2 env cont) (eval-expression arg2 env (prim-cont prim val cont)))(prim-cont (prim arg1-val cont) (apply-cont cont (apply-primitive prim arg1-val val)))... 91

Page 68: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter: Application

... ; in eval-expression:(app-exp (rator rand) (eval-expression rator env (app-arg-cont rand env cont)))...... ; in apply-cont:(app-arg-cont (rand env cont) (eval-expression rand env (app-cont val cont))) (app-cont (f cont) (apply-proc f val cont))...

92

Page 69: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter: If

... ; in eval-expression:(if-exp (test then else) (eval-expression test env (if-cont then else env cont)))...... ; in apply-cont:(if-cont (then else env cont) (eval-expression (if (zero? val) else then) env cont))...

93

Page 70: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Interpreter: Complete Implementation

(in DrScheme, instrumented to show traces)

94

Page 71: Recursion in C - School of Computing · Recursion in the Book Language Why don't we get a stack overflow in the book language? • This is actually a question about Scheme: We implement

Variable and Control Stacks

One more way to look at continuations:

• environment = variable stack

• continuation = control stack

In most imperative languages (e.g., C, Java), the variable stack andcontrol stack are merged

That's why a special form is needed for loops

95