Skip to content

Misleading function arity mismatch error with TypeApplications

{-# LANGUAGE TypeApplications #-}

f :: a -> a
f x = x

g :: Int
g = f @Int 42 5
GHCi, version 8.3.20170614: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/rgscott/.ghci
[1 of 1] Compiling Main             ( Bug.hs, interpreted )

Bug.hs:7:5: error:
    • Couldn't match expected type ‘Integer -> Int’
                  with actual type ‘Int’
    • The function ‘f’ is applied to three arguments,
      but its type ‘Int -> Int’ has only one
      In the expression: f @Int 42 5
      In an equation for ‘g’: g = f @Int 42 5
  |
7 | g = f @Int 42 5
  |     ^^^^^^^^^^^

That error message is quite confusing to read, since it reports that:

  • f is applied to three arguments, which //includes// a visible type application
  • f only has one argument, which //excludes// the visible type application

We ought to be able to do better. My suggestion would be to report this instead:

    • The expression ‘f @Int’ is applied to two arguments,
      but its type ‘Int -> Int’ has only one
      In the expression: f @Int 42 5
      In an equation for ‘g’: g = f @Int 42 5

Although I'm sure there are other ways the same information could be conveyed (e.g., The function ‘f’ is applied to two value arguments).

Edited by Ryan Scott
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information