How can Integer have an Enum instance? toEnum and fromEnum use only Int, which is limited

I don’t quite understand why this works perfectly:

[9223372036854775809..9223372036854775815] :: [Integer]

These are integers that are greater than maxBound :: Int. However, these are the type signatures of the two most important functions Enum:

toEnum :: Int -> a
fromEnum :: a -> Int

As you can see, they have Intwhich is limited.

So why did it work?

Clarification:

The point of my question is this: not enumFromTodefined in terms of toEnumand fromEnum? And since these two only work on regular Ints, how enumFromTo(for which ..is syntactic sugar) work for Integers?

+4
source share
1 answer

enumFromTo Integer toEnum fromEnum, , . , enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer].

instance  Enum Integer  where
    succ x               = x + 1
    pred x               = x - 1
    toEnum (I# n)        = smallInteger n
    fromEnum n           = I# (integerToInt n)

    {-# INLINE enumFrom #-}
    {-# INLINE enumFromThen #-}
    {-# INLINE enumFromTo #-}
    {-# INLINE enumFromThenTo #-}
    enumFrom x             = enumDeltaInteger   x 1
    enumFromThen x y       = enumDeltaInteger   x (y-x)
    enumFromTo x lim       = enumDeltaToInteger x 1     lim
    enumFromThenTo x y lim = enumDeltaToInteger x (y-x) lim
+8

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


All Articles