Typecheck / agda (push) Successful in 5m16s
Model/Mathlib states one field per lemma of Mathlib's ZFSet (Mathlib/SetTheory/ZFC/Basic.lean), with Lean names and statements in Agda syntax. Model/FromMathlib derives the base ZF axioms from it (ω as the least inductive subset of Mathlib's omega). Mathlib's ZFSet has no universes, so Model/ConsistencyZF adds Grothendieck's universe axiom on top: no-False-Mathlib, no-0≡1-Mathlib, no-∀A→A-Mathlib. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q7dfaHQ3dYSfqcxR7V5UrJ
526 lines
26 KiB
Markdown
526 lines
26 KiB
Markdown
# UniverseLevelTypes
|
||
|
||
[](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 `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):
|
||
ZF, FromZF, Grothendieck, FromGrothendieck,
|
||
TarskiGrothendieck, FromTarski, Mathlib,
|
||
FromMathlib, SetTheory,
|
||
Interpretation, Conversion, Soundness, Consistency,
|
||
ConsistencyZF
|
||
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):
|
||
|
||
```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:
|
||
|
||
- λ 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:
|
||
|
||
```agda
|
||
⊢-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:
|
||
|
||
```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
|
||
(`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*:
|
||
|
||
```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**
|
||
(`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:
|
||
|
||
```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).
|
||
|
||
## Decidability of conversion
|
||
|
||
```agda
|
||
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:
|
||
|
||
```agda
|
||
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
|
||
|
||
```agda
|
||
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** (`ZF.agda`): the ZF axioms as an interface in the style of
|
||
con-leche's `SetTheory` class (extensionality, pairing, union,
|
||
power set, replacement as a scheme, regularity, infinity) with Grothendieck
|
||
universes along the strata `fin k` (`Set k`) and `ω+ m` (`Setω m`), i.e.
|
||
ω·2 inaccessible cardinals (con-leche's chain is indexed by ℕ; `Setω 0`
|
||
needs a universe above every `Set k`). As in con-leche, each universe is
|
||
a member of the next; since `Setω 0` has no predecessor, one more axiom puts
|
||
every `Set k` in it. Classical logic is a field (`em`),
|
||
since Agda is constructive; con-leche inherits it from Lean. The interface
|
||
is a record passed as an argument, not a postulate, so the result
|
||
(`ConsistencyZF.agda`) is consistency relative to classical ZF with ω·2
|
||
inaccessibles.
|
||
|
||
`FromZF.agda` constructs from these axioms the operations the model uses
|
||
(`SetTheory.agda`): the empty set (from regularity on a nonempty transitive
|
||
universe, as in con-leche), separation (from replacement and `em`), Kuratowski
|
||
pairs with their projections, Σ-sets, function graphs with application and
|
||
domain, von Neumann numerals (injective by regularity), and the closure of
|
||
the universes under all of them.
|
||
|
||
**Published set theories.** The consistency theorems also hold relative to
|
||
published axiomatisations, each transcribed as a record and shown to
|
||
yield the ZF record above (`ConsistencyZF.agda`: `no-False-UA`, `no-False-TG`,
|
||
…):
|
||
|
||
- ZFC with **Grothendieck's universe axiom** (SGA 4, Exp. I, appendix by
|
||
Bourbaki, 1972; `Grothendieck.agda`): every set is a member of a
|
||
universe (transitive, closed under pairing, power set and unions of
|
||
families). `FromGrothendieck.agda` builds the chain by iterating the
|
||
axiom from ω, and the limit `Setω 0` as a universe containing all `Set k`
|
||
(replacement over ω).
|
||
- **Tarski–Grothendieck** set theory (Tarski's Axiom A, 1938; the
|
||
foundation of Mizar; `TarskiGrothendieck.agda`): every set is a member of
|
||
a Tarski universe (transitive as in con-leche, closed under subsets, with
|
||
a member containing all subsets of each member, and every subset a member
|
||
or equinumerous with it). `FromTarski.agda` shows that Tarski universes
|
||
are Grothendieck universes without choice: replacement by Cantor's
|
||
theorem (an image as big as the universe would map a member onto its
|
||
power set), unions by regularity (otherwise the images of the members
|
||
form a member Y with Y ∈ y ∈ Y), pairs as an image of 𝒫 (𝒫 ∅).
|
||
|
||
- The rules of **Mathlib's `ZFSet`** (`Mathlib/SetTheory/ZFC/Basic.lean`;
|
||
`Mathlib.agda`): one field per Mathlib lemma (`ext`, `notMem-empty`,
|
||
`mem-insert-iff`, `mem-sep`, `mem-powerset`, `mem-sUnion`, `mem-image`,
|
||
`omega-zero`, `omega-succ`, `regularity`, `inductionOn`), with Lean names
|
||
and statements in Agda syntax. `FromMathlib.agda` derives the base axioms
|
||
(ω as the least inductive subset of Mathlib's `omega`); since `ZFSet` has
|
||
no universes, Grothendieck's universe axiom is added on top
|
||
(`no-False-Mathlib`, …).
|
||
|
||
Schemes quantify over Agda functions and predicates, and classical logic is
|
||
a field, in every record.
|
||
|
||
**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).
|
||
|