cumulativity.lagda.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ************
  2. Cumulativity
  3. ************
  4. Basics
  5. ------
  6. Since version 2.6.1, Agda supports optional cumulativity of universes
  7. under the ``--cumulativity`` flag.
  8. .. code-block:: agda
  9. {-# OPTIONS --cumulativity #-}
  10. When the ``--cumulativity`` flag is enabled, Agda uses the subtyping
  11. rule ``Set i =< Set j`` whenever ``i =< j``. For example, in addition
  12. to its usual type ``Set``, ``Nat`` also has the type ``Set₁`` and even
  13. ``Set i`` for any ``i : Level``.
  14. ..
  15. ::
  16. module language.cumulativity where
  17. open import Agda.Primitive
  18. open import Agda.Builtin.Nat
  19. variable
  20. ℓ ℓ₁ ℓ₂ : Level
  21. A B C : Set ℓ
  22. n : Nat
  23. data Vec (A : Set ℓ) : Nat → Set ℓ where
  24. [] : Vec A 0
  25. _∷_ : A → Vec A n → Vec A (suc n)
  26. .. code-block:: agda
  27. _ : Set
  28. _ = Nat
  29. _ : Set₁
  30. _ = Nat
  31. _ : ∀ {i} → Set i
  32. _ = Nat
  33. With cumulativity is enabled, one can implement lifting to a higher
  34. universe as the identity function.
  35. .. code-block:: agda
  36. lift : ∀ {a b} → Set a → Set (a ⊔ b)
  37. lift x = x
  38. Example usage: N-ary functions
  39. ------------------------------
  40. In Agda without cumulativity, it is tricky to define a
  41. universe-polymorphic N-ary function type ``A → A → ... → A → B``
  42. because the universe level depends on whether the number of arguments
  43. is zero:
  44. .. code-block:: agda
  45. module Without-Cumulativity where
  46. N-ary-level : Level → Level → Nat → Level
  47. N-ary-level ℓ₁ ℓ₂ zero = ℓ₂
  48. N-ary-level ℓ₁ ℓ₂ (suc n) = ℓ₁ ⊔ N-ary-level ℓ₁ ℓ₂ n
  49. N-ary : ∀ {ℓ₁ ℓ₂} n → Set ℓ₁ → Set ℓ₂ → Set (N-ary-level ℓ₁ ℓ₂ n)
  50. N-ary zero A B = B
  51. N-ary (suc n) A B = A → N-ary n A B
  52. In contrast, in Agda with cumulativity one can always work with the
  53. highest possible universe level. This makes it much easier to define
  54. the type of N-ary functions.
  55. .. code-block:: agda
  56. module With-Cumulativity where
  57. N-ary : Nat → Set ℓ₁ → Set ℓ₂ → Set (ℓ₁ ⊔ ℓ₂)
  58. N-ary zero A B = B
  59. N-ary (suc n) A B = A → N-ary n A B
  60. curryⁿ : (Vec A n → B) → N-ary n A B
  61. curryⁿ {n = zero} f = f []
  62. curryⁿ {n = suc n} f = λ x → curryⁿ λ xs → f (x ∷ xs)
  63. _$ⁿ_ : N-ary n A B → (Vec A n → B)
  64. f $ⁿ [] = f
  65. f $ⁿ (x ∷ xs) = f x $ⁿ xs
  66. ∀ⁿ : ∀ {A : Set ℓ₁} n → N-ary n A (Set ℓ₂) → Set (ℓ₁ ⊔ ℓ₂)
  67. ∀ⁿ zero P = P
  68. ∀ⁿ (suc n) P = ∀ x → ∀ⁿ n (P x)
  69. Limitations
  70. -----------
  71. Currently cumulativity only enables subtyping between universes, but
  72. not between any other types containing universes. For example, ``List
  73. Set`` is not a subtype of ``List Set₁``. Agda also does not have
  74. cumulativity for any other types containing universe levels, so ``List
  75. {lzero} Nat`` is not a subtype of ``List {lsuc lzero} Nat``. Such
  76. rules might be added in a future version of Agda.
  77. Constraint solving
  78. ------------------
  79. When working in Agda with cumulativity, universe level metavariables
  80. are often underconstrained. For example, the expression ``List Nat``
  81. could mean ``List {lzero} Nat``, but also ``List {lsuc lzero} Nat``,
  82. or indeed ``List {i} Nat`` for any ``i : Level``.
  83. Currently Agda uses the following heuristic to instantiate universe
  84. level metavariables. At the end of each type signature, each mutual
  85. block, or declaration that is not part of a mutual block, Agda
  86. instantiates all universe level metavariables that are *unbounded from
  87. above*. A metavariable ``_l : Level`` is unbounded from above if all
  88. unsolved constraints that mention the metavariable are of the form
  89. ``aᵢ =< _l : Level``, and ``_l`` does not occur in the type of any
  90. other unsolved metavariables. For each metavariable that satisfies
  91. these conditions, it is instantiated to ``a₁ ⊔ a₂ ⊔ ... ⊔ aₙ`` where
  92. ``a₁ =< _l : Level``, ..., ``aₙ =< _l : Level`` are all constraints
  93. that mention `_l`.
  94. The heuristic as described above is considered experimental and is
  95. subject to change in future versions of Agda.