Files
univTypes/README.md
Julius FischerandClaude Opus 5.5 411c93d27f
Typecheck / agda (push) Failing after 1m5s
State decidability of conversion
Decidability/Statement.agda states Decidable-≈ and Decidable-≈ₗ for
well-typed terms (conversion is undecidable on untyped terms) and the
normalisation property the proof is expected to go through. TODO and
README updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016LG5WunTkTJFuvdzgET3Af
2026-11-19 15:05:04 +01:00

306 lines
14 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# UniverseLevelTypes
[![Typecheck](https://git.narl.io/minze/univTypes/actions/workflows/typecheck.yml/badge.svg?branch=main)](https://git.narl.io/minze/univTypes/actions)
Master Project
## Motivation
Agda's universe levels are first-class: `Level` is a type, and the sort of a
type can depend on a term of type `Level`. This interacts badly with
reduction. Agda issue [#5810](https://github.com/agda/agda/issues/5810)
("Loss of subject reduction with Setω", open since 2022) shows that a term
can change its type when it is normalised:
```agda
open import Agda.Primitive
open import Agda.Builtin.Unit
bad : Setω
bad = (λ (ℓ : ⊤ → Level) → ∀ t → Set (ℓ t)) (λ _ → lzero)
```
Agda accepts `bad : Setω`, but `bad` β-reduces to `(t : ⊤) → Set`, which
Agda types as `Set₁`. Asking Agda for the normal form and using it with the
original type fails with `Set₁ != Setω`. Subject reduction (if `Γ ⊢ t : A`
and `t ⟶ t'`, then `Γ ⊢ t' : A`) therefore does not hold.
The cause is that the sort of a function type `(x : A) → B` depends on
whether the level of `B` mentions `x`. If it does, the type lives in `Setω`;
if it does not, it lives in `Set (a ⊔ b)`. This is decided by a free-variable
check, and that check is not stable under substitution or reduction. In the
example above, `ℓ t` mentions `t` before substituting `λ _ → lzero` for `ℓ`,
and `lzero` does not afterwards.
The same root problem shows up elsewhere, e.g. in
[#8106](https://github.com/agda/agda/issues/8106), where `--level-universe`
changes what `Level → Set` reduces to across module boundaries. The
discussion on #5810 suggests separating level-dependent function types
(always in `Setω`) from ordinary function types with non-dependent sorts.
This project formalises a type theory with universe levels in Agda and
proves subject reduction for it (see `SubjectReduction/`), aiming at a
treatment of level-dependent sorts for which the property holds.
## Layout and checking
- `SubjectReduction/Specification/`: the specification (syntax,
substitution, values, reduction, level equality, conversion, typing);
`SubjectReduction/Specification.agda` re-exports all of it.
- `SubjectReduction/`: the proofs. The main chain is
`LevelEqualityProperties` (injectivity, stability under substitution),
`Confluence` (Takahashi), `ConfluenceModulo` (Church–Rosser modulo level
equality), `ConversionProperties`, `Inversion`,
`SubstitutionPreservesTyping` and `SubjectReduction`; `Progress`
builds on `ConfluenceModulo` and `ConversionProperties`. The level
normaliser (`LevelNormalForm.agda`, correctness proofs in
`LevelNormalForm/`) gives decidability of level equality
(`LevelEqualityDecision.agda`); subject reduction does not depend on it.
- `Decidability/`: decidability of conversion for well-typed terms
(`Statement.agda` states it; the proof is open, see `TODO.md`).
- `Tests/`: example derivations and computational tests. A test passes if
its file typechecks.
Modules are named `univTypes.…`, so the library root is the directory
*containing* this repository (with an `.agda-lib` there that includes `.`
and depends on `standard-library`). Check a file with e.g.
`agda Tests/LevelNormalForm.agda`.
## Design: universe hierarchy
Two towers of universes: `Set l` is indexed by a level term `l : Level`,
`Setω k` by a meta-level natural number `k : ℕ`:
```
Set l : Set (lsuc l)
Setω k : Setω (suc k)
Set l ≤ Setω 0 (for every l)
```
- There are infinitely many `Setω`s, so every `Setω k` has a type and the
hierarchy never ends at a top sort.
- `Setω 0` is a supertype of every `Set l`. This is what allows
level-polymorphic types such as `∀ (l : Level) → Set l` to live in
`Setω 0`.
- As in Agda (`Setω`, `Setω₁`, …), the index of `Setω` is a meta-level
number, not a `Level` term. So `Setω` cannot depend on a level variable,
is unaffected by renaming, substitution, reduction and level equality,
and needs no well-formedness premise.
Cumulativity is a single lifting rule (no general subtyping):
```agda
⊢-Set<Setω : Γ ⊢ A ⦂ Set l → Γ ⊢ A ⦂ Setω 0
```
There is no lifting between the `Setω`s: `Setω k₁ ≤ Setω k₂` does not hold
for `k₁ < k₂`.
Function types get one of three rules:
```agda
⊢-∀ : Γ ⊢ A ⦂ Set l₁ → Γ , A ⊢ B ⦂ Set (ren wk l₂)
→ Γ ⊢ ∀[x⦂ A ] B ⦂ Set (l₁ ⊔ l₂)
⊢-∀ω : Γ ⊢ A ⦂ Setω k₁ → Γ , A ⊢ B ⦂ Setω k₂
→ Γ ⊢ ∀[x⦂ A ] B ⦂ Setω (k₁ ⊔ℕ k₂)
⊢-∀-dep : Γ ⊢ A ⦂ Setω k₁ → Γ , A ⊢ B ⦂ Set l₂ -- l₂ may mention x
→ Γ ⊢ ∀[x⦂ A ] B ⦂ Setω k₁
```
- In `⊢-∀` the codomain level is a weakened outer level (`ren wk l₂`), so
it cannot mention `x`. No free-variable check is involved: the rule only
matches when the level has that form. In `⊢-∀ω` the codomain index is a
natural number, so it cannot mention `x` either.
- `⊢-∀-dep` handles level-dependent codomains such as
`∀ (l : Level) → Set l : Setω 0` (domain `Level : Set` lifted to
`Setω 0`). It overlaps with `⊢-∀` when the codomain level does not
mention `x`, which is harmless because of `⊢-Set<Setω`.
- This avoids the problem of #5810: there, the sort of a function type is
computed from the term, so it changes when a dependency disappears under
reduction. Here typing is a relation. Reduction and substitution can only
remove a dependency on `x`, never create one, and a type that drops from
`Setω` to `Set` is still accepted via `Set l ≤ Setω 0`.
- A level-dependent `Setω` (`∀ (l : Level) → Setω l`) cannot even be
written.
`∃` follows the same three rules (`⊢-∃`, `⊢-∃ω`, `⊢-∃-dep`), so e.g.
`Σ (l : Level) (Set l) : Setω 0`. The other rules fit in as follows:
- `⊢-λ` requires `Γ ⊢ A ⦂ Setω k` for its domain. Every type has such a
typing (small types via `⊢-Set<Setω`), so this only rules out domains
that are not types.
- `Set l` requires the level to be well formed, `Γ ⊢ₗ l` (see below).
- Equality is small, as in Agda: `e₁ ≡ e₂ : Set l` for `t : Set l`, and
the motive of `subst` is in `Set (ren wk l)`.
An alternative formulation with an explicit `Bad` predicate is described in
`IDEAS.md`.
## Design: level equality
### Two sorts of syntax
Level expressions have their own syntax, separate from terms. `Tm` is
indexed by a sort, `Term = Tm tm` and `Lvl = Tm lv`, so renaming,
substitution, reduction and level equality are still single definitions:
```agda
-- level expressions (Lvl n)
`lzero : Lvl n `lsuc : Lvl n → Lvl n _`⊔_ : Lvl n → Lvl n → Lvl n
`latom : Term n → Lvl n -- a term of type Level, used as a level
-- terms (Term n), besides the usual ones
`lvl : Lvl n → Term n -- a level expression, as a term of type Level
`Setn_ : Lvl n → Term n `Setω_ : ℕ → Term n
```
with the reduction `` `latom (`lvl l) ↪ l ``. `Level` stays an ordinary
type, so levels can be passed around, and neutral levels such as
`` `latom (f · x) `` are possible. Typing gets a second judgement
`Γ ⊢ₗ l` for well-formed levels (`⊢-latom` from `Γ ⊢ e ⦂ Level`,
`⊢-lzero`, `⊢-lsuc`, `⊢-⊔`), and ``⊢-lvl : Γ ⊢ₗ l → Γ ⊢ `lvl l ⦂ Level``.
### Level equality
Level expressions are equal modulo the equations of a join-semilattice
with `lzero` as least element and `lsuc` as an inflationary endomorphism
that distributes over `_⊔_`:
```
(l₁ ⊔ l₂) ⊔ l₃ = l₁ ⊔ (l₂ ⊔ l₃) l₁ ⊔ l₂ = l₂ ⊔ l₁
l ⊔ l = l l ⊔ lzero = l
lsuc (l₁ ⊔ l₂) = lsuc l₁ ⊔ lsuc l₂ l ⊔ lsuc l = lsuc l
```
`_~_` (`Specification/LevelEquality.agda`) is the congruence closure of
these equations on both sorts. The equations only relate level
expressions, so on terms `_~_` is a pure congruence: `~`-equal terms have
the same outermost constructor with `~`-equal arguments
(`LevelEqualityProperties.agda`, via the one-layer relation `_≋_`). This
gives injectivity of every term constructor through `~`, and shows that
different term constructors are never `~`-equal. In particular a term is a
redex iff every `~`-equal term is one.
This is why the separate syntax is needed. With levels as ordinary terms,
`(id ⊔ lzero) · Level ~ id · Level ↪ Level`, while the left side has no
redex at all: level equality could hide and create redexes, and
conversion was not transitive. Restricting the equations to neutral atoms
does not help, because substitution can turn a neutral atom into a λ.
### Conversion
Conversion is a common reduct under *reduction modulo level equality*:
```agda
data _↪~_ e e' : Set where -- one step: a reduction or a level equation
↪~-↪ : e ↪ e' → e ↪~ e'
↪~-~ : e ~ e' → e ↪~ e'
e₁ ≈ e₂ = ∃ u. e₁ ↪~* u × e₂ ↪~* u
```
Comparing with `~` only after reducing (`e₁ ↪* u₁ ~ u₂ ↫* e₂`) would make
transitivity hard to prove: `l ⊔ l ~ l` duplicates `l`, and the two copies
can then be reduced differently.
Transitivity follows from **Church–Rosser modulo level equality**
(`ConfluenceModulo.agda`), an extension of Takahashi's proof
(`Confluence.agda`):
1. Complete development respects level equality: `u ~ u' → u⁺ ~ u'⁺`. In
a redex position both sides have the same constructor (by `_≋_`), and
the equations never look into their arguments; duplicated copies of `l`
are developed to the same `l⁺`. The β case needs `~` to be stable under
substitution, which holds because the equations are schematic.
2. So the relation `~ ; ↪ₚ ; ~` has the triangle property with respect to
`_⁺` (`e ⇛ e' → e' ⇛ e⁺`), hence the diamond property, and its
reflexive-transitive closure, which is exactly `_↪~*_`, is confluent.
Inversion of conversion (`∀[x⦂ A ] B ≈ ∀[x⦂ A' ] B'` gives `A ≈ A'` and
`B ≈ B'`, universes are never convertible to `∀`, …) follows from shape
lemmas: neither a reduction step nor a level equation changes the
outermost constructor of a `∀`, `∃`, `≡`, `Set l` or `Setω k` (and
nothing changes `Setω k` at all).
An order on levels is derivable if needed later (e.g. for a more general
cumulativity `Set l₁ ≤ Set l₂`): `l₁ ≤ l₂` iff `l₁ ⊔ l₂ ~ l₂`.
### Level normal form
`SubjectReduction/LevelNormalForm.agda` defines `nf : Tm s n → Tm s n`,
which normalises every level expression inside a term. A level expression
is a tree of `lzero`, `lsuc` and `_⊔_` over *atoms* `` `latom a ``; it
denotes `max(c, a₁ + k₁, …, aₘ + kₘ)` and is brought into a canonical shape:
1. atom terms are normalised first, so `~`-equal atoms become identical;
2. each atom appears once, with its largest offset
(`x ⊔ lsuc x` becomes `lsuc x`);
3. atoms are sorted by a fixed total order on terms (`compare`:
variables by index first, then head constructor, then the arguments
from left to right);
4. the constant `c` is dropped if some offset `kᵢ ≥ c`, since
`aᵢ + kᵢ ≥ kᵢ` (`lsuc x ⊔ lsuc lzero` becomes `lsuc x`);
5. the result is read back as a right-nested join
`lsucᵏ¹ a₁ ⊔ (… ⊔ (lsucᵏᵐ aₘ ⊔ lsucᶜ lzero))`, with `lsucᶜ lzero`
present only if it was not dropped; a lone atom with offset 0 is just
the atom.
All other constructors are normalised componentwise, so e.g.
`nf (∀[x⦂ A ] B) = ∀[x⦂ nf A ] nf B`. The theorem is
`u ~ u' ⇔ nf u ≡ nf u'`, proven in two halves:
- **Soundness**, `sound : u ~ nf u` (`LevelNormalForm/Soundness.agda`).
A normal form `(c, [(a₁,k₁), …])` denotes the level
`a₁+k₁ ⊔ (… ⊔ c)`; `flatten l` denotes a level `~`-equal to `l`,
`shift` and `join` denote `lsuc` and `⊔`, and `readback` produces a level
`~`-equal to the denotation (dropping `c` uses
`c ≤ k → lsucᶜ lzero ⊔ lsucᵏ a ~ lsucᵏ a`).
- **Completeness**, `complete : u ~ u' → nf u ≡ nf u'`
(`LevelNormalForm/Completeness.agda`), one case per equation and
congruence. The level normal forms built by `flatten` are *well formed*:
sorted, every offset at most the constant `c` (an atom starts at
`c = 0`, offset 0; `shift` raises both, `join` takes maxima), and every
atom term is in normal form. On well-formed normal forms
`flatten (readback L) ≡ L`, so `flatten l` is determined by `nf l`;
this is what the congruences for `lsuc` and `⊔` need. It also gives
idempotence `nf (nf u) ≡ nf u`.
The order on terms is defined by encoding terms as trees
(`encode : Tm s n → Tree`, injective since it has a left inverse) and
comparing trees lexicographically, so the order laws (`LevelNormalForm/Order.agda`)
are proven once for trees instead of for every pair of constructors.
`merge` is commutative and idempotent on all lists; associativity holds on
sorted lists and is proven through lookup: two sorted lists with the same
lookup function are equal (`LevelNormalForm/Merge.agda`).
`LevelEqualityDecision.agda` concludes that `_~?_` decides `~`, and proves
injectivity of `lsuc` (`~-lsuc-inj : lsuc l ~ lsuc l' → l ~ l'`): `lsuc`
takes part in the level equations, so this does not follow from the
one-layer structure of `~`; instead `nf (lsuc l) = readback (shift
(flatten l))`, `readback` is injective on well-formed normal forms and
`shift` is injective.
Alternatives considered are described in `IDEAS.md`.
## Progress
Reduction is full (ξ rules under every constructor, including binders), so
`Value` describes *normal forms* and `Neutral` the stuck terms: a variable
under applications, projections and `subst`, with normal arguments.
`Progress.agda` proves progress for open terms:
```agda
progress : Γ ⊢ e ⦂ t → Value e ⊎ ∃[ e' ] e ↪ e'
progressₗ : Γ ⊢ₗ l → Value l ⊎ ∃[ l' ] l ↪ l'
```
and `value-normal : Value e → e ↪ e' → ⊥`, so a well-typed term is a
`Value` exactly when it does not reduce. (For closed terms, `Neutral` is
empty, so normal forms are introduction forms.)
The key lemma is canonical forms: a normal form whose type has head `h`
(∀, ∃, ≡, `Level`, or a universe) is neutral or the introduction form for
`h` (`λ`, `,`, `refl`, `` `lvl ``, or a type former). It rests on a
generic head lemma, `≈-head`: reduction and level equality preserve the
outermost constructor of a type former, so convertible type formers have
the same head (`Setn` and `Setω` share one, since the cumulativity rules
move between them).