<s>
In	O
mathematics	O
,	O
Church	B-Application
encoding	I-Application
is	O
a	O
means	O
of	O
representing	O
data	O
and	O
operators	O
in	O
the	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
The	O
Church	B-Application
numerals	I-Application
are	O
a	O
representation	O
of	O
the	O
natural	O
numbers	O
using	O
lambda	O
notation	O
.	O
</s>
<s>
The	O
method	O
is	O
named	O
for	O
Alonzo	O
Church	O
,	O
who	O
first	O
encoded	O
data	O
in	O
the	O
lambda	B-Language
calculus	I-Language
this	O
way	O
.	O
</s>
<s>
Terms	O
that	O
are	O
usually	O
considered	O
primitive	O
in	O
other	O
notations	O
(	O
such	O
as	O
integers	O
,	O
booleans	O
,	O
pairs	O
,	O
lists	O
,	O
and	O
tagged	O
unions	O
)	O
are	O
mapped	O
to	O
higher-order	B-Language
functions	I-Language
under	O
Church	B-Application
encoding	I-Application
.	O
</s>
<s>
The	O
Church-Turing	O
thesis	O
asserts	O
that	O
any	O
computable	O
operator	O
(	O
and	O
its	O
operands	O
)	O
can	O
be	O
represented	O
under	O
Church	B-Application
encoding	I-Application
.	O
</s>
<s>
In	O
the	O
untyped	B-Language
lambda	I-Language
calculus	I-Language
the	O
only	O
primitive	O
data	O
type	O
is	O
the	O
function	O
.	O
</s>
<s>
A	O
straightforward	O
implementation	O
of	O
Church	B-Application
encoding	I-Application
slows	O
some	O
access	O
operations	O
from	O
to	O
,	O
where	O
is	O
the	O
size	O
of	O
the	O
data	O
structure	O
,	O
making	O
Church	B-Application
encoding	I-Application
impractical	O
.	O
</s>
<s>
Research	O
has	O
shown	O
that	O
this	O
can	O
be	O
addressed	O
by	O
targeted	O
optimizations	O
,	O
but	O
most	O
functional	B-Language
programming	I-Language
languages	I-Language
instead	O
expand	O
their	O
intermediate	O
representations	O
to	O
contain	O
algebraic	O
data	O
types	O
.	O
</s>
<s>
Nonetheless	O
Church	B-Application
encoding	I-Application
is	O
often	O
used	O
in	O
theoretical	O
arguments	O
,	O
as	O
it	O
is	O
a	O
natural	O
representation	O
for	O
partial	O
evaluation	O
and	O
theorem	O
proving	O
.	O
</s>
<s>
Church	B-Application
encoding	I-Application
is	O
complete	O
but	O
only	O
representationally	O
.	O
</s>
<s>
Lambda	B-Language
calculus	I-Language
is	O
usually	O
interpreted	O
as	O
using	O
intensional	O
equality	O
.	O
</s>
<s>
Church	B-Application
numerals	I-Application
are	O
the	O
representations	O
of	O
natural	O
numbers	O
under	O
Church	B-Application
encoding	I-Application
.	O
</s>
<s>
The	O
higher-order	B-Language
function	I-Language
that	O
represents	O
natural	O
number	O
n	O
is	O
a	O
function	O
that	O
maps	O
any	O
function	O
to	O
its	O
n-fold	O
composition	B-Application
.	O
</s>
<s>
All	O
Church	B-Application
numerals	I-Application
are	O
functions	O
that	O
take	O
two	O
parameters	O
.	O
</s>
<s>
Church	B-Application
numerals	I-Application
0	O
,	O
1	O
,	O
2	O
,	O
...	O
,	O
are	O
defined	O
as	O
follows	O
in	O
the	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
The	O
Church	B-Application
numeral	I-Application
3	O
represents	O
the	O
action	O
of	O
applying	O
any	O
given	O
function	O
three	O
times	O
to	O
a	O
value	O
.	O
</s>
<s>
The	O
function	O
itself	O
,	O
and	O
not	O
its	O
end	O
result	O
,	O
is	O
the	O
Church	B-Application
numeral	I-Application
3	O
.	O
</s>
<s>
The	O
Church	B-Application
numeral	I-Application
3	O
means	O
simply	O
to	O
do	O
anything	O
three	O
times	O
.	O
</s>
<s>
Arithmetic	O
operations	O
on	O
numbers	O
may	O
be	O
represented	O
by	O
functions	O
on	O
Church	B-Application
numerals	I-Application
.	O
</s>
<s>
These	O
functions	O
may	O
be	O
defined	O
in	O
lambda	B-Language
calculus	I-Language
,	O
or	O
implemented	O
in	O
most	O
functional	B-Language
programming	I-Language
languages	I-Language
(	O
see	O
converting	O
lambda	O
expressions	O
to	O
functions	O
)	O
.	O
</s>
<s>
The	O
exponentiation	O
function	O
is	O
given	O
by	O
the	O
definition	O
of	O
Church	B-Application
numerals	I-Application
,	O
.	O
</s>
<s>
A	O
Church	B-Application
numeral	I-Application
applies	O
a	O
function	O
n	O
times	O
.	O
</s>
<s>
The	O
predecessor	O
function	O
used	O
in	O
the	O
Church	B-Application
encoding	I-Application
is	O
,	O
</s>
<s>
If	O
this	O
expression	O
is	O
used	O
then	O
the	O
mathematical	O
definition	O
of	O
division	O
given	O
above	O
is	O
translated	O
into	O
function	O
on	O
Church	B-Application
numerals	I-Application
as	O
,	O
</s>
<s>
The	O
Y	B-Application
combinator	I-Application
may	O
be	O
used	O
to	O
implement	O
the	O
recursion	O
.	O
</s>
<s>
Using	O
a	O
lambda	B-Language
calculus	I-Language
calculator	O
,	O
the	O
above	O
expression	O
reduces	O
to	O
3	O
,	O
using	O
normal	O
order	O
.	O
</s>
<s>
One	O
simple	O
approach	O
for	O
extending	O
Church	B-Application
Numerals	I-Application
to	O
signed	O
numbers	O
is	O
to	O
use	O
a	O
Church	O
pair	B-Protocol
,	O
containing	O
Church	B-Application
numerals	I-Application
representing	O
a	O
positive	O
and	O
a	O
negative	O
value	O
.	O
</s>
<s>
The	O
integer	O
value	O
is	O
the	O
difference	O
between	O
the	O
two	O
Church	B-Application
numerals	I-Application
.	O
</s>
<s>
The	O
integer	O
value	O
is	O
more	O
naturally	O
represented	O
if	O
one	O
of	O
the	O
pair	B-Protocol
is	O
zero	O
.	O
</s>
<s>
The	O
recursion	O
may	O
be	O
implemented	O
using	O
the	O
Y	B-Application
combinator	I-Application
,	O
</s>
<s>
Addition	O
is	O
defined	O
mathematically	O
on	O
the	O
pair	B-Protocol
by	O
,	O
</s>
<s>
The	O
last	O
expression	O
is	O
translated	O
into	O
lambda	B-Language
calculus	I-Language
as	O
,	O
</s>
<s>
The	O
last	O
expression	O
is	O
translated	O
into	O
lambda	B-Language
calculus	I-Language
as	O
,	O
</s>
<s>
A	O
similar	O
definition	O
is	O
given	O
here	O
for	O
division	O
,	O
except	O
in	O
this	O
definition	O
,	O
one	O
value	O
in	O
each	O
pair	B-Protocol
must	O
be	O
zero	O
(	O
see	O
OneZero	O
above	O
)	O
.	O
</s>
<s>
Rational	O
and	O
computable	O
real	O
numbers	O
may	O
also	O
be	O
encoded	O
in	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
Rational	O
numbers	O
may	O
be	O
encoded	O
as	O
a	O
pair	B-Protocol
of	O
signed	O
numbers	O
.	O
</s>
<s>
The	O
references	O
given	O
describe	O
software	O
that	O
could	O
,	O
in	O
theory	O
,	O
be	O
translated	O
into	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
Once	O
real	O
numbers	O
are	O
defined	O
,	O
complex	O
numbers	O
are	O
naturally	O
encoded	O
as	O
a	O
pair	B-Protocol
of	O
real	O
numbers	O
.	O
</s>
<s>
The	O
data	O
types	O
and	O
functions	O
described	O
above	O
demonstrate	O
that	O
any	O
data	O
type	O
or	O
calculation	O
may	O
be	O
encoded	O
in	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
Most	O
real-world	O
languages	O
have	O
support	O
for	O
machine-native	O
integers	O
;	O
the	O
church	O
and	O
unchurch	O
functions	O
convert	O
between	O
nonnegative	O
integers	O
and	O
their	O
corresponding	O
Church	B-Application
numerals	I-Application
.	O
</s>
<s>
The	O
functions	O
are	O
given	O
here	O
in	O
Haskell	B-Language
,	O
where	O
the	O
\	O
corresponds	O
to	O
the	O
λ	O
of	O
Lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
Church	O
Booleans	O
are	O
the	O
Church	B-Application
encoding	I-Application
of	O
the	O
Boolean	O
values	O
true	O
and	O
false	O
.	O
</s>
<s>
Some	O
programming	O
languages	O
use	O
these	O
as	O
an	O
implementation	O
model	O
for	O
Boolean	O
arithmetic	O
;	O
examples	O
are	O
Smalltalk	B-Application
and	O
Pico	B-Language
.	O
</s>
<s>
The	O
Church	B-Application
encoding	I-Application
of	O
true	O
and	O
false	O
are	O
functions	O
of	O
two	O
parameters	O
:	O
</s>
<s>
The	O
most	O
fundamental	O
predicate	O
is	O
,	O
which	O
returns	O
if	O
its	O
argument	O
is	O
the	O
Church	B-Application
numeral	I-Application
,	O
and	O
if	O
its	O
argument	O
is	O
any	O
other	O
Church	B-Application
numeral	I-Application
:	O
</s>
<s>
Church	O
pairs	O
are	O
the	O
Church	B-Application
encoding	I-Application
of	O
the	O
pair	B-Protocol
(	O
two-tuple	O
)	O
type	O
.	O
</s>
<s>
The	O
pair	B-Protocol
is	O
represented	O
as	O
a	O
function	O
that	O
takes	O
a	O
function	O
argument	O
.	O
</s>
<s>
When	O
given	O
its	O
argument	O
it	O
will	O
apply	O
the	O
argument	O
to	O
the	O
two	O
components	O
of	O
the	O
pair	B-Protocol
.	O
</s>
<s>
The	O
definition	O
in	O
lambda	B-Language
calculus	I-Language
is	O
,	O
</s>
<s>
An	O
(	O
immutable	B-Application
)	O
list	O
is	O
constructed	O
from	O
list	O
nodes	O
.	O
</s>
<s>
cons	B-Protocol
Prepend	O
a	O
given	O
value	O
to	O
a	O
(	O
possibly	O
empty	O
)	O
list	O
.	O
</s>
<s>
Build	O
each	O
list	O
node	O
from	O
one	O
pair	B-Protocol
.	O
</s>
<s>
Represent	O
the	O
list	O
using	O
the	O
right	B-Application
fold	I-Application
function	I-Application
.	O
</s>
<s>
A	O
nonempty	O
list	O
can	O
be	O
implemented	O
by	O
a	O
Church	O
pair	B-Protocol
;	O
</s>
<s>
To	O
represent	O
null	O
,	O
the	O
pair	B-Protocol
may	O
be	O
wrapped	O
in	O
another	O
pair	B-Protocol
,	O
giving	O
free	O
values	O
,	O
</s>
<s>
Expression	O
Description	O
The	O
first	O
element	O
of	O
the	O
pair	B-Protocol
is	O
true	O
meaning	O
the	O
list	O
is	O
null	O
.	O
</s>
<s>
As	O
an	O
alternative	O
to	O
the	O
encoding	O
using	O
Church	O
pairs	O
,	O
a	O
list	O
can	O
be	O
encoded	O
by	O
identifying	O
it	O
with	O
its	O
right	B-Application
fold	I-Application
function	I-Application
.	O
</s>
<s>
For	O
example	O
,	O
a	O
list	O
of	O
three	O
elements	O
x	O
,	O
y	O
and	O
z	O
can	O
be	O
encoded	O
by	O
a	O
higher-order	B-Language
function	I-Language
that	O
when	O
applied	O
to	O
a	O
combinator	O
c	O
and	O
a	O
value	O
n	O
returns	O
c	O
x	O
(	O
c	O
y	O
(	O
c	O
z	O
n	O
)	O
)	O
.	O
</s>
<s>
An	O
alternative	O
representation	O
is	O
Scott	B-Application
encoding	I-Application
,	O
which	O
uses	O
the	O
idea	O
of	O
continuations	O
and	O
can	O
lead	O
to	O
simpler	O
code	O
.	O
</s>
<s>
(	O
see	O
also	O
Mogensen	B-Application
–	I-Application
Scott	I-Application
encoding	I-Application
)	O
.	O
</s>
<s>
For	O
example	O
,	O
using	O
Scala	B-Application
notation	O
,	O
if	O
list	O
denotes	O
a	O
value	O
of	O
type	O
List	O
with	O
empty	O
list	O
Nil	O
and	O
constructor	O
Cons(h, t )	O
we	O
can	O
inspect	O
the	O
list	O
and	O
compute	O
nilCode	O
in	O
case	O
the	O
list	O
is	O
empty	O
and	O
when	O
the	O
list	O
is	O
not	O
empty	O
:	O
</s>
<s>
Scott	B-Application
encoding	I-Application
can	O
be	O
done	O
in	O
untyped	B-Language
lambda	I-Language
calculus	I-Language
,	O
whereas	O
its	O
use	O
with	O
types	O
requires	O
a	O
type	O
system	O
with	O
recursion	O
and	O
type	O
polymorphism	O
.	O
</s>
