+ -- mk_return builds the expression
+ -- returnIO @ [()] [coerce () x, .., coerce () z]
+ mk_return ids = HsApp (TyApp (HsVar return_id) [mkListTy unitTy])
+ (ExplicitListOut unitTy (map mk_item ids))
+
+ mk_item id = HsApp (TyApp (HsVar unsafeCoerceId) [idType id, unitTy])
+ (HsVar id)
+ in
+
+ traceTc (text "tcs 2") `thenNF_Tc_`
+ tcStmtsAndThen combine (DoCtxt DoExpr) io_ty stmts (
+ -- Look up the names right in the middle,
+ -- where they will all be in scope
+ mapNF_Tc tcLookupId names `thenNF_Tc` \ ids ->
+ returnTc ((ids, [ResultStmt (mk_return ids) noSrcLoc]), emptyLIE)
+ ) `thenTc` \ ((ids, tc_stmts), lie) ->
+
+ -- Simplify the context right here, so that we fail
+ -- if there aren't enough instances. Notably, when we see
+ -- e
+ -- we use tryTc_ to try it <- e
+ -- and then let it = e
+ -- It's the simplify step that rejects the first.
+
+ traceTc (text "tcs 3") `thenNF_Tc_`
+ tcSimplifyTop lie `thenTc` \ const_binds ->
+ traceTc (text "tcs 4") `thenNF_Tc_`
+
+ returnTc (mkHsLet const_binds $
+ HsDoOut DoExpr tc_stmts return_id bind_id fail_id
+ (mkTyConApp ioTyCon [mkListTy unitTy]) noSrcLoc,
+ ids)
+ where
+ combine stmt (ids, stmts) = (ids, stmt:stmts)
+\end{code}
+
+%************************************************************************
+%* *
+\subsection{Typechecking an expression}
+%* *
+%************************************************************************
+
+\begin{code}
+typecheckExpr :: DynFlags
+ -> PersistentCompilerState
+ -> HomeSymbolTable
+ -> TypeEnv -- The interactive context's type envt
+ -> PrintUnqualified -- For error printing
+ -> Module
+ -> (SyntaxMap,
+ RenamedHsExpr, -- The expression itself
+ [RenamedHsDecl]) -- Plus extra decls it sucked in from interface files
+ -> IO (Maybe (PersistentCompilerState,
+ TypecheckedHsExpr,
+ [Id], -- always empty (matches typecheckStmt)
+ Type))
+
+typecheckExpr dflags pcs hst ic_type_env unqual this_mod (syn_map, expr, decls)
+ = typecheck dflags syn_map pcs hst unqual $
+
+ -- use the default default settings, i.e. [Integer, Double]
+ tcSetDefaultTys defaultDefaultTys $
+
+ -- Typecheck the extra declarations
+ fixTc (\ ~(unf_env, _, _, _, _) ->
+ tcImports unf_env pcs hst get_fixity this_mod decls
+ ) `thenTc` \ (env, new_pcs, local_inst_info, deriv_binds, local_rules) ->
+ ASSERT( null local_inst_info && nullBinds deriv_binds && null local_rules )
+
+ -- Now typecheck the expression
+ tcSetEnv env $
+ tcExtendGlobalTypeEnv ic_type_env $
+
+ newTyVarTy openTypeKind `thenTc` \ ty ->
+ tcMonoExpr expr ty `thenTc` \ (e', lie) ->
+ tcSimplifyInfer smpl_doc (tyVarsOfType ty) lie
+ `thenTc` \ (qtvs, lie_free, dict_binds, dict_ids) ->
+ tcSimplifyTop lie_free `thenTc` \ const_binds ->
+
+ let all_expr = mkHsLet const_binds $
+ TyLam qtvs $
+ DictLam dict_ids $
+ mkHsLet dict_binds $
+ e'
+
+ all_expr_ty = mkForAllTys qtvs $
+ mkFunTys (map idType dict_ids) $
+ ty
+ in
+
+ zonkExpr all_expr `thenNF_Tc` \ zonked_expr ->
+ zonkTcType all_expr_ty `thenNF_Tc` \ zonked_ty ->
+ ioToTc (dumpIfSet_dyn dflags
+ Opt_D_dump_tc "Typechecked" (ppr zonked_expr)) `thenNF_Tc_`
+ returnTc (new_pcs, zonked_expr, [], zonked_ty)
+
+ where
+ get_fixity :: Name -> Maybe Fixity
+ get_fixity n = pprPanic "typecheckExpr" (ppr n)
+
+ smpl_doc = ptext SLIT("main expression")
+\end{code}
+
+%************************************************************************
+%* *
+\subsection{Typechecking a module}
+%* *
+%************************************************************************