Ticket #5568 (closed bug: fixed)

Opened 20 months ago

Last modified 17 months ago

Add Show and Binary instances for TypeRep

Reported by: simonmar Owned by:
Priority: normal Milestone: 7.4.2
Component: libraries/base Version: 7.2.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

(was a re-opening of #3480; copied into a new ticket so we can leave #3480 dead and buried)

Unfortunately the new TypeRepKey type is abstract and provides no operations apart from Eq and Ord instances. Hence, there is still no way to write a type representation into a file or across the wire. Jush showing a TypeRep won't cut it, since that loses the distinctions between similarly named types in different modules.

So, I think we need a Show instance for TypeRepKey, or better yet, a Binary instance. Moreover, a non-deprecated pure function TypeRep -> TypeRepKey is in place, since the keys have an actual use as a compact, efficient, and serializable injection of the typereps.

Alternatively, the Show instance of TypeRep could be made injective by printing out the package and module.

Commentary from #3480

simonmar says:

I've exposed the representation via the Data.Typeable.Internal module. Of course that's not guaranteed to be a stable API, but I'm happy to add a stable API if we can agree on what it should be. Lennart Augustsson and Neil Mitchell are doing exactly this (serialising TypeRep) - I think they're using the Data.Typeable.Internal API right now.

So what shall we do?

Lennart says:

For older versions of ghc I just used (show . typeOf). When ghc 7.2 broke this I had to start using Internal, so now I use tyConModule and tyConName to emulate the behaviour of the old show.

lealanko says:

I'm sorry for messing up the ticket. I had assumed that the missing Show instance had been a simple oversight. If so, this would suffice:

instance Show TypeRepKey where
  show (TypeRepKey (Fingerprint hi lo)) = printf "%016x%016x" hi lo

A Binary instance is more difficult: where would it be put? It cannot be in Typeable.hs (as base cannot depend on binary), but it cannot be in Binary.hs either (as TypeRepKey is opaque and not exposed from Typeable).

I don't think it's a good idea to make TypeReps? themselves publicly deserializable, as that would allow the creation of fake typereps. A separate one-way injection to a serializable TypeRepKey seems much more secure.

Change History

Changed 18 months ago by simonmar

  • difficulty set to Unknown
  • milestone changed from 7.4.1 to 7.4.2

We now export

        tyConPackage,   -- :: TyCon   -> String
        tyConModule,    -- :: TyCon   -> String
        tyConName,      -- :: TyCon   -> String

from Data.Typeable, which gives you enough information to serialise/deserialise a TyCon.

Is that enough? If you want to get at the fingerprint you can still get it via Data.Typeable.Internal.

If TypeRepKey were a supported API, we would have to say what properties it has - does it produce the same results on different platforms, or different GHC versions, for example?

Changed 17 months ago by simonmar

  • status changed from new to closed
  • resolution set to fixed

Closing - if anyone needs something more, please re-open and give details.

Note: See TracTickets for help on using tickets.