2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[DsListComp]{Desugaring list comprehensions}
7 #include "HsVersions.h"
9 module DsListComp ( dsListComp ) where
12 IMPORT_DELOOPER(DsLoop) -- break dsExpr-ish loop
14 import HsSyn ( Stmt(..), HsExpr, HsBinds )
15 import TcHsSyn ( SYN_IE(TypecheckedStmt), SYN_IE(TypecheckedHsExpr) , SYN_IE(TypecheckedHsBinds) )
16 import DsHsSyn ( outPatType )
19 import DsMonad -- the monadery used in the desugarer
22 import CmdLineOpts ( opt_FoldrBuildOn )
23 import CoreUtils ( coreExprType, mkCoreIfThenElse )
24 import PrelVals ( mkBuild, foldrId )
25 import Type ( mkTyVarTy, mkForAllTy, mkFunTys, mkFunTy )
26 import TysPrim ( alphaTy )
27 import TysWiredIn ( nilDataCon, consDataCon, listTyCon )
28 import TyVar ( alphaTyVar )
29 import Match ( matchSimply )
33 List comprehensions may be desugared in one of two ways: ``ordinary''
34 (as you would expect if you read SLPJ's book) and ``with foldr/build
35 turned on'' (if you read Gill {\em et al.}'s paper on the subject).
37 There will be at least one ``qualifier'' in the input.
40 dsListComp :: [TypecheckedStmt]
41 -> Type -- Type of list elements
44 dsListComp quals elt_ty
45 | not opt_FoldrBuildOn -- Be boring
46 = deListComp quals nil_expr
48 | otherwise -- foldr/build lives!
49 = newTyVarsDs [alphaTyVar] `thenDs` \ [n_tyvar] ->
51 alpha_to_alpha = alphaTy `mkFunTy` alphaTy
53 n_ty = mkTyVarTy n_tyvar
54 c_ty = mkFunTys [elt_ty, n_ty] n_ty
55 g_ty = mkForAllTy alphaTyVar (
56 (elt_ty `mkFunTy` alpha_to_alpha)
61 newSysLocalsDs [c_ty,n_ty,g_ty] `thenDs` \ [c, n, g] ->
65 quals `thenDs` \ result ->
67 returnDs (mkBuild elt_ty n_tyvar c n g result)
69 nil_expr = mkCon nilDataCon [] [elt_ty] []
72 %************************************************************************
74 \subsection[DsListComp-ordinary]{Ordinary desugaring of list comprehensions}
76 %************************************************************************
78 Just as in Phil's chapter~7 in SLPJ, using the rules for
79 optimally-compiled list comprehensions. This is what Kevin followed
80 as well, and I quite happily do the same. The TQ translation scheme
81 transforms a list of qualifiers (either boolean expressions or
82 generators) into a single expression which implements the list
83 comprehension. Because we are generating 2nd-order polymorphic
84 lambda-calculus, calls to NIL and CONS must be applied to a type
85 argument, as well as their usual value arguments.
87 TE << [ e | qs ] >> = TQ << [ e | qs ] ++ Nil (typeOf e) >>
90 TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>
93 TQ << [ e | b , qs ] ++ L >> =
94 if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>
97 TQ << [ e | p <- L1, qs ] ++ L2 >> =
103 (( \ TE << p >> -> ( TQ << [e | qs] ++ (h u3) >> )) u2)
108 "h", "u1", "u2", and "u3" are new variables.
111 @deListComp@ is the TQ translation scheme. Roughly speaking, @dsExpr@
112 is the TE translation scheme. Note that we carry around the @L@ list
113 already desugared. @dsListComp@ does the top TE rule mentioned above.
116 deListComp :: [TypecheckedStmt] -> CoreExpr -> DsM CoreExpr
118 deListComp [ReturnStmt expr] list -- Figure 7.4, SLPJ, p 135, rule C above
119 = dsExpr expr `thenDs` \ core_expr ->
120 mkConDs consDataCon [TyArg (coreExprType core_expr), VarArg core_expr, VarArg list]
122 deListComp (GuardStmt guard locn : quals) list -- rule B above
123 = dsExpr guard `thenDs` \ core_guard ->
124 deListComp quals list `thenDs` \ core_rest ->
125 returnDs (mkCoreIfThenElse core_guard core_rest list)
127 -- [e | let B, qs] = let B in [e | qs]
128 deListComp (LetStmt binds : quals) list
129 = dsBinds binds `thenDs` \ core_binds ->
130 deListComp quals list `thenDs` \ core_rest ->
131 returnDs (mkCoLetsAny core_binds core_rest)
133 deListComp (BindStmt pat list1 locn : quals) core_list2 -- rule A' above
134 = dsExpr list1 `thenDs` \ core_list1 ->
136 u3_ty@u1_ty = coreExprType core_list1 -- two names, same thing
138 -- u1_ty is a [alpha] type, and u2_ty = alpha
139 u2_ty = outPatType pat
141 res_ty = coreExprType core_list2
142 h_ty = u1_ty `mkFunTy` res_ty
144 newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty] `thenDs` \ [h, u1, u2, u3] ->
146 -- the "fail" value ...
147 mkAppDs (Var h) [VarArg (Var u3)] `thenDs` \ core_fail ->
148 deListComp quals core_fail `thenDs` \ rest_expr ->
149 matchSimply (Var u2) pat res_ty
150 rest_expr core_fail `thenDs` \ core_match ->
151 mkAppDs (Var h) [VarArg core_list1] `thenDs` \ letrec_body ->
159 [(nilDataCon, [], core_list2),
160 (consDataCon, [u2, u3], core_match)]
166 %************************************************************************
168 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
170 %************************************************************************
172 @dfListComp@ are the rules used with foldr/build turned on:
174 TE < [ e | ] >> c n = c e n
175 TE << [ e | b , q ] >> c n = if b then TE << [ e | q ] >> c n else n
176 TE << [ e | p <- l , q ] c n = foldr
177 (\ TE << p >> b -> TE << [ e | q ] >> c b
181 dfListComp :: Type -> Id -- 'c'; its type and id
182 -> Type -> Id -- 'n'; its type and id
183 -> [TypecheckedStmt] -- the rest of the qual's
186 dfListComp c_ty c_id n_ty n_id [ReturnStmt expr]
187 = dsExpr expr `thenDs` \ core_expr ->
188 mkAppDs (Var c_id) [VarArg core_expr, VarArg (Var n_id)]
190 dfListComp c_ty c_id n_ty n_id (GuardStmt guard locn : quals)
191 = dsExpr guard `thenDs` \ core_guard ->
192 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
193 returnDs (mkCoreIfThenElse core_guard core_rest (Var n_id))
195 dfListComp c_ty c_id n_ty n_id (LetStmt binds : quals)
196 -- new in 1.3, local bindings
197 = dsBinds binds `thenDs` \ core_binds ->
198 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
199 returnDs (mkCoLetsAny core_binds core_rest)
201 dfListComp c_ty c_id n_ty n_id (BindStmt pat list1 locn : quals)
202 -- evaluate the two lists
203 = dsExpr list1 `thenDs` \ core_list1 ->
205 -- find the required type
207 let p_ty = outPatType pat
208 b_ty = n_ty -- alias b_ty to n_ty
209 fn_ty = mkFunTys [p_ty, b_ty] b_ty
210 lst_ty = coreExprType core_list1
213 -- create some new local id's
215 newSysLocalsDs [b_ty,p_ty,fn_ty,lst_ty] `thenDs` \ [b,p,fn,lst] ->
217 -- build rest of the comprehesion
219 dfListComp c_ty c_id b_ty b quals `thenDs` \ core_rest ->
220 -- build the pattern match
222 matchSimply (Var p) pat b_ty core_rest (Var b) `thenDs` \ core_expr ->
224 -- now build the outermost foldr, and return
228 [NonRec fn (mkValLam [p, b] core_expr),
229 NonRec lst core_list1]
230 (mkFoldr p_ty n_ty fn n_id lst)
234 = mkValApp (mkTyApp (Var foldrId) [a,b]) [VarArg f, VarArg z, VarArg xs]