[Git][ghc/ghc][wip/sand-witch/lazy-skol] Add Note [Reporting application arity errors]

Simon Peyton Jones (@simonpj) gitlab at gitlab.haskell.org
Tue Jan 30 09:16:53 UTC 2024



Simon Peyton Jones pushed to branch wip/sand-witch/lazy-skol at Glasgow Haskell Compiler / GHC


Commits:
acdc948d by Simon Peyton Jones at 2024-01-30T09:16:33+00:00
Add Note [Reporting application arity errors]

- - - - -


1 changed file:

- compiler/GHC/Tc/Utils/Unify.hs


Changes:

=====================================
compiler/GHC/Tc/Utils/Unify.hs
=====================================
@@ -881,6 +881,7 @@ new_check_arg_ty herald arg_pos -- Position for error messages only
 mkFunTysMsg :: ExpectedFunTyOrigin
             -> (Arity, TcType)
             -> TidyEnv -> ZonkM (TidyEnv, SDoc)
+-- See Note [Reporting application arity errors]
 mkFunTysMsg herald (n_val_args_in_call, fun_ty) env
   = do { (env', fun_ty) <- zonkTidyTcType env fun_ty
 
@@ -904,7 +905,38 @@ mkFunTysMsg herald (n_val_args_in_call, fun_ty) env
             <+> speakNOf n_val_args_in_call (text "value argument")
 
 
-{- *********************************************************************
+{- Note [Reporting application arity errors]
+--------------------------------------------
+Consider      f :: Int -> Int -> Int
+and the call  foo = f 3 4 5
+We'd like to get an error like:
+
+    • Couldn't match expected type ‘t0 -> t’ with actual type ‘Int’
+    • The function ‘f’ is applied to three value arguments,
+        but its type ‘Int -> Int -> Int’ has only two
+
+That is that `mkFunTysMsg` tries to do.  But what is the "type of the function".
+Most obviously, we can report its full, polymorphic type; that is simple and
+expliciable.  But sometimes a bit odd.  Consider
+    f :: Bool -> t Int Int
+    foo = f True 5 10
+We get this error:
+    • Couldn't match type ‘Int’ with ‘t0 -> t’
+      Expected: Int -> t0 -> t
+        Actual: Int -> Int
+    • The function ‘f’ is applied to three value arguments,
+        but its type ‘Bool -> t Int Int’ has only one
+
+That's not /quite/ right beause we can instantiate `t` to an arrow and get
+two arrows (but not three!).  With that in mind, one could consider reporting
+the /instantiated/ type, and GHC used to do so.  But it's more work, and in
+some ways more confusing, especially when nested quantifiers are concerned, e.g.
+    f :: Bool -> forall t. t Int Int
+
+So we just keep it simple and report the original function type.
+
+
+************************************************************************
 *                                                                      *
                     Other matchExpected functions
 *                                                                      *



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/acdc948dd1025868a56914c353bf3e8b053e74eb

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/acdc948dd1025868a56914c353bf3e8b053e74eb
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240130/5120995d/attachment-0001.html>


More information about the ghc-commits mailing list