201

In Haskell, what is the difference between an Int and an Integer? Where is the answer documented?

duplode
  • 33,731
  • 7
  • 79
  • 150
0xAX
  • 20,957
  • 26
  • 117
  • 206

6 Answers6

214

"Integer" is an arbitrary precision type: it will hold any number no matter how big, up to the limit of your machine's memory…. This means you never have arithmetic overflows. On the other hand it also means your arithmetic is relatively slow. Lisp users may recognise the "bignum" type here.

"Int" is the more common 32 or 64 bit integer. Implementations vary, although it is guaranteed to be at least 30 bits.

Source: The Haskell Wikibook. Also, you may find the Numbers section of A Gentle Introduction to Haskell useful.

bcat
  • 8,833
  • 3
  • 35
  • 41
  • According to [this answer](http://stackoverflow.com/a/3646472/673206), using `Integer` is often faster than is – Maarten Jan 04 '15 at 17:06
  • 7
    @Maarten, that's only because `Int64` is implemented rather badly on 32-bit systems. On 64-bit systems, it's great. – dfeuer Sep 21 '16 at 05:09
27

Int is Bounded, which means that you can use minBound and maxBound to find out the limits, which are implementation-dependent but guaranteed to hold at least [-229 .. 229-1].

For example:

Prelude> (minBound, maxBound) :: (Int, Int)
(-9223372036854775808,9223372036854775807)

However, Integer is arbitrary precision, and not Bounded.

Prelude> (minBound, maxBound) :: (Integer, Integer)

<interactive>:3:2:
    No instance for (Bounded Integer) arising from a use of `minBound'
    Possible fix: add an instance declaration for (Bounded Integer)
    In the expression: minBound
    In the expression: (minBound, maxBound) :: (Integer, Integer)
    In an equation for `it':
        it = (minBound, maxBound) :: (Integer, Integer)
200_success
  • 7,286
  • 1
  • 43
  • 74
22

Int is the type of machine integers, with guaranteed range at least -229 to 229 - 1, while Integer is arbitrary precision integers, with range as large as you have memory for.

https://mail.haskell.org/pipermail/haskell-cafe/2005-May/009906.html

NullUserException
  • 83,810
  • 28
  • 209
  • 234
11

Int is the C int, which means its values range from -2147483647 to 2147483647, while an Integer range from the whole Z set, that means, it can be arbitrarily large.

$ ghci
Prelude> (12345678901234567890 :: Integer, 12345678901234567890 :: Int)
(12345678901234567890,-350287150)

Notice the value of the Int literal.

Ming-Tang
  • 17,410
  • 8
  • 38
  • 76
  • 2
    GHCi, version 7.10.3 gives warning : Literal 12345678901234567890 is out of the Int range -9223372036854775808..9223372036854775807 – Adam Aug 06 '17 at 17:55
5

The Prelude defines only the most basic numeric types: fixed sized integers (Int), arbitrary precision integers (Integer), ...

...

The finite-precision integer type Int covers at least the range [ - 2^29, 2^29 - 1].

from the Haskell report: http://www.haskell.org/onlinereport/basic.html#numbers

newacct
  • 119,665
  • 29
  • 163
  • 224
5

An Integer is implemented as an Int# until it gets larger than the maximum value an Int# can store. At that point, it's a GMP number.

Nate Symer
  • 2,185
  • 1
  • 20
  • 27
  • 2
    This sounds implementation specific. Is there a reference saying that Integer needs to be implemented this way? – yoniLavi Apr 08 '16 at 19:38
  • 5
    No, you're right, this is GHC specific. That said, 1. GHC is what most people use, 2. This is the most intelligent way I can think of to implement such a data type. – Nate Symer Apr 09 '16 at 16:48
  • 1
    Does this mean that (in GHC) there's no performance tradeoff for using `Integer`, and therefore `Integer` is always the better option? – Kirk Broadhurst Jun 19 '19 at 17:51