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 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
13 IMPORT_DELOOPER(DsLoop) -- break dsExpr-ish loop
15 import {-# SOURCE #-} DsExpr ( dsExpr )
16 import {-# SOURCE #-} DsBinds ( dsBinds )
19 import HsSyn ( Stmt(..), HsExpr, HsBinds )
20 import TcHsSyn ( SYN_IE(TypecheckedStmt), SYN_IE(TypecheckedHsExpr) , SYN_IE(TypecheckedHsBinds) )
21 import DsHsSyn ( outPatType )
24 import DsMonad -- the monadery used in the desugarer
27 import CmdLineOpts ( opt_FoldrBuildOn )
28 import CoreUtils ( coreExprType, mkCoreIfThenElse )
29 import Id ( SYN_IE(Id) )
30 import PrelVals ( mkBuild, foldrId )
31 import Type ( mkTyVarTy, mkForAllTy, mkFunTys, mkFunTy, SYN_IE(Type) )
32 import TysPrim ( alphaTy )
33 import TysWiredIn ( nilDataCon, consDataCon, listTyCon )
34 import TyVar ( alphaTyVar )
35 import Match ( matchSimply )
39 List comprehensions may be desugared in one of two ways: ``ordinary''
40 (as you would expect if you read SLPJ's book) and ``with foldr/build
41 turned on'' (if you read Gill {\em et al.}'s paper on the subject).
43 There will be at least one ``qualifier'' in the input.
46 dsListComp :: [TypecheckedStmt]
47 -> Type -- Type of list elements
50 dsListComp quals elt_ty
51 | not opt_FoldrBuildOn -- Be boring
52 = deListComp quals nil_expr
54 | otherwise -- foldr/build lives!
55 = newTyVarsDs [alphaTyVar] `thenDs` \ [n_tyvar] ->
57 alpha_to_alpha = alphaTy `mkFunTy` alphaTy
59 n_ty = mkTyVarTy n_tyvar
60 c_ty = mkFunTys [elt_ty, n_ty] n_ty
61 g_ty = mkForAllTy alphaTyVar (
62 (elt_ty `mkFunTy` alpha_to_alpha)
67 newSysLocalsDs [c_ty,n_ty,g_ty] `thenDs` \ [c, n, g] ->
71 quals `thenDs` \ result ->
73 returnDs (mkBuild elt_ty n_tyvar c n g result)
75 nil_expr = mkCon nilDataCon [] [elt_ty] []
78 %************************************************************************
80 \subsection[DsListComp-ordinary]{Ordinary desugaring of list comprehensions}
82 %************************************************************************
84 Just as in Phil's chapter~7 in SLPJ, using the rules for
85 optimally-compiled list comprehensions. This is what Kevin followed
86 as well, and I quite happily do the same. The TQ translation scheme
87 transforms a list of qualifiers (either boolean expressions or
88 generators) into a single expression which implements the list
89 comprehension. Because we are generating 2nd-order polymorphic
90 lambda-calculus, calls to NIL and CONS must be applied to a type
91 argument, as well as their usual value arguments.
93 TE << [ e | qs ] >> = TQ << [ e | qs ] ++ Nil (typeOf e) >>
96 TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>
99 TQ << [ e | b , qs ] ++ L >> =
100 if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>
103 TQ << [ e | p <- L1, qs ] ++ L2 >> =
109 (( \ TE << p >> -> ( TQ << [e | qs] ++ (h u3) >> )) u2)
114 "h", "u1", "u2", and "u3" are new variables.
117 @deListComp@ is the TQ translation scheme. Roughly speaking, @dsExpr@
118 is the TE translation scheme. Note that we carry around the @L@ list
119 already desugared. @dsListComp@ does the top TE rule mentioned above.
122 deListComp :: [TypecheckedStmt] -> CoreExpr -> DsM CoreExpr
124 deListComp [ReturnStmt expr] list -- Figure 7.4, SLPJ, p 135, rule C above
125 = dsExpr expr `thenDs` \ core_expr ->
126 mkConDs consDataCon [TyArg (coreExprType core_expr), VarArg core_expr, VarArg list]
128 deListComp (GuardStmt guard locn : quals) list -- rule B above
129 = dsExpr guard `thenDs` \ core_guard ->
130 deListComp quals list `thenDs` \ core_rest ->
131 returnDs (mkCoreIfThenElse core_guard core_rest list)
133 -- [e | let B, qs] = let B in [e | qs]
134 deListComp (LetStmt binds : quals) list
135 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
136 deListComp quals list `thenDs` \ core_rest ->
137 returnDs (mkCoLetsAny core_binds core_rest)
139 deListComp (BindStmt pat list1 locn : quals) core_list2 -- rule A' above
140 = dsExpr list1 `thenDs` \ core_list1 ->
142 u3_ty@u1_ty = coreExprType core_list1 -- two names, same thing
144 -- u1_ty is a [alpha] type, and u2_ty = alpha
145 u2_ty = outPatType pat
147 res_ty = coreExprType core_list2
148 h_ty = u1_ty `mkFunTy` res_ty
150 newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty] `thenDs` \ [h, u1, u2, u3] ->
152 -- the "fail" value ...
153 mkAppDs (Var h) [VarArg (Var u3)] `thenDs` \ core_fail ->
154 deListComp quals core_fail `thenDs` \ rest_expr ->
155 matchSimply (Var u2) ListCompMatch pat res_ty
156 rest_expr core_fail `thenDs` \ core_match ->
157 mkAppDs (Var h) [VarArg core_list1] `thenDs` \ letrec_body ->
165 [(nilDataCon, [], core_list2),
166 (consDataCon, [u2, u3], core_match)]
172 %************************************************************************
174 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
176 %************************************************************************
178 @dfListComp@ are the rules used with foldr/build turned on:
180 TE < [ e | ] >> c n = c e n
181 TE << [ e | b , q ] >> c n = if b then TE << [ e | q ] >> c n else n
182 TE << [ e | p <- l , q ] c n = foldr
183 (\ TE << p >> b -> TE << [ e | q ] >> c b
187 dfListComp :: Type -> Id -- 'c'; its type and id
188 -> Type -> Id -- 'n'; its type and id
189 -> [TypecheckedStmt] -- the rest of the qual's
192 dfListComp c_ty c_id n_ty n_id [ReturnStmt expr]
193 = dsExpr expr `thenDs` \ core_expr ->
194 mkAppDs (Var c_id) [VarArg core_expr, VarArg (Var n_id)]
196 dfListComp c_ty c_id n_ty n_id (GuardStmt guard locn : quals)
197 = dsExpr guard `thenDs` \ core_guard ->
198 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
199 returnDs (mkCoreIfThenElse core_guard core_rest (Var n_id))
201 dfListComp c_ty c_id n_ty n_id (LetStmt binds : quals)
202 -- new in 1.3, local bindings
203 = dsBinds False{-don't auto scc-} binds `thenDs` \ core_binds ->
204 dfListComp c_ty c_id n_ty n_id quals `thenDs` \ core_rest ->
205 returnDs (mkCoLetsAny core_binds core_rest)
207 dfListComp c_ty c_id n_ty n_id (BindStmt pat list1 locn : quals)
208 -- evaluate the two lists
209 = dsExpr list1 `thenDs` \ core_list1 ->
211 -- find the required type
213 let p_ty = outPatType pat
214 b_ty = n_ty -- alias b_ty to n_ty
215 fn_ty = mkFunTys [p_ty, b_ty] b_ty
216 lst_ty = coreExprType core_list1
219 -- create some new local id's
221 newSysLocalsDs [b_ty,p_ty,fn_ty,lst_ty] `thenDs` \ [b,p,fn,lst] ->
223 -- build rest of the comprehesion
225 dfListComp c_ty c_id b_ty b quals `thenDs` \ core_rest ->
226 -- build the pattern match
228 matchSimply (Var p) ListCompMatch pat b_ty core_rest (Var b) `thenDs` \ core_expr ->
230 -- now build the outermost foldr, and return
234 [NonRec fn (mkValLam [p, b] core_expr),
235 NonRec lst core_list1]
236 (mkFoldr p_ty n_ty fn n_id lst)
240 = mkValApp (mkTyApp (Var foldrId) [a,b]) [VarArg f, VarArg z, VarArg xs]