Data Types

Simple datatypes

Example datatypes

In the introduction we already showed the definition of the data type of natural numbers (in unary notation):

  1. data Nat : Set where
  2. zero : Nat
  3. suc : Nat Nat

We give a few more examples. First the data type of truth values:

  1. data Bool : Set where
  2. true : Bool
  3. false : Bool

The True set represents the trivially true proposition:

  1. data True : Set where
  2. tt : True

The False set has no constructor and hence no elements. It represents the trivially false proposition:

  1. data False : Set where

Another example is the data type of non-empty binary trees with natural numbers in the leaves:

  1. data BinTree : Set where
  2. leaf : Nat BinTree
  3. branch : BinTree BinTree BinTree

Finally, the data type of Brouwer ordinals:

  1. data Ord : Set where
  2. zeroOrd : Ord
  3. sucOrd : Ord Ord
  4. limOrd : (Nat Ord) Ord

General form

The general form of the definition of a simple datatype D is the following

  1. data D : Set where
  2. c : A
  3. ...
  4. c : A

The name D of the data type and the names c₁, …, cₙ of the constructors must be new w.r.t. the current signature and context, and the types A₁, …, Aₙ must be function types ending in D, i.e. they must be of the form

  1. (y : B₁) ... (y : Bₘ) D

Parametrized datatypes

Datatypes can have parameters. They are declared after the name of the datatype but before the colon, for example:

  1. data List (A : Set) : Set where
  2. [] : List A
  3. __ : A List A List A

Indexed datatypes

In addition to parameters, datatypes can also have indices. In contrast to parameters which are required to be the same for all constructors, indices can vary from constructor to constructor. They are declared after the colon as function arguments to Set. For example, fixed-length vectors can be defined by indexing them over their length of type Nat:

  1. data Vector (A : Set) : Nat Set where
  2. [] : Vector A zero
  3. __ : {n : Nat} A Vector A n Vector A (suc n)

Notice that the parameter A is bound once for all constructors, while the index {n : Nat} must be bound locally in the constructor _∷_.

Indexed datatypes can also be used to describe predicates, for example the predicate Even : Nat → Set can be defined as follows:

  1. data Even : Nat Set where
  2. even-zero : Even zero
  3. even-plus2 : {n : Nat} Even n Even (suc (suc n))

General form

The general form of the definition of a (parametrized, indexed) datatype D is the following

  1. data D (x : P₁) ... (x : Pₖ) : (y : Q₁) ... (y : Qₗ) Set where
  2. c : A
  3. ...
  4. c : A

where the types A₁, …, Aₙ are function types of the form

  1. (z : B₁) ... (z : Bₘ) D x ... x t ... t

Strict positivity

When defining a datatype D, Agda poses an additional requirement on the types of the constructors of D, namely that D may only occur strictly positively in the types of their arguments.

Concretely, for a datatype with constructors c₁ : A₁, …, cₙ : Aₙ, Agda checks that each Aᵢ has the form

  1. (y : B₁) ... (y : Bₘ) D

where an argument types Bᵢ of the constructors is either

  • non-inductive (a side condition) and does not mention D at all,

  • or inductive and has the form

    1. (z : C₁) ... (z : Cₖ) D

    where D must not occur in any Cⱼ.

The strict positivity condition rules out declarations such as

  1. data Bad : Set where
  2. bad : (Bad Bad) Bad
  3. -- A B C
  4. -- A is in a negative position, B and C are OK

since there is a negative occurrence of Bad in the type of the argument of the constructor. (Note that the corresponding data type declaration of Bad is allowed in standard functional languages such as Haskell and ML.).

Non strictly-positive declarations are rejected because they admit non-terminating functions.

If the positivity check is disabled, so that a similar declaration of Bad is allowed, it is possible to construct a term of the empty type, even without recursion.

  1. {-# OPTIONS --no-positivity-check #-}
  1. data : Set where
  2. data Bad : Set where
  3. bad : (Bad ⊥) Bad
  4. self-app : Bad
  5. self-app (bad f) = f (bad f)
  6. absurd :
  7. absurd = self-app (bad self-app)

For more general information on termination see Termination Checking.