principles of programming languagesproglang.informatik.uni-freiburg.de/teaching/... · principles...
TRANSCRIPT
![Page 1: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/1.jpg)
Principles of Programming LanguagesLecture 10 Continuations
Albert-Ludwigs-Universität Freiburg
Peter ThiemannUniversity of Freiburg, Germany
09 July 2018
![Page 2: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/2.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 2 / 36
![Page 3: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/3.jpg)
Continuations
concept for expressing exceptions, backtracking, coroutines, multi-threading, . . .became popular with server-side web-programming“enforced” by reactive programming
Thiemann POPL 2018-07-09 3 / 36
![Page 4: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/4.jpg)
Continuation-Passing Style (CPS)
Places syntactic restrictions on programsall functions are tail-recursivefunctions never returneach function has one or more continuation parameters, each of which is afunctionto return a value, a function invokes a continuation and passes the return value(s)as a parameter.
Thiemann POPL 2018-07-09 4 / 36
![Page 5: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/5.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 5 / 36
![Page 6: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/6.jpg)
Motivation: Exceptions
Multiply a tree of numbers
product xt =if null xtthen 1else product (left xt) * value xt * product (right xt)
inefficient if xt contains zero
Thiemann POPL 2018-07-09 6 / 36
![Page 7: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/7.jpg)
Trying to improve . . .
product1 xt =if null xtthen 1else if value xt == 0then 0else product (left xt) * value xt * product (right xt)
better, but still many useless multiplications
Thiemann POPL 2018-07-09 7 / 36
![Page 8: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/8.jpg)
Rewrite in CPS
product2 xt =prod xt (\x -> x)whereprod xt c =
if null xtthen c 1else if value xt == 0then c 0else prod (left xt) (\l -> prod (right xt) (\r -> c (l * r * value xt)))
same as product1, but in CPSc argument is continuationnow we can exploit the presence of continuations
Thiemann POPL 2018-07-09 8 / 36
![Page 9: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/9.jpg)
Changing the flow of control in CPS
product3 xt =prod xt (\x -> x)whereprod xt c =
if null xtthen c 1else if value xt == 0then 0 -- do NOT invoke the continuation celse prod (left xt) (\l -> prod (right xt) (\r -> c (l * r * value xt)))
deliberate violation of CPSachieves the desired effectuseful, but clumsy
Thiemann POPL 2018-07-09 9 / 36
![Page 10: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/10.jpg)
Call/cc — a Control Operator
product3 xt =call/cc (\ abort ->
let prod xt =if null xtthen 1else if value xt == 0then abort 0else prod (left xt) * value xt * prod (right xt)
in prod xt
Call/cc = call with current continuationApplies its argument to the current continuationAdvantage: no need to write programs in CPSDisadvantage: some experience/insight needed
Thiemann POPL 2018-07-09 10 / 36
![Page 11: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/11.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 11 / 36
![Page 12: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/12.jpg)
A Problem
Consider the subset sum problem which is a specialized version of the knapsackproblem:You are given
a positive integer C (the weight that you can carry) anda list of positive integers (the weights of items you want to carry).
Is there a subset of the items the weights of which add up to C? (This problem isknown to be NP-complete.)
Thiemann POPL 2018-07-09 12 / 36
![Page 13: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/13.jpg)
An Implementation (pseudo code)
subsetsum target items =let work path target items =
if target == 0then RESULT pathelse if null itemsthen FAILelse if (head items) <= targetthen TRY (work (head items : path) (target - head items) (tail items))
ANDTHEN work path target (tail items)else work path target (tail items)
inwork [] target items
Thiemann POPL 2018-07-09 13 / 36
![Page 14: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/14.jpg)
Observations
Primitives in pseudo code
RESULT announces a result.FAIL declares the current invokation to fail.TRY ...ANDTHEN ... searches for a result in the first argument and then in thesecond.
Implementation with continuations
each function has two continuations succ and failinvoke succ to indicate success and return a resultinvoke fail to indicate failure
implement TRY ...ANDTHEN ... by nesting continuations
Thiemann POPL 2018-07-09 14 / 36
![Page 15: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/15.jpg)
Observations
Primitives in pseudo code
RESULT announces a result.FAIL declares the current invokation to fail.TRY ...ANDTHEN ... searches for a result in the first argument and then in thesecond.
Implementation with continuations
each function has two continuations succ and failinvoke succ to indicate success and return a resultinvoke fail to indicate failure
implement TRY ...ANDTHEN ... by nesting continuations
Thiemann POPL 2018-07-09 14 / 36
![Page 16: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/16.jpg)
Double-barreled CPS
subsetsum1 target items =let work path target items succ fail =
if target == 0then succ pathelse if null itemsthen fail ()else let hi = head items inif hi <= targetthen work (hi : path) (target - hi) (tail items) succ (\ () ->
work path target (tail items) succ fail)else work path target (tail items) succ fail
inwork [] target items (\ x -> True) (\ () -> False)
Thiemann POPL 2018-07-09 15 / 36
![Page 17: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/17.jpg)
Comments
Using the success continuation in this example is overblown.Replacing succ path with True yields the same behavior.However, the success continuation may be used to compose a list of all results orto compute a best approximation:
Thiemann POPL 2018-07-09 16 / 36
![Page 18: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/18.jpg)
List of all results
subsetsum1 target items =let work path target items succ fail =
if target == 0then succ path failelse if null itemsthen fail ()else let hi = head items inif hi <= targetthen work (hi : path) (target - hi) (tail items) succ (\ () ->
work path target (tail items) succ fail)else work path target (tail items) succ fail
let results = ref []in work [] target items (\ path fail -> results := path : !results;
fail ())(\ () -> return !results)
Thiemann POPL 2018-07-09 17 / 36
![Page 19: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/19.jpg)
Best approximation
subsetsum2 target items =let work path target items succ fail =
if target == 0 || null itemsthen succ target path failelse let hi = head items inif hi <= targetthen work (hi : path) (target - hi) (tail items) succ (\ () ->
work path target (tail items) succ fail)else work path target (tail items) succ fail;
let best = ref target;let result = ref [];in work [] target items (\ rest path fail ->
if rest < !best then (best := rest; result := path);fail ())
(\ () -> return !result)
Thiemann POPL 2018-07-09 18 / 36
![Page 20: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/20.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 19 / 36
![Page 21: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/21.jpg)
Motivation: Coroutines
Coroutines
program components like subroutineson equal footing, without caller-callee hierarchyexactly one coroutine is active at any instanceactive coroutine can yield control (with parameters) to another, which resumesfrom where it yielded previously
What are they good for
Coroutines are well suited for implementing programming patterns such ascooperative tasks, iterators, infinite lists, and pipes.Available in Python, Lua, C#, etc
Thiemann POPL 2018-07-09 20 / 36
![Page 22: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/22.jpg)
Motivation: Coroutines
Coroutines
program components like subroutineson equal footing, without caller-callee hierarchyexactly one coroutine is active at any instanceactive coroutine can yield control (with parameters) to another, which resumesfrom where it yielded previously
What are they good for
Coroutines are well suited for implementing programming patterns such ascooperative tasks, iterators, infinite lists, and pipes.Available in Python, Lua, C#, etc
Thiemann POPL 2018-07-09 20 / 36
![Page 23: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/23.jpg)
Coroutines, example use
/** run-length decompression */void decompress () {
while ((c = getchar()) != EOF) {if (c == 0xFF) {
len = getchar();c = getchar();while (len--)
emit(c);} else
emit(c);}emit(EOF);
}
Thiemann POPL 2018-07-09 21 / 36
![Page 24: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/24.jpg)
Scanner
void scanner () {while ((c = getchar()) != EOF) {
if (isalpha(c)) {do {
add_to_token(c);c = getchar();
} while (isalpha(c));got_token(WORD);
}add_to_token(c);got_token(PUNCT);
}}
Thiemann POPL 2018-07-09 22 / 36
![Page 25: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/25.jpg)
Problem: combine decompress and scanner
simple code in separationtask: scanner for compressed documentsstandard approach: rewrite one of the functionsnot required with coroutines(here: symmetric coroutines; simpler with asymmetric coroutines as in Python)
Thiemann POPL 2018-07-09 23 / 36
![Page 26: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/26.jpg)
Combining with coroutines
void scanner (COROUTINE producer) {while ((c = yield(producer)) != EOF) {
if (isalpha(c)) {do {
add_to_token(c);c = yield(producer);
} while (isalpha(c));got_token(WORD);
}add_to_token(c);got_token(PUNCT);
}}
Thiemann POPL 2018-07-09 24 / 36
![Page 27: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/27.jpg)
Combining with coroutines, part 2
void decompress (COROUTINE consumer) {while ((c = getchar ()) != EOF) {
if (c == 0xFF) {len = getchar();c = getchar();while (len--)
yield(consumer, c);} else
yield(consumer, c);}yield(consumer, EOF);
}
Thiemann POPL 2018-07-09 25 / 36
![Page 28: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/28.jpg)
Driver
run (COROUTINE producer, COROUTINE consumer) {do {
c = yield (producer);yield (consumer, c);
} while (c != EOF);}
...COROUTINE producer = make_coroutine (decompress);COROUTINE consumer = make_coroutine (scanner);COROUTINE driver = make_coroutine (run);
driver (producer, consumer);...
Thiemann POPL 2018-07-09 26 / 36
![Page 29: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/29.jpg)
Implementing Coroutines with Call/cc
running = ref (ref Nothing)make_coroutine f =
let mycont = ref Nothingin \ x ->
running := Just mycontcase cont of
Nothing -> f xJust ff -> ff x
yield g y =call/cc (\ resume ->
!running := Just resume;g y)
Idea: represent each coroutine state by the coroutine’s current continuation.Thiemann POPL 2018-07-09 27 / 36
![Page 30: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/30.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 28 / 36
![Page 31: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/31.jpg)
Motivation: Threads
native threads vs. simulated threads. The former rely on the operating systemand may be executed on different processors. The latter simulate concurrencyinside of a sequential process.preemptive vs. cooperative. In each thread implementation, a schedulerdetermines which thread becomes active next. With preemption, the schedulerruns at regular time intervals. It suspends the currently active thread and selectsanother thread from a pool of suspended threads to run in the next time slice.With cooperative threading, a thread remains active until it explicitly relinquishescontrol or until it gets blocked due to an I/O operation.
Thiemann POPL 2018-07-09 29 / 36
![Page 32: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/32.jpg)
Implementing cooperative threads with call/cc
Simple user-level implementation of simulated, cooperative threads with call/cc.A thread yields to the scheduler.Threads communicate exclusively via shared state. They cannot receiveparameters or return values while they are running.
Thiemann POPL 2018-07-09 30 / 36
![Page 33: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/33.jpg)
A typical thread interface
spawn :: (Unit -> Unit) -> Threadyield :: Unit -> Unitterminate :: Unit -> Unit
Thiemann POPL 2018-07-09 31 / 36
![Page 34: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/34.jpg)
A simple thread implementation
currentThread = NULLrunQueue = emptyQueue
spawn f =enqueue (runQueue, makeThread f)
makeThread f ={ cont =
\ () ->f (); terminate ()
...}
Thiemann POPL 2018-07-09 32 / 36
![Page 35: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/35.jpg)
A simple thread implementation (cont)
terminate () =scheduleThread (dequeue (runQueue))
scheduleThread (thread) =currentThread = thread;currentThread.cont ()
yield () =call/cc (\ mycont ->
currentThread.cont = mycont;enqueue (runQueue, currentThread);scheduleThread (dequeue (runQueue));)
Thiemann POPL 2018-07-09 33 / 36
![Page 36: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/36.jpg)
Plan
1 ContinuationsMotivation: ExceptionsMotivation: BacktrackingMotivation: CoroutinesMotivation: ThreadsImplementing first-class continuations
Thiemann POPL 2018-07-09 34 / 36
![Page 37: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/37.jpg)
Implementing first-class continuationsvia interpretation
Syntax
e ::= x | λx .e | e e | 0 | e+e | if e e e | call/cc
Semantic domains
y ∈ Val = Z + (Val → Comp)κ ∈ Cont = Val → Answer
Comp = Cont → Answerρ ∈ Env = Var → Val
E : Exp → Env → Comp
Thiemann POPL 2018-07-09 35 / 36
![Page 38: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/38.jpg)
Implementing first-class continuationsvia interpretation
Syntax
e ::= x | λx .e | e e | 0 | e+e | if e e e | call/cc
Semantic domains
y ∈ Val = Z + (Val → Comp)κ ∈ Cont = Val → Answer
Comp = Cont → Answerρ ∈ Env = Var → Val
E : Exp → Env → Comp
Thiemann POPL 2018-07-09 35 / 36
![Page 39: Principles of Programming Languagesproglang.informatik.uni-freiburg.de/teaching/... · Principles of Programming Languages Lecture10Continuations Albert-Ludwigs-Universität Freiburg](https://reader034.vdocument.in/reader034/viewer/2022042310/5ed8a4d66714ca7f47684e6b/html5/thumbnails/39.jpg)
Definition of the interpreter
EJxKρκ = κ(ρ(x))EJλx .eKρκ = κ(λy .λκ.EJeKρ[x 7→ y ]κ)EJe1 e2Kρκ = EJe1Kρ(λy1.EJe2Kρ(λy2.y1 y2 κ))EJ0Kρκ = κ(0)EJe1+e2Kρκ = EJe1Kρ(λy1.EJe2Kρ(λy2.κ(y1+y2)))EJif e1 e2 e2Kρκ = EJe1Kρ(λy .if y (EJe2Kρκ) (EJe3Kρκ))EJcall/ccKρκ = κ(λf .λκ.f (λy .λκ′.κy)κ)EJcall/cc eKρκ = EJeKρ(λf .f (λy .λκ′.κy)κ)
Interpreter E is written in CPSBTW, internalizes call-by-valueAlternatives
transform the program to CPS and run it directlyimplement call/cc natively
Thiemann POPL 2018-07-09 36 / 36