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,
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
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 ( ppShow, ppBesides, ppPStr, ppStr )
49 import TyCon ( isDataTyCon, isNewTyCon )
50 import Type ( splitSigmaTy, splitFunTy, typePrimRep,
51 getAppDataTyConExpandingDicts, maybeAppTyCon, getAppTyCon, applyTy,
52 maybeBoxedPrimType, splitAppTy
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 mk_nil_con ty = mkCon nilDataCon [] [ty] [] -- micro utility...
66 The funny business to do with variables is that we look them up in the
67 Id-to-Id and Id-to-Id maps that the monadery is carrying
68 around; if we get hits, we use the value accordingly.
70 %************************************************************************
72 \subsection[DsExpr-vars-and-cons]{Variables and constructors}
74 %************************************************************************
77 dsExpr :: TypecheckedHsExpr -> DsM CoreExpr
79 dsExpr e@(HsVar var) = dsId var
82 %************************************************************************
84 \subsection[DsExpr-literals]{Literals}
86 %************************************************************************
88 We give int/float literals type Integer and Rational, respectively.
89 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
92 ToDo: put in range checks for when converting "i"
93 (or should that be in the typechecker?)
95 For numeric literals, we try to detect there use at a standard type
96 (Int, Float, etc.) are directly put in the right constructor.
97 [NB: down with the @App@ conversion.]
98 Otherwise, we punt, putting in a "NoRep" Core literal (where the
99 representation decisions are delayed)...
101 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
104 dsExpr (HsLitOut (HsString s) _)
106 = returnDs (mk_nil_con charTy)
110 the_char = mkCon charDataCon [] [] [LitArg (MachChar (_HEAD_ s))]
111 the_nil = mk_nil_con charTy
113 mkConDs consDataCon [TyArg charTy, VarArg the_char, VarArg the_nil]
115 -- "_" => build (\ c n -> c 'c' n) -- LATER
117 -- "str" ==> build (\ c n -> foldr charTy T c n "str")
120 dsExpr (HsLitOut (HsString str) _)
121 = newTyVarsDs [alphaTyVar] `thenDs` \ [new_tyvar] ->
123 new_ty = mkTyVarTy new_tyvar
126 charTy `mkFunTy` (new_ty `mkFunTy` new_ty),
128 mkForallTy [alphaTyVar]
129 ((charTy `mkFunTy` (alphaTy `mkFunTy` alphaTy))
130 `mkFunTy` (alphaTy `mkFunTy` alphaTy))
131 ] `thenDs` \ [c,n,g] ->
132 returnDs (mkBuild charTy new_tyvar c n g (
134 (CoTyApp (CoTyApp (Var foldrId) charTy) new_ty) *** ensure non-prim type ***
135 [VarArg c,VarArg n,LitArg (NoRepStr str)]))
138 -- otherwise, leave it as a NoRepStr;
139 -- the Core-to-STG pass will wrap it in an application of "unpackCStringId".
141 dsExpr (HsLitOut (HsString str) _)
142 = returnDs (Lit (NoRepStr str))
144 dsExpr (HsLitOut (HsLitLit s) ty)
145 = returnDs ( mkCon data_con [] [] [LitArg (MachLitLit s kind)] )
148 = case (maybeBoxedPrimType ty) of
149 Just (boxing_data_con, prim_ty)
150 -> (boxing_data_con, typePrimRep prim_ty)
152 -> pprError "ERROR: ``literal-literal'' not a single-constructor type: "
153 (ppBesides [ppPStr s, ppStr "; type: ", ppr PprDebug ty])
155 dsExpr (HsLitOut (HsInt i) ty)
156 = returnDs (Lit (NoRepInteger i ty))
158 dsExpr (HsLitOut (HsFrac r) ty)
159 = returnDs (Lit (NoRepRational r ty))
161 -- others where we know what to do:
163 dsExpr (HsLitOut (HsIntPrim i) _)
164 = if (i >= toInteger minInt && i <= toInteger maxInt) then
165 returnDs (Lit (mkMachInt i))
167 error ("ERROR: Int constant " ++ show i ++ out_of_range_msg)
169 dsExpr (HsLitOut (HsFloatPrim f) _)
170 = returnDs (Lit (MachFloat f))
171 -- ToDo: range checking needed!
173 dsExpr (HsLitOut (HsDoublePrim d) _)
174 = returnDs (Lit (MachDouble d))
175 -- ToDo: range checking needed!
177 dsExpr (HsLitOut (HsChar c) _)
178 = returnDs ( mkCon charDataCon [] [] [LitArg (MachChar c)] )
180 dsExpr (HsLitOut (HsCharPrim c) _)
181 = returnDs (Lit (MachChar c))
183 dsExpr (HsLitOut (HsStringPrim s) _)
184 = returnDs (Lit (MachStr s))
186 -- end of literals magic. --
188 dsExpr expr@(HsLam a_Match)
189 = matchWrapper LambdaMatch [a_Match] "lambda" `thenDs` \ (binders, matching_code) ->
190 returnDs ( mkValLam binders matching_code )
192 dsExpr expr@(HsApp e1 e2) = dsApp expr []
193 dsExpr expr@(OpApp e1 op _ e2) = dsApp expr []
196 Operator sections. At first it looks as if we can convert
205 But no! expr might be a redex, and we can lose laziness badly this
210 for example. So we convert instead to
212 let y = expr in \x -> op y x
214 If \tr{expr} is actually just a variable, say, then the simplifier
218 dsExpr (SectionL expr op)
219 = dsExpr op `thenDs` \ core_op ->
220 dsExpr expr `thenDs` \ core_expr ->
221 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
223 -- for the type of x, we need the type of op's 2nd argument
225 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
226 case (splitFunTy tau_ty) of {
227 ((_:arg2_ty:_), _) -> arg2_ty;
228 _ -> panic "dsExpr:SectionL:arg 2 ty" }}
230 newSysLocalDs x_ty `thenDs` \ x_id ->
231 returnDs (mkValLam [x_id] (core_op `App` y_atom `App` VarArg x_id))
233 -- dsExpr (SectionR op expr) -- \ x -> op x expr
234 dsExpr (SectionR op expr)
235 = dsExpr op `thenDs` \ core_op ->
236 dsExpr expr `thenDs` \ core_expr ->
237 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
239 -- for the type of x, we need the type of op's 1st argument
241 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
242 case (splitFunTy tau_ty) of {
243 ((arg1_ty:_), _) -> arg1_ty;
244 _ -> panic "dsExpr:SectionR:arg 1 ty" }}
246 newSysLocalDs x_ty `thenDs` \ x_id ->
247 returnDs (mkValLam [x_id] (core_op `App` VarArg x_id `App` y_atom))
249 dsExpr (CCall label args may_gc is_asm result_ty)
250 = mapDs dsExpr args `thenDs` \ core_args ->
251 dsCCall label core_args may_gc is_asm result_ty
252 -- dsCCall does all the unboxification, etc.
254 dsExpr (HsSCC cc expr)
255 = dsExpr expr `thenDs` \ core_expr ->
256 getModuleAndGroupDs `thenDs` \ (mod_name, group_name) ->
257 returnDs ( SCC (mkUserCC cc mod_name group_name) core_expr)
259 dsExpr expr@(HsCase discrim matches src_loc)
260 = putSrcLocDs src_loc $
261 dsExpr discrim `thenDs` \ core_discrim ->
262 matchWrapper CaseMatch matches "case" `thenDs` \ ([discrim_var], matching_code) ->
263 returnDs ( mkCoLetAny (NonRec discrim_var core_discrim) matching_code )
265 dsExpr (HsLet binds expr)
266 = dsBinds binds `thenDs` \ core_binds ->
267 dsExpr expr `thenDs` \ core_expr ->
268 returnDs ( mkCoLetsAny core_binds core_expr )
270 dsExpr (HsDoOut do_or_lc stmts return_id then_id zero_id result_ty src_loc)
271 | maybeToBool maybe_list_comp -- Special case for list comprehensions
272 = putSrcLocDs src_loc $
273 dsListComp stmts elt_ty
276 = putSrcLocDs src_loc $
277 dsDo do_or_lc stmts return_id then_id zero_id result_ty
279 maybe_list_comp = case maybeAppTyCon result_ty of
280 Just (tycon, [elt_ty]) | tycon == listTyCon
283 Just elt_ty = maybe_list_comp
285 dsExpr (HsIf guard_expr then_expr else_expr src_loc)
286 = putSrcLocDs src_loc $
287 dsExpr guard_expr `thenDs` \ core_guard ->
288 dsExpr then_expr `thenDs` \ core_then ->
289 dsExpr else_expr `thenDs` \ core_else ->
290 returnDs (mkCoreIfThenElse core_guard core_then core_else)
294 Type lambda and application
295 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
297 dsExpr (TyLam tyvars expr)
298 = dsExpr expr `thenDs` \ core_expr ->
299 returnDs (mkTyLam tyvars core_expr)
301 dsExpr expr@(TyApp e tys) = dsApp expr []
305 Various data construction things
306 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308 dsExpr (ExplicitListOut ty xs)
310 [] -> returnDs (mk_nil_con ty)
312 dsExpr y `thenDs` \ core_hd ->
313 dsExpr (ExplicitListOut ty ys) `thenDs` \ core_tl ->
314 mkConDs consDataCon [TyArg ty, VarArg core_hd, VarArg core_tl]
316 dsExpr (ExplicitTuple expr_list)
317 = mapDs dsExpr expr_list `thenDs` \ core_exprs ->
318 mkConDs (tupleCon (length expr_list))
319 (map (TyArg . coreExprType) core_exprs ++ map VarArg core_exprs)
321 dsExpr (ArithSeqOut expr (From from))
322 = dsExpr expr `thenDs` \ expr2 ->
323 dsExpr from `thenDs` \ from2 ->
324 mkAppDs expr2 [VarArg from2]
326 dsExpr (ArithSeqOut expr (FromTo from two))
327 = dsExpr expr `thenDs` \ expr2 ->
328 dsExpr from `thenDs` \ from2 ->
329 dsExpr two `thenDs` \ two2 ->
330 mkAppDs expr2 [VarArg from2, VarArg two2]
332 dsExpr (ArithSeqOut expr (FromThen from thn))
333 = dsExpr expr `thenDs` \ expr2 ->
334 dsExpr from `thenDs` \ from2 ->
335 dsExpr thn `thenDs` \ thn2 ->
336 mkAppDs expr2 [VarArg from2, VarArg thn2]
338 dsExpr (ArithSeqOut expr (FromThenTo from thn two))
339 = dsExpr expr `thenDs` \ expr2 ->
340 dsExpr from `thenDs` \ from2 ->
341 dsExpr thn `thenDs` \ thn2 ->
342 dsExpr two `thenDs` \ two2 ->
343 mkAppDs expr2 [VarArg from2, VarArg thn2, VarArg two2]
346 Record construction and update
347 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
348 For record construction we do this (assuming T has three arguments)
352 let err = /\a -> recConErr a
353 T (recConErr t1 "M.lhs/230/op1")
355 (recConErr t1 "M.lhs/230/op3")
357 recConErr then converts its arugment string into a proper message
358 before printing it as
360 M.lhs, line 230: missing field op1 was evaluated
364 dsExpr (RecordCon con_expr rbinds)
365 = dsExpr con_expr `thenDs` \ con_expr' ->
367 con_id = get_con con_expr'
368 (arg_tys, _) = splitFunTy (coreExprType con_expr')
371 = case [rhs | (sel_id,rhs,_) <- rbinds,
372 lbl == recordSelectorFieldLabel sel_id] of
373 (rhs:rhss) -> ASSERT( null rhss )
375 [] -> mkErrorAppDs rEC_CON_ERROR_ID arg_ty (showForErr lbl)
377 mapDs mk_arg (zipEqual "dsExpr:RecordCon" arg_tys (dataConFieldLabels con_id)) `thenDs` \ con_args ->
378 mkAppDs con_expr' (map VarArg con_args)
380 -- "con_expr'" is simply an application of the constructor Id
381 -- to types and (perhaps) dictionaries. This gets the constructor...
382 get_con (Var con) = con
383 get_con (App fun _) = get_con fun
386 Record update is a little harder. Suppose we have the decl:
388 data T = T1 {op1, op2, op3 :: Int}
389 | T2 {op4, op2 :: Int}
392 Then we translate as follows:
398 T1 op1 _ op3 -> T1 op1 op2 op3
399 T2 op4 _ -> T2 op4 op2
400 other -> recUpdError "M.lhs/230"
402 It's important that we use the constructor Ids for T1, T2 etc on the
403 RHSs, and do not generate a Core Con directly, because the constructor
404 might do some argument-evaluation first; and may have to throw away some
408 dsExpr (RecordUpdOut record_expr dicts rbinds)
409 = dsExpr record_expr `thenDs` \ record_expr' ->
411 -- Desugar the rbinds, and generate let-bindings if
412 -- necessary so that we don't lose sharing
413 dsRbinds rbinds $ \ rbinds' ->
415 record_ty = coreExprType record_expr'
416 (tycon, inst_tys, cons) = --trace "DsExpr.getAppDataTyConExpandingDicts" $
417 getAppDataTyConExpandingDicts record_ty
418 cons_to_upd = filter has_all_fields cons
420 -- initial_args are passed to every constructor
421 initial_args = map TyArg inst_tys ++ map VarArg dicts
423 mk_val_arg (field, arg_id)
424 = case [arg | (f, arg) <- rbinds',
425 field == recordSelectorFieldLabel f] of
426 (arg:args) -> ASSERT(null args)
431 = newSysLocalsDs (dataConArgTys con inst_tys) `thenDs` \ arg_ids ->
433 val_args = map mk_val_arg (zipEqual "dsExpr:RecordUpd" (dataConFieldLabels con) arg_ids)
435 returnDs (con, arg_ids, mkGenApp (mkGenApp (Var con) initial_args) val_args)
438 | length cons_to_upd == length cons
441 = newSysLocalDs record_ty `thenDs` \ deflt_id ->
442 mkErrorAppDs rEC_UPD_ERROR_ID record_ty "" `thenDs` \ err ->
443 returnDs (BindDefault deflt_id err)
445 mapDs mk_alt cons_to_upd `thenDs` \ alts ->
446 mk_default `thenDs` \ deflt ->
448 returnDs (Case record_expr' (AlgAlts alts deflt))
451 has_all_fields :: Id -> Bool
452 has_all_fields con_id
455 con_fields = dataConFieldLabels con_id
456 ok (sel_id, _, _) = recordSelectorFieldLabel sel_id `elem` con_fields
459 Dictionary lambda and application
460 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
461 @DictLam@ and @DictApp@ turn into the regular old things.
462 (OLD:) @DictFunApp@ also becomes a curried application, albeit slightly more
463 complicated; reminiscent of fully-applied constructors.
465 dsExpr (DictLam dictvars expr)
466 = dsExpr expr `thenDs` \ core_expr ->
467 returnDs( mkValLam dictvars core_expr )
471 dsExpr expr@(DictApp e dicts) -- becomes a curried application
475 @SingleDicts@ become @Locals@; @Dicts@ turn into tuples, unless
477 @ClassDictLam dictvars methods expr@ is ``the opposite'':
479 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
482 dsExpr (SingleDict dict) -- just a local
483 = lookupEnvWithDefaultDs dict (Var dict)
485 dsExpr (Dictionary dicts methods)
486 = -- hey, these things may have been substituted away...
487 zipWithDs lookupEnvWithDefaultDs
488 dicts_and_methods dicts_and_methods_exprs
489 `thenDs` \ core_d_and_ms ->
491 (case num_of_d_and_ms of
492 0 -> returnDs (Var voidId)
494 1 -> returnDs (head core_d_and_ms) -- just a single Id
497 mkConDs (tupleCon num_of_d_and_ms)
498 (map (TyArg . coreExprType) core_d_and_ms ++ map VarArg core_d_and_ms)
501 dicts_and_methods = dicts ++ methods
502 dicts_and_methods_exprs = map Var dicts_and_methods
503 num_of_d_and_ms = length dicts_and_methods
505 dsExpr (ClassDictLam dicts methods expr)
506 = dsExpr expr `thenDs` \ core_expr ->
507 case num_of_d_and_ms of
508 0 -> newSysLocalDs voidTy `thenDs` \ new_x ->
509 returnDs (mkValLam [new_x] core_expr)
512 returnDs (mkValLam dicts_and_methods core_expr)
515 newSysLocalDs tuple_ty `thenDs` \ new_x ->
517 Lam (ValBinder new_x)
520 [(tuple_con, dicts_and_methods, core_expr)]
523 num_of_d_and_ms = length dicts + length methods
524 dicts_and_methods = dicts ++ methods
525 tuple_ty = mkTupleTy num_of_d_and_ms (map idType dicts_and_methods)
526 tuple_con = tupleCon num_of_d_and_ms
529 -- HsSyn constructs that just shouldn't be here:
530 dsExpr (HsDo _ _ _) = panic "dsExpr:HsDo"
531 dsExpr (ExplicitList _) = panic "dsExpr:ExplicitList"
532 dsExpr (ExprWithTySig _ _) = panic "dsExpr:ExprWithTySig"
533 dsExpr (ArithSeqIn _) = panic "dsExpr:ArithSeqIn"
536 out_of_range_msg -- ditto
537 = " out of range: [" ++ show minInt ++ ", " ++ show maxInt ++ "]\n"
540 %--------------------------------------------------------------------
542 @(dsApp e [t_1,..,t_n, e_1,..,e_n])@ returns something with the same
545 e t_1 ... t_n e_1 .. e_n
548 We're doing all this so we can saturate constructors (as painlessly as
552 dsApp :: TypecheckedHsExpr -- expr to desugar
553 -> [DsCoreArg] -- accumulated ty/val args: NB:
554 -> DsM CoreExpr -- final result
556 dsApp (HsApp e1 e2) args
557 = dsExpr e2 `thenDs` \ core_e2 ->
558 dsApp e1 (VarArg core_e2 : args)
560 dsApp (OpApp e1 op _ e2) args
561 = dsExpr e1 `thenDs` \ core_e1 ->
562 dsExpr e2 `thenDs` \ core_e2 ->
563 dsApp op (VarArg core_e1 : VarArg core_e2 : args)
565 dsApp (DictApp expr dicts) args
566 = -- now, those dicts may have been substituted away...
567 zipWithDs lookupEnvWithDefaultDs dicts (map Var dicts)
568 `thenDs` \ core_dicts ->
569 dsApp expr (map VarArg core_dicts ++ args)
571 dsApp (TyApp expr tys) args
572 = dsApp expr (map TyArg tys ++ args)
574 -- we might should look out for SectionLs, etc., here, but we don't
576 dsApp anything_else args
577 = dsExpr anything_else `thenDs` \ core_expr ->
578 mkAppDs core_expr args
581 = lookupEnvDs v `thenDs` \ maybe_expr ->
582 returnDs (case maybe_expr of { Nothing -> Var v; Just expr -> expr })
586 dsRbinds :: TypecheckedRecordBinds -- The field bindings supplied
587 -> ([(Id, CoreArg)] -> DsM CoreExpr) -- A continuation taking the field
588 -- bindings with atomic rhss
589 -> DsM CoreExpr -- The result of the continuation,
590 -- wrapped in suitable Lets
592 dsRbinds [] continue_with
595 dsRbinds ((sel_id, rhs, pun_flag) : rbinds) continue_with
596 = dsExpr rhs `thenDs` \ rhs' ->
597 dsExprToAtom (VarArg rhs') $ \ rhs_atom ->
598 dsRbinds rbinds $ \ rbinds' ->
599 continue_with ((sel_id, rhs_atom) : rbinds')
603 -- do_unfold ty_env val_env (Lam (TyBinder tyvar) body) (TyArg ty : args)
604 -- = do_unfold (addOneToTyVarEnv ty_env tyvar ty) val_env body args
606 -- do_unfold ty_env val_env (Lam (ValBinder binder) body) (arg@(VarArg expr) : args)
607 -- = dsExprToAtom arg $ \ arg_atom ->
609 -- (addOneToIdEnv val_env binder (argToExpr arg_atom))
612 -- do_unfold ty_env val_env body args
613 -- = -- Clone the remaining part of the template
614 -- uniqSMtoDsM (substCoreExpr val_env ty_env body) `thenDs` \ body' ->
616 -- -- Apply result to remaining arguments
617 -- mkAppDs body' args
620 Basically does the translation given in the Haskell~1.3 report:
624 -> Id -- id for: return m
625 -> Id -- id for: (>>=) m
626 -> Id -- id for: zero m
627 -> Type -- Element type; the whole expression has type (m t)
630 dsDo do_or_lc stmts return_id then_id zero_id result_ty
631 = dsId return_id `thenDs` \ return_ds ->
632 dsId then_id `thenDs` \ then_ds ->
633 dsId zero_id `thenDs` \ zero_ds ->
635 (_, b_ty) = splitAppTy result_ty -- result_ty must be of the form (m b)
638 = dsExpr expr `thenDs` \ expr2 ->
639 mkAppDs return_ds [TyArg b_ty, VarArg expr2]
641 go (GuardStmt expr locn : stmts)
642 = do_expr expr locn `thenDs` \ expr2 ->
643 go stmts `thenDs` \ rest ->
644 mkAppDs zero_ds [TyArg b_ty] `thenDs` \ zero_expr ->
645 returnDs (mkCoreIfThenElse expr2 rest zero_expr)
647 go (ExprStmt expr locn : stmts)
648 = do_expr expr locn `thenDs` \ expr2 ->
650 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
655 go stmts `thenDs` \ rest ->
656 newSysLocalDs a_ty `thenDs` \ ignored_result_id ->
657 mkAppDs then_ds [TyArg a_ty, TyArg b_ty, VarArg expr2,
658 VarArg (mkValLam [ignored_result_id] rest)]
660 go (LetStmt binds : stmts )
661 = dsBinds binds `thenDs` \ binds2 ->
662 go stmts `thenDs` \ rest ->
663 returnDs (mkCoLetsAny binds2 rest)
665 go (BindStmt pat expr locn : stmts)
667 dsExpr expr `thenDs` \ expr2 ->
669 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
670 zero_expr = TyApp (HsVar zero_id) [b_ty]
671 main_match = PatMatch pat (SimpleMatch (
672 HsDoOut do_or_lc stmts return_id then_id zero_id result_ty locn))
674 = if failureFreePat pat
676 else [main_match, PatMatch (WildPat a_ty) (SimpleMatch zero_expr)]
678 matchWrapper DoBindMatch the_matches match_msg
679 `thenDs` \ (binders, matching_code) ->
680 mkAppDs then_ds [TyArg a_ty, TyArg b_ty,
681 VarArg expr2, VarArg (mkValLam binders matching_code)]
686 do_expr expr locn = putSrcLocDs locn (dsExpr expr)
688 match_msg = case do_or_lc of
689 DoStmt -> "`do' statement"
690 ListComp -> "comprehension"