-tcExpr in_expr@(ExprWithTySig expr poly_ty)
- = tcExpr expr `thenTc` \ (texpr, lie, tau_ty) ->
- tcPolyType poly_ty `thenTc` \ sigma_sig ->
-
- -- Check the tau-type part
- tcSetErrCtxt (exprSigCtxt in_expr) $
- tcInstType [] sigma_sig `thenNF_Tc` \ sigma_sig' ->
- let
- (sig_tyvars', sig_theta', sig_tau') = splitSigmaTy sigma_sig'
- in
- unifyTauTy tau_ty sig_tau' `thenTc_`
-
- -- Check the type variables of the signature
- checkSigTyVars sig_tyvars' sig_tau' `thenTc_`
-
- -- Check overloading constraints
- newDicts SignatureOrigin sig_theta' `thenNF_Tc` \ (sig_dicts, _) ->
- tcSimplifyAndCheck
- (mkTyVarSet sig_tyvars')
- sig_dicts lie `thenTc_`
-
- -- If everything is ok, return the stuff unchanged, except for
- -- the effect of any substutions etc. We simply discard the
- -- result of the tcSimplifyAndCheck, except for any default
- -- resolution it may have done, which is recorded in the
- -- substitution.
- returnTc (texpr, lie, tau_ty)
+tcMonoExpr in_expr@(ExprWithTySig expr poly_ty) res_ty
+ = tcSetErrCtxt (exprSigCtxt in_expr) $
+ tcHsType poly_ty `thenTc` \ sig_tc_ty ->
+
+ if not (isForAllTy sig_tc_ty) then
+ -- Easy case
+ unifyTauTy sig_tc_ty res_ty `thenTc_`
+ tcMonoExpr expr sig_tc_ty
+
+ else -- Signature is polymorphic
+ tcPolyExpr expr sig_tc_ty `thenTc` \ (_, _, expr, expr_ty, lie) ->
+
+ -- Now match the signature type with res_ty.
+ -- We must not do this earlier, because res_ty might well
+ -- mention variables free in the environment, and we'd get
+ -- bogus complaints about not being able to for-all the
+ -- sig_tyvars
+ unifyTauTy res_ty expr_ty `thenTc_`
+
+ -- If everything is ok, return the stuff unchanged, except for
+ -- the effect of any substutions etc. We simply discard the
+ -- result of the tcSimplifyAndCheck (inside tcPolyExpr), except for any default
+ -- resolution it may have done, which is recorded in the
+ -- substitution.
+ returnTc (expr, lie)
+\end{code}
+
+Implicit Parameter bindings.
+
+\begin{code}
+tcMonoExpr (HsWith expr binds) res_ty
+ = tcMonoExpr expr res_ty `thenTc` \ (expr', lie) ->
+ tcIPBinds binds `thenTc` \ (binds', types, lie2) ->
+ partitionPredsOfLIE isBound lie `thenTc` \ (ips, lie', dict_binds) ->
+ pprTrace "tcMonoExpr With" (ppr (ips, lie', dict_binds)) $
+ let expr'' = if nullMonoBinds dict_binds
+ then expr'
+ else HsLet (mkMonoBind (revBinds dict_binds) [] NonRecursive)
+ expr'
+ in
+ tcCheckIPBinds binds' types ips `thenTc_`
+ returnTc (HsWith expr'' binds', lie' `plusLIE` lie2)
+ where isBound p
+ = case ipName_maybe p of
+ Just n -> n `elem` names
+ Nothing -> False
+ names = map fst binds
+ -- revBinds is used because tcSimplify outputs the bindings
+ -- out-of-order. it's not a problem elsewhere because these
+ -- bindings are normally used in a recursive let
+ -- ZZ probably need to find a better solution
+ revBinds (b1 `AndMonoBinds` b2) =
+ (revBinds b2) `AndMonoBinds` (revBinds b1)
+ revBinds b = b
+
+tcIPBinds ((name, expr) : binds)
+ = newTyVarTy_OpenKind `thenTc` \ ty ->
+ tcGetSrcLoc `thenTc` \ loc ->
+ let id = ipToId name ty loc in
+ tcMonoExpr expr ty `thenTc` \ (expr', lie) ->
+ zonkTcType ty `thenTc` \ ty' ->
+ tcIPBinds binds `thenTc` \ (binds', types, lie2) ->
+ returnTc ((id, expr') : binds', ty : types, lie `plusLIE` lie2)
+tcIPBinds [] = returnTc ([], [], emptyLIE)
+
+tcCheckIPBinds binds types ips
+ = foldrTc tcCheckIPBind (getIPsOfLIE ips) (zip binds types)
+
+-- ZZ how do we use the loc?
+tcCheckIPBind bt@((v, _), t1) ((n, t2) : ips) | getName v == n
+ = unifyTauTy t1 t2 `thenTc_`
+ tcCheckIPBind bt ips `thenTc` \ ips' ->
+ returnTc ips'
+tcCheckIPBind bt (ip : ips)
+ = tcCheckIPBind bt ips `thenTc` \ ips' ->
+ returnTc (ip : ips')
+tcCheckIPBind bt []
+ = returnTc []
+\end{code}
+
+Typecheck expression which in most cases will be an Id.
+
+\begin{code}
+tcExpr_id :: RenamedHsExpr
+ -> TcM s (TcExpr,
+ LIE,
+ TcType)
+tcExpr_id id_expr
+ = case id_expr of
+ HsVar name -> tcId name `thenNF_Tc` \ stuff ->
+ returnTc stuff
+ other -> newTyVarTy_OpenKind `thenNF_Tc` \ id_ty ->
+ tcMonoExpr id_expr id_ty `thenTc` \ (id_expr', lie_id) ->
+ returnTc (id_expr', lie_id, id_ty)