One of the most beneficial aspects of Haskell’s type system is in its ability to generate type errors . Errors might sound like a bad thing, but Haskell’s type errors help you refactor your code more quickly and find problems with your program early on in the development cycle before your code gets to users. In this section, you’ll see some simple type errors and learn how to read the compiler’s output and use it to fix errors in your program. Haskell type errors can grow quite long and become complex to read as you introduce more features of the type system into your program. For now, we’ll start out with some minimal examples, and throughout this book we’ll return to type errors that might come up as you’re using new features of the language, so your understanding of these messages can grow along with your comfort in the language.

To start, let’s bring up ghci and look at some simple examples of type errors we could introduce in a program and see how the compiler will tell us about them. Type out each of these examples and read the error messages yourself, and try to get comfortable with the format and spot the common elements of the messages. Afterwards, we’ll review each of these errors one by one:

​  ​ λ ​ ​ "one" ​ :: ​ Int ​
​  ​ λ ​ (​ True ​, ​ False ​) :: (​ Bool ​, ​ Int ​)
​  ​ λ ​ ​ let ​ f = (+) :: ​ Int ​ -> ​ Int ​ -> ​ Int ​ ​ in ​ f 1 (2 :: ​ Float ​)

Now that you’ve gotten the error messages yourself, let’s look at them individually, starting with the first of our erroneous expressions:

​  λ "one" :: Int
​  
​  <interactive>:1:1: error:
​  • No instance for (Data.String.IsString Int)
​  arising from the literal ‘"one "’
​  • In the expression: "one " :: Int
​  In an equation for ‘it’: it = "one " :: Int

We’re getting this error because we’ve added a type annotation saying that we expect our expression to have type Int , but the value we’re supplying is actually a [Char] (plain Haskell strings are stored as a list of characters). Everything that we need to diagnose the problem is in the error message, so let’s go line by line:

​  <interactive>:1:1: error:

The very first part of our error message is telling us what happened (an error), and where. The <interactive> marker is telling us that this error ocurred in an interactive session through ghci . If your error message was in a file, then it would have the full path to the file containing the error. The next two numbers are the line number of the error and the column of text where the expression containing the error started. In this case, the error occurred starting with the first character, on the first line of our ghci session. If you ran this command in an existing ghci session, or typed the example into a file instead of the REPL, then you might have had a different location in your error message.

​  • Couldn't match expected type ‘Int’ with actual type ‘[Char]’

The next line of the error message tells exactly what went wrong. The compiler expected an Int , because we told it that’s what it should expect with our type annotation, but the actual value of the expression was [Char] . The way the compiler phrases this error message gives you a hint into how it detects errors. A lot of error messages that the compiler will find for you happen because you have a value that the compiler has inferred a type for, and you’re trying to use it in an expression that has some other type, and the compiler can’t figure out how the two types can match up.

​  • In the expression: "one" :: Int

The next line of the error message tells you what expression contained the error. In this case, the entire line only contained a single expression. When you have errors in longer programs, this section of the error message can become quite long, and it can take some practice to get a better understanding of how to read it effectively.

​  In an equation for ‘it’: it = "one" :: Int

The final line of the error message is providing some additional context to the error. In this case, it’s telling you that the expression was being assigned to a variable called it . The it variable is how ghci refers to your current statement in the REPL, so you won’t see it in errors you are building from a source file.

Going through this information line by line gives you all of the information you need to understand what your error was, where it was introduced in the source file, and what expressions it was a part of. Now let’s move on to the next error and see how it compares to this one:

​  λ (True, False) :: (Bool, Int)
​  
​  <interactive>:2:8: error:
​  • Couldn't match expected type ‘Int’ with actual type ‘Bool’
​  • In the expression: False
​  In the expression: (True, False) :: (Bool, Int)
​  In an equation for ‘it’: it = (True, False3) :: (Bool, Int)

In this line of code we’ve mistakenly used False as the second element of a tuple, where our type annotation says that it should be an Int . You can see that the shape of the error message is similar to the previous error message, but there are a few key differences. Let’s walk through this message, and note where there are similarities or differences so you can get a better handle on the subtleties of how error messages are displayed.

​  <interactive>:2:8: error:

The first obvious change in our new error message is in the line and column number. Note that column 8 is the start of the second tuple element, False . The error location is helpfully giving you the location of the start of the inner-most expression where the error occurred.

​  • Couldn't match expected type ‘Int’ with actual type ‘Bool’

The second line of our new error message calls out that we’ve tried to use a Bool where the compiler expected an Int . Notice in this case that the compiler doesn’t consider the entire tuple type, (Bool,Int) as a single type, but is able to look inside of the tuple and identify the part of it that doesn’t match. In many cases this can be extremely helpful, since it will allow you to see what specific part of a larger type doesn’t match.

​  • In the expression: False

This line is giving you a narrow view of the specific part of the expression that contains the error. In this case, the error tells you that you had an expression False that caused the error. Thankfully, the next line of the error message adds some additional detail that can make it easier for us to understand what’s going on.

​  In the expression: (True, False) :: (Bool, Int)

The previous two lines of our error message were quite specific about the part of the code that had an error in it. In some cases, such a specific view into the code might not be helpful because it can be hard to see what the nature of the error is. The next line here is providing us with some additional context by giving us a view into the expression that contains our erroneous expression. Since our first error message didn’t have any nested expressions, we didn’t get this additional line.

​  In an equation for ‘it’: it = (True, False) :: (Bool, Int)

The final line of our second error message again contains a reference to our interactive session.

Now let’s take a look at the last of our error messages. In this error, we’ve defined a function named f that performs addition on two Int values. We are calling this function with a type-inferred Int value and a Float . Unlike the previous examples we’ve looked at, the function and the values we’re passing it are all well typed. The error here is that we’re calling f with a Float value when it expects an Int .

​  λ let f = (+) :: Int -> Int -> Int in f 1 (2 :: Float)
​  
​  <interactive>:3:42: error:
​  • Couldn't match expected type ‘Int’ with actual type ‘Float’
​  • In the second argument of ‘f’, namely ‘(2 :: Float)’
​  In the expression: f 1 (2 :: Float)
​  In the expression:
​  let f = (+) :: Int -> Int -> Int in f 1 (2 :: Float)

Once again, the general shape of this error message follows the pattern of the other errors you’ve seen, with the first line showing the location of the error, the second explaining the nature of the error, and the remaining lines giving you some context about where in the source code the error was found. Let’s walk through this last error line by line to look at how it compares to the errors you’ve seen before.

​  <interactive>:3:42: error:
​  • Couldn't match expected type ‘Int’ with actual type ‘Float’

Just like all of the other errors that you’ve seen so far, this one starts with the line number and column of the error. Based on the column number of this error, we can see that the problem starts at the number 2 .

The second line of the error message shows the specific type mismatch. Just like with the error caused by an invalid tuple field, our function call error starts by focusing on the inner-mode expression that contains the error, in this case the use of a Float where it expected an Int .

​  • In the second argument of ‘f’, namely ‘(2 :: Float)’

In this line of our error we have something different than we’ve seen before. The compiler is giving us the information that the error was in the second argument of our call to f . This might seem a bit odd given our focus on understanding currying, but it certainly results in much more readable error messages.

​  In the expression: f 1 (2 :: Float)
​  In the expression:
​  let f = (+) :: Int -> Int -> Int in f 1 (2 :: Float)

The remainder of the function call error again follows the familiar pattern of providing some additional context clues to where the error ocurred. You might notice that in this case we didn’t get any reference to ghci ’s it value. Error messages have an upper limit to the amount of context provided in the error, so as you add more expressions you might no longer see the outermost expression.