Partial Combinatory Algebras
This blueprint, and more generally the partial-combinatory-algebras Lean project, is part of the teaching materials for the course Formalized mathematics and proof assistants. It serves as a model blueprint to help students create their own class project blueprints.
1 Basic definitions
The set of partial elements \(\widetilde{A}\) in \(A\) is
We think of \(\emptyset \) as the undefined value and \(\{ a\} \) the defined, or total value \(a \in A\). We construe each \(a \in A\) also as the element \(\{ a\} \in \widetilde{A}\), i.e., there is an implicit coercion \(A \to \widetilde{A}\).
Given \(u \in \widetilde{A}\), write \(u\, {\Downarrow }\) to mean that \(u\) is defined, i.e., \(\exists a \in A.\, a \in u\).
Initial experimentation with formalization shows that it is advantageous to work with partial elements rather than elements, as that avoids coercions and helps the rpoof assistant compute types. When a partial element should be total this is expressed as a separate claim.
A partial application on a set \(A\) is a map \({-} \cdot _A {-} : \widetilde{A} \times \widetilde{A} \to \widetilde{A}\).
We also write \(u \, v\) and \(u \cdot v\) in place of \(u \cdot _A v\) and we associate application to the left, so that \(u \cdot v \cdot w = (u \cdot v) \cdot w\).
The preceding defintion is non-standard, as one usually expects \({-} \cdot _A {-} : A \times A \to \widetilde{A}\). However, as explained above, we prefer our definition as it is easier to work with. You might also have expected strictness conditions stating that \(u \cdot _A v\, {\Downarrow }\) implies \(u\, {\Downarrow }\) and \(v\, {\Downarrow }\), which we did have in the initial formalization, but they were never used. It looks like our formalization already lead to a small discovery, namely that partial combinatory algebras work just as well when its elements can be applied to undefined values.
A set \(A\) with a partial map \({-} \cdot _A {-}\) is a partial combinatory algebra (PCA) if there are elements \(\mathsf{K}, \mathsf{S}\in \widetilde{A}\) such that, for all \(u, v, w \in \widetilde{A}\):
2 Combinatory completeness
Given a set \(\Gamma \) of variables and a set \(A\) of elements, the expressions \(\mathsf{Expr}\, \Gamma \, A\) are generated inductively by the following clauses:
the constants \(\mathtt{K}\) and \(\mathtt{S}\) are expressions,
an element \(a \in A\) is an expression,
a variable \(x \in \Gamma \) is an expression,
if \(e_1\) and \(e_2\) are expressions then \(e_1 \cdot e_2\) is an expression.
The application \(e_1 \cdot e_2\) in the third clause of the definition is formal, i.e., it is just a constructor of an inductive datatype. We again deviated from the standard definition in an inessential way by including primitive constants \(\mathsf{K}\) and \(\mathsf{S}\), separate from the corresponding elements of \(A\).
Given a set of variables \(\Gamma \) and a set \(A\), a valuation is a map \(\eta : \Gamma \to A\) which assigns elements to variables.
Given a valuation \(\eta : \Gamma \to A\), \(x \in A\) and \(a \in a\), we may override \(\eta \) to get a valuation \(\eta [x \mapsto a] : \Gamma \to A\) such that
The evaluation \([\! [ e ]\! ] \, \eta \) of an expression \(e \in \mathsf{Expr}\, \Gamma \, A\) in a PCA \(A\) at a valuation \(\eta : \Gamma \to A\) is defined recursively by the clauses
An expression \(e \in \mathsf{Expr}\, \Gamma \, A\) is defined when \([\! [ e ]\! ] \, \eta \, {\Downarrow }\) for all \(\eta : \Gamma \to A\).
The abstraction of an expression \(e \in \mathsf{Expr}\, \Gamma \, A\) with respect to a variable \(x \in \Gamma \) is the expression \(\langle x \rangle \, e \in \mathsf{Expr}\, \Gamma \, A\) defined recursively by
An abstraction \(\langle x \rangle \, e\) is defined.
By straightforward structural induction on \(e\).
Let \(x \in \Gamma \) and \(e \in \mathsf{Expr}\, (\Gamma \cup \{ x\} )\, A\). Then for every \(a \in A\) and \(\eta : \Gamma \to A\)
By straightforward structural induction on \(e\).
3 Programming with PCAs
We next show how to write programs in a PCA \(A\). We call the elements of \(A\) accomplishing various programming tasks combinators.
There is the identity combinator \(\mathsf{I} \in A\) such that \(I \, a = a\) for all \(a \in A\).
There are combinators \(\mathsf{pair}, \mathsf{fst}, \mathsf{snd} \in A\) such that, for all \(a, b \in A\),
There are combinators \(\mathsf{ite} \, \mathsf{fal}, \mathsf{tru} \in A\) such that, for all \(a, b \in a\),
For each \(n \in \mathbb {N}\) there is \(\overline{n} \in A\), as well as combinators \(\mathsf{succ}, \mathsf{primrec} \in A\) such that, for all \(n \in \mathbb {N}\) and \(a, f \in A\),
There is a combinator \(\mathsf{Y} \in A\) such that, for all \(a \in A\),
There is a combinator \(\mathsf{Z} \in A\) such that, for all \(f, a \in A\),
A partial map \(f : A \rightharpoonup A\) is represented by \(a \in A\) when, for all \(b \in B\), \(f(b) = a \cdot b\).
Every general recursive map \(f : \mathbb {N}\rightharpoonup \mathbb {N}\) is represented in the following sense: there is \(a \in A\) such that, for all \(n \in \mathbb {N}\), if \(f(n)\, {\Downarrow }\) then \(\overline{f(n)} = a \cdot \overline{n}\).
4 (Total) combinatory algebras
A (total) combinatory algebra (CA) is given by a carrier set \(A\) and a total binary operation \({-} \cdot {-} : A \times A \to A\), such that there are \(\mathsf{K}, \mathsf{S}\) satisfying the characteristic equations
Every combinatory algebra is a partial combinatory algebra.
Simply reuse the total application as the partial one.
5 Examples of PCAs
5.1 Free combinatory algebra
The free combinatory algebra is generated freely by the symbols \(\mathsf{K}\) and \(\mathsf{S}\) and formal binary application, quotiented by provable equality.
5.2 The graph model
A listing on a set \(A\) is a section \(\mathsf{fromList} : \mathsf{List}\, A \to A\) and a retraction \(\mathsf{toList} : A \to \mathsf{List}\, A\).
Suppose \(A\) is a set with a listing. It induces a pairing on \(A\): \(\langle x, y \rangle = \mathsf{fromList}\, [x,y]\) with corresponding projections. Each \(x \in A\) may be seen as a finite subset of \(A\),
The application \({-} \cdot {-} : \mathcal{P}\, A \times \mathcal{P}\, A \to \mathcal{P}\, A\) is defined by
The set \(\mathcal{P}\, A\) with the application as above is a combinatory algebra.