Why is Int Type 2 ^ 31 not out of range in GHCi?

I am reading Haskell Programming . The book and testing provided examples in the GHCi interpreter. It turns out that there is a difference in the behavior of type Int in the interpreter GHCi and Hugs. According to Chapter 3, "Programming in Haskel," 2^31 :: Int should go beyond the range of Int . Meanwhile, in the GHCi interpreter, I get:

 Prelude> 2^31 :: Int 2147483648 

while in Hugs it behaves exactly like the book says:

 Hugs> 2^31 :: Int -2147483648 

In GHCi, I can even check if the result is an Int type

 Prelude> let x = 2^31 :: Int Prelude> :type x x :: Int Prelude> x 2147483648 

What is the source of the described difference? Should I run examples from a book in Hugs or use GHCi, which seems like the recommended choice for learning Haskell? I would be grateful for your help.

+6
source share
3 answers

An Int in Haskell should support at least the range [-2^29 .. 2^29-1] , but it can also be larger. The exact size will depend on both the compiler you are using and the architecture you are in. (For more information, see the 2010 Haskell Report , the latest standard for the Haskell language.)

With the GHC on a 64-bit machine, you will have a range of [-2^63..2^63 - 1] . But even on a 32-bit machine, I believe the GHC range gives you a little more than a strict minimum (presumably [-2^31..2^31 - 1] ).

You can check what the actual border with maxBound and minBound :

 > maxBound :: Int 9223372036854775807 

Differences between implementations arise because the definition of a language explicitly allows them to implement these types in different ways. Personally, I would continue to use GHCi , just considering this, because GHC is by far the most likely compiler you will use. If you encounter a large number of inconsistencies, you can look at them in the standard or ask someone (like here!); think of it as a learning process;).

In this regard, the standard is flexible, allowing various compilers and architectures to optimize their code in different ways. I assume (but not 100% sure) that the minimum range is set taking into account the 32-bit system, and also allows the compiler to use a pair of bits from the base 32-bit value for its own internal purposes, such as easily distinguishing numbers from pointers. (Something that I know, Python and OCaml, at least, do.) GHC does not need to do this, so it provides full 32 or 64 bits suitable for its architecture.

+20
source

Most likely you are on a 64-bit system where Int has, well, 64 bits.

Try the following:

 Prelude> 2^62::Int 4611686018427387904 Prelude> 2^63::Int -9223372036854775808 
+4
source

Int is the size of the machine. Thus, on a 32-bit platform, it will overflow with a speed of 2 31 .

 $ ssh pi@192.168.0.3 Linux raspberrypi 3.12.28+ #709 PREEMPT Mon Sep 8 15:28:00 BST 2014 armv6l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Tue Nov 11 12:58:20 2014 from 192.168.0.102 pi@raspberrypi :~$ ghci GHCi, version 7.8.2: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> 2^31 :: Int -2147483648 

Note that the Haskell report doesn’t really indicate how big Int , exactly - as Tikhon Elvis says, it just guaranteed processing 2 29 . But the GHC certainly uses all machine integers, which, as a rule, are fairly optimal performance and need.

+4
source

Source: https://habr.com/ru/post/978162/


All Articles