[Haskell-cafe] ghc-api Static Semantics?

JP Moresmau jpmoresmau at gmail.com
Thu Jan 26 17:33:16 CET 2012


Thomas, thank you for that explanation about the different type of
identifiers in the different phases of analysis. I've never seen that
information so clearly laid out before, can it be added to the wikis
(in http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/API
or http://www.haskell.org/haskellwiki/GHC/As_a_library maybe)? I think
it would be helpful to all people that want to dive into the GHC API.

On a side note, I'm going to do something very similar in my
BuildWrapper project (which is now the backend of the EclipseFP IDE
plugins): instead of going back to the API every time the user
requests to know the type of "something" in the AST, I'm thinking of
sending the whole typed AST to the Java code. Maybe that's something
Christopher could use. Both the BuildWrapper code and Thomas's scion
code are available on GitHub, as they provide examples on how to use
the GHC API.

JP


On Thu, Jan 26, 2012 at 2:31 PM, Thomas Schilling
<nominolo at googlemail.com> wrote:
>
>
> On 26 January 2012 09:24, Christopher Brown <cmb21 at st-andrews.ac.uk> wrote:
>> Hi Thomas,
>>
>> By static semantics I mean use and bind locations for every name in the
>> AST.
>
> Right, that's what the renamer does in GHC.  The GHC AST is parameterised
> over the type of identifiers used.  The three different identifier types
> are:
>
> RdrName: is the name as it occurred in source code. This is the output of
> the parser.
> Name: is basically RdrName + unique ID, so you can distinguish two "x"s
> bound at different locations (this is what you want). This is the output of
> the renamer.
> Id: is Name + Type information and consequently is the output of the type
> checker.
>
> Diagram:
>
>    String  --parser-->  HsModule RdrName  --renamer-->  HsModule Name
>  --type-checker-->  HsBinds Id
>
> Since you can't hook in-between renamer and type checker, it's perhaps more
> accurately depicted as:
>
>    String  --parser-->  HsModule RdrName  --renamer+type-checker-->
>  (HsModule Name,  HsBinds Id)
>
> The main reasons why it's tricky to use the GHC API are:
>
> You need to setup the environment of packages etc.  E.g., the renamer needs
> to look up imported modules to correctly resolve imported names (or give a
> error).
> The second is that the current API is not designed for external use.  As I
> mentioned, you cannot run renamer and typechecker independently, there are
> dozens of invariants, there are environments being updated by the various
> phases, etc.  For example, if you want to generate code it's probably best
> to either generate HsModure RdrName or perhaps the Template Haskell API
> (never tried that path).
>
>
> / Thomas
>
> --
> Push the envelope. Watch it bend.
>



-- 
JP Moresmau
http://jpmoresmau.blogspot.com/



More information about the Haskell-Cafe mailing list