| ... |
... |
@@ -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
|
|
| ... |
... |
@@ -277,39 +278,30 @@ tcApp works on the application chain by: |
|
277
|
278
|
variables", written using \kappa in the paper.
|
|
278
|
279
|
See Note [Instantiating type variables in QuickLook]
|
|
279
|
280
|
|
|
280
|
|
-Now we split into two cases:
|
|
|
281
|
+ Note that we take a quick look at the arguments before quick-looking
|
|
|
282
|
+ the result type; see Note [QuickLook: arguments before result]
|
|
281
|
283
|
|
|
282
|
|
-3. Case NoQL: no Quick Look
|
|
|
284
|
+3. In case DoQL, when we are checking, use quickLookResultType to take a
|
|
|
285
|
+ quick look at the result type.
|
|
283
|
286
|
|
|
284
|
|
- 3.1 Use checkResultTy to connect the the result type.
|
|
285
|
|
- Do this /before/ checking the arguments; see
|
|
286
|
|
- Note [Unify with expected type before typechecking arguments]
|
|
|
287
|
+The next steps are in `finishApp`:
|
|
287
|
288
|
|
|
288
|
|
- 3.2 Check the arguments with `tcValArgs`.
|
|
|
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.
|
|
289
|
295
|
|
|
290
|
|
- 3.3 Use `finishApp` to wrap up.
|
|
|
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]
|
|
291
|
299
|
|
|
292
|
|
-4. Case DoQL: use Quick Look
|
|
|
300
|
+6. Check the arguments with `tcValArgs`. Importantly, we must remember to zonk in
|
|
|
301
|
+ the EValArg case, to expose polymorphism
|
|
293
|
302
|
|
|
294
|
|
- 4.1 Use `quickLookResultType` to take a quick look at the result type,
|
|
295
|
|
- when in checking mode. This is the shaded part of APP-Downarrow
|
|
296
|
|
- in Fig 4. It also implements the key part of
|
|
297
|
|
- Note [Unify with expected type before typechecking arguments]
|
|
298
|
|
-
|
|
299
|
|
- 4.2 Check the arguments with `tcValArgs`. Importantly, this will
|
|
300
|
|
- monomorphise all the instantiation variables of the call.
|
|
301
|
|
- See Note [Monomorphise instantiation variables].
|
|
302
|
|
-
|
|
303
|
|
- 4.3 Use `zonkTcType` to expose the polymophism hidden under instantiation
|
|
304
|
|
- variables in `app_res_rho`, and the monomorphic versions of any
|
|
305
|
|
- un-unified instantiation variables.
|
|
306
|
|
-
|
|
307
|
|
- 4.4 Use `checkResTy` to do the subsumption check as usual
|
|
308
|
|
-
|
|
309
|
|
- 4.5 Use `finishApp` to wrap up
|
|
310
|
|
-
|
|
311
|
|
-The funcion `finishApp` mainly calls `rebuildHsApps` to rebuild the
|
|
312
|
|
-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`.
|
|
313
|
305
|
|
|
314
|
306
|
Note [Quick Look for particular Ids]
|
|
315
|
307
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -328,6 +320,53 @@ particular Ids: |
|
328
|
320
|
so that (f `op`), say, will work OK even if `f` is higher rank.
|
|
329
|
321
|
See Note [Left and right sections] in GHC.Rename.Expr.
|
|
330
|
322
|
|
|
|
323
|
+Note [QuickLook: arguments before result]
|
|
|
324
|
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
325
|
+Generally we try to push the expected result type into the call;
|
|
|
326
|
+see Note [Unify with expected type before typechecking arguments].
|
|
|
327
|
+But surprisingly we can't do that for QuickLook. (#26599 was a failed
|
|
|
328
|
+attempt to suggest this.) Consider
|
|
|
329
|
+ h :: (Int->Int) -> Int ids :: [forall a. a->a]
|
|
|
330
|
+and the call
|
|
|
331
|
+ h (head ids)
|
|
|
332
|
+Let's say we instantiate `head :: [a] -> a` with `a:=kappa`.
|
|
|
333
|
+We QuickLook at the argument with expected type (Int->Int). We must NOT
|
|
|
334
|
+unify kappa:=(Int->Int), because kappa:=forall a. a->a is the only
|
|
|
335
|
+solution, and one that we will find if we look at the argument first.
|
|
|
336
|
+
|
|
|
337
|
+TL;DR: we must do `quickLookArg` on all arguments before doing `quickLookResultType`
|
|
|
338
|
+on the result type.
|
|
|
339
|
+
|
|
|
340
|
+Remember: `quickLookResultType` is really anticipating a /subsumption/ test, not
|
|
|
341
|
+an equality test. Doing `qlUnify` on the result type (always a rho-type) is
|
|
|
342
|
+in general wrong if there is an un-guarded kappa in the result type. It's wrong
|
|
|
343
|
+UNLESS we have already gotten all the info about `kappa` (from the
|
|
|
344
|
+arguments) that we are going to get. That's why we QuickLook the arguments first.
|
|
|
345
|
+
|
|
|
346
|
+This is a bit sad, as #26599 shows. Consider
|
|
|
347
|
+
|
|
|
348
|
+ woo :: (x, [forall a. a->a])
|
|
|
349
|
+ bar1 = snd woo
|
|
|
350
|
+ bar2 = snd (id woo)
|
|
|
351
|
+
|
|
|
352
|
+For `bar1` we instantiate `snd` at [a:=k1,b:=k2]. Then we QL on the argument `woo`
|
|
|
353
|
+(instantiating with `x:=k3`). Since the result type is guarded we qlUnify
|
|
|
354
|
+ (k3,forall a.a->a) ~ (k1,k2)
|
|
|
355
|
+and thus succeed in discovering k2:=forall a.a->a, as desired.
|
|
|
356
|
+
|
|
|
357
|
+But for `bar2` we fail. When quick-looking (id woo) we instantiate `id` with [a:=k],
|
|
|
358
|
+and quick-look the argument `woo` with expected type `k`. That's not guarded,
|
|
|
359
|
+so we get no information. Adding the `id` call makes it un-typeable. Boo.
|
|
|
360
|
+
|
|
|
361
|
+This is a bit tricky.
|
|
|
362
|
+* We QuickLook the arguments (tcInstFun)
|
|
|
363
|
+ THEN QuickLook the result type (quickLookResultType).
|
|
|
364
|
+ See this Note
|
|
|
365
|
+* and then...
|
|
|
366
|
+* We typecheck the result type (checkResultTy)
|
|
|
367
|
+ THEN typecheck the arguments (tcValArgs)
|
|
|
368
|
+ see Note [Unify with expected type before typechecking arguments]
|
|
|
369
|
+
|
|
331
|
370
|
Note [Unify with expected type before typechecking arguments]
|
|
332
|
371
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
333
|
372
|
Consider this (#19364)
|
| ... |
... |
@@ -353,7 +392,7 @@ Unify result type /before/ typechecking the args |
|
353
|
392
|
Actual: String
|
|
354
|
393
|
• In the first argument of ‘Pair’, namely ‘"yes"’
|
|
355
|
394
|
|
|
356
|
|
-The latter is much better. That is why we call `checkResultTy` before tcValArgs.
|
|
|
395
|
+The latter is much better. That is why we call `checkResultTy` before `tcValArgs`.
|
|
357
|
396
|
-}
|
|
358
|
397
|
|
|
359
|
398
|
--------------------
|
| ... |
... |
@@ -365,18 +404,18 @@ tcApp :: HsExpr GhcRn -- ^ The whole application (For error messages) |
|
365
|
404
|
-- See Note [tcApp: typechecking applications]
|
|
366
|
405
|
-- See Note [splitHsApps] in GHC.Tc.Head
|
|
367
|
406
|
tcApp rn_expr rn_fun rn_args exp_res_ty
|
|
368
|
|
- = do { fun_lspan <- getFunSrcSpan rn_args
|
|
|
407
|
+ = do { do_ql <- wantQuickLook rn_fun
|
|
|
408
|
+ ; fun_lspan <- getFunSrcSpan rn_args
|
|
369
|
409
|
; traceTc "tcApp {" $
|
|
370
|
410
|
vcat [ text "rn_fun:" <+> ppr rn_fun
|
|
371
|
411
|
, text "fun_lspan:" <+> ppr fun_lspan
|
|
372
|
412
|
, text "rn_args:" <+> ppr rn_args ]
|
|
373
|
413
|
|
|
374
|
|
- ; let rn_head = (rn_fun, fun_lspan)
|
|
375
|
414
|
|
|
376
|
415
|
-- Step 1: Infer the type of `fun`, the head of the application
|
|
|
416
|
+ ; let rn_head = (rn_fun, fun_lspan)
|
|
377
|
417
|
; (tc_fun, fun_sigma) <- tcInferAppHead rn_head
|
|
378
|
|
- ; let tc_head = (tc_fun, fun_lspan)
|
|
379
|
|
- -- inst_final: top-instantiate the result type of the application,
|
|
|
418
|
+ ; let -- inst_final: top-instantiate the result type of the application,
|
|
380
|
419
|
-- EXCEPT if we are trying to infer a sigma-type
|
|
381
|
420
|
inst_final = case exp_res_ty of
|
|
382
|
421
|
Check {} -> True
|
| ... |
... |
@@ -386,75 +425,73 @@ tcApp rn_expr rn_fun rn_args exp_res_ty |
|
386
|
425
|
IIF_DeepRho -> True
|
|
387
|
426
|
IIF_Sigma -> False
|
|
388
|
427
|
|
|
389
|
|
- -- Step 2: Instantiate the function type (taking a quick look at args)
|
|
390
|
|
- -- See Note [Quick Look for particular Ids]
|
|
391
|
|
- ; do_ql <- wantQuickLook rn_fun
|
|
392
|
|
-
|
|
393
|
428
|
; traceTc "tcApp:inferAppHead" $
|
|
394
|
429
|
vcat [ text "tc_fun:" <+> ppr tc_fun
|
|
395
|
430
|
, text "fun_sigma:" <+> ppr fun_sigma
|
|
396
|
431
|
, text "do_ql:" <+> ppr do_ql]
|
|
397
|
|
- ; (inst_args, app_res_rho)
|
|
398
|
|
- <- tcInstFun do_ql inst_final rn_head tc_fun fun_sigma rn_args
|
|
399
|
|
- -- See (TCAPP1) and (TCAPP2) in
|
|
400
|
|
- -- Note [tcApp: typechecking applications]
|
|
401
|
|
-
|
|
402
|
|
- ; case do_ql of
|
|
403
|
|
- NoQL -> do { traceTc "tcApp:NoQL" (ppr rn_fun $$ ppr app_res_rho)
|
|
404
|
|
-
|
|
405
|
|
- -- Step 3.1: subsumption check against expected result type
|
|
406
|
|
- -- See Note [Unify with expected type before typechecking arguments]
|
|
407
|
|
- ; res_wrap <- checkResultTy rn_expr tc_head inst_args
|
|
408
|
|
- app_res_rho exp_res_ty
|
|
409
|
|
- -- Step 3.2: typecheck the arguments
|
|
410
|
|
- ; tc_args <- tcValArgs NoQL rn_head inst_args
|
|
411
|
|
-
|
|
412
|
|
- -- Step 3.3: wrap up
|
|
413
|
|
- ; finishApp tc_fun tc_args app_res_rho res_wrap }
|
|
414
|
|
-
|
|
415
|
|
- DoQL -> do { traceTc "tcApp:DoQL" (ppr rn_fun $$ ppr app_res_rho)
|
|
416
|
|
-
|
|
417
|
|
- -- Step 4.1: Take a quick look at the result type
|
|
418
|
|
- ; quickLookResultType app_res_rho exp_res_ty
|
|
419
|
|
-
|
|
420
|
|
- -- Step 4.2: typecheck the arguments, and monomorphise
|
|
421
|
|
- -- any un-unified instantiation variables
|
|
422
|
|
- ; tc_args <- tcValArgs DoQL rn_head inst_args
|
|
423
|
|
-
|
|
424
|
|
- -- Step 4.3: zonk to expose the polymorphism hidden under
|
|
425
|
|
- -- QuickLook instantiation variables in `app_res_rho`
|
|
426
|
|
- ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
|
|
427
|
|
-
|
|
428
|
|
- -- Step 4.4: subsumption check against the expected type
|
|
429
|
|
- ; res_wrap <- checkResultTy rn_expr tc_head inst_args
|
|
430
|
|
- app_res_rho exp_res_ty
|
|
431
|
|
- -- Step 4.5: wrap up
|
|
432
|
|
- ; finishApp tc_fun tc_args app_res_rho res_wrap } }
|
|
433
|
|
-
|
|
434
|
|
-quickLookResultType :: TcRhoType -> ExpRhoType -> TcM ()
|
|
435
|
|
--- This function implements the shaded bit of rule APP-Downarrow in
|
|
436
|
|
--- Fig 5 of the QL paper: "A quick look at impredicativity" (ICFP'20).
|
|
437
|
|
-quickLookResultType app_res_rho (Check exp_rho) = qlUnify app_res_rho exp_rho
|
|
438
|
|
-quickLookResultType _ _ = return ()
|
|
439
|
432
|
|
|
440
|
|
-finishApp :: HsExpr GhcTc -> [HsExprArg 'TcpTc]
|
|
441
|
|
- -> TcRhoType -> HsWrapper
|
|
|
433
|
+ -- Step 2: Instantiate the function type (taking a quick look at args)
|
|
|
434
|
+ -- See Note [Quick Look for particular Ids]
|
|
|
435
|
+ ; (inst_args, app_res_rho) <- tcInstFun do_ql inst_final rn_head tc_fun fun_sigma rn_args
|
|
|
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 ()
|
|
|
442
|
+
|
|
|
443
|
+ ; finishApp do_ql rn_expr fun_lspan rn_fun tc_fun
|
|
|
444
|
+ inst_args app_res_rho exp_res_ty }
|
|
|
445
|
+
|
|
|
446
|
+finishApp :: QLFlag
|
|
|
447
|
+ -> HsExpr GhcRn -- The whole application
|
|
|
448
|
+ -> SrcSpan -- Span for the /head/ of the application
|
|
|
449
|
+ -> HsExpr GhcRn -- The head of the application (renamed)
|
|
|
450
|
+ -> HsExpr GhcTc -- The head of the application (typechecked)
|
|
|
451
|
+ -> [HsExprArg 'TcpInst] -- Arguments
|
|
|
452
|
+ -> TcRhoType -> ExpRhoType
|
|
442
|
453
|
-> TcM (HsExpr GhcTc)
|
|
443
|
|
--- Do final checks and wrap up the result
|
|
444
|
|
-finishApp tc_fun tc_args app_res_rho res_wrap
|
|
445
|
|
- = do {
|
|
446
|
|
- -- Reconstruct, with a horrible special case for tagToEnum#.
|
|
447
|
|
- res_expr <- if isTagToEnum tc_fun
|
|
|
454
|
+-- Instantiation and Quick Look are done. Now do typechecking,
|
|
|
455
|
+-- of both the result type and the arguments, before rebuilding.
|
|
|
456
|
+finishApp do_ql rn_expr fun_loc rn_fun tc_fun inst_args
|
|
|
457
|
+ app_res_rho exp_res_ty
|
|
|
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
|
|
|
468
|
+ -- See Note [Unify with expected type before typechecking arguments]
|
|
|
469
|
+ ; res_wrap <- checkResultTy rn_expr (tc_fun, fun_loc) inst_args
|
|
|
470
|
+ app_res_rho exp_res_ty
|
|
|
471
|
+
|
|
|
472
|
+ -- Step 6: Typecheck the arguments
|
|
|
473
|
+ ; tc_args <- tcValArgs do_ql (rn_fun, fun_loc) inst_args
|
|
|
474
|
+
|
|
|
475
|
+ -- Step 7: Horrible special case for tagToEnum#.
|
|
|
476
|
+ ; res_expr <- if isTagToEnum tc_fun
|
|
448
|
477
|
then tcTagToEnum tc_fun tc_args app_res_rho
|
|
449
|
478
|
else return (rebuildHsApps tc_fun tc_args)
|
|
|
479
|
+
|
|
450
|
480
|
; traceTc "End tcApp }" (ppr tc_fun)
|
|
451
|
481
|
; return (mkHsWrap res_wrap res_expr) }
|
|
452
|
482
|
|
|
|
483
|
+quickLookResultType :: TcRhoType -> TcRhoType -> TcM ()
|
|
|
484
|
+-- This function implements the shaded bit of rule APP-Downarrow in
|
|
|
485
|
+-- Fig 5 of the QL paper: "A quick look at impredicativity" (ICFP'20).
|
|
|
486
|
+-- See Note [QuickLook: arguments before result]
|
|
|
487
|
+quickLookResultType app_res_rho exp_res_rho
|
|
|
488
|
+ = qlUnify app_res_rho exp_res_rho
|
|
|
489
|
+
|
|
453
|
490
|
-- | Connect up the inferred type of an application with the expected type.
|
|
454
|
491
|
-- This is usually just a unification, but with deep subsumption there is more to do.
|
|
455
|
492
|
checkResultTy :: HsExpr GhcRn
|
|
456
|
493
|
-> (HsExpr GhcTc, SrcSpan) -- Head
|
|
457
|
|
- -> [HsExprArg p] -- Arguments, just error messages
|
|
|
494
|
+ -> [HsExprArg p] -- Arguments, just for error messages
|
|
458
|
495
|
-> TcRhoType -- Inferred type of the application; zonked to
|
|
459
|
496
|
-- expose foralls, but maybe not /deeply/ instantiated
|
|
460
|
497
|
-> ExpRhoType -- Expected type; this is deeply skolemised
|
| ... |
... |
@@ -476,13 +513,10 @@ checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho res_ty |
|
476
|
513
|
|
|
477
|
514
|
----------------
|
|
478
|
515
|
tcValArgs :: QLFlag
|
|
479
|
|
- -> (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)
|
|
480
|
518
|
-> [HsExprArg 'TcpInst]
|
|
481
|
519
|
-> TcM [HsExprArg 'TcpTc]
|
|
482
|
|
--- Importantly, tcValArgs works left-to-right, so that by the time we
|
|
483
|
|
--- encounter an argument, we have monomorphised all the instantiation
|
|
484
|
|
--- variables that its type contains. All that is left to do is an ordinary
|
|
485
|
|
--- zonkTcType. See Note [Monomorphise instantiation variables].
|
|
486
|
520
|
tcValArgs do_ql rn_head args = go do_ql 0 args
|
|
487
|
521
|
where
|
|
488
|
522
|
go _ _ [] = return []
|
| ... |
... |
@@ -505,16 +539,15 @@ tcValArg :: QLFlag -- ^ Are we typechecking with Quick Look tur |
|
505
|
539
|
-> (HsExpr GhcRn, SrcSpan) -- ^ Head of the application chain (used only for error message generation)
|
|
506
|
540
|
-> HsExprArg 'TcpInst -- ^ Actual argument
|
|
507
|
541
|
-> TcM (HsExprArg 'TcpTc) -- ^ Resulting argument
|
|
508
|
|
-tcValArg _ _ _ (EPrag l p) = return (EPrag l (tcExprPrag p))
|
|
509
|
|
-tcValArg _ _ _ (ETypeArg l hty ty) = return (ETypeArg l hty ty)
|
|
510
|
|
-tcValArg do_ql _ _ (EWrap (EHsWrap w)) = do { whenQL do_ql $ qlMonoHsWrapper w
|
|
511
|
|
- ; return (EWrap (EHsWrap w)) }
|
|
512
|
|
- -- qlMonoHsWrapper: see Note [Monomorphise instantiation variables]
|
|
513
|
|
-tcValArg _ _ _ (EWrap ew) = return (EWrap ew)
|
|
514
|
|
-
|
|
515
|
|
-tcValArg do_ql pos rn_fun_head (EValArg { ea_loc_span = lspan
|
|
516
|
|
- , ea_arg = larg@(L arg_loc arg)
|
|
517
|
|
- , 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 })
|
|
518
|
551
|
= addArgCtxt pos rn_fun_head larg $
|
|
519
|
552
|
do { -- Crucial step: expose QL results before checking exp_arg_ty
|
|
520
|
553
|
-- So far as the paper is concerned, this step applies
|
| ... |
... |
@@ -546,17 +579,17 @@ tcValArg do_ql pos rn_fun_head (EValArg { ea_loc_span = lspan |
|
546
|
579
|
, ea_arg = L arg_loc arg'
|
|
547
|
580
|
, ea_arg_ty = noExtField }) }
|
|
548
|
581
|
|
|
549
|
|
-tcValArg _ pos rn_fun_head (EValArgQL {
|
|
550
|
|
- eaql_wanted = wanted
|
|
551
|
|
- , eaql_loc_span = lspan
|
|
552
|
|
- , eaql_arg_ty = sc_arg_ty
|
|
553
|
|
- , eaql_larg = larg@(L arg_loc rn_expr)
|
|
554
|
|
- , eaql_tc_fun = tc_arg_head@(tc_fun,arg_head_loc)
|
|
555
|
|
- , eaql_rn_fun = rn_arg_head
|
|
556
|
|
- , eaql_fun_ue = head_ue
|
|
557
|
|
- , eaql_args = inst_args
|
|
558
|
|
- , eaql_encl = arg_influences_enclosing_call
|
|
559
|
|
- , 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 })
|
|
560
|
593
|
= addArgCtxt pos rn_fun_head larg $
|
|
561
|
594
|
do { -- Expose QL results to tcSkolemise, as in EValArg case
|
|
562
|
595
|
Scaled mult exp_arg_ty <- liftZonkM $ zonkScaledTcType sc_arg_ty
|
| ... |
... |
@@ -582,14 +615,11 @@ tcValArg _ pos rn_fun_head (EValArgQL { |
|
582
|
615
|
|
|
583
|
616
|
-- Unify with context if we have not already done so
|
|
584
|
617
|
-- See (QLA4) in Note [Quick Look at value arguments]
|
|
585
|
|
- ; unless arg_influences_enclosing_call $ -- Don't repeat
|
|
586
|
|
- qlUnify 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
|
|
587
|
620
|
|
|
588
|
|
- ; tc_args <- tcValArgs DoQL (rn_arg_head, arg_head_loc) inst_args
|
|
589
|
|
- ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
|
|
590
|
|
- ; res_wrap <- checkResultTy rn_expr tc_arg_head inst_args
|
|
591
|
|
- app_res_rho (mkCheckExpType exp_arg_rho)
|
|
592
|
|
- ; finishApp tc_fun tc_args app_res_rho res_wrap }
|
|
|
621
|
+ ; finishApp DoQL rn_expr arg_head_loc rn_fun tc_fun inst_args app_res_rho
|
|
|
622
|
+ (mkCheckExpType exp_arg_rho) }
|
|
593
|
623
|
|
|
594
|
624
|
; traceTc "tcEValArgQL }" $
|
|
595
|
625
|
vcat [ text "app_res_rho:" <+> ppr app_res_rho ]
|
| ... |
... |
@@ -606,6 +636,13 @@ wantQuickLook (HsVar _ (L _ (WithUserRdr _ f))) |
|
606
|
636
|
wantQuickLook _ = do { impred <- xoptM LangExt.ImpredicativeTypes
|
|
607
|
637
|
; if impred then return DoQL else return NoQL }
|
|
608
|
638
|
|
|
|
639
|
+skipQuickLook :: HsExprLoc -> LHsExpr GhcRn -> Scaled TcRhoType
|
|
|
640
|
+ -> TcM (HsExprArg 'TcpInst)
|
|
|
641
|
+skipQuickLook loc larg arg_ty
|
|
|
642
|
+ = return (EValArg { ea_loc_span = loc
|
|
|
643
|
+ , ea_arg = larg
|
|
|
644
|
+ , ea_arg_ty = arg_ty })
|
|
|
645
|
+
|
|
609
|
646
|
quickLookKeys :: [Unique]
|
|
610
|
647
|
-- See Note [Quick Look for particular Ids]
|
|
611
|
648
|
quickLookKeys = [dollarIdKey, leftSectionKey, rightSectionKey]
|
| ... |
... |
@@ -1860,17 +1897,6 @@ quickLookArg DoQL pos app_lspan fun_and_lspan larg orig_arg_ty |
|
1860
|
1897
|
then skipQuickLook app_lspan larg orig_arg_ty
|
|
1861
|
1898
|
else quickLookArg1 pos app_lspan fun_and_lspan larg orig_arg_ty }
|
|
1862
|
1899
|
|
|
1863
|
|
-skipQuickLook :: HsExprLoc -> LHsExpr GhcRn -> Scaled TcRhoType
|
|
1864
|
|
- -> TcM (HsExprArg 'TcpInst)
|
|
1865
|
|
-skipQuickLook app_lspan larg arg_ty
|
|
1866
|
|
- = return (EValArg { ea_loc_span = app_lspan
|
|
1867
|
|
- , ea_arg = larg
|
|
1868
|
|
- , ea_arg_ty = arg_ty })
|
|
1869
|
|
-
|
|
1870
|
|
-whenQL :: QLFlag -> ZonkM () -> TcM ()
|
|
1871
|
|
-whenQL DoQL thing_inside = liftZonkM thing_inside
|
|
1872
|
|
-whenQL NoQL _ = return ()
|
|
1873
|
|
-
|
|
1874
|
1900
|
qlArgHasRhoType :: TcType -> TcM Bool
|
|
1875
|
1901
|
-- `qlArgHasRhoType` checks that the expected argument type in rule
|
|
1876
|
1902
|
-- App-lightning-bolt (Fig 5 in the paper) is indeed a rho-type.
|
| ... |
... |
@@ -1959,7 +1985,7 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_ |
|
1959
|
1985
|
<- captureConstraints $
|
|
1960
|
1986
|
tcInstFun do_ql True (rn_fun_arg, fun_lspan_arg) tc_fun_arg_head fun_sigma_arg_head rn_args
|
|
1961
|
1987
|
-- We must capture type-class and equality constraints here, but
|
|
1962
|
|
- -- not usage information. See (QLA6) in Note [Quick Look at
|
|
|
1988
|
+ -- not usage constraints. See (QLA6) in Note [Quick Look at
|
|
1963
|
1989
|
-- value arguments]
|
|
1964
|
1990
|
|
|
1965
|
1991
|
; traceTc "quickLookArg 2" $
|
| ... |
... |
@@ -1969,13 +1995,16 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_ |
|
1969
|
1995
|
|
|
1970
|
1996
|
-- Step 3: Check the two other premises of APP-lightning-bolt (Fig 5 in the paper)
|
|
1971
|
1997
|
-- Namely: (A) is orig_arg_rho is guarded
|
|
1972
|
|
- -- or: (B) fiv(app_res_rho) = emptyset
|
|
|
1998
|
+ -- or: (B) fiv(app_res_rho) = emptyset
|
|
|
1999
|
+ -- then Step 4: do quick-look unification if either (A) or (B) hold
|
|
|
2000
|
+ --
|
|
|
2001
|
+ -- For (B) see Note [The fiv test in quickLookArg]
|
|
1973
|
2002
|
-- This tells us if the quick look at the argument yields information that
|
|
1974
|
2003
|
-- influences the enclosing function call
|
|
1975
|
|
- -- NB: guardedness is computed based on the original,
|
|
1976
|
|
- -- unzonked orig_arg_rho, so that we deliberately do
|
|
1977
|
|
- -- not exploit guardedness that emerges a result of QL on earlier args
|
|
1978
|
|
- -- We must do the anyFreeKappa test /after/ tcInstFun; see (QLA3).
|
|
|
2004
|
+ -- NB: `isGuardedTy` is computed based on the original,
|
|
|
2005
|
+ -- unzonked orig_arg_rho, so that we deliberately do not exploit
|
|
|
2006
|
+ -- guardedness that emerges a result of QL on earlier args
|
|
|
2007
|
+ -- In contrast, we must do the `anyFreeKappa` test /after/ tcInstFun; see (QLA3).
|
|
1979
|
2008
|
; arg_influences_enclosing_call
|
|
1980
|
2009
|
<- if isGuardedTy orig_arg_rho
|
|
1981
|
2010
|
then return True
|
| ... |
... |
@@ -1983,6 +2012,7 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_ |
|
1983
|
2012
|
-- For (B) see Note [The fiv test in quickLookArg]
|
|
1984
|
2013
|
|
|
1985
|
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)
|
|
1986
|
2016
|
-- NB: orig_arg_rho may not be zonked, but that's ok
|
|
1987
|
2017
|
; when arg_influences_enclosing_call $
|
|
1988
|
2018
|
qlUnify app_res_rho orig_arg_rho
|
| ... |
... |
@@ -2000,7 +2030,6 @@ quickLookArg1 pos app_lspan rn_head larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_ |
|
2000
|
2030
|
, eaql_encl = arg_influences_enclosing_call
|
|
2001
|
2031
|
, eaql_res_rho = app_res_rho }) }}}
|
|
2002
|
2032
|
|
|
2003
|
|
-
|
|
2004
|
2033
|
{- *********************************************************************
|
|
2005
|
2034
|
* *
|
|
2006
|
2035
|
Folding over instantiation variables
|
| ... |
... |
@@ -2014,20 +2043,16 @@ When we are done with Quick Look on a call, we must turn any un-unified |
|
2014
|
2043
|
lower-case 'theta' (a mono-substitution) in the APP-DOWN rule of Fig 5 of the
|
|
2015
|
2044
|
Quick Look paper.
|
|
2016
|
2045
|
|
|
2017
|
|
-We so this by look at the arguments, left to right, monomorphising the free
|
|
2018
|
|
-instantiation variables of the /type/ arguments of the call. Those type
|
|
2019
|
|
-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
|
|
2020
|
2049
|
* the `WpTyApp` components of
|
|
2021
|
2050
|
* the `HsWrapper` of
|
|
2022
|
2051
|
* a `EWrap` argument
|
|
2023
|
|
-See `qlMonoHsWrapper`.
|
|
2024
|
|
-
|
|
2025
|
|
-By going left to right, we are sure to monomorphise instantiation variables
|
|
2026
|
|
-before we encounter them in an argument type (in `tcValArg`).
|
|
2027
|
2052
|
|
|
2028
|
2053
|
All instantiation variables for a call will be reachable from the type(s)
|
|
2029
|
2054
|
at which the function is instantiated -- i.e. those WpTyApps. Even instantiation
|
|
2030
|
|
-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
|
|
2031
|
2056
|
connected to the original type(s) at which the function is instantiated.
|
|
2032
|
2057
|
|
|
2033
|
2058
|
To monomorphise the free QL instantiation variables of a type, we use
|
| ... |
... |
@@ -2048,6 +2073,16 @@ Wrinkles: |
|
2048
|
2073
|
we want to monomrphise it. Hence the do_hole in `foldQLInstTyVars`
|
|
2049
|
2074
|
-}
|
|
2050
|
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
|
+
|
|
2051
|
2086
|
qlMonoHsWrapper :: HsWrapper -> ZonkM ()
|
|
2052
|
2087
|
-- See Note [Monomorphise instantiation variables]
|
|
2053
|
2088
|
qlMonoHsWrapper (WpCompose w1 w2) = qlMonoHsWrapper w1 >> qlMonoHsWrapper w2
|
| ... |
... |
@@ -2108,13 +2143,14 @@ We will instantiate Just with kappa, say, and then call |
|
2108
|
2143
|
quickLookArg1 False {kappa} (ids ++ ids) kappa
|
|
2109
|
2144
|
The call to tcInstFun will return with app_res_rho = [forall a. a->a]
|
|
2110
|
2145
|
which has no free instantiation variables, so we can QL-unify
|
|
2111
|
|
- kappa ~ [Forall a. a->a]
|
|
|
2146
|
+ kappa ~ [forall a. a->a]
|
|
2112
|
2147
|
-}
|
|
2113
|
2148
|
|
|
2114
|
2149
|
anyFreeKappa :: TcType -> TcM Bool
|
|
2115
|
2150
|
-- True if there is a free instantiation variable
|
|
2116
|
2151
|
-- in the argument type, after zonking
|
|
2117
|
2152
|
-- See Note [The fiv test in quickLookArg]
|
|
|
2153
|
+-- The function is monadic; it does not require a zonked type as input
|
|
2118
|
2154
|
anyFreeKappa ty = unTcMBool (foldQLInstVars go_tv ty)
|
|
2119
|
2155
|
where
|
|
2120
|
2156
|
go_tv tv = TCMB $ do { info <- readMetaTyVar tv
|