Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

24 changed files:

Changes:

  • changelog.d/T26543
    1
    +section: compiler
    
    2
    +synopsis: Fix a bug in ImpredicativeTypes
    
    3
    +description:
    
    4
    +  The QuickLook algorithm (which implements `ImpredicativeTypes`) was defeated if there
    
    5
    +  was a kind coercion in the types being unified.  That bug is now fixed.
    
    6
    +mrs: !16566
    
    7
    +issues: #26543

  • compiler/GHC/Core/FVs.hs
    ... ... @@ -223,11 +223,11 @@ addCoreBndrsFV bndrs fv = foldr addCoreBndrFV fv bndrs
    223 223
     unitFV :: Var -> SelectiveDFV
    
    224 224
     -- Deals with an occurrence
    
    225 225
     -- Shallow: does not look at the kind
    
    226
    -unitFV v = MkFV (\bvs -> EndoOS (do_it bvs))
    
    226
    +unitFV v = MkFV (\env -> EndoOS (do_it env))
    
    227 227
       where
    
    228
    -    do_it (is_interesting,bvs) acc
    
    228
    +    do_it (bvs, is_interesting) acc
    
    229
    +      | v `elemVarSet` bvs     = acc  -- First exclude the bound vars
    
    229 230
           | not (is_interesting v) = acc  -- The "selective" bit
    
    230
    -      | v `elemVarSet` bvs     = acc
    
    231 231
           | v `elemDVarSet` acc    = acc
    
    232 232
           | otherwise              = acc `extendDVarSet` v
    
    233 233
     
    

  • compiler/GHC/Core/TyCo/FVs.hs
    ... ... @@ -278,14 +278,15 @@ deepUnitFV :: (Type -> TyCoFV) -> TyCoVar -> TyCoFV
    278 278
     -- Deal with a single TyCoVar
    
    279 279
     -- Takes a function to find free vars of the kind
    
    280 280
     -- See Note [Computing deep free variables]
    
    281
    -deepUnitFV fvs_of_kind v
    
    282
    -  = MkFV (\bvs -> EndoOS (do_it bvs))
    
    281
    +{-# INLINE deepUnitFV #-}   -- INLINE to specialise on fvs_of_kind
    
    282
    +deepUnitFV fvs_of_kind
    
    283
    +  = \v -> MkFV (\bvs -> EndoOS (do_it bvs v))
    
    283 284
       where
    
    284
    -    do_it :: BoundVars -> TyCoVarSet -> TyCoVarSet
    
    285
    -    do_it bvs acc | v `elemVarSet` bvs = acc
    
    286
    -                  | v `elemVarSet` acc = acc
    
    287
    -                  | otherwise          = runFVAcc (fvs_of_kind (varType v)) acc
    
    288
    -                                         `extendVarSet` v
    
    285
    +    do_it :: BoundVars -> TyCoVar -> TyCoVarSet -> TyCoVarSet
    
    286
    +    do_it bvs v acc | v `elemVarSet` bvs = acc
    
    287
    +                    | v `elemVarSet` acc = acc
    
    288
    +                    | otherwise          = runFVAcc (fvs_of_kind (varType v)) acc
    
    289
    +                                           `extendVarSet` v
    
    289 290
                       -- Left-to-right: add the kind variables to the
    
    290 291
                       --                accumulator before v itself
    
    291 292
     
    
    ... ... @@ -443,11 +444,11 @@ selectiveTcvFolder
    443 444
                    , tcf_hole  = do_hole, tcf_lit = const mempty
    
    444 445
                    , tcf_tycobinder = addBndrSelectiveFV }
    
    445 446
       where
    
    446
    -    do_tcv v = MkFV (\bvs -> EndoOS (do_it bvs))
    
    447
    +    do_tcv v = MkFV (\env -> EndoOS (do_it env))
    
    447 448
           where
    
    448
    -        do_it (is_interesting,bvs) acc
    
    449
    +        do_it (bvs,is_interesting) acc
    
    450
    +          | v `elemVarSet` bvs     = acc  -- First exclude bound variables
    
    449 451
               | not (is_interesting v) = acc  -- The "selective" bit
    
    450
    -          | v `elemVarSet` bvs     = acc
    
    451 452
               | v `elemDVarSet` acc    = acc
    
    452 453
               | otherwise              = acc `extendDVarSet` v
    
    453 454
     
    
    ... ... @@ -848,43 +849,77 @@ invisibleVarsOfTypes = foldr (unionVarSet . invisibleVarsOfType) emptyVarSet
    848 849
     *                                                                      *
    
    849 850
     ********************************************************************* -}
    
    850 851
     
    
    851
    -{-# INLINE afvFolder #-}   -- so that specialization to (const True) works
    
    852
    -afvFolder :: (TyCoVar -> Bool) -> TyCoFolder (FV TyCoVarSet DM.Any)
    
    852
    +{- Note [Any-free-var folder]
    
    853
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    854
    +The "any-free-var" folders take a `check_fv` predicate (TyCoVar -> Bool) and
    
    855
    +check if any /deep/ free variable of the type/coercion satisfies it. So
    
    856
    +
    
    857
    +   anyFreeVarsOfType p ty = anyVarSet p (tyCoVarsOfType ty)
    
    858
    +
    
    859
    +Our main folders look like:
    
    860
    +
    
    861
    +   afv_type :: Type -> FV (BoundVars, TyVar -> Bool) Any
    
    862
    +
    
    863
    +where we cleverly pair the predicate with the forall-bound variables of the type
    
    864
    +in the "env" field of (FV env Any).  This way we can build the /folder/ once and
    
    865
    +for all (independent of the `check_fv` predicate), and the loop passes down
    
    866
    +the pair.
    
    867
    +
    
    868
    +The same idiom is used in `exprFreeVarsDSet` and friends.
    
    869
    +
    
    870
    +However we are careful to INLINE `atvFolder` and `runAny` so that we get those
    
    871
    +nice tight loops.
    
    872
    +-}
    
    873
    +
    
    874
    +{-# INLINE afvFolder #-}   -- See Note [Any-free-var folder]
    
    875
    +afvFolder :: TyCoFolder (FV (BoundVars, TyCoVar -> Bool) DM.Any)
    
    853 876
     -- 'afvFolder' is short for "any-free-var folder", good for checking
    
    854
    --- if any free var of a type satisfies a predicate `check_fv`
    
    855
    -afvFolder check_fv = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    856
    -                                , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    857
    -                                , tcf_hole = do_hole, tcf_lit = const mempty
    
    858
    -                                , tcf_tycobinder = addBndrFV }
    
    877
    +--   if any /deep/ free var of a type satisfies a predicate `check_fv`
    
    878
    +-- The "env" of the (FV env Any) is a pair (bvs,pred) of
    
    879
    +--    the bound variables (which are definitely not free), and
    
    880
    +--    an "interesting var" predicate which the client supplies
    
    881
    +afvFolder = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    882
    +                       , tcf_tyvar = do_tcv
    
    883
    +                       , tcf_covar = do_tcv
    
    884
    +                       , tcf_hole  = do_hole
    
    885
    +                       , tcf_lit   = const mempty
    
    886
    +                       , tcf_tycobinder = addBndrSelectiveFV }
    
    859 887
       where
    
    860
    -    do_tcv tv = MkFV $ \ bvs ->
    
    861
    -                Any (not (tv `elemVarSet` bvs) && check_fv tv)
    
    862
    -    do_hole _ = mempty    -- I'm unsure; probably never happens
    
    888
    +    -- Check the variable itself with check_fv,
    
    889
    +    -- and check its kind (this is a deep folder)
    
    890
    +    do_tcv tcv = MkFV $ \ (bvs,check_fv) ->
    
    891
    +                 Any (not (tcv `elemVarSet` bvs) &&
    
    892
    +                      (check_fv tcv || runAny check_fv (afv_type (varType tcv))))
    
    893
    +    do_hole hole = do_tcv (coHoleCoVar hole)
    
    894
    +
    
    895
    +afv_type  :: Type     -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    896
    +afv_types :: [Type]   -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    897
    +afv_co    :: Coercion -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    898
    +(afv_type, afv_types, afv_co, _) = foldTyCo afvFolder
    
    899
    +
    
    900
    +runAny :: (TyVar -> Bool) -> FV (BoundVars, TyCoVar -> Bool) DM.Any -> Bool
    
    901
    +{-# INLINE runAny #-}
    
    902
    +runAny check_fv fvs = DM.getAny (runFV fvs (emptyVarSet, check_fv))
    
    863 903
     
    
    864 904
     anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool
    
    865
    -anyFreeVarsOfType check_fv ty = DM.getAny (runFVTop (f ty))
    
    866
    -  where (f, _, _, _) = foldTyCo (afvFolder check_fv)
    
    905
    +{-# INLINE anyFreeVarsOfType #-}
    
    906
    +anyFreeVarsOfType check_fv ty = runAny check_fv (afv_type ty)
    
    867 907
     
    
    868 908
     anyFreeVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> Bool
    
    869
    -anyFreeVarsOfTypes check_fv tys = DM.getAny (runFVTop (f tys))
    
    870
    -  where (_, f, _, _) = foldTyCo (afvFolder check_fv)
    
    909
    +{-# INLINE anyFreeVarsOfTypes #-}
    
    910
    +anyFreeVarsOfTypes check_fv tys = runAny check_fv (afv_types tys)
    
    871 911
     
    
    872 912
     anyFreeVarsOfCo :: (TyCoVar -> Bool) -> Coercion -> Bool
    
    873
    -anyFreeVarsOfCo check_fv co = DM.getAny (runFVTop (f co))
    
    874
    -  where (_, _, f, _) = foldTyCo (afvFolder check_fv)
    
    913
    +anyFreeVarsOfCo check_fv co = runAny check_fv (afv_co co)
    
    875 914
     
    
    876 915
     noFreeVarsOfType :: Type -> Bool
    
    877
    -noFreeVarsOfType ty = not $ DM.getAny (runFVTop (f ty))
    
    878
    -  where (f, _, _, _) = foldTyCo (afvFolder (const True))
    
    916
    +noFreeVarsOfType ty = not $ runAny (const True) (afv_type ty)
    
    879 917
     
    
    880 918
     noFreeVarsOfTypes :: [Type] -> Bool
    
    881
    -noFreeVarsOfTypes tys = not $ DM.getAny (runFVTop (f tys))
    
    882
    -  where (_, f, _, _) = foldTyCo (afvFolder (const True))
    
    919
    +noFreeVarsOfTypes tys = not $ runAny (const True) (afv_types tys)
    
    883 920
     
    
    884 921
     noFreeVarsOfCo :: Coercion -> Bool
    
    885
    -noFreeVarsOfCo co = not $ DM.getAny (runFVTop (f co))
    
    886
    -  where (_, _, f, _) = foldTyCo (afvFolder (const True))
    
    887
    -
    
    922
    +noFreeVarsOfCo co = not $ runAny (const True) (afv_co co)
    
    888 923
     
    
    889 924
     {-
    
    890 925
     ************************************************************************
    

  • compiler/GHC/Runtime/Heap/Inspect.hs
    ... ... @@ -628,18 +628,16 @@ be sigmas and not taus.
    628 628
     
    
    629 629
     {- Note [RuntimeUnkTv]
    
    630 630
     ~~~~~~~~~~~~~~~~~~~~~~
    
    631
    -In the GHCi debugger we use unification variables whose MetaInfo is
    
    632
    -RuntimeUnkTv.  The special property of a RuntimeUnkTv is that it can
    
    633
    -unify with a polytype (see GHC.Tc.Solver.Equality.checkTypeEqNoUnification).
    
    634
    -If we don't do this `:print <term>` will fail if the type of <term>
    
    635
    -has nested `forall`s or `=>`s.
    
    636
    -
    
    637
    -This is because the GHCi debugger's internals will attempt to unify a
    
    638
    -metavariable with the type of <term> and then display the result, but
    
    639
    -if the type has nested `forall`s or `=>`s, then unification will fail
    
    640
    -unless we do something special.  As a result, `:print` will bail out
    
    641
    -and the unhelpful result will be `<term> = (_t1::t1)` (where `t1` is a
    
    642
    -metavariable).
    
    631
    +In the GHCi debugger we use unification variables whose MetaInfo is RuntimeUnkTv.
    
    632
    +The special property of a RuntimeUnkTv is that it can unify with a polytype (see
    
    633
    +GHC.Tc.Utils.Unify.simpleUnifyCheck).  If we don't do this `:print <term>` will fail if
    
    634
    +the type of <term> has nested `forall`s or `=>`s.
    
    635
    +
    
    636
    +This is because the GHCi debugger's internals will attempt to unify a metavariable
    
    637
    +with the type of <term> and then display the result, but if the type has nested
    
    638
    +`forall`s or `=>`s, then unification will fail unless we do something special.  As
    
    639
    +a result, `:print` will bail out and the unhelpful result will be `<term> =
    
    640
    +(_t1::t1)` (where `t1` is a metavariable).
    
    643 641
     
    
    644 642
     Beware: <term> can have nested `forall`s even if its definition doesn't use
    
    645 643
     RankNTypes! Here is an example from #14828:
    
    ... ... @@ -654,13 +652,18 @@ We could envision deeply instantiating this type to get the type
    654 652
     `forall f a b. Functor f => (a -> b) -> f a -> f b`,
    
    655 653
     but this trick wouldn't work for higher-rank types.
    
    656 654
     
    
    657
    -Instead, we adopt a simpler fix: allow RuntimeUnkTv to unify with a
    
    658
    -polytype (specifically, see ghci_tv in GHC.Tc.Utils.Unify.preCheck).
    
    659
    -This allows metavariables to unify with types that have
    
    660
    -nested (or higher-rank) `forall`s/`=>`s, which makes `:print fmap`
    
    661
    -display as
    
    662
    -`fmap = (_t1::forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b)`,
    
    655
    +Instead, we adopt a simpler fix: allow RuntimeUnkTv to unify with a polytype
    
    656
    +(specifically, see ghci_tv in GHC.Tc.Utils.Unify.preCheck).  This allows
    
    657
    +metavariables to unify with types that have nested (or higher-rank)
    
    658
    +`forall`s/`=>`s, which makes `:print fmap` display as
    
    659
    +  `fmap = (_t1::forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b)`,
    
    663 660
     as expected.
    
    661
    +
    
    662
    +(RUT1) The debugger uses the "on-the-fly" unifier, namely
    
    663
    +          GHC.Tc.Utils.Unify.unifyType
    
    664
    +   and /ignores/ any constraints it generates: see `addConstraint`. It never
    
    665
    +   calls the constraint solver, so the constraint solver does not need to
    
    666
    +   understand about RuntimeUnkTvs.
    
    664 667
     -}
    
    665 668
     
    
    666 669
     
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -37,6 +37,7 @@ import GHC.Core.ConLike ( ConLike(..) )
    37 37
     import GHC.Core.DataCon ( dataConConcreteTyVars, isNewDataCon, dataConOrigArgTys )
    
    38 38
     import GHC.Core.TyCon
    
    39 39
     import GHC.Core.TyCo.Rep
    
    40
    +import GHC.Core.TyCo.FVs
    
    40 41
     import GHC.Core.TyCo.Ppr
    
    41 42
     import GHC.Core.TyCo.Subst ( substTyWithInScope )
    
    42 43
     import GHC.Core.Type
    
    ... ... @@ -631,11 +632,13 @@ tcInstFun :: QLFlag
    631 632
     -- plus the modification in Fig 5, of the QL paper:
    
    632 633
     -- "A quick look at impredicativity" (ICFP'20).
    
    633 634
     tcInstFun do_ql inst_final rn_head@(_, fun_lspan) tc_fun fun_sigma rn_args
    
    634
    -  = do { traceTc "tcInstFun" (vcat [ text "tc_fun" <+> ppr tc_fun
    
    635
    +  = do { lvl <- getTcLevel
    
    636
    +       ; traceTc "tcInstFun" (vcat [ text "tc_fun" <+> ppr tc_fun
    
    635 637
                                        , text "rn_fun" <+> ppr rn_head
    
    636 638
                                        , text "fun_sigma" <+> ppr fun_sigma
    
    637 639
                                        , text "args:" <+> ppr rn_args
    
    638
    -                                   , text "do_ql" <+> ppr do_ql])
    
    640
    +                                   , text "do_ql" <+> ppr do_ql
    
    641
    +                                   , text "lvl:" <+> ppr lvl ])
    
    639 642
            ; fun_origin <- mk_origin rn_head
    
    640 643
            ; res@(_, fun_ty) <- go fun_origin 1 [] fun_sigma rn_args
    
    641 644
            ; traceTc "tcInstFun:ret" (ppr fun_ty)
    
    ... ... @@ -1377,13 +1380,15 @@ tc_inst_forall_arg conc_tvs (tvb, inner_ty) hs_ty
    1377 1380
                    -- is not fully zonked, because ty_arg is fully zonked.
    
    1378 1381
                    -- See Note [Type application substitution].
    
    1379 1382
     
    
    1383
    +       ; lvl <- getTcLevel
    
    1380 1384
            ; traceTc "tc_inst_forall_arg (VTA/VDQ)" (
    
    1381 1385
                       vcat [ text "fun_ty" <+> ppr fun_ty
    
    1382 1386
                            , text "tv" <+> ppr tv <+> dcolon <+> debugPprType kind
    
    1383 1387
                            , text "ty_arg" <+> debugPprType ty_arg <+> dcolon
    
    1384 1388
                                            <+> debugPprType (typeKind ty_arg)
    
    1385 1389
                            , text "inner_ty" <+> debugPprType inner_ty
    
    1386
    -                       , text "insted_ty" <+> debugPprType insted_ty ])
    
    1390
    +                       , text "insted_ty" <+> debugPprType insted_ty
    
    1391
    +                       , text "lvl:" <+> ppr lvl ])
    
    1387 1392
            ; return (ty_arg, insted_ty) }
    
    1388 1393
     
    
    1389 1394
     {- Note [Visible type application and abstraction]
    
    ... ... @@ -1934,11 +1939,13 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_
    1934 1939
              -- capture and save it in the `EValArgQL`.  See (QLA6) in
    
    1935 1940
              -- Note [Quick Look at value arguments]
    
    1936 1941
     
    
    1942
    +       ; lvl <- getTcLevel
    
    1937 1943
            ; traceTc "quickLookArg {" $
    
    1938 1944
              vcat [ text "arg:" <+> ppr arg
    
    1939 1945
                   , text "orig_arg_rho:" <+> ppr orig_arg_rho
    
    1940 1946
                   , text "head:" <+> ppr rn_fun_arg <+> dcolon <+> ppr mb_fun_ty
    
    1941
    -              , text "args:" <+> ppr rn_args ]
    
    1947
    +              , text "args:" <+> ppr rn_args
    
    1948
    +              , text "level:" <+> ppr lvl ]
    
    1942 1949
     
    
    1943 1950
            ; case mb_fun_ty of {
    
    1944 1951
                Nothing -> skipQuickLook app_lspan larg sc_arg_ty ;    -- fun is too complicated
    
    ... ... @@ -2048,6 +2055,8 @@ qlMonoHsWrapper (WpTyApp ty) = qlMonoTcType ty
    2048 2055
     qlMonoHsWrapper _                 = return ()
    
    2049 2056
     
    
    2050 2057
     qlMonoTcType :: TcType -> ZonkM ()
    
    2058
    +-- Monomorphise any free instantiation variables of `ty`
    
    2059
    +-- This functions zonks as it goes
    
    2051 2060
     -- See Note [Monomorphise instantiation variables]
    
    2052 2061
     qlMonoTcType ty
    
    2053 2062
       = do { traceZonk "monomorphiseQLInstVars {" (ppr ty)
    
    ... ... @@ -2152,8 +2161,7 @@ foldQLInstVars check_tv ty
    2152 2161
     
    
    2153 2162
     qlUnify ::  TcType -> TcType -> TcM ()
    
    2154 2163
     -- Unify ty1 with ty2:
    
    2155
    ---   * It can unify both instantiation variables (possibly with polytypes),
    
    2156
    ---     and ordinary unification variables (but only with monotypes)
    
    2164
    +--   * It unifies /only/ instantiation variables (see (UQL6)), hopefully with polytypes
    
    2157 2165
     --   * It does not return a coercion (unlike unifyType); it is called
    
    2158 2166
     --     for the sole purpose of unifying instantiation variables, although it
    
    2159 2167
     --     may also (opportunistically) unify regular unification variables.
    
    ... ... @@ -2161,18 +2169,23 @@ qlUnify :: TcType -> TcType -> TcM ()
    2161 2169
     --   * It may return without having made the argument types equal, of course;
    
    2162 2170
     --     it just makes best efforts.
    
    2163 2171
     qlUnify ty1 ty2
    
    2164
    -  = do { traceTc "qlUnify" (ppr ty1 $$ ppr ty2)
    
    2172
    +  = do { lvl <- getTcLevel
    
    2173
    +       ; traceTc "qlUnify" (ppr lvl $$ ppr ty1 $$ ppr ty2)
    
    2165 2174
            ; go ty1 ty2 }
    
    2166 2175
       where
    
    2167 2176
         go :: TcType -> TcType -> TcM ()
    
    2168 2177
     
    
    2178
    +    go t1 t2 = do { traceTc "qlUinfy:go" (ppr t1 <+> char '~' <+> ppr t2)
    
    2179
    +                  ; go' t1 t2 }
    
    2180
    +
    
    2169 2181
         -- Decompose (arg1 -> res1) ~ (arg2 -> res2)
    
    2170 2182
         -- and         (c1 => res1) ~   (c2 => res2)
    
    2171 2183
         -- But for the latter we only learn instantiation info from res1~res2
    
    2172
    -    go (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
    
    2184
    +    go' (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
    
    2173 2185
            (FunTy { ft_af = af2, ft_arg = arg2, ft_res = res2 })
    
    2174 2186
           | af1 == af2 -- Match the arrow TyCon
    
    2175
    -      = do { when (isVisibleFunArg af1) (go arg1 arg2)
    
    2187
    +      = do { traceTc "go_fun" (ppr arg1 $$ ppr arg2)
    
    2188
    +           ; when (isVisibleFunArg af1) (go arg1 arg2)
    
    2176 2189
     
    
    2177 2190
             -- NB: we do not unify the multiplicities; that would be too strong.
    
    2178 2191
             -- We might only require mult1 ⩽ mult2, as in Note [Multiplicity in deep subsumption].
    
    ... ... @@ -2181,30 +2194,29 @@ qlUnify ty1 ty2
    2181 2194
                ; go res1 res2 }
    
    2182 2195
     
    
    2183 2196
         -- Make sure to not unify "kappa := (a %1 -> b)". See (UQL5).
    
    2184
    -    go (FunTy { ft_mult = OneTy }) _ = return ()
    
    2185
    -    go _ (FunTy { ft_mult = OneTy }) = return ()
    
    2186
    -      -- NB: we do want to be able to unify "kappa := a => b", as that's
    
    2187
    -      -- the main point of QuickLook (allowing meta-variables to be unified
    
    2188
    -      -- with qualified types).
    
    2197
    +    -- NB: we /do/ want to be able to unify "kappa := a => b", as that's
    
    2198
    +    -- the main point of QuickLook (allowing meta-variables to be unified
    
    2199
    +    -- with qualified types).
    
    2200
    +    go' (FunTy { ft_mult = OneTy }) _ = return ()
    
    2201
    +    go' _ (FunTy { ft_mult = OneTy }) = return ()
    
    2189 2202
     
    
    2190
    -    go (TyVarTy tv) ty2
    
    2191
    -      | isMetaTyVar tv = go_kappa tv ty2
    
    2192
    -    go ty1 (TyVarTy tv)
    
    2193
    -      | isMetaTyVar tv = go_kappa tv ty1
    
    2203
    +    -- Type variables; see (UQL6) in Note [QuickLook unification]
    
    2204
    +    go' (TyVarTy tv) ty2 | isMetaTyVar tv = go_meta tv ty2
    
    2205
    +    go' ty1 (TyVarTy tv) | isMetaTyVar tv = go_meta tv ty1
    
    2194 2206
     
    
    2195
    -    go (CastTy ty1 _) ty2 = go ty1 ty2
    
    2196
    -    go ty1 (CastTy ty2 _) = go ty1 ty2
    
    2207
    +    go' (CastTy ty1 _) ty2 = go ty1 ty2
    
    2208
    +    go' ty1 (CastTy ty2 _) = go ty1 ty2
    
    2197 2209
     
    
    2198
    -    go (TyConApp tc1 []) (TyConApp tc2 [])
    
    2210
    +    go' (TyConApp tc1 []) (TyConApp tc2 [])
    
    2199 2211
           | tc1 == tc2 -- See GHC.Tc.Utils.Unify
    
    2200 2212
           = return ()  -- Note [Expanding synonyms during unification]
    
    2201 2213
     
    
    2202 2214
         -- Now, and only now, expand synonyms
    
    2203
    -    go rho1 rho2
    
    2215
    +    go' rho1 rho2
    
    2204 2216
           | Just rho1 <- coreView rho1 = go rho1 rho2
    
    2205 2217
           | Just rho2 <- coreView rho2 = go rho1 rho2
    
    2206 2218
     
    
    2207
    -    go (TyConApp tc1 tys1) (TyConApp tc2 tys2)
    
    2219
    +    go' (TyConApp tc1 tys1) (TyConApp tc2 tys2)
    
    2208 2220
           | tc1 == tc2
    
    2209 2221
           , not (isTypeFamilyTyCon tc1)
    
    2210 2222
           , tys1 `equalLength` tys2
    
    ... ... @@ -2212,67 +2224,79 @@ qlUnify ty1 ty2
    2212 2224
     
    
    2213 2225
         -- Don't allow unifying (a => b) with the AppTy 'arr[tau] a b'.
    
    2214 2226
         -- To ensure this, use 'tcSplitAppTyNoView_maybe' which does not split (=>).
    
    2215
    -    go (AppTy t1a t1b) ty2
    
    2227
    +    go' (AppTy t1a t1b) ty2
    
    2216 2228
           | Just (t2a, t2b) <- tcSplitAppTyNoView_maybe ty2
    
    2217 2229
           = do { go t1a t2a; go t1b t2b }
    
    2218
    -    go ty1 (AppTy t2a t2b)
    
    2230
    +    go' ty1 (AppTy t2a t2b)
    
    2219 2231
           | Just (t1a, t1b) <- tcSplitAppTyNoView_maybe ty1
    
    2220 2232
           = do { go t1a t2a; go t1b t2b }
    
    2221 2233
     
    
    2222
    -    go _ _ = return ()
    
    2234
    +    -- Type families have a monomorphic result, so if we see (say)
    
    2235
    +    --     Maybe kappa ~ F ty
    
    2236
    +    -- we know that kappa must be a monotype
    
    2237
    +    go' (TyConApp tc1 _) ty2 | isTypeFamilyTyCon tc1 = monomorphise ty2
    
    2238
    +    go' ty1 (TyConApp tc2 _) | isTypeFamilyTyCon tc2 = monomorphise ty1
    
    2239
    +
    
    2240
    +    go' _ _ = return ()
    
    2223 2241
            -- Don't look under foralls; see (UQL4) of Note [QuickLook unification]
    
    2224 2242
     
    
    2225 2243
         ----------------
    
    2226
    -    go_kappa kappa ty2
    
    2227
    -      = assertPpr (isMetaTyVar kappa) (ppr kappa) $
    
    2228
    -        do { info <- readMetaTyVar kappa
    
    2229
    -           ; case info of
    
    2244
    +    go_meta tv1 ty2
    
    2245
    +      = assertPpr (isMetaTyVar tv1) (ppr tv1) $
    
    2246
    +        do { info <- readMetaTyVar tv1
    
    2247
    +           ; case info of  -- See (UQL9)
    
    2230 2248
                    Indirect ty1 -> go ty1 ty2
    
    2231 2249
                    Flexi        -> do { ty2 <- liftZonkM $ zonkTcType ty2
    
    2232
    -                                  ; go_flexi kappa ty2 } }
    
    2250
    +                                  ; go_flexi tv1 ty2 } }
    
    2233 2251
     
    
    2234 2252
         ----------------
    
    2235
    -    -- Swap (kappa1[conc] ~ kappa2[tau])
    
    2236
    -    -- otherwise we'll fail to unify and emit a coercion.
    
    2237
    -    -- Just an optimisation: emitting a coercion is fine
    
    2238
    -    go_flexi kappa (TyVarTy tv2)
    
    2239
    -      | lhsPriority tv2 > lhsPriority kappa
    
    2240
    -      = go_flexi1 tv2 (TyVarTy kappa)
    
    2241
    -    go_flexi kappa ty2
    
    2242
    -      = go_flexi1 kappa ty2
    
    2243
    -
    
    2244
    -    go_flexi1 kappa ty2  -- ty2 is zonked
    
    2245
    -      = do { cur_lvl <- getTcLevel
    
    2246
    -              -- See Note [Unification preconditions], (UNTOUCHABLE) wrinkles
    
    2247
    -              -- Here we are in the TcM monad, which does not track enclosing
    
    2248
    -              -- Given equalities; so for quick-look unification we conservatively
    
    2249
    -              -- treat /any/ level outside this one as untouchable. Hence cur_lvl.
    
    2250
    -           ; case simpleUnifyCheck UC_QuickLook cur_lvl kappa ty2 of
    
    2251
    -              SUC_CanUnify ->
    
    2252
    -                do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
    
    2253
    -                           -- unifyKind: see (UQL2) in Note [QuickLook unification]
    
    2254
    -                           --            and (MIV2) in Note [Monomorphise instantiation variables]
    
    2255
    -                   ; let ty2' = mkCastTy ty2 co
    
    2256
    -                   ; traceTc "qlUnify:update" $
    
    2257
    -                     ppr kappa <+> text ":=" <+> ppr ty2
    
    2258
    -                   ; liftZonkM $ writeMetaTyVar kappa ty2' }
    
    2259
    -              _ -> return () -- e.g. occurs-check or forall-bound variable
    
    2260
    -           }
    
    2253
    +    -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
    
    2254
    +    -- Zonking ty2 makes the occurs-check easier
    
    2255
    +    go_flexi tv1 ty2
    
    2256
    +      | TyVarTy tv2 <- ty2
    
    2257
    +      , lhsPriority tv2 > lhsPriority tv1
    
    2258
    +      = go_flexi1 tv2 (TyVarTy tv1)
    
    2259
    +      | otherwise
    
    2260
    +      = go_flexi1 tv1 ty2
    
    2261
    +
    
    2262
    +    -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
    
    2263
    +    go_flexi1 tv1 ty2
    
    2264
    +      | isQLInstTyVar tv1 = go_kappa tv1 ty2
    
    2265
    +      | otherwise         = monomorphise ty2
    
    2266
    +
    
    2267
    +    go_kappa kappa ty2
    
    2268
    +      | isConcreteTyVar kappa  -- See (UQL7) in Note [QuickLook unification]
    
    2269
    +      = return ()
    
    2270
    +      | anyFreeVarsOfType (== kappa) ty2
    
    2271
    +      = return ()  -- Occurs check.  See (UQL10) in Note [QuickLook unification]
    
    2272
    +      | otherwise
    
    2273
    +      = do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
    
    2274
    +                   -- unifyKind: see (UQL2) in Note [QuickLook unification]
    
    2275
    +                   --            and (MIV2) in Note [Monomorphise instantiation variables]
    
    2276
    +           ; let ty2' = mkCastTy ty2 co
    
    2277
    +           ; traceTc "qlUnify:update" $
    
    2278
    +             ppr kappa <+> text ":=" <+> ppr ty2
    
    2279
    +           ; liftZonkM $ writeMetaTyVar kappa ty2'
    
    2280
    +           ; return () }
    
    2261 2281
           where
    
    2262 2282
             kappa_kind = tyVarKind kappa
    
    2263 2283
             ty2_kind   = typeKind ty2
    
    2264 2284
     
    
    2285
    +    monomorphise :: TcType -> TcM ()
    
    2286
    +    monomorphise ty  -- See (UQL8)
    
    2287
    +      = do { _ <- liftZonkM $ qlMonoTcType ty; return () }
    
    2288
    +
    
    2265 2289
     {- Note [QuickLook unification]
    
    2266 2290
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2267 2291
     In qlUnify, if we find (kappa ~ ty), we are going to update kappa := ty.
    
    2268 2292
     That is the entire point of qlUnify!   Wrinkles:
    
    2269 2293
     
    
    2270
    -(UQL1) Before unifying an instantiation variable in `go_flexi`, we must check
    
    2271
    -  the usual unification conditions, by calling `GHC.Tc.Utils.Unify.simpleUnifyCheck`.
    
    2272
    -  For example that checks for
    
    2294
    +(UQL1) Before unifying an instantiation variable in `go_kappa`, we must check
    
    2295
    +  the usual unification conditions. In particular:
    
    2273 2296
         * An occurs-check
    
    2274
    -    * Level mis-match
    
    2275 2297
         * An attempt to unify a concrete type variable with a non-concrete type.
    
    2298
    +  We don't need a level-check because the level of an instantiation variable
    
    2299
    +  is infinity -- see (UQL3).
    
    2276 2300
     
    
    2277 2301
     (UQL2) What if kappa and ty have different kinds?  We simply call the
    
    2278 2302
       ordinary unifier and use the coercion to connect the two.
    
    ... ... @@ -2345,6 +2369,47 @@ That is the entire point of qlUnify! Wrinkles:
    2345 2369
       constraint arrows =>, so that (UQL4) applies to them as well.
    
    2346 2370
       See Note [Multiplicity in deep subsumption].
    
    2347 2371
     
    
    2372
    +(UQL6) What if `qlUnify` sees a regular (monotyped) unification variable, `alpha`, rather
    
    2373
    +  than an instantiation variable `kappa`?  Thus (alpha ~ ty).   It's tempting to just
    
    2374
    +  unify it, but the regular, on-the-fly unifier has lots of careful checks (levels,
    
    2375
    +  concreteness etc).  In regular unification it doesn't matter whether we unify on-the-fly
    
    2376
    +  or later -- it's just an efficiency issue -- but we must maintain all the careful invariants.
    
    2377
    +
    
    2378
    +  The simplest thing is to say that `qlUnify` unifies /only/ instantiation variables
    
    2379
    +  (see calls to `isQLInstTyVar`).  We could be more ambitious, perhaps, in future, but
    
    2380
    +  beware of accidentally causing obscure /user-visible/ consequences (#26543),
    
    2381
    +  e.g. via Note [Occurs check and coercions].
    
    2382
    +
    
    2383
    +  Remember, though, that the /only/ reason for QuickLoook is to unify instantiation variables
    
    2384
    +  with polytypes; anything else is optional, and will be picked up with later "ordinary"
    
    2385
    +  typechecking.  So unifing only instantiation variables is enough.
    
    2386
    +
    
    2387
    +(UQL7) If `qlUnify` sees a concrete `kaapa`, thus
    
    2388
    +            kappa[conc] ~ some-type
    
    2389
    +  we just give up. It's tiresome to do all the checks for concreteness; and concrete
    
    2390
    +  type variables are never unified with polytypes, so it's fine to leave it for the
    
    2391
    +  regular unifier.
    
    2392
    +
    
    2393
    +(UQL8) If we see (Maybe kappa ~ alpha), where `kappa` is an instantiation variable
    
    2394
    +  and `alpha` is a regular unification variable, we know that `kappa` must be a
    
    2395
    +  monotype, so we can use `qlMonoTcType` to monomorphise it.  This is important,
    
    2396
    +  because rule APP-LIGHTNING in the QuickLook paper only fires when there are no free
    
    2397
    +  instantiation variables.  Unifying `kappa` with a monotype makes it more possible
    
    2398
    +  for APP-LIGHTNING to fire.
    
    2399
    +
    
    2400
    +  Same if we see (Maybe kappa ~ F tya) where F is a type function, becuase the result
    
    2401
    +  of a type function is always a monotype.
    
    2402
    +
    
    2403
    +(UQL9) If we have (Maybe kappa ~ alpha) and we have /already/ unified alpha:=Maybe beta,
    
    2404
    +  then it's more efficient to look through that unification and decompose, so that
    
    2405
    +  we get (kappa ~ alpha).  That way we don't have to monomorphise `kappa`.  So we
    
    2406
    +  "look through" all meta-tyvars, not just the instantiation vars.
    
    2407
    +
    
    2408
    +(UQL10) For the occurs check in `qlUnify` we simply use `anyFreeVarsOfType`.  For
    
    2409
    +  coercions that looks at the free vars of the coercion; but for coercion /holes/
    
    2410
    +  all it can do is look at the free vars of the hole's type.
    
    2411
    +  See Note [Occurs check and coercions] in GHC.Tc.Utils.Unify
    
    2412
    +
    
    2348 2413
     Sadly discarded design alternative
    
    2349 2414
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2350 2415
     It is very tempting to use `unifyType` rather than `qlUnify`, killing off the
    

  • compiler/GHC/Tc/Solver/Equality.hs
    ... ... @@ -2390,7 +2390,7 @@ checkTypeEqNoUnification ev eq_rel lhs rhs =
    2390 2390
             given_pred = mkNomEqPred fam_app new_ty
    
    2391 2391
             given_term = evCoercion $ mkNomReflCo new_ty  -- See Detail (4) of Note
    
    2392 2392
     
    
    2393
    -    -- See Detail (7) of the Note
    
    2393
    +    -- See Detail (7) of Note [Type equality cycles: implementation]
    
    2394 2394
         cb_loc = updateCtLocOrigin (ctEvLoc ev) CycleBreakerOrigin
    
    2395 2395
     
    
    2396 2396
     ----------------------
    
    ... ... @@ -2648,15 +2648,15 @@ turn controls the behaviour of the workhorse: GHC.Tc.Utils.Unify.checkTyEqRhs.
    2648 2648
     
    
    2649 2649
     The details depend on whether we're working with a Given or a Wanted.
    
    2650 2650
     
    
    2651
    -Given
    
    2652
    ------
    
    2651
    +Note [Type equality cycles: Givens]
    
    2652
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2653 2653
     We emit a new Given, [G] F a ~ cbv, equating the type family application
    
    2654 2654
     to our new cbv. This is actually done by `BreakGiven` in the `given_flags`
    
    2655 2655
     in `checkTypeEqNoUnification`.
    
    2656 2656
     
    
    2657
    -Note its orientation: The type family ends up on the left; see
    
    2658
    -Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
    
    2659
    -CycleBreakerTvs is necessary. This scenario is now easily soluble, by using
    
    2657
    +Note its orientation: The type family ends up on the left;
    
    2658
    +see Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
    
    2659
    +GivenCycleBreakerTvs is necessary. This scenario is now easily soluble, by using
    
    2660 2660
     the first Given to rewrite the Wanted, which can now be solved.
    
    2661 2661
     
    
    2662 2662
     (The first Given actually also rewrites the second one, giving
    
    ... ... @@ -2677,10 +2677,11 @@ to
    2677 2677
     
    
    2678 2678
     Note that
    
    2679 2679
     * `cbv` is a fresh cycle breaker variable.
    
    2680
    -* `cbv` is a meta-tyvar, but it is completely untouchable.
    
    2680
    +* `cbv` is a meta-tyvar, of flavour GivenCycleBreakerTv, but it is
    
    2681
    +  completely untouchable.
    
    2681 2682
     * We track the cycle-breaker variables in inert_cycle_breakers in InertSet
    
    2682
    -* We eventually fill in the cycle-breakers, with `cbv := F lhs`.
    
    2683
    -  No one else fills in CycleBreakerTvs!
    
    2683
    +* We eventually fill in the cycle-breakers, with `cbv := F lhs`,
    
    2684
    +  via `restoreTyVarCycles`.  No one else fills in GivenCycleBreakerTvs!
    
    2684 2685
     * The evidence for the new `F lhs ~ cbv` constraint is Refl, because we know
    
    2685 2686
       this fill-in is ultimately going to happen.
    
    2686 2687
     * In `inert_cycle_breakers`, we remember the (cbv, F lhs) pair; that is, we
    
    ... ... @@ -2690,8 +2691,8 @@ Note that
    2690 2691
     * This fill-in is done when solving is complete, by restoreTyVarCycles
    
    2691 2692
       in nestImplicTcS and runTcSWithEvBinds.
    
    2692 2693
     
    
    2693
    -Wanted
    
    2694
    -------
    
    2694
    +Note [Type equality cycles: Wanteds]
    
    2695
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2695 2696
     First, we do not cycle-break unless the LHS is a unifiable type variable
    
    2696 2697
     See Note [Don't cycle-break Wanteds when not unifying].
    
    2697 2698
     
    
    ... ... @@ -2750,9 +2751,9 @@ to unify the cbvs:
    2750 2751
       [W] AllEqF '[Bool] alpha
    
    2751 2752
     
    
    2752 2753
     Without the logic detailed in this Note, we're stuck here, as AllEqF cannot
    
    2753
    -reduce and alpha cannot unify. Let's instead apply our cycle-breaker approach,
    
    2754
    -just as described above. We thus invent cbv1 and cbv2 and unify
    
    2755
    -alpha := cbv1 -> cbv2, yielding (after zonking)
    
    2754
    +reduce and alpha cannot unify (occurs check). Let's instead apply our
    
    2755
    +cycle-breaker approach, just as described above. We thus invent cbv1 and cbv2
    
    2756
    +and unify alpha := (cbv1 : cbv2), yielding (after zonking)
    
    2756 2757
     
    
    2757 2758
       [W] Head (cbv1 : cbv2) ~ cbv1
    
    2758 2759
       [W] Tail (cbv1 : cbv2) ~ cbv2
    
    ... ... @@ -2790,8 +2791,8 @@ Note that we need to unify the cbvs here; if we did not, there would be
    2790 2791
     no way to solve those constraints. That's why the cycle-breakers are
    
    2791 2792
     ordinary TauTvs.
    
    2792 2793
     
    
    2793
    -How all this is implemented
    
    2794
    ----------------------------
    
    2794
    +Note [Type equality cycles: implementation]
    
    2795
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2795 2796
     We implement all this via the `TEFA_Break` constructor of `TyEqFamApp`,
    
    2796 2797
     itself stored in the `tef_fam_app` field of `TyEqFlags`, which controls
    
    2797 2798
     the behaviour of `GHC.Tc.Utils.Unify.checkTyEqRhs`.  The `TEFA_Break`
    
    ... ... @@ -2918,7 +2919,7 @@ More details:
    2918 2919
          CycleBreakerOrigin. This works for both Givens and Wanteds, as we need the
    
    2919 2920
          logic in the W case for e.g. typecheck/should_fail/T17139.  Because this
    
    2920 2921
          logic needs to work for Wanteds, too, we cannot simply look for a
    
    2921
    -     CycleBreakerTv on the left: Wanteds don't use them.
    
    2922
    +     GivenCycleBreakerTv on the left: Wanteds don't use them.
    
    2922 2923
     
    
    2923 2924
     
    
    2924 2925
     **********************************************************************
    

  • compiler/GHC/Tc/Solver/InertSet.hs
    ... ... @@ -304,7 +304,7 @@ instance Outputable WorkList where
    304 304
     ********************************************************************* -}
    
    305 305
     
    
    306 306
     type CycleBreakerVarStack = NE.NonEmpty (Bag (TcTyVar, TcType))
    
    307
    -   -- ^ a stack of (CycleBreakerTv, original family applications) lists
    
    307
    +   -- ^ A stack of (GivenCycleBreakerTv, original family applications) lists
    
    308 308
        -- first element in the stack corresponds to current implication;
    
    309 309
        --   later elements correspond to outer implications
    
    310 310
        -- used to undo the cycle-breaking needed to handle
    
    ... ... @@ -324,6 +324,8 @@ data InertSet
    324 324
                   -- See `resetInertCans`
    
    325 325
     
    
    326 326
            , inert_cycle_breakers :: CycleBreakerVarStack
    
    327
    +              -- The equalities for /given/ cycle breakers, GivenCycleBreakerTvs
    
    328
    +              -- See Note [
    
    327 329
     
    
    328 330
            , inert_famapp_cache :: FunEqMap Reduction
    
    329 331
                   -- Just a hash-cons cache for use when reducing family applications
    
    ... ... @@ -1955,7 +1957,8 @@ prohibitedSuperClassSolve given_loc wanted_loc
    1955 1957
     
    
    1956 1958
     {- *********************************************************************
    
    1957 1959
     *                                                                      *
    
    1958
    -    Cycle breakers
    
    1960
    +                 Given Cycle breakers
    
    1961
    +  See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
    
    1959 1962
     *                                                                      *
    
    1960 1963
     ********************************************************************* -}
    
    1961 1964
     
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -2325,7 +2325,7 @@ wrapUnifier rws loc role do_unifications
    2325 2325
     -}
    
    2326 2326
     
    
    2327 2327
     -- | Fill in CycleBreakerTvs with the variables they stand for.
    
    2328
    --- See Note [Type equality cycles] in GHC.Tc.Solver.Equality
    
    2328
    +-- See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
    
    2329 2329
     restoreTyVarCycles :: InertSet -> TcM ()
    
    2330 2330
     restoreTyVarCycles is
    
    2331 2331
       = TcM.liftZonkM
    

  • compiler/GHC/Tc/Utils/TcMType.hs
    ... ... @@ -726,11 +726,11 @@ cloneMetaTyVarName name
    726 726
     metaInfoToTyVarName :: MetaInfo -> FastString
    
    727 727
     metaInfoToTyVarName  meta_info =
    
    728 728
       case meta_info of
    
    729
    -       TauTv          -> fsLit "t"
    
    730
    -       TyVarTv        -> fsLit "a"
    
    731
    -       RuntimeUnkTv   -> fsLit "r"
    
    732
    -       CycleBreakerTv -> fsLit "b"
    
    733
    -       ConcreteTv {}  -> fsLit "c"
    
    729
    +       TauTv               -> fsLit "t"
    
    730
    +       TyVarTv             -> fsLit "a"
    
    731
    +       RuntimeUnkTv        -> fsLit "r"
    
    732
    +       GivenCycleBreakerTv -> fsLit "b"
    
    733
    +       ConcreteTv {}       -> fsLit "c"
    
    734 734
     
    
    735 735
     newAnonMetaTyVar :: MetaInfo -> Kind -> TcM TcTyVar
    
    736 736
     newAnonMetaTyVar mi = newNamedAnonMetaTyVar (metaInfoToTyVarName mi) mi
    
    ... ... @@ -824,7 +824,7 @@ cloneAnonMetaTyVar info tv kind
    824 824
     -- in GHC.Tc.Solver.Equality.
    
    825 825
     newCycleBreakerTyVar :: TcKind -> TcM TcTyVar
    
    826 826
     newCycleBreakerTyVar kind
    
    827
    -  = do { details <- newMetaDetails CycleBreakerTv
    
    827
    +  = do { details <- newMetaDetails GivenCycleBreakerTv
    
    828 828
            ; name <- newMetaTyVarName (fsLit "cbv")
    
    829 829
            ; return (mkTcTyVar name kind details) }
    
    830 830
     
    

  • compiler/GHC/Tc/Utils/TcType.hs
    ... ... @@ -688,9 +688,10 @@ data MetaInfo
    688 688
        | RuntimeUnkTv  -- ^ A unification variable used in the GHCi debugger.
    
    689 689
                        -- It /is/ allowed to unify with a polytype, unlike TauTv
    
    690 690
     
    
    691
    -   | CycleBreakerTv  -- ^ Used to fix occurs-check problems in Givens
    
    692
    -                     -- See Note [Type equality cycles] in
    
    693
    -                     -- GHC.Tc.Solver.Equality
    
    691
    +   | GivenCycleBreakerTv  -- ^ Used to fix occurs-check problems in Givens
    
    692
    +                          -- Never used for Wanteds
    
    693
    +                          -- See Note [Type equality cycles] in
    
    694
    +                          -- GHC.Tc.Solver.Equality
    
    694 695
     
    
    695 696
        | ConcreteTv ConcreteTvOrigin
    
    696 697
             -- ^ A unification variable that can only be unified
    
    ... ... @@ -705,11 +706,11 @@ instance Outputable MetaDetails where
    705 706
       ppr (Indirect ty) = text "Indirect" <+> ppr ty
    
    706 707
     
    
    707 708
     instance Outputable MetaInfo where
    
    708
    -  ppr TauTv           = text "tau"
    
    709
    -  ppr TyVarTv         = text "tyv"
    
    710
    -  ppr RuntimeUnkTv    = text "rutv"
    
    711
    -  ppr CycleBreakerTv  = text "cbv"
    
    712
    -  ppr (ConcreteTv {}) = text "conc"
    
    709
    +  ppr TauTv               = text "tau"
    
    710
    +  ppr TyVarTv             = text "tyv"
    
    711
    +  ppr RuntimeUnkTv        = text "rutv"
    
    712
    +  ppr GivenCycleBreakerTv = text "cbv"
    
    713
    +  ppr (ConcreteTv {})     = text "conc"
    
    713 714
     
    
    714 715
     -- | What caused us to create a 'ConcreteTv' metavariable?
    
    715 716
     -- See Note [ConcreteTv] in GHC.Tc.Utils.Concrete.
    
    ... ... @@ -1299,7 +1300,7 @@ isRuntimeUnkTyVar tv
    1299 1300
     
    
    1300 1301
     isCycleBreakerTyVar tv
    
    1301 1302
       | isTyVar tv -- See Note [Coercion variables in free variable lists]
    
    1302
    -  , MetaTv { mtv_info = CycleBreakerTv } <- tcTyVarDetails tv
    
    1303
    +  , MetaTv { mtv_info = GivenCycleBreakerTv } <- tcTyVarDetails tv
    
    1303 1304
       = True
    
    1304 1305
     
    
    1305 1306
       | otherwise
    
    ... ... @@ -1350,8 +1351,8 @@ metaTyVarInfo tv
    1350 1351
     
    
    1351 1352
     isTouchableInfo :: MetaInfo -> Bool
    
    1352 1353
     isTouchableInfo info
    
    1353
    -  | CycleBreakerTv <- info = False
    
    1354
    -  | otherwise              = True
    
    1354
    +  | GivenCycleBreakerTv <- info = False
    
    1355
    +  | otherwise                   = True
    
    1355 1356
     
    
    1356 1357
     metaTyVarTcLevel :: TcTyVar -> TcLevel
    
    1357 1358
     metaTyVarTcLevel tv
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -103,7 +103,6 @@ import GHC.Types.Id( idType )
    103 103
     import GHC.Types.Var as Var
    
    104 104
     import GHC.Types.Var.Set
    
    105 105
     import GHC.Types.Var.Env
    
    106
    -import GHC.Types.Var.FV
    
    107 106
     import GHC.Types.Basic
    
    108 107
     import GHC.Types.Unique.Set (nonDetEltsUniqSet)
    
    109 108
     
    
    ... ... @@ -120,7 +119,6 @@ import GHC.Data.Maybe (firstJusts)
    120 119
     import Control.Monad
    
    121 120
     import Data.Functor.Identity (Identity(..))
    
    122 121
     import qualified Data.List.NonEmpty as NE
    
    123
    -import Data.Monoid as DM ( Any(..) )
    
    124 122
     import qualified Data.Semigroup as S ( (<>) )
    
    125 123
     import Data.Traversable (for)
    
    126 124
     
    
    ... ... @@ -3143,6 +3141,7 @@ uUnfilledVar2 env@(UE { u_defer = def_eq_ref, u_given_eq_lvl = given_eq_lvl })
    3143 3141
         do { traceTc "uUnfilledVar2 not ok" $
    
    3144 3142
                  vcat [ text "tv1:" <+> ppr tv1
    
    3145 3143
                       , text "ty2:" <+> ppr ty2
    
    3144
    +                  , text "given_eq_lvl:" <+> ppr given_eq_lvl
    
    3146 3145
                       , text "simple-unify-chk:" <+> ppr (simpleUnifyCheck UC_OnTheFly given_eq_lvl tv1 ty2)
    
    3147 3146
                       ]
    
    3148 3147
                    -- Occurs check or an untouchable: just defer
    
    ... ... @@ -3236,16 +3235,17 @@ lhsPriority tv
    3236 3235
             -> 5  -- Eliminate instantiation variables first
    
    3237 3236
             | otherwise
    
    3238 3237
             -> case info of
    
    3239
    -             CycleBreakerTv -> 0
    
    3240
    -             TyVarTv        -> 1
    
    3241
    -             ConcreteTv {}  -> 2
    
    3242
    -             TauTv          -> 3
    
    3243
    -             RuntimeUnkTv   -> 4
    
    3238
    +             GivenCycleBreakerTv -> 0
    
    3239
    +             TyVarTv             -> 1
    
    3240
    +             ConcreteTv {}       -> 2
    
    3241
    +             TauTv               -> 3
    
    3242
    +             RuntimeUnkTv        -> 4
    
    3244 3243
     
    
    3245 3244
     {- Note [Unification preconditions]
    
    3246 3245
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3247 3246
     Question: given a homogeneous equality (alpha ~# ty), when is it OK to
    
    3248 3247
     unify alpha := ty?
    
    3248
    +
    
    3249 3249
     (This note only applies to /homogeneous/ equalities, in which both
    
    3250 3250
     sides have the same kind.)
    
    3251 3251
     
    
    ... ... @@ -3285,7 +3285,7 @@ There are five reasons not to unify:
    3285 3285
          structured type.  So if 'ty' is a structured type, such as (Maybe x),
    
    3286 3286
          don't unify.
    
    3287 3287
     
    
    3288
    -   * CycleBreakerTv: never unified, except by restoreTyVarCycles.
    
    3288
    +   * GivenCycleBreakerTv: never unified, except by restoreGivenCycles.
    
    3289 3289
     
    
    3290 3290
     4. (CONCRETE) A ConcreteTv can only unify with a concrete type,
    
    3291 3291
         by definition.
    
    ... ... @@ -3354,6 +3354,14 @@ Needless to say, all there are wrinkles:
    3354 3354
         GHC.Tc.Solver.floatEqualities, around Nov 2020.  It's much easier
    
    3355 3355
         to unify in-place, with no floating.
    
    3356 3356
     
    
    3357
    +* (COERCIONS) What if there are coercions in the RHS?  E.g.
    
    3358
    +        alpha ~ (ty |> co)
    
    3359
    +   or   alpha ~ (ty co)
    
    3360
    +   We only recurse into the `coercionType` of `co` rather than `co` itself.
    
    3361
    +   Why? Mainly because `co` might be a coercion hole, in which case we /can't/
    
    3362
    +   recurse into the coercion that will eventually fill the hole.  This came
    
    3363
    +   up in #26543.
    
    3364
    +
    
    3357 3365
     Note [TyVar/TyVar orientation]
    
    3358 3366
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3359 3367
     See also Note [Fundeps with instances, and equality orientation]
    
    ... ... @@ -3382,7 +3390,7 @@ So we look for a positive reason to swap, using a three-step test:
    3382 3390
       touchable and can be unified.
    
    3383 3391
     
    
    3384 3392
       Tie-breaking rules for MetaTvs:
    
    3385
    -  - CycleBreakerTv: This is essentially a stand-in for another type;
    
    3393
    +  - GivenCycleBreakerTv: This is essentially a stand-in for another type;
    
    3386 3394
            it's untouchable and should have the same priority as a skolem: 0.
    
    3387 3395
     
    
    3388 3396
       - TyVarTv: These can unify only with another tyvar, but we can't unify
    
    ... ... @@ -3588,7 +3596,6 @@ matchExpectedFunKind hs_ty n k = go n k
    3588 3596
     
    
    3589 3597
     data UnifyCheckCaller
    
    3590 3598
       = UC_OnTheFly   -- Called from the on-the-fly unifier
    
    3591
    -  | UC_QuickLook  -- Called from Quick Look
    
    3592 3599
       | UC_Solver     -- Called from constraint solver
    
    3593 3600
     
    
    3594 3601
     -- | The result type of 'simpleUnifyCheck'.
    
    ... ... @@ -3619,40 +3626,22 @@ simpleUnifyCheck :: UnifyCheckCaller -> TcLevel -> TcTyVar -> TcType -> SimpleUn
    3619 3626
     -- unification might still be OK, but it'll take more work to do
    
    3620 3627
     -- (use the full 'checkTyEqRhs').
    
    3621 3628
     --
    
    3622
    --- It returns SUC_CannotUnify for
    
    3623
    ---   * Touchability failure
    
    3624
    ---   * Top level-shape failure
    
    3625
    ---
    
    3626
    --- It returns SUC_NotSure for
    
    3627
    ---   * Occurs check: lhs_tv occurs in rhs_ty
    
    3628
    ---   * Type families unless fam_ok=True
    
    3629
    ---   * Foralls unless
    
    3630
    ---        lhs_tv is RuntimeUnk (used by GHCi debugger)
    
    3631
    ---            or is a QL instantiation variable
    
    3632
    ---   * Concreteness: lhs_tv is concrete and rhs is a non-concrete type
    
    3633
    ---   * Skolem-escape: a level-check for type variables, to avoid skolem escape
    
    3629
    +-- See Note [simpleUnifyCheck]
    
    3634 3630
     --
    
    3635 3631
     -- This function is pretty heavily used, so it's optimised not to allocate
    
    3636 3632
     simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    
    3637
    -  | not $ touchabilityTest given_eq_lvl lhs_tv
    
    3638
    -  = SUC_CannotUnify
    
    3639
    -  | not $ checkTopShape lhs_info rhs
    
    3640
    -  = SUC_CannotUnify
    
    3641
    -  | rhs_is_ok rhs
    
    3642
    -  = SUC_CanUnify
    
    3643
    -  | otherwise
    
    3644
    -  = SUC_NotSure
    
    3633
    +  | not $ touchabilityTest given_eq_lvl lhs_tv = SUC_CannotUnify
    
    3634
    +  | not $ checkTopShape lhs_info rhs           = SUC_CannotUnify
    
    3635
    +  | not (rhs_is_bad rhs)                       = SUC_CanUnify
    
    3636
    +  | otherwise                                  = SUC_NotSure
    
    3645 3637
       where
    
    3646
    -    lhs_info = metaTyVarInfo lhs_tv
    
    3647
    -
    
    3648
    -    !(occ_in_ty, occ_in_co) = mkOccFolders (tyVarName lhs_tv)
    
    3649
    -
    
    3638
    +    lhs_info           = metaTyVarInfo lhs_tv
    
    3650 3639
         lhs_tv_lvl         = tcTyVarLevel lhs_tv
    
    3651 3640
         lhs_tv_is_concrete = isConcreteTyVar lhs_tv
    
    3641
    +    lhs_tv_nm          = tyVarName lhs_tv
    
    3652 3642
     
    
    3653
    -    forall_ok = case caller of
    
    3654
    -                   UC_QuickLook -> isQLInstTyVar lhs_tv
    
    3655
    -                   _            -> isRuntimeUnkTyVar lhs_tv
    
    3643
    +    -- See Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect, esp (RUT1).
    
    3644
    +    forall_ok = isRuntimeUnkTyVar lhs_tv
    
    3656 3645
     
    
    3657 3646
         -- This fam_ok thing relates to a very specific perf problem
    
    3658 3647
         -- See Note [Prevent unification with type families]
    
    ... ... @@ -3661,58 +3650,78 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3661 3650
         --   see if it bites us)
    
    3662 3651
         fam_ok = case caller of
    
    3663 3652
                    UC_Solver     -> True
    
    3664
    -               UC_QuickLook  -> True
    
    3665 3653
                    UC_OnTheFly   -> False
    
    3666 3654
     
    
    3667
    -    rhs_is_ok (TyVarTy tv)
    
    3668
    -      | lhs_tv == tv                                    = False
    
    3669
    -      | tcTyVarLevel tv `strictlyDeeperThan` lhs_tv_lvl = False
    
    3670
    -      | lhs_tv_is_concrete, not (isConcreteTyVar tv)    = False
    
    3671
    -      | occ_in_ty $! (tyVarKind tv)                     = False
    
    3672
    -      | otherwise                                       = True
    
    3673
    -
    
    3674
    -    rhs_is_ok (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
    
    3675
    -      | not forall_ok, isInvisibleFunArg af = False
    
    3676
    -      | otherwise                           = rhs_is_ok w && rhs_is_ok a && rhs_is_ok r
    
    3677
    -
    
    3678
    -    rhs_is_ok (TyConApp tc tys)
    
    3679
    -      | lhs_tv_is_concrete, not (isConcreteTyCon tc) = False
    
    3680
    -      | not forall_ok, not (isTauTyCon tc)           = False
    
    3681
    -      | not fam_ok,    not (isFamFreeTyCon tc)       = False
    
    3682
    -      | otherwise                                    = all rhs_is_ok tys
    
    3683
    -
    
    3684
    -    rhs_is_ok (ForAllTy (Bndr tv _) ty)
    
    3685
    -      | forall_ok = rhs_is_ok (tyVarKind tv) && (tv == lhs_tv || rhs_is_ok ty)
    
    3686
    -      | otherwise = False
    
    3687
    -
    
    3688
    -    rhs_is_ok (AppTy t1 t2)    = rhs_is_ok t1 && rhs_is_ok t2
    
    3689
    -    rhs_is_ok (CastTy ty co)   = not (occ_in_co co) && rhs_is_ok ty
    
    3690
    -    rhs_is_ok (CoercionTy co)  = not (occ_in_co co)
    
    3691
    -    rhs_is_ok (LitTy {})       = True
    
    3692
    -
    
    3693
    -
    
    3694
    -mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool)
    
    3695
    --- These functions return True
    
    3696
    ---   * if lhs_tv occurs (incl deeply, in the kind of variable)
    
    3697
    ---   * if there is a coercion hole
    
    3698
    --- No expansion of type synonyms
    
    3699
    -mkOccFolders lhs_tv = ( getAny . runFVTop . check_ty
    
    3700
    -                      , getAny . runFVTop . check_co)
    
    3701
    -  where
    
    3702
    -    check_ty :: Type -> FV BoundVars Any
    
    3703
    -    !(check_ty, _, check_co, _) = foldTyCo occ_folder
    
    3704
    -
    
    3705
    -    occ_folder :: TyCoFolder (FV BoundVars Any)
    
    3706
    -    occ_folder = TyCoFolder { tcf_view  = noView  -- Don't expand synonyms
    
    3707
    -                            , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    3708
    -                            , tcf_lit = mempty, tcf_hole  = do_hole
    
    3709
    -                            , tcf_tycobinder = addBndrFV }
    
    3655
    +    rhs_tcv_is_bad tcv
    
    3656
    +       -- c.f. checkTyVar, the TEFTyVar case
    
    3657
    +       | isCoVar tcv                                      = occ_check
    
    3658
    +       | tcTyVarLevel tcv `strictlyDeeperThan` lhs_tv_lvl = True
    
    3659
    +       | lhs_tv_is_concrete, not (isConcreteTyVar tcv)    = True
    
    3660
    +       | occ_check                                        = True
    
    3661
    +       | otherwise                                        = False
    
    3662
    +       where
    
    3663
    +         occ_check = simpleOccursCheck lhs_tv_nm tcv
    
    3664
    +
    
    3665
    +    rhs_is_bad (TyVarTy tv) = rhs_tcv_is_bad tv
    
    3666
    +
    
    3667
    +    rhs_is_bad (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
    
    3668
    +      | not forall_ok, isInvisibleFunArg af = True
    
    3669
    +      | otherwise                           = rhs_is_bad w || rhs_is_bad a || rhs_is_bad r
    
    3670
    +
    
    3671
    +    rhs_is_bad (TyConApp tc tys)
    
    3672
    +      | lhs_tv_is_concrete, not (isConcreteTyCon tc) = True
    
    3673
    +      | not forall_ok, not (isTauTyCon tc)           = True
    
    3674
    +      | not fam_ok,    not (isFamFreeTyCon tc)       = True
    
    3675
    +      | otherwise                                    = any rhs_is_bad tys
    
    3676
    +
    
    3677
    +    rhs_is_bad (ForAllTy (Bndr tv _) ty)
    
    3678
    +      | not forall_ok               = True
    
    3679
    +      | rhs_is_bad (tyVarKind tv)   = True
    
    3680
    +      | tv /= lhs_tv, rhs_is_bad ty = True
    
    3681
    +      | otherwise                   = False
    
    3682
    +
    
    3683
    +    rhs_is_bad (AppTy t1 t2)    = rhs_is_bad t1 || rhs_is_bad t2
    
    3684
    +    rhs_is_bad (CastTy ty co)   = co_is_bad co || rhs_is_bad ty
    
    3685
    +    rhs_is_bad (CoercionTy co)  = co_is_bad co
    
    3686
    +    rhs_is_bad (LitTy {})       = False
    
    3687
    +
    
    3688
    +    -- Coercions: see Note [Occurs check and coercions]
    
    3689
    +    co_is_bad co = anyFreeVarsOfCo rhs_tcv_is_bad co
    
    3690
    +
    
    3691
    +{- Note [simpleUnifyCheck]
    
    3692
    +~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3693
    +The function `simpleUnifyCheck` is a simple, /fast/ check for unifying (tv ~ rhs).
    
    3694
    +It can return a definite decision (SUC_CannotUnify of SUC_CanUnify), or uncertainty
    
    3695
    +(SUC_NotSure).  In the latter case we will later use `checkTyEqRhs` to resolve.
    
    3696
    +In particular:
    
    3697
    +
    
    3698
    +   It returns SUC_CannotUnify for
    
    3699
    +   * Touchability failure
    
    3700
    +   * Top level-shape failure
    
    3701
    +
    
    3702
    +   It returns SUC_NotSure for
    
    3703
    +   * Occurs check: lhs_tv occurs in rhs_ty
    
    3704
    +   * Type families unless fam_ok=True
    
    3705
    +   * Foralls unless
    
    3706
    +        lhs_tv is RuntimeUnk (used by GHCi debugger)
    
    3707
    +            or is a QL instantiation variable
    
    3708
    +   * Concreteness: lhs_tv is concrete and rhs is a non-concrete type
    
    3709
    +   * Skolem-escape: a level-check for type variables, to avoid skolem escape
    
    3710
    +
    
    3711
    +Note [Occurs check and coercions]
    
    3712
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3713
    +What if we have
    
    3714
    +    alpha ~ Int |>  (CoHole ch :: Type ~ blah)
    
    3710 3715
     
    
    3711
    -    do_tcv v = (MkFV $ \ bvs ->
    
    3712
    -                Any (not (v `elemVarSet` bvs) && tyVarName v == lhs_tv))
    
    3713
    -               `mappend` check_ty (varType v)
    
    3716
    +Clearly we want to check that `alpha` doesn't appear free in `blah`.  But even if
    
    3717
    +it doesn't, what if `ch` is later filled in with a coercion mentioning `alpha`?
    
    3718
    +Tht would be bad: we'd get an infinite structure.
    
    3714 3719
     
    
    3715
    -    do_hole _hole = MkFV $ \ _bvs -> DM.Any True  -- Reject coercion holes
    
    3720
    +The Right Thing to do is to check for a loop when (later) filling in `ch`.
    
    3721
    +We don't do that right now, and it's very obscure.  But in `simpleUnifyCheck`
    
    3722
    +we just look in the free vars of the /kind/ of the hole, which is what
    
    3723
    +`anyFreeVarsOfCo` does.
    
    3724
    +-}
    
    3716 3725
     
    
    3717 3726
     {- *********************************************************************
    
    3718 3727
     *                                                                      *
    
    ... ... @@ -4086,13 +4095,14 @@ wantConcreteCheck = \case
    4086 4095
     -- where @cbv@ is a fresh loop-breaker tyvar (for Given), or
    
    4087 4096
     -- just a fresh 'TauTv' (for Wanted)
    
    4088 4097
     famAppBreaker :: FamAppBreaker a -> TcType -> TcM (PuResult a Reduction)
    
    4089
    -famAppBreaker BreakGiven fam_app
    
    4098
    +famAppBreaker BreakGiven fam_app    -- Givens
    
    4090 4099
        = do { new_tv <- TcM.newCycleBreakerTyVar (typeKind fam_app)
    
    4091 4100
             ; return (PuOK (unitBag (new_tv, fam_app))
    
    4092 4101
                            (mkReflRedn Nominal (mkTyVarTy new_tv))) }
    
    4093 4102
                      -- Why reflexive? See Detail (4) of Note [Type equality cycles]
    
    4094 4103
                      -- in GHC.Tc.Solver.Equality
    
    4095
    -famAppBreaker (BreakWanted ev lhs_tv) fam_app
    
    4104
    +
    
    4105
    +famAppBreaker (BreakWanted ev lhs_tv) fam_app   -- Wanteds
    
    4096 4106
       -- Occurs check or skolem escape; so flatten
    
    4097 4107
       = do { reason <- checkPromoteFreeVars cteInsolubleOccurs
    
    4098 4108
                          (tyVarName lhs_tv) lhs_tv_lvl
    
    ... ... @@ -4236,10 +4246,16 @@ the RHS by
    4236 4246
     Its specific behaviour is governed by the `TyEqFlags` that are passed
    
    4237 4247
     to it; see Note [TyEqFlags].
    
    4238 4248
     
    
    4239
    -Note, however, that `checkTyEqRhs` specifically does /not/ check for:
    
    4240
    -  * Touchability of the LHS (in the case of a unification variable)
    
    4241
    -  * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
    
    4242
    -These things are checked by `simpleUnifyCheck`.
    
    4249
    +Wrinkles
    
    4250
    +
    
    4251
    +(CTER1) Note, however, that `checkTyEqRhs` specifically does /not/ check for:
    
    4252
    +         * Touchability of the LHS (in the case of a unification variable)
    
    4253
    +         * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
    
    4254
    +    These things are checked by `simpleUnifyCheck`.
    
    4255
    +
    
    4256
    +(CTER2) Also `checkTyEqRhs` does not need to worry about RuntimeUnkTvs, because
    
    4257
    +    the solver is never called on constraints involving them.  See (RUT1) in
    
    4258
    +    Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect.
    
    4243 4259
     -}
    
    4244 4260
     
    
    4245 4261
     
    
    ... ... @@ -4297,6 +4313,7 @@ check_ty_eq_rhs :: forall m a
    4297 4313
                     => TyEqFlags m a
    
    4298 4314
                     -> TcType           -- Already zonked
    
    4299 4315
                     -> m (PuResult a Reduction)
    
    4316
    +{-# INLINEABLE check_ty_eq_rhs #-}
    
    4300 4317
     check_ty_eq_rhs flags ty
    
    4301 4318
       = case ty of
    
    4302 4319
           LitTy {}        -> return $ okCheckRefl ty
    
    ... ... @@ -4327,7 +4344,7 @@ check_ty_eq_rhs flags ty
    4327 4344
                               ; return (mkReflCoRedn Nominal <$> co_res) }
    
    4328 4345
     
    
    4329 4346
           ForAllTy {}   -> return $ PuFail impredicativeProblem -- Not allowed (TyEq:F)
    
    4330
    -{-# INLINEABLE check_ty_eq_rhs #-}
    
    4347
    +         -- No need to worry about RuntimeUnkTv
    
    4331 4348
     
    
    4332 4349
     -------------------
    
    4333 4350
     checkCo :: Monad m => TyEqFlags m a -> Coercion -> m (PuResult a Coercion)
    
    ... ... @@ -4351,7 +4368,10 @@ checkCo flags co =
    4351 4368
             -- Occurs check (can promote)
    
    4352 4369
             | OC_Check lhs_tv occ_prob <- occ
    
    4353 4370
             , LC_Promote { lc_lvlp = lhs_tv_lvl } <- lc
    
    4354
    -        -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl (tyCoVarsOfCo co)
    
    4371
    +        -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl $
    
    4372
    +                          tyCoVarsOfCo co
    
    4373
    +                           -- See Note [Occurs check and coercions]
    
    4374
    +                          --  and Note [checkCo] (2)
    
    4355 4375
                   ; return $
    
    4356 4376
                     if cterHasNoProblem reason
    
    4357 4377
                     then pure co
    
    ... ... @@ -4648,7 +4668,7 @@ checkTyVar flags occ_tv
    4648 4668
                       -- so the only reason for a filled meta-tyvar is promotion
    
    4649 4669
     
    
    4650 4670
                     else do_rhs_checks
    
    4651
    -                     [ simpleOccursCheck      occ  occ_tv
    
    4671
    +                     [ tyVarOccursCheck      occ  occ_tv
    
    4652 4672
                          , tyVarLevelCheck        lc   occ_tv
    
    4653 4673
                          , tyVarConcretenessCheck conc occ_tv ]
    
    4654 4674
                   }
    
    ... ... @@ -4706,17 +4726,24 @@ promotionDone occ_tv _ (CC_Promote {}) = isFilledMetaTyVar occ_tv
    4706 4726
     promotionDone _      _               _ = return False
    
    4707 4727
     
    
    4708 4728
     ---------------------
    
    4709
    -simpleOccursCheck :: OccursCheck -> TcTyVar -> TyVarCheckResult m
    
    4729
    +tyVarOccursCheck :: OccursCheck -> TcTyCoVar -> TyVarCheckResult m
    
    4710 4730
     -- ^ The "interpreter" for 'OccursCheck'
    
    4711
    -simpleOccursCheck OC_None _
    
    4712
    -  = TyVarCheck_Success
    
    4713
    -simpleOccursCheck (OC_Check lhs_tv occ_prob) occ_tv
    
    4714
    -  | lhs_tv == tyVarName occ_tv || check_kind (tyVarKind occ_tv)
    
    4715
    -  = TyVarCheck_Error (cteProblem occ_prob)
    
    4716
    -  | otherwise
    
    4731
    +-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
    
    4732
    +tyVarOccursCheck OC_None _
    
    4717 4733
       = TyVarCheck_Success
    
    4734
    +tyVarOccursCheck (OC_Check { occurs_tv_name = lhs_tv, occurs_problem = prob }) occ_tcv
    
    4735
    +  | simpleOccursCheck lhs_tv occ_tcv = TyVarCheck_Error (cteProblem prob)
    
    4736
    +  | otherwise                        = TyVarCheck_Success
    
    4737
    +
    
    4738
    +simpleOccursCheck :: Name -> TyCoVar -> Bool
    
    4739
    +-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
    
    4740
    +-- True <=> occurs check fires
    
    4741
    +simpleOccursCheck lhs_tv occ_tcv
    
    4742
    +  = is_bad occ_tcv || anyFreeVarsOfType is_bad (varType occ_tcv)
    
    4743
    +    -- anyFreeVarsOfType looks at the /deep/ free vars of the type
    
    4718 4744
       where
    
    4719
    -    (check_kind, _) = mkOccFolders lhs_tv
    
    4745
    +    is_bad :: TyCoVar -> Bool
    
    4746
    +    is_bad occ_tcv = lhs_tv == tyVarName occ_tcv
    
    4720 4747
     
    
    4721 4748
     -------------------------
    
    4722 4749
     tyVarLevelCheck :: LevelCheck m -> TcTyVar -> TyVarCheckResult m
    
    ... ... @@ -4836,7 +4863,7 @@ checkTopShape info xi
    4836 4863
                             RuntimeUnk  -> True
    
    4837 4864
                             MetaTv { mtv_info = TyVarTv } -> True
    
    4838 4865
                             _                             -> False
    
    4839
    -      CycleBreakerTv -> False  -- We never unify these
    
    4866
    +      GivenCycleBreakerTv -> False  -- We never unify these
    
    4840 4867
           _ -> True
    
    4841 4868
     
    
    4842 4869
     --------------------------------------------------------------------------------
    
    ... ... @@ -5149,14 +5176,14 @@ mightEqualLater inert_set given_pred given_loc wanted_pred wanted_loc
    5149 5176
     
    
    5150 5177
         -- True for TauTv and TyVarTv (and RuntimeUnkTv) meta-tyvars
    
    5151 5178
         -- (as they can be unified)
    
    5152
    -    -- and also for CycleBreakerTvs that mentions meta-tyvars
    
    5179
    +    -- and also for GivenCycleBreakerTvs that mentions meta-tyvars
    
    5153 5180
         mentions_meta_ty_var :: TyVar -> Bool
    
    5154 5181
         mentions_meta_ty_var tv
    
    5155 5182
           | isMetaTyVar tv
    
    5156 5183
           = case metaTyVarInfo tv of
    
    5157 5184
               -- See Examples 8 and 9 in the Note
    
    5158
    -          CycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
    
    5159
    -                              (lookupCycleBreakerVar tv inert_set)
    
    5185
    +          GivenCycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
    
    5186
    +                                    (lookupCycleBreakerVar tv inert_set)
    
    5160 5187
               _ -> True
    
    5161 5188
           | otherwise
    
    5162 5189
           = False
    
    ... ... @@ -5199,7 +5226,7 @@ We want to solve w1 using g1 and g2. The presence of g3 is irrelevant, because
    5199 5226
     we cannot unify 'f[tau:1] v[tau:1]' and 'g[sk:1] x[sk:3]' due to skolem escape.
    
    5200 5227
     -}
    
    5201 5228
     
    
    5202
    --- | Return the type family application a CycleBreakerTv maps to.
    
    5229
    +-- | Return the type family application a GivenCycleBreakerTv maps to.
    
    5203 5230
     lookupCycleBreakerVar :: TcTyVar    -- ^ cbv, must be a CycleBreakerTv
    
    5204 5231
                           -> InertSet
    
    5205 5232
                           -> TcType     -- ^ type family application the cbv maps to
    

  • compiler/GHC/Types/Var/FV.hs
    ... ... @@ -9,7 +9,7 @@ module GHC.Types.Var.FV (
    9 9
             runFVSelective, runFVSelectiveList, runFVSelectiveSet,
    
    10 10
             InterestingVarFun,
    
    11 11
     
    
    12
    -        addBndrFV, addBndrsFV, addBndrSelectiveFV, addBndrsSelectiveFV,
    
    12
    +        updEnvFV, addBndrFV, addBndrsFV, addBndrSelectiveFV, addBndrsSelectiveFV,
    
    13 13
             emptyFV,   -- or `mempty`
    
    14 14
             unionFV,   -- or `mappend`
    
    15 15
             mapUnionFV
    
    ... ... @@ -138,7 +138,7 @@ type BoundVars = TyCoVarSet
    138 138
     
    
    139 139
     type VarSetFV     = FV BoundVars (EndoOS TyCoVarSet)
    
    140 140
     type DVarSetFV    = FV BoundVars (EndoOS DTyCoVarSet)
    
    141
    -type SelectiveDFV = FV (InterestingVarFun, BoundVars) (EndoOS DVarSet)
    
    141
    +type SelectiveDFV = FV (BoundVars, InterestingVarFun) (EndoOS DVarSet)
    
    142 142
     -- VarSetFV:     collects a VarSet
    
    143 143
     -- DVarSetFV:    collects a DVarSet (deterministic)
    
    144 144
     -- SelectiveDFV: selectively collects a DVarSet
    
    ... ... @@ -158,9 +158,14 @@ pattern MkFV :: (env -> acc) -> FV env acc
    158 158
     {-# COMPLETE MkFV #-}
    
    159 159
     pattern MkFV f <- MkFV' f
    
    160 160
           where
    
    161
    -        MkFV f = MkFV' (oneShot f)
    
    161
    +        MkFV f = MkFV' (oneShot (\env -> env `seq` f env))
    
    162 162
              -- oneShot: this is the core of the one-shot trick!
    
    163 163
              -- Note [The one-shot state monad trick] in  GHC.Utils.Monad.
    
    164
    +         --
    
    165
    +         -- seq: this is /important/: it makes the function strict.
    
    166
    +         -- When the `env` is a pair (as it is in anyFreeVarsOfType), that means
    
    167
    +         -- we get good worker/wrapper splitting.  Without it we get extra boxed
    
    168
    +         -- pairs allocated by `anyFreeVarsOfType`.
    
    164 169
     
    
    165 170
     instance Semigroup a => Semigroup (FV env a) where
    
    166 171
       (<>) = unionFV
    
    ... ... @@ -174,28 +179,28 @@ emptyFV = MkFV (\_ -> mempty)
    174 179
     unionFV :: Semigroup a => FV env a -> FV env a -> FV env a
    
    175 180
     unionFV (MkFV' f1) (MkFV' f2) = MkFV (\env -> f1 env <> f2 env)
    
    176 181
     
    
    177
    -upd_bndrs_fv :: (env -> env) -> FV env a -> FV env a
    
    178
    -{-# INLINE addBndrFV #-}
    
    179
    -upd_bndrs_fv upd f = MkFV (\bvs -> runFV f $! upd bvs)
    
    182
    +updEnvFV :: (env -> env) -> FV env a -> FV env a
    
    183
    +updEnvFV upd f = MkFV (\bvs -> runFV f $! upd bvs)
    
    180 184
         -- Strict application to avoid making a thunk
    
    181 185
     
    
    182 186
     addBndrFV :: TyCoVar -> FV BoundVars a -> FV BoundVars a
    
    183
    -addBndrFV tcv = upd_bndrs_fv (\bvs -> extendVarSet bvs tcv)
    
    187
    +{-# INLINE addBndrFV #-}
    
    188
    +addBndrFV tcv = updEnvFV (\bvs -> extendVarSet bvs tcv)
    
    184 189
     
    
    185 190
     addBndrsFV :: [Var] -> FV BoundVars a -> FV BoundVars a
    
    186
    -addBndrsFV tcvs = upd_bndrs_fv (\bvs -> extendVarSetList bvs tcvs)
    
    191
    +addBndrsFV tcvs = updEnvFV (\bvs -> extendVarSetList bvs tcvs)
    
    187 192
     
    
    188
    -addBndrSelectiveFV :: TyCoVar -> FV (f, BoundVars) a -> FV (f, BoundVars) a
    
    193
    +addBndrSelectiveFV :: TyCoVar -> FV (BoundVars, f) a -> FV (BoundVars, f) a
    
    189 194
     addBndrSelectiveFV tcv
    
    190
    -  = upd_bndrs_fv (\(f,bvs) -> let !bvs' = extendVarSet bvs tcv
    
    195
    +  = updEnvFV (\(bvs,f) -> let !bvs' = extendVarSet bvs tcv
    
    191 196
                                       -- Strict let to avoid thunks
    
    192
    -                              in (f,bvs'))
    
    197
    +                          in (bvs',f))
    
    193 198
     
    
    194
    -addBndrsSelectiveFV :: [Var] -> FV (f, BoundVars) a -> FV (f, BoundVars) a
    
    199
    +addBndrsSelectiveFV :: [Var] -> FV (BoundVars, f) a -> FV (BoundVars, f) a
    
    195 200
     addBndrsSelectiveFV bs
    
    196
    -  = upd_bndrs_fv (\(f,bvs) -> let !bvs' = extendVarSetList bvs bs
    
    201
    +  = updEnvFV (\(bvs,f) -> let !bvs' = extendVarSetList bvs bs
    
    197 202
                                       -- Strict let to avoid thunks
    
    198
    -                              in (f,bvs'))
    
    203
    +                          in (bvs',f))
    
    199 204
     
    
    200 205
     mapUnionFV :: (Foldable t, Monoid acc)
    
    201 206
               => (a -> FV env acc) -> t a -> FV env acc
    
    ... ... @@ -221,7 +226,7 @@ runTyCoVarsDSet f = runFVAcc f emptyDVarSet
    221 226
     
    
    222 227
     runFVSelective :: InterestingVarFun -> SelectiveDFV -> DVarSet
    
    223 228
     runFVSelective interesting f
    
    224
    -  = runEndoOS (runFV f (interesting, emptyVarSet)) emptyDVarSet
    
    229
    +  = runEndoOS (runFV f (emptyVarSet, interesting)) emptyDVarSet
    
    225 230
     
    
    226 231
     runFVSelectiveList :: InterestingVarFun -> SelectiveDFV -> [Var]
    
    227 232
     runFVSelectiveList interesting f = dVarSetElems (runFVSelective interesting f)
    

  • testsuite/tests/default/default-fail05.stderr
    1 1
     default-fail05.hs:11:10: error: [GHC-39999]
    
    2
    -    • Ambiguous type variable ‘f0’ arising from a use of ‘toList’
    
    3
    -      prevents the constraint ‘(Foldable f0)’ from being solved.
    
    4
    -      Probable fix: use a type annotation to specify what ‘f0’ should be.
    
    2
    +    • Ambiguous type variable ‘t0’ arising from a use of ‘toList’
    
    3
    +      prevents the constraint ‘(Foldable t0)’ from being solved.
    
    4
    +      Probable fix: use a type annotation to specify what ‘t0’ should be.
    
    5 5
           Potentially matching instances:
    
    6 6
             instance Foldable (Either a)
    
    7 7
               -- Defined in ‘GHC.Internal.Data.Foldable’
    
    8 8
             instance Foldable Maybe -- Defined in ‘GHC.Internal.Data.Foldable’
    
    9 9
             ...plus three others
    
    10
    -        ...plus 26 instances involving out-of-scope types
    
    10
    +        ...plus 11 instances involving out-of-scope types
    
    11 11
             (use -fprint-potential-instances to see them all)
    
    12 12
         • In the first argument of ‘($)’, namely ‘toList’
    
    13 13
           In the first argument of ‘print’, namely ‘(toList $ pure 21)’
    
    14 14
           In a stmt of a 'do' block: print (toList $ pure 21)
    
    15 15
     
    
    16 16
     default-fail05.hs:11:19: error: [GHC-39999]
    
    17
    -    • Ambiguous type variable ‘f0’ arising from a use of ‘pure’
    
    18
    -      prevents the constraint ‘(Applicative f0)’ from being solved.
    
    19
    -      Probable fix: use a type annotation to specify what ‘f0’ should be.
    
    17
    +    • Ambiguous type variable ‘t0’ arising from a use of ‘pure’
    
    18
    +      prevents the constraint ‘(Applicative t0)’ from being solved.
    
    19
    +      Probable fix: use a type annotation to specify what ‘t0’ should be.
    
    20 20
           Potentially matching instances:
    
    21 21
             instance Applicative IO -- Defined in ‘GHC.Internal.Base’
    
    22 22
             instance Applicative Maybe -- Defined in ‘GHC.Internal.Base’
    
    23 23
             ...plus six others
    
    24
    -        ...plus 9 instances involving out-of-scope types
    
    24
    +        ...plus five instances involving out-of-scope types
    
    25 25
             (use -fprint-potential-instances to see them all)
    
    26 26
         • In the second argument of ‘($)’, namely ‘pure 21’
    
    27 27
           In the first argument of ‘print’, namely ‘(toList $ pure 21)’
    
    28 28
           In a stmt of a 'do' block: print (toList $ pure 21)
    
    29 29
     
    
    30 30
     default-fail05.hs:12:3: error: [GHC-39999]
    
    31
    -    • Ambiguous type variable ‘t0’ arising from a use of ‘traverse’
    
    32
    -      prevents the constraint ‘(Traversable t0)’ from being solved.
    
    31
    +    • Ambiguous type variable ‘t1’ arising from a use of ‘traverse’
    
    32
    +      prevents the constraint ‘(Traversable t1)’ from being solved.
    
    33 33
           Relevant bindings include
    
    34
    -        main :: IO (t0 ()) (bound at default-fail05.hs:10:1)
    
    35
    -      Probable fix: use a type annotation to specify what ‘t0’ should be.
    
    34
    +        main :: IO (t1 ()) (bound at default-fail05.hs:10:1)
    
    35
    +      Probable fix: use a type annotation to specify what ‘t1’ should be.
    
    36 36
           Potentially matching instances:
    
    37 37
             instance Traversable (Either a)
    
    38 38
               -- Defined in ‘GHC.Internal.Data.Traversable’
    
    39 39
             instance Traversable Maybe
    
    40 40
               -- Defined in ‘GHC.Internal.Data.Traversable’
    
    41 41
             ...plus three others
    
    42
    -        ...plus 28 instances involving out-of-scope types
    
    42
    +        ...plus 13 instances involving out-of-scope types
    
    43 43
             (use -fprint-potential-instances to see them all)
    
    44 44
         • In a stmt of a 'do' block: traverse print (pure 42)
    
    45 45
           In the expression:
    
    ... ... @@ -51,16 +51,16 @@ default-fail05.hs:12:3: error: [GHC-39999]
    51 51
                      traverse print (pure 42)
    
    52 52
     
    
    53 53
     default-fail05.hs:12:19: error: [GHC-39999]
    
    54
    -    • Ambiguous type variable ‘t0’ arising from a use of ‘pure’
    
    55
    -      prevents the constraint ‘(Applicative t0)’ from being solved.
    
    54
    +    • Ambiguous type variable ‘t1’ arising from a use of ‘pure’
    
    55
    +      prevents the constraint ‘(Applicative t1)’ from being solved.
    
    56 56
           Relevant bindings include
    
    57
    -        main :: IO (t0 ()) (bound at default-fail05.hs:10:1)
    
    58
    -      Probable fix: use a type annotation to specify what ‘t0’ should be.
    
    57
    +        main :: IO (t1 ()) (bound at default-fail05.hs:10:1)
    
    58
    +      Probable fix: use a type annotation to specify what ‘t1’ should be.
    
    59 59
           Potentially matching instances:
    
    60 60
             instance Applicative IO -- Defined in ‘GHC.Internal.Base’
    
    61 61
             instance Applicative Maybe -- Defined in ‘GHC.Internal.Base’
    
    62 62
             ...plus six others
    
    63
    -        ...plus 9 instances involving out-of-scope types
    
    63
    +        ...plus five instances involving out-of-scope types
    
    64 64
             (use -fprint-potential-instances to see them all)
    
    65 65
         • In the second argument of ‘traverse’, namely ‘(pure 42)’
    
    66 66
           In a stmt of a 'do' block: traverse print (pure 42)
    

  • testsuite/tests/ghci.debugger/scripts/T14628.stderr
    1
    -
    
    2
    -<interactive>:4:17: error: [GHC-25897]
    
    1
    +<interactive>:4:7: error: [GHC-25897]
    
    3 2
         • Couldn't match type ‘m’ with ‘(,) a0’
    
    4
    -      Expected: StateT Int ((,) a0) ()
    
    5
    -        Actual: StateT Int m ()
    
    3
    +      Expected: (a0, ((), Int))
    
    4
    +        Actual: m ((), Int)
    
    6 5
           ‘m’ is an interactive-debugger skolem
    
    7
    -    • In the first argument of ‘runStateT’, namely ‘_result’
    
    8
    -      In the second argument of ‘($)’, namely ‘runStateT _result 0’
    
    6
    +    • In the second argument of ‘($)’, namely ‘runStateT _result 0’
    
    9 7
           In the expression: snd $ runStateT _result 0
    
    8
    +      In an equation for ‘it’: it = snd $ runStateT _result 0
    
    9
    +

  • testsuite/tests/impredicative/T27917.hs
    1
    +{-# LANGUAGE ImpredicativeTypes #-}
    
    2
    +module T27917 where
    
    3
    +
    
    4
    +pair :: forall p q. Maybe p -> q -> (p, q)
    
    5
    +pair _ _ = undefined
    
    6
    +
    
    7
    +ids :: [forall a. a -> a]
    
    8
    +ids = []
    
    9
    +
    
    10
    +use :: forall s. s -> ()
    
    11
    +use _ = ()
    
    12
    +
    
    13
    +isMaybe :: Maybe a -> ()
    
    14
    +isMaybe (Just {}) = ()
    
    15
    +
    
    16
    +-- #27917 reported that foo1 was accepted but foo2 was rejected!
    
    17
    +foo1 = \y -> (isMaybe y, use (pair y ids))
    
    18
    +foo2 = \y -> (use (pair y ids), isMaybe y)
    
    19
    +
    
    20
    +{-   y :: Maybe alpha
    
    21
    +     pair @k1 @k2 y ids
    
    22
    +
    
    23
    +Maybe k1 ~ Maybe alpha
    
    24
    +k2 ~ [ forall a. a->a ]
    
    25
    +
    
    26
    +use @ss (pair ...)
    
    27
    +
    
    28
    +ss ~ (k1, [forall a. a->a])
    
    29
    +-}

  • testsuite/tests/impredicative/all.T
    ... ... @@ -22,3 +22,4 @@ test('T17332', normal, compile_fail, [''])
    22 22
     test('expr-sig', normal, compile, [''])
    
    23 23
     test('Dict', normal, compile, [''])
    
    24 24
     test('T24676', normal, compile, [''])
    
    25
    +test('T27917', normal, compile, [''])

  • testsuite/tests/indexed-types/should_fail/T1897b.stderr
    1 1
     T1897b.hs:16:1: error: [GHC-83865]
    
    2
    -    • Couldn't match type: Depend b0
    
    3
    -                     with: Depend b
    
    4
    -      Expected: t (Depend b) -> Bool
    
    5
    -        Actual: t (Depend b0) -> Bool
    
    2
    +    • Couldn't match type: Depend a0
    
    3
    +                     with: Depend a
    
    4
    +      Expected: t (Depend a) -> Bool
    
    5
    +        Actual: t (Depend a0) -> Bool
    
    6 6
           Note: ‘Depend’ is a non-injective type family.
    
    7
    -      The type variable ‘b0’ is ambiguous
    
    7
    +      The type variable ‘a0’ is ambiguous
    
    8 8
         • In the ambiguity check for the inferred type for ‘isValid’
    
    9 9
           To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
    
    10 10
           When checking the inferred type
    
    11
    -        isValid :: forall {t :: * -> *} {b}.
    
    12
    -                   (Foldable t, Bug b) =>
    
    13
    -                   t (Depend b) -> Bool
    
    11
    +        isValid :: forall {t :: * -> *} {a}.
    
    12
    +                   (Foldable t, Bug a) =>
    
    13
    +                   t (Depend a) -> Bool
    
    14 14
     

  • testsuite/tests/typecheck/should_compile/LocalGivenEqs.hs
    ... ... @@ -22,7 +22,7 @@ it mentions b in its RHS. It thus triggers Note [Type equality cycles]
    22 22
     in GHC.Tc.Solver.Equality. That Note says we change the situation to
    
    23 23
       [G] b ~ cbv GhcPs
    
    24 24
       [G] Body b ~ cbv
    
    25
    -for some fresh CycleBreakerTv cbv. Now, our original equality looks to be let-like,
    
    25
    +for some fresh GivenCycleBreakerTv cbv. Now, our original equality looks to be let-like,
    
    26 26
     but the new cbv equality is *not* let-like -- note that the variable is on the RHS.
    
    27 27
     The solution is to consider any equality whose free variables are all at the current
    
    28 28
     level to not stop equalities from floating. These are called *local*. Because both
    

  • testsuite/tests/typecheck/should_compile/T26543_orig.hs
    1
    +-- This test is from the Description of #26543
    
    2
    +
    
    3
    +{-# LANGUAGE GHC2024, TypeAbstractions, AllowAmbiguousTypes, NoImplicitPrelude,
    
    4
    +                      TypeFamilies, UndecidableInstances #-}
    
    5
    +module T26543_orig where
    
    6
    +
    
    7
    +import Data.Kind
    
    8
    +import Control.Applicative (Applicative(..))
    
    9
    +import Prelude (type (~), ($))
    
    10
    +
    
    11
    +type CAT k = k -> k -> Type
    
    12
    +
    
    13
    +type family (~>) :: CAT k
    
    14
    +type family Ob (a :: k) :: Constraint
    
    15
    +type family UN (w :: j -> k) (wa :: k) :: j
    
    16
    +
    
    17
    +class HasBinaryProducts k where
    
    18
    +  type (a :: k) && (b :: k) :: k
    
    19
    +  withObProd :: (Ob (a :: k), Ob b) => ((Ob (a && b)) => r) -> r
    
    20
    +  (&&&) :: ((a :: k) ~> x) -> (a ~> y) -> (a ~> (x && y))
    
    21
    +
    
    22
    +data AP (f :: Type -> Type) k = A k
    
    23
    +type instance UN A (A k) = k
    
    24
    +
    
    25
    +type Ap :: CAT (AP f k)
    
    26
    +data Ap a b where
    
    27
    +  Ap :: forall {k} a b f. (Ob a, Ob b) => f (a ~> b) -> Ap (A a :: AP f k) (A b)
    
    28
    +
    
    29
    +type instance (~>) = Ap
    
    30
    +type instance Ob a = (a ~ A (UN A a), Ob (UN A a))
    
    31
    +
    
    32
    +instance (Applicative f, HasBinaryProducts k) => HasBinaryProducts (AP f k) where
    
    33
    +  type a && b = A (UN A a && UN A b)
    
    34
    +  withObProd @(A a) @(A b) r = withObProd @k @a @b r
    
    35
    +  -- (&&&) :: Ap (a :: AP f k) x -> Ap a y -> Ap a (x && y)
    
    36
    +  Ap @_ @x f &&& Ap @_ @y g = withObProd @k @x @y $ Ap (liftA2 (&&&) f g)

  • testsuite/tests/typecheck/should_compile/all.T
    ... ... @@ -971,3 +971,5 @@ test('LazyFieldAnnotations', normal, compile, [''])
    971 971
     test('T27557', normal, compile, [''])
    
    972 972
     test('T27664', normal, compile, [''])
    
    973 973
     test('T27916', normal, compile, [''])
    
    974
    +test('T26543_orig', normal, compile, [''])
    
    975
    +

  • testsuite/tests/typecheck/should_fail/T22707.stderr
    1
    -T22707.hs:6:59: error: [GHC-25897]
    
    2
    -    • Couldn't match expected type ‘i1’
    
    3
    -                  with actual type ‘m0
    
    4
    -                                      GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0’
    
    5
    -      ‘i1’ is a rigid type variable bound by
    
    6
    -        the type signature for:
    
    7
    -          t1 :: forall i2 o i1 a. Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
    
    8
    -        at T22707.hs:5:1-47
    
    9
    -    • In the expression: evalCont (t1 c) >>== \ ati1 -> return ati1 xa
    
    10
    -      In the second argument of ‘($)’, namely
    
    11
    -        ‘\ xa -> evalCont (t1 c) >>== \ ati1 -> return ati1 xa’
    
    12
    -      In the second argument of ‘runCont’, namely
    
    13
    -        ‘(ati1tti2 $ \ xa -> evalCont (t1 c) >>== \ ati1 -> return ...)’
    
    14
    -    • Relevant bindings include
    
    15
    -        ati1tti2 :: (a -> i1) -> i2 (bound at T22707.hs:6:16)
    
    16
    -        c :: Cont (i2 -> o) i1 a (bound at T22707.hs:6:4)
    
    17
    -        t1 :: Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
    
    18
    -          (bound at T22707.hs:6:1)
    
    19
    -
    
    20 1
     T22707.hs:6:72: error: [GHC-25897]
    
    21
    -    • Couldn't match type ‘o’
    
    22
    -                     with ‘m0
    
    23
    -                             GHC.Internal.Types.LiftedRep
    
    24
    -                             GHC.Internal.Types.LiftedRep
    
    25
    -                             (m0 GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0)’
    
    2
    +    • Couldn't match type ‘i2’ with ‘a -> i1’
    
    26 3
           Expected: Cont
    
    27
    -                  ((a -> i1)
    
    28
    -                   -> m0
    
    29
    -                        GHC.Internal.Types.LiftedRep
    
    30
    -                        GHC.Internal.Types.LiftedRep
    
    31
    -                        (m0 GHC.Internal.Types.LiftedRep GHC.Internal.Types.LiftedRep b0))
    
    4
    +                  ((a -> i1) -> m0 (a -> i1) (a -> i1) (m0 (a -> i1) (a -> i1) b0))
    
    32 5
                       i1
    
    33 6
                       a
    
    34 7
             Actual: Cont (i2 -> o) i1 a
    
    35
    -      ‘o’ is a rigid type variable bound by
    
    8
    +      ‘i2’ is a rigid type variable bound by
    
    36 9
             the type signature for:
    
    37 10
               t1 :: forall i2 o i1 a. Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
    
    38 11
             at T22707.hs:5:1-47
    
    ... ... @@ -40,6 +13,9 @@ T22707.hs:6:72: error: [GHC-25897]
    40 13
           In the first argument of ‘evalCont’, namely ‘(t1 c)’
    
    41 14
           In the first argument of ‘(>>==)’, namely ‘evalCont (t1 c)’
    
    42 15
         • Relevant bindings include
    
    16
    +        xa :: a (bound at T22707.hs:6:53)
    
    17
    +        ati1tti2 :: (a -> m0 (a -> i1) (a -> i1) b0) -> a -> i1
    
    18
    +          (bound at T22707.hs:6:16)
    
    43 19
             c :: Cont (i2 -> o) i1 a (bound at T22707.hs:6:4)
    
    44 20
             t1 :: Cont (i2 -> o) i1 a -> Cont o i2 (a -> i1)
    
    45 21
               (bound at T22707.hs:6:1)
    

  • testsuite/tests/typecheck/should_fail/T26543a.hs
    1
    +{-# LANGUAGE ImpredicativeTypes, PolyKinds #-}
    
    2
    +
    
    3
    +-- Should be rejected via an occurs-check in the kinds,
    
    4
    +-- but was erroneously accpeted with ImpredicativeTypes
    
    5
    +-- See !16566
    
    6
    +
    
    7
    +module T26543a where
    
    8
    +
    
    9
    +import Data.Kind
    
    10
    +import Data.Proxy
    
    11
    +
    
    12
    +h :: forall k (m :: k -> Type) (a :: k). (Proxy k, Proxy (m a))
    
    13
    +h = (Proxy, Proxy)
    
    14
    +
    
    15
    +use2 :: forall t. (Proxy t, Proxy t) -> ()
    
    16
    +use2 _ = ()
    
    17
    +
    
    18
    +test = use2 h

  • testsuite/tests/typecheck/should_fail/T26543a.stderr
    1
    +T26543a.hs:18:13: error: [GHC-27958]
    
    2
    +    • Couldn't match type ‘t0’ with ‘m0 a0’
    
    3
    +      Expected: (Proxy t0, Proxy t0)
    
    4
    +        Actual: (Proxy t0, Proxy (m0 a0))
    
    5
    +      Type variable kinds:
    
    6
    +        m0 :: t0 -> *
    
    7
    +        a0 :: t0
    
    8
    +    • In the first argument of ‘use2’, namely ‘h’
    
    9
    +      In the expression: use2 h
    
    10
    +      In an equation for ‘test’: test = use2 h

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -767,4 +767,4 @@ test('T27210', normal, compile_fail, [''])
    767 767
     test('T26532', normal, compile_fail, [''])
    
    768 768
     test('T27731', normal, compile_fail, [''])
    
    769 769
     test('T27844', normal, compile_fail, [''])
    
    770
    -
    770
    +test('T26543a', normal, compile_fail, [''])