Comparing instance heads

David Waern david.waern at gmail.com
Wed Nov 18 14:05:29 EST 2009


2009/11/15 David Waern <david.waern at gmail.com>:
> Hi,
>
> My little sunday afternoon project consists of implementing comments
> on instances in Haddock, but now I'm stuck.
>
> Here's the problem:
>
> Haddock associates an instance declaration with a comment using their
> respective SrcLocs. So this association has to be done using the
> instance declarations from the renamed syntax, because that seems like
> the last point where the SrcLocs of instances are retained (correct me
> if I'm wrong). This results in a map from instance heads to
> documentation. The map contains all instances that is defined in the
> module being processed.
>
> Later, Haddock uses the .hi files to get all instances belonging to a
> certain export item, using getAllInfo. Note that these are all
> instances known by GHC, so they may not only come from the package
> being processed. These are the instances shown in the documentation.
> For each such instance, I'd like to look up its documentation in the
> previously mentioned map. However, since there is no Id/Name for the
> instances in the map, this lookup has to be done based on the instance
> head itself.
>
> How do you suggest I do the comparison of instance heads? Is there
> something in GHC that can be used? Should the GHC API just provide
> Eq/Ord for HsType?

To be more precise, I want to know if an HsDecl.InstDecl and an
InstEnv.Instance represent the same instance.

Doing this by comparing the types seems complicated. After digging
around in the GHC code, I found that I could take the SrcSpan of the
Name of the DFunId in Instance, and compare it with the SrcSpan of the
instance head in InstDecl. That seems to work fine for normal
user-written instances, which is good enough for my use-case.

David



More information about the Cvs-ghc mailing list