Search code examples
typesfunctional-programmingagdaalgebraic-data-typesgadt

Definition of List in Agda


On page 4 of Dependently Typed Programming in Agda, List is defined as follows

infixr 40 _::_
data List (A : Set) : Set where
  [] : List A
  _::_ : A -> List A -> List A

I have difficulties wrapping my head around the last line. I've learned some Haskell a while ago, so I am familiar with the cons operator.

So, either you have an empty list, which is of type List A, or you create a new value with function :: of type A -> List A -> List A, which takes some element of type A and a list of type A and returns a new list?

This seems to be the intuition, but this does not map to the concept of recursive data type definitions as I know it (from haskell), e.g.

data Tree a = Leaf a | Branch (Tree a) (Tree a)

Question So what kind of type is this? What concepts of Agda are involved here?


Solution

  • There are two syntaxes to define data types in Haskell and Agda.

    Simple one:

    data List a = Nil | a :# List a
    

    And more expressive one (in Haskell it is used to define GADTs):

    {-# LANGUAGE GADTs #-}
    data List a where
        Nil :: List a
        (:#) :: a -> List a -> List a