Mogensen–Scott encoding explained

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.

History

Scott encoding appears first in a set of unpublished lecture notes by Dana Scottwhose first citation occurs in the book Combinatorial Logic, Volume II.[1] Michel Parigot gave a logical interpretation of and strongly normalizing recursor for Scott-encoded numerals,[2] referring to them as the "Stack type" representation of numbers.Torben Mogensen later extended Scott encoding for the encoding of Lambda terms as data.[3]

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,

((λx1\ldotsxn.λc.cx1\ldotsxn)v1\ldotsvn)f

May be thought of as a record or struct where the fields

x1\ldotsxn

have been initialized with the values

v1\ldotsvn

. These values may then be accessed by applying the term to a function f. This reduces to,

fv1\ldotsvn

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

Ai

arguments;

\begin{array}{c|c|c} Constructor&Givenarguments&Result\\ \hline ((λx1\ldots

x
A1

.λc1\ldotscN.c1 x1\ldots

x
A1

)v1\ldots

v
A1

)& f1\ldotsfN& f1 v1\ldots

v
A1

\\ ((λx1\ldots

x
A2

.λc1\ldotscN.c2 x1\ldots

x
A2

)v1\ldots

v
A2

)& f1\ldotsfN& f2 v1\ldots

v
A2

\\ \vdots&\vdots&\vdots\\ ((λx1\ldots

x
AN

.λc1\ldotscN.cNx1\ldots

x
AN

)v1\ldots

v
AN

)& f1\ldotsfN& fNv1\ldots

v
AN

\end{array}

Each constructor selects a different function from the function parameters

f1\ldotsfN

. 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.

Definition

Let D be a datatype with N constructors,

\{ci\}

N
i=1
, such that constructor

ci

has arity

Ai

.

Scott encoding

The Scott encoding of constructor

ci

of the data type D is

λx1\ldots

x
Ai

.λc1\ldotscN.cix1\ldots

x
Ai

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;

\begin{align} \operatorname{mse}[x]&=λa,b,c.ax\\ \operatorname{mse}[MN]&=λa,b,c.b\operatorname{mse}[M]\operatorname{mse}[N]\\ \operatorname{mse}[λx.M]&=λa,b,c.c(λx.\operatorname{mse}[M])\\ \end{align}

The "lambda term" is represented as a tagged union with three cases:

For example,

\begin{array}{l} \operatorname{mse}[λx.f(xx)]\\ λa,b,c.c(λx.\operatorname{mse}[f(xx)])\\ λa,b,c.c(λx.λa,b,c.b\operatorname{mse}[f]\operatorname{mse}[xx])\\ λa,b,c.c(λx.λa,b,c.b(λa,b,c.af)\operatorname{mse}[xx])\\ λa,b,c.c(λx.λa,b,c.b(λa,b,c.af)(λa,b,c.b\operatorname{mse}[x]\operatorname{mse}[x]))\\ λa,b,c.c(λx.λa,b,c.b(λa,b,c.af)(λa,b,c.b(λa,b,c.ax)(λa,b,c.ax))) \end{array}

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

ci

of D above as

λx1\ldots

x
Ai

.λc1\ldotscN.ci(x1c1\ldotscN)\ldots

(x
Ai

c1\ldotscN)

compare this to the Mogensen Scott encoding,

λx1\ldots

x
Ai

.λc1\ldotscN.cix1\ldots

x
Ai

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).

Concerning the practicality of using either the Church or Scott encoding for programming, there is a symmetric trade-off:[4] Church-encoded numerals support a constant-time addition operation and have no better than a linear-time predecessor operation; Scott-encoded numerals support a constant-time predecessor operation and have no better than a linear-time addition operation.

Type definitions

Church-encoded data and operations on them are typable in system F, as are Scott-encoded data and operations. However, the encoding is significantly more complicated.[5]

The type of the Scott encoding of the natural numbers is the positive recursive type:

\muX.\forallR.R\to(X\toR)\toR

Full recursive types are not part of System F, but positive recursive types are expressible in System F via the encoding:

\muX.G[X]=\forallX.((G[X]\toX)\toX)

Combining these two facts yields the System F type of the Scott encoding:

\forallX.(((\forallR.R\to(X\toR)\toR)\toX)\toX)

This can be contrasted with the type of the Church encoding:

\forallX.X\to(X\toX)\toX

The Church encoding is a second-order type, but the Scott encoding is fourth-order!

See also

References

Notes and References

  1. Book: Curry . Haskell . Haskell Curry . Combinatorial Logic, Volume II . 1972 . North-Holland Publishing Company . 0-7204-2208-6.
  2. 2nd European Symposium on Programming. Nancy, France, March 21–24, 1988. Parigot . Michel . Programming with proofs: A second order type theory . H. Ganzinger. European Symposium on Programming: ESOP '88 . Lecture Notes in Computer Science . 1988 . 300 . Springer. 145–159 . 10.1007/3-540-19027-9_10 . 978-3-540-19027-1 . free .
  3. Mogensen. Torben. Efficient Self-Interpretation in Lambda Calculus. Journal of Functional Programming. 1994. 2. 3. 345–364. 10.1017/S0956796800000423. 8736707.
  4. Parigot . Michel . On the representation of data in lambda-calculus . International Workshop on Computer Science Logic: CSL '89 . Lecture Notes in Computer Science . 1990 . 440 . 209–321 . 10.1007/3-540-52753-2_47 . 978-3-540-52753-4 . Springer . 3rd Workshop on Computer Science Logic. Kaiserslautern, FRG, October 2-6, 1989 . Egon Börger . Hans Kleine Büning. Michael M. Richter .
  5. See the note "Types for the Scott numerals" by Martín Abadi, Luca Cardelli and Gordon Plotkin (February 18, 1993).