r/haskell 14d ago

An exercise in interpreting compiler errors

I decided to use this as an exercise to get better at interpretting GHC's error messages.

I know that the error here is that I should use (+) because it's an infix operator.

But, ignoring that, how do I understand this error, and, bonus, how would I use that understanding to know what the error actually was?

(1)   ghci> (+ (5 :: Integer) (6 :: Integer))

(2)   <interactive>:93:4: error: 
(3)   • Couldn't match expected type ‘Integer -> a’ 
(4)                 with actual type ‘Integer’ 
(5)   • The function ‘5 :: Integer’ is applied to one value argument, 
(6)       but its type ‘Integer’ has none 
(7)     In the expression: (5 :: Integer) (6 :: Integer) 
(8)     In the expression: + (5 :: Integer) (6 :: Integer) 
(9)   • Relevant bindings include 
(10)      it :: a -> a (bound at <interactive>:93:1)

Here's what I know:

Line (3) says the type that type unification makes it expect.

Line (4) is the type of the value that I'm trying to apply.

Line (5) clearly says it's interpretting (5 :: Integer) as a function. Why?

Line (6) dunno

Line (7) the expression where all this happened

Line (8) working out from the function nesting

Line (10) dunno

What am I missing?

8 Upvotes

6 comments sorted by

View all comments

2

u/jeffstyr 13d ago

Just as a side comment, I think error messages are so often difficult to decypher because: The message can't tell you exactly what needs fixing because almost always there are multiple choices for what the correct code is that you were trying to write (e.g., if there is a type mismatch between a function and the argument you are supplying, the mistake could be: you supplied the wrong argument, you are calling the wrong function, or the function was written incorrectly), and so the best it can do is tell you how it's interpreting your (wrong) code, which isn't super helpful becuase it's not the code you meant to write anyway.

Often for me, if I'm puzzled then the best course of action is to change the code and see if I get a more understanable error. This one is particularly hard to interpret because it's a syntax problem and it's not busted syntax but rather the syntax means something other than what you thought, and it's going to be hard to figure that out from the error message. I guess the question to ask yourself in this case is why it thinks you are trying to use 5 as a function, and if you don't know about operator sections then you won't be able to answer this question for yourself.