# 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 `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): ```agda ⊢-Set