2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[AbsSynFuns]{Abstract syntax: help functions}
7 #include "HsVersions.h"
10 collectTopLevelBinders,
11 collectBinders, collectTypedBinders,
13 collectMonoBindersAndLocs,
16 collectTypedPatBinders,
18 collectParQualBinders,
19 #endif {- Data Parallel Haskell -}
22 {-OLD:-}getMentionedVars, -- MENTIONED
36 import HsTypes ( cmpMonoType )
37 import Id ( Id, DictVar(..), DictFun(..) )
38 import Maybes ( Maybe(..) )
39 import ProtoName ( ProtoName(..), cmpProtoName )
40 import Rename ( PreludeNameFun(..) )
44 %************************************************************************
46 \subsection[AbsSynFuns-MonoBinds]{Bindings: @MonoBinds@}
48 %************************************************************************
50 Get all the binders in some @ProtoNameMonoBinds@, IN THE ORDER OF
59 it should return @[x, y, f, a, b]@ (remember, order important).
62 collectTopLevelBinders :: Binds name (InPat name) -> [name]
63 collectTopLevelBinders EmptyBinds = []
64 collectTopLevelBinders (SingleBind b) = collectBinders b
65 collectTopLevelBinders (BindWith b _) = collectBinders b
66 collectTopLevelBinders (ThenBinds b1 b2)
67 = (collectTopLevelBinders b1) ++ (collectTopLevelBinders b2)
69 collectBinders :: Bind name (InPat name) -> [name]
70 collectBinders EmptyBind = []
71 collectBinders (NonRecBind monobinds) = collectMonoBinders monobinds
72 collectBinders (RecBind monobinds) = collectMonoBinders monobinds
74 collectTypedBinders :: TypecheckedBind -> [Id]
75 collectTypedBinders EmptyBind = []
76 collectTypedBinders (NonRecBind monobinds) = collectTypedMonoBinders monobinds
77 collectTypedBinders (RecBind monobinds) = collectTypedMonoBinders monobinds
79 collectMonoBinders :: MonoBinds name (InPat name) -> [name]
80 collectMonoBinders EmptyMonoBinds = []
81 collectMonoBinders (PatMonoBind pat grhss_w_binds _) = collectPatBinders pat
82 collectMonoBinders (FunMonoBind f matches _) = [f]
83 collectMonoBinders (VarMonoBind v expr) = error "collectMonoBinders"
84 collectMonoBinders (AndMonoBinds bs1 bs2)
85 = (collectMonoBinders bs1) ++ (collectMonoBinders bs2)
87 collectTypedMonoBinders :: TypecheckedMonoBinds -> [Id]
88 collectTypedMonoBinders EmptyMonoBinds = []
89 collectTypedMonoBinders (PatMonoBind pat grhss_w_binds _) = collectTypedPatBinders pat
90 collectTypedMonoBinders (FunMonoBind f matches _) = [f]
91 collectTypedMonoBinders (VarMonoBind v expr) = [v]
92 collectTypedMonoBinders (AndMonoBinds bs1 bs2)
93 = (collectTypedMonoBinders bs1) ++ (collectTypedMonoBinders bs2)
95 -- We'd like the binders -- and where they came from --
96 -- so we can make new ones with equally-useful origin info.
98 collectMonoBindersAndLocs
99 :: MonoBinds name (InPat name) -> [(name, SrcLoc)]
101 collectMonoBindersAndLocs EmptyMonoBinds = []
103 collectMonoBindersAndLocs (AndMonoBinds bs1 bs2)
104 = collectMonoBindersAndLocs bs1 ++ collectMonoBindersAndLocs bs2
106 collectMonoBindersAndLocs (PatMonoBind pat grhss_w_binds locn)
107 = collectPatBinders pat `zip` repeat locn
109 collectMonoBindersAndLocs (FunMonoBind f matches locn) = [(f, locn)]
111 collectMonoBindersAndLocs (VarMonoBind v expr)
112 = trace "collectMonoBindersAndLocs:VarMonoBind" []
113 -- ToDo: this is dubious, i.e., wrong, but harmless?
116 %************************************************************************
118 \subsection[AbsSynFuns-Expr]{Help functions: @Expr@}
120 %************************************************************************
122 And some little help functions that remove redundant redundancy:
124 mkTyApp :: TypecheckedExpr -> [UniType] -> TypecheckedExpr
125 mkTyApp expr [] = expr
126 mkTyApp expr tys = TyApp expr tys
128 mkDictApp :: TypecheckedExpr -> [DictVar] -> TypecheckedExpr
129 mkDictApp expr [] = expr
130 mkDictApp expr dict_vars = DictApp expr dict_vars
132 mkTyLam :: [TyVar] -> TypecheckedExpr -> TypecheckedExpr
133 mkTyLam [] expr = expr
134 mkTyLam tyvars expr = TyLam tyvars expr
136 mkDictLam :: [DictVar] -> TypecheckedExpr -> TypecheckedExpr
137 mkDictLam [] expr = expr
138 mkDictLam dicts expr = DictLam dicts expr
141 %************************************************************************
143 \subsection[AbsSynFuns-Qual]{Help functions: @Quals@}
145 %************************************************************************
149 collectParQualBinders :: RenamedParQuals -> [Name]
150 collectParQualBinders (AndParQuals q1 q2)
151 = collectParQualBinders q1 ++ collectParQualBinders q2
153 collectParQualBinders (DrawnGenIn pats pat expr)
154 = concat ((map collectPatBinders pats)++[collectPatBinders pat])
156 collectParQualBinders (IndexGen exprs pat expr)
157 = (collectPatBinders pat)
159 collectParQualBinders (ParFilter expr) = []
160 #endif {- Data Parallel HAskell -}
163 %************************************************************************
165 \subsection[AbsSynFuns-ParQuals]{Help functions: @ParQuals@}
167 %************************************************************************
170 collectQualBinders :: [RenamedQual] -> [Name]
172 collectQualBinders quals
173 = concat (map collect quals)
175 collect (GeneratorQual pat expr) = collectPatBinders pat
176 collect (FilterQual expr) = []
179 %************************************************************************
181 \subsection[AbsSynFuns-pats]{Help functions: patterns}
183 %************************************************************************
185 With un-parameterised patterns, we have to have ``duplicate'' copies
186 of one or two functions:
188 collectPatBinders :: InPat a -> [a]
189 collectPatBinders (VarPatIn var) = [var]
190 collectPatBinders (LazyPatIn pat) = collectPatBinders pat
191 collectPatBinders (AsPatIn a pat) = a : (collectPatBinders pat)
192 collectPatBinders (ConPatIn c pats) = concat (map collectPatBinders pats)
193 collectPatBinders (ConOpPatIn p1 c p2)= (collectPatBinders p1) ++ (collectPatBinders p2)
194 collectPatBinders (ListPatIn pats) = concat (map collectPatBinders pats)
195 collectPatBinders (TuplePatIn pats) = concat (map collectPatBinders pats)
196 collectPatBinders (NPlusKPatIn n _) = [n]
198 collectPatBinders (ProcessorPatIn pats pat)
199 = concat (map collectPatBinders pats) ++ (collectPatBinders pat)
201 collectPatBinders any_other_pat = [ {-no binders-} ]
204 Nota bene: DsBinds relies on the fact that at least for simple
205 tuple patterns @collectTypedPatBinders@ returns the binders in
206 the same order as they appear in the tuple.
209 collectTypedPatBinders :: TypecheckedPat -> [Id]
210 collectTypedPatBinders (VarPat var) = [var]
211 collectTypedPatBinders (LazyPat pat) = collectTypedPatBinders pat
212 collectTypedPatBinders (AsPat a pat) = a : (collectTypedPatBinders pat)
213 collectTypedPatBinders (ConPat _ _ pats) = concat (map collectTypedPatBinders pats)
214 collectTypedPatBinders (ConOpPat p1 _ p2 _) = (collectTypedPatBinders p1) ++ (collectTypedPatBinders p2)
215 collectTypedPatBinders (ListPat t pats) = concat (map collectTypedPatBinders pats)
216 collectTypedPatBinders (TuplePat pats) = concat (map collectTypedPatBinders pats)
217 collectTypedPatBinders (NPlusKPat n _ _ _ _ _) = [n]
219 collectTypedPatBinders (ProcessorPat pats _ pat)
220 = (concat (map collectTypedPatBinders pats)) ++
221 (collectTypedPatBinders pat)
222 #endif {- Data Parallel Haskell -}
223 collectTypedPatBinders any_other_pat = [ {-no binders-} ]
226 %************************************************************************
228 \subsection[AbsSynFuns-MonoType]{Help functions: @MonoType@}
230 %************************************************************************
232 Get the type variable names from a @MonoType@. Don't use class @Eq@
233 because @ProtoNames@ aren't in it.
236 extractMonoTyNames :: (name -> name -> Bool) -> MonoType name -> [name]
238 extractMonoTyNames eq monotype
241 get (MonoTyVar name) acc | name `is_elem` acc = acc
242 | otherwise = name : acc
243 get (MonoTyCon con tys) acc = foldr get acc tys
244 get (ListMonoTy ty) acc = get ty acc
245 get (FunMonoTy ty1 ty2) acc = get ty1 (get ty2 acc)
246 get (TupleMonoTy tys) acc
247 = foldr get_poly acc tys
249 get_poly (UnoverloadedTy ty) acc = get ty acc
250 get_poly (ForAllTy _ ty) acc = get ty acc
251 get_poly (OverloadedTy ctxt ty) acc = panic "extractMonoTyNames"
252 get (MonoDict _ ty) acc = get ty acc
253 get (MonoTyVarTemplate _) acc = acc
255 get (MonoTyProc tys ty) acc = foldr get (get ty acc) tys
256 get (MonoTyPod ty) acc = get ty acc
257 #endif {- Data Parallel Haskell -}
260 is_elem n (x:xs) = n `eq` x || n `is_elem` xs
263 @cmpInstanceTypes@ compares two @MonoType@s which are being used as
264 ``instance types.'' This is used when comparing as-yet-unrenamed
265 instance decls to eliminate duplicates. We allow things (e.g.,
266 overlapping instances) which standard Haskell doesn't, so we must
267 cater for that. Generally speaking, the instance-type
268 ``shape''-checker in @tcInstDecl@ will catch any mischief later on.
270 All we do is call @cmpMonoType@, passing it a tyvar-comparing function
271 that always claims that tyvars are ``equal;'' the result is that we
272 end up comparing the non-tyvar-ish structure of the two types.
275 cmpInstanceTypes :: ProtoNameMonoType -> ProtoNameMonoType -> TAG_
277 cmpInstanceTypes ty1 ty2
278 = cmpMonoType funny_cmp ty1 ty2
280 funny_cmp :: ProtoName -> ProtoName -> TAG_
282 {- The only case we are really trying to catch
283 is when both types are tyvars: which are both
284 "Unk"s and names that start w/ a lower-case letter! (Whew.)
286 funny_cmp (Unk u1) (Unk u2)
287 | isLower s1 && isLower s2 = EQ_
292 funny_cmp x y = cmpProtoName x y -- otherwise completely normal
295 @getNonPrelOuterTyCon@ is a yukky function required when deciding
296 whether to import an instance decl. If the class name or type
297 constructor are ``wanted'' then we should import it, otherwise not.
298 But the built-in core constructors for lists, tuples and arrows are
299 never ``wanted'' in this sense. @getNonPrelOuterTyCon@ catches just a
300 user-defined tycon and returns it.
303 getNonPrelOuterTyCon :: ProtoNameMonoType -> Maybe ProtoName
305 getNonPrelOuterTyCon (MonoTyCon con _) = Just con
306 getNonPrelOuterTyCon _ = Nothing
309 %************************************************************************
311 \subsection[AbsSynFuns-mentioned-vars]{Collect mentioned variables}
313 %************************************************************************
315 This is just a {\em hack} whichs collects, from a module body, all the
316 variables that are ``mentioned,'' either as top-level binders or as
317 free variables. We can then use this list when walking over
318 interfaces, using it to avoid imported variables that are patently of
321 We have to be careful to look out for \tr{M..} constructs in the
322 export list; if so, the game is up (and we must so report).
326 getMentionedVars :: PreludeNameFun -- a prelude-name lookup function, so
327 -- we can avoid recording prelude things
329 -> [IE]{-exports-} -- All the bits of the module body to
330 -> [ProtoNameFixityDecl]-- look in for "mentioned" vars.
331 -> [ProtoNameClassDecl]
332 -> [ProtoNameInstDecl]
335 -> (Bool, -- True <=> M.. construct in exports
336 [FAST_STRING]) -- list of vars "mentioned" in the module body
338 getMentionedVars val_nf exports fixes class_decls inst_decls binds
339 = case (mention_IE exports) of { (module_dotdot_seen, export_mentioned) ->
341 concat [export_mentioned,
342 mention_Fixity fixes,
343 mention_ClassDecls val_nf class_decls,
344 mention_InstDecls val_nf inst_decls,
345 mention_Binds val_nf True{-top-level-} binds])
350 mention_IE :: [IE] -> (Bool, [FAST_STRING])
353 = foldr men (False, []) exps
355 men (IEVar str) (dotdot_seen, so_far) = (dotdot_seen, str : so_far)
356 men (IEModuleContents _) (_, so_far) = (True, so_far)
357 men other_ie acc = acc
361 mention_Fixity :: [ProtoNameFixityDecl] -> [FAST_STRING]
363 mention_Fixity fixity_decls = []
364 -- ToDo: if we ever do something proper with fixity declarations,
365 -- this might need to do something.
369 mention_ClassDecls :: PreludeNameFun -> [ProtoNameClassDecl] -> [FAST_STRING]
371 mention_ClassDecls val_nf [] = []
372 mention_ClassDecls val_nf (ClassDecl _ _ _ _ binds _ _ : rest)
373 = mention_MonoBinds val_nf True{-toplev-} binds
374 ++ mention_ClassDecls val_nf rest
378 mention_InstDecls :: PreludeNameFun -> [ProtoNameInstDecl] -> [FAST_STRING]
380 mention_InstDecls val_nf [] = []
381 mention_InstDecls val_nf (InstDecl _ _ _ binds _ _ _ _ _ _ : rest)
382 = mention_MonoBinds val_nf True{-toplev-} binds
383 ++ mention_InstDecls val_nf rest
387 mention_Binds :: PreludeNameFun -> Bool -> ProtoNameBinds -> [FAST_STRING]
389 mention_Binds val_nf toplev EmptyBinds = []
390 mention_Binds val_nf toplev (ThenBinds a b)
391 = mention_Binds val_nf toplev a ++ mention_Binds val_nf toplev b
392 mention_Binds val_nf toplev (SingleBind a) = mention_Bind val_nf toplev a
393 mention_Binds val_nf toplev (BindWith a _) = mention_Bind val_nf toplev a
397 mention_Bind :: PreludeNameFun -> Bool -> ProtoNameBind -> [FAST_STRING]
399 mention_Bind val_nf toplev EmptyBind = []
400 mention_Bind val_nf toplev (NonRecBind a) = mention_MonoBinds val_nf toplev a
401 mention_Bind val_nf toplev (RecBind a) = mention_MonoBinds val_nf toplev a
405 mention_MonoBinds :: PreludeNameFun -> Bool -> ProtoNameMonoBinds -> [FAST_STRING]
407 mention_MonoBinds val_nf toplev EmptyMonoBinds = []
408 mention_MonoBinds val_nf toplev (AndMonoBinds a b)
409 = mention_MonoBinds val_nf toplev a ++ mention_MonoBinds val_nf toplev b
410 mention_MonoBinds val_nf toplev (PatMonoBind p gb _)
412 rest = mention_GRHSsAndBinds val_nf gb
415 then (map stringify (collectPatBinders p)) ++ rest
418 mention_MonoBinds val_nf toplev (FunMonoBind v ms _)
420 rest = concat (map (mention_Match val_nf) ms)
422 if toplev then (stringify v) : rest else rest
424 stringify :: ProtoName -> FAST_STRING
425 stringify (Unk s) = s
429 mention_Match :: PreludeNameFun -> ProtoNameMatch -> [FAST_STRING]
431 mention_Match val_nf (PatMatch _ m) = mention_Match val_nf m
432 mention_Match val_nf (GRHSMatch gb) = mention_GRHSsAndBinds val_nf gb
436 mention_GRHSsAndBinds :: PreludeNameFun -> ProtoNameGRHSsAndBinds -> [FAST_STRING]
438 mention_GRHSsAndBinds val_nf (GRHSsAndBindsIn gs bs)
439 = mention_GRHSs val_nf gs ++ mention_Binds val_nf False bs
443 mention_GRHSs :: PreludeNameFun -> [ProtoNameGRHS] -> [FAST_STRING]
445 mention_GRHSs val_nf grhss
446 = concat (map mention_grhs grhss)
448 mention_grhs (OtherwiseGRHS e _) = mention_Expr val_nf [] e
449 mention_grhs (GRHS g e _)
450 = mention_Expr val_nf [] g ++ mention_Expr val_nf [] e
454 mention_Expr :: PreludeNameFun -> [FAST_STRING] -> ProtoNameExpr -> [FAST_STRING]
456 mention_Expr val_nf acc (Var v)
458 Unk str | _LENGTH_ str >= 3
459 -> case (val_nf str) of
464 mention_Expr val_nf acc (Lit _) = acc
465 mention_Expr val_nf acc (Lam m) = acc ++ (mention_Match val_nf m)
466 mention_Expr val_nf acc (App a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
467 mention_Expr val_nf acc (OpApp a b c) = mention_Expr val_nf (mention_Expr val_nf (mention_Expr val_nf acc a) b) c
468 mention_Expr val_nf acc (SectionL a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
469 mention_Expr val_nf acc (SectionR a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
470 mention_Expr val_nf acc (CCall _ es _ _ _) = mention_Exprs val_nf acc es
471 mention_Expr val_nf acc (SCC _ e) = mention_Expr val_nf acc e
472 mention_Expr val_nf acc (Case e ms) = mention_Expr val_nf acc e ++ concat (map (mention_Match val_nf) ms)
473 mention_Expr val_nf acc (ListComp e q) = mention_Expr val_nf acc e ++ mention_Quals val_nf q
474 mention_Expr val_nf acc (Let b e) = (mention_Expr val_nf acc e) ++ (mention_Binds val_nf False{-not toplev-} b)
475 mention_Expr val_nf acc (ExplicitList es) = mention_Exprs val_nf acc es
476 mention_Expr val_nf acc (ExplicitTuple es) = mention_Exprs val_nf acc es
477 mention_Expr val_nf acc (ExprWithTySig e _) = mention_Expr val_nf acc e
478 mention_Expr val_nf acc (If b t e) = mention_Expr val_nf (mention_Expr val_nf (mention_Expr val_nf acc b) t) e
479 mention_Expr val_nf acc (ArithSeqIn s) = mention_ArithSeq val_nf acc s
481 mention_Expr val_nf acc (ParallelZF e q) = (mention_Expr val_nf acc e) ++
482 (mention_ParQuals val_nf q)
483 mention_Expr val_nf acc (ExplicitPodIn es) = mention_Exprs val_nf acc es
484 mention_Expr val_nf acc (ExplicitProcessor es e) = mention_Expr val_nf (mention_Exprs val_nf acc es) e
485 #endif {- Data Parallel Haskell -}
489 mention_Exprs :: PreludeNameFun -> [FAST_STRING] -> [ProtoNameExpr] -> [FAST_STRING]
491 mention_Exprs val_nf acc [] = acc
492 mention_Exprs val_nf acc (e:es) = mention_Exprs val_nf (mention_Expr val_nf acc e) es
496 mention_ArithSeq :: PreludeNameFun -> [FAST_STRING] -> ProtoNameArithSeqInfo -> [FAST_STRING]
498 mention_ArithSeq val_nf acc (From e1)
499 = mention_Expr val_nf acc e1
500 mention_ArithSeq val_nf acc (FromThen e1 e2)
501 = mention_Expr val_nf (mention_Expr val_nf acc e1) e2
502 mention_ArithSeq val_nf acc (FromTo e1 e2)
503 = mention_Expr val_nf (mention_Expr val_nf acc e1) e2
504 mention_ArithSeq val_nf acc (FromThenTo e1 e2 e3)
505 = mention_Expr val_nf (mention_Expr val_nf (mention_Expr val_nf acc e1) e2) e3
509 mention_Quals :: PreludeNameFun -> [ProtoNameQual] -> [FAST_STRING]
511 mention_Quals val_nf quals
512 = concat (map mention quals)
514 mention (GeneratorQual _ e) = mention_Expr val_nf [] e
515 mention (FilterQual e) = mention_Expr val_nf [] e
520 mention_ParQuals :: PreludeNameFun -> ProtoNameParQuals -> [FAST_STRING]
521 mention_ParQuals val_nf (ParFilter e) = mention_Expr val_nf [] e
522 mention_ParQuals val_nf (DrawnGenIn _ _ e) = mention_Expr val_nf [] e
523 mention_ParQuals val_nf (AndParQuals a b) = mention_ParQuals val_nf a ++
524 mention_ParQuals val_nf b
525 mention_ParQuals val_nf (IndexGen es _ e) = mention_Exprs val_nf [] es
526 ++ mention_Expr val_nf [] e
527 #endif {- Data Parallel Haskell -}
529 {- END OLD:MENTIONED -}