简体   繁体   English

仅具有零元或一元构造函数的 Haskell 数据类型是否被视为代数数据类型?

[英]Are Haskell data types with only nullary or unary constructors considered as Algebraic Data Types?

To my limited knowledge Algebraic Data Types in Haskell can be of two forms namely, sums (eg data Bool = True | False ) or products (eg data Pair = P Int Double ).据我所知,Haskell 中的代数数据类型可以是两个 forms,即总和(例如data Bool = True | False )或乘积(例如data Pair = P Int Double )。 Below are some data types with nullary and unary value constructors.下面是一些具有空值和一元值构造函数的数据类型。

data Z  = Z
data S n = S n  -- I think that this is a singleton type?
data S = S Int

Are these types considered as algebraic data types?这些类型是否被视为代数数据类型? If so what are they called?如果有,它们叫什么?

Z is just a differently named unit type. Z只是一个不同名称的单位类型。 A 1 as an ADT. A 1作为 ADT。 In other words, it is an empty product type.换句话说,它是一个空的产品类型。 (Likewise, a type with no constructors at all may be considered an empty sum type.) (同样,完全没有构造函数的类型可能被认为是空的 sum 类型。)

S n is isomorphic to n itself. S nn本身同构。 In the framework of ADTs, it is considered equivalent , ie if n is an algebraic type then so is S n .在 ADT 的框架中,它被认为是等价的,即如果n是代数类型,那么S n也是。

Note however that in Haskell, type definitions are often deliberately treated as opaque, by way of avoiding to export the value constructors.但是请注意,在 Haskell 中,类型定义通常被故意视为不透明,以避免导出值构造函数。 The type is then rather an abstract data type instead of an algebraic one.然后,该类型是一种抽象数据类型,而不是代数类型。

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

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