简体   繁体   English

haskell sum类型的多个声明错误

[英]haskell sum type multiple declaration error

data A=A
data B=B
data AB=A|B

Which makes a sum type AB from A and B. 根据A和B得出总和类型AB。

but the last line induces a compile error "multiple declarations of B" 但最后一行引发了编译错误“ B的多个声明”

I also tried sth like this: 我也尝试过这样的事情:

data A=Int|Bool

It compiles. 它编译。 but why ghc disallows me from making sum types for user-defined types? 但是为什么ghc不允许我为用户定义类型创建求和类型?

You're getting fooled. 你被骗了。 You think when you write data A=Int|Bool that you are saying that a value of type A can be a value of type Int or a value of type Bool ; 您认为当您写data A=Int|Bool ,您是在说A类型的值可以是Int类型的值或Bool类型的值; but what you are actually saying is that there are two new value-level constructors named Int and Bool , each containing no information at all, of type A . 但是您实际上要说的是,有两个名为IntBool新值级别构造函数,每个都根本不包含任何信息,类型为A Similarly, you think that data AB=A|B says you can either be of type A or type B , but in fact you are saying you can either have value A or value B . 同样,您认为data AB=A|B表示您可以是A类型或B类型,但实际上您是在说您可以具有 A B

The key thing to keep in mind is that there are two namespaces, type-level and term-level, and that they are distinct. 要记住的关键是,有两个名称空间,类型级别和术语级别,并且它们是不同的。

Here is a simple example of how to do it right: 这是一个正确的方法的简单示例:

data A=A
data B=B
data AB=L A|R B

The last line declares two new term-level constructors, L and R . 最后一行声明了两个新的术语级构造函数LR The L constructor carries a value of type A , while the R constructor carries a value of type B . L构造函数带有类型 A的值,而R构造函数带有类型 B的值。

You might also like the Either type, defined as follows: 您可能还喜欢以下定义的Either类型:

data Either a b = Left a | Right b

You could use this to implement your AB if you wanted: 如果需要,可以使用此方法来实现AB

type AB = Either A B

Similarly, you could use Either Int Bool for your tagged union of Int and Bool . 同样,您可以将Either Int Bool用于标记的IntBool联合。

Because the type of the value created using data constructor A or B will be ambiguous. 因为使用数据构造函数AB创建的值的类型将是模棱两可的。 When I have a = B for instance, what is the type of a ? 例如,当我有a = Ba的类型a什么? It is A or AB ? A还是AB

You should consider using different data constructor as follows: 您应该考虑使用以下不同的数据构造函数:

data A = MkA
data B = MkB
data AB = A A | B B

When you say data AB = A | B 当您说data AB = A | B data AB = A | B , you are not referring to the types A and B , but rather are defining data constructors A and B . data AB = A | B ,您不是在引用类型 AB ,而是在定义数据构造函数 AB These conflict with the constructors defined on the the previous lines. 这些与前几行中定义的构造函数冲突。

If you want to create a type AB that is the sum of A and B , you must provide data constructors that wrap the types A and B , eg: 如果你想创建一个类型AB是的总和AB ,你必须提供的数据构造函数包裹类型AB ,如:

data AB = ABA A | ABB B

Sum types have to be tagged. 总和类型必须加标签。 a+a has to have two injections from a . a+a必须从a进行两次注入

To understand how algebraic data types work, take a simple example: 要了解代数数据类型如何工作,请举一个简单的示例:

data X = A | B C

This defines a new type constructor, X , along with data constructors A and B . 这定义了一个新的类型构造函数X以及数据构造函数AB The B constructor takes/holds an argument of type C. B构造函数采用/持有C类型的参数。

The primary canonical sum type in Haskell is Either : Haskell中主要的规范和类型为Either

data Either a b = Left a | Right b

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

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