Collecting all external names in a module

Simon Peyton-Jones simonpj at
Mon Sep 20 06:47:07 EDT 2010

I found the spot where the collected RdrNames are used to generate the unused import warnings, but I don't quite understand where they are gathered. Is there an AST traversal function somewhere that gathers these RdrNames? If so, I could use it as a blue print to write my own traversal.
No, it won't be useful. It's currently done as a side effect by the renamer.  One could add more, but I'm reluctant to do that. Better to design a separate traversal.

Could you expand a little bit on this design? Is the idea that the Gather data type carries functions to apply in different parts of the AST? What's "occ" short for, OccName? What about "del"?

Thomas had it right; it's just a particular kind of fold.  The key parts of the traversal would be:

*        Occurrences.    getExpr g (HsVar v) = g_occ g v

*        Combining.      getExpr g (HsApp e1 e2) = g_union (getExpr g e1) (getExpr g e2)

*        Binding>          getExpr (Lam v e) = g_del v (getExpr g e)

Does that help?  Maybe one could generalise a bit.

There are different kind of ASTs (e.g. after renaming, after type checking, etc), which one should I use if I want to gather all qualified names?

Well, the traversal function is polymorphic so you don't need to decide.

But you need to decide when you apply it.  When you say "gather all qualified names" do you really mean to gather only names that the programmer wrote qualified?  That is, gather, but not map? If so, you need to traverse before renaming, because the renamer throws away the info of whether the user wrote the thing qualified or not.

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Glasgow-haskell-users mailing list