sort-system.lagda.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. ..
  2. ::
  3. module language.sort-system where
  4. open import Agda.Builtin.Unit
  5. .. _sort-system:
  6. ***********
  7. Sort System
  8. ***********
  9. .. _intro-sorts:
  10. Sorts (also known as universes) are types whose members themselves are
  11. again types. The fundamental sort in Agda is named ``Set`` and it
  12. denotes the universe of small types. But for some applications, other
  13. sorts are needed. This page explains the need for additional sorts and
  14. describes all the sorts that are used by Agda.
  15. ..
  16. ::
  17. module Monomorphic where
  18. Introduction to universes
  19. =========================
  20. Russell's paradox implies that the collection of all sets is not
  21. itself a set. Namely, if there were such a set ``U``, then one could
  22. form the subset ``A ⊆ U`` of all sets that do not contain
  23. themselves. Then we would have ``A ∈ A`` if and only if ``A ∉ A``, a
  24. contradiction.
  25. Likewise, Martin-Löf’s type theory had originally a rule ``Set : Set``
  26. but Girard showed that it is inconsistent. This result is known as
  27. Girard’s paradox. Hence, not every Agda type is a ``Set``. For
  28. example, we have
  29. .. code-block:: agda
  30. Bool : Set
  31. Nat : Set
  32. but not ``Set : Set``. However, it is often convenient for ``Set`` to
  33. have a type of its own, and so in Agda, it is given the type ``Set₁``:
  34. .. code-block:: agda
  35. Set : Set₁
  36. In many ways, expressions of type ``Set₁`` behave just like
  37. expressions of type ``Set``; for example, they can be used as types of
  38. other things. However, the elements of ``Set₁`` are potentially
  39. *larger*; when ``A : Set₁``, then ``A`` is sometimes called a **large
  40. set**. In turn, we have
  41. .. code-block:: agda
  42. Set₁ : Set₂
  43. Set₂ : Set₃
  44. and so on. A type whose elements are types is called a **sort** or a
  45. **universe**; Agda provides an infinite number of universes ``Set``,
  46. ``Set₁``, ``Set₂``, ``Set₃``, ..., each of which is an element of the
  47. next one. In fact, ``Set`` itself is just an abbreviation for
  48. ``Set₀``. The subscript ``n`` is called the **level** of the universe
  49. ``Setₙ``.
  50. .. note:: You can also write ``Set1``, ``Set2``, etc., instead of
  51. ``Set₁``, ``Set₂``. To enter a subscript in the Emacs mode, type
  52. "``\_1``".
  53. Universe example
  54. ----------------
  55. So why are universes useful? Because sometimes it is necessary to
  56. define, and prove theorems about, functions that operate not just on
  57. sets but on large sets. In fact, most Agda users sooner or later
  58. experience an error message where Agda complains that ``Set₁ !=
  59. Set``. These errors usually mean that a small set was used where a
  60. large one was expected, or vice versa.
  61. For example, suppose you have defined the usual datatypes for lists
  62. and cartesian products:
  63. ::
  64. data List (A : Set) : Set where
  65. [] : List A
  66. _::_ : A → List A → List A
  67. data _×_ (A B : Set) : Set where
  68. _,_ : A → B → A × B
  69. infixr 5 _::_
  70. infixr 4 _,_
  71. infixr 2 _×_
  72. Now suppose you would like to define an operator ``Prod`` that inputs
  73. a list of ``n`` sets and takes their cartesian product, like this:
  74. .. code-block:: agda
  75. Prod (A :: B :: C :: []) = A × B × C
  76. There is only one small problem with this definition. The type of
  77. ``Prod`` should be
  78. .. code-block:: agda
  79. Prod : List Set → Set
  80. However, the definition of ``List A`` specified that ``A`` had to be a
  81. ``Set``. Therefore, ``List Set`` is not a valid type. The solution is
  82. to define a special version of the ``List`` operator that works for
  83. large sets:
  84. ::
  85. data List₁ (A : Set₁) : Set₁ where
  86. [] : List₁ A
  87. _::_ : A → List₁ A → List₁ A
  88. With this, we can indeed define:
  89. ::
  90. Prod : List₁ Set → Set
  91. Prod [] = ⊤
  92. Prod (A :: As) = A × Prod As
  93. Universe polymorphism
  94. ---------------------
  95. To allow definitions of functions and datatypes that work for all
  96. possible universes ``Setᵢ``, Agda provides a type ``Level`` of
  97. universe levels and level-polymorphic universes ``Set ℓ`` where ``ℓ :
  98. Level``. For more information, see the page on :ref:`universe levels
  99. <universe-levels>`.
  100. Agda's sort system
  101. ==================
  102. The implementation of Agda’s sort system is closely based on the
  103. theory of pure type systems. The full sort system of Agda consists of
  104. the following sorts:
  105. - ``Setᵢ`` and its universe-polymorphic variant ``Set ℓ``
  106. - ``Propᵢ`` and its universe-polymorphic variant ``Prop ℓ``
  107. - ``Setωᵢ``
  108. Sorts ``Setᵢ`` and ``Set ℓ``
  109. ----------------------------
  110. As explained in the introduction, Agda has a hierarchy of sorts ``Setᵢ
  111. : Setᵢ₊₁``, where ``i`` is any concrete natural number, i.e. ``0``,
  112. ``1``, ``2``, ``3``, ... The sort ``Set`` is an abbreviation for
  113. ``Set₀``.
  114. You can also refer to these sorts with the alternative syntax
  115. ``Seti``. That means that you can also write ``Set0``, ``Set1``,
  116. ``Set2``, etc., instead of ``Set₀``, ``Set₁``, ``Set₂``.
  117. In addition, Agda supports the universe-polymorphic version ``Set ℓ``
  118. where ``ℓ : Level`` (see :ref:`universe levels <universe-levels>`).
  119. Sorts ``Propᵢ`` and ``Prop ℓ``
  120. ------------------------------
  121. In addition to the hierarchy ``Setᵢ``, Agda also supports a second
  122. hierarchy ``Propᵢ : Setᵢ₊₁`` (or ``Propi``) of :ref:`proof-irrelevant
  123. propositions <prop>`. Like ``Set``, ``Prop`` also has a
  124. universe-polymorphic version ``Prop ℓ`` where ``ℓ : Level``.
  125. .. _set-omega-plus-n:
  126. Sorts ``Setωᵢ``
  127. ---------------
  128. To assign a sort to types such as ``(ℓ : Level) → Set ℓ``, Agda
  129. further supports an additional sort ``Setω`` that stands above all
  130. sorts ``Setᵢ``.
  131. Just as for ``Set`` and ``Prop``, ``Setω`` is the lowest level at an
  132. infinite hierarchy ``Setωᵢ : Setωᵢ₊₁`` where ``Setω = Setω₀``. You can
  133. also refer to these sorts with the alternative syntax ``Setωi``. That
  134. means that you can also write ``Setω0``, ``Setω1``, ``Setω2``, etc.,
  135. instead of ``Setω₀``, ``Setω₁``, ``Setω₂``.
  136. Now it is allowed, for instance, to declare a datatype in ``Setω``.
  137. This means that ``Setω`` before the implementation of this hierarchy,
  138. ``Setω`` used to be a term, and there was no bigger sort that it in
  139. Agda. Now a type can be assigned to it, in this case, ``Setω₁``.
  140. However, unlike the standard hierarchy of universes ``Setᵢ``, this
  141. second hierarchy ``Setωᵢ`` does not support universe
  142. polymorphism. This means that it is not possible to quantify over
  143. *all* Setωᵢ at once. For example, the expression ``∀ {i} (A : Setω i)
  144. → A → A`` would not be a well-formed agda term. See the section
  145. on ``Setω`` on the page on :ref:`universe levels <set-omega>` for more
  146. information.
  147. Concerning other applications, It should not be necessary to refer to
  148. these sorts during normal usage of Agda, but they might be useful for
  149. defining :ref:`reflection-based macros <macros>`.
  150. .. note:: When ``--omega-in-omega`` is enabled, ``Setωᵢ`` is
  151. considered to be equal to ``Setω`` for all ``i`` (thus rendering
  152. Agda inconsistent).
  153. Sort metavariables and unknown sorts
  154. ====================================
  155. Under universe polymorphism, levels can be arbitrary terms, e.g., a
  156. level that contains free variables. Sometimes, we will have to check
  157. that some expression has a valid type without knowing what sort it has.
  158. For this reason, Agda’s internal representation of sorts implements a constructor (sort
  159. metavariable) representing an unknown sort. The constraint solver can
  160. compute these sort metavariables, just like it does when computing
  161. regular term metavariables.
  162. However, the presence of sort metavariables also means that sorts of
  163. other types can sometimes not be computed directly. For this reason,
  164. Agda's internal representation of sorts includes three additional
  165. constructors ``funSort``, ``univSort``, and ``piSort``. These
  166. constructors compute to the proper sort once enough metavariables in
  167. their arguments have been solved.
  168. .. note::
  169. ``funSort``, ``univSort`` and ``piSort`` are *internal* constructors
  170. that may be printed when evaluating a term. The user can not enter
  171. them, nor introduce them in agda code. All these constructors do
  172. not represent new sorts but instead, they compute to the right sort
  173. once their arguments are known.
  174. .. _funSort:
  175. funSort
  176. -------
  177. The constructor ``funSort`` computes the sort of a function type
  178. even if the sort of the domain and the sort of the codomain are still
  179. unknown.
  180. To understand how ``funSort`` works in general, let us assume the following
  181. scenario:
  182. * ``sA`` and ``sB`` are two (possibly different) sorts.
  183. * ``A : sA``, meaning that ``A`` is a type that has sort ``sA``.
  184. * ``B : sB``, meaning that ``B`` is a (possibly different) type that has
  185. sort ``sB``.
  186. Under these conditions, we can build the function type
  187. ``A → B : funSort sA sB``. This type signature means that the function type
  188. ``A → B`` has a (possibly unknown) but well-defined sort ``funSort sA sB``,
  189. specified in terms of the sorts of its domain and codomain.
  190. If ``sA`` and ``sB`` happen to be known, then ``funSort sA sB`` can be computed
  191. to a sort value. We list below all the possible computations that ``funSort``
  192. can perform:
  193. .. code-block:: agda
  194. funSort Setωᵢ Setωⱼ = Setωₖ (where k = max(i,j))
  195. funSort Setωᵢ (Set b) = Setωᵢ
  196. funSort Setωᵢ (Prop b) = Setωᵢ
  197. funSort (Set a) Setωⱼ = Setωⱼ
  198. funSort (Prop a) Setωⱼ = Setωⱼ
  199. funSort (Set a) (Set b) = Set (a ⊔ b)
  200. funSort (Prop a) (Set b) = Set (a ⊔ b)
  201. funSort (Set a) (Prop b) = Prop (a ⊔ b)
  202. funSort (Prop a) (Prop b) = Prop (a ⊔ b)
  203. Example: the sort of the function type ``∀ {A} → A → A`` with normal form
  204. ``{A : _5} → A → A`` evaluates to ``funSort (univSort _5) (funSort _5 _5)``
  205. where:
  206. * ``_5`` is a metavariable that represents the sort of ``A``.
  207. * ``funSort _5 _5`` is the sort of ``A → A``.
  208. .. note:: ``funSort`` can admit just two arguments, so it will be
  209. iterated when the function type has multiple arguments. E.g. the
  210. function type ``∀ {A} → A → A → A`` evaluates to ``funSort (univSort
  211. _5) (funSort _5 (funSort _5 _5))``
  212. .. _univSort: agda
  213. univSort
  214. --------
  215. ``univSort`` returns the successor sort of a given sort.
  216. Example: the sort of the function type ``∀ {A} → A`` with normal form
  217. ``{A : _5} → A`` evaluates to ``funSort (univSort _5) _5`` where:
  218. * ``univSort _5`` is the sort where the sort of ``A`` lives, ie. the
  219. successor level of ``_5``.
  220. We list below all the possible computations that ``univSort`` can perform:
  221. .. code-block:: agda
  222. univSort (Set a) = Set (lsuc a)
  223. univSort (Prop a) = Set (lsuc a)
  224. univSort Setωᵢ = Setωᵢ₊₁
  225. .. _piSort:
  226. piSort
  227. ------
  228. Similarly, ``piSort s1 s2`` is a constructor that computes the sort of
  229. a Π-type given the sort ``s1`` of its domain and the sort ``s2`` of its
  230. codomain as arguments.
  231. To understand how ``piSort`` works in general, we set the following scenario:
  232. * ``sA`` and ``sB`` are two (possibly different) sorts.
  233. * ``A : sA``, meaning that ``A`` is a type that has sort ``sA``.
  234. * ``x : A``, meaning that ``x`` has type ``A``.
  235. * ``B : sB``, meaning that ``B`` is a type (possibly different than ``A``) that
  236. has sort ``sB``.
  237. Under these conditions, we can build the dependent function type
  238. ``(x : A) → B : piSort sA (λ x → sB)``. This type signature means that the
  239. dependent function type ``(x : A) → B`` has a (possibly unknown) but
  240. well-defined sort ``piSort sA sB``, specified in terms of the element
  241. ``x : A`` and the sorts of its domain and codomain.
  242. We list below all the possible computations that ``piSort`` can perform:
  243. .. code-block:: agda
  244. piSort s1 (λ x → s2) = funSort s1 s2 (if x does not occur freely in s2)
  245. piSort (Set ℓ) (λ x → s2) = Setω (if x occurs rigidly in s2)
  246. piSort (Prop ℓ) (λ x → s2) = Setω (if x occurs rigidly in s2)
  247. piSort Setωᵢ (λ x → s2) = Setωᵢ (if x occurs rigidly in s2)
  248. With these rules, we can compute the sort of the function type ``∀ {A}
  249. → ∀ {B} → B → A → B`` (or more explicitly, ``{A : _9} {B : _7} → B → A
  250. → B``) to be ``piSort (univSort _9) (λ A → funSort (univSort _7)
  251. (funSort _7 (funSort _9 _7)))``
  252. More examples:
  253. * ``piSort Level (λ l → Set l)`` evaluates to ``Setω``
  254. * ``piSort (Set l) (λ _ → Set l')`` evaluates to ``Set (l ⊔ l')``
  255. * ``univSort (Set l)`` evaluates to ``Set (lsuc l)``
  256. * ``piSort s (λ x -> Setωi)`` evaluates to ``funSort s Setω``