function-definitions.lagda.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ..
  2. ::
  3. {-# OPTIONS --rewriting --sized-types #-}
  4. module language.function-definitions where
  5. open import language.built-ins
  6. .. _function-definitions:
  7. ********************
  8. Function Definitions
  9. ********************
  10. Introduction
  11. ============
  12. A function is defined by first declaring its type followed by a number of
  13. equations called *clauses*. Each clause consists of the function being defined
  14. applied to a number of *patterns*, followed by ``=`` and a term called the
  15. *right-hand side*. For example:
  16. ::
  17. not : Bool → Bool
  18. not true = false
  19. not false = true
  20. Functions are allowed to call themselves recursively, for example:
  21. ::
  22. twice : Nat → Nat
  23. twice zero = zero
  24. twice (suc n) = suc (suc (twice n))
  25. General form
  26. ============
  27. The general form for defining a function is
  28. .. code-block:: agda
  29. f : (x₁ : A₁) → … → (xₙ : Aₙ) → B
  30. f p₁ … pₙ = d
  31. f q₁ … qₙ = e
  32. where ``f`` is a new identifier, ``pᵢ`` and ``qᵢ`` are patterns of type ``Aᵢ``,
  33. and ``d`` and ``e`` are expressions.
  34. The declaration above gives the identifier ``f`` the type
  35. ``(x₁ : A₁) → … → (xₙ : Aₙ) → B`` and ``f`` is defined by the defining
  36. equations. Patterns are matched from top to bottom, i.e., the first pattern
  37. that matches the actual parameters is the one that is used.
  38. By default, Agda checks the following properties of a function definition:
  39. - The patterns in the left-hand side of each clause should consist only of
  40. constructors and variables.
  41. - No variable should occur more than once on the left-hand side of a single
  42. clause.
  43. - The patterns of all clauses should together cover all possible inputs of
  44. the function, see :ref:`coverage-checking`.
  45. - The function should be terminating on all possible inputs, see
  46. :ref:`termination-checking`.
  47. Special patterns
  48. ================
  49. In addition to constructors consisting of constructors and variables, Agda
  50. supports two special kinds of patterns: dot patterns and absurd patterns.
  51. .. _dot-patterns:
  52. Dot patterns
  53. ------------
  54. A dot pattern (also called *inaccessible pattern*) can be used when
  55. the only type-correct value of the argument is determined by the
  56. patterns given for the other arguments.
  57. The syntax for a dot pattern is ``.t``.
  58. As an example, consider the datatype ``Square`` defined as follows
  59. ::
  60. data Square : Nat → Set where
  61. sq : (m : Nat) → Square (m * m)
  62. Suppose we want to define a function ``root : (n : Nat) → Square n → Nat`` that
  63. takes as its arguments a number ``n`` and a proof that it is a square, and
  64. returns the square root of that number. We can do so as follows:
  65. ::
  66. root : (n : Nat) → Square n → Nat
  67. root .(m * m) (sq m) = m
  68. Notice that by matching on the argument of type ``Square n`` with the constructor
  69. ``sq : (m : Nat) → Square (m * m)``, ``n`` is forced to be equal to ``m * m``.
  70. In general, when matching on an argument of type ``D i₁ … iₙ`` with a constructor
  71. ``c : (x₁ : A₁) → … → (xₘ : Aₘ) → D j₁ … jₙ``, Agda will attempt to unify
  72. ``i₁ … iₙ`` with ``j₁ … jₙ``. When the unification algorithm instantiates a
  73. variable ``x`` with value ``t``, the corresponding argument of the function
  74. can be replaced by a dot pattern ``.t``. Using a dot pattern is optional, but
  75. can help readability. The following are also legal definitions of
  76. ``root``:
  77. Since Agda 2.4.2.4::
  78. root₁ : (n : Nat) → Square n → Nat
  79. root₁ _ (sq m) = m
  80. Since Agda 2.5.2::
  81. root₂ : (n : Nat) → Square n → Nat
  82. root₂ n (sq m) = m
  83. In the case of ``root₂``, ``n`` evaluates to ``m * m`` in the body of the
  84. function and is thus equivalent to
  85. ::
  86. root₃ : (n : Nat) → Square n → Nat
  87. root₃ _ (sq m) = let n = m * m in m
  88. .. _absurd-patterns:
  89. Absurd patterns
  90. ---------------
  91. Absurd patterns can be used when none of the constructors for a particular
  92. argument would be valid. The syntax for an absurd pattern is ``()``.
  93. As an example, if we have a datatype ``Even`` defined as follows
  94. ::
  95. data Even : Nat → Set where
  96. even-zero : Even zero
  97. even-plus2 : {n : Nat} → Even n → Even (suc (suc n))
  98. then we can define a function ``one-not-even : Even 1 → ⊥`` by using an absurd
  99. pattern:
  100. ::
  101. one-not-even : Even 1 → ⊥
  102. one-not-even ()
  103. Note that if the left-hand side of a clause contains an absurd pattern, its
  104. right-hand side must be omitted.
  105. In general, when matching on an argument of type ``D i₁ … iₙ`` with an absurd
  106. pattern, Agda will attempt for each constructor
  107. ``c : (x₁ : A₁) → … → (xₘ : Aₘ) → D j₁ … jₙ`` of the datatype ``D`` to unify
  108. ``i₁ … iₙ`` with ``j₁ … jₙ``. The absurd pattern will only be accepted if all
  109. of these unifications end in a conflict.
  110. As-patterns
  111. -----------
  112. As-patterns (or ``@-patterns``) can be used to name a pattern. The name has the
  113. same scope as normal pattern variables (i.e. the right-hand side, where clause,
  114. and dot patterns). The name reduces to the value of the named pattern. For example::
  115. module _ {A : Set} (_<_ : A → A → Bool) where
  116. merge : List A → List A → List A
  117. merge xs [] = xs
  118. merge [] ys = ys
  119. merge xs@(x ∷ xs₁) ys@(y ∷ ys₁) =
  120. if x < y then x ∷ merge xs₁ ys
  121. else y ∷ merge xs ys₁
  122. As-patterns are properly supported since Agda 2.5.2.
  123. .. _case-trees:
  124. Case trees
  125. ==========
  126. Internally, Agda represents function definitions as *case trees*. For example,
  127. a function definition
  128. ::
  129. max : Nat → Nat → Nat
  130. max zero n = n
  131. max m zero = m
  132. max (suc m) (suc n) = suc (max m n)
  133. will be represented internally as a case tree that looks like this:
  134. .. code-block:: agda
  135. max m n = case m of
  136. zero → n
  137. suc m' → case n of
  138. zero → suc m'
  139. suc n' → suc (max m' n')
  140. Note that because Agda uses this representation of the function
  141. ``max``, the clause ``max m zero = m`` does not hold definitionally
  142. (i.e. as a reduction rule). If you would try to prove that this
  143. equation holds, you would not be able to write ``refl``:
  144. .. code-block:: agda
  145. data _≡_ {A : Set} (x : A) : A → Set where
  146. refl : x ≡ x
  147. -- Does not work!
  148. lemma : (m : Nat) → max m zero ≡ m
  149. lemma = refl
  150. Clauses which do not hold definitionally are usually (but not always)
  151. the result of writing clauses by hand instead of using Agda's case
  152. split tactic. These clauses are :ref:`highlighted <highlight>` by
  153. Emacs.
  154. .. _catchall-pragma:
  155. The ``--exact-split`` flag causes Agda to raise an error whenever a
  156. clause in a definition by pattern matching cannot be made to hold
  157. definitionally. Specific clauses can be excluded from this check by
  158. means of the ``{-# CATCHALL #-}`` pragma.
  159. For instance, the above definition of ``max`` will be rejected when
  160. using the ``--exact-split`` flag because its second clause does not to
  161. hold definitionally.
  162. When using the :option:`--exact-split` flag, catch-all clauses have to
  163. be marked as such, for instance: ::
  164. eq : Nat → Nat → Bool
  165. eq zero zero = true
  166. eq (suc m) (suc n) = eq m n
  167. {-# CATCHALL #-}
  168. eq _ _ = false
  169. The :option:`--no-exact-split` flag can be used to override a global
  170. :option:`--exact-split` in a file, by adding a pragma
  171. ``{-# OPTIONS --no-exact-split #-}``. This option is enabled by
  172. default.