Haskell Types & Signatures
Every expression in Haskell has a type, and the compiler checks that all the types fit together before the program runs. A type signature states a name's type with ::, read "has type".
age :: Int
age = 36
name :: String
name = "Grace"
ratio :: Double
ratio = 0.75
isAdmin :: Bool
isAdmin = False
initial :: Char
initial = 'G'
main :: IO ()
main = do
print age
putStrLn name
print ratio
print isAdmin
print initial36
Grace
0.75
False
'G'
The basic types
| Type | Values | Notes |
|---|---|---|
Int |
42, -7 |
Fixed-size integer, 64 bits on modern machines |
Integer |
2 ^ 100 |
Arbitrary precision, never overflows (slower) |
Double |
3.14, 1.0e-3 |
Double-precision floating point |
Float |
3.14 |
Single precision, rarely used |
Bool |
True, False |
Capitalised, because they are constructors |
Char |
'a', '\n' |
One Unicode character, single quotes |
String |
"hello" |
Just a synonym for [Char], a list of characters |
() |
() |
"Unit": the type with exactly one value, used when there is no interesting result |
Type names always start with an uppercase letter.
Function types
The type of a function lists its argument types and result type separated by arrows. The last type is the result:
isEven :: Int -> Bool
isEven n = n `mod` 2 == 0
fullName :: String -> String -> String
fullName first family = first ++ " " ++ family
clamp :: Int -> Int -> Int -> Int
clamp low high x = max low (min high x)
main :: IO ()
main = do
print (isEven 10)
putStrLn (fullName "Alan" "Turing")
print (clamp 0 100 150, clamp 0 100 (-3), clamp 0 100 42)True
Alan Turing
(100,0,42)
String -> String -> String reads "takes a String, then another String, and returns a String". Why arrows between all the arguments instead of something like (String, String) -> String? Because Haskell functions really take one argument at a time; the lesson on currying explains why that is so useful.
Int versus Integer
big :: Integer
big = 2 ^ 70
wrapped :: Int
wrapped = 2 ^ 63
main :: IO ()
main = do
print big
print wrapped
print (maxBound :: Int)
print (product [1 .. 25 :: Integer])1180591620717411303424
-9223372036854775808
9223372036854775807
15511210043330985984000000
Int silently wraps around on overflow, so 2 ^ 63 became the most negative Int. Use Integer when numbers can grow large (factorials, cryptography, money in cents over many years) and Int for counters, indexes and sizes.
Note:
maxBound :: Intshows an inline type annotation. You can add:: Typeto any expression to tell GHC which type you mean, which is handy when a value could have several types.
Type errors
When types don't line up, the program is rejected. Learning to read the message is a key Haskell skill:
isEven :: Int -> Bool
isEven n = n `mod` 2 == 0
main :: IO ()
main = print (isEven "ten")<source>:5:22: error: [GHC-83865]
* Couldn't match type `[Char]' with `Int'
Expected: Int
Actual: String
* In the first argument of `isEven', namely `"ten"'
In the first argument of `print', namely `(isEven "ten")'
In the expression: print (isEven "ten")
|
5 | main = print (isEven "ten")
| ^^^^^
The important parts are the location (5:22, line 5, column 22), Expected versus Actual, and the "In the first argument of …" context that points at the culprit.
Polymorphic types
Some functions work for any type. Their signatures use type variables, lowercase names such as a and b:
firstOf :: (a, b) -> a
firstOf (x, _) = x
swap :: (a, b) -> (b, a)
swap (x, y) = (y, x)
main :: IO ()
main = do
print (firstOf (1, "one"))
print (firstOf ("yes", True))
print (swap ('k', 3.5))
print (length [True, False], length "abc")1
"yes"
(3.5,'k')
(2,3)
firstOf works for pairs of any two types; the signature promises that the result has the same type as the first component. This is called parametric polymorphism, and it is what "generics" are in other languages. The built-in fst, snd and length :: [a] -> Int (simplified) work the same way.
Test yourself
In the signature clamp :: Int -> Int -> Int -> Int, what is the result type?
- The first
Int - The last
Int - A tuple of three Ints
- There is no result type
What is String in Haskell?
- A primitive type unrelated to lists
- A synonym for
[Char] - A synonym for
Text - An array of bytes
Which type should you use for a value that may exceed 2^63?
IntIntegerDoubleChar