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