[Haskell] Deferred instance declarations (serialization of
avatar at hot.ee
Wed Sep 20 19:36:05 EDT 2006
I asked this question on haskell-cafe once, but my post didn't get any
responses, so I try once again here explaining a bit more where my
problem comes from.
Suppose I have a monomorphic datatype, say T and want to do something
with it, which depends on an instance of class C being defined for T.
However I can only provide this instance later, in a different module.
So I would like to write something like
> f :: C T => T -> T
> instance C T => C T' where
> -- a body that uses functions from C T
By this I mean that f can only be called or C T' can only be derived
in a module that defines C T. However, I don't see any way to make this
work with ghc. Note that if I had a polymorphic type like (T a),
everything would be fine.
What I need it for is serialization of datatypes with existential
I am having a box like
> data Box = forall a. Cxt a => Box a
and want to write a Read instances for it. The problem is that
currently I can only do that when I am aware of all types that will ever
go into the box, which is not in the same module where the Box is
defined. But there is code in the same module that needs to refer to
this instance. I hope you see now where my problem comes from.
I would be very thankful for any help or suggestions. If someone has
implemented serialization with existentials before, it would be really
interesting, if they had a similar problem. Another question: would it
be possible to allow definitions of f and C T' above in GHC?
P.S. Currently I am solving this problem by introducing recursively
More information about the Haskell