How to catch errors/unsolved constraints from GHC type checker?

Simon Peyton Jones simonpj at microsoft.com
Mon Sep 22 19:07:34 UTC 2014


I'm sorry about being slow to reply -- utterly swamped.

|    2. 'tcSubType' does not generate errors, just postponed constraints,
| which are checked later in a clean-up phase.  Thus, I do not get an
| error now I could handle, but the error is generated later during the
| constraint solving phase, where I do not have a handler in place.

This is dead right, and I think it's a huge strength of GHC's current type inference mechanism.  Essentially all type error messages are generated in TcErrors, not in places scattered over the compiler.  This is not a "clean-up phase".  It is *the* place that type errors are generated.

For tcSubType, the way to control your error messages is through the "CtOrigin" and "UserTypeCtxt" passed to tcSubType.

The UserTypeCtxt is attached (via the ic_info field of Implication) to the implication constraint generated by the tcSubType.  If you want, you can add an extra constructor to UserTypeCtxt.

The CtOrigin is attached to the instantiated constraints generated by the sub-type check.

Both are accessible to the error-message generation in TcErrors.

In short, instead of catching the error(s) *now*, GHC just attaches enough information to the constraints that you can generate a good error message later.

I hope that is of some help.

Simon




| -----Original Message-----
| From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Andreas
| Abel
| Sent: 22 September 2014 14:44
| To: ghc-devs at haskell.org
| Subject: How to catch errors/unsolved constraints from GHC type checker?
| 
| Hi, I am stuck fixing issue #9582
| 
|    https://ghc.haskell.org/trac/ghc/ticket/9582
| 
| I need to test whether one type is a subtype of another and throw a
| custom error if this is not the case.  I tried
| 
|    (errMsgs, result) <- tryTcErrs $
|      tcSubType ctOrigin userTypeCtxt local_meth_ty sig_ty
| 
|    case result of
|      Just _coercion -> return ()
|      Nothing -> badInstSigErr sel_name local_meth_ty
| 
| However, it does not seem to do what I expect.  It acts as if I had not
| tried to catch the error, it acts as just
| 
|    tcSubType ctOrigin userTypeCtxt local_meth_ty sig_ty
| 
| My hypothetical explanations:
| 
|    1. 'tryTcErrs' is not the correct function to catch errors in the
| type checker.  Or,
| 
|    2. 'tcSubType' does not generate errors, just postponed constraints,
| which are checked later in a clean-up phase.  Thus, I do not get an
| error now I could handle, but the error is generated later during the
| constraint solving phase, where I do not have a handler in place.  In
| this case, I would need something like Agda's noConstraints,
| 
|    tryTcErrs $ noConstraints $ tcSubType...
| 
| which forces 'tcSubType' to check without generating new unification
| constraints.
| 
| Any advice how to do is or where to continue searching?
| 
| Thanks,
| Andreas
| 
| 
| --
| Andreas Abel  <><      Du bist der geliebte Mensch.
| 
| Department of Computer Science and Engineering
| Chalmers and Gothenburg University, Sweden
| 
| andreas.abel at gu.se
| http://www2.tcs.ifi.lmu.de/~abel/
| _______________________________________________
| ghc-devs mailing list
| ghc-devs at haskell.org
| http://www.haskell.org/mailman/listinfo/ghc-devs


More information about the ghc-devs mailing list