Exposing newtype coercions to Haskell

Nicolas Frisby nicolas.frisby at gmail.com
Wed Jul 3 21:19:27 CEST 2013

On Wed, Jul 3, 2013 at 5:33 AM, Joachim Breitner
<mail at joachim-breitner.de>wrote:

> [snip]
> strange, why did I miss that?
> But I can’t get [the GlobalRdrEnv lookup] to work, even looking up an
> element that I took from
> the GRE itself returns []:
>     let e' = head (head (occEnvElts env))
>     putMsgS $ showSDoc dflags (ppr e')
>     putMsgS $ showSDoc dflags (ppr (lookupGRE_RdrName (nameRdrName
> (gre_name e')) env))
> prints:
>         GHC.NT.Type.NT
>           imported from `GHC.NT.Type' at GHC/NT.hs:5:1-18
>           (and originally defined at GHC/NT/Type.hs:6:6-7)
>         []
> Also, trying to construct a RdrName that I can look up fails:
>     let rdrName = mkRdrQual (mkModuleName "GHC.NT.Type") (mkTcOcc "NT")
>     putMsgS $ showSDoc dflags (ppr (lookupGRE_RdrName rdrName env))
> prints also just [].
> What am I doing wrong?
> Thanks,
> Joachim

lookupGRE_RdrName looks something up in the environment and then prunes the
results via `pickGREs`. I suspect your lookup is succeeding, but all of
your results are getting pruned away.

> pickGREs :: RdrName -> [GlobalRdrElt] -> [GlobalRdrElt]
> -- ^ Take a list of GREs which have the right OccName

> -- Pick those GREs that are suitable for this RdrName

> -- And for those, keep only only the Provenances that are suitable

> -- *****Only used for Qual and Unqual, not Orig or Exact*****

Emphasis mine. NB that `nameRdrName` builds a RdrName via the Exact
constructor. That's why your first attempt failed (I think).

Your second attempt fails for a sneakier reason, something I posted an
email about a little while ago (cf "invoking front-end phases from within a
GHC plugin?").

The GlobalRdrEnv is keyed on the getUnique result of an OccName. That
unique is derived from the unique of the FastString contained in the
OccName. The uniques of FastStrings are allocated linearly via a global
variable. Unfortunately, your plugin image and the hosting compiler's image
have distinct copies of this global variable, so FastStrings from your
plugin do not get the same unique as the similar FastStrings that were used
to build the GlobalRdrEnv's keys. Your plugin creates FastStrings when
calling things like `mkTcOcc`, for example.

Unless we eventually include the FastStrings' global variable in the
`CoreMonad.reinitializeGlobals` mechanism, I think the available workaround
here is to filter `occEnvElts env` for gre_names where `occNameString` and
`occNameSpace` match what you're looking for. Robustness may require also
constraining the gre_prov field.

Good luck.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20130703/b18719bc/attachment.htm>

More information about the Glasgow-haskell-users mailing list