[Haskell-cafe] Types

John Dorsey haskell at colquitt.org
Fri Nov 27 00:28:10 EST 2009

> Hello,
> I´ve a small question about Haskell. It´s really vague explained in "Haskell
> in 10 minutes" and sometimes it's really detailed explained which is very
> hard for people who just got into Haskell to read. Can someone explain me
> something about the type Integral? What exactly is it?

Integral isn't actually a type; it's a type class.

Int is a fixed-size type; it's often 32 or 64 bits in practice, but you
can't rely on that portably.  Int operations are fast.

Integer is an unbounded-size type.  It's less efficient than Int, for
obvious reasons, but it's often more correct because it won't over or

Integral is a class of types that support integer-like operations.  If you
ask ghci:

ePrelude> :info Integral
class (Real a, Enum a) => Integral a where
  quot :: a -> a -> a
  rem :: a -> a -> a
  div :: a -> a -> a
  mod :: a -> a -> a
  quotRem :: a -> a -> (a, a)
  divMod :: a -> a -> (a, a)
  toInteger :: a -> Integer
        -- Defined in GHC.Real
instance Integral Integer -- Defined in GHC.Real
instance Integral Int -- Defined in GHC.Real

Any type for which those operations (quot, rem, div, and so forth) make
sense, can be made an instance of class Integral.  Int and Integer are two
types that are such instances.

> I know that 2 is something of the type Integral, however what does this
> differ from Int? The same question goes for Fractional and Floating, what
> exactly is their commonality and what exactly is their difference?

Numeric literals are a special case.  To make it possible to write "2" and
mean it as an Int or an Integer (or any other numeric type), 2 is defined
to *really* mean (fromIntegral 2).  fromIntegral is a function that takes an
Integral value (such as 2), and converts it to a type of your choice, so
long as it's a member of the Num class.

> Thank you for explaining these basic things to me.

This is one of those areas where Haskell beginners get quickly thrown a
handful of things that are unfamiliar (type classes, defaulting, tricky
numeric literals), while doing the most basic things (arithmetic).  Don't
despair; it'll make sense soon if it doesn't yet.

John Dorsey

More information about the Haskell-Cafe mailing list