Static Type Check
We also said that Haskell is a statically-typed language, which means that each expression type is known at compilation time instead of run-time. This makes Haskell very reliable – if we successfully compile our program, we can be sure that no type errors will occur. This benefit does come at a cost of increased compilation time, and doing any changes to our program requires us to re-compile it.
Another feature of Haskell is type inference, which means that Haskell can automatically interpret types of expressions. This can make our code more concise, but it is still encouraged to explicitly specify the types when we define functions, i.e. to write functions by also including their type signatures. For our previous function
triple, we could add a type signature to it in our
triple :: Int -> Int -- function type signature
triple x = 3 * x -- function definition
triple :: Int -> Intreads as "
tripleis a function that takes in one argument of the type
Intand produces a result of a type
Int". Now that we have altered our module code, we need to reload the practice module in order to apply the changes in GHCi:
*Practice> :r Practice -- :r stands for :reload
Ok, one module loaded.
*Practice> :t triple -- :t stands for :type
triple :: Int -> Int
*Practice> triple 3
But what happens if we try to apply our
triplefunction to a floating-point number?
*Practice> triple 3.5
• No instance for (Fractional Int) arising from the literal ‘3.5’
• In the first argument of ‘triple’, namely ‘3.5’
In the expression: triple 3.5
In an equation for ‘it’: it = triple 3.5
We get a type error because we strictly defined the function as one that takes in an
Intas its only argument, but we passed in a floating-point number.
So how can we make our function accept both
Float? To do that, we can use Haskell's polymorphism and specify a polymorphic type of
Num a(an overloaded type specified with a type class - more on those on the next page), which supports both integer and floating-point numbers:
triple :: Num a => a -> a
triple x = 3 * x
*Practice> triple 3.5
triple :: Num a => a -> anow reads as "
tripleis a function that takes in one argument of type
aand gives a result of type
awhere the type of
Num". So here we see an example of polymorphism where we can work with different types for the same function argument. Note that we use the variable named
aas a placeholder for the type class
Num, but it could be any valid name starting with a lowercase character.