Throughout this chapter, we’ve put types and type annotations at the forefront of our examples. Adding explicit type annotations to all our let and where has been helpful as a learning exercise, but it’s not a common style in real-world applications. Most Haskell code you’ll see in the wild will include explicit type-level annotations for top-level bindings, but it’s common to leave off the annotations inside of a function body and instead rely on type inference.

Omitting the type annotations inside of a function offers some substantial benefits. First, it means that we don’t have to spend time updating type annotations when we’re refactoring our code. In a larger code base this can be a substantial win for refactoring speed. Second, when we don’t have to worry about adding annotations for all of our intermediate values, we often end up creating fewer intermediate values, and the resulting code ends up shorter and, in practice, is often easier to read and maintain.

All of these benefits also come with one major drawback: the compiler is good at doing type inference and figuring out what the type of any particular expression should be, but the human brain sometimes isn’t. It’s easy to get lost in a larger function and find yourself struggling to keep track of what types are what. Type holes solve this problem by giving us a way to use Haskell’s type inference system more interactively, letting us ask the compiler directly what the type should be for a particular expression.

You can create a type hole by replacing any expression with an underscore ( _ ), or a name that starts with an underscore. When you compile the code, the compiler will notice the type hole and will give you an error message that tells you exactly what the type of the expression should be in order to fill in the type hole. You can create type holes in regular source files, or add them into expressions in ghci . Similarly, you can get information about type holes by compiling a program or loading it into ghci .

Let’s start by creating a new file, TypeHoleDemo.hs :

​  ​ module ​ ​ TypeHoleDemo ​ ​ where ​
​  
​  exampleNumbers :: [​ Int ​]
​  exampleNumbers = [1..10]
​  
​  getFiveNumbers :: [​ Int ​]
​  getFiveNumbers = take 5 ​ _ ​

In this example, we’ve created a list of numbers, exampleNumbers , and a function, getFiveNumbers , which is using a type hole in place of the second argument to take . If we load this into ghci we can see that, as we expect, we’ll get an error message telling us about the type hole:

​  λ :load TypeHoleDemo.hs
​  [1 of 1] Compiling TypeHoleDemo ( TypeHoleDemo.hs, interpreted )
​  
​  TypeHoleDemo.hs:7:25: error:
​  • Found hole: _ :: [Int]
​  • In the second argument of ‘take’, namely ‘_’
​  In the expression: take 5 _
​  In an equation for ‘getFiveNumbers’: getFiveNumbers = take 5 _
​  • Relevant bindings include
​  getFiveNumbers :: [Int] (bound at TypeHoleDemo.hs:7:1)
​  Valid hole fits include
​  getFiveNumbers :: [Int] (bound at TypeHoleDemo.hs:7:1)
​  exampleNumbers :: [Int] (defined at TypeHoleDemo.hs:4:1)
​  [] :: forall a. [a]
​  with [] @Int
​  (bound at <wired into compiler>)
​  mempty :: forall a. Monoid a => a
​  with mempty @[Int]
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))
​  |
​  7 | getFiveNumbers = take 5 _
​  | ^
​  Failed, no modules loaded.

At first glance the type hole error that we get looks similar to the type errors we’ve already seen, but there are some differences. Let’s walk through this and see what we can learn about type hole errors:

​  TypeHoleDemo.hs:7:25: error:

The first thing you’ll notice is that, just like standard type errors, our type hole error message starts with the line and column of our error. In this case, the type hole was in the file TypeHoleDemo.hs on line 7 and started in column 25.

​  • Found hole: _ :: [Int]

In the type errors we’ve looked at up until now, the compiler has typically given us two pieces of information: the type it expected, and the type it actually saw. When we create a type hole, there’s no actual type found, so the compiler only needs to tell us what type it expects. In practice, this can make error messages a lot easier to follow by removing some extraneous information.

​  • In the second argument of ‘take’, namely ‘_’
​  In the expression: take 5 _
​  In an equation for ‘getFiveNumbers’: getFiveNumbers = take 5 _

Like other type errors, the compiler gives us some information about the expression where our error has occurred. In this case, it’s showing us which argument of take had the type hole, along with some expressions to help us spot where the type hole is.

​  • Relevant bindings include
​  getFiveNumbers :: [Int] (bound at TypeHoleDemo.hs:7:1)

The type hole error also shows us information about any other bindings that have been defined. In our simple example, we haven’t created any let bindings, so the only thing in scope that is relevant is the function we’re trying to define. If we added a let binding, we would also see it show up here. For example, we can change the definition of our function to this:

​  getFiveNumbers :: [​ Int ​]
​  getFiveNumbers = ​ let ​ quantity = 5 ​ in ​ take quantity ​ _ ​

If we reload our file in ghci now, you’ll see that quantity is now also a relevant binding included in the error message:

​  • Relevant bindings include
​  quantity :: Int (bound at TypeHoleDemo.hs:7:22)
​  getFiveNumbers :: [Int] (bound at TypeHoleDemo.hs:7:1)

The final part of the error message is a list of values that are defined that have the right type, and so could be potential values to make the program typecheck:

​  Valid hole fits include
​  getFiveNumbers :: [Int] (bound at TypeHoleDemo.hs:7:1)
​  exampleNumbers :: [Int] (defined at TypeHoleDemo.hs:4:1)
​  [] :: forall a. [a]
​  with [] @Int
​  (bound at <wired into compiler>)
​  mempty :: forall a. Monoid a => a
​  with mempty @[Int]
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))

In this example, the compiler is telling us that both getFiveNumbers and exampleNumbers from our module are values that would let our program typecheck. An empty list would work too, along with mempty , which comes from Prelude .

Using type holes like this to figure out the particular type of an argument to a function can be particularly helpful when you are dealing with polymorphic functions. Let’s write a bit more code to see exactly how we can get some value out of type holes when we’re dealing with polymorphism.

We’ll start by adding two functions, one that computes the permutations of a thruple, and another that combines the first two elements of a thruple using some combining function:

​  permuteThruple ::
​  (a,b,c) ->
​  ((a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a))
​  permuteThruple (a,b,c) =
​  ((a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a))
​  
​  mergeFirstTwo :: (a,b,c) -> (a -> b -> d) -> (d,c)
​  mergeFirstTwo (a,b,c) f = (f a b, c)

Working with functions like this could be tricky. Although the functions themselves are polymorphic, the code that calls them might introduce some constraints that narrow down the possible types. With so many type variables, we can start to get lost. Using a type hole can help us recover some helpful information. Let’s add a new function that uses a type hole to help us figure out what’s going on:

​  showFields :: ​ String ​
​  showFields =
​  ​ let ​ (a,b) = combinePermutations . permuteThruple $ ​ _ ​
​  ​ in ​ unlines [fst a, fst b]
​  ​ where ​
​  joinFields a b = show a <> ​ " - " ​ <> b
​  combinePermutations (a,b,c,d,e,f) =
​  ( mergeFirstTwo a joinFields
​  , mergeFirstTwo c joinFields
​  )

You can try to manually review this code to figure out what type we should use in the type hole, but it quickly becomes apparent that it’s going to be a lot of work. If we load the code up in ghci , the compiler immediately tells us what we need to know:

​  TypeHoleDemo.hs:20:54: error:
​  • Found hole: _ :: (String, String, c)
​  Where: ‘c’ is a rigid type variable bound by
​  the inferred types of
​  a :: (String, c)
​  b :: (String, c)
​  at TypeHoleDemo.hs:20:7-54
​  • In the second argument of ‘($)’, namely ‘_’
​  In the expression: combinePermutations . permuteThruple $ _
​  In a pattern binding:
​  (a, b) = combinePermutations . permuteThruple $ _
​  • Relevant bindings include
​  combinePermutations :: forall {a1} {a2} {c1} {b} {c2} {d} {e} {f}.
​  (Show a1, Show a2) =>
​  ((a1, String, c1), b, (a2, String, c2), d, e, f)
​  -> ((String, c1), (String, c2))
​  (bound at TypeHoleDemo.hs:24:5)
​  joinFields :: forall {a}. Show a => a -> String -> String
​  (bound at TypeHoleDemo.hs:23:5)
​  showFields :: String (bound at TypeHoleDemo.hs:19:1)
​  |
​  20 | let (a,b) = combinePermutations . permuteThruple $ _
​  | ^
​  Failed, no modules loaded.

In the second line of the error, the compiler tells us exactly what kind of tuple we should pass in:

​  • Found hole: _ :: (String, String, c)

The first two elements of our tuple need to be strings, and the final element appears to not be used at all, and so it can be any type that we want. Let’s test it out by adding some hardcoded thruple argument:

​  showFields :: ​ String ​
​  showFields =
​  ​ let ​ (a,b) = combinePermutations . permuteThruple $ (​ "hello" ​, ​ "world" ​, 10)
​  ​ in ​ unlines [fst a, fst b]
​  ​ where ​
​  joinFields a b = show a <> ​ " - " ​ <> b
​  combinePermutations (a,b,c,d,e,f) =
​  ( mergeFirstTwo a joinFields
​  , mergeFirstTwo c joinFields
​  )

If we load this in ghci and run it, we’ll get a message back:

​  ​ λ ​ putStrLn showFields
​  ​ "hello" ​ - world
​  ​ "world" ​ - hello

Another case where type holes can come in handy is when the compiler finds a type error in your program, but attributes the error to the wrong part of the expression. Let’s add some more code to our example file. This time we’ll write code that has a type error, but we won’t use a type hole just yet:

​  showStringPair :: (​ String ​,​ String ​) -> ​ String ​
​  showStringPair (a,b) = ​ "fst: " ​ <> a <> ​ ", snd: " ​ <> b
​  
​  doubleField :: a -> (a,a)
​  doubleField a = (a,a)
​  
​  showValues :: ​ String ​
​  showValues = unlines $ map (showStringPair . doubleField) [1..10]

If we load this into ghci we’ll get a type error, but it’s not the one we’d expect:

​  λ :load TypeHoleDemo.hs
​  [1 of 1] Compiling TypeHoleDemo ( TypeHoleDemo.hs, interpreted )
​  
​  TypeHoleDemo.hs:36:59: error:
​  • No instance for (Enum String)
​  arising from the arithmetic sequence ‘1 .. 10’
​  • In the second argument of ‘map’, namely ‘[1 .. 10]’
​  In the second argument of ‘($)’, namely
​  ‘map (showStringPair . doubleField) [1 .. 10]’
​  
​  
​  
​  In the expression:
​  unlines $ map (showStringPair . doubleField) [1 .. 10]
​  |
​  36 | showValues = unlines $ map (showStringPair . doubleField) [1..10]
​  | ^^^^^^^
​  
​  TypeHoleDemo.hs:36:60: error:
​  • No instance for (Num String) arising from the literal ‘1’
​  • In the expression: 1
​  In the second argument of ‘map’, namely ‘[1 .. 10]’
​  In the second argument of ‘($)’, namely
​  ‘map (showStringPair . doubleField) [1 .. 10]’
​  |
​  36 | showValues = unlines $ map (showStringPair . doubleField) [1..10]
​  | ^
​  Failed, no modules loaded.

In this example, the actual bug that we have is due to the function we’re passing to map . It should call show before it calls doubleField . The compiler is assuming this is well typed, and instead thinks we’re passing the wrong value into map. With type inference, this is a problem you’ll encounter from time to time. Two types don’t match, but the compiler makes the wrong decision about which one is correct, and tries to suggest that you change the wrong thing.

When this happens, we can sometimes use a type hole to narrow down the problem. In this case, let’s try turning doubleField into a type hole. Rather than replacing it entirely with an underscore, we can add an underscore prefix. This gives us a named type hole, and it will be easier when we want to remove the type hole and go back to the actual function call later:

​  showValues :: ​ String ​
​  showValues = unlines $ map (showStringPair . _doubleField) [1..10]

When we load this version of the program into ghci , our error is much more clear:

​  λ :load TypeHoleDemo.hs
​  [1 of 1] Compiling TypeHoleDemo ( TypeHoleDemo.hs, interpreted )
​  
​  TypeHoleDemo.hs:36:46: error:
​  • Found hole: _doubleField :: Integer -> (String, String)
​  Or perhaps ‘_doubleField’ is mis-spelled, or not in scope
​  • In the second argument of ‘(.)’, namely ‘_doubleField’
​  In the first argument of ‘map’, namely
​  ‘(showStringPair . _doubleField)’
​  In the second argument of ‘($)’, namely
​  ‘map (showStringPair . _doubleField) [1 .. 10]’
​  • Relevant bindings include
​  showValues :: String (bound at TypeHoleDemo.hs:36:1)
​  Valid hole fits include
​  mempty :: forall a. Monoid a => a
​  with mempty @(Integer -> (String, String))
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))
​  |
​  36 | showValues = unlines $ map (showStringPair . _doubleField) [1..10]
​  | ^^^^^^^^^^^^
​  Failed, no modules loaded.

Now that we’ve told the compiler where to look, the type hole is telling us that for our code to be valid, doubleField would need to have the type Integer -> (String,String) :

​  • Found hole: _doubleField :: Integer -> (String, String)

Of course, doubleField returns a tuple of whatever type we pass into it, so we’ll need to replace doubleField with a new function that both converts the value to a String and then creates the tuple. Let’s try keeping our type hole around, but adding show to our function:

​  TypeHoleDemo.hs:36:46: error:
​  • Found hole: _doubleField :: String -> (String, String)
​  Or perhaps ‘_doubleField’ is mis-spelled, or not in scope
​  • In the first argument of ‘(.)’, namely ‘_doubleField’
​  In the second argument of ‘(.)’, namely ‘_doubleField . show’
​  In the first argument of ‘map’, namely
​  ‘(showStringPair . _doubleField . show)’
​  • Relevant bindings include
​  showValues :: String (bound at TypeHoleDemo.hs:36:1)
​  Valid hole fits include
​  doubleField :: forall a. a -> (a, a)
​  with doubleField @String
​  (bound at TypeHoleDemo.hs:33:1)
​  return :: forall (m :: * -> *) a. Monad m => a -> m a
​  with return @((,) String) @String
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))
​  pure :: forall (f :: * -> *) a. Applicative f => a -> f a
​  with pure @((,) String) @String
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))
​  read :: forall a. Read a => String -> a
​  with read @(String, String)
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘Text.Read’))
​  mempty :: forall a. Monoid a => a
​  with mempty @(String -> (String, String))
​  (imported from ‘Prelude’ at TypeHoleDemo.hs:1:8-19
​  (and originally defined in ‘GHC.Base’))
​  
​  
​  |
​  36 | showValues = unlines $ map (showStringPair . _doubleField . show) [1..10]
​  | ^^^^^^^^^^^^

Now the type hole needs to have the type String -> (String,String) :

​  • Found hole: _doubleField :: String -> (String, String)

That lines up perfectly with the type of doubleField , and in fact the compiler even recognizes that and suggests that it might be a valid fit:

​  Valid hole fits include
​  doubleField :: forall a. a -> (a, a)
​  with doubleField @String
​  (bound at TypeHoleDemo.hs:33:1)

Let’s take the compiler’s advice and fill our type hole in with doubleField :

​  showValues :: ​ String ​
​  showValues = unlines $ map (showStringPair . doubleField . show) [1..10]

If we load this in ghci it seems to work, and we can call showValues :

​  ​ λ ​ :load ​ TypeHoleDemo ​.hs
​  [1 ​ of ​ 1] ​ Compiling ​ ​ TypeHoleDemo ​ ( ​ TypeHoleDemo ​.hs, interpreted )
​  ​ Ok ​, one ​ module ​ ​ loaded. ​
​  ​ λ ​ ​ put ​​ StrLn ​ showValues
​  fst: 1, snd: 1
​  fst: 2, snd: 2
​  fst: 3, snd: 3
​  fst: 4, snd: 4
​  fst: 5, snd: 5
​  fst: 6, snd: 6
​  fst: 7, snd: 7
​  fst: 8, snd: 8
​  fst: 9, snd: 9
​  fst: 10, snd: 10

As you are working through the examples in this book or trying out the exercises, be sure to keep type holes in mind as a helpful tool for narrowing down type errors and figuring out how to fix code that doesn’t compile.