[project @ 1996-06-05 06:44:31 by partain]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsListComp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[DsListComp]{Desugaring list comprehensions}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module DsListComp ( dsListComp ) where
10
11 IMP_Ubiq()
12 IMPORT_DELOOPER(DsLoop)         -- break dsExpr-ish loop
13
14 import HsSyn            ( Qual(..), HsExpr, HsBinds )
15 import TcHsSyn          ( TypecheckedQual(..), TypecheckedHsExpr(..) , TypecheckedHsBinds(..) )
16 import DsHsSyn          ( outPatType )
17 import CoreSyn
18
19 import DsMonad          -- the monadery used in the desugarer
20 import DsUtils
21
22 import CmdLineOpts      ( opt_FoldrBuildOn )
23 import CoreUtils        ( coreExprType, mkCoreIfThenElse )
24 import PrelVals         ( mkBuild, foldrId )
25 import Type             ( mkTyVarTy, mkForAllTy, mkFunTys )
26 import TysPrim          ( alphaTy )
27 import TysWiredIn       ( nilDataCon, consDataCon, listTyCon )
28 import TyVar            ( alphaTyVar )
29 import Match            ( matchSimply )
30 import Util             ( panic )
31 \end{code}
32
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).
36
37 There will be at least one ``qualifier'' in the input.
38
39 \begin{code}
40 dsListComp :: CoreExpr -> [TypecheckedQual] -> DsM CoreExpr
41
42 dsListComp expr quals
43   = let
44         expr_ty = coreExprType expr
45     in
46     if not opt_FoldrBuildOn then -- be boring
47         deListComp expr quals (nIL_EXPR expr_ty)
48
49     else -- foldr/build lives!
50         new_alpha_tyvar             `thenDs` \ (n_tyvar, n_ty) ->
51         let
52             alpha_to_alpha = mkFunTys [alphaTy] alphaTy
53
54             c_ty = mkFunTys [expr_ty, n_ty] n_ty
55             g_ty = mkForAllTy alphaTyVar (
56                         (mkFunTys [expr_ty, alpha_to_alpha] alpha_to_alpha))
57         in
58         newSysLocalsDs [c_ty,n_ty,g_ty]  `thenDs` \ [c, n, g] ->
59
60         dfListComp expr expr_ty
61                         c_ty c
62                         n_ty n
63                         quals       `thenDs` \ result ->
64
65         returnDs (mkBuild expr_ty n_tyvar c n g result)
66   where
67     nIL_EXPR ty = mkCon nilDataCon [] [ty] []
68
69     new_alpha_tyvar :: DsM (TyVar, Type)
70     new_alpha_tyvar
71       = newTyVarsDs [alphaTyVar]    `thenDs` \ [new_ty] ->
72         returnDs (new_ty, mkTyVarTy new_ty)
73 \end{code}
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection[DsListComp-ordinary]{Ordinary desugaring of list comprehensions}
78 %*                                                                      *
79 %************************************************************************
80
81 Just as in Phil's chapter~7 in SLPJ, using the rules for
82 optimally-compiled list comprehensions.  This is what Kevin followed
83 as well, and I quite happily do the same.  The TQ translation scheme
84 transforms a list of qualifiers (either boolean expressions or
85 generators) into a single expression which implements the list
86 comprehension.  Because we are generating 2nd-order polymorphic
87 lambda-calculus, calls to NIL and CONS must be applied to a type
88 argument, as well as their usual value arguments.
89 \begin{verbatim}
90 TE << [ e | qs ] >>  =  TQ << [ e | qs ] ++ Nil (typeOf e) >>
91
92 (Rule C)
93 TQ << [ e | ] ++ L >> = Cons (typeOf e) TE <<e>> TE <<L>>
94
95 (Rule B)
96 TQ << [ e | b , qs ] ++ L >> =
97     if TE << b >> then TQ << [ e | qs ] ++ L >> else TE << L >>
98
99 (Rule A')
100 TQ << [ e | p <- L1, qs ]  ++  L2 >> =
101   letrec
102     h = \ u1 ->
103           case u1 of
104             []        ->  TE << L2 >>
105             (u2 : u3) ->
106                   (( \ TE << p >> -> ( TQ << [e | qs]  ++  (h u3) >> )) u2)
107                     [] (h u3)
108   in
109     h ( TE << L1 >> )
110
111 "h", "u1", "u2", and "u3" are new variables.
112 \end{verbatim}
113
114 @deListComp@ is the TQ translation scheme.  Roughly speaking, @dsExpr@
115 is the TE translation scheme.  Note that we carry around the @L@ list
116 already desugared.  @dsListComp@ does the top TE rule mentioned above.
117
118 \begin{code}
119 deListComp :: CoreExpr -> [TypecheckedQual] -> CoreExpr -> DsM CoreExpr
120
121 deListComp expr [] list         -- Figure 7.4, SLPJ, p 135, rule C above
122   = mkConDs consDataCon [coreExprType expr] [expr, list]
123
124 deListComp expr (FilterQual filt : quals) list  -- rule B above
125   = dsExpr filt                `thenDs` \ core_filt ->
126     deListComp expr quals list `thenDs` \ core_rest ->
127     returnDs ( mkCoreIfThenElse core_filt core_rest list )
128
129 deListComp expr (LetQual binds : quals) list
130   = panic "deListComp:LetQual"
131
132 deListComp expr ((GeneratorQual pat list1):quals) core_list2 -- rule A' above
133   = dsExpr list1                    `thenDs` \ core_list1 ->
134     let
135         u3_ty@u1_ty = coreExprType core_list1   -- two names, same thing
136
137         -- u1_ty is a [alpha] type, and u2_ty = alpha
138         u2_ty = outPatType pat
139
140         res_ty = coreExprType core_list2
141         h_ty = mkFunTys [u1_ty] res_ty
142     in
143     newSysLocalsDs [h_ty, u1_ty, u2_ty, u3_ty]
144                                     `thenDs` \ [h', u1, u2, u3] ->
145     {-
146        Make the function h unfoldable by the deforester.
147        Since it only occurs once in the body, we can't get
148        an increase in code size by unfolding it.
149     -}
150     let
151         h = if False -- LATER: sw_chkr DoDeforest???
152             then panic "deListComp:deforest"
153                  -- replaceIdInfo h' (addInfo (getIdInfo h') DoDeforest)
154             else h'
155     in
156     -- the "fail" value ...
157     mkAppDs (Var h) [] [Var u3]  `thenDs` \ core_fail ->
158
159     deListComp expr quals core_fail `thenDs` \ rest_expr ->
160
161     matchSimply (Var u2) pat res_ty rest_expr core_fail `thenDs` \ core_match ->
162
163     mkAppDs (Var h) [] [core_list1]  `thenDs` \ letrec_body ->
164
165     returnDs (
166       mkCoLetrecAny [
167       ( h,
168         (Lam (ValBinder u1)
169          (Case (Var u1)
170             (AlgAlts
171               [(nilDataCon,  [], core_list2),
172                (consDataCon, [u2, u3], core_match)]
173             NoDefault)))
174       )] letrec_body
175     )
176 \end{code}
177
178 %************************************************************************
179 %*                                                                      *
180 \subsection[DsListComp-foldr-build]{Foldr/Build desugaring of list comprehensions}
181 %*                                                                      *
182 %************************************************************************
183
184 @dfListComp@ are the rules used with foldr/build turned on:
185 \begin{verbatim}
186 TE < [ e | ] >>          c n = c e n
187 TE << [ e | b , q ] >>   c n = if b then TE << [ e | q ] >> c n else n
188 TE << [ e | p <- l , q ] c n =  foldr
189                         (\ TE << p >> b -> TE << [ e | q ] >> c b
190                            _          b  -> b)  n l
191 \end{verbatim}
192 \begin{code}
193 dfListComp :: CoreExpr          -- the inside of the comp
194            -> Type                      -- the type of the inside
195            -> Type -> Id                -- 'c'; its type and id
196            -> Type -> Id                -- 'n'; its type and id
197            -> [TypecheckedQual]         -- the rest of the qual's
198            -> DsM CoreExpr
199
200 dfListComp expr expr_ty c_ty c_id n_ty n_id []
201   = mkAppDs (Var c_id) [] [expr, Var n_id]
202
203 dfListComp expr expr_ty c_ty c_id n_ty n_id (FilterQual filt : quals)
204   = dsExpr filt                                 `thenDs` \ core_filt ->
205     dfListComp expr expr_ty c_ty c_id n_ty n_id quals
206                                                 `thenDs` \ core_rest ->
207     returnDs (mkCoreIfThenElse core_filt core_rest (Var n_id))
208
209 dfListComp expr expr_ty c_ty c_id n_ty n_id (LetQual binds : quals)
210   = panic "dfListComp:LetQual"
211
212 dfListComp expr expr_ty c_ty c_id n_ty n_id (GeneratorQual pat list1 : quals)
213     -- evaluate the two lists
214   = dsExpr list1                                `thenDs` \ core_list1 ->
215
216     -- find the required type
217
218     let p_ty   = outPatType pat
219         b_ty   = n_ty           -- alias b_ty to n_ty
220         fn_ty  = mkFunTys [p_ty, b_ty] b_ty
221         lst_ty = coreExprType core_list1
222     in
223
224     -- create some new local id's
225
226     newSysLocalsDs [b_ty,p_ty,fn_ty,lst_ty]             `thenDs` \ [b,p,fn,lst] ->
227
228     -- build rest of the comprehesion
229
230     dfListComp expr expr_ty c_ty c_id b_ty b quals      `thenDs` \ core_rest ->
231     -- build the pattern match
232
233     matchSimply (Var p) pat b_ty core_rest (Var b)      `thenDs` \ core_expr ->
234
235     -- now build the outermost foldr, and return
236
237     returnDs (
238       mkCoLetsAny
239         [NonRec fn (mkValLam [p, b] core_expr),
240          NonRec lst core_list1]
241         (mkFoldr p_ty n_ty fn n_id lst)
242     )
243
244 mkFoldr a b f z xs
245   = mkValApp (mkTyApp (Var foldrId) [a,b]) [VarArg f, VarArg z, VarArg xs]
246 \end{code}