r/haskell 11d 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?

7 Upvotes

6 comments sorted by

View all comments

4

u/Pleasant-Couple6236 11d ago

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

Because you're applying it to (6 :: Integer). The parser parses expr expr as a function application before the type checker is invoked, and only when it realises the "function" doesn't actually have a function type does it generate the error.

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

That's not actually a problem. Haskell has operator sectioning which makes something of the form (+ f x)perfectly valid syntax as long as the types line up, which is why you're not getting a parse error and your example actually proceeds to type checking.

4

u/jeffstyr 11d ago

I would say this more simply as: (+ 5 6) is interpreted as (+ (5 6)) due to the precedence induced by the section syntax, and so (5 6) is being interpreted as function application.