Lecture 10: Categorical combinators, Categorical Abstract Machines

Teacher: Pierre-Louis Curien

Lecture 10

\[\newcommand\xto\xrightarrow \newcommand\xfrom\xleftarrow \newcommand{\Tr}{\mathop{\mathrm{Tr}}}\]
  • $λ$-calculus $\leftrightarrow$ combinatory logic (S, K, I, …)
  • De Bruijn notation
  • categorical combinators

this connection works in a typed setting (simply typed $λ$-calculus + product types)

⟶ Categorical Abstract Machine (1985) ⟹ gave rise to CAML (then: Xavier Leroy ⟶ Zinc machine: enhanced a lot CAML)


$λ$-calculus

\[M \; ≝ \; x \; \mid \; λx. M \; \mid \; MM\]

$λ$-calculus in (De Bruijn) DB notation

\[M \; ≝ \; \underbrace{n}_{∈ ℕ} \; \mid \; λ.M \; \mid \; MM\]

Intuition: In $λx. x (λy. xu(zy))$, you can draw pointers from variables to their binders. For free variables, you can imagine they are bound by external implicit binders.

In DB notation: a variable is replaced by $n$, where $n$ is the number of $λ$’s to cross in the tree representation to reach the relevant binder.

\[λx. x (λy. xu(zy)) \leadsto λ. 0 (λ. 12(30))\]

Notation:

$DB(M)_l$, where $l$ is a list for free variables:

\[DB(x)_{\vec{x_1}, x, \vec{x_2}} = \vert \vec{x_2} \vert \qquad \text{where } x ∉ \vec{x_2} \\ DB(MN) = DB(M) DB(N)\\ DB(λx. M)_l = λ.DB(M)_{l, x}\]

where $l,x$ is thought of as the list x :: l

Invariant: $DB(M)_l$ correctly defined ⟺ $l$ contains all free variables of $M$


Th: \(M ⟶_β N ∧ fv(M) ⊆ l ⟹ DB(M)_l ⟶ DB(N)_l\)

Dynamics:

\[(λx. M) N ⟶ M[N/x]\\ (λ.M) N ⟶ M[0 ← N]\]

where

\[m[i ← N] = \begin{cases} m &&\text{ if } m<i \\ τ_0^i(N) &&\text{ else if } m = i\\ m-1 &&\text{ else if } m > i \end{cases}\\ (M_1 M_2)[i ← N] = M_1[i ← N] M_2[i ← N]\\ (λ.M)[i ← N] = λ. (M [i+1 ← N])\]
\[τ_i^n(j) = \begin{cases} j &&\text{ if } j<i \\ j+n &&\text{ else if } j ≥ i \end{cases}\\ τ_i^n(N_1 N_2) = τ_i^n(N_1) τ_i^n(N_2)\\ τ_i^n(λ.N) = λ.τ_{i+1}^n(N)\\\]

Categorical combinators

Morphisms: identity, composition, pairing, first and second projections, exponential

\[f \; ≝ \; id \; \mid \; f \circ f \\ \; \mid \; ⟨f, g⟩ \; \mid \; π \; \mid \; π ' \; \mid \; ! \\ \; \mid \; Λ(f) \; \mid \; ev\] \[A \; ≝ \; \underbrace{\underline{a}}_{\text{ground type}} \\ \; \mid \; 1 \; \mid \; A × A \\ \; \mid \; A ⇒ A\]

Morphism from $A$ to $B$ denoted by

\[A ⊢ f: B\]

Inference rules:

\[\cfrac{}{A ⊢ id: A}\] \[\cfrac{A ⊢ f: B \qquad B ⊢ g: C}{A ⊢ g \circ f: C}\] \[\cfrac{A ⊢ f: B \qquad A ⊢ g: C}{A ⊢ ⟨f, g⟩: B × C}\] \[\cfrac{}{A × B ⊢ π: A}\] \[\cfrac{}{A × B ⊢ π': B}\] \[\cfrac{}{A ⊢ \, !: 1}\]

Cone condition for the product:

\[π \circ ⟨f, g⟩ ⟶ f\\ π' \circ ⟨f, g⟩ ⟶ g\\\]

Uniqueness condition:

\[⟨f, g⟩ \circ h ⟶ ⟨f \circ h, g \circ h⟩\\ ⟨π, π'⟩ = id \qquad η\text{-rule/surjective pairing}\]

Terminal object:

\[x ⟶ \, ! \qquad \text{provided that } A ⊢ x: 1\]
\[\cfrac{A × B ⊢ f: C}{A ⊢ Λ(f): B ⇒ C}\] \[\cfrac{}{(A ⇒ B) × A ⊢ ev: B}\]

Universal triangle (for the counit) condition:

\[ev \circ ⟨Λ(f), g⟩ ⟶ f \circ ⟨id, g⟩\\ Λ(f) \circ h ⟶ Λ(f \circ ⟨h \circ π, π'⟩)\\ Λ(ev) = id \qquad η\text{-rule}\]

Explicit substitutions

\[M \; ≝ \; \underbrace{n}_{∈ ℕ} \; \mid \; λ.M \; \mid \; MM \; \mid \; M[i ← N] \; \mid \; τ_i^n(N)\] \[(λ.M) N ⟶_B M[0 ← N]\]

and what were equations before are now rewriting rules:

\[m[i ← N] ⟶ \begin{cases} m &&\text{ if } m<i \\ τ_0^i(N) &&\text{ else if } m = i\\ m-1 &&\text{ else if } m > i \end{cases}\\ (M_1 M_2)[i ← N] ⟶ M_1[i ← N] M_2[i ← N]\\ (λ.M)[i ← N] ⟶ λ. (M [i+1 ← N])\] \[τ_i^n(j) ⟶ \begin{cases} j &&\text{ if } j<i \\ j+n &&\text{ else if } j ≥ i \end{cases}\\ τ_i^n(N_1 N_2) ⟶ τ_i^n(N_1) τ_i^n(N_2)\\ τ_i^n(λ.N) ⟶ λ.τ_{i+1}^n(N)\\\]

Typing rules for DB $λ$-calculus:

\[\cfrac{}{Γ, A_i, …, A_0 ⊢ i: A_i}\]

etc…

Then, we need to go from an infinitary syntax ($n ∈ ℕ$) to a finitary one:

\[[[i]] = π' \circ π^i\]

because to go from $Γ × A_i × ⋯ × A_0$ categorically: apply $π$ $i$ times, then $π’$

\[[[MN]] = ev \circ ⟨[[M]], [[N]]⟩\\ [[λ.M]] = Λ([[M]])\]

Then, define the syntactic sugar:

\[P(f) ≝ ⟨f \circ π, π'⟩\] \[[[M[i ← N]]] = [[M]] \circ P^i(⟨id, [[N]]⟩)\\ [[τ_i^n(N)]] = [[N]] \circ P^i(π^n)\]

Th: If $M ⟶ N$ in DB calculus with explicit substitutions, then \([[M]] ⟶^\ast [[N]]\)

Ex:

\[τ_i^n(λ.N) ⟶ λ.τ_{i+1}^n(N)\]

corresponds to

\[Λ(f) \circ h ⟶ Λ(f \circ ⟨h \circ π, π'⟩)\]

⟹ categorically, this amounts to the naturality of $Λ$

etc…

Categorical Abstract Machine

When would an operational semantics be called an abstract machine?

Example of a (deterministic) operational semantics:

CBV

\[\cfrac{M ⟶ M_1}{M N ⟶ M_1 N}\\ \, \\ \, \\ \cfrac{N ⟶ N_1}{(λx. M) N ⟶ (λx.M) N_1} \\ \, \\ \, \\ \cfrac{V \text{ value}}{(λx.M) V ⟶ M[x ← V]}\]

But it is not an abstract machine: in an abstract machine, the part on which the machine should focus is explicitely specified:

\[MN \leadsto \underbrace{(- N)}_{\text{environment}}[- ← \underbrace{M}_{\text{code}}]\]

In an abstract machine:

\[⟨ \underbrace{\text{code}}_{\text{will be a cat. combinator}} \; \mid \; \text{environment}⟩\]

Cat. combinator:

\[g \circ f \leadsto f ; g\\ ⟨f,g⟩ \leadsto ⟨; f;,; g; ⟩\]

Big Step Operational Semantics

Formal judgements of this form:

\[⟨f \; \mid \; s⟩ = t\]

where

  • $f$ is a categorical combinator
  • $s$ is an environment
  • $s$ and $t$ are in the same syntactic category
  • categorical interpretation: $f$ applied to $s$
\[⟨id \; \mid \; s⟩ = s\]

$id$ is tantamount to

  • the skip instruction
  • the identity morphism in the CCC $Set$

Then:

\[\cfrac{⟨f \; \mid \; s⟩ = s_1 \qquad ⟨g \; \mid \; s_1⟩ = s_2}{⟨f; g \; \mid \; s⟩ = s_2}\] \[\cfrac{}{⟨π \; \mid \; (s_1, s_2)⟩ = s_1}\\ \, \\ \cfrac{}{⟨π' \; \mid \; (s_1, s_2)⟩ = s_2}\\ \, \\ \cfrac{}{⟨! \; \mid \; s⟩ = ()}\] \[\cfrac{⟨f \; \mid \; s⟩ = s_1 \qquad ⟨g \; \mid \; s⟩ = s_2}{⟨⟨f, g⟩ \; \mid \; s⟩ = (s_1, s_2)}\\ \, \\\] \[\cfrac{}{⟨Λ(f) \; \mid \; s⟩ = \underbrace{Λ(f)s}_{\text{a closure}}}\\ \, \\ \cfrac{⟨f \; \mid \; (s,t)⟩ = s_1}{⟨ev \; \mid \; (Λ(f)s, t)⟩ = s_1}\\ \, \\ \cfrac{}{⟨\underline{f} \; \mid \; g⟩ = \underline{f \circ g}}\\ \, \\\]

Hence:

Code/Cat. combinators:

\[f \; ≝ \; id \; \mid \; f \circ f \\ \; \mid \; ⟨f, g⟩ \; \mid \; π \; \mid \; π ' \; \mid \; ! \\ \; \mid \; Λ(f) \; \mid \; ev\]

Environments/Values:

\[s \; ≝ \; () \; \mid \; (s, s) \; \mid \; Λ(f) s\]

CAM (Call-by-value)

Abstract machine: constituted of judgements of the form

\[⟨f \; \mid \; s⟩ ⟶ ⟨f_1 \; \mid \; s_1⟩\]

Code (relooking of categorical combinators):

\[C \; ≝ \; skip \; \mid \; C;C' \; \mid \; PUSH \; \mid \; SWAP \; \mid \; CONS \; \mid \; CAR \; \mid \; CDR \; \mid \; Λ(C) \; \mid \; ev\] \[⟨ \text{code} \; \mid \; \underbrace{s}_{\text{environment}} \; \mid \; \text{ stack} ⟩\]

Rules of the CAM:

\[⟨SKIP; C \; \mid \; s \; \mid \; S⟩ ⟶ ⟨C \; \mid \; s \; \mid \; S⟩\\ ⟨CAR; C \; \mid \; (s_1, s_2) \; \mid \; S⟩ ⟶ ⟨C \; \mid \; s_1 \; \mid \; S⟩\\ CDR; C \; \mid \; (s_1, s_2) \; \mid \; S⟩ ⟶ ⟨C \; \mid \; s_2 \; \mid \; S⟩\\\]

for

\[⟨id \; \mid \; s⟩ = s\\ \cfrac{⟨f \; \mid \; s⟩ = s_1 \qquad ⟨g \; \mid \; s_1⟩ = s_2}{⟨f; g \; \mid \; s⟩ = s_2}\\ \cfrac{}{⟨π \; \mid \; (s_1, s_2)⟩ = s_1}\\ \, \\ \cfrac{}{⟨π' \; \mid \; (s_1, s_2)⟩ = s_2}\\ \,\]

NB: the second rule is not translated, because it happens de facto in the CAM

Now:

\[⟨PUSH; C \; \mid \; s \; \mid \; S⟩ ⟶ ⟨C \; \mid \; s \; \mid \; s \cdot S⟩\\ ⟨SWAP; C \; \mid \; s_1 \; \mid \; s_2 \cdot S⟩ ⟶ ⟨C \; \mid \; s_2 \; \mid \; s_1 \cdot S⟩\\ ⟨CONS; C \; \mid \; s_2 \; \mid \; s_1 \cdot S⟩ ⟶ ⟨C \; \mid \; (s_1, s_2) \; \mid \; S⟩\\ \; \\ \; \\ ⟨Λ(C); C' \; \mid \; s \; \mid \; S⟩ ⟶ ⟨C' \; \mid \; Λ(C)s \; \mid \; S⟩\\ ⟨ev; C' \; \mid \; (Λ(C)s, t) \; \mid \; S⟩ ⟶ ⟨C; C' \; \mid \; (s,t) \; \mid \; S⟩\\\]

NB:

  • the stack is used only in the PUSH, SWAP and CONS rules
  • CBV:

    • because of \(\cfrac{⟨f \; \mid \; s⟩ = s_1 \qquad ⟨g \; \mid \; s⟩ = s_2}{⟨⟨f, g⟩ \; \mid \; s⟩ = (s_1, s_2)}\\ \, \\\)

    • eager valued: a value of type $ℕ × ℕ$ is something of the form $(3, 4)$ (eager), and not $((λx.x)3, 4)$ (lazy)

Types

Type everything.

Ex: Typing of the syntactic category $s$ (values)

Termination

Consider a closed term $M$:

\[M \leadsto DB(M) \leadsto f \leadsto C\]

Then

\[⟨C \; \mid \; () \; \mid \; ∅ ⟩ ⟶^\ast \text{ state of the form } ⟨∅ \; \mid \; s \; \mid \; ∅⟩ \not ⟶\]

Deterministic

This system is deterministic:

\[⟨f \; \mid \; s⟩ = s_1 ∧ ⟨f \; \mid \; s⟩ = s_2 ⟹ s_1 = s_2\]

⟶ it is a partial function

Termination amounts to saying that this function is actually total.


TODO:

  • correctly type $s$
  • prove that for correctly typed and matching $f, s$: there exists $s_1$ such that $⟨f, s⟩ = s_1$

Yves Lafont’s theorem

Free cartesian closed category:

\[f \; ≝ \; \underbrace{\underline{f}}_{\text{from the orginal category}} \; \mid \; id \; \mid \; f \circ f \\ \; \mid \; ⟨f, g⟩ \; \mid \; π \; \mid \; π ' \; \mid \; ! \\ \; \mid \; Λ(f) \; \mid \; ev\] \[A \; ≝ \; \underline{a} \; \mid \; 1 \; \mid \; A × A \; \mid \; A ⇒ A\]

and quotient by the relevant equations (seen earlier) +

\[\underline{g} \circ \underline{f} = \underline{g \circ f}\\ \underline{id} = id\]

Theorem: Let $𝒞$ be a category. Let $CCC(𝒞)$ be the free cartesian closed category over $𝒞$. Then the functor

\[\begin{cases} 𝒞 &⟶ CCC(𝒞) \\ a &⟼ \underline{a}\\ f:a → b &⟼ \underline{f}\\ \end{cases}\]

is fully faithful.


\[s \; ≝ \; \underline{f} \; \mid \; () \; \mid \; (s, s) \; \mid \; Λ(f) s\]

Values can also be typed:

\[⊢_{\underline{a}} \; s: B \text{ for } \underline{a} ⊢ s: B\]

where $\underline{a}$ is a base type.

Typing rules for values

\[\cfrac{f: 𝒞(a,b)}{⊢_{\underline{a}} \; f: \underline{b}}\] \[\cfrac{}{⊢_{\underline{a}} \; (): 1}\] \[\cfrac{⊢_{\underline{a}} \; s: B \qquad ⊢_{\underline{a}} \; t:C}{⊢_{\underline{a}} \; (s,t): B × C}\] \[\cfrac{C × B_1 ⊢ f: B_2 \qquad ⊢_{\underline{a}} \; s:C}{⊢_{\underline{a}} \; Λ(f)s: B_1 ⇒ B_2}\]

We will define a coercion $`$ from $s$ to $f$ s.t.

\[⟨f \; \mid \; s⟩ = s_1 \quad ⟹ \quad f \circ `s = `s_1 \text{ using the equations defining } CCC(𝒞)\]

Definition of $`$

\[`\underline{f} = \underline{f}\\ `() = !\\ `(s, t) = ⟨`s, `t⟩\\ `Λ(f)s = Λ(f) \circ `s\]

Now:

\[\cfrac{A ⊢ f:B \qquad ⊢_{\underline{a}} \; s:A}{⊢_{\underline{a}} \; ⟨f \; \mid \; s⟩:B}\]

Typing rules for categorical combinators

\[\cfrac{f: 𝒞(a,b)}{\underline{a} ⊢ f: \underline{b}}\]

Proposition: If $B ⊢ f: C$ and $⊢_{\underline{a}} \; s:B$, then

\[∃t; \; ⟨f \; \mid \; s⟩ = t\]

Disguised termination theorem:

Realization: $s \Vdash_{\underline{a}} B$

  1. \[B ⊢ f: C ∧ s \Vdash_{\underline{a}} B ⟹ ∃ t; \; ⟨f \; \mid \; s⟩ = t ∧ t \Vdash_{\underline{a}} C\]
  2. \[⊢_{\underline{a}} s: B \quad ⟹ \quad ⊨_{\underline{a}} s: B\]


sequel:

Reminders

An equational theory:

  • category: $(f \circ g) \circ h ⋯$
  • cartesian: $π \circ ⟨f, g⟩ = f ⋯$
  • closed: $ev \circ ⟨Λ(f), g⟩ = ⋯$
  • over a given category: $\underline {f \circ g} = \underline f \circ \underline g, \underline {id} = id$
\[A \; ≝ \; \underline A \; \mid \; 1 \; \mid \; A × A \; \mid \; A → A\\ φ \; ≝ \; \underbrace{\underline f}_{\text{over a category } ℂ} \; \underbrace{\mid \; id \; \mid \; f \circ f }_{\text{category}}\; \\ \underbrace{\mid \; ⟨f, g⟩ \; \mid \; π \; \mid \; π' \; \mid \; !}_{\text{cartesian}} \\ \; \underbrace{\mid \; ev \; \; \mid \; Λ(φ)}_{\text{closed}}\]
Formal system:
\[⟨ φ \; \mid \; s ⟩ = t\\ \text{Values: } \quad s \; ≝ \; \underbrace{\underline f}_{\text{part corresponding to } ℂ} \; \mid \; ⋯\]

Two properties about this formal system:

  1. There is a function $`$ from values to categorical combinators such that

    \[⟨ φ \; \mid \; s⟩ = t \quad ⟹ \quad ⊢ φ \circ `s = `t\]
  2. Termination of execution on the abstract machine: for all $φ, s$ compatible, there exists $t$ such that $⟨φ \; \mid \; s⟩ = t$

Theorem (Lafont): Le $ℂ$ be a category.

The inclusion functor

\[⊆ : \begin{cases} ℂ &⟶ CCC(ℂ) \\ a &⟼ \underline a\\ f: a → b &⟼ [\underline f]: \underline a → \underline b\\ \end{cases}\]

is fully faithful

Reminder:

  • Full: \(∀ a, b ∈ ℂ, f': Fa → Fb, ∃ f: a → b; Ff = f'\)

  • Faithful: \(∀ a, b ∈ ℂ, f,g: a → b, Ff = Fg ⟹ f=g\)

  • So here:

    • Full: for all $φ: \underline a ⟶ \underline b$, there exists $f ∈ ℂ(a, b)$ such that \([φ] = [\underline f]\)

    • Faithful: for all $f, g ∈ ℂ(a,b)$: \([\underline f] = [\underline g] ⟹ f = g\)

    denoted by: \(⊢ \underline f = \underline g ⟹ f=g\)

Proof:

Full: Let $φ: \underline a → \underline b$, pick an $s = \underline {id_a}$. By property 2, there exists $t$ such that

\[⟨φ \; \mid \; \underline {id_a}⟩ = t\]

By well-typedness, this $t$ must be of the form $\underline f$ (it’s a value corresponding to $ℂ$).

By property 1,

\[⊢ φ \circ \underline{id_a} = \underline f\\ \text{so } ⊢ φ \circ id_a = \underline f\\ \text{ hence } ⊢ φ = \underline f\\\]

Faithful:

Lemma (Subject Reduction): If $⊢ φ = ψ$ and $⟨φ \; \mid \; s⟩ = t$, then $⟨ψ \; \mid \; s⟩ = t$

Suppose that $⊢ \underline f = \underline g$ (provably equal), $s = \underline {id_a}$:

\[⟨\underline f \; \mid \; \underline{id_a}⟩ = \underline{f \circ id_a} = \underline f\]

By subject reduction:

\[\underline g = ⟨\underline g \; \mid \; id_a⟩ = \underline f\]

Hence

\[\underline f = \underline g\]

which means $f=g$

NB: Yoneda argument:

\[\begin{xy} \xymatrix{ ℂ \ar[r]^{ y } \ar[rd]_{⊆} & \hat ℂ \\ & CCC(ℂ) \ar[u] } \end{xy}\]

As $y$ is faithful, so is $⊆$! We need the “gluing” construction to show fullness though.

Leave a comment