简体   繁体   English

记录没有无限类型错误

[英]No infinite type error for records

Why is there no list-style infinite type error when I define something like this in Haskell (GHC)?为什么当我在 Haskell (GHC) 中定义这样的东西时,没有列表样式的无限类型错误?

data Broken = Broken { title :: String,
                       loop  :: Broken }

It compiles without a type error, but clearly it's an unusable type: I'd have to define它编译时没有类型错误,但显然它是一种无法使用的类型:我必须定义

foo = Broken "one" (Broken "two" (Broken "three" ... foo = Broken "one" (Broken "two" (Broken "three" ...

There's nothing broken about it.没有任何问题。 It's perfectly possible to define a value of that type:完全可以定义该类型的值:

foo = Broken "one" foo

Basically it's the same thing as defining a list type that has no nil value (which is also perfectly legal).基本上这与定义一个没有 nil 值的列表类型是一样的(这也是完全合法的)。 It's perfectly possible to define values of that type, but all such values will have to be infinite.完全可以定义该类型的值,但所有这些值都必须是无限的。

If you define如果你定义

type Foo = (String, Foo)

Then you should get this error: Cycle in type synonym declarations .然后你应该得到这个错误: Cycle in type synonym declarations

But if you define但是如果你定义

data Foo = Foo String Foo

you get no such error.你没有这样的错误。

Exercise: explain the difference between these two situations.练习:解释这两种情况的区别。

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

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