Deferred instance declarations (serialization of existential boxes)
hello all, 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 or 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 constructors (see http://haskell.org/pipermail/haskell-cafe/2006-September/018041.html) 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? Cheers, Misha P.S. Currently I am solving this problem by introducing recursively dependent modules.
On 9/20/06, Misha Aizatulin <avatar@hot.ee> wrote:
What I need it for is serialization of datatypes with existential constructors (see http://haskell.org/pipermail/haskell-cafe/2006-September/018041.html) I am having a box like
data Box = forall a. Cxt a => Box a and want to write a Read instances for it.
Can you define instance Cxt a => Read (Box a)? If not, then you need typecase and Haskell does not really support that. -- Taral <taralx@gmail.com> "You can't prove anything." -- Gödel's Incompetence Theorem
Hello Misha, Thursday, September 21, 2006, 3:36:05 AM, you wrote:
f :: C T => T -> T or instance C T => C T' where -- a body that uses functions from C T
i think that is a great idea, in a true Haskell spirit you don't got any answers because of Haskell Workshop and ICFP are hold now, just wait until next week -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (3)
-
Bulat Ziganshin -
Misha Aizatulin -
Taral