Why is there no list-style infinite type error when I define something like this in 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"
...
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). 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
.
But if you define
data Foo = Foo String Foo
you get no such error.
Exercise: explain the difference between these two situations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With