3

I have a class that provides a globally unique identifier for types:

class Named a where
 nameOf :: a -> (String,String,String) -- (Package, Module, Identifier)
 default nameOf :: (Generic a, Named' (Rep a)) => a -> (String,String,String)
 nameOf = nameOf' . from

which almost works:

>>> data D = C
>>> instance Named D
>>> nameOf C
("","Main","D")

but I can't get the datatype's package with GHC.Generics:

class Named' f where  nameOf' :: f a -> (String,String,String)
instance (Datatype t) => Named' (M1 D t f) where nameOf' d = ("", moduleName d, datatypeName d)

Can I? The GUI's not really "globally" unique without the package.

btw, I know that with Data.Typeable I can write:

>>> import Data.Typeable
>>> :set -XDeriveDataTypeable
>>> let nameOf = (\t -> (tyConPackage t, tyConModule t, tyConName t)) . typeRepTyCon . typeRep
>>> data D = C deriving Typeable
>>> nameOf (Proxy :: Proxy D)
("interactive" "Ghci3" "D")

Which is what I may do. But I'm curious about GHC.Generics.

phadej
  • 11,947
  • 41
  • 78
sam boosalis
  • 1,997
  • 4
  • 20
  • 32
  • 1
    AFAIK no. But it's impossible to have same module twice in the same program (even from different packages). – phadej Jan 27 '15 at 05:56
  • [1] even with `PackageImports`? (haven't used it, just heard about it) – sam boosalis Jan 27 '15 at 06:34
  • [2] what does "program" mean, in this context? what if you import two different modules from different packages, which each import the "same" module which are from different packages, neither of which packages (the latter two) you directly depend on. does that code not compile? And thanks! – sam boosalis Jan 27 '15 at 06:36
  • 1
    Seems that with `PackageImports` you can import the same module from different packages, even into the same module: https://gist.github.com/phadej/8b628d579ddf6958d937 Didn't ever tried to do anything like that! – phadej Jan 27 '15 at 06:55
  • 1
    I created a [ghc ticket](https://ghc.haskell.org/trac/ghc/ticket/10030#ticket) let's see what more wise people respond. – phadej Jan 27 '15 at 07:09

1 Answers1

1

So far it's impossible to get package name using Generics. There is a GHC feature request -ticket now. It was straightforward to implement, but let's see when the patch lands release version.

phadej
  • 11,947
  • 41
  • 78