| ... |
... |
@@ -354,84 +354,42 @@ solveImplication imp@(Implic { ic_tclvl = tclvl |
|
354
|
354
|
----------------------
|
|
355
|
355
|
setImplicationStatus :: Implication -> TcS Implication
|
|
356
|
356
|
-- Finalise the implication returned from solveImplication,
|
|
357
|
|
--- setting the ic_status field
|
|
|
357
|
+-- * Set the ic_status field
|
|
|
358
|
+-- * Prune unnecessary evidence bindings
|
|
|
359
|
+-- * Prune unnecessary child implications
|
|
358
|
360
|
-- Precondition: the ic_status field is not already IC_Solved
|
|
359
|
|
--- Return Nothing if we can discard the implication altogether
|
|
360
|
361
|
setImplicationStatus implic@(Implic { ic_status = old_status
|
|
361
|
362
|
, ic_info = info
|
|
362
|
363
|
, ic_wanted = wc })
|
|
363
|
|
- | assertPpr (not (isSolvedStatus old_status)) (ppr info) $
|
|
|
364
|
+ = assertPpr (not (isSolvedStatus old_status)) (ppr info) $
|
|
364
|
365
|
-- Precondition: we only set the status if it is not already solved
|
|
365
|
|
- not (isSolvedWC wc)
|
|
366
|
|
- = do { traceTcS "setImplicationStatus(not-all-solved) {" (ppr implic)
|
|
|
366
|
+ do { traceTcS "setImplicationStatus {" (ppr implic)
|
|
367
|
367
|
|
|
368
|
|
- ; let new_status | insolubleWC wc = IC_Insoluble
|
|
369
|
|
- | otherwise = IC_Unsolved
|
|
370
|
|
- new_implic = pruneImplications (implic { ic_status = new_status })
|
|
371
|
|
-
|
|
372
|
|
- ; traceTcS "setImplicationStatus(not-all-solved) }" (ppr new_implic)
|
|
373
|
|
-
|
|
374
|
|
- ; return new_implic }
|
|
375
|
|
-
|
|
376
|
|
- | otherwise
|
|
377
|
|
- = do { traceTcS "setImplicationStatus(solved) {" (ppr implic)
|
|
|
368
|
+ ; let solved = isSolvedWC wc
|
|
|
369
|
+ ; new_implic <- neededEvVars implic
|
|
|
370
|
+ ; bad_telescope <- if solved then checkBadTelescope implic
|
|
|
371
|
+ else return False
|
|
378
|
372
|
|
|
379
|
|
- ; (dead_givens, implic) <- neededEvVars implic
|
|
380
|
|
-
|
|
381
|
|
- ; bad_telescope <- checkBadTelescope implic
|
|
|
373
|
+ ; let new_status | insolubleWC wc = IC_Insoluble
|
|
|
374
|
+ | not solved = IC_Unsolved
|
|
|
375
|
+ | bad_telescope = IC_BadTelescope
|
|
|
376
|
+ | otherwise = IC_Solved { ics_dead = dead_givens }
|
|
|
377
|
+ dead_givens = findRedundantGivens new_implic
|
|
382
|
378
|
|
|
383
|
|
- ; let final_status
|
|
384
|
|
- | bad_telescope = IC_BadTelescope
|
|
385
|
|
- | otherwise = IC_Solved { ics_dead = dead_givens }
|
|
386
|
|
- final_implic = pruneImplications (implic { ic_status = final_status })
|
|
|
379
|
+ final_implic = new_implic { ic_status = new_status }
|
|
387
|
380
|
|
|
388
|
|
- ; traceTcS "setImplicationStatus(solved) }" (ppr final_implic)
|
|
|
381
|
+ ; traceTcS "setImplicationStatus }" (ppr final_implic)
|
|
389
|
382
|
; return final_implic }
|
|
390
|
383
|
|
|
391
|
|
-pruneImplications :: Implication -> Implication
|
|
392
|
|
--- We have now taken account of the `needs_outer` variables of these
|
|
393
|
|
--- implications, so we can drop any that are no longer necessary
|
|
394
|
|
-pruneImplications implic@(Implic { ic_wanted = wc
|
|
395
|
|
- , ic_need_pruned = old_needs })
|
|
396
|
|
- = implic { ic_need_pruned = new_needs
|
|
397
|
|
- , ic_wanted = wc { wc_impl = new_implics } }
|
|
398
|
|
- -- Do not prune holes; these should be reported
|
|
399
|
|
- where
|
|
400
|
|
- (new_needs, new_implics) = foldr do_one (old_needs, emptyBag) (wc_impl wc)
|
|
401
|
|
-
|
|
402
|
|
- do_one :: Implication -> (EvNeedSet, Bag Implication) -> (EvNeedSet, Bag Implication)
|
|
403
|
|
- do_one implic (ens, implics)
|
|
404
|
|
- | keep_me implic = (ens, implic `consBag` implics)
|
|
405
|
|
- | otherwise = (add_needs ens implic, implics)
|
|
406
|
|
-
|
|
407
|
|
- keep_me :: Implication -> Bool
|
|
408
|
|
- keep_me (Implic { ic_status = status, ic_wanted = wanted })
|
|
409
|
|
- | IC_Solved { ics_dead = dead_givens } <- status -- Fully solved
|
|
410
|
|
- , null dead_givens -- No redundant givens to report
|
|
411
|
|
- , isEmptyBag (wc_impl wanted) -- No children that might have things to report
|
|
412
|
|
- = False
|
|
413
|
|
- | otherwise
|
|
414
|
|
- = True -- Otherwise, keep it
|
|
415
|
|
-
|
|
416
|
|
- add_needs :: EvNeedSet -> Implication -> EvNeedSet
|
|
417
|
|
- -- For a default-method implication, add all its needed vars to ens_dms
|
|
418
|
|
- -- For anything else, just propagate
|
|
419
|
|
- add_needs (ENS { ens_dms = dms, ens_fvs = fvs })
|
|
420
|
|
- (Implic { ic_need_outer = ENS { ens_dms = dms1, ens_fvs = fvs1 }
|
|
421
|
|
- , ic_info = info })
|
|
422
|
|
- | is_dm_skol info = ENS { ens_dms = dms `unionVarSet` dms1 `unionVarSet` fvs1
|
|
423
|
|
- , ens_fvs = fvs }
|
|
424
|
|
- | otherwise = ENS { ens_dms = dms `unionVarSet` dms1
|
|
425
|
|
- , ens_fvs = fvs `unionVarSet` fvs1 }
|
|
426
|
|
-
|
|
427
|
|
-findUnnecessaryGivens :: SkolemInfoAnon -> VarSet -> [EvVar] -> [EvVar]
|
|
428
|
|
-findUnnecessaryGivens info need_inner givens
|
|
|
384
|
+findRedundantGivens :: Implication -> [EvVar]
|
|
|
385
|
+findRedundantGivens (Implic { ic_info = info, ic_need = need, ic_given = givens })
|
|
429
|
386
|
| not (warnRedundantGivens info) -- Don't report redundant constraints at all
|
|
430
|
|
- = []
|
|
|
387
|
+ = [] -- See (TRC4) of Note [Tracking redundant constraints]
|
|
431
|
388
|
|
|
432
|
389
|
| not (null unused_givens) -- Some givens are literally unused
|
|
433
|
390
|
= unused_givens
|
|
434
|
391
|
|
|
|
392
|
+ -- Only try this if unused_givens is empty: see (TRC2a)
|
|
435
|
393
|
| otherwise -- All givens are used, but some might
|
|
436
|
394
|
= redundant_givens -- still be redundant e.g. (Eq a, Ord a)
|
|
437
|
395
|
|
| ... |
... |
@@ -441,11 +399,13 @@ findUnnecessaryGivens info need_inner givens |
|
441
|
399
|
|
|
442
|
400
|
unused_givens = filterOut is_used givens
|
|
443
|
401
|
|
|
|
402
|
+ needed_givens_ignoring_default_methods = ens_fvs need
|
|
444
|
403
|
is_used given = is_type_error given
|
|
445
|
|
- || given `elemVarSet` need_inner
|
|
|
404
|
+ || given `elemVarSet` needed_givens_ignoring_default_methods
|
|
446
|
405
|
|| (in_instance_decl && is_improving (idType given))
|
|
447
|
406
|
|
|
448
|
|
- minimal_givens = mkMinimalBySCs evVarPred givens
|
|
|
407
|
+ minimal_givens = mkMinimalBySCs evVarPred givens -- See (TRC2)
|
|
|
408
|
+
|
|
449
|
409
|
is_minimal = (`elemVarSet` mkVarSet minimal_givens)
|
|
450
|
410
|
redundant_givens
|
|
451
|
411
|
| in_instance_decl = []
|
| ... |
... |
@@ -457,6 +417,26 @@ findUnnecessaryGivens info need_inner givens |
|
457
|
417
|
is_improving pred -- (transSuperClasses p) does not include p
|
|
458
|
418
|
= any isImprovementPred (pred : transSuperClasses pred)
|
|
459
|
419
|
|
|
|
420
|
+warnRedundantGivens :: SkolemInfoAnon -> Bool
|
|
|
421
|
+warnRedundantGivens (SigSkol ctxt _ _)
|
|
|
422
|
+ = case ctxt of
|
|
|
423
|
+ FunSigCtxt _ rrc -> reportRedundantConstraints rrc
|
|
|
424
|
+ ExprSigCtxt rrc -> reportRedundantConstraints rrc
|
|
|
425
|
+ _ -> False
|
|
|
426
|
+
|
|
|
427
|
+warnRedundantGivens (InstSkol from _)
|
|
|
428
|
+ -- Do not report redundant constraints for quantified constraints
|
|
|
429
|
+ -- See (TRC4) in Note [Tracking redundant constraints]
|
|
|
430
|
+ -- Fortunately it is easy to spot implications constraints that arise
|
|
|
431
|
+ -- from quantified constraints, from their SkolInfo
|
|
|
432
|
+ = case from of
|
|
|
433
|
+ IsQC {} -> False
|
|
|
434
|
+ IsClsInst {} -> True
|
|
|
435
|
+
|
|
|
436
|
+ -- To think about: do we want to report redundant givens for
|
|
|
437
|
+ -- pattern synonyms, PatSynSigSkol? c.f #9953, comment:21.
|
|
|
438
|
+warnRedundantGivens _ = False
|
|
|
439
|
+
|
|
460
|
440
|
{- Note [Redundant constraints in instance decls]
|
|
461
|
441
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
462
|
442
|
Instance declarations are special in two ways:
|
| ... |
... |
@@ -508,21 +488,11 @@ checkBadTelescope (Implic { ic_info = info |
|
508
|
488
|
| otherwise
|
|
509
|
489
|
= go (later_skols `extendVarSet` one_skol) earlier_skols
|
|
510
|
490
|
|
|
511
|
|
-warnRedundantGivens :: SkolemInfoAnon -> Bool
|
|
512
|
|
-warnRedundantGivens (SigSkol ctxt _ _)
|
|
513
|
|
- = case ctxt of
|
|
514
|
|
- FunSigCtxt _ rrc -> reportRedundantConstraints rrc
|
|
515
|
|
- ExprSigCtxt rrc -> reportRedundantConstraints rrc
|
|
516
|
|
- _ -> False
|
|
517
|
|
-
|
|
518
|
|
- -- To think about: do we want to report redundant givens for
|
|
519
|
|
- -- pattern synonyms, PatSynSigSkol? c.f #9953, comment:21.
|
|
520
|
|
-warnRedundantGivens (InstSkol {}) = True
|
|
521
|
|
-warnRedundantGivens _ = False
|
|
522
|
|
-
|
|
523
|
|
-neededEvVars :: Implication -> TcS ([EvVar], Implication)
|
|
|
491
|
+neededEvVars :: Implication -> TcS Implication
|
|
524
|
492
|
-- Find all the evidence variables that are "needed",
|
|
|
493
|
+-- /and/ delete dead evidence bindings
|
|
|
494
|
+-- /and/ delete unnecessary child implications
|
|
|
495
|
+--
|
|
525
|
496
|
-- See Note [Tracking redundant constraints]
|
|
526
|
497
|
-- See Note [Delete dead Given evidence bindings]
|
|
527
|
498
|
--
|
| ... |
... |
@@ -539,78 +509,89 @@ neededEvVars :: Implication -> TcS ([EvVar], Implication) |
|
539
|
509
|
--
|
|
540
|
510
|
-- - Prune out all Given bindings that are not needed
|
|
541
|
511
|
--
|
|
542
|
|
--- - From the 'needed' set, delete ev_bndrs, the binders of the
|
|
543
|
|
--- evidence bindings, to give the final needed variables
|
|
544
|
|
---
|
|
545
|
|
-neededEvVars implic@(Implic { ic_given = givens
|
|
546
|
|
- , ic_info = info
|
|
547
|
|
- , ic_binds = ev_binds_var
|
|
548
|
|
- , ic_wanted = WC { wc_impl = implics }
|
|
549
|
|
- , ic_need_pruned = need_pruned })
|
|
|
512
|
+-- - Prune out all child implications that are no longer useful
|
|
|
513
|
+
|
|
|
514
|
+neededEvVars implic@(Implic { ic_info = info
|
|
|
515
|
+ , ic_binds = ev_binds_var
|
|
|
516
|
+ , ic_wanted = old_wanted
|
|
|
517
|
+ , ic_need_implic = old_need_implic -- See (TRC1)
|
|
|
518
|
+ })
|
|
|
519
|
+ | WC { wc_impl = old_implics } <- old_wanted
|
|
550
|
520
|
= do { ev_binds <- TcS.getTcEvBindsMap ev_binds_var
|
|
551
|
521
|
; tcvs <- TcS.getTcEvTyCoVars ev_binds_var
|
|
552
|
522
|
|
|
553
|
|
- ; let -- Get the variables needed by the implications
|
|
554
|
|
- ENS { ens_dms = implic_dm_seeds, ens_fvs = implic_other_seeds }
|
|
555
|
|
- = foldr add_implic_seeds need_pruned implics
|
|
|
523
|
+ ; let -- Augment `need_implic` (see (TRC1)) with the variables needed by the implications
|
|
|
524
|
+ new_need_implic@(ENS { ens_dms = dm_seeds, ens_fvs = other_seeds })
|
|
|
525
|
+ = foldr add_implic old_need_implic old_implics
|
|
556
|
526
|
|
|
557
|
527
|
-- Get the variables needed by the solved bindings
|
|
|
528
|
+ -- (It's OK to use a non-deterministic fold here
|
|
|
529
|
+ -- because add_wanted is commutative.)
|
|
558
|
530
|
seeds_w = nonDetStrictFoldEvBindMap add_wanted tcvs ev_binds
|
|
559
|
|
- -- `seeds_w` are the vars mentioned by all the solved Wanted bindings
|
|
560
|
|
- -- (It's OK to use a non-deterministic fold here
|
|
561
|
|
- -- because add_wanted is commutative.)
|
|
562
|
531
|
|
|
563
|
|
- need_ignoring_dms = findNeededGivenEvVars ev_binds (implic_other_seeds `unionVarSet` seeds_w)
|
|
564
|
|
- need_from_dms = findNeededGivenEvVars ev_binds implic_dm_seeds
|
|
|
532
|
+ need_ignoring_dms = findNeededGivenEvVars ev_binds (other_seeds `unionVarSet` seeds_w)
|
|
|
533
|
+ need_from_dms = findNeededGivenEvVars ev_binds dm_seeds
|
|
565
|
534
|
need_full = need_ignoring_dms `unionVarSet` need_from_dms
|
|
566
|
|
- live_ev_binds = filterEvBindMap (needed_ev_bind need_full) ev_binds
|
|
567
|
535
|
|
|
|
536
|
+ -- `new_need`: the Givens from outer scopes that are used in this implication
|
|
|
537
|
+ need | is_dm_skol info = ENS { ens_dms = trim ev_binds need_full
|
|
|
538
|
+ , ens_fvs = emptyVarSet }
|
|
|
539
|
+ | otherwise = ENS { ens_dms = trim ev_binds need_from_dms
|
|
|
540
|
+ , ens_fvs = trim ev_binds need_ignoring_dms }
|
|
|
541
|
+
|
|
|
542
|
+ -- `new_implics`: we have now taken account of the `ic_need` variables
|
|
|
543
|
+ -- of `old_implics`, so we can drop any that are no longer necessary
|
|
|
544
|
+ pruned_implics = filterBag keep_me old_implics
|
|
|
545
|
+ pruned_wanted = old_wanted { wc_impl = pruned_implics }
|
|
|
546
|
+ -- Do not prune holes; these should be reported
|
|
|
547
|
+
|
|
|
548
|
+ -- Delete dead Given evidence bindings
|
|
|
549
|
+ -- See Note [Delete dead Given evidence bindings]
|
|
|
550
|
+ ; let live_ev_binds = filterEvBindMap (needed_ev_bind need_full) ev_binds
|
|
568
|
551
|
; TcS.setTcEvBindsMap ev_binds_var live_ev_binds
|
|
569
|
|
- -- See Note [Delete dead Given evidence bindings]
|
|
570
|
|
-
|
|
571
|
|
- ; let -- `dead_givens` are the Givens from this implication that are unused
|
|
572
|
|
- dead_givens = findUnnecessaryGivens info need_ignoring_dms givens
|
|
573
|
552
|
|
|
574
|
|
- -- `need_outer` are the Givens from outer scopes that are used in this implication
|
|
575
|
|
- need_outer
|
|
576
|
|
- | is_dm_skol info = ENS { ens_dms = trim live_ev_binds need_full
|
|
577
|
|
- , ens_fvs = emptyVarSet }
|
|
578
|
|
- | otherwise = ENS { ens_dms = trim live_ev_binds need_from_dms
|
|
579
|
|
- , ens_fvs = trim live_ev_binds need_ignoring_dms }
|
|
580
|
553
|
; traceTcS "neededEvVars" $
|
|
581
|
|
- vcat [ text "old need_pruned:" <+> ppr need_pruned
|
|
|
554
|
+ vcat [ text "old_need_implic:" <+> ppr old_need_implic
|
|
|
555
|
+ , text "new_need_implic:" <+> ppr new_need_implic
|
|
582
|
556
|
, text "tcvs:" <+> ppr tcvs
|
|
583
|
557
|
, text "need_ignoring_dms:" <+> ppr need_ignoring_dms
|
|
584
|
558
|
, text "need_from_dms:" <+> ppr need_from_dms
|
|
585
|
|
- , text "need_outer:" <+> ppr need_outer
|
|
586
|
|
- , text "dead_givens:" <+> ppr dead_givens
|
|
|
559
|
+ , text "need:" <+> ppr need
|
|
587
|
560
|
, text "ev_binds:" <+> ppr ev_binds
|
|
588
|
561
|
, text "live_ev_binds:" <+> ppr live_ev_binds ]
|
|
589
|
|
-
|
|
590
|
|
- ; return ( dead_givens
|
|
591
|
|
- , implic { ic_need_outer = need_outer }) }
|
|
|
562
|
+ ; return (implic { ic_need = need
|
|
|
563
|
+ , ic_need_implic = new_need_implic
|
|
|
564
|
+ , ic_wanted = pruned_wanted }) }
|
|
592
|
565
|
where
|
|
593
|
|
- trim :: EvBindMap -> VarSet -> VarSet
|
|
594
|
|
- -- Delete variables bound by Givens or bindings
|
|
595
|
|
- trim live_ev_binds needs = (needs `varSetMinusEvBindMap` live_ev_binds)
|
|
596
|
|
- `delVarSetList` givens
|
|
|
566
|
+ trim :: EvBindMap -> VarSet -> VarSet
|
|
|
567
|
+ -- Delete variables bound by Givens or bindings
|
|
|
568
|
+ trim ev_binds needs = needs `varSetMinusEvBindMap` ev_binds
|
|
597
|
569
|
|
|
598
|
|
- add_implic_seeds :: Implication -> EvNeedSet -> EvNeedSet
|
|
599
|
|
- add_implic_seeds (Implic { ic_need_outer = needs }) acc
|
|
600
|
|
- = needs `unionEvNeedSet` acc
|
|
|
570
|
+ add_implic :: Implication -> EvNeedSet -> EvNeedSet
|
|
|
571
|
+ add_implic (Implic { ic_given = givens, ic_need = need }) acc
|
|
|
572
|
+ = (need `delGivensFromEvNeedSet` givens) `unionEvNeedSet` acc
|
|
601
|
573
|
|
|
602
|
|
- needed_ev_bind needed (EvBind { eb_lhs = ev_var, eb_info = info })
|
|
603
|
|
- | EvBindGiven{} <- info = ev_var `elemVarSet` needed
|
|
604
|
|
- | otherwise = True -- Keep all wanted bindings
|
|
|
574
|
+ needed_ev_bind needed (EvBind { eb_lhs = ev_var, eb_info = info })
|
|
|
575
|
+ | EvBindGiven{} <- info = ev_var `elemVarSet` needed
|
|
|
576
|
+ | otherwise = True -- Keep all wanted bindings
|
|
605
|
577
|
|
|
606
|
|
- add_wanted :: EvBind -> VarSet -> VarSet
|
|
607
|
|
- add_wanted (EvBind { eb_info = info, eb_rhs = rhs }) needs
|
|
608
|
|
- | EvBindGiven{} <- info = needs -- Add the rhs vars of the Wanted bindings only
|
|
609
|
|
- | otherwise = evVarsOfTerm rhs `unionVarSet` needs
|
|
|
578
|
+ add_wanted :: EvBind -> VarSet -> VarSet
|
|
|
579
|
+ add_wanted (EvBind { eb_info = info, eb_rhs = rhs }) needs
|
|
|
580
|
+ | EvBindGiven{} <- info = needs -- Add the rhs vars of the Wanted bindings only
|
|
|
581
|
+ | otherwise = evVarsOfTerm rhs `unionVarSet` needs
|
|
610
|
582
|
|
|
611
|
|
-is_dm_skol :: SkolemInfoAnon -> Bool
|
|
612
|
|
-is_dm_skol (MethSkol _ is_dm) = is_dm
|
|
613
|
|
-is_dm_skol _ = False
|
|
|
583
|
+ keep_me :: Implication -> Bool
|
|
|
584
|
+ keep_me (Implic { ic_status = status, ic_wanted = wanted })
|
|
|
585
|
+ | IC_Solved { ics_dead = dead_givens } <- status -- Fully solved
|
|
|
586
|
+ , null dead_givens -- No redundant givens to report
|
|
|
587
|
+ , isEmptyBag (wc_impl wanted) -- No children that might have things to report
|
|
|
588
|
+ = False
|
|
|
589
|
+ | otherwise
|
|
|
590
|
+ = True -- Otherwise, keep it
|
|
|
591
|
+
|
|
|
592
|
+ is_dm_skol :: SkolemInfoAnon -> Bool
|
|
|
593
|
+ is_dm_skol (MethSkol _ is_dm) = is_dm
|
|
|
594
|
+ is_dm_skol _ = False
|
|
614
|
595
|
|
|
615
|
596
|
findNeededGivenEvVars :: EvBindMap -> VarSet -> VarSet
|
|
616
|
597
|
-- Find all the Given evidence needed by seeds,
|
| ... |
... |
@@ -752,133 +733,82 @@ in GHC.Tc.Gen.HsType. |
|
752
|
733
|
|
|
753
|
734
|
Note [Tracking redundant constraints]
|
|
754
|
735
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
755
|
|
-With Opt_WarnRedundantConstraints, GHC can report which
|
|
756
|
|
-constraints of a type signature (or instance declaration) are
|
|
757
|
|
-redundant, and can be omitted. Here is an overview of how it
|
|
758
|
|
-works.
|
|
759
|
|
-
|
|
760
|
|
-This is all tested in typecheck/should_compile/T20602 (among
|
|
761
|
|
-others).
|
|
762
|
|
-
|
|
763
|
|
------ What is a redundant constraint?
|
|
764
|
|
-
|
|
765
|
|
-* The things that can be redundant are precisely the Given
|
|
766
|
|
- constraints of an implication.
|
|
|
736
|
+With Opt_WarnRedundantConstraints, GHC can report which constraints of a type
|
|
|
737
|
+signature (or instance declaration) are redundant, and can be omitted. Here is
|
|
|
738
|
+an overview of how it works.
|
|
767
|
739
|
|
|
768
|
|
-* A constraint can be redundant in two different ways:
|
|
769
|
|
- a) It is not needed by the Wanted constraints covered by the
|
|
770
|
|
- implication E.g.
|
|
771
|
|
- f :: Eq a => a -> Bool
|
|
772
|
|
- f x = True -- Equality not used
|
|
773
|
|
- b) It is implied by other givens. E.g.
|
|
774
|
|
- f :: (Eq a, Ord a) => blah -- Eq a unnecessary
|
|
775
|
|
- g :: (Eq a, a~b, Eq b) => blah -- Either Eq a or Eq b unnecessary
|
|
776
|
|
-
|
|
777
|
|
-* To find (a) we need to know which evidence bindings are 'wanted';
|
|
778
|
|
- hence the eb_is_given field on an EvBind.
|
|
779
|
|
-
|
|
780
|
|
-* To find (b), we use mkMinimalBySCs on the Givens to see if any
|
|
781
|
|
- are unnecessary.
|
|
|
740
|
+This is all tested in typecheck/should_compile/T20602 (among others).
|
|
782
|
741
|
|
|
783
|
742
|
----- How tracking works
|
|
784
|
743
|
|
|
785
|
|
-(RC1) When two Givens are the same, we drop the evidence for the one
|
|
786
|
|
- that requires more superclass selectors. This is done
|
|
787
|
|
- according to 2(c) of Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
|
|
788
|
|
-
|
|
789
|
|
-(RC2) The ic_need fields of an Implic records in-scope (given) evidence
|
|
790
|
|
- variables bound by the context, that were needed to solve this
|
|
791
|
|
- implication (so far). See the declaration of Implication.
|
|
|
744
|
+* We maintain the `ic_need` field in an implication:
|
|
|
745
|
+ ic_need: the set of Given evidence variables that are needed somewhere
|
|
|
746
|
+ in this implication; and are bound either by this implication
|
|
|
747
|
+ or by an enclosing one.
|
|
792
|
748
|
|
|
793
|
|
-(RC3) setImplicationStatus:
|
|
|
749
|
+* `setImplicationStatus` does all the work:
|
|
794
|
750
|
When the constraint solver finishes solving all the wanteds in
|
|
795
|
751
|
an implication, it sets its status to IC_Solved
|
|
796
|
752
|
|
|
797
|
|
- - The ics_dead field, of IC_Solved, records the subset of this
|
|
798
|
|
- implication's ic_given that are redundant (not needed).
|
|
799
|
|
-
|
|
800
|
|
- - We compute which evidence variables are needed by an implication
|
|
801
|
|
- in setImplicationStatus. A variable is needed if
|
|
802
|
|
- a) it is free in the RHS of a Wanted EvBind,
|
|
803
|
|
- b) it is free in the RHS of an EvBind whose LHS is needed, or
|
|
804
|
|
- c) it is in the ics_need of a nested implication.
|
|
805
|
|
-
|
|
806
|
|
- - After computing which variables are needed, we then look at the
|
|
807
|
|
- remaining variables for internal redundancies. This is case (b)
|
|
808
|
|
- from above. This is also done in setImplicationStatus.
|
|
809
|
|
- Note that we only look for case (b) if case (a) shows up empty,
|
|
810
|
|
- as exemplified below.
|
|
811
|
|
-
|
|
812
|
|
- - We need to be careful not to discard an implication
|
|
813
|
|
- prematurely, even one that is fully solved, because we might
|
|
814
|
|
- thereby forget which variables it needs, and hence wrongly
|
|
815
|
|
- report a constraint as redundant. But we can discard it once
|
|
816
|
|
- its free vars have been incorporated into its parent; or if it
|
|
817
|
|
- simply has no free vars. This careful discarding is also
|
|
818
|
|
- handled in setImplicationStatus.
|
|
819
|
|
-
|
|
820
|
|
-(RC4) We do not want to report redundant constraints for implications
|
|
821
|
|
- that come from quantified constraints. Example #23323:
|
|
822
|
|
- data T a
|
|
823
|
|
- instance Show (T a) where ... -- No context!
|
|
824
|
|
- foo :: forall f c. (forall a. c a => Show (f a)) => Proxy c -> f Int -> Int
|
|
825
|
|
- bar = foo @T @Eq
|
|
826
|
|
-
|
|
827
|
|
- The call to `foo` gives us
|
|
828
|
|
- [W] d : (forall a. Eq a => Show (T a))
|
|
829
|
|
- To solve this, GHC.Tc.Solver.Solve.solveForAll makes an implication constraint:
|
|
830
|
|
- forall a. Eq a => [W] ds : Show (T a)
|
|
831
|
|
- and because of the degnerate instance for `Show (T a)`, we don't need the `Eq a`
|
|
832
|
|
- constraint. But we don't want to report it as redundant!
|
|
833
|
|
-
|
|
834
|
|
-(RC5) Consider this (#25992), where `op2` has a default method
|
|
835
|
|
- class C a where { op1, op2 :: a -> a
|
|
836
|
|
- ; op2 = op1 . op1 }
|
|
837
|
|
- instance C a => C [a] where
|
|
838
|
|
- op1 x = x
|
|
839
|
|
-
|
|
840
|
|
- Plainly the (C a) constraint is unused; but the expanded decl will
|
|
841
|
|
- look like
|
|
842
|
|
- $dmop2 :: C a => a -> a
|
|
843
|
|
- $dmop2 = op1 . op2
|
|
844
|
|
-
|
|
845
|
|
- instance C a = C [a] b
|
|
|
753
|
+ - `neededEvVars`: computes which evidence variables are needed by an
|
|
|
754
|
+ implication in `setImplicationStatus`. A variable is needed if
|
|
846
|
755
|
|
|
847
|
|
-*** INCOMPLETE TODO ***
|
|
|
756
|
+ a) It is in the ic_need field of this implication, computed in
|
|
|
757
|
+ a previous call to `setImplicationStatus`; see (TRC1)
|
|
848
|
758
|
|
|
|
759
|
+ b) It is in the ics_need of a nested implication; see `add_implic`
|
|
|
760
|
+ in `neededEvVars`
|
|
849
|
761
|
|
|
850
|
|
-* Examples:
|
|
851
|
|
-
|
|
852
|
|
- f, g, h :: (Eq a, Ord a) => a -> Bool
|
|
853
|
|
- f x = x == x
|
|
854
|
|
- g x = x > x
|
|
855
|
|
- h x = x == x && x > x
|
|
|
762
|
+ c) It is free in the RHS of any /Wanted/ EvBind; each such binding
|
|
|
763
|
+ solves a Wanted, so we want them all. See `add_wanted` in
|
|
|
764
|
+ `neededEvVars`
|
|
856
|
765
|
|
|
857
|
|
- All three will discover that they have two [G] Eq a constraints:
|
|
858
|
|
- one as given and one extracted from the Ord a constraint. They will
|
|
859
|
|
- both discard the latter, as noted above and in
|
|
860
|
|
- Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
|
|
|
766
|
+ d) It is free in the RHS of a /Given/ EvBind whose LHS is needed:
|
|
|
767
|
+ see `findNeededGivenEvVars` called from `neededEvVars`.
|
|
861
|
768
|
|
|
862
|
|
- The body of f uses the [G] Eq a, but not the [G] Ord a. It will
|
|
863
|
|
- report a redundant Ord a using the logic for case (a).
|
|
864
|
|
-
|
|
865
|
|
- The body of g uses the [G] Ord a, but not the [G] Eq a. It will
|
|
866
|
|
- report a redundant Eq a using the logic for case (a).
|
|
867
|
|
-
|
|
868
|
|
- The body of h uses both [G] Ord a and [G] Eq a. Case (a) will
|
|
869
|
|
- thus come up with nothing redundant. But then, the case (b)
|
|
870
|
|
- check will discover that Eq a is redundant and report this.
|
|
871
|
|
-
|
|
872
|
|
- If we did case (b) even when case (a) reports something, then
|
|
873
|
|
- we would report both constraints as redundant for f, which is
|
|
874
|
|
- terrible.
|
|
875
|
|
-
|
|
876
|
|
------ Reporting redundant constraints
|
|
|
769
|
+ - Next, if the final status is IC_Solved, `setImplicationStatus` uses
|
|
|
770
|
+ `findRedunantGivens` to decide which of this implicaion's Givens are redundant.
|
|
877
|
771
|
|
|
878
|
772
|
* GHC.Tc.Errors does the actual warning, in warnRedundantConstraints.
|
|
879
|
773
|
|
|
880
|
|
-* We don't report redundant givens for *every* implication; only
|
|
881
|
|
- for those which reply True to GHC.Tc.Solver.warnRedundantGivens:
|
|
|
774
|
+
|
|
|
775
|
+Wrinkles:
|
|
|
776
|
+
|
|
|
777
|
+(TRC1) `pruneImplications` drops any sub-implications of an Implication
|
|
|
778
|
+ that are irrelevant for error reporting:
|
|
|
779
|
+ - no unsolved wanteds
|
|
|
780
|
+ - no sub-implications
|
|
|
781
|
+ - no redundant givens to report
|
|
|
782
|
+ But in doing so we must not lose track of the variables that those implications
|
|
|
783
|
+ needed! So we do not recompute `ic_need` from scratch each time; rather, we
|
|
|
784
|
+ simply grow it -- see the use of `old_need` in `neededEvVars`.
|
|
|
785
|
+
|
|
|
786
|
+ Starting from `old_needs` also means that the transitive closure algorithm in
|
|
|
787
|
+ `findNeededGivenEvVars` will terminate faster
|
|
|
788
|
+
|
|
|
789
|
+(TRC2) A Given can be redundant because it is implied by other Givens
|
|
|
790
|
+ f :: (Eq a, Ord a) => blah -- Eq a unnecessary
|
|
|
791
|
+ g :: (Eq a, a~b, Eq b) => blah -- Either Eq a or Eq b unnecessary
|
|
|
792
|
+ We nail this by using `mkMinimalBySCs` in `findRedundantGivens`.
|
|
|
793
|
+ (TRC2a) But NOTE that we only attempt this mkMinimalBySCs stuff if all Givens
|
|
|
794
|
+ used by evidence bindings. Example:
|
|
|
795
|
+ f :: (Eq a, Ord a) => a -> Bool
|
|
|
796
|
+ f x = x == x
|
|
|
797
|
+ We report (Ord a) as unused because it is. But we must not also report (Eq a)
|
|
|
798
|
+ as unused because it is a superclass of Ord!
|
|
|
799
|
+
|
|
|
800
|
+(TRC3) When two Givens are the same, prefer one that does not involve superclass
|
|
|
801
|
+ selection, or more generally has shallower superclass-selection depth:
|
|
|
802
|
+ see 2(b,c) in Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
|
|
|
803
|
+ e.g f :: (Eq a, Ord a) => a -> Bool
|
|
|
804
|
+ f x = x == x
|
|
|
805
|
+ Eager superclass expansion gives us two [G] Eq a constraints. We want to keep
|
|
|
806
|
+ the one from the user-written Eq a, not the superclass selection. This means
|
|
|
807
|
+ we report the Ord a as redundant with -Wredundant-constraints, not the Eq a.
|
|
|
808
|
+ Getting this wrong was #20602.
|
|
|
809
|
+
|
|
|
810
|
+(TRC4) We don't compute redundant givens for *every* implication; only
|
|
|
811
|
+ for those which reply True to `warnRedundantGivens`:
|
|
882
|
812
|
|
|
883
|
813
|
- For example, in a class declaration, the default method *can*
|
|
884
|
814
|
use the class constraint, but it certainly doesn't *have* to,
|
| ... |
... |
@@ -897,9 +827,68 @@ others). |
|
897
|
827
|
- GHC.Tc.Gen.Bind.tcSpecPrag
|
|
898
|
828
|
- GHC.Tc.Gen.Bind.tcTySig
|
|
899
|
829
|
|
|
900
|
|
- This decision is taken in setImplicationStatus, rather than GHC.Tc.Errors
|
|
901
|
|
- so that we can discard implication constraints that we don't need.
|
|
902
|
|
- So ics_dead consists only of the *reportable* redundant givens.
|
|
|
830
|
+ - We do not want to report redundant constraints for implications
|
|
|
831
|
+ that come from quantified constraints. Example #23323:
|
|
|
832
|
+ data T a
|
|
|
833
|
+ instance Show (T a) where ... -- No context!
|
|
|
834
|
+ foo :: forall f c. (forall a. c a => Show (f a)) => Proxy c -> f Int -> Int
|
|
|
835
|
+ bar = foo @T @Eq
|
|
|
836
|
+
|
|
|
837
|
+ The call to `foo` gives us
|
|
|
838
|
+ [W] d : (forall a. Eq a => Show (T a))
|
|
|
839
|
+ To solve this, GHC.Tc.Solver.Solve.solveForAll makes an implication constraint:
|
|
|
840
|
+ forall a. Eq a => [W] ds : Show (T a)
|
|
|
841
|
+ and because of the degnerate instance for `Show (T a)`, we don't need the `Eq a`
|
|
|
842
|
+ constraint. But we don't want to report it as redundant!
|
|
|
843
|
+
|
|
|
844
|
+(TRC5) Consider this (#25992), where `op2` has a default method
|
|
|
845
|
+ class C a where { op1, op2 :: a -> a
|
|
|
846
|
+ ; op2 = op1 . op1 }
|
|
|
847
|
+ instance C a => C [a] where
|
|
|
848
|
+ op1 x = x
|
|
|
849
|
+
|
|
|
850
|
+ Plainly the (C a) constraint is unused; but the expanded decl will look like
|
|
|
851
|
+ $dmop2 :: C a => a -> a
|
|
|
852
|
+ $dmop2 = op1 . op2
|
|
|
853
|
+
|
|
|
854
|
+ $fCList :: forall a. C a => C [a]
|
|
|
855
|
+ $fCList @a (d::C a) = MkC (\(x:a).x) ($dmop2 @a d)
|
|
|
856
|
+
|
|
|
857
|
+ Notice that `d` gets passed to `$dmop`: it is "needed". But it's only
|
|
|
858
|
+ /really/ needed if some /other/ method (in this case `op1`) uses it.
|
|
|
859
|
+
|
|
|
860
|
+ So, rather than one set of "needed Givens" we use `EvNeedSet` to track a /pair/
|
|
|
861
|
+ of sets:
|
|
|
862
|
+ ens_dms: needed /only/ by default-method calls
|
|
|
863
|
+ ens_fvs: needed by something other than a default-method call
|
|
|
864
|
+ It's a bit of a palaver, but not really difficult.
|
|
|
865
|
+ All the works is in `neededEvVars`.
|
|
|
866
|
+
|
|
|
867
|
+
|
|
|
868
|
+
|
|
|
869
|
+----- Reporting redundant constraints
|
|
|
870
|
+
|
|
|
871
|
+
|
|
|
872
|
+----- Examples
|
|
|
873
|
+
|
|
|
874
|
+ f, g, h :: (Eq a, Ord a) => a -> Bool
|
|
|
875
|
+ f x = x == x
|
|
|
876
|
+ g x = x > x
|
|
|
877
|
+ h x = x == x && x > x
|
|
|
878
|
+
|
|
|
879
|
+ All of f,g,h will discover that they have two [G] Eq a constraints: one as
|
|
|
880
|
+ given and one extracted from the Ord a constraint. They will both discard
|
|
|
881
|
+ the latter; see (TRC3).
|
|
|
882
|
+
|
|
|
883
|
+ The body of f uses the [G] Eq a, but not the [G] Ord a. It will report a
|
|
|
884
|
+ redundant Ord a.
|
|
|
885
|
+
|
|
|
886
|
+ The body of g uses the [G] Ord a, but not the [G] Eq a. It will report a
|
|
|
887
|
+ redundant Eq a.
|
|
|
888
|
+
|
|
|
889
|
+ The body of h uses both [G] Ord a and [G] Eq a; each is used in a solved
|
|
|
890
|
+ Wanted evidence binding. But (TRC2) kicks in and discovers the Eq a
|
|
|
891
|
+ is redundant.
|
|
903
|
892
|
|
|
904
|
893
|
----- Shortcomings
|
|
905
|
894
|
|