Skip to content

Type Queries

First, there is :type or :t, which tells us the type of an expression:

GHCi
>>> x = 5
>>> :t x
x :: Num p => p

So, x is not an integer value. We only defined x to be equal to 5. 5 can be an integer or floating point number or in fact any type that can be constructed from an integer literal, such as 5. That's exactly the type class Num. Thus, our type query tells us that x has type p, for any number type p (the constraint Num p).

Similarly, if we define a function, such as our double function to double a number, we can query its type:

GHCi
>>> double x = 2 * x
>>> :t double
double :: Num a => a -> a

Once again, double is a function whose argument can be of any number type a, and the result is then of the same type. Don't ask me why GHCi uses p to refer to the type of x, and a to refer to the type of the argument of double. Since these are type variables, they can be named anything. Even double :: Num doodleDoo => doodleDoo -> doodleDoo would have been valid.