简体   繁体   English

无法识别Haskell数据类型

[英]Haskell data type not recognised

I got this code: 我得到以下代码:

data Station a b = Machine a b
             | Line [Station a b]
        deriving(Show)

data Machine a b = Req [(a,Int)] b
    deriving(Show)


machine :: [(a, Int)] -> b -> Station a b
machine l b = Req l b

and when i try to compile, it says that the signature of machine is wrong. 当我尝试编译时,它说机器的签名是错误的。 It says it is [(a, Int)] -> b -> Machine ab, instead of [(a, Int)] -> b -> Station a b. 它说是[[a,Int)]-> b->机器ab,而不是[[a,Int)]-> b->站a b。 But in my data type i say that Station ab = Machine a b. 但是在我的数据类型中,我说Station ab = Machine a b。 I don't get why this won't work? 我不明白为什么这行不通?

You have two things called Machine . 您有两件事叫做Machine One is a type constructor that returns a Station type, and the other is a type. 一个是返回Station类型的类型构造函数,另一个是类型。 The two are unrelated, even though they share the same name. 即使它们共享相同的名称,两者也不相关。

It may be helpful to disambiguate the two terms by adding a ' (prime) to the data constructor. 通过在数据构造函数中添加' (质数)来消除两个术语的歧义可能会有所帮助。 In doing so, we can tie the Machine' data constructor to hold a Machine as its only argument: 为此,我们可以将Machine'数据构造函数绑定为将Machine作为其唯一参数:

data Station a b = Machine' (Machine a b)
             | Line [Station a b]
        deriving(Show)

And now we can update the machine function to use the newly defined constructor: 现在我们可以更新machine功能以使用新定义的构造函数:

machine :: [(a, Int)] -> b -> Station a b
machine l b = Machine' (Req l b)

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

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