简体   繁体   English

Agda中List的定义

[英]Definition of List in Agda

On page 4 of Dependently Typed Programming in Agda , List is defined as follows 在Agda中依赖类型编程的第4页上, List定义如下

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. 我之前学过一些Haskell,所以我对cons运算符很熟悉。

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? 因此,您将拥有一个空列表(其类型为List A )或使用函数::创建类型A -> List A -> List A的新值:: -类型A -> List A -> List A ,其中需要一个类型为A元素以及一个类型为list的列表A并返回一个新的列表?

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 这似乎是直觉,但这并不映射到我所知道的递归数据类型定义的概念(来自haskell),例如

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. 在Haskell和Agda中有两种语法来定义数据类型。

Simple one: 简单的一个:

data List a = Nil | a :# List a

And more expressive one (in Haskell it is used to define GADTs ): 更具表现力的一种(在Haskell中,它用于定义GADT ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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