implicit-arguments.lagda.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. ..
  2. ::
  3. {-# OPTIONS --allow-unsolved-metas #-}
  4. module language.implicit-arguments (A B : Set) (C : A → Set) where
  5. open import Agda.Builtin.Equality
  6. open import Agda.Builtin.Unit using (⊤)
  7. open import Agda.Builtin.Nat using (Nat; zero; suc)
  8. _is-the-same-as_ = _≡_
  9. .. _implicit-arguments:
  10. ******************
  11. Implicit Arguments
  12. ******************
  13. It is possible to omit terms that the type checker can figure out for
  14. itself, replacing them by ``_``.
  15. If the type checker cannot infer the value of an ``_`` it will report
  16. an error.
  17. For instance, for the polymorphic identity function
  18. ..
  19. ::
  20. module example₁ where
  21. postulate
  22. ::
  23. id : (A : Set) → A → A
  24. the first argument can be inferred from the type of the second argument,
  25. so we might write ``id _ zero`` for the application of the identity function to ``zero``.
  26. We can even write this function application without the first argument.
  27. In that case we declare an implicit function space:
  28. ..
  29. ::
  30. module example₂ where
  31. postulate
  32. ::
  33. id : {A : Set} → A → A
  34. and then we can use the notation ``id zero``.
  35. Another example:
  36. ..
  37. ::
  38. postulate
  39. ::
  40. _==_ : {A : Set} → A → A → Set
  41. subst : {A : Set} (C : A → Set) {x y : A} → x == y → C x → C y
  42. Note how the first argument to ``_==_`` is left implicit.
  43. Similarly, we may leave out the implicit arguments ``A``, ``x``, and ``y`` in an
  44. application of ``subst``.
  45. To give an implicit argument explicitly, enclose it in curly braces.
  46. The following two expressions are equivalent:
  47. ..
  48. ::
  49. module example₄ (x y : A) (eq : x == y) (cx : C x) where
  50. ::
  51. x1 = subst C eq cx
  52. x2 = subst {_} C {_} {_} eq cx
  53. ..
  54. ::
  55. prop-hidden : x1 is-the-same-as x2
  56. prop-hidden = refl
  57. It is worth noting that implicit arguments are also inserted at the end of an application,
  58. if it is required by the type.
  59. For example, in the following, ``y1`` and ``y2`` are equivalent.
  60. ..
  61. ::
  62. module example₅ (a b : A ) where
  63. ::
  64. y1 : a == b → C a → C b
  65. y1 = subst C
  66. y2 : a == b → C a → C b
  67. y2 = subst C {_} {_}
  68. ..
  69. ::
  70. prop-hidden : y1 is-the-same-as y2
  71. prop-hidden = refl
  72. Implicit arguments are inserted eagerly in left-hand sides so ``y3`` and ``y4``
  73. are equivalent. An exception is when no type signature is given, in which case
  74. no implicit argument insertion takes place. Thus in the definition of ``y5``
  75. the only implicit is the ``A`` argument of ``subst``.
  76. ::
  77. y3 : {x y : A} → x == y → C x → C y
  78. y3 = subst C
  79. y4 : {x y : A} → x == y → C x → C y
  80. y4 {x} {y} = subst C {_} {_}
  81. y5 = subst C
  82. ..
  83. ::
  84. prop-hidden₅ : y3 is-the-same-as y4
  85. prop-hidden₅ = refl
  86. prop-hidden₆ : y4 is-the-same-as y5
  87. prop-hidden₆ = refl
  88. It is also possible to write lambda abstractions with implicit arguments. For
  89. example, given ``id : (A : Set) → A → A``, we can define the identity function with
  90. implicit type argument as
  91. ..
  92. ::
  93. postulate id : (A : Set) → A → A
  94. ::
  95. id’ = λ {A} → id A
  96. Implicit arguments can also be referred to by name,
  97. so if we want to give the expression ``e`` explicitly for ``y``
  98. without giving a value for ``x`` we can write
  99. ..
  100. ::
  101. module example₆ (x : A) (e : A) (eq : x == e) (cx : C x) where
  102. y6 =
  103. ::
  104. subst C {y = e} eq cx
  105. In rare circumstances it can be useful to separate the name used to give an
  106. argument by name from the name of the bound variable, for instance if the desired
  107. name shadows an existing name. To do this you write
  108. ::
  109. id₂ : {A = X : Set} → X → X -- name of bound variable is X
  110. id₂ x = x
  111. use-id₂ : (Y : Set) → Y → Y
  112. use-id₂ Y = id₂ {A = Y} -- but the label is A
  113. Labeled bindings must appear by themselves when typed, so the type ``Set`` needs to
  114. be repeated in this example:
  115. ::
  116. const : {A = X : Set} {B = Y : Set} → A → B → A
  117. const x y = x
  118. When constructing implicit function spaces the implicit argument can be omitted,
  119. so both expressions below are valid expressions of type ``{A : Set} → A → A``:
  120. ::
  121. z1 = λ {A} x → x
  122. z2 = λ x → x
  123. ..
  124. ::
  125. postulate P : ({A : Set} → A → A) → Set
  126. postulate P₁ : P z1
  127. postulate P₂ : P z2
  128. The ``∀`` (or ``forall``) syntax for function types also has implicit variants:
  129. ::
  130. ① : (∀ {x : A} → B) is-the-same-as ({x : A} → B)
  131. ② : (∀ {x} → B) is-the-same-as ({x : _} → B)
  132. ③ : (∀ {x y} → B) is-the-same-as (∀ {x} → ∀ {y} → B)
  133. ..
  134. ::
  135. ① = refl
  136. ② = refl
  137. ③ = refl
  138. In very special situations it makes sense to declare *unnamed* hidden arguments
  139. ``{A} → B``. In the following ``example``, the hidden argument to ``scons`` of type
  140. ``zero ≤ zero`` can be solved by η-expansion, since this type reduces to ``⊤``.
  141. ..
  142. ::
  143. module UnnamedImplicit where
  144. ::
  145. data ⊥ : Set where
  146. _≤_ : Nat → Nat → Set
  147. zero ≤ _ = ⊤
  148. suc m ≤ zero = ⊥
  149. suc m ≤ suc n = m ≤ n
  150. data SList (bound : Nat) : Set where
  151. [] : SList bound
  152. scons : (head : Nat) → {head ≤ bound} → (tail : SList head) → SList bound
  153. example : SList zero
  154. example = scons zero []
  155. There are no restrictions on when a function space can be implicit.
  156. Internally, explicit and implicit function spaces are treated in the same way.
  157. This means that there are no guarantees that implicit arguments will be solved.
  158. When there are unsolved implicit arguments the type checker will give
  159. an error message indicating which application contains the unsolved
  160. arguments.
  161. The reason for this liberal approach to implicit arguments is that
  162. limiting the use of implicit argument to the cases where we guarantee
  163. that they are solved rules out many useful cases in practice.
  164. .. _tactic_arguments:
  165. Tactic arguments
  166. ----------------
  167. ..
  168. ::
  169. open import Agda.Builtin.Reflection
  170. open import Agda.Builtin.Unit
  171. open import Agda.Builtin.Nat
  172. open import Agda.Builtin.List
  173. Proof = Nat
  174. Goal = Nat
  175. You can declare :ref:`tactics<reflection>` to be used to solve a particular implicit argument using
  176. the ``@(tactic t)`` attribute, where ``t : Term → TC ⊤``. For instance::
  177. clever-search : Term → TC ⊤
  178. clever-search hole = unify hole (lit (nat 17))
  179. the-best-number : {@(tactic clever-search) n : Nat} → Nat
  180. the-best-number {n} = n
  181. check : the-best-number ≡ 17
  182. check = refl
  183. The tactic can be an arbitrary term of the right type and may depend on previous arguments to the function::
  184. default : {A : Set} → A → Term → TC ⊤
  185. default x hole = bindTC (quoteTC x) (unify hole)
  186. search : (depth : Nat) → Term → TC ⊤
  187. example : {@(tactic default 10) depth : Nat}
  188. {@(tactic search depth) proof : Proof} →
  189. Goal
  190. ..
  191. ::
  192. search depth hole = unify hole (lit (nat depth))
  193. example {proof = p} = p
  194. check₁ : example ≡ 10
  195. check₁ = refl
  196. .. _metavariables:
  197. Metavariables
  198. -------------
  199. .. _unification:
  200. Unification
  201. -----------