In computer science, **Scott encoding** is a way to represent (recursive) data types in the lambda calculus. Church encoding performs a similar function. The data and operators form a mathematical structure which is embedded in the lambda calculus.

Whereas Church encoding starts with representations of the basic data types, and builds up from it, Scott encoding starts from the simplest method to compose algebraic data types.

**Mogensen–Scott encoding** extends and slightly modifies Scott encoding by applying the encoding to Metaprogramming. This encoding allows the representation of lambda calculus terms, as data, to be operated on by a meta program.

Scott encoding appears first in a set of unpublished lecture notes by Dana Scott. Torben Mogensen later extended Scott encoding for the encoding of Lambda terms as data.

Lambda calculus allows data to be stored as parameters to a function that does not yet have all the parameters required for application. For example,

(
(
λ
x
1
…
x
n
.
λ
c
.
c
x
1
…
x
n
)
v
1
…
v
n
)
f
May be thought of as a record or struct where the fields
x
1
…
x
n
have been initialized with the values
v
1
…
v
n
. These values may then be accessed by applying the term to a function *f*. This reduces to,

f
v
1
…
v
n

*c* may represent a constructor for an algebraic data type in functional languages such as Haskell. Now suppose there are *N* constructors, each with
A
i
arguments;

Constructor
Given arguments
Result
(
(
λ
x
1
…
x
A
1
.
λ
c
1
…
c
N
.
c
1
x
1
…
x
A
1
)
v
1
…
v
A
1
)
f
1
…
f
N
f
1
v
1
…
v
A
1
(
(
λ
x
1
…
x
A
2
.
λ
c
1
…
c
N
.
c
2
x
1
…
x
A
2
)
v
1
…
v
A
2
)
f
1
…
f
N
f
2
v
1
…
v
A
2
⋮
⋮
⋮
(
(
λ
x
1
…
x
A
N
.
λ
c
1
…
c
N
.
c
N
x
1
…
x
A
N
)
v
1
…
v
A
N
)
f
1
…
f
N
f
N
v
1
…
v
A
N
Each constructor selects a different function from the function parameters
f
1
…
f
N
. This provides branching in the process flow, based on the constructor. Each constructor may have a different arity (number of parameters). If the constructors have no parameters then the set of constructors acts like an *enum*; a type with a fixed number of values. If the constructors have parameters, recursive data structures may be constructed.

Let *D* be a datatype with *N* constructors,
{
c
i
}
i
=
1
N
, such that constructor
c
i
has arity
A
i
.

The Scott encoding of constructor
c
i
of the data type *D* is

λ
x
1
…
x
A
i
.
λ
c
1
…
c
N
.
c
i
x
1
…
x
A
i
Mogensen extends Scott encoding to encode any untyped lambda term as data. This allows a lambda term to be represented as data, within a Lambda calculus meta program. The meta function *mse* converts a lambda term into the corresponding data representation of the lambda term;

mse
[
x
]
=
λ
a
,
b
,
c
.
a
x
mse
[
M
N
]
=
λ
a
,
b
,
c
.
b
mse
[
M
]
mse
[
N
]
mse
[
λ
x
.
M
]
=
λ
a
,
b
,
c
.
c
(
λ
x
.
mse
[
M
]
)
The "lambda term" is represented as a tagged union with three cases:

Constructor *a* - a variable (arity 1, not recursive)
Constructor *b* - function application (arity 2, recursive in both arguments),
Constructor *c* - lambda-abstraction (arity 1, recursive).
For example,

mse
[
λ
x
.
f
(
x
x
)
]
λ
a
,
b
,
c
.
c
(
λ
x
.
mse
[
f
(
x
x
)
]
)
λ
a
,
b
,
c
.
c
(
λ
x
.
λ
a
,
b
,
c
.
b
mse
[
f
]
mse
[
x
x
]
)
λ
a
,
b
,
c
.
c
(
λ
x
.
λ
a
,
b
,
c
.
b
(
λ
a
,
b
,
c
.
a
f
)
mse
[
x
x
]
)
λ
a
,
b
,
c
.
c
(
λ
x
.
λ
a
,
b
,
c
.
b
(
λ
a
,
b
,
c
.
a
f
)
(
λ
a
,
b
,
c
.
b
mse
[
x
]
mse
[
x
]
)
)
λ
a
,
b
,
c
.
c
(
λ
x
.
λ
a
,
b
,
c
.
b
(
λ
a
,
b
,
c
.
a
f
)
(
λ
a
,
b
,
c
.
b
(
λ
a
,
b
,
c
.
a
x
)
(
λ
a
,
b
,
c
.
a
x
)
)
)

The Scott encoding coincides with the Church encoding for booleans. Church encoding of pairs may be generalized to arbitrary data types by encoding
c
i
of *D* above as

λ
x
1
…
x
A
i
.
λ
c
1
…
c
N
.
c
i
(
x
1
c
1
…
c
N
)
…
(
x
A
i
c
1
…
c
N
)
compare this to the Mogensen Scott encoding,

λ
x
1
…
x
A
i
.
λ
c
1
…
c
N
.
c
i
x
1
…
x
A
i
With this generalization, the Scott and Church encodings coincide on all enumerated datatypes (such as the boolean datatype) because each constructor is a constant (no parameters).

Church-encoded data and operations on them are typable in system F, but Scott-encoded data and operations are not obviously typable in system F. Universal as well as recursive types appear to be required, and since strong normalization does not hold for recursively typed lambda calculus, termination of programs manipulating Scott-encoded data cannot be established by determining well-typedness of such programs.