6
votes

The Learn You A Haskell for Great Good! book by Miran Lipovača says in the chapter Making Our Own Types and Typeclasses that the idea of Haskell's Int type could be represented like this:

data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | 2 | ... | 2147483647  

Nevertheless, it says that it worked just as demonstrative purposes, but it doesn't say how Int is actually defined. Is Int defined especially by the compiler or can it be definable with plain Haskell code?

3
It's probably implementation-dependent, but it appears to simply be a signed 32-bit value. - chepner
In GHC, it is data Int = I# Int#, and data Int#. Int# is magic which is compiled in a special way, the compiler will treat it as an int in the resulting c code. - user2407038

3 Answers

5
votes

Int is magic - defined by the compiler. As the other authors have said, it is not actually defined as an algebraic data type, it is implementation-defined, much like Double!

There are some rules though, Int is guaranteed to be at least a 30-bit signed integer. Int must be able to express every value in the range [-2^29, 2^29) (upper bound exclusive). In practice, Int is defined by the compiler to be equivalent to a 32-bit integer value. The reason for this is that Int can be optimized in certain ways that a machine word cannot. Pointer tagging is important for Haskell performance, and so implementations are free to use some number of bits.

If you want guaranteed sized values, Data.Int has Int32, and Data.Word has Word32, which guarantee exact correspondance to 32-bit signed and unsigned machine integers.

3
votes

It could be defined in Haskell by actually defining all 2^32 constructors (which couldn't be called 1, 2 etc. since those aren't legal constructor names) and then defining fromInteger appropriately. However that would not be very practical.

In all real-world implementations Int is a built-in.

2
votes

http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Int.html#Int8

The above is the sized (and I believe signed) Int datatypes, but the default Int is a built-in.

He's making a point about how algebraic datatypes encode "precise" types that outline what the members of the type are. This is in some respects a step removed from dependent typing but that's another kettle of fish.