Typed Lambda Calculus and CCCs
The typed lambda calculus and the cartesian closed category are two presentations of the same theory. Types become objects, terms with one free variable become arrows, product types become products, and function types become exponentials, with abstraction matching currying and application matching evaluation.
╌╌╌╌
The cartesian closed category was built to model functions of several variables: products for tupling, exponentials for currying, evaluation for application. The typed lambda calculus is a syntax for exactly the same operations, written with variables and binders instead of arrows and composites. The two are equivalent, and the equivalence is constructive: a lambda theory generates a category, a category has an internal language, and the two constructions undo one another.1
The typed lambda calculus
A typed lambda calculus is a formal theory of four kinds of syntactic object: types, terms, variables, and equations. Every term has a type , written . The type-forming and term-forming rules below are the minimum a typed lambda calculus must contain; a given theory may add further types, terms, and equations on top.2
The notation marks a term that may contain the variable ; is the result of substituting for every free occurrence of . The application term is applied to ; the backtick is usually dropped, writing . A variable occurrence is bound in and free otherwise; a term is closed if no variable is free in it.
The equations have the form , where and are terms of the same type and is a finite set of variables containing every variable free in or . The equality is a congruence (TL-9 through TL-14 make reflexive, symmetric, transitive, and compatible with pairing, projection, application, and abstraction). The content is in the last five rules.
The equation does not assert that and are identical strings; two terms are identical only if they are literally the same. It asserts that in every interpretation of the calculus the two terms must denote the same thing. The pair TL-15/TL-16 says the product type behaves like a product; the pair / says the function type behaves like a space of functions.
The category of a lambda theory
From a typed lambda calculus one builds a category .
Equivalence classes are needed because any two variables of the same type must name the same arrow — the identity. A variable of type , read as a term of type with one free variable of type , is the identity arrow ; choosing a different variable of type must not change the arrow, so the two terms are identified. Because a term of type is equated to by TL-10, the type becomes a terminal object.
Composition is substitution. If of type names an arrow and of type names an arrow , their composite is the class of , the term obtained by substituting for the free variable of . Substitution is associative and the variable-term is a two-sided unit, so this is a category.
The cartesian closed structure is the obvious one. Given a term with of type naming an arrow , use TL-15 to substitute for , getting with of type and of type ; then the transpose is the class of . Uniqueness of the cartesian closed structure means no separate verification of the exponential is required beyond exhibiting it.
The construction turns each term-forming rule into a piece of categorical structure, and each characteristic equation into a categorical law.
| Lambda calculus | Cartesian closed category |
|---|---|
| type | object |
| term of type , one free | arrow |
| type , term | terminal object |
| product type | product |
| pairing | pair arrow |
| projections | product projections |
| function type | exponential |
| abstraction | currying |
| application | |
| (TL-17) | |
| (TL-18) | |
| substitution | composition |
Beta and eta as categorical equations
The two characteristic function-type equations are the two triangle-shaped laws of the exponential. The rule is the defining equation of currying, and the rule is its uniqueness clause stated backwards.
Here again denotes the transpose . On the left, is TL-17: applying the abstraction to returns . On the right, is TL-18: the abstraction is itself. The rule TL-19, renaming a bound variable, has no categorical content at all: after translation there are no variables to rename, so the two sides are literally equal.
The internal language and the round trip
The reverse construction reads a category as a syntax. Given a cartesian closed category with chosen finite products, its internal language is the typed lambda calculus whose types are the objects of , with variables of each type, and with terms and equations generated by TL-3 through TL-19. The two constructions are inverse up to equivalence.
The equivalence is why either formalism may be used and translated to the other without loss. A construction that is awkward as syntax (substitution under binders, variable capture) may be clean as arrows (composition), and vice versa.
Arrows against terms
The two presentations describe the same function differently. Consider the polynomial given by
In the typed lambda calculus this is written almost as in ordinary mathematics, with explicit variables and binders,
whereas categorically it is a composite of product projections, a diagonal to duplicate arguments, multiplications, and an addition, which condenses to
reading for the projections that the variables named. The only surface difference on this example is that the arrow form writes where the term form writes . The deeper difference is emphasis: the term is a formula for the result, the arrow is a computation process, and the arrow form exposes that the two multiplications may run in parallel.3
The arrow form handles repeated variables explicitly. In the term , a variable free in both and must not be duplicated into two independent inputs; the categorical translation inserts a generalized diagonal on the shared variables. The single-variable case is the diagonal above: is , not the identity on , whereas with distinct of type is the identity . Distinct variables of the same type stay distinct inputs; the same variable used twice is one input fed forward.
Strengths of each presentation
- The categorical form has no variables. There is nothing to rename, so the clash of variables — the source of the side conditions on , , and — cannot arise. The rule , hedged in the syntax by "provided is not free in ," becomes the identically true once the variables are gone.
- Composition is built in. An arrow is a composite by construction, so the full apparatus of commutative diagrams and the theorems of category theory apply directly to programs written this way.
- The lambda form is closer to practice. Variables and binders match how functions are written and read, and substitution is the familiar operation of plugging in an argument.
The equivalence means neither presentation is primary. A cartesian closed category is a variable-free account of a typed functional language, and a typed lambda calculus is a syntax with variables for a cartesian closed category. Neither, on its own, provides recursion: that turns on which arrows of a cartesian closed category admit fixed points.
Footnotes
- Barr & Wells, Category Theory for Computing Science, §6.4 — the equivalence of typed lambda calculi and cartesian closed categories, following Lambek and Scott. ↩
- Barr & Wells, §6.3.1–6.3.3 — the definition of a typed lambda calculus: type/term formation rules TL-1 through TL-8 and the equations TL-9 through TL-19, with free/bound variables and substitution. ↩
- Barr & Wells, §6.5.1–6.5.3 — the example rendered as a term and as an arrow, the generalized diagonal for repeated variables, and the advantages of the variable-free categorical presentation. ↩
╌╌ END ╌╌