简体   繁体   中英

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), eg

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM