Reputation: 16645
I'm working on a data structure that I would like to serialize, with an API like:
serialize :: Foo a -> ByteString
deserialize :: ByteString -> Maybe (Foo a)
In my case a
is a phantom parameter; there are no values in Foo
to witness that type, but preserving the type is important.
GHC provides Typeable
which can give me a TypeRep
form which I can extract a Fingerprint
, but I don't believe that representation is necessarily stable across different architectures and versions of GHC.
Is there some way I can reliably serialize a type representation, perhaps with the new static pointers thing, or something else in base
?
EDIT: I've implemented this type versioning functionality (sort of "hashing of types") in hashabler; see TypeHash
.
Upvotes: 3
Views: 126
Reputation: 48611
You might be able to play games with Typeable
, as the binary-typed package does. It allows you to remove and reconstruct fingerprints as needed. I'm not sure if that will work for types manufactured on the fly. Another option is to manufacture some tags yourself, as long as you know or can figure out which values have the same phantoms. An extreme approach might be to build a Map
of the TypeRep
s you see along the way, mapping them to unique Int
values. On the other end, you can make an IntMap
from the tags to functions that wrap up the values properly.
Upvotes: 2