generalization-of-declared-variables.lagda.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ..
  2. ::
  3. {-# OPTIONS --allow-unsolved-metas #-}
  4. module language.generalization-of-declared-variables where
  5. open import Agda.Primitive
  6. open import Agda.Builtin.Equality
  7. open import Agda.Builtin.Nat
  8. open import Agda.Builtin.Bool
  9. .. _generalization-of-declared-variables:
  10. ************************************
  11. Generalization of Declared Variables
  12. ************************************
  13. .. contents::
  14. :depth: 1
  15. :local:
  16. Overview
  17. ~~~~~~~~
  18. Since version 2.6.0, Agda supports implicit generalization over variables in types.
  19. Variables to be generalized over must be declared with their types in a ``variable``
  20. block. For example:
  21. ::
  22. variable
  23. ℓ : Level
  24. n m : Nat
  25. data Vec (A : Set ℓ) : Nat → Set ℓ where
  26. [] : Vec A 0
  27. _∷_ : A → Vec A n → Vec A (suc n)
  28. Here the parameter ``ℓ`` and the ``n`` in the type of ``_∷_`` are not bound explicitly,
  29. but since they are declared as generalizable variables, bindings for them are inserted
  30. automatically. The level ``ℓ`` is added as a parameter to the datatype and ``n`` is added
  31. as an argument to ``_∷_``. The resulting declaration is
  32. .. code-block:: agda
  33. data Vec {ℓ : Level} (A : Set ℓ) : Nat → Set ℓ where
  34. [] : Vec A 0
  35. _∷_ : {n : Nat} → A → Vec A n → Vec A (suc n)
  36. See `Placement of generalized bindings`_ below for more details on where bindings
  37. are inserted.
  38. Variables are generalized in top-level type signatures, module telescopes, and record
  39. and datatype parameter telescopes.
  40. Issues related to this feature are marked with
  41. `generalize <https://github.com/agda/agda/labels/generalize>`_ in the issue
  42. tracker.
  43. Nested generalization
  44. ~~~~~~~~~~~~~~~~~~~~~
  45. ..
  46. ::
  47. module _ where
  48. When generalizing a variable, any generalizable variables in its type are also generalized
  49. over. For instance, you can declare ``A`` to be a type at some level ``ℓ`` as
  50. ::
  51. variable
  52. A : Set ℓ
  53. Now if ``A`` is mentioned in a type, the level ``ℓ`` will also be generalized over::
  54. -- id : {A.ℓ : Level} {A : Set ℓ} → A → A
  55. id : A → A
  56. id x = x
  57. The nesting can be arbitrarily deep, so
  58. ::
  59. variable
  60. x : A
  61. refl′ : x ≡ x
  62. refl′ = refl
  63. expands to
  64. .. code-block:: agda
  65. refl′ : {x.A.ℓ : Level} {x.A : Set x.A.ℓ} {x : x.A} → x ≡ x
  66. See `Naming of nested variables`_ below for how the names are chosen.
  67. Nested variables are not necessarily generalized over. In this example, if the universe
  68. level of ``A`` is fixed there is nothing to generalize::
  69. postulate
  70. -- pure : {A : Set} {F : Set → Set} → A → F A
  71. pure : {F : Set → Set} → A → F A
  72. See `Generalization over unsolved metavariables`_ for more details.
  73. .. note::
  74. Nested generalized variables are local to each variable, so if you declare
  75. ::
  76. variable
  77. B : Set ℓ
  78. then ``A`` and ``B`` can still be generalized at different levels. For instance,
  79. ::
  80. -- _$_ : {A.ℓ : Level} {A : Set A.ℓ} {B.ℓ : Level} {B : Set B.ℓ} → (A → B) → A → B
  81. _$_ : (A → B) → A → B
  82. f $ x = f x
  83. Generalization over unsolved metavariables
  84. ------------------------------------------
  85. Generalization over nested variables is implemented by creating a metavariable for each
  86. nested variable and generalize over any such meta that is still unsolved after type
  87. checking. This is what makes the ``pure`` example from the previous section work: the
  88. metavariable created for ``ℓ`` is solved to level 0 and is thus not generalized over.
  89. A typical case where this happens is when you have dependencies between different nested
  90. variables. For instance::
  91. postulate
  92. Con : Set
  93. variable
  94. Γ Δ Θ : Con
  95. postulate
  96. Sub : Con → Con → Set
  97. idS : Sub Γ Γ
  98. _∘_ : Sub Γ Δ → Sub Δ Θ → Sub Γ Θ
  99. variable
  100. δ σ γ : Sub Γ Δ
  101. postulate
  102. assoc : δ ∘ (σ ∘ γ) ≡ (δ ∘ σ) ∘ γ
  103. In the type of ``assoc`` each substitution gets two nested variable metas for their contexts,
  104. but the type of ``_∘_`` requires the contexts of its arguments to match up, so some of
  105. these metavariables are solved. The resulting type is
  106. .. code-block:: agda
  107. assoc : {δ.Γ δ.Δ : Con} {δ : Sub δ.Γ δ.Δ} {σ.Δ : Con} {σ : Sub δ.Δ σ.Δ}
  108. {γ.Δ : Con} {γ : Sub σ.Δ γ.Δ} → (δ ∘ (σ ∘ γ)) ≡ ((δ ∘ σ) ∘ γ)
  109. where we can see from the names that ``σ.Γ`` was unified with ``δ.Δ`` and ``γ.Γ`` with
  110. ``σ.Δ``. In general, when unifying two metavariables the "youngest" one is eliminated which
  111. is why ``δ.Δ`` and ``σ.Δ`` are the ones that remain in the type.
  112. If a metavariable for a nested generalizable variable is partially solved, the left-over
  113. metas are generalized over. For instance,
  114. ..
  115. ::
  116. sum : Vec Nat n → Nat
  117. sum [] = 0
  118. sum (x ∷ xs) = x + sum xs
  119. ::
  120. variable
  121. xs : Vec A n
  122. head : Vec A (suc n) → A
  123. head (x ∷ _) = x
  124. -- lemma : {xs.n.1 : Nat} {xs : Vec Nat (suc xs.n.1)} → head xs ≡ 1 → (0 < sum xs) ≡ true
  125. lemma : head xs ≡ 1 → (0 < sum xs) ≡ true
  126. ..
  127. ::
  128. lemma {xs = x ∷ _} refl = refl
  129. In the type of ``lemma`` a metavariable is created for the length of ``xs``, which
  130. the application ``head xs`` refines to ``suc _n``, for some new metavariable ``_n``.
  131. Since there are no further constraints on ``_n``, it's generalized over, creating the
  132. type given in the comment. See :ref:`Naming of nested variables
  133. <naming-of-nested-variables>` below for how the name ``xs.n.1`` is chosen.
  134. .. _note-free-metas:
  135. .. note::
  136. Only metavariables originating from nested variables are generalized over. An exception
  137. to this is in ``variable`` blocks where all unsolved metas are turned into nested variables.
  138. This means writing
  139. .. code-block:: agda
  140. variable
  141. A : Set _
  142. is equivalent to ``A : Set ℓ`` up to naming of the nested variable (see below).
  143. .. _naming-of-nested-variables:
  144. Naming of nested variables
  145. --------------------------
  146. The general naming scheme for nested generalized variables is
  147. ``parentVar.nestedVar``. So, in the case of the identity function
  148. ``id : A → A`` expanding to
  149. .. code-block:: agda
  150. id : {A.ℓ : Level} {A : Set ℓ} → A → A
  151. the name of the level variable is ``A.ℓ`` since the name of the nested variable is
  152. ``ℓ`` and its parent is the named variable ``A``. For multiple levels of nesting the
  153. parent can be another nested variable as in the ``refl′`` case above
  154. .. code-block:: agda
  155. refl′ : {x.A.ℓ : Level} {x.A : Set x.A.ℓ} {x : x.A} → x ≡ x
  156. If a nested generalizable variable is solved with a term containing
  157. further metas, these are generalized over as explained in the ``lemma`` example
  158. above. The names of the new variables are of the form ``parentName.i`` where
  159. ``parentName`` is the name of the solved variable and ``i`` numbers the metas,
  160. starting from 1, in the order they appear in the solution.
  161. If a variable comes from a free unsolved metavariable in a ``variable`` block
  162. (see `this note <note-free-metas_>`_), its name is chosen as follows:
  163. - If it is a labelled argument to a function, the label is used as the name,
  164. - otherwise the name is its left-to-right index (starting at 1) in the list of unnamed
  165. variables in the type.
  166. It is then given a hierarchical name based on the named variable whose type it occurs
  167. in. For example,
  168. ::
  169. postulate
  170. V : (A : Set) → Nat → Set
  171. P : V A n → Set
  172. variable
  173. v : V _ _
  174. postulate
  175. thm : P v
  176. Here there are two unnamed variables in the type of ``v``, namely the two arguments to ``V``.
  177. The first argument has the label ``A`` in the definition of ``V``, so this variable gets the name
  178. ``v.A``. The second argument has no label and thus gets the name ``v.2`` since it is the second
  179. unnamed variable in the type of ``v``.
  180. If the variable comes from a partially instantiated nested variable the name of the metavariable
  181. is used unqualified.
  182. .. note::
  183. Currently it is not allowed to use hierarchical names when giving parameters
  184. to functions, see `Issue #3208 <issue-3280_>`_.
  185. .. _issue-3280: https://github.com/agda/agda/issues/3280
  186. Placement of generalized bindings
  187. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. The following rules are used to place generalized variables:
  189. - Generalized variables are placed at the front of the type signature or telescope.
  190. - Type signatures appearing inside other type signatures, for instance in let bindings or
  191. dependent function arguments are not generalized. Instead any generalizable variables
  192. in such types are generalized over in the parent signature.
  193. - Variables mentioned eariler are placed before variables mentioned later, where
  194. nested variables count as being mentioned together with their parent.
  195. .. note::
  196. This means that an implicitly quantified variable cannot depend on an explicitly
  197. quantified one. See `Issue #3352 <https://github.com/agda/agda/issues/3352>`_ for
  198. the feature request to lift this restriction.
  199. Indexed datatypes
  200. -----------------
  201. When generalizing datatype parameters and indicies a variable is turned into
  202. an index if it is only mentioned in indices and into a parameter otherwise.
  203. For instance,
  204. ..
  205. ::
  206. module Vectors where
  207. ::
  208. data All (P : A → Set) : Vec A n → Set where
  209. [] : All P []
  210. _∷_ : P x → All P xs → All P (x ∷ xs)
  211. Here ``A`` is generalized as a parameter and ``n`` as an index. That is, the
  212. resulting signature is
  213. .. code-block:: agda
  214. data All {A : Set} (P : A → Set) : {n : Nat} → Vec A n → Set where
  215. Instance and irrelevant variables
  216. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217. Generalized variables are introduced as implicit arguments by default, but this can be
  218. changed to :ref:`instance arguments <instance-arguments>` or
  219. :ref:`irrelevant arguments <irrelevance>` by annotating the declaration of the variable::
  220. record Eq (A : Set) : Set where
  221. field eq : A → A → Bool
  222. variable
  223. {{EqA}} : Eq A -- generalized as an instance argument
  224. .ignore : A -- generalized as an irrelevant (implicit) argument
  225. Variables are never generalized as explicit arguments.
  226. Importing and exporting variables
  227. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228. Generalizable variables are treated in the same way as other declared symbols
  229. (functions, datatypes, etc) and use the same mechanisms for importing and exporting
  230. between modules. This means that unless marked ``private`` they are exported from a
  231. module.
  232. Interaction
  233. ~~~~~~~~~~~
  234. When developing types interactively, generalizable variables can be used in holes if
  235. they have already been generalized, but it is not possible to introduce `new`
  236. generalizations interactively. For instance,
  237. ..
  238. ::
  239. map : (A → B) → Vec A n → Vec B n
  240. map f [] = []
  241. map f (x ∷ xs) = f x ∷ map f xs
  242. ::
  243. works : (A → B) → Vec A n → Vec B {!n!}
  244. fails : (A → B) → Vec A {!n!} → Vec B {!n!}
  245. ..
  246. ::
  247. works = map
  248. fails = map
  249. In ``works`` you can give ``n`` in the hole, since a binding for ``n`` has been introduced
  250. by its occurrence in the argument vector. In ``fails`` on the other hand, there is no reference
  251. to ``n`` so neither hole can be filled interactively.
  252. Modalities
  253. ~~~~~~~~~~
  254. One can give a modality when declaring a generalizable variable:
  255. ::
  256. variable
  257. @0 o : Nat
  258. In the generalization process generalizable variables get the modality
  259. that they are declared with, whereas other variables always get the
  260. default modality.