data-types.lagda.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ..
  2. ::
  3. {-# OPTIONS --no-positivity-check #-}
  4. module language.data-types where
  5. .. _data-types:
  6. **********
  7. Data Types
  8. **********
  9. Simple datatypes
  10. ================
  11. Example datatypes
  12. -----------------
  13. In the introduction we already showed the definition of the data type of natural numbers (in unary notation):
  14. ::
  15. data Nat : Set where
  16. zero : Nat
  17. suc : Nat → Nat
  18. We give a few more examples. First the data type of truth values:
  19. ::
  20. data Bool : Set where
  21. true : Bool
  22. false : Bool
  23. The ``True`` set represents the trivially true proposition:
  24. ::
  25. data True : Set where
  26. tt : True
  27. The ``False`` set has no constructor and hence no elements. It
  28. represents the trivially false proposition: ::
  29. data False : Set where
  30. Another example is the data type of non-empty binary trees with natural numbers in the leaves::
  31. data BinTree : Set where
  32. leaf : Nat → BinTree
  33. branch : BinTree → BinTree → BinTree
  34. Finally, the data type of Brouwer ordinals::
  35. data Ord : Set where
  36. zeroOrd : Ord
  37. sucOrd : Ord → Ord
  38. limOrd : (Nat → Ord) → Ord
  39. General form
  40. ------------
  41. The general form of the definition of a simple datatype ``D`` is the
  42. following
  43. .. code-block:: agda
  44. data D : Setᵢ where
  45. c₁ : A₁
  46. ...
  47. cₙ : Aₙ
  48. The name ``D`` of the data type and the names ``c₁``, ..., ``cₙ`` of
  49. the constructors must be new w.r.t. the current signature and context,
  50. and the types ``A₁``, ..., ``Aₙ`` must be function types ending in
  51. ``D``, i.e. they must be of the form
  52. .. code-block:: agda
  53. (y₁ : B₁) → ... → (yₘ : Bₘ) → D
  54. .. _parametrized-datatypes:
  55. Parametrized datatypes
  56. ======================
  57. Datatypes can have *parameters*. They are declared after the name of the
  58. datatype but before the colon, for example::
  59. data List (A : Set) : Set where
  60. [] : List A
  61. _∷_ : A → List A → List A
  62. .. _indexed-datatypes:
  63. Indexed datatypes
  64. =================
  65. In addition to parameters, datatypes can also have *indices*. In
  66. contrast to parameters which are required to be the same for all
  67. constructors, indices can vary from constructor to constructor. They
  68. are declared after the colon as function arguments to ``Set``. For
  69. example, fixed-length vectors can be defined by indexing them over
  70. their length of type ``Nat``::
  71. data Vector (A : Set) : Nat → Set where
  72. [] : Vector A zero
  73. _∷_ : {n : Nat} → A → Vector A n → Vector A (suc n)
  74. Notice that the parameter ``A`` is bound once for all constructors,
  75. while the index ``{n : Nat}`` must be bound locally in the constructor
  76. ``_∷_``.
  77. Indexed datatypes can also be used to describe predicates, for example
  78. the predicate ``Even : Nat → Set`` can be defined as follows:
  79. ::
  80. data Even : Nat → Set where
  81. even-zero : Even zero
  82. even-plus2 : {n : Nat} → Even n → Even (suc (suc n))
  83. General form
  84. ------------
  85. The general form of the definition of a (parametrized, indexed)
  86. datatype ``D`` is the following
  87. .. code-block:: agda
  88. data D (x₁ : P₁) ... (xₖ : Pₖ) : (y₁ : Q₁) → ... → (yₗ : Qₗ) → Set ℓ where
  89. c₁ : A₁
  90. ...
  91. cₙ : Aₙ
  92. where the types ``A₁``, ..., ``Aₙ`` are function types of the form
  93. .. code-block:: agda
  94. (z₁ : B₁) → ... → (zₘ : Bₘ) → D x₁ ... xₖ t₁ ... tₗ
  95. Strict positivity
  96. =================
  97. When defining a datatype ``D``, Agda poses an additional requirement
  98. on the types of the constructors of ``D``, namely that ``D`` may only
  99. occur **strictly positively** in the types of their arguments.
  100. Concretely, for a datatype with constructors ``c₁ : A₁``, ..., ``cₙ :
  101. Aₙ``, Agda checks that each `Aᵢ` has the form
  102. .. code-block:: agda
  103. (y₁ : B₁) → ... → (yₘ : Bₘ) → D
  104. where an argument types `Bᵢ` of the constructors is either
  105. * *non-inductive* (a *side condition*) and does not mention ``D`` at
  106. all,
  107. * or *inductive* and has the form
  108. .. code-block:: agda
  109. (z₁ : C₁) → ... → (zₖ : Cₖ) → D
  110. where ``D`` must not occur in any `Cⱼ`.
  111. ..
  112. ::
  113. module hidden₁ where
  114. The strict positivity condition rules out declarations such as
  115. ::
  116. data Bad : Set where
  117. bad : (Bad → Bad) → Bad
  118. -- A B C
  119. -- A is in a negative position, B and C are OK
  120. since there is a negative occurrence of ``Bad`` in the type of the
  121. argument of the constructor. (Note that the corresponding data type
  122. declaration of ``Bad`` is allowed in standard functional languages
  123. such as Haskell and ML.).
  124. Non strictly-positive declarations are rejected because
  125. they admit non-terminating functions.
  126. If the positivity check is disabled, so that a similar declaration of
  127. ``Bad`` is allowed, it is possible to construct a term of the empty
  128. type, even without recursion.
  129. .. code-block:: agda
  130. {-# OPTIONS --no-positivity-check #-}
  131. ::
  132. data ⊥ : Set where
  133. data Bad : Set where
  134. bad : (Bad → ⊥) → Bad
  135. self-app : Bad → ⊥
  136. self-app (bad f) = f (bad f)
  137. absurd : ⊥
  138. absurd = self-app (bad self-app)
  139. For more general information on termination see :ref:`termination-checking`.