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 Outputable ( PprStyle(..), Outputable(..) )
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 Type ( splitSigmaTy, splitFunTy, typePrimRep,
50 getAppDataTyConExpandingDicts, maybeAppTyCon, getAppTyCon, applyTy,
51 maybeBoxedPrimType, splitAppTy, SYN_IE(Type)
53 import TysPrim ( voidTy )
54 import TysWiredIn ( mkTupleTy, tupleCon, nilDataCon, consDataCon, listTyCon,
57 import TyVar ( nullTyVarEnv, addOneToTyVarEnv, GenTyVar{-instance Eq-} )
58 import Usage ( SYN_IE(UVar) )
59 import Maybes ( maybeToBool )
60 import Util ( zipEqual, pprError, panic, assertPanic )
62 mk_nil_con ty = mkCon nilDataCon [] [ty] [] -- micro utility...
65 The funny business to do with variables is that we look them up in the
66 Id-to-Id and Id-to-Id maps that the monadery is carrying
67 around; if we get hits, we use the value accordingly.
69 %************************************************************************
71 \subsection[DsExpr-vars-and-cons]{Variables and constructors}
73 %************************************************************************
76 dsExpr :: TypecheckedHsExpr -> DsM CoreExpr
78 dsExpr e@(HsVar var) = dsId var
81 %************************************************************************
83 \subsection[DsExpr-literals]{Literals}
85 %************************************************************************
87 We give int/float literals type Integer and Rational, respectively.
88 The typechecker will (presumably) have put \tr{from{Integer,Rational}s}
91 ToDo: put in range checks for when converting "i"
92 (or should that be in the typechecker?)
94 For numeric literals, we try to detect there use at a standard type
95 (Int, Float, etc.) are directly put in the right constructor.
96 [NB: down with the @App@ conversion.]
97 Otherwise, we punt, putting in a "NoRep" Core literal (where the
98 representation decisions are delayed)...
100 See also below where we look for @DictApps@ for \tr{plusInt}, etc.
103 dsExpr (HsLitOut (HsString s) _)
105 = returnDs (mk_nil_con charTy)
109 the_char = mkCon charDataCon [] [] [LitArg (MachChar (_HEAD_ s))]
110 the_nil = mk_nil_con charTy
112 mkConDs consDataCon [TyArg charTy, VarArg the_char, VarArg the_nil]
114 -- "_" => build (\ c n -> c 'c' n) -- LATER
116 -- "str" ==> build (\ c n -> foldr charTy T c n "str")
119 dsExpr (HsLitOut (HsString str) _)
120 = newTyVarsDs [alphaTyVar] `thenDs` \ [new_tyvar] ->
122 new_ty = mkTyVarTy new_tyvar
125 charTy `mkFunTy` (new_ty `mkFunTy` new_ty),
127 mkForallTy [alphaTyVar]
128 ((charTy `mkFunTy` (alphaTy `mkFunTy` alphaTy))
129 `mkFunTy` (alphaTy `mkFunTy` alphaTy))
130 ] `thenDs` \ [c,n,g] ->
131 returnDs (mkBuild charTy new_tyvar c n g (
133 (CoTyApp (CoTyApp (Var foldrId) charTy) new_ty) *** ensure non-prim type ***
134 [VarArg c,VarArg n,LitArg (NoRepStr str)]))
137 -- otherwise, leave it as a NoRepStr;
138 -- the Core-to-STG pass will wrap it in an application of "unpackCStringId".
140 dsExpr (HsLitOut (HsString str) _)
141 = returnDs (Lit (NoRepStr str))
143 dsExpr (HsLitOut (HsLitLit s) ty)
144 = returnDs ( mkCon data_con [] [] [LitArg (MachLitLit s kind)] )
147 = case (maybeBoxedPrimType ty) of
148 Just (boxing_data_con, prim_ty)
149 -> (boxing_data_con, typePrimRep prim_ty)
151 -> pprError "ERROR: ``literal-literal'' not a single-constructor type: "
152 (hcat [ptext s, text "; type: ", ppr PprDebug ty])
154 dsExpr (HsLitOut (HsInt i) ty)
155 = returnDs (Lit (NoRepInteger i ty))
157 dsExpr (HsLitOut (HsFrac r) ty)
158 = returnDs (Lit (NoRepRational r ty))
160 -- others where we know what to do:
162 dsExpr (HsLitOut (HsIntPrim i) _)
163 = if (i >= toInteger minInt && i <= toInteger maxInt) then
164 returnDs (Lit (mkMachInt i))
166 error ("ERROR: Int constant " ++ show i ++ out_of_range_msg)
168 dsExpr (HsLitOut (HsFloatPrim f) _)
169 = returnDs (Lit (MachFloat f))
170 -- ToDo: range checking needed!
172 dsExpr (HsLitOut (HsDoublePrim d) _)
173 = returnDs (Lit (MachDouble d))
174 -- ToDo: range checking needed!
176 dsExpr (HsLitOut (HsChar c) _)
177 = returnDs ( mkCon charDataCon [] [] [LitArg (MachChar c)] )
179 dsExpr (HsLitOut (HsCharPrim c) _)
180 = returnDs (Lit (MachChar c))
182 dsExpr (HsLitOut (HsStringPrim s) _)
183 = returnDs (Lit (MachStr s))
185 -- end of literals magic. --
187 dsExpr expr@(HsLam a_Match)
188 = matchWrapper LambdaMatch [a_Match] "lambda" `thenDs` \ (binders, matching_code) ->
189 returnDs ( mkValLam binders matching_code )
191 dsExpr expr@(HsApp e1 e2) = dsApp expr []
192 dsExpr expr@(OpApp e1 op _ e2) = dsApp expr []
195 Operator sections. At first it looks as if we can convert
204 But no! expr might be a redex, and we can lose laziness badly this
209 for example. So we convert instead to
211 let y = expr in \x -> op y x
213 If \tr{expr} is actually just a variable, say, then the simplifier
217 dsExpr (SectionL expr op)
218 = dsExpr op `thenDs` \ core_op ->
219 dsExpr expr `thenDs` \ core_expr ->
220 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
222 -- for the type of x, we need the type of op's 2nd argument
224 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
225 case (splitFunTy tau_ty) of {
226 ((_:arg2_ty:_), _) -> arg2_ty;
227 _ -> panic "dsExpr:SectionL:arg 2 ty" }}
229 newSysLocalDs x_ty `thenDs` \ x_id ->
230 returnDs (mkValLam [x_id] (core_op `App` y_atom `App` VarArg x_id))
232 -- dsExpr (SectionR op expr) -- \ x -> op x expr
233 dsExpr (SectionR op expr)
234 = dsExpr op `thenDs` \ core_op ->
235 dsExpr expr `thenDs` \ core_expr ->
236 dsExprToAtom (VarArg core_expr) $ \ y_atom ->
238 -- for the type of x, we need the type of op's 1st argument
240 x_ty = case (splitSigmaTy (coreExprType core_op)) of { (_, _, tau_ty) ->
241 case (splitFunTy tau_ty) of {
242 ((arg1_ty:_), _) -> arg1_ty;
243 _ -> panic "dsExpr:SectionR:arg 1 ty" }}
245 newSysLocalDs x_ty `thenDs` \ x_id ->
246 returnDs (mkValLam [x_id] (core_op `App` VarArg x_id `App` y_atom))
248 dsExpr (CCall label args may_gc is_asm result_ty)
249 = mapDs dsExpr args `thenDs` \ core_args ->
250 dsCCall label core_args may_gc is_asm result_ty
251 -- dsCCall does all the unboxification, etc.
253 dsExpr (HsSCC cc expr)
254 = dsExpr expr `thenDs` \ core_expr ->
255 getModuleAndGroupDs `thenDs` \ (mod_name, group_name) ->
256 returnDs ( SCC (mkUserCC cc mod_name group_name) core_expr)
258 dsExpr expr@(HsCase discrim matches src_loc)
259 = putSrcLocDs src_loc $
260 dsExpr discrim `thenDs` \ core_discrim ->
261 matchWrapper CaseMatch matches "case" `thenDs` \ ([discrim_var], matching_code) ->
262 returnDs ( mkCoLetAny (NonRec discrim_var core_discrim) matching_code )
264 dsExpr (HsLet binds expr)
265 = dsBinds binds `thenDs` \ core_binds ->
266 dsExpr expr `thenDs` \ core_expr ->
267 returnDs ( mkCoLetsAny core_binds core_expr )
269 dsExpr (HsDoOut do_or_lc stmts return_id then_id zero_id result_ty src_loc)
270 | maybeToBool maybe_list_comp
271 = -- 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
280 = case (do_or_lc, maybeAppTyCon result_ty) of
281 (ListComp, Just (tycon, [elt_ty]))
285 -- We need the ListComp form to use deListComp (rather than the "do" form)
286 -- because the "return" in a do block is a call to "PrelBase.return", and
287 -- not a ReturnStmt. Only the ListComp form has ReturnStmts
289 Just elt_ty = maybe_list_comp
291 dsExpr (HsIf guard_expr then_expr else_expr src_loc)
292 = putSrcLocDs src_loc $
293 dsExpr guard_expr `thenDs` \ core_guard ->
294 dsExpr then_expr `thenDs` \ core_then ->
295 dsExpr else_expr `thenDs` \ core_else ->
296 returnDs (mkCoreIfThenElse core_guard core_then core_else)
300 Type lambda and application
301 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
303 dsExpr (TyLam tyvars expr)
304 = dsExpr expr `thenDs` \ core_expr ->
305 returnDs (mkTyLam tyvars core_expr)
307 dsExpr expr@(TyApp e tys) = dsApp expr []
311 Various data construction things
312 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314 dsExpr (ExplicitListOut ty xs)
316 [] -> returnDs (mk_nil_con ty)
318 dsExpr y `thenDs` \ core_hd ->
319 dsExpr (ExplicitListOut ty ys) `thenDs` \ core_tl ->
320 mkConDs consDataCon [TyArg ty, VarArg core_hd, VarArg core_tl]
322 dsExpr (ExplicitTuple expr_list)
323 = mapDs dsExpr expr_list `thenDs` \ core_exprs ->
324 mkConDs (tupleCon (length expr_list))
325 (map (TyArg . coreExprType) core_exprs ++ map VarArg core_exprs)
327 dsExpr (ArithSeqOut expr (From from))
328 = dsExpr expr `thenDs` \ expr2 ->
329 dsExpr from `thenDs` \ from2 ->
330 mkAppDs expr2 [VarArg from2]
332 dsExpr (ArithSeqOut expr (FromTo from two))
333 = dsExpr expr `thenDs` \ expr2 ->
334 dsExpr from `thenDs` \ from2 ->
335 dsExpr two `thenDs` \ two2 ->
336 mkAppDs expr2 [VarArg from2, VarArg two2]
338 dsExpr (ArithSeqOut expr (FromThen from thn))
339 = dsExpr expr `thenDs` \ expr2 ->
340 dsExpr from `thenDs` \ from2 ->
341 dsExpr thn `thenDs` \ thn2 ->
342 mkAppDs expr2 [VarArg from2, VarArg thn2]
344 dsExpr (ArithSeqOut expr (FromThenTo from thn two))
345 = dsExpr expr `thenDs` \ expr2 ->
346 dsExpr from `thenDs` \ from2 ->
347 dsExpr thn `thenDs` \ thn2 ->
348 dsExpr two `thenDs` \ two2 ->
349 mkAppDs expr2 [VarArg from2, VarArg thn2, VarArg two2]
352 Record construction and update
353 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354 For record construction we do this (assuming T has three arguments)
358 let err = /\a -> recConErr a
359 T (recConErr t1 "M.lhs/230/op1")
361 (recConErr t1 "M.lhs/230/op3")
363 recConErr then converts its arugment string into a proper message
364 before printing it as
366 M.lhs, line 230: missing field op1 was evaluated
370 dsExpr (RecordCon con_expr rbinds)
371 = dsExpr con_expr `thenDs` \ con_expr' ->
373 con_id = get_con con_expr'
374 (arg_tys, _) = splitFunTy (coreExprType con_expr')
377 = case [rhs | (sel_id,rhs,_) <- rbinds,
378 lbl == recordSelectorFieldLabel sel_id] of
379 (rhs:rhss) -> ASSERT( null rhss )
381 [] -> mkErrorAppDs rEC_CON_ERROR_ID arg_ty (showForErr lbl)
383 mapDs mk_arg (zipEqual "dsExpr:RecordCon" arg_tys (dataConFieldLabels con_id)) `thenDs` \ con_args ->
384 mkAppDs con_expr' (map VarArg con_args)
386 -- "con_expr'" is simply an application of the constructor Id
387 -- to types and (perhaps) dictionaries. This gets the constructor...
388 get_con (Var con) = con
389 get_con (App fun _) = get_con fun
392 Record update is a little harder. Suppose we have the decl:
394 data T = T1 {op1, op2, op3 :: Int}
395 | T2 {op4, op2 :: Int}
398 Then we translate as follows:
404 T1 op1 _ op3 -> T1 op1 op2 op3
405 T2 op4 _ -> T2 op4 op2
406 other -> recUpdError "M.lhs/230"
408 It's important that we use the constructor Ids for T1, T2 etc on the
409 RHSs, and do not generate a Core Con directly, because the constructor
410 might do some argument-evaluation first; and may have to throw away some
414 dsExpr (RecordUpdOut record_expr record_out_ty dicts rbinds)
415 = dsExpr record_expr `thenDs` \ record_expr' ->
417 -- Desugar the rbinds, and generate let-bindings if
418 -- necessary so that we don't lose sharing
419 dsRbinds rbinds $ \ rbinds' ->
421 record_in_ty = coreExprType record_expr'
422 (tycon, in_inst_tys, cons) = getAppDataTyConExpandingDicts record_in_ty
423 (_, out_inst_tys, _) = getAppDataTyConExpandingDicts record_out_ty
424 cons_to_upd = filter has_all_fields cons
426 -- initial_args are passed to every constructor
427 initial_args = map TyArg out_inst_tys ++ map VarArg dicts
429 mk_val_arg (field, arg_id)
430 = case [arg | (f, arg) <- rbinds',
431 field == recordSelectorFieldLabel f] of
432 (arg:args) -> ASSERT(null args)
437 = newSysLocalsDs (dataConArgTys con in_inst_tys) `thenDs` \ arg_ids ->
439 val_args = map mk_val_arg (zipEqual "dsExpr:RecordUpd" (dataConFieldLabels con) arg_ids)
441 returnDs (con, arg_ids, mkGenApp (mkGenApp (Var con) initial_args) val_args)
444 | length cons_to_upd == length cons
447 = newSysLocalDs record_in_ty `thenDs` \ deflt_id ->
448 mkErrorAppDs rEC_UPD_ERROR_ID record_out_ty "" `thenDs` \ err ->
449 returnDs (BindDefault deflt_id err)
451 mapDs mk_alt cons_to_upd `thenDs` \ alts ->
452 mk_default `thenDs` \ deflt ->
454 returnDs (Case record_expr' (AlgAlts alts deflt))
457 has_all_fields :: Id -> Bool
458 has_all_fields con_id
461 con_fields = dataConFieldLabels con_id
462 ok (sel_id, _, _) = recordSelectorFieldLabel sel_id `elem` con_fields
465 Dictionary lambda and application
466 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
467 @DictLam@ and @DictApp@ turn into the regular old things.
468 (OLD:) @DictFunApp@ also becomes a curried application, albeit slightly more
469 complicated; reminiscent of fully-applied constructors.
471 dsExpr (DictLam dictvars expr)
472 = dsExpr expr `thenDs` \ core_expr ->
473 returnDs( mkValLam dictvars core_expr )
477 dsExpr expr@(DictApp e dicts) -- becomes a curried application
481 @SingleDicts@ become @Locals@; @Dicts@ turn into tuples, unless
483 @ClassDictLam dictvars methods expr@ is ``the opposite'':
485 \ x -> case x of ( dictvars-and-methods-tuple ) -> expr
488 dsExpr (SingleDict dict) -- just a local
489 = lookupEnvDs dict `thenDs` \ dict' ->
492 dsExpr (Dictionary [] []) -- Empty dictionary represented by void,
493 = returnDs (Var voidId) -- (not, as would happen if we took the next case, by ())
495 dsExpr (Dictionary dicts methods)
496 = mapDs lookupEnvDs (dicts ++ methods) `thenDs` \ d_and_ms' ->
497 returnDs (mkTupleExpr d_and_ms')
499 dsExpr (ClassDictLam dicts methods expr)
500 = dsExpr expr `thenDs` \ core_expr ->
501 case num_of_d_and_ms of
502 0 -> newSysLocalDs voidTy `thenDs` \ new_x ->
503 returnDs (mkValLam [new_x] core_expr)
506 returnDs (mkValLam dicts_and_methods core_expr)
509 newSysLocalDs tuple_ty `thenDs` \ new_x ->
511 Lam (ValBinder new_x)
514 [(tuple_con, dicts_and_methods, core_expr)]
517 num_of_d_and_ms = length dicts + length methods
518 dicts_and_methods = dicts ++ methods
519 tuple_ty = mkTupleTy num_of_d_and_ms (map idType dicts_and_methods)
520 tuple_con = tupleCon num_of_d_and_ms
523 -- HsSyn constructs that just shouldn't be here:
524 dsExpr (HsDo _ _ _) = panic "dsExpr:HsDo"
525 dsExpr (ExplicitList _) = panic "dsExpr:ExplicitList"
526 dsExpr (ExprWithTySig _ _) = panic "dsExpr:ExprWithTySig"
527 dsExpr (ArithSeqIn _) = panic "dsExpr:ArithSeqIn"
530 out_of_range_msg -- ditto
531 = " out of range: [" ++ show minInt ++ ", " ++ show maxInt ++ "]\n"
534 %--------------------------------------------------------------------
536 @(dsApp e [t_1,..,t_n, e_1,..,e_n])@ returns something with the same
539 e t_1 ... t_n e_1 .. e_n
542 We're doing all this so we can saturate constructors (as painlessly as
546 dsApp :: TypecheckedHsExpr -- expr to desugar
547 -> [DsCoreArg] -- accumulated ty/val args: NB:
548 -> DsM CoreExpr -- final result
550 dsApp (HsApp e1 e2) args
551 = dsExpr e2 `thenDs` \ core_e2 ->
552 dsApp e1 (VarArg core_e2 : args)
554 dsApp (OpApp e1 op _ e2) args
555 = dsExpr e1 `thenDs` \ core_e1 ->
556 dsExpr e2 `thenDs` \ core_e2 ->
557 dsApp op (VarArg core_e1 : VarArg core_e2 : args)
559 dsApp (DictApp expr dicts) args
560 = mapDs lookupEnvDs dicts `thenDs` \ core_dicts ->
561 dsApp expr (map (VarArg . Var) core_dicts ++ args)
563 dsApp (TyApp expr tys) args
564 = dsApp expr (map TyArg tys ++ args)
566 -- we might should look out for SectionLs, etc., here, but we don't
568 dsApp anything_else args
569 = dsExpr anything_else `thenDs` \ core_expr ->
570 mkAppDs core_expr args
573 = lookupEnvDs v `thenDs` \ v' ->
578 dsRbinds :: TypecheckedRecordBinds -- The field bindings supplied
579 -> ([(Id, CoreArg)] -> DsM CoreExpr) -- A continuation taking the field
580 -- bindings with atomic rhss
581 -> DsM CoreExpr -- The result of the continuation,
582 -- wrapped in suitable Lets
584 dsRbinds [] continue_with
587 dsRbinds ((sel_id, rhs, pun_flag) : rbinds) continue_with
588 = dsExpr rhs `thenDs` \ rhs' ->
589 dsExprToAtom (VarArg rhs') $ \ rhs_atom ->
590 dsRbinds rbinds $ \ rbinds' ->
591 continue_with ((sel_id, rhs_atom) : rbinds')
595 -- do_unfold ty_env val_env (Lam (TyBinder tyvar) body) (TyArg ty : args)
596 -- = do_unfold (addOneToTyVarEnv ty_env tyvar ty) val_env body args
598 -- do_unfold ty_env val_env (Lam (ValBinder binder) body) (arg@(VarArg expr) : args)
599 -- = dsExprToAtom arg $ \ arg_atom ->
601 -- (addOneToIdEnv val_env binder (argToExpr arg_atom))
604 -- do_unfold ty_env val_env body args
605 -- = -- Clone the remaining part of the template
606 -- uniqSMtoDsM (substCoreExpr val_env ty_env body) `thenDs` \ body' ->
608 -- -- Apply result to remaining arguments
609 -- mkAppDs body' args
612 Basically does the translation given in the Haskell~1.3 report:
616 -> Id -- id for: return m
617 -> Id -- id for: (>>=) m
618 -> Id -- id for: zero m
619 -> Type -- Element type; the whole expression has type (m t)
622 dsDo do_or_lc stmts return_id then_id zero_id result_ty
623 = dsId return_id `thenDs` \ return_ds ->
624 dsId then_id `thenDs` \ then_ds ->
625 dsId zero_id `thenDs` \ zero_ds ->
627 (_, b_ty) = splitAppTy result_ty -- result_ty must be of the form (m b)
630 = dsExpr expr `thenDs` \ expr2 ->
631 mkAppDs return_ds [TyArg b_ty, VarArg expr2]
633 go (GuardStmt expr locn : stmts)
634 = do_expr expr locn `thenDs` \ expr2 ->
635 go stmts `thenDs` \ rest ->
636 mkAppDs zero_ds [TyArg b_ty] `thenDs` \ zero_expr ->
637 returnDs (mkCoreIfThenElse expr2 rest zero_expr)
639 go (ExprStmt expr locn : stmts)
640 = do_expr expr locn `thenDs` \ expr2 ->
642 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
647 go stmts `thenDs` \ rest ->
648 newSysLocalDs a_ty `thenDs` \ ignored_result_id ->
649 mkAppDs then_ds [TyArg a_ty, TyArg b_ty, VarArg expr2,
650 VarArg (mkValLam [ignored_result_id] rest)]
652 go (LetStmt binds : stmts )
653 = dsBinds binds `thenDs` \ binds2 ->
654 go stmts `thenDs` \ rest ->
655 returnDs (mkCoLetsAny binds2 rest)
657 go (BindStmt pat expr locn : stmts)
659 dsExpr expr `thenDs` \ expr2 ->
661 (_, a_ty) = splitAppTy (coreExprType expr2) -- Must be of form (m a)
662 zero_expr = TyApp (HsVar zero_id) [b_ty]
663 main_match = PatMatch pat (SimpleMatch (
664 HsDoOut do_or_lc stmts return_id then_id zero_id result_ty locn))
666 = if failureFreePat pat
668 else [main_match, PatMatch (WildPat a_ty) (SimpleMatch zero_expr)]
670 matchWrapper DoBindMatch the_matches match_msg
671 `thenDs` \ (binders, matching_code) ->
672 mkAppDs then_ds [TyArg a_ty, TyArg b_ty,
673 VarArg expr2, VarArg (mkValLam binders matching_code)]
678 do_expr expr locn = putSrcLocDs locn (dsExpr expr)
680 match_msg = case do_or_lc of
681 DoStmt -> "`do' statement"
682 ListComp -> "comprehension"