Simon Peyton Jones pushed to branch wip/T26543b at Glasgow Haskell Compiler / GHC

Commits:

9 changed files:

Changes:

  • compiler/GHC/Core/TyCo/FVs.hs
    ... ... @@ -55,7 +55,6 @@ module GHC.Core.TyCo.FVs
    55 55
       ) where
    
    56 56
     
    
    57 57
     import GHC.Prelude
    
    58
    -import GHC.Exts( inline )
    
    59 58
     
    
    60 59
     import {-# SOURCE #-} GHC.Core.Type( partitionInvisibleTypes, coreView, rewriterView )
    
    61 60
     
    
    ... ... @@ -852,8 +851,11 @@ invisibleVarsOfTypes = foldr (unionVarSet . invisibleVarsOfType) emptyVarSet
    852 851
     {- Note [Any-free-var folder]
    
    853 852
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    854 853
     The "any-free-var" folders take a `check_fv` predicate (TyCoVar -> Bool) and
    
    855
    -check if any free variable of the type/coercion satisfies it.  Our main folders
    
    856
    -look like:
    
    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:
    
    857 859
     
    
    858 860
        afv_type :: Type -> FV (BoundVars, TyVar -> Bool) Any
    
    859 861
     
    
    ... ... @@ -871,7 +873,7 @@ nice tight loops.
    871 873
     {-# INLINE afvFolder #-}   -- See Note [Any-free-var folder]
    
    872 874
     afvFolder :: TyCoFolder (FV (BoundVars, TyCoVar -> Bool) DM.Any)
    
    873 875
     -- 'afvFolder' is short for "any-free-var folder", good for checking
    
    874
    ---   if any shallow free var of a type satisfies a predicate `check_fv`
    
    876
    +--   if any /deep/ free var of a type satisfies a predicate `check_fv`
    
    875 877
     -- The "env" of the (FV env Any) is a pair (bvs,pred) of
    
    876 878
     --    the bound variables (which are definitely not free), and
    
    877 879
     --    an "interesting var" predicate which the client supplies
    
    ... ... @@ -881,8 +883,11 @@ afvFolder = TyCoFolder { tcf_view = noView -- See Note [Free vars and synonyms]
    881 883
                            , tcf_hole  = do_hole
    
    882 884
                            , tcf_tycobinder = addBndrSelectiveFV }
    
    883 885
       where
    
    884
    -    do_tcv tv    = MkFV $ \ (bvs,check_fv) ->
    
    885
    -                   Any (not (tv `elemVarSet` bvs) && check_fv tv)
    
    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))))
    
    886 891
         do_hole hole = do_tcv (coHoleCoVar hole)
    
    887 892
     
    
    888 893
     afv_type  :: Type     -> FV (BoundVars, TyVar -> Bool) DM.Any
    
    ... ... @@ -892,7 +897,7 @@ afv_co :: Coercion -> FV (BoundVars, TyVar -> Bool) DM.Any
    892 897
     
    
    893 898
     runAny :: (TyVar -> Bool) -> FV (BoundVars, TyCoVar -> Bool) DM.Any -> Bool
    
    894 899
     {-# INLINE runAny #-}
    
    895
    -runAny check_fv fvs = DM.getAny $ runFV fvs (emptyVarSet, check_fv)
    
    900
    +runAny check_fv fvs = DM.getAny (runFV fvs (emptyVarSet, check_fv))
    
    896 901
     
    
    897 902
     anyFreeVarsOfType :: (TyCoVar -> Bool) -> Type -> Bool
    
    898 903
     {-# INLINE anyFreeVarsOfType #-}
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -2196,10 +2196,9 @@ qlUnify ty1 ty2
    2196 2196
           -- the main point of QuickLook (allowing meta-variables to be unified
    
    2197 2197
           -- with qualified types).
    
    2198 2198
     
    
    2199
    -    go' (TyVarTy tv) ty2
    
    2200
    -      | isQLInstTyVar tv = go_kappa tv ty2
    
    2201
    -    go' ty1 (TyVarTy tv)
    
    2202
    -      | isQLInstTyVar tv = go_kappa tv ty1
    
    2199
    +    -- Type variables; see (UQL6) in Note [QuickLook unification]
    
    2200
    +    go' (TyVarTy tv) ty2 | isQLInstTyVar tv = go_kappa tv ty2
    
    2201
    +    go' ty1 (TyVarTy tv) | isQLInstTyVar tv = go_kappa tv ty1
    
    2203 2202
     
    
    2204 2203
         go' (CastTy ty1 _) ty2 = go ty1 ty2
    
    2205 2204
         go' ty1 (CastTy ty2 _) = go ty1 ty2
    
    ... ... @@ -2242,6 +2241,8 @@ qlUnify ty1 ty2
    2242 2241
     
    
    2243 2242
         ----------------
    
    2244 2243
         go_flexi kappa ty2  -- ty2 is zonked
    
    2244
    +      | isConcreteTyVar kappa  -- See (UQL7) in Note [QuickLook unification]
    
    2245
    +      = return ()
    
    2245 2246
           | anyFreeVarsOfType (== kappa) ty2
    
    2246 2247
           = return ()  -- Occurs check
    
    2247 2248
           | otherwise
    
    ... ... @@ -2340,6 +2341,22 @@ That is the entire point of qlUnify! Wrinkles:
    2340 2341
       constraint arrows =>, so that (UQL4) applies to them as well.
    
    2341 2342
       See Note [Multiplicity in deep subsumption].
    
    2342 2343
     
    
    2344
    +(UQL6) What if `qlUnify` sees a regular (monotyped) unification variable, `alpha`, rather
    
    2345
    +  than an instantiation variable `kappa`?  Thus (alpha ~ ty).   It's tempting to just
    
    2346
    +  unify it, but the regular, on-the-fly unifier has lots of careful checks (levels,
    
    2347
    +  concreteness etc).  In regular unification it doesn't matter whether we unify on-the-fly
    
    2348
    +  or later -- it's just an efficiency issue -- but here it matters because `qlUnify` has
    
    2349
    +  /user-visible/ consequences.   This led to #26543.
    
    2350
    +
    
    2351
    +  The simplest thing is to say that `qlUnify` unifies /only/ instantiation variables
    
    2352
    +  (see calls to `isQLInstTyVar`).  We could be more ambitious, perhaps, in future.
    
    2353
    +
    
    2354
    +(UQL7) If `qlUnify` sees a concrete `kaapa`, thus
    
    2355
    +            kappa[conc] ~ some-type
    
    2356
    +  we just give up. It's tiresome to do all the checks for concreteness; and concrete
    
    2357
    +  type variables are never unified with polytypes, so it's fine to leave it for the
    
    2358
    +  regular unifier.
    
    2359
    +
    
    2343 2360
     Sadly discarded design alternative
    
    2344 2361
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2345 2362
     It is very tempting to use `unifyType` rather than `qlUnify`, killing off the
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -3596,7 +3596,6 @@ matchExpectedFunKind hs_ty n k = go n k
    3596 3596
     
    
    3597 3597
     data UnifyCheckCaller
    
    3598 3598
       = UC_OnTheFly   -- Called from the on-the-fly unifier
    
    3599
    -  | UC_QuickLook  -- Called from Quick Look
    
    3600 3599
       | UC_Solver     -- Called from constraint solver
    
    3601 3600
     
    
    3602 3601
     -- | The result type of 'simpleUnifyCheck'.
    
    ... ... @@ -3649,9 +3648,7 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3649 3648
         lhs_tv_is_concrete = isConcreteTyVar lhs_tv
    
    3650 3649
         lhs_tv_nm          = tyVarName lhs_tv
    
    3651 3650
     
    
    3652
    -    forall_ok = case caller of
    
    3653
    -                   UC_QuickLook -> isQLInstTyVar lhs_tv
    
    3654
    -                   _            -> isRuntimeUnkTyVar lhs_tv
    
    3651
    +    forall_ok = isRuntimeUnkTyVar lhs_tv
    
    3655 3652
     
    
    3656 3653
         -- This fam_ok thing relates to a very specific perf problem
    
    3657 3654
         -- See Note [Prevent unification with type families]
    
    ... ... @@ -3660,7 +3657,6 @@ simpleUnifyCheck caller given_eq_lvl lhs_tv rhs
    3660 3657
         --   see if it bites us)
    
    3661 3658
         fam_ok = case caller of
    
    3662 3659
                    UC_Solver     -> True
    
    3663
    -               UC_QuickLook  -> True
    
    3664 3660
                    UC_OnTheFly   -> False
    
    3665 3661
     
    
    3666 3662
         rhs_tcv_is_bad tcv
    

  • 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/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_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 T26543 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/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, [''])