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.
Contents
- History
- Discussion
- Definition
- Scott encoding
- MogensenScott encoding
- Comparison to the Church encoding
- Type definitions
- References
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.
History
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.
Discussion
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,
May be thought of as a record or struct where the fields
c may represent a constructor for an algebraic data type in functional languages such as Haskell. Now suppose there are N constructors, each with
Each constructor selects a different function from the function parameters
Definition
Let D be a datatype with N constructors,
Scott encoding
The Scott encoding of constructor
Mogensen–Scott encoding
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;
The "lambda term" is represented as a tagged union with three cases:
For example,
Comparison to the Church encoding
The Scott encoding coincides with the Church encoding for booleans. Church encoding of pairs may be generalized to arbitrary data types by encoding
compare this to the Mogensen Scott encoding,
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).
Type definitions
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.