2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Desugaring list comprehensions and array comprehensions
9 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 module DsListComp ( dsListComp, dsPArrComp ) where
18 #include "HsVersions.h"
20 import {-# SOURCE #-} DsExpr ( dsLExpr, dsLocalBinds )
27 import DsMonad -- the monadery used in the desugarer
42 List comprehensions may be desugared in one of two ways: ``ordinary''
43 (as you would expect if you read SLPJ's book) and ``with foldr/build
44 turned on'' (if you read Gill {\em et al.}'s paper on the subject).
46 There will be at least one ``qualifier'' in the input.
49 dsListComp :: [LStmt Id]
51 -> Type -- Type of list elements
53 dsListComp lquals body elt_ty = do
55 let quals = map unLoc lquals
57 if not (dopt Opt_EnableRewriteRules dflags) || dopt Opt_IgnoreInterfacePragmas dflags
58 -- Either rules are switched off, or we are ignoring what there are;
59 -- Either way foldr/build won't happen, so use the more efficient
60 -- Wadler-style desugaring
61 || isParallelComp quals
62 -- Foldr-style desugaring can't handle parallel list comprehensions
63 then deListComp quals body (mkNilExpr elt_ty)
64 else mkBuildExpr elt_ty (\(c, _) (n, _) -> dfListComp c n quals body)
65 -- Foldr/build should be enabled, so desugar
66 -- into foldrs and builds
69 -- We must test for ParStmt anywhere, not just at the head, because an extension
70 -- to list comprehensions would be to add brackets to specify the associativity
71 -- of qualifier lists. This is really easy to do by adding extra ParStmts into the
72 -- mix of possibly a single element in length, so we do this to leave the possibility open
73 isParallelComp = any isParallelStmt
75 isParallelStmt (ParStmt _) = True
76 isParallelStmt _ = False
79 -- This function lets you desugar a inner list comprehension and a list of the binders
80 -- of that comprehension that we need in the outer comprehension into such an expression
81 -- and the type of the elements that it outputs (tuples of binders)
82 dsInnerListComp :: ([LStmt Id], [Id]) -> DsM (CoreExpr, Type)
83 dsInnerListComp (stmts, bndrs) = do
84 expr <- dsListComp stmts (mkBigLHsVarTup bndrs) bndrs_tuple_type
85 return (expr, bndrs_tuple_type)
87 bndrs_types = map idType bndrs
88 bndrs_tuple_type = mkBigCoreTupTy bndrs_types
91 -- This function factors out commonality between the desugaring strategies for TransformStmt.
92 -- Given such a statement it gives you back an expression representing how to compute the transformed
93 -- list and the tuple that you need to bind from that list in order to proceed with your desugaring
94 dsTransformStmt :: Stmt Id -> DsM (CoreExpr, LPat Id)
95 dsTransformStmt (TransformStmt stmts binders usingExpr maybeByExpr) = do
96 (expr, binders_tuple_type) <- dsInnerListComp (stmts, binders)
97 usingExpr' <- dsLExpr usingExpr
101 Nothing -> return [expr]
103 byExpr' <- dsLExpr byExpr
105 us <- newUniqueSupply
106 [tuple_binder] <- newSysLocalsDs [binders_tuple_type]
107 let byExprWrapper = mkTupleCase us binders byExpr' tuple_binder (Var tuple_binder)
109 return [Lam tuple_binder byExprWrapper, expr]
111 let inner_list_expr = mkApps usingExpr' ((Type binders_tuple_type) : using_args)
113 let pat = mkBigLHsVarPatTup binders
114 return (inner_list_expr, pat)
116 -- This function factors out commonality between the desugaring strategies for GroupStmt.
117 -- Given such a statement it gives you back an expression representing how to compute the transformed
118 -- list and the tuple that you need to bind from that list in order to proceed with your desugaring
119 dsGroupStmt :: Stmt Id -> DsM (CoreExpr, LPat Id)
120 dsGroupStmt (GroupStmt stmts binderMap by using) = do
121 let (fromBinders, toBinders) = unzip binderMap
123 fromBindersTypes = map idType fromBinders
124 toBindersTypes = map idType toBinders
126 toBindersTupleType = mkBigCoreTupTy toBindersTypes
128 -- Desugar an inner comprehension which outputs a list of tuples of the "from" binders
129 (expr, from_tup_ty) <- dsInnerListComp (stmts, fromBinders)
131 -- Work out what arguments should be supplied to that expression: i.e. is an extraction
132 -- function required? If so, create that desugared function and add to arguments
133 usingExpr' <- dsLExpr (either id noLoc using)
134 usingArgs <- case by of
135 Nothing -> return [expr]
136 Just by_e -> do { by_e' <- dsLExpr by_e
137 ; us <- newUniqueSupply
138 ; [from_tup_id] <- newSysLocalsDs [from_tup_ty]
139 ; let by_wrap = mkTupleCase us fromBinders by_e'
140 from_tup_id (Var from_tup_id)
141 ; return [Lam from_tup_id by_wrap, expr] }
143 -- Create an unzip function for the appropriate arity and element types and find "map"
144 (unzip_fn, unzip_rhs) <- mkUnzipBind fromBindersTypes
145 map_id <- dsLookupGlobalId mapName
147 -- Generate the expressions to build the grouped list
148 let -- First we apply the grouping function to the inner list
149 inner_list_expr = mkApps usingExpr' ((Type from_tup_ty) : usingArgs)
150 -- Then we map our "unzip" across it to turn the lists of tuples into tuples of lists
151 -- We make sure we instantiate the type variable "a" to be a list of "from" tuples and
152 -- the "b" to be a tuple of "to" lists!
153 unzipped_inner_list_expr = mkApps (Var map_id)
154 [Type (mkListTy from_tup_ty), Type toBindersTupleType, Var unzip_fn, inner_list_expr]
155 -- Then finally we bind the unzip function around that expression
156 bound_unzipped_inner_list_expr = Let (Rec [(unzip_fn, unzip_rhs)]) unzipped_inner_list_expr
158 -- Build a pattern that ensures the consumer binds into the NEW binders, which hold lists rather than single values
159 let pat = mkBigLHsVarPatTup toBinders
160 return (bound_unzipped_inner_list_expr, pat)
164 %************************************************************************
166 \subsection[DsListComp-ordinary]{Ordinary desugaring of list comprehensions}
168 %************************************************************************
170 Just as in Phil's chapter~7 in SLPJ, using the rules for
171 optimally-compiled list comprehensions. This is what Kevin followed
172 as well, and I quite happily do the same. The TQ translation scheme
173 transforms a list of qualifiers (either boolean expressions or
174 generators) into a single expression which implements the list
175 comprehension. Because we are generating 2nd-order polymorphic
176 lambda-calculus, calls to NIL and CONS must be applied to a type
177 argument, as well as their usual value arguments.
179 TE << [ e | qs ] >> = TQ << [ e | qs ] ++ Nil (typeOf e) >>
182 TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>
185 TQ << [ e | b , qs ] ++ L >> =
186 if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>
189 TQ << [ e | p <- L1, qs ] ++ L2 >> =
195 (( \ TE << p >> -> ( TQ << [e | qs] ++ (h u3) >> )) u2)
200 "h", "u1", "u2", and "u3" are new variables.
203 @deListComp@ is the TQ translation scheme. Roughly speaking, @dsExpr@
204 is the TE translation scheme. Note that we carry around the @L@ list
205 already desugared. @dsListComp@ does the top TE rule mentioned above.
207 To the above, we add an additional rule to deal with parallel list
208 comprehensions. The translation goes roughly as follows:
209 [ e | p1 <- e11, let v1 = e12, p2 <- e13
210 | q1 <- e21, let v2 = e22, q2 <- e23]
212 [ e | ((x1, .., xn), (y1, ..., ym)) <-
213 zip [(x1,..,xn) | p1 <- e11, let v1 = e12, p2 <- e13]
214 [(y1,..,ym) | q1 <- e21, let v2 = e22, q2 <- e23]]
215 where (x1, .., xn) are the variables bound in p1, v1, p2
216 (y1, .., ym) are the variables bound in q1, v2, q2
218 In the translation below, the ParStmt branch translates each parallel branch
219 into a sub-comprehension, and desugars each independently. The resulting lists
220 are fed to a zip function, we create a binding for all the variables bound in all
221 the comprehensions, and then we hand things off the the desugarer for bindings.
222 The zip function is generated here a) because it's small, and b) because then we
223 don't have to deal with arbitrary limits on the number of zip functions in the
224 prelude, nor which library the zip function came from.
225 The introduced tuples are Boxed, but only because I couldn't get it to work
226 with the Unboxed variety.
230 deListComp :: [Stmt Id] -> LHsExpr Id -> CoreExpr -> DsM CoreExpr
232 deListComp (ParStmt stmtss_w_bndrs : quals) body list
234 exps_and_qual_tys <- mapM dsInnerListComp stmtss_w_bndrs
235 let (exps, qual_tys) = unzip exps_and_qual_tys
237 (zip_fn, zip_rhs) <- mkZipBind qual_tys
239 -- Deal with [e | pat <- zip l1 .. ln] in example above
240 deBindComp pat (Let (Rec [(zip_fn, zip_rhs)]) (mkApps (Var zip_fn) exps))
244 bndrs_s = map snd stmtss_w_bndrs
246 -- pat is the pattern ((x1,..,xn), (y1,..,ym)) in the example above
247 pat = mkBigLHsPatTup pats
248 pats = map mkBigLHsVarPatTup bndrs_s
250 -- Last: the one to return
251 deListComp [] body list = do -- Figure 7.4, SLPJ, p 135, rule C above
252 core_body <- dsLExpr body
253 return (mkConsExpr (exprType core_body) core_body list)
255 -- Non-last: must be a guard
256 deListComp (ExprStmt guard _ _ : quals) body list = do -- rule B above
257 core_guard <- dsLExpr guard
258 core_rest <- deListComp quals body list
259 return (mkIfThenElse core_guard core_rest list)
261 -- [e | let B, qs] = let B in [e | qs]
262 deListComp (LetStmt binds : quals) body list = do
263 core_rest <- deListComp quals body list
264 dsLocalBinds binds core_rest
266 deListComp (stmt@(TransformStmt {}) : quals) body list = do
267 (inner_list_expr, pat) <- dsTransformStmt stmt
268 deBindComp pat inner_list_expr quals body list
270 deListComp (stmt@(GroupStmt {}) : quals) body list = do
271 (inner_list_expr, pat) <- dsGroupStmt stmt
272 deBindComp pat inner_list_expr quals body list
274 deListComp (BindStmt pat list1 _ _ : quals) body core_list2 = do -- rule A' above
275 core_list1 <- dsLExpr list1
276 deBindComp pat core_list1 quals body core_list2
281 deBindComp :: OutPat Id
287 deBindComp pat core_list1 quals body core_list2 = do
289 u3_ty@u1_ty = exprType core_list1 -- two names, same thing
291 -- u1_ty is a [alpha] type, and u2_ty = alpha
292 u2_ty = hsLPatType pat
294 res_ty = exprType core_list2
295 h_ty = u1_ty `mkFunTy` res_ty
297 [h, u1, u2, u3] <- newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty]
299 -- the "fail" value ...
301 core_fail = App (Var h) (Var u3)
302 letrec_body = App (Var h) core_list1
304 rest_expr <- deListComp quals body core_fail
305 core_match <- matchSimply (Var u2) (StmtCtxt ListComp) pat rest_expr core_fail
309 Case (Var u1) u1 res_ty
310 [(DataAlt nilDataCon, [], core_list2),
311 (DataAlt consDataCon, [u2, u3], core_match)]
312 -- Increasing order of tag
314 return (Let (Rec [(h, rhs)]) letrec_body)
317 %************************************************************************
319 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
321 %************************************************************************
323 @dfListComp@ are the rules used with foldr/build turned on:
326 TE[ e | ] c n = c e n
327 TE[ e | b , q ] c n = if b then TE[ e | q ] c n else n
328 TE[ e | p <- l , q ] c n = let
329 f = \ x b -> case x of
337 dfListComp :: Id -> Id -- 'c' and 'n'
338 -> [Stmt Id] -- the rest of the qual's
342 -- Last: the one to return
343 dfListComp c_id n_id [] body = do
344 core_body <- dsLExpr body
345 return (mkApps (Var c_id) [core_body, Var n_id])
347 -- Non-last: must be a guard
348 dfListComp c_id n_id (ExprStmt guard _ _ : quals) body = do
349 core_guard <- dsLExpr guard
350 core_rest <- dfListComp c_id n_id quals body
351 return (mkIfThenElse core_guard core_rest (Var n_id))
353 dfListComp c_id n_id (LetStmt binds : quals) body = do
354 -- new in 1.3, local bindings
355 core_rest <- dfListComp c_id n_id quals body
356 dsLocalBinds binds core_rest
358 dfListComp c_id n_id (stmt@(TransformStmt {}) : quals) body = do
359 (inner_list_expr, pat) <- dsTransformStmt stmt
360 -- Anyway, we bind the newly transformed list via the generic binding function
361 dfBindComp c_id n_id (pat, inner_list_expr) quals body
363 dfListComp c_id n_id (stmt@(GroupStmt {}) : quals) body = do
364 (inner_list_expr, pat) <- dsGroupStmt stmt
365 -- Anyway, we bind the newly grouped list via the generic binding function
366 dfBindComp c_id n_id (pat, inner_list_expr) quals body
368 dfListComp c_id n_id (BindStmt pat list1 _ _ : quals) body = do
369 -- evaluate the two lists
370 core_list1 <- dsLExpr list1
372 -- Do the rest of the work in the generic binding builder
373 dfBindComp c_id n_id (pat, core_list1) quals body
375 dfBindComp :: Id -> Id -- 'c' and 'n'
376 -> (LPat Id, CoreExpr)
377 -> [Stmt Id] -- the rest of the qual's
380 dfBindComp c_id n_id (pat, core_list1) quals body = do
381 -- find the required type
382 let x_ty = hsLPatType pat
385 -- create some new local id's
386 [b, x] <- newSysLocalsDs [b_ty, x_ty]
388 -- build rest of the comprehesion
389 core_rest <- dfListComp c_id b quals body
391 -- build the pattern match
392 core_expr <- matchSimply (Var x) (StmtCtxt ListComp)
393 pat core_rest (Var b)
395 -- now build the outermost foldr, and return
396 mkFoldrExpr x_ty b_ty (mkLams [x, b] core_expr) (Var n_id) core_list1
399 %************************************************************************
401 \subsection[DsFunGeneration]{Generation of zip/unzip functions for use in desugaring}
403 %************************************************************************
407 mkZipBind :: [Type] -> DsM (Id, CoreExpr)
408 -- mkZipBind [t1, t2]
409 -- = (zip, \as1:[t1] as2:[t2]
412 -- (a1:as'1) -> case as2 of
414 -- (a2:as'2) -> (a1, a2) : zip as'1 as'2)]
416 mkZipBind elt_tys = do
417 ass <- mapM newSysLocalDs elt_list_tys
418 as' <- mapM newSysLocalDs elt_tys
419 as's <- mapM newSysLocalDs elt_list_tys
421 zip_fn <- newSysLocalDs zip_fn_ty
423 let inner_rhs = mkConsExpr elt_tuple_ty
424 (mkBigCoreVarTup as')
425 (mkVarApps (Var zip_fn) as's)
426 zip_body = foldr mk_case inner_rhs (zip3 ass as' as's)
428 return (zip_fn, mkLams ass zip_body)
430 elt_list_tys = map mkListTy elt_tys
431 elt_tuple_ty = mkBigCoreTupTy elt_tys
432 elt_tuple_list_ty = mkListTy elt_tuple_ty
434 zip_fn_ty = mkFunTys elt_list_tys elt_tuple_list_ty
436 mk_case (as, a', as') rest
437 = Case (Var as) as elt_tuple_list_ty
438 [(DataAlt nilDataCon, [], mkNilExpr elt_tuple_ty),
439 (DataAlt consDataCon, [a', as'], rest)]
440 -- Increasing order of tag
443 mkUnzipBind :: [Type] -> DsM (Id, CoreExpr)
444 -- mkUnzipBind [t1, t2]
445 -- = (unzip, \ys :: [(t1, t2)] -> foldr (\ax :: (t1, t2) axs :: ([t1], [t2])
447 -- (x1, x2) -> case axs of
448 -- (xs1, xs2) -> (x1 : xs1, x2 : xs2))
452 -- We use foldr here in all cases, even if rules are turned off, because we may as well!
453 mkUnzipBind elt_tys = do
454 ax <- newSysLocalDs elt_tuple_ty
455 axs <- newSysLocalDs elt_list_tuple_ty
456 ys <- newSysLocalDs elt_tuple_list_ty
457 xs <- mapM newSysLocalDs elt_tys
458 xss <- mapM newSysLocalDs elt_list_tys
460 unzip_fn <- newSysLocalDs unzip_fn_ty
462 [us1, us2] <- sequence [newUniqueSupply, newUniqueSupply]
464 let nil_tuple = mkBigCoreTup (map mkNilExpr elt_tys)
466 concat_expressions = map mkConcatExpression (zip3 elt_tys (map Var xs) (map Var xss))
467 tupled_concat_expression = mkBigCoreTup concat_expressions
469 folder_body_inner_case = mkTupleCase us1 xss tupled_concat_expression axs (Var axs)
470 folder_body_outer_case = mkTupleCase us2 xs folder_body_inner_case ax (Var ax)
471 folder_body = mkLams [ax, axs] folder_body_outer_case
473 unzip_body <- mkFoldrExpr elt_tuple_ty elt_list_tuple_ty folder_body nil_tuple (Var ys)
474 return (unzip_fn, mkLams [ys] unzip_body)
476 elt_tuple_ty = mkBigCoreTupTy elt_tys
477 elt_tuple_list_ty = mkListTy elt_tuple_ty
478 elt_list_tys = map mkListTy elt_tys
479 elt_list_tuple_ty = mkBigCoreTupTy elt_list_tys
481 unzip_fn_ty = elt_tuple_list_ty `mkFunTy` elt_list_tuple_ty
483 mkConcatExpression (list_element_ty, head, tail) = mkConsExpr list_element_ty head tail
489 %************************************************************************
491 \subsection[DsPArrComp]{Desugaring of array comprehensions}
493 %************************************************************************
497 -- entry point for desugaring a parallel array comprehension
499 -- [:e | qss:] = <<[:e | qss:]>> () [:():]
501 dsPArrComp :: [Stmt Id]
503 -> Type -- Don't use; called with `undefined' below
505 dsPArrComp [ParStmt qss] body _ = -- parallel comprehension
506 dePArrParComp qss body
508 -- Special case for simple generators:
510 -- <<[:e' | p <- e, qs:]>> = <<[: e' | qs :]>> p e
512 -- if matching again p cannot fail, or else
514 -- <<[:e' | p <- e, qs:]>> =
515 -- <<[:e' | qs:]>> p (filterP (\x -> case x of {p -> True; _ -> False}) e)
517 dsPArrComp (BindStmt p e _ _ : qs) body _ = do
518 filterP <- dsLookupGlobalId filterPName
520 let ety'ce = parrElemType ce
521 false = Var falseDataConId
522 true = Var trueDataConId
523 v <- newSysLocalDs ety'ce
524 pred <- matchSimply (Var v) (StmtCtxt PArrComp) p true false
525 let gen | isIrrefutableHsPat p = ce
526 | otherwise = mkApps (Var filterP) [Type ety'ce, mkLams [v] pred, ce]
527 dePArrComp qs body p gen
529 dsPArrComp qs body _ = do -- no ParStmt in `qs'
530 sglP <- dsLookupGlobalId singletonPName
531 let unitArray = mkApps (Var sglP) [Type unitTy, mkCoreTup []]
532 dePArrComp qs body (noLoc $ WildPat unitTy) unitArray
538 dePArrComp :: [Stmt Id]
540 -> LPat Id -- the current generator pattern
541 -> CoreExpr -- the current generator expression
544 -- <<[:e' | :]>> pa ea = mapP (\pa -> e') ea
546 dePArrComp [] e' pa cea = do
547 mapP <- dsLookupGlobalId mapPName
548 let ty = parrElemType cea
549 (clam, ty'e') <- deLambda ty pa e'
550 return $ mkApps (Var mapP) [Type ty, Type ty'e', clam, cea]
552 -- <<[:e' | b, qs:]>> pa ea = <<[:e' | qs:]>> pa (filterP (\pa -> b) ea)
554 dePArrComp (ExprStmt b _ _ : qs) body pa cea = do
555 filterP <- dsLookupGlobalId filterPName
556 let ty = parrElemType cea
557 (clam,_) <- deLambda ty pa b
558 dePArrComp qs body pa (mkApps (Var filterP) [Type ty, clam, cea])
561 -- <<[:e' | p <- e, qs:]>> pa ea =
564 -- <<[:e' | qs:]>> (pa, p) (crossMap ea ef)
566 -- if matching again p cannot fail, or else
568 -- <<[:e' | p <- e, qs:]>> pa ea =
569 -- let ef = \pa -> filterP (\x -> case x of {p -> True; _ -> False}) e
571 -- <<[:e' | qs:]>> (pa, p) (crossMapP ea ef)
573 dePArrComp (BindStmt p e _ _ : qs) body pa cea = do
574 filterP <- dsLookupGlobalId filterPName
575 crossMapP <- dsLookupGlobalId crossMapPName
577 let ety'cea = parrElemType cea
578 ety'ce = parrElemType ce
579 false = Var falseDataConId
580 true = Var trueDataConId
581 v <- newSysLocalDs ety'ce
582 pred <- matchSimply (Var v) (StmtCtxt PArrComp) p true false
583 let cef | isIrrefutableHsPat p = ce
584 | otherwise = mkApps (Var filterP) [Type ety'ce, mkLams [v] pred, ce]
585 (clam, _) <- mkLambda ety'cea pa cef
586 let ety'cef = ety'ce -- filter doesn't change the element type
587 pa' = mkLHsPatTup [pa, p]
589 dePArrComp qs body pa' (mkApps (Var crossMapP)
590 [Type ety'cea, Type ety'cef, cea, clam])
592 -- <<[:e' | let ds, qs:]>> pa ea =
593 -- <<[:e' | qs:]>> (pa, (x_1, ..., x_n))
594 -- (mapP (\v@pa -> let ds in (v, (x_1, ..., x_n))) ea)
596 -- {x_1, ..., x_n} = DV (ds) -- Defined Variables
598 dePArrComp (LetStmt ds : qs) body pa cea = do
599 mapP <- dsLookupGlobalId mapPName
600 let xs = collectLocalBinders ds
601 ty'cea = parrElemType cea
602 v <- newSysLocalDs ty'cea
603 clet <- dsLocalBinds ds (mkCoreTup (map Var xs))
604 let'v <- newSysLocalDs (exprType clet)
605 let projBody = mkCoreLet (NonRec let'v clet) $
606 mkCoreTup [Var v, Var let'v]
607 errTy = exprType projBody
608 errMsg = ptext (sLit "DsListComp.dePArrComp: internal error!")
609 cerr <- mkErrorAppDs pAT_ERROR_ID errTy errMsg
610 ccase <- matchSimply (Var v) (StmtCtxt PArrComp) pa projBody cerr
611 let pa' = mkLHsPatTup [pa, mkLHsPatTup (map nlVarPat xs)]
612 proj = mkLams [v] ccase
613 dePArrComp qs body pa' (mkApps (Var mapP)
614 [Type ty'cea, Type errTy, proj, cea])
616 -- The parser guarantees that parallel comprehensions can only appear as
617 -- singeltons qualifier lists, which we already special case in the caller.
618 -- So, encountering one here is a bug.
620 dePArrComp (ParStmt _ : _) _ _ _ =
621 panic "DsListComp.dePArrComp: malformed comprehension AST"
623 -- <<[:e' | qs | qss:]>> pa ea =
624 -- <<[:e' | qss:]>> (pa, (x_1, ..., x_n))
625 -- (zipP ea <<[:(x_1, ..., x_n) | qs:]>>)
627 -- {x_1, ..., x_n} = DV (qs)
629 dePArrParComp :: [([LStmt Id], [Id])] -> LHsExpr Id -> DsM CoreExpr
630 dePArrParComp qss body = do
631 (pQss, ceQss) <- deParStmt qss
632 dePArrComp [] body pQss ceQss
635 -- empty parallel statement lists have no source representation
636 panic "DsListComp.dePArrComp: Empty parallel list comprehension"
637 deParStmt ((qs, xs):qss) = do -- first statement
638 let res_expr = mkLHsVarTuple xs
639 cqs <- dsPArrComp (map unLoc qs) res_expr undefined
640 parStmts qss (mkLHsVarPatTup xs) cqs
642 parStmts [] pa cea = return (pa, cea)
643 parStmts ((qs, xs):qss) pa cea = do -- subsequent statements (zip'ed)
644 zipP <- dsLookupGlobalId zipPName
645 let pa' = mkLHsPatTup [pa, mkLHsVarPatTup xs]
646 ty'cea = parrElemType cea
647 res_expr = mkLHsVarTuple xs
648 cqs <- dsPArrComp (map unLoc qs) res_expr undefined
649 let ty'cqs = parrElemType cqs
650 cea' = mkApps (Var zipP) [Type ty'cea, Type ty'cqs, cea, cqs]
651 parStmts qss pa' cea'
653 -- generate Core corresponding to `\p -> e'
655 deLambda :: Type -- type of the argument
656 -> LPat Id -- argument pattern
657 -> LHsExpr Id -- body
658 -> DsM (CoreExpr, Type)
660 mkLambda ty p =<< dsLExpr e
662 -- generate Core for a lambda pattern match, where the body is already in Core
664 mkLambda :: Type -- type of the argument
665 -> LPat Id -- argument pattern
666 -> CoreExpr -- desugared body
667 -> DsM (CoreExpr, Type)
668 mkLambda ty p ce = do
669 v <- newSysLocalDs ty
670 let errMsg = ptext (sLit "DsListComp.deLambda: internal error!")
672 cerr <- mkErrorAppDs pAT_ERROR_ID ce'ty errMsg
673 res <- matchSimply (Var v) (StmtCtxt PArrComp) p ce cerr
674 return (mkLams [v] res, ce'ty)
676 -- obtain the element type of the parallel array produced by the given Core
679 parrElemType :: CoreExpr -> Type
681 case splitTyConApp_maybe (exprType e) of
682 Just (tycon, [ty]) | tycon == parrTyCon -> ty
684 "DsListComp.parrElemType: not a parallel array type"