From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C8044BB81 for ; Mon, 3 Jan 2005 12:17:33 +0100 (CET) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j03BHXhH006383 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 3 Jan 2005 12:17:33 +0100 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1ClQDM-000K18-Jq for caml-list@yquem.inria.fr; Mon, 03 Jan 2005 11:17:32 +0000 From: Jon Harrop Subject: Fwd: Re: [Caml-list] string_of_polymorphic Date: Mon, 3 Jan 2005 11:20:07 +0000 User-Agent: KMail/1.6.2 To: caml-list@yquem.inria.fr MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-Id: <200501031120.07548.jon@jdh30.plus.com> X-Miltered: at nez-perce with ID 41D929CD.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 constructors:01 ad-hoc:01 run-time:01 hash:01 constructors:01 avoided:01 marshalling:01 hash:01 run-time:01 cheers:01 ...:98 exception:01 exception:01 polymorphic:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: > >Why do you want to do this? > > Logging purposes, Error handling and so on. > I have a function that accept some polymorphic constructors as input > type, and I want to log what is arrived before possibly without writing > everytime a pattern matching ad-hoc, or raising an exception, I'd like > to attach the constructor that raised the exception. If the program is running in the top-level then you may be able to use the relevant installed pretty printer. I've no idea how though. :-) > >I think the run-time representation of a polymorphic variants' value is a > > hash of its name and, therefore, cannot be mapped back onto a string in > > general. There may be something else you can do specifically for the > > top-level but I can't think what... > > I don't know. > Polymorphic constructors survive to the marshalling-unmarshalling. > In the marshalled string, there is a representation of their name. > That's why I thought there was a method to get their names into a functio= n. > I think that if Obj is strongly not recommended, analizing a marshalled > string has to be avoided, so I was looking for other ways. I believe that the name of a polymorphic variant, as a string, is not in the marshalled string. Instead, the name is in the type, which is not conveyed = by marshalling. I think the marshalled string will actually contain a fixed-si= ze hash of the string name. In theory, two polymorphic variant names which produce the same hash should be indistinguishable at run-time. Producing them will be tricky though. A consequence of this is the fixed-size (1-word) of a marshalled polymorphic variant: # Marshal.to_string `dkdfbgkadjfbgljbfgljkabhvlkjblvbalvbvlabalbvaslbk [];; =2D : string =3D "\132\149=A6=BE\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000= \000\0 02=ED\014=E2=ED" # Marshal.to_string `abcd [];; =2D : string =3D "\132\149=A6=BE\000\000\000\005\000\000\000\000\000\000\000\000\000\000\000= \000\0 02=C0he=C2" Cheers, Jon.