Simon Peyton Jones pushed to branch wip/T27994 at Glasgow Haskell Compiler / GHC Commits: 1d55c7c2 by Simon Peyton Jones at 2026-10-21T11:28:06+01:00 Fix a nasty bug in fundeps This MR fixes #27994, which turned out to be very tricky. See Note [Exploiting closed type famililes], especially (CF7). I was ending up with TyVars (not TcTyVars) in the solver, because the substitution computed in getRelevantBranches mixed the two. On the way I discovered that niFixSubst could produce a substitution that mentioned domain variables on in its range. Boo! I fixed that, but I'm not very pleased with the result. The "re-kinding" business seems very shady. But it's not new, so I'll leave that as a battle for another day. - - - - - 2f218858 by Simon Peyton Jones at 2026-10-21T11:28:39+01:00 Simplify a tc-trace which could crash the compiler The trace formatted an error message, which prematurely read the ExpType in a FunResCtxt. Solution: don't put call the error message formatting when tc-tracing. - - - - - 8 changed files: - compiler/GHC/Core/Predicate.hs - compiler/GHC/Core/TyCo/FVs.hs - compiler/GHC/Core/Unify.hs - compiler/GHC/Tc/Solver/FunDeps.hs - compiler/GHC/Tc/Utils/Unify.hs - + testsuite/tests/typecheck/should_compile/T27994.hs - + testsuite/tests/typecheck/should_compile/T27994a.hs - testsuite/tests/typecheck/should_compile/all.T Changes: ===================================== compiler/GHC/Core/Predicate.hs ===================================== @@ -700,7 +700,9 @@ types/kinds are fully settled and zonked. -- See also Note [Ordering of implicit variables] in "GHC.Rename.HsType" scopedSort :: [Var] -> [Var] -scopedSort = go [] [] +scopedSort xs + | null xs = [] + | otherwise = go [] [] xs where go :: [Var] -- already sorted, in reverse order -> [TyCoVarSet] -- each set contains all the variables which must be placed ===================================== compiler/GHC/Core/TyCo/FVs.hs ===================================== @@ -49,6 +49,7 @@ module GHC.Core.TyCo.FVs -- Any and No Free vars anyFreeVarsOfType, anyFreeVarsOfTypes, anyFreeVarsOfCo, noFreeVarsOfType, noFreeVarsOfTypes, noFreeVarsOfCo, + anyShallowFreeVarsOfType, -- * Free type constructors tyConsOfType, tyConsOfTypes, @@ -1159,10 +1160,12 @@ nice tight loops. {-# INLINE afvFolder #-} -- See Note [Any-free-var folder] afvFolder :: TyCoFolder (FV (BoundVars, TyCoVar -> Bool) DM.Any) -- 'afvFolder' is short for "any-free-var folder", good for checking --- if any /deep/ free var of a type satisfies a predicate `check_fv` +-- if any /shallow/ free var of a type satisfies a predicate `check_fv` -- The "env" of the (FV env Any) is a pair (bvs,pred) of -- the bound variables (which are definitely not free), and -- an "interesting var" predicate which the client supplies +-- +-- You can get "deep" by supplying a predicte that also looks in the kind afvFolder = TyCoFolder { tcf_view = noView -- See Note [Free vars and synonyms] , tcf_tyvar = do_tcv , tcf_covar = do_tcv @@ -1173,8 +1176,7 @@ afvFolder = TyCoFolder { tcf_view = noView -- See Note [Free vars and synonyms] -- Check the variable itself with check_fv, -- and check its kind (this is a deep folder) do_tcv tcv = MkFV $ \ (bvs,check_fv) -> - Any (not (tcv `elemVarSet` bvs) && - (check_fv tcv || runAny check_fv (afv_type (varType tcv)))) + Any (not (tcv `elemVarSet` bvs) && check_fv tcv) do_hole hole = do_tcv (coHoleCoVar hole) afv_type :: Type -> FV (BoundVars, TyVar -> Bool) DM.Any @@ -1186,16 +1188,34 @@ runAny :: (TyVar -> Bool) -> FV (BoundVars, TyCoVar -> Bool) DM.Any -> Bool {-# INLINE runAny #-} runAny check_fv fvs = DM.getAny (runFV fvs (emptyVarSet, check_fv)) +--------------------------------------------- +-- anyShallowFreeVarsOfType and friends look only for shallow free vars + +anyShallowFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool +{-# INLINE anyShallowFreeVarsOfType #-} +-- Shallow only +anyShallowFreeVarsOfType check_fv ty = runAny check_fv (afv_type ty) + + +--------------------------------------------- +-- anyFreeVarsOfType and friends look for deep free vars + +deep_check :: (TyCoVar -> Bool) -> TyCoVar -> Bool +-- This is inefficient because if one tyvar occurs many times +-- in a type, it'll look in its kind at each occurrence. Boo. +deep_check check_fv tcv = check_fv tcv || anyFreeVarsOfType check_fv (varType tcv) + anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool {-# INLINE anyFreeVarsOfType #-} -anyFreeVarsOfType check_fv ty = runAny check_fv (afv_type ty) +-- Looks deeply, into the kinds of the free vars +anyFreeVarsOfType check_fv ty = runAny (deep_check check_fv) (afv_type ty) anyFreeVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> Bool {-# INLINE anyFreeVarsOfTypes #-} -anyFreeVarsOfTypes check_fv tys = runAny check_fv (afv_types tys) +anyFreeVarsOfTypes check_fv tys = runAny (deep_check check_fv) (afv_types tys) anyFreeVarsOfCo :: (TyCoVar -> Bool) -> Coercion -> Bool -anyFreeVarsOfCo check_fv co = runAny check_fv (afv_co co) +anyFreeVarsOfCo check_fv co = runAny (deep_check check_fv) (afv_co co) noFreeVarsOfType :: Type -> Bool noFreeVarsOfType ty = not $ runAny (const True) (afv_type ty) @@ -1206,6 +1226,7 @@ noFreeVarsOfTypes tys = not $ runAny (const True) (afv_types tys) noFreeVarsOfCo :: Coercion -> Bool noFreeVarsOfCo co = not $ runAny (const True) (afv_co co) + {- ************************************************************************ * * ===================================== compiler/GHC/Core/Unify.hs ===================================== @@ -33,7 +33,9 @@ import GHC.Core.TyCon import GHC.Core.Predicate( CanEqLHS(..), canEqLHS_maybe ) import GHC.Core.TyCon.Env import GHC.Core.TyCo.Rep +import GHC.Core.TyCo.FVs import GHC.Core.TyCo.Compare ( eqType, tcEqType, tcEqTyConAppArgs ) +import qualified GHC.Core.TyCo.Subst as S import GHC.Core.TyCo.Subst ( mkTvSubst ) import GHC.Core.Map.Type import GHC.Core.Multiplicity @@ -1090,43 +1092,37 @@ the invocation of F! We don't have a function that recursively applies substitutions inside the kinds of variable occurrences (and probably rightly so). -So, we work as follows: +Ugh! This whole thing is pretty horrible. We work as follows: 1. Start with the current substitution (which we are trying to fixpoint [ xs :-> F a (z :: a) (rest :: G a (z :: a)) , a :-> b ] - 2. Take all the free vars of the range of the substitution: - {a, z, rest, b} - NB: the free variable finder closes over - the kinds of variable occurrences + 2. Find its fixed point. Remember, substitution is shallow. That gives: + [ xs :-> F b (z :: a) (rest :: G a (z :: a)) + , a :-> b ] + Note the 'a' lurking inside (z::a) - 3. If none are in the domain of the substitution, stop. - We have found a fixpoint. + 3. Take all the deep free vars of the range of the substitution: + {a, (z::a), (rest::G a z), b} 4. Remove the variables that are bound by the substitution, leaving {z, rest, b} - 5. Do a topo-sort to put them in dependency order: + 5. Do a `scopedSort` to put them in dependency order: [ b :: *, z :: a, rest :: G a z ] - 6. Apply the substitution left-to-right to the kinds of these - tyvars, extending it each time with a new binding, so we - finish up with - [ xs :-> ..as before.. - , a :-> b - , b :-> b :: * + 6. Make a "re-kinding substitution" by applying the fixpoint from (2) + to the kind of each tyvar in this list -- and then applying the + re-kinding from earlier variables. + [ b :-> b :: * , z :-> z :: b , rest :-> rest :: G b (z :: b) ] Note that rest now has the right kind 7. Apply this extended substitution (once) to the range of the - /original/ substitution. (Note that the extended substitution - would go on forever if you tried to find its fixpoint, because it - maps z to z.) - - 8. And go back to step 1 + fixpoint substitution from (2). In Step 6 we use the free vars from Step 2 as the initial in-scope set, because all of those variables appear in the @@ -1136,13 +1132,59 @@ variables in the in-scope set; it is used only to ensure no shadowing. -} -niFixSubst :: InScopeSet -> TvSubstEnv -> Subst +niFixSubst, niFixSubst' :: InScopeSet -> TvSubstEnv -> Subst -- Find the idempotent fixed point of the non-idempotent substitution -- This is surprisingly tricky: -- see Note [Finding the substitution fixpoint] -- ToDo: use laziness instead of iteration? niFixSubst in_scope tenv - | not_fixpoint = niFixSubst in_scope (mapVarEnv (substTy subst) tenv) + = pprTrace "niFixSubst" (vcat [ text "In: " <+> ppr tenv + , text "Out:" <+> ppr (S.getTvSubstEnv subst) ]) + subst + where + subst = niFixSubst' in_scope tenv + +niFixSubst' in_scope orig_tenv + = mkTvSubst in_scope (mapVarEnv (subst_tenv rekinding_tenv) fix_tenv) + where + in_dom tv = tv `elemVarEnv` orig_tenv + + fix_tenv :: TvSubstEnv + fix_tenv = go_fix orig_tenv -- Repeatedly apply tenv until we get a fixpoint + + go_fix tenv_so_far + | anyVarEnv (anyShallowFreeVarsOfType in_dom) tenv_so_far + = go_fix (mapVarEnv (subst_tenv tenv_so_far) tenv_so_far) + | otherwise + = tenv_so_far + + free_tvs :: [TyVar] + free_tvs = scopedSort $ + filterOut in_dom $ + nonDetVarSetElems $ + tyCoVarsOfTypes $ + nonDetEltsUFM orig_tenv + -- It's OK to use nonDetEltsUFM (twice) here because + -- the order of range_tvs is immaterial + + rekinding_tenv = foldl' add_free_tv emptyTvSubstEnv free_tvs + + add_free_tv :: TvSubstEnv -> TyVar -> TvSubstEnv + add_free_tv rke_so_far tv + | isTyVar tv + , let k' = subst_tenv rke_so_far $ + subst_tenv fix_tenv $ + tyVarKind tv + = extendVarEnv rke_so_far tv (mkTyVarTy (setTyVarKind tv k')) + | otherwise + = rke_so_far + + subst_tenv :: TvSubstEnv -> Type -> Type + subst_tenv tenv = substTy (mkTvSubst in_scope tenv) + +{- +-- | not_fixpoint = niFixSubst' in_scope (mapVarEnv (substTy subst) tenv) + | not_fixpoint = niFixSubst' in_scope (mapVarEnv (substTy subst) tenv) | otherwise = tenv_subst where tenv_subst = mkTvSubst in_scope tenv -- This is our starting point @@ -1154,12 +1196,26 @@ niFixSubst in_scope tenv -- It's OK to use nonDetEltsUFM (twice) here because -- the order of range_tvs is immaterial - not_fixpoint = any in_domain range_tvs - in_domain tv = tv `elemVarEnv` tenv - - free_tvs = scopedSort (filterOut in_domain range_tvs) + not_fixpoint = any in_domain range_tvs + in_domain tv = tv `elemVarEnv` tenv + free_tvs = scopedSort (filterOut in_domain range_tvs) -- See Note [Finding the substitution fixpoint], Step 6 + rekinding_tenv = foldl' add_free_tv emptyTvSubstEnv free_tvs + add_free_tv :: TvSubstEnv -> TyVar -> TvSubstEnv + add_free_tv rke tv + | isTyVar tv, anyFreeVarsOfType in_domain (varType tv) + , let tv' = updateTyVarKind (substTy tenv_subst) tv + = extendVarEnv rke tv (mkTyVarTy tv') + | otherwise + = rke + + rekinding_subst = mkTvSubst in_scope rekinding_tenv + + subst = mkTvSubst in_scope (rekinding_tenv `plusVarEnv` + mapVarEnv (substTy rekinding_subst) tenv) + +{- subst = foldl' add_free_tv tenv_subst free_tvs add_free_tv :: Subst -> TyVar -> Subst @@ -1168,6 +1224,8 @@ niFixSubst in_scope tenv | otherwise = subst -- Ignore free coercion variables where tv' = updateTyVarKind (substTy subst) tv +-} +-} {- ************************************************************************ ===================================== compiler/GHC/Tc/Solver/FunDeps.hs ===================================== @@ -29,13 +29,10 @@ import GHC.Core.TyCon import GHC.Core.Unify( AmIUnifying(..) , tcUnifyTysForInjectivity, typeListsAreApart ) import GHC.Core.Coercion.Axiom -import GHC.Core.TyCo.Subst( elemSubst, extendTvSubstWithClone ) import GHC.Builtin.WiredIn.TypeLits( tryInteractTopFam, tryInteractInertFam ) -import GHC.Types.Var( TyVar, tyVarKind, setTyVarKind ) import GHC.Types.Var.Set -import GHC.Types.Var.Env( mkInScopeSet ) import GHC.Utils.Outputable import GHC.Utils.Panic @@ -57,7 +54,6 @@ Here is our plan for dealing with functional dependencies * send the current constraint back to the start of the pipeline; it might now be soluble, and it probably isn't inert - * Once a unification is done, it can't be un-done. So we must be very careful not to threaten completeness by doing a unification when there might be another way to solve the constraint. @@ -709,18 +705,16 @@ getRelevantBranches ax work_args work_rhs , cab_lhs = lhs_tys , cab_rhs = rhs_ty }) | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) work_tys - -- See (RW1)-(RW3) in Note [Relevance] - -- This is a pre-/unification/ check (not matching) - -- so `subst` can bind both `qtvs` and fvs(work_tys) - , let (subst',qtvs') = trim_qtvs subst qtvs - lhs_tys' = substTys subst' lhs_tys - rhs_ty' = substTy subst' rhs_ty - branch_tys' = rhs_ty' : lhs_tys' + -- See (RW1)-(RW3) in Note [Relevance] + , let lhs_tys' = substTys subst lhs_tys , noEarlierMatchCheck lhs_tys' branch -- See (RW4) in Note [Relevance] - = Just (FDEqns { fd_qtvs = qtvs' - , fd_eqs = zipWith Pair branch_tys' work_tys }) - -- Do not apply the substition to `work_tys`; - -- See (CF7) in Note [Exploiting closed type families] + = -- Ignore the substitution, except for `noEarlierMatchCheck` + -- See (CF7) in Note [Exploiting closed type families] + Just (FDEqns { fd_qtvs = qtvs + , fd_eqs = zipWith Pair (rhs_ty:lhs_tys) work_tys }) + -- Do not apply the substitution to `work_tys`; + -- See (CF8) in Note [Exploiting closed type families] + | otherwise = Nothing @@ -738,12 +732,10 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs }) do_one (CoAxBranch { cab_tvs = branch_qtvs , cab_lhs = branch_lhs_tys , cab_rhs = branch_rhs }) - | Just subst <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs] - , let (subst', qtvs') = trim_qtvs subst branch_qtvs - branch_lhs_tys' = substTys subst' branch_lhs_tys - -- No need for an apartness check here; for open type families - -- there are no "preceding" equations - = Just (mkInjectivityFDEqn inj_flags qtvs' branch_lhs_tys' work_args) + | Just {} <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs] + -- No need for an apartness check here; + -- for open type families there are no "preceding" equations + = Just (mkInjectivityFDEqn inj_flags branch_qtvs branch_lhs_tys work_args) | otherwise = Nothing @@ -770,6 +762,9 @@ mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs mk_eqn iargs = mkInjectivityFDEqn inj_flags [] work_args iargs + +{- See Historical Note [trim_qtvs] + trim_qtvs :: Subst -> [TyVar] -> (Subst, [TyVar]) -- Tricky stuff: see (OF1) in Note [Exploiting open type families] trim_qtvs unif_subst qtvs @@ -790,7 +785,7 @@ trim_qtvs unif_subst qtvs subst' = extendTvSubstWithClone subst tv tv' -- extendTvSubstWithClone: see (OF1b) in -- Note [Exploiting open type families] - +-} ----------------------------------------- -- Built-in type families @@ -1104,7 +1099,7 @@ the Injective Type Families paper. For open type families, suppose we have [W] F alpha ~ Int->Int -Then, since F has been checked as injective, and the RHS `a->a` (pre-)unifies +Then, since F has been checked as injective, and the RHS `a->a` (pre-)matches with the `Int->Int` in the Wanted with substitution [a :-> Int], we can add a derived FunDepEqns FDEqns{ fd_qtvs = [], fd_eqs = [ [Int] ~ alpha ] } @@ -1133,36 +1128,44 @@ In general we have: type instance forall qs. F t1 ... tn = rhs [W] F a1 ... an ~ b -Suppose `b` pre-unifies with `rhs`, with substitution `S` that binds -some (but perhaps not all) of the `qs`. Lets say - unbound_qs = qs \ dom(S) - -Then we generate the FunDepEqs - FDEqns{ fd_qtvs = unbound_qs - , fd_eqs = [ S(t1) ~ S(a1), S(t3) ~ S(a3) ] - -These 'fd_qtvs' are subsequently instantiated to fresh metavariables by -'instantiateFunDepEqns'. - -(OF1) The computation of `unbound_qs` is done by `trim_qtvs`, but we - have to be careful. Suppose (OF1a) - qs = [ k::*, a::k ] - S = [ k :-> UnliftedType ] - Then we want - unbound_qs = [ a::UnliftedType ] -- Must apply S to q's kind!! - - And (OF1b), although I'm not sure if this can acutally happen: - qs = [ k::*, a::(k::*), b::(a::k) ] - S = [ k :-> UnliftedType ] - We want - unbound_qs = [ a::UnliftedType, b::(a::UnliftedType) ] - Note that the occurrence of `a` in b's kind should be replaced by the - newly-kinded `a` not the original `a::k`. - - This fancy footwork for `fd_qtvs` is done by `trim_qtvs`. It - * Drops a quantified tyvar that /is/ matched - * Substitutes in the kind of a quantified tyvar `a` that is /not/ matched - * Adds the substitution [a::k :-> a::S(k)] to the substitution +Suppose `b` pre-matches with `rhs`, with substitution `S`. We /ignore/ +`S` and simply generate the FunDepEqs + FDEqns{ fd_qtvs = qs, fd_eqs = [ t1 ~ a1, t3 ~ a3 ] +These 'fd_qtvs' are subsequently instantiated to fresh meta-variables by +'instantiateFunDepEqns'. See Historial Note [trim_qtvs]. + +Historial Note [trim_qtvs] +~~~~~~~~~~~~~~~~~~~~~~~~~~ +In Note [Exploiting open type families], we previously tried to make use of +the matching substitution `S`, by defining + unbound_qs = qs \ dom(S) +so that we could make fresh meta-variables for only the unbound `qs`. +But it turned out to be tricky. Suppose (TQ1) + qs = [ k::*, a::k ] + S = [ k :-> UnliftedType ] +Then we want + unbound_qs = [ a::UnliftedType ] -- Must apply S to q's kind!! + +And (TQ2), although I'm not sure if this can acutally happen: + qs = [ k::*, a::(k::*), b::(a::k) ] + S = [ k :-> UnliftedType ] +We want + unbound_qs = [ a::UnliftedType, b::(a::UnliftedType) ] +Note that the occurrence of `a` in b's kind should be replaced by the +newly-kinded `a` not the original `a::k`. + +This fancy footwork for `fd_qtvs` used to be done by `trim_qtvs`. It + * Drops a quantified tyvar that /is/ matched + * Substitutes in the kind of a quantified tyvar `a` that is /not/ matched + * Adds the substitution [a::k :-> a::S(k)] to the substitution + +(TQ3) This was hard enough for open type type families, which use matching. +But it was ridiculously hard for /closed/ type families which use pre-/unification/ +so that the substitution `S` has both meta-tyvars and instance-head tyvars in +the range. + +It's much easier to drop all this and just make fresh meta-tyvars for +all the quantified type variables. Note [Exploiting closed type families] see `mkTopClosedFamEqFDs` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1194,9 +1197,6 @@ More precisely, here is the very clever Closed Family Fundep Algorithm (CFFA) with fresh unification vars in lhs and rhs for the quantified variables of the equation. - Actually we take advantage of the substitution generated by the relevance - check, see (CF7) below. - See Note [Relevance] for what "relevant" means. We need to take care about non-termination; see (CF3). @@ -1391,13 +1391,29 @@ Key point: equations that are not relevant do not need to be considered for fund (CF7) Relevance. When computing relevance in the Closed Family Fundep Algorithm, for [W] F wlhs1 wlhs2 ~ wrhs, against an equation F lhs1 lhs2 ~ rhs we get back a pre-unifying substitution S that makes the two as equal as poss. - Then we use `trim_qtvs` in exactly the same way as in - (OF1) of Note [Exploiting open type families], to generate qtvs' and S' - Then we generate - FDEqns { fd_qtvs = qtvs', fd_eqs [ S'(lhs1) ~ wlhs1 - , S'(lhs2) ~ wlhs2 - , S'(rhs) ~ wrhs ] } - Notice that we do /not/ apply S' to wlhs/wrhs. These type contain unification + This substitution can bind both /quantified/ type variables from the instance, + and /unification variables/ from the solver. Moreover, both kind of type variable + can appear in the range of the substitution. + + That's pretty complicated, so we are very cautious about using the returned + substitution. I had a long stuggle (#27994) when we were previously trying to + apply that returned substitution as an optimisation. But because type variables + can occur in kinds, it was a losing battle -- I ended up with TyVars from the + instance head escaping into the solver. Consider + + type F :: forall k. k -> * + type instance F k a where + F (k->*) (b::k->*) = Int + + [W] F kappa (alpha::kappa) ~ Int + + We unify the Wanted with the equation to get the subsitution + [ kappa :-> k->*, b :-> alpha ] + I tried using `trim_qtvs` (see Historical Note [trim_qtvs]) but my head hurt + FAR too much: #27994 is an excellent example. I gave up and now just ignore + the substitution, making a fresh meta-var for each quantified variable. + +(CF8) Notice that we do /not/ apply S' to wlhs/wrhs. These type contain unification variables that we must actually unify! Example type family F a b where F [a] (Maybe b) = (a, G b) @@ -1408,7 +1424,6 @@ Key point: equations that are not relevant do not need to be considered for fund FDEqns { fd_qtvs = [b], fd_eqs [ [Int] ~ alpha , Maybe b ~ beta , (Int,G b) ~ (Int, [gamma]) ] } - Notice also that by applying the substitution to the template but not to the Wanted, we uphold the `fd_eqs` invariant in `FDEqns` (see its data type decl) @@ -1517,10 +1532,12 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) rev_eqs = reverse eqs -- (reverse eqs): See Note [Reverse order of fundep equations] + empty_subst = emptySubst + -- If you re-instate `trim_qtvs`, take care here: -- The kinds of `qtvs` can have free variables, because of `trim_qtvs` -- Example: T27828b - empty_subst = mkEmptySubst $ mkInScopeSet $ - tyCoVarsOfTypes (map tyVarKind qtvs) + -- empty_subst = mkEmptySubst $ mkInScopeSet $ + -- tyCoVarsOfTypes (map tyVarKind qtvs) subst_pair subst (Pair ty1 ty2) = Pair (substTy subst' ty1) ty2 ===================================== compiler/GHC/Tc/Utils/Unify.hs ===================================== @@ -61,7 +61,6 @@ import GHC.Prelude import GHC.Hs import GHC.Tc.Errors.Types ( HsCtxt(..) ) -import GHC.Tc.Errors.Ppr ( pprHsCtxt ) import GHC.Tc.Utils.Concrete import GHC.Tc.Utils.Env import GHC.Tc.Utils.Instantiate @@ -2801,17 +2800,15 @@ uType_defer (UE { u_loc = loc, u_defer = ref -- snocBag: see Note [Work-list ordering] in GHC.Tc.Solver.Equality -- Error trace only - -- NB. do *not* call tidyErrCtxt unless tracing is on, - -- because it is hugely expensive (#5631) + -- NB. do *not* call tidyErrCtxt because + -- (a) it is hugely expensive (#5631) and + -- (b) it crashes in some cases because because it reads + -- res_ty too early via FunResCtxt and getCheckExpType ; whenDOptM Opt_D_dump_tc_trace $ - do { ctxt <- getErrCtxt - ; err_ctxt <- tidyErrCtxt emptyTidyEnv ctxt - ; traceTc "utype_defer" $ - vcat ( ppr role - : debugPprType ty1 - : debugPprType ty2 - : map pprHsCtxt err_ctxt ) - ; traceTc "utype_defer2" (ppr co) } + traceTc "utype_defer" $ + vcat [ text "role:" <+> ppr role + , text "ty1:" <+> debugPprType ty1 + , text "ty2:" <+> debugPprType ty2 ] ; return co } ===================================== testsuite/tests/typecheck/should_compile/T27994.hs ===================================== @@ -0,0 +1,48 @@ +{-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FunctionalDependencies #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE TypeFamilies #-} +module T27994 (l2) where + +import Data.Kind (Constraint, Type) + +l1 :: [String] +l1 = ["hello", "world!"] + +l2 :: [Int] +l2 = gmap (HCons length HNil) l1 + +----- + +type HList :: [Type] -> Type +data HList p where + HNil :: HList '[] + HCons :: x -> HList xs -> HList (x:xs) + +type ($$) :: k -> [Type] -> Type +type family s $$ p where + (s :: Type) $$ '[] = s + (s :: Type -> k) $$ (a:b) = s a $$ b + +type Doms :: [Type] -> [Type] +type family Doms t where + Doms '[] = '[] + Doms ((a -> b) : t) = a : Doms t + +type Codoms :: [Type] -> [Type] +type family Codoms t where + Codoms '[] = '[] + Codoms ((a -> b) : t) = b : Codoms t + +type GMap :: k -> [Type] -> Constraint +class GMap (s :: k) t | t -> k where + gmap :: HList t -> s $$ Doms t -> s $$ Codoms t + +instance GMap [] '[a -> b] where + gmap :: HList '[a -> b] -> [a] -> [b] + gmap (HCons f HNil) = go + where + go :: [a] -> [b] + go [] = [] + go (x:xs) = f x : go xs ===================================== testsuite/tests/typecheck/should_compile/T27994a.hs ===================================== @@ -0,0 +1,53 @@ +{-# LANGUAGE DataKinds, TypeFamilies, PolyKinds, GADTs, AllowAmbiguousTypes #-} +module T27994 where + +import Data.Kind (Type) +import Data.Proxy (Proxy) + +type F :: forall{k}. k -> Type -> Type +type family F s x where + F (s :: Type -> k) (Proxy s) = Int + +f :: forall k (s :: k) x. F s x -> x +f _ = undefined + +g :: Proxy Maybe -> () +g _ = () + +data T a where + MkT :: T Int + +test :: T a -> () +test t = (\y -> case t of MkT -> g y) (f (3 :: Int)) + +{- [W] F @k_azX (s_azY::k_azX) p_azN ~ Int + +unify [*->k, s::*->k, Proxy @(*->k) (s::*->k), Int] + ~ [k_azX, s_azY::k_azX, p_azN, Int] + + --> [ k_azX :-> (* -> k) + , s :-> s_azY::k_azX + , p_azN :-> Proxy @(*->k) (s::*->k) ] + + fixpt --> [ k_azX :-> Type -> k + , s :-> s_azY::(Type -> k) + , p_azN :-> Proxy @(Type->k) (s::k_azX) ] + +Generate a [W] for each pair + [W] k ~ k_azX + [W] s ~ s_azY + [W] Proxy s ~ p_azN + [W] Int ~ Int +As an opt (claim) apply subst first + [W] k_azX ~ k_azX + [W] s_azY ~ s_azY + [W] Proxy s_azY ~ p_azN + [W] Int ~ Int + +-- [W] F @k_azX s_azY[tau] p_azN ~ Int +-- Want: fresh k' s' +-- [W] k' ~ k_azX +-- [W] s':k' ~ s_azY:k_azX +-- [W] Proxy s' ~ x +-} + ===================================== testsuite/tests/typecheck/should_compile/all.T ===================================== @@ -972,4 +972,6 @@ test('T27557', normal, compile, ['']) test('T27664', normal, compile, ['']) test('T27916', normal, compile, ['']) test('T26543_orig', normal, compile, ['']) +test('T7994', normal, compile, ['']) +test('T7994a', normal, compile, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/147f46324d4fc0e461e05ad578bd4e3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/147f46324d4fc0e461e05ad578bd4e3... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help