A couple of GHC-API questions

Eric Seidel gridaphobe at gmail.com
Thu Jul 24 17:14:17 UTC 2014


Sorry for the double email Simon, I hadn’t signed up for ghc-devs so my response was rejected..

---

Thanks for getting back to me so quickly! 

On July 24, 2014 at 0:33:44, Simon Peyton Jones (simonpj at microsoft.com) wrote: 
> Eric 
> 
> I'd like to help but I don't understand the question. What do you mean by "resolve" all 
> the types that are imported from module A? 
> 
> If you have a TyCon (for A.Foo) in your hand, it has a Name. That Name tells you where it comes 
> from. Inside it you will find its DataCons which also come from A. And so on. 

Sorry for the confusion, I’m actually asking about how to get the right Name. When we first parse a specification, all of the TyCons etc. are plain Strings, i.e. we have a type 

    data RefType tycon tyvar = … 
    type ParsedRefType = RefType String String 

But we don’t want to deal with Strings, we want to use GHC’s TyCons and TyVars 

    type GHCRefType = RefType TyCon TyVar 

so we need a function that maps Strings to Names (and then finding the TyCon is easy).  

This is what I mean by “resolving” the types. For single-module programs this is trivial, we can do something like 

    resolve x = do rn <- hscParseIdentifier x 
                   hscTcRnLookupRdrName rn 

For multi-module programs it becomes trickier because we also have to resolve the types that we’ve imported from other modules. So, using my example from earlier, when we type-check module B we have to turn the String “Foo” into the Name A.Foo. This is problematic because module B imported module A qualified, so “Foo” is not in scope inside B but “A.Foo” is.  

I believe GHC might be avoiding this issue via the .hi files, so when you import a Type from another module, it is already using TyCons instead of Strings. 

> Later you say "is there a simple way to ask GHC to resolve the name x in the context of module 
> m". You could mean 
> * Imagine that the unqualified name "x" appeared in module m. How do I look it up, in m's 
> top-level lexical environment. 
> but I don’t think that is what you mean. 

I think this is almost exactly what I mean, except that I want to be able to look up the unqualified “x” as well as the qualified “SomeModuleThatMayHaveBeenRenamed.x” inside m’s top-level environment. This is more or less what the DynamicLoading.lookupRdrNameInModuleForPlugins function that I’ve copied and tweaked does, but it requires the presence of the original source code. I’m hoping there may be some other function out there that does the same thing without requiring the source code, so we can use it for the base libraries as well. 

> I'm confused. Could you be more concrete? 
> Possibly this may help? https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/NameType 

Thanks for the reference, I’m familiar with most of the info there already from browsing GHC’s source, but this is laid out much more newcomer-friendly manner :) 

Hopefully I’ve made my question a bit clearer. 

Eric 


More information about the ghc-devs mailing list