Simon Peyton Jones pushed to branch wip/T26331 at Glasgow Haskell Compiler / GHC
Commits:
-
00478944
by Simon Peyton Jones at 2025-08-27T16:48:30+01:00
-
a7884589
by Simon Peyton Jones at 2025-08-28T11:08:23+01:00
-
8adfc222
by sheaf at 2025-08-28T19:47:17-04:00
-
1ad0218f
by Simon Peyton Jones at 2025-08-29T09:53:13+01:00
22 changed files:
- compiler/GHC/Core/TyCo/Compare.hs
- compiler/GHC/Core/Unify.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Gen/Bind.hs
- compiler/GHC/Tc/Gen/Expr.hs
- compiler/GHC/Tc/Gen/Expr.hs-boot
- compiler/GHC/Tc/Gen/Head.hs
- compiler/GHC/Tc/Gen/HsType.hs
- compiler/GHC/Tc/Gen/Match.hs
- compiler/GHC/Tc/Gen/Pat.hs
- compiler/GHC/Tc/Module.hs
- compiler/GHC/Tc/Types/Evidence.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/TcType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- + testsuite/tests/patsyn/should_compile/T26331.hs
- + testsuite/tests/patsyn/should_compile/T26331a.hs
- testsuite/tests/patsyn/should_compile/all.T
- + testsuite/tests/typecheck/should_compile/T26346.hs
- + testsuite/tests/typecheck/should_compile/T26350.hs
- + testsuite/tests/typecheck/should_compile/T26358.hs
- testsuite/tests/typecheck/should_compile/all.T
Changes:
| ... | ... | @@ -229,6 +229,8 @@ tcEqTyConApps tc1 args1 tc2 args2 |
| 229 | 229 | = tc1 == tc2 && tcEqTyConAppArgs args1 args2
|
| 230 | 230 | |
| 231 | 231 | tcEqTyConAppArgs :: [Type] -> [Type] -> Bool
|
| 232 | +-- Args do not have to have equal length;
|
|
| 233 | +-- we discard the excess of the longer one
|
|
| 232 | 234 | tcEqTyConAppArgs args1 args2
|
| 233 | 235 | = and (zipWith tcEqTypeNoKindCheck args1 args2)
|
| 234 | 236 | -- No kind check necessary: if both arguments are well typed, then
|
| ... | ... | @@ -245,16 +245,21 @@ give up on), but for /substitutivity/. If we have (F x x), we can see that (F x |
| 245 | 245 | can reduce to Double. So, it had better be the case that (F blah blah) can
|
| 246 | 246 | reduce to Double, no matter what (blah) is!
|
| 247 | 247 | |
| 248 | -To achieve this, `go_fam` in `uVarOrFam` does this;
|
|
| 248 | +To achieve this, `go` in `uVarOrFam` does this;
|
|
| 249 | + |
|
| 250 | +* We maintain /two/ substitutions, not just one:
|
|
| 251 | + * um_tv_env: the regular substitution, mapping TyVar :-> Type
|
|
| 252 | + * um_fam_env: maps (TyCon,[Type]) :-> Type, where the LHS is a type-fam application
|
|
| 253 | + In effect, these constitute one substitution mapping
|
|
| 254 | + CanEqLHS :-> Types
|
|
| 249 | 255 | |
| 250 | 256 | * When we attempt to unify (G Float) ~ Int, we return MaybeApart..
|
| 251 | - but we /also/ extend a "family substitution" [G Float :-> Int],
|
|
| 252 | - in `um_fam_env`, alongside the regular [tyvar :-> type] substitution in
|
|
| 253 | - `um_tv_env`. See the `BindMe` case of `go_fam` in `uVarOrFam`.
|
|
| 257 | + but we /also/ add a "family substitution" [G Float :-> Int],
|
|
| 258 | + to `um_fam_env`. See the `BindMe` case of `go` in `uVarOrFam`.
|
|
| 254 | 259 | |
| 255 | 260 | * When we later encounter (G Float) ~ Bool, we apply the family substitution,
|
| 256 | 261 | very much as we apply the conventional [tyvar :-> type] substitution
|
| 257 | - when we encounter a type variable. See the `lookupFamEnv` in `go_fam` in
|
|
| 262 | + when we encounter a type variable. See the `lookupFamEnv` in `go` in
|
|
| 258 | 263 | `uVarOrFam`.
|
| 259 | 264 | |
| 260 | 265 | So (G Float ~ Bool) becomes (Int ~ Bool) which is SurelyApart. Bingo.
|
| ... | ... | @@ -329,7 +334,7 @@ Wrinkles |
| 329 | 334 | alternative path. So `noMatchableGivenDicts` must return False;
|
| 330 | 335 | so `mightMatchLater` must return False; so when um_bind_fam_fun returns
|
| 331 | 336 | `DontBindMe`, the unifier must return `SurelyApart`, not `MaybeApart`. See
|
| 332 | - `go_fam` in `uVarOrFam`
|
|
| 337 | + `go` in `uVarOrFam`
|
|
| 333 | 338 | |
| 334 | 339 | (ATF6) When /matching/ can we ever have a type-family application on the LHS, in
|
| 335 | 340 | the template? You might think not, because type-class-instance and
|
| ... | ... | @@ -426,6 +431,9 @@ Wrinkles |
| 426 | 431 | (ATF12) There is a horrid exception for the injectivity check. See (UR1) in
|
| 427 | 432 | in Note [Specification of unification].
|
| 428 | 433 | |
| 434 | +(ATF13) We have to be careful about the occurs check.
|
|
| 435 | + See Note [The occurs check in the Core unifier]
|
|
| 436 | + |
|
| 429 | 437 | SIDE NOTE. The paper "Closed type families with overlapping equations"
|
| 430 | 438 | http://research.microsoft.com/en-us/um/people/simonpj/papers/ext-f/axioms-extended.pdf
|
| 431 | 439 | tries to achieve the same effect with a standard yes/no unifier, by "flattening"
|
| ... | ... | @@ -449,6 +457,49 @@ and all is lost. But with the current algorithm we have that |
| 449 | 457 | a a ~ (Var A) (Var B)
|
| 450 | 458 | is SurelyApart, so the first equation definitely doesn't match and we can try the
|
| 451 | 459 | second, which does. END OF SIDE NOTE.
|
| 460 | + |
|
| 461 | +Note [Shortcomings of the apartness test]
|
|
| 462 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 463 | +Note [Apartness and type families] is very clever.
|
|
| 464 | + |
|
| 465 | +But it still has shortcomings (#26358). Consider unifying
|
|
| 466 | + [F a, F Int, Int] ~ [Bool, Char, a]
|
|
| 467 | +Working left to right you might think we would build the mapping
|
|
| 468 | + F a :-> Bool
|
|
| 469 | + F Int :-> Char
|
|
| 470 | +Now we discover that `a` unifies with `Int`. So really these two lists are Apart
|
|
| 471 | +because F Int can't be both Bool and Char.
|
|
| 472 | + |
|
| 473 | +Just the same applies when adding a type-family binding to um_fam_env:
|
|
| 474 | + [F (G Float), F Int, G Float] ~ [Bool, Char, Iont]
|
|
| 475 | +Again these are Apart, because (G Float = Int),
|
|
| 476 | +and (F Int) can't be both Bool and Char
|
|
| 477 | + |
|
| 478 | +But achieving this is very tricky! Perhaps whenever we unify a type variable,
|
|
| 479 | +or a type family, we should run it over the domain and (maybe range) of the
|
|
| 480 | +type-family mapping too? Sigh.
|
|
| 481 | + |
|
| 482 | +For now we make no such attempt.
|
|
| 483 | +* The um_fam_env has only /un-substituted/ types.
|
|
| 484 | +* We look up only /un-substituted/ types in um_fam_env
|
|
| 485 | + |
|
| 486 | +This may make us say MaybeApart when we could say SurelyApart, but it has no
|
|
| 487 | +effect on the correctness of unification: if we return Unifiable, it really is
|
|
| 488 | +Unifiable.
|
|
| 489 | + |
|
| 490 | +This is all quite subtle. suppose we have:
|
|
| 491 | + um_tv_env: c :-> b
|
|
| 492 | + um_fam_env F b :-> a
|
|
| 493 | +and we are trying to add a :-> F c. We will call lookupFamEnv on (F, [c]), which will
|
|
| 494 | +fail because b and c are not equal. So we go ahead and add a :-> F c as a new tyvar eq,
|
|
| 495 | +getting:
|
|
| 496 | + um_tv_env: a :-> F c, c :-> b
|
|
| 497 | + um_fam_env F b :-> a
|
|
| 498 | + |
|
| 499 | +Does that loop, like this:
|
|
| 500 | + a --> F c --> F b --> a?
|
|
| 501 | +No, because we do not substitute (F c) to (F b) and then look up in um_fam_env;
|
|
| 502 | +we look up only un-substituted types.
|
|
| 452 | 503 | -}
|
| 453 | 504 | |
| 454 | 505 | {- *********************************************************************
|
| ... | ... | @@ -1767,6 +1818,11 @@ uVarOrFam :: UMEnv -> CanEqLHS -> InType -> OutCoercion -> UM () |
| 1767 | 1818 | -- Why saturated? See (ATF4) in Note [Apartness and type families]
|
| 1768 | 1819 | uVarOrFam env ty1 ty2 kco
|
| 1769 | 1820 | = do { substs <- getSubstEnvs
|
| 1821 | +-- ; pprTrace "uVarOrFam" (vcat
|
|
| 1822 | +-- [ text "ty1" <+> ppr ty1
|
|
| 1823 | +-- , text "ty2" <+> ppr ty2
|
|
| 1824 | +-- , text "tv_env" <+> ppr (um_tv_env substs)
|
|
| 1825 | +-- , text "fam_env" <+> ppr (um_fam_env substs) ]) $
|
|
| 1770 | 1826 | ; go NotSwapped substs ty1 ty2 kco }
|
| 1771 | 1827 | where
|
| 1772 | 1828 | -- `go` takes two bites at the cherry; if the first one fails
|
| ... | ... | @@ -1776,16 +1832,12 @@ uVarOrFam env ty1 ty2 kco |
| 1776 | 1832 | -- E.g. a ~ F p q
|
| 1777 | 1833 | -- Starts with: go a (F p q)
|
| 1778 | 1834 | -- if `a` not bindable, swap to: go (F p q) a
|
| 1779 | - go swapped substs (TyVarLHS tv1) ty2 kco
|
|
| 1780 | - = go_tv swapped substs tv1 ty2 kco
|
|
| 1781 | - |
|
| 1782 | - go swapped substs (TyFamLHS tc tys) ty2 kco
|
|
| 1783 | - = go_fam swapped substs tc tys ty2 kco
|
|
| 1784 | 1835 | |
| 1785 | 1836 | -----------------------------
|
| 1786 | - -- go_tv: LHS is a type variable
|
|
| 1837 | + -- LHS is a type variable
|
|
| 1787 | 1838 | -- The sequence of tests is very similar to go_tv
|
| 1788 | - go_tv swapped substs tv1 ty2 kco
|
|
| 1839 | + go :: SwapFlag -> UMState -> CanEqLHS -> InType -> OutCoercion -> UM ()
|
|
| 1840 | + go swapped substs lhs@(TyVarLHS tv1) ty2 kco
|
|
| 1789 | 1841 | | Just ty1' <- lookupVarEnv (um_tv_env substs) tv1'
|
| 1790 | 1842 | = -- We already have a substitution for tv1
|
| 1791 | 1843 | if | um_unif env -> unify_ty env ty1' ty2 kco
|
| ... | ... | @@ -1837,9 +1889,8 @@ uVarOrFam env ty1 ty2 kco |
| 1837 | 1889 | where
|
| 1838 | 1890 | tv1' = umRnOccL env tv1
|
| 1839 | 1891 | ty2_fvs = tyCoVarsOfType ty2
|
| 1840 | - rhs_fvs = ty2_fvs `unionVarSet` tyCoVarsOfCo kco
|
|
| 1841 | 1892 | rhs = ty2 `mkCastTy` mkSymCo kco
|
| 1842 | - tv1_is_bindable | not (tv1' `elemVarSet` um_foralls env)
|
|
| 1893 | + tv1_is_bindable | not (tv1' `elemVarSet` foralld_tvs)
|
|
| 1843 | 1894 | -- tv1' is not forall-bound, but tv1 can still differ
|
| 1844 | 1895 | -- from tv1; see Note [Cloning the template binders]
|
| 1845 | 1896 | -- in GHC.Core.Rules. So give tv1' to um_bind_tv_fun.
|
| ... | ... | @@ -1848,16 +1899,16 @@ uVarOrFam env ty1 ty2 kco |
| 1848 | 1899 | | otherwise
|
| 1849 | 1900 | = False
|
| 1850 | 1901 | |
| 1851 | - occurs_check = um_unif env &&
|
|
| 1852 | - occursCheck (um_tv_env substs) tv1 rhs_fvs
|
|
| 1902 | + foralld_tvs = um_foralls env
|
|
| 1903 | + occurs_check = um_unif env && uOccursCheck substs foralld_tvs lhs rhs
|
|
| 1853 | 1904 | -- Occurs check, only when unifying
|
| 1854 | 1905 | -- see Note [Infinitary substitutions]
|
| 1855 | - -- Make sure you include `kco` in rhs_tvs #14846
|
|
| 1906 | + -- Make sure you include `kco` in rhs #14846
|
|
| 1856 | 1907 | |
| 1857 | 1908 | -----------------------------
|
| 1858 | - -- go_fam: LHS is a saturated type-family application
|
|
| 1909 | + -- LHS is a saturated type-family application
|
|
| 1859 | 1910 | -- Invariant: ty2 is not a TyVarTy
|
| 1860 | - go_fam swapped substs tc1 tys1 ty2 kco
|
|
| 1911 | + go swapped substs lhs@(TyFamLHS tc1 tys1) ty2 kco
|
|
| 1861 | 1912 | -- If we are under a forall, just give up and return MaybeApart
|
| 1862 | 1913 | -- see (ATF3) in Note [Apartness and type families]
|
| 1863 | 1914 | | not (isEmptyVarSet (um_foralls env))
|
| ... | ... | @@ -1878,14 +1929,17 @@ uVarOrFam env ty1 ty2 kco |
| 1878 | 1929 | -- Check for equality F tys1 ~ F tys2
|
| 1879 | 1930 | | Just (tc2, tys2) <- isSatFamApp ty2
|
| 1880 | 1931 | , tc1 == tc2
|
| 1881 | - = go_fam_fam tc1 tys1 tys2 kco
|
|
| 1932 | + = go_fam_fam substs tc1 tys1 tys2 kco
|
|
| 1882 | 1933 | |
| 1883 | 1934 | -- Now check if we can bind the (F tys) to the RHS
|
| 1884 | 1935 | -- This can happen even when matching: see (ATF7)
|
| 1885 | 1936 | | BindMe <- um_bind_fam_fun env tc1 tys1 rhs
|
| 1886 | - = -- ToDo: do we need an occurs check here?
|
|
| 1887 | - do { extendFamEnv tc1 tys1 rhs
|
|
| 1888 | - ; maybeApart MARTypeFamily }
|
|
| 1937 | + = if uOccursCheck substs emptyVarSet lhs rhs
|
|
| 1938 | + then maybeApart MARInfinite
|
|
| 1939 | + else do { extendFamEnv tc1 tys1 rhs
|
|
| 1940 | + -- We don't substitute tys1 before extending
|
|
| 1941 | + -- See Note [Shortcomings of the apartness test]
|
|
| 1942 | + ; maybeApart MARTypeFamily }
|
|
| 1889 | 1943 | |
| 1890 | 1944 | -- Swap in case of (F a b) ~ (G c d e)
|
| 1891 | 1945 | -- Maybe um_bind_fam_fun is False of (F a b) but true of (G c d e)
|
| ... | ... | @@ -1905,7 +1959,8 @@ uVarOrFam env ty1 ty2 kco |
| 1905 | 1959 | -----------------------------
|
| 1906 | 1960 | -- go_fam_fam: LHS and RHS are both saturated type-family applications,
|
| 1907 | 1961 | -- for the same type-family F
|
| 1908 | - go_fam_fam tc tys1 tys2 kco
|
|
| 1962 | + -- Precondition: um_foralls is empty
|
|
| 1963 | + go_fam_fam substs tc tys1 tys2 kco
|
|
| 1909 | 1964 | -- Decompose (F tys1 ~ F tys2): (ATF9)
|
| 1910 | 1965 | -- Use injectivity information of F: (ATF10)
|
| 1911 | 1966 | -- But first bind the type-fam if poss: (ATF11)
|
| ... | ... | @@ -1925,13 +1980,13 @@ uVarOrFam env ty1 ty2 kco |
| 1925 | 1980 | bind_fam_if_poss
|
| 1926 | 1981 | | not (um_unif env) -- Not when matching (ATF11-1)
|
| 1927 | 1982 | = return ()
|
| 1928 | - | tcEqTyConAppArgs tys1 tys2 -- Detect (F tys ~ F tys);
|
|
| 1929 | - = return () -- otherwise we'd build an infinite substitution
|
|
| 1930 | 1983 | | BindMe <- um_bind_fam_fun env tc tys1 rhs1
|
| 1931 | - = extendFamEnv tc tys1 rhs1
|
|
| 1932 | - | um_unif env
|
|
| 1933 | - , BindMe <- um_bind_fam_fun env tc tys2 rhs2
|
|
| 1934 | - = extendFamEnv tc tys2 rhs2
|
|
| 1984 | + = unless (uOccursCheck substs emptyVarSet (TyFamLHS tc tys1) rhs1) $
|
|
| 1985 | + extendFamEnv tc tys1 rhs1
|
|
| 1986 | + -- At this point um_unif=True, so we can unify either way
|
|
| 1987 | + | BindMe <- um_bind_fam_fun env tc tys2 rhs2
|
|
| 1988 | + = unless (uOccursCheck substs emptyVarSet (TyFamLHS tc tys2) rhs2) $
|
|
| 1989 | + extendFamEnv tc tys2 rhs2
|
|
| 1935 | 1990 | | otherwise
|
| 1936 | 1991 | = return ()
|
| 1937 | 1992 | |
| ... | ... | @@ -1939,17 +1994,92 @@ uVarOrFam env ty1 ty2 kco |
| 1939 | 1994 | rhs2 = mkTyConApp tc tys1 `mkCastTy` kco
|
| 1940 | 1995 | |
| 1941 | 1996 | |
| 1942 | -occursCheck :: TvSubstEnv -> TyVar -> TyCoVarSet -> Bool
|
|
| 1943 | -occursCheck env tv1 tvs
|
|
| 1944 | - = anyVarSet bad tvs
|
|
| 1997 | +uOccursCheck :: UMState
|
|
| 1998 | + -> TyVarSet -- Bound by enclosing foralls; see (OCU1)
|
|
| 1999 | + -> CanEqLHS -> Type -- Can we unify (lhs := ty)?
|
|
| 2000 | + -> Bool
|
|
| 2001 | +-- See Note [The occurs check in the Core unifier] and (ATF13)
|
|
| 2002 | +uOccursCheck (UMState { um_tv_env = tv_env, um_fam_env = fam_env }) bvs lhs ty
|
|
| 2003 | + = go bvs ty
|
|
| 1945 | 2004 | where
|
| 1946 | - bad tv | Just ty <- lookupVarEnv env tv
|
|
| 1947 | - = anyVarSet bad (tyCoVarsOfType ty)
|
|
| 1948 | - | otherwise
|
|
| 1949 | - = tv == tv1
|
|
| 1950 | - |
|
| 1951 | -{- Note [Unifying coercion-foralls]
|
|
| 1952 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2005 | + go :: TyCoVarSet -- Bound by enclosing foralls; see (OCU1)
|
|
| 2006 | + -> Type -> Bool
|
|
| 2007 | + go bvs ty | Just ty' <- coreView ty = go bvs ty'
|
|
| 2008 | + go bvs (TyVarTy tv) | Just ty' <- lookupVarEnv tv_env tv
|
|
| 2009 | + = go bvs ty'
|
|
| 2010 | + | TyVarLHS tv' <- lhs, tv==tv'
|
|
| 2011 | + = True
|
|
| 2012 | + | otherwise
|
|
| 2013 | + = go bvs (tyVarKind tv)
|
|
| 2014 | + go bvs (AppTy ty1 ty2) = go bvs ty1 || go bvs ty2
|
|
| 2015 | + go _ (LitTy {}) = False
|
|
| 2016 | + go bvs (FunTy _ w arg res) = go bvs w || go bvs arg || go bvs res
|
|
| 2017 | + go bvs (TyConApp tc tys) = go_tc bvs tc tys
|
|
| 2018 | + |
|
| 2019 | + go bvs (ForAllTy (Bndr tv _) ty)
|
|
| 2020 | + = go bvs (tyVarKind tv) ||
|
|
| 2021 | + (case lhs of
|
|
| 2022 | + TyVarLHS tv' | tv==tv' -> False -- Shadowing
|
|
| 2023 | + | otherwise -> go (bvs `extendVarSet` tv) ty
|
|
| 2024 | + TyFamLHS {} -> False) -- Lookups don't happen under a forall
|
|
| 2025 | + |
|
| 2026 | + go bvs (CastTy ty _co) = go bvs ty -- ToDo: should we worry about `co`?
|
|
| 2027 | + go _ (CoercionTy _co) = False -- ToDo: should we worry about `co`?
|
|
| 2028 | + |
|
| 2029 | + go_tc bvs tc tys
|
|
| 2030 | + | isEmptyVarSet bvs -- Never look up in um_fam_env under a forall (ATF3)
|
|
| 2031 | + , isTypeFamilyTyCon tc
|
|
| 2032 | + , Just ty' <- lookupFamEnv fam_env tc (take arity tys)
|
|
| 2033 | + -- NB: we look up /un-substituted/ types;
|
|
| 2034 | + -- See Note [Shortcomings of the apartness test]
|
|
| 2035 | + = go bvs ty' || any (go bvs) (drop arity tys)
|
|
| 2036 | + |
|
| 2037 | + | TyFamLHS tc' tys' <- lhs
|
|
| 2038 | + , tc == tc'
|
|
| 2039 | + , tys `lengthAtLeast` arity -- Saturated, or over-saturated
|
|
| 2040 | + , tcEqTyConAppArgs tys tys'
|
|
| 2041 | + = True
|
|
| 2042 | + |
|
| 2043 | + | otherwise
|
|
| 2044 | + = any (go bvs) tys
|
|
| 2045 | + where
|
|
| 2046 | + arity = tyConArity tc
|
|
| 2047 | + |
|
| 2048 | +{- Note [The occurs check in the Core unifier]
|
|
| 2049 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 2050 | +The unifier applies both substitutions (um_tv_env and um_fam_env) as it goes,
|
|
| 2051 | +so we'll get an infinite loop if we have, for example
|
|
| 2052 | + um_tv_env: a :-> F b -- (1)
|
|
| 2053 | + um_fam_env F b :-> a -- (2)
|
|
| 2054 | + |
|
| 2055 | +So (uOccursCheck substs lhs ty) returns True iff extending `substs` with `lhs :-> ty`
|
|
| 2056 | +could lead to a loop. That is, could there by a type `s` such that
|
|
| 2057 | + applySubsts( (substs + lhs:->ty), s ) is infinite
|
|
| 2058 | + |
|
| 2059 | +It's vital that we do both at once: we might have (1) already and add (2);
|
|
| 2060 | +or we might have (2) already and add (1).
|
|
| 2061 | + |
|
| 2062 | +A very similar task is done by GHC.Tc.Utils.Unify.checkTyEqRhs.
|
|
| 2063 | + |
|
| 2064 | +(OCU1) We keep track of the forall-bound variables because the um_fam_env is inactive
|
|
| 2065 | + under a forall; indeed it is /unsound/ to consult it because we may have a binding
|
|
| 2066 | + (F a :-> Int), and then unify (forall a. ...(F a)...) with something. We don't
|
|
| 2067 | + want to map that (F a) to Int!
|
|
| 2068 | + |
|
| 2069 | +(OCU2) Performance. Consider unifying
|
|
| 2070 | + [a, b] ~ [big-ty, (a,a,a)]
|
|
| 2071 | + We'll unify a:=big-ty. Then we'll attempt b:=(a,a,a), but must do an occurs check.
|
|
| 2072 | + So we'll walk over big-ty, looking for `b`. And then again, and again, once for
|
|
| 2073 | + each occurrence of `a`. A similar thing happens for
|
|
| 2074 | + [a, (b,b,b)] ~ [big-ty, (a,a,a)]
|
|
| 2075 | + albeit a bit less obviously.
|
|
| 2076 | + |
|
| 2077 | + Potentially we could use a cache to record checks we have already done;
|
|
| 2078 | + but I have not attempted that yet. Precisely similar remarks would apply
|
|
| 2079 | + to GHC.Tc.Utils.Unify.checkTyEqRhs
|
|
| 2080 | + |
|
| 2081 | +Note [Unifying coercion-foralls]
|
|
| 2082 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1953 | 2083 | Suppose we try to unify (forall cv. t1) ~ (forall cv. t2).
|
| 1954 | 2084 | See Note [ForAllTy] in GHC.Core.TyCo.Rep.
|
| 1955 | 2085 |
| ... | ... | @@ -16,7 +16,6 @@ |
| 16 | 16 | |
| 17 | 17 | module GHC.Tc.Gen.App
|
| 18 | 18 | ( tcApp
|
| 19 | - , tcInferSigma
|
|
| 20 | 19 | , tcExprPrag ) where
|
| 21 | 20 | |
| 22 | 21 | import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcPolyExpr )
|
| ... | ... | @@ -165,26 +164,6 @@ Note [Instantiation variables are short lived] |
| 165 | 164 | -}
|
| 166 | 165 | |
| 167 | 166 | |
| 168 | -{- *********************************************************************
|
|
| 169 | -* *
|
|
| 170 | - tcInferSigma
|
|
| 171 | -* *
|
|
| 172 | -********************************************************************* -}
|
|
| 173 | - |
|
| 174 | -tcInferSigma :: Bool -> LHsExpr GhcRn -> TcM TcSigmaType
|
|
| 175 | --- Used only to implement :type; see GHC.Tc.Module.tcRnExpr
|
|
| 176 | --- True <=> instantiate -- return a rho-type
|
|
| 177 | --- False <=> don't instantiate -- return a sigma-type
|
|
| 178 | -tcInferSigma inst (L loc rn_expr)
|
|
| 179 | - = addExprCtxt rn_expr $
|
|
| 180 | - setSrcSpanA loc $
|
|
| 181 | - do { (fun@(rn_fun,fun_ctxt), rn_args) <- splitHsApps rn_expr
|
|
| 182 | - ; do_ql <- wantQuickLook rn_fun
|
|
| 183 | - ; (tc_fun, fun_sigma) <- tcInferAppHead fun
|
|
| 184 | - ; (inst_args, app_res_sigma) <- tcInstFun do_ql inst (tc_fun, fun_ctxt) fun_sigma rn_args
|
|
| 185 | - ; _ <- tcValArgs do_ql inst_args
|
|
| 186 | - ; return app_res_sigma }
|
|
| 187 | - |
|
| 188 | 167 | {- *********************************************************************
|
| 189 | 168 | * *
|
| 190 | 169 | Typechecking n-ary applications
|
| ... | ... | @@ -219,7 +198,7 @@ using the application chain route, and we can just recurse to tcExpr. |
| 219 | 198 | |
| 220 | 199 | A "head" has three special cases (for which we can infer a polytype
|
| 221 | 200 | using tcInferAppHead_maybe); otherwise is just any old expression (for
|
| 222 | -which we can infer a rho-type (via tcInfer).
|
|
| 201 | +which we can infer a rho-type (via runInferExpr).
|
|
| 223 | 202 | |
| 224 | 203 | There is no special treatment for HsHole (HsVar ...), HsOverLit, etc, because
|
| 225 | 204 | we can't get a polytype from them.
|
| ... | ... | @@ -403,13 +382,22 @@ tcApp rn_expr exp_res_ty |
| 403 | 382 | -- Step 2: Infer the type of `fun`, the head of the application
|
| 404 | 383 | ; (tc_fun, fun_sigma) <- tcInferAppHead fun
|
| 405 | 384 | ; let tc_head = (tc_fun, fun_ctxt)
|
| 385 | + -- inst_final: top-instantiate the result type of the application,
|
|
| 386 | + -- EXCEPT if we are trying to infer a sigma-type
|
|
| 387 | + inst_final = case exp_res_ty of
|
|
| 388 | + Check {} -> True
|
|
| 389 | + Infer (IR {ir_inst=iif}) ->
|
|
| 390 | + case iif of
|
|
| 391 | + IIF_ShallowRho -> True
|
|
| 392 | + IIF_DeepRho -> True
|
|
| 393 | + IIF_Sigma -> False
|
|
| 406 | 394 | |
| 407 | 395 | -- Step 3: Instantiate the function type (taking a quick look at args)
|
| 408 | 396 | ; do_ql <- wantQuickLook rn_fun
|
| 409 | 397 | ; (inst_args, app_res_rho)
|
| 410 | 398 | <- setQLInstLevel do_ql $ -- See (TCAPP1) and (TCAPP2) in
|
| 411 | 399 | -- Note [tcApp: typechecking applications]
|
| 412 | - tcInstFun do_ql True tc_head fun_sigma rn_args
|
|
| 400 | + tcInstFun do_ql inst_final tc_head fun_sigma rn_args
|
|
| 413 | 401 | |
| 414 | 402 | ; case do_ql of
|
| 415 | 403 | NoQL -> do { traceTc "tcApp:NoQL" (ppr rn_fun $$ ppr app_res_rho)
|
| ... | ... | @@ -420,6 +408,7 @@ tcApp rn_expr exp_res_ty |
| 420 | 408 | app_res_rho exp_res_ty
|
| 421 | 409 | -- Step 4.2: typecheck the arguments
|
| 422 | 410 | ; tc_args <- tcValArgs NoQL inst_args
|
| 411 | + |
|
| 423 | 412 | -- Step 4.3: wrap up
|
| 424 | 413 | ; finishApp tc_head tc_args app_res_rho res_wrap }
|
| 425 | 414 | |
| ... | ... | @@ -427,15 +416,18 @@ tcApp rn_expr exp_res_ty |
| 427 | 416 | |
| 428 | 417 | -- Step 5.1: Take a quick look at the result type
|
| 429 | 418 | ; quickLookResultType app_res_rho exp_res_ty
|
| 419 | + |
|
| 430 | 420 | -- Step 5.2: typecheck the arguments, and monomorphise
|
| 431 | 421 | -- any un-unified instantiation variables
|
| 432 | 422 | ; tc_args <- tcValArgs DoQL inst_args
|
| 433 | - -- Step 5.3: zonk to expose the polymophism hidden under
|
|
| 423 | + |
|
| 424 | + -- Step 5.3: zonk to expose the polymorphism hidden under
|
|
| 434 | 425 | -- QuickLook instantiation variables in `app_res_rho`
|
| 435 | 426 | ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
|
| 427 | + |
|
| 436 | 428 | -- Step 5.4: subsumption check against the expected type
|
| 437 | 429 | ; res_wrap <- checkResultTy rn_expr tc_head inst_args
|
| 438 | - app_res_rho exp_res_ty
|
|
| 430 | + app_res_rho exp_res_ty
|
|
| 439 | 431 | -- Step 5.5: wrap up
|
| 440 | 432 | ; finishApp tc_head tc_args app_res_rho res_wrap } }
|
| 441 | 433 | |
| ... | ... | @@ -470,32 +462,12 @@ checkResultTy :: HsExpr GhcRn |
| 470 | 462 | -> (HsExpr GhcTc, AppCtxt) -- Head
|
| 471 | 463 | -> [HsExprArg p] -- Arguments, just error messages
|
| 472 | 464 | -> TcRhoType -- Inferred type of the application; zonked to
|
| 473 | - -- expose foralls, but maybe not deeply instantiated
|
|
| 465 | + -- expose foralls, but maybe not /deeply/ instantiated
|
|
| 474 | 466 | -> ExpRhoType -- Expected type; this is deeply skolemised
|
| 475 | 467 | -> TcM HsWrapper
|
| 476 | 468 | checkResultTy rn_expr _fun _inst_args app_res_rho (Infer inf_res)
|
| 477 | - = fillInferResultDS (exprCtOrigin rn_expr) app_res_rho inf_res
|
|
| 478 | - -- See Note [Deeply instantiate in checkResultTy when inferring]
|
|
| 479 | - |
|
| 480 | -{- Note [Deeply instantiate in checkResultTy when inferring]
|
|
| 481 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 482 | -To accept the following program (T26225b) with -XDeepSubsumption, we need to
|
|
| 483 | -deeply instantiate when inferring in checkResultTy:
|
|
| 484 | - |
|
| 485 | - f :: Int -> (forall a. a->a)
|
|
| 486 | - g :: Int -> Bool -> Bool
|
|
| 487 | - |
|
| 488 | - test b =
|
|
| 489 | - case b of
|
|
| 490 | - True -> f
|
|
| 491 | - False -> g
|
|
| 492 | - |
|
| 493 | -If we don't deeply instantiate in the branches of the case expression, we will
|
|
| 494 | -try to unify the type of 'f' with that of 'g', which fails. If we instead
|
|
| 495 | -deeply instantiate 'f', we will fill the 'InferResult' with 'Int -> alpha -> alpha'
|
|
| 496 | -which then successfully unifies with the type of 'g' when we come to fill the
|
|
| 497 | -'InferResult' hole a second time for the second case branch.
|
|
| 498 | --}
|
|
| 469 | + = fillInferResult (exprCtOrigin rn_expr) app_res_rho inf_res
|
|
| 470 | + -- fillInferResult does deep instantiation if DeepSubsumption is on
|
|
| 499 | 471 | |
| 500 | 472 | checkResultTy rn_expr (tc_fun, fun_ctxt) inst_args app_res_rho (Check res_ty)
|
| 501 | 473 | -- Unify with expected type from the context
|
| ... | ... | @@ -651,18 +623,16 @@ quickLookKeys = [dollarIdKey, leftSectionKey, rightSectionKey] |
| 651 | 623 | ********************************************************************* -}
|
| 652 | 624 | |
| 653 | 625 | tcInstFun :: QLFlag
|
| 654 | - -> Bool -- False <=> Instantiate only /inferred/ variables at the end
|
|
| 626 | + -> Bool -- False <=> Instantiate only /top-level, inferred/ variables;
|
|
| 655 | 627 | -- so may return a sigma-type
|
| 656 | - -- True <=> Instantiate all type variables at the end:
|
|
| 657 | - -- return a rho-type
|
|
| 658 | - -- The /only/ call site that passes in False is the one
|
|
| 659 | - -- in tcInferSigma, which is used only to implement :type
|
|
| 660 | - -- Otherwise we do eager instantiation; in Fig 5 of the paper
|
|
| 628 | + -- True <=> Instantiate /top-level, invisible/ type variables;
|
|
| 629 | + -- always return a rho-type (but not a deep-rho type)
|
|
| 630 | + -- Generally speaking we pass in True; in Fig 5 of the paper
|
|
| 661 | 631 | -- |-inst returns a rho-type
|
| 662 | 632 | -> (HsExpr GhcTc, AppCtxt)
|
| 663 | 633 | -> TcSigmaType -> [HsExprArg 'TcpRn]
|
| 664 | 634 | -> TcM ( [HsExprArg 'TcpInst]
|
| 665 | - , TcSigmaType )
|
|
| 635 | + , TcSigmaType ) -- Does not instantiate trailing invisible foralls
|
|
| 666 | 636 | -- This crucial function implements the |-inst judgement in Fig 4, plus the
|
| 667 | 637 | -- modification in Fig 5, of the QL paper:
|
| 668 | 638 | -- "A quick look at impredicativity" (ICFP'20).
|
| ... | ... | @@ -704,13 +674,9 @@ tcInstFun do_ql inst_final (tc_fun, fun_ctxt) fun_sigma rn_args |
| 704 | 674 | _ -> False
|
| 705 | 675 | |
| 706 | 676 | inst_fun :: [HsExprArg 'TcpRn] -> ForAllTyFlag -> Bool
|
| 707 | - -- True <=> instantiate a tyvar with this ForAllTyFlag
|
|
| 677 | + -- True <=> instantiate a tyvar that has this ForAllTyFlag
|
|
| 708 | 678 | inst_fun [] | inst_final = isInvisibleForAllTyFlag
|
| 709 | 679 | | otherwise = const False
|
| 710 | - -- Using `const False` for `:type` avoids
|
|
| 711 | - -- `forall {r1} (a :: TYPE r1) {r2} (b :: TYPE r2). a -> b`
|
|
| 712 | - -- turning into `forall a {r2} (b :: TYPE r2). a -> b`.
|
|
| 713 | - -- See #21088.
|
|
| 714 | 680 | inst_fun (EValArg {} : _) = isInvisibleForAllTyFlag
|
| 715 | 681 | inst_fun _ = isInferredForAllTyFlag
|
| 716 | 682 |
| ... | ... | @@ -1305,8 +1305,8 @@ tcMonoBinds is_rec sig_fn no_gen |
| 1305 | 1305 | do { mult <- newMultiplicityVar
|
| 1306 | 1306 | |
| 1307 | 1307 | ; ((co_fn, matches'), rhs_ty')
|
| 1308 | - <- tcInferFRR (FRRBinder name) $ \ exp_ty ->
|
|
| 1309 | - -- tcInferFRR: the type of a let-binder must have
|
|
| 1308 | + <- runInferRhoFRR (FRRBinder name) $ \ exp_ty ->
|
|
| 1309 | + -- runInferRhoFRR: the type of a let-binder must have
|
|
| 1310 | 1310 | -- a fixed runtime rep. See #23176
|
| 1311 | 1311 | tcExtendBinderStack [TcIdBndr_ExpType name exp_ty NotTopLevel] $
|
| 1312 | 1312 | -- We extend the error context even for a non-recursive
|
| ... | ... | @@ -1333,8 +1333,8 @@ tcMonoBinds is_rec sig_fn no_gen |
| 1333 | 1333 | = addErrCtxt (PatMonoBindsCtxt pat grhss) $
|
| 1334 | 1334 | do { mult <- tcMultAnnOnPatBind mult_ann
|
| 1335 | 1335 | |
| 1336 | - ; (grhss', pat_ty) <- tcInferFRR FRRPatBind $ \ exp_ty ->
|
|
| 1337 | - -- tcInferFRR: the type of each let-binder must have
|
|
| 1336 | + ; (grhss', pat_ty) <- runInferRhoFRR FRRPatBind $ \ exp_ty ->
|
|
| 1337 | + -- runInferRhoFRR: the type of each let-binder must have
|
|
| 1338 | 1338 | -- a fixed runtime rep. See #23176
|
| 1339 | 1339 | tcGRHSsPat mult grhss exp_ty
|
| 1340 | 1340 | |
| ... | ... | @@ -1522,7 +1522,7 @@ tcLhs sig_fn no_gen (PatBind { pat_lhs = pat, pat_rhs = grhss, pat_mult = mult_a |
| 1522 | 1522 | -- See Note [Typechecking pattern bindings]
|
| 1523 | 1523 | ; ((pat', nosig_mbis), pat_ty)
|
| 1524 | 1524 | <- addErrCtxt (PatMonoBindsCtxt pat grhss) $
|
| 1525 | - tcInferFRR FRRPatBind $ \ exp_ty ->
|
|
| 1525 | + runInferSigmaFRR FRRPatBind $ \ exp_ty ->
|
|
| 1526 | 1526 | tcLetPat inst_sig_fun no_gen pat (Scaled mult exp_ty) $
|
| 1527 | 1527 | -- The above inferred type get an unrestricted multiplicity. It may be
|
| 1528 | 1528 | -- worth it to try and find a finer-grained multiplicity here
|
| ... | ... | @@ -19,7 +19,7 @@ module GHC.Tc.Gen.Expr |
| 19 | 19 | ( tcCheckPolyExpr, tcCheckPolyExprNC,
|
| 20 | 20 | tcCheckMonoExpr, tcCheckMonoExprNC,
|
| 21 | 21 | tcMonoExpr, tcMonoExprNC,
|
| 22 | - tcInferRho, tcInferRhoNC,
|
|
| 22 | + tcInferExpr, tcInferSigma, tcInferRho, tcInferRhoNC,
|
|
| 23 | 23 | tcPolyLExpr, tcPolyExpr, tcExpr, tcPolyLExprSig,
|
| 24 | 24 | tcSyntaxOp, tcSyntaxOpGen, SyntaxOpType(..), synKnownType,
|
| 25 | 25 | tcCheckId,
|
| ... | ... | @@ -233,17 +233,24 @@ tcPolyExprCheck expr res_ty |
| 233 | 233 | * *
|
| 234 | 234 | ********************************************************************* -}
|
| 235 | 235 | |
| 236 | +tcInferSigma :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcSigmaType)
|
|
| 237 | +tcInferSigma = tcInferExpr IIF_Sigma
|
|
| 238 | + |
|
| 236 | 239 | tcInferRho, tcInferRhoNC :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
|
| 237 | 240 | -- Infer a *rho*-type. The return type is always instantiated.
|
| 238 | -tcInferRho (L loc expr)
|
|
| 239 | - = setSrcSpanA loc $ -- Set location /first/; see GHC.Tc.Utils.Monad
|
|
| 241 | +tcInferRho = tcInferExpr IIF_DeepRho
|
|
| 242 | +tcInferRhoNC = tcInferExprNC IIF_DeepRho
|
|
| 243 | + |
|
| 244 | +tcInferExpr, tcInferExprNC :: InferInstFlag -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcType)
|
|
| 245 | +tcInferExpr iif (L loc expr)
|
|
| 246 | + = setSrcSpanA loc $ -- Set location /first/; see GHC.Tc.Utils.Monad
|
|
| 240 | 247 | addExprCtxt expr $ -- Note [Error contexts in generated code]
|
| 241 | - do { (expr', rho) <- tcInfer (tcExpr expr)
|
|
| 248 | + do { (expr', rho) <- runInfer iif IFRR_Any (tcExpr expr)
|
|
| 242 | 249 | ; return (L loc expr', rho) }
|
| 243 | 250 | |
| 244 | -tcInferRhoNC (L loc expr)
|
|
| 245 | - = setSrcSpanA loc $
|
|
| 246 | - do { (expr', rho) <- tcInfer (tcExpr expr)
|
|
| 251 | +tcInferExprNC iif (L loc expr)
|
|
| 252 | + = setSrcSpanA loc $
|
|
| 253 | + do { (expr', rho) <- runInfer iif IFRR_Any (tcExpr expr)
|
|
| 247 | 254 | ; return (L loc expr', rho) }
|
| 248 | 255 | |
| 249 | 256 | ---------------
|
| ... | ... | @@ -878,7 +885,7 @@ tcInferTupArgs boxity args |
| 878 | 885 | ; return (Missing (Scaled mult arg_ty), arg_ty) }
|
| 879 | 886 | tc_infer_tup_arg i (Present x lexpr@(L l expr))
|
| 880 | 887 | = do { (expr', arg_ty) <- case boxity of
|
| 881 | - Unboxed -> tcInferFRR (FRRUnboxedTuple i) (tcPolyExpr expr)
|
|
| 888 | + Unboxed -> runInferRhoFRR (FRRUnboxedTuple i) (tcPolyExpr expr)
|
|
| 882 | 889 | Boxed -> do { arg_ty <- newFlexiTyVarTy liftedTypeKind
|
| 883 | 890 | ; L _ expr' <- tcCheckPolyExpr lexpr arg_ty
|
| 884 | 891 | ; return (expr', arg_ty) }
|
| 1 | 1 | module GHC.Tc.Gen.Expr where
|
| 2 | 2 | import GHC.Hs ( HsExpr, LHsExpr, SyntaxExprRn
|
| 3 | 3 | , SyntaxExprTc )
|
| 4 | -import GHC.Tc.Utils.TcType ( TcRhoType, TcSigmaType, TcSigmaTypeFRR
|
|
| 5 | - , SyntaxOpType
|
|
| 4 | +import GHC.Tc.Utils.TcType ( TcType, TcRhoType, TcSigmaType, TcSigmaTypeFRR
|
|
| 5 | + , SyntaxOpType, InferInstFlag
|
|
| 6 | 6 | , ExpType, ExpRhoType, ExpSigmaType )
|
| 7 | 7 | import GHC.Tc.Types ( TcM )
|
| 8 | 8 | import GHC.Tc.Types.BasicTypes( TcCompleteSig )
|
| ... | ... | @@ -33,6 +33,8 @@ tcExpr :: HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) |
| 33 | 33 | tcInferRho, tcInferRhoNC ::
|
| 34 | 34 | LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
|
| 35 | 35 | |
| 36 | +tcInferExpr :: InferInstFlag -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcType)
|
|
| 37 | + |
|
| 36 | 38 | tcSyntaxOp :: CtOrigin
|
| 37 | 39 | -> SyntaxExprRn
|
| 38 | 40 | -> [SyntaxOpType] -- ^ shape of syntax operator arguments
|
| ... | ... | @@ -556,7 +556,7 @@ tcInferAppHead (fun,ctxt) |
| 556 | 556 | do { mb_tc_fun <- tcInferAppHead_maybe fun
|
| 557 | 557 | ; case mb_tc_fun of
|
| 558 | 558 | Just (fun', fun_sigma) -> return (fun', fun_sigma)
|
| 559 | - Nothing -> tcInfer (tcExpr fun) }
|
|
| 559 | + Nothing -> runInferRho (tcExpr fun) }
|
|
| 560 | 560 | |
| 561 | 561 | tcInferAppHead_maybe :: HsExpr GhcRn
|
| 562 | 562 | -> TcM (Maybe (HsExpr GhcTc, TcSigmaType))
|
| ... | ... | @@ -1063,9 +1063,9 @@ tc_infer_lhs_type mode (L span ty) |
| 1063 | 1063 | -- | Infer the kind of a type and desugar. This is the "up" type-checker,
|
| 1064 | 1064 | -- as described in Note [Bidirectional type checking]
|
| 1065 | 1065 | tc_infer_hs_type :: TcTyMode -> HsType GhcRn -> TcM (TcType, TcKind)
|
| 1066 | - |
|
| 1067 | 1066 | tc_infer_hs_type mode rn_ty
|
| 1068 | - = tcInfer $ \exp_kind -> tcHsType mode rn_ty exp_kind
|
|
| 1067 | + = runInferKind $ \exp_kind ->
|
|
| 1068 | + tcHsType mode rn_ty exp_kind
|
|
| 1069 | 1069 | |
| 1070 | 1070 | {-
|
| 1071 | 1071 | Note [Typechecking HsCoreTys]
|
| ... | ... | @@ -1985,7 +1985,7 @@ checkExpKind rn_ty ty ki (Check ki') = |
| 1985 | 1985 | checkExpKind _rn_ty ty ki (Infer cell) = do
|
| 1986 | 1986 | -- NB: do not instantiate.
|
| 1987 | 1987 | -- See Note [Do not always instantiate eagerly in types]
|
| 1988 | - co <- fillInferResult ki cell
|
|
| 1988 | + co <- fillInferResultNoInst ki cell
|
|
| 1989 | 1989 | pure (ty `mkCastTy` co)
|
| 1990 | 1990 | |
| 1991 | 1991 | ---------------------------
|
| ... | ... | @@ -1034,7 +1034,7 @@ tcDoStmt ctxt (RecStmt { recS_stmts = L l stmts, recS_later_ids = later_names |
| 1034 | 1034 | |
| 1035 | 1035 | ; tcExtendIdEnv tup_ids $ do
|
| 1036 | 1036 | { ((stmts', (ret_op', tup_rets)), stmts_ty)
|
| 1037 | - <- tcInfer $ \ exp_ty ->
|
|
| 1037 | + <- runInferRho $ \ exp_ty ->
|
|
| 1038 | 1038 | tcStmtsAndThen ctxt tcDoStmt stmts exp_ty $ \ inner_res_ty ->
|
| 1039 | 1039 | do { tup_rets <- zipWithM tcCheckId tup_names
|
| 1040 | 1040 | (map mkCheckExpType tup_elt_tys)
|
| ... | ... | @@ -1046,7 +1046,7 @@ tcDoStmt ctxt (RecStmt { recS_stmts = L l stmts, recS_later_ids = later_names |
| 1046 | 1046 | ; return (ret_op', tup_rets) }
|
| 1047 | 1047 | |
| 1048 | 1048 | ; ((_, mfix_op'), mfix_res_ty)
|
| 1049 | - <- tcInfer $ \ exp_ty ->
|
|
| 1049 | + <- runInferRho $ \ exp_ty ->
|
|
| 1050 | 1050 | tcSyntaxOp DoOrigin mfix_op
|
| 1051 | 1051 | [synKnownType (mkVisFunTyMany tup_ty stmts_ty)] exp_ty $
|
| 1052 | 1052 | \ _ _ -> return ()
|
| ... | ... | @@ -1172,7 +1172,7 @@ tcApplicativeStmts |
| 1172 | 1172 | tcApplicativeStmts ctxt pairs rhs_ty thing_inside
|
| 1173 | 1173 | = do { body_ty <- newFlexiTyVarTy liftedTypeKind
|
| 1174 | 1174 | ; let arity = length pairs
|
| 1175 | - ; ts <- replicateM (arity-1) $ newInferExpType
|
|
| 1175 | + ; ts <- replicateM (arity-1) $ newInferExpType IIF_DeepRho
|
|
| 1176 | 1176 | ; exp_tys <- replicateM arity $ newFlexiTyVarTy liftedTypeKind
|
| 1177 | 1177 | ; pat_tys <- replicateM arity $ newFlexiTyVarTy liftedTypeKind
|
| 1178 | 1178 | ; let fun_ty = mkVisFunTysMany pat_tys body_ty
|
| ... | ... | @@ -26,7 +26,7 @@ where |
| 26 | 26 | |
| 27 | 27 | import GHC.Prelude
|
| 28 | 28 | |
| 29 | -import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcSyntaxOp, tcSyntaxOpGen, tcInferRho )
|
|
| 29 | +import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcSyntaxOp, tcSyntaxOpGen, tcInferExpr )
|
|
| 30 | 30 | |
| 31 | 31 | import GHC.Hs
|
| 32 | 32 | import GHC.Hs.Syn.Type
|
| ... | ... | @@ -220,7 +220,7 @@ tcInferPat :: FixedRuntimeRepContext |
| 220 | 220 | -> TcM a
|
| 221 | 221 | -> TcM ((LPat GhcTc, a), TcSigmaTypeFRR)
|
| 222 | 222 | tcInferPat frr_orig ctxt pat thing_inside
|
| 223 | - = tcInferFRR frr_orig $ \ exp_ty ->
|
|
| 223 | + = runInferSigmaFRR frr_orig $ \ exp_ty ->
|
|
| 224 | 224 | tc_lpat (unrestricted exp_ty) penv pat thing_inside
|
| 225 | 225 | where
|
| 226 | 226 | penv = PE { pe_lazy = False, pe_ctxt = LamPat ctxt, pe_orig = PatOrigin }
|
| ... | ... | @@ -694,15 +694,17 @@ tc_pat pat_ty penv ps_pat thing_inside = case ps_pat of |
| 694 | 694 | -- restriction need to be put in place, if any, for linear view
|
| 695 | 695 | -- patterns to desugar to type-correct Core.
|
| 696 | 696 | |
| 697 | - ; (expr',expr_ty) <- tcInferRho expr
|
|
| 698 | - -- Note [View patterns and polymorphism]
|
|
| 697 | + ; (expr', expr_rho) <- tcInferExpr IIF_ShallowRho expr
|
|
| 698 | + -- IIF_ShallowRho: do not perform deep instantiation, regardless of
|
|
| 699 | + -- DeepSubsumption (Note [View patterns and polymorphism])
|
|
| 700 | + -- But we must do top-instantiation to expose the arrow to matchActualFunTy
|
|
| 699 | 701 | |
| 700 | 702 | -- Expression must be a function
|
| 701 | 703 | ; let herald = ExpectedFunTyViewPat $ unLoc expr
|
| 702 | 704 | ; (expr_wrap1, Scaled _mult inf_arg_ty, inf_res_sigma)
|
| 703 | - <- matchActualFunTy herald (Just . HsExprRnThing $ unLoc expr) (1,expr_ty) expr_ty
|
|
| 705 | + <- matchActualFunTy herald (Just . HsExprRnThing $ unLoc expr) (1,expr_rho) expr_rho
|
|
| 704 | 706 | -- See Note [View patterns and polymorphism]
|
| 705 | - -- expr_wrap1 :: expr_ty "->" (inf_arg_ty -> inf_res_sigma)
|
|
| 707 | + -- expr_wrap1 :: expr_rho "->" (inf_arg_ty -> inf_res_sigma)
|
|
| 706 | 708 | |
| 707 | 709 | -- Check that overall pattern is more polymorphic than arg type
|
| 708 | 710 | ; expr_wrap2 <- tc_sub_type penv (scaledThing pat_ty) inf_arg_ty
|
| ... | ... | @@ -715,18 +717,18 @@ tc_pat pat_ty penv ps_pat thing_inside = case ps_pat of |
| 715 | 717 | ; pat_ty <- readExpType h_pat_ty
|
| 716 | 718 | ; let expr_wrap2' = mkWpFun expr_wrap2 idHsWrapper
|
| 717 | 719 | (Scaled w pat_ty) inf_res_sigma
|
| 718 | - -- expr_wrap2' :: (inf_arg_ty -> inf_res_sigma) "->"
|
|
| 719 | - -- (pat_ty -> inf_res_sigma)
|
|
| 720 | - -- NB: pat_ty comes from matchActualFunTy, so it has a
|
|
| 721 | - -- fixed RuntimeRep, as needed to call mkWpFun.
|
|
| 722 | - ; let
|
|
| 720 | + -- expr_wrap2' :: (inf_arg_ty -> inf_res_sigma) "->"
|
|
| 721 | + -- (pat_ty -> inf_res_sigma)
|
|
| 722 | + -- NB: pat_ty comes from matchActualFunTy, so it has a
|
|
| 723 | + -- fixed RuntimeRep, as needed to call mkWpFun.
|
|
| 724 | + |
|
| 723 | 725 | expr_wrap = expr_wrap2' <.> expr_wrap1
|
| 724 | 726 | |
| 725 | 727 | ; return $ (ViewPat pat_ty (mkLHsWrap expr_wrap expr') pat', res) }
|
| 726 | 728 | |
| 727 | 729 | {- Note [View patterns and polymorphism]
|
| 728 | 730 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 729 | -Consider this exotic example:
|
|
| 731 | +Consider this exotic example (test T26331a):
|
|
| 730 | 732 | pair :: forall a. Bool -> a -> forall b. b -> (a,b)
|
| 731 | 733 | |
| 732 | 734 | f :: Int -> blah
|
| ... | ... | @@ -735,11 +737,15 @@ Consider this exotic example: |
| 735 | 737 | The expression (pair True) should have type
|
| 736 | 738 | pair True :: Int -> forall b. b -> (Int,b)
|
| 737 | 739 | so that it is ready to consume the incoming Int. It should be an
|
| 738 | -arrow type (t1 -> t2); hence using (tcInferRho expr).
|
|
| 740 | +arrow type (t1 -> t2); and we must not instantiate that `forall b`,
|
|
| 741 | +/even with DeepSubsumption/. Hence using `IIF_ShallowRho`; this is the only
|
|
| 742 | +place where `IIF_ShallowRho` is used.
|
|
| 739 | 743 | |
| 740 | 744 | Then, when taking that arrow apart we want to get a *sigma* type
|
| 741 | 745 | (forall b. b->(Int,b)), because that's what we want to bind 'x' to.
|
| 742 | 746 | Fortunately that's what matchActualFunTy returns anyway.
|
| 747 | + |
|
| 748 | +Another example is #26331.
|
|
| 743 | 749 | -}
|
| 744 | 750 | |
| 745 | 751 | -- Type signatures in patterns
|
| ... | ... | @@ -768,8 +774,7 @@ Fortunately that's what matchActualFunTy returns anyway. |
| 768 | 774 | penv pats thing_inside
|
| 769 | 775 | ; pat_ty <- readExpType (scaledThing pat_ty)
|
| 770 | 776 | ; return (mkHsWrapPat coi
|
| 771 | - (ListPat elt_ty pats') pat_ty, res)
|
|
| 772 | -}
|
|
| 777 | + (ListPat elt_ty pats') pat_ty, res) }
|
|
| 773 | 778 | |
| 774 | 779 | TuplePat _ pats boxity -> do
|
| 775 | 780 | { let arity = length pats
|
| ... | ... | @@ -62,7 +62,6 @@ import GHC.Tc.Gen.Match |
| 62 | 62 | import GHC.Tc.Utils.Unify( checkConstraints, tcSubTypeSigma )
|
| 63 | 63 | import GHC.Tc.Zonk.Type
|
| 64 | 64 | import GHC.Tc.Gen.Expr
|
| 65 | -import GHC.Tc.Gen.App( tcInferSigma )
|
|
| 66 | 65 | import GHC.Tc.Utils.Monad
|
| 67 | 66 | import GHC.Tc.Gen.Export
|
| 68 | 67 | import GHC.Tc.Types.Evidence
|
| ... | ... | @@ -2628,10 +2627,11 @@ tcRnExpr hsc_env mode rdr_expr |
| 2628 | 2627 | failIfErrsM ;
|
| 2629 | 2628 | |
| 2630 | 2629 | -- Typecheck the expression
|
| 2631 | - ((tclvl, res_ty), lie)
|
|
| 2630 | + ((tclvl, (_tc_expr, res_ty)), lie)
|
|
| 2632 | 2631 | <- captureTopConstraints $
|
| 2633 | 2632 | pushTcLevelM $
|
| 2634 | - tcInferSigma inst rn_expr ;
|
|
| 2633 | + (if inst then tcInferRho rn_expr
|
|
| 2634 | + else tcInferSigma rn_expr);
|
|
| 2635 | 2635 | |
| 2636 | 2636 | -- Generalise
|
| 2637 | 2637 | uniq <- newUnique ;
|
| ... | ... | @@ -206,9 +206,15 @@ instance Monoid HsWrapper where |
| 206 | 206 | (<.>) :: HsWrapper -> HsWrapper -> HsWrapper
|
| 207 | 207 | WpHole <.> c = c
|
| 208 | 208 | c <.> WpHole = c
|
| 209 | -WpCast c1 <.> WpCast c2 = WpCast (c1 `mkTransCo` c2)
|
|
| 209 | +WpCast c1 <.> WpCast c2 = WpCast (c2 `mkTransCo` c1)
|
|
| 210 | 210 | -- If we can represent the HsWrapper as a cast, try to do so: this may avoid
|
| 211 | 211 | -- unnecessary eta-expansion (see 'mkWpFun').
|
| 212 | + --
|
|
| 213 | + -- NB: <.> behaves like function composition:
|
|
| 214 | + --
|
|
| 215 | + -- WpCast c1 <.> WpCast c2 :: coercionLKind c2 ~> coercionRKind c1
|
|
| 216 | + --
|
|
| 217 | + -- This is thus the same as WpCast (c2 ; c1) and not WpCast (c1 ; c2).
|
|
| 212 | 218 | c1 <.> c2 = c1 `WpCompose` c2
|
| 213 | 219 | |
| 214 | 220 | -- | Smart constructor to create a 'WpFun' 'HsWrapper', which avoids introducing
|
| ... | ... | @@ -65,7 +65,7 @@ module GHC.Tc.Utils.TcMType ( |
| 65 | 65 | -- Expected types
|
| 66 | 66 | ExpType(..), ExpSigmaType, ExpRhoType,
|
| 67 | 67 | mkCheckExpType, newInferExpType, newInferExpTypeFRR,
|
| 68 | - tcInfer, tcInferFRR,
|
|
| 68 | + runInfer, runInferRho, runInferSigma, runInferKind, runInferRhoFRR, runInferSigmaFRR,
|
|
| 69 | 69 | readExpType, readExpType_maybe, readScaledExpType,
|
| 70 | 70 | expTypeToType, scaledExpTypeToType,
|
| 71 | 71 | checkingExpType_maybe, checkingExpType,
|
| ... | ... | @@ -438,30 +438,29 @@ See test case T21325. |
| 438 | 438 | |
| 439 | 439 | -- actual data definition is in GHC.Tc.Utils.TcType
|
| 440 | 440 | |
| 441 | -newInferExpType :: TcM ExpType
|
|
| 442 | -newInferExpType = new_inferExpType Nothing
|
|
| 441 | +newInferExpType :: InferInstFlag -> TcM ExpType
|
|
| 442 | +newInferExpType iif = new_inferExpType iif IFRR_Any
|
|
| 443 | 443 | |
| 444 | -newInferExpTypeFRR :: FixedRuntimeRepContext -> TcM ExpTypeFRR
|
|
| 445 | -newInferExpTypeFRR frr_orig
|
|
| 444 | +newInferExpTypeFRR :: InferInstFlag -> FixedRuntimeRepContext -> TcM ExpTypeFRR
|
|
| 445 | +newInferExpTypeFRR iif frr_orig
|
|
| 446 | 446 | = do { th_lvl <- getThLevel
|
| 447 | - ; if
|
|
| 448 | - -- See [Wrinkle: Typed Template Haskell]
|
|
| 449 | - -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
|
|
| 450 | - | TypedBrack _ <- th_lvl
|
|
| 451 | - -> new_inferExpType Nothing
|
|
| 447 | + ; let mb_frr = case th_lvl of
|
|
| 448 | + TypedBrack {} -> IFRR_Any
|
|
| 449 | + _ -> IFRR_Check frr_orig
|
|
| 450 | + -- mb_frr: see [Wrinkle: Typed Template Haskell]
|
|
| 451 | + -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
|
|
| 452 | 452 | |
| 453 | - | otherwise
|
|
| 454 | - -> new_inferExpType (Just frr_orig) }
|
|
| 453 | + ; new_inferExpType iif mb_frr }
|
|
| 455 | 454 | |
| 456 | -new_inferExpType :: Maybe FixedRuntimeRepContext -> TcM ExpType
|
|
| 457 | -new_inferExpType mb_frr_orig
|
|
| 455 | +new_inferExpType :: InferInstFlag -> InferFRRFlag -> TcM ExpType
|
|
| 456 | +new_inferExpType iif ifrr
|
|
| 458 | 457 | = do { u <- newUnique
|
| 459 | 458 | ; tclvl <- getTcLevel
|
| 460 | 459 | ; traceTc "newInferExpType" (ppr u <+> ppr tclvl)
|
| 461 | 460 | ; ref <- newMutVar Nothing
|
| 462 | 461 | ; return (Infer (IR { ir_uniq = u, ir_lvl = tclvl
|
| 463 | - , ir_ref = ref
|
|
| 464 | - , ir_frr = mb_frr_orig })) }
|
|
| 462 | + , ir_inst = iif, ir_frr = ifrr
|
|
| 463 | + , ir_ref = ref })) }
|
|
| 465 | 464 | |
| 466 | 465 | -- | Extract a type out of an ExpType, if one exists. But one should always
|
| 467 | 466 | -- exist. Unless you're quite sure you know what you're doing.
|
| ... | ... | @@ -515,12 +514,12 @@ inferResultToType (IR { ir_uniq = u, ir_lvl = tc_lvl |
| 515 | 514 | where
|
| 516 | 515 | -- See Note [TcLevel of ExpType]
|
| 517 | 516 | new_meta = case mb_frr of
|
| 518 | - Nothing -> do { rr <- newMetaTyVarTyAtLevel tc_lvl runtimeRepTy
|
|
| 517 | + IFRR_Any -> do { rr <- newMetaTyVarTyAtLevel tc_lvl runtimeRepTy
|
|
| 519 | 518 | ; newMetaTyVarTyAtLevel tc_lvl (mkTYPEapp rr) }
|
| 520 | - Just frr -> mdo { rr <- newConcreteTyVarTyAtLevel conc_orig tc_lvl runtimeRepTy
|
|
| 521 | - ; tau <- newMetaTyVarTyAtLevel tc_lvl (mkTYPEapp rr)
|
|
| 522 | - ; let conc_orig = ConcreteFRR $ FixedRuntimeRepOrigin tau frr
|
|
| 523 | - ; return tau }
|
|
| 519 | + IFRR_Check frr -> mdo { rr <- newConcreteTyVarTyAtLevel conc_orig tc_lvl runtimeRepTy
|
|
| 520 | + ; tau <- newMetaTyVarTyAtLevel tc_lvl (mkTYPEapp rr)
|
|
| 521 | + ; let conc_orig = ConcreteFRR $ FixedRuntimeRepOrigin tau frr
|
|
| 522 | + ; return tau }
|
|
| 524 | 523 | |
| 525 | 524 | {- Note [inferResultToType]
|
| 526 | 525 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -537,20 +536,31 @@ Note [fillInferResult] in GHC.Tc.Utils.Unify. |
| 537 | 536 | -- | Infer a type using a fresh ExpType
|
| 538 | 537 | -- See also Note [ExpType] in "GHC.Tc.Utils.TcMType"
|
| 539 | 538 | --
|
| 540 | --- Use 'tcInferFRR' if you require the type to have a fixed
|
|
| 539 | +-- Use 'runInferFRR' if you require the type to have a fixed
|
|
| 541 | 540 | -- runtime representation.
|
| 542 | -tcInfer :: (ExpSigmaType -> TcM a) -> TcM (a, TcSigmaType)
|
|
| 543 | -tcInfer = tc_infer Nothing
|
|
| 541 | +runInferSigma :: (ExpSigmaType -> TcM a) -> TcM (a, TcSigmaType)
|
|
| 542 | +runInferSigma = runInfer IIF_Sigma IFRR_Any
|
|
| 544 | 543 | |
| 545 | --- | Like 'tcInfer', except it ensures that the resulting type
|
|
| 544 | +runInferRho :: (ExpRhoType -> TcM a) -> TcM (a, TcRhoType)
|
|
| 545 | +runInferRho = runInfer IIF_DeepRho IFRR_Any
|
|
| 546 | + |
|
| 547 | +runInferKind :: (ExpSigmaType -> TcM a) -> TcM (a, TcSigmaType)
|
|
| 548 | +-- Used for kind-checking types, where we never want deep instantiation,
|
|
| 549 | +-- nor FRR checks
|
|
| 550 | +runInferKind = runInfer IIF_Sigma IFRR_Any
|
|
| 551 | + |
|
| 552 | +-- | Like 'runInferRho', except it ensures that the resulting type
|
|
| 546 | 553 | -- has a syntactically fixed RuntimeRep as per Note [Fixed RuntimeRep] in
|
| 547 | 554 | -- GHC.Tc.Utils.Concrete.
|
| 548 | -tcInferFRR :: FixedRuntimeRepContext -> (ExpSigmaTypeFRR -> TcM a) -> TcM (a, TcSigmaTypeFRR)
|
|
| 549 | -tcInferFRR frr_orig = tc_infer (Just frr_orig)
|
|
| 555 | +runInferRhoFRR :: FixedRuntimeRepContext -> (ExpRhoTypeFRR -> TcM a) -> TcM (a, TcRhoTypeFRR)
|
|
| 556 | +runInferRhoFRR frr_orig = runInfer IIF_DeepRho (IFRR_Check frr_orig)
|
|
| 557 | + |
|
| 558 | +runInferSigmaFRR :: FixedRuntimeRepContext -> (ExpSigmaTypeFRR -> TcM a) -> TcM (a, TcSigmaTypeFRR)
|
|
| 559 | +runInferSigmaFRR frr_orig = runInfer IIF_Sigma (IFRR_Check frr_orig)
|
|
| 550 | 560 | |
| 551 | -tc_infer :: Maybe FixedRuntimeRepContext -> (ExpSigmaType -> TcM a) -> TcM (a, TcSigmaType)
|
|
| 552 | -tc_infer mb_frr tc_check
|
|
| 553 | - = do { res_ty <- new_inferExpType mb_frr
|
|
| 561 | +runInfer :: InferInstFlag -> InferFRRFlag -> (ExpSigmaType -> TcM a) -> TcM (a, TcSigmaType)
|
|
| 562 | +runInfer iif mb_frr tc_check
|
|
| 563 | + = do { res_ty <- new_inferExpType iif mb_frr
|
|
| 554 | 564 | ; result <- tc_check res_ty
|
| 555 | 565 | ; res_ty <- readExpType res_ty
|
| 556 | 566 | ; return (result, res_ty) }
|
| ... | ... | @@ -24,14 +24,14 @@ module GHC.Tc.Utils.TcType ( |
| 24 | 24 | --------------------------------
|
| 25 | 25 | -- Types
|
| 26 | 26 | TcType, TcSigmaType, TcTypeFRR, TcSigmaTypeFRR,
|
| 27 | - TcRhoType, TcTauType, TcPredType, TcThetaType,
|
|
| 27 | + TcRhoType, TcRhoTypeFRR, TcTauType, TcPredType, TcThetaType,
|
|
| 28 | 28 | TcTyVar, TcTyVarSet, TcDTyVarSet, TcTyCoVarSet, TcDTyCoVarSet,
|
| 29 | 29 | TcKind, TcCoVar, TcTyCoVar, TcTyVarBinder, TcInvisTVBinder, TcReqTVBinder,
|
| 30 | 30 | TcTyCon, MonoTcTyCon, PolyTcTyCon, TcTyConBinder, KnotTied,
|
| 31 | 31 | |
| 32 | - ExpType(..), ExpKind, InferResult(..),
|
|
| 32 | + ExpType(..), ExpKind, InferResult(..), InferInstFlag(..), InferFRRFlag(..),
|
|
| 33 | 33 | ExpTypeFRR, ExpSigmaType, ExpSigmaTypeFRR,
|
| 34 | - ExpRhoType,
|
|
| 34 | + ExpRhoType, ExpRhoTypeFRR,
|
|
| 35 | 35 | mkCheckExpType,
|
| 36 | 36 | checkingExpType_maybe, checkingExpType,
|
| 37 | 37 | |
| ... | ... | @@ -380,6 +380,7 @@ type TcSigmaType = TcType |
| 380 | 380 | -- See Note [Return arguments with a fixed RuntimeRep.
|
| 381 | 381 | type TcSigmaTypeFRR = TcSigmaType
|
| 382 | 382 | -- TODO: consider making this a newtype.
|
| 383 | +type TcRhoTypeFRR = TcRhoType
|
|
| 383 | 384 | |
| 384 | 385 | type TcRhoType = TcType -- Note [TcRhoType]
|
| 385 | 386 | type TcTauType = TcType
|
| ... | ... | @@ -408,9 +409,13 @@ data InferResult |
| 408 | 409 | , ir_lvl :: TcLevel
|
| 409 | 410 | -- ^ See Note [TcLevel of ExpType] in GHC.Tc.Utils.TcMType
|
| 410 | 411 | |
| 411 | - , ir_frr :: Maybe FixedRuntimeRepContext
|
|
| 412 | + , ir_frr :: InferFRRFlag
|
|
| 412 | 413 | -- ^ See Note [FixedRuntimeRep context in ExpType] in GHC.Tc.Utils.TcMType
|
| 413 | 414 | |
| 415 | + , ir_inst :: InferInstFlag
|
|
| 416 | + -- ^ True <=> when DeepSubsumption is on, deeply instantiate before filling,
|
|
| 417 | + -- See Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify
|
|
| 418 | + |
|
| 414 | 419 | , ir_ref :: IORef (Maybe TcType) }
|
| 415 | 420 | -- ^ The type that fills in this hole should be a @Type@,
|
| 416 | 421 | -- that is, its kind should be @TYPE rr@ for some @rr :: RuntimeRep@.
|
| ... | ... | @@ -419,26 +424,48 @@ data InferResult |
| 419 | 424 | -- @rr@ must be concrete, in the sense of Note [Concrete types]
|
| 420 | 425 | -- in GHC.Tc.Utils.Concrete.
|
| 421 | 426 | |
| 422 | -type ExpSigmaType = ExpType
|
|
| 427 | +data InferFRRFlag
|
|
| 428 | + = IFRR_Check -- Check that the result type has a fixed runtime rep
|
|
| 429 | + FixedRuntimeRepContext -- Typically used for function arguments and lambdas
|
|
| 430 | + |
|
| 431 | + | IFRR_Any -- No need to check for fixed runtime-rep
|
|
| 432 | + |
|
| 433 | +data InferInstFlag -- Specifies whether the inference should return an uninstantiated
|
|
| 434 | + -- SigmaType, or a (possibly deeply) instantiated RhoType
|
|
| 435 | + -- See Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify
|
|
| 436 | + |
|
| 437 | + = IIF_Sigma -- Trying to infer a SigmaType
|
|
| 438 | + -- Don't instantiate at all, regardless of DeepSubsumption
|
|
| 439 | + -- Typically used when inferring the type of a pattern
|
|
| 440 | + |
|
| 441 | + | IIF_ShallowRho -- Trying to infer a shallow RhoType (no foralls or => at the top)
|
|
| 442 | + -- Top-instantiate (only, regardless of DeepSubsumption) before filling the hole
|
|
| 443 | + -- Typically used when inferring the type of an expression
|
|
| 444 | + |
|
| 445 | + | IIF_DeepRho -- Trying to infer a possibly-deep RhoType (depending on DeepSubsumption)
|
|
| 446 | + -- If DeepSubsumption is off, same as IIF_ShallowRho
|
|
| 447 | + -- If DeepSubsumption is on, instantiate deeply before filling the hole
|
|
| 448 | + |
|
| 449 | +type ExpSigmaType = ExpType
|
|
| 450 | +type ExpRhoType = ExpType
|
|
| 451 | + -- Invariant: in ExpRhoType, if -XDeepSubsumption is on,
|
|
| 452 | + -- and we are in checking mode (i.e. the ExpRhoType is (Check rho)),
|
|
| 453 | + -- then the `rho` is deeply skolemised
|
|
| 423 | 454 | |
| 424 | 455 | -- | An 'ExpType' which has a fixed RuntimeRep.
|
| 425 | 456 | --
|
| 426 | 457 | -- For a 'Check' 'ExpType', the stored 'TcType' must have
|
| 427 | 458 | -- a fixed RuntimeRep. For an 'Infer' 'ExpType', the 'ir_frr'
|
| 428 | --- field must be of the form @Just frr_orig@.
|
|
| 429 | -type ExpTypeFRR = ExpType
|
|
| 459 | +-- field must be of the form @IFRR_Check frr_orig@.
|
|
| 460 | +type ExpTypeFRR = ExpType
|
|
| 430 | 461 | |
| 431 | 462 | -- | Like 'TcSigmaTypeFRR', but for an expected type.
|
| 432 | 463 | --
|
| 433 | 464 | -- See 'ExpTypeFRR'.
|
| 434 | 465 | type ExpSigmaTypeFRR = ExpTypeFRR
|
| 466 | +type ExpRhoTypeFRR = ExpTypeFRR
|
|
| 435 | 467 | -- TODO: consider making this a newtype.
|
| 436 | 468 | |
| 437 | -type ExpRhoType = ExpType
|
|
| 438 | - -- Invariant: if -XDeepSubsumption is on,
|
|
| 439 | - -- and we are checking (i.e. the ExpRhoType is (Check rho)),
|
|
| 440 | - -- then the `rho` is deeply skolemised
|
|
| 441 | - |
|
| 442 | 469 | -- | Like 'ExpType', but on kind level
|
| 443 | 470 | type ExpKind = ExpType
|
| 444 | 471 | |
| ... | ... | @@ -447,12 +474,17 @@ instance Outputable ExpType where |
| 447 | 474 | ppr (Infer ir) = ppr ir
|
| 448 | 475 | |
| 449 | 476 | instance Outputable InferResult where
|
| 450 | - ppr (IR { ir_uniq = u, ir_lvl = lvl, ir_frr = mb_frr })
|
|
| 451 | - = text "Infer" <> mb_frr_text <> braces (ppr u <> comma <> ppr lvl)
|
|
| 477 | + ppr (IR { ir_uniq = u, ir_lvl = lvl, ir_frr = mb_frr, ir_inst = inst })
|
|
| 478 | + = text "Infer" <> parens (pp_inst <> pp_frr)
|
|
| 479 | + <> braces (ppr u <> comma <> ppr lvl)
|
|
| 452 | 480 | where
|
| 453 | - mb_frr_text = case mb_frr of
|
|
| 454 | - Just _ -> text "FRR"
|
|
| 455 | - Nothing -> empty
|
|
| 481 | + pp_inst = case inst of
|
|
| 482 | + IIF_Sigma -> text "Sigma"
|
|
| 483 | + IIF_ShallowRho -> text "ShallowRho"
|
|
| 484 | + IIF_DeepRho -> text "DeepRho"
|
|
| 485 | + pp_frr = case mb_frr of
|
|
| 486 | + IFRR_Check {} -> text ",FRR"
|
|
| 487 | + IFRR_Any -> empty
|
|
| 456 | 488 | |
| 457 | 489 | -- | Make an 'ExpType' suitable for checking.
|
| 458 | 490 | mkCheckExpType :: TcType -> ExpType
|
| ... | ... | @@ -27,7 +27,7 @@ module GHC.Tc.Utils.Unify ( |
| 27 | 27 | -- Skolemisation
|
| 28 | 28 | DeepSubsumptionFlag(..), getDeepSubsumptionFlag, isRhoTyDS,
|
| 29 | 29 | tcSkolemise, tcSkolemiseCompleteSig, tcSkolemiseExpectedType,
|
| 30 | - deeplyInstantiate,
|
|
| 30 | + dsInstantiate,
|
|
| 31 | 31 | |
| 32 | 32 | -- Various unifications
|
| 33 | 33 | unifyType, unifyKind, unifyInvisibleType,
|
| ... | ... | @@ -40,7 +40,6 @@ module GHC.Tc.Utils.Unify ( |
| 40 | 40 | |
| 41 | 41 | --------------------------------
|
| 42 | 42 | -- Holes
|
| 43 | - tcInfer,
|
|
| 44 | 43 | matchExpectedListTy,
|
| 45 | 44 | matchExpectedTyConApp,
|
| 46 | 45 | matchExpectedAppTy,
|
| ... | ... | @@ -60,7 +59,7 @@ module GHC.Tc.Utils.Unify ( |
| 60 | 59 | |
| 61 | 60 | simpleUnifyCheck, UnifyCheckCaller(..), SimpleUnifyResult(..),
|
| 62 | 61 | |
| 63 | - fillInferResult, fillInferResultDS
|
|
| 62 | + fillInferResult, fillInferResultNoInst
|
|
| 64 | 63 | ) where
|
| 65 | 64 | |
| 66 | 65 | import GHC.Prelude
|
| ... | ... | @@ -801,13 +800,13 @@ matchExpectedFunTys :: forall a. |
| 801 | 800 | -- If exp_ty is Infer {}, then [ExpPatType] and ExpRhoType results are all Infer{}
|
| 802 | 801 | matchExpectedFunTys herald _ctxt arity (Infer inf_res) thing_inside
|
| 803 | 802 | = do { arg_tys <- mapM (new_infer_arg_ty herald) [1 .. arity]
|
| 804 | - ; res_ty <- newInferExpType
|
|
| 803 | + ; res_ty <- newInferExpType (ir_inst inf_res)
|
|
| 805 | 804 | ; result <- thing_inside (map ExpFunPatTy arg_tys) res_ty
|
| 806 | 805 | ; arg_tys <- mapM (\(Scaled m t) -> Scaled m <$> readExpType t) arg_tys
|
| 807 | 806 | ; res_ty <- readExpType res_ty
|
| 808 | 807 | -- NB: mkScaledFunTys arg_tys res_ty does not contain any foralls
|
| 809 | 808 | -- (even nested ones), so no need to instantiate.
|
| 810 | - ; co <- fillInferResult (mkScaledFunTys arg_tys res_ty) inf_res
|
|
| 809 | + ; co <- fillInferResultNoInst (mkScaledFunTys arg_tys res_ty) inf_res
|
|
| 811 | 810 | ; return (mkWpCastN co, result) }
|
| 812 | 811 | |
| 813 | 812 | matchExpectedFunTys herald ctx arity (Check top_ty) thing_inside
|
| ... | ... | @@ -914,10 +913,10 @@ matchExpectedFunTys herald ctx arity (Check top_ty) thing_inside |
| 914 | 913 | ; co <- unifyType Nothing (mkScaledFunTys more_arg_tys res_ty) fun_ty
|
| 915 | 914 | ; return (mkWpCastN co, result) }
|
| 916 | 915 | |
| 917 | -new_infer_arg_ty :: ExpectedFunTyOrigin -> Int -> TcM (Scaled ExpSigmaTypeFRR)
|
|
| 916 | +new_infer_arg_ty :: ExpectedFunTyOrigin -> Int -> TcM (Scaled ExpRhoTypeFRR)
|
|
| 918 | 917 | new_infer_arg_ty herald arg_pos -- position for error messages only
|
| 919 | 918 | = do { mult <- newFlexiTyVarTy multiplicityTy
|
| 920 | - ; inf_hole <- newInferExpTypeFRR (FRRExpectedFunTy herald arg_pos)
|
|
| 919 | + ; inf_hole <- newInferExpTypeFRR IIF_DeepRho (FRRExpectedFunTy herald arg_pos)
|
|
| 921 | 920 | ; return (mkScaled mult inf_hole) }
|
| 922 | 921 | |
| 923 | 922 | new_check_arg_ty :: ExpectedFunTyOrigin -> Int -> TcM (Scaled TcType)
|
| ... | ... | @@ -1075,18 +1074,6 @@ matchExpectedAppTy orig_ty |
| 1075 | 1074 | *
|
| 1076 | 1075 | ********************************************************************** -}
|
| 1077 | 1076 | |
| 1078 | -{- Note [inferResultToType]
|
|
| 1079 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1080 | -expTypeToType and inferResultType convert an InferResult to a monotype.
|
|
| 1081 | -It must be a monotype because if the InferResult isn't already filled in,
|
|
| 1082 | -we fill it in with a unification variable (hence monotype). So to preserve
|
|
| 1083 | -order-independence we check for mono-type-ness even if it *is* filled in
|
|
| 1084 | -already.
|
|
| 1085 | - |
|
| 1086 | -See also Note [TcLevel of ExpType] in GHC.Tc.Utils.TcType, and
|
|
| 1087 | -Note [fillInferResult].
|
|
| 1088 | --}
|
|
| 1089 | - |
|
| 1090 | 1077 | -- | Fill an 'InferResult' with the given type.
|
| 1091 | 1078 | --
|
| 1092 | 1079 | -- If @co = fillInferResult t1 infer_res@, then @co :: t1 ~# t2@,
|
| ... | ... | @@ -1098,14 +1085,14 @@ Note [fillInferResult]. |
| 1098 | 1085 | -- The stored type @t2@ is at the same level as given by the
|
| 1099 | 1086 | -- 'ir_lvl' field.
|
| 1100 | 1087 | -- - FRR invariant.
|
| 1101 | --- Whenever the 'ir_frr' field is not @Nothing@, @t2@ is guaranteed
|
|
| 1088 | +-- Whenever the 'ir_frr' field is `IFRR_Check`, @t2@ is guaranteed
|
|
| 1102 | 1089 | -- to have a syntactically fixed RuntimeRep, in the sense of
|
| 1103 | 1090 | -- Note [Fixed RuntimeRep] in GHC.Tc.Utils.Concrete.
|
| 1104 | -fillInferResult :: TcType -> InferResult -> TcM TcCoercionN
|
|
| 1105 | -fillInferResult act_res_ty (IR { ir_uniq = u
|
|
| 1106 | - , ir_lvl = res_lvl
|
|
| 1107 | - , ir_frr = mb_frr
|
|
| 1108 | - , ir_ref = ref })
|
|
| 1091 | +fillInferResultNoInst :: TcType -> InferResult -> TcM TcCoercionN
|
|
| 1092 | +fillInferResultNoInst act_res_ty (IR { ir_uniq = u
|
|
| 1093 | + , ir_lvl = res_lvl
|
|
| 1094 | + , ir_frr = mb_frr
|
|
| 1095 | + , ir_ref = ref })
|
|
| 1109 | 1096 | = do { mb_exp_res_ty <- readTcRef ref
|
| 1110 | 1097 | ; case mb_exp_res_ty of
|
| 1111 | 1098 | Just exp_res_ty
|
| ... | ... | @@ -1126,7 +1113,7 @@ fillInferResult act_res_ty (IR { ir_uniq = u |
| 1126 | 1113 | ppr u <> colon <+> ppr act_res_ty <+> char '~' <+> ppr exp_res_ty
|
| 1127 | 1114 | ; cur_lvl <- getTcLevel
|
| 1128 | 1115 | ; unless (cur_lvl `sameDepthAs` res_lvl) $
|
| 1129 | - ensureMonoType act_res_ty
|
|
| 1116 | + ensureMonoType act_res_ty -- See (FIR1)
|
|
| 1130 | 1117 | ; unifyType Nothing act_res_ty exp_res_ty }
|
| 1131 | 1118 | Nothing
|
| 1132 | 1119 | -> do { traceTc "Filling inferred ExpType" $
|
| ... | ... | @@ -1140,16 +1127,28 @@ fillInferResult act_res_ty (IR { ir_uniq = u |
| 1140 | 1127 | -- fixed RuntimeRep (if necessary, i.e. 'mb_frr' is not 'Nothing').
|
| 1141 | 1128 | ; (frr_co, act_res_ty) <-
|
| 1142 | 1129 | case mb_frr of
|
| 1143 | - Nothing -> return (mkNomReflCo act_res_ty, act_res_ty)
|
|
| 1144 | - Just frr_orig -> hasFixedRuntimeRep frr_orig act_res_ty
|
|
| 1130 | + IFRR_Any -> return (mkNomReflCo act_res_ty, act_res_ty)
|
|
| 1131 | + IFRR_Check frr_orig -> hasFixedRuntimeRep frr_orig act_res_ty
|
|
| 1145 | 1132 | |
| 1146 | 1133 | -- Compose the two coercions.
|
| 1147 | 1134 | ; let final_co = prom_co `mkTransCo` frr_co
|
| 1148 | 1135 | |
| 1149 | 1136 | ; writeTcRef ref (Just act_res_ty)
|
| 1150 | 1137 | |
| 1151 | - ; return final_co }
|
|
| 1152 | - }
|
|
| 1138 | + ; return final_co } }
|
|
| 1139 | + |
|
| 1140 | +fillInferResult :: CtOrigin -> TcType -> InferResult -> TcM HsWrapper
|
|
| 1141 | +-- See Note [Instantiation of InferResult]
|
|
| 1142 | +fillInferResult ct_orig res_ty ires@(IR { ir_inst = iif })
|
|
| 1143 | + = case iif of
|
|
| 1144 | + IIF_Sigma -> do { co <- fillInferResultNoInst res_ty ires
|
|
| 1145 | + ; return (mkWpCastN co) }
|
|
| 1146 | + IIF_ShallowRho -> do { (wrap, res_ty') <- topInstantiate ct_orig res_ty
|
|
| 1147 | + ; co <- fillInferResultNoInst res_ty' ires
|
|
| 1148 | + ; return (mkWpCastN co <.> wrap) }
|
|
| 1149 | + IIF_DeepRho -> do { (wrap, res_ty') <- dsInstantiate ct_orig res_ty
|
|
| 1150 | + ; co <- fillInferResultNoInst res_ty' ires
|
|
| 1151 | + ; return (mkWpCastN co <.> wrap) }
|
|
| 1153 | 1152 | |
| 1154 | 1153 | {- Note [fillInferResult]
|
| 1155 | 1154 | ~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -1210,39 +1209,96 @@ For (2), we simply look to see if the hole is filled already. |
| 1210 | 1209 | - if it is filled, we simply unify with the type that is
|
| 1211 | 1210 | already there
|
| 1212 | 1211 | |
| 1213 | -There is one wrinkle. Suppose we have
|
|
| 1214 | - case e of
|
|
| 1215 | - T1 -> e1 :: (forall a. a->a) -> Int
|
|
| 1216 | - G2 -> e2
|
|
| 1217 | -where T1 is not GADT or existential, but G2 is a GADT. Then suppose the
|
|
| 1218 | -T1 alternative fills the hole with (forall a. a->a) -> Int, which is fine.
|
|
| 1219 | -But now the G2 alternative must not *just* unify with that else we'd risk
|
|
| 1220 | -allowing through (e2 :: (forall a. a->a) -> Int). If we'd checked G2 first
|
|
| 1221 | -we'd have filled the hole with a unification variable, which enforces a
|
|
| 1222 | -monotype.
|
|
| 1223 | - |
|
| 1224 | -So if we check G2 second, we still want to emit a constraint that restricts
|
|
| 1225 | -the RHS to be a monotype. This is done by ensureMonoType, and it works
|
|
| 1226 | -by simply generating a constraint (alpha ~ ty), where alpha is a fresh
|
|
| 1212 | +(FIR1) There is one wrinkle. Suppose we have
|
|
| 1213 | + case e of
|
|
| 1214 | + T1 -> e1 :: (forall a. a->a) -> Int
|
|
| 1215 | + G2 -> e2
|
|
| 1216 | + where T1 is not GADT or existential, but G2 is a GADT. Then suppose the
|
|
| 1217 | + T1 alternative fills the hole with (forall a. a->a) -> Int, which is fine.
|
|
| 1218 | + But now the G2 alternative must not *just* unify with that else we'd risk
|
|
| 1219 | + allowing through (e2 :: (forall a. a->a) -> Int). If we'd checked G2 first
|
|
| 1220 | + we'd have filled the hole with a unification variable, which enforces a
|
|
| 1221 | + monotype.
|
|
| 1222 | + |
|
| 1223 | + So if we check G2 second, we still want to emit a constraint that restricts
|
|
| 1224 | + the RHS to be a monotype. This is done by ensureMonoType, and it works
|
|
| 1225 | + by simply generating a constraint (alpha ~ ty), where alpha is a fresh
|
|
| 1227 | 1226 | unification variable. We discard the evidence.
|
| 1228 | 1227 | |
| 1229 | --}
|
|
| 1228 | +Note [Instantiation of InferResult]
|
|
| 1229 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1230 | +When typechecking expressions (not types, not patterns), we always almost
|
|
| 1231 | +always instantiate before filling in `InferResult`, so that the result is a
|
|
| 1232 | +TcRhoType. This behaviour is controlled by the `ir_inst :: InferInstFlag`
|
|
| 1233 | +field of `InferResult`.
|
|
| 1230 | 1234 | |
| 1231 | --- | A version of 'fillInferResult' that also performs deep instantiation
|
|
| 1232 | --- when deep subsumption is enabled.
|
|
| 1233 | ---
|
|
| 1234 | --- See also Note [Instantiation of InferResult].
|
|
| 1235 | -fillInferResultDS :: CtOrigin -> TcRhoType -> InferResult -> TcM HsWrapper
|
|
| 1236 | -fillInferResultDS ct_orig rho inf_res
|
|
| 1237 | - = do { massertPpr (isRhoTy rho) $
|
|
| 1238 | - vcat [ text "fillInferResultDS: input type is not a rho-type"
|
|
| 1239 | - , text "ty:" <+> ppr rho ]
|
|
| 1240 | - ; ds_flag <- getDeepSubsumptionFlag
|
|
| 1241 | - ; case ds_flag of
|
|
| 1242 | - Shallow -> mkWpCastN <$> fillInferResult rho inf_res
|
|
| 1243 | - Deep -> do { (inst_wrap, rho') <- deeplyInstantiate ct_orig rho
|
|
| 1244 | - ; co <- fillInferResult rho' inf_res
|
|
| 1245 | - ; return (mkWpCastN co <.> inst_wrap) } }
|
|
| 1235 | +If we do instantiate (ir_inst = IIF_DeepRho), and DeepSubsumption is enabled,
|
|
| 1236 | +we instantiate deeply. See `tcInferResult`.
|
|
| 1237 | + |
|
| 1238 | +Usually this field is `IIF_DeepRho` meaning "return a (possibly deep) rho-type".
|
|
| 1239 | +Why is this the common case? See #17173 for discussion. Here are some examples
|
|
| 1240 | +of why:
|
|
| 1241 | + |
|
| 1242 | +1. Consider
|
|
| 1243 | + f x = (*)
|
|
| 1244 | + We want to instantiate the type of (*) before returning, else we
|
|
| 1245 | + will infer the type
|
|
| 1246 | + f :: forall {a}. a -> forall b. Num b => b -> b -> b
|
|
| 1247 | + This is surely confusing for users.
|
|
| 1248 | + |
|
| 1249 | + And worse, the monomorphism restriction won't work properly. The MR is
|
|
| 1250 | + dealt with in simplifyInfer, and simplifyInfer has no way of
|
|
| 1251 | + instantiating. This could perhaps be worked around, but it may be
|
|
| 1252 | + hard to know even when instantiation should happen.
|
|
| 1253 | + |
|
| 1254 | +2. Another reason. Consider
|
|
| 1255 | + f :: (?x :: Int) => a -> a
|
|
| 1256 | + g y = let ?x = 3::Int in f
|
|
| 1257 | + Here want to instantiate f's type so that the ?x::Int constraint
|
|
| 1258 | + gets discharged by the enclosing implicit-parameter binding.
|
|
| 1259 | + |
|
| 1260 | +3. Suppose one defines plus = (+). If we instantiate lazily, we will
|
|
| 1261 | + infer plus :: forall a. Num a => a -> a -> a. However, the monomorphism
|
|
| 1262 | + restriction compels us to infer
|
|
| 1263 | + plus :: Integer -> Integer -> Integer
|
|
| 1264 | + (or similar monotype). Indeed, the only way to know whether to apply
|
|
| 1265 | + the monomorphism restriction at all is to instantiate
|
|
| 1266 | + |
|
| 1267 | +HOWEVER, not always! Here are places where we want `IIF_Sigma` meaning
|
|
| 1268 | +"return a sigma-type":
|
|
| 1269 | + |
|
| 1270 | +* IIF_Sigma: In GHC.Tc.Module.tcRnExpr, which implements GHCi's :type
|
|
| 1271 | + command, we want to return a completely uninstantiated type.
|
|
| 1272 | + See Note [Implementing :type] in GHC.Tc.Module.
|
|
| 1273 | + |
|
| 1274 | +* IIF_Sigma: In types we can't lambda-abstract, so we must be careful not to instantiate
|
|
| 1275 | + at all. See calls to `runInferHsType`
|
|
| 1276 | + |
|
| 1277 | +* IIF_Sigma: in patterns we don't want to instantiate at all. See the use of
|
|
| 1278 | + `runInferSigmaFRR` in GHC.Tc.Gen.Pat
|
|
| 1279 | + |
|
| 1280 | +* IIF_ShallowRho: in the expression part of a view pattern, we must top-instantiate
|
|
| 1281 | + but /not/ deeply instantiate (#26331). See Note [View patterns and polymorphism]
|
|
| 1282 | + in GHC.Tc.Gen.Pat. This the only place we use IIF_ShallowRho.
|
|
| 1283 | + |
|
| 1284 | +Why do we want to deeply instantiate, ever? Why isn't top-instantiation enough?
|
|
| 1285 | +Answer: to accept the following program (T26225b) with -XDeepSubsumption, we
|
|
| 1286 | +need to deeply instantiate when inferring in checkResultTy:
|
|
| 1287 | + |
|
| 1288 | + f :: Int -> (forall a. a->a)
|
|
| 1289 | + g :: Int -> Bool -> Bool
|
|
| 1290 | + |
|
| 1291 | + test b =
|
|
| 1292 | + case b of
|
|
| 1293 | + True -> f
|
|
| 1294 | + False -> g
|
|
| 1295 | + |
|
| 1296 | +If we don't deeply instantiate in the branches of the case expression, we will
|
|
| 1297 | +try to unify the type of 'f' with that of 'g', which fails. If we instead
|
|
| 1298 | +deeply instantiate 'f', we will fill the 'InferResult' with 'Int -> alpha -> alpha'
|
|
| 1299 | +which then successfully unifies with the type of 'g' when we come to fill the
|
|
| 1300 | +'InferResult' hole a second time for the second case branch.
|
|
| 1301 | +-}
|
|
| 1246 | 1302 | |
| 1247 | 1303 | {-
|
| 1248 | 1304 | ************************************************************************
|
| ... | ... | @@ -1337,7 +1393,7 @@ tcSubTypeMono rn_expr act_ty exp_ty |
| 1337 | 1393 | , text "act_ty:" <+> ppr act_ty
|
| 1338 | 1394 | , text "rn_expr:" <+> ppr rn_expr]) $
|
| 1339 | 1395 | case exp_ty of
|
| 1340 | - Infer inf_res -> fillInferResult act_ty inf_res
|
|
| 1396 | + Infer inf_res -> fillInferResultNoInst act_ty inf_res
|
|
| 1341 | 1397 | Check exp_ty -> unifyType (Just $ HsExprRnThing rn_expr) act_ty exp_ty
|
| 1342 | 1398 | |
| 1343 | 1399 | ------------------------
|
| ... | ... | @@ -1351,7 +1407,7 @@ tcSubTypePat inst_orig ctxt (Check ty_actual) ty_expected |
| 1351 | 1407 | = tc_sub_type unifyTypeET inst_orig ctxt ty_actual ty_expected
|
| 1352 | 1408 | |
| 1353 | 1409 | tcSubTypePat _ _ (Infer inf_res) ty_expected
|
| 1354 | - = do { co <- fillInferResult ty_expected inf_res
|
|
| 1410 | + = do { co <- fillInferResultNoInst ty_expected inf_res
|
|
| 1355 | 1411 | -- In patterns we do not instantatiate
|
| 1356 | 1412 | |
| 1357 | 1413 | ; return (mkWpCastN (mkSymCo co)) }
|
| ... | ... | @@ -1377,7 +1433,7 @@ tcSubTypeDS rn_expr act_rho exp_rho |
| 1377 | 1433 | -- | Checks that the 'actual' type is more polymorphic than the 'expected' type.
|
| 1378 | 1434 | tcSubType :: CtOrigin -- ^ Used when instantiating
|
| 1379 | 1435 | -> UserTypeCtxt -- ^ Used when skolemising
|
| 1380 | - -> Maybe TypedThing -- ^ The expression that has type 'actual' (if known)
|
|
| 1436 | + -> Maybe TypedThing -- ^ The expression that has type 'actual' (if known)
|
|
| 1381 | 1437 | -> TcSigmaType -- ^ Actual type
|
| 1382 | 1438 | -> ExpRhoType -- ^ Expected type
|
| 1383 | 1439 | -> TcM HsWrapper
|
| ... | ... | @@ -1386,10 +1442,7 @@ tcSubType inst_orig ctxt m_thing ty_actual res_ty |
| 1386 | 1442 | Check ty_expected -> tc_sub_type (unifyType m_thing) inst_orig ctxt
|
| 1387 | 1443 | ty_actual ty_expected
|
| 1388 | 1444 | |
| 1389 | - Infer inf_res -> do { (wrap, rho) <- topInstantiate inst_orig ty_actual
|
|
| 1390 | - -- See Note [Instantiation of InferResult]
|
|
| 1391 | - ; inst <- fillInferResultDS inst_orig rho inf_res
|
|
| 1392 | - ; return (inst <.> wrap) }
|
|
| 1445 | + Infer inf_res -> fillInferResult inst_orig ty_actual inf_res
|
|
| 1393 | 1446 | |
| 1394 | 1447 | ---------------
|
| 1395 | 1448 | tcSubTypeSigma :: CtOrigin -- where did the actual type arise / why are we
|
| ... | ... | @@ -1428,47 +1481,6 @@ addSubTypeCtxt ty_actual ty_expected thing_inside |
| 1428 | 1481 | ; return (tidy_env, SubTypeCtxt ty_expected ty_actual) }
|
| 1429 | 1482 | |
| 1430 | 1483 | |
| 1431 | -{- Note [Instantiation of InferResult]
|
|
| 1432 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1433 | -When typechecking expressions (not types, not patterns), we always instantiate
|
|
| 1434 | -before filling in InferResult, so that the result is a TcRhoType.
|
|
| 1435 | -See #17173 for discussion.
|
|
| 1436 | - |
|
| 1437 | -For example:
|
|
| 1438 | - |
|
| 1439 | -1. Consider
|
|
| 1440 | - f x = (*)
|
|
| 1441 | - We want to instantiate the type of (*) before returning, else we
|
|
| 1442 | - will infer the type
|
|
| 1443 | - f :: forall {a}. a -> forall b. Num b => b -> b -> b
|
|
| 1444 | - This is surely confusing for users.
|
|
| 1445 | - |
|
| 1446 | - And worse, the monomorphism restriction won't work properly. The MR is
|
|
| 1447 | - dealt with in simplifyInfer, and simplifyInfer has no way of
|
|
| 1448 | - instantiating. This could perhaps be worked around, but it may be
|
|
| 1449 | - hard to know even when instantiation should happen.
|
|
| 1450 | - |
|
| 1451 | -2. Another reason. Consider
|
|
| 1452 | - f :: (?x :: Int) => a -> a
|
|
| 1453 | - g y = let ?x = 3::Int in f
|
|
| 1454 | - Here want to instantiate f's type so that the ?x::Int constraint
|
|
| 1455 | - gets discharged by the enclosing implicit-parameter binding.
|
|
| 1456 | - |
|
| 1457 | -3. Suppose one defines plus = (+). If we instantiate lazily, we will
|
|
| 1458 | - infer plus :: forall a. Num a => a -> a -> a. However, the monomorphism
|
|
| 1459 | - restriction compels us to infer
|
|
| 1460 | - plus :: Integer -> Integer -> Integer
|
|
| 1461 | - (or similar monotype). Indeed, the only way to know whether to apply
|
|
| 1462 | - the monomorphism restriction at all is to instantiate
|
|
| 1463 | - |
|
| 1464 | -There is one place where we don't want to instantiate eagerly,
|
|
| 1465 | -namely in GHC.Tc.Module.tcRnExpr, which implements GHCi's :type
|
|
| 1466 | -command. See Note [Implementing :type] in GHC.Tc.Module.
|
|
| 1467 | - |
|
| 1468 | -This also means that, if DeepSubsumption is enabled, we should also instantiate
|
|
| 1469 | -deeply; we do this by using fillInferResultDS.
|
|
| 1470 | --}
|
|
| 1471 | - |
|
| 1472 | 1484 | ---------------
|
| 1473 | 1485 | tc_sub_type :: (TcType -> TcType -> TcM TcCoercionN) -- How to unify
|
| 1474 | 1486 | -> CtOrigin -- Used when instantiating
|
| ... | ... | @@ -2133,7 +2145,17 @@ deeplySkolemise skol_info ty |
| 2133 | 2145 | = return (idHsWrapper, [], [], substTy subst ty)
|
| 2134 | 2146 | -- substTy is a quick no-op on an empty substitution
|
| 2135 | 2147 | |
| 2148 | +dsInstantiate :: CtOrigin -> TcType -> TcM (HsWrapper, Type)
|
|
| 2149 | +-- Do topInstantiate or deeplyInstantiate, depending on -XDeepSubsumption
|
|
| 2150 | +dsInstantiate orig ty
|
|
| 2151 | + = do { ds_flag <- getDeepSubsumptionFlag
|
|
| 2152 | + ; case ds_flag of
|
|
| 2153 | + Shallow -> topInstantiate orig ty
|
|
| 2154 | + Deep -> deeplyInstantiate orig ty }
|
|
| 2155 | + |
|
| 2136 | 2156 | deeplyInstantiate :: CtOrigin -> TcType -> TcM (HsWrapper, Type)
|
| 2157 | +-- Instantiate invisible foralls, even ones nested
|
|
| 2158 | +-- (to the right) under arrows
|
|
| 2137 | 2159 | deeplyInstantiate orig ty
|
| 2138 | 2160 | = go init_subst ty
|
| 2139 | 2161 | where
|
| ... | ... | @@ -3342,8 +3364,9 @@ mapCheck f xs |
| 3342 | 3364 | -- | Options describing how to deal with a type equality
|
| 3343 | 3365 | -- in the eager unifier. See 'checkTyEqRhs'
|
| 3344 | 3366 | data TyEqFlags m a
|
| 3345 | - -- | LHS is a type family application; we are not unifying.
|
|
| 3346 | - = TEFTyFam
|
|
| 3367 | + = -- | TFTyFam: LHS is a type family application
|
|
| 3368 | + -- Invariant: we are not unifying; see `notUnifying_TEFTask`
|
|
| 3369 | + TEFTyFam
|
|
| 3347 | 3370 | { tefTyFam_occursCheck :: CheckTyEqProblem
|
| 3348 | 3371 | -- ^ The 'CheckTyEqProblem' to report for occurs-check failures
|
| 3349 | 3372 | -- (soluble or insoluble)
|
| ... | ... | @@ -3352,7 +3375,8 @@ data TyEqFlags m a |
| 3352 | 3375 | , tef_fam_app :: TyEqFamApp m a
|
| 3353 | 3376 | -- ^ How to deal with type family applications
|
| 3354 | 3377 | }
|
| 3355 | - -- | LHS is a 'TyVar'.
|
|
| 3378 | + |
|
| 3379 | + -- | TEFTyVar: LHS is a 'TyVar'.
|
|
| 3356 | 3380 | | TEFTyVar
|
| 3357 | 3381 | -- NB: this constructor does not actually store a 'TyVar', in order to
|
| 3358 | 3382 | -- support being called from 'makeTypeConcrete' (which works as if we
|
| 1 | +{-# LANGUAGE DeepSubsumption #-}
|
|
| 2 | + |
|
| 3 | +{-# LANGUAGE DataKinds #-}
|
|
| 4 | +{-# LANGUAGE PatternSynonyms #-}
|
|
| 5 | +{-# LANGUAGE PolyKinds #-}
|
|
| 6 | +{-# LANGUAGE RankNTypes #-}
|
|
| 7 | +{-# LANGUAGE ScopedTypeVariables #-}
|
|
| 8 | +{-# LANGUAGE TypeApplications #-}
|
|
| 9 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 10 | +{-# LANGUAGE ViewPatterns #-}
|
|
| 11 | +{-# LANGUAGE TypeAbstractions #-}
|
|
| 12 | +{-# LANGUAGE StandaloneKindSignatures #-}
|
|
| 13 | + |
|
| 14 | +module T26331 where
|
|
| 15 | + |
|
| 16 | +import Data.Kind (Constraint, Type)
|
|
| 17 | + |
|
| 18 | +type Apply :: (k1 ~> k2) -> k1 -> k2
|
|
| 19 | +type family Apply (f :: k1 ~> k2) (x :: k1) :: k2
|
|
| 20 | + |
|
| 21 | +type (~>) :: Type -> Type -> Type
|
|
| 22 | +type a ~> b = TyFun a b -> Type
|
|
| 23 | +infixr 0 ~>
|
|
| 24 | + |
|
| 25 | +data TyFun :: Type -> Type -> Type
|
|
| 26 | + |
|
| 27 | +type Sing :: k -> Type
|
|
| 28 | +type family Sing @k :: k -> Type
|
|
| 29 | + |
|
| 30 | +type SingFunction2 :: (a1 ~> a2 ~> b) -> Type
|
|
| 31 | +type SingFunction2 (f :: a1 ~> a2 ~> b) =
|
|
| 32 | + forall t1 t2. Sing t1 -> Sing t2 -> Sing (f `Apply` t1 `Apply` t2)
|
|
| 33 | + |
|
| 34 | +unSingFun2 :: forall f. Sing f -> SingFunction2 f
|
|
| 35 | +-- unSingFun2 :: forall f. Sing f -> forall t1 t2. blah
|
|
| 36 | +unSingFun2 sf x = error "urk"
|
|
| 37 | + |
|
| 38 | +singFun2 :: forall f. SingFunction2 f -> Sing f
|
|
| 39 | +singFun2 f = error "urk"
|
|
| 40 | + |
|
| 41 | +-------- This is the tricky bit -------
|
|
| 42 | +pattern SLambda2 :: forall f. SingFunction2 f -> Sing f
|
|
| 43 | +pattern SLambda2 x <- (unSingFun2 -> x) -- We want to push down (SingFunction2 f)
|
|
| 44 | + -- /uninstantiated/ into the pattern `x`
|
|
| 45 | + where
|
|
| 46 | + SLambda2 lam2 = singFun2 lam2
|
|
| 47 | + |
| 1 | +{-# LANGUAGE DeepSubsumption #-}
|
|
| 2 | +{-# LANGUAGE ViewPatterns #-}
|
|
| 3 | +{-# LANGUAGE RankNTypes #-}
|
|
| 4 | + |
|
| 5 | +module T26331a where
|
|
| 6 | + |
|
| 7 | +pair :: forall a. Bool -> a -> forall b. b -> (a,b)
|
|
| 8 | +pair = error "urk"
|
|
| 9 | + |
|
| 10 | +f :: Int -> ((Int,Bool),(Int,Char))
|
|
| 11 | +f (pair True -> x) = (x True, x 'c') -- (x :: forall b. b -> (Int,b)) |
| ... | ... | @@ -85,3 +85,5 @@ test('T21531', [ grep_errmsg(r'INLINE') ], compile, ['-ddump-ds']) |
| 85 | 85 | test('T22521', normal, compile, [''])
|
| 86 | 86 | test('T23038', normal, compile_fail, [''])
|
| 87 | 87 | test('T22328', normal, compile, [''])
|
| 88 | +test('T26331', normal, compile, [''])
|
|
| 89 | +test('T26331a', normal, compile, ['']) |
| 1 | +{-# LANGUAGE GHC2024 #-}
|
|
| 2 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 3 | +{-# LANGUAGE UndecidableInstances #-}
|
|
| 4 | +module T26346 (warble) where
|
|
| 5 | + |
|
| 6 | +import Data.Kind (Type)
|
|
| 7 | +import Data.Type.Equality ((:~:)(..))
|
|
| 8 | + |
|
| 9 | +type Nat :: Type
|
|
| 10 | +data Nat = Z | S Nat
|
|
| 11 | + |
|
| 12 | +type SNat :: Nat -> Type
|
|
| 13 | +data SNat n where
|
|
| 14 | + SZ :: SNat Z
|
|
| 15 | + SS :: SNat n -> SNat (S n)
|
|
| 16 | + |
|
| 17 | +type NatPlus :: Nat -> Nat -> Nat
|
|
| 18 | +type family NatPlus a b where
|
|
| 19 | + NatPlus Z b = b
|
|
| 20 | + NatPlus (S a) b = S (NatPlus a b)
|
|
| 21 | + |
|
| 22 | +sNatPlus ::
|
|
| 23 | + forall (a :: Nat) (b :: Nat).
|
|
| 24 | + SNat a ->
|
|
| 25 | + SNat b ->
|
|
| 26 | + SNat (NatPlus a b)
|
|
| 27 | +sNatPlus SZ b = b
|
|
| 28 | +sNatPlus (SS a) b = SS (sNatPlus a b)
|
|
| 29 | + |
|
| 30 | +data Bin
|
|
| 31 | + = Zero
|
|
| 32 | + | Even Bin
|
|
| 33 | + | Odd Bin
|
|
| 34 | + |
|
| 35 | +type SBin :: Bin -> Type
|
|
| 36 | +data SBin b where
|
|
| 37 | + SZero :: SBin Zero
|
|
| 38 | + SEven :: SBin n -> SBin (Even n)
|
|
| 39 | + SOdd :: SBin n -> SBin (Odd n)
|
|
| 40 | + |
|
| 41 | +type Incr :: Bin -> Bin
|
|
| 42 | +type family Incr b where
|
|
| 43 | + Incr Zero = Odd Zero -- 0 + 1 = (2*0) + 1
|
|
| 44 | + Incr (Even n) = Odd n -- 2n + 1
|
|
| 45 | + Incr (Odd n) = Even (Incr n) -- (2n + 1) + 1 = 2*(n + 1)
|
|
| 46 | + |
|
| 47 | +type BinToNat :: Bin -> Nat
|
|
| 48 | +type family BinToNat b where
|
|
| 49 | + BinToNat Zero = Z
|
|
| 50 | + BinToNat (Even n) = NatPlus (BinToNat n) (BinToNat n)
|
|
| 51 | + BinToNat (Odd n) = S (NatPlus (BinToNat n) (BinToNat n))
|
|
| 52 | + |
|
| 53 | +sBinToNat ::
|
|
| 54 | + forall (b :: Bin).
|
|
| 55 | + SBin b ->
|
|
| 56 | + SNat (BinToNat b)
|
|
| 57 | +sBinToNat SZero = SZ
|
|
| 58 | +sBinToNat (SEven n) = sNatPlus (sBinToNat n) (sBinToNat n)
|
|
| 59 | +sBinToNat (SOdd n) = SS (sNatPlus (sBinToNat n) (sBinToNat n))
|
|
| 60 | + |
|
| 61 | +warble ::
|
|
| 62 | + forall (b :: Bin).
|
|
| 63 | + SBin b ->
|
|
| 64 | + BinToNat (Incr b) :~: S (BinToNat b)
|
|
| 65 | +warble SZero = Refl
|
|
| 66 | +warble (SEven {}) = Refl
|
|
| 67 | +warble (SOdd sb) | Refl <- warble sb
|
|
| 68 | + , Refl <- plusComm sbn (SS sbn)
|
|
| 69 | + = Refl
|
|
| 70 | + where
|
|
| 71 | + sbn = sBinToNat sb
|
|
| 72 | + |
|
| 73 | + plus0R ::
|
|
| 74 | + forall (n :: Nat).
|
|
| 75 | + SNat n ->
|
|
| 76 | + NatPlus n Z :~: n
|
|
| 77 | + plus0R SZ = Refl
|
|
| 78 | + plus0R (SS sn)
|
|
| 79 | + | Refl <- plus0R sn
|
|
| 80 | + = Refl
|
|
| 81 | + |
|
| 82 | + plusSnR ::
|
|
| 83 | + forall (n :: Nat) (m :: Nat).
|
|
| 84 | + SNat n ->
|
|
| 85 | + SNat m ->
|
|
| 86 | + NatPlus n (S m) :~: S (NatPlus n m)
|
|
| 87 | + plusSnR SZ _ = Refl
|
|
| 88 | + plusSnR (SS sn) sm
|
|
| 89 | + | Refl <- plusSnR sn sm
|
|
| 90 | + = Refl
|
|
| 91 | + |
|
| 92 | + plusComm ::
|
|
| 93 | + forall (n :: Nat) (m :: Nat).
|
|
| 94 | + SNat n ->
|
|
| 95 | + SNat m ->
|
|
| 96 | + NatPlus n m :~: NatPlus m n
|
|
| 97 | + plusComm SZ sm
|
|
| 98 | + | Refl <- plus0R sm
|
|
| 99 | + = Refl
|
|
| 100 | + plusComm (SS sn) sm
|
|
| 101 | + | Refl <- plusComm sn sm
|
|
| 102 | + , Refl <- plusSnR sm sn
|
|
| 103 | + = Refl |
| 1 | +{-# LANGUAGE DeepSubsumption #-}
|
|
| 2 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 3 | +{-# LANGUAGE TypeOperators #-}
|
|
| 4 | + |
|
| 5 | +{-# OPTIONS_GHC -dcore-lint #-}
|
|
| 6 | + |
|
| 7 | +module T26350 where
|
|
| 8 | + |
|
| 9 | +import Control.Arrow (first)
|
|
| 10 | + |
|
| 11 | +infix 6 .-.
|
|
| 12 | + |
|
| 13 | +class AffineSpace p where
|
|
| 14 | + type Diff p
|
|
| 15 | + (.-.) :: p -> p -> Diff p
|
|
| 16 | + |
|
| 17 | +affineCombo :: (AffineSpace p, v ~ Diff p) => p -> (p,v) -> (v,v)
|
|
| 18 | +affineCombo z l = first (.-. z) l |
| 1 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 2 | + |
|
| 3 | +module T26358 where
|
|
| 4 | +import Data.Kind
|
|
| 5 | +import Data.Proxy
|
|
| 6 | + |
|
| 7 | +{- Two failing tests, described in GHC.Core.Unify
|
|
| 8 | + Note [Shortcomings of the apartness test]
|
|
| 9 | + |
|
| 10 | +Explanation for TF2
|
|
| 11 | +* We try to reduce
|
|
| 12 | + (TF2 (F (G Float)) (F Int) (G Float))
|
|
| 13 | +* We can only do so if those arguments are apart from the first
|
|
| 14 | + equation of TF2, namely (Bool,Char,Int).
|
|
| 15 | +* So we try to unify
|
|
| 16 | + [F (G Float), F Int, G Float] ~ [Bool, Char, Int]
|
|
| 17 | +* They really are apart, but we can't quite spot that yet;
|
|
| 18 | + hence #26358
|
|
| 19 | + |
|
| 20 | +TF1 is similar.
|
|
| 21 | +-}
|
|
| 22 | + |
|
| 23 | + |
|
| 24 | +type TF1 :: Type -> Type -> Type -> Type
|
|
| 25 | +type family TF1 a b c where
|
|
| 26 | + TF1 Bool Char a = Word
|
|
| 27 | + TF1 a b c = (a,b,c)
|
|
| 28 | + |
|
| 29 | +type F :: Type -> Type
|
|
| 30 | +type family F a where
|
|
| 31 | + |
|
| 32 | +foo :: Proxy a
|
|
| 33 | + -> Proxy (TF1 (F a) (F Int) Int)
|
|
| 34 | + -> Proxy (F a, F Int, Int)
|
|
| 35 | +foo _ px = px
|
|
| 36 | + |
|
| 37 | +type TF2 :: Type -> Type -> Type -> Type
|
|
| 38 | +type family TF2 a b c where
|
|
| 39 | + TF2 Bool Char Int = Word
|
|
| 40 | + TF2 a b c = (a,b,c)
|
|
| 41 | + |
|
| 42 | +type G :: Type -> Type
|
|
| 43 | +type family G a where
|
|
| 44 | + |
|
| 45 | +bar :: Proxy (TF2 (F (G Float)) (F Int) (G Float))
|
|
| 46 | + -> Proxy (F (G Float), F Int, G Float)
|
|
| 47 | +bar px = px
|
|
| 48 | + |
| ... | ... | @@ -862,6 +862,7 @@ test('DeepSubsumption06', normal, compile, ['-XHaskell98']) |
| 862 | 862 | test('DeepSubsumption07', normal, compile, ['-XHaskell2010'])
|
| 863 | 863 | test('DeepSubsumption08', normal, compile, [''])
|
| 864 | 864 | test('DeepSubsumption09', normal, compile, [''])
|
| 865 | +test('T26350', normal, compile, [''])
|
|
| 865 | 866 | test('T26225', normal, compile, [''])
|
| 866 | 867 | test('T26225b', normal, compile, [''])
|
| 867 | 868 | test('T21765', normal, compile, [''])
|
| ... | ... | @@ -945,3 +946,5 @@ test('T25992', normal, compile, ['']) |
| 945 | 946 | test('T14010', normal, compile, [''])
|
| 946 | 947 | test('T26256a', normal, compile, [''])
|
| 947 | 948 | test('T25992a', normal, compile, [''])
|
| 949 | +test('T26346', normal, compile, [''])
|
|
| 950 | +test('T26358', expect_broken(26358), compile, ['']) |