37a72050c52343b6224b2943acd54a7b8513e6ce
[ghc-hetmet.git] / compiler / hsSyn / HsUtils.lhs
1
2 %
3 % (c) The University of Glasgow, 1992-2006
4 %
5
6 Here we collect a variety of helper functions that construct or
7 analyse HsSyn.  All these functions deal with generic HsSyn; functions
8 which deal with the intantiated versions are located elsewhere:
9
10    Parameterised by     Module
11    ----------------     -------------
12    RdrName              parser/RdrHsSyn
13    Name                 rename/RnHsSyn
14    Id                   typecheck/TcHsSyn       
15
16 \begin{code}
17 module HsUtils where
18
19 import HsBinds
20 import HsExpr
21 import HsPat
22 import HsTypes  
23 import HsLit
24
25 import RdrName
26 import Var
27 import Coercion
28 import Type
29 import DataCon
30 import Name
31 import BasicTypes
32 import SrcLoc
33 import FastString
34 import Outputable
35 import Util
36 import Bag
37 \end{code}
38
39
40 %************************************************************************
41 %*                                                                      *
42         Some useful helpers for constructing syntax
43 %*                                                                      *
44 %************************************************************************
45
46 These functions attempt to construct a not-completely-useless SrcSpan
47 from their components, compared with the nl* functions below which
48 just attach noSrcSpan to everything.
49
50 \begin{code}
51 mkHsPar :: LHsExpr id -> LHsExpr id
52 mkHsPar e = L (getLoc e) (HsPar e)
53
54 mkSimpleMatch :: [LPat id] -> LHsExpr id -> LMatch id
55 mkSimpleMatch pats rhs 
56   = L loc $
57     Match pats Nothing (unguardedGRHSs rhs)
58   where
59     loc = case pats of
60                 []      -> getLoc rhs
61                 (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
62
63 unguardedGRHSs :: LHsExpr id -> GRHSs id
64 unguardedGRHSs rhs = GRHSs (unguardedRHS rhs) emptyLocalBinds
65
66 unguardedRHS :: LHsExpr id -> [LGRHS id]
67 unguardedRHS rhs@(L loc _) = [L loc (GRHS [] rhs)]
68
69 mkHsAppTy :: LHsType name -> LHsType name -> LHsType name
70 mkHsAppTy t1 t2 = addCLoc t1 t2 (HsAppTy t1 t2)
71
72 mkHsApp :: LHsExpr name -> LHsExpr name -> LHsExpr name
73 mkHsApp e1 e2 = addCLoc e1 e2 (HsApp e1 e2)
74
75 nlHsTyApp :: name -> [Type] -> LHsExpr name
76 nlHsTyApp fun_id tys = noLoc (HsWrap (mkWpTyApps tys) (HsVar fun_id))
77
78 mkLHsWrap :: HsWrapper -> LHsExpr id -> LHsExpr id
79 mkLHsWrap co_fn (L loc e) = L loc (mkHsWrap co_fn e)
80
81 mkHsWrap :: HsWrapper -> HsExpr id -> HsExpr id
82 mkHsWrap co_fn e | isIdHsWrapper co_fn = e
83                  | otherwise           = HsWrap co_fn e
84
85 mkHsWrapCoI :: CoercionI -> HsExpr id -> HsExpr id
86 mkHsWrapCoI IdCo     e = e
87 mkHsWrapCoI (ACo co) e = mkHsWrap (WpCast co) e
88
89 coiToHsWrapper :: CoercionI -> HsWrapper
90 coiToHsWrapper IdCo     = idHsWrapper
91 coiToHsWrapper (ACo co) = WpCast co
92
93 mkHsLam :: [LPat id] -> LHsExpr id -> LHsExpr id
94 mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam matches))
95         where
96           matches = mkMatchGroup [mkSimpleMatch pats body]
97
98 mkMatchGroup :: [LMatch id] -> MatchGroup id
99 mkMatchGroup matches = MatchGroup matches placeHolderType
100
101 mkHsDictLet :: LHsBinds Id -> LHsExpr Id -> LHsExpr Id
102 -- Used for the dictionary bindings gotten from TcSimplify
103 -- We make them recursive to be on the safe side
104 mkHsDictLet binds expr 
105   | isEmptyLHsBinds binds = expr
106   | otherwise             = L (getLoc expr) (HsLet (HsValBinds val_binds) expr)
107                           where
108                             val_binds = ValBindsOut [(Recursive, binds)] []
109
110 mkHsConApp :: DataCon -> [Type] -> [HsExpr Id] -> LHsExpr Id
111 -- Used for constructing dictionary terms etc, so no locations 
112 mkHsConApp data_con tys args 
113   = foldl mk_app (nlHsTyApp (dataConWrapId data_con) tys) args
114   where
115     mk_app f a = noLoc (HsApp f (noLoc a))
116
117 mkSimpleHsAlt :: LPat id -> LHsExpr id -> LMatch id
118 -- A simple lambda with a single pattern, no binds, no guards; pre-typechecking
119 mkSimpleHsAlt pat expr 
120   = mkSimpleMatch [pat] expr
121
122 -------------------------------
123 -- These are the bits of syntax that contain rebindable names
124 -- See RnEnv.lookupSyntaxName
125
126 mkHsIntegral   :: Integer -> PostTcType -> HsOverLit id
127 mkHsFractional :: Rational -> PostTcType -> HsOverLit id
128 mkHsIsString   :: FastString -> PostTcType -> HsOverLit id
129 mkHsDo         :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> HsExpr id
130
131 mkNPat      :: HsOverLit id -> Maybe (SyntaxExpr id) -> Pat id
132 mkNPlusKPat :: Located id -> HsOverLit id -> Pat id
133
134 mkTransformStmt   :: [LStmt idL] -> LHsExpr idR                -> StmtLR idL idR
135 mkTransformByStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
136
137 mkGroupUsingStmt   :: [LStmt idL]                -> LHsExpr idR -> StmtLR idL idR
138 mkGroupByStmt      :: [LStmt idL] -> LHsExpr idR                -> StmtLR idL idR
139 mkGroupByUsingStmt :: [LStmt idL] -> LHsExpr idR -> LHsExpr idR -> StmtLR idL idR
140
141 mkExprStmt :: LHsExpr idR -> StmtLR idL idR
142 mkBindStmt :: LPat idL -> LHsExpr idR -> StmtLR idL idR
143
144 emptyRecStmt :: StmtLR idL idR
145 mkRecStmt    :: [LStmtLR idL idR] -> StmtLR idL idR
146
147
148 mkHsIntegral   i       = OverLit (HsIntegral   i)  noRebindableInfo noSyntaxExpr
149 mkHsFractional f       = OverLit (HsFractional f)  noRebindableInfo noSyntaxExpr
150 mkHsIsString   s       = OverLit (HsIsString   s)  noRebindableInfo noSyntaxExpr
151
152 noRebindableInfo :: Bool
153 noRebindableInfo = error "noRebindableInfo"     -- Just another placeholder; 
154
155 mkHsDo ctxt stmts body = HsDo ctxt stmts body placeHolderType
156
157 mkNPat lit neg     = NPat lit neg noSyntaxExpr
158 mkNPlusKPat id lit = NPlusKPat id lit noSyntaxExpr noSyntaxExpr
159
160 mkTransformStmt   stmts usingExpr        = TransformStmt (stmts, []) usingExpr Nothing
161 mkTransformByStmt stmts usingExpr byExpr = TransformStmt (stmts, []) usingExpr (Just byExpr)
162
163 mkGroupUsingStmt   stmts usingExpr        = GroupStmt (stmts, []) (GroupByNothing usingExpr)
164 mkGroupByStmt      stmts byExpr           = GroupStmt (stmts, []) (GroupBySomething (Right noSyntaxExpr) byExpr)
165 mkGroupByUsingStmt stmts byExpr usingExpr = GroupStmt (stmts, []) (GroupBySomething (Left usingExpr) byExpr)
166
167 mkExprStmt expr     = ExprStmt expr noSyntaxExpr placeHolderType
168 mkBindStmt pat expr = BindStmt pat expr noSyntaxExpr noSyntaxExpr
169
170 emptyRecStmt = RecStmt { recS_stmts = [], recS_later_ids = [], recS_rec_ids = []
171                        , recS_ret_fn = noSyntaxExpr, recS_mfix_fn = noSyntaxExpr
172                        , recS_bind_fn = noSyntaxExpr
173                        , recS_rec_rets = [], recS_dicts = emptyLHsBinds }
174
175 mkRecStmt stmts = emptyRecStmt { recS_stmts = stmts }
176
177 -------------------------------
178 --- A useful function for building @OpApps@.  The operator is always a
179 -- variable, and we don't know the fixity yet.
180 mkHsOpApp :: LHsExpr id -> id -> LHsExpr id -> HsExpr id
181 mkHsOpApp e1 op e2 = OpApp e1 (noLoc (HsVar op)) (error "mkOpApp:fixity") e2
182
183 mkHsSplice :: LHsExpr RdrName -> HsSplice RdrName
184 mkHsSplice e = HsSplice unqualSplice e
185
186 unqualSplice :: RdrName
187 unqualSplice = mkRdrUnqual (mkVarOccFS (fsLit "splice"))
188                 -- A name (uniquified later) to
189                 -- identify the splice
190
191 mkHsQuasiQuote :: RdrName -> SrcSpan -> FastString -> HsQuasiQuote RdrName
192 mkHsQuasiQuote quoter span quote = HsQuasiQuote quoter span quote
193
194 unqualQuasiQuote :: RdrName
195 unqualQuasiQuote = mkRdrUnqual (mkVarOccFS (fsLit "quasiquote"))
196                 -- A name (uniquified later) to
197                 -- identify the quasi-quote
198
199 mkHsString :: String -> HsLit
200 mkHsString s = HsString (mkFastString s)
201
202 -------------
203 userHsTyVarBndrs :: [Located name] -> [Located (HsTyVarBndr name)]
204 userHsTyVarBndrs bndrs = [ L loc (UserTyVar v) | L loc v <- bndrs ]
205 \end{code}
206
207
208 %************************************************************************
209 %*                                                                      *
210         Constructing syntax with no location info
211 %*                                                                      *
212 %************************************************************************
213
214 \begin{code}
215 nlHsVar :: id -> LHsExpr id
216 nlHsVar n = noLoc (HsVar n)
217
218 nlHsLit :: HsLit -> LHsExpr id
219 nlHsLit n = noLoc (HsLit n)
220
221 nlVarPat :: id -> LPat id
222 nlVarPat n = noLoc (VarPat n)
223
224 nlLitPat :: HsLit -> LPat id
225 nlLitPat l = noLoc (LitPat l)
226
227 nlHsApp :: LHsExpr id -> LHsExpr id -> LHsExpr id
228 nlHsApp f x = noLoc (HsApp f x)
229
230 nlHsIntLit :: Integer -> LHsExpr id
231 nlHsIntLit n = noLoc (HsLit (HsInt n))
232
233 nlHsApps :: id -> [LHsExpr id] -> LHsExpr id
234 nlHsApps f xs = foldl nlHsApp (nlHsVar f) xs
235              
236 nlHsVarApps :: id -> [id] -> LHsExpr id
237 nlHsVarApps f xs = noLoc (foldl mk (HsVar f) (map HsVar xs))
238                  where
239                    mk f a = HsApp (noLoc f) (noLoc a)
240
241 nlConVarPat :: id -> [id] -> LPat id
242 nlConVarPat con vars = nlConPat con (map nlVarPat vars)
243
244 nlInfixConPat :: id -> LPat id -> LPat id -> LPat id
245 nlInfixConPat con l r = noLoc (ConPatIn (noLoc con) (InfixCon l r))
246
247 nlConPat :: id -> [LPat id] -> LPat id
248 nlConPat con pats = noLoc (ConPatIn (noLoc con) (PrefixCon pats))
249
250 nlNullaryConPat :: id -> LPat id
251 nlNullaryConPat con = noLoc (ConPatIn (noLoc con) (PrefixCon []))
252
253 nlWildConPat :: DataCon -> LPat RdrName
254 nlWildConPat con = noLoc (ConPatIn (noLoc (getRdrName con))
255                                    (PrefixCon (nOfThem (dataConSourceArity con) nlWildPat)))
256
257 nlWildPat :: LPat id
258 nlWildPat  = noLoc (WildPat placeHolderType)    -- Pre-typechecking
259
260 nlHsDo :: HsStmtContext Name -> [LStmt id] -> LHsExpr id -> LHsExpr id
261 nlHsDo ctxt stmts body = noLoc (mkHsDo ctxt stmts body)
262
263 nlHsOpApp :: LHsExpr id -> id -> LHsExpr id -> LHsExpr id
264 nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
265
266 nlHsLam  :: LMatch id -> LHsExpr id
267 nlHsPar  :: LHsExpr id -> LHsExpr id
268 nlHsIf   :: LHsExpr id -> LHsExpr id -> LHsExpr id -> LHsExpr id
269 nlHsCase :: LHsExpr id -> [LMatch id] -> LHsExpr id
270 nlList   :: [LHsExpr id] -> LHsExpr id
271
272 nlHsLam match           = noLoc (HsLam (mkMatchGroup [match]))
273 nlHsPar e               = noLoc (HsPar e)
274 nlHsIf cond true false  = noLoc (HsIf cond true false)
275 nlHsCase expr matches   = noLoc (HsCase expr (mkMatchGroup matches))
276 nlList exprs            = noLoc (ExplicitList placeHolderType exprs)
277
278 nlHsAppTy :: LHsType name -> LHsType name -> LHsType name
279 nlHsTyVar :: name                         -> LHsType name
280 nlHsFunTy :: LHsType name -> LHsType name -> LHsType name
281
282 nlHsAppTy f t           = noLoc (HsAppTy f t)
283 nlHsTyVar x             = noLoc (HsTyVar x)
284 nlHsFunTy a b           = noLoc (HsFunTy a b)
285
286 nlHsTyConApp :: name -> [LHsType name] -> LHsType name
287 nlHsTyConApp tycon tys  = foldl nlHsAppTy (nlHsTyVar tycon) tys
288 \end{code}
289
290 Tuples.  All these functions are *pre-typechecker* because they lack
291 types on the tuple.
292
293 \begin{code}
294 mkLHsTupleExpr :: [LHsExpr a] -> LHsExpr a
295 -- Makes a pre-typechecker boxed tuple, deals with 1 case
296 mkLHsTupleExpr [e] = e
297 mkLHsTupleExpr es  = noLoc $ ExplicitTuple (map Present es) Boxed
298
299 mkLHsVarTuple :: [a] -> LHsExpr a
300 mkLHsVarTuple ids  = mkLHsTupleExpr (map nlHsVar ids)
301
302 nlTuplePat :: [LPat id] -> Boxity -> LPat id
303 nlTuplePat pats box = noLoc (TuplePat pats box placeHolderType)
304
305 missingTupArg :: HsTupArg a
306 missingTupArg = Missing placeHolderType
307 \end{code}
308
309 %************************************************************************
310 %*                                                                      *
311                 Bindings; with a location at the top
312 %*                                                                      *
313 %************************************************************************
314
315 \begin{code}
316 mkFunBind :: Located id -> [LMatch id] -> HsBind id
317 -- Not infix, with place holders for coercion and free vars
318 mkFunBind fn ms = FunBind { fun_id = fn, fun_infix = False, fun_matches = mkMatchGroup ms,
319                             fun_co_fn = idHsWrapper, bind_fvs = placeHolderNames,
320                             fun_tick = Nothing }
321
322
323 mkHsVarBind :: SrcSpan -> id -> LHsExpr id -> LHsBind id
324 mkHsVarBind loc var rhs = mk_easy_FunBind loc var [] rhs
325
326 mkVarBind :: id -> LHsExpr id -> LHsBind id
327 mkVarBind var rhs = L (getLoc rhs) $
328                     VarBind { var_id = var, var_rhs = rhs, var_inline = False }
329
330 ------------
331 mk_easy_FunBind :: SrcSpan -> id -> [LPat id]
332                 -> LHsExpr id -> LHsBind id
333
334 mk_easy_FunBind loc fun pats expr
335   = L loc $ mkFunBind (L loc fun) [mkMatch pats expr emptyLocalBinds]
336
337 ------------
338 mk_FunBind :: SrcSpan -> id
339            -> [([LPat id], LHsExpr id)]
340            -> LHsBind id
341
342 mk_FunBind _   _   [] = panic "TcGenDeriv:mk_FunBind"
343 mk_FunBind loc fun pats_and_exprs
344   = L loc $ mkFunBind (L loc fun) matches
345   where
346     matches = [mkMatch p e emptyLocalBinds | (p,e) <-pats_and_exprs]
347
348 ------------
349 mkMatch :: [LPat id] -> LHsExpr id -> HsLocalBinds id -> LMatch id
350 mkMatch pats expr binds
351   = noLoc (Match (map paren pats) Nothing 
352                  (GRHSs (unguardedRHS expr) binds))
353   where
354     paren lp@(L l p) | hsPatNeedsParens p = L l (ParPat lp) 
355                      | otherwise          = lp
356 \end{code}
357
358
359 %************************************************************************
360 %*                                                                      *
361         Collecting binders from HsBindGroups and HsBinds
362 %*                                                                      *
363 %************************************************************************
364
365 Get all the binders in some HsBindGroups, IN THE ORDER OF APPEARANCE. eg.
366
367 ...
368 where
369   (x, y) = ...
370   f i j  = ...
371   [a, b] = ...
372
373 it should return [x, y, f, a, b] (remember, order important).
374
375 \begin{code}
376 collectLocalBinders :: HsLocalBindsLR idL idR -> [Located idL]
377 collectLocalBinders (HsValBinds val_binds) = collectHsValBinders val_binds
378 collectLocalBinders (HsIPBinds _)   = []
379 collectLocalBinders EmptyLocalBinds = []
380
381 collectHsValBinders :: HsValBindsLR idL idR -> [Located idL]
382 collectHsValBinders (ValBindsIn  binds _) = collectHsBindLocatedBinders binds
383 collectHsValBinders (ValBindsOut binds _) = foldr collect_one [] binds
384   where
385    collect_one (_,binds) acc = foldrBag (collectAcc . unLoc) acc binds
386
387 collectAcc :: HsBindLR idL idR -> [Located idL] -> [Located idL]
388 collectAcc (PatBind { pat_lhs = p }) acc = collectLocatedPatBinders p ++ acc
389 collectAcc (FunBind { fun_id = f })  acc    = f : acc
390 collectAcc (VarBind { var_id = f })  acc    = noLoc f : acc
391 collectAcc (AbsBinds { abs_exports = dbinds, abs_binds = _binds }) acc
392   = [noLoc dp | (_,dp,_,_) <- dbinds] ++ acc
393         -- ++ foldr collectAcc acc binds
394         -- I don't think we want the binders from the nested binds
395         -- The only time we collect binders from a typechecked 
396         -- binding (hence see AbsBinds) is in zonking in TcHsSyn
397
398 collectHsBindBinders :: LHsBindsLR idL idR -> [idL]
399 collectHsBindBinders binds = map unLoc (collectHsBindLocatedBinders binds)
400
401 collectHsBindLocatedBinders :: LHsBindsLR idL idR -> [Located idL]
402 collectHsBindLocatedBinders binds = foldrBag (collectAcc . unLoc) [] binds
403 \end{code}
404
405
406 %************************************************************************
407 %*                                                                      *
408         Getting binders from statements
409 %*                                                                      *
410 %************************************************************************
411
412 \begin{code}
413 collectLStmtsBinders :: [LStmtLR idL idR] -> [Located idL]
414 collectLStmtsBinders = concatMap collectLStmtBinders
415
416 collectStmtsBinders :: [StmtLR idL idR] -> [Located idL]
417 collectStmtsBinders = concatMap collectStmtBinders
418
419 collectLStmtBinders :: LStmtLR idL idR -> [Located idL]
420 collectLStmtBinders = collectStmtBinders . unLoc
421
422 collectStmtBinders :: StmtLR idL idR -> [Located idL]
423   -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
424 collectStmtBinders (BindStmt pat _ _ _) = collectLocatedPatBinders pat
425 collectStmtBinders (LetStmt binds)      = collectLocalBinders binds
426 collectStmtBinders (ExprStmt _ _ _)     = []
427 collectStmtBinders (ParStmt xs)         = collectLStmtsBinders
428                                         $ concatMap fst xs
429 collectStmtBinders (TransformStmt (stmts, _) _ _) = collectLStmtsBinders stmts
430 collectStmtBinders (GroupStmt (stmts, _) _)       = collectLStmtsBinders stmts
431 collectStmtBinders (RecStmt { recS_stmts = ss })  = collectLStmtsBinders ss
432 \end{code}
433
434
435 %************************************************************************
436 %*                                                                      *
437 %*      Gathering stuff out of patterns
438 %*                                                                      *
439 %************************************************************************
440
441 This function @collectPatBinders@ works with the ``collectBinders''
442 functions for @HsBinds@, etc.  The order in which the binders are
443 collected is important; see @HsBinds.lhs@.
444
445 It collects the bounds *value* variables in renamed patterns; type variables
446 are *not* collected.
447
448 \begin{code}
449 collectPatBinders :: LPat a -> [a]
450 collectPatBinders pat = map unLoc (collectLocatedPatBinders pat)
451
452 collectLocatedPatBinders :: LPat a -> [Located a]
453 collectLocatedPatBinders pat = collectl pat []
454
455 collectPatsBinders :: [LPat a] -> [a]
456 collectPatsBinders pats = map unLoc (collectLocatedPatsBinders pats)
457
458 collectLocatedPatsBinders :: [LPat a] -> [Located a]
459 collectLocatedPatsBinders pats = foldr collectl [] pats
460
461 ---------------------
462 collectl :: LPat name -> [Located name] -> [Located name]
463 collectl (L l pat) bndrs
464   = go pat
465   where
466     go (VarPat var)               = L l var : bndrs
467     go (VarPatOut var bs)         = L l var : collectHsBindLocatedBinders bs 
468                                     ++ bndrs
469     go (WildPat _)                = bndrs
470     go (LazyPat pat)              = collectl pat bndrs
471     go (BangPat pat)              = collectl pat bndrs
472     go (AsPat a pat)              = a : collectl pat bndrs
473     go (ViewPat _ pat _)          = collectl pat bndrs
474     go (ParPat  pat)              = collectl pat bndrs
475                                   
476     go (ListPat pats _)           = foldr collectl bndrs pats
477     go (PArrPat pats _)           = foldr collectl bndrs pats
478     go (TuplePat pats _ _)        = foldr collectl bndrs pats
479                                   
480     go (ConPatIn _ ps)            = foldr collectl bndrs (hsConPatArgs ps)
481     go (ConPatOut {pat_args=ps})  = foldr collectl bndrs (hsConPatArgs ps)
482         -- See Note [Dictionary binders in ConPatOut]
483     go (LitPat _)                 = bndrs
484     go (NPat _ _ _)               = bndrs
485     go (NPlusKPat n _ _ _)        = n : bndrs
486                                   
487     go (SigPatIn pat _)           = collectl pat bndrs
488     go (SigPatOut pat _)          = collectl pat bndrs
489     go (QuasiQuotePat _)          = bndrs
490     go (TypePat _)                = bndrs
491     go (CoPat _ pat _)            = collectl (noLoc pat) bndrs
492 \end{code}
493
494 Note [Dictionary binders in ConPatOut]
495 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
496 Do *not* gather (a) dictionary and (b) dictionary bindings as binders
497 of a ConPatOut pattern.  For most calls it doesn't matter, because
498 it's pre-typechecker and there are no ConPatOuts.  But it does matter
499 more in the desugarer; for example, DsUtils.mkSelectorBinds uses
500 collectPatBinders.  In a lazy pattern, for example f ~(C x y) = ...,
501 we want to generate bindings for x,y but not for dictionaries bound by
502 C.  (The type checker ensures they would not be used.)
503
504 Desugaring of arrow case expressions needs these bindings (see DsArrows
505 and arrowcase1), but SPJ (Jan 2007) says it's safer for it to use its
506 own pat-binder-collector:
507
508 Here's the problem.  Consider
509
510 data T a where
511    C :: Num a => a -> Int -> T a
512
513 f ~(C (n+1) m) = (n,m)
514
515 Here, the pattern (C (n+1)) binds a hidden dictionary (d::Num a),
516 and *also* uses that dictionary to match the (n+1) pattern.  Yet, the
517 variables bound by the lazy pattern are n,m, *not* the dictionary d.
518 So in mkSelectorBinds in DsUtils, we want just m,n as the variables bound.
519
520 \begin{code}
521 collectSigTysFromPats :: [InPat name] -> [LHsType name]
522 collectSigTysFromPats pats = foldr collect_lpat [] pats
523
524 collectSigTysFromPat :: InPat name -> [LHsType name]
525 collectSigTysFromPat pat = collect_lpat pat []
526
527 collect_lpat :: InPat name -> [LHsType name] -> [LHsType name]
528 collect_lpat pat acc = collect_pat (unLoc pat) acc
529
530 collect_pat :: Pat name -> [LHsType name] -> [LHsType name]
531 collect_pat (SigPatIn pat ty)   acc = collect_lpat pat (ty:acc)
532 collect_pat (TypePat ty)        acc = ty:acc
533
534 collect_pat (LazyPat pat)       acc = collect_lpat pat acc
535 collect_pat (BangPat pat)       acc = collect_lpat pat acc
536 collect_pat (AsPat _ pat)       acc = collect_lpat pat acc
537 collect_pat (ParPat  pat)       acc = collect_lpat pat acc
538 collect_pat (ListPat pats _)    acc = foldr collect_lpat acc pats
539 collect_pat (PArrPat pats _)    acc = foldr collect_lpat acc pats
540 collect_pat (TuplePat pats _ _) acc = foldr collect_lpat acc pats
541 collect_pat (ConPatIn _ ps)     acc = foldr collect_lpat acc (hsConPatArgs ps)
542 collect_pat _                   acc = acc       -- Literals, vars, wildcard
543 \end{code}