Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No infinite type error for records

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" ...

like image 831
amindfv Avatar asked Dec 06 '22 15:12

amindfv


2 Answers

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.

like image 96
sepp2k Avatar answered Dec 09 '22 13:12

sepp2k


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.

like image 37
Dan Burton Avatar answered Dec 09 '22 14:12

Dan Burton