<s>
In	O
functional	B-Language
programming	I-Language
,	O
continuation-passing	B-Application
style	I-Application
(	O
CPS	O
)	O
is	O
a	O
style	O
of	O
programming	O
in	O
which	O
control	O
is	O
passed	O
explicitly	O
in	O
the	O
form	O
of	O
a	O
continuation	O
.	O
</s>
<s>
This	O
is	O
contrasted	O
with	O
direct	B-Application
style	I-Application
,	O
which	O
is	O
the	O
usual	O
style	O
of	O
programming	O
.	O
</s>
<s>
Gerald	O
Jay	O
Sussman	O
and	O
Guy	O
L	O
.	O
Steele	O
,	O
Jr.	O
coined	O
the	O
phrase	O
in	O
AI	O
Memo	O
349	O
(	O
1975	O
)	O
,	O
which	O
sets	O
out	O
the	O
first	O
version	O
of	O
the	O
Scheme	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
John	O
C	B-Language
.	O
Reynolds	O
gives	O
a	O
detailed	O
account	O
of	O
the	O
numerous	O
discoveries	O
of	O
continuations	O
.	O
</s>
<s>
A	O
function	O
written	O
in	O
continuation-passing	B-Application
style	I-Application
takes	O
an	O
extra	O
argument	O
:	O
an	O
explicit	O
"	O
continuation	O
"	O
;	O
i.e.	O
,	O
a	O
function	O
of	O
one	O
argument	O
.	O
</s>
<s>
Expressing	O
code	O
in	O
this	O
form	O
makes	O
a	O
number	O
of	O
things	O
explicit	O
which	O
are	O
implicit	O
in	O
direct	B-Application
style	I-Application
.	O
</s>
<s>
These	O
include	O
:	O
procedure	O
returns	O
,	O
which	O
become	O
apparent	O
as	O
calls	O
to	O
a	O
continuation	O
;	O
intermediate	O
values	O
,	O
which	O
are	O
all	O
given	O
names	O
;	O
order	O
of	O
argument	O
evaluation	O
,	O
which	O
is	O
made	O
explicit	O
;	O
and	O
tail	B-Language
calls	I-Language
,	O
which	O
simply	O
call	O
a	O
procedure	O
with	O
the	O
same	O
continuation	O
,	O
unmodified	O
,	O
that	O
was	O
passed	O
to	O
the	O
caller	O
.	O
</s>
<s>
Programs	O
can	O
be	O
automatically	O
transformed	O
from	O
direct	B-Application
style	I-Application
to	O
CPS	O
.	O
</s>
<s>
Functional	O
and	O
logic	B-Language
compilers	B-Language
often	O
use	O
CPS	O
as	O
an	O
intermediate	B-Application
representation	I-Application
where	O
a	O
compiler	B-Language
for	O
an	O
imperative	B-Application
or	O
procedural	B-Application
programming	I-Application
language	I-Application
would	O
use	O
static	O
single	O
assignment	O
form	O
(	O
SSA	O
)	O
.	O
</s>
<s>
SSA	O
is	O
formally	O
equivalent	O
to	O
a	O
subset	O
of	O
CPS	O
(	O
excluding	O
non-local	O
control	O
flow	O
,	O
which	O
does	O
not	O
occur	O
when	O
CPS	O
is	O
used	O
as	O
intermediate	B-Application
representation	I-Application
)	O
.	O
</s>
<s>
Functional	O
compilers	B-Language
can	O
also	O
use	O
A-normal	B-Application
form	I-Application
(	O
ANF	O
)	O
(	O
but	O
only	O
for	O
languages	O
requiring	O
eager	O
evaluation	O
)	O
,	O
rather	O
than	O
with	O
'	O
thunks	B-Application
 '	O
(	O
described	O
in	O
the	O
examples	O
below	O
)	O
in	O
CPS	O
.	O
</s>
<s>
CPS	O
is	O
used	O
more	O
frequently	O
by	O
compilers	B-Language
than	O
by	O
programmers	O
as	O
a	O
local	O
or	O
global	O
style	O
.	O
</s>
<s>
This	O
,	O
along	O
with	O
a	O
restrictive	O
style	O
prohibiting	O
a	O
variety	O
of	O
constructs	O
usually	O
available	O
,	O
is	O
used	O
to	O
expose	O
the	O
semantics	B-Application
of	O
programs	O
,	O
making	O
them	O
easier	O
to	O
analyze	O
.	O
</s>
<s>
The	O
key	O
to	O
CPS	O
is	O
to	O
remember	O
that	O
(	O
a	O
)	O
every	O
function	O
takes	O
an	O
extra	O
argument	O
known	O
as	O
its	O
continuation	O
,	O
and	O
(	O
b	O
)	O
every	O
argument	O
in	O
a	O
function	O
call	O
must	O
be	O
either	O
a	O
variable	O
or	O
a	O
lambda	B-General_Concept
expression	I-General_Concept
(	O
not	O
a	O
more	O
complex	O
expression	O
)	O
.	O
</s>
<s>
Some	O
examples	O
of	O
code	O
in	O
direct	B-Application
style	I-Application
and	O
the	O
corresponding	O
CPS	O
appear	O
below	O
.	O
</s>
<s>
These	O
examples	O
are	O
written	O
in	O
the	O
Scheme	B-Language
programming	I-Language
language	I-Language
;	O
by	O
convention	O
the	O
continuation	O
function	O
is	O
represented	O
as	O
a	O
parameter	O
named	O
"	O
k	O
"	O
:	O
</s>
<s>
Note	O
that	O
in	O
the	O
CPS	O
versions	O
,	O
the	O
primitives	O
used	O
,	O
like	O
+&	O
and	O
*&	O
are	O
themselves	O
CPS	O
,	O
not	O
direct	B-Application
style	I-Application
,	O
so	O
to	O
make	O
the	O
above	O
examples	O
work	O
in	O
a	O
Scheme	B-Language
system	O
we	O
would	O
need	O
to	O
write	O
these	O
CPS	O
versions	O
of	O
primitives	O
,	O
with	O
for	O
instance	O
*&	O
defined	O
by	O
:	O
</s>
<s>
To	O
do	O
this	O
in	O
general	O
,	O
we	O
might	O
write	O
a	B-Language
conversion	I-Language
routine	O
:	O
</s>
<s>
In	O
order	O
to	O
call	O
a	O
procedure	O
written	O
in	O
CPS	O
from	O
a	O
procedure	O
written	O
in	O
direct	B-Application
style	I-Application
,	O
it	O
is	O
necessary	O
to	O
provide	O
a	O
continuation	O
that	O
will	O
receive	O
the	O
result	O
computed	O
by	O
the	O
CPS	O
procedure	O
.	O
</s>
<s>
In	O
the	O
example	O
above	O
(	O
assuming	O
that	O
CPS	O
primitives	O
have	O
been	O
provided	O
)	O
,	O
we	O
might	O
call	O
(	O
factorial&	O
10	O
(	O
lambda	B-General_Concept
(	O
x	O
)	O
(	O
display	O
x	O
)	O
(	O
newline	O
)	O
)	O
)	O
.	O
</s>
<s>
There	O
is	O
some	O
variety	O
between	O
compilers	B-Language
in	O
the	O
way	O
primitive	O
functions	O
are	O
provided	O
in	O
CPS	O
.	O
</s>
<s>
Above	O
we	O
have	O
used	O
the	O
simplest	O
convention	O
,	O
however	O
sometimes	O
boolean	O
primitives	O
are	O
provided	O
that	O
take	O
two	O
thunks	B-Application
to	O
be	O
called	O
in	O
the	O
two	O
possible	O
cases	O
,	O
so	O
the	O
(=	O
&	O
n	O
0	O
(	O
lambda	B-General_Concept
(	O
b	O
)	O
(	O
if	O
b	O
...	O
)	O
)	O
)	O
call	O
inside	O
f-aux	O
&	O
definition	O
above	O
would	O
be	O
written	O
instead	O
as	O
(=	O
&	O
n	O
0	O
(	O
lambda	B-General_Concept
(	O
)	O
(	O
k	O
a	O
)	O
)	O
(	O
lambda	B-General_Concept
(	O
)	O
(	O
-	O
&	O
n	O
1	O
...	O
)	O
)	O
)	O
.	O
</s>
<s>
Similarly	O
,	O
sometimes	O
the	O
if	O
primitive	O
itself	O
is	O
not	O
included	O
in	O
CPS	O
,	O
and	O
instead	O
a	O
function	O
if&	O
is	O
provided	O
which	O
takes	O
three	O
arguments	O
:	O
a	O
boolean	O
condition	O
and	O
the	O
two	O
thunks	B-Application
corresponding	O
to	O
the	O
two	O
arms	O
of	O
the	O
conditional	O
.	O
</s>
<s>
Thus	O
,	O
to	O
ensure	O
the	O
total	O
absence	O
of	O
a	O
function	B-General_Concept
stack	I-General_Concept
,	O
the	O
entire	O
program	O
must	O
be	O
in	O
CPS	O
.	O
</s>
<s>
First	O
we	O
calculate	O
the	O
square	O
of	O
a	O
in	O
pyth	O
 '	O
function	O
and	O
pass	O
a	O
lambda	B-General_Concept
function	I-General_Concept
as	O
a	O
continuation	O
which	O
will	O
accept	O
a	O
square	O
of	O
a	O
as	O
a	O
first	O
argument	O
.	O
</s>
<s>
The	O
mtl	O
library	O
,	O
which	O
is	O
shipped	O
with	O
GHC	B-Application
,	O
has	O
the	O
module	O
Control.Monad.Cont	O
.	O
</s>
<s>
This	O
is	O
in	O
Java	B-Language
8	O
using	O
the	O
Swing	B-Language
UI	O
framework	O
:	O
</s>
<s>
Every	O
call	O
in	O
CPS	O
is	O
a	O
tail	B-Language
call	I-Language
,	O
and	O
the	O
continuation	O
is	O
explicitly	O
passed	O
.	O
</s>
<s>
Using	O
CPS	O
without	O
tail	B-Language
call	I-Language
optimization	I-Language
(	O
TCO	O
)	O
will	O
cause	O
not	O
only	O
the	O
constructed	O
continuation	O
to	O
potentially	O
grow	O
during	O
recursion	O
,	O
but	O
also	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
This	O
is	O
usually	O
undesirable	O
,	O
but	O
has	O
been	O
used	O
in	O
interesting	O
ways	O
—	O
see	O
the	O
Chicken	B-Language
Scheme	I-Language
compiler	I-Language
.	O
</s>
<s>
As	O
CPS	O
and	O
TCO	O
eliminate	O
the	O
concept	O
of	O
an	O
implicit	O
function	O
return	O
,	O
their	O
combined	O
use	O
can	O
eliminate	O
the	O
need	O
for	O
a	O
run-time	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Several	O
compilers	B-Language
and	O
interpreters	O
for	O
functional	B-Language
programming	I-Language
languages	I-Language
use	O
this	O
ability	O
in	O
novel	O
ways	O
.	O
</s>
<s>
Continuation	B-Application
passing	I-Application
style	I-Application
can	O
be	O
used	O
to	O
implement	O
continuations	O
and	O
control	O
flow	O
operators	O
in	O
a	O
functional	B-Language
language	I-Language
that	O
does	O
not	O
feature	O
first-class	O
continuations	O
but	O
does	O
have	O
first-class	B-Application
functions	I-Application
and	O
tail-call	B-Language
optimization	I-Language
.	O
</s>
<s>
Without	O
tail-call	B-Language
optimization	I-Language
,	O
techniques	O
such	O
as	O
trampolining	B-General_Concept
,	O
i.e.	O
</s>
<s>
using	O
a	O
loop	O
that	O
iteratively	O
invokes	O
thunk-returning	O
functions	O
,	O
can	O
be	O
used	O
;	O
without	O
first-class	B-Application
functions	I-Application
,	O
it	O
is	O
even	O
possible	O
to	O
convert	O
tail	B-Language
calls	I-Language
into	O
just	O
gotos	O
in	O
such	O
a	O
loop	O
.	O
</s>
<s>
There	O
are	O
various	O
translations	O
,	O
usually	O
defined	O
as	O
one	O
-	O
or	O
two-pass	O
conversions	O
of	O
pure	O
lambda	B-Language
calculus	I-Language
,	O
which	O
convert	O
direct	B-Application
style	I-Application
expressions	O
into	O
CPS	O
expressions	O
.	O
</s>
<s>
Writing	O
in	O
trampolined	O
style	O
,	O
however	O
,	O
is	O
extremely	O
difficult	O
;	O
when	O
used	O
,	O
it	O
is	O
usually	O
the	O
target	O
of	O
some	O
sort	O
of	O
transformation	O
,	O
such	O
as	O
compilation	B-Language
.	O
</s>
<s>
Functions	O
using	O
more	O
than	O
one	O
continuation	O
can	O
be	O
defined	O
to	O
capture	O
various	O
control	O
flow	O
paradigms	O
,	O
for	O
example	O
(	O
in	O
Scheme	B-Language
)	O
:	O
</s>
<s>
It	O
is	O
also	O
similar	O
to	O
the	O
embedding	O
of	O
lambda	B-Language
calculus	I-Language
in	O
π-calculus	O
.	O
</s>
<s>
Outside	O
of	O
computer	B-General_Concept
science	I-General_Concept
,	O
CPS	O
is	O
of	O
more	O
general	O
interest	O
as	O
an	O
alternative	O
to	O
the	O
conventional	O
method	O
of	O
composing	O
simple	O
expressions	O
into	O
complex	O
expressions	O
.	O
</s>
<s>
For	O
example	O
,	O
within	O
linguistic	O
semantics	B-Application
,	O
Chris	O
Barker	O
and	O
his	O
collaborators	O
have	O
suggested	O
that	O
specifying	O
the	O
denotations	O
of	O
sentences	O
using	O
CPS	O
might	O
explain	O
certain	O
phenomena	O
in	O
natural	O
language	O
.	O
</s>
<s>
In	O
mathematics	O
,	O
the	O
Curry	O
–	O
Howard	O
isomorphism	O
between	O
computer	O
programs	O
and	O
mathematical	O
proofs	O
relates	O
continuation-passing	B-Application
style	I-Application
translation	O
to	O
a	O
variation	O
of	O
double-negation	O
embeddings	O
of	O
classical	O
logic	B-Language
into	O
intuitionistic	O
(	O
constructive	O
)	O
logic	B-Language
.	O
</s>
<s>
Unlike	O
the	O
regular	O
double-negation	O
translation	O
,	O
which	O
maps	O
atomic	O
propositions	O
p	O
to	O
( ( 	O
p	O
→	O
⊥	O
)	O
→	O
⊥	O
)	O
,	O
the	O
continuation	B-Application
passing	I-Application
style	I-Application
replaces	O
⊥	O
by	O
the	O
type	O
of	O
the	O
final	O
expression	O
.	O
</s>
<s>
Classical	O
logic	B-Language
itself	O
relates	O
to	O
manipulating	O
the	O
continuation	O
of	O
programs	O
directly	O
,	O
as	O
in	O
Scheme	B-Language
's	O
call-with-current-continuation	O
control	O
operator	O
,	O
an	O
observation	O
due	O
to	O
Tim	O
Griffin	O
(	O
using	O
the	O
closely	O
related	O
C	B-Language
control	O
operator	O
)	O
.	O
</s>
