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 {- --------- DO THIS WHEN VarMonoBind binds a "name" rather than a "Id"
71 collectBinders :: Bind name (InPat name) -> [name]
72 collectBinders = collectGenericBinders collectPatBinders
73 collectTypedBinders :: TypecheckedBind -> TypecheckedPat -> [name]
74 collectTypedBinders = collectGenericBinders collectTypedPatBinders
76 collectGenericBinders :: (pat -> [name]) -> Bind name pat -> [name]
77 collectGenericBinders pat_fn EmptyBind = []
78 collectGenericBinders pat_fn (NonRecBind monobinds)
79 = collectGenericMonoBinders pat_fn monobinds
80 collectGenericBinders pat_fn (RecBind monobinds)
81 = collectGenericMonoBinders pat_fn monobinds
83 collectMonoBinders :: MonoBinds name (InPat name) -> [name]
84 collectMonoBinders = collectGenericMonoBinders collectPatBinders
87 collectGenericMonoBinders :: (pat -> [name]) -> MonoBinds name pat -> [name]
88 collectGenericMonoBinders pat_fn EmptyMonoBinds = []
89 collectGenericMonoBinders pat_fn (AndMonoBinds bs1 bs2)
90 = (collectGenericMonoBinders pat_fn bs1) ++ (collectGenericMonoBinders pat_fn bs2)
91 collectGenericMonoBinders pat_fn (PatMonoBind pat grhss_w_binds locn)
93 collectGenericMonoBinders pat_fn (FunMonoBind f matches locn) = [f]
94 collectGenericMonoBinders pat_fn (VarMonoBind v expr) = [v]
98 -- ------- UNTIL THEN, WE DUPLICATE CODE -----------}
100 collectBinders :: Bind name (InPat name) -> [name]
101 collectBinders EmptyBind = []
102 collectBinders (NonRecBind monobinds) = collectMonoBinders monobinds
103 collectBinders (RecBind monobinds) = collectMonoBinders monobinds
105 collectTypedBinders :: TypecheckedBind -> [Id]
106 collectTypedBinders EmptyBind = []
107 collectTypedBinders (NonRecBind monobinds) = collectTypedMonoBinders monobinds
108 collectTypedBinders (RecBind monobinds) = collectTypedMonoBinders monobinds
110 collectMonoBinders :: MonoBinds name (InPat name) -> [name]
111 collectMonoBinders EmptyMonoBinds = []
112 collectMonoBinders (PatMonoBind pat grhss_w_binds _) = collectPatBinders pat
113 collectMonoBinders (FunMonoBind f matches _) = [f]
114 collectMonoBinders (VarMonoBind v expr) = error "collectMonoBinders"
115 collectMonoBinders (AndMonoBinds bs1 bs2)
116 = (collectMonoBinders bs1) ++ (collectMonoBinders bs2)
118 collectTypedMonoBinders :: TypecheckedMonoBinds -> [Id]
119 collectTypedMonoBinders EmptyMonoBinds = []
120 collectTypedMonoBinders (PatMonoBind pat grhss_w_binds _) = collectTypedPatBinders pat
121 collectTypedMonoBinders (FunMonoBind f matches _) = [f]
122 collectTypedMonoBinders (VarMonoBind v expr) = [v]
123 collectTypedMonoBinders (AndMonoBinds bs1 bs2)
124 = (collectTypedMonoBinders bs1) ++ (collectTypedMonoBinders bs2)
126 -- ---------- END OF DUPLICATED CODE
128 -- We'd like the binders -- and where they came from --
129 -- so we can make new ones with equally-useful origin info.
131 collectMonoBindersAndLocs
132 :: MonoBinds name (InPat name) -> [(name, SrcLoc)]
134 collectMonoBindersAndLocs EmptyMonoBinds = []
136 collectMonoBindersAndLocs (AndMonoBinds bs1 bs2)
137 = collectMonoBindersAndLocs bs1 ++ collectMonoBindersAndLocs bs2
139 collectMonoBindersAndLocs (PatMonoBind pat grhss_w_binds locn)
140 = collectPatBinders pat `zip` repeat locn
142 collectMonoBindersAndLocs (FunMonoBind f matches locn) = [(f, locn)]
144 collectMonoBindersAndLocs (VarMonoBind v expr)
145 = trace "collectMonoBindersAndLocs:VarMonoBind" []
146 -- ToDo: this is dubious, i.e., wrong, but harmless?
149 %************************************************************************
151 \subsection[AbsSynFuns-Expr]{Help functions: @Expr@}
153 %************************************************************************
155 And some little help functions that remove redundant redundancy:
157 mkTyApp :: TypecheckedExpr -> [UniType] -> TypecheckedExpr
158 mkTyApp expr [] = expr
159 mkTyApp expr tys = TyApp expr tys
161 mkDictApp :: TypecheckedExpr -> [DictVar] -> TypecheckedExpr
162 mkDictApp expr [] = expr
163 mkDictApp expr dict_vars = DictApp expr dict_vars
165 mkTyLam :: [TyVar] -> TypecheckedExpr -> TypecheckedExpr
166 mkTyLam [] expr = expr
167 mkTyLam tyvars expr = TyLam tyvars expr
169 mkDictLam :: [DictVar] -> TypecheckedExpr -> TypecheckedExpr
170 mkDictLam [] expr = expr
171 mkDictLam dicts expr = DictLam dicts expr
174 %************************************************************************
176 \subsection[AbsSynFuns-Qual]{Help functions: @Quals@}
178 %************************************************************************
182 collectParQualBinders :: RenamedParQuals -> [Name]
183 collectParQualBinders (AndParQuals q1 q2)
184 = collectParQualBinders q1 ++ collectParQualBinders q2
186 collectParQualBinders (DrawnGenIn pats pat expr)
187 = concat ((map collectPatBinders pats)++[collectPatBinders pat])
189 collectParQualBinders (IndexGen exprs pat expr)
190 = (collectPatBinders pat)
192 collectParQualBinders (ParFilter expr) = []
193 #endif {- Data Parallel HAskell -}
196 %************************************************************************
198 \subsection[AbsSynFuns-ParQuals]{Help functions: @ParQuals@}
200 %************************************************************************
203 collectQualBinders :: [RenamedQual] -> [Name]
205 collectQualBinders quals
206 = concat (map collect quals)
208 collect (GeneratorQual pat expr) = collectPatBinders pat
209 collect (FilterQual expr) = []
212 %************************************************************************
214 \subsection[AbsSynFuns-pats]{Help functions: patterns}
216 %************************************************************************
218 With un-parameterised patterns, we have to have ``duplicate'' copies
219 of one or two functions:
221 collectPatBinders :: InPat a -> [a]
222 collectPatBinders (VarPatIn var) = [var]
223 collectPatBinders (LazyPatIn pat) = collectPatBinders pat
224 collectPatBinders (AsPatIn a pat) = a : (collectPatBinders pat)
225 collectPatBinders (ConPatIn c pats) = concat (map collectPatBinders pats)
226 collectPatBinders (ConOpPatIn p1 c p2)= (collectPatBinders p1) ++ (collectPatBinders p2)
227 collectPatBinders (ListPatIn pats) = concat (map collectPatBinders pats)
228 collectPatBinders (TuplePatIn pats) = concat (map collectPatBinders pats)
229 collectPatBinders (NPlusKPatIn n _) = [n]
231 collectPatBinders (ProcessorPatIn pats pat)
232 = concat (map collectPatBinders pats) ++ (collectPatBinders pat)
234 collectPatBinders any_other_pat = [ {-no binders-} ]
237 Nota bene: DsBinds relies on the fact that at least for simple
238 tuple patterns @collectTypedPatBinders@ returns the binders in
239 the same order as they appear in the tuple.
242 collectTypedPatBinders :: TypecheckedPat -> [Id]
243 collectTypedPatBinders (VarPat var) = [var]
244 collectTypedPatBinders (LazyPat pat) = collectTypedPatBinders pat
245 collectTypedPatBinders (AsPat a pat) = a : (collectTypedPatBinders pat)
246 collectTypedPatBinders (ConPat _ _ pats) = concat (map collectTypedPatBinders pats)
247 collectTypedPatBinders (ConOpPat p1 _ p2 _) = (collectTypedPatBinders p1) ++ (collectTypedPatBinders p2)
248 collectTypedPatBinders (ListPat t pats) = concat (map collectTypedPatBinders pats)
249 collectTypedPatBinders (TuplePat pats) = concat (map collectTypedPatBinders pats)
250 collectTypedPatBinders (NPlusKPat n _ _ _ _ _) = [n]
252 collectTypedPatBinders (ProcessorPat pats _ pat)
253 = (concat (map collectTypedPatBinders pats)) ++
254 (collectTypedPatBinders pat)
255 #endif {- Data Parallel Haskell -}
256 collectTypedPatBinders any_other_pat = [ {-no binders-} ]
259 %************************************************************************
261 \subsection[AbsSynFuns-MonoType]{Help functions: @MonoType@}
263 %************************************************************************
265 Get the type variable names from a @MonoType@. Don't use class @Eq@
266 because @ProtoNames@ aren't in it.
269 extractMonoTyNames :: (name -> name -> Bool) -> MonoType name -> [name]
271 extractMonoTyNames eq monotype
274 get (MonoTyVar name) acc | name `is_elem` acc = acc
275 | otherwise = name : acc
276 get (MonoTyCon con tys) acc = foldr get acc tys
277 get (ListMonoTy ty) acc = get ty acc
278 get (FunMonoTy ty1 ty2) acc = get ty1 (get ty2 acc)
279 get (TupleMonoTy tys) acc
280 = foldr get_poly acc tys
282 get_poly (UnoverloadedTy ty) acc = get ty acc
283 get_poly (ForAllTy _ ty) acc = get ty acc
284 get_poly (OverloadedTy ctxt ty) acc = panic "extractMonoTyNames"
285 get (MonoDict _ ty) acc = get ty acc
286 get (MonoTyVarTemplate _) acc = acc
288 get (MonoTyProc tys ty) acc = foldr get (get ty acc) tys
289 get (MonoTyPod ty) acc = get ty acc
290 #endif {- Data Parallel Haskell -}
293 is_elem n (x:xs) = n `eq` x || n `is_elem` xs
296 @cmpInstanceTypes@ compares two @MonoType@s which are being used as
297 ``instance types.'' This is used when comparing as-yet-unrenamed
298 instance decls to eliminate duplicates. We allow things (e.g.,
299 overlapping instances) which standard Haskell doesn't, so we must
300 cater for that. Generally speaking, the instance-type
301 ``shape''-checker in @tcInstDecl@ will catch any mischief later on.
303 All we do is call @cmpMonoType@, passing it a tyvar-comparing function
304 that always claims that tyvars are ``equal;'' the result is that we
305 end up comparing the non-tyvar-ish structure of the two types.
308 cmpInstanceTypes :: ProtoNameMonoType -> ProtoNameMonoType -> TAG_
310 cmpInstanceTypes ty1 ty2
311 = cmpMonoType funny_cmp ty1 ty2
313 funny_cmp :: ProtoName -> ProtoName -> TAG_
315 {- The only case we are really trying to catch
316 is when both types are tyvars: which are both
317 "Unk"s and names that start w/ a lower-case letter! (Whew.)
319 funny_cmp (Unk u1) (Unk u2)
320 | isLower s1 && isLower s2 = EQ_
325 funny_cmp x y = cmpProtoName x y -- otherwise completely normal
328 @getNonPrelOuterTyCon@ is a yukky function required when deciding
329 whether to import an instance decl. If the class name or type
330 constructor are ``wanted'' then we should import it, otherwise not.
331 But the built-in core constructors for lists, tuples and arrows are
332 never ``wanted'' in this sense. @getNonPrelOuterTyCon@ catches just a
333 user-defined tycon and returns it.
336 getNonPrelOuterTyCon :: ProtoNameMonoType -> Maybe ProtoName
338 getNonPrelOuterTyCon (MonoTyCon con _) = Just con
339 getNonPrelOuterTyCon _ = Nothing
342 %************************************************************************
344 \subsection[AbsSynFuns-mentioned-vars]{Collect mentioned variables}
346 %************************************************************************
348 This is just a {\em hack} whichs collects, from a module body, all the
349 variables that are ``mentioned,'' either as top-level binders or as
350 free variables. We can then use this list when walking over
351 interfaces, using it to avoid imported variables that are patently of
354 We have to be careful to look out for \tr{M..} constructs in the
355 export list; if so, the game is up (and we must so report).
359 getMentionedVars :: PreludeNameFun -- a prelude-name lookup function, so
360 -- we can avoid recording prelude things
362 -> [IE]{-exports-} -- All the bits of the module body to
363 -> [ProtoNameFixityDecl]-- look in for "mentioned" vars.
364 -> [ProtoNameClassDecl]
365 -> [ProtoNameInstDecl]
368 -> (Bool, -- True <=> M.. construct in exports
369 [FAST_STRING]) -- list of vars "mentioned" in the module body
371 getMentionedVars val_nf exports fixes class_decls inst_decls binds
372 = case (mention_IE exports) of { (module_dotdot_seen, export_mentioned) ->
374 concat [export_mentioned,
375 mention_Fixity fixes,
376 mention_ClassDecls val_nf class_decls,
377 mention_InstDecls val_nf inst_decls,
378 mention_Binds val_nf True{-top-level-} binds])
383 mention_IE :: [IE] -> (Bool, [FAST_STRING])
386 = foldr men (False, []) exps
388 men (IEVar str) (dotdot_seen, so_far) = (dotdot_seen, str : so_far)
389 men (IEModuleContents _) (_, so_far) = (True, so_far)
390 men other_ie acc = acc
394 mention_Fixity :: [ProtoNameFixityDecl] -> [FAST_STRING]
396 mention_Fixity fixity_decls = []
397 -- ToDo: if we ever do something proper with fixity declarations,
398 -- this might need to do something.
402 mention_ClassDecls :: PreludeNameFun -> [ProtoNameClassDecl] -> [FAST_STRING]
404 mention_ClassDecls val_nf [] = []
405 mention_ClassDecls val_nf (ClassDecl _ _ _ _ binds _ _ : rest)
406 = mention_MonoBinds val_nf True{-toplev-} binds
407 ++ mention_ClassDecls val_nf rest
411 mention_InstDecls :: PreludeNameFun -> [ProtoNameInstDecl] -> [FAST_STRING]
413 mention_InstDecls val_nf [] = []
414 mention_InstDecls val_nf (InstDecl _ _ _ binds _ _ _ _ _ _ : rest)
415 = mention_MonoBinds val_nf True{-toplev-} binds
416 ++ mention_InstDecls val_nf rest
420 mention_Binds :: PreludeNameFun -> Bool -> ProtoNameBinds -> [FAST_STRING]
422 mention_Binds val_nf toplev EmptyBinds = []
423 mention_Binds val_nf toplev (ThenBinds a b)
424 = mention_Binds val_nf toplev a ++ mention_Binds val_nf toplev b
425 mention_Binds val_nf toplev (SingleBind a) = mention_Bind val_nf toplev a
426 mention_Binds val_nf toplev (BindWith a _) = mention_Bind val_nf toplev a
430 mention_Bind :: PreludeNameFun -> Bool -> ProtoNameBind -> [FAST_STRING]
432 mention_Bind val_nf toplev EmptyBind = []
433 mention_Bind val_nf toplev (NonRecBind a) = mention_MonoBinds val_nf toplev a
434 mention_Bind val_nf toplev (RecBind a) = mention_MonoBinds val_nf toplev a
438 mention_MonoBinds :: PreludeNameFun -> Bool -> ProtoNameMonoBinds -> [FAST_STRING]
440 mention_MonoBinds val_nf toplev EmptyMonoBinds = []
441 mention_MonoBinds val_nf toplev (AndMonoBinds a b)
442 = mention_MonoBinds val_nf toplev a ++ mention_MonoBinds val_nf toplev b
443 mention_MonoBinds val_nf toplev (PatMonoBind p gb _)
445 rest = mention_GRHSsAndBinds val_nf gb
448 then (map stringify (collectPatBinders p)) ++ rest
451 mention_MonoBinds val_nf toplev (FunMonoBind v ms _)
453 rest = concat (map (mention_Match val_nf) ms)
455 if toplev then (stringify v) : rest else rest
457 stringify :: ProtoName -> FAST_STRING
458 stringify (Unk s) = s
462 mention_Match :: PreludeNameFun -> ProtoNameMatch -> [FAST_STRING]
464 mention_Match val_nf (PatMatch _ m) = mention_Match val_nf m
465 mention_Match val_nf (GRHSMatch gb) = mention_GRHSsAndBinds val_nf gb
469 mention_GRHSsAndBinds :: PreludeNameFun -> ProtoNameGRHSsAndBinds -> [FAST_STRING]
471 mention_GRHSsAndBinds val_nf (GRHSsAndBindsIn gs bs)
472 = mention_GRHSs val_nf gs ++ mention_Binds val_nf False bs
476 mention_GRHSs :: PreludeNameFun -> [ProtoNameGRHS] -> [FAST_STRING]
478 mention_GRHSs val_nf grhss
479 = concat (map mention_grhs grhss)
481 mention_grhs (OtherwiseGRHS e _) = mention_Expr val_nf [] e
482 mention_grhs (GRHS g e _)
483 = mention_Expr val_nf [] g ++ mention_Expr val_nf [] e
487 mention_Expr :: PreludeNameFun -> [FAST_STRING] -> ProtoNameExpr -> [FAST_STRING]
489 mention_Expr val_nf acc (Var v)
491 Unk str | _LENGTH_ str >= 3
492 -> case (val_nf str) of
497 mention_Expr val_nf acc (Lit _) = acc
498 mention_Expr val_nf acc (Lam m) = acc ++ (mention_Match val_nf m)
499 mention_Expr val_nf acc (App a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
500 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
501 mention_Expr val_nf acc (SectionL a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
502 mention_Expr val_nf acc (SectionR a b) = mention_Expr val_nf (mention_Expr val_nf acc a) b
503 mention_Expr val_nf acc (CCall _ es _ _ _) = mention_Exprs val_nf acc es
504 mention_Expr val_nf acc (SCC _ e) = mention_Expr val_nf acc e
505 mention_Expr val_nf acc (Case e ms) = mention_Expr val_nf acc e ++ concat (map (mention_Match val_nf) ms)
506 mention_Expr val_nf acc (ListComp e q) = mention_Expr val_nf acc e ++ mention_Quals val_nf q
507 mention_Expr val_nf acc (Let b e) = (mention_Expr val_nf acc e) ++ (mention_Binds val_nf False{-not toplev-} b)
508 mention_Expr val_nf acc (ExplicitList es) = mention_Exprs val_nf acc es
509 mention_Expr val_nf acc (ExplicitTuple es) = mention_Exprs val_nf acc es
510 mention_Expr val_nf acc (ExprWithTySig e _) = mention_Expr val_nf acc e
511 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
512 mention_Expr val_nf acc (ArithSeqIn s) = mention_ArithSeq val_nf acc s
514 mention_Expr val_nf acc (ParallelZF e q) = (mention_Expr val_nf acc e) ++
515 (mention_ParQuals val_nf q)
516 mention_Expr val_nf acc (ExplicitPodIn es) = mention_Exprs val_nf acc es
517 mention_Expr val_nf acc (ExplicitProcessor es e) = mention_Expr val_nf (mention_Exprs val_nf acc es) e
518 #endif {- Data Parallel Haskell -}
522 mention_Exprs :: PreludeNameFun -> [FAST_STRING] -> [ProtoNameExpr] -> [FAST_STRING]
524 mention_Exprs val_nf acc [] = acc
525 mention_Exprs val_nf acc (e:es) = mention_Exprs val_nf (mention_Expr val_nf acc e) es
529 mention_ArithSeq :: PreludeNameFun -> [FAST_STRING] -> ProtoNameArithSeqInfo -> [FAST_STRING]
531 mention_ArithSeq val_nf acc (From e1)
532 = mention_Expr val_nf acc e1
533 mention_ArithSeq val_nf acc (FromThen e1 e2)
534 = mention_Expr val_nf (mention_Expr val_nf acc e1) e2
535 mention_ArithSeq val_nf acc (FromTo e1 e2)
536 = mention_Expr val_nf (mention_Expr val_nf acc e1) e2
537 mention_ArithSeq val_nf acc (FromThenTo e1 e2 e3)
538 = mention_Expr val_nf (mention_Expr val_nf (mention_Expr val_nf acc e1) e2) e3
542 mention_Quals :: PreludeNameFun -> [ProtoNameQual] -> [FAST_STRING]
544 mention_Quals val_nf quals
545 = concat (map mention quals)
547 mention (GeneratorQual _ e) = mention_Expr val_nf [] e
548 mention (FilterQual e) = mention_Expr val_nf [] e
553 mention_ParQuals :: PreludeNameFun -> ProtoNameParQuals -> [FAST_STRING]
554 mention_ParQuals val_nf (ParFilter e) = mention_Expr val_nf [] e
555 mention_ParQuals val_nf (DrawnGenIn _ _ e) = mention_Expr val_nf [] e
556 mention_ParQuals val_nf (AndParQuals a b) = mention_ParQuals val_nf a ++
557 mention_ParQuals val_nf b
558 mention_ParQuals val_nf (IndexGen es _ e) = mention_Exprs val_nf [] es
559 ++ mention_Expr val_nf [] e
560 #endif {- Data Parallel Haskell -}
562 {- END OLD:MENTIONED -}