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
14 KiB
UniverseLevelTypes
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
("Loss of subject reduction with Setω", open since 2022) shows that a term
can change its type when it is normalised:
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, 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.agdare-exports all of it.SubjectReduction/: the proofs. The main chain isLevelEqualityProperties(injectivity, stability under substitution),Confluence(Takahashi),ConfluenceModulo(Church–Rosser modulo level equality),ConversionProperties,Inversion,SubstitutionPreservesTypingandSubjectReduction;Progressbuilds onConfluenceModuloandConversionProperties. The level normaliser (LevelNormalForm.agda, correctness proofs inLevelNormalForm/) gives decidability of level equality (LevelEqualityDecision.agda); subject reduction does not depend on it.Decidability/: decidability of conversion for well-typed terms (Statement.agdastates it; the proof is open, seeTODO.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 everySetω khas a type and the hierarchy never ends at a top sort. Setω 0is a supertype of everySet l. This is what allows level-polymorphic types such as∀ (l : Level) → Set lto live inSetω 0.- As in Agda (
Setω,Setω₁, …), the index ofSetωis a meta-level number, not aLevelterm. SoSetω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):
⊢-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:
⊢-∀ : Γ ⊢ 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 mentionx. 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 mentionxeither. ⊢-∀-dephandles level-dependent codomains such as∀ (l : Level) → Set l : Setω 0(domainLevel : Setlifted toSetω 0). It overlaps with⊢-∀when the codomain level does not mentionx, 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 fromSetωtoSetis still accepted viaSet 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ω kfor its domain. Every type has such a typing (small types via⊢-Set<Setω), so this only rules out domains that are not types.Set lrequires the level to be well formed,Γ ⊢ₗ l(see below).- Equality is small, as in Agda:
e₁ ≡ e₂ : Set lfort : Set l, and the motive ofsubstis inSet (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:
-- 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:
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):
- 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 oflare developed to the samel⁺. The β case needs~to be stable under substitution, which holds because the equations are schematic. - 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:
- atom terms are normalised first, so
~-equal atoms become identical; - each atom appears once, with its largest offset
(
x ⊔ lsuc xbecomeslsuc x); - 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); - the constant
cis dropped if some offsetkᵢ ≥ c, sinceaᵢ + kᵢ ≥ kᵢ(lsuc x ⊔ lsuc lzerobecomeslsuc x); - the result is read back as a right-nested join
lsucᵏ¹ a₁ ⊔ (… ⊔ (lsucᵏᵐ aₘ ⊔ lsucᶜ lzero)), withlsucᶜ lzeropresent 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 levela₁+k₁ ⊔ (… ⊔ c);flatten ldenotes a level~-equal tol,shiftandjoindenotelsucand⊔, andreadbackproduces a level~-equal to the denotation (droppingcusesc ≤ 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 byflattenare well formed: sorted, every offset at most the constantc(an atom starts atc = 0, offset 0;shiftraises both,jointakes maxima), and every atom term is in normal form. On well-formed normal formsflatten (readback L) ≡ L, soflatten lis determined bynf l; this is what the congruences forlsucand⊔need. It also gives idempotencenf (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:
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).