| ... |
... |
@@ -24,16 +24,16 @@ import GHC.Tc.Types.Constraint |
|
24
|
24
|
import GHC.Core.FamInstEnv
|
|
25
|
25
|
import GHC.Core.Coercion
|
|
26
|
26
|
import GHC.Core.TyCo.Rep( Type(..) )
|
|
27
|
|
-import GHC.Core.Predicate( EqRel(..) )
|
|
|
27
|
+import GHC.Core.TyCo.Subst( deepSubstTy, elemSubst, extendTvSubstWithClone )
|
|
|
28
|
+import GHC.Core.Predicate( EqRel(..), saturatedFamApp_maybe )
|
|
28
|
29
|
import GHC.Core.TyCon
|
|
29
|
30
|
import GHC.Core.Unify( AmIUnifying(..)
|
|
30
|
31
|
, tcUnifyTysForInjectivity, typeListsAreApart )
|
|
31
|
32
|
import GHC.Core.Coercion.Axiom
|
|
32
|
|
-import GHC.Core.TyCo.Subst( elemSubst, extendTvSubstWithClone )
|
|
33
|
33
|
|
|
34
|
34
|
import GHC.Builtin.WiredIn.TypeLits( tryInteractTopFam, tryInteractInertFam )
|
|
35
|
35
|
|
|
36
|
|
-import GHC.Types.Var( TyVar, tyVarKind, setTyVarKind )
|
|
|
36
|
+import GHC.Types.Var( TyVar, updateVarType, tyVarKind )
|
|
37
|
37
|
import GHC.Types.Var.Set
|
|
38
|
38
|
import GHC.Types.Var.Env( mkInScopeSet )
|
|
39
|
39
|
|
| ... |
... |
@@ -57,7 +57,6 @@ Here is our plan for dealing with functional dependencies |
|
57
|
57
|
* send the current constraint back to the start of the pipeline;
|
|
58
|
58
|
it might now be soluble, and it probably isn't inert
|
|
59
|
59
|
|
|
60
|
|
-
|
|
61
|
60
|
* Once a unification is done, it can't be un-done. So we must be very careful
|
|
62
|
61
|
not to threaten completeness by doing a unification when there might be
|
|
63
|
62
|
another way to solve the constraint.
|
| ... |
... |
@@ -613,6 +612,7 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args |
|
613
|
612
|
|
|
614
|
613
|
mkTopFamEqFDs :: TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns]
|
|
615
|
614
|
mkTopFamEqFDs fam_tc work_args work_item
|
|
|
615
|
+ -- NB: work_args exactly saturate fam_tc
|
|
616
|
616
|
| isOpenTypeFamilyTyCon fam_tc
|
|
617
|
617
|
, Injective inj_flags <- tyConInjectivityInfo fam_tc
|
|
618
|
618
|
= -- Open, injective type families
|
| ... |
... |
@@ -694,6 +694,8 @@ hasRelevantGiven eqs_for_me work_args (EqCt { eq_rhs = work_rhs }) |
|
694
|
694
|
getRelevantBranches :: CoAxiom Branched -> [TcType] -> Xi -> [FunDepEqns]
|
|
695
|
695
|
-- Return the FunDepEqns that arise from each relevant branch
|
|
696
|
696
|
-- Implements Note [Relevance]
|
|
|
697
|
+-- Caller will only use these FunDepEqns if there is just one relevant
|
|
|
698
|
+-- equation, i.e. the returned [FunDepEqns] is a singleton.
|
|
697
|
699
|
getRelevantBranches ax work_args work_rhs
|
|
698
|
700
|
= go (fromBranches (coAxiomBranches ax))
|
|
699
|
701
|
where
|
| ... |
... |
@@ -709,24 +711,33 @@ getRelevantBranches ax work_args work_rhs |
|
709
|
711
|
, cab_lhs = lhs_tys
|
|
710
|
712
|
, cab_rhs = rhs_ty })
|
|
711
|
713
|
| Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) work_tys
|
|
712
|
|
- -- See (RW1)-(RW3) in Note [Relevance]
|
|
713
|
|
- -- This is a pre-/unification/ check (not matching)
|
|
714
|
|
- -- so `subst` can bind both `qtvs` and fvs(work_tys)
|
|
715
|
|
- , let (subst',qtvs') = trim_qtvs subst qtvs
|
|
716
|
|
- lhs_tys' = substTys subst' lhs_tys
|
|
717
|
|
- rhs_ty' = substTy subst' rhs_ty
|
|
718
|
|
- branch_tys' = rhs_ty' : lhs_tys'
|
|
|
714
|
+ -- See (RW1)-(RW3) in Note [Relevance]
|
|
|
715
|
+ , let lhs_tys' = map (deepSubstTy subst) lhs_tys
|
|
|
716
|
+ -- deepSubstTy: see (CF8) in Note [Exploiting closed type families]
|
|
719
|
717
|
, noEarlierMatchCheck lhs_tys' branch -- See (RW4) in Note [Relevance]
|
|
720
|
|
- = Just (FDEqns { fd_qtvs = qtvs'
|
|
721
|
|
- , fd_eqs = zipWith Pair branch_tys' work_tys })
|
|
722
|
|
- -- Do not apply the substition to `work_tys`;
|
|
723
|
|
- -- See (CF7) in Note [Exploiting closed type families]
|
|
|
718
|
+ = -- Ignore the substitution, except for `noEarlierMatchCheck`
|
|
|
719
|
+ Just $
|
|
|
720
|
+ if all (`elemSubst` subst) qtvs
|
|
|
721
|
+ then -- `subst` eliminates all qtvs; and no qtvs in range of subst
|
|
|
722
|
+ -- See (CF7)(A) in Note [Exploiting closed type families]
|
|
|
723
|
+ FDEqns { fd_qtvs = []
|
|
|
724
|
+ , fd_eqs = zipWith Pair (substTy subst rhs_ty : lhs_tys')
|
|
|
725
|
+ work_tys }
|
|
|
726
|
+ -- Do not apply `subst` to `work_tys`;
|
|
|
727
|
+ -- See (CF9) in Note [Exploiting closed type families]
|
|
|
728
|
+
|
|
|
729
|
+ else -- Abandon `subst`; re-create it from (rhs_ty ~ work_rhs)
|
|
|
730
|
+ -- See (CF7)(B) in Note [Exploiting closed type families]
|
|
|
731
|
+ FDEqns { fd_qtvs = qtvs
|
|
|
732
|
+ , fd_eqs = zipWith Pair (rhs_ty:lhs_tys) work_tys }
|
|
|
733
|
+
|
|
724
|
734
|
| otherwise
|
|
725
|
735
|
= Nothing
|
|
726
|
736
|
|
|
727
|
737
|
mkTopOpenFamEqFDs :: TyCon -> [Bool] -> [TcType] -> EqCt -> TcS [FunDepEqns]
|
|
728
|
738
|
-- Implements (INJFAM:Wanted/top-open)
|
|
729
|
739
|
-- See Note [Exploiting open type families]
|
|
|
740
|
+-- NB: work_args exactly saturate fam_tc
|
|
730
|
741
|
mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs })
|
|
731
|
742
|
= do { fam_envs <- getFamInstEnvs
|
|
732
|
743
|
; let branches :: [CoAxBranch]
|
| ... |
... |
@@ -739,11 +750,13 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs }) |
|
739
|
750
|
, cab_lhs = branch_lhs_tys
|
|
740
|
751
|
, cab_rhs = branch_rhs })
|
|
741
|
752
|
| Just subst <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs]
|
|
742
|
|
- , let (subst', qtvs') = trim_qtvs subst branch_qtvs
|
|
|
753
|
+ -- No need for an apartness check here;
|
|
|
754
|
+ -- for open type families there are no "preceding" equations
|
|
|
755
|
+ -- Both branch_lhs_tys and work_args exactly saturate fam_tc
|
|
|
756
|
+ , let (subst', qtvs') = trimQtvs subst branch_qtvs
|
|
743
|
757
|
branch_lhs_tys' = substTys subst' branch_lhs_tys
|
|
744
|
|
- -- No need for an apartness check here; for open type families
|
|
745
|
|
- -- there are no "preceding" equations
|
|
746
|
|
- = Just (mkInjectivityFDEqn inj_flags qtvs' branch_lhs_tys' work_args)
|
|
|
758
|
+ -- We must apply the substitution here see (OF1)
|
|
|
759
|
+ = Just (mkInjectivityFDEqn qtvs' inj_flags branch_lhs_tys' work_args)
|
|
747
|
760
|
|
|
748
|
761
|
| otherwise
|
|
749
|
762
|
= Nothing
|
| ... |
... |
@@ -756,9 +769,9 @@ mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs |
|
756
|
769
|
= do { let -- eqns_from_inerts: see (INJFAM:Wanted/other)
|
|
757
|
770
|
eqns_from_inerts = mapMaybe do_one eqs_for_me
|
|
758
|
771
|
-- eqns_from_self: see (INJFAM:Wanted/Self)
|
|
759
|
|
- eqns_from_self = case tcSplitTyConApp_maybe work_rhs of
|
|
760
|
|
- Just (tc,rhs_tys) | tc==fam_tc -> [mk_eqn rhs_tys]
|
|
761
|
|
- _ -> []
|
|
|
772
|
+ eqns_from_self = case saturatedFamApp_maybe work_rhs of
|
|
|
773
|
+ Just (tc, rhs_tys) | fam_tc == tc -> [mk_eqn rhs_tys]
|
|
|
774
|
+ _ -> []
|
|
762
|
775
|
|
|
763
|
776
|
; return (eqns_from_inerts ++ eqns_from_self) }
|
|
764
|
777
|
where
|
| ... |
... |
@@ -768,11 +781,12 @@ mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs |
|
768
|
781
|
| otherwise = Nothing
|
|
769
|
782
|
do_one _ = pprPanic "interactFunEq 2" (ppr fam_tc) -- TyVarLHS
|
|
770
|
783
|
|
|
771
|
|
- mk_eqn iargs = mkInjectivityFDEqn inj_flags [] work_args iargs
|
|
|
784
|
+ mk_eqn iargs = mkInjectivityFDEqn [] inj_flags work_args iargs
|
|
|
785
|
+
|
|
772
|
786
|
|
|
773
|
|
-trim_qtvs :: Subst -> [TyVar] -> (Subst, [TyVar])
|
|
|
787
|
+trimQtvs :: Subst -> [TyVar] -> (Subst, [TyVar])
|
|
774
|
788
|
-- Tricky stuff: see (OF1) in Note [Exploiting open type families]
|
|
775
|
|
-trim_qtvs unif_subst qtvs
|
|
|
789
|
+trimQtvs unif_subst qtvs
|
|
776
|
790
|
= go unif_subst qtvs
|
|
777
|
791
|
where
|
|
778
|
792
|
go :: Subst -> [TyVar] -> (Subst, [TyVar])
|
| ... |
... |
@@ -786,7 +800,7 @@ trim_qtvs unif_subst qtvs |
|
786
|
800
|
go_fresh subst tv tvs = (subst'', tv':tvs')
|
|
787
|
801
|
where
|
|
788
|
802
|
!(subst'', tvs') = go subst' tvs
|
|
789
|
|
- tv' = tv `setTyVarKind` substTy subst (tyVarKind tv)
|
|
|
803
|
+ tv' = updateVarType (substTy subst) tv
|
|
790
|
804
|
subst' = extendTvSubstWithClone subst tv tv'
|
|
791
|
805
|
-- extendTvSubstWithClone: see (OF1b) in
|
|
792
|
806
|
-- Note [Exploiting open type families]
|
| ... |
... |
@@ -883,15 +897,22 @@ mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs |
|
883
|
897
|
; return (eqns_from_inerts ++ eqns_from_self) }
|
|
884
|
898
|
|
|
885
|
899
|
--------------------
|
|
886
|
|
-mkInjectivityFDEqn :: [Bool] -- Injectivity flags
|
|
887
|
|
- -> [TcTyVar] -- Quantify these
|
|
|
900
|
+mkInjectivityFDEqn :: [TcTyVar] -- Quantify these
|
|
|
901
|
+ -> [Bool] -- Injectivity flags
|
|
888
|
902
|
-> [TcType] -> [TcType] -- Make these equal
|
|
889
|
903
|
-> FunDepEqns
|
|
890
|
904
|
-- When F s1 s2 s3 ~ F t1 t2 t3, and F has injectivity info [True,False,True]
|
|
891
|
905
|
-- return the FDEqns { fd_eqs = [Pair s1 t1, Pair s3 t3] }
|
|
892
|
|
--- The injectivity flags [Bool] will not all be False, but nothing goes wrong if they are
|
|
893
|
|
-mkInjectivityFDEqn inj_args qtvs lhs_args rhs_args
|
|
894
|
|
- = FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }
|
|
|
906
|
+-- The injectivity flags [Bool] will not all be False,
|
|
|
907
|
+-- but nothing goes wrong if they are
|
|
|
908
|
+mkInjectivityFDEqn qtvs inj_args lhs_args rhs_args
|
|
|
909
|
+ = assertPpr (let len = length inj_args in
|
|
|
910
|
+ len == length lhs_args && len == length rhs_args)
|
|
|
911
|
+ (vcat [ text "inj_args:" <+> ppr inj_args
|
|
|
912
|
+ , text "lhs_args:" <+> ppr lhs_args
|
|
|
913
|
+ , text "rhs_args:" <+> ppr rhs_args ]) $
|
|
|
914
|
+ -- Need inj_args lhs_args and rhs_args to be same length for zip3
|
|
|
915
|
+ FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }
|
|
895
|
916
|
where
|
|
896
|
917
|
eqs = [ Pair lhs_arg rhs_arg
|
|
897
|
918
|
| (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ]
|
| ... |
... |
@@ -1096,73 +1117,108 @@ Note [Exploiting closed type families]. |
|
1096
|
1117
|
|
|
1097
|
1118
|
Note [Exploiting open type families] see `mkTopOpenFamEqFDs`
|
|
1098
|
1119
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1099
|
|
-Generating fundeps from a top-level type instance is covered in Section 5.2 in
|
|
1100
|
|
-the Injective Type Families paper. For open type families, suppose we have
|
|
1101
|
|
-
|
|
1102
|
|
- type family F a = r | r -> a
|
|
1103
|
|
- type instance F [a] = a->a
|
|
1104
|
|
-
|
|
1105
|
|
- [W] F alpha ~ Int->Int
|
|
1106
|
|
-
|
|
1107
|
|
-Then, since F has been checked as injective, and the RHS `a->a` (pre-)unifies
|
|
1108
|
|
-with the `Int->Int` in the Wanted with substitution [a :-> Int], we can
|
|
1109
|
|
-add a derived FunDepEqns
|
|
|
1120
|
+For an open type family, generating fundeps from a top-level type instance
|
|
|
1121
|
+is covered in Section 5.2 in the Injective Type Families paper.
|
|
|
1122
|
+
|
|
|
1123
|
+Basic plan: Suppose type family F a b = r | r -> b
|
|
|
1124
|
+ and Wanted [W] F wlhs1 wlhs2 ~ wrhs
|
|
|
1125
|
+ Then for each type instance (F lhs1 lhs2 = rhs)
|
|
|
1126
|
+ * Pre-match `rhs` against `wrhs` to get a substitution S
|
|
|
1127
|
+ * Generate fundep equalities S(lhs2) ~ wrhs
|
|
|
1128
|
+ one for each fundep in the type family
|
|
|
1129
|
+
|
|
|
1130
|
+For example, suppose we have
|
|
|
1131
|
+ type family F a b = r | r -> a
|
|
|
1132
|
+ type instance F [a] Int = a->a
|
|
|
1133
|
+ [W] F alpha beta ~ Int->Int
|
|
|
1134
|
+Then we pre-match template (a->a) against target (Int->Int) to get
|
|
|
1135
|
+substitution S = [a :-> Int]. Then we can generate
|
|
1110
|
1136
|
FDEqns{ fd_qtvs = [], fd_eqs = [ [Int] ~ alpha ] }
|
|
1111
|
1137
|
and thereby emit (via `instantiateFunDepEqns`)
|
|
1112
|
1138
|
[W] [Int] ~ alpha
|
|
|
1139
|
+Note that `fd_qtvs = []`; we have substituted away the quantifed type variable `a`.
|
|
1113
|
1140
|
|
|
1114
|
1141
|
Things are a bit trickier when not all the quantified variables of the
|
|
1115
|
1142
|
instance are bound by the pre-unification step. Suppose we have
|
|
1116
|
|
-
|
|
1117
|
1143
|
type family F a = r | r -> a
|
|
1118
|
|
- type instance F (Maybe a) = [G a] -- G is a type family
|
|
|
1144
|
+ type instance F (b -> Maybe a) = [(G a,b)] -- G is a type family
|
|
|
1145
|
+ [W] F alpha ~ [(ty,Int)]
|
|
1119
|
1146
|
|
|
1120
|
|
- [W] F alpha ~ [ty]
|
|
1121
|
|
-
|
|
1122
|
|
-Then, the RHS [G a] pre-unifies with the RHS of the Wanted, but with an
|
|
1123
|
|
-empty substitution, which does not fix `a`. So we get this FunDepEqns:
|
|
1124
|
|
- FDEqns{ fd_qtvs = a, fd_eqs = [ Maybe a ~ alpha ] }
|
|
|
1147
|
+Then, the RHS [(G a,b)] pre-matches with the RHS of the Wanted, but with
|
|
|
1148
|
+the substitution [b :-> Int], which does not fix `a`. So, after trimming
|
|
|
1149
|
+the quantified type variables, we get this FunDepEqns:
|
|
|
1150
|
+ FDEqns{ fd_qtvs = [a], fd_eqs = [ (Int -> Maybe a) ~ alpha ] }
|
|
1125
|
1151
|
(note the fd_qtvs=a) and thereby emit (via `instantiateFunDepEqns`)
|
|
1126
|
|
- [W] Maybe gamma ~ alpha -- Fresh gamma
|
|
1127
|
|
-We have learned that `alpha` must be a list, but that is all; we don't
|
|
1128
|
|
-know what is inside it, hence the fresh `gamma`.
|
|
1129
|
|
-
|
|
1130
|
|
-In general we have:
|
|
1131
|
|
-
|
|
1132
|
|
- type family F x1 ... xn = r | r -> x1 x3 (say)
|
|
1133
|
|
- type instance forall qs. F t1 ... tn = rhs
|
|
1134
|
|
- [W] F a1 ... an ~ b
|
|
1135
|
|
-
|
|
1136
|
|
-Suppose `b` pre-unifies with `rhs`, with substitution `S` that binds
|
|
1137
|
|
-some (but perhaps not all) of the `qs`. Lets say
|
|
1138
|
|
- unbound_qs = qs \ dom(S)
|
|
1139
|
|
-
|
|
1140
|
|
-Then we generate the FunDepEqs
|
|
1141
|
|
- FDEqns{ fd_qtvs = unbound_qs
|
|
1142
|
|
- , fd_eqs = [ S(t1) ~ S(a1), S(t3) ~ S(a3) ]
|
|
1143
|
|
-
|
|
1144
|
|
-These 'fd_qtvs' are subsequently instantiated to fresh metavariables by
|
|
1145
|
|
-'instantiateFunDepEqns'.
|
|
1146
|
|
-
|
|
1147
|
|
-(OF1) The computation of `unbound_qs` is done by `trim_qtvs`, but we
|
|
1148
|
|
- have to be careful. Suppose (OF1a)
|
|
1149
|
|
- qs = [ k::*, a::k ]
|
|
1150
|
|
- S = [ k :-> UnliftedType ]
|
|
1151
|
|
- Then we want
|
|
1152
|
|
- unbound_qs = [ a::UnliftedType ] -- Must apply S to q's kind!!
|
|
1153
|
|
-
|
|
1154
|
|
- And (OF1b), although I'm not sure if this can acutally happen:
|
|
1155
|
|
- qs = [ k::*, a::(k::*), b::(a::k) ]
|
|
1156
|
|
- S = [ k :-> UnliftedType ]
|
|
1157
|
|
- We want
|
|
1158
|
|
- unbound_qs = [ a::UnliftedType, b::(a::UnliftedType) ]
|
|
1159
|
|
- Note that the occurrence of `a` in b's kind should be replaced by the
|
|
1160
|
|
- newly-kinded `a` not the original `a::k`.
|
|
1161
|
|
-
|
|
1162
|
|
- This fancy footwork for `fd_qtvs` is done by `trim_qtvs`. It
|
|
1163
|
|
- * Drops a quantified tyvar that /is/ matched
|
|
1164
|
|
- * Substitutes in the kind of a quantified tyvar `a` that is /not/ matched
|
|
1165
|
|
- * Adds the substitution [a::k :-> a::S(k)] to the substitution
|
|
|
1152
|
+ [W] (Int -> Maybe gamma) ~ alpha -- Fresh gamma
|
|
|
1153
|
+We have learned that `alpha` must be a list of functions, but that is all;
|
|
|
1154
|
+we don't know `gamma`.
|
|
|
1155
|
+
|
|
|
1156
|
+It gets tricker. Suppose we have
|
|
|
1157
|
+ type family F @k a = r | r -> k,a
|
|
|
1158
|
+ type instance F @k (Proxy @k (a::k)) = T k
|
|
|
1159
|
+ [W] F alpha beta ~ T Type
|
|
|
1160
|
+Matching the RHS gives substitution [k :-> Type]. But we do /not/ want
|
|
|
1161
|
+to end up with this:
|
|
|
1162
|
+ FDEqns{ fd_qtvs = [a::k], fd_eqs = [ Proxy @Type (a::k) ~ alpha ] }
|
|
|
1163
|
+because that `k`, hidden inside the kind of `a`, escapes into the solver.
|
|
|
1164
|
+
|
|
|
1165
|
+Instead we must not only "trim" the quantified variables of the equation,
|
|
|
1166
|
+but /substitute/ in the kinds of the leftover quantified ones: see Note [trimQtvs].
|
|
|
1167
|
+Then we get
|
|
|
1168
|
+ FDEqns{ fd_qtvs = [a::Type], fd_eqs = [ Proxy @Type (a::Type) ~ alpha ] }
|
|
|
1169
|
+
|
|
|
1170
|
+Side note:
|
|
|
1171
|
+
|
|
|
1172
|
+You might wonder whether we can use the same solution as we do for /closed/
|
|
|
1173
|
+families, by generating an equality for the RHSs; see (CF7). But we can't.
|
|
|
1174
|
+Example:
|
|
|
1175
|
+ type family G a where G Int = Char
|
|
|
1176
|
+ type family F a b = r | r -> a
|
|
|
1177
|
+ type instance F a [c] = (a, G a) -- (I1)
|
|
|
1178
|
+ type instance F Int Bool = (Int, Bool) -- (I2)
|
|
|
1179
|
+ [W] F alpha Bool ~ (Int,gamma)
|
|
|
1180
|
+Note that (I1) and (I2) are pariwise-injective: if the RHSs are the same,
|
|
|
1181
|
+the LHSs are the same /in the injective position(s)/. See Section 4.2 in
|
|
|
1182
|
+the "Injective Type Families" paper. We pre-match (I1) against the Wanted
|
|
|
1183
|
+giving substitution [a :-> Int]. We /cannot/ then generate
|
|
|
1184
|
+ FDEqns { fd_qtvs = [a,c], fd_eqs = [ a~alpha
|
|
|
1185
|
+ , (a,G a)~(Int,gamma) ] }
|
|
|
1186
|
+where the second equality (wrongly)equates the RHSs. This is wrong because we
|
|
|
1187
|
+can solve those fundeps unifying alpha:=Int, gamma:=Char, by reducing (G a) to
|
|
|
1188
|
+Char. So we end up with [W] F Int Char, which is insoluble. Bad.
|
|
|
1189
|
+
|
|
|
1190
|
+End of side note.
|
|
|
1191
|
+
|
|
|
1192
|
+Note [trimQtvs]
|
|
|
1193
|
+~~~~~~~~~~~~~~~
|
|
|
1194
|
+In Note [Exploiting open type families], we needed to "trim" the quantified
|
|
|
1195
|
+type variables of the axiom, removing ones that are eliminated by the
|
|
|
1196
|
+auubstitution `S`. So can we just do this?
|
|
|
1197
|
+ unbound_qs = qs \ dom(S)
|
|
|
1198
|
+so that we could make fresh meta-variables for only the unbound `qs`.
|
|
|
1199
|
+No, we can't. Suppose (TQ1)
|
|
|
1200
|
+ qs = [ k::*, a::k ]
|
|
|
1201
|
+ S = [ k :-> UnliftedType ]
|
|
|
1202
|
+Then we want
|
|
|
1203
|
+ unbound_qs = [ a::UnliftedType ] -- Must apply S to q's kind!!
|
|
|
1204
|
+
|
|
|
1205
|
+And (TQ2), although I'm not sure if this can actually happen:
|
|
|
1206
|
+ qs = [ k::*, a::(k::*), b::(a::k) ]
|
|
|
1207
|
+ S = [ k :-> UnliftedType ]
|
|
|
1208
|
+We want
|
|
|
1209
|
+ unbound_qs = [ a::UnliftedType, b::(a::UnliftedType) ]
|
|
|
1210
|
+Note that the occurrence of `a` in b's kind should be replaced by the
|
|
|
1211
|
+newly-kinded `a` not the original `a::k`.
|
|
|
1212
|
+
|
|
|
1213
|
+The function `trimQtvs` does the job. It
|
|
|
1214
|
+ * Drops a quantified tyvar that /is/ matched
|
|
|
1215
|
+ * Substitutes in the kind of a quantified tyvar `a` that is /not/ matched
|
|
|
1216
|
+ * Adds the substitution [a::k :-> a::S(k)] to the substitution
|
|
|
1217
|
+
|
|
|
1218
|
+We only use `trimQtvs` for /open/ families. For /closed/ families where we
|
|
|
1219
|
+/unify/ (rather than match) the equation with the Wanted, it just doesn't work
|
|
|
1220
|
+(#27994), because the substitution `S` has both meta-tyvars and instance-head
|
|
|
1221
|
+tyvars in the range. See (CF7).
|
|
1166
|
1222
|
|
|
1167
|
1223
|
Note [Exploiting closed type families] see `mkTopClosedFamEqFDs`
|
|
1168
|
1224
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -1194,9 +1250,6 @@ More precisely, here is the very clever Closed Family Fundep Algorithm (CFFA) |
|
1194
|
1250
|
with fresh unification vars in lhs and rhs for the quantified variables of the
|
|
1195
|
1251
|
equation.
|
|
1196
|
1252
|
|
|
1197
|
|
- Actually we take advantage of the substitution generated by the relevance
|
|
1198
|
|
- check, see (CF7) below.
|
|
1199
|
|
-
|
|
1200
|
1253
|
See Note [Relevance] for what "relevant" means.
|
|
1201
|
1254
|
We need to take care about non-termination; see (CF3).
|
|
1202
|
1255
|
|
| ... |
... |
@@ -1303,7 +1356,7 @@ Key point: equations that are not relevant do not need to be considered for fund |
|
1303
|
1356
|
I was looking at non-termination for closed type families, but it's a small
|
|
1304
|
1357
|
improvement in general.
|
|
1305
|
1358
|
|
|
1306
|
|
- This bale-out is implementedn in `tryFDEqns`.
|
|
|
1359
|
+ This bale-out is implemented in `tryFDEqns`.
|
|
1307
|
1360
|
|
|
1308
|
1361
|
(CF5) Consider (see "Yikes5" in #23162):
|
|
1309
|
1362
|
type family F a where
|
| ... |
... |
@@ -1353,32 +1406,20 @@ Key point: equations that are not relevant do not need to be considered for fund |
|
1353
|
1406
|
`app` Proxy @'[ty_n]
|
|
1354
|
1407
|
|
|
1355
|
1408
|
Every `app` call gives rise to a Wanted of the form:
|
|
1356
|
|
-
|
|
1357
|
1409
|
[W] MapId acc_i ++ MapId '[ty_i] ~ MapId (acc_i ++ '[ty_i])
|
|
1358
|
|
-
|
|
1359
|
1410
|
while the overall result type gives us a Wanted of the form
|
|
1360
|
|
-
|
|
1361
|
1411
|
[W] acc_n ++ '[ty_n] ~ [ty_1, ..., ty_n]
|
|
1362
|
|
-
|
|
1363
|
1412
|
By using (CFFA) on this result Wanted, we deduce that we must have
|
|
1364
|
|
-
|
|
1365
|
1413
|
acc_n ~ [ty_1, ..., ty_{n-1}]
|
|
1366
|
|
-
|
|
1367
|
1414
|
which is a flat list. Repeating the process, (CFFA) allows us to deduce that
|
|
1368
|
|
-
|
|
1369
|
1415
|
acc_i ~ [ty_1, ..., ty_{i-1}]
|
|
1370
|
|
-
|
|
1371
|
1416
|
for all i. This allows the other Wanteds to be solved directly, giving rise to
|
|
1372
|
1417
|
proof terms with the typical triangular O(n^2) shape
|
|
1373
|
|
-
|
|
1374
|
1418
|
co_i = (O(i) proof that MapId acc_i ++ MapId '[ty_i] ~ acc_{i+1})
|
|
1375
|
1419
|
; (O(i) proof that acc_{i+1} ~ MapId (acc_i ++ '[ty_i]))
|
|
1376
|
|
-
|
|
1377
|
1420
|
However, /without/ (CFFA), 'acc_i' is not unified with a flat list but is left
|
|
1378
|
1421
|
as the nested application:
|
|
1379
|
|
-
|
|
1380
|
1422
|
acc_i ~ (... (('[ty_1] ++ '[ty_2]) ++ '[ty_3]) ... ++ '[ty_{i-1}])
|
|
1381
|
|
-
|
|
1382
|
1423
|
This means that 'MapId acc_i' is stuck until we reduce the above, which takes
|
|
1383
|
1424
|
O(i^2) type family reduction steps, instead of O(i). The same applies to
|
|
1384
|
1425
|
the other proof term involving 'MapId (acc_i ++ '[ty_i])'.
|
| ... |
... |
@@ -1388,29 +1429,77 @@ Key point: equations that are not relevant do not need to be considered for fund |
|
1388
|
1429
|
instead of relying on recursively built sub-proof terms, which brings down
|
|
1389
|
1430
|
coercion sizes (in certain situations) from O(n^3) to O(n^2).
|
|
1390
|
1431
|
|
|
1391
|
|
-(CF7) Relevance. When computing relevance in the Closed Family Fundep Algorithm,
|
|
1392
|
|
- for [W] F wlhs1 wlhs2 ~ wrhs, against an equation F lhs1 lhs2 ~ rhs
|
|
1393
|
|
- we get back a pre-unifying substitution S that makes the two as equal as poss.
|
|
1394
|
|
- Then we use `trim_qtvs` in exactly the same way as in
|
|
1395
|
|
- (OF1) of Note [Exploiting open type families], to generate qtvs' and S'
|
|
1396
|
|
- Then we generate
|
|
1397
|
|
- FDEqns { fd_qtvs = qtvs', fd_eqs [ S'(lhs1) ~ wlhs1
|
|
1398
|
|
- , S'(lhs2) ~ wlhs2
|
|
1399
|
|
- , S'(rhs) ~ wrhs ] }
|
|
1400
|
|
- Notice that we do /not/ apply S' to wlhs/wrhs. These type contain unification
|
|
1401
|
|
- variables that we must actually unify! Example
|
|
|
1432
|
+(CF7) Relevance. The function `getRelevantBranches` finds the relevant branch(es)
|
|
|
1433
|
+ using the (CFFA). Given an equation
|
|
|
1434
|
+ type family F a b where { ...; F lhs1 lhs2 = rhs; ... }
|
|
|
1435
|
+ is the equation relevant for this Wanted?
|
|
|
1436
|
+ [W] F wlhs1 wlhs2 ~ wrhs
|
|
|
1437
|
+ We pre-unify [wrhs,wlhs1,wlhs2] against [rhs,lhs1,lhs2], getting back a
|
|
|
1438
|
+ substitution `S` that makes the two as equal as poss. This substitution can bind
|
|
|
1439
|
+ both /quantified/ type variables from the instance, and /unification variables/
|
|
|
1440
|
+ from the solver. Moreover, both kind of type variable can appear in the range
|
|
|
1441
|
+ of the substitution.
|
|
|
1442
|
+
|
|
|
1443
|
+ That's pretty complicated, so we are very cautious about using the returned
|
|
|
1444
|
+ substitution `S`. I had a long struggle (#27994) when we were previously trying to
|
|
|
1445
|
+ apply that returned substitution as an optimisation. But because type variables
|
|
|
1446
|
+ can occur in kinds, it was a losing battle -- I ended up with TyVars from the
|
|
|
1447
|
+ instance head escaping into the solver. Consider
|
|
|
1448
|
+ type F :: forall k. k -> *
|
|
|
1449
|
+ type instance F k a where
|
|
|
1450
|
+ F (k->*) (b::k->*) = Int
|
|
|
1451
|
+ [W] F kappa (alpha::kappa) ~ Int
|
|
|
1452
|
+ We unify the Wanted with the equation to get the substitution
|
|
|
1453
|
+ [ kappa :-> k->*, b :-> (alpha::k->*) ]
|
|
|
1454
|
+ I tried using `trimQtvs` (see Note [trimQtvs]) but my head hurt
|
|
|
1455
|
+ FAR too much: #27994 is an excellent example.
|
|
|
1456
|
+
|
|
|
1457
|
+ So here's the plan:
|
|
|
1458
|
+ (A) As a fast-path optimisation, if `S` binds all the quantified variables of
|
|
|
1459
|
+ the type-family equation (which is common), it's all easy: generate
|
|
|
1460
|
+ FDEqns { fd_qtvs = []
|
|
|
1461
|
+ , fd_eqs = [ S(lhs1) ~ wlhs1, S(lhs2) ~ wlhs2, S(rhs) ~ wrhs }
|
|
|
1462
|
+ Note that we do not apply S to `wlhs1/2` or `wrhs`; see (CF9).
|
|
|
1463
|
+ And note that `fd_qtvs` is empty, because `S` eliminates all the quantified
|
|
|
1464
|
+ type variables.
|
|
|
1465
|
+
|
|
|
1466
|
+ (B) If not, then /discard `S` entirely/ and instead generate
|
|
|
1467
|
+ FDEqns { fd_qtvs = qtvs
|
|
|
1468
|
+ , fd_eqs = [ lhs ~ wlhs1, lhs2 ~ wlhs2, rhs ~ wrhs }
|
|
|
1469
|
+ Now `instantiateFDEqns` will generate fresh unification variables for the
|
|
|
1470
|
+ `qtvs` and the solver will in effect reconstruct `S`.
|
|
|
1471
|
+
|
|
|
1472
|
+(CF8) When computing relevance (Note [Relevance] (R2)) we use `noEarlierMatch`
|
|
|
1473
|
+ on S(lhs). But we must use /deep/ substitution on LHS. Consider
|
|
|
1474
|
+ type family F (a :: k) r where
|
|
|
1475
|
+ F @Type (Maybe Bool) r = r -- B0
|
|
|
1476
|
+ F @Type ((f :: Type -> Type) Int) r = Bool -- B1
|
|
|
1477
|
+ F @k ((g :: Type -> k) Int) r = r -- B2
|
|
|
1478
|
+
|
|
|
1479
|
+ [W] F @Type alpha Char ~ Char
|
|
|
1480
|
+ The `tcUnifyTysForInjectivity` will return substitution
|
|
|
1481
|
+ [k :-> Type, r :-> Char, alpha :-> (g :: Type->Type) Int], with g unbound
|
|
|
1482
|
+ If we used `substTy` on the LHS-tys for B2 we'd get
|
|
|
1483
|
+ lhs_tys' = [Type, (g :: Type -> k) Int, Char]
|
|
|
1484
|
+ That's ill-kinded because of the free `k`; and (worse) when `noEarlierMatch` matches
|
|
|
1485
|
+ lhs_tys' against earlier equations, (B1) will fail to match. So (B2) will be
|
|
|
1486
|
+ judged relevant, even though it isn't. Bad, bad. Use deep substitution.
|
|
|
1487
|
+
|
|
|
1488
|
+(CF9) Notice that, in (CF7) when generating the fundep equations for the solver to
|
|
|
1489
|
+ deal with, we do /not/ apply the pre-unifying substitution `S` to the Wanted types.
|
|
|
1490
|
+ The types wlhs,wrhs contain meta-variables that we must actually unify! Example
|
|
1402
|
1491
|
type family F a b where
|
|
1403
|
1492
|
F [a] (Maybe b) = (a, G b)
|
|
1404
|
1493
|
[W] F alpha beta ~ (Int, [gamma])
|
|
1405
|
1494
|
Pre-unification gives [alpha :-> [a], beta :-> Maybe b, a :-> Int]
|
|
1406
|
|
- This substitution eliminates the quantified variable `a` but not `b`, so we
|
|
1407
|
|
- want this:
|
|
1408
|
|
- FDEqns { fd_qtvs = [b], fd_eqs [ [Int] ~ alpha
|
|
1409
|
|
- , Maybe b ~ beta
|
|
1410
|
|
- , (Int,G b) ~ (Int, [gamma]) ] }
|
|
1411
|
|
-
|
|
1412
|
|
- Notice also that by applying the substitution to the template but not to the
|
|
1413
|
|
- Wanted, we uphold the `fd_eqs` invariant in `FDEqns` (see its data type decl)
|
|
|
1495
|
+ We don't want to substitute `alpha` away! In fact we just generate these eqns:
|
|
|
1496
|
+ FDEqns { fd_qtvs = [a,b], fd_eqs [ [a] ~ alpha
|
|
|
1497
|
+ , Maybe b ~ beta
|
|
|
1498
|
+ , (a, G b) ~ (Int, [gamma]) ] }
|
|
|
1499
|
+ with `fd_qtvs` being the quantified variables of the equation, the LHSs
|
|
|
1500
|
+ being the types from the equation and the RHSs being the types from the Wanted.
|
|
|
1501
|
+ Notice that we thereby uphold the `fd_eqs` invariant in `FDEqns` (see its data
|
|
|
1502
|
+ type decl).
|
|
1414
|
1503
|
|
|
1415
|
1504
|
Note [Cache-caused loops]
|
|
1416
|
1505
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -1517,8 +1606,8 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) |
|
1517
|
1606
|
rev_eqs = reverse eqs
|
|
1518
|
1607
|
-- (reverse eqs): See Note [Reverse order of fundep equations]
|
|
1519
|
1608
|
|
|
1520
|
|
- -- The kinds of `qtvs` can have free variables, because of `trim_qtvs`
|
|
1521
|
|
- -- Example: T27828b
|
|
|
1609
|
+ -- The kinds of `qtvs` can have free variables;
|
|
|
1610
|
+ -- in the FDEqns produced by GHC.Tc.Instance.FunDeps.improveFromInstEnv
|
|
1522
|
1611
|
empty_subst = mkEmptySubst $ mkInScopeSet $
|
|
1523
|
1612
|
tyCoVarsOfTypes (map tyVarKind qtvs)
|
|
1524
|
1613
|
|