module Cat.CartesianClosed.Lambda
Simply-typed lambda calculus🔗
{o ℓ} (C : Precategory o ℓ) (fp : has-products C) (term : Terminal C)
(cc : Cartesian-closed C fp term)
where
open Binary-products C fp hiding (unique₂)
open Cartesian-closed cc
open Cat.Reasoning C
The simply typed (STLC) is a very small typed programming language very strongly associated with Cartesian closed categories. In this module, we define the syntax for STLC with base types, and inhabitants of these, given by the objects and morphisms of an arbitrary CCC. This syntax can be used to reflect morphisms of a CCC, making the “structural” morphisms explicit. We then build a normalisation procedure, which can be used to effectively compare morphisms in the CCC.
The types of STLC are generated by function types a `⇒ b
and product types a `× b
, along with an inclusion of
objects from the target category to serve as “base types”, e.g. the
natural numbers. The contexts are simply lists of types.
data Ty : Type o where
_`×_ : Ty → Ty → Ty
_`⇒_ : Ty → Ty → Ty
_ : Ob → Ty
`
data Cx : Type o where
: Cx
∅ _,_ : Cx → Ty → Cx
private variable
: Cx
Γ Δ Θ : Ty τ σ
To use contexts, we introduce variables. A variable
gives an index in
where something of type
can be found. It can either be right here, in which case we stop
, or it can be further back in the
context, and we must pop
the top
variable off to get to it.
data Var : Cx → Ty → Type o where
: Var (Γ , τ) τ
stop : Var Γ τ → Var (Γ , σ) τ pop
_⟧ᵗ : Ty → Ob
⟦_⟧ᶜ : Cx → Ob
⟦data Expr (Γ : Cx) : Ty → Typeω
We can now write down the type of expressions, or, really, of
typing judgements. An inhabitant of Expr Γ τ
is a
tree representing a complete derivation of something of type
τ
. We insist on the name expression rather than
term since there are more expressions than there are terms: For
example, in the context
the expressions
and
denote the same term.
data Expr Γ where
: Var Γ τ → Expr Γ τ
`var : Expr Γ (τ `× σ) → Expr Γ τ
`π₁ : Expr Γ (τ `× σ) → Expr Γ σ
`π₂ _,_⟩ : Expr Γ τ → Expr Γ σ → Expr Γ (τ `× σ)
`⟨: Expr (Γ , τ) σ → Expr Γ (τ `⇒ σ)
`λ : Expr Γ (τ `⇒ σ) → Expr Γ τ → Expr Γ σ
`$ _ : Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ → Expr Γ τ `
Using the Cartesian closed structure, we can interpret types, contexts, variables and expressions in terms of the structural morphisms: For example, the empty context is interpreted by the terminal object, and1 the zeroth variable is given by the second projection map
= ⟦ X ⟧ᵗ ⊗₀ ⟦ Y ⟧ᵗ
⟦ X `× Y ⟧ᵗ = Exp.B^A ⟦ X ⟧ᵗ ⟦ Y ⟧ᵗ
⟦ X `⇒ Y ⟧ᵗ = X
⟦ ` X ⟧ᵗ
= ⟦ Γ ⟧ᶜ ⊗₀ ⟦ τ ⟧ᵗ
⟦ Γ , τ ⟧ᶜ = Terminal.top term
⟦ ∅ ⟧ᶜ
_⟧ⁿ : Var Γ τ → Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ
⟦= π₂
⟦ stop ⟧ⁿ = ⟦ x ⟧ⁿ ∘ π₁
⟦ pop x ⟧ⁿ
_⟧ᵉ : Expr Γ τ → Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ
⟦= ⟦ x ⟧ⁿ
⟦ `var x ⟧ᵉ = π₁ ∘ ⟦ p ⟧ᵉ
⟦ `π₁ p ⟧ᵉ = π₂ ∘ ⟦ p ⟧ᵉ
⟦ `π₂ p ⟧ᵉ = ⟨ ⟦ a ⟧ᵉ , ⟦ b ⟧ᵉ ⟩
⟦ `⟨ a , b ⟩ ⟧ᵉ = ƛ ⟦ e ⟧ᵉ
⟦ `λ e ⟧ᵉ = ev ∘ ⟨ ⟦ f ⟧ᵉ , ⟦ x ⟧ᵉ ⟩
⟦ `$ f x ⟧ᵉ = x ⟦ ` x ⟧ᵉ
The type of expressions could (and, to some extent, should)
be higher-inductive-recursive, with path constructors expressing the
rules — the universal properties that we want to capture. However, this
would significantly complicate the development of the rest of this
module. We choose to work with raw derivations, instead, for
convenience. Equality of Expr
being
coarser than that of Hom
does not significantly affect our
application, which is metaprogramming.
Context inclusions🔗
We will implement our semantics using normalisation by evaluation, by embedding our expressions into a domain where the judgemental equalities of the object-level are also judgemental at the meta-level. We choose presheaves on a category of inclusions, where the objects are contexts and the maps indicate that, starting from you can get to by dropping some of the variables, and keeping everything else in-order.
data Ren : Cx → Cx → Type (o ⊔ ℓ) where
: Ren Γ Γ
stop : Ren Γ Δ → Ren (Γ , τ) Δ
drop : Ren Γ Δ → Ren (Γ , τ) (Δ , τ) keep
Here we must confess to another crime: Since our (types, hence our)
contexts include objects of the base category, they do not form a set:
therefore, neither does the type Ren
of context inclusions. This means
that we can not use Ren
as the
morphisms in a (pre)category. This could be remedied by instead working
relative to a given set of base types, which are equipped with a map
into semantic objects. This does not significantly alter the
development, but it would be an additional inconvenience.
Regardless, we can define composition of context inclusions by recursion, and, if necessary, we could prove that this is associative and unital. However, since we are not building an actual category of presheaves (only gesturing towards one), we omit these proofs.
_∘ʳ_ : ∀ {Γ Δ Θ} → Ren Γ Δ → Ren Δ Θ → Ren Γ Θ
= ρ
stop ∘ʳ ρ = drop (σ ∘ʳ ρ)
drop σ ∘ʳ ρ = keep σ
keep σ ∘ʳ stop = drop (σ ∘ʳ ρ)
keep σ ∘ʳ drop ρ = keep (σ ∘ʳ ρ) keep σ ∘ʳ keep ρ
If we fix a type then the family which sends a context to the variables in that context is actually a presheaf. Put less pretentiously, renamings act on variables:
: ∀ {Γ Δ τ} → Ren Γ Δ → Var Δ τ → Var Γ τ
ren-var = v
ren-var stop v (drop σ) v = pop (ren-var σ v)
ren-var (keep σ) stop = stop
ren-var (keep σ) (pop v) = pop (ren-var σ v) ren-var
Finally, we can define an interpretation of renamings into our model CCC. Note that this interpretation lands in monomorphisms.
_⟧ʳ : Ren Γ Δ → Hom ⟦ Γ ⟧ᶜ ⟦ Δ ⟧ᶜ
⟦= id
⟦ stop ⟧ʳ = ⟦ r ⟧ʳ ∘ π₁
⟦ drop r ⟧ʳ = ⟦ r ⟧ʳ ⊗₁ id ⟦ keep r ⟧ʳ
Normals and neutrals🔗
To define evaluation, we need a type of normal forms: In our setting, these include lambda abstractions and pairs, along with morphisms of the base category. However, we are not working with evaluation, rather with normalisation, where reduction takes place in arbitrary contexts. Therefore, there are expressions that we can not give a value to, but for which no further normalisation can happen: for example, applying a variable. Therefore, we define mutually inductive types of normal forms and neutral forms.
data Nf : Cx → Ty → Type (o ⊔ ℓ)
data Ne : Cx → Ty → Type (o ⊔ ℓ)
A normal form is indeed one for which no more reduction is possible: lambda expressions and pairs. A neutral form is also normal. These come primarily from non-empty contexts. Neutral forms are, essentially, variables together with a stack of pending eliminations (applications or projections that will be reduced in the future). However, in our setting, we also consider the base terms as neutral at base types.
data Nf where
: Nf (Γ , τ) σ → Nf Γ (τ `⇒ σ)
lam : Nf Γ τ → Nf Γ σ → Nf Γ (τ `× σ)
pair : ∀ {x} → Ne Γ (` x) → Nf Γ (` x)
ne
data Ne where
: Var Γ τ → Ne Γ τ
var : Ne Γ (τ `⇒ σ) → Nf Γ τ → Ne Γ σ
app : Ne Γ (τ `× σ) → Ne Γ τ
fstₙ : Ne Γ (τ `× σ) → Ne Γ σ
sndₙ : ∀ {o} → Hom ⟦ Γ ⟧ᶜ o → Ne Γ (` o) hom
By a fairly obvious recursion, renamings act on neutrals and normals, thus making these, too, into presheaves.
: ∀ {Γ Δ τ} → Ren Δ Γ → Ne Γ τ → Ne Δ τ
ren-ne : ∀ {Γ Δ τ} → Ren Δ Γ → Nf Γ τ → Nf Δ τ ren-nf
This is the only case that requires attention: to rename a morphism
of the base category, we must reify the renaming into its denotation,
and compose with the morphism. The alternative here would be to keep a
stack of pending renamings at each hom
, which could then be optimised before
composing at the end.
(hom h) = hom (h ∘ ⟦ σ ⟧ʳ) ren-ne σ
(var v) = var (ren-var σ v)
ren-ne σ (app f a) = app (ren-ne σ f) (ren-nf σ a)
ren-ne σ (fstₙ a) = fstₙ (ren-ne σ a)
ren-ne σ (sndₙ a) = sndₙ (ren-ne σ a)
ren-ne σ
(lam n) = lam (ren-nf (keep σ) n)
ren-nf σ (pair a b) = pair (ren-nf σ a) (ren-nf σ b)
ren-nf σ (ne x) = ne (ren-ne σ x) ren-nf σ
Normals and neutrals also have a straightforward denotation given by the Cartesian closed structure.
_⟧ₙ : Nf Γ τ → Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ
⟦_⟧ₛ : Ne Γ τ → Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ
⟦
= ƛ ⟦ h ⟧ₙ
⟦ lam h ⟧ₙ = ⟨ ⟦ a ⟧ₙ , ⟦ b ⟧ₙ ⟩
⟦ pair a b ⟧ₙ = ⟦ x ⟧ₛ
⟦ ne x ⟧ₙ
= ⟦ x ⟧ⁿ
⟦ var x ⟧ₛ = ev ∘ ⟨ ⟦ f ⟧ₛ , ⟦ x ⟧ₙ ⟩
⟦ app f x ⟧ₛ = π₁ ∘ ⟦ h ⟧ₛ
⟦ fstₙ h ⟧ₛ = π₂ ∘ ⟦ h ⟧ₛ
⟦ sndₙ h ⟧ₛ = h ⟦ hom h ⟧ₛ
We also have to prove a few hateful lemmas about how renamings, and its action on variables, neutrals and normals, interact with the denotation brackets. These lemmas essentially say that so that it doesn’t matter whether we first pass to the semantics in or apply a renaming.
: (ρ : Ren Γ Δ) (σ : Ren Δ Θ) → ⟦ ρ ∘ʳ σ ⟧ʳ ≡ ⟦ σ ⟧ʳ ∘ ⟦ ρ ⟧ʳ
⟦⟧-∘ʳ
: (ρ : Ren Δ Γ) (v : Var Γ τ) → ⟦ ren-var ρ v ⟧ⁿ ≡ ⟦ v ⟧ⁿ ∘ ⟦ ρ ⟧ʳ
ren-⟦⟧ⁿ : (ρ : Ren Δ Γ) (t : Ne Γ τ) → ⟦ ren-ne ρ t ⟧ₛ ≡ ⟦ t ⟧ₛ ∘ ⟦ ρ ⟧ʳ
ren-⟦⟧ₛ : (ρ : Ren Δ Γ) (t : Nf Γ τ) → ⟦ ren-nf ρ t ⟧ₙ ≡ ⟦ t ⟧ₙ ∘ ⟦ ρ ⟧ʳ ren-⟦⟧ₙ
If you want, you can choose to read the proofs of these substitution
correctness lemmas by clicking on this <details>
tag.
= intror refl
⟦⟧-∘ʳ stop σ (drop ρ) σ = pushl (⟦⟧-∘ʳ ρ σ)
⟦⟧-∘ʳ (keep ρ) stop = introl refl
⟦⟧-∘ʳ (keep ρ) (drop σ) = pushl (⟦⟧-∘ʳ ρ σ) ∙ sym (pullr π₁∘⟨⟩)
⟦⟧-∘ʳ (keep ρ) (keep σ) = sym $ Product.unique (fp _ _)
⟦⟧-∘ʳ (pulll π₁∘⟨⟩ ∙ pullr π₁∘⟨⟩ ∙ pulll (sym (⟦⟧-∘ʳ ρ σ)))
(pulll π₂∘⟨⟩ ∙ pullr π₂∘⟨⟩ ∙ idl _)
= intror refl
ren-⟦⟧ⁿ stop v (drop ρ) v = pushl (ren-⟦⟧ⁿ ρ v)
ren-⟦⟧ⁿ (keep ρ) stop = sym (π₂∘⟨⟩ ∙ idl _)
ren-⟦⟧ⁿ (keep ρ) (pop v) = pushl (ren-⟦⟧ⁿ ρ v) ∙ sym (pullr π₁∘⟨⟩)
ren-⟦⟧ⁿ
(var x) = ren-⟦⟧ⁿ ρ x
ren-⟦⟧ₛ ρ (app f x) = ap₂ _∘_ refl
ren-⟦⟧ₛ ρ (ap₂ ⟨_,_⟩ (ren-⟦⟧ₛ ρ f) (ren-⟦⟧ₙ ρ x) ∙ sym (⟨⟩∘ _))
∙ pulll refl(fstₙ t) = pushr (ren-⟦⟧ₛ ρ t)
ren-⟦⟧ₛ ρ (sndₙ t) = pushr (ren-⟦⟧ₛ ρ t)
ren-⟦⟧ₛ ρ (hom x) = refl
ren-⟦⟧ₛ ρ
(lam t) =
ren-⟦⟧ₙ ρ (ren-⟦⟧ₙ (keep ρ) t)
ap ƛ (unique _ (ap₂ _∘_ refl rem₁ ∙ pulll (commutes ⟦ t ⟧ₙ)))
∙ sym where
: (⟦ lam t ⟧ₙ ∘ ⟦ ρ ⟧ʳ) ⊗₁ id ≡ (⟦ lam t ⟧ₙ ⊗₁ id) ∘ ⟦ ρ ⟧ʳ ⊗₁ id
rem₁ = Bifunctor.first∘first ×-functor
rem₁
(pair a b) = ap₂ ⟨_,_⟩ (ren-⟦⟧ₙ ρ a) (ren-⟦⟧ₙ ρ b) ∙ sym (⟨⟩∘ _)
ren-⟦⟧ₙ ρ (ne x) = ren-⟦⟧ₛ ρ x ren-⟦⟧ₙ ρ
Normalization🔗
We would like to write a map of type Expr Γ τ → Nf Γ τ
.
However, by design, this is not straightforward: the type of
normal forms is…
2 However, note that, since both
Nf
and Ne
are “presheaves” on the “category of
renamings”, we can use the Cartesian closed structure of
presheaves to interpret the lambda calculus. The idea here is that
presheaves, being functors into
have a computational structure on the relevant connectives that closely
matches that of
itself: for example, composing the first projection with a pairing, in
the category of presheaves, is (componentwise) definitionally equal to
the first component of the pair. It’s a boosted up version of exactly
the same idea used for the category
solver.
Then, as long as we can reify these presheaves back to normal forms, we have a normalisation procedure! Expressions are interpreted as sections of the relevant presheaves, then reified into normal forms. And, to be clear, we only need to reflect the presheaves that actually represent types: these can be built by recursion (on the type!) so that they are very easy to reify.
However, that still leaves the question of correctness for
the NbE algorithm. Given an expression
we will have two wildly different approaches to interpreting
as a morphism
There’s the naïve denotation ⟦_⟧ᵉ
, which
interprets an expression directly using the relevant connections; But
now we can also interpret an expression
into a section
then reify that to a normal form
and take the denotation
Normalisation should compute a
form, and
is validated by CCCs, so
and
should be the same. How do we ensure this?
It would be totally possible to do this in two passes - first define
the algorithm, then prove it correct. But the proof of correctness would
mirror the structure of the algorithm almost 1:1! Can we define the
algorithm in a way that is forced into correctness? It turns
out that we can! The idea here is an adaptation of the
gluing method in the semantics of type theory. Rather
than have a mere presheaf
to represent the semantic values in
our full construction Tyᵖ
has
three arguments: The type
the context
(over which it is functorial), and a denotation
— and in prose, we’ll write Tyᵖ
as
we say that
tracks
when
Since all our operations on semantic values will be written against a type indexed by their denotations, the core of the algorithm will have to be written in a way that evidently preserves denotations — the type checker is doing most of the work. Our only actual correctness theorem boils down to showing that, given we have in
To summarize all the parts, we have:
Expressions — the user writes these, and they may have redices.
Denotations Since a CCC has “structural” morphisms corresponding to each kind of expression, we can directly read off a morphism from an expression.
Neutrals and normals A neutral is something that wants to reduce but can’t (e.g. a projection applied to a variable), and a normal is something that definitely won’t reduce anymore (e.g. a lambda expression). Normals and neutrals also have denotations, so we may also write when or
Semantic values, The presheaf is computed by recursion on so that its sections have a good computational representation. As mentioned above, it’s indexed by a denotation forcing the core of the algorithm to preserve denotations.
The reification and reflection transformations which turns a semantic value into a normal form, and which witnesses that the semantic values include the neutrals.
Our main objective is a normalisation function that maps expressions to semantic values Let’s get started.
While we have been talking about presheaves above, the actual code does not actually set up a presheaf category to work in. The reason for this is parsimony. Referring to presheaves for the motivation, but working with simpler type families, lends itself better to formalisation; many of the details, e.g. functoriality of are not used, and would simply be noise.
Semantic values🔗
: (τ : Ty) (Γ : Cx) → Hom ⟦ Γ ⟧ᶜ ⟦ τ ⟧ᵗ → Type (o ⊔ ℓ) Tyᵖ
We define by recursion on and its definition is mostly unsurprising: at each case, we use the Cartesian closed structure of presheaf categories to interpret the given type into a presheaf that has the right universal property, but is “definitionally nicer”. Let’s go through it case-by-case. When faced with a product type in the object language, we can simply return a meta-language product type. However, we must also preserve the tracking information: if a section of a product type is to track what should each component track? Well, we know that by the uniqueness property of Cartesian products. So the first component should track and the second
(τ `× σ) Γ h = Tyᵖ τ Γ (π₁ ∘ h) × Tyᵖ σ Γ (π₂ ∘ h) Tyᵖ
For a function type, we once again appeal to the construction in presheaves. The components of the exponential are defined to be the natural transformations which, at the component, are given by maps A function value must preserve tracking information: A function which tracks if given an argument tracking it must return a value which tracks the application of to relative to the renaming In a CCC, that’s given by
which is precisely what we require.
(τ `⇒ σ) Γ h =
Tyᵖ ∀ {Δ} (ρ : Ren Δ Γ) {a}
→ Tyᵖ τ Δ a
→ Tyᵖ σ Δ (ev ∘ ⟨ h ∘ ⟦ ρ ⟧ʳ , a ⟩)
Finally, we have the case of base types, for which the corresponding presheaf is that of neutral forms. Here, we can finally discharge the tracking information: a neutral tracks iff.
(` x) Γ h = Σ (Ne Γ (` x)) λ n → ⟦ n ⟧ₛ ≡ h Tyᵖ
To work on open contexts, we can define (now by induction), the presheaf of parallel substitutions, which are decorated sequences of terms. These also have a morphism of attached, but keep in mind that a parallel substitution interprets as an element of hence that is what they are indexed over.
data Subᵖ : ∀ Γ Δ → Hom ⟦ Δ ⟧ᶜ ⟦ Γ ⟧ᶜ → Type (o ⊔ ℓ) where
: ∀ {i} → Subᵖ ∅ Δ i
∅ _,_ : ∀ {h} → Subᵖ Γ Δ (π₁ ∘ h) → Tyᵖ σ Δ (π₂ ∘ h) → Subᵖ (Γ , σ) Δ h
_⟩ : ∀ {τ Γ h h'} → h ≡ h' → Tyᵖ τ Γ h → Tyᵖ τ Γ h'
tyᵖ⟨_⟩ {τ `× σ} p (a , b) = tyᵖ⟨ ap (π₁ ∘_) p ⟩ a , tyᵖ⟨ ap (π₂ ∘_) p ⟩ b
tyᵖ⟨_⟩ {τ `⇒ σ} p ν ρ x = tyᵖ⟨ ap (λ e → ev ∘ ⟨ e ∘ ⟦ ρ ⟧ʳ , _ ⟩) p ⟩ (ν ρ x)
tyᵖ⟨_⟩ {` x} p (n , q) .fst = n
tyᵖ⟨_⟩ {` x} p (n , q) .snd = q ∙ p
tyᵖ⟨
_⟩ : ∀ {Γ Δ h h'} → h ≡ h' → Subᵖ Γ Δ h → Subᵖ Γ Δ h'
subᵖ⟨_⟩ p ∅ = ∅
subᵖ⟨_⟩ p (r , x) = subᵖ⟨ ap (π₁ ∘_) p ⟩ r , tyᵖ⟨ ap (π₂ ∘_) p ⟩ x subᵖ⟨
As always, we must show that these have an action by renamings. Renamings act on the semantic component, too: if tracks then tracks
: ∀ {Δ Γ τ m} (ρ : Ren Δ Γ) → Tyᵖ τ Γ m → Tyᵖ τ Δ (m ∘ ⟦ ρ ⟧ʳ)
ren-tyᵖ : ∀ {Δ Γ Θ m} (ρ : Ren Θ Δ) → Subᵖ Γ Δ m → Subᵖ Γ Θ (m ∘ ⟦ ρ ⟧ʳ) ren-subᵖ
{τ = τ `× σ} r (a , b) =
ren-tyᵖ (assoc _ _ _) ⟩ (ren-tyᵖ r a)
tyᵖ⟨ sym (assoc _ _ _) ⟩ (ren-tyᵖ r b)
, tyᵖ⟨ sym {τ = τ `⇒ σ} r t {Θ} ρ {α} a =
ren-tyᵖ (λ e → ev ∘ ⟨ e , α ⟩) (pushr (⟦⟧-∘ʳ ρ r)) ⟩ (t (ρ ∘ʳ r) a)
tyᵖ⟨ ap {τ = ` x} r (f , p) = ren-ne r f , ren-⟦⟧ₛ r f ∙ ap₂ _∘_ p refl
ren-tyᵖ
= ∅
ren-subᵖ r ∅ (c , x) =
ren-subᵖ r (assoc _ _ _) ⟩ (ren-subᵖ r c)
subᵖ⟨ sym (assoc _ _ _) ⟩ (ren-tyᵖ r x) , tyᵖ⟨ sym
Reification and reflection🔗
We can now define the reification and reflection functions. Reflection embeds a neutral form into semantic values; Reification turns semantic values into normal forms. Since we have defined the semantic values by recursion, we can exploit the good computational behaviour of Agda types in our reification/reflection functions: for example, when reifying at a product type, we know that we have an honest-to-god product of semantic values at hand.
: ∀ {h} → Tyᵖ τ Γ h → Nf Γ τ
reifyᵖ : (n : Ne Γ τ) → Tyᵖ τ Γ ⟦ n ⟧ₛ
reflectᵖ : ∀ {h} (v : Tyᵖ τ Γ h) → ⟦ reifyᵖ v ⟧ₙ ≡ h
reifyᵖ-correct
{τ = τ `× s} (a , b) = pair (reifyᵖ a) (reifyᵖ b)
reifyᵖ {τ = τ `⇒ s} f = lam (reifyᵖ (f (drop stop) (reflectᵖ (var stop))))
reifyᵖ {τ = ` x} d = ne (d .fst)
reifyᵖ
{τ = τ `× σ} n = reflectᵖ (fstₙ n) , reflectᵖ (sndₙ n)
reflectᵖ {τ = τ `⇒ σ} n ρ a = tyᵖ⟨ ap₂ (λ e f → ev ∘ ⟨ e , f ⟩) (ren-⟦⟧ₛ ρ n) (reifyᵖ-correct a) ⟩
reflectᵖ (reflectᵖ (app (ren-ne ρ n) (reifyᵖ a)))
{τ = ` x} n = n , refl reflectᵖ
The interesting cases deal with function types: To reify a lambda —
which is semantically represented by a function that operates
on (a renaming and) the actual argument — we produce a lam
constructor, but we must then somehow
reify “all possible bodies”.
However, since the semantic value of a function takes arguments and returns results in an arbitrary extension of the given context, we can introduce a new variable — thus a new neutral — and apply the body to that. Evaluation keeps going, but anything that actually depends on the body will be blocked on this new neutral!
Conversely, reflection starts from a neutral, and must build a semantic value that captures all the pending applications. At the case of a lambda, we have a neutral a renaming and an argument We can thus build the stuck application
This is also where we encounter the only correctness lemma that is
not forced on us by the types involved, since the type of normal forms
is not indexed by a denotation in
We must write an external function reifyᵖ-correct
, actually establishing
that
when
tracks
{τ = τ `× σ} (a , b) = sym $
reifyᵖ-correct .unique (fp _ _) (sym (reifyᵖ-correct a)) (sym (reifyᵖ-correct b))
Product{τ = τ `⇒ σ} {h = h} ν =
reifyᵖ-correct let
: ⟦ reifyᵖ (ν (drop stop) (reflectᵖ (var stop))) ⟧ₙ ≡ ev ∘ ⟨ h ∘ id ∘ π₁ , π₂ ⟩
p = reifyᵖ-correct (ν (drop stop) (reflectᵖ (var stop)))
p in ap ƛ p
(unique _ (ap₂ _∘_ refl (ap₂ ⟨_,_⟩ (sym (pulll (elimr refl))) (eliml refl))))
∙ sym {τ = ` x} d = d .snd reifyᵖ-correct
_⟧ˢ : ∀ {Γ Δ h} → Subᵖ Γ Δ h → Hom ⟦ Δ ⟧ᶜ ⟦ Γ ⟧ᶜ
⟦_⟧ˢ ∅ = Terminal.! term
⟦_⟧ˢ (c , x) = ⟨ ⟦ c ⟧ˢ , ⟦ reifyᵖ x ⟧ₙ ⟩
⟦
: ∀ {Γ Δ h} (ρ : Subᵖ Γ Δ h) → ⟦ ρ ⟧ˢ ≡ h
⟦⟧ˢ-correct = Terminal.!-unique term _
⟦⟧ˢ-correct ∅ (ρ , x) = sym (Product.unique (fp _ _) (sym (⟦⟧ˢ-correct ρ)) (sym (reifyᵖ-correct x))) ⟦⟧ˢ-correct
Interpreting expressions🔗
With our semantic values carefully chosen to allow reflection and
reification, we can set out to build an Expr
-algebra. To work in open contexts,
an expression
will be interpreted as a natural transformation from parallel
substitutions to types: the parallel substitution acts as our
“environment”, so is used in the interpretation of variables:
: ∀ {ρ} (π : Var Δ τ) → Subᵖ Δ Γ ρ → Tyᵖ τ Γ (⟦ π ⟧ⁿ ∘ ρ)
varᵖ (_ , x) = x
varᵖ stop (pop v) (c , _) = tyᵖ⟨ assoc _ _ _ ⟩ (varᵖ v c) varᵖ
We must interpret morphisms from the model category in a type-directed way, and eta-expand as we go. That’s because we made the decision to only have morphisms as neutrals at base type. Therefore, if a morphism from the base category lands in (e.g.) products, we must interpret it as the semantic product of its projections:
: ∀ {h'} (h : Hom ⟦ Δ ⟧ᶜ ⟦ τ ⟧ᵗ) → Subᵖ Δ Γ h' → Tyᵖ τ Γ (h ∘ h')
baseᵖ
{τ = τ `× τ₁} x c =
baseᵖ (assoc _ _ _) ⟩ (baseᵖ (π₁ ∘ x) c)
tyᵖ⟨ sym (assoc _ _ _) ⟩ (baseᵖ (π₂ ∘ x) c)
, tyᵖ⟨ sym
{τ = τ `⇒ σ} {h' = h'} h c ρ {α} a = tyᵖ⟨ pullr (Product.unique (fp _ _) (pulll π₁∘⟨⟩ ∙ extendr π₁∘⟨⟩) (pulll π₂∘⟨⟩ ∙ π₂∘⟨⟩)) ⟩
baseᵖ (baseᵖ (ev ∘ ⟨ h ∘ π₁ , π₂ ⟩) (
(ren-subᵖ ρ c), tyᵖ⟨ sym π₂∘⟨⟩ ⟩ a))
subᵖ⟨ sym π₁∘⟨⟩ ⟩
{τ = ` t} x c = hom (x ∘ ⟦ c ⟧ˢ) , ap (x ∘_) (⟦⟧ˢ-correct c) baseᵖ
Those are the hard bits, we can now interpret everything else by a simple recursion! Note that, when interpreting a lambda expression, we return a function which evaluates the body, and when eliminating it, we apply it to the value of its argument3.
: ∀ {h} (e : Expr Δ τ) (ρ : Subᵖ Δ Γ h) → Tyᵖ τ Γ (⟦ e ⟧ᵉ ∘ h)
exprᵖ (`var x) c = varᵖ x c
exprᵖ (`π₁ p) c = tyᵖ⟨ assoc _ _ _ ⟩ (exprᵖ p c .fst)
exprᵖ (`π₂ p) c = tyᵖ⟨ assoc _ _ _ ⟩ (exprᵖ p c .snd)
exprᵖ =
exprᵖ `⟨ a , b ⟩ c (pulll π₁∘⟨⟩) ⟩ (exprᵖ a c) , tyᵖ⟨ sym (pulll π₂∘⟨⟩) ⟩ (exprᵖ b c)
tyᵖ⟨ sym {h = h} (`$ f a) c = tyᵖ⟨ ap (ev ∘_) (ap₂ ⟨_,_⟩ (idr _) refl ∙ sym (⟨⟩∘ h)) ∙ assoc _ _ _ ⟩
exprᵖ (exprᵖ f c stop (exprᵖ a c))
(` x) c = baseᵖ x c
exprᵖ {h = h} (`λ f) ρ σ {m} a = tyᵖ⟨ fixup ⟩ (exprᵖ f
exprᵖ ( subᵖ⟨ sym π₁∘⟨⟩ ⟩ (ren-subᵖ σ ρ)
)) , tyᵖ⟨ sym π₂∘⟨⟩ ⟩ a
where abstract
: ⟦ f ⟧ᵉ ∘ ⟨ h ∘ ⟦ σ ⟧ʳ , m ⟩ ≡ ev ∘ ⟨ (⟦ `λ f ⟧ᵉ ∘ h) ∘ ⟦ σ ⟧ʳ , m ⟩
fixup = sym $
fixup (⟦ `λ f ⟧ᵉ ∘ h) ∘ ⟦ σ ⟧ʳ , m ⟩ ≡˘⟨ ap₂ _∘_ refl (Product.unique (fp _ _) (pulll π₁∘⟨⟩ ∙ extendr π₁∘⟨⟩) (pulll π₂∘⟨⟩ ·· pullr π₂∘⟨⟩ ·· eliml refl)) ⟩
ev ∘ ⟨ (is-exponential.commutes has-is-exp _) ⟩
ev ∘ ⟦ `λ f ⟧ᵉ ⊗₁ id ∘ ⟨ h ∘ ⟦ σ ⟧ʳ , m ⟩ ≡⟨ pulll ⟦ f ⟧ᵉ ∘ ⟨ h ∘ ⟦ σ ⟧ʳ , m ⟩ ∎
If we apply the semantic value of an expression to the “identity parallel substitution”, a context where all the variables are given a neutral value, we get a normal form!
: ∀ Γ → Subᵖ Γ Γ id
id-subᵖ = ∅
id-subᵖ ∅ (Γ , x) =
id-subᵖ _ ·· idl _ ·· sym (idr _) ⟩ (ren-subᵖ (drop stop) (id-subᵖ Γ))
subᵖ⟨ idl (idr _) ⟩ (reflectᵖ (var stop))
, tyᵖ⟨ sym
: Expr Γ τ → Nf Γ τ
nf = reifyᵖ (exprᵖ x (id-subᵖ _)) nf x
Moreover, we can appeal to our key correctness theorem to conclude the correctness of the entire normalisation procedure. That’s the only explicit theorem we proved, with the rest of the pieces being threaded through the definitions of the various transformations.
opaque: (e : Expr Γ τ) → ⟦ nf e ⟧ₙ ≡ ⟦ e ⟧ᵉ
nf-sound {Γ = Γ} e = reifyᵖ-correct (exprᵖ e (id-subᵖ Γ)) ∙ elimr refl nf-sound
As a demonstration, we can normalise the expression
which lets Agda reduce it away to be simply the variable (which is the second in the context). Moreover, by appeal to the correctness theorem, we can prove that the complicated morphism that denotes is equal to the much simpler
module _ {a b : Ob} where private
: Expr ((∅ , ` a) , ` b) _
e1 = `π₁ `⟨ `$ (`λ (`var stop)) (`var (pop stop)) , `var stop ⟩
e1
_ : nf e1 ≡ ne (var (pop stop))
_ = refl
_ : π₂ ∘ π₁ ≡ π₁ ∘ ⟨ ev ∘ ⟨ ƛ π₂ , π₂ ∘ π₁ ⟩ , π₂ ⟩
_ = nf-sound e1
-- An attempt to normalise this proof produced 4 MiB of garbage, and
-- ran for over an hour before our patience ran out.
: (e e' : Expr Γ τ) → nf e ≡ nf e' → ⟦ e ⟧ᵉ ≡ ⟦ e' ⟧ᵉ
solve = sym (nf-sound e) ·· ap ⟦_⟧ₙ prf ·· nf-sound e' solve e e' prf
An application🔗
The normalisation algorithm serves to decide the semantic equality of expressions in the simply-typed lambda calculus, but I’ll freely admit that’s not a task that comes up every day. We can also use this algorithm to prove things about the simply-typed lambda calculus! As an example, we have canonicity: every term in a base type denotes an actual element of the base type. In our categorical setting, that means that, given where is one of the non-pair, non-function types we have included from the category then there is a global element which denotes.
: ∀ {a} → (e : Expr ∅ (` a)) → Σ (Hom (Terminal.top term) a) λ h → ⟦ e ⟧ᵉ ≡ h
canonicity {a = a} e = go (nf e) (nf-sound e) where
canonicity : ∀ {a b} → Ne ∅ (a `⇒ b) → ⊥
no-functions : ∀ {a b} → Ne ∅ (a `× b) → ⊥
no-pairs
(app f _) = no-functions f
no-functions (fstₙ x) = no-pairs x
no-functions (sndₙ x) = no-pairs x
no-functions
(app f _) = no-functions f
no-pairs (fstₙ x) = no-pairs x
no-pairs (sndₙ x) = no-pairs x
no-pairs
: (nf : Nf ∅ (` a)) → ⟦ nf ⟧ₙ ≡ ⟦ e ⟧ᵉ → Σ (Hom (Terminal.top term) a) λ h → ⟦ e ⟧ᵉ ≡ h
go (ne (app f _)) p = absurd (no-functions f)
go (ne (fstₙ x)) p = absurd (no-pairs x)
go (ne (sndₙ x)) p = absurd (no-pairs x)
go (ne (hom x)) p = x , sym p go