Throughout this chapter, you’ve learned how to create new data types from scratch to describe the values in your program, but there are some cases where creating an entirely new type from scratch isn’t necessary. In some cases, you might just want to provide a new name to some existing type. In these cases, rather than creating a new type, you can use a type alias .

Type aliases can be a useful way to improve the readability of your code while retaining a degree of flexibility and avoiding introducing the overhead of constructing new data types, but they can also be misused in ways that remove some of the protections you can get with proper types. In this section, we’ll look first at one of the cases where type aliases might make your code quality worse. Once you have had a chance to see how to not use type aliases, we’ll look at a couple of examples of cases where they can be useful.

Wrapping Basic Data Types

Type aliases allow you to provide a new name for some existing type. You’ve already been using one type alias throughout this book, perhaps without realizing it: the String type is a type alias for a list of Char values. If you look at the info for String in ghci you can see precisely how it’s defined:

​  ​ λ ​ :i ​ String ​
​  ​ type ​ ​ String ​ = [​ Char ​] ​ -- Defined in ‘GHC.Base’ ​

From this example you can see that a type alias is defined with the type keyword, followed by the alias name. The type on the right-hand side is the type that is being aliased. Note that, unlike with a data type, there is no data constructor on the right-hand side of the equals sign here.

Defining a type alias is a handy way to be able to give some general type a name that’s more semantically meaningful in your application. String is a great example of providing a more meaningful name, but let’s build our own function that makes use of type aliases as well. We’ll write a program that will let us calculate the average velocity of an object given the distance that it has traveled, in meters, over some period of time, given in seconds. We could start with a version that doesn’t use type aliases:

​  velocity :: ​ Double ​ -> ​ Double ​ -> ​ Double ​
​  velocity meters seconds = meters / seconds
​  speedLimit :: ​ Double ​
​  speedLimit =
​  ​ let ​
​  meters = 299792458 :: ​ Double ​
​  seconds = 1.0 :: ​ Double ​
​  ​ in ​ velocity meters seconds

If someone is looking at the implementation of our function, it’s fairly straightforward how to use it, but imagine that our function was made available in some library and a user wants to call it. They might start by checking its type signature:

​  ​ λ ​ :​ type ​ velocity
​  velocity :: ​ Double ​ -> ​ Double ​ -> ​ Double ​

Without looking at the implementation, or reading the documentation, it’s hard to use this function. Our users will have no idea what the order of the arguments should be, nor the units to use for input or what units to expect for output. Let’s look at what happens when we use type aliases to provide a more human readable name for the Double s in the type signature:

​  ​ type ​ ​ Meters ​ = ​ Double ​
​  ​ type ​ ​ Seconds ​ = ​ Double ​
​  ​ type ​ ​ MetersPerSecond ​ = ​ Double ​
​  
​  velocity :: ​ Meters ​ -> ​ Seconds ​ -> ​ MetersPerSecond ​
​  velocity meters seconds = meters / seconds

We haven’t changed the implementation of velocity here at all, instead we’ve simply created some aliases for Double and used them in the type annotation for velocity . We can rebuild our program and you’ll see that it builds and even the speedLimit function continues to work, although we are passing it Double s instead of the new type aliases.

​  ​ λ ​ :​ type ​ velocity
​  velocity :: ​ Double ​ -> ​ Double ​ -> ​ Double ​
​  ​ λ ​ :​ type ​ speedLimit
​  speedLimit :: ​ Double ​
​  ​ λ ​ speedLimit
​  9.8

The flexibility with type aliases that allows our speedLimit method to continue working after we’ve refactored velocity has worked out for us in this case, but it hints at one of the cases where type aliases can cause us some problems: because all type aliases that resolve to the same type are equivalent, the compiler gives us no extra help if we misuse them.

One straightforward example of this is that since Meters and Seconds are both aliases for Double , we can use them arbitrarily in ways that don’t make sense for what the values should semantically represent. For example, the compiler will happily let us add meters to seconds, or pass seconds in as the first parameter of velocity :

​  ​ λ ​ oneMinute = 60 :: ​ Seconds ​
​  ​ λ ​ oneKilometer = 1000 :: ​ Meters ​
​  ​ λ ​ someMeters = (velocity (oneMinute + oneKilometer) oneKilometer) :: ​ Meters ​
​  ​ λ ​ :​ type ​ someMeters
​  someMeters :: ​ Meters ​

Similarly, if we refactor velocity to swap the order of the arguments, our speedLimit function would continue to compile but return incorrect results:

​  ​ λ ​ :​ type ​ velocity
​  velocity :: ​ Seconds ​ -> ​ Meters ​ -> ​ MetersPerSecond ​
​  ​ λ ​ speedLimit
​  0.1020408163265306

It’s worth noting that all of these errors would have been possible if we’d never created a type alias in the first place. We could just as easily add a double that was supposed to represent Meters with one that was supposed to represent Seconds . Introducing type aliases hasn’t allowed any new errors as far as the compiler is concerned, but it has provided us with a false sense of safety, and in making the choice to use type errors over data types, we’ve given up the opportunity for the compiler to help detect some of these errors for us.

As you can see, type aliases offer some benefits over using built-in types, but they can sometimes make code a bit more error prone, or at the very least we give up the opportunity to get better type safety from using a proper data type. So when is the right time to use a type alias?

Type aliases are best reserved for cases where the aliased type really does have the same meaning as the type it’s aliasing. String as an alias for [Char] is a good example of this. A String makes sense as a list of characters, and it’s intuitive that any operation that would work on one would work on the other. On the other hand, an alias like Seconds has a very different meaning from a plain Int , so it’s not a good example of when to use a type alias.

Using Type Aliases with Type Parameters

One of the most common ways to make use of type aliases is to use them to fix some type parameters. In other words, you can use a type alias as a way to give a name to some partially applied type. This avoids some of the problems you ran into in the last section where you create type aliases that have some distinct semantic meaning from their underlying type, and instead encourages you to create aliases that do have some logical mapping to the underlying type.

In fact, the String alias that we looked at in the previous section as an example of a “good” type alias is an example of this pattern. Recall that a String is an alias for a list of characters, which we typically write:

​  ​ type ​ ​ String ​ = [​ Char ​]

Let’s rewrite this using our own list type, to help clarify what’s happening. We’ll call our string type String’ to differentiate it from the standard string type:

​  ​ data ​ ​ List ​ a = ​ Empty ​ | ​ List ​ a
​  ​ type ​ ​ String' ​ = ​ List ​ ​ Char ​

It’s easier to see here that a String is just applying the Char type to List or [] .

You can extend this pattern to types that have more than one parameter. For example, consider the Either type. Imagine that you have some specific error type that you’ve defined for your application. It might have an error message along with some additional context:

​  ​ data ​ ​ AppError ​ = ​ AppError ​
​  { errorMessage :: ​ String ​
​  , errorContext :: [​ String ​]
​  , errorWrapped :: ​ Maybe ​ ​ AppError ​
​  }

You could write all of the functions in your program so they return Either AppError a . For example, imagine you were writing a small inventory management program and you had some functions that all might fail in some way:

​  parseUserOrder :: ​ String ​ -> ​ Either ​ ​ AppError ​ ​ Order ​
​  generateInvoice :: ​ Order ​ -> ​ Either ​ ​ AppError ​ ​ Invoice ​
​  updateInventory :: [​ Order ​] -> [(​ Widget ​,​ Int ​)] -> ​ Either ​ ​ AppError ​ [(​ Widget ​,​ Int ​)]

There’s nothing necessarily wrong with the repetition of Either AppError here, but if you are going to consistently use this pattern throughout your program, making use of a type alias can make the program both more readable, and also can make it a little bit easier for you to avoid mistakenly writing a function that returns some other error value. Let’s take a look at how we could do this.

We’ll start by creating a type alias for Either AppError . Type aliases can have parameters just like regular data types, so we could write this:

​  ​ type ​ ​ AppValue ​ a = ​ Either ​ ​ AppError ​ a

Alternatively, and more idiomatically, you can η -reduce this and write:

​  ​ type ​ ​ AppValue ​ = ​ Either ​ ​ AppError ​

With the type alias, you can rewrite your functions:

​  parseUserOrder :: ​ String ​ -> ​ AppValue ​ ​ Order ​
​  generateInvoice :: ​ Order ​ -> ​ AppValue ​ ​ Invoice ​
​  updateInventory :: [​ Order ​] -> [(​ Widget ​,​ Int ​)] -> ​ AppValue ​ [(​ Widget ​,​ Int ​)]

Using a type alias here makes our code a little bit more readable, by removing some of the noise of having Either AppError repeated everywhere in the code. Instead, we have AppValue which is shorter and conveys a bit of useful meaning. We also get the benefit that we ensure that anyone using an AppValue will consistently use our AppError type for errors. If we had continued to use Either directly, someone working in the code base might have reasonably decided to use some other value instead of AppError . By fixing that parameter to Either , we’re making it more clear that we intend to standardize on that as our error value in all of the functions we’re using in our application.