Simon Peyton Jones pushed to branch wip/T23162-part2 at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • compiler/GHC/Core/TyCon.hs
    ... ... @@ -2448,8 +2448,9 @@ isOpenTypeFamilyTyCon (TyCon { tyConDetails = details })
    2448 2448
       | FamilyTyCon {famTcFlav = OpenSynFamilyTyCon } <- details = True
    
    2449 2449
       | otherwise                                                = False
    
    2450 2450
     
    
    2451
    --- | Is this a non-empty closed type family? Returns 'Nothing' for
    
    2452
    --- abstract or empty closed families.
    
    2451
    +-- | Is this a /non-empty/ closed type family?
    
    2452
    +--   Returns 'Nothing' for closed type family with no equations, as well
    
    2453
    +--      as for open families, data famlilies, abstract families
    
    2453 2454
     isClosedFamilyTyCon_maybe :: TyCon -> Maybe (CoAxiom Branched)
    
    2454 2455
     isClosedFamilyTyCon_maybe (TyCon { tyConDetails = details })
    
    2455 2456
       | FamilyTyCon {famTcFlav = ClosedSynFamilyTyCon mb} <- details = mb
    

  • compiler/GHC/Tc/Solver/FunDeps.hs
    ... ... @@ -25,6 +25,7 @@ import GHC.Core.FamInstEnv
    25 25
     import GHC.Core.Coercion
    
    26 26
     import GHC.Core.Predicate( EqRel(..) )
    
    27 27
     import GHC.Core.TyCon
    
    28
    +import GHC.Core.Type( tyConAppTyCon_maybe )
    
    28 29
     import GHC.Core.Unify( tcUnifyTysForInjectivity, typeListsAreApart )
    
    29 30
     import GHC.Core.Coercion.Axiom
    
    30 31
     import GHC.Core.TyCo.Subst( elemSubst )
    
    ... ... @@ -467,7 +468,8 @@ tryEqFunDeps work_item@(EqCt { eq_lhs = work_lhs
    467 468
                                  , eq_eq_rel = eq_rel })
    
    468 469
       | NomEq <- eq_rel
    
    469 470
       , TyFamLHS fam_tc work_args <- work_lhs     -- We have F args ~N# rhs
    
    470
    -  = do { eqs_for_me <- simpleStage$ getInertFamEqsFor fam_tc work_args work_rhs
    
    471
    +  = do { simpleStage $ traceTcS "tryEqFunDeps" (ppr work_item)
    
    472
    +       ; eqs_for_me <- simpleStage $ getInertFamEqsFor fam_tc work_args work_rhs
    
    471 473
            ; tryFamEqFunDeps eqs_for_me fam_tc work_args work_item }
    
    472 474
       | otherwise
    
    473 475
       = nopStage ()
    
    ... ... @@ -482,7 +484,7 @@ tryFamEqFunDeps eqs_for_me fam_tc work_args
    482 484
         else do { -- Note [Do local fundeps before top-level instances]
    
    483 485
                   tryFDEqns fam_tc work_args work_item $
    
    484 486
                   mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs
    
    485
    -            ; if null eqs_for_me
    
    487
    +            ; if all (isWanted . eqCtEvidence) eqs_for_me
    
    486 488
                   then tryFDEqns fam_tc work_args work_item $
    
    487 489
                        mkTopBuiltinFamEqFDs fam_tc ops work_args work_rhs
    
    488 490
                   else nopStage () }
    
    ... ... @@ -492,38 +494,37 @@ tryFamEqFunDeps eqs_for_me fam_tc work_args
    492 494
     
    
    493 495
       -- Only Wanted constraints below here
    
    494 496
     
    
    495
    -  | isOpenTypeFamilyTyCon fam_tc
    
    496
    -  , Injective inj_flags <- tyConInjectivityInfo fam_tc
    
    497
    -  = -- Open, injective type families
    
    498
    -    do { -- Note [Do local fundeps before top-level instances]
    
    499
    -         tryFDEqns fam_tc work_args work_item $
    
    500
    -         mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs
    
    501
    -
    
    502
    -       ; if null eqs_for_me
    
    503
    -         then tryFDEqns fam_tc work_args work_item $
    
    504
    -              mkTopOpenFamEqFDs fam_tc inj_flags work_args work_rhs
    
    505
    -         else nopStage () }
    
    506
    -
    
    507
    -  | Just ax <- isClosedFamilyTyCon_maybe fam_tc
    
    508
    -  = -- Closed type families
    
    509
    -    do { -- Note [Do local fundeps before top-level instances]
    
    510
    -         simpleStage $ traceTcS "fundep closed" (ppr fam_tc)
    
    511
    -
    
    512
    -       ; case tyConInjectivityInfo fam_tc of
    
    497
    +  | otherwise   -- Wanted, user-defined type families
    
    498
    +  = do { -- Note [Do local fundeps before top-level instances]
    
    499
    +         case tyConInjectivityInfo fam_tc of
    
    513 500
                NotInjective  -> nopStage()
    
    514 501
                Injective inj -> tryFDEqns fam_tc work_args work_item $
    
    515 502
                                 mkLocalFamEqFDs eqs_for_me fam_tc inj work_args work_rhs
    
    516 503
     
    
    517
    -       -- Now look at the top-level axioms; we effectively infer injectivity,
    
    518
    -       -- so we don't need tyConInjectivtyInfo.  This works fine for closed
    
    519
    -       -- type families without injectivity info
    
    520
    -       ; if null eqs_for_me
    
    504
    +       ; if all (isWanted . eqCtEvidence) eqs_for_me
    
    521 505
              then tryFDEqns fam_tc work_args work_item $
    
    522
    -              mkTopClosedFamEqFDs ax work_args work_rhs
    
    506
    +              mkTopFamEqFDs fam_tc work_args work_rhs
    
    523 507
              else nopStage () }
    
    524 508
     
    
    525
    -  | otherwise -- Data families, abstract families
    
    526
    -  = nopStage ()
    
    509
    +mkTopFamEqFDs :: TyCon -> [TcType] -> Xi -> TcS [FunDepEqns]
    
    510
    +mkTopFamEqFDs fam_tc work_args work_rhs
    
    511
    +  | isOpenTypeFamilyTyCon fam_tc
    
    512
    +  , Injective inj_flags <- tyConInjectivityInfo fam_tc
    
    513
    +  = -- Open, injective type families
    
    514
    +    mkTopOpenFamEqFDs fam_tc inj_flags work_args work_rhs
    
    515
    +
    
    516
    +  | Just ax <- isClosedFamilyTyCon_maybe fam_tc
    
    517
    +  = -- Closed type families
    
    518
    +    -- Look at the top-level axioms; we effectively infer injectivity,
    
    519
    +    -- so we don't need tyConInjectivtyInfo.  This works fine for closed
    
    520
    +    -- type families without injectivity info
    
    521
    +    mkTopClosedFamEqFDs ax work_args work_rhs
    
    522
    +
    
    523
    +  | otherwise
    
    524
    +  = -- Data families, abstract families,
    
    525
    +    -- open families that are not injective,
    
    526
    +    -- closed type families with no equations (isClosedFamilyTyCon_maybe returns Nothing)
    
    527
    +    return []
    
    527 528
     
    
    528 529
     tryFDEqns :: TyCon -> [TcType] -> EqCt -> TcS [FunDepEqns] -> SolverStage ()
    
    529 530
     tryFDEqns fam_tc work_args work_item@(EqCt { eq_ev = ev, eq_rhs= rhs }) mk_fd_eqns
    
    ... ... @@ -542,6 +543,8 @@ tryFDEqns fam_tc work_args work_item@(EqCt { eq_ev = ev, eq_rhs= rhs }) mk_fd_eq
    542 543
     -----------------------------------------
    
    543 544
     mkTopClosedFamEqFDs :: CoAxiom Branched -> [TcType] -> Xi -> TcS [FunDepEqns]
    
    544 545
     mkTopClosedFamEqFDs ax work_args work_rhs
    
    546
    +  | Just tc <- tyConAppTyCon_maybe work_rhs   -- Does RHS have anything useful to say?
    
    547
    +  , isGenerativeTyCon tc Nominal
    
    545 548
       = do { let branches = fromBranches (coAxiomBranches ax)
    
    546 549
            ; traceTcS "mkTopClosed" (ppr branches $$ ppr work_args $$ ppr work_rhs)
    
    547 550
            ; case getRelevantBranches ax work_args work_rhs of
    
    ... ... @@ -549,6 +552,8 @@ mkTopClosedFamEqFDs ax work_args work_rhs
    549 552
                   -> return [FDEqns { fd_qtvs = qtvs
    
    550 553
                                     , fd_eqs = zipWith Pair (rhs_ty:lhs_tys) (work_rhs:work_args) }]
    
    551 554
                _  -> return [] }
    
    555
    +   | otherwise
    
    556
    +   = return []
    
    552 557
     
    
    553 558
     
    
    554 559
     getRelevantBranches :: CoAxiom Branched -> [TcType] -> Xi -> [CoAxBranch]
    
    ... ... @@ -727,12 +732,14 @@ mkInjectivityFDEqn inj_args qtvs lhs_args rhs_args
    727 732
               | (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ]
    
    728 733
     
    
    729 734
     getInertFamEqsFor :: TyCon -> [TcType] -> Xi -> TcS [EqCt]
    
    730
    --- Returns a mixture of Given and Wanted
    
    731 735
     -- Look in the InertSet, and return all inert equalities
    
    732 736
     --    F tys ~N# rhs
    
    733 737
     --    where F is the specified TyCon
    
    734
    --- But filter out ones that can't possibly help, is apart from the Wanted
    
    735
    --- Representational equalities don't interact with type family dependencies
    
    738
    +-- But filter out ones that can't possibly help;
    
    739
    +--    that is, ones that are "apart" from the Wanted
    
    740
    +-- Returns a mixture of Given and Wanted
    
    741
    +-- Nominal only, becaues Representational equalities don't interact
    
    742
    +--    with type family dependencies
    
    736 743
     getInertFamEqsFor fam_tc work_args work_rhs
    
    737 744
       = do { IC {inert_funeqs = funeqs } <- getInertCans
    
    738 745
            ; return [ funeq_ct | equal_ct_list <- findFunEqsByTyCon funeqs fam_tc