2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[DsExpr]{Matching expressions (Exprs)}
7 #include "HsVersions.h"
9 module DsExpr ( dsExpr ) where
12 IMPORT_DELOOPER(DsLoop) -- partly to get dsBinds, partly to chk dsExpr
14 import HsSyn ( failureFreePat,
15 HsExpr(..), OutPat(..), HsLit(..), ArithSeqInfo(..),
16 Stmt(..), DoOrListComp(..), Match(..), HsBinds, HsType, Fixity,
19 import TcHsSyn ( SYN_IE(TypecheckedHsExpr), SYN_IE(TypecheckedHsBinds),
20 SYN_IE(TypecheckedRecordBinds), SYN_IE(TypecheckedPat),
21 SYN_IE(TypecheckedStmt)
26 import DsCCall ( dsCCall )
27 import DsHsSyn ( outPatType )
28 import DsListComp ( dsListComp )
29 import DsUtils ( mkAppDs, mkConDs, mkPrimDs, dsExprToAtom, mkTupleExpr,
30 mkErrorAppDs, showForErr, EquationInfo,
31 MatchResult, SYN_IE(DsCoreArg)
33 import Match ( matchWrapper )
35 import CoreUtils ( coreExprType, substCoreExpr, argToExpr,
36 mkCoreIfThenElse, unTagBinders )
37 import CostCentre ( mkUserCC )
38 import FieldLabel ( fieldLabelType, FieldLabel )
39 import Id ( idType, nullIdEnv, addOneToIdEnv,
40 dataConArgTys, dataConFieldLabels,
41 recordSelectorFieldLabel, SYN_IE(Id)
43 import Literal ( mkMachInt, Literal(..) )
44 import Name ( Name{--O only-} )
45 import PprStyle ( PprStyle(..) )
46 import PprType ( GenType )
47 import PrelVals ( rEC_CON_ERROR_ID, rEC_UPD_ERROR_ID, voidId )
48 import Pretty ( Doc, hcat, ptext, text )
49 import TyCon ( isDataTyCon, isNewTyCon )
50 import Type ( splitSigmaTy, splitFunTy, typePrimRep,
51 getAppDataTyConExpandingDicts, maybeAppTyCon, getAppTyCon, applyTy,
52 maybeBoxedPrimType, splitAppTy, SYN_IE(Type)
54 import TysPrim ( voidTy )
55 import TysWiredIn ( mkTupleTy, tupleCon, nilDataCon, consDataCon, listTyCon,
58 import TyVar ( nullTyVarEnv, addOneToTyVarEnv, GenTyVar{-instance Eq-} )
59 import Usage ( SYN_IE(UVar) )
60 import Maybes ( maybeToBool )
61 import Util ( zipEqual, pprError, panic, assertPanic )
63 #if __GLASGOW_HASKELL__ >= 202
67 mk_nil_con ty = mkCon nilDataCon [] [ty] [] -- micro utility...
70 The funny business to do with variables is that we look them up in the
71 Id-to-Id and Id-to-Id maps that the monadery is carrying
72 around; if we get hits, we use the value accordingly.
74 %************************************************************************
76 \subsection[DsExpr-vars-and-cons]{Variables and constructors}
78 %************************************************************************
81 dsExpr :: TypecheckedHsExpr -> DsM CoreExpr
83 dsExpr e@(HsVar var) = dsId var
86 %************************************************************************
88 \subsection[DsExpr-literals]{Literals}
90 %************************************************************************
92 We give int/float literals type Integer and Rational, respectively.
93 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
96 ToDo: put in range checks for when converting "i"
97 (or should that be in the typechecker?)
99 For numeric literals, we try to detect there use at a standard type
100 (Int, Float, etc.) are directly put in the right constructor.
101 [NB: down with the @App@ conversion.]
102 Otherwise, we punt, putting in a "NoRep" Core literal (where the
103 representation decisions are delayed)...
105 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
108 dsExpr (HsLitOut (HsString s) _)
110 = returnDs (mk_nil_con charTy)
114 the_char = mkCon charDataCon [] [] [LitArg (MachChar (_HEAD_ s))]
115 the_nil = mk_nil_con charTy
117 mkConDs consDataCon [TyArg charTy, VarArg the_char, VarArg the_nil]
119 -- "_" => build (\ c n -> c 'c' n) -- LATER
121 -- "str" ==> build (\ c n -> foldr charTy T c n "str")
124 dsExpr (HsLitOut (HsString str) _)
125 = newTyVarsDs [alphaTyVar] `thenDs` \ [new_tyvar] ->
127 new_ty = mkTyVarTy new_tyvar
130 charTy `mkFunTy` (new_ty `mkFunTy` new_ty),
132 mkForallTy [alphaTyVar]
133 ((charTy `mkFunTy` (alphaTy `mkFunTy` alphaTy))
134 `mkFunTy` (alphaTy `mkFunTy` alphaTy))
135 ] `thenDs` \ [c,n,g] ->
136 returnDs (mkBuild charTy new_tyvar c n g (
138 (CoTyApp (CoTyApp (Var foldrId) charTy) new_ty) *** ensure non-prim type ***
139 [VarArg c,VarArg n,LitArg (NoRepStr str)]))
142 -- otherwise, leave it as a NoRepStr;
143 -- the Core-to-STG pass will wrap it in an application of "unpackCStringId".
145 dsExpr (HsLitOut (HsString str) _)
146 = returnDs (Lit (NoRepStr str))
148 dsExpr (HsLitOut (HsLitLit s) ty)
149 = returnDs ( mkCon data_con [] [] [LitArg (MachLitLit s kind)] )
152 = case (maybeBoxedPrimType ty) of
153 Just (boxing_data_con, prim_ty)
154 -> (boxing_data_con, typePrimRep prim_ty)
156 -> pprError "ERROR: ``literal-literal'' not a single-constructor type: "
157 (hcat [ptext s, text "; type: ", ppr PprDebug ty])
159 dsExpr (HsLitOut (HsInt i) ty)
160 = returnDs (Lit (NoRepInteger i ty))
162 dsExpr (HsLitOut (HsFrac r) ty)
163 = returnDs (Lit (NoRepRational r ty))
165 -- others where we know what to do:
167 dsExpr (HsLitOut (HsIntPrim i) _)
168 = if (i >= toInteger minInt && i <= toInteger maxInt) then
169 returnDs (Lit (mkMachInt i))
171 error ("ERROR: Int constant " ++ show i ++ out_of_range_msg)
173 dsExpr (HsLitOut (HsFloatPrim f) _)
174 = returnDs (Lit (MachFloat f))
175 -- ToDo: range checking needed!
177 dsExpr (HsLitOut (HsDoublePrim d) _)
178 = returnDs (Lit (MachDouble d))
179 -- ToDo: range checking needed!
181 dsExpr (HsLitOut (HsChar c) _)
182 = returnDs ( mkCon charDataCon [] [] [LitArg (MachChar c)] )
184 dsExpr (HsLitOut (HsCharPrim c) _)
185 = returnDs (Lit (MachChar c))
187 dsExpr (HsLitOut (HsStringPrim s) _)
188 = returnDs (Lit (MachStr s))
190 -- end of literals magic. --
192 dsExpr expr@(HsLam a_Match)
193 = matchWrapper LambdaMatch [a_Match] "lambda" `thenDs` \ (binders, matching_code) ->
194 returnDs ( mkValLam binders matching_code )
196 dsExpr expr@(HsApp e1 e2) = dsApp expr []
197 dsExpr expr@(OpApp e1 op _ e2) = dsApp expr []
200 Operator sections. At first it looks as if we can convert
209 But no! expr might be a redex, and we can lose laziness badly this
214 for example. So we convert instead to
216 let y = expr in \x -> op y x
218 If \tr{expr} is actually just a variable, say, then the simplifier
222 dsExpr (SectionL expr op)
223 = dsExpr op `thenDs` \ core_op ->
224 dsExpr expr `thenDs` \ core_expr ->
225 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
227 -- for the type of x, we need the type of op's 2nd argument
229 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
230 case (splitFunTy tau_ty) of {
231 ((_:arg2_ty:_), _) -> arg2_ty;
232 _ -> panic "dsExpr:SectionL:arg 2 ty" }}
234 newSysLocalDs x_ty `thenDs` \ x_id ->
235 returnDs (mkValLam [x_id] (core_op `App` y_atom `App` VarArg x_id))
237 -- dsExpr (SectionR op expr) -- \ x -> op x expr
238 dsExpr (SectionR op expr)
239 = dsExpr op `thenDs` \ core_op ->
240 dsExpr expr `thenDs` \ core_expr ->
241 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
243 -- for the type of x, we need the type of op's 1st argument
245 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
246 case (splitFunTy tau_ty) of {
247 ((arg1_ty:_), _) -> arg1_ty;
248 _ -> panic "dsExpr:SectionR:arg 1 ty" }}
250 newSysLocalDs x_ty `thenDs` \ x_id ->
251 returnDs (mkValLam [x_id] (core_op `App` VarArg x_id `App` y_atom))
253 dsExpr (CCall label args may_gc is_asm result_ty)
254 = mapDs dsExpr args `thenDs` \ core_args ->
255 dsCCall label core_args may_gc is_asm result_ty
256 -- dsCCall does all the unboxification, etc.
258 dsExpr (HsSCC cc expr)
259 = dsExpr expr `thenDs` \ core_expr ->
260 getModuleAndGroupDs `thenDs` \ (mod_name, group_name) ->
261 returnDs ( SCC (mkUserCC cc mod_name group_name) core_expr)
263 dsExpr expr@(HsCase discrim matches src_loc)
264 = putSrcLocDs src_loc $
265 dsExpr discrim `thenDs` \ core_discrim ->
266 matchWrapper CaseMatch matches "case" `thenDs` \ ([discrim_var], matching_code) ->
267 returnDs ( mkCoLetAny (NonRec discrim_var core_discrim) matching_code )
269 dsExpr (HsLet binds expr)
270 = dsBinds binds `thenDs` \ core_binds ->
271 dsExpr expr `thenDs` \ core_expr ->
272 returnDs ( mkCoLetsAny core_binds core_expr )
274 dsExpr (HsDoOut do_or_lc stmts return_id then_id zero_id result_ty src_loc)
275 | maybeToBool maybe_list_comp
276 = -- Special case for list comprehensions
277 putSrcLocDs src_loc $
278 dsListComp stmts elt_ty
281 = putSrcLocDs src_loc $
282 dsDo do_or_lc stmts return_id then_id zero_id result_ty
285 = case (do_or_lc, maybeAppTyCon result_ty) of
286 (ListComp, Just (tycon, [elt_ty]))
290 -- We need the ListComp form to use deListComp (rather than the "do" form)
291 -- because the "return" in a do block is a call to "PrelBase.return", and
292 -- not a ReturnStmt. Only the ListComp form has ReturnStmts
294 Just elt_ty = maybe_list_comp
296 dsExpr (HsIf guard_expr then_expr else_expr src_loc)
297 = putSrcLocDs src_loc $
298 dsExpr guard_expr `thenDs` \ core_guard ->
299 dsExpr then_expr `thenDs` \ core_then ->
300 dsExpr else_expr `thenDs` \ core_else ->
301 returnDs (mkCoreIfThenElse core_guard core_then core_else)
305 Type lambda and application
306 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
308 dsExpr (TyLam tyvars expr)
309 = dsExpr expr `thenDs` \ core_expr ->
310 returnDs (mkTyLam tyvars core_expr)
312 dsExpr expr@(TyApp e tys) = dsApp expr []
316 Various data construction things
317 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319 dsExpr (ExplicitListOut ty xs)
321 [] -> returnDs (mk_nil_con ty)
323 dsExpr y `thenDs` \ core_hd ->
324 dsExpr (ExplicitListOut ty ys) `thenDs` \ core_tl ->
325 mkConDs consDataCon [TyArg ty, VarArg core_hd, VarArg core_tl]
327 dsExpr (ExplicitTuple expr_list)
328 = mapDs dsExpr expr_list `thenDs` \ core_exprs ->
329 mkConDs (tupleCon (length expr_list))
330 (map (TyArg . coreExprType) core_exprs ++ map VarArg core_exprs)
332 dsExpr (ArithSeqOut expr (From from))
333 = dsExpr expr `thenDs` \ expr2 ->
334 dsExpr from `thenDs` \ from2 ->
335 mkAppDs expr2 [VarArg from2]
337 dsExpr (ArithSeqOut expr (FromTo from two))
338 = dsExpr expr `thenDs` \ expr2 ->
339 dsExpr from `thenDs` \ from2 ->
340 dsExpr two `thenDs` \ two2 ->
341 mkAppDs expr2 [VarArg from2, VarArg two2]
343 dsExpr (ArithSeqOut expr (FromThen from thn))
344 = dsExpr expr `thenDs` \ expr2 ->
345 dsExpr from `thenDs` \ from2 ->
346 dsExpr thn `thenDs` \ thn2 ->
347 mkAppDs expr2 [VarArg from2, VarArg thn2]
349 dsExpr (ArithSeqOut expr (FromThenTo from thn two))
350 = dsExpr expr `thenDs` \ expr2 ->
351 dsExpr from `thenDs` \ from2 ->
352 dsExpr thn `thenDs` \ thn2 ->
353 dsExpr two `thenDs` \ two2 ->
354 mkAppDs expr2 [VarArg from2, VarArg thn2, VarArg two2]
357 Record construction and update
358 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359 For record construction we do this (assuming T has three arguments)
363 let err = /\a -> recConErr a
364 T (recConErr t1 "M.lhs/230/op1")
366 (recConErr t1 "M.lhs/230/op3")
368 recConErr then converts its arugment string into a proper message
369 before printing it as
371 M.lhs, line 230: missing field op1 was evaluated
375 dsExpr (RecordCon con_expr rbinds)
376 = dsExpr con_expr `thenDs` \ con_expr' ->
378 con_id = get_con con_expr'
379 (arg_tys, _) = splitFunTy (coreExprType con_expr')
382 = case [rhs | (sel_id,rhs,_) <- rbinds,
383 lbl == recordSelectorFieldLabel sel_id] of
384 (rhs:rhss) -> ASSERT( null rhss )
386 [] -> mkErrorAppDs rEC_CON_ERROR_ID arg_ty (showForErr lbl)
388 mapDs mk_arg (zipEqual "dsExpr:RecordCon" arg_tys (dataConFieldLabels con_id)) `thenDs` \ con_args ->
389 mkAppDs con_expr' (map VarArg con_args)
391 -- "con_expr'" is simply an application of the constructor Id
392 -- to types and (perhaps) dictionaries. This gets the constructor...
393 get_con (Var con) = con
394 get_con (App fun _) = get_con fun
397 Record update is a little harder. Suppose we have the decl:
399 data T = T1 {op1, op2, op3 :: Int}
400 | T2 {op4, op2 :: Int}
403 Then we translate as follows:
409 T1 op1 _ op3 -> T1 op1 op2 op3
410 T2 op4 _ -> T2 op4 op2
411 other -> recUpdError "M.lhs/230"
413 It's important that we use the constructor Ids for T1, T2 etc on the
414 RHSs, and do not generate a Core Con directly, because the constructor
415 might do some argument-evaluation first; and may have to throw away some
419 dsExpr (RecordUpdOut record_expr record_out_ty dicts rbinds)
420 = dsExpr record_expr `thenDs` \ record_expr' ->
422 -- Desugar the rbinds, and generate let-bindings if
423 -- necessary so that we don't lose sharing
424 dsRbinds rbinds $ \ rbinds' ->
426 record_in_ty = coreExprType record_expr'
427 (tycon, in_inst_tys, cons) = getAppDataTyConExpandingDicts record_in_ty
428 (_, out_inst_tys, _) = getAppDataTyConExpandingDicts record_out_ty
429 cons_to_upd = filter has_all_fields cons
431 -- initial_args are passed to every constructor
432 initial_args = map TyArg out_inst_tys ++ map VarArg dicts
434 mk_val_arg (field, arg_id)
435 = case [arg | (f, arg) <- rbinds',
436 field == recordSelectorFieldLabel f] of
437 (arg:args) -> ASSERT(null args)
442 = newSysLocalsDs (dataConArgTys con in_inst_tys) `thenDs` \ arg_ids ->
444 val_args = map mk_val_arg (zipEqual "dsExpr:RecordUpd" (dataConFieldLabels con) arg_ids)
446 returnDs (con, arg_ids, mkGenApp (mkGenApp (Var con) initial_args) val_args)
449 | length cons_to_upd == length cons
452 = newSysLocalDs record_in_ty `thenDs` \ deflt_id ->
453 mkErrorAppDs rEC_UPD_ERROR_ID record_out_ty "" `thenDs` \ err ->
454 returnDs (BindDefault deflt_id err)
456 mapDs mk_alt cons_to_upd `thenDs` \ alts ->
457 mk_default `thenDs` \ deflt ->
459 returnDs (Case record_expr' (AlgAlts alts deflt))
462 has_all_fields :: Id -> Bool
463 has_all_fields con_id
466 con_fields = dataConFieldLabels con_id
467 ok (sel_id, _, _) = recordSelectorFieldLabel sel_id `elem` con_fields
470 Dictionary lambda and application
471 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
472 @DictLam@ and @DictApp@ turn into the regular old things.
473 (OLD:) @DictFunApp@ also becomes a curried application, albeit slightly more
474 complicated; reminiscent of fully-applied constructors.
476 dsExpr (DictLam dictvars expr)
477 = dsExpr expr `thenDs` \ core_expr ->
478 returnDs( mkValLam dictvars core_expr )
482 dsExpr expr@(DictApp e dicts) -- becomes a curried application
486 @SingleDicts@ become @Locals@; @Dicts@ turn into tuples, unless
488 @ClassDictLam dictvars methods expr@ is ``the opposite'':
490 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
493 dsExpr (SingleDict dict) -- just a local
494 = lookupEnvDs dict `thenDs` \ dict' ->
497 dsExpr (Dictionary [] []) -- Empty dictionary represented by void,
498 = returnDs (Var voidId) -- (not, as would happen if we took the next case, by ())
500 dsExpr (Dictionary dicts methods)
501 = mapDs lookupEnvDs (dicts ++ methods) `thenDs` \ d_and_ms' ->
502 returnDs (mkTupleExpr d_and_ms')
504 dsExpr (ClassDictLam dicts methods expr)
505 = dsExpr expr `thenDs` \ core_expr ->
506 case num_of_d_and_ms of
507 0 -> newSysLocalDs voidTy `thenDs` \ new_x ->
508 returnDs (mkValLam [new_x] core_expr)
511 returnDs (mkValLam dicts_and_methods core_expr)
514 newSysLocalDs tuple_ty `thenDs` \ new_x ->
516 Lam (ValBinder new_x)
519 [(tuple_con, dicts_and_methods, core_expr)]
522 num_of_d_and_ms = length dicts + length methods
523 dicts_and_methods = dicts ++ methods
524 tuple_ty = mkTupleTy num_of_d_and_ms (map idType dicts_and_methods)
525 tuple_con = tupleCon num_of_d_and_ms
528 -- HsSyn constructs that just shouldn't be here:
529 dsExpr (HsDo _ _ _) = panic "dsExpr:HsDo"
530 dsExpr (ExplicitList _) = panic "dsExpr:ExplicitList"
531 dsExpr (ExprWithTySig _ _) = panic "dsExpr:ExprWithTySig"
532 dsExpr (ArithSeqIn _) = panic "dsExpr:ArithSeqIn"
535 out_of_range_msg -- ditto
536 = " out of range: [" ++ show minInt ++ ", " ++ show maxInt ++ "]\n"
539 %--------------------------------------------------------------------
541 @(dsApp e [t_1,..,t_n, e_1,..,e_n])@ returns something with the same
544 e t_1 ... t_n e_1 .. e_n
547 We're doing all this so we can saturate constructors (as painlessly as
551 dsApp :: TypecheckedHsExpr -- expr to desugar
552 -> [DsCoreArg] -- accumulated ty/val args: NB:
553 -> DsM CoreExpr -- final result
555 dsApp (HsApp e1 e2) args
556 = dsExpr e2 `thenDs` \ core_e2 ->
557 dsApp e1 (VarArg core_e2 : args)
559 dsApp (OpApp e1 op _ e2) args
560 = dsExpr e1 `thenDs` \ core_e1 ->
561 dsExpr e2 `thenDs` \ core_e2 ->
562 dsApp op (VarArg core_e1 : VarArg core_e2 : args)
564 dsApp (DictApp expr dicts) args
565 = mapDs lookupEnvDs dicts `thenDs` \ core_dicts ->
566 dsApp expr (map (VarArg . Var) core_dicts ++ args)
568 dsApp (TyApp expr tys) args
569 = dsApp expr (map TyArg tys ++ args)
571 -- we might should look out for SectionLs, etc., here, but we don't
573 dsApp anything_else args
574 = dsExpr anything_else `thenDs` \ core_expr ->
575 mkAppDs core_expr args
578 = lookupEnvDs v `thenDs` \ v' ->
583 dsRbinds :: TypecheckedRecordBinds -- The field bindings supplied
584 -> ([(Id, CoreArg)] -> DsM CoreExpr) -- A continuation taking the field
585 -- bindings with atomic rhss
586 -> DsM CoreExpr -- The result of the continuation,
587 -- wrapped in suitable Lets
589 dsRbinds [] continue_with
592 dsRbinds ((sel_id, rhs, pun_flag) : rbinds) continue_with
593 = dsExpr rhs `thenDs` \ rhs' ->
594 dsExprToAtom (VarArg rhs') $ \ rhs_atom ->
595 dsRbinds rbinds $ \ rbinds' ->
596 continue_with ((sel_id, rhs_atom) : rbinds')
600 -- do_unfold ty_env val_env (Lam (TyBinder tyvar) body) (TyArg ty : args)
601 -- = do_unfold (addOneToTyVarEnv ty_env tyvar ty) val_env body args
603 -- do_unfold ty_env val_env (Lam (ValBinder binder) body) (arg@(VarArg expr) : args)
604 -- = dsExprToAtom arg $ \ arg_atom ->
606 -- (addOneToIdEnv val_env binder (argToExpr arg_atom))
609 -- do_unfold ty_env val_env body args
610 -- = -- Clone the remaining part of the template
611 -- uniqSMtoDsM (substCoreExpr val_env ty_env body) `thenDs` \ body' ->
613 -- -- Apply result to remaining arguments
614 -- mkAppDs body' args
617 Basically does the translation given in the Haskell~1.3 report:
621 -> Id -- id for: return m
622 -> Id -- id for: (>>=) m
623 -> Id -- id for: zero m
624 -> Type -- Element type; the whole expression has type (m t)
627 dsDo do_or_lc stmts return_id then_id zero_id result_ty
628 = dsId return_id `thenDs` \ return_ds ->
629 dsId then_id `thenDs` \ then_ds ->
630 dsId zero_id `thenDs` \ zero_ds ->
632 (_, b_ty) = splitAppTy result_ty -- result_ty must be of the form (m b)
635 = dsExpr expr `thenDs` \ expr2 ->
636 mkAppDs return_ds [TyArg b_ty, VarArg expr2]
638 go (GuardStmt expr locn : stmts)
639 = do_expr expr locn `thenDs` \ expr2 ->
640 go stmts `thenDs` \ rest ->
641 mkAppDs zero_ds [TyArg b_ty] `thenDs` \ zero_expr ->
642 returnDs (mkCoreIfThenElse expr2 rest zero_expr)
644 go (ExprStmt expr locn : stmts)
645 = do_expr expr locn `thenDs` \ expr2 ->
647 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
652 go stmts `thenDs` \ rest ->
653 newSysLocalDs a_ty `thenDs` \ ignored_result_id ->
654 mkAppDs then_ds [TyArg a_ty, TyArg b_ty, VarArg expr2,
655 VarArg (mkValLam [ignored_result_id] rest)]
657 go (LetStmt binds : stmts )
658 = dsBinds binds `thenDs` \ binds2 ->
659 go stmts `thenDs` \ rest ->
660 returnDs (mkCoLetsAny binds2 rest)
662 go (BindStmt pat expr locn : stmts)
664 dsExpr expr `thenDs` \ expr2 ->
666 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
667 zero_expr = TyApp (HsVar zero_id) [b_ty]
668 main_match = PatMatch pat (SimpleMatch (
669 HsDoOut do_or_lc stmts return_id then_id zero_id result_ty locn))
671 = if failureFreePat pat
673 else [main_match, PatMatch (WildPat a_ty) (SimpleMatch zero_expr)]
675 matchWrapper DoBindMatch the_matches match_msg
676 `thenDs` \ (binders, matching_code) ->
677 mkAppDs then_ds [TyArg a_ty, TyArg b_ty,
678 VarArg expr2, VarArg (mkValLam binders matching_code)]
683 do_expr expr locn = putSrcLocDs locn (dsExpr expr)
685 match_msg = case do_or_lc of
686 DoStmt -> "`do' statement"
687 ListComp -> "comprehension"