Simon Peyton Jones pushed to branch wip/T26543b 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
    
    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
     
    
    ... ... @@ -847,43 +848,76 @@ invisibleVarsOfTypes = foldr (unionVarSet . invisibleVarsOfType) emptyVarSet
    847 848
     *                                                                      *
    
    848 849
     ********************************************************************* -}
    
    849 850
     
    
    850
    -{-# INLINE afvFolder #-}   -- so that specialization to (const True) works
    
    851
    -afvFolder :: (TyCoVar -> Bool) -> TyCoFolder (FV TyCoVarSet DM.Any)
    
    851
    +{- Note [Any-free-var folder]
    
    852
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    853
    +The "any-free-var" folders take a `check_fv` predicate (TyCoVar -> Bool) and
    
    854
    +check if any /deep/ free variable of the type/coercion satisfies it. So
    
    855
    +
    
    856
    +   anyFreeVarsOfType p ty = anyVarSet p (tyCoVarsOfType ty)
    
    857
    +
    
    858
    +Our main folders look like:
    
    859
    +
    
    860
    +   afv_type :: Type -> FV (BoundVars, TyVar -> Bool) Any
    
    861
    +
    
    862
    +where we cleverly pair the predicate with the forall-bound variables of the type
    
    863
    +in the "env" field of (FV env Any).  This way we can build the /folder/ once and
    
    864
    +for all (independent of the `check_fv` predicate), and the loop passes down
    
    865
    +the pair.
    
    866
    +
    
    867
    +The same idiom is used in `exprFreeVarsDSet` and friends.
    
    868
    +
    
    869
    +However we are careful to INLINE `atvFolder` and `runAny` so that we get those
    
    870
    +nice tight loops.
    
    871
    +-}
    
    872
    +
    
    873
    +{-# INLINE afvFolder #-}   -- See Note [Any-free-var folder]
    
    874
    +afvFolder :: TyCoFolder (FV (BoundVars, TyCoVar -> Bool) DM.Any)
    
    852 875
     -- 'afvFolder' is short for "any-free-var folder", good for checking
    
    853
    --- if any free var of a type satisfies a predicate `check_fv`
    
    854
    -afvFolder check_fv = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    855
    -                                , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    856
    -                                , tcf_hole = do_hole
    
    857
    -                                , tcf_tycobinder = addBndrFV }
    
    876
    +--   if any /deep/ free var of a type satisfies a predicate `check_fv`
    
    877
    +-- The "env" of the (FV env Any) is a pair (bvs,pred) of
    
    878
    +--    the bound variables (which are definitely not free), and
    
    879
    +--    an "interesting var" predicate which the client supplies
    
    880
    +afvFolder = TyCoFolder { tcf_view = noView  -- See Note [Free vars and synonyms]
    
    881
    +                       , tcf_tyvar = do_tcv
    
    882
    +                       , tcf_covar = do_tcv
    
    883
    +                       , tcf_hole  = do_hole
    
    884
    +                       , tcf_tycobinder = addBndrSelectiveFV }
    
    858 885
       where
    
    859
    -    do_tcv tv = MkFV $ \ bvs ->
    
    860
    -                Any (not (tv `elemVarSet` bvs) && check_fv tv)
    
    861
    -    do_hole _ = mempty    -- I'm unsure; probably never happens
    
    886
    +    -- Check the variable itself with check_fv,
    
    887
    +    -- and check its kind (this is a deep folder)
    
    888
    +    do_tcv tcv = MkFV $ \ (bvs,check_fv) ->
    
    889
    +                 Any (not (tcv `elemVarSet` bvs) &&
    
    890
    +                      (check_fv tcv || runAny check_fv (afv_type (varType tcv))))
    
    891
    +    do_hole hole = do_tcv (coHoleCoVar hole)
    
    892
    +
    
    893
    +afv_type  :: Type     -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    894
    +afv_types :: [Type]   -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    895
    +afv_co    :: Coercion -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    896
    +(afv_type, afv_types, afv_co, _) = foldTyCo afvFolder
    
    897
    +
    
    898
    +runAny :: (TyVar -> Bool) -> FV (BoundVars, TyCoVar -> Bool) DM.Any -> Bool
    
    899
    +{-# INLINE runAny #-}
    
    900
    +runAny check_fv fvs = DM.getAny (runFV fvs (emptyVarSet, check_fv))
    
    862 901
     
    
    863 902
     anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool
    
    864
    -anyFreeVarsOfType check_fv ty = DM.getAny (runFVTop (f ty))
    
    865
    -  where (f, _, _, _) = foldTyCo (afvFolder check_fv)
    
    903
    +{-# INLINE anyFreeVarsOfType #-}
    
    904
    +anyFreeVarsOfType check_fv ty = runAny check_fv (afv_type ty)
    
    866 905
     
    
    867 906
     anyFreeVarsOfTypes :: (TyCoVar -> Bool) -> [Type] -> Bool
    
    868
    -anyFreeVarsOfTypes check_fv tys = DM.getAny (runFVTop (f tys))
    
    869
    -  where (_, f, _, _) = foldTyCo (afvFolder check_fv)
    
    907
    +{-# INLINE anyFreeVarsOfTypes #-}
    
    908
    +anyFreeVarsOfTypes check_fv tys = runAny check_fv (afv_types tys)
    
    870 909
     
    
    871 910
     anyFreeVarsOfCo :: (TyCoVar -> Bool) -> Coercion -> Bool
    
    872
    -anyFreeVarsOfCo check_fv co = DM.getAny (runFVTop (f co))
    
    873
    -  where (_, _, f, _) = foldTyCo (afvFolder check_fv)
    
    911
    +anyFreeVarsOfCo check_fv co = runAny check_fv (afv_co co)
    
    874 912
     
    
    875 913
     noFreeVarsOfType :: Type -> Bool
    
    876
    -noFreeVarsOfType ty = not $ DM.getAny (runFVTop (f ty))
    
    877
    -  where (f, _, _, _) = foldTyCo (afvFolder (const True))
    
    914
    +noFreeVarsOfType ty = not $ runAny (const True) (afv_type ty)
    
    878 915
     
    
    879 916
     noFreeVarsOfTypes :: [Type] -> Bool
    
    880
    -noFreeVarsOfTypes tys = not $ DM.getAny (runFVTop (f tys))
    
    881
    -  where (_, f, _, _) = foldTyCo (afvFolder (const True))
    
    917
    +noFreeVarsOfTypes tys = not $ runAny (const True) (afv_types tys)
    
    882 918
     
    
    883 919
     noFreeVarsOfCo :: Coercion -> Bool
    
    884
    -noFreeVarsOfCo co = not $ DM.getAny (runFVTop (f co))
    
    885
    -  where (_, _, f, _) = foldTyCo (afvFolder (const True))
    
    886
    -
    
    920
    +noFreeVarsOfCo co = not $ runAny (const True) (afv_co co)
    
    887 921
     
    
    888 922
     {-
    
    889 923
     ************************************************************************
    

  • 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.Utils.Unify.checkTypeEq).
    
    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)
    
    ... ... @@ -2149,8 +2158,7 @@ foldQLInstVars check_tv ty
    2149 2158
     
    
    2150 2159
     qlUnify ::  TcType -> TcType -> TcM ()
    
    2151 2160
     -- Unify ty1 with ty2:
    
    2152
    ---   * It can unify both instantiation variables (possibly with polytypes),
    
    2153
    ---     and ordinary unification variables (but only with monotypes)
    
    2161
    +--   * It unifies /only/ instantiation variables (see (UQL6)), hopefully with polytypes
    
    2154 2162
     --   * It does not return a coercion (unlike unifyType); it is called
    
    2155 2163
     --     for the sole purpose of unifying instantiation variables, although it
    
    2156 2164
     --     may also (opportunistically) unify regular unification variables.
    
    ... ... @@ -2158,18 +2166,23 @@ qlUnify :: TcType -> TcType -> TcM ()
    2158 2166
     --   * It may return without having made the argument types equal, of course;
    
    2159 2167
     --     it just makes best efforts.
    
    2160 2168
     qlUnify ty1 ty2
    
    2161
    -  = do { traceTc "qlUnify" (ppr ty1 $$ ppr ty2)
    
    2169
    +  = do { lvl <- getTcLevel
    
    2170
    +       ; traceTc "qlUnify" (ppr lvl $$ ppr ty1 $$ ppr ty2)
    
    2162 2171
            ; go ty1 ty2 }
    
    2163 2172
       where
    
    2164 2173
         go :: TcType -> TcType -> TcM ()
    
    2165 2174
     
    
    2175
    +    go t1 t2 = do { traceTc "qlUinfy:go" (ppr t1 <+> char '~' <+> ppr t2)
    
    2176
    +                  ; go' t1 t2 }
    
    2177
    +
    
    2166 2178
         -- Decompose (arg1 -> res1) ~ (arg2 -> res2)
    
    2167 2179
         -- and         (c1 => res1) ~   (c2 => res2)
    
    2168 2180
         -- But for the latter we only learn instantiation info from res1~res2
    
    2169
    -    go (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
    
    2181
    +    go' (FunTy { ft_af = af1, ft_arg = arg1, ft_res = res1 })
    
    2170 2182
            (FunTy { ft_af = af2, ft_arg = arg2, ft_res = res2 })
    
    2171 2183
           | af1 == af2 -- Match the arrow TyCon
    
    2172
    -      = do { when (isVisibleFunArg af1) (go arg1 arg2)
    
    2184
    +      = do { traceTc "go_fun" (ppr arg1 $$ ppr arg2)
    
    2185
    +           ; when (isVisibleFunArg af1) (go arg1 arg2)
    
    2173 2186
     
    
    2174 2187
             -- NB: we do not unify the multiplicities; that would be too strong.
    
    2175 2188
             -- We might only require mult1 ⩽ mult2, as in Note [Multiplicity in deep subsumption].
    
    ... ... @@ -2178,30 +2191,29 @@ qlUnify ty1 ty2
    2178 2191
                ; go res1 res2 }
    
    2179 2192
     
    
    2180 2193
         -- Make sure to not unify "kappa := (a %1 -> b)". See (UQL5).
    
    2181
    -    go (FunTy { ft_mult = OneTy }) _ = return ()
    
    2182
    -    go _ (FunTy { ft_mult = OneTy }) = return ()
    
    2183
    -      -- NB: we do want to be able to unify "kappa := a => b", as that's
    
    2184
    -      -- the main point of QuickLook (allowing meta-variables to be unified
    
    2185
    -      -- with qualified types).
    
    2194
    +    -- NB: we /do/ want to be able to unify "kappa := a => b", as that's
    
    2195
    +    -- the main point of QuickLook (allowing meta-variables to be unified
    
    2196
    +    -- with qualified types).
    
    2197
    +    go' (FunTy { ft_mult = OneTy }) _ = return ()
    
    2198
    +    go' _ (FunTy { ft_mult = OneTy }) = return ()
    
    2186 2199
     
    
    2187
    -    go (TyVarTy tv) ty2
    
    2188
    -      | isMetaTyVar tv = go_kappa tv ty2
    
    2189
    -    go ty1 (TyVarTy tv)
    
    2190
    -      | isMetaTyVar tv = go_kappa tv ty1
    
    2200
    +    -- Type variables; see (UQL6) in Note [QuickLook unification]
    
    2201
    +    go' (TyVarTy tv) ty2 | isMetaTyVar tv = go_meta tv ty2
    
    2202
    +    go' ty1 (TyVarTy tv) | isMetaTyVar tv = go_meta tv ty1
    
    2191 2203
     
    
    2192
    -    go (CastTy ty1 _) ty2 = go ty1 ty2
    
    2193
    -    go ty1 (CastTy ty2 _) = go ty1 ty2
    
    2204
    +    go' (CastTy ty1 _) ty2 = go ty1 ty2
    
    2205
    +    go' ty1 (CastTy ty2 _) = go ty1 ty2
    
    2194 2206
     
    
    2195
    -    go (TyConApp tc1 []) (TyConApp tc2 [])
    
    2207
    +    go' (TyConApp tc1 []) (TyConApp tc2 [])
    
    2196 2208
           | tc1 == tc2 -- See GHC.Tc.Utils.Unify
    
    2197 2209
           = return ()  -- Note [Expanding synonyms during unification]
    
    2198 2210
     
    
    2199 2211
         -- Now, and only now, expand synonyms
    
    2200
    -    go rho1 rho2
    
    2212
    +    go' rho1 rho2
    
    2201 2213
           | Just rho1 <- coreView rho1 = go rho1 rho2
    
    2202 2214
           | Just rho2 <- coreView rho2 = go rho1 rho2
    
    2203 2215
     
    
    2204
    -    go (TyConApp tc1 tys1) (TyConApp tc2 tys2)
    
    2216
    +    go' (TyConApp tc1 tys1) (TyConApp tc2 tys2)
    
    2205 2217
           | tc1 == tc2
    
    2206 2218
           , not (isTypeFamilyTyCon tc1)
    
    2207 2219
           , tys1 `equalLength` tys2
    
    ... ... @@ -2209,67 +2221,79 @@ qlUnify ty1 ty2
    2209 2221
     
    
    2210 2222
         -- Don't allow unifying (a => b) with the AppTy 'arr[tau] a b'.
    
    2211 2223
         -- To ensure this, use 'tcSplitAppTyNoView_maybe' which does not split (=>).
    
    2212
    -    go (AppTy t1a t1b) ty2
    
    2224
    +    go' (AppTy t1a t1b) ty2
    
    2213 2225
           | Just (t2a, t2b) <- tcSplitAppTyNoView_maybe ty2
    
    2214 2226
           = do { go t1a t2a; go t1b t2b }
    
    2215
    -    go ty1 (AppTy t2a t2b)
    
    2227
    +    go' ty1 (AppTy t2a t2b)
    
    2216 2228
           | Just (t1a, t1b) <- tcSplitAppTyNoView_maybe ty1
    
    2217 2229
           = do { go t1a t2a; go t1b t2b }
    
    2218 2230
     
    
    2219
    -    go _ _ = return ()
    
    2231
    +    -- Type families have a monomorphic result, so if we see (say)
    
    2232
    +    --     Maybe kappa ~ F ty
    
    2233
    +    -- we know that kappa must be a monotype
    
    2234
    +    go' (TyConApp tc1 _) ty2 | isTypeFamilyTyCon tc1 = monomorphise ty2
    
    2235
    +    go' ty1 (TyConApp tc2 _) | isTypeFamilyTyCon tc2 = monomorphise ty1
    
    2236
    +
    
    2237
    +    go' _ _ = return ()
    
    2220 2238
            -- Don't look under foralls; see (UQL4) of Note [QuickLook unification]
    
    2221 2239
     
    
    2222 2240
         ----------------
    
    2223
    -    go_kappa kappa ty2
    
    2224
    -      = assertPpr (isMetaTyVar kappa) (ppr kappa) $
    
    2225
    -        do { info <- readMetaTyVar kappa
    
    2226
    -           ; case info of
    
    2241
    +    go_meta tv1 ty2
    
    2242
    +      = assertPpr (isMetaTyVar tv1) (ppr tv1) $
    
    2243
    +        do { info <- readMetaTyVar tv1
    
    2244
    +           ; case info of  -- See (UQL9)
    
    2227 2245
                    Indirect ty1 -> go ty1 ty2
    
    2228 2246
                    Flexi        -> do { ty2 <- liftZonkM $ zonkTcType ty2
    
    2229
    -                                  ; go_flexi kappa ty2 } }
    
    2247
    +                                  ; go_flexi tv1 ty2 } }
    
    2230 2248
     
    
    2231 2249
         ----------------
    
    2232
    -    -- Swap (kappa1[conc] ~ kappa2[tau])
    
    2233
    -    -- otherwise we'll fail to unify and emit a coercion.
    
    2234
    -    -- Just an optimisation: emitting a coercion is fine
    
    2235
    -    go_flexi kappa (TyVarTy tv2)
    
    2236
    -      | lhsPriority tv2 > lhsPriority kappa
    
    2237
    -      = go_flexi1 tv2 (TyVarTy kappa)
    
    2238
    -    go_flexi kappa ty2
    
    2239
    -      = go_flexi1 kappa ty2
    
    2240
    -
    
    2241
    -    go_flexi1 kappa ty2  -- ty2 is zonked
    
    2242
    -      = do { cur_lvl <- getTcLevel
    
    2243
    -              -- See Note [Unification preconditions], (UNTOUCHABLE) wrinkles
    
    2244
    -              -- Here we are in the TcM monad, which does not track enclosing
    
    2245
    -              -- Given equalities; so for quick-look unification we conservatively
    
    2246
    -              -- treat /any/ level outside this one as untouchable. Hence cur_lvl.
    
    2247
    -           ; case simpleUnifyCheck UC_QuickLook cur_lvl kappa ty2 of
    
    2248
    -              SUC_CanUnify ->
    
    2249
    -                do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
    
    2250
    -                           -- unifyKind: see (UQL2) in Note [QuickLook unification]
    
    2251
    -                           --            and (MIV2) in Note [Monomorphise instantiation variables]
    
    2252
    -                   ; let ty2' = mkCastTy ty2 co
    
    2253
    -                   ; traceTc "qlUnify:update" $
    
    2254
    -                     ppr kappa <+> text ":=" <+> ppr ty2
    
    2255
    -                   ; liftZonkM $ writeMetaTyVar kappa ty2' }
    
    2256
    -              _ -> return () -- e.g. occurs-check or forall-bound variable
    
    2257
    -           }
    
    2250
    +    -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
    
    2251
    +    -- Zonking ty2 makes the occurs-check easier
    
    2252
    +    go_flexi tv1 ty2
    
    2253
    +      | TyVarTy tv2 <- ty2
    
    2254
    +      , lhsPriority tv2 > lhsPriority tv1
    
    2255
    +      = go_flexi1 tv2 (TyVarTy tv1)
    
    2256
    +      | otherwise
    
    2257
    +      = go_flexi1 tv1 ty2
    
    2258
    +
    
    2259
    +    -- Both tv1 and ty2 are zonked; tv1 is a unfilled meta-tyvar
    
    2260
    +    go_flexi1 tv1 ty2
    
    2261
    +      | isQLInstTyVar tv1 = go_kappa tv1 ty2
    
    2262
    +      | otherwise         = monomorphise ty2
    
    2263
    +
    
    2264
    +    go_kappa kappa ty2
    
    2265
    +      | isConcreteTyVar kappa  -- See (UQL7) in Note [QuickLook unification]
    
    2266
    +      = return ()
    
    2267
    +      | anyFreeVarsOfType (== kappa) ty2
    
    2268
    +      = return ()  -- Occurs check.  See (UQL10) in Note [QuickLook unification]
    
    2269
    +      | otherwise
    
    2270
    +      = do { co <- unifyKind (Just (TypeThing ty2)) ty2_kind kappa_kind
    
    2271
    +                   -- unifyKind: see (UQL2) in Note [QuickLook unification]
    
    2272
    +                   --            and (MIV2) in Note [Monomorphise instantiation variables]
    
    2273
    +           ; let ty2' = mkCastTy ty2 co
    
    2274
    +           ; traceTc "qlUnify:update" $
    
    2275
    +             ppr kappa <+> text ":=" <+> ppr ty2
    
    2276
    +           ; liftZonkM $ writeMetaTyVar kappa ty2'
    
    2277
    +           ; return () }
    
    2258 2278
           where
    
    2259 2279
             kappa_kind = tyVarKind kappa
    
    2260 2280
             ty2_kind   = typeKind ty2
    
    2261 2281
     
    
    2282
    +    monomorphise :: TcType -> TcM ()
    
    2283
    +    monomorphise ty  -- See (UQL8)
    
    2284
    +      = do { _ <- liftZonkM $ qlMonoTcType ty; return () }
    
    2285
    +
    
    2262 2286
     {- Note [QuickLook unification]
    
    2263 2287
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2264 2288
     In qlUnify, if we find (kappa ~ ty), we are going to update kappa := ty.
    
    2265 2289
     That is the entire point of qlUnify!   Wrinkles:
    
    2266 2290
     
    
    2267
    -(UQL1) Before unifying an instantiation variable in `go_flexi`, we must check
    
    2268
    -  the usual unification conditions, by calling `GHC.Tc.Utils.Unify.simpleUnifyCheck`.
    
    2269
    -  For example that checks for
    
    2291
    +(UQL1) Before unifying an instantiation variable in `go_kappa`, we must check
    
    2292
    +  the usual unification conditions. In particular:
    
    2270 2293
         * An occurs-check
    
    2271
    -    * Level mis-match
    
    2272 2294
         * An attempt to unify a concrete type variable with a non-concrete type.
    
    2295
    +  We don't need a level-check because the level of an instantiation variable
    
    2296
    +  is infinity -- see (UQL3).
    
    2273 2297
     
    
    2274 2298
     (UQL2) What if kappa and ty have different kinds?  We simply call the
    
    2275 2299
       ordinary unifier and use the coercion to connect the two.
    
    ... ... @@ -2342,6 +2366,47 @@ That is the entire point of qlUnify! Wrinkles:
    2342 2366
       constraint arrows =>, so that (UQL4) applies to them as well.
    
    2343 2367
       See Note [Multiplicity in deep subsumption].
    
    2344 2368
     
    
    2369
    +(UQL6) What if `qlUnify` sees a regular (monotyped) unification variable, `alpha`, rather
    
    2370
    +  than an instantiation variable `kappa`?  Thus (alpha ~ ty).   It's tempting to just
    
    2371
    +  unify it, but the regular, on-the-fly unifier has lots of careful checks (levels,
    
    2372
    +  concreteness etc).  In regular unification it doesn't matter whether we unify on-the-fly
    
    2373
    +  or later -- it's just an efficiency issue -- but we must maintain all the careful invariants.
    
    2374
    +
    
    2375
    +  The simplest thing is to say that `qlUnify` unifies /only/ instantiation variables
    
    2376
    +  (see calls to `isQLInstTyVar`).  We could be more ambitious, perhaps, in future, but
    
    2377
    +  beware of accidentally causing obscure /user-visible/ consequences (#26543),
    
    2378
    +  e.g. via Note [Occurs check and coercions].
    
    2379
    +
    
    2380
    +  Remember, though, that the /only/ reason for QuickLoook is to unify instantiation variables
    
    2381
    +  with polytypes; anything else is optional, and will be picked up with later "ordinary"
    
    2382
    +  typechecking.  So unifing only instantiation variables is enough.
    
    2383
    +
    
    2384
    +(UQL7) If `qlUnify` sees a concrete `kaapa`, thus
    
    2385
    +            kappa[conc] ~ some-type
    
    2386
    +  we just give up. It's tiresome to do all the checks for concreteness; and concrete
    
    2387
    +  type variables are never unified with polytypes, so it's fine to leave it for the
    
    2388
    +  regular unifier.
    
    2389
    +
    
    2390
    +(UQL8) If we see (Maybe kappa ~ alpha), where `kappa` is an instantiation variable
    
    2391
    +  and `alpha` is a regular unification variable, we know that `kappa` must be a
    
    2392
    +  monotype, so we can use `qlMonoTcType` to monomorphise it.  This is important,
    
    2393
    +  because rule APP-LIGHTNING in the QuickLook paper only fires when there are no free
    
    2394
    +  instantiation variables.  Unifying `kappa` with a monotype makes it more possible
    
    2395
    +  for APP-LIGHTNING to fire.
    
    2396
    +
    
    2397
    +  Same if we see (Maybe kappa ~ F tya) where F is a type function, becuase the result
    
    2398
    +  of a type function is always a monotype.
    
    2399
    +
    
    2400
    +(UQL9) If we have (Maybe kappa ~ alpha) and we have /already/ unified alpha:=Maybe beta,
    
    2401
    +  then it's more efficient to look through that unification and decompose, so that
    
    2402
    +  we get (kappa ~ alpha).  That way we don't have to monomorphise `kappa`.  So we
    
    2403
    +  "look through" all meta-tyvars, not just the instantiation vars.
    
    2404
    +
    
    2405
    +(UQL10) For the occurs check in `qlUnify` we simply use `anyFreeVarsOfType`.  For
    
    2406
    +  coercions that looks at the free vars of the coercion; but for coercion /holes/
    
    2407
    +  all it can do is look at the free vars of the hole's type.
    
    2408
    +  See Note [Occurs check and coercions] in GHC.Tc.Utils.Unify
    
    2409
    +
    
    2345 2410
     Sadly discarded design alternative
    
    2346 2411
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2347 2412
     It is very tempting to use `unifyType` rather than `qlUnify`, killing off the
    

  • compiler/GHC/Tc/Solver/Equality.hs
    ... ... @@ -2584,15 +2584,15 @@ turn controls the behaviour of the workhorse: GHC.Tc.Utils.Unify.checkTyEqRhs.
    2584 2584
     
    
    2585 2585
     The details depend on whether we're working with a Given or a Wanted.
    
    2586 2586
     
    
    2587
    -Given
    
    2588
    ------
    
    2587
    +Note [Type equality cycles: Givens]
    
    2588
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2589 2589
     We emit a new Given, [G] F a ~ cbv, equating the type family application
    
    2590 2590
     to our new cbv. This is actually done by `break_given` in
    
    2591 2591
     `GHC.Tc.Solver.Monad.checkTypeEq`.
    
    2592 2592
     
    
    2593
    -Note its orientation: The type family ends up on the left; see
    
    2594
    -Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
    
    2595
    -CycleBreakerTvs is necessary. This scenario is now easily soluble, by using
    
    2593
    +Note its orientation: The type family ends up on the left;
    
    2594
    +see Note [Orienting TyFamLHS/TyFamLHS]. No special treatment for
    
    2595
    +GivenCycleBreakerTvs is necessary. This scenario is now easily soluble, by using
    
    2596 2596
     the first Given to rewrite the Wanted, which can now be solved.
    
    2597 2597
     
    
    2598 2598
     (The first Given actually also rewrites the second one, giving
    
    ... ... @@ -2613,10 +2613,11 @@ to
    2613 2613
     
    
    2614 2614
     Note that
    
    2615 2615
     * `cbv` is a fresh cycle breaker variable.
    
    2616
    -* `cbv` is a meta-tyvar, but it is completely untouchable.
    
    2616
    +* `cbv` is a meta-tyvar, of flavour GivenCycleBreakerTv, but it is
    
    2617
    +  completely untouchable.
    
    2617 2618
     * We track the cycle-breaker variables in inert_cycle_breakers in InertSet
    
    2618
    -* We eventually fill in the cycle-breakers, with `cbv := F lhs`.
    
    2619
    -  No one else fills in CycleBreakerTvs!
    
    2619
    +* We eventually fill in the cycle-breakers, with `cbv := F lhs`,
    
    2620
    +  via `restoreTyVarCycles`.  No one else fills in GivenCycleBreakerTvs!
    
    2620 2621
     * The evidence for the new `F lhs ~ cbv` constraint is Refl, because we know
    
    2621 2622
       this fill-in is ultimately going to happen.
    
    2622 2623
     * In `inert_cycle_breakers`, we remember the (cbv, F lhs) pair; that is, we
    
    ... ... @@ -2626,8 +2627,8 @@ Note that
    2626 2627
     * This fill-in is done when solving is complete, by restoreTyVarCycles
    
    2627 2628
       in nestImplicTcS and runTcSWithEvBinds.
    
    2628 2629
     
    
    2629
    -Wanted
    
    2630
    -------
    
    2630
    +Note [Type equality cycles: Wanteds]
    
    2631
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2631 2632
     First, we do not cycle-break unless the LHS is a unifiable type variable
    
    2632 2633
     See Note [Don't cycle-break Wanteds when not unifying] in GHC.Tc.Solver.Monad.
    
    2633 2634
     
    
    ... ... @@ -2686,9 +2687,9 @@ to unify the cbvs:
    2686 2687
       [W] AllEqF '[Bool] alpha
    
    2687 2688
     
    
    2688 2689
     Without the logic detailed in this Note, we're stuck here, as AllEqF cannot
    
    2689
    -reduce and alpha cannot unify. Let's instead apply our cycle-breaker approach,
    
    2690
    -just as described above. We thus invent cbv1 and cbv2 and unify
    
    2691
    -alpha := cbv1 -> cbv2, yielding (after zonking)
    
    2690
    +reduce and alpha cannot unify (occurs check). Let's instead apply our
    
    2691
    +cycle-breaker approach, just as described above. We thus invent cbv1 and cbv2
    
    2692
    +and unify alpha := (cbv1 : cbv2), yielding (after zonking)
    
    2692 2693
     
    
    2693 2694
       [W] Head (cbv1 : cbv2) ~ cbv1
    
    2694 2695
       [W] Tail (cbv1 : cbv2) ~ cbv2
    
    ... ... @@ -2726,8 +2727,8 @@ Note that we need to unify the cbvs here; if we did not, there would be
    2726 2727
     no way to solve those constraints. That's why the cycle-breakers are
    
    2727 2728
     ordinary TauTvs.
    
    2728 2729
     
    
    2729
    -How all this is implemented
    
    2730
    ----------------------------
    
    2730
    +Note [Type equality cycles: implementation]
    
    2731
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2731 2732
     We implement all this via the `TEFA_Break` constructor of `TyEqFamApp`,
    
    2732 2733
     itself stored in the `tef_fam_app` field of `TyEqFlags`, which controls
    
    2733 2734
     the behaviour of `GHC.Tc.Utils.Unify.checkTyEqRhs`.  The `TEFA_Break`
    
    ... ... @@ -2854,7 +2855,7 @@ More details:
    2854 2855
          CycleBreakerOrigin. This works for both Givens and Wanteds, as we need the
    
    2855 2856
          logic in the W case for e.g. typecheck/should_fail/T17139.  Because this
    
    2856 2857
          logic needs to work for Wanteds, too, we cannot simply look for a
    
    2857
    -     CycleBreakerTv on the left: Wanteds don't use them.
    
    2858
    +     GivenCycleBreakerTv on the left: Wanteds don't use them.
    
    2858 2859
     
    
    2859 2860
     
    
    2860 2861
     **********************************************************************
    

  • 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
    
    ... ... @@ -1942,7 +1944,8 @@ prohibitedSuperClassSolve given_loc wanted_loc
    1942 1944
     
    
    1943 1945
     {- *********************************************************************
    
    1944 1946
     *                                                                      *
    
    1945
    -    Cycle breakers
    
    1947
    +                 Given Cycle breakers
    
    1948
    +  See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
    
    1946 1949
     *                                                                      *
    
    1947 1950
     ********************************************************************* -}
    
    1948 1951
     
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -2344,12 +2344,12 @@ checkTypeEq ev eq_rel lhs rhs =
    2344 2344
             given_pred = mkNomEqPred fam_app new_ty
    
    2345 2345
             given_term = evCoercion $ mkNomReflCo new_ty  -- See Detail (4) of Note
    
    2346 2346
     
    
    2347
    -    -- See Detail (7) of the Note
    
    2347
    +    -- See Detail (7) of Note [Type equality cycles: implementation]
    
    2348 2348
         cb_loc = updateCtLocOrigin (ctEvLoc ev) CycleBreakerOrigin
    
    2349 2349
     
    
    2350 2350
     -------------------------
    
    2351 2351
     -- | Fill in CycleBreakerTvs with the variables they stand for.
    
    2352
    --- See Note [Type equality cycles] in GHC.Tc.Solver.Equality
    
    2352
    +-- See Note [Type equality cycles: Givens] in GHC.Tc.Solver.Equality
    
    2353 2353
     restoreTyVarCycles :: InertSet -> TcM ()
    
    2354 2354
     restoreTyVarCycles is
    
    2355 2355
       = 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,35 +3626,29 @@ 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 'checkTypeEq').
    
    3621 3628
     --
    
    3629
    +-- See Note [simpleUnifyCheck]
    
    3630
    +--
    
    3622 3631
     -- * Rejects if lhs_tv occurs in rhs_ty (occurs check)
    
    3623 3632
     -- * Rejects foralls unless
    
    3624 3633
     --      lhs_tv is RuntimeUnk (used by GHCi debugger)
    
    3625
    ---          or is a QL instantiation variable
    
    3626 3634
     -- * Rejects a non-concrete type if lhs_tv is concrete
    
    3627 3635
     -- * Rejects type families unless fam_ok=True
    
    3628 3636
     -- * Does a level-check for type variables, to avoid skolem escape
    
    3629 3637
     --
    
    3630 3638
     -- This function is pretty heavily used, so it's optimised not to allocate
    
    3631 3639
     simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    
    3632
    -  | not $ touchabilityTest given_eq_lvl lhs_tv
    
    3633
    -  = SUC_CannotUnify
    
    3634
    -  | not $ checkTopShape lhs_info rhs
    
    3635
    -  = SUC_CannotUnify
    
    3636
    -  | rhs_is_ok rhs
    
    3637
    -  = SUC_CanUnify
    
    3638
    -  | otherwise
    
    3639
    -  = SUC_NotSure
    
    3640
    +  | not $ touchabilityTest given_eq_lvl lhs_tv = SUC_CannotUnify
    
    3641
    +  | not $ checkTopShape lhs_info rhs           = SUC_CannotUnify
    
    3642
    +  | not (rhs_is_bad rhs)                       = SUC_CanUnify
    
    3643
    +  | otherwise                                  = SUC_NotSure
    
    3640 3644
       where
    
    3641
    -    lhs_info = metaTyVarInfo lhs_tv
    
    3642
    -
    
    3643
    -    !(occ_in_ty, occ_in_co) = mkOccFolders (tyVarName lhs_tv)
    
    3644
    -
    
    3645
    +    lhs_info           = metaTyVarInfo lhs_tv
    
    3645 3646
         lhs_tv_lvl         = tcTyVarLevel lhs_tv
    
    3646 3647
         lhs_tv_is_concrete = isConcreteTyVar lhs_tv
    
    3648
    +    lhs_tv_nm          = tyVarName lhs_tv
    
    3647 3649
     
    
    3648
    -    forall_ok = case caller of
    
    3649
    -                   UC_QuickLook -> isQLInstTyVar lhs_tv
    
    3650
    -                   _            -> isRuntimeUnkTyVar lhs_tv
    
    3650
    +    -- See Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect, esp (RUT1).
    
    3651
    +    forall_ok = isRuntimeUnkTyVar lhs_tv
    
    3651 3652
     
    
    3652 3653
         -- This fam_ok thing relates to a very specific perf problem
    
    3653 3654
         -- See Note [Prevent unification with type families]
    
    ... ... @@ -3656,58 +3657,74 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3656 3657
         --   see if it bites us)
    
    3657 3658
         fam_ok = case caller of
    
    3658 3659
                    UC_Solver     -> True
    
    3659
    -               UC_QuickLook  -> True
    
    3660 3660
                    UC_OnTheFly   -> False
    
    3661 3661
     
    
    3662
    -    rhs_is_ok (TyVarTy tv)
    
    3663
    -      | lhs_tv == tv                                    = False
    
    3664
    -      | tcTyVarLevel tv `strictlyDeeperThan` lhs_tv_lvl = False
    
    3665
    -      | lhs_tv_is_concrete, not (isConcreteTyVar tv)    = False
    
    3666
    -      | occ_in_ty $! (tyVarKind tv)                     = False
    
    3667
    -      | otherwise                                       = True
    
    3668
    -
    
    3669
    -    rhs_is_ok (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
    
    3670
    -      | not forall_ok, isInvisibleFunArg af = False
    
    3671
    -      | otherwise                           = rhs_is_ok w && rhs_is_ok a && rhs_is_ok r
    
    3672
    -
    
    3673
    -    rhs_is_ok (TyConApp tc tys)
    
    3674
    -      | lhs_tv_is_concrete, not (isConcreteTyCon tc) = False
    
    3675
    -      | not forall_ok, not (isTauTyCon tc)           = False
    
    3676
    -      | not fam_ok,    not (isFamFreeTyCon tc)       = False
    
    3677
    -      | otherwise                                    = all rhs_is_ok tys
    
    3678
    -
    
    3679
    -    rhs_is_ok (ForAllTy (Bndr tv _) ty)
    
    3680
    -      | forall_ok = rhs_is_ok (tyVarKind tv) && (tv == lhs_tv || rhs_is_ok ty)
    
    3681
    -      | otherwise = False
    
    3682
    -
    
    3683
    -    rhs_is_ok (AppTy t1 t2)    = rhs_is_ok t1 && rhs_is_ok t2
    
    3684
    -    rhs_is_ok (CastTy ty co)   = not (occ_in_co co) && rhs_is_ok ty
    
    3685
    -    rhs_is_ok (CoercionTy co)  = not (occ_in_co co)
    
    3686
    -    rhs_is_ok (LitTy {})       = True
    
    3687
    -
    
    3688
    -
    
    3689
    -mkOccFolders :: Name -> (TcType -> Bool, TcCoercion -> Bool)
    
    3690
    --- These functions return True
    
    3691
    ---   * if lhs_tv occurs (incl deeply, in the kind of variable)
    
    3692
    ---   * if there is a coercion hole
    
    3693
    --- No expansion of type synonyms
    
    3694
    -mkOccFolders lhs_tv = ( getAny . runFVTop . check_ty
    
    3695
    -                      , getAny . runFVTop . check_co)
    
    3696
    -  where
    
    3697
    -    check_ty :: Type -> FV BoundVars Any
    
    3698
    -    !(check_ty, _, check_co, _) = foldTyCo occ_folder
    
    3662
    +    rhs_tcv_is_bad tcv
    
    3663
    +       -- c.f. checkTyVar, the TEFTyVar case
    
    3664
    +       | isCoVar tcv                                      = occ_check
    
    3665
    +       | tcTyVarLevel tcv `strictlyDeeperThan` lhs_tv_lvl = True
    
    3666
    +       | lhs_tv_is_concrete, not (isConcreteTyVar tcv)    = True
    
    3667
    +       | occ_check                                        = True
    
    3668
    +       | otherwise                                        = False
    
    3669
    +       where
    
    3670
    +         occ_check = simpleOccursCheck lhs_tv_nm tcv
    
    3671
    +
    
    3672
    +    rhs_is_bad (TyVarTy tv) = rhs_tcv_is_bad tv
    
    3673
    +
    
    3674
    +    rhs_is_bad (FunTy {ft_af = af, ft_mult = w, ft_arg = a, ft_res = r})
    
    3675
    +      | not forall_ok, isInvisibleFunArg af = True
    
    3676
    +      | otherwise                           = rhs_is_bad w || rhs_is_bad a || rhs_is_bad r
    
    3677
    +
    
    3678
    +    rhs_is_bad (TyConApp tc tys)
    
    3679
    +      | lhs_tv_is_concrete, not (isConcreteTyCon tc) = True
    
    3680
    +      | not forall_ok, not (isTauTyCon tc)           = True
    
    3681
    +      | not fam_ok,    not (isFamFreeTyCon tc)       = True
    
    3682
    +      | otherwise                                    = any rhs_is_bad tys
    
    3683
    +
    
    3684
    +    rhs_is_bad (ForAllTy (Bndr tv _) ty)
    
    3685
    +      | not forall_ok               = True
    
    3686
    +      | rhs_is_bad (tyVarKind tv)   = True
    
    3687
    +      | tv /= lhs_tv, rhs_is_bad ty = True
    
    3688
    +      | otherwise                   = False
    
    3689
    +
    
    3690
    +    rhs_is_bad (AppTy t1 t2)    = rhs_is_bad t1 || rhs_is_bad t2
    
    3691
    +    rhs_is_bad (CastTy ty co)   = co_is_bad co || rhs_is_bad ty
    
    3692
    +    rhs_is_bad (CoercionTy co)  = co_is_bad co
    
    3693
    +    rhs_is_bad (LitTy {})       = False
    
    3694
    +
    
    3695
    +    -- Coercions: see Note [Occurs check and coercions]
    
    3696
    +    co_is_bad co = anyFreeVarsOfCo rhs_tcv_is_bad co
    
    3697
    +
    
    3698
    +{- Note [simpleUnifyCheck]
    
    3699
    +~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3700
    +The function `simpleUnifyCheck` is a simple, /fast/ check for unifying (tv ~ rhs).
    
    3701
    +It can return a definite decision (SUC_CannotUnify of SUC_CanUnify), or uncertainty
    
    3702
    +(SUC_NotSure).  In the latter case we will later use `checkTyEqRhs` to resolve.
    
    3703
    +In particular, `simpleUnifyCheck`:
    
    3704
    +
    
    3705
    +* Rejects if lhs_tv occurs in rhs_ty
    
    3706
    +* Rejects foralls unless
    
    3707
    +      lhs_tv is RuntimeUnk (used by GHCi debugger)
    
    3708
    +* Rejects a non-concrete type if lhs_tv is concrete
    
    3709
    +* Rejects type families unless fam_ok=True
    
    3710
    +* Does a level-check for type variables, to avoid skolem escape
    
    3699 3711
     
    
    3700
    -    occ_folder :: TyCoFolder (FV BoundVars Any)
    
    3701
    -    occ_folder = TyCoFolder { tcf_view  = noView  -- Don't expand synonyms
    
    3702
    -                            , tcf_tyvar = do_tcv, tcf_covar = do_tcv
    
    3703
    -                            , tcf_hole  = do_hole
    
    3704
    -                            , tcf_tycobinder = addBndrFV }
    
    3712
    +This function is pretty heavily used, so it's optimised not to allocate.
    
    3705 3713
     
    
    3706
    -    do_tcv v = (MkFV $ \ bvs ->
    
    3707
    -                Any (not (v `elemVarSet` bvs) && tyVarName v == lhs_tv))
    
    3708
    -               `mappend` check_ty (varType v)
    
    3714
    +Note [Occurs check and coercions]
    
    3715
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3716
    +What if we have
    
    3717
    +    alpha ~ Int |>  (CoHole ch :: Type ~ blah)
    
    3718
    +
    
    3719
    +Clearly we want to check that `alpha` doesn't appear free in `blah`.  But even if
    
    3720
    +it doesn't, what if `ch` is later filled in with a coercion mentioning `alpha`?
    
    3721
    +Tht would be bad: we'd get an infinite structure.
    
    3709 3722
     
    
    3710
    -    do_hole _hole = MkFV $ \ _bvs -> DM.Any True  -- Reject coercion holes
    
    3723
    +The Right Thing to do is to check for a loop when (later) filling in `ch`.
    
    3724
    +We don't do that right now, and it's very obscure.  But in `simpleUnifyCheck`
    
    3725
    +we just look in the free vars of the /kind/ of the hole, which is what
    
    3726
    +`anyFreeVarsOfCo` does.
    
    3727
    +-}
    
    3711 3728
     
    
    3712 3729
     {- *********************************************************************
    
    3713 3730
     *                                                                      *
    
    ... ... @@ -4081,13 +4098,14 @@ wantConcreteCheck = \case
    4081 4098
     -- where @cbv@ is a fresh loop-breaker tyvar (for Given), or
    
    4082 4099
     -- just a fresh 'TauTv' (for Wanted)
    
    4083 4100
     famAppBreaker :: FamAppBreaker a -> TcType -> TcM (PuResult a Reduction)
    
    4084
    -famAppBreaker BreakGiven fam_app
    
    4101
    +famAppBreaker BreakGiven fam_app    -- Givens
    
    4085 4102
        = do { new_tv <- TcM.newCycleBreakerTyVar (typeKind fam_app)
    
    4086 4103
             ; return (PuOK (unitBag (new_tv, fam_app))
    
    4087 4104
                            (mkReflRedn Nominal (mkTyVarTy new_tv))) }
    
    4088 4105
                      -- Why reflexive? See Detail (4) of Note [Type equality cycles]
    
    4089 4106
                      -- in GHC.Tc.Solver.Equality
    
    4090
    -famAppBreaker (BreakWanted ev lhs_tv) fam_app
    
    4107
    +
    
    4108
    +famAppBreaker (BreakWanted ev lhs_tv) fam_app   -- Wanteds
    
    4091 4109
       -- Occurs check or skolem escape; so flatten
    
    4092 4110
       = do { reason <- checkPromoteFreeVars cteInsolubleOccurs
    
    4093 4111
                          (tyVarName lhs_tv) lhs_tv_lvl
    
    ... ... @@ -4222,10 +4240,16 @@ Notably, it can check for things like:
    4222 4240
     Its specific behaviour is governed by the `TyEqFlags` that are passed
    
    4223 4241
     to it; see Note [TyEqFlags].
    
    4224 4242
     
    
    4225
    -Note, however, that `checkTyEqRhs` specifically does /not/ check for:
    
    4226
    -  * Touchability of the LHS (in the case of a unification variable)
    
    4227
    -  * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
    
    4228
    -These things are checked by `simpleUnifyCheck`.
    
    4243
    +Wrinkles
    
    4244
    +
    
    4245
    +(CTER1) Note, however, that `checkTyEqRhs` specifically does /not/ check for:
    
    4246
    +         * Touchability of the LHS (in the case of a unification variable)
    
    4247
    +         * Shape of the LHS (e.g. we can't unify Int with a TyVarTv)
    
    4248
    +    These things are checked by `simpleUnifyCheck`.
    
    4249
    +
    
    4250
    +(CTER2) Also `checkTyEqRhs` does not need to worry about RuntimeUnkTvs, because
    
    4251
    +    the solver is never called on constraints involving them.  See (RUT1) in
    
    4252
    +    Note [RuntimeUnkTv] in GHC.Runtime.Heap.Inspect.
    
    4229 4253
     -}
    
    4230 4254
     
    
    4231 4255
     
    
    ... ... @@ -4283,6 +4307,7 @@ check_ty_eq_rhs :: forall m a
    4283 4307
                     => TyEqFlags m a
    
    4284 4308
                     -> TcType           -- Already zonked
    
    4285 4309
                     -> m (PuResult a Reduction)
    
    4310
    +{-# INLINEABLE check_ty_eq_rhs #-}
    
    4286 4311
     check_ty_eq_rhs flags ty
    
    4287 4312
       = case ty of
    
    4288 4313
           LitTy {}        -> return $ okCheckRefl ty
    
    ... ... @@ -4313,7 +4338,7 @@ check_ty_eq_rhs flags ty
    4313 4338
                               ; return (mkReflCoRedn Nominal <$> co_res) }
    
    4314 4339
     
    
    4315 4340
           ForAllTy {}   -> return $ PuFail impredicativeProblem -- Not allowed (TyEq:F)
    
    4316
    -{-# INLINEABLE check_ty_eq_rhs #-}
    
    4341
    +         -- No need to worry about RuntimeUnkTv
    
    4317 4342
     
    
    4318 4343
     -------------------
    
    4319 4344
     checkCo :: Monad m => TyEqFlags m a -> Coercion -> m (PuResult a Coercion)
    
    ... ... @@ -4337,7 +4362,10 @@ checkCo flags co =
    4337 4362
             -- Occurs check (can promote)
    
    4338 4363
             | OC_Check lhs_tv occ_prob <- occ
    
    4339 4364
             , LC_Promote { lc_lvlp = lhs_tv_lvl } <- lc
    
    4340
    -        -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl (tyCoVarsOfCo co)
    
    4365
    +        -> do { reason <- checkPromoteFreeVars occ_prob lhs_tv lhs_tv_lvl $
    
    4366
    +                          tyCoVarsOfCo co
    
    4367
    +                           -- See Note [Occurs check and coercions]
    
    4368
    +                          --  and Note [checkCo] (2)
    
    4341 4369
                   ; return $
    
    4342 4370
                     if cterHasNoProblem reason
    
    4343 4371
                     then pure co
    
    ... ... @@ -4634,7 +4662,7 @@ checkTyVar flags occ_tv
    4634 4662
                       -- so the only reason for a filled meta-tyvar is promotion
    
    4635 4663
     
    
    4636 4664
                     else do_rhs_checks
    
    4637
    -                     [ simpleOccursCheck      occ  occ_tv
    
    4665
    +                     [ tyVarOccursCheck      occ  occ_tv
    
    4638 4666
                          , tyVarLevelCheck        lc   occ_tv
    
    4639 4667
                          , tyVarConcretenessCheck conc occ_tv ]
    
    4640 4668
                   }
    
    ... ... @@ -4692,17 +4720,24 @@ promotionDone occ_tv _ (CC_Promote {}) = isFilledMetaTyVar occ_tv
    4692 4720
     promotionDone _      _               _ = return False
    
    4693 4721
     
    
    4694 4722
     ---------------------
    
    4695
    -simpleOccursCheck :: OccursCheck -> TcTyVar -> TyVarCheckResult m
    
    4723
    +tyVarOccursCheck :: OccursCheck -> TcTyCoVar -> TyVarCheckResult m
    
    4696 4724
     -- ^ The "interpreter" for 'OccursCheck'
    
    4697
    -simpleOccursCheck OC_None _
    
    4698
    -  = TyVarCheck_Success
    
    4699
    -simpleOccursCheck (OC_Check lhs_tv occ_prob) occ_tv
    
    4700
    -  | lhs_tv == tyVarName occ_tv || check_kind (tyVarKind occ_tv)
    
    4701
    -  = TyVarCheck_Error (cteProblem occ_prob)
    
    4702
    -  | otherwise
    
    4725
    +-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
    
    4726
    +tyVarOccursCheck OC_None _
    
    4703 4727
       = TyVarCheck_Success
    
    4728
    +tyVarOccursCheck (OC_Check { occurs_tv_name = lhs_tv, occurs_problem = prob }) occ_tcv
    
    4729
    +  | simpleOccursCheck lhs_tv occ_tcv = TyVarCheck_Error (cteProblem prob)
    
    4730
    +  | otherwise                        = TyVarCheck_Success
    
    4731
    +
    
    4732
    +simpleOccursCheck :: Name -> TyCoVar -> Bool
    
    4733
    +-- Check for an occurrence of lhs_tv in occ_tv /or/ its kind
    
    4734
    +-- True <=> occurs check fires
    
    4735
    +simpleOccursCheck lhs_tv occ_tcv
    
    4736
    +  = is_bad occ_tcv || anyFreeVarsOfType is_bad (varType occ_tcv)
    
    4737
    +    -- anyFreeVarsOfType looks at the /deep/ free vars of the type
    
    4704 4738
       where
    
    4705
    -    (check_kind, _) = mkOccFolders lhs_tv
    
    4739
    +    is_bad :: TyCoVar -> Bool
    
    4740
    +    is_bad occ_tcv = lhs_tv == tyVarName occ_tcv
    
    4706 4741
     
    
    4707 4742
     -------------------------
    
    4708 4743
     tyVarLevelCheck :: LevelCheck m -> TcTyVar -> TyVarCheckResult m
    
    ... ... @@ -4823,7 +4858,7 @@ checkTopShape info xi
    4823 4858
                             RuntimeUnk  -> True
    
    4824 4859
                             MetaTv { mtv_info = TyVarTv } -> True
    
    4825 4860
                             _                             -> False
    
    4826
    -      CycleBreakerTv -> False  -- We never unify these
    
    4861
    +      GivenCycleBreakerTv -> False  -- We never unify these
    
    4827 4862
           _ -> True
    
    4828 4863
     
    
    4829 4864
     --------------------------------------------------------------------------------
    
    ... ... @@ -5136,14 +5171,14 @@ mightEqualLater inert_set given_pred given_loc wanted_pred wanted_loc
    5136 5171
     
    
    5137 5172
         -- True for TauTv and TyVarTv (and RuntimeUnkTv) meta-tyvars
    
    5138 5173
         -- (as they can be unified)
    
    5139
    -    -- and also for CycleBreakerTvs that mentions meta-tyvars
    
    5174
    +    -- and also for GivenCycleBreakerTvs that mentions meta-tyvars
    
    5140 5175
         mentions_meta_ty_var :: TyVar -> Bool
    
    5141 5176
         mentions_meta_ty_var tv
    
    5142 5177
           | isMetaTyVar tv
    
    5143 5178
           = case metaTyVarInfo tv of
    
    5144 5179
               -- See Examples 8 and 9 in the Note
    
    5145
    -          CycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
    
    5146
    -                              (lookupCycleBreakerVar tv inert_set)
    
    5180
    +          GivenCycleBreakerTv -> anyFreeVarsOfType mentions_meta_ty_var
    
    5181
    +                                    (lookupCycleBreakerVar tv inert_set)
    
    5147 5182
               _ -> True
    
    5148 5183
           | otherwise
    
    5149 5184
           = False
    
    ... ... @@ -5186,7 +5221,7 @@ We want to solve w1 using g1 and g2. The presence of g3 is irrelevant, because
    5186 5221
     we cannot unify 'f[tau:1] v[tau:1]' and 'g[sk:1] x[sk:3]' due to skolem escape.
    
    5187 5222
     -}
    
    5188 5223
     
    
    5189
    --- | Return the type family application a CycleBreakerTv maps to.
    
    5224
    +-- | Return the type family application a GivenCycleBreakerTv maps to.
    
    5190 5225
     lookupCycleBreakerVar :: TcTyVar    -- ^ cbv, must be a CycleBreakerTv
    
    5191 5226
                           -> InertSet
    
    5192 5227
                           -> 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
    ... ... @@ -763,3 +763,4 @@ test('T26861', normal, compile_fail, [''])
    763 763
     test('T26862', normal, compile_fail, [''])
    
    764 764
     test('T27210', normal, compile_fail, [''])
    
    765 765
     test('T26532', normal, compile_fail, [''])
    
    766
    +test('T26543a', normal, compile_fail, [''])