




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of continuations and delayed evaluation in programming. Continuations are functions that represent the rest of the program and are used in various programming paradigms such as lisp and ml compilation, operating systems, and web application development. Delayed evaluation is a technique used to defer a computation until it is actually needed, which can save resources and improve performance. Examples and explanations of how these concepts are used in practice.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





L. Dillon, CSE 452, Fall 2008
1
L. Dillon, CSE 452, Fall 2008
L. Dillon, CSE 452, Fall 2008
3
notQuadratic;
exception
imaginaryRoots;
fun
equal(x:real,
y:real)
x
y
andalso
x
y;
fun
roots(a,
b,
c)
let
val
discBase
(b*b)
(4.0ac)
val
denom
2.0*a
in
if
equal(denom,
then
raise
notQuadratic
else
if
discBase
then
raise
imaginaryRoots
else
let
val
disc
Math.sqrt(discBase)
in
if
disc
then
(~b
disc)/denom,
(~b
disc)/denom]
else
[~b/denom]
end
end;
L. Dillon, CSE 452, Fall 2008
fun
checkQuad
(x,
n_continuation,
e_continuation)
if
equal(x,
then
e_continuation()
else
n_continuation(x);
fun
roots1(a,
b,
c)
let
fun
econt
raise
notQuadratic
fun
ncont
(x)
let
val
discBase
(b*b)
(4.0ac)
in
if
discBase
then
raise
imaginaryRoots
else
let
val
disc
Math.sqrt(discBase)
in
if
disc>0.
then
(~b
disc)/x,
(~b
disc)/x]
else
[~b/x]
end
end
in
checkQuad(2.0*a,
ncont,
econt)
end;
L. Dillon, CSE 452, Fall 2008
5
checkImaginary
(x,
n_continuation,
e_continuation)
if
x
then
e_continuation()
else
n_continuation(Math.sqrt(x));
fun
roots2(a,
b,
c)
let
fun
econt
raise
notQuadratic
fun
ncont
(x)
let
fun
econt
raise
imaginaryRoots
fun
ncont
(disc)
if
disc
then
(~b
disc)/x,
(~b
disc)/x]
else
[~b/x]
in
checkImaginary(b*b
4.0ac,
ncont,
econt)
end
in
checkQuad(2.0*a,
ncont,
econt)
end;
L. Dillon, CSE 452, Fall 2008
checkNumRoots(disc,
continuation1,
continuation2)
if
disc
then
continuation1(disc)
else
continuation2();
fun
roots3(a,
b,
c)
let
fun
econt
raise
notQuadratic
fun
ncont
(x)
let
fun
econt
raise
imaginaryRoots
fun
ncont
(disc)
let
fun
con
(disc)
(~b
disc)/x,
(~b
disc)/x]
fun
con
[~b/x]
in
checkNumRoots(disc,
con1,
con2)
end
in
checkImaginary(b*b
4.0ac,
ncont,
econt)
end
in
checkQuad(2.0*a,
ncont,
econt)
end;
L. Dillon, CSE 452, Fall 2008
7
of fac.)of the tail-recursive version (See Mitchell for derivation
L. Dillon, CSE 452, Fall 2008
cpf
cpf
cpf
1
2
n
1
2
n
cpf
1
2
n
1
2
n
1
2
n
L. Dillon, CSE 452, Fall 2008
13
Recall: The
continuation
of an expression
e
is an
abstraction of what the system will do with the value of
e
SML/NJ
provides a structure (library module) for
“ capturing
” a continuation for later use (application):
To
use it:
L. Dillon, CSE 452, Fall 2008
L. Dillon, CSE 452, Fall 2008
15
Example
Intuition
L. Dillon, CSE 452, Fall 2008
OS kernel schedules multiple threads
Mach−
“continuation” approach
Advantage
/Disadvantage
L. Dillon, CSE 452, Fall 2008
17
Asynchronous XHR similar to continuations:
Usage:
L. Dillon, CSE 452, Fall 2008
L. Dillon, CSE 452, Fall 2008
19
Evaluation of ML expression is
eager
Other evaluation strategies
L. Dillon, CSE 452, Fall 2008
L. Dillon, CSE 452, Fall 2008
25
Suppose the delayed value is needed more than once in
If theevaluating the function body.
argument is pure (no side-effects), would like to
avoid re-evaluating the argument:
datatype
ʻa
delay
of
ʻa
of
unit
ʻa;
Delay(e)
ref(UN(fn()
e)
fun
ev(EV(x))
x
ev(UN(f))
f();
fun
force(d)
let
val
v
ev(!d)
in
(d
EV(v);
v)
end;
L. Dillon, CSE 452, Fall 2008
datatype
ʻa
delay
of
ʻa
of
unit
ʻa;
ev:
ʻa
delay
ʻa
fun
ev(EV(x))
x
ev(UN(f))
f();
force:
ʻa
delay
ref
ʻa
fun
force(d)
let
val
v
ev(!d)
in
(d
EV(v);
v)
end;
lazy_poly:
real
delay
ref
int
real
fun
lazy_poly(x,n)
if
n<
then
else
Math.pow(force(x),real(n))
lazy_poly(x,n-1);
expensive:
int
real
fun
expensive(x)
lazy_poly(ref(UN(fn()
expensive(7))),
assume:
expensive(7)
(lazy_...)
...
n: x:
(force(x))
...
v: d:
(ev(!d))
...
f:
(f())
...
(expen...)
...
x:
7
UN
a :
a
:PC
EP:
L. Dillon, CSE 452, Fall 2008
27
datatype
ʻa
delay
of
ʻa
of
unit
ʻa;
ev:
ʻa
delay
ʻa
fun
ev(EV(x))
x
ev(UN(f))
f();
force:
ʻa
delay
ref
ʻa
fun
force(d)
let
val
v
ev(!d)
in
(d
EV(v);
v)
end;
lazy_poly:
real
delay
ref
int
real
fun
lazy_poly(x,n)
if
n<
then
else
Math.pow(force(x),real(n))
lazy_poly(x,n-1);
expensive:
int
real
fun
expensive(x)
lazy_poly(ref(UN(fn()
expensive(7))),
assume:
expensive(7)
(lazy_...)
...
n: x:
(force(x))
...
v: d:
(ev(!d))
...
f:
(f())
...
(expen...)
...
x:
7
UN
a :
a
EV
:PC
EP:
L. Dillon, CSE 452, Fall 2008
datatype
ʻa
delay
of
ʻa
of
unit
ʻa;
ev:
ʻa
delay
ʻa
fun
ev(EV(x))
x
ev(UN(f))
f();
force:
ʻa
delay
ref
ʻa
fun
force(d)
let
val
v
ev(!d)
in
(d
EV(v);
v)
end;
lazy_poly:
real
delay
ref
int
real
fun
lazy_poly(x,n)
if
n<
then
else
Math.pow(force(x),real(n))
lazy_poly(x,n-1);
expensive:
int
real
fun
expensive(x)
lazy_poly(ref(UN(fn()
expensive(7))),
assume:
expensive(7)
(lazy_...)
...
n: x:
(force(x))
...
v: d:
(ev(!d))
...
f:
(f())
...
(expen...)
...
x:
7
UN
a :
a
EV
(lazy_...)
...
n: x:
(force(x))
...
v: d:
(ev(!d))
...
x:
EP:
:PC
L. Dillon, CSE 452, Fall 2008
Structured Programming
Exceptions−
Continuations−
Delay and force