TcSimplify.hs 90.6 KB
Newer Older
1 2
{-# LANGUAGE CPP #-}

3
module TcSimplify(
4
       simplifyInfer, InferMode(..),
5
       growThetaTyVars,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
6
       simplifyAmbiguityCheck,
7
       simplifyDefault,
8 9
       simplifyTop, captureTopConstraints,
       simplifyInteractive, solveEqualities,
10
       simplifyWantedsTcM,
11
       tcCheckSatisfiability,
12

13 14
       -- For Rules we need these
       solveWanteds, runTcSDeriveds
15
  ) where
16

17
#include "HsVersions.h"
18

19
import Bag
20
import Class         ( Class, classKey, classTyCon )
21
import DynFlags      ( WarningFlag ( Opt_WarnMonomorphism )
22
                     , WarnReason ( Reason )
23
                     , DynFlags( solverIterations ) )
24
import Inst
25
import ListSetOps
26
import Maybes
27 28
import Name
import Outputable
29 30
import PrelInfo
import PrelNames
31 32 33
import TcErrors
import TcEvidence
import TcInteract
34
import TcCanonical   ( makeSuperClasses )
35
import TcMType   as TcM
36
import TcRnMonad as TcM
37 38 39
import TcSMonad  as TcS
import TcType
import TrieMap       () -- DV: for now
40
import Type
Richard Eisenberg's avatar
Richard Eisenberg committed
41
import TysWiredIn    ( liftedRepTy )
Richard Eisenberg's avatar
Richard Eisenberg committed
42
import Unify         ( tcMatchTyKi )
43 44 45
import Util
import Var
import VarSet
niteria's avatar
niteria committed
46
import UniqFM
47 48
import BasicTypes    ( IntWithInf, intGtLimit )
import ErrUtils      ( emptyMessages )
49
import qualified GHC.LanguageExtensions as LangExt
50

51
import Control.Monad ( when, unless )
52
import Data.List     ( partition )
53

Austin Seipp's avatar
Austin Seipp committed
54
{-
55
*********************************************************************************
56
*                                                                               *
57 58 59
*                           External interface                                  *
*                                                                               *
*********************************************************************************
Austin Seipp's avatar
Austin Seipp committed
60
-}
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
captureTopConstraints :: TcM a -> TcM (a, WantedConstraints)
-- (captureTopConstraints m) runs m, and returns the type constraints it
-- generates plus the constraints produced by static forms inside.
-- If it fails with an exception, it reports any insolubles
-- (out of scope variables) before doing so
captureTopConstraints thing_inside
  = do { static_wc_var <- TcM.newTcRef emptyWC ;
       ; (mb_res, lie) <- TcM.updGblEnv (\env -> env { tcg_static_wc = static_wc_var } ) $
                          TcM.tryCaptureConstraints thing_inside
       ; stWC <- TcM.readTcRef static_wc_var

       -- See TcRnMonad Note [Constraints and errors]
       -- If the thing_inside threw an exception, but generated some insoluble
       -- constraints, report the latter before propagating the exception
       -- Otherwise they will be lost altogether
       ; case mb_res of
           Right res -> return (res, lie `andWC` stWC)
           Left {}   -> do { _ <- reportUnsolved lie; failM } }
                -- This call to reportUnsolved is the reason
                -- this function is here instead of TcRnMonad

83 84
simplifyTop :: WantedConstraints -> TcM (Bag EvBind)
-- Simplify top-level constraints
85 86 87
-- Usually these will be implications,
-- but when there is nothing to quantify we don't wrap
-- in a degenerate implication, so we do that here instead
88
simplifyTop wanteds
89
  = do { traceTc "simplifyTop {" $ text "wanted = " <+> ppr wanteds
Simon Peyton Jones's avatar
Simon Peyton Jones committed
90 91 92 93
       ; ((final_wc, unsafe_ol), binds1) <- runTcS $
            do { final_wc <- simpl_top wanteds
               ; unsafe_ol <- getSafeOverlapFailures
               ; return (final_wc, unsafe_ol) }
94 95 96
       ; traceTc "End simplifyTop }" empty

       ; traceTc "reportUnsolved {" empty
97
       ; binds2 <- reportUnsolved final_wc
98
       ; traceTc "reportUnsolved }" empty
99

100 101 102 103
       ; traceTc "reportUnsolved (unsafe overlapping) {" empty
       ; unless (isEmptyCts unsafe_ol) $ do {
           -- grab current error messages and clear, warnAllUnsolved will
           -- update error messages which we'll grab and then restore saved
104
           -- messages.
105
           ; errs_var  <- getErrsVar
106 107
           ; saved_msg <- TcM.readTcRef errs_var
           ; TcM.writeTcRef errs_var emptyMessages
108 109 110 111 112

           ; warnAllUnsolved $ WC { wc_simple = unsafe_ol
                                  , wc_insol = emptyCts
                                  , wc_impl = emptyBag }

113 114
           ; whyUnsafe <- fst <$> TcM.readTcRef errs_var
           ; TcM.writeTcRef errs_var saved_msg
115 116 117 118
           ; recordUnsafeInfer whyUnsafe
           }
       ; traceTc "reportUnsolved (unsafe overlapping) }" empty

119 120 121
       ; return (evBindMapBinds binds1 `unionBags` binds2) }

-- | Type-check a thing that emits only equality constraints, then
122
-- solve those constraints. Fails outright if there is trouble.
123 124
solveEqualities :: TcM a -> TcM a
solveEqualities thing_inside
125 126
  = checkNoErrs $  -- See Note [Fail fast on kind errors]
    do { (result, wanted) <- captureConstraints thing_inside
127
       ; traceTc "solveEqualities {" $ text "wanted = " <+> ppr wanted
Simon Peyton Jones's avatar
Simon Peyton Jones committed
128
       ; final_wc <- runTcSEqualities $ simpl_top wanted
129 130 131 132 133 134
       ; traceTc "End solveEqualities }" empty

       ; traceTc "reportAllUnsolved {" empty
       ; reportAllUnsolved final_wc
       ; traceTc "reportAllUnsolved }" empty
       ; return result }
135

Simon Peyton Jones's avatar
Simon Peyton Jones committed
136
simpl_top :: WantedConstraints -> TcS WantedConstraints
137
    -- See Note [Top-level Defaulting Plan]
138
simpl_top wanteds
139
  = do { wc_first_go <- nestTcS (solveWantedsAndDrop wanteds)
140
                            -- This is where the main work happens
Simon Peyton Jones's avatar
Simon Peyton Jones committed
141
       ; try_tyvar_defaulting wc_first_go }
142
  where
143 144
    try_tyvar_defaulting :: WantedConstraints -> TcS WantedConstraints
    try_tyvar_defaulting wc
145
      | isEmptyWC wc
146 147
      = return wc
      | otherwise
148 149
      = do { free_tvs <- TcS.zonkTyCoVarsAndFVList (tyCoVarsOfWCList wc)
           ; let meta_tvs = filter (isTyVar <&&> isMetaTyVar) free_tvs
150
                   -- zonkTyCoVarsAndFV: the wc_first_go is not yet zonked
151
                   -- filter isMetaTyVar: we might have runtime-skolems in GHCi,
152
                   -- and we definitely don't want to try to assign to those!
153
                   -- The isTyVar is needed to weed out coercion variables
154

155 156 157
           ; defaulted <- mapM defaultTyVarTcS meta_tvs   -- Has unification side effects
           ; if or defaulted
             then do { wc_residual <- nestTcS (solveWanteds wc)
158
                            -- See Note [Must simplify after defaulting]
159 160
                     ; try_class_defaulting wc_residual }
             else try_class_defaulting wc }     -- No defaulting took place
161

162 163
    try_class_defaulting :: WantedConstraints -> TcS WantedConstraints
    try_class_defaulting wc
Austin Seipp's avatar
Austin Seipp committed
164
      | isEmptyWC wc
165 166
      = return wc
      | otherwise  -- See Note [When to do type-class defaulting]
167
      = do { something_happened <- applyDefaultingRules wc
168
                                   -- See Note [Top-level Defaulting Plan]
169
           ; if something_happened
170
             then do { wc_residual <- nestTcS (solveWantedsAndDrop wc)
171
                     ; try_class_defaulting wc_residual }
Simon Peyton Jones's avatar
Simon Peyton Jones committed
172
                  -- See Note [Overview of implicit CallStacks] in TcEvidence
173 174 175 176 177 178 179 180 181 182 183 184 185 186
             else try_callstack_defaulting wc }

    try_callstack_defaulting :: WantedConstraints -> TcS WantedConstraints
    try_callstack_defaulting wc
      | isEmptyWC wc
      = return wc
      | otherwise
      = defaultCallStacks wc

-- | Default any remaining @CallStack@ constraints to empty @CallStack@s.
defaultCallStacks :: WantedConstraints -> TcS WantedConstraints
-- See Note [Overview of implicit CallStacks] in TcEvidence
defaultCallStacks wanteds
  = do simples <- handle_simples (wc_simple wanteds)
187 188 189
       mb_implics <- mapBagM handle_implic (wc_impl wanteds)
       return (wanteds { wc_simple = simples
                       , wc_impl = catBagMaybes mb_implics })
190 191 192 193 194 195

  where

  handle_simples simples
    = catBagMaybes <$> mapBagM defaultCallStack simples

196 197 198
  handle_implic :: Implication -> TcS (Maybe Implication)
  -- The Maybe is because solving the CallStack constraint
  -- may well allow us to discard the implication entirely
199
  handle_implic implic
200 201 202
    | isSolvedStatus (ic_status implic)
    = return (Just implic)
    | otherwise
203 204 205 206
    = do { wanteds <- setEvBindsTcS (ic_binds implic) $
                      -- defaultCallStack sets a binding, so
                      -- we must set the correct binding group
                      defaultCallStacks (ic_wanted implic)
207
         ; setImplicationStatus (implic { ic_wanted = wanteds }) }
208 209 210 211 212 213 214 215

  defaultCallStack ct
    | Just _ <- isCallStackPred (ctPred ct)
    = do { solveCallStack (cc_ev ct) EvCsEmpty
         ; return Nothing }

  defaultCallStack ct
    = return (Just ct)
216

217

218 219 220 221 222
{- Note [Fail fast on kind errors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
solveEqualities is used to solve kind equalities when kind-checking
user-written types. If solving fails we should fail outright, rather
than just accumulate an error message, for two reasons:
Simon Peyton Jones's avatar
Simon Peyton Jones committed
223

224 225 226
  * A kind-bogus type signature may cause a cascade of knock-on
    errors if we let it pass

Simon Peyton Jones's avatar
Simon Peyton Jones committed
227 228 229 230 231
  * More seriously, we don't have a convenient term-level place to add
    deferred bindings for unsolved kind-equality constraints, so we
    don't build evidence bindings (by usine reportAllUnsolved). That
    means that we'll be left with with a type that has coercion holes
    in it, something like
232 233 234 235 236 237 238
           <type> |> co-hole
    where co-hole is not filled in.  Eeek!  That un-filled-in
    hole actually causes GHC to crash with "fvProv falls into a hole"
    See Trac #11563, #11520, #11516, #11399

So it's important to use 'checkNoErrs' here!

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
Note [When to do type-class defaulting]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In GHC 7.6 and 7.8.2, we did type-class defaulting only if insolubleWC
was false, on the grounds that defaulting can't help solve insoluble
constraints.  But if we *don't* do defaulting we may report a whole
lot of errors that would be solved by defaulting; these errors are
quite spurious because fixing the single insoluble error means that
defaulting happens again, which makes all the other errors go away.
This is jolly confusing: Trac #9033.

So it seems better to always do type-class defaulting.

However, always doing defaulting does mean that we'll do it in
situations like this (Trac #5934):
   run :: (forall s. GenST s) -> Int
Austin Seipp's avatar
Austin Seipp committed
254
   run = fromInteger 0
255 256 257
We don't unify the return type of fromInteger with the given function
type, because the latter involves foralls.  So we're left with
    (Num alpha, alpha ~ (forall s. GenST s) -> Int)
Austin Seipp's avatar
Austin Seipp committed
258 259
Now we do defaulting, get alpha := Integer, and report that we can't
match Integer with (forall s. GenST s) -> Int.  That's not totally
260 261 262 263 264 265
stupid, but perhaps a little strange.

Another potential alternative would be to suppress *all* non-insoluble
errors if there are *any* insoluble errors, anywhere, but that seems
too drastic.

266 267 268 269 270 271 272 273 274
Note [Must simplify after defaulting]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We may have a deeply buried constraint
    (t:*) ~ (a:Open)
which we couldn't solve because of the kind incompatibility, and 'a' is free.
Then when we default 'a' we can solve the constraint.  And we want to do
that before starting in on type classes.  We MUST do it before reporting
errors, because it isn't an error!  Trac #7967 was due to this.

275 276
Note [Top-level Defaulting Plan]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277 278
We have considered two design choices for where/when to apply defaulting.
   (i) Do it in SimplCheck mode only /whenever/ you try to solve some
279
       simple constraints, maybe deep inside the context of implications.
280
       This used to be the case in GHC 7.4.1.
281
   (ii) Do it in a tight loop at simplifyTop, once all other constraints have
282 283
        finished. This is the current story.

284
Option (i) had many disadvantages:
285 286
   a) Firstly, it was deep inside the actual solver.
   b) Secondly, it was dependent on the context (Infer a type signature,
287 288
      or Check a type signature, or Interactive) since we did not want
      to always start defaulting when inferring (though there is an exception to
289
      this, see Note [Default while Inferring]).
290 291 292 293 294 295 296 297 298 299 300
   c) It plainly did not work. Consider typecheck/should_compile/DfltProb2.hs:
          f :: Int -> Bool
          f x = const True (\y -> let w :: a -> a
                                      w a = const a (y+1)
                                  in w y)
      We will get an implication constraint (for beta the type of y):
               [untch=beta] forall a. 0 => Num beta
      which we really cannot default /while solving/ the implication, since beta is
      untouchable.

Instead our new defaulting story is to pull defaulting out of the solver loop and
301
go with option (ii), implemented at SimplifyTop. Namely:
302 303
     - First, have a go at solving the residual constraint of the whole
       program
304 305
     - Try to approximate it with a simple constraint
     - Figure out derived defaulting equations for that simple constraint
306 307 308 309
     - Go round the loop again if you did manage to get some equations

Now, that has to do with class defaulting. However there exists type variable /kind/
defaulting. Again this is done at the top-level and the plan is:
310
     - At the top-level, once you had a go at solving the constraint, do
Gabor Greif's avatar
Gabor Greif committed
311
       figure out /all/ the touchable unification variables of the wanted constraints.
312 313 314
     - Apply defaulting to their kinds

More details in Note [DefaultTyVar].
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

Note [Safe Haskell Overlapping Instances]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In Safe Haskell, we apply an extra restriction to overlapping instances. The
motive is to prevent untrusted code provided by a third-party, changing the
behavior of trusted code through type-classes. This is due to the global and
implicit nature of type-classes that can hide the source of the dictionary.

Another way to state this is: if a module M compiles without importing another
module N, changing M to import N shouldn't change the behavior of M.

Overlapping instances with type-classes can violate this principle. However,
overlapping instances aren't always unsafe. They are just unsafe when the most
selected dictionary comes from untrusted code (code compiled with -XSafe) and
overlaps instances provided by other modules.

In particular, in Safe Haskell at a call site with overlapping instances, we
apply the following rule to determine if it is a 'unsafe' overlap:

 1) Most specific instance, I1, defined in an `-XSafe` compiled module.
 2) I1 is an orphan instance or a MPTC.
 3) At least one overlapped instance, Ix, is both:
    A) from a different module than I1
    B) Ix is not marked `OVERLAPPABLE`

This is a slightly involved heuristic, but captures the situation of an
imported module N changing the behavior of existing code. For example, if
condition (2) isn't violated, then the module author M must depend either on a
type-class or type defined in N.

Secondly, when should these heuristics be enforced? We enforced them when the
type-class method call site is in a module marked `-XSafe` or `-XTrustworthy`.
This allows `-XUnsafe` modules to operate without restriction, and for Safe
Haskell inferrence to infer modules with unsafe overlaps as unsafe.

One alternative design would be to also consider if an instance was imported as
a `safe` import or not and only apply the restriction to instances imported
safely. However, since instances are global and can be imported through more
than one path, this alternative doesn't work.

Note [Safe Haskell Overlapping Instances Implementation]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

358
How is this implemented? It's complicated! So we'll step through it all:
359 360

 1) `InstEnv.lookupInstEnv` -- Performs instance resolution, so this is where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
361 362 363 364
    we check if a particular type-class method call is safe or unsafe. We do this
    through the return type, `ClsInstLookupResult`, where the last parameter is a
    list of instances that are unsafe to overlap. When the method call is safe,
    the list is null.
365

366
 2) `TcInteract.matchClassInst` -- This module drives the instance resolution
Simon Peyton Jones's avatar
Simon Peyton Jones committed
367 368 369
    / dictionary generation. The return type is `LookupInstResult`, which either
    says no instance matched, or one found, and if it was a safe or unsafe
    overlap.
370 371

 3) `TcInteract.doTopReactDict` -- Takes a dictionary / class constraint and
Simon Peyton Jones's avatar
Simon Peyton Jones committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
     tries to resolve it by calling (in part) `matchClassInst`. The resolving
     mechanism has a work list (of constraints) that it process one at a time. If
     the constraint can't be resolved, it's added to an inert set. When compiling
     an `-XSafe` or `-XTrustworthy` module, we follow this approach as we know
     compilation should fail. These are handled as normal constraint resolution
     failures from here-on (see step 6).

     Otherwise, we may be inferring safety (or using `-Wunsafe`), and
     compilation should succeed, but print warnings and/or mark the compiled module
     as `-XUnsafe`. In this case, we call `insertSafeOverlapFailureTcS` which adds
     the unsafe (but resolved!) constraint to the `inert_safehask` field of
     `InertCans`.

 4) `TcSimplify.simplifyTop`:
       * Call simpl_top, the top-level function for driving the simplifier for
         constraint resolution.

       * Once finished, call `getSafeOverlapFailures` to retrieve the
         list of overlapping instances that were successfully resolved,
         but unsafe. Remember, this is only applicable for generating warnings
         (`-Wunsafe`) or inferring a module unsafe. `-XSafe` and `-XTrustworthy`
         cause compilation failure by not resolving the unsafe constraint at all.

       * For unresolved constraints (all types), call `TcErrors.reportUnsolved`,
         while for resolved but unsafe overlapping dictionary constraints, call
         `TcErrors.warnAllUnsolved`. Both functions convert constraints into a
         warning message for the user.

       * In the case of `warnAllUnsolved` for resolved, but unsafe
         dictionary constraints, we collect the generated warning
         message (pop it) and call `TcRnMonad.recordUnsafeInfer` to
         mark the module we are compiling as unsafe, passing the
         warning message along as the reason.

 5) `TcErrors.*Unsolved` -- Generates error messages for constraints by
    actually calling `InstEnv.lookupInstEnv` again! Yes, confusing, but all we
    know is the constraint that is unresolved or unsafe. For dictionary, all we
    know is that we need a dictionary of type C, but not what instances are
    available and how they overlap. So we once again call `lookupInstEnv` to
    figure that out so we can generate a helpful error message.

 6) `TcRnMonad.recordUnsafeInfer` -- Save the unsafe result and reason in an
      IORef called `tcg_safeInfer`.

 7) `HscMain.tcRnModule'` -- Reads `tcg_safeInfer` after type-checking, calling
    `HscMain.markUnsafeInfer` (passing the reason along) when safe-inferrence
    failed.
419 420 421 422 423 424 425 426 427 428

Note [No defaulting in the ambiguity check]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When simplifying constraints for the ambiguity check, we use
solveWantedsAndDrop, not simpl_top, so that we do no defaulting.
Trac #11947 was an example:
   f :: Num a => Int -> Int
This is ambiguous of course, but we don't want to default the
(Num alpha) constraint to (Num Int)!  Doing so gives a defaulting
warning, but no error.
Austin Seipp's avatar
Austin Seipp committed
429
-}
430

431
------------------
432 433 434
simplifyAmbiguityCheck :: Type -> WantedConstraints -> TcM ()
simplifyAmbiguityCheck ty wanteds
  = do { traceTc "simplifyAmbiguityCheck {" (text "type = " <+> ppr ty $$ text "wanted = " <+> ppr wanteds)
435 436 437
       ; (final_wc, _) <- runTcS $ solveWantedsAndDrop wanteds
             -- NB: no defaulting!  See Note [No defaulting in the ambiguity check]

438 439 440 441 442
       ; traceTc "End simplifyAmbiguityCheck }" empty

       -- Normally report all errors; but with -XAllowAmbiguousTypes
       -- report only insoluble ones, since they represent genuinely
       -- inaccessible code
443
       ; allow_ambiguous <- xoptM LangExt.AllowAmbiguousTypes
444
       ; traceTc "reportUnsolved(ambig) {" empty
445
       ; unless (allow_ambiguous && not (insolubleWC final_wc))
446
                (discardResult (reportUnsolved final_wc))
447 448 449 450
       ; traceTc "reportUnsolved(ambig) }" empty

       ; return () }

451 452
------------------
simplifyInteractive :: WantedConstraints -> TcM (Bag EvBind)
453
simplifyInteractive wanteds
454
  = traceTc "simplifyInteractive" empty >>
455
    simplifyTop wanteds
456 457

------------------
458
simplifyDefault :: ThetaType    -- Wanted; has no type variables in it
459
                -> TcM ()       -- Succeeds if the constraint is soluble
460
simplifyDefault theta
461
  = do { traceTc "simplifyDefault" empty
462 463
       ; wanteds  <- newWanteds DefaultOrigin theta
       ; unsolved <- runTcSDeriveds (solveWantedsAndDrop (mkSimpleWC wanteds))
464
       ; traceTc "reportUnsolved {" empty
465
       ; reportAllUnsolved unsolved
466
       ; traceTc "reportUnsolved }" empty
467
       ; return () }
468

469 470 471
------------------
tcCheckSatisfiability :: Bag EvVar -> TcM Bool
-- Return True if satisfiable, False if definitely contradictory
472
tcCheckSatisfiability given_ids
473
  = do { lcl_env <- TcM.getLclEnv
474 475
       ; let given_loc = mkGivenLoc topTcLevel UnkSkol lcl_env
       ; (res, _ev_binds) <- runTcS $
476
             do { traceTcS "checkSatisfiability {" (ppr given_ids)
477
                ; let given_cts = mkGivens given_loc (bagToList given_ids)
478
                     -- See Note [Superclasses and satisfiability]
479 480
                ; solveSimpleGivens given_cts
                ; insols <- getInertInsols
481 482 483 484
                ; insols <- try_harder insols
                ; traceTcS "checkSatisfiability }" (ppr insols)
                ; return (isEmptyBag insols) }
       ; return res }
485
 where
486 487 488
    try_harder :: Cts -> TcS Cts
    -- Maybe we have to search up the superclass chain to find
    -- an unsatisfiable constraint.  Example: pmcheck/T3927b.
489
    -- At the moment we try just once
490 491 492 493 494 495
    try_harder insols
      | not (isEmptyBag insols)   -- We've found that it's definitely unsatisfiable
      = return insols             -- Hurrah -- stop now.
      | otherwise
      = do { pending_given <- getPendingScDicts
           ; new_given <- makeSuperClasses pending_given
496 497
           ; solveSimpleGivens new_given
           ; getInertInsols }
498

499
{- Note [Superclasses and satisfiability]
500 501 502 503 504 505 506 507 508 509 510 511 512
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand superclasses before starting, because (Int ~ Bool), has
(Int ~~ Bool) as a superclass, which in turn has (Int ~N# Bool)
as a superclass, and it's the latter that is insoluble.  See
Note [The equality types story] in TysPrim.

If we fail to prove unsatisfiability we (arbitrarily) try just once to
find superclasses, using try_harder.  Reason: we might have a type
signature
   f :: F op (Implements push) => ..
where F is a type function.  This happened in Trac #3972.

We could do more than once but we'd have to have /some/ limit: in the
513
the recursive case, we would go on forever in the common case where
514 515 516 517 518
the constraints /are/ satisfiable (Trac #10592 comment:12!).

For stratightforard situations without type functions the try_harder
step does nothing.

519

520
***********************************************************************************
521
*                                                                                 *
522 523 524
*                            Inference
*                                                                                 *
***********************************************************************************
525

526 527 528 529 530 531 532 533
Note [Inferring the type of a let-bound variable]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
   f x = rhs

To infer f's type we do the following:
 * Gather the constraints for the RHS with ambient level *one more than*
   the current one.  This is done by the call
534
        pushLevelAndCaptureConstraints (tcMonoBinds...)
535 536 537 538
   in TcBinds.tcPolyInfer

 * Call simplifyInfer to simplify the constraints and decide what to
   quantify over. We pass in the level used for the RHS constraints,
539
   here called rhs_tclvl.
540 541 542 543

This ensures that the implication constraint we generate, if any,
has a strictly-increased level compared to the ambient level outside
the let binding.
544

Austin Seipp's avatar
Austin Seipp committed
545
-}
546

547 548 549 550 551 552 553 554 555 556 557 558 559 560
-- | How should we choose which constraints to quantify over?
data InferMode = ApplyMR          -- ^ Apply the monomorphism restriction,
                                  -- never quantifying over any constraints
               | EagerDefaulting  -- ^ See Note [TcRnExprMode] in TcRnDriver,
                                  -- the :type +d case; this mode refuses
                                  -- to quantify over any defaultable constraint
               | NoRestrictions   -- ^ Quantify over any constraint that
                                  -- satisfies TcType.pickQuantifiablePreds

instance Outputable InferMode where
  ppr ApplyMR         = text "ApplyMR"
  ppr EagerDefaulting = text "EagerDefaulting"
  ppr NoRestrictions  = text "NoRestrictions"

561
simplifyInfer :: TcLevel               -- Used when generating the constraints
562
              -> InferMode
563
              -> [TcIdSigInst]         -- Any signatures (possibly partial)
564 565
              -> [(Name, TcTauType)]   -- Variables to be generalised,
                                       -- and their tau-types
566
              -> WantedConstraints
567
              -> TcM ([TcTyVar],    -- Quantify over these type variables
568
                      [EvVar],      -- ... and these constraints (fully zonked)
569
                      TcEvBinds)    -- ... binding these evidence variables
570
simplifyInfer rhs_tclvl infer_mode sigs name_taus wanteds
571
  | isEmptyWC wanteds
572
  = do { gbl_tvs <- tcGetGlobalTyCoVars
573
       ; dep_vars <- zonkTcTypesAndSplitDepVars (map snd name_taus)
574
       ; qtkvs <- quantifyZonkedTyVars gbl_tvs dep_vars
575
       ; traceTc "simplifyInfer: empty WC" (ppr name_taus $$ ppr qtkvs)
576
       ; return (qtkvs, [], emptyTcEvBinds) }
577

578
  | otherwise
579
  = do { traceTc "simplifyInfer {"  $ vcat
580 581 582
             [ text "sigs =" <+> ppr sigs
             , text "binds =" <+> ppr name_taus
             , text "rhs_tclvl =" <+> ppr rhs_tclvl
583
             , text "infer_mode =" <+> ppr infer_mode
584
             , text "(unzonked) wanted =" <+> ppr wanteds
585 586
             ]

587 588
       ; let partial_sigs = filter isPartialSig sigs
             psig_theta   = concatMap sig_inst_theta partial_sigs
589

590 591 592 593 594 595 596
       -- First do full-blown solving
       -- NB: we must gather up all the bindings from doing
       -- this solving; hence (runTcSWithEvBinds ev_binds_var).
       -- And note that since there are nested implications,
       -- calling solveWanteds will side-effect their evidence
       -- bindings, so we can't just revert to the input
       -- constraint.
597

598 599 600 601 602
       ; tc_lcl_env      <- TcM.getLclEnv
       ; ev_binds_var    <- TcM.newTcEvBinds
       ; psig_theta_vars <- mapM TcM.newEvVar psig_theta
       ; wanted_transformed_incl_derivs
            <- setTcLevel rhs_tclvl $
603
               runTcSWithEvBinds ev_binds_var $
604
               do { let loc = mkGivenLoc rhs_tclvl UnkSkol tc_lcl_env
605
                        psig_givens = mkGivens loc psig_theta_vars
606 607 608
                  ; _ <- solveSimpleGivens psig_givens
                         -- See Note [Add signature contexts as givens]
                  ; solveWanteds wanteds }
609
       ; wanted_transformed_incl_derivs <- TcM.zonkWC wanted_transformed_incl_derivs
610

611 612
       -- Find quant_pred_candidates, the predicates that
       -- we'll consider quantifying over
613 614 615 616
       -- NB1: wanted_transformed does not include anything provable from
       --      the psig_theta; it's just the extra bit
       -- NB2: We do not do any defaulting when inferring a type, this can lead
       --      to less polymorphic types, see Note [Default while Inferring]
617

618
       ; let wanted_transformed = dropDerivedWC wanted_transformed_incl_derivs
619
       ; quant_pred_candidates   -- Fully zonked
620
           <- if insolubleWC wanted_transformed_incl_derivs
621
              then return []   -- See Note [Quantification with errors]
622
                               -- NB: must include derived errors in this test,
623 624
                               --     hence "incl_derivs"

625
              else do { let quant_cand = approximateWC False wanted_transformed
niteria's avatar
niteria committed
626 627
                            meta_tvs   = filter isMetaTyVar $
                                         tyCoVarsOfCtsList quant_cand
628 629

                      ; gbl_tvs <- tcGetGlobalTyCoVars
630 631
                            -- Miminise quant_cand.  We are not interested in any evidence
                            -- produced, because we are going to simplify wanted_transformed
632
                            -- again later. All we want here are the predicates over which to
633
                            -- quantify.
634
                            --
635 636
                            -- If any meta-tyvar unifications take place (unlikely),
                            -- we'll pick that up later.
637

638 639 640 641 642 643 644
                      -- See Note [Promote _and_ default when inferring]
                      ; let def_tyvar tv
                              = when (not $ tv `elemVarSet` gbl_tvs) $
                                defaultTyVar tv
                      ; mapM_ def_tyvar meta_tvs
                      ; mapM_ (promoteTyVar rhs_tclvl) meta_tvs

645
                      ; clone_wanteds <- mapM cloneWanted (bagToList quant_cand)
646
                      ; WC { wc_simple = simples }
647
                           <- setTcLevel rhs_tclvl $
648 649
                              simplifyWantedsTcM clone_wanteds
                              -- Discard evidence; result is zonked
650

651
                      ; return [ ctEvPred ev | ct <- bagToList simples
652
                                             , let ev = ctEvidence ct ] }
653

654
       -- NB: quant_pred_candidates is already fully zonked
655

656
       -- Decide what type variables and constraints to quantify
657 658
       -- NB: bound_theta are constraints we want to quantify over,
       --     /apart from/ the psig_theta, which we always quantify over
659
       ; (qtvs, bound_theta) <- decideQuantification infer_mode name_taus psig_theta
660
                                                     quant_pred_candidates
661 662 663 664 665 666 667 668 669

         -- Promote any type variables that are free in the inferred type
         -- of the function:
         --    f :: forall qtvs. bound_theta => zonked_tau
         -- These variables now become free in the envt, and hence will show
         -- up whenever 'f' is called.  They may currently at rhs_tclvl, but
         -- they had better be unifiable at the outer_tclvl!
         -- Example:   envt mentions alpha[1]
         --            tau_ty = beta[2] -> beta[2]
670
         --            constraints = alpha ~ [beta]
671 672 673
         -- we don't quantify over beta (since it is fixed by envt)
         -- so we must promote it!  The inferred type is just
         --   f :: beta -> beta
674
       ; zonked_taus <- mapM (TcM.zonkTcType . snd) name_taus
675 676 677
              -- decideQuantification turned some meta tyvars into
              -- quantified skolems, so we have to zonk again

678
       ; let phi_tkvs = tyCoVarsOfTypes bound_theta  -- Already zonked
679
                        `unionVarSet` tyCoVarsOfTypes zonked_taus
680
             promote_tkvs = closeOverKinds phi_tkvs `delVarSetList` qtvs
681

682 683 684 685 686
       ; MASSERT2( closeOverKinds promote_tkvs `subVarSet` promote_tkvs
                 , ppr phi_tkvs $$
                   ppr (closeOverKinds phi_tkvs) $$
                   ppr promote_tkvs $$
                   ppr (closeOverKinds promote_tkvs) )
687 688 689
           -- we really don't want a type to be promoted when its kind isn't!

           -- promoteTyVar ignores coercion variables
690
       ; outer_tclvl <- TcM.getTcLevel
niteria's avatar
niteria committed
691 692 693
       ; mapM_ (promoteTyVar outer_tclvl) (nonDetEltsUFM promote_tkvs)
           -- It's OK to use nonDetEltsUFM here because promoteTyVar is
           -- commutative
694 695 696

           -- Emit an implication constraint for the
           -- remaining constraints from the RHS
697
           -- extra_qtvs: see Note [Quantification and partial signatures]
698
       ; bound_theta_vars <- mapM TcM.newEvVar bound_theta
699
       ; psig_theta_vars  <- mapM zonkId psig_theta_vars
700 701 702 703
       ; all_qtvs         <- add_psig_tvs qtvs
                             [ tv | sig <- partial_sigs
                                  , (_,tv) <- sig_inst_skols sig ]

704 705 706
       ; let full_theta      = psig_theta      ++ bound_theta
             full_theta_vars = psig_theta_vars ++ bound_theta_vars
             skol_info   = InferSkol [ (name, mkSigmaTy [] full_theta ty)
707
                                     | (name, ty) <- name_taus ]
708
                        -- Don't add the quantified variables here, because
709 710
                        -- they are also bound in ic_skols and we want them
                        -- to be tidied uniformly
711

712
             implic = Implic { ic_tclvl    = rhs_tclvl
713
                             , ic_skols    = all_qtvs
714
                             , ic_no_eqs   = False
715
                             , ic_given    = full_theta_vars
716
                             , ic_wanted   = wanted_transformed
717
                             , ic_status   = IC_Unsolved
718
                             , ic_binds    = ev_binds_var
719
                             , ic_info     = skol_info
720
                             , ic_needed   = emptyVarSet
721
                             , ic_env      = tc_lcl_env }
722
       ; emitImplication implic
723

724
         -- All done!
725
       ; traceTc "} simplifyInfer/produced residual implication for quantification" $
726
         vcat [ text "quant_pred_candidates =" <+> ppr quant_pred_candidates
727
              , text "promote_tvs=" <+> ppr promote_tkvs
728
              , text "psig_theta =" <+> ppr psig_theta
729
              , text "bound_theta =" <+> ppr bound_theta
730
              , text "full_theta =" <+> ppr full_theta
731 732
              , text "qtvs =" <+> ppr qtvs
              , text "implic =" <+> ppr implic ]
733

734
       ; return ( qtvs, full_theta_vars, TcEvBinds ev_binds_var ) }
735 736 737 738 739 740 741 742 743 744
  where
    add_psig_tvs qtvs [] = return qtvs
    add_psig_tvs qtvs (tv:tvs)
      = do { tv <- zonkTcTyVarToTyVar tv
           ; if tv `elem` qtvs
             then add_psig_tvs qtvs tvs
             else do { mb_tv <- zonkQuantifiedTyVar False tv
                     ; case mb_tv of
                         Nothing -> add_psig_tvs qtvs      tvs
                         Just tv -> add_psig_tvs (tv:qtvs) tvs } }
745 746 747

{- Note [Add signature contexts as givens]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
748 749 750
Consider this (Trac #11016):
  f2 :: (?x :: Int) => _
  f2 = ?x
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
or this
  f3 :: a ~ Bool => (a, _)
  f3 = (True, False)
or theis
  f4 :: (Ord a, _) => a -> Bool
  f4 x = x==x

We'll use plan InferGen because there are holes in the type.  But:
 * For f2 we want to have the (?x :: Int) constraint floating around
   so that the functional dependencies kick in.  Otherwise the
   occurrence of ?x on the RHS produces constraint (?x :: alpha), and
   we won't unify alpha:=Int.
 * For f3 we want the (a ~ Bool) available to solve the wanted (a ~ Bool)
   in the RHS
 * For f4 we want to use the (Ord a) in the signature to solve the Eq a
   constraint.
767 768

Solution: in simplifyInfer, just before simplifying the constraints
769 770
gathered from the RHS, add Given constraints for the context of any
type signatures.
771

Austin Seipp's avatar
Austin Seipp committed
772 773
************************************************************************
*                                                                      *
774
                Quantification
Austin Seipp's avatar
Austin Seipp committed
775 776
*                                                                      *
************************************************************************
777 778 779 780

Note [Deciding quantification]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the monomorphism restriction does not apply, then we quantify as follows:
781

782 783 784
  * Take the global tyvars, and "grow" them using the equality constraints
    E.g.  if x:alpha is in the environment, and alpha ~ [beta] (which can
          happen because alpha is untouchable here) then do not quantify over
785 786
          beta, because alpha fixes beta, and beta is effectively free in
          the environment too
787 788 789
    These are the mono_tvs

  * Take the free vars of the tau-type (zonked_tau_tvs) and "grow" them
790
    using all the constraints.  These are tau_tvs_plus
791

792 793 794 795
  * Use quantifyTyVars to quantify over (tau_tvs_plus - mono_tvs), being
    careful to close over kinds, and to skolemise the quantified tyvars.
    (This actually unifies each quantifies meta-tyvar with a fresh skolem.)
    Result is qtvs.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
796

797
  * Filter the constraints using pickQuantifiablePreds and the qtvs.
798 799
    We have to zonk the constraints first, so they "see" the freshly
    created skolems.
800

801 802 803
If the MR does apply, mono_tvs includes all the constrained tyvars --
including all covars -- and the quantified constraints are empty/insoluble.

Austin Seipp's avatar
Austin Seipp committed
804
-}
805

806
decideQuantification
807
  :: InferMode
808
  -> [(Name, TcTauType)]   -- Variables to be generalised
809 810
  -> [PredType]            -- All annotated constraints from signatures
  -> [PredType]            -- Candidate theta
811 812
  -> TcM ( [TcTyVar]       -- Quantify over these (skolems)
         , [PredType] )    -- and this context (fully zonked)
813
-- See Note [Deciding quantification]
814
decideQuantification infer_mode name_taus psig_theta candidates
815
  = do { gbl_tvs <- tcGetGlobalTyCoVars
816 817
       ; zonked_taus <- mapM TcM.zonkTcType (psig_theta ++ taus)
                        -- psig_theta: see Note [Quantification and partial signatures]
818 819
       ; ovl_strings <- xoptM LangExt.OverloadedStrings
       ; let DV {dv_kvs = zkvs, dv_tvs = ztvs} = splitDepVarsOfTypes zonked_taus
820
             (gbl_cand, quant_cand)  -- gbl_cand   = do not quantify me
821 822 823 824 825 826 827 828 829 830 831
                = case infer_mode of   -- quant_cand = try to quantify me
                    ApplyMR         -> (candidates, [])
                    NoRestrictions  -> ([], candidates)
                    EagerDefaulting -> partition is_interactive_ct candidates
                      where
                        is_interactive_ct ct
                          | Just (cls, _) <- getClassPredTys_maybe ct
                          = isInteractiveClass ovl_strings cls
                          | otherwise
                          = False

832 833 834 835 836 837 838
             eq_constraints  = filter isEqPred quant_cand
             constrained_tvs = tyCoVarsOfTypes gbl_cand
             mono_tvs        = growThetaTyVars eq_constraints $
                               gbl_tvs `unionVarSet` constrained_tvs
             tau_tvs_plus    = growThetaTyVarsDSet quant_cand ztvs
             dvs_plus        = DV { dv_kvs = zkvs, dv_tvs = tau_tvs_plus }

839
       ; qtvs <- quantifyZonkedTyVars mono_tvs dvs_plus
840 841 842 843 844
          -- We don't grow the kvs, as there's no real need to. Recall
          -- that quantifyTyVars uses the separation between kvs and tvs
          -- only for defaulting, and we don't want (ever) to default a tv
          -- to *. So, don't grow the kvs.

845
       ; quant_cand <- TcM.zonkTcTypes quant_cand
Simon Peyton Jones's avatar
Simon Peyton Jones committed
846
                 -- quantifyTyVars turned some meta tyvars into
847 848
                 -- quantified skolems, so we have to zonk again

849
       ; let qtv_set   = mkVarSet qtvs
850
             theta     = pickQuantifiablePreds qtv_set quant_cand
851 852 853
             min_theta = mkMinimalBySCs theta
               -- See Note [Minimize by Superclasses]

854 855
           -- Warn about the monomorphism restriction
       ; warn_mono <- woptM Opt_WarnMonomorphism
856 857 858 859
       ; let mr_bites | ApplyMR <- infer_mode
                      = constrained_tvs `intersectsVarSet` tcDepVarSet dvs_plus
                      | otherwise
                      = False
860 861 862 863 864 865 866
       ; warnTc (Reason Opt_WarnMonomorphism) (warn_mono && mr_bites) $
         hang (text "The Monomorphism Restriction applies to the binding"
               <> plural bndrs <+> text "for" <+> pp_bndrs)
             2 (text "Consider giving a type signature for"
                <+> if isSingleton bndrs then pp_bndrs
                                         else text "these binders")

867 868 869
       ; traceTc "decideQuantification"
           (vcat [ text "infer_mode:"   <+> ppr infer_mode
                 , text "gbl_cand:"     <+> ppr gbl_cand
870
                 , text "quant_cand:"   <+> ppr quant_cand
871 872 873 874 875
                 , text "gbl_tvs:"      <+> ppr gbl_tvs
                 , text "mono_tvs:"     <+> ppr mono_tvs
                 , text "tau_tvs_plus:" <+> ppr tau_tvs_plus
                 , text "qtvs:"         <+> ppr qtvs
                 , text "min_theta:"    <+> ppr min_theta ])
876
       ; return (qtvs, min_theta) }
877
  where
878
    pp_bndrs = pprWithCommas (quotes . ppr) bndrs
879
    (bndrs, taus) = unzip name_taus
880

881
------------------
882
growThetaTyVars :: ThetaType -> TyCoVarSet -> TyVarSet
883
-- See Note [Growing the tau-tvs using constraints]
884
-- NB: only returns tyvars, never covars
885
growThetaTyVars theta tvs
886 887 888
  | null theta = tvs_only
  | otherwise  = filterVarSet isTyVar $
                 transCloVarSet mk_next seed_tvs
889
  where
890 891
    tvs_only = filterVarSet isTyVar tvs
    seed_tvs = tvs `unionVarSet` tyCoVarsOfTypes ips
892
    (ips, non_ips) = partition isIPPred theta
893
                         -- See Note [Inheriting implicit parameters] in TcType
894 895 896 897 898 899

    mk_next :: VarSet -> VarSet -- Maps current set to newly-grown ones
    mk_next so_far = foldr (grow_one so_far) emptyVarSet non_ips
    grow_one so_far pred tvs
       | pred_tvs `intersectsVarSet` so_far = tvs `unionVarSet` pred_tvs
       | otherwise                          = tvs
900
       where
901
         pred_tvs = tyCoVarsOfType pred
902

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
------------------
growThetaTyVarsDSet :: ThetaType -> DTyCoVarSet -> DTyVarSet
-- See Note [Growing the tau-tvs using constraints]
-- NB: only returns tyvars, never covars
-- It takes a deterministic set of TyCoVars and returns a deterministic set
-- of TyVars.
-- The implementation mirrors growThetaTyVars, the only difference is that
-- it avoids unionDVarSet and uses more efficient extendDVarSetList.
growThetaTyVarsDSet theta tvs
  | null theta = tvs_only
  | otherwise  = filterDVarSet isTyVar $
                 transCloDVarSet mk_next seed_tvs
  where
    tvs_only = filterDVarSet isTyVar tvs
    seed_tvs = tvs `extendDVarSetList` tyCoVarsOfTypesList ips
    (ips, non_ips) = partition isIPPred theta
                         -- See Note [Inheriting implicit parameters] in TcType

    mk_next :: DVarSet -> DVarSet -- Maps current set to newly-grown ones
    mk_next so_far = foldr (grow_one so_far) emptyDVarSet non_ips
    grow_one so_far pred tvs
       | any (`elemDVarSet` so_far) pred_tvs = tvs `extendDVarSetList` pred_tvs
       | otherwise                           = tvs
       where
         pred_tvs = tyCoVarsOfTypeList pred

929 930
{- Note [Quantification and partial signatures]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
931 932 933 934
When choosing type variables to quantify, the basic plan is to
quantify over all type variables that are
 * free in the tau_tvs, and
 * not forced to be monomorphic (mono_tvs),
935
   for example by being free in the environment.
936

937 938
However, in the case of a partial type signature, be doing inference
*in the presence of a type signature*. For example:
939 940
   f :: _ -> a
   f x = ...
941 942
or
   g :: (Eq _a) => _b -> _b
943
In both cases we use plan InferGen, and hence call simplifyInfer.
944
But those 'a' variables are skolems, and we should be sure to quantify
945 946 947 948 949 950 951 952 953 954 955
over them, for two reasons

* In the case of a type error
     f :: _ -> Maybe a
     f x = True && x
  The inferred type of 'f' is f :: Bool -> Bool, but there's a
  left-over error of form (HoleCan (Maybe a ~ Bool)).  The error-reporting
  machine expects to find a binding site for the skolem 'a', so we
  add it to the ic_skols of the residual implication.

  Note that we /only/ do this to the residual implication. We don't
956
  complicate the quantified type variables of 'f' for downstream code;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
  it's just a device to make the error message generator know what to
  report.

* Consider the partial type signature
     f :: (Eq _) => Int -> Int
     f x = x
  In normal cases that makes sense; e.g.
     g :: Eq _a => _a -> _a
     g x = x
  where the signature makes the type less general than it could
  be. But for 'f' we must therefore quantify over the user-annotated
  constraints, to get
     f :: forall a. Eq a => Int -> Int
  (thereby correctly triggering an ambiguity error later).  If we don't
  we'll end up with a strange open type
     f :: Eq alpha => Int -> Int
  which isn't ambiguous but is still very wrong.  That's why include
  psig_theta in the variables to quantify over, passed to
  decideQuantification.
976

977 978 979 980 981 982 983 984
Note [Quantifying over equality constraints]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Should we quantify over an equality constraint (s ~ t)?  In general, we don't.
Doing so may simply postpone a type error from the function definition site to
its call site.  (At worst, imagine (Int ~ Bool)).

However, consider this
         forall a. (F [a] ~ Int) => blah
985
Should we quantify over the (F [a] ~ Int)?  Perhaps yes, because at the call
986 987 988 989
site we will know 'a', and perhaps we have instance  F [Bool] = Int.
So we *do* quantify over a type-family equality where the arguments mention