So far we’ve looked at types that represent a specific sort of value like an Int or a String . A value with a single type like this is called monomorphic . Monomorphic functions are great when you know exactly what kind of values you’ll be working with, but in many cases they’re unnecessarily restrictive. You can get around these restrictions and write more general functions by using polymorphism . In this section, you’ll learn about parametric polymorphism , which is the most common sort of polymorphism that you’ll encounter while writing Haskell. Later on in this book, you’ll be introduced to another kind of polymorphism called ad hoc polymorphism .
To understand how polymorphism works and how it can help us write better code, let’s start by looking at a problem that we can’t solve without polymorphism. There’s a standard library function called id that will return any value you pass into it unmodified. This might sound a bit useless, but it turns out to have some utility, for example, when you’re working with higher-order functions that can optionally transform your data when you don’t want any transformation. We’d like to implement a version of this function ourselves; we’ll call ours identity . Without using polymorphism there’s no way we can implement a single function like this. To prove this to yourself, try to write the identity function yourself without polymorphism.
No matter what monomorphic type we assign to the function, you’re limiting yourself to only working with that particular type of data. To implement a single function that works with any type, we’ll need to use polymorphism. Let’s take a look at the implementation with the type signature first, and then dive in to understand the type annotation and what it tells us about polymorphic functions in Haskell.
| | identity :: a -> a |
| | identity val = val |
The type of identity function, a -> a is making use of a type variable , in this case a . Type variables in Haskell are a way of representing polymorphic types. Just like a regular variable in Haskell can represent any value of the appropriate type, a type variable in Haskell can represent any type of the appropriate kind . You’ll learn more about kinds later. Just like regular variables, type variables in Haskell start with a lowercase letter. You can use longer names for type variables in Haskell:
| | identity :: someValue -> someValue |
Although you can use longer names for type variables, you’ll find that single-letter variable names are quite common in Haskell, with the vast majority of type variable names in most Haskell code bases being between one and three letters long. In most cases, because type variables are representing very abstract concepts, it’s hard to come up with a better variable name than you get with a single-letter identifier anyway.
The scope of a type variable is the type annotation in which it appears. That means that if you have two different functions, each with type variables, they can refer to different types, but within a single type expression, a given variable will always refer to the same type. Let’s look at a couple of examples of type annotations:
| | len :: [a] -> Int |
| | identity :: a -> a |
| | uncurry :: (a -> b -> c) -> (a, b) -> c |
You’ll notice that we’re using the type variable a in each of these functions, but they aren’t related. The scope of a is limited to each individual type annotation. The a in the type annotation for len is completely unrelated to the a in identity or uncurry . On the other hand, within the type for identity or uncurry , each time a type variable appears we know that it has to hold the same type.
The fact that a type variable has to always represent the same type within a type expression means that we know some information about how a function behaves, even though we don’t know what type it might be operating on for any given call. In the identity function for instance, we know that the type of the value that is returned will always be the same as the type of the value applied to it.
This is an important concept to understand, so let’s take a deeper look at another example. In this example, we’ll write a function that takes a function and a value and applies the function to the value and returns the results:
| | apply :: (a -> b) -> a -> b |
| | apply f val = f val |
In this example, our first parameter is a function from a to b , and our second parameter is a value of type a . The a in the first parameter and the a in our second parameter can be anything as long as they are the same. Likewise, the return value of the first parameter and the return value of our function are both b ; again, they can be anything as long as they match.
Keep in mind that in apply :: (a -> b) -> a -> b the occurrences of a have to refer to the same type, and the occurrences of b have to refer to the same type, but a and b may or may not be the same type. It’s easy to think at first glance that they have to to be different.
Let’s look at a short example. We’ll start by adding a new function, incrementInt , and then using it with apply :
| | incrementInt :: Int -> Int |
| | incrementInt n = n + 1 |
| | |
| | incremented :: Int |
| | incremented = apply incrementInt 1 |
If you replace the type variables in the type of apply with the types from incrementInt , you’ll end up with this:
| | ( Int -> Int ) -> Int -> Int |
So in this case, the a and b in apply are both Int .
A type variable can stand in for any type. In the examples you’ve seen so far, we’ve used basic types like Int and String as examples for what a type variable might be standing in for, but a type variable can also be satisfied by more sophisticated types, like lists or function types.
An easy way to demonstrate this is by looking back at the id function, which has the type id :: a -> a . The a in the type signature can stand in for a list if we pass one into id .
| | λ id ([1,2,3] :: [ Int ]) |
| | [1,2,3] |