Functions in Haskell have types just like non-function values do. The types of Haskell functions are written using an arrow ( -> ). Let’s look at an example of a function that takes an Int and adds 1 to it:
| | addOne :: Int -> Int |
| | addOne n = n + 1 |
The type signature for this function says that it is going to accept an Int and will return an Int . From the type signature, you can also know that the variable n must be an Int . That’s because n is the first parameter to the function, and we’ve said in the function’s type that its parameter must be an Int .
It’s also possible to have function types that have parameters even when you’re not explicitly binding the parameter to a variable. For example, we can rewrite addOne to not use any intermediate variable, but the type will not change. Try this example yourself:
| | addOne :: Int -> Int |
| | addOne = (+1) |
What about functions that take multiple arguments? Let’s take a look at an example first, and then pick apart why it works:
| | addThreeNumbers :: Int -> ( Int -> ( Int -> Int )) |
| | addThreeNumbers a b c = a + b + c |
The function itself is pretty straightforward: we take in three numbers, add them together, and return the sum. Looking at the type signature though, you might notice that there are a lot of parentheses! Why do we need so many parentheses, and why are they grouped like that? Recall that functions in Haskell are curried automatically, so we could have written it like this:
| | addThreeNumbers = \ a -> \ b -> \ c -> a + b + c |
Let’s use some let bindings with type annotations here to make sense of what’s going on:
| | addThreeNumbers :: Int -> ( Int -> ( Int -> Int )) |
| | addThreeNumbers x y z = |
| | let |
| | f :: Int -> ( Int -> ( Int -> Int )) |
| | f a = |
| | let |
| | g :: Int -> ( Int -> Int ) |
| | g b = |
| | let |
| | h :: Int -> Int |
| | h c = a + b + c |
| | in h |
| | in g |
| | in f x y z |
In this example, we’ve rewritten our function to use explicit names instead of anonymous functions, and we’ve given each of the functions a type signature. Let’s walk through this function’s implementation and the type signatures for each of the functions we’ve defined to try to get a better feel for how the types and the implementation of the functions match up. Let’s add a comment above our type annotation to help make it easier for us to see how the parameters and types line up:
| | -- :: x -> (y -> (z -> Int)) |
| | addThreeNumbers :: Int -> ( Int -> ( Int -> Int )) |
| | addThreeNumbers x y z = |
The next thing we do is create a let binding to define a new function, f . Our f function has the same type as addThreeNumbers , but we’ve only named a single one of its parameters, a . If you read through the definition of f , it becomes clear that the reason for this is that from f we’re returning a new function named g that has the type (Int -> (Int -> Int)) , so f really is taking one parameter and returning a function.
If we add a comment before the type annotation for f showing the parts of the annotation that match up with a and g we’d get:
| | -- a -> (g) |
| | f :: Int -> ( Int -> ( Int -> Int )) |
| | f a = |
So f takes an Int and returns a function, and we’ve defined that function as another nested let binding that we’ve named g . Just like f , the g function takes a single parameter, in this case b , and returns a new function, which we’ve defined as h . Let’s add comments to these functions as well:
| | -- b -> (h) |
| | g :: Int -> ( Int -> Int ) |
| | g b = |
| | let |
| | -- c -> (a + b + c) |
| | h :: Int -> Int |
| | h c = a + b + c |
As a last step in our function, we call f with our three top level function parameters:
| | in f x y z |
If this call to f with three parameters looks a little confusing, you can try re-writing it with parentheses to get a better idea of what’s going on:
| | in ((f x) y) z |
These parentheses are optional because function application in Haskell is left-to-right associative, so saying (f x) y is the same as saying f x y .
Just like function application at the value level, we can usually drop the extra parentheses when we’re writing types for Haskell functions. Unlike function application, function types are right associative, so instead of Int -> (Int -> Int) we can write Int -> Int -> Int . In the case of addThreeNumbers , we can drop all of our parentheses and simply write:
| | addThreeNumbers :: Int -> Int -> Int -> Int |
| | addThreeNumbers a b c = a + b + c |
When you’re passing a function as an argument to another function, you will need to use parentheses to capture the type of the function that’s being passed in as an argument:
| | incrementAndShow :: Int -> ( Int -> String ) -> String |
| | incrementAndShow num formatter = formatter (num + 1) |
In this function, the formatter argument is a function with the type Int -> String . In the type annotation for the function we need to use parentheses to show the type of the second argument is a function type. If we omitted the parentheses here, we’d be telling the compiler that our function takes three non-function arguments.
As you might expect, you can also nest parentheses in types if you are passing higher-order functions to other higher order functions. To see an example of how this works, let’s make a function that we can pass incrementAndShow into:
| | incrementAndShow' :: Int -> ( Int -> ( Int -> String ) -> String ) -> String |
| | incrementAndShow' num f = f (num + 1) show |
From the example you can see how we’ve taken the original type for increment AndShow , put parentheses around it, and made it the second argument of our new incrementAndShow’ function.
Considering Readability of η -Reduced and Pointfree Functions
So far we’ve looked at examples of type annotations for functions that bind names to all of their arguments, but one area where function type annotations particularly improve readability is in functions that have been η -reduced or are written in pointfree style. Let’s look at how type annotations can make functions like this easier to read. We’ll start with a function named pointful that calculates the sum of a list of numbers and then multiplies that sum by some constant:
| | pointful :: [ Int ] -> Int -> Int |
| | pointful xs n = foldr (+) 0 xs * n |
The type annotation here looks like you’d expect, with two named function arguments in the function definition associated with the two arguments shown in the type annotation. We can η -reduce this function to remove the binding for n , giving us this new implementation of the function:
| | etaReduced xs = (*) (foldr (+) 0 xs) |
What should the type for this function look like? Our refactored function now takes a single named argument, xs , which holds the list of numbers we’ll sum. We’re partially applying the result of our sum to (*) , and returning a function that will take the second argument to (*) and return an Int , so the type of our function should be:
| | etaReduced :: [ Int ] -> ( Int -> Int ) |
As you’ve already seen, because arrows in function types are left-to-right associative, we can drop the parentheses:
| | etaReduced :: [ Int ] -> Int -> Int |
The type of the η -reduced function is the same as our original function! In fact, the type signature will again be unchanged if we create a completely pointfree definition of our function. We can write a pointfree version by once again performing η -reduction to remove the binding for xs and instead using function composition:
| | pointfree :: [ Int ] -> Int -> Int |
| | pointfree = (*) . foldr (+) 0 |
The type annotation of the pointfree version of the function in this case can significantly improve the ease of understanding the function by giving you a good starting point for thinking through the function composition.