module Cat.CartesianClosed.Lambda

Simply-typed lambda calculusπŸ”—

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

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
  stop : Var (Ξ“ , Ο„) Ο„
  pop  : Var Ξ“ Ο„ β†’ Var (Ξ“ , Οƒ) Ο„

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    : Var Ξ“ Ο„             β†’ Expr Ξ“ Ο„
  `π₁     : 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 βŸ§α΅—
⟦ X `β‡’ Y βŸ§α΅— = Exp.B^A ⟦ X βŸ§α΅— ⟦ Y βŸ§α΅—
⟦ ` X βŸ§α΅—    = X

⟦ Ξ“ , Ο„ ⟧ᢜ = ⟦ Ξ“ ⟧ᢜ βŠ—β‚€ ⟦ Ο„ βŸ§α΅—
⟦ βˆ… ⟧ᢜ     = Terminal.top term

⟦_⟧ⁿ : Var Ξ“ Ο„ β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ο„ βŸ§α΅—
⟦ stop ⟧ⁿ  = Ο€β‚‚
⟦ pop x ⟧ⁿ = ⟦ x ⟧ⁿ ∘ π₁

⟦_βŸ§α΅‰ : Expr Ξ“ Ο„ β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ο„ βŸ§α΅—
⟦ `var x     βŸ§α΅‰ = ⟦ x ⟧ⁿ
⟦ `π₁ p      βŸ§α΅‰ = π₁ ∘ ⟦ p βŸ§α΅‰
⟦ `Ο€β‚‚ p      βŸ§α΅‰ = Ο€β‚‚ ∘ ⟦ p βŸ§α΅‰
⟦ `⟨ a , b ⟩ βŸ§α΅‰ = ⟨ ⟦ a βŸ§α΅‰ , ⟦ b βŸ§α΅‰ ⟩
⟦ `Ξ» e       βŸ§α΅‰ = Ζ› ⟦ e βŸ§α΅‰
⟦ `$ f x     βŸ§α΅‰ = ev ∘ ⟨ ⟦ 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
  stop : Ren Ξ“ Ξ“
  drop : Ren Ξ“ Ξ” β†’ Ren (Ξ“ , Ο„) Ξ”
  keep : Ren Ξ“ Ξ” β†’ Ren (Ξ“ , Ο„) (Ξ” , Ο„)

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 Οƒ ∘ʳ stop   = keep Οƒ
keep Οƒ ∘ʳ drop ρ = 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 : βˆ€ {Ξ“ Ξ” Ο„} β†’ Ren Ξ“ Ξ” β†’ Var Ξ” Ο„ β†’ Var Ξ“ Ο„
ren-var stop     v       = v
ren-var (drop Οƒ) v       = pop (ren-var Οƒ v)
ren-var (keep Οƒ) stop    = stop
ren-var (keep Οƒ) (pop v) = pop (ren-var Οƒ v)

Finally, we can define an interpretation of renamings into our model CCC. Note that this interpretation lands in monomorphisms.

⟦_⟧ʳ : Ren Ξ“ Ξ” β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ξ” ⟧ᢜ
⟦ stop   ⟧ʳ = id
⟦ drop r ⟧ʳ = ⟦ r ⟧ʳ ∘ π₁
⟦ keep r ⟧ʳ = ⟦ r ⟧ʳ βŠ—β‚ id

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
  lam  : Nf (Ξ“ , Ο„) Οƒ    β†’ Nf Ξ“ (Ο„ `β‡’ Οƒ)
  pair : Nf Ξ“ Ο„ β†’ Nf Ξ“ Οƒ β†’ Nf Ξ“ (Ο„ `Γ— Οƒ)
  ne   : Ne Ξ“ Οƒ β†’ Nf Ξ“ Οƒ

data Ne where
  var  : Var Ξ“ Ο„ β†’ Ne Ξ“ Ο„
  app  : Ne Ξ“ (Ο„ `β‡’ Οƒ) β†’ Nf Ξ“ Ο„ β†’ Ne Ξ“ Οƒ
  fstβ‚™ : Ne Ξ“ (Ο„ `Γ— Οƒ) β†’ Ne Ξ“ Ο„
  sndβ‚™ : Ne Ξ“ (Ο„ `Γ— Οƒ) β†’ Ne Ξ“ Οƒ
  hom  : βˆ€ {o} β†’ Hom ⟦ Ξ“ ⟧ᢜ o β†’ Ne Ξ“ (` o)

By a fairly obvious recursion, renamings act on neutrals and normals, thus making these, too, into presheaves.

ren-ne : βˆ€ {Ξ“ Ξ” Ο„} β†’ Ren Ξ” Ξ“ β†’ Ne Ξ“ Ο„ β†’ Ne Ξ” Ο„
ren-nf : βˆ€ {Ξ“ Ξ” Ο„} β†’ Ren Ξ” Ξ“ β†’ Nf Ξ“ Ο„ β†’ 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.

ren-ne Οƒ (hom h)   = hom  (h ∘ ⟦ Οƒ ⟧ʳ)

Normals and neutrals also have a straightforward denotation given by the Cartesian closed structure.

⟦_βŸ§β‚™  : Nf Ξ“ Ο„ β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ο„ βŸ§α΅—
⟦_βŸ§β‚›  : Ne Ξ“ Ο„ β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ο„ βŸ§α΅—

⟦ lam h    βŸ§β‚™ = Ζ› ⟦ h βŸ§β‚™
⟦ pair a b βŸ§β‚™ = ⟨ ⟦ a βŸ§β‚™ , ⟦ b βŸ§β‚™ ⟩
⟦ ne x     βŸ§β‚™ = ⟦ x βŸ§β‚›

⟦ var x   βŸ§β‚› = ⟦ x ⟧ⁿ
⟦ app f x βŸ§β‚› = ev ∘ ⟨ ⟦ f βŸ§β‚› , ⟦ x βŸ§β‚™ ⟩
⟦ fstβ‚™ h  βŸ§β‚› = π₁ ∘ ⟦ h βŸ§β‚›
⟦ sndβ‚™ h  βŸ§β‚› = Ο€β‚‚ ∘ ⟦ h βŸ§β‚›
⟦ hom h   βŸ§β‚› = 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-⟦⟧ⁿ : (ρ : Ren Ξ” Ξ“) (v : Var Ξ“ Ο„) β†’ ⟦ ren-var ρ v ⟧ⁿ ≑ ⟦ v ⟧ⁿ ∘ ⟦ ρ ⟧ʳ
ren-βŸ¦βŸ§β‚› : (ρ : Ren Ξ” Ξ“) (t : Ne Ξ“ Ο„)  β†’ ⟦ ren-ne ρ t  βŸ§β‚› ≑ ⟦ t βŸ§β‚› ∘ ⟦ ρ ⟧ʳ
ren-βŸ¦βŸ§β‚™ : (ρ : Ren Ξ” Ξ“) (t : Nf Ξ“ Ο„)  β†’ ⟦ ren-nf ρ t  βŸ§β‚™ ≑ ⟦ t βŸ§β‚™ ∘ ⟦ ρ ⟧ʳ
If you want, you can choose to read the proofs of these substitution correctness lemmas by clicking on this <details> tag.
⟦⟧-∘ʳ stop Οƒ = intror refl
⟦⟧-∘ʳ (drop ρ) Οƒ = pushl (⟦⟧-∘ʳ ρ Οƒ)
⟦⟧-∘ʳ (keep ρ) stop = introl refl
⟦⟧-∘ʳ (keep ρ) (drop Οƒ) = pushl (⟦⟧-∘ʳ ρ Οƒ) βˆ™ sym (pullr Ο€β‚βˆ˜βŸ¨βŸ©)
⟦⟧-∘ʳ (keep ρ) (keep Οƒ) = sym $ Product.unique (fp _ _) _
  (pulll Ο€β‚βˆ˜βŸ¨βŸ© βˆ™ pullr Ο€β‚βˆ˜βŸ¨βŸ© βˆ™ pulll (sym (⟦⟧-∘ʳ ρ Οƒ)))
  (pulll Ο€β‚‚βˆ˜βŸ¨βŸ© βˆ™ pullr Ο€β‚‚βˆ˜βŸ¨βŸ© βˆ™ idl _)

ren-⟦⟧ⁿ stop v           = intror refl
ren-⟦⟧ⁿ (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
  (apβ‚‚ ⟨_,_⟩ (ren-βŸ¦βŸ§β‚› ρ f) (ren-βŸ¦βŸ§β‚™ ρ x) βˆ™ sym (⟨⟩∘ _))
  βˆ™ pulll refl
ren-βŸ¦βŸ§β‚› ρ (fstβ‚™ t) = pushr (ren-βŸ¦βŸ§β‚› ρ t)
ren-βŸ¦βŸ§β‚› ρ (sndβ‚™ t) = pushr (ren-βŸ¦βŸ§β‚› ρ t)
ren-βŸ¦βŸ§β‚› ρ (hom x) = refl

ren-βŸ¦βŸ§β‚™ ρ (lam t) =
    ap Ζ› (ren-βŸ¦βŸ§β‚™ (keep ρ) t)
  βˆ™ sym (unique _ (apβ‚‚ _∘_ refl rem₁ βˆ™ pulll (commutes ⟦ t βŸ§β‚™)))
  where
  rem₁ : (⟦ lam t βŸ§β‚™ ∘ ⟦ ρ ⟧ʳ) βŠ—β‚ id ≑ (⟦ lam t βŸ§β‚™ βŠ—β‚ id) ∘ ⟦ ρ ⟧ʳ βŠ—β‚ id
  rem₁ = Bifunctor.first∘first Γ—-functor

ren-βŸ¦βŸ§β‚™ ρ (pair a b) = apβ‚‚ ⟨_,_⟩ (ren-βŸ¦βŸ§β‚™ ρ a) (ren-βŸ¦βŸ§β‚™ ρ b) βˆ™ sym (⟨⟩∘ _)
ren-βŸ¦βŸ§β‚™ ρ (ne x) = ren-βŸ¦βŸ§β‚› ρ x

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.

\ Warning

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α΅– : (Ο„ : Ty) (Ξ“ : Cx) β†’ Hom ⟦ Ξ“ ⟧ᢜ ⟦ Ο„ βŸ§α΅— β†’ Type (o βŠ” β„“)

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

Tyα΅– (Ο„ `Γ— Οƒ) Ξ“ h = Tyα΅– Ο„ Ξ“ (π₁ ∘ h) Γ— Tyα΅– Οƒ Ξ“ (Ο€β‚‚ ∘ h)

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.

Tyα΅– (Ο„ `β‡’ Οƒ) Ξ“ h =
  βˆ€ {Ξ”} (ρ : 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.

Tyα΅– (` x)    Ξ“ h = Ξ£ (Ne Ξ“ (` x)) Ξ» n β†’ ⟦ n βŸ§β‚› ≑ h

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

As always, we must show that these have an action by renamings. Renamings act on the semantic component, too: if tracks then tracks

ren-tyα΅–  : βˆ€ {Ξ” Ξ“ Ο„ m} (ρ : Ren Ξ” Ξ“) β†’ Tyα΅– Ο„ Ξ“ m  β†’ Tyα΅–  Ο„ Ξ” (m ∘ ⟦ ρ ⟧ʳ)
ren-subα΅– : βˆ€ {Ξ” Ξ“ Θ m} (ρ : Ren Θ Ξ”) β†’ Subα΅– Ξ“ Ξ” m β†’ Subα΅– Ξ“ Θ (m ∘ ⟦ ρ ⟧ʳ)

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.

reifyα΅–         : βˆ€ {h}                 β†’ Tyα΅– Ο„ Ξ“ h β†’ Nf Ξ“ Ο„
reflectα΅–       : (n : Ne Ξ“ Ο„)          β†’ Tyα΅– Ο„ Ξ“ ⟦ n βŸ§β‚›
reifyα΅–-correct : βˆ€ {h} (v : Tyα΅– Ο„ Ξ“ h) β†’ ⟦ reifyα΅– v βŸ§β‚™ ≑ h

reifyα΅– {Ο„ = Ο„ `Γ— 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)

reflectα΅– {Ο„ = Ο„ `Γ— Οƒ} n     = reflectα΅– (fstβ‚™ n) , reflectα΅– (sndβ‚™ n)
reflectα΅– {Ο„ = Ο„ `β‡’ Οƒ} n ρ a = tyα΅–βŸ¨ apβ‚‚ (Ξ» e f β†’ ev ∘ ⟨ e , f ⟩) (ren-βŸ¦βŸ§β‚› ρ n) (reifyα΅–-correct a) ⟩tyα΅–
  (reflectα΅– (app (ren-ne ρ n) (reifyα΅– a)))
reflectα΅– {Ο„ = ` x}    n     = n , refl

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

reifyα΅–-correct {Ο„ = Ο„ `Γ— Οƒ} (a , b) = sym $
  Product.unique (fp _ _) _ (sym (reifyα΅–-correct a)) (sym (reifyα΅–-correct b))
reifyα΅–-correct {Ο„ = Ο„ `β‡’ Οƒ} {h = h} Ξ½ =
  let
    p : ⟦ reifyα΅– (Ξ½ (drop stop) (reflectα΅– (var stop))) βŸ§β‚™ ≑ ev ∘ ⟨ h ∘ id ∘ π₁ , Ο€β‚‚ ⟩
    p = reifyα΅–-correct (Ξ½ (drop stop) (reflectα΅– (var stop)))
  in ap Ζ› p
   βˆ™ sym (unique _ (apβ‚‚ _∘_ refl (apβ‚‚ ⟨_,_⟩ (sym (pulll (elimr refl))) (eliml refl))))
reifyα΅–-correct {Ο„ = ` x} d = d .snd

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α΅– : βˆ€ {ρ} (Ο€ : Var Ξ” Ο„) β†’ Subα΅– Ξ” Ξ“ ρ β†’ Tyα΅– Ο„ Ξ“ (⟦ Ο€ ⟧ⁿ ∘ ρ)
varα΅– stop    (_ , x) = x
varα΅– (pop v) (c , _) = tyα΅–βŸ¨ assoc _ _ _ ⟩tyα΅– (varα΅– v c)

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:

baseα΅– : βˆ€ {h'} (h : Hom ⟦ Ξ” ⟧ᢜ ⟦ Ο„ βŸ§α΅—) β†’ Subα΅– Ξ” Ξ“ h' β†’ Tyα΅– Ο„ Ξ“ (h ∘ h')

baseα΅– {Ο„ = Ο„ `Γ— τ₁} x c     =
    tyα΅–βŸ¨ sym (assoc _ _ _) ⟩tyα΅– (baseα΅– (π₁ ∘ x) c)
  , tyα΅–βŸ¨ sym (assoc _ _ _) ⟩tyα΅– (baseα΅– (Ο€β‚‚ ∘ x) c)

baseα΅– {Ο„ = Ο„ `β‡’ Οƒ} {h' = h'} h c ρ {Ξ±} a = tyα΅–βŸ¨ pullr (Product.unique (fp _ _) _ (pulll Ο€β‚βˆ˜βŸ¨βŸ© βˆ™ extendr Ο€β‚βˆ˜βŸ¨βŸ©) (pulll Ο€β‚‚βˆ˜βŸ¨βŸ© βˆ™ Ο€β‚‚βˆ˜βŸ¨βŸ©)) ⟩tyα΅–
  (baseα΅– (ev ∘ ⟨ h ∘ π₁ , Ο€β‚‚ ⟩) (
    subα΅–βŸ¨ sym Ο€β‚βˆ˜βŸ¨βŸ© ⟩subα΅– (ren-subα΅– ρ c), tyα΅–βŸ¨ sym Ο€β‚‚βˆ˜βŸ¨βŸ© ⟩tyα΅– a))

baseα΅– {Ο„ = ` t} x c = hom (x ∘ ⟦ c ⟧˒) , ap (x ∘_) (⟦⟧˒-correct c)

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.

exprα΅– : βˆ€ {h} (e : Expr Ξ” Ο„) (ρ : Subα΅– Ξ” Ξ“ h) β†’ Tyα΅– Ο„ Ξ“ (⟦ e βŸ§α΅‰ ∘ h)
exprα΅– (`var x)   c = varα΅– x c
exprα΅– (`π₁ p)    c = tyα΅–βŸ¨ assoc _ _ _ ⟩tyα΅– (exprα΅– p c .fst)
exprα΅– (`Ο€β‚‚ p)    c = tyα΅–βŸ¨ assoc _ _ _ ⟩tyα΅– (exprα΅– p c .snd)
exprα΅– `⟨ a , b ⟩ c =
  tyα΅–βŸ¨ sym (pulll Ο€β‚βˆ˜βŸ¨βŸ©) ⟩tyα΅– (exprα΅– a c) , tyα΅–βŸ¨ sym (pulll Ο€β‚‚βˆ˜βŸ¨βŸ©) ⟩tyα΅– (exprα΅– b c)
exprα΅– {h = h} (`$ f a) c = tyα΅–βŸ¨ ap (ev ∘_) (apβ‚‚ ⟨_,_⟩ (idr _) refl βˆ™ sym (⟨⟩∘ h)) βˆ™ assoc _ _ _ ⟩tyα΅–
  (exprα΅– f c stop (exprα΅– a c))
exprα΅– (` x)      c = baseα΅– x c
exprα΅– {h = h} (`Ξ» f) ρ Οƒ {m} a = tyα΅–βŸ¨ fixup ⟩tyα΅– (exprα΅– f
  ( subα΅–βŸ¨ sym Ο€β‚βˆ˜βŸ¨βŸ© ⟩subα΅– (ren-subα΅– Οƒ ρ)
  , tyα΅–βŸ¨ sym Ο€β‚‚βˆ˜βŸ¨βŸ© ⟩tyα΅–  a ))

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!

id-subα΅– : βˆ€ Ξ“ β†’ Subα΅– Ξ“ Ξ“ id
id-subα΅– βˆ…     = βˆ…
id-subα΅– (Ξ“ , x) =
    subα΅–βŸ¨ idl _ Β·Β· idl _ Β·Β· sym (idr _) ⟩subα΅– (ren-subα΅– (drop stop) (id-subα΅– Ξ“))
  , tyα΅–βŸ¨ sym (idr _) ⟩tyα΅–  (reflectα΅– (var stop))

nf : Expr Ξ“ Ο„ β†’ Nf Ξ“ Ο„
nf x = reifyα΅– (exprα΅– x (id-subα΅– _))

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
  nf-sound : (e : Expr Ξ“ Ο„) β†’ ⟦ nf e βŸ§β‚™ ≑ ⟦ e βŸ§α΅‰
  nf-sound {Ξ“ = Ξ“} e = reifyα΅–-correct (exprα΅– e (id-subα΅– Ξ“)) βˆ™ elimr refl

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
  e1 : Expr ((βˆ… , ` a) , ` b) _
  e1 = `π₁ `⟨ `$ (`Ξ» (`var stop)) (`var (pop stop)) , `var stop ⟩

  _ : 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.

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.

canonicity : βˆ€ {a} β†’ (e : Expr βˆ… (` a)) β†’ Ξ£ (Hom (Terminal.top term) a) Ξ» h β†’ ⟦ e βŸ§α΅‰ ≑ h
canonicity {a = a} e = go (nf e) (nf-sound e) where
  no-functions : βˆ€ {a b} β†’ Ne βˆ… (a `β‡’ b) β†’ βŠ₯
  no-pairs     : βˆ€ {a b} β†’ Ne βˆ… (a `Γ— b) β†’ βŠ₯

  no-functions (app f _) = no-functions f
  no-functions (fstβ‚™ x)  = no-pairs x
  no-functions (sndβ‚™ x)  = no-pairs x

  no-pairs (app f _) = no-functions f
  no-pairs (fstβ‚™ x)  = no-pairs x
  no-pairs (sndβ‚™ x)  = no-pairs x

  go : (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

  1. since contexts are built by extension on the rightβ†©οΈŽ

  2. It is not, however, β†©οΈŽ

  3. and the identity renaming, since our argument lives in the same contextβ†©οΈŽ