2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[DsListComp]{Desugaring list comprehensions}
7 module DsListComp ( dsListComp ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} DsExpr ( dsExpr )
12 import {-# SOURCE #-} DsBinds ( dsBinds )
14 import HsSyn ( Stmt(..), HsExpr, HsBinds )
15 import TcHsSyn ( TypecheckedStmt, TypecheckedHsExpr , TypecheckedHsBinds )
16 import DsHsSyn ( outPatType )
19 import DsMonad -- the monadery used in the desugarer
22 import CmdLineOpts ( opt_FoldrBuildOn )
23 import CoreUtils ( coreExprType, mkCoreIfThenElse )
25 import PrelVals ( mkBuild, foldrId )
26 import Type ( mkTyVarTy, mkForAllTy, mkFunTys, mkFunTy, Type )
27 import TysPrim ( alphaTy )
28 import TysWiredIn ( nilDataCon, consDataCon, listTyCon )
29 import TyVar ( alphaTyVar )
30 import Match ( matchSimply )
34 List comprehensions may be desugared in one of two ways: ``ordinary''
35 (as you would expect if you read SLPJ's book) and ``with foldr/build
36 turned on'' (if you read Gill {\em et al.}'s paper on the subject).
38 There will be at least one ``qualifier'' in the input.
41 dsListComp :: [TypecheckedStmt]
42 -> Type -- Type of list elements
45 dsListComp quals elt_ty
46 | not opt_FoldrBuildOn -- Be boring
47 = deListComp quals nil_expr
49 | otherwise -- foldr/build lives!
50 = newTyVarsDs [alphaTyVar] `thenDs` \ [n_tyvar] ->
52 alpha_to_alpha = alphaTy `mkFunTy` alphaTy
54 n_ty = mkTyVarTy n_tyvar
55 c_ty = mkFunTys [elt_ty, n_ty] n_ty
56 g_ty = mkForAllTy alphaTyVar (
57 (elt_ty `mkFunTy` alpha_to_alpha)
62 newSysLocalsDs [c_ty,n_ty,g_ty] `thenDs` \ [c, n, g] ->
66 quals `thenDs` \ result ->
68 returnDs (mkBuild elt_ty n_tyvar c n g result)
70 nil_expr = mkCon nilDataCon [elt_ty] []
73 %************************************************************************
75 \subsection[DsListComp-ordinary]{Ordinary desugaring of list comprehensions}
77 %************************************************************************
79 Just as in Phil's chapter~7 in SLPJ, using the rules for
80 optimally-compiled list comprehensions. This is what Kevin followed
81 as well, and I quite happily do the same. The TQ translation scheme
82 transforms a list of qualifiers (either boolean expressions or
83 generators) into a single expression which implements the list
84 comprehension. Because we are generating 2nd-order polymorphic
85 lambda-calculus, calls to NIL and CONS must be applied to a type
86 argument, as well as their usual value arguments.
88 TE << [ e | qs ] >> = TQ << [ e | qs ] ++ Nil (typeOf e) >>
91 TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>
94 TQ << [ e | b , qs ] ++ L >> =
95 if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>
98 TQ << [ e | p <- L1, qs ] ++ L2 >> =
104 (( \ TE << p >> -> ( TQ << [e | qs] ++ (h u3) >> )) u2)
109 "h", "u1", "u2", and "u3" are new variables.
112 @deListComp@ is the TQ translation scheme. Roughly speaking, @dsExpr@
113 is the TE translation scheme. Note that we carry around the @L@ list
114 already desugared. @dsListComp@ does the top TE rule mentioned above.
117 deListComp :: [TypecheckedStmt] -> CoreExpr -> DsM CoreExpr
119 deListComp [ReturnStmt expr] list -- Figure 7.4, SLPJ, p 135, rule C above
120 = dsExpr expr `thenDs` \ core_expr ->
121 mkConDs consDataCon [TyArg (coreExprType core_expr), VarArg core_expr, VarArg list]
123 deListComp (GuardStmt guard locn : quals) list -- rule B above
124 = dsExpr guard `thenDs` \ core_guard ->
125 deListComp quals list `thenDs` \ core_rest ->
126 returnDs (mkCoreIfThenElse core_guard core_rest list)
128 -- [e | let B, qs] = let B in [e | qs]
129 deListComp (LetStmt binds : quals) list
130 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
131 deListComp quals list `thenDs` \ core_rest ->
132 returnDs (mkCoLetsAny core_binds core_rest)
134 deListComp (BindStmt pat list1 locn : quals) core_list2 -- rule A' above
135 = dsExpr list1 `thenDs` \ core_list1 ->
137 u3_ty@u1_ty = coreExprType core_list1 -- two names, same thing
139 -- u1_ty is a [alpha] type, and u2_ty = alpha
140 u2_ty = outPatType pat
142 res_ty = coreExprType core_list2
143 h_ty = u1_ty `mkFunTy` res_ty
145 newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty] `thenDs` \ [h, u1, u2, u3] ->
147 -- the "fail" value ...
148 mkAppDs (Var h) [VarArg (Var u3)] `thenDs` \ core_fail ->
149 deListComp quals core_fail `thenDs` \ rest_expr ->
150 matchSimply (Var u2) ListCompMatch pat res_ty
151 rest_expr core_fail `thenDs` \ core_match ->
152 mkAppDs (Var h) [VarArg core_list1] `thenDs` \ letrec_body ->
160 [(nilDataCon, [], core_list2),
161 (consDataCon, [u2, u3], core_match)]
167 %************************************************************************
169 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
171 %************************************************************************
173 @dfListComp@ are the rules used with foldr/build turned on:
175 TE < [ e | ] >> c n = c e n
176 TE << [ e | b , q ] >> c n = if b then TE << [ e | q ] >> c n else n
177 TE << [ e | p <- l , q ] c n = foldr
178 (\ TE << p >> b -> TE << [ e | q ] >> c b
182 dfListComp :: Type -> Id -- 'c'; its type and id
183 -> Type -> Id -- 'n'; its type and id
184 -> [TypecheckedStmt] -- the rest of the qual's
187 dfListComp c_ty c_id n_ty n_id [ReturnStmt expr]
188 = dsExpr expr `thenDs` \ core_expr ->
189 mkAppDs (Var c_id) [VarArg core_expr, VarArg (Var n_id)]
191 dfListComp c_ty c_id n_ty n_id (GuardStmt guard locn : quals)
192 = dsExpr guard `thenDs` \ core_guard ->
193 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
194 returnDs (mkCoreIfThenElse core_guard core_rest (Var n_id))
196 dfListComp c_ty c_id n_ty n_id (LetStmt binds : quals)
197 -- new in 1.3, local bindings
198 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
199 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
200 returnDs (mkCoLetsAny core_binds core_rest)
202 dfListComp c_ty c_id n_ty n_id (BindStmt pat list1 locn : quals)
203 -- evaluate the two lists
204 = dsExpr list1 `thenDs` \ core_list1 ->
206 -- find the required type
208 let p_ty = outPatType pat
209 b_ty = n_ty -- alias b_ty to n_ty
210 fn_ty = mkFunTys [p_ty, b_ty] b_ty
211 lst_ty = coreExprType core_list1
214 -- create some new local id's
216 newSysLocalsDs [b_ty,p_ty,fn_ty,lst_ty] `thenDs` \ [b,p,fn,lst] ->
218 -- build rest of the comprehesion
220 dfListComp c_ty c_id b_ty b quals `thenDs` \ core_rest ->
221 -- build the pattern match
223 matchSimply (Var p) ListCompMatch pat b_ty core_rest (Var b) `thenDs` \ core_expr ->
225 -- now build the outermost foldr, and return
229 [NonRec fn (mkValLam [p, b] core_expr),
230 NonRec lst core_list1]
231 (mkFoldr p_ty n_ty fn n_id lst)
235 = mkValApp (mkTyApp (Var foldrId) [a,b]) [VarArg f, VarArg z, VarArg xs]