TcInteract.lhs 79.8 KB
Newer Older
1
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
2 3 4 5 6 7 8
{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details

9
module TcInteract ( 
10 11
     solveInteractGiven,  -- Solves [EvVar],GivenLoc
     solveInteractCts,    -- Solves [Cts]
12 13 14 15
  ) where  

#include "HsVersions.h"

16

17
import BasicTypes ()
18 19 20
import TcCanonical
import VarSet
import Type
dimitris's avatar
dimitris committed
21
import Unify
22 23
import FamInstEnv
import Coercion( mkAxInstRHS )
24 25 26

import Var
import TcType
27
import PrelNames (singIClassName)
28

29 30
import Class
import TyCon
31
import Name
32
import IParam
33 34 35

import FunDeps

36
import TcEvidence
37 38
import Outputable

39 40
import TcMType ( zonkTcPredType )

41
import TcRnTypes
42
import TcErrors
43
import TcSMonad
44
import Maybes( orElse )
45
import Bag
46

47 48
import Control.Monad ( foldM )

dimitris's avatar
dimitris committed
49 50
import VarEnv
import qualified Data.Traversable as Traversable
51
import Data.Maybe ( isJust )
dimitris's avatar
dimitris committed
52

53
import Control.Monad( when, unless )
54
import Pair ()
55
import UniqFM
56 57 58
import FastString ( sLit ) 
import DynFlags
\end{code}
59 60
**********************************************************************
*                                                                    * 
61 62 63 64
*                      Main Interaction Solver                       *
*                                                                    *
**********************************************************************

65 66
Note [Basic Simplifier Plan] 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67

68 69
1. Pick an element from the WorkList if there exists one with depth 
   less thanour context-stack depth. 
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
2. Run it down the 'stage' pipeline. Stages are: 
      - canonicalization
      - inert reactions
      - spontaneous reactions
      - top-level intreactions
   Each stage returns a StopOrContinue and may have sideffected 
   the inerts or worklist.
  
   The threading of the stages is as follows: 
      - If (Stop) is returned by a stage then we start again from Step 1. 
      - If (ContinueWith ct) is returned by a stage, we feed 'ct' on to 
        the next stage in the pipeline. 
4. If the element has survived (i.e. ContinueWith x) the last stage 
   then we add him in the inerts and jump back to Step 1.

If in Step 1 no such element exists, we have exceeded our context-stack 
depth and will simply fail.
88 89
\begin{code}

90 91 92
solveInteractCts :: [Ct] -> TcS (Bag Implication)
-- Returns a bag of residual implications that have arisen while solving
-- this particular worklist.
93
solveInteractCts cts 
dimitris's avatar
dimitris committed
94
  = do { traceTcS "solveInteractCtS" (vcat [ text "cts =" <+> ppr cts ]) 
95 96 97 98 99 100 101 102 103 104
       ; updWorkListTcS (appendWorkListCt cts) >> solveInteract 
       ; impls <- getTcSImplics
       ; updTcSImplics (const emptyBag) -- Nullify residual implications
       ; return impls }

solveInteractGiven :: GivenLoc -> [EvVar] -> TcS (Bag Implication)
-- In principle the givens can kick out some wanteds from the inert
-- resulting in solving some more wanted goals here which could emit
-- implications. That's why I return a bag of implications. Not sure
-- if this can happen in practice though.
105 106
solveInteractGiven gloc evs
  = solveInteractCts (map mk_noncan evs)
107 108 109 110 111
  where 
    mk_noncan ev = CNonCanonical { cc_ev = Given { ctev_gloc = gloc 
                                                 , ctev_evtm = EvId ev
                                                 , ctev_pred = evVarPred ev }
                                 , cc_depth = 0 }
112 113 114 115 116 117

-- The main solver loop implements Note [Basic Simplifier Plan]
---------------------------------------------------------------
solveInteract :: TcS ()
-- Returns the final InertSet in TcS, WorkList will be eventually empty.
solveInteract
118 119
  = {-# SCC "solveInteract" #-}
    do { dyn_flags <- getDynFlags
120 121
       ; let max_depth = ctxtStkDepth dyn_flags
             solve_loop
122 123
              = {-# SCC "solve_loop" #-}
                do { sel <- selectNextWorkItem max_depth
124 125 126 127
                   ; case sel of 
                      NoWorkRemaining     -- Done, successfuly (modulo frozen)
                        -> return ()
                      MaxDepthExceeded ct -- Failure, depth exceeded
128
                        -> wrapErrTcS $ solverDepthErrorTcS (cc_depth ct) [ct]
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                      NextWorkItem ct     -- More work, loop around!
                        -> runSolverPipeline thePipeline ct >> solve_loop }
       ; solve_loop }

type WorkItem = Ct
type SimplifierStage = WorkItem -> TcS StopOrContinue

continueWith :: WorkItem -> TcS StopOrContinue
continueWith work_item = return (ContinueWith work_item) 

data SelectWorkItem 
       = NoWorkRemaining      -- No more work left (effectively we're done!)
       | MaxDepthExceeded Ct  -- More work left to do but this constraint has exceeded
                              -- the max subgoal depth and we must stop 
       | NextWorkItem Ct      -- More work left, here's the next item to look at 

selectNextWorkItem :: SubGoalDepth -- Max depth allowed
                   -> TcS SelectWorkItem
selectNextWorkItem max_depth
  = updWorkListTcS_return pick_next
149
  where 
150
    pick_next :: WorkList -> (SelectWorkItem, WorkList)
dimitris's avatar
dimitris committed
151 152 153 154 155 156 157 158
    pick_next wl = case selectWorkItem wl of
                     (Nothing,_) 
                         -> (NoWorkRemaining,wl)           -- No more work
                     (Just ct, new_wl) 
                         | cc_depth ct > max_depth         -- Depth exceeded
                         -> (MaxDepthExceeded ct,new_wl)
                     (Just ct, new_wl) 
                         -> (NextWorkItem ct, new_wl)      -- New workitem and worklist
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 189 190 191

runSolverPipeline :: [(String,SimplifierStage)] -- The pipeline 
                  -> WorkItem                   -- The work item 
                  -> TcS () 
-- Run this item down the pipeline, leaving behind new work and inerts
runSolverPipeline pipeline workItem 
  = do { initial_is <- getTcSInerts 
       ; traceTcS "Start solver pipeline {" $ 
                  vcat [ ptext (sLit "work item = ") <+> ppr workItem 
                       , ptext (sLit "inerts    = ") <+> ppr initial_is]

       ; final_res  <- run_pipeline pipeline (ContinueWith workItem)

       ; final_is <- getTcSInerts
       ; case final_res of 
           Stop            -> do { traceTcS "End solver pipeline (discharged) }" 
                                       (ptext (sLit "inerts    = ") <+> ppr final_is)
                                 ; return () }
           ContinueWith ct -> do { traceTcS "End solver pipeline (not discharged) }" $
                                       vcat [ ptext (sLit "final_item = ") <+> ppr ct
                                            , ptext (sLit "inerts     = ") <+> ppr final_is]
                                 ; updInertSetTcS ct }
       }
  where run_pipeline :: [(String,SimplifierStage)] -> StopOrContinue -> TcS StopOrContinue
        run_pipeline [] res = return res 
        run_pipeline _ Stop = return Stop 
        run_pipeline ((stg_name,stg):stgs) (ContinueWith ct)
          = do { traceTcS ("runStage " ++ stg_name ++ " {")
                          (text "workitem   = " <+> ppr ct) 
               ; res <- stg ct 
               ; traceTcS ("end stage " ++ stg_name ++ " }") empty
               ; run_pipeline stgs res 
               }
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
\end{code}

Example 1:
  Inert:   {c ~ d, F a ~ t, b ~ Int, a ~ ty} (all given)
  Reagent: a ~ [b] (given)

React with (c~d)     ==> IR (ContinueWith (a~[b]))  True    []
React with (F a ~ t) ==> IR (ContinueWith (a~[b]))  False   [F [b] ~ t]
React with (b ~ Int) ==> IR (ContinueWith (a~[Int]) True    []

Example 2:
  Inert:  {c ~w d, F a ~g t, b ~w Int, a ~w ty}
  Reagent: a ~w [b]

React with (c ~w d)   ==> IR (ContinueWith (a~[b]))  True    []
React with (F a ~g t) ==> IR (ContinueWith (a~[b]))  True    []    (can't rewrite given with wanted!)
etc.

Example 3:
  Inert:  {a ~ Int, F Int ~ b} (given)
  Reagent: F a ~ b (wanted)

React with (a ~ Int)   ==> IR (ContinueWith (F Int ~ b)) True []
React with (F Int ~ b) ==> IR Stop True []    -- after substituting we re-canonicalize and get nothing

\begin{code}
218
thePipeline :: [(String,SimplifierStage)]
219 220
thePipeline = [ ("lookup-in-inerts",        lookupInInertsStage)
              , ("canonicalization",        canonicalizationStage)
221 222 223
              , ("spontaneous solve",       spontaneousSolveStage)
              , ("interact with inerts",    interactWithInertsStage)
              , ("top-level reactions",     topReactionsStage) ]
224 225 226 227
\end{code}


\begin{code}
228

229 230 231 232
-- A quick lookup everywhere to see if we know about this constraint
--------------------------------------------------------------------
lookupInInertsStage :: SimplifierStage
lookupInInertsStage ct
233
  | Wanted { ctev_evar = ev_id, ctev_pred = pred } <- cc_ev ct
234
  = do { is <- getTcSInerts
235 236 237 238 239
       ; case lookupInInerts is pred of
           Just ctev
             |  not (isDerived ctev)
             -> do { setEvBind ev_id (ctEvTerm ctev)
                   ; return Stop }
240 241 242 243 244
           _ -> continueWith ct }
  | otherwise -- I could do something like that for givens 
              -- as well I suppose but it is not a big deal
  = continueWith ct

dimitris's avatar
dimitris committed
245

246 247 248 249
-- The canonicalization stage, see TcCanonical for details
----------------------------------------------------------
canonicalizationStage :: SimplifierStage
canonicalizationStage = TcCanonical.canonicalize 
250 251 252 253 254 255 256 257
\end{code}

*********************************************************************************
*                                                                               * 
                       The spontaneous-solve Stage
*                                                                               *
*********************************************************************************

258 259 260 261 262 263
Note [Efficient Orientation] 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are two cases where we have to be careful about 
orienting equalities to get better efficiency. 

264
Case 1: In Rewriting Equalities (function rewriteEqLHS) 
265

266 267 268 269 270 271 272 273 274
    When rewriting two equalities with the same LHS:
          (a)  (tv ~ xi1) 
          (b)  (tv ~ xi2) 
    We have a choice of producing work (xi1 ~ xi2) (up-to the
    canonicalization invariants) However, to prevent the inert items
    from getting kicked out of the inerts first, we prefer to
    canonicalize (xi1 ~ xi2) if (b) comes from the inert set, or (xi2
    ~ xi1) if (a) comes from the inert set.
    
275 276 277 278 279
Case 2: Functional Dependencies 
    Again, we should prefer, if possible, the inert variables on the RHS

Case 3: IP improvement work
    We must always rewrite so that the inert type is on the right. 
280

281 282
\begin{code}
spontaneousSolveStage :: SimplifierStage 
283
spontaneousSolveStage workItem
284
  = do { mSolve <- trySpontaneousSolve workItem
285
       ; spont_solve mSolve } 
286 287 288 289 290 291 292
  where spont_solve SPCantSolve 
          | isCTyEqCan workItem                    -- Unsolved equality
          = do { kickOutRewritableInerts workItem  -- NB: will add workItem in inerts
               ; return Stop }
          | otherwise
          = continueWith workItem
        spont_solve (SPSolved workItem')           -- Post: workItem' must be equality
293 294
          = do { bumpStepCountTcS
               ; traceFireTcS (cc_depth workItem) $
295
                 ptext (sLit "Spontaneous:") <+> ppr workItem
296 297 298 299

                 -- NB: will add the item in the inerts
               ; kickOutRewritableInerts workItem'
               -- .. and Stop
300 301 302 303
               ; return Stop }

kickOutRewritableInerts :: Ct -> TcS () 
-- Pre:  ct is a CTyEqCan 
304 305 306
-- Post: The TcS monad is left with the thinner non-rewritable inerts; but which
--       contains the new constraint.
--       The rewritable end up in the worklist
dimitris's avatar
dimitris committed
307
kickOutRewritableInerts ct
308
  = {-# SCC "kickOutRewritableInerts" #-}
dimitris's avatar
dimitris committed
309 310
    do { traceTcS "kickOutRewritableInerts" $ text "workitem = " <+> ppr ct
       ; (wl,ieqs) <- {-# SCC "kick_out_rewritable" #-}
311
                      modifyInertTcS (kick_out_rewritable ct)
dimitris's avatar
dimitris committed
312 313 314
       ; traceTcS "Kicked out the following constraints" $ ppr wl
       ; is <- getTcSInerts 
       ; traceTcS "Remaining inerts are" $ ppr is
315

dimitris's avatar
dimitris committed
316 317 318 319 320
       -- Step 1: Rewrite as many of the inert_eqs on the spot!
       -- NB: if it is a given constraint just use the cached evidence
       -- to optimize e.g. mkRefl coercions from spontaneously solved cts.
       ; bnds <- getTcEvBindsMap
       ; let ct_coercion = getCtCoercion bnds ct 
321 322

       ; new_ieqs <- {-# SCC "rewriteInertEqsFromInertEq" #-}
dimitris's avatar
dimitris committed
323
                     rewriteInertEqsFromInertEq (cc_tyvar ct,
324
                                                 ct_coercion,cc_ev ct) ieqs
dimitris's avatar
dimitris committed
325 326 327 328 329 330 331 332 333
       ; let upd_eqs is = is { inert_cans = new_ics }
                        where ics     = inert_cans is
                              new_ics = ics { inert_eqs = new_ieqs }
       ; modifyInertTcS (\is -> ((), upd_eqs is)) 
         
       ; is <- getTcSInerts 
       ; traceTcS "Final inerts are" $ ppr is
       
         -- Step 2: Add the new guy in
334
       ; updInertSetTcS ct
335 336 337

       ; traceTcS "Kick out" (ppr ct $$ ppr wl)
       ; updWorkListTcS (unionWorkList wl) }
dimitris's avatar
dimitris committed
338

339
rewriteInertEqsFromInertEq :: (TcTyVar, TcCoercion, CtEvidence) -- A new substitution
dimitris's avatar
dimitris committed
340 341
                           -> TyVarEnv Ct                     -- All the inert equalities
                           -> TcS (TyVarEnv Ct)               -- The new inert equalities
342 343 344
rewriteInertEqsFromInertEq (subst_tv, _subst_co, subst_fl) ieqs
-- The goal: traverse the inert equalities and throw some of them back to the worklist
-- if you have to rewrite and recheck them for occurs check errors. 
345
-- To see which ones we must throw out see Note [Delicate equality kick-out]
346 347 348
 = do { mieqs <- Traversable.mapM do_one ieqs 
      ; traceTcS "Original inert equalities:" (ppr ieqs)
      ; let flatten_justs elem venv
dimitris's avatar
dimitris committed
349
              | Just act <- elem = extendVarEnv venv (cc_tyvar act) act
350 351 352 353
              | otherwise = venv                                     
            final_ieqs = foldVarEnv flatten_justs emptyVarEnv mieqs
      ; traceTcS "Remaining inert equalities:" (ppr final_ieqs)
      ; return final_ieqs }
354

dimitris's avatar
dimitris committed
355
 where do_one ct
356
         | subst_fl `canRewrite` fl && (subst_tv `elemVarSet` tyVarsOfCt ct) 
dimitris's avatar
dimitris committed
357
         = if fl `canRewrite` subst_fl then
358 359
               -- If also the inert can rewrite the subst then there is no danger of 
               -- occurs check errors sor keep it there. No need to rewrite the inert equality
360 361
               -- (as we did in the past) because of point (8) of 
               -- Note [Detailed InertCans Invariants] and 
362 363
             return (Just ct)
             -- used to be: rewrite_on_the_spot ct >>= ( return . Just )
364
           else -- We have to throw inert back to worklist for occurs checks 
365
             updWorkListTcS (extendWorkListEq ct) >> return Nothing
366
         | otherwise -- Just keep it there
367
         = return (Just ct)
368
         where 
369
           fl  = cc_ev ct
dimitris's avatar
dimitris committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

kick_out_rewritable :: Ct 
                    -> InertSet 
                    -> ((WorkList, TyVarEnv Ct),InertSet)
-- Post: returns ALL inert equalities, to be dealt with later
-- 
kick_out_rewritable ct is@(IS { inert_cans = 
                                   IC { inert_eqs    = eqmap
                                      , inert_eq_tvs = inscope
                                      , inert_dicts  = dictmap
                                      , inert_ips    = ipmap
                                      , inert_funeqs = funeqmap
                                      , inert_irreds = irreds }
                              , inert_frozen = frozen })
  = ((kicked_out,eqmap), remaining)
385
  where
386 387
    rest_out = fro_out `andCts` dicts_out 
                   `andCts` ips_out `andCts` irs_out
388
    kicked_out = WorkList { wl_eqs    = []
dimitris's avatar
dimitris committed
389
                          , wl_funeqs = bagToList feqs_out
390
                          , wl_rest   = bagToList rest_out }
391
  
dimitris's avatar
dimitris committed
392 393 394 395 396 397 398 399 400 401 402 403
    remaining = is { inert_cans = IC { inert_eqs = emptyVarEnv
                                     , inert_eq_tvs = inscope 
                                       -- keep the same, safe and cheap
                                     , inert_dicts = dicts_in
                                     , inert_ips = ips_in
                                     , inert_funeqs = feqs_in
                                     , inert_irreds = irs_in }
                   , inert_frozen = fro_in } 
                -- NB: Notice that don't rewrite 
                -- inert_solved, inert_flat_cache and inert_solved_funeqs
                -- optimistically. But when we lookup we have to take the 
                -- subsitution into account
404
    fl = cc_ev ct
405
    tv = cc_tyvar ct
406 407
                               
    (ips_out,   ips_in)     = partitionCCanMap rewritable ipmap
408

dimitris's avatar
dimitris committed
409
    (feqs_out,  feqs_in)    = partCtFamHeadMap rewritable funeqmap
410
    (dicts_out, dicts_in)   = partitionCCanMap rewritable dictmap
411 412 413

    (irs_out,   irs_in)   = partitionBag rewritable irreds
    (fro_out,   fro_in)   = partitionBag rewritable frozen
dimitris's avatar
dimitris committed
414

415
    rewritable ct = (fl `canRewrite` cc_ev ct)  &&
416 417 418 419 420 421 422 423 424 425
                    (tv `elemVarSet` tyVarsOfCt ct) 
                    -- NB: tyVarsOfCt will return the type 
                    --     variables /and the kind variables/ that are 
                    --     directly visible in the type. Hence we will
                    --     have exposed all the rewriting we care about
                    --     to make the most precise kinds visible for 
                    --     matching classes etc. No need to kick out 
                    --     constraints that mention type variables whose
                    --     kinds could contain this variable!

426
\end{code}
427

428 429
Note [Delicate equality kick-out]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
dimitris's avatar
dimitris committed
430

431 432 433
Delicate:
When kicking out rewritable constraints, it would be safe to simply
kick out all rewritable equalities, but instead we only kick out those
434
that, when rewritten, may result in occur-check errors. Example:
435

436
          WorkItem =   [G] a ~ b
437 438
          Inerts   = { [W] b ~ [a] }
Now at this point the work item cannot be further rewritten by the
439 440 441 442 443 444
inert (due to the weaker inert flavor). Instead the workitem can 
rewrite the inert leading to potential occur check errors. So we must
kick the inert out. On the other hand, if the inert flavor was as 
powerful or more powerful than the workitem flavor, the work-item could 
not have reached this stage (because it would have already been 
rewritten by the inert).
445 446

The coclusion is: we kick out the 'dangerous' equalities that may
447 448
require recanonicalization (occurs checks) and the rest we keep 
there in the inerts without further checks.
449

450 451
In the past we used to rewrite-on-the-spot those equalities that we keep in,
but this is no longer necessary see Note [Non-idempotent inert substitution].
452 453

\begin{code}
454 455
data SPSolveResult = SPCantSolve
                   | SPSolved WorkItem 
456

457 458 459
-- SPCantSolve means that we can't do the unification because e.g. the variable is untouchable
-- SPSolved workItem' gives us a new *given* to go on 

460
-- @trySpontaneousSolve wi@ solves equalities where one side is a
461
-- touchable unification variable.
462
--     	    See Note [Touchables and givens] 
463
trySpontaneousSolve :: WorkItem -> TcS SPSolveResult
464
trySpontaneousSolve workItem@(CTyEqCan { cc_ev = gw
465
                                       , cc_tyvar = tv1, cc_rhs = xi, cc_depth = d })
466
  | isGiven gw
467
  = return SPCantSolve
468 469 470 471
  | Just tv2 <- tcGetTyVar_maybe xi
  = do { tch1 <- isTouchableMetaTyVar tv1
       ; tch2 <- isTouchableMetaTyVar tv2
       ; case (tch1, tch2) of
dimitris's avatar
dimitris committed
472 473 474
           (True,  True)  -> trySpontaneousEqTwoWay d gw tv1 tv2
           (True,  False) -> trySpontaneousEqOneWay d gw tv1 xi
           (False, True)  -> trySpontaneousEqOneWay d gw tv2 (mkTyVarTy tv1)
475
	   _ -> return SPCantSolve }
476 477
  | otherwise
  = do { tch1 <- isTouchableMetaTyVar tv1
dimitris's avatar
dimitris committed
478
       ; if tch1 then trySpontaneousEqOneWay d gw tv1 xi
479 480
                 else do { traceTcS "Untouchable LHS, can't spontaneously solve workitem:" $
                           ppr workItem 
481
                         ; return SPCantSolve }
482
       }
483 484 485 486

  -- No need for 
  --      trySpontaneousSolve (CFunEqCan ...) = ...
  -- See Note [No touchables as FunEq RHS] in TcSMonad
487
trySpontaneousSolve _ = return SPCantSolve
488 489

----------------
490
trySpontaneousEqOneWay :: SubGoalDepth 
491
                       -> CtEvidence -> TcTyVar -> Xi -> TcS SPSolveResult
492
-- tv is a MetaTyVar, not untouchable
dimitris's avatar
dimitris committed
493
trySpontaneousEqOneWay d gw tv xi
494
  | not (isSigTyVar tv) || isTyVarTy xi
dimitris's avatar
dimitris committed
495
  = solveWithIdentity d gw tv xi
496
  | otherwise -- Still can't solve, sig tyvar and non-variable rhs
497
  = return SPCantSolve
498 499

----------------
500
trySpontaneousEqTwoWay :: SubGoalDepth 
501
                       -> CtEvidence -> TcTyVar -> TcTyVar -> TcS SPSolveResult
502
-- Both tyvars are *touchable* MetaTyvars so there is only a chance for kind error here
503

dimitris's avatar
dimitris committed
504
trySpontaneousEqTwoWay d gw tv1 tv2
Simon Peyton Jones's avatar
Simon Peyton Jones committed
505
  = do { let k1_sub_k2 = k1 `tcIsSubKind` k2
dreixel's avatar
dreixel committed
506
       ; if k1_sub_k2 && nicer_to_update_tv2
dimitris's avatar
dimitris committed
507 508
         then solveWithIdentity d gw tv2 (mkTyVarTy tv1)
         else solveWithIdentity d gw tv1 (mkTyVarTy tv2) }
509 510 511 512 513 514
  where
    k1 = tyVarKind tv1
    k2 = tyVarKind tv2
    nicer_to_update_tv2 = isSigTyVar tv1 || isSystemName (Var.varName tv2)
\end{code}

515 516 517 518
Note [Kind errors] 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider the wanted problem: 
      alpha ~ (# Int, Int #) 
519
where alpha :: ArgKind and (# Int, Int #) :: (#). We can't spontaneously solve this constraint, 
520
but we should rather reject the program that give rise to it. If 'trySpontaneousEqTwoWay' 
521
simply returns @CantSolve@ then that wanted constraint is going to propagate all the way and 
522
get quantified over in inference mode. That's bad because we do know at this point that the 
523
constraint is insoluble. Instead, we call 'recKindErrorTcS' here, which will fail later on.
524 525

The same applies in canonicalization code in case of kind errors in the givens. 
526

527
However, when we canonicalize givens we only check for compatibility (@compatKind@). 
528
If there were a kind error in the givens, this means some form of inconsistency or dead code.
529

530 531 532 533 534
You may think that when we spontaneously solve wanteds we may have to look through the 
bindings to determine the right kind of the RHS type. E.g one may be worried that xi is 
@alpha@ where alpha :: ? and a previous spontaneous solving has set (alpha := f) with (f :: *).
But we orient our constraints so that spontaneously solved ones can rewrite all other constraint
so this situation can't happen. 
535

536 537
Note [Spontaneous solving and kind compatibility] 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
538 539 540
Note that our canonical constraints insist that *all* equalities (tv ~
xi) or (F xis ~ rhs) require the LHS and the RHS to have *compatible*
the same kinds.  ("compatible" means one is a subKind of the other.)
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555
  - It can't be *equal* kinds, because
     b) wanted constraints don't necessarily have identical kinds
               eg   alpha::? ~ Int
     b) a solved wanted constraint becomes a given

  - SPJ thinks that *given* constraints (tv ~ tau) always have that
    tau has a sub-kind of tv; and when solving wanted constraints
    in trySpontaneousEqTwoWay we re-orient to achieve this.

  - Note that the kind invariant is maintained by rewriting.
    Eg wanted1 rewrites wanted2; if both were compatible kinds before,
       wanted2 will be afterwards.  Similarly givens.

Caveat:
556 557 558 559 560 561 562 563 564
  - Givens from higher-rank, such as: 
          type family T b :: * -> * -> * 
          type instance T Bool = (->) 

          f :: forall a. ((T a ~ (->)) => ...) -> a -> ... 
          flop = f (...) True 
     Whereas we would be able to apply the type instance, we would not be able to 
     use the given (T Bool ~ (->)) in the body of 'flop' 

565 566 567 568 569 570 571

Note [Avoid double unifications] 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The spontaneous solver has to return a given which mentions the unified unification
variable *on the left* of the equality. Here is what happens if not: 
  Original wanted:  (a ~ alpha),  (alpha ~ Int) 
We spontaneously solve the first wanted, without changing the order! 
572
      given : a ~ alpha      [having unified alpha := a] 
573 574 575
Now the second wanted comes along, but he cannot rewrite the given, so we simply continue.
At the end we spontaneously solve that guy, *reunifying*  [alpha := Int] 

576
We avoid this problem by orienting the resulting given so that the unification
577 578
variable is on the left.  [Note that alternatively we could attempt to
enforce this at canonicalization]
579

580 581 582
See also Note [No touchables as FunEq RHS] in TcSMonad; avoiding
double unifications is the main reason we disallow touchable
unification variables as RHS of type family equations: F xis ~ alpha.
583 584 585

\begin{code}
----------------
586

587
solveWithIdentity :: SubGoalDepth 
588
                  -> CtEvidence -> TcTyVar -> Xi -> TcS SPSolveResult
589 590
-- Solve with the identity coercion 
-- Precondition: kind(xi) is a sub-kind of kind(tv)
591
-- Precondition: CtEvidence is Wanted or Derived
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
592 593
-- See [New Wanted Superclass Work] to see why solveWithIdentity 
--     must work for Derived as well as Wanted
594
-- Returns: workItem where 
595
--        workItem = the new Given constraint
dimitris's avatar
dimitris committed
596 597 598 599 600 601 602
solveWithIdentity d wd tv xi 
  = do { let tv_ty = mkTyVarTy tv
       ; traceTcS "Sneaky unification:" $ 
                       vcat [text "Constraint:" <+> ppr wd,
                             text "Coercion:" <+> pprEq tv_ty xi,
                             text "Left Kind is:" <+> ppr (typeKind tv_ty),
                             text "Right Kind is:" <+> ppr (typeKind xi) ]
603

604 605 606 607 608 609
       ; let xi' = defaultKind xi      
               -- We only instantiate kind unification variables
               -- with simple kinds like *, not OpenKind or ArgKind
               -- cf TcUnify.uUnboundKVar

       ; setWantedTyBind tv xi'
610 611
       ; let refl_evtm = EvCoercion (mkTcReflCo xi')
             refl_pred = mkTcEqPred tv_ty xi'
612

dimitris's avatar
dimitris committed
613
       ; when (isWanted wd) $ 
614
              setEvBind (ctev_evar wd) refl_evtm
615

616 617 618
       ; let given_fl = Given { ctev_gloc = mkGivenLoc (ctev_wloc wd) UnkSkol
                              , ctev_pred = refl_pred
                              , ctev_evtm = refl_evtm }
dimitris's avatar
dimitris committed
619 620
             
       ; return $ 
621
         SPSolved (CTyEqCan { cc_ev = given_fl
dimitris's avatar
dimitris committed
622
                            , cc_tyvar  = tv, cc_rhs = xi', cc_depth = d }) }
623 624
\end{code}

625 626 627 628 629 630 631

*********************************************************************************
*                                                                               * 
                       The interact-with-inert Stage
*                                                                               *
*********************************************************************************

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
Note [The Solver Invariant]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
We always add Givens first.  So you might think that the solver has
the invariant

   If the work-item is Given, 
   then the inert item must Given

But this isn't quite true.  Suppose we have, 
    c1: [W] beta ~ [alpha], c2 : [W] blah, c3 :[W] alpha ~ Int
After processing the first two, we get
     c1: [G] beta ~ [alpha], c2 : [W] blah
Now, c3 does not interact with the the given c1, so when we spontaneously
solve c3, we must re-react it with the inert set.  So we can attempt a 
reaction between inert c2 [W] and work-item c3 [G].

It *is* true that [Solver Invariant]
   If the work-item is Given, 
   AND there is a reaction
   then the inert item must Given
or, equivalently,
   If the work-item is Given, 
   and the inert item is Wanted/Derived
   then there is no reaction

657
\begin{code}
658
-- Interaction result of  WorkItem <~> Ct
659

660 661 662 663
data InteractResult 
    = IRWorkItemConsumed { ir_fire :: String } 
    | IRInertConsumed    { ir_fire :: String } 
    | IRKeepGoing        { ir_fire :: String }
664

665 666
irWorkItemConsumed :: String -> TcS InteractResult
irWorkItemConsumed str = return (IRWorkItemConsumed str) 
667

668 669
irInertConsumed :: String -> TcS InteractResult
irInertConsumed str = return (IRInertConsumed str) 
670

671 672 673 674
irKeepGoing :: String -> TcS InteractResult 
irKeepGoing str = return (IRKeepGoing str) 
-- You can't discard neither workitem or inert, but you must keep 
-- going. It's possible that new work is waiting in the TcS worklist. 
675 676


677 678 679 680
interactWithInertsStage :: WorkItem -> TcS StopOrContinue 
-- Precondition: if the workitem is a CTyEqCan then it will not be able to 
-- react with anything at this stage. 
interactWithInertsStage wi 
681 682 683 684
  = do { traceTcS "interactWithInerts" $ text "workitem = " <+> ppr wi
       ; rels <- extractRelevantInerts wi 
       ; traceTcS "relevant inerts are:" $ ppr rels
       ; foldlBagM interact_next (ContinueWith wi) rels }
685 686 687 688 689 690

  where interact_next Stop atomic_inert 
          = updInertSetTcS atomic_inert >> return Stop
        interact_next (ContinueWith wi) atomic_inert 
          = do { ir <- doInteractWithInert atomic_inert wi
               ; let mk_msg rule keep_doc 
691 692 693
                       = vcat [ text rule <+> keep_doc
                              , ptext (sLit "InertItem =") <+> ppr atomic_inert
                              , ptext (sLit "WorkItem  =") <+> ppr wi ]
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
               ; case ir of 
                   IRWorkItemConsumed { ir_fire = rule } 
                       -> do { bumpStepCountTcS
                             ; traceFireTcS (cc_depth wi) 
                                            (mk_msg rule (text "WorkItemConsumed"))
                             ; updInertSetTcS atomic_inert
                             ; return Stop } 
                   IRInertConsumed { ir_fire = rule }
                       -> do { bumpStepCountTcS
                             ; traceFireTcS (cc_depth atomic_inert) 
                                            (mk_msg rule (text "InertItemConsumed"))
                             ; return (ContinueWith wi) }
                   IRKeepGoing {} -- Should we do a bumpStepCountTcS? No for now.
                       -> do { updInertSetTcS atomic_inert
                             ; return (ContinueWith wi) }
               }
710 711 712

\end{code}

713
\begin{code}
714 715
--------------------------------------------

716 717
doInteractWithInert :: Ct -> Ct -> TcS InteractResult
-- Identical class constraints.
718
doInteractWithInert
719 720
  inertItem@(CDictCan { cc_ev = fl1, cc_class = cls1, cc_tyargs = tys1 }) 
   workItem@(CDictCan { cc_ev = fl2, cc_class = cls2, cc_tyargs = tys2 })
721

722
  | cls1 == cls2  
batterseapower's avatar
batterseapower committed
723 724
  = do { let pty1 = mkClassPred cls1 tys1
             pty2 = mkClassPred cls2 tys2
725
             inert_pred_loc     = (pty1, pprFlavorArising fl1)
726
             work_item_pred_loc = (pty2, pprFlavorArising fl2)
727

728 729 730
       ; traceTcS "doInteractWithInert" (vcat [ text "inertItem = " <+> ppr inertItem
                                              , text "workItem  = " <+> ppr workItem ])

731
       ; any_fundeps 
732
           <- if isGiven fl1 && isGiven fl2 then return Nothing
733 734 735 736
              -- NB: We don't create fds for given (and even solved), have not seen a useful
              -- situation for these and even if we did we'd have to be very careful to only
              -- create Derived's and not Wanteds. 

737
              else do { let fd_eqns = improveFromAnother inert_pred_loc work_item_pred_loc
738
                            wloc    = getWantedLoc fl2 
739
                      ; rewriteWithFunDeps fd_eqns tys2 wloc }
740 741 742 743 744
                      -- See Note [Efficient Orientation], [When improvement happens]

       ; case any_fundeps of
           -- No Functional Dependencies 
           Nothing             
dimitris's avatar
dimitris committed
745
               | eqTypes tys1 tys2 -> solveOneFromTheOther "Cls/Cls" fl1 workItem
746
               | otherwise         -> irKeepGoing "NOP"
747 748

           -- Actual Functional Dependencies
749
           Just (_rewritten_tys2, fd_work)
750
              -- Standard thing: create derived fds and keep on going. Importantly we don't
751
               -- throw workitem back in the worklist because this can cause loops. See #5236.
752 753
               -> do { emitFDWorkAsDerived fd_work (cc_depth workItem)
                     ; irKeepGoing "Cls/Cls (new fundeps)" } -- Just keep going without droping the inert 
754
       }
755
 
dimitris's avatar
dimitris committed
756 757 758 759
-- Two pieces of irreducible evidence: if their types are *exactly identical* 
-- we can rewrite them. We can never improve using this: 
-- if we want ty1 :: Constraint and have ty2 :: Constraint it clearly does not 
-- mean that (ty1 ~ ty2)
760
doInteractWithInert (CIrredEvCan { cc_ev = ifl, cc_ty = ty1 })
761 762
           workItem@(CIrredEvCan { cc_ty = ty2 })
  | ty1 `eqType` ty2
dimitris's avatar
dimitris committed
763
  = solveOneFromTheOther "Irred/Irred" ifl workItem
764

765 766 767 768 769
-- Two implicit parameter constraints.  If the names are the same,
-- but their types are not, we generate a wanted type equality 
-- that equates the type (this is "improvement").  
-- However, we don't actually need the coercion evidence,
-- so we just generate a fresh coercion variable that isn't used anywhere.
770 771 772
doInteractWithInert (CIPCan { cc_ev = ifl, cc_ip_nm = nm1, cc_ip_ty = ty1 }) 
           workItem@(CIPCan { cc_ev = wfl, cc_ip_nm = nm2, cc_ip_ty = ty2 })
  | nm1 == nm2 && isGiven wfl && isGiven ifl
773 774 775
  = 	-- See Note [Overriding implicit parameters]
        -- Dump the inert item, override totally with the new one
	-- Do not require type equality
776 777
	-- For example, given let ?x::Int = 3 in let ?x::Bool = True in ...
	--              we must *override* the outer one with the inner one
778
    irInertConsumed "IP/IP (override inert)"
779

780
  | nm1 == nm2 && ty1 `eqType` ty2 
dimitris's avatar
dimitris committed
781
  = solveOneFromTheOther "IP/IP" ifl workItem 
782

783
  | nm1 == nm2
784
  =  	-- See Note [When improvement happens]
785
    do { mb_eqv <- newWantedEvVar new_wloc (mkEqPred ty2 ty1)
dimitris's avatar
dimitris committed
786 787 788 789
         -- co :: ty2 ~ ty1, see Note [Efficient orientation]
       ; cv <- case mb_eqv of
                 Fresh eqv  -> 
                   do { updWorkListTcS $ extendWorkListEq $ 
790
                        CNonCanonical { cc_ev = eqv
dimitris's avatar
dimitris committed
791
                                      , cc_depth = cc_depth workItem }
792
                      ; return (ctEvTerm eqv) }
dimitris's avatar
dimitris committed
793 794
                 Cached eqv -> return eqv
       ; case wfl of
795 796 797 798
            Wanted { ctev_evar = ev_id } ->
              let ip_co = mkTcTyConAppCo (ipTyCon nm1) [evTermCoercion cv]
              in do { setEvBind ev_id $
                      mkEvCast (ctEvTerm ifl) (mkTcSymCo ip_co)
dimitris's avatar
dimitris committed
799 800
                    ; irWorkItemConsumed "IP/IP (solved by rewriting)" }
            _ -> pprPanic "Unexpected IP constraint" (ppr workItem) }
801 802 803
  where 
    new_wloc | isGiven wfl = getWantedLoc ifl
             | otherwise   = getWantedLoc wfl
804

805
doInteractWithInert ii@(CFunEqCan { cc_ev = fl1, cc_fun = tc1
dimitris's avatar
dimitris committed
806
                                  , cc_tyargs = args1, cc_rhs = xi1, cc_depth = d1 }) 
807
                    wi@(CFunEqCan { cc_ev = fl2, cc_fun = tc2
dimitris's avatar
dimitris committed
808
                                  , cc_tyargs = args2, cc_rhs = xi2, cc_depth = d2 })
809
{-  ToDo: Check with Dimitrios
810
  | lhss_match  
dimitris's avatar
dimitris committed
811 812
  , isSolved fl1 -- Inert is solved and we can simply ignore it
                 -- when workitem is given/solved
813
  , isGiven fl2
814
  = irInertConsumed "FunEq/FunEq"
dimitris's avatar
dimitris committed
815 816 817
  | lhss_match
  , isSolved fl2 -- Workitem is solved and we can ignore it when
                 -- the inert is given/solved
818
  , isGiven fl1                 
819
  = irWorkItemConsumed "FunEq/FunEq" 
820 821
-}

822
  | fl1 `canSolve` fl2 && lhss_match
dimitris's avatar
dimitris committed
823
  = do { traceTcS "interact with inerts: FunEq/FunEq" $ 
824 825 826 827 828 829 830 831 832 833
         vcat [ text "workItem =" <+> ppr wi
              , text "inertItem=" <+> ppr ii ]

       ; let xev = XEvTerm xcomp xdecomp
             -- xcomp : [(xi2 ~ xi1)] -> (F args ~ xi2) 
             xcomp [x] = EvCoercion (co1 `mkTcTransCo` mk_sym_co x)
             xcomp _   = panic "No more goals!"
             -- xdecomp : (F args ~ xi2) -> [(xi2 ~ xi1)]                 
             xdecomp x = [EvCoercion (mk_sym_co x `mkTcTransCo` co1)]

834
       ; ctevs <- xCtFlavor_cache False fl2 [mkTcEqPred xi2 xi1] xev
835 836
                         -- Why not simply xCtFlavor? See Note [Cache-caused loops]
                         -- Why not (mkTcEqPred xi1 xi2)? See Note [Efficient orientation]
837
       ; add_to_work d2 ctevs 
838
       ; irWorkItemConsumed "FunEq/FunEq" }
839

840
  | fl2 `canSolve` fl1 && lhss_match
841 842 843 844 845 846
  = do { traceTcS "interact with inerts: FunEq/FunEq" $ 
         vcat [ text "workItem =" <+> ppr wi
              , text "inertItem=" <+> ppr ii ]

       ; let xev = XEvTerm xcomp xdecomp
              -- xcomp : [(xi2 ~ xi1)] -> [(F args ~ xi1)]
847
             xcomp [x] = EvCoercion (co2 `mkTcTransCo` evTermCoercion x)
848 849
             xcomp _ = panic "No more goals!"
             -- xdecomp : (F args ~ xi1) -> [(xi2 ~ xi1)]
850
             xdecomp x = [EvCoercion (mkTcSymCo co2 `mkTcTransCo` evTermCoercion x)]
851

852
       ; ctevs <- xCtFlavor_cache False fl1 [mkTcEqPred xi2 xi1] xev 
853 854 855
                          -- Why not simply xCtFlavor? See Note [Cache-caused loops]
                          -- Why not (mkTcEqPred xi1 xi2)? See Note [Efficient orientation]

856
       ; add_to_work d1 ctevs 
857
       ; irInertConsumed "FunEq/FunEq"}
858
  where
859 860 861 862
    add_to_work d [ctev] = updWorkListTcS $ extendWorkListEq $
                           CNonCanonical {cc_ev = ctev, cc_depth = d}
    add_to_work _ _ = return ()

863
    lhss_match = tc1 == tc2 && eqTypes args1 args2 
864 865 866
    co1 = evTermCoercion $ ctEvTerm fl1
    co2 = evTermCoercion $ ctEvTerm fl2
    mk_sym_co x = mkTcSymCo (evTermCoercion x)
dimitris's avatar
dimitris committed
867
    
868 869
doInteractWithInert _ _ = irKeepGoing "NOP"

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
\end{code}

Note [Cache-caused loops]
~~~~~~~~~~~~~~~~~~~~~~~~~
It is very dangerous to cache a rewritten wanted family equation as 'solved' in our 
solved cache (which is the default behaviour or xCtFlavor), because the interaction 
may not be contributing towards a solution. Here is an example:

Initial inert set:
  [W] g1 : F a ~ beta1
Work item:
  [W] g2 : F a ~ beta2
The work item will react with the inert yielding the _same_ inert set plus:
    i)   Will set g2 := g1 `cast` g3   
    ii)  Will add to our solved cache that [S] g2 : F a ~ beta2
    iii) Will emit [W] g3 : beta1 ~ beta2 
Now, the g3 work item will be spontaneously solved to [G] g3 : beta1 ~ beta2
and then it will react the item in the inert ([W] g1 : F a ~ beta1). So it 
will set 
      g1 := g ; sym g3 
and what is g? Well it would ideally be a new goal of type (F a ~ beta2) but
remember that we have this in our solved cache, and it is ... g2! In short we 
created the evidence loop:

        g2 := g1 ; g3 
        g3 := refl
        g1 := g2 ; sym g3 

To avoid this situation we do not cache as solved any workitems (or inert) 
which did not really made a 'step' towards proving some goal. Solved's are 
just an optimization so we don't lose anything in terms of completeness of 
solving.

\begin{code}

dimitris's avatar
dimitris committed
905
solveOneFromTheOther :: String    -- Info 
906
                     -> CtEvidence  -- Inert 
907 908 909 910 911
                     -> Ct        -- WorkItem 
                     -> TcS InteractResult
-- Preconditions: 
-- 1) inert and work item represent evidence for the /same/ predicate
-- 2) ip/class/irred evidence (no coercions) only
dimitris's avatar
dimitris committed
912
solveOneFromTheOther info ifl workItem
913
  | isDerived wfl
914
  = irWorkItemConsumed ("Solved[DW] " ++ info)
915

916 917 918
  | isDerived ifl -- The inert item is Derived, we can just throw it away, 
    	      	  -- The workItem is inert wrt earlier inert-set items, 
		  -- so it's safe to continue on from this point
919
  = irInertConsumed ("Solved[DI] " ++ info)
920
  
921 922
{-  ToDo: Check with Dimitrios
  | isSolved ifl, isGiven wfl
dimitris's avatar
dimitris committed
923
    -- Same if the inert is a GivenSolved -- just get rid of it
924
  = irInertConsumed ("Solved[SI] " ++ info)
925
-}
dimitris's avatar
dimitris committed
926

927 928 929
  | otherwise
  = ASSERT( ifl `canSolve` wfl )
      -- Because of Note [The Solver Invariant], plus Derived dealt with
930 931 932
    do { case wfl of
           Wanted { ctev_evar = ev_id } -> setEvBind ev_id (ctEvTerm ifl)
           _                            -> return ()
933 934
           -- Overwrite the binding, if one exists
	   -- If both are Given, we already have evidence; no need to duplicate
935
       ; irWorkItemConsumed ("Solved " ++ info) }
936
  where 
937
     wfl = cc_ev workItem
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
\end{code}

Note [Superclasses and recursive dictionaries]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Overlaps with Note [SUPERCLASS-LOOP 1]
                  Note [SUPERCLASS-LOOP 2]
                  Note [Recursive instances and superclases]
    ToDo: check overlap and delete redundant stuff

Right before adding a given into the inert set, we must
produce some more work, that will bring the superclasses 
of the given into scope. The superclass constraints go into 
our worklist. 

When we simplify a wanted constraint, if we first see a matching
instance, we may produce new wanted work. To (1) avoid doing this work 
twice in the future and (2) to handle recursive dictionaries we may ``cache'' 
955 956 957
this item as given into our inert set WITHOUT adding its superclass constraints, 
otherwise we'd be in danger of creating a loop [In fact this was the exact reason
for doing the isGoodRecEv check in an older version of the type checker]. 
958 959 960 961 962 963 964 965 966 967

But now we have added partially solved constraints to the worklist which may 
interact with other wanteds. Consider the example: 

Example 1: 

    class Eq b => Foo a b        --- 0-th selector
    instance Eq a => Foo [a] a   --- fooDFun

and wanted (Foo [t] t). We are first going to see that the instance matches 
968
and create an inert set that includes the solved (Foo [t] t) but not its superclasses:
969 970 971 972
       d1 :_g Foo [t] t                 d1 := EvDFunApp fooDFun d3 
Our work list is going to contain a new *wanted* goal
       d3 :_w Eq t 

973
Ok, so how do we get recursive dictionaries, at all: 
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

Example 2:

    data D r = ZeroD | SuccD (r (D r));
    
    instance (Eq (r (D r))) => Eq (D r) where
        ZeroD     == ZeroD     = True
        (SuccD a) == (SuccD b) = a == b
        _         == _         = False;
    
    equalDC :: D [] -> D [] -> Bool;
    equalDC = (==);

We need to prove (Eq (D [])). Here's how we go:

	d1 :_w Eq (D [])

by instance decl, holds if
	d2 :_w Eq [D []]
	where 	d1 = dfEqD d2

*BUT* we have an inert set which gives us (no superclasses): 
        d1 :_g Eq (D []) 
By the instance declaration of Eq we can show the 'd2' goal if 
	d3 :_w Eq (D [])
	where	d2 = dfEqList d3
		d1 = dfEqD d2
Now, however this wanted can interact with our inert d1 to set: 
        d3 := d1 
and solve the goal. Why was this interaction OK? Because, if we chase the 
evidence of d1 ~~> dfEqD d2 ~~-> dfEqList d3, so by setting d3 := d1 we 
are really setting
        d3 := dfEqD2 (dfEqList d3) 
which is FINE because the use of d3 is protected by the instance function 
applications. 

So, our strategy is to try to put solved wanted dictionaries into the
inert set along with their superclasses (when this is meaningful,
i.e. when new wanted goals are generated) but solve a wanted dictionary
from a given only in the case where the evidence variable of the
wanted is mentioned in the evidence of the given (recursively through
the evidence binds) in a protected way: more instance function applications 
than superclass selectors.

Here are some more examples from GHC's previous type checker


Example 3: 
This code arises in the context of "Scrap Your Boilerplate with Class"

    class Sat a
    class Data ctx a
    instance  Sat (ctx Char)             => Data ctx Char       -- dfunData1
    instance (Sat (ctx [a]), Data ctx a) => Data ctx [a]        -- dfunData2

    class Data Maybe a => Foo a    

    instance Foo t => Sat (Maybe t)                             -- dfunSat

    instance Data Maybe a => Foo a                              -- dfunFoo1
    instance Foo a        => Foo [a]                            -- dfunFoo2
    instance                 Foo [Char]                         -- dfunFoo3

Consider generating the superclasses of the instance declaration
	 instance Foo a => Foo [a]

So our problem is this
    d0 :_g Foo t
    d1 :_w Data Maybe [t] 

We may add the given in the inert set, along with its superclasses
[assuming we don't fail because there is a matching instance, see 
 tryTopReact, given case ]
  Inert:
    d0 :_g Foo t 
  WorkList 
    d01 :_g Data Maybe t  -- d2 := EvDictSuperClass d0 0 
    d1 :_w Data Maybe [t] 
Then d2 can readily enter the inert, and we also do solving of the wanted
  Inert: 
    d0 :_g Foo t 
    d1 :_s Data Maybe [t]           d1 := dfunData2 d2 d3 
  WorkList
    d2 :_w Sat (Maybe [t])          
    d3 :_w Data Maybe t
    d01 :_g Data Maybe t 
Now, we may simplify d2 more: 
  Inert:
      d0 :_g Foo t 
      d1 :_s Data Maybe [t]           d1 := dfunData2 d2 d3 
      d1 :_g Data Maybe [t] 
      d2 :_g Sat (Maybe [t])          d2 := dfunSat d4 
  WorkList: 
      d3 :_w Data Maybe t 
      d4 :_w Foo [t] 
      d01 :_g Data Maybe t 

Now, we can just solve d3.
  Inert
      d0 :_g Foo t 
      d1 :_s Data Maybe [t]           d1 := dfunData2 d2 d3 
      d2 :_g Sat (Maybe [t])          d2 := dfunSat d4 
  WorkList
      d4 :_w Foo [t] 
      d01 :_g Data Maybe t 
And now we can simplify d4 again, but since it has superclasses we *add* them to the worklist:
  Inert
      d0 :_g Foo t 
      d1 :_s Data Maybe [t]           d1 := dfunData2 d2 d3 
      d2 :_g Sat (Maybe [t])          d2 := dfunSat d4 
      d4 :_g Foo [t]                  d4 := dfunFoo2 d5 
  WorkList:
      d5 :_w Foo t 
      d6 :_g Data Maybe [t]           d6 := EvDictSuperClass d4 0
      d01 :_g Data Maybe t 
Now, d5 can be solved! (and its superclass enter scope) 
  Inert
      d0 :_g Foo t 
      d1 :_s Data Maybe [t]           d1 := dfunData2 d2 d3 
      d2 :_g Sat (Maybe [t])          d2 := dfunSat d4 
      d4 :_g Foo [t]                  d4 := dfunFoo2 d5 
      d5 :_g Foo t                    d5 := dfunFoo1 d7
  WorkList:
      d7 :_w Data Maybe t
      d6 :_g Data Maybe [t]
      d8 :_g Data Maybe t            d8 := EvDictSuperClass d5 0
      d01 :_g Data Maybe t 

Now, two problems: 
   [1] Suppose we pick d8 and we react him with d01. Which of the two givens should 
       we keep? Well, we *MUST NOT* drop d01 because d8 contains recursive evidence 
       that must not be used (look at case interactInert where both inert and workitem
       are givens). So we have several options: 
       - Drop the workitem always (this will drop d8)
              This feels very unsafe -- what if the work item was the "good" one
              that should be used later to solve another wanted?
       - Don't drop anyone: the inert set may contain multiple givens! 
              [This is currently implemented] 

The "don't drop anyone" seems the most safe thing to do, so now we come to problem 2: 
  [2] We have added both d6 and d01 in the inert set, and we are interacting our wanted
      d7. Now the [isRecDictEv] function in the ineration solver 
      [case inert-given workitem-wanted] will prevent us from interacting d7 := d8 
      precisely because chasing the evidence of d8 leads us to an unguarded use of d7. 

      So, no interaction happens there. Then we meet d01 and there is no recursion 
      problem there [isRectDictEv] gives us the OK to interact and we do solve d7 := d01! 
             
Note [SUPERCLASS-LOOP 1]
~~~~~~~~~~~~~~~~~~~~~~~~
We have to be very, very careful when generating superclasses, lest we
accidentally build a loop. Here's an example:

  class S a

  class S a => C a where { opc :: a -> a }
  class S b => D b where { opd :: b -> b }
  
  instance C Int where
     opc = opd
  
  instance D Int where
     opd = opc

From (instance C Int) we get the constraint set {ds1:S Int, dd:D Int}
Simplifying, we may well get:
	$dfCInt = :C ds1 (opd dd)
	dd  = $dfDInt
	ds1 = $p1 dd
Notice that we spot that we can extract ds1 from dd.  

Alas!  Alack! We can do the same for (instance D Int):

	$dfDInt = :D ds2 (opc dc)
	dc  = $dfCInt
	ds2 = $p1 dc

And now we've defined the superclass in terms of itself.
Two more nasty cases are in
	tcrun021
	tcrun033

Solution: 
  - Satisfy the superclass context *all by itself* 
    (tcSimplifySuperClasses)
  - And do so completely; i.e. no left-over constraints
    to mix with the constraints arising from method declarations


Note [SUPERCLASS-LOOP 2]
~~~~~~~~~~~~~~~~~~~~~~~~
We need to be careful when adding "the constaint we are trying to prove".
Suppose we are *given* d1:Ord a, and want to deduce (d2:C [a]) where

	class Ord a => C a where
	instance Ord [a] => C [a] where ...

Then we'll use the instance decl to deduce C [a] from Ord [a], and then add the
superclasses of C [a] to avails.  But we must not overwrite the binding
for Ord [a] (which is obtained from Ord a) with a superclass selection or we'll just
build a loop! 

Here's another variant, immortalised in tcrun020
	class Monad m => C1 m
	class C1 m => C2 m x
	instance C2 Maybe Bool
For the instance decl we need to build (C1 Maybe), and it's no good if
we run around and add (C2 Maybe Bool) and its superclasses to the avails 
before we search for C1 Maybe.

Here's another example 
 	class Eq b => Foo a b
	instance Eq a => Foo [a] a
If we are reducing
	(Foo [t] t)

we'll first deduce that it holds (via the instance decl).  We must not
then overwrite the Eq t constraint with a superclass selection!

At first I had a gross hack, whereby I simply did not add superclass constraints
in addWanted, though I did for addGiven and addIrred.  This was sub-optimal,
becuase it lost legitimate superclass sharing, and it still didn't do the job:
I found a very obscure program (now tcrun021) in which improvement meant the
simplifier got two bites a the cherry... so something seemed to be an Stop
first time, but reducible next time.

Now we implement the Right Solution, which is to check for loops directly 
when adding superclasses.  It's a bit like the occurs check in unification.

Note [Recursive instances and superclases]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider this code, which arises in the context of "Scrap Your 
Boilerplate with Class".  

    class Sat a
    class Data ctx a
    instance  Sat (ctx Char)             => Data ctx Char
    instance (Sat (ctx [a]), Data ctx a) => Data ctx [a]

    class Data Maybe a => Foo a

    instance Foo t => Sat (Maybe t)