Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
-
eb2ab1e2
by Oleg Grenrus at 2025-08-29T11:00:53-04:00
-
2d575a7f
by fendor at 2025-08-29T11:01:36-04:00
-
450d7226
by Simon Peyton Jones at 2025-08-29T13:06:36-04:00
-
69889431
by sheaf at 2025-08-29T13:06:53-04:00
30 changed files:
- .gitlab-ci.yml
- compiler/GHC/Core/Opt/Monad.hs
- compiler/GHC/Driver/Env.hs
- compiler/GHC/Driver/Env/Types.hs
- compiler/GHC/Driver/Main.hs
- compiler/GHC/Plugins.hs
- compiler/GHC/StgToByteCode.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/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/TcType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/GHC/ThToHs.hs
- configure.ac
- libraries/ghc-boot-th/GHC/Boot/TH/Ppr.hs
- libraries/ghc-internal/src/GHC/Internal/TH/Syntax.hs
- libraries/template-haskell/Language/Haskell/TH/Lib.hs
- libraries/template-haskell/Language/Haskell/TH/Syntax.hs
- libraries/template-haskell/changelog.md
- testsuite/tests/interface-stability/template-haskell-exports.stdout
- + testsuite/tests/patsyn/should_compile/T26331.hs
- + testsuite/tests/patsyn/should_compile/T26331a.hs
- testsuite/tests/patsyn/should_compile/all.T
Changes:
... | ... | @@ -104,6 +104,7 @@ workflow: |
104 | 104 | # which versions of GHC to allow bootstrap with
|
105 | 105 | .bootstrap_matrix : &bootstrap_matrix
|
106 | 106 | matrix:
|
107 | + # If you update this version, be sure to also update 'MinBootGhcVersion' in configure.ac
|
|
107 | 108 | - GHC_VERSION: 9.10.1
|
108 | 109 | DOCKER_IMAGE: "registry.gitlab.haskell.org/ghc/ci-images/x86_64-linux-deb12-ghc9_10:$DOCKER_REV"
|
109 | 110 | - GHC_VERSION: 9.12.2
|
... | ... | @@ -16,7 +16,7 @@ module GHC.Core.Opt.Monad ( |
16 | 16 | mapDynFlagsCoreM, dropSimplCount,
|
17 | 17 | |
18 | 18 | -- ** Reading from the monad
|
19 | - getHscEnv, getModule,
|
|
19 | + getModule,
|
|
20 | 20 | initRuleEnv, getExternalRuleBase,
|
21 | 21 | getDynFlags, getPackageFamInstEnv,
|
22 | 22 | getInteractiveContext,
|
... | ... | @@ -243,8 +243,8 @@ liftIOWithCount what = liftIO what >>= (\(count, x) -> addSimplCount count >> re |
243 | 243 | ************************************************************************
|
244 | 244 | -}
|
245 | 245 | |
246 | -getHscEnv :: CoreM HscEnv
|
|
247 | -getHscEnv = read cr_hsc_env
|
|
246 | +instance HasHscEnv CoreM where
|
|
247 | + getHscEnv = read cr_hsc_env
|
|
248 | 248 | |
249 | 249 | getHomeRuleBase :: CoreM RuleBase
|
250 | 250 | getHomeRuleBase = read cr_rule_base
|
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | module GHC.Driver.Env
|
3 | 3 | ( Hsc(..)
|
4 | 4 | , HscEnv (..)
|
5 | + , HasHscEnv (..)
|
|
5 | 6 | , hsc_mod_graph
|
6 | 7 | , setModuleGraph
|
7 | 8 | , hscUpdateFlags
|
... | ... | @@ -49,7 +50,7 @@ import GHC.Driver.Errors ( printOrThrowDiagnostics ) |
49 | 50 | import GHC.Driver.Errors.Types ( GhcMessage )
|
50 | 51 | import GHC.Driver.Config.Logger (initLogFlags)
|
51 | 52 | import GHC.Driver.Config.Diagnostic (initDiagOpts, initPrintConfig)
|
52 | -import GHC.Driver.Env.Types ( Hsc(..), HscEnv(..) )
|
|
53 | +import GHC.Driver.Env.Types ( Hsc(..), HscEnv(..), HasHscEnv (..) )
|
|
53 | 54 | |
54 | 55 | import GHC.Runtime.Context
|
55 | 56 | import GHC.Runtime.Interpreter.Types (Interp)
|
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | module GHC.Driver.Env.Types
|
4 | 4 | ( Hsc(..)
|
5 | 5 | , HscEnv(..)
|
6 | + , HasHscEnv(..)
|
|
6 | 7 | ) where
|
7 | 8 | |
8 | 9 | import GHC.Driver.Errors.Types ( GhcMessage )
|
... | ... | @@ -34,6 +35,9 @@ newtype Hsc a = Hsc (HscEnv -> Messages GhcMessage -> IO (a, Messages GhcMessage |
34 | 35 | deriving (Functor, Applicative, Monad, MonadIO)
|
35 | 36 | via ReaderT HscEnv (StateT (Messages GhcMessage) IO)
|
36 | 37 | |
38 | +instance HasHscEnv Hsc where
|
|
39 | + getHscEnv = Hsc $ \e w -> return (e, w)
|
|
40 | + |
|
37 | 41 | instance HasDynFlags Hsc where
|
38 | 42 | getDynFlags = Hsc $ \e w -> return (hsc_dflags e, w)
|
39 | 43 | |
... | ... | @@ -109,3 +113,5 @@ data HscEnv |
109 | 113 | -- ^ LLVM configuration cache.
|
110 | 114 | }
|
111 | 115 | |
116 | +class HasHscEnv m where
|
|
117 | + getHscEnv :: m HscEnv |
... | ... | @@ -368,9 +368,6 @@ clearDiagnostics = Hsc $ \_ _ -> return ((), emptyMessages) |
368 | 368 | logDiagnostics :: Messages GhcMessage -> Hsc ()
|
369 | 369 | logDiagnostics w = Hsc $ \_ w0 -> return ((), w0 `unionMessages` w)
|
370 | 370 | |
371 | -getHscEnv :: Hsc HscEnv
|
|
372 | -getHscEnv = Hsc $ \e w -> return (e, w)
|
|
373 | - |
|
374 | 371 | handleWarnings :: Hsc ()
|
375 | 372 | handleWarnings = do
|
376 | 373 | diag_opts <- initDiagOpts <$> getDynFlags
|
... | ... | @@ -143,7 +143,7 @@ import Data.Maybe |
143 | 143 | |
144 | 144 | import GHC.Iface.Env ( lookupNameCache )
|
145 | 145 | import GHC.Prelude
|
146 | -import GHC.Utils.Monad ( mapMaybeM )
|
|
146 | +import GHC.Utils.Monad ( MonadIO, mapMaybeM )
|
|
147 | 147 | import GHC.ThToHs ( thRdrNameGuesses )
|
148 | 148 | import GHC.Tc.Utils.Env ( lookupGlobal )
|
149 | 149 | import GHC.Types.Name.Cache ( NameCache )
|
... | ... | @@ -178,7 +178,22 @@ instance MonadThings CoreM where |
178 | 178 | -- exactly. Qualified or unqualified TH names will be dynamically bound
|
179 | 179 | -- to names in the module being compiled, if possible. Exact TH names
|
180 | 180 | -- will be bound to the name they represent, exactly.
|
181 | -thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
|
|
181 | +--
|
|
182 | +-- 'thNameToGhcName' can be used in 'CoreM', 'Hsc' and 'TcM' monads.
|
|
183 | +--
|
|
184 | +-- 'thNameToGhcName' is recommended way to lookup 'Name's in GHC plugins.
|
|
185 | +--
|
|
186 | +-- @
|
|
187 | +-- {-# LANGUAGE TemplateHaskellQuotes #-}
|
|
188 | +--
|
|
189 | +-- getNames :: Hsc (Maybe Name, Maybe Name)
|
|
190 | +-- getNames = do
|
|
191 | +-- class_Eq <- thNameToGhcName ''Eq
|
|
192 | +-- fun_eq <- thNameToGhcName '(==)
|
|
193 | +-- return (classEq, fun_eq)
|
|
194 | +-- @
|
|
195 | +--
|
|
196 | +thNameToGhcName :: (MonadIO m, HasHscEnv m) => TH.Name -> m (Maybe Name)
|
|
182 | 197 | thNameToGhcName th_name = do
|
183 | 198 | hsc_env <- getHscEnv
|
184 | 199 | liftIO $ thNameToGhcNameIO (hsc_NC hsc_env) th_name
|
... | ... | @@ -2641,8 +2641,8 @@ runBc hsc_env this_mod mbs (BcM m) |
2641 | 2641 | instance HasDynFlags BcM where
|
2642 | 2642 | getDynFlags = hsc_dflags <$> getHscEnv
|
2643 | 2643 | |
2644 | -getHscEnv :: BcM HscEnv
|
|
2645 | -getHscEnv = BcM $ \env st -> return (bcm_hsc_env env, st)
|
|
2644 | +instance HasHscEnv BcM where
|
|
2645 | + getHscEnv = BcM $ \env st -> return (bcm_hsc_env env, st)
|
|
2646 | 2646 | |
2647 | 2647 | getProfile :: BcM Profile
|
2648 | 2648 | getProfile = targetProfile <$> getDynFlags
|
... | ... | @@ -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
|
423 | + |
|
433 | 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 ;
|
... | ... | @@ -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
|
... | ... | @@ -915,12 +915,6 @@ cvtPragmaD (OpaqueP nm) |
915 | 915 | srcTxt = SourceText $ fsLit "{-# OPAQUE"
|
916 | 916 | ; returnJustLA $ Hs.SigD noExtField $ InlineSig noAnn nm' ip }
|
917 | 917 | |
918 | -cvtPragmaD (SpecialiseP nm ty inline phases)
|
|
919 | - = do { nm' <- vNameN nm
|
|
920 | - ; ty' <- cvtSigType ty
|
|
921 | - ; let ip = cvtInlinePhases inline phases
|
|
922 | - ; returnJustLA $ Hs.SigD noExtField $ SpecSig noAnn nm' [ty'] ip }
|
|
923 | - |
|
924 | 918 | cvtPragmaD (SpecialiseInstP ty)
|
925 | 919 | = do { ty' <- cvtSigType ty
|
926 | 920 | ; returnJustLA $ Hs.SigD noExtField $
|
... | ... | @@ -219,7 +219,7 @@ if test "$WithGhc" = "" |
219 | 219 | then
|
220 | 220 | AC_MSG_ERROR([GHC is required.])
|
221 | 221 | fi
|
222 | -MinBootGhcVersion="9.8"
|
|
222 | +MinBootGhcVersion="9.10"
|
|
223 | 223 | FP_COMPARE_VERSIONS([$GhcVersion],[-lt],[$MinBootGhcVersion],
|
224 | 224 | [AC_MSG_ERROR([GHC version $MinBootGhcVersion or later is required to compile GHC.])])
|
225 | 225 |
... | ... | @@ -646,13 +646,6 @@ instance Ppr Pragma where |
646 | 646 | <+> text "#-}"
|
647 | 647 | ppr (OpaqueP n)
|
648 | 648 | = text "{-# OPAQUE" <+> pprName' Applied n <+> text "#-}"
|
649 | - ppr (SpecialiseP n ty inline phases)
|
|
650 | - = text "{-# SPECIALISE"
|
|
651 | - <+> maybe empty ppr inline
|
|
652 | - <+> ppr phases
|
|
653 | - <+> sep [ pprName' Applied n <+> dcolon
|
|
654 | - , nest 2 $ ppr ty ]
|
|
655 | - <+> text "#-}"
|
|
656 | 649 | ppr (SpecialiseEP ty_bndrs tm_bndrs spec_e inline phases)
|
657 | 650 | = sep [ text "{-# SPECIALISE"
|
658 | 651 | <+> maybe empty ppr inline
|
... | ... | @@ -2115,12 +2115,8 @@ data Pragma = InlineP Name Inline RuleMatch Phases |
2115 | 2115 | -- 'Inline' and 'RuleMatch'.
|
2116 | 2116 | | OpaqueP Name
|
2117 | 2117 | -- ^ @{ {\-\# OPAQUE T #-} }@
|
2118 | - | SpecialiseP Name Type (Maybe Inline) Phases
|
|
2119 | - -- ^ @{ {\-\# SPECIALISE [INLINE] [phases] nm :: ty #-} }@
|
|
2120 | - --
|
|
2121 | - -- NB: this constructor is deprecated and will be removed in GHC 9.18
|
|
2122 | 2118 | | SpecialiseEP (Maybe [TyVarBndr ()]) [RuleBndr] Exp (Maybe Inline) Phases
|
2123 | - -- ^ @{ {\-\# SPECIALISE [INLINE] [phases] exp #-} }@
|
|
2119 | + -- ^ @{ {\-\# SPECIALISE [forall t_1 ... t_i]. [forall b_1 ... b_j] [INLINE] [phases] exp #-} }@
|
|
2124 | 2120 | | SpecialiseInstP Type
|
2125 | 2121 | -- ^ @{ {\-\# SPECIALISE instance I #-} }@
|
2126 | 2122 | | RuleP String (Maybe [TyVarBndr ()]) [RuleBndr] Exp Exp Phases
|
... | ... | @@ -443,16 +443,14 @@ varStrictType :: Quote m => Name -> m StrictType -> m VarStrictType |
443 | 443 | varStrictType = varBangType
|
444 | 444 | |
445 | 445 | --------------------------------------------------------------------------------
|
446 | --- * Specialisation pragmas (deprecated)
|
|
446 | +-- * Specialisation pragmas (backwards compatibility)
|
|
447 | 447 | |
448 | -{-# DEPRECATED pragSpecD "Please use 'pragSpecED' instead. 'pragSpecD' will be removed in GHC 9.18." #-}
|
|
449 | 448 | pragSpecD :: Quote m => Name -> m Type -> Phases -> m Dec
|
450 | 449 | pragSpecD n ty phases
|
451 | 450 | = do
|
452 | 451 | ty1 <- ty
|
453 | 452 | pure $ PragmaD $ SpecialiseP n ty1 Nothing phases
|
454 | 453 | |
455 | -{-# DEPRECATED pragSpecInlD "Please use 'pragSpecInlED' instead. 'pragSpecInlD' will be removed in GHC 9.18." #-}
|
|
456 | 454 | pragSpecInlD :: Quote m => Name -> m Type -> Inline -> Phases -> m Dec
|
457 | 455 | pragSpecInlD n ty inline phases
|
458 | 456 | = do
|
1 | 1 | {-# LANGUAGE MagicHash #-}
|
2 | +{-# LANGUAGE PatternSynonyms #-}
|
|
2 | 3 | {-# LANGUAGE RankNTypes #-}
|
3 | 4 | {-# LANGUAGE ScopedTypeVariables #-}
|
4 | 5 | {-# LANGUAGE TemplateHaskellQuotes #-}
|
... | ... | @@ -157,7 +158,7 @@ module Language.Haskell.TH.Syntax ( |
157 | 158 | PatSynType,
|
158 | 159 | Phases (..),
|
159 | 160 | PkgName (..),
|
160 | - Pragma (..),
|
|
161 | + Pragma (SpecialiseP, ..),
|
|
161 | 162 | Quasi (..),
|
162 | 163 | Range (..),
|
163 | 164 | Role (..),
|
... | ... | @@ -457,3 +458,12 @@ reassociate the tree as necessary. |
457 | 458 | 'InfixT', 'PromotedInfixT, 'ParensE', 'ParensP', or 'ParensT' constructors.
|
458 | 459 | |
459 | 460 | -}
|
461 | + |
|
462 | +--------------------------------------------------------------------------------
|
|
463 | +-- Back-compat for Specialise pragmas
|
|
464 | + |
|
465 | +-- | Old-form specialise pragma @{ {\-\# SPECIALISE [INLINE] [phases] (var :: ty) #-} }@.
|
|
466 | +--
|
|
467 | +-- Subsumed by the more general 'SpecialiseEP' constructor.
|
|
468 | +pattern SpecialiseP :: Name -> Type -> (Maybe Inline) -> Phases -> Pragma
|
|
469 | +pattern SpecialiseP nm ty inl phases = SpecialiseEP Nothing [] (SigE (VarE nm) ty) inl phases |
... | ... | @@ -7,16 +7,9 @@ |
7 | 7 | |
8 | 8 | * Introduce `dataToCodeQ` and `liftDataTyped`, typed variants of `dataToExpQ` and `liftData` respectively.
|
9 | 9 | |
10 | - * As part of the implementation of [GHC proposal 493](https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0493-specialise-expressions.rst),
|
|
11 | - the ``SpecialiseP`` constructor of `Pragma`, as well as the helper functions
|
|
12 | - `pragSpecD` and `pragSpecInlD`, have been deprecated.
|
|
13 | - |
|
14 | - They are replaced, respectively, by `SpecialiseEP`, `pragSpecED` and
|
|
15 | - `pragSpecInlED`.
|
|
16 | -
|
|
17 | 10 | * Remove the `Language.Haskell.TH.Lib.Internal` module. This module has long been deprecated, and exposes compiler internals.
|
18 | 11 | Users should use `Language.Haskell.TH.Lib` instead, which exposes a more stable version of this API.
|
19 | -
|
|
12 | + |
|
20 | 13 | * Remove `addrToByteArrayName` and `addrToByteArray` from `Language.Haskell.TH.Syntax`. These were part of the implementation of the `Lift ByteArray` instance and were accidentally exported because this module lacked an explicit export list. They have no usages on Hackage.
|
21 | 14 | |
22 | 15 | ## 2.23.0.0
|
... | ... | @@ -347,7 +347,7 @@ module Language.Haskell.TH where |
347 | 347 | ppr_list :: [a] -> GHC.Boot.TH.PprLib.Doc
|
348 | 348 | {-# MINIMAL ppr #-}
|
349 | 349 | type Pragma :: *
|
350 | - data Pragma = InlineP Name Inline RuleMatch Phases | OpaqueP Name | SpecialiseP Name Type (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseEP (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseInstP Type | RuleP GHC.Internal.Base.String (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp Exp Phases | AnnP AnnTarget Exp | LineP GHC.Internal.Types.Int GHC.Internal.Base.String | CompleteP [Name] (GHC.Internal.Maybe.Maybe Name) | SCCP Name (GHC.Internal.Maybe.Maybe GHC.Internal.Base.String)
|
|
350 | + data Pragma = InlineP Name Inline RuleMatch Phases | OpaqueP Name | SpecialiseEP (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseInstP Type | RuleP GHC.Internal.Base.String (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp Exp Phases | AnnP AnnTarget Exp | LineP GHC.Internal.Types.Int GHC.Internal.Base.String | CompleteP [Name] (GHC.Internal.Maybe.Maybe Name) | SCCP Name (GHC.Internal.Maybe.Maybe GHC.Internal.Base.String)
|
|
351 | 351 | type Pred :: *
|
352 | 352 | type Pred = Type
|
353 | 353 | type PredQ :: *
|
... | ... | @@ -381,6 +381,7 @@ module Language.Haskell.TH where |
381 | 381 | data SourceUnpackedness = NoSourceUnpackedness | SourceNoUnpack | SourceUnpack
|
382 | 382 | type SourceUnpackednessQ :: *
|
383 | 383 | type SourceUnpackednessQ = Q SourceUnpackedness
|
384 | + pattern SpecialiseP :: Name -> Type -> GHC.Internal.Maybe.Maybe Inline -> Phases -> Pragma
|
|
384 | 385 | type Specificity :: *
|
385 | 386 | data Specificity = SpecifiedSpec | InferredSpec
|
386 | 387 | type Stmt :: *
|
... | ... | @@ -1693,7 +1694,7 @@ module Language.Haskell.TH.Syntax where |
1693 | 1694 | type PkgName :: *
|
1694 | 1695 | newtype PkgName = PkgName GHC.Internal.Base.String
|
1695 | 1696 | type Pragma :: *
|
1696 | - data Pragma = InlineP Name Inline RuleMatch Phases | OpaqueP Name | SpecialiseP Name Type (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseEP (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseInstP Type | RuleP GHC.Internal.Base.String (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp Exp Phases | AnnP AnnTarget Exp | LineP GHC.Internal.Types.Int GHC.Internal.Base.String | CompleteP [Name] (GHC.Internal.Maybe.Maybe Name) | SCCP Name (GHC.Internal.Maybe.Maybe GHC.Internal.Base.String)
|
|
1697 | + data Pragma = InlineP Name Inline RuleMatch Phases | OpaqueP Name | SpecialiseEP (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp (GHC.Internal.Maybe.Maybe Inline) Phases | SpecialiseInstP Type | RuleP GHC.Internal.Base.String (GHC.Internal.Maybe.Maybe [TyVarBndr ()]) [RuleBndr] Exp Exp Phases | AnnP AnnTarget Exp | LineP GHC.Internal.Types.Int GHC.Internal.Base.String | CompleteP [Name] (GHC.Internal.Maybe.Maybe Name) | SCCP Name (GHC.Internal.Maybe.Maybe GHC.Internal.Base.String)
|
|
1697 | 1698 | type Pred :: *
|
1698 | 1699 | type Pred = Type
|
1699 | 1700 | type role Q nominal
|
... | ... | @@ -1748,6 +1749,7 @@ module Language.Haskell.TH.Syntax where |
1748 | 1749 | data SourceStrictness = NoSourceStrictness | SourceLazy | SourceStrict
|
1749 | 1750 | type SourceUnpackedness :: *
|
1750 | 1751 | data SourceUnpackedness = NoSourceUnpackedness | SourceNoUnpack | SourceUnpack
|
1752 | + pattern SpecialiseP :: Name -> Type -> GHC.Internal.Maybe.Maybe Inline -> Phases -> Pragma
|
|
1751 | 1753 | type Specificity :: *
|
1752 | 1754 | data Specificity = SpecifiedSpec | InferredSpec
|
1753 | 1755 | type Stmt :: *
|
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, ['']) |