# 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