| ... |
... |
@@ -40,9 +40,10 @@ import GHC.Types.Var.Env( mkInScopeSet ) |
|
40
|
40
|
import GHC.Utils.Outputable
|
|
41
|
41
|
import GHC.Utils.Panic
|
|
42
|
42
|
|
|
43
|
|
-import Control.Monad( unless )
|
|
44
|
43
|
import GHC.Data.Pair
|
|
45
|
|
-import Data.Maybe( isJust, mapMaybe )
|
|
|
44
|
+import Data.List.NonEmpty( NonEmpty(..), nonEmpty )
|
|
|
45
|
+import qualified Data.List.NonEmpty as NE
|
|
|
46
|
+import Data.Maybe( isJust, mapMaybe, maybeToList )
|
|
46
|
47
|
|
|
47
|
48
|
|
|
48
|
49
|
{- Note [Overview of functional dependencies in type inference]
|
| ... |
... |
@@ -584,10 +585,8 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args |
|
584
|
585
|
eqns <- mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs
|
|
585
|
586
|
; tryFDEqns False fam_tc work_args work_item eqns
|
|
586
|
587
|
|
|
587
|
|
- ; unless (hasRelevantGiven eqs_for_me work_args work_item) $
|
|
588
|
|
- -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families]
|
|
589
|
|
- do { eqns <- mkTopBuiltinFamEqFDs fam_tc ops work_args work_rhs
|
|
590
|
|
- ; tryFDEqns True fam_tc work_args work_item eqns } }
|
|
|
588
|
+ ; eqns <- mkTopBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_item
|
|
|
589
|
+ ; tryFDEqns True fam_tc work_args work_item eqns }
|
|
591
|
590
|
|
|
592
|
591
|
| isGiven ev -- See (INJFAM:Given)
|
|
593
|
592
|
, not (tcsmResumable mode) -- In the pattern-match checker, continue even for
|
| ... |
... |
@@ -598,20 +597,16 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args |
|
598
|
597
|
|
|
599
|
598
|
| otherwise -- Wanted, user-defined type families
|
|
600
|
599
|
= do { -- Note [Do local fundeps before top-level instances]
|
|
601
|
|
- case tyConInjectivityInfo fam_tc of
|
|
602
|
|
- NotInjective -> nopStage ()
|
|
603
|
|
- Injective inj -> do { eqns <- mkLocalFamEqFDs eqs_for_me fam_tc inj work_args work_rhs
|
|
604
|
|
- ; tryFDEqns False fam_tc work_args work_item eqns }
|
|
|
600
|
+ eqns <- mkLocalFamEqFDs eqs_for_me fam_tc work_args work_rhs
|
|
|
601
|
+ ; tryFDEqns False fam_tc work_args work_item eqns
|
|
605
|
602
|
-- NB: tryFDEqns bales out on an insoluble fundep
|
|
606
|
603
|
-- See (CF4) in Note [Exploiting closed type families]
|
|
607
|
604
|
|
|
608
|
|
- ; unless (hasRelevantGiven eqs_for_me work_args work_item) $
|
|
609
|
|
- -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families]
|
|
610
|
|
- do { eqns <- mkTopFamEqFDs fam_tc work_args work_item
|
|
611
|
|
- ; tryFDEqns True fam_tc work_args work_item eqns } }
|
|
|
605
|
+ ; eqns <- mkTopFamEqFDs eqs_for_me fam_tc work_args work_item
|
|
|
606
|
+ ; tryFDEqns True fam_tc work_args work_item eqns }
|
|
612
|
607
|
|
|
613
|
|
-mkTopFamEqFDs :: TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns]
|
|
614
|
|
-mkTopFamEqFDs fam_tc work_args work_item
|
|
|
608
|
+mkTopFamEqFDs :: [EqCt] -> TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns]
|
|
|
609
|
+mkTopFamEqFDs eqs_for_me fam_tc work_args work_item
|
|
615
|
610
|
-- NB: work_args exactly saturate fam_tc
|
|
616
|
611
|
| isOpenTypeFamilyTyCon fam_tc
|
|
617
|
612
|
, Injective inj_flags <- tyConInjectivityInfo fam_tc
|
| ... |
... |
@@ -621,7 +616,11 @@ mkTopFamEqFDs fam_tc work_args work_item |
|
621
|
616
|
-- Non built-in closed type families
|
|
622
|
617
|
-- (Built-in closed type families were dealt with in 'tryFamEqFunDeps'.)
|
|
623
|
618
|
| Just ax <- closedFamilyTyConCoAxiom_maybe fam_tc
|
|
624
|
|
- = mkTopClosedFamEqFDs ax work_args work_item
|
|
|
619
|
+ = if hasRelevantGiven eqs_for_me work_args work_item
|
|
|
620
|
+ -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families]
|
|
|
621
|
+ -- NB: (CF1) applies only to /closed/ type families (#28082)
|
|
|
622
|
+ then return []
|
|
|
623
|
+ else mkTopClosedFamEqFDs ax work_args work_item
|
|
625
|
624
|
|
|
626
|
625
|
| otherwise
|
|
627
|
626
|
= -- Anything else: data families, open type families that are not injective,
|
| ... |
... |
@@ -630,6 +629,8 @@ mkTopFamEqFDs fam_tc work_args work_item |
|
630
|
629
|
|
|
631
|
630
|
tryFDEqns :: Bool -> TyCon -> [TcType] -> EqCt -> [FunDepEqns] -> SolverStage ()
|
|
632
|
631
|
tryFDEqns is_top fam_tc work_args work_item@(EqCt { eq_ev = ev, eq_rhs= rhs }) fd_eqns
|
|
|
632
|
+ | null fd_eqns = return () -- nothing to do
|
|
|
633
|
+ | otherwise
|
|
633
|
634
|
= Stage $
|
|
634
|
635
|
do { traceTcS "tryFDEqns" (vcat [ text "lhs:" <+> ppr fam_tc <+> ppr work_args
|
|
635
|
636
|
, text "rhs:" <+> ppr rhs
|
| ... |
... |
@@ -699,7 +700,7 @@ getRelevantBranches :: CoAxiom Branched -> [TcType] -> Xi -> [FunDepEqns] |
|
699
|
700
|
getRelevantBranches ax work_args work_rhs
|
|
700
|
701
|
= go (fromBranches (coAxiomBranches ax))
|
|
701
|
702
|
where
|
|
702
|
|
- work_tys = work_rhs : work_args
|
|
|
703
|
+ work_tys = work_rhs :| work_args
|
|
703
|
704
|
|
|
704
|
705
|
go [] = []
|
|
705
|
706
|
go (branch:branches)
|
| ... |
... |
@@ -710,7 +711,7 @@ getRelevantBranches ax work_args work_rhs |
|
710
|
711
|
is_relevant branch@(CoAxBranch { cab_tvs = qtvs
|
|
711
|
712
|
, cab_lhs = lhs_tys
|
|
712
|
713
|
, cab_rhs = rhs_ty })
|
|
713
|
|
- | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) work_tys
|
|
|
714
|
+ | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) (NE.toList work_tys)
|
|
714
|
715
|
-- See (RW1)-(RW3) in Note [Relevance]
|
|
715
|
716
|
, let lhs_tys' = map (deepSubstTy subst) lhs_tys
|
|
716
|
717
|
-- deepSubstTy: see (CF8) in Note [Exploiting closed type families]
|
| ... |
... |
@@ -721,15 +722,15 @@ getRelevantBranches ax work_args work_rhs |
|
721
|
722
|
then -- `subst` eliminates all qtvs; and no qtvs in range of subst
|
|
722
|
723
|
-- See (CF7)(A) in Note [Exploiting closed type families]
|
|
723
|
724
|
FDEqns { fd_qtvs = []
|
|
724
|
|
- , fd_eqs = zipWith Pair (substTy subst rhs_ty : lhs_tys')
|
|
725
|
|
- work_tys }
|
|
|
725
|
+ , fd_eqs = NE.zipWith Pair (substTy subst rhs_ty :| lhs_tys')
|
|
|
726
|
+ work_tys }
|
|
726
|
727
|
-- Do not apply `subst` to `work_tys`;
|
|
727
|
728
|
-- See (CF9) in Note [Exploiting closed type families]
|
|
728
|
729
|
|
|
729
|
730
|
else -- Abandon `subst`; re-create it from (rhs_ty ~ work_rhs)
|
|
730
|
731
|
-- See (CF7)(B) in Note [Exploiting closed type families]
|
|
731
|
732
|
FDEqns { fd_qtvs = qtvs
|
|
732
|
|
- , fd_eqs = zipWith Pair (rhs_ty:lhs_tys) work_tys }
|
|
|
733
|
+ , fd_eqs = NE.zipWith Pair (rhs_ty :| lhs_tys) work_tys }
|
|
733
|
734
|
|
|
734
|
735
|
| otherwise
|
|
735
|
736
|
= Nothing
|
| ... |
... |
@@ -757,32 +758,36 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs }) |
|
757
|
758
|
branch_lhs_tys' = substTys subst' branch_lhs_tys
|
|
758
|
759
|
-- We must apply the substitution here see
|
|
759
|
760
|
-- the Basic Plan in Note [Exploiting open type families]
|
|
760
|
|
- = Just (mkInjectivityFDEqn qtvs' inj_flags branch_lhs_tys' work_args)
|
|
|
761
|
+ = mkInjectivityFDEqn qtvs' inj_flags branch_lhs_tys' work_args
|
|
761
|
762
|
|
|
762
|
763
|
| otherwise
|
|
763
|
764
|
= Nothing
|
|
764
|
765
|
|
|
765
|
|
-mkLocalFamEqFDs :: [EqCt] -> TyCon -> [Bool] -> [TcType] -> Xi -> SolverStage [FunDepEqns]
|
|
|
766
|
+mkLocalFamEqFDs :: [EqCt] -> TyCon -> [TcType] -> Xi -> SolverStage [FunDepEqns]
|
|
766
|
767
|
-- Wanted constraints only
|
|
767
|
768
|
-- Both open and closed type families, but only ones with declared injectivity
|
|
768
|
769
|
-- See Note [Exploiting local type-family injectivity]
|
|
769
|
|
-mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs
|
|
770
|
|
- = do { let -- eqns_from_inerts: see (INJFAM:Wanted/other)
|
|
771
|
|
- eqns_from_inerts = mapMaybe do_one eqs_for_me
|
|
772
|
|
- -- eqns_from_self: see (INJFAM:Wanted/Self)
|
|
773
|
|
- eqns_from_self = case saturatedFamApp_maybe work_rhs of
|
|
774
|
|
- Just (tc, rhs_tys) | fam_tc == tc -> [mk_eqn rhs_tys]
|
|
775
|
|
- _ -> []
|
|
|
770
|
+mkLocalFamEqFDs eqs_for_me fam_tc work_args work_rhs
|
|
|
771
|
+ | Injective inj_flags <- tyConInjectivityInfo fam_tc
|
|
|
772
|
+ = let -- eqns_from_inerts: see (INJFAM:Wanted/other)
|
|
|
773
|
+ eqns_from_inerts = mapMaybe do_one eqs_for_me
|
|
|
774
|
+ -- eqns_from_self: see (INJFAM:Wanted/Self)
|
|
|
775
|
+ eqns_from_self = case saturatedFamApp_maybe work_rhs of
|
|
|
776
|
+ Just (tc, rhs_tys) | fam_tc == tc -> maybeToList (mk_eqn rhs_tys)
|
|
|
777
|
+ _ -> []
|
|
776
|
778
|
|
|
777
|
|
- ; return (eqns_from_inerts ++ eqns_from_self) }
|
|
778
|
|
- where
|
|
779
|
|
- do_one :: EqCt -> Maybe FunDepEqns
|
|
780
|
|
- do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs })
|
|
781
|
|
- | work_rhs `tcEqType` inert_rhs = Just (mk_eqn inert_args)
|
|
782
|
|
- | otherwise = Nothing
|
|
783
|
|
- do_one _ = pprPanic "interactFunEq 2" (ppr fam_tc) -- TyVarLHS
|
|
|
779
|
+ do_one :: EqCt -> Maybe FunDepEqns
|
|
|
780
|
+ do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs })
|
|
|
781
|
+ | work_rhs `tcEqType` inert_rhs = mk_eqn inert_args
|
|
|
782
|
+ | otherwise = Nothing
|
|
|
783
|
+ do_one _ = pprPanic "interactFunEq 2" (ppr fam_tc) -- TyVarLHS
|
|
784
|
784
|
|
|
785
|
|
- mk_eqn iargs = mkInjectivityFDEqn [] inj_flags work_args iargs
|
|
|
785
|
+ mk_eqn iargs = mkInjectivityFDEqn [] inj_flags work_args iargs
|
|
|
786
|
+
|
|
|
787
|
+ in return (eqns_from_inerts ++ eqns_from_self)
|
|
|
788
|
+
|
|
|
789
|
+ | otherwise -- Not injective
|
|
|
790
|
+ = return []
|
|
786
|
791
|
|
|
787
|
792
|
|
|
788
|
793
|
trimQtvs :: Subst -> [TyVar] -> (Subst, [TyVar])
|
| ... |
... |
@@ -871,28 +876,34 @@ tryGivenBuiltinFamEqFDs eqs_for_me fam_tc ops work_args |
|
871
|
876
|
|
|
872
|
877
|
do_one _ = return ()
|
|
873
|
878
|
|
|
874
|
|
-mkTopBuiltinFamEqFDs :: TyCon -> BuiltinClosedTyFam -> [TcType] -> Xi -> SolverStage [FunDepEqns]
|
|
875
|
|
-mkTopBuiltinFamEqFDs fam_tc ops work_args work_rhs
|
|
876
|
|
- = return [FDEqns { fd_qtvs = []
|
|
877
|
|
- , fd_eqs = map snd $ tryInteractTopFam ops fam_tc work_args work_rhs }]
|
|
|
879
|
+mkTopBuiltinFamEqFDs :: [EqCt] -> TyCon -> BuiltinClosedTyFam -> [TcType] -> EqCt
|
|
|
880
|
+ -> SolverStage [FunDepEqns]
|
|
|
881
|
+mkTopBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_item@(EqCt { eq_rhs = work_rhs })
|
|
|
882
|
+ | hasRelevantGiven eqs_for_me work_args work_item
|
|
|
883
|
+ -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families]
|
|
|
884
|
+ -- A built-in family is very like a closed type family
|
|
|
885
|
+ = return []
|
|
|
886
|
+ | otherwise
|
|
|
887
|
+ = return [ FDEqns { fd_qtvs = [], fd_eqs = eqs }
|
|
|
888
|
+ | Just eqs <- [nonEmpty $ map snd $ tryInteractTopFam ops fam_tc work_args work_rhs] ]
|
|
878
|
889
|
|
|
879
|
890
|
mkLocalBuiltinFamEqFDs :: [EqCt] -> TyCon -> BuiltinClosedTyFam
|
|
880
|
891
|
-> [TcType] -> Xi -> SolverStage [FunDepEqns]
|
|
881
|
892
|
mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs
|
|
882
|
893
|
= do { let do_one :: EqCt -> [FunDepEqns]
|
|
883
|
894
|
do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs })
|
|
884
|
|
- | inert_rhs `tcEqType` work_rhs = [mk_eqn inert_args]
|
|
|
895
|
+ | inert_rhs `tcEqType` work_rhs = maybeToList (mk_eqn inert_args)
|
|
885
|
896
|
| otherwise = []
|
|
886
|
897
|
do_one _ = pprPanic "interactFunEq 1" (ppr fam_tc) -- TyVarLHS
|
|
887
|
898
|
|
|
888
|
|
- mk_eqn :: [TcType] -> FunDepEqns
|
|
889
|
|
- mk_eqn iargs = FDEqns { fd_qtvs = []
|
|
890
|
|
- , fd_eqs = map snd $ tryInteractInertFam ops fam_tc
|
|
891
|
|
- work_args iargs }
|
|
|
899
|
+ mk_eqn :: [TcType] -> Maybe FunDepEqns
|
|
|
900
|
+ mk_eqn iargs = do { eqs <- nonEmpty $ map snd $
|
|
|
901
|
+ tryInteractInertFam ops fam_tc work_args iargs
|
|
|
902
|
+ ; return (FDEqns { fd_qtvs = [], fd_eqs = eqs }) }
|
|
892
|
903
|
|
|
893
|
904
|
; let eqns_from_inerts = concatMap do_one eqs_for_me
|
|
894
|
905
|
eqns_from_self = case tcSplitTyConApp_maybe work_rhs of
|
|
895
|
|
- Just (tc,rhs_tys) | tc==fam_tc -> [mk_eqn rhs_tys]
|
|
|
906
|
+ Just (tc,rhs_tys) | tc==fam_tc -> maybeToList (mk_eqn rhs_tys)
|
|
896
|
907
|
_ -> []
|
|
897
|
908
|
; return (eqns_from_inerts ++ eqns_from_self) }
|
|
898
|
909
|
|
| ... |
... |
@@ -900,7 +911,7 @@ mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs |
|
900
|
911
|
mkInjectivityFDEqn :: [TcTyVar] -- Quantify these
|
|
901
|
912
|
-> [Bool] -- Injectivity flags
|
|
902
|
913
|
-> [TcType] -> [TcType] -- Make these equal
|
|
903
|
|
- -> FunDepEqns
|
|
|
914
|
+ -> Maybe FunDepEqns
|
|
904
|
915
|
-- When F s1 s2 s3 ~ F t1 t2 t3, and F has injectivity info [True,False,True]
|
|
905
|
916
|
-- return the FDEqns { fd_eqs = [Pair s1 t1, Pair s3 t3] }
|
|
906
|
917
|
-- The injectivity flags [Bool] will not all be False,
|
| ... |
... |
@@ -912,10 +923,11 @@ mkInjectivityFDEqn qtvs inj_args lhs_args rhs_args |
|
912
|
923
|
, text "lhs_args:" <+> ppr lhs_args
|
|
913
|
924
|
, text "rhs_args:" <+> ppr rhs_args ]) $
|
|
914
|
925
|
-- Need inj_args lhs_args and rhs_args to be same length for zip3
|
|
915
|
|
- FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }
|
|
|
926
|
+ do { eqs <- nonEmpty eqs_list
|
|
|
927
|
+ ; return (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) }
|
|
916
|
928
|
where
|
|
917
|
|
- eqs = [ Pair lhs_arg rhs_arg
|
|
918
|
|
- | (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ]
|
|
|
929
|
+ eqs_list = [ Pair lhs_arg rhs_arg
|
|
|
930
|
+ | (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ]
|
|
919
|
931
|
|
|
920
|
932
|
getInertFamEqsFor :: TyCon -> [TcType] -> Xi -> TcS [EqCt]
|
|
921
|
933
|
-- Look in the InertSet, and return all inert equalities
|
| ... |
... |
@@ -1603,8 +1615,8 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) |
|
1603
|
1615
|
; (_, subst) <- instFlexiXTcM empty_subst qtvs -- Takes account of kind substitution
|
|
1604
|
1616
|
; return (map (subst_pair subst) rev_eqs) }
|
|
1605
|
1617
|
where
|
|
1606
|
|
- rev_eqs = reverse eqs
|
|
1607
|
|
- -- (reverse eqs): See Note [Reverse order of fundep equations]
|
|
|
1618
|
+ rev_eqs = reverse $ NE.toList eqs
|
|
|
1619
|
+ -- reverse: See Note [Reverse order of fundep equations]
|
|
1608
|
1620
|
|
|
1609
|
1621
|
-- The kinds of `qtvs` can have free variables;
|
|
1610
|
1622
|
-- in the FDEqns produced by GHC.Tc.Instance.FunDeps.improveFromInstEnv
|