+ What you type The IO [HValue] that hscStmt returns
+ ------------- ------------------------------------
+ let pat = expr ==> let pat = expr in return [coerce HVal x, coerce HVal y, ...]
+ bindings: [x,y,...]
+
+ pat <- expr ==> expr >>= \ pat -> return [coerce HVal x, coerce HVal y, ...]
+ bindings: [x,y,...]
+
+ expr (of IO type) ==> expr >>= \ v -> return [v]
+ [NB: result not printed] bindings: [it]
+
+
+ expr (of non-IO type,
+ result showable) ==> let v = expr in print v >> return [v]
+ bindings: [it]
+
+ expr (of non-IO type,
+ result not showable) ==> error
+
+
+\begin{code}
+tcUserStmt :: [Name] -> RenamedStmt -> TcM (TypecheckedHsExpr, [Id])
+
+tcUserStmt names (ExprStmt expr _ loc)
+ = ASSERT( null names )
+ tcGetUnique `thenNF_Tc` \ uniq ->
+ let
+ fresh_it = itName uniq
+ the_bind = FunMonoBind fresh_it False
+ [ mkSimpleMatch [] expr placeHolderType loc ] loc
+ in
+ tryTc_ (traceTc (text "tcs 1b") `thenNF_Tc_`
+ tc_stmts [fresh_it] [
+ LetStmt (MonoBind the_bind [] NonRecursive),
+ ExprStmt (HsApp (HsVar printName) (HsVar fresh_it)) placeHolderType loc])
+ ( traceTc (text "tcs 1a") `thenNF_Tc_`
+ tc_stmts [fresh_it] [BindStmt (VarPatIn fresh_it) expr loc])
+
+tcUserStmt names stmt
+ = tc_stmts names [stmt]
+
+
+tc_stmts names stmts
+ = tcLookupGlobalId returnIOName `thenNF_Tc` \ return_id ->
+ tcLookupGlobalId bindIOName `thenNF_Tc` \ bind_id ->
+ tcLookupGlobalId failIOName `thenNF_Tc` \ fail_id ->
+ tcLookupTyCon ioTyConName `thenNF_Tc` \ ioTyCon ->
+ newTyVarTy liftedTypeKind `thenNF_Tc` \ res_ty ->
+ let
+ io_ty = (\ ty -> mkTyConApp ioTyCon [ty], res_ty)
+
+ -- mk_return builds the expression
+ -- returnIO @ [()] [coerce () x, .., coerce () z]
+ mk_return ids = HsApp (TyApp (HsVar return_id) [mkListTy unitTy])
+ (ExplicitList 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}
+%* *
+%************************************************************************