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

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

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

16
#include "HsVersions.h"
17

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

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

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
simplifyTop :: WantedConstraints -> TcM (Bag EvBind)
-- Simplify top-level constraints
64 65 66
-- 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
67
simplifyTop wanteds
68
  = do { traceTc "simplifyTop {" $ text "wanted = " <+> ppr wanteds
69
       ; ((final_wc, unsafe_ol), binds1) <- runTcS $ simpl_top wanteds
70 71 72
       ; traceTc "End simplifyTop }" empty

       ; traceTc "reportUnsolved {" empty
73
       ; binds2 <- reportUnsolved final_wc
74
       ; traceTc "reportUnsolved }" empty
75

76 77 78 79
       ; 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
80
           -- messages.
81
           ; errs_var  <- getErrsVar
82 83
           ; saved_msg <- TcM.readTcRef errs_var
           ; TcM.writeTcRef errs_var emptyMessages
84 85 86 87 88

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

89 90
           ; whyUnsafe <- fst <$> TcM.readTcRef errs_var
           ; TcM.writeTcRef errs_var saved_msg
91 92 93 94
           ; recordUnsafeInfer whyUnsafe
           }
       ; traceTc "reportUnsolved (unsafe overlapping) }" empty

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
       ; return (evBindMapBinds binds1 `unionBags` binds2) }

-- | Type-check a thing that emits only equality constraints, then
-- solve those constraints. Emits errors -- but does not fail --
-- if there is trouble.
solveEqualities :: TcM a -> TcM a
solveEqualities thing_inside
  = do { (result, wanted) <- captureConstraints thing_inside
       ; traceTc "solveEqualities {" $ text "wanted = " <+> ppr wanted
       ; (final_wc, _) <- runTcSEqualities $ simpl_top wanted
       ; traceTc "End solveEqualities }" empty

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

112 113 114 115 116 117
type SafeOverlapFailures = Cts
-- ^ See Note [Safe Haskell Overlapping Instances Implementation]

type FinalConstraints = (WantedConstraints, SafeOverlapFailures)

simpl_top :: WantedConstraints -> TcS FinalConstraints
118
    -- See Note [Top-level Defaulting Plan]
119
simpl_top wanteds
120
  = do { wc_first_go <- nestTcS (solveWantedsAndDrop wanteds)
121
                            -- This is where the main work happens
122 123 124
       ; wc_final <- try_tyvar_defaulting wc_first_go
       ; unsafe_ol <- getSafeOverlapFailures
       ; return (wc_final, unsafe_ol) }
125
  where
126 127
    try_tyvar_defaulting :: WantedConstraints -> TcS WantedConstraints
    try_tyvar_defaulting wc
128
      | isEmptyWC wc
129 130
      = return wc
      | otherwise
131
      = do { free_tvs <- TcS.zonkTyCoVarsAndFV (tyCoVarsOfWC wc)
132
           ; let meta_tvs = varSetElems (filterVarSet isMetaTyVar free_tvs)
133
                   -- zonkTyCoVarsAndFV: the wc_first_go is not yet zonked
134
                   -- filter isMetaTyVar: we might have runtime-skolems in GHCi,
135 136
                   -- and we definitely don't want to try to assign to those!

137 138 139
           ; defaulted <- mapM defaultTyVarTcS meta_tvs   -- Has unification side effects
           ; if or defaulted
             then do { wc_residual <- nestTcS (solveWanteds wc)
140
                            -- See Note [Must simplify after defaulting]
141 142
                     ; try_class_defaulting wc_residual }
             else try_class_defaulting wc }     -- No defaulting took place
143

144 145
    try_class_defaulting :: WantedConstraints -> TcS WantedConstraints
    try_class_defaulting wc
Austin Seipp's avatar
Austin Seipp committed
146
      | isEmptyWC wc
147 148
      = return wc
      | otherwise  -- See Note [When to do type-class defaulting]
149
      = do { something_happened <- applyDefaultingRules wc
150
                                   -- See Note [Top-level Defaulting Plan]
151
           ; if something_happened
152
             then do { wc_residual <- nestTcS (solveWantedsAndDrop wc)
153
                     ; try_class_defaulting wc_residual }
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                  -- See Note [Overview of implicit CallStacks]
             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]
defaultCallStacks wanteds
  = do simples <- handle_simples (wc_simple wanteds)
       implics <- mapBagM handle_implic (wc_impl wanteds)
       return (wanteds { wc_simple = simples, wc_impl = implics })

  where

  handle_simples simples
    = catBagMaybes <$> mapBagM defaultCallStack simples

  handle_implic implic = do
    wanteds <- defaultCallStacks (ic_wanted implic)
    return (implic { ic_wanted = wanteds })

  defaultCallStack ct@(CDictCan { cc_ev = ev_w })
    | Just _ <- isCallStackCt ct
    = do { solveCallStack ev_w EvCsEmpty
         ; return Nothing }

  defaultCallStack ct
    = return (Just ct)

189

190 191 192 193 194 195 196 197
-- | Type-check a thing, returning the result and any EvBinds produced
-- during solving. Emits errors -- but does not fail -- if there is trouble.
solveTopConstraints :: TcM a -> TcM (a, Bag EvBind)
solveTopConstraints thing_inside
  = do { (result, wanted) <- captureConstraints thing_inside
       ; ev_binds <- simplifyTop wanted
       ; return (result, ev_binds) }

Austin Seipp's avatar
Austin Seipp committed
198
{-
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
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
214
   run = fromInteger 0
215 216 217
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
218 219
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
220 221 222 223 224 225
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.

226 227 228 229 230 231 232 233 234
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.

235 236
Note [Top-level Defaulting Plan]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237 238
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
239
       simple constraints, maybe deep inside the context of implications.
240
       This used to be the case in GHC 7.4.1.
241
   (ii) Do it in a tight loop at simplifyTop, once all other constraints have
242 243
        finished. This is the current story.

244
Option (i) had many disadvantages:
245 246
   a) Firstly, it was deep inside the actual solver.
   b) Secondly, it was dependent on the context (Infer a type signature,
247 248
      or Check a type signature, or Interactive) since we did not want
      to always start defaulting when inferring (though there is an exception to
249
      this, see Note [Default while Inferring]).
250 251 252 253 254 255 256 257 258 259 260
   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
261
go with option (ii), implemented at SimplifyTop. Namely:
262 263
     - First, have a go at solving the residual constraint of the whole
       program
264 265
     - Try to approximate it with a simple constraint
     - Figure out derived defaulting equations for that simple constraint
266 267 268 269
     - 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:
270
     - At the top-level, once you had a go at solving the constraint, do
Gabor Greif's avatar
Gabor Greif committed
271
       figure out /all/ the touchable unification variables of the wanted constraints.
272 273 274
     - Apply defaulting to their kinds

More details in Note [DefaultTyVar].
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

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]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

318
How is this implemented? It's complicated! So we'll step through it all:
319 320 321 322 323 324 325

 1) `InstEnv.lookupInstEnv` -- Performs instance resolution, so this is where
 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.

326 327 328 329
 2) `TcInteract.matchClassInst` -- This module drives the instance resolution
 / 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.
330 331 332 333 334

 3) `TcInteract.doTopReactDict` -- Takes a dictionary / class constraint and
 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
335
 an `-XSafe` or `-XTrustworthy` module, we follow this approach as we know
336 337 338
 compilation should fail. These are handled as normal constraint resolution
 failures from here-on (see step 6).

339
 Otherwise, we may be inferring safety (or using `-Wunsafe`), and
340 341 342 343 344 345 346 347 348
 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.simpl_top` -- Top-level function for driving the simplifier for
 constraint resolution. Once finished, we call `getSafeOverlapFailures` to
 retrieve the list of overlapping instances that were successfully resolved,
 but unsafe. Remember, this is only applicable for generating warnings
349
 (`-Wunsafe`) or inferring a module unsafe. `-XSafe` and `-XTrustworthy`
350 351 352 353 354 355 356 357 358
 cause compilation failure by not resolving the unsafe constraint at all.
 `simpl_top` returns a list of unresolved constraints (all types), and resolved
 (but unsafe) resolved dictionary constraints.

 5) `TcSimplify.simplifyTop` -- Is the caller of `simpl_top`. For unresolved
 constraints, it calls `TcErrors.reportUnsolved`, while for unsafe overlapping
 instance constraints, it calls `TcErrors.warnAllUnsolved`. Both functions
 convert constraints into a warning message for the user.

359
 6) `TcErrors.*Unsolved` -- Generates error messages for constraints by
360
 actually calling `InstEnv.lookupInstEnv` again! Yes, confusing, but all we
361 362 363 364
 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.
365 366 367 368 369 370 371 372 373 374 375 376

 7) `TcSimplify.simplifyTop` -- 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.

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

 9) `HscMain.tcRnModule'` -- Reads `tcg_safeInfer` after type-checking, calling
 `HscMain.markUnsafeInfer` (passing the reason along) when safe-inferrence
 failed.
Austin Seipp's avatar
Austin Seipp committed
377
-}
378

379
------------------
380 381 382
simplifyAmbiguityCheck :: Type -> WantedConstraints -> TcM ()
simplifyAmbiguityCheck ty wanteds
  = do { traceTc "simplifyAmbiguityCheck {" (text "type = " <+> ppr ty $$ text "wanted = " <+> ppr wanteds)
383
       ; ((final_wc, _), _) <- runTcS $ simpl_top wanteds
384 385 386 387 388
       ; traceTc "End simplifyAmbiguityCheck }" empty

       -- Normally report all errors; but with -XAllowAmbiguousTypes
       -- report only insoluble ones, since they represent genuinely
       -- inaccessible code
389
       ; allow_ambiguous <- xoptM LangExt.AllowAmbiguousTypes
390
       ; traceTc "reportUnsolved(ambig) {" empty
391
       ; tc_lvl <- TcM.getTcLevel
392
       ; unless (allow_ambiguous && not (insolubleWC tc_lvl final_wc))
393
                (discardResult (reportUnsolved final_wc))
394 395 396 397
       ; traceTc "reportUnsolved(ambig) }" empty

       ; return () }

398 399
------------------
simplifyInteractive :: WantedConstraints -> TcM (Bag EvBind)
400
simplifyInteractive wanteds
401
  = traceTc "simplifyInteractive" empty >>
402
    simplifyTop wanteds
403 404

------------------
405
simplifyDefault :: ThetaType    -- Wanted; has no type variables in it
406
                -> TcM ()       -- Succeeds if the constraint is soluble
407
simplifyDefault theta
408
  = do { traceTc "simplifyInteractive" empty
409
       ; wanted <- newWanteds DefaultOrigin theta
410
       ; unsolved <- simplifyWantedsTcM wanted
411 412 413

       ; traceTc "reportUnsolved {" empty
       -- See Note [Deferring coercion errors to runtime]
414
       ; reportAllUnsolved unsolved
415 416
       ; traceTc "reportUnsolved }" empty

417
       ; return () }
418

419 420 421
------------------
tcCheckSatisfiability :: Bag EvVar -> TcM Bool
-- Return True if satisfiable, False if definitely contradictory
422
tcCheckSatisfiability given_ids
423
  = do { lcl_env <- TcM.getLclEnv
424 425
       ; let given_loc = mkGivenLoc topTcLevel UnkSkol lcl_env
       ; (res, _ev_binds) <- runTcS $
426 427
             do { traceTcS "checkSatisfiability {" (ppr given_ids)
                ; given_cts <- mkGivensWithSuperClasses given_loc (bagToList given_ids)
428
                     -- See Note [Superclases and satisfiability]
429 430 431 432 433
                ; insols <- solveSimpleGivens given_cts
                ; insols <- try_harder insols
                ; traceTcS "checkSatisfiability }" (ppr insols)
                ; return (isEmptyBag insols) }
       ; return res }
434
 where
435 436 437
    try_harder :: Cts -> TcS Cts
    -- Maybe we have to search up the superclass chain to find
    -- an unsatisfiable constraint.  Example: pmcheck/T3927b.
438
    -- At the moment we try just once
439 440 441 442 443 444
    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
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
           ; solveSimpleGivens new_given }

{- Note [Superclases and satisfiability]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
the recurisve case, we would go on forever in the common case where
the constraints /are/ satisfiable (Trac #10592 comment:12!).

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

467

468
***********************************************************************************
469
*                                                                                 *
470 471 472
*                            Inference
*                                                                                 *
***********************************************************************************
473

474 475 476 477 478 479 480 481
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
482
        pushLevelAndCaptureConstraints (tcMonoBinds...)
483 484 485 486
   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,
487
   here called rhs_tclvl.
488 489 490 491

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

Austin Seipp's avatar
Austin Seipp committed
493
-}
494

495
simplifyInfer :: TcLevel               -- Used when generating the constraints
496
              -> Bool                  -- Apply monomorphism restriction
497
              -> [TcIdSigInfo]         -- Any signatures (possibly partial)
498 499
              -> [(Name, TcTauType)]   -- Variables to be generalised,
                                       -- and their tau-types
500
              -> WantedConstraints
501
              -> TcM ([TcTyVar],    -- Quantify over these type variables
502
                      [EvVar],      -- ... and these constraints (fully zonked)
503
                      TcEvBinds)    -- ... binding these evidence variables
504
simplifyInfer rhs_tclvl apply_mr sigs name_taus wanteds
505
  | isEmptyWC wanteds
506 507 508
  = do { gbl_tvs <- tcGetGlobalTyCoVars
       ; qtkvs <- quantify_tvs sigs gbl_tvs $
                  splitDepVarsOfTypes (map snd name_taus)
509
       ; traceTc "simplifyInfer: empty WC" (ppr name_taus $$ ppr qtkvs)
510
       ; return (qtkvs, [], emptyTcEvBinds) }
511

512
  | otherwise
513
  = do { traceTc "simplifyInfer {"  $ vcat
514 515
             [ ptext (sLit "sigs =") <+> ppr sigs
             , ptext (sLit "binds =") <+> ppr name_taus
516
             , ptext (sLit "rhs_tclvl =") <+> ppr rhs_tclvl
517
             , ptext (sLit "apply_mr =") <+> ppr apply_mr
518
             , ptext (sLit "(unzonked) wanted =") <+> ppr wanteds
519 520
             ]

521 522 523 524 525 526 527
       -- 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.
528

529
       ; ev_binds_var <- TcM.newTcEvBinds
530
       ; wanted_transformed_incl_derivs <- setTcLevel rhs_tclvl $
531
           do { sig_derived <- concatMapM mkSigDerivedWanteds sigs
532 533
                  -- the False says we don't really need to solve all Deriveds
              ; runTcSWithEvBinds False (Just ev_binds_var) $
534
                solveWanteds (wanteds `addSimples` listToBag sig_derived) }
535
       ; wanted_transformed_incl_derivs <- TcM.zonkWC wanted_transformed_incl_derivs
536

537 538 539 540
       -- Find quant_pred_candidates, the predicates that
       -- we'll consider quantifying over
       -- NB: We do not do any defaulting when inferring a type, this can lead
       -- to less polymorphic types, see Note [Default while Inferring]
541

542
       ; tc_lcl_env <- TcM.getLclEnv
543
       ; let wanted_transformed = dropDerivedWC wanted_transformed_incl_derivs
544
       ; quant_pred_candidates   -- Fully zonked
545
           <- if insolubleWC rhs_tclvl wanted_transformed_incl_derivs
546
              then return []   -- See Note [Quantification with errors]
547
                               -- NB: must include derived errors in this test,
548 549 550
                               --     hence "incl_derivs"

              else do { let quant_cand = approximateWC wanted_transformed
551 552 553
                            meta_tvs   = filter isMetaTyVar (varSetElems (tyCoVarsOfCts quant_cand))

                      ; gbl_tvs <- tcGetGlobalTyCoVars
554 555
                            -- Miminise quant_cand.  We are not interested in any evidence
                            -- produced, because we are going to simplify wanted_transformed
556
                            -- again later. All we want here are the predicates over which to
557
                            -- quantify.
558 559 560 561
                            --
                            -- If any meta-tyvar unifications take place (unlikely), we'll
                            -- pick that up later.

562 563 564 565 566 567 568
                      -- 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

569
                      ; WC { wc_simple = simples }
570 571 572 573 574 575 576
                           <- setTcLevel rhs_tclvl $
                              runTcSDeriveds       $
                              solveSimpleWanteds $ mapBag toDerivedCt quant_cand
                                -- NB: we don't want evidence, so used
                                -- Derived constraints

                      ; simples <- TcM.zonkSimples simples
577

578
                      ; return [ ctEvPred ev | ct <- bagToList simples
579
                                             , let ev = ctEvidence ct ] }
580

581
       -- NB: quant_pred_candidates is already fully zonked
582

583
       -- Decide what type variables and constraints to quantify
584
       ; zonked_taus <- mapM (TcM.zonkTcType . snd) name_taus
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
       ; let zonked_tau_tkvs = splitDepVarsOfTypes zonked_taus
       ; (qtvs, bound_theta)
           <- decideQuantification apply_mr sigs name_taus
                                   quant_pred_candidates zonked_tau_tkvs

         -- 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]
         --            consraints = alpha ~ [beta]
         -- 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
       ; outer_tclvl    <- TcM.getTcLevel
       ; zonked_tau_tvs <- fold <$>
                           traverse TcM.zonkTyCoVarsAndFV zonked_tau_tkvs
              -- decideQuantification turned some meta tyvars into
              -- quantified skolems, so we have to zonk again

       ; let phi_tvs     = tyCoVarsOfTypes bound_theta
                           `unionVarSet` zonked_tau_tvs

             promote_tvs = closeOverKinds phi_tvs `delVarSetList` qtvs
       ; MASSERT2( closeOverKinds promote_tvs `subVarSet` promote_tvs
                 , ppr phi_tvs $$
                   ppr (closeOverKinds phi_tvs) $$
                   ppr promote_tvs $$
                   ppr (closeOverKinds promote_tvs) )
           -- we really don't want a type to be promoted when its kind isn't!

           -- promoteTyVar ignores coercion variables
       ; mapM_ (promoteTyVar outer_tclvl) (varSetElems promote_tvs)

           -- Emit an implication constraint for the
           -- remaining constraints from the RHS
       ; bound_theta_vars <- mapM TcM.newEvVar bound_theta
       ; let skol_info   = InferSkol [ (name, mkSigmaTy [] bound_theta ty)
                                     | (name, ty) <- name_taus ]
627
                        -- Don't add the quantified variables here, because
628 629
                        -- they are also bound in ic_skols and we want them
                        -- to be tidied uniformly
630

631
             implic = Implic { ic_tclvl    = rhs_tclvl
632
                             , ic_skols    = qtvs
633
                             , ic_no_eqs   = False
634
                             , ic_given    = bound_theta_vars
635
                             , ic_wanted   = wanted_transformed
636
                             , ic_status   = IC_Unsolved
637
                             , ic_binds    = Just ev_binds_var
638
                             , ic_info     = skol_info
639
                             , ic_env      = tc_lcl_env }
640
       ; emitImplication implic
641

642
         -- All done!
643
       ; traceTc "} simplifyInfer/produced residual implication for quantification" $
644
         vcat [ ptext (sLit "quant_pred_candidates =") <+> ppr quant_pred_candidates
645
              , ptext (sLit "zonked_taus") <+> ppr zonked_taus
646
              , ptext (sLit "zonked_tau_tvs=") <+> ppr zonked_tau_tvs
647
              , ptext (sLit "promote_tvs=") <+> ppr promote_tvs
648
              , ptext (sLit "bound_theta =") <+> ppr bound_theta
649 650
              , ptext (sLit "qtvs =") <+> ppr qtvs
              , ptext (sLit "implic =") <+> ppr implic ]
651

652
       ; return ( qtvs, bound_theta_vars, TcEvBinds ev_binds_var ) }
653

654 655 656 657 658
mkSigDerivedWanteds :: TcIdSigInfo -> TcM [Ct]
-- See Note [Add deriveds for signature contexts]
mkSigDerivedWanteds (TISI { sig_bndr = PartialSig { sig_name = name }
                          , sig_theta = theta, sig_tau = tau })
 = do { let skol_info = InferSkol [(name, mkSigmaTy [] theta tau)]
659
      ; loc <- getCtLocM (GivenOrigin skol_info) (Just TypeLevel)
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
      ; return [ mkNonCanonical (CtDerived { ctev_pred = pred
                                           , ctev_loc = loc })
               | pred <- theta ] }
mkSigDerivedWanteds _ = return []

{- Note [Add deriveds for signature contexts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider this (Trac #11016):
  f2 :: (?x :: Int) => _
  f2 = ?x
We'll use plan InferGen because there are holes in the type.  But 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 wont unify alpha:=Int.

Solution: in simplifyInfer, just before simplifying the constraints
gathered from the RHS, add Derived constraints for the context of any
type signatures.  This is rare; if there is a type signature we'll usually
be doing CheckGen.  But it happens for signatures with holes.

Austin Seipp's avatar
Austin Seipp committed
680 681
************************************************************************
*                                                                      *
682
                Quantification
Austin Seipp's avatar
Austin Seipp committed
683 684
*                                                                      *
************************************************************************
685 686 687 688 689 690 691

Note [Deciding quantification]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the monomorphism restriction does not apply, then we quantify as follows:
  * 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
692 693
          beta, because alpha fixes beta, and beta is effectively free in
          the environment too
694 695 696
    These are the mono_tvs

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

699 700 701 702
  * 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
703

704
  * Filter the constraints using pickQuantifiablePreds and the qtvs.
705 706
    We have to zonk the constraints first, so they "see" the freshly
    created skolems.
707

708 709 710
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
711
-}
712

713
decideQuantification
714 715 716 717 718 719 720
  :: Bool                  -- try the MR restriction?
  -> [TcIdSigInfo]
  -> [(Name, TcTauType)]   -- variables to be generalised (for errors only)
  -> [PredType]            -- candidate theta
  -> Pair TcTyCoVarSet     -- dependent (kind) variables & type variables
  -> TcM ( [TcTyVar]       -- Quantify over these (skolems)
         , [PredType] )    -- and this context (fully zonked)
721
-- See Note [Deciding quantification]
722 723
decideQuantification apply_mr sigs name_taus constraints
                     zonked_pair@(Pair zonked_tau_kvs zonked_tau_tvs)
724
  | apply_mr     -- Apply the Monomorphism restriction
725 726 727
  = do { gbl_tvs <- tcGetGlobalTyCoVars
       ; let constrained_tvs = tyCoVarsOfTypes constraints `unionVarSet`
                               filterVarSet isCoVar zonked_tkvs
728
             mono_tvs = gbl_tvs `unionVarSet` constrained_tvs
729
       ; qtvs <- quantify_tvs sigs mono_tvs zonked_pair
730

731
           -- Warn about the monomorphism restriction
732
       ; warn_mono <- woptM Opt_WarnMonomorphism
733
       ; let mr_bites = constrained_tvs `intersectsVarSet` zonked_tkvs
734
       ; warnTc (warn_mono && mr_bites) $
735
         hang (text "The Monomorphism Restriction applies to the binding"
736
               <> plural bndrs <+> ptext (sLit "for") <+> pp_bndrs)
737 738 739
             2 (text "Consider giving a type signature for"
                <+> if isSingleton bndrs then pp_bndrs
                                         else ptext (sLit "these binders"))
740

741 742 743
       -- All done
       ; traceTc "decideQuantification 1" (vcat [ppr constraints, ppr gbl_tvs, ppr mono_tvs
                                                , ppr qtvs, ppr mr_bites])
744
       ; return (qtvs, []) }
745 746

  | otherwise
747
  = do { gbl_tvs <- tcGetGlobalTyCoVars
748
       ; let mono_tvs     = growThetaTyVars equality_constraints gbl_tvs
749
             tau_tvs_plus = growThetaTyVars constraints zonked_tau_tvs
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
       ; qtvs <- quantify_tvs sigs mono_tvs (Pair zonked_tau_kvs tau_tvs_plus)
          -- 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.

       ; constraints <- TcM.zonkTcTypes constraints
                 -- quantiyTyVars turned some meta tyvars into
                 -- quantified skolems, so we have to zonk again

       ; let theta     = pickQuantifiablePreds (mkVarSet qtvs) constraints
             min_theta = mkMinimalBySCs theta
               -- See Note [Minimize by Superclasses]

       ; traceTc "decideQuantification 2"
           (vcat [ text "constraints:"  <+> ppr constraints
                 , text "gbl_tvs:"      <+> ppr gbl_tvs
                 , text "mono_tvs:"     <+> ppr mono_tvs
                 , text "zonked_kvs:"   <+> ppr zonked_tau_kvs
                 , text "tau_tvs_plus:" <+> ppr tau_tvs_plus
                 , text "qtvs:"         <+> ppr qtvs
                 , text "min_theta:"    <+> ppr min_theta ])
772
       ; return (qtvs, min_theta) }
773
  where
774
    zonked_tkvs = zonked_tau_kvs `unionVarSet` zonked_tau_tvs
775 776
    bndrs    = map fst name_taus
    pp_bndrs = pprWithCommas (quotes . ppr) bndrs
777 778
    equality_constraints = filter isEqPred constraints

779 780 781 782 783 784 785 786 787 788
quantify_tvs :: [TcIdSigInfo]
             -> TcTyVarSet   -- the monomorphic tvs
             -> Pair TcTyVarSet   -- kvs, tvs to quantify
             -> TcM [TcTyVar]
-- See Note [Which type variables to quantify]
quantify_tvs sigs mono_tvs (Pair tau_kvs tau_tvs)
  = quantifyTyVars (mono_tvs `delVarSetList` sig_qtvs)
                   (Pair tau_kvs
                         (tau_tvs `extendVarSetList` sig_qtvs
                                  `extendVarSetList` sig_wcs))
789 790 791 792 793
                   -- NB: quantifyTyVars zonks its arguments
  where
    sig_qtvs = [ skol | sig <- sigs, (_, skol) <- sig_skols sig ]
    sig_wcs  = [ wc   | TISI { sig_bndr = PartialSig { sig_wcs = wcs } } <- sigs
                      , (_, wc) <- wcs ]
794

795 796

------------------
797
growThetaTyVars :: ThetaType -> TyCoVarSet -> TyVarSet
798
-- See Note [Growing the tau-tvs using constraints]
799
-- NB: only returns tyvars, never covars
800
growThetaTyVars theta tvs
801 802 803
  | null theta = tvs_only
  | otherwise  = filterVarSet isTyVar $
                 transCloVarSet mk_next seed_tvs
804
  where
805 806
    tvs_only = filterVarSet isTyVar tvs
    seed_tvs = tvs `unionVarSet` tyCoVarsOfTypes ips
807
    (ips, non_ips) = partition isIPPred theta
808
                         -- See Note [Inheriting implicit parameters] in TcType
809 810 811 812 813 814

    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
815
       where
816
         pred_tvs = tyCoVarsOfType pred
817

818 819 820 821 822 823
{- Note [Which type variables to quantify]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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),
824
   for example by being free in the environment.
825

826
However, for a pattern binding, or with wildcards, we might
827
be doing inference *in the presence of a type signature*.
828 829
Mostly, if there is a signature we use CheckGen, not InferGen,
but with pattern bindings or wildcards we might do InferGen
830 831 832
and still have a type signature.  For example:
   f :: _ -> a
   f x = ...
833 834
or
   g :: (Eq _a) => _b -> _b
835 836 837
or
   p :: a -> a
   (p,q) = e
838
In all these cases we use plan InferGen, and hence call simplifyInfer.
839 840 841 842 843
But those 'a' variables are skolems, and we should be sure to quantify
over them, regardless of the monomorphism restriction etc.  If we
don't, when reporting a type error we panic when we find that a
skolem isn't bound by any enclosing implication.

844 845 846 847 848 849 850 851 852 853
Moreover we must quantify over all wildcards that are not free in
the environment.  In the case of 'g' for example, silly though it is,
we want to get the inferred type
   g :: forall t. Eq t => Int -> Int
and then report ambiguity, rather than *not* quantifying over 't'
and getting some much more mysterious error later.  A similar case
is
  h :: F _a -> Int

That's why we pass sigs to simplifyInfer, and make sure (in
854 855 856
quantify_tvs) that we do quantify over them.  Trac #10615 is
a case in point.

857 858 859 860 861 862 863 864
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
865
Should we quantify over the (F [a] ~ Int)?  Perhaps yes, because at the call
866 867 868 869
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
the quantified variables.

870 871 872
Note [Growing the tau-tvs using constraints]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(growThetaTyVars insts tvs) is the result of extending the set
873
    of tyvars, tvs, using all conceivable links from pred
874 875 876 877 878 879 880 881

E.g. tvs = {a}, preds = {H [a] b, K (b,Int) c, Eq e}
Then growThetaTyVars preds tvs = {a,b,c}

Notice that
   growThetaTyVars is conservative       if v might be fixed by vs
                                         => v `elem` grow(vs,C)

882 883 884 885 886 887 888 889
Note [Quantification with errors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If we find that the RHS of the definition has some absolutely-insoluble
constraints, we abandon all attempts to find a context to quantify
over, and instead make the function fully-polymorphic in whatever
type we have found.  For two reasons
  a) Minimise downstream errors
  b) Avoid spurious errors from this function
890

891
But NB that we must include *derived* errors in the check. Example:
892 893 894
    (a::*) ~ Int#
We get an insoluble derived error *~#, and we don't want to discard
it before doing the isInsolubleWC test!  (Trac #8262)
895

896 897
Note [Default while Inferring]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
898
Our current plan is that defaulting only happens at simplifyTop and
899
not simplifyInfer.  This may lead to some insoluble deferred constraints.
900 901
Example:

902
instance D g => C g Int b
903 904

constraint inferred = (forall b. 0 => C gamma alpha b) /\ Num alpha
905
type inferred       = gamma -> gamma
906

907 908 909
Now, if we try to default (alpha := Int) we will be able to refine the implication to
  (forall b. 0 => C gamma Int b)
which can then be simplified further to
910
  (forall b. 0 => D gamma)
911
Finally, we /can/ approximate this implication with (D gamma) and infer the quantified
912 913
type:  forall g. D g => g -> g

914
Instead what will currently happen is that we will get a quantified type
915 916 917
(forall g. g -> g) and an implication:
       forall g. 0 => (forall b. 0 => C g alpha b) /\ Num alpha

918
Which, even if the simplifyTop defaults (alpha := Int) we will still be left with an
919 920 921
unsolvable implication:
       forall g. 0 => (forall b. 0 => D g)

922
The concrete example would be:
923 924 925
       h :: C g a s => g -> a -> ST s a
       f (x::gamma) = (\_ -> x) (runST (h x (undefined::alpha)) + 1)

926 927
But it is quite tedious to do defaulting and resolve the implication constraints, and
we have not observed code breaking because of the lack of defaulting in inference, so
928 929 930 931
we don't do it for now.



932
Note [Minimize by Superclasses]
Simon Peyton Jones's avatar