Why it is impossible to divide Integer number in Haskell?

Because the Integer type has no Fractional instance.

The type for (/) is Fractional a => a -> a -> a. Consider what happens when a = Integer. You'd have Integer -> Integer -> Integer. But 1/2 is not an integer, it's 0.5. So the only way to fit the division operator would be to round the result. But there is not a single way to round, and the best choice depends on the application, hence it was decided to not provide that instance.

If you want to perform integer division use the div or quot functions (they use different rounding). Otherwise convert to something that supports a well-defined division operation like Rational (this is what the fromIntegral is doing).


Because the division operator for integers has two results (quotient and remainder):

divMod :: Integer -> Integer -> (Integer, Integer)

You can also use the div operator:

n `div` m

which returns only one component of the division result (the quotient), but that's not the same thing as n / m. / is for types where the division operator has only one result which combines 'quotient' and 'remainder' in one fraction.

Equationally, if (q, r) = n `divMod` m, then

n = m * q + r

whereas if q = x / y, then

x = y * q

(up to the usual caveats about floating point numbers and approximations).

Substituting div for / breaks that relationship, because it throws away some of the information you need to reproduce n.

Tags:

Haskell