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

Commits:

1 changed file:

Changes:

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -114,10 +114,11 @@ Note [Instantiation variables are short lived]
    114 114
     * Ordinary unification variables always stand for monotypes; only instantiation
    
    115 115
       variables can be unified with a polytype (by `qlUnify`).
    
    116 116
     
    
    117
    -* When we start typechecking the arguments of the call, in tcValArgs, we will
    
    118
    -  (a) monomorphise any un-filled-in instantiation variables
    
    117
    +* Before we start typechecking the arguments of the call, in tcValArgs, we will
    
    118
    +  (a) Monomorphise any un-filled-in instantiation variables
    
    119 119
           (see Note [Monomorphise instantiation variables]),
    
    120
    -  (b) zonk the argument type to reveal any polytypes before typechecking that
    
    120
    +      This happens in Step 4, at the start of `finishApp`.
    
    121
    +  (b) Zonk the argument type to reveal any polytypes before typechecking that
    
    121 122
           argument (see calls to `zonkTcType` and "Crucial step" in tcValArg).
    
    122 123
       See Section 4.3 "Applications and instantiation" of the paper.
    
    123 124
     
    
    ... ... @@ -280,37 +281,27 @@ tcApp works on the application chain by:
    280 281
        Note that we take a quick look at the arguments before quick-looking
    
    281 282
        the result type; see Note [QuickLook: arguments before result]
    
    282 283
     
    
    283
    -Now we split into two cases:
    
    284
    +3. In case DoQL, when we are checking, use quickLookResultType to take a
    
    285
    +   quick look at the result type.
    
    284 286
     
    
    285
    -3. Case NoQL: no Quick Look
    
    287
    +The next steps are in `finishApp`:
    
    286 288
     
    
    287
    -   3.1 Use checkResultTy to connect the the result type.
    
    288
    -       Do this /before/ checking the arguments; see
    
    289
    -       Note [Unify with expected type before typechecking arguments]
    
    289
    +4. In case DoQL:
    
    290
    +   4.1  Monomorphise any as-yet-ununified instantiation variables (`qlMonoInstArgs`)
    
    291
    +        See Note [Monomorphise instantiation variables]
    
    292
    +   4.2  Use `zonkTcType` to expose the polymorphism hidden under instantiation
    
    293
    +        variables in `app_res_rho`, and the monomorphic versions of any
    
    294
    +        un-unified instantiation variables.
    
    290 295
     
    
    291
    -   3.2 Check the arguments with `tcValArgs`.
    
    296
    +5. Use `checkResultTy` to connect the result type.
    
    297
    +   Do this /before/ checking the arguments; see
    
    298
    +   Note [Unify with expected type before typechecking arguments]
    
    292 299
     
    
    293
    -   3.3 Use `finishApp` to wrap up.
    
    300
    +6.  Check the arguments with `tcValArgs`. Importantly, we must remember to zonk in
    
    301
    +    the EValArg case, to expose polymorphism
    
    294 302
     
    
    295
    -4. Case DoQL: use Quick Look
    
    296
    -
    
    297
    -   4.1 Use `quickLookResultType` to take a quick look at the result type,
    
    298
    -       when in checking mode.  This is the shaded part of APP-Downarrow
    
    299
    -       in Fig 4.  It also implements the key part of
    
    300
    -       Note [Unify with expected type before typechecking arguments]
    
    301
    -
    
    302
    -   4.2 Use `zonkTcType` to expose the polymophism hidden under instantiation
    
    303
    -       variables in `app_res_rho`, and the monomorphic versions of any
    
    304
    -       un-unified instantiation variables.
    
    305
    -
    
    306
    -   4.3 Use `checkResultTy` to do the subsumption check as usual
    
    307
    -
    
    308
    -   4.4 Check the arguments with `tcValArgs`. Importantly, this will
    
    309
    -       monomorphise all the instantiation variables of the call.
    
    310
    -       See Note [Monomorphise instantiation variables].
    
    311
    -
    
    312
    -The funcion `finishApp` mainly calls `rebuildHsApps` to rebuild the
    
    313
    -application; but it also has a horrile special case for `tagToEnum`.
    
    303
    +7. The function `finishApp` mainly calls `rebuildHsApps` to rebuild the
    
    304
    +   application; but it also has a horrible special case for `tagToEnum`.
    
    314 305
     
    
    315 306
     Note [Quick Look for particular Ids]
    
    316 307
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -359,7 +350,7 @@ This is a bit sad, as #26599 shows. Consider
    359 350
       bar2 = snd (id woo)
    
    360 351
     
    
    361 352
     For `bar1` we instantiate `snd` at [a:=k1,b:=k2].  Then we QL on the argument `woo`
    
    362
    -(instantiating with `x:=k3`.  Since the result type is guarded we qlUnify
    
    353
    +(instantiating with `x:=k3`).  Since the result type is guarded we qlUnify
    
    363 354
         (k3,forall a.a->a) ~ (k1,k2)
    
    364 355
     and thus succeed in discovering k2:=forall a.a->a, as desired.
    
    365 356
     
    
    ... ... @@ -369,7 +360,7 @@ so we get no information. Adding the `id` call makes it un-typeable. Boo.
    369 360
     
    
    370 361
     This is a bit tricky.
    
    371 362
     * We QuickLook the arguments (tcInstFun)
    
    372
    -     THEN QuickLook the result type (quickLookResultType.
    
    363
    +     THEN QuickLook the result type (quickLookResultType).
    
    373 364
       See this Note
    
    374 365
     * and then...
    
    375 366
     * We typecheck the result type (checkResultTy)
    
    ... ... @@ -413,15 +404,16 @@ tcApp :: HsExpr GhcRn -- ^ The whole application (For error messages)
    413 404
     -- See Note [tcApp: typechecking applications]
    
    414 405
     -- See Note [splitHsApps] in GHC.Tc.Head
    
    415 406
     tcApp rn_expr rn_fun rn_args exp_res_ty
    
    416
    -  = do { fun_lspan <- getFunSrcSpan rn_args
    
    407
    +  = do { do_ql     <- wantQuickLook rn_fun
    
    408
    +       ; fun_lspan <- getFunSrcSpan rn_args
    
    417 409
            ; traceTc "tcApp {" $
    
    418 410
                vcat [ text "rn_fun:" <+> ppr rn_fun
    
    419 411
                     , text "fun_lspan:" <+> ppr fun_lspan
    
    420 412
                     , text "rn_args:" <+> ppr rn_args ]
    
    421 413
     
    
    422
    -       ; let rn_head = (rn_fun, fun_lspan)
    
    423 414
     
    
    424 415
            -- Step 1: Infer the type of `fun`, the head of the application
    
    416
    +       ; let rn_head = (rn_fun, fun_lspan)
    
    425 417
            ; (tc_fun, fun_sigma) <- tcInferAppHead rn_head
    
    426 418
            ; let -- inst_final: top-instantiate the result type of the application,
    
    427 419
                  -- EXCEPT if we are trying to infer a sigma-type
    
    ... ... @@ -433,30 +425,20 @@ tcApp rn_expr rn_fun rn_args exp_res_ty
    433 425
                                       IIF_DeepRho    -> True
    
    434 426
                                       IIF_Sigma      -> False
    
    435 427
     
    
    436
    -       -- Step 2: Instantiate the function type (taking a quick look at args)
    
    437
    -       -- See Note [Quick Look for particular Ids]
    
    438
    -       ; do_ql <- wantQuickLook rn_fun
    
    439
    -
    
    440 428
            ; traceTc "tcApp:inferAppHead" $
    
    441 429
              vcat [ text "tc_fun:" <+> ppr tc_fun
    
    442 430
                   , text "fun_sigma:" <+> ppr fun_sigma
    
    443 431
                   , text "do_ql:" <+> ppr do_ql]
    
    432
    +
    
    433
    +       -- Step 2: Instantiate the function type (taking a quick look at args)
    
    434
    +       -- See Note [Quick Look for particular Ids]
    
    444 435
            ; (inst_args, app_res_rho) <- tcInstFun do_ql inst_final rn_head tc_fun fun_sigma rn_args
    
    445
    -         -- See (TCAPP1) and (TCAPP2) in
    
    446
    -         -- Note [tcApp: typechecking applications]
    
    447
    -
    
    448
    -       ; app_res_rho <- case do_ql of
    
    449
    -            NoQL -> return app_res_rho
    
    450
    -            DoQL -> do { -- Step 5.1: Take a quick look at the result type
    
    451
    -                         -- See Note [QuickLook: arguments before result]
    
    452
    -                         case exp_res_ty of
    
    453
    -                           Check exp_rho -> quickLookResultType app_res_rho exp_rho
    
    454
    -                           Infer {}      -> return ()
    
    455
    -
    
    456
    -                         -- Step 5.3: zonk to expose the polymorphism hidden under
    
    457
    -                         --           QuickLook instantiation variables in `app_res_rho`
    
    458
    -                         --           from either quickLookArg or quickLookResultType
    
    459
    -                       ; liftZonkM $ zonkTcType app_res_rho }
    
    436
    +
    
    437
    +       ; -- Step 3: Take a quick look at the result type
    
    438
    +         -- See Note [QuickLook: arguments before result]
    
    439
    +         case (do_ql, exp_res_ty) of
    
    440
    +            (DoQL, Check exp_rho) -> quickLookResultType app_res_rho exp_rho
    
    441
    +            _                     -> return ()
    
    460 442
     
    
    461 443
            ; finishApp do_ql rn_expr fun_lspan rn_fun tc_fun
    
    462 444
                              inst_args app_res_rho exp_res_ty }
    
    ... ... @@ -469,20 +451,28 @@ finishApp :: QLFlag
    469 451
               -> [HsExprArg 'TcpInst]   -- Arguments
    
    470 452
               -> TcRhoType -> ExpRhoType
    
    471 453
               -> TcM (HsExpr GhcTc)
    
    472
    --- Do final checks and wrap up the result
    
    473
    --- Precondition: app_res_rho has no polymorphism hidden under instantiation variables
    
    454
    +-- Instantiation and Quick Look are done.  Now do typechecking,
    
    455
    +-- of both the result type and the arguments, before rebuilding.
    
    474 456
     finishApp do_ql rn_expr fun_loc rn_fun tc_fun inst_args
    
    475 457
               app_res_rho exp_res_ty
    
    476
    -  = do { -- Step 4.3: subsumption check against the expected type
    
    458
    +  = do { -- Step 4.1: monomorphise un-unified QuickLook instantiation variables
    
    459
    +         -- Step 4.2: zonk app_res_rho to expose the polymorphism hidden under
    
    460
    +         --           instantiation variables in `app_res_rho`
    
    461
    +         app_res_rho <- case do_ql of
    
    462
    +           NoQL -> return app_res_rho
    
    463
    +           DoQL -> liftZonkM $
    
    464
    +                   do { qlMonoInstArgs inst_args
    
    465
    +                      ; zonkTcType app_res_rho }
    
    466
    +
    
    467
    +         -- Step 5: subsumption check against the expected type
    
    477 468
              -- See Note [Unify with expected type before typechecking arguments]
    
    478
    -         res_wrap <- checkResultTy rn_expr (tc_fun, fun_loc) inst_args
    
    469
    +       ; res_wrap <- checkResultTy rn_expr (tc_fun, fun_loc) inst_args
    
    479 470
                                        app_res_rho exp_res_ty
    
    480 471
     
    
    481
    -         -- Step 4.4: Typecheck the arguments, and monomorphise
    
    482
    -         --           any un-unified instantiation variables
    
    472
    +         -- Step 6: Typecheck the arguments
    
    483 473
            ; tc_args <- tcValArgs do_ql (rn_fun, fun_loc) inst_args
    
    484 474
     
    
    485
    -         -- Horrible special case for tagToEnum#.
    
    475
    +         -- Step 7: Horrible special case for tagToEnum#.
    
    486 476
            ; res_expr <- if isTagToEnum tc_fun
    
    487 477
                          then tcTagToEnum tc_fun tc_args app_res_rho
    
    488 478
                          else return (rebuildHsApps tc_fun tc_args)
    
    ... ... @@ -494,8 +484,8 @@ quickLookResultType :: TcRhoType -> TcRhoType -> TcM ()
    494 484
     -- This function implements the shaded bit of rule APP-Downarrow in
    
    495 485
     -- Fig 5 of the QL paper: "A quick look at impredicativity" (ICFP'20).
    
    496 486
     -- See Note [QuickLook: arguments before result]
    
    497
    -quickLookResultType app_rest_rho exp_res_rho
    
    498
    -  = qlUnify app_rest_rho exp_res_rho
    
    487
    +quickLookResultType app_res_rho exp_res_rho
    
    488
    +  = qlUnify app_res_rho exp_res_rho
    
    499 489
     
    
    500 490
     -- | Connect up the inferred type of an application with the expected type.
    
    501 491
     -- This is usually just a unification, but with deep subsumption there is more to do.
    
    ... ... @@ -523,13 +513,10 @@ checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho res_ty
    523 513
     
    
    524 514
     ----------------
    
    525 515
     tcValArgs :: QLFlag
    
    526
    -          -> (HsExpr GhcRn, SrcSpan) -- Head of the application chain (used only for error message generation)
    
    516
    +          -> (HsExpr GhcRn, SrcSpan) -- Head of the application chain
    
    517
    +                                     -- (used only for error message generation)
    
    527 518
               -> [HsExprArg 'TcpInst]
    
    528 519
               -> TcM [HsExprArg 'TcpTc]
    
    529
    --- Importantly, tcValArgs works left-to-right, so that by the time we
    
    530
    --- encounter an argument, we have monomorphised all the instantiation
    
    531
    --- variables that its type contains.  All that is left to do is an ordinary
    
    532
    --- zonkTcType.  See Note [Monomorphise instantiation variables].
    
    533 520
     tcValArgs do_ql rn_head args = go do_ql 0 args
    
    534 521
       where
    
    535 522
         go _ _ [] = return []
    
    ... ... @@ -552,16 +539,15 @@ tcValArg :: QLFlag -- ^ Are we typechecking with Quick Look tur
    552 539
              -> (HsExpr GhcRn, SrcSpan) -- ^ Head of the application chain (used only for error message generation)
    
    553 540
              -> HsExprArg 'TcpInst      -- ^ Actual argument
    
    554 541
              -> TcM (HsExprArg 'TcpTc)  -- ^ Resulting argument
    
    555
    -tcValArg _     _ _ (EPrag l p)         = return (EPrag l (tcExprPrag p))
    
    556
    -tcValArg _     _ _ (ETypeArg l hty ty) = return (ETypeArg l hty ty)
    
    557
    -tcValArg do_ql _ _ (EWrap (EHsWrap w)) = do { whenQL do_ql $ qlMonoHsWrapper w
    
    558
    -                                            ; return (EWrap (EHsWrap w)) }
    
    559
    -  -- qlMonoHsWrapper: see Note [Monomorphise instantiation variables]
    
    560
    -tcValArg _     _ _ (EWrap ew)          = return (EWrap ew)
    
    561
    -
    
    562
    -tcValArg do_ql pos rn_fun_head (EValArg { ea_loc_span  = lspan
    
    563
    -                            , ea_arg    = larg@(L arg_loc arg)
    
    564
    -                            , ea_arg_ty = sc_arg_ty })
    
    542
    +tcValArg _ _ _ (EPrag l p)         = return (EPrag l (tcExprPrag p))
    
    543
    +tcValArg _ _ _ (ETypeArg l hty ty) = return (ETypeArg l hty ty)
    
    544
    +tcValArg _ _ _ (EWrap (EHsWrap w)) = return (EWrap (EHsWrap w))
    
    545
    +tcValArg _ _ _ (EWrap ew)          = return (EWrap ew)
    
    546
    +
    
    547
    +tcValArg do_ql pos rn_fun_head
    
    548
    +         (EValArg { ea_loc_span  = lspan
    
    549
    +                  , ea_arg    = larg@(L arg_loc arg)
    
    550
    +                  , ea_arg_ty = sc_arg_ty })
    
    565 551
       = addArgCtxt pos rn_fun_head larg $
    
    566 552
         do { -- Crucial step: expose QL results before checking exp_arg_ty
    
    567 553
              -- So far as the paper is concerned, this step applies
    
    ... ... @@ -593,17 +579,17 @@ tcValArg do_ql pos rn_fun_head (EValArg { ea_loc_span = lspan
    593 579
                              , ea_arg = L arg_loc arg'
    
    594 580
                              , ea_arg_ty = noExtField }) }
    
    595 581
     
    
    596
    -tcValArg _ pos rn_fun_head (EValArgQL {
    
    597
    -                        eaql_wanted   = wanted
    
    598
    -                      , eaql_loc_span = lspan
    
    599
    -                      , eaql_arg_ty   = sc_arg_ty
    
    600
    -                      , eaql_larg     = larg@(L arg_loc rn_expr)
    
    601
    -                      , eaql_rn_fun   = rn_fun
    
    602
    -                      , eaql_tc_fun   = tc_arg_head@(tc_fun,arg_head_loc)
    
    603
    -                      , eaql_fun_ue   = head_ue
    
    604
    -                      , eaql_args     = inst_args
    
    605
    -                      , eaql_encl     = arg_influences_enclosing_call
    
    606
    -                      , eaql_res_rho  = app_res_rho })
    
    582
    +tcValArg _ pos rn_fun_head
    
    583
    +         (EValArgQL { eaql_wanted   = wanted
    
    584
    +                    , eaql_loc_span = lspan
    
    585
    +                    , eaql_arg_ty   = sc_arg_ty
    
    586
    +                    , eaql_larg     = larg@(L arg_loc rn_expr)
    
    587
    +                    , eaql_rn_fun   = rn_fun
    
    588
    +                    , eaql_tc_fun   = tc_arg_head@(tc_fun,arg_head_loc)
    
    589
    +                    , eaql_fun_ue   = head_ue
    
    590
    +                    , eaql_args     = inst_args
    
    591
    +                    , eaql_encl     = arg_influences_enclosing_call
    
    592
    +                    , eaql_res_rho  = app_res_rho })
    
    607 593
       = addArgCtxt pos rn_fun_head larg $
    
    608 594
         do { -- Expose QL results to tcSkolemise, as in EValArg case
    
    609 595
              Scaled mult exp_arg_ty <- liftZonkM $ zonkScaledTcType sc_arg_ty
    
    ... ... @@ -629,10 +615,9 @@ tcValArg _ pos rn_fun_head (EValArgQL {
    629 615
     
    
    630 616
                         -- Unify with context if we have not already done so
    
    631 617
                         -- See (QLA4) in Note [Quick Look at value arguments]
    
    632
    -                  ; unless arg_influences_enclosing_call $       -- Don't repeat
    
    633
    -                    quickLookResultType app_res_rho exp_arg_rho  -- the qlUnify
    
    618
    +                  ; unless arg_influences_enclosing_call $   -- Don't repeat
    
    619
    +                    qlUnify app_res_rho exp_arg_rho          -- the qlUnify
    
    634 620
     
    
    635
    -                  ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
    
    636 621
                       ; finishApp DoQL rn_expr arg_head_loc rn_fun tc_fun inst_args app_res_rho
    
    637 622
                                   (mkCheckExpType exp_arg_rho) }
    
    638 623
     
    
    ... ... @@ -645,10 +630,6 @@ tcValArg _ pos rn_fun_head (EValArgQL {
    645 630
     
    
    646 631
     
    
    647 632
     --------------------
    
    648
    -whenQL :: QLFlag -> ZonkM () -> TcM ()
    
    649
    -whenQL DoQL thing_inside = liftZonkM thing_inside
    
    650
    -whenQL NoQL _            = return ()
    
    651
    -
    
    652 633
     wantQuickLook :: HsExpr GhcRn -> TcM QLFlag
    
    653 634
     wantQuickLook (HsVar _ (L _ (WithUserRdr _ f)))
    
    654 635
       | getUnique f `elem` quickLookKeys = return DoQL
    
    ... ... @@ -1966,7 +1947,6 @@ quickLookArg1 :: Int
    1966 1947
                   -> Scaled TcRhoType  -- Deeply skolemised
    
    1967 1948
                   -> TcM (HsExprArg 'TcpInst)
    
    1968 1949
     -- quickLookArg1 implements the "QL Argument" judgement in Fig 5 of the paper
    
    1969
    --- NB: orig_arg_rho may not be zonked, but that's ok
    
    1970 1950
     quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    
    1971 1951
       = addArgCtxt pos rn_head larg $ -- Context needed for constraints
    
    1972 1952
                                                -- generated by calls in arg
    
    ... ... @@ -2032,9 +2012,10 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_
    2032 2012
                         -- For (B) see Note [The fiv test in quickLookArg]
    
    2033 2013
     
    
    2034 2014
            -- Step 4: do quick-look unification if either (A) or (B) hold
    
    2015
    +       -- This is part of APP-lightning-bolt (Fig 5 in the paper)
    
    2035 2016
            -- NB: orig_arg_rho may not be zonked, but that's ok
    
    2036 2017
            ; when arg_influences_enclosing_call $
    
    2037
    -         quickLookResultType app_res_rho orig_arg_rho
    
    2018
    +         qlUnify app_res_rho orig_arg_rho
    
    2038 2019
     
    
    2039 2020
            ; traceTc "quickLookArg done }" (ppr rn_fun_arg)
    
    2040 2021
     
    
    ... ... @@ -2062,20 +2043,16 @@ When we are done with Quick Look on a call, we must turn any un-unified
    2062 2043
     lower-case 'theta' (a mono-substitution) in the APP-DOWN rule of Fig 5 of the
    
    2063 2044
     Quick Look paper.
    
    2064 2045
     
    
    2065
    -We so this by look at the arguments, left to right, monomorphising the free
    
    2066
    -instantiation variables of the /type/ arguments of the call.  Those type
    
    2067
    -arguments appear (only) in
    
    2046
    +We do this in `qlMonoInstArgs` by looking at the arguments, monomorphising
    
    2047
    +the free instantiation variables of the /type/ arguments of the call.  Those
    
    2048
    +type arguments appear (only) in
    
    2068 2049
       * the `WpTyApp` components of
    
    2069 2050
       * the `HsWrapper` of
    
    2070 2051
       * a `EWrap` argument
    
    2071
    -See `qlMonoHsWrapper`.
    
    2072
    -
    
    2073
    -By going left to right, we are sure to monomorphise instantiation variables
    
    2074
    -before we encounter them in an argument type (in `tcValArg`).
    
    2075 2052
     
    
    2076 2053
     All instantiation variables for a call will be reachable from the type(s)
    
    2077 2054
     at which the function is instantiated -- i.e. those WpTyApps.  Even instantiation
    
    2078
    -variables allocoated by tcInstFun itself, such as in the IRESULT rule, end up
    
    2055
    +variables allocated by tcInstFun itself, such as in the IRESULT rule, end up
    
    2079 2056
     connected to the original type(s) at which the function is instantiated.
    
    2080 2057
     
    
    2081 2058
     To monomorphise the free QL instantiation variables of a type, we use
    
    ... ... @@ -2096,6 +2073,16 @@ Wrinkles:
    2096 2073
       we want to monomrphise it.  Hence the do_hole in `foldQLInstTyVars`
    
    2097 2074
     -}
    
    2098 2075
     
    
    2076
    +
    
    2077
    +qlMonoInstArgs :: [HsExprArg 'TcpInst] -> ZonkM ()
    
    2078
    +-- See Note [Monomorphise instantiation variables]
    
    2079
    +qlMonoInstArgs args = mapM_ qlMonoInstArg args
    
    2080
    +
    
    2081
    +qlMonoInstArg :: HsExprArg 'TcpInst -> ZonkM ()
    
    2082
    +-- See Note [Monomorphise instantiation variables]
    
    2083
    +qlMonoInstArg (EWrap (EHsWrap w)) = qlMonoHsWrapper w
    
    2084
    +qlMonoInstArg _                   = return ()
    
    2085
    +
    
    2099 2086
     qlMonoHsWrapper :: HsWrapper -> ZonkM ()
    
    2100 2087
     -- See Note [Monomorphise instantiation variables]
    
    2101 2088
     qlMonoHsWrapper (WpCompose w1 w2) = qlMonoHsWrapper w1 >> qlMonoHsWrapper w2