coverage-checking.lagda.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ..
  2. ::
  3. module language.coverage-checking where
  4. open import Agda.Builtin.Bool
  5. open import Agda.Builtin.Nat
  6. open import Agda.Builtin.String
  7. .. _coverage-checking:
  8. *****************
  9. Coverage Checking
  10. *****************
  11. To ensure completeness of definitions by pattern matching, Agda
  12. performs a coverage check on each definition by pattern matching. This
  13. page explains how this coverage check works by starting from simple
  14. examples and building up to the general case.
  15. Single match on a non-indexed datatype
  16. --------------------------------------
  17. When a :ref:`function definition <function-definitions>` pattern
  18. matches on a single argument of a simple (i.e. non-indexed)
  19. :ref:`datatype <data-types>`, there should be a clause for each
  20. constructor. For example:
  21. ::
  22. data TrafficLight : Set where
  23. red yellow green : TrafficLight
  24. go : TrafficLight → Bool
  25. go red = false
  26. go yellow = false
  27. go green = true
  28. Alternatively, one or more cases may be replaced by a *catchall
  29. clause* that uses a variable pattern or a wildcard pattern `_`.
  30. In this case, the catchall clause should be last.
  31. ::
  32. go' : TrafficLight → Bool
  33. go' green = true
  34. go' _ = false
  35. .. note::
  36. When the `--exact-split` flag is enabled, catchall clauses should be
  37. marked explicitly by a :ref:`catchall pragma <catchall-pragma>`
  38. (``{-# CATCHALL #-}``).
  39. The coverage check can be turned off for an individual definition by
  40. putting a ``{-# NON_COVERING #-}`` pragma immediately in front of the
  41. type signature.
  42. ::
  43. {-# NON_COVERING #-}
  44. go'' : TrafficLight → Bool
  45. go'' red = false
  46. go'' green = true
  47. In the special case of a datatype with no constructors (i.e. an empty
  48. type), there should be a single *absurd clause* with an absurd pattern
  49. `()` and no right-hand side.
  50. ::
  51. data ⊥ : Set where
  52. -- no constructors
  53. magic : {A : Set} → ⊥ → A
  54. magic ()
  55. Matching on multiple arguments
  56. ------------------------------
  57. If a function matches on several arguments, there should be a case for
  58. each possible combinations of constructors.
  59. ::
  60. sameColor : TrafficLight → TrafficLight → Bool
  61. sameColor red red = true
  62. sameColor red yellow = false
  63. sameColor red green = false
  64. sameColor yellow red = false
  65. sameColor yellow yellow = true
  66. sameColor yellow green = false
  67. sameColor green red = false
  68. sameColor green yellow = false
  69. sameColor green green = true
  70. Again, one or more cases may be replaced by a catchall clause.
  71. ::
  72. sameColor' : TrafficLight → TrafficLight → Bool
  73. sameColor' red red = true
  74. sameColor' yellow yellow = true
  75. sameColor' green green = true
  76. sameColor' _ _ = false
  77. Copattern matching
  78. ------------------
  79. Functions that return an element of a :ref:`record type
  80. <record-types>` can use :ref:`copatterns <copatterns>` to give the
  81. individual fields. The coverage check will ensure that there is a
  82. single case for each field of the record type. For example:
  83. ::
  84. record Person : Set where
  85. field
  86. name : String
  87. age : Nat
  88. open Person
  89. bob : Person
  90. name bob = "Bob"
  91. age bob = 25
  92. Absurd copatterns or wildcard copatterns are not supported.
  93. Matching on indexed datatypes
  94. -----------------------------
  95. When a function definition matches on an argument of an indexed
  96. datatype, the following conditions should be satisfied:
  97. - For each clause that matches on a constructor pattern ``c u₁ … uₙ``,
  98. the indices of the type of the pattern should be unifiable with the
  99. indices of the datatype being matched on.
  100. - For each constructor ``c`` that does not appear in a clause,
  101. unification of the indices of the type of the constructor with the
  102. indices of the datatype should end in a conflict.
  103. For example, consider the definition of the ``head`` function on
  104. vectors:
  105. ::
  106. data Vec (A : Set) : Nat → Set where
  107. [] : Vec A 0
  108. _∷_ : ∀ {n} → A → Vec A n → Vec A (suc n)
  109. head : ∀ {A m} → Vec A (suc m) → A
  110. head (x ∷ xs) = x
  111. The type of the pattern ``x ∷ xs`` is ``Vec A (suc n)``, which is
  112. unifiable with the type ``Vec A (suc m)``. Meanwhile, unification of
  113. the type ``Vec A 0`` of the constructor ``[]`` with the type ``Vec A
  114. (suc n)`` results in a conflict between ``0`` and ``suc n``, so there
  115. is no case for ``[]``.
  116. In case a function matches on several arguments and one or more of
  117. them are of indexed datatypes, only those combinations of arguments
  118. should be considered where the indices do not lead to a conflict. For
  119. example, consider the ``zipWith`` function on vectors:
  120. ::
  121. zipWith : ∀ {A B C m} → (A → B → C) → Vec A m → Vec B m → Vec C m
  122. zipWith f [] [] = []
  123. zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys
  124. Since both input vectors have the same length ``m``, there is are no
  125. cases for the combinations where one vector has length ``0`` and the
  126. other has length ``suc n``.
  127. In the special case where unification ends in a conflict for *all*
  128. constructors, there should be a single absurd clase (as for an empty
  129. type). For example:
  130. ::
  131. data Fin : Nat → Set where
  132. zero : ∀ {n} → Fin (suc n)
  133. suc : ∀ {n} → Fin n → Fin (suc n)
  134. no-fin-zero : Fin 0 → ⊥
  135. no-fin-zero ()
  136. In many common cases, absurd clauses may be omitted as long as the
  137. remaining clauses reveal sufficient information to indicate what
  138. arguments to case split on. As an example, consider the definition of
  139. the ``lookup`` function for vectors:
  140. ::
  141. lookup : ∀ {A} {n} → Vec A n → Fin n → A
  142. lookup [] ()
  143. lookup (x ∷ xs) zero = x
  144. lookup (x ∷ xs) (suc i) = lookup xs i
  145. This definition pattern matches on both its (explicit) arguments in
  146. both the absurd clause and the two regular clauses. Hence it is
  147. allowed to leave out the absurd clause from the definition:
  148. ::
  149. lookup' : ∀ {A} {n} → Vec A n → Fin n → A
  150. lookup' (x ∷ xs) zero = x
  151. lookup' (x ∷ xs) (suc i) = lookup' xs i
  152. Refer to the next section for a precise explanation of when an absurd
  153. clause may be omitted.
  154. General case
  155. ------------
  156. In the general case, the coverage checker constructs a :ref:`case tree
  157. <case-trees>` from the definition given by the user. It then ensures
  158. that the following properties are satisfied:
  159. - The non-absurd clauses of a definition should arise as the leaves of
  160. the case tree.
  161. - The absurd clauses of a definition should arise as the internal
  162. nodes of the case tree that have no children.
  163. - Absurd clauses may be omitted if removing the corresponding internal
  164. nodes from the case tree does not result in other internal nodes
  165. becoming childless.
  166. - Non-absurd clauses may be replaced by catchall clauses if (1) the
  167. patterns of those catchall clauses are more general than the omitted
  168. clauses, (2) the added catchall clauses are not more general than
  169. any of the clauses that follow it, and (3) removing the leaves
  170. corresponding to the omitted clauses does not result in any internal
  171. nodes becoming childless.
  172. As an example, consider the case tree for the definition of the
  173. ``lookup`` function defined above:
  174. .. code-block:: agda
  175. lookup xs i = case xs of
  176. [] → case i of {}
  177. (x ∷ xs) → case i of
  178. zero → x
  179. (suc i) → lookup xs i
  180. The absurd clause arises from the case split on ``i`` in the branch
  181. where ``xs = []``, which leads to zero cases. The two normal clauses
  182. arise from the two leaves of the case tree. If the case ``[] → case i
  183. of {}`` is removed from the case tree, all the remaining internal
  184. nodes still have at least one child, hence the absurd clause may be
  185. left out of the definition.
  186. For a full formal description of the algorithm that Agda uses to
  187. construct a case tree and check coverage of definitions by pattern
  188. matching, refer to the article `Elaborating dependent (co)pattern
  189. matching: No pattern left behind
  190. <https://www.cambridge.org/core/journals/journal-of-functional-programming/article/elaborating-dependent-copattern-matching-no-pattern-left-behind/F13CECDAB2B6200135D45452CA44A8B3>`__.