Accessing the "original" names via GHC API

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing the "original" names via GHC API

Ranjit Jhala
Hi, 

I'm quite vexed trying to do the following. Suppose I have a file:

```
module Foo 

import qualified Data.Set as S

baz :: S.Set Int 
baz = S.empty
```

My goal is to write a function

   tyconString :: TyCon -> String 

(perhaps with extra parameters) such that given the 
`TyCon` corresponding to `Set`, I get back the "original" 
name `S.Set`, or even `Data.Set.Set`. 

Everything I've tried, which is fiddling with different variants of `PprStyle`, end up giving me `Data.Set.Base.Set`

Does anyone have a suggestion for how to proceed?

Thanks!

- Ranjit.



_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the "original" names via GHC API

Joachim Breitner-2
Hi Ranjit,

Am Dienstag, den 24.01.2017, 16:09 -0800 schrieb Ranjit Jhala:

> My goal is to write a function
>
>    tyconString :: TyCon -> String 
>
> (perhaps with extra parameters) such that given the 
> `TyCon` corresponding to `Set`, I get back the "original" 
> name `S.Set`, or even `Data.Set.Set`. 
>
> Everything I've tried, which is fiddling with different variants of
> `PprStyle`, end up giving me `Data.Set.Base.Set`
>
> Does anyone have a suggestion for how to proceed?
in a way, `Data.Set.Base.Set` is the “original”, proper name for Set,
everything else is just a local view on the name.

So, are you maybe looking for a way to get the “most natural way” to
print a name in a certain module context?

This functionality must exist somewhere, as ghci is printing out errors
this way. But it certainly would require an additional argument to
tyconString, to specify in which module to print the name.

Greetings,
Joachim


--
Joachim “nomeata” Breitner
  [hidden email]https://www.joachim-breitner.de/
  XMPP: [hidden email] • OpenPGP-Key: 0xF0FBF51F
  Debian Developer: [hidden email]
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the "original" names via GHC API

Ranjit Jhala
Dear Joachim,

You are right -- some more context.

Given 
  
  tc  :: TyCon 
  m   :: ModName
  env :: HscEnv 

I want to get a 

  s :: String 

such that _in_ the context given by `m` and `env` I can resolve `s` to get
back the original `TyCon`, e.g. something like

  L _ rn <- hscParseIdentifier env s
  name   <- lookupRdrName env modName rn

would then return `name :: Name` which corresponds to the original `TyCon`.

That is, the goal is _not_ pretty printing, but "serialization" into a String
representation that lets me recover the original `TyCon` later.

(Consequently, `"Data.Set.Base.Set"` doesn't work as the `Data.Set.Base` module is hidden and hence, when I try the above, GHC complains that the name is not in scope.

Does that clarify the problem?

Thanks!

- Ranjit.


On Tue, Jan 24, 2017 at 6:11 PM, Joachim Breitner <[hidden email]> wrote:
Hi Ranjit,

Am Dienstag, den 24.01.2017, 16:09 -0800 schrieb Ranjit Jhala:
> My goal is to write a function
>
>    tyconString :: TyCon -> String 
>
> (perhaps with extra parameters) such that given the 
> `TyCon` corresponding to `Set`, I get back the "original" 
> name `S.Set`, or even `Data.Set.Set`. 
>
> Everything I've tried, which is fiddling with different variants of
> `PprStyle`, end up giving me `Data.Set.Base.Set`
>
> Does anyone have a suggestion for how to proceed?

in a way, `Data.Set.Base.Set` is the “original”, proper name for Set,
everything else is just a local view on the name.

So, are you maybe looking for a way to get the “most natural way” to
print a name in a certain module context?

This functionality must exist somewhere, as ghci is printing out errors
this way. But it certainly would require an additional argument to
tyconString, to specify in which module to print the name.

Greetings,
Joachim


--
Joachim “nomeata” Breitner
  [hidden email]https://www.joachim-breitner.de/
  XMPP: [hidden email] • OpenPGP-Key: 0xF0FBF51F
  Debian Developer: [hidden email]

_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users



_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

RE: Accessing the "original" names via GHC API

Haskell - Glasgow-haskell-users mailing list
In reply to this post by Ranjit Jhala

The TyCon has a Name (use tyConName to get it).

The Name has a Module and an OccName (use nameModule and nameOccName to get them)

The OccName has a string (occNameString)

The Module has a ModuleName and a Package.

 

 

All of these will give the “original-name” info, ignoring what’s in scope.


Does that help?

 

S

 

From: Glasgow-haskell-users [mailto:[hidden email]] On Behalf Of Ranjit Jhala
Sent: 25 January 2017 00:09
To: [hidden email]
Subject: Accessing the "original" names via GHC API

 

Hi, 

 

I'm quite vexed trying to do the following. Suppose I have a file:

 

```

module Foo 

 

import qualified Data.Set as S

 

baz :: S.Set Int 

baz = S.empty

```

 

My goal is to write a function

 

   tyconString :: TyCon -> String 

 

(perhaps with extra parameters) such that given the 

`TyCon` corresponding to `Set`, I get back the "original" 

name `S.Set`, or even `Data.Set.Set`. 

 

Everything I've tried, which is fiddling with different variants of `PprStyle`, end up giving me `Data.Set.Base.Set`

 

Does anyone have a suggestion for how to proceed?

 

Thanks!

 

- Ranjit.

 

 


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the "original" names via GHC API

Edward Z. Yang
In reply to this post by Ranjit Jhala
Hi Ranjit,

Unfortunately you need more information to do this, since the
set of modules which are available for import can vary depending
on whether or not packages are hidden or not (not even counting
whether or not a module is exposed or not!)

The way GHC's pretty printer gives a good name is that it keeps
track of all of the names in scope and where they came from
in a GlobalRdrEnv.  The relevant code is in 'mkPrintUnqualified'
in HscTypes, but if you pretty print using user-style with
an appropriately set up GlobalRdrEnv you should
get the things you want.

Edward

Excerpts from Ranjit Jhala's message of 2017-01-24 19:00:05 -0800:

> Dear Joachim,
>
> You are right -- some more context.
>
> Given
>
>   tc  :: TyCon
>   m   :: ModName
>   env :: HscEnv
>
> I want to get a
>
>   s :: String
>
> such that _in_ the context given by `m` and `env` I can resolve `s` to get
> back the original `TyCon`, e.g. something like
>
>   L _ rn <- hscParseIdentifier env s
>   name   <- lookupRdrName env modName rn
>
> would then return `name :: Name` which corresponds to the original `TyCon`.
>
> That is, the goal is _not_ pretty printing, but "serialization" into a
> String
> representation that lets me recover the original `TyCon` later.
>
> (Consequently, `"Data.Set.Base.Set"` doesn't work as the `Data.Set.Base`
> module is hidden and hence, when I try the above, GHC complains that the
> name is not in scope.
>
> Does that clarify the problem?
>
> Thanks!
>
> - Ranjit.
>
>
> On Tue, Jan 24, 2017 at 6:11 PM, Joachim Breitner <[hidden email]>
> wrote:
>
> > Hi Ranjit,
> >
> > Am Dienstag, den 24.01.2017, 16:09 -0800 schrieb Ranjit Jhala:
> > > My goal is to write a function
> > >
> > >    tyconString :: TyCon -> String
> > >
> > > (perhaps with extra parameters) such that given the
> > > `TyCon` corresponding to `Set`, I get back the "original"
> > > name `S.Set`, or even `Data.Set.Set`.
> > >
> > > Everything I've tried, which is fiddling with different variants of
> > > `PprStyle`, end up giving me `Data.Set.Base.Set`
> > >
> > > Does anyone have a suggestion for how to proceed?
> >
> > in a way, `Data.Set.Base.Set` is the “original”, proper name for Set,
> > everything else is just a local view on the name.
> >
> > So, are you maybe looking for a way to get the “most natural way” to
> > print a name in a certain module context?
> >
> > This functionality must exist somewhere, as ghci is printing out errors
> > this way. But it certainly would require an additional argument to
> > tyconString, to specify in which module to print the name.
> >
> > Greetings,
> > Joachim
> >
> >
> > --
> > Joachim “nomeata” Breitner
> >   [hidden email]https://www.joachim-breitner.de/
> >   XMPP: [hidden email] • OpenPGP-Key: 0xF0FBF51F
> >   Debian Developer: [hidden email]
> > _______________________________________________
> > Glasgow-haskell-users mailing list
> > [hidden email]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
> >
> >
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Accessing the "original" names via GHC API

Ranjit Jhala
@Simon,

The route you outline was the one we already use -- but that gives back 
the full name "Data.Set.Base.Set" (instead of "S.Set" or "Data.Set.Set"),
as the module is "Data.Set.Base". 

@Edward,

Yes, I think your suggestion should work! This business of determining
which packages/modules are hidden is exactly what we are trying to get 
around. (In this case, Data.Set.Base is hidden...)

Thanks all!

- Ranjit.


On Wed, Jan 25, 2017 at 1:00 AM, Edward Z. Yang <[hidden email]> wrote:
Hi Ranjit,

Unfortunately you need more information to do this, since the
set of modules which are available for import can vary depending
on whether or not packages are hidden or not (not even counting
whether or not a module is exposed or not!)

The way GHC's pretty printer gives a good name is that it keeps
track of all of the names in scope and where they came from
in a GlobalRdrEnv.  The relevant code is in 'mkPrintUnqualified'
in HscTypes, but if you pretty print using user-style with
an appropriately set up GlobalRdrEnv you should
get the things you want.

Edward

Excerpts from Ranjit Jhala's message of 2017-01-24 19:00:05 -0800:
> Dear Joachim,
>
> You are right -- some more context.
>
> Given
>
>   tc  :: TyCon
>   m   :: ModName
>   env :: HscEnv
>
> I want to get a
>
>   s :: String
>
> such that _in_ the context given by `m` and `env` I can resolve `s` to get
> back the original `TyCon`, e.g. something like
>
>   L _ rn <- hscParseIdentifier env s
>   name   <- lookupRdrName env modName rn
>
> would then return `name :: Name` which corresponds to the original `TyCon`.
>
> That is, the goal is _not_ pretty printing, but "serialization" into a
> String
> representation that lets me recover the original `TyCon` later.
>
> (Consequently, `"Data.Set.Base.Set"` doesn't work as the `Data.Set.Base`
> module is hidden and hence, when I try the above, GHC complains that the
> name is not in scope.
>
> Does that clarify the problem?
>
> Thanks!
>
> - Ranjit.
>
>
> On Tue, Jan 24, 2017 at 6:11 PM, Joachim Breitner <[hidden email]>
> wrote:
>
> > Hi Ranjit,
> >
> > Am Dienstag, den 24.01.2017, 16:09 -0800 schrieb Ranjit Jhala:
> > > My goal is to write a function
> > >
> > >    tyconString :: TyCon -> String
> > >
> > > (perhaps with extra parameters) such that given the
> > > `TyCon` corresponding to `Set`, I get back the "original"
> > > name `S.Set`, or even `Data.Set.Set`.
> > >
> > > Everything I've tried, which is fiddling with different variants of
> > > `PprStyle`, end up giving me `Data.Set.Base.Set`
> > >
> > > Does anyone have a suggestion for how to proceed?
> >
> > in a way, `Data.Set.Base.Set` is the “original”, proper name for Set,
> > everything else is just a local view on the name.
> >
> > So, are you maybe looking for a way to get the “most natural way” to
> > print a name in a certain module context?
> >
> > This functionality must exist somewhere, as ghci is printing out errors
> > this way. But it certainly would require an additional argument to
> > tyconString, to specify in which module to print the name.
> >
> > Greetings,
> > Joachim
> >
> >
> > --
> > Joachim “nomeata” Breitner
> >   [hidden email]https://www.joachim-breitner.de/
> >   XMPP: [hidden email] • OpenPGP-Key: 0xF0FBF51F
> >   Debian Developer: [hidden email]
> > _______________________________________________
> > Glasgow-haskell-users mailing list
> > [hidden email]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
> >
> >


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users