Skip to content

Mogensen–Scott encoding

Mogensen–Scott encoding represents constructor data as functions that select a matching handler and expose immediate fields.

Version
v1 · 2026-10-03 · History
Domain-specific #
13440
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomains
Functional Programming, Lambda Calculus → Computer Science & Software Engineering
Aliases
Scott Encoding

Core Idea

A Scott-style encoded value is a function that takes one handler for each data constructor, selects its own constructor's handler and passes its immediate fields. Mogensen used the same dispatch idea, with higher-order syntax for binding, to encode lambda terms as lambda-calculus data. The compound name should not imply that Mogensen originated every Scott-style encoding.[1][2]

How would you explain it like I'm…

The Toy That Picks Helpers

Imagine a toy that knows what kind of toy it is. You hand it a row of helpers, one helper for each kind of toy. The toy picks the helper that matches its own kind and hands that helper the parts it's holding. Mogensen–Scott encoding makes computer data act like that toy.

Data That Picks Its Handler

In some programming languages built only out of functions, there is no special 'look at what kind of thing this is' command. A Scott-style encoding solves this by turning each piece of data into a function that waits for one helper function for each possible kind of data. When you give it the helpers, the data picks the helper for its own kind and passes along the pieces it was built from. If one of those pieces is another piece of the same kind of data, it is passed along just as it is, not already worked out. Torben Mogensen used this idea to store whole programs as data inside the same function-only language.

Constructor-Dispatch Lambda Encoding

A Scott-style encoding represents a value of an algebraic data type, such as a list or tree, as a lambda term that waits for one handler per constructor. Applied to the handlers, the value selects the handler that matches its own constructor and passes it that constructor's immediate fields. This expresses case analysis, like a 'case' or 'match' statement, purely through function application. Its key difference from a Church encoding is that a recursive child, such as the tail of a list, is handed over as a child, not already folded into a result. Torben Mogensen applied this constructor-dispatch idea, combined with higher-order abstract syntax for variable binding, to represent lambda-calculus programs as data within the lambda calculus itself. The scheme was not invented whole by Mogensen; the compound name covers the family of encodings plus his notable application.

 

The Mogensen–Scott encoding names a family of Scott-style representations of algebraic data in the lambda calculus together with Mogensen's notable application to lambda syntax. In a Scott-style encoding, a value is a lambda term that awaits one handler per constructor; applying it to the handlers selects the branch for its own constructor and supplies that constructor's immediate fields. Case analysis is thus expressed by function application rather than a primitive case operator. The decisive contrast with Church-style encodings of recursive data is that recursive children are passed to the branch as children, not pre-folded into results. Mogensen used this constructor-dispatch scheme, combined with higher-order abstract syntax for binders, to represent lambda terms as data within the lambda calculus itself, enabling self-representation. The naming needs care: Scott-style encodings predate Mogensen, and the compound label covers both the general encoding family and his application.

Scope of Application

In pure lambda calculus this implements case analysis without a primitive case. For Peano numerals, Zero = λz s.z and Succ n = λz s.s n. For Mogensen's lambda terms, variable, application and abstraction select three distinct handlers; abstraction passes a host lambda for its bound body. A separate self-interpreter, not constructor selection alone, performs evaluation.[1][2]

Clarity

Let pred = λn.n Zero (λp.p). Applied to Succ (Succ Zero), the successor branch receives the stored Succ Zero and returns it; applied to Zero, the zero branch returns Zero. That is one-layer deconstruction in a few beta steps, not a guarantee about wall-clock time. Mogensen's original p. 348 encoding of λx.xx selects abstraction at the root, application in the body, then variable for each x: subterm codes are passed, not already evaluated.[1][2]

Manages Complexity

Count the constructors and give a handler for each. Which handler runs identifies the constructor, and its arguments are the fields. This separates quotation, one-layer inspection and recursive interpretation. A whole-structure traversal still needs an external recursor or fixed point.[1]

Abstract Reasoning

Replace the immediate recursive child with a pre-folded result and the representation becomes Church-like. Scott encoding favors local deconstruction; Church-style folding favors whole-structure elimination already embodied in the value. Which is convenient depends on the operation, not a universal speed ranking.[2][3]

Knowledge Transfer

The dispatch equations transfer to finite-constructor types when arities and handler order are explicit. This is a specialized Encoding and Decoding pattern, not a name for every Scott encoding. Mogensen's binder representation is a specific extension for lambda syntax; recursive children are not assumed pre-folded.[1]

Relationships to Other Abstractions

Local relationship map for Mogensen–Scott encodingParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Mogensen–ScottencodingDOMAINPrime abstraction: Encoding And Decoding — is a kind ofEncodingAnd DecodingPRIME

Current abstraction Mogensen–Scott encoding Domain-specific

Parents (1) — outgoing typed relations

  • Mogensen–Scott encoding is a kind of Encoding And Decoding Prime

    Mogensen–Scott is a constructor encoding.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Mogensen–Scott encoding sits in a sparse region of the domain-specific corpus (72nd percentile for distinctiveness): few abstractions share its structure, so a faithful description tends to retrieve it precisely.

Family — Type Systems & Functional Constructs (20 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] Torben Æ. Mogensen, “Efficient Self-Interpretation in Lambda Calculus,” JFP 2(3) (1992), 345–364, pp. 347–349. registry ↩a ↩b ↩c ↩d ↩e

[2] Ben Lynn, “The Scott encoding”, original worked construction. registry ↩a ↩b ↩c ↩d

[3] Jan Martin Jansen, “Programming in the λ-Calculus: From Church to Scott and Back” (2013), publisher abstract. registry ↩