Julius FischerandClaude Opus 5.5 5d83754d18
Typecheck / agda (push) Successful in 2m22s
True and False, with ex falso; False is empty in the model
True is the unit type (`True = `Unit, `trivial = `tt). `False : Set has
no introduction form; `absurd P e : P for e : False never reduces at the
root (neutral when e is). Covered through level equality, confluence,
the level normaliser, subject reduction, progress, decidability and
strong normalisation (members of False are terms reducing to a neutral
term). The set-theory interface gains an empty set, which False denotes;
Model/Consistency proves no-False.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q7dfaHQ3dYSfqcxR7V5UrJ
2026-11-22 23:11:57 +01:00
2026-10-22 14:34:37 +02:00
2026-04-01 18:19:34 +02:00

UniverseLevelTypes

Typecheck

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 Metatheory/Typing/), aiming at a treatment of level-dependent sorts for which the property holds.

Layout and checking

Theorems.agda          the main results, re-exported in one place
Specification.agda     re-exports Specification/
Specification/         definitions only: syntax, substitution, values,
                       reduction, level equality, conversion, typing
Metatheory/            proofs about the specification
├── Substitution       fusion lemmas for renaming and substitution
├── Levels/            level equality: structure (Equality), the level
│                      normaliser (NormalForm, NormalForm/), Decision (_~?_)
├── Confluence/        Parallel (Takahashi), Modulo (Church–Rosser modulo
│                      level equality), Conversion (≈ is a congruence, …)
└── Typing/            Inversion, Substitution, SubjectReduction, Progress
Decidability/          decidability of conversion (see below)
├── Statement, Decision, NormalForms, Fundamental, Canonicity
├── SN/                Inductive (inductive SN), Properties, Strong
└── LogRel/            Core (the logical relation), Lift, LevelValue,
                       Universes
Model/                 consistency via a set-theoretic model (see below):
                       SetTheory, Interpretation, Conversion, Soundness,
                       Consistency
Examples/              hand-written examples (for presentations)
Tests/                 generated regression tests
Util/

Imports go one way: Specification (which uses only Util), then Metatheory, then Decidability and Model. Subject reduction does not depend on the level normaliser. Examples/ and Tests/ both typecheck as part of Everything.agda; 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):

⊢-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 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:

  • λ carries its domain, `λ[x⦂ A ] e, and ⊢-λ requires Γ ⊢ A ⦂ Setω k for it. Every type has such a typing (small types via ⊢-Set<Setω), so this only rules out domains that are not types. The annotation lets a λ denote a graph over its domain in the set-theoretic model; β discards it.
  • 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).

Natural numbers (ℕ : Set, zero, suc) have a non-dependent-syntax eliminator natrec P z s t: the motive P is an ordinary term of function type, not a binder, and the instances of the motive are applications P · t. As for ∀, the motive lives in Set l or in Setω m, with one rule each:

⊢-natrec  : Γ ⊢ P ⦂ ∀[x⦂ ℕ ] Set (ren wk l) → Γ ⊢ z ⦂ P · zero
          → Γ ⊢ s ⦂ ∀[x⦂ ℕ ] ∀[x⦂ ren wk P · x ] (ren wk (ren wk P) · suc x)
          → Γ ⊢ t ⦂ ℕ → Γ ⊢ natrec P z s t ⦂ P · t
⊢-natrecω : Γ ⊢ P ⦂ ∀[x⦂ ℕ ] Setω m → …    (same premises otherwise)

with natrec P z s zero ↪ z and natrec P z s (suc k) ↪ s · k · natrec P z s k. Tests/Nat.agda defines addition, multiplication and the factorial with it. 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 (Metatheory/Levels/Equality.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 (Metatheory/Confluence/Modulo.agda), an extension of Takahashi's proof (Metatheory/Confluence/Parallel.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

Metatheory/Levels/NormalForm.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 (Metatheory/Levels/NormalForm/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' (Metatheory/Levels/NormalForm/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 (Metatheory/Levels/NormalForm/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 (Metatheory/Levels/NormalForm/Merge.agda).

Metatheory/Levels/Decision.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. Metatheory/Typing/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).

Decidability of conversion

decidable-≈  : ⊢ Γ → Γ ⊢ e₁ ⦂ t₁ → Γ ⊢ e₂ ⦂ t₂ → Dec (e₁ ≈ e₂)
decidable-≈ₗ : ⊢ Γ → Γ ⊢ₗ l₁ → Γ ⊢ₗ l₂ → Dec (l₁ ≈ l₂)

⊢ Γ says that every context entry is a type (Statement.agda); the typing rules themselves do not check contexts. On untyped terms conversion is undecidable, so the statements require typing.

Decision procedure (Decision.agda, NormalForms.agda). Normalise both sides (e ↪* v with v a Value), then compare the normal forms with the level normaliser (_~?_). This is complete because a normal form only takes ~ steps under ↪~*, and ~ preserves normal forms, so convertible normal forms are ~-equal.

Strong normalisation (SN/Inductive.agda), inductively: neutral terms with SN arguments, introduction forms with SN components, and closure under weak head expansion (with SN side conditions for discarded parts). A derivation yields a normal form by plain reduction (normalisation). SN is about ↪ alone: accessibility modulo ~ is false for levels, since ~ can duplicate an atom and one copy can then be reduced, returning to a ~-equal level.

Logical relation (LogRel/Core.agda), unary and untyped, adapted from Abel, Öhman, Vezzosi (POPL 2018). A reducible type weak-head reduces to a neutral type, Level, Unit, ℕ, ≡, a universe, ∀ or ∃; members are defined by induction-recursion, Kripke-style over renamings. Differences to AÖV:

  • Conversion is untyped, so the relation is unary, and conversion irrelevance (convertible reducible types have the same members) uses confluence modulo ~ and the head lemmas instead of the determinism of weak head reduction.
  • The domain and codomain families of ∀/∃ are reducible only up to conversion: ⊢-≈ accepts any convertible type, so typing does not make types well-formed ((λy. Set) · Ω ≈ Set), and the fundamental theorem is stated up to conversion. Reducible types are therefore not SN themselves; members of universes carry SN separately.
  • Members of ℕ are given by an inductive predicate (Nat): they weak-head reduce to zero, to suc of a member, or to a neutral term. natrec is reducible by induction on it, using the motive's reducible type at every member.

Universes (LogRel/Universes.agda, LogRel/LevelValue.agda, LogRel/Lift.agda). Strata fin 0 < fin 1 < … < ω+ 0 < ω+ 1 < …; the relation at a stratum knows the universes below it, built by structural recursion (Set k added at fin (k + 1), all Set j at ω+ 0, Setω m added at ω+ (m + 1)). The stratum of Set l is the value of l: the value of its normal form, with neutral atoms counted as 0. That is sound because typing only uses level equations valid for all values of the atoms, and a substitution replaces an atom before it is evaluated. Levels are evaluated after one weakening, so that the codomain level of ⊢-∀, known only under the binder, can be strengthened back by a renaming. Type annotations use the relation of all universes at once (kit∞); universe members are reducible at their exact stratum, and cumulativity lifts them along the strata.

Fundamental theorem (Fundamental.agda): under a reducible substitution, every well-typed term is a member of a reducible type convertible to its type, and every well-formed level is SN. The identity substitution is reducible for a well-formed context, which gives SN of well-typed terms and hence decidable-≈ and normalisation.

Strong normalisation in the classical sense, every reduction sequence from a well-typed term is finite (strong-normalisation, SN/Strong.agda), follows by soundness of inductive SN for accessibility. The core is backward closure under weak head expansion: a step from a weak head redex is the head step itself or commutes with it, either reducing the contractum or only the part the head step discards, so the proof recurses lexicographically on the discarded part and the contractum.

Canonicity (Canonicity.agda): a closed well-typed term whose type has head h reduces to an introduction form for h, and a closed natural number reduces to a numeral:

canonicity   : ∅ ⊢ e ⦂ T → head T ≡ just h → ∃[ v ] e ↪* v × Canonical h v
canonicity-ℕ : ∅ ⊢ e ⦂ ℕ → Σ ℕ λ k → e ↪* ⌜ k ⌝

It follows from normalisation, subject reduction and canonical forms, since a closed normal form is never neutral. The proof does not compute inside Agda (e.g. to evaluate fac 3): transports along substitution lemmas proven with fun-ext block evaluation. True is the unit type (True = Unit, member trivial = tt). False : Set has no introduction form; its eliminator absurd P e : P (for e : False and a type P) never reduces at the root, so it is neutral when e is. In the logical relation the members of False are the terms reducing to a neutral term; in the set-theoretic model False denotes the empty set.

The pair rule ⊢-, requires its codomain to be a type, as in Agda: without that premise (λz. Unit , tt) : ∃[x⦂ ∀ (z : Level) → Setω 0 ] (x · x) is derivable although x · x is not a type, and no reducible type is convertible to the pair's type.

Consistency

no-False : ¬ (∅ ⊢ e ⦂ False)
no-0≡1   : ¬ (∅ ⊢ e ⦂ zero ≡ suc zero)
no-∀A→A  : ¬ (∅ ⊢ f ⦂ ∀ (A : Set) → A)

relative to a model of set theory (Model/), following con-leche (an external Lean checker with a set-theoretic consistency proof).

Set theory (SetTheory.agda): an interface passed as a module parameter (no postulate) with dependent function and pair sets, equality as a subset of a singleton, ω, and cumulative universes U β for the strata fin k (Set k) and ω+ m (Setω m). ZFC with ω·2 inaccessible cardinals has an instance, so the result is consistency relative to it.

Interpretation (Interpretation.agda): total and type-free, as in con-leche. Terms denote sets, levels natural numbers (⊔ ↦ max, an atom latom e the number ⟦e⟧); a λ denotes the graph of its body over its annotated domain, ℕ and Level denote ω, Set l the universe U (fin ⟦l⟧). Junk terms get some meaning; well-denotedness (WD) says that every application's argument lies in the function's domain (and two side conditions for suc and natrec).

Meaning preservation (Conversion.agda): reduction preserves the meaning and well-denotedness of well-denoted terms, and level equality preserves meanings outright (the level laws hold in ℕ). So convertible well-denoted terms have the same meaning.

Soundness (Soundness.agda), up to conversion as in the decidability proof: for Γ ⊢ e ⦂ T there is a syntactic witness W ≈ T such that in every environment satisfying the context, e and W are well-denoted and ⟦e⟧ ∈ ⟦W⟧. Types themselves need not have a meaning (⊢-≈ accepts junk), so ⊢-≈ keeps its form. Two changes to the system were needed:

  • λ carries its domain (`λ[x⦂ A ] e), so that it denotes a graph.
  • ⊢-∀ and ⊢-∃ require their codomain level to be well formed (Γ ⊢ₗ l₂): over an empty domain the codomain says nothing about it (con-leche's "certification tax" serves the same purpose).
S
Description
Master Project
Readme
1.2 MiB
1 Stars 1 Watchers 0 Forks
Languages
Agda 100%