No infinite type error for records
-
29-05-2021 - |
Question
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"
...
Solution
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.
OTHER TIPS
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.