在“Learn you a Haskell for Great Good”一书中缺少“代数数据类型”的定义。 [英] Missing definition of 'algebraic data type' in the book "Learn You a Haskell for Great Good"

查看:144
本文介绍了在“Learn you a Haskell for Great Good”一书中缺少“代数数据类型”的定义。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读学习你一个Haskell

a>,而且似乎作者没有明确地定义术语'代数数据类型'指的是什么。该术语首先出现在本书的PDF版本的第126页,并且在第一次出现之前没有明确的定义。

有人可以按照本书的精神填写这个缺失的定义吗?换句话说,有人可以给出一个简单的定义,这个定义可以根据本书前126页中包含的信息来理解吗?



我的怀疑是代数数据类型是关键字'data'定义的任何东西,但我不是100%确定的。



据我所知,网上有很多关于代数数据类型的解释,但我只想知道代数数据类型 strong>在本书中指 。感谢您的阅读。



编辑:这不是关于名称代数的问题。这是关于术语ADT缺失定义的问题。换句话说,这本书没有清楚地解释ADT是否与数据相同。哪个是。但这本书混淆不清这一点。这是这个问题的关键,而不是代数这个词。这个名字也可以是'斑马纹数据类型'的问题是一样的。

解决方案

代数数据类型被称为是因为它们构成了一个类型的代数。这个代数由两个基本操作组成:


  1. 产品

  2. 总和

这里的乘积和求和操作同构于逻辑连接词(逻辑与)和(逻辑或)运算布尔代数




类型的乘积是一个逻辑类型的联合。例如考虑:

  data Person = Person {name :: String 
,age :: Int
,gender :: Char
}

这里的数据构造函数 Person 是类型 String Int Char 。这意味着它由字符串 组成, Int a Char 。因此,它是这些类型的产品:

  data Person = Person(String * Int * Char)

这种数据类型称为产品类型。几乎每种语言都有产品类型。例如在C中,产品类​​型被称为 struct






类型的总和是类型的逻辑分解。例如,考虑:

  data Race = Dwarf |精灵| Hobbit |人类

这里的数据类型 Race 是一个数据构造函数的分离 Dwarf Elf Hobbit 人类。这意味着它可能是矮人 Elf a Hobbit a 人类。因此它是这些类型的总和:

  data Race = Dwarf + Elf + Hobbit + Human 

这种数据类型称为总和类型。总和类型几乎适用于所有语言。例如在C中,sum类型被称为 union 。在Java中,它被称为枚举






代数数据类型功能强大,因为它们允许您轻松创建产品和总计类型。例如考虑:

  data Shape = Circle {x :: Double 
,y :: Double
,r :: Double
}
| Rectangle {x1 :: Double
,y1 :: Double
,x2 :: Double
,y2 :: Double
}

这里数据类型 Shape 是数据构造函数的总和 Circle Rectangle ,它们又是类型 Double ^ 3 Double ^ 4 分别。



在Haskell和其他函数式编程语言中创建这样的代数数据类型很简单。但是,像C和Java这样的语言创建这样的数据类型是笨拙而冗长的。


I'm reading Learn You a Haskell and it seems that the author did not explicitly define what the term 'algebraic data type' is referring to. This term first appears on page 126 in the PDF version of the book and no explicit definition precedes its first appearance.

Could someone please fill in this missing definition in the spirit of the book?

In other words, could someone give a simple definition which is understandable based on the information contained in the first 126 pages of the book?

My suspicion is that an algebraic data type is anything defined by the keyword 'data' but I am not 100% certain.

I understand that there are lot of explanations of algebraic data types on the net but I just would like to know what algebraic data types mean in the context of this book. Thanks for reading.

EDIT : This is not a question about the name "algebraic". This is a question about the missing definition of the term ADT. In other words, the book does not clearly explain if ADT is the same as 'data'. Which is. But the book confusingly does not make this connection. This is what this question is about and not about the word 'algebraic'. The name could be also 'zebraic data types' the question would be the same.

解决方案

Algebraic data types are so called because they form an algebra of types. This algebra consist of two primitive operations:

  1. Product
  2. Sum

Here the product and sum operations are isomorphic to the logical conjunction (logical AND) and logical disjunction (logical OR) operations from boolean algebra.


A product of types is a logical conjunction of types. For example consider:

data Person = Person { name   :: String
                     , age    :: Int
                     , gender :: Char
                     }

Here the data constructor Person is a conjunction of the types String, Int and Char. It means that it is composed of a String and an Int and a Char. Hence it's a product of those types:

data Person = Person (String * Int * Char)

Such a data type is called a product type. Product types are available in almost every language. For example in C a product type is called a struct.


A sum of types is a logical disjunction of types. For example consider:

data Race = Dwarf | Elf | Hobbit | Human

Here the data type Race is a disjunction of the data constructors Dwarf, Elf, Hobbit and Human. It means that it may be either a Dwarf or an Elf or a Hobbit or a Human. Hence it's a sum of those types:

data Race = Dwarf + Elf + Hobbit + Human

Such a data type is called a sum type. Sum types are available in almost every language. For example in C a sum type is called a union. In Java it's called an enum.


Algebraic data types are powerful because they allow you to create product and sum types easily. For example consider:

data Shape = Circle { x :: Double
                    , y :: Double
                    , r :: Double
                    }
           | Rectangle { x1 :: Double
                       , y1 :: Double
                       , x2 :: Double
                       , y2 :: Double
                       }

Here the data type Shape is a sum of the data constructors Circle and Rectangle which in turn are products of the types Double ^ 3 and Double ^ 4 respectively.

Creating such "algebraic" data types in Haskell and other functional programming languages is laconic. However creating such data types in languages like C and Java is clumsy and verbose.

这篇关于在“Learn you a Haskell for Great Good”一书中缺少“代数数据类型”的定义。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆