Fuss-free universe hierarchies
The thing about universe hierarchies is that they are both painful to implement properly, and punting on them leads to even more pain later on because introducing a hierarchy is an extremely disruptive change for both the system and its libraries. After going through this treadmill a few times, I’ve learned my lesson: universes are something we need to get right from day one. Let me clarify that it is a non-goal to have inferred universe levels; I instead want to make working with universe levels as simple as possible, and to minimise the number of places where they need to appear.
The first thing people ask is whether we will use universes “à la Russell” or “à la Tarski”. This is a question laden with false ideology, because the only kind of universes that exist in an objective (i.e. generalised algebraic) sense are the latter. But there is a valid question lying under the surface, which is whether or not users will be expected to explicitly write the decodings and universe level coercions. To that, I answer that we have the elaboration technology today to reliably avoid these bureaucratic details cluttering the source language.
So you want a coherent universe hierarchy…
- October 6, 2025
- Jon Sterling
Someone who wishes to deploy modern elaboration technology to achieve an implicit source language, however, must think very carefully about the algebraic formulation of the core language to ensure that it has enough equations to maintain predictable behaviour under the implicit Russell-style notation. For example, if the level coercions are not coherent enough, it is easy to create unacceptable error messages that appear to the user like “Could not unify A -> B with A -> B.” Unfortunately, strong enough core languages are very laborious to specify and implement; for example, you will need the following (or some variation):
- You need top-level type constructors for all type theoretic connectives.
- For each universe level
i, a typeU[i] : Typeand a dependent typeel[i] : U[i] -> Typeand codes closingU[i]under all type theoretic connectives, including codes forU[j]withj<i. Theel[i]operation needs to commute strictly with the type constructor codes. - For universe levels
i <= j, you need a lifting operationlift[i<=j]: U[i] -> U[j]that commutes with all type constructor codes and is, moreover, definitionally functorial in the level preorder. - You need
elto commute withlift. (Thanks to Meven Lennon-Bertrand for catching this!) - You need both
lift[i<=j]andel[i]to be definitionally injective.
There could be ways to refactor the above; for example, you could get rid of the top level of types altogether and just have universes. This is sometimes referred to as “universes à la Coquand”. Although admirable for its parsimony, I no longer like this approach because I agree with Per Martin-Löf that types and their elements are different concepts. And, for engineering purposes, although it seems at first easiest to have only universes, I think there are subtle ways in which that choice can make other aspects of the implementation more complicated. I think the importance of a separate top-level notion of type is argued for very well in the recent paper of Bezem, Coquand, Dybjer and Escardó.
Anyway, this is a lot to implement in the kernel of a proof assistant.
A new idea: Working your way down from the top
- October 6, 2025
- Jon Sterling
About a year ago, I started to get the inkling of an idea that we should not program with respect to a universe at all if we don’t need it. Instead, we should do large things by default (just like in everyday mathematics) and then when we have a need to talk about small things, we should just specialise them as-needed. For example, let’s consider the generalised algebraic theory of a category as phrased in univalent foundations:
- We have a -type of objects
C. - For every two
x,y:Cwe have a -type of arrowsC(x,y). - And a bunch of operations and axioms involving the above.
There is nothing about this definition that requires any particular constraints on the size of C and of C(x,y). In fact, the proper definition would not require that C(x,y) have the same size as C(u,v)! From the point of view of (generalised) algebra, these are just sorts. In fact, you never need to constrain the size of these things until you wish to start talking about things like the (bi)category of categories, etc.
In contrast, if you require all types mentioned in a theory to have a given size, you immediately create a lot of complexity. First of all, you will be automatically assuming that the category is locally small for some universe—by requiring that C(x,y) be V-small for all x,y; there can be times when this is an unnatural assumption. But maybe the bigger problem is that when you want to start developing the theory of functors and natural transformations and so on, you will end up having at a half-dozen universe levels annotating every definition. Implicit universe polymorphism is not a solution to this problem: implicitly resolving the levels does not remove the complexity, and any experienced mechaniser will know that you inevitably need to debug a universe constraint graph.
Some dream code
- October 6, 2025
- Jon Sterling
What I would like is the best of both worlds: to be allowed to develop the theory with fully unconstrained sizes, and then specialise the theory to more specific sizes later. Consider the following dream code for the theory of categories:
theory Category where
ob: Type
hom: ob -> ob -> Type
// operations and axioms...There is no implicit polymorphism happening in this definition. It is literally a very large theory of large categories. We can develop the theory of functors and natural transformations at this level too:
theory Functor where
C: Category
D: Category
ob: C.ob -> D.ob
hom: (x y: C.ob) -> C.hom x y -> D.hom (ob x) (ob y)
// axioms...
theory NaturalTransformation where
C: Category
D: Category
F: Functor / {C => C, D => D}
G: Functor / {C => C, D => D}
go: (x: C.ob) -> D.hom (F.ob x) (G.ob x)
// naturality axiom...If we want to talk about small or locally small strict categories, this is going to be very easy, and we can reuse everything from the theory of functors and natural transformations by instantiation.
theory LocallySmallCategory where
V: Universe
ob: Type
hom: ob -> ob -> V
inherit Category / {ob => ob, hom => hom}
theory SmallCategory where
U: Universe
ob: U
inherit LocallySmallCategory / {V => U, ob => ob}The above registers implicit coercions SmallCategory >-> LocallySmallCategory >-> Category. So if we want to talk about functors between X: SmallCategory and Y: LocallySmallCategory, we can do so by specialisation with no fuss:
Functor / {C => X, D => Y}We will also be able to define the large category of small categories, again with no fuss.
theory SmallStrictCategory where
inherit SmallCategory
code: U
code/is-set: is-set code
encode: code -> ob
encode/surjective: is-surjective encode
theory CategoryOfSmallStrictCategories where
U: Universe
scat: Category
scat.ob => SmallStrictCategory / {U => U}
scat.hom X Y => Functor / {C => X, D => Y}
// ...The theoretical foundation
- October 6, 2025
- Jon Sterling
I am now going to describe a way to set up a coherent universe hierarchy so as to ensure a parsimonious interpretation. I would like to emphasise that the dream code above can be elaborated to a traditional coherent universe hierarchy with enough equations, and that my reformulation here is only aiming to make this easier.
In essence the main idea is to think about the image of the universe decoding maps el[i]: U[i] -> Type. In the generalised algebraic theory, we have required that these encoding maps be injective; an easily overlooked consequence of the injectivity is that the image of the decoding map is a derivable sort in the generalised/essentially algebraic doctrine.
From this observation, we might instead consider a reformulation where instead of axiomatising the type codes and decoding maps and lifting coercions at each level and all the equations they must satisfy, we might instead axiomatise the images of the decoding maps in Type. This corresponds syntactically to defining a smallness judgement on types, which has a particularly extrinsic feeling to it. In fact, we would define this judgment using a judgemental partial ordering of universe levels; one example rule is the following:
(The real generalised algebraic theory would have a sort of smallness-proofs, but this sort is axiomatised as a proposition. The fact that coherence/proof-irrelevance is baked into the theory fully justifies a notation in which the proofs are omitted.)
Now, we recover the codes and the level coercions in one fell swoop by means of the following simple rules:
Now, supposing that the smallness and level inequality judgements have been filled out sufficiently, we can derive codes for connectives at the lower levels from the corresponding top-level type connectives. For example, consider the dependent function space:
Of course, the notation is a little noisy, but these operations are precisely the sorts of things that are easy for a bidirectional elaborator to insert (and then hide from the user). It works equally well for Agda-style hierarchies with joins:
The beauty of this simple approach is that we cut by two-thirds the number of universe-related operations we must implement in the core language. In that sense, we have an equivalent reformulation of coherent universe hierarchies that is more friendly on the engineering side.
What about universe polymorphism?
- October 6, 2025
- Jon Sterling
There are a few directions that one might go for introducing universe polymorphism. I will outline a couple options, both of which can be built on top of the above.
Explicit polymorphism with a sort or type of levels
- October 6, 2025
- Jon Sterling
In Agda, the levels themselves form a type; this simplifies some things, and makes other things more complicated; on the other hand, Bezem, Coquand, Dybjer, and Escardó have proposed a stratified system in which levels can be abstracted but are not organised into an actual type. The main comment I would like to make here is that the formulation of universe heirarchies that I have described above will work regardless of whether levels form a type. The main syntactical challenge is to implement the smallness and inequality judgements, but is easily done in the style of Allais, Mc Bride, and Boutillier as recently reformulated by Corbyn et al.. Put in terms of the latter, the type of universe levels is the free semilattice with successor on the presheaf of neutral terms of level type; and it is easy to decide equality in free semilattices with successor.
A semantic challenge for Agda-style levels is that the semilattice laws are very strict, and so an interpretation into a higher-topos model might be a problem; for example, we cannot rely on the natural numbers being strict in every model of HoTT, which means that the semilattice laws might not hold definitionally. But we can stratify things in such a way that the type of levels is not small for any universe; then the universe levels could be interpreted as the “exo-naturals” in the extensional outer-layer of two-level type theory, which is conservative over HoTT.
Mc Bride’s Crude but Effective Stratification
- October 6, 2025
- Jon Sterling
In 2012, Conor Titania Mc Bride proposed what they called Crude but Effective Stratification. Their idea is to define everything at the lowest possible level where it can live, but then subject the entire type theory to a functoriality in displacement operations, which allows developments to be shunted upward in the hierarchy as-needed. One way to think about the simplest version of Crude but Effective Stratification is that every top-level definition is polymorphic in a secret universe level variable, which can be restricted along edges in the universe level preorder. For more advanced functionality, you instead think of the top-level definitions as being polymorphic in a displacement.
Conor’s idea is very flexible. As Favonia, Angiuli, and Mullanix pointed out, you could also have a universe hierarchy indexed in integers, and then shunt the developments both upward and downward. This one is justified in syntactical way—a semantic model of the system would not have have negative universe levels, but would instead pass through the observation that a given piece of syntax can only use finitely many universe levels.
Either way, the benefit of Mc Bride’s universe displacements is that when you have an arrangement of universes that need to be in some linear relationship to each other, you can just pick the tightest possible instantiation (e.g. U, U+, U++), and then later use a displacement to create extra room between the universes.
Anyway, I think this is a good idea for handling universe polymorphism, but that question is a orthogonal to the design of the hierarchy itself. If we want, we can implement Crude but Effective Stratification on top of our foundation. I don’t plan to do this on day one, because I first want to see what the usability limitations are before introducing general displacements.
In Project Pterodactyl, I will start with a preference for the explicit polymorphism to keep things simple. I am open to revisiting this in the future. By the way, I have already implemented everything I have described in this post in my secret Pterodactyl kernel (minus the elaboration itself).