- -- ret_expr is the expression
- -- returnIO [coerce () x, .., coerce () z]
- ret_stmt = ResultStmt ret_expr noSrcLoc ;
- ret_expr = HsApp (HsVar returnIOName)
- (ExplicitList placeHolderType (map mk_item names)) ;
- mk_item name = HsApp (HsVar unsafeCoerceName) (HsVar name) ;
-
- all_stmts = stmts ++ [ret_stmt]
+ -- mk_return builds the expression
+ -- returnIO @ [()] [coerce () x, .., coerce () z]
+ --
+ -- Despite the inconvenience of building the type applications etc,
+ -- this *has* to be done in type-annotated post-typecheck form
+ -- because we are going to return a list of *polymorphic* values
+ -- coerced to type (). If we built a *source* stmt
+ -- return [coerce x, ..., coerce z]
+ -- then the type checker would instantiate x..z, and we wouldn't
+ -- get their *polymorphic* values. (And we'd get ambiguity errs
+ -- if they were overloaded, since they aren't applied to anything.)
+ mk_return ret_id ids = HsApp (TyApp (HsVar ret_id) [ret_ty])
+ (ExplicitList unitTy (map mk_item ids)) ;
+ mk_item id = HsApp (TyApp (HsVar unsafeCoerceId) [idType id, unitTy])
+ (HsVar id) ;
+
+ io_ty = mkTyConApp ioTyCon []