- -- Now generalise the bindings
- let
- -- local_binds is a bunch of bindings of the form
- -- f_mono = f_poly tyvars dicts
- -- one for each binder, f, that lacks a type signature.
- -- This bunch of bindings is put at the top of the RHS of every
- -- binding in the group, so as to bind all the f_monos.
-
- local_binds = [ (local_id, mkHsDictApp (mkHsTyApp (HsVar local_id) tys_to_gen) dicts_to_gen)
- | local_id <- nosig_local_ids
- ]
-
- find_sig lid = head [ (pid, tvs, ds, lie)
- | SigInfo _ pid lid' tvs ds lie,
- lid==lid'
- ]
-
- gen_bind (bind, lie)
- = tcSimplifyWithExtraGlobals tyvars_not_to_gen tyvars_to_gen avail lie
- `thenTc` \ (lie_free, dict_binds) ->
- returnTc (AbsBind tyvars_to_gen_here
- dicts
- (local_ids `zipEqual` poly_ids)
- (dict_binds ++ local_binds)
- bind,
- lie_free)
- where
- local_ids = bindersOf bind
- local_sigs = [sig | sig@(SigInfo _ _ local_id _ _) <- all_sig_infos,
- local_id `elem` local_ids
- ]
-
- (tyvars_to_gen_here, dicts, avail)
- = case (local_ids, sigs) of
-
- ([local_id], [SigInfo _ _ _ tyvars_to_gen dicts lie])
- -> (tyvars_to_gen, dicts, lie)
-
- other -> (tyvars_to_gen, dicts, avail)
+
+%************************************************************************
+%* *
+\subsection{tcMonoBind}
+%* *
+%************************************************************************
+
+@tcMonoBinds@ deals with a single @MonoBind@.
+The signatures have been dealt with already.
+
+\begin{code}
+tcMonoBinds :: LHsBinds Name
+ -> TcSigFun -> RecFlag
+ -> TcM (LHsBinds TcId, [MonoBindInfo])
+
+tcMonoBinds binds lookup_sig is_rec
+ = do { tc_binds <- mapBagM (wrapLocM (tcLhs lookup_sig)) binds
+
+ -- Bring (a) the scoped type variables, and (b) the Ids, into scope for the RHSs
+ -- For (a) it's ok to bring them all into scope at once, even
+ -- though each type sig should scope only over its own RHS,
+ -- because the renamer has sorted all that out.
+ ; let mono_info = getMonoBindInfo tc_binds
+ rhs_tvs = [ (name, mkTyVarTy tv)
+ | (_, Just sig, _) <- mono_info,
+ (name, tv) <- sig_scoped sig `zip` sig_tvs sig ]
+ rhs_id_env = map mk mono_info -- A binding for each term variable
+
+ ; binds' <- tcExtendTyVarEnv2 rhs_tvs $
+ tcExtendIdEnv2 rhs_id_env $
+ traceTc (text "tcMonoBinds" <+> vcat [ppr n <+> ppr id <+> ppr (idType id) | (n,id) <- rhs_id_env]) `thenM_`
+ mapBagM (wrapLocM tcRhs) tc_binds
+ ; return (binds', mono_info) }
+ where
+ mk (name, Just sig, _) = (name, sig_id sig) -- Use the type sig if there is one
+ mk (name, Nothing, mono_id) = (name, mono_id) -- otherwise use a monomorphic version
+
+------------------------
+-- tcLhs typechecks the LHS of the bindings, to construct the environment in which
+-- we typecheck the RHSs. Basically what we are doing is this: for each binder:
+-- if there's a signature for it, use the instantiated signature type
+-- otherwise invent a type variable
+-- You see that quite directly in the FunBind case.
+--
+-- But there's a complication for pattern bindings:
+-- data T = MkT (forall a. a->a)
+-- MkT f = e
+-- Here we can guess a type variable for the entire LHS (which will be refined to T)
+-- but we want to get (f::forall a. a->a) as the RHS environment.
+-- The simplest way to do this is to typecheck the pattern, and then look up the
+-- bound mono-ids. Then we want to retain the typechecked pattern to avoid re-doing
+-- it; hence the TcMonoBind data type in which the LHS is done but the RHS isn't
+
+data TcMonoBind -- Half completed; LHS done, RHS not done
+ = TcFunBind MonoBindInfo (Located TcId) Bool (MatchGroup Name)
+ | TcPatBind [MonoBindInfo] (LPat TcId) (GRHSs Name) TcSigmaType
+
+type MonoBindInfo = (Name, Maybe TcSigInfo, TcId)
+ -- Type signature (if any), and
+ -- the monomorphic bound things
+
+bndrNames :: [MonoBindInfo] -> [Name]
+bndrNames mbi = [n | (n,_,_) <- mbi]
+
+getMonoType :: MonoBindInfo -> TcTauType
+getMonoType (_,_,mono_id) = idType mono_id
+
+tcLhs :: TcSigFun -> HsBind Name -> TcM TcMonoBind
+tcLhs lookup_sig (FunBind (L nm_loc name) inf matches)
+ = do { let mb_sig = lookup_sig name
+ ; mono_name <- newLocalName name
+ ; mono_ty <- mk_mono_ty mb_sig
+ ; let mono_id = mkLocalId mono_name mono_ty
+ ; return (TcFunBind (name, mb_sig, mono_id) (L nm_loc mono_id) inf matches) }
+ where
+ mk_mono_ty (Just sig) = return (sig_tau sig)
+ mk_mono_ty Nothing = newTyFlexiVarTy argTypeKind
+
+tcLhs lookup_sig bind@(PatBind pat grhss _)
+ = do { let tc_pat exp_ty = tcPat (LetPat lookup_sig) pat exp_ty lookup_infos
+ ; ((pat', ex_tvs, infos), pat_ty)
+ <- addErrCtxt (patMonoBindsCtxt pat grhss)
+ (tcInfer tc_pat)
+
+ -- Don't know how to deal with pattern-bound existentials yet
+ ; checkTc (null ex_tvs) (existentialExplode bind)
+
+ ; return (TcPatBind infos pat' grhss pat_ty) }
+ where
+ names = collectPatBinders pat
+
+ -- After typechecking the pattern, look up the binder
+ -- names, which the pattern has brought into scope.
+ lookup_infos :: TcM [MonoBindInfo]
+ lookup_infos = do { mono_ids <- tcLookupLocalIds names
+ ; return [ (name, lookup_sig name, mono_id)
+ | (name, mono_id) <- names `zip` mono_ids] }
+
+-------------------
+tcRhs :: TcMonoBind -> TcM (HsBind TcId)
+tcRhs (TcFunBind info fun'@(L _ mono_id) inf matches)
+ = do { matches' <- tcMatchesFun (idName mono_id) matches
+ (Check (idType mono_id))
+ ; return (FunBind fun' inf matches') }
+
+tcRhs bind@(TcPatBind _ pat' grhss pat_ty)
+ = do { grhss' <- addErrCtxt (patMonoBindsCtxt pat' grhss) $
+ tcGRHSsPat grhss (Check pat_ty)
+ ; return (PatBind pat' grhss' pat_ty) }
+
+
+---------------------
+getMonoBindInfo :: Bag (Located TcMonoBind) -> [MonoBindInfo]
+getMonoBindInfo tc_binds
+ = foldrBag (get_info . unLoc) [] tc_binds
+ where
+ get_info (TcFunBind info _ _ _) rest = info : rest
+ get_info (TcPatBind infos _ _ _) rest = infos ++ rest
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{getTyVarsToGen}
+%* *
+%************************************************************************
+
+Type signatures are tricky. See Note [Signature skolems] in TcType
+
+\begin{code}
+tcTySigs :: [LSig Name] -> TcM [TcSigInfo]
+-- The trick here is that all the signatures should have the same
+-- context, and we want to share type variables for that context, so that
+-- all the right hand sides agree a common vocabulary for their type
+-- constraints
+tcTySigs [] = return []
+
+tcTySigs sigs
+ = do { (tc_sig1 : tc_sigs) <- mappM tcTySig sigs
+ ; mapM (check_ctxt tc_sig1) tc_sigs
+ ; return (tc_sig1 : tc_sigs) }
+ where
+ -- Check tha all the signature contexts are the same
+ -- The type signatures on a mutually-recursive group of definitions
+ -- must all have the same context (or none).
+ --
+ -- We unify them because, with polymorphic recursion, their types
+ -- might not otherwise be related. This is a rather subtle issue.
+ check_ctxt :: TcSigInfo -> TcSigInfo -> TcM ()
+ check_ctxt sig1@(TcSigInfo { sig_theta = theta1 }) sig@(TcSigInfo { sig_theta = theta })
+ = setSrcSpan (instLocSrcSpan (sig_loc sig)) $
+ addErrCtxt (sigContextsCtxt sig1 sig) $
+ unifyTheta theta1 theta
+
+
+tcTySig :: LSig Name -> TcM TcSigInfo
+tcTySig (L span (Sig (L _ name) ty))
+ = setSrcSpan span $
+ do { sigma_ty <- tcHsSigType (FunSigCtxt name) ty
+ ; (tvs, theta, tau) <- tcInstSigType name sigma_ty
+ ; loc <- getInstLoc (SigOrigin (SigSkol name))
+
+ ; let poly_id = mkLocalId name sigma_ty
+
+ -- The scoped names are the ones explicitly mentioned
+ -- in the HsForAll. (There may be more in sigma_ty, because
+ -- of nested type synonyms. See Note [Scoped] with TcSigInfo.)
+ scoped_names = case ty of
+ L _ (HsForAllTy _ tvs _ _) -> hsLTyVarNames tvs
+ other -> []
+
+ ; return (TcSigInfo { sig_id = poly_id, sig_scoped = scoped_names,
+ sig_tvs = tvs, sig_theta = theta, sig_tau = tau,
+ sig_loc = loc }) }