| ... |
... |
@@ -3,6 +3,7 @@ |
|
3
|
3
|
{-# LANGUAGE FlexibleContexts #-}
|
|
4
|
4
|
{-# LANGUAGE GADTs #-}
|
|
5
|
5
|
{-# LANGUAGE MultiWayIf #-}
|
|
|
6
|
+{-# LANGUAGE RecursiveDo #-}
|
|
6
|
7
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
7
|
8
|
{-# LANGUAGE TypeFamilies #-}
|
|
8
|
9
|
{-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]
|
| ... |
... |
@@ -47,7 +48,7 @@ import GHC.Core.TyCo.Subst ( substTyWithInScope ) |
|
47
|
48
|
import GHC.Core.Type
|
|
48
|
49
|
import GHC.Core.Coercion
|
|
49
|
50
|
|
|
50
|
|
-import GHC.Builtin.Types ( multiplicityTy )
|
|
|
51
|
+import GHC.Builtin.Types ( multiplicityTy, runtimeRepTy )
|
|
51
|
52
|
import GHC.Builtin.PrimOps( tagToEnumKey )
|
|
52
|
53
|
import GHC.Builtin.Names
|
|
53
|
54
|
|
| ... |
... |
@@ -59,6 +60,7 @@ import GHC.Types.SrcLoc |
|
59
|
60
|
import GHC.Types.Var.Env ( emptyTidyEnv, mkInScopeSet )
|
|
60
|
61
|
|
|
61
|
62
|
import GHC.Data.Maybe
|
|
|
63
|
+import GHC.Data.FastString
|
|
62
|
64
|
|
|
63
|
65
|
import GHC.Utils.Misc
|
|
64
|
66
|
import GHC.Utils.Outputable as Outputable
|
| ... |
... |
@@ -395,9 +397,7 @@ tcApp rn_expr exp_res_ty |
|
395
|
397
|
-- Step 3: Instantiate the function type (taking a quick look at args)
|
|
396
|
398
|
; do_ql <- wantQuickLook rn_fun
|
|
397
|
399
|
; (inst_args, app_res_rho)
|
|
398
|
|
- <- setQLInstLevel do_ql $ -- See (TCAPP1) and (TCAPP2) in
|
|
399
|
|
- -- Note [tcApp: typechecking applications]
|
|
400
|
|
- tcInstFun do_ql inst_final tc_head fun_sigma rn_args
|
|
|
400
|
+ <- tcInstFun do_ql inst_final tc_head fun_sigma rn_args
|
|
401
|
401
|
|
|
402
|
402
|
; case do_ql of
|
|
403
|
403
|
NoQL -> do { traceTc "tcApp:NoQL" (ppr rn_fun $$ ppr app_res_rho)
|
| ... |
... |
@@ -431,10 +431,6 @@ tcApp rn_expr exp_res_ty |
|
431
|
431
|
-- Step 5.5: wrap up
|
|
432
|
432
|
; finishApp tc_head tc_args app_res_rho res_wrap } }
|
|
433
|
433
|
|
|
434
|
|
-setQLInstLevel :: QLFlag -> TcM a -> TcM a
|
|
435
|
|
-setQLInstLevel DoQL thing_inside = setTcLevel QLInstVar thing_inside
|
|
436
|
|
-setQLInstLevel NoQL thing_inside = thing_inside
|
|
437
|
|
-
|
|
438
|
434
|
quickLookResultType :: TcRhoType -> ExpRhoType -> TcM ()
|
|
439
|
435
|
-- This function implements the shaded bit of rule APP-Downarrow in
|
|
440
|
436
|
-- Fig 5 of the QL paper: "A quick look at impredicativity" (ICFP'20).
|
| ... |
... |
@@ -723,7 +719,7 @@ tcInstFun do_ql inst_final (tc_fun, fun_ctxt) fun_sigma rn_args |
|
723
|
719
|
-- addHeadCtxt: important for the class constraints
|
|
724
|
720
|
-- that may be emitted from instantiating fun_sigma
|
|
725
|
721
|
addHeadCtxt fun_ctxt $
|
|
726
|
|
- instantiateSigma fun_orig fun_conc_tvs tvs theta body2
|
|
|
722
|
+ instantiateSigma do_ql fun_orig fun_conc_tvs tvs theta body2
|
|
727
|
723
|
-- See Note [Representation-polymorphism checking built-ins]
|
|
728
|
724
|
-- in GHC.Tc.Utils.Concrete.
|
|
729
|
725
|
-- NB: we are doing this even when "acc" is not empty,
|
| ... |
... |
@@ -786,8 +782,7 @@ tcInstFun do_ql inst_final (tc_fun, fun_ctxt) fun_sigma rn_args |
|
786
|
782
|
-- not defer in any way, because this is a QL instantiation variable.
|
|
787
|
783
|
-- It's easier just to do the job directly here.
|
|
788
|
784
|
do { arg_tys <- zipWithM new_arg_ty (leadingValArgs args) [pos..]
|
|
789
|
|
- ; rr_ty <- newFlexiTyVarTyQL do_ql runtimeRepTy
|
|
790
|
|
- ; res_ty <- newFlexiTyVarTyQL do_ql (mkTYPEapp rr_ty)
|
|
|
785
|
+ ; res_ty <- newOpenFlexiTyVarTyQL do_ql TauTv
|
|
791
|
786
|
; let fun_ty' = mkScaledFunTys arg_tys res_ty
|
|
792
|
787
|
|
|
793
|
788
|
-- Fill in kappa := nu_1 -> .. -> nu_n -> res_nu
|
| ... |
... |
@@ -827,12 +822,12 @@ tcInstFun do_ql inst_final (tc_fun, fun_ctxt) fun_sigma rn_args |
|
827
|
822
|
new_arg_ty :: LHsExpr GhcRn -> Int -> TcM (Scaled TcType)
|
|
828
|
823
|
-- Make a fresh nus for each argument in rule IVAR
|
|
829
|
824
|
new_arg_ty (L _ arg) i
|
|
830
|
|
- = do { arg_nu <- newOpenFlexiFRRTyVarTy $
|
|
|
825
|
+ = do { arg_nu <- newArgTyVarTyQL do_ql $
|
|
831
|
826
|
FRRExpectedFunTy (ExpectedFunTyArg (HsExprTcThing tc_fun) arg) i
|
|
832
|
827
|
-- Following matchActualFunTy, we create nu_i :: TYPE kappa_i[conc],
|
|
833
|
828
|
-- thereby ensuring that the arguments have concrete runtime representations
|
|
834
|
829
|
|
|
835
|
|
- ; mult_ty <- newFlexiTyVarTyQL do_ql multiplicityTy
|
|
|
830
|
+ ; mult_ty <- newFlexiTyVarTyQL do_ql (mkTyVarOccFS (fsLit "m")) TauTv multiplicityTy
|
|
836
|
831
|
-- mult_ty: e need variables for argument multiplicities (#18731)
|
|
837
|
832
|
-- Otherwise, 'undefined x' wouldn't be linear in x
|
|
838
|
833
|
|
| ... |
... |
@@ -916,30 +911,55 @@ addArgCtxt ctxt (L arg_loc arg) thing_inside |
|
916
|
911
|
{- *********************************************************************
|
|
917
|
912
|
* *
|
|
918
|
913
|
Instantiating fresh type variables
|
|
|
914
|
+
|
|
|
915
|
+ Functions in here use getTcLevelQL to decide what level
|
|
|
916
|
+ to put on fresh unification variables. If do_ql = DoQL, we
|
|
|
917
|
+ ignore the level in the monad, and use QLInstVar instead,
|
|
|
918
|
+ thereby giving birth to a Quick Look instantiation varaible
|
|
919
|
919
|
* *
|
|
920
|
920
|
********************************************************************* -}
|
|
921
|
921
|
|
|
922
|
|
-getTcLevelQL :: DoQL -> TcM TcLevel
|
|
|
922
|
+getTcLevelQL :: QLFlag -> TcM TcLevel
|
|
|
923
|
+-- If Quick Look is on, instantiate all fresh unification variables
|
|
|
924
|
+-- at level QLInstVar; they are instantiation variables
|
|
923
|
925
|
getTcLevelQL DoQL = return QLInstVar
|
|
924
|
926
|
getTcLevelQL NoQL = getTcLevel
|
|
925
|
927
|
|
|
926
|
|
-newFlexiTyVarTyQL :: DoQL -> TcKind -> TcM TcType
|
|
927
|
|
-newFlexiTyVarTyQL do_ql kind
|
|
928
|
|
- = do { lvl <- getTcLevelQL do_ql
|
|
929
|
|
- ; newMetaTyVarTyAtLevel lvl kind }
|
|
930
|
|
-
|
|
931
|
|
-{-
|
|
932
|
|
-newArgTyVarQL do_ql frr_ctxt
|
|
933
|
|
- = do { th_lvl <- getThLevel
|
|
934
|
|
- ; rr_ty <- case th_lvl of
|
|
935
|
|
- TypedBrack {} -> newFlexiTyVarTyQL do_ql runtimeRepTy
|
|
936
|
|
- _ -> mdo {
|
|
937
|
|
- ; rr_ty <-
|
|
938
|
|
- ; newFlexiTyVarTyQL do_ql (mkTYPEapp rr_ty) }
|
|
939
|
|
--}
|
|
940
|
|
-
|
|
941
|
|
-instantiateSigma :: CtOrigin
|
|
942
|
|
- -> QLFlag
|
|
|
928
|
+newFlexiTyVarQL :: QLFlag -> OccName -> MetaInfo -> TcKind -> TcM TcTyVar
|
|
|
929
|
+newFlexiTyVarQL do_ql occ info kind
|
|
|
930
|
+ = do { lvl <- getTcLevelQL do_ql
|
|
|
931
|
+ ; ref <- newMutVar Flexi
|
|
|
932
|
+ ; name <- newSysName occ -- See Note [Name of an unification variable]
|
|
|
933
|
+ -- in GHC.Tc.Utils.TcMType
|
|
|
934
|
+ ; let details = MetaTv { mtv_info = info
|
|
|
935
|
+ , mtv_ref = ref
|
|
|
936
|
+ , mtv_tclvl = lvl }
|
|
|
937
|
+ ; return (mkTcTyVar name kind details) }
|
|
|
938
|
+
|
|
|
939
|
+newFlexiTyVarTyQL :: QLFlag -> OccName -> MetaInfo -> TcKind -> TcM TcType
|
|
|
940
|
+newFlexiTyVarTyQL do_ql occ info kind
|
|
|
941
|
+ = mkTyVarTy <$> newFlexiTyVarQL do_ql occ info kind
|
|
|
942
|
+
|
|
|
943
|
+newOpenFlexiTyVarTyQL :: QLFlag -> MetaInfo -> TcM TcType
|
|
|
944
|
+newOpenFlexiTyVarTyQL do_ql rr_info
|
|
|
945
|
+ = do { let rr_occ = mkTyVarOccFS (fsLit "cx")
|
|
|
946
|
+ tv_occ = mkTyVarOccFS (fsLit "q")
|
|
|
947
|
+ ; rr_ty <- newFlexiTyVarTyQL do_ql rr_occ rr_info runtimeRepTy
|
|
|
948
|
+ ; arg_nu <- newFlexiTyVarTyQL do_ql tv_occ TauTv (mkTYPEapp rr_ty)
|
|
|
949
|
+ ; return arg_nu }
|
|
|
950
|
+
|
|
|
951
|
+newArgTyVarTyQL :: QLFlag -> FixedRuntimeRepContext -> TcM TcType
|
|
|
952
|
+newArgTyVarTyQL do_ql frr_ctxt
|
|
|
953
|
+ = mdo { let conc_orig = ConcreteFRR $
|
|
|
954
|
+ FixedRuntimeRepOrigin
|
|
|
955
|
+ { frr_context = frr_ctxt
|
|
|
956
|
+ , frr_type = arg_nu }
|
|
|
957
|
+ ; rr_info <- mkConcreteInfo conc_orig
|
|
|
958
|
+ ; arg_nu <- newOpenFlexiTyVarTyQL do_ql rr_info
|
|
|
959
|
+ ; return arg_nu }
|
|
|
960
|
+
|
|
|
961
|
+instantiateSigma :: QLFlag
|
|
|
962
|
+ -> CtOrigin
|
|
943
|
963
|
-> ConcreteTyVars -- ^ concreteness information
|
|
944
|
964
|
-> [TyVar]
|
|
945
|
965
|
-> TcThetaType -> TcSigmaType
|
| ... |
... |
@@ -947,9 +967,8 @@ instantiateSigma :: CtOrigin |
|
947
|
967
|
-- (instantiate orig tvs theta ty)
|
|
948
|
968
|
-- instantiates the type variables tvs, emits the (instantiated)
|
|
949
|
969
|
-- constraints theta, and returns the (instantiated) type ty
|
|
950
|
|
-instantiateSigma orig do_ql concs tvs theta body_ty
|
|
951
|
|
- = do { tv_lvl <- getTcLevelQL do_ql
|
|
952
|
|
- ; rec (subst, inst_tvs) <- mapAccumLM (new_meta tv_lvl subst) empty_subst tvs
|
|
|
970
|
+instantiateSigma do_ql orig concs tvs theta body_ty
|
|
|
971
|
+ = do { rec (subst, inst_tvs) <- mapAccumLM (new_meta subst) empty_subst tvs
|
|
953
|
972
|
; let inst_theta = substTheta subst theta
|
|
954
|
973
|
inst_body = substTy subst body_ty
|
|
955
|
974
|
|
| ... |
... |
@@ -959,7 +978,7 @@ instantiateSigma orig do_ql concs tvs theta body_ty |
|
959
|
978
|
, text "tvs" <+> ppr tvs
|
|
960
|
979
|
, text "theta" <+> ppr theta
|
|
961
|
980
|
, text "type" <+> debugPprType body_ty
|
|
962
|
|
- , text "with" <+> vcat (map debugPprType inst_tv_tys)
|
|
|
981
|
+ , text "with" <+> ppr inst_tvs
|
|
963
|
982
|
, text "theta:" <+> ppr inst_theta ])
|
|
964
|
983
|
|
|
965
|
984
|
; return (wrap, inst_body) }
|
| ... |
... |
@@ -969,35 +988,26 @@ instantiateSigma orig do_ql concs tvs theta body_ty |
|
969
|
988
|
-- We just want an accurate free-var set
|
|
970
|
989
|
empty_subst = mkEmptySubst in_scope
|
|
971
|
990
|
|
|
972
|
|
- new_meta :: TcLevel -> Subst -> Subst -> TyVar -> TcM (Subst, TcTyVar)
|
|
973
|
|
- new_meta tv_lvl final_subst subst tv
|
|
974
|
|
- = do { name <- cloneMetaTyVarName (tyVarName tv)
|
|
975
|
|
- ; ref <- newMutVar Flexi
|
|
976
|
|
- ; info <- get_info tv
|
|
977
|
|
- ; let details = MetaTv { mtv_info = info
|
|
978
|
|
- , mtv_ref = ref
|
|
979
|
|
- , mtv_tclvl = tv_lvl }
|
|
980
|
|
- ; let substd_kind = substTy subst (tyVarKind tv)
|
|
981
|
|
- new_tv = mkTcTyVar name substd_kind details
|
|
982
|
|
- new_subst = extendTvSubstWithClone subst tv new_tv
|
|
|
991
|
+ new_meta :: Subst -> Subst -> TyVar -> TcM (Subst, TcTyVar)
|
|
|
992
|
+ new_meta final_subst subst tv
|
|
|
993
|
+ = do { let occ = getOccName tv
|
|
|
994
|
+ substd_kind = substTy subst (tyVarKind tv)
|
|
|
995
|
+ ; info <- get_info final_subst tv
|
|
|
996
|
+ ; new_tv <- newFlexiTyVarQL do_ql occ info substd_kind
|
|
|
997
|
+ ; let new_subst = extendTvSubstWithClone subst tv new_tv
|
|
983
|
998
|
; return (new_subst, new_tv) }
|
|
984
|
999
|
|
|
985
|
1000
|
get_info :: Subst -> TyVar -> TcM MetaInfo
|
|
986
|
|
- get_info concs final_subst tv
|
|
|
1001
|
+ get_info final_subst tv
|
|
987
|
1002
|
-- Is this a type variable that must be instantiated to a concrete type?
|
|
988
|
1003
|
-- If so, create a ConcreteTv metavariable instead of a plain TauTv.
|
|
989
|
1004
|
-- See Note [Representation-polymorphism checking built-ins]
|
|
990
|
1005
|
-- in GHC.Tc.Utils.Concrete.
|
|
991
|
|
- --
|
|
992
|
|
- -- But check TH level: see [Wrinkle: Typed Template Haskell]
|
|
993
|
|
- -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
|
|
|
1006
|
+
|
|
994
|
1007
|
| Just conc_orig0 <- lookupNameEnv concs (tyVarName tv)
|
|
995
|
1008
|
, let conc_orig = substConcreteTvOrigin final_subst body_ty conc_orig0
|
|
996
|
1009
|
-- See Note [substConcreteTvOrigin].
|
|
997
|
|
- = do { th_lvl <- getThLevel
|
|
998
|
|
- ; case th_lvl of
|
|
999
|
|
- TypedBrack {} -> return TauTv
|
|
1000
|
|
- _ -> return (ConcreteTv conc) }
|
|
|
1010
|
+ = mkConcreteInfo conc_orig
|
|
1001
|
1011
|
|
|
1002
|
1012
|
-- The vastly common case
|
|
1003
|
1013
|
| otherwise
|