Skip to content

Real

Real is the class of all number types that can be viewed as representing real numbers. Here's the definition of the Real type class:

GHCi
>>> :info Real
type Real :: * -> Constraint
class (Num a, Ord a) => Real a where
  toRational :: a -> Rational
  {-# MINIMAL toRational #-}
    -- Defined in ‘GHC.Real’
[More omitted output]

So the only requirements that a type a that is an instance of Real has to satisfy is that it is a number type (Num), that there exists a total order of the values of type a (Ord), and that we can convert a value of type a to a Rational number.

Float and Double—floating number types—are the common approximation of real numbers in programming languages, and both Float and Double are instances of both Fractional and Real. Real captures exactly the additional constraints that these approximations of real numbers satisfy (having an ordering and conversion to rational numbers) that other fractional number types, such as complex numbers, do not necessarily satisfy.

But why is Integral a subclass of Real. Surely, integral number types are not approximations of real numbers. However, in mathematics, every integer is also a real number: \(\mathbb{Z} \subset \mathbb{R}\). So, while an Integral number type cannot (approximately) represent a wide range of real numbers, every number it can represent is a real number. Hence the subclass relation.