caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] generic data type -> int function
Date: Tue, 29 Mar 2005 09:29:29 +0200	[thread overview]
Message-ID: <20050329072929.GB365@first.in-berlin.de> (raw)
In-Reply-To: <1111778159.3062.21.camel@localhost.localdomain>

On Fri, Mar 25, 2005 at 08:15:59PM +0100, Kim Nguyen wrote:
> Le jeudi 24 mars 2005 à 08:38 -0800, Hal Daume III a écrit :
> > Hi all --
> > 
> > Is there a straightforward way (or a built in function, or...) to 
> > automatically map an enumerated data type to integers (and back, if 
> > possible, but that's not strictly necessary).  In particular, I need 
> > something like:
> 
> Hi,
> 	you can use the polymorphic function : Hashtbl.hash_param
> 	which happens to map constructors to their internal tag.
> 	You should be aware that this is only a (cool) side-effect of 	
> 	the current implementation and could change in the future.
> 	This is a bit of a hack but prevents you from using Obj.magic
> 	or automatically generating pattern matching  (which you should
> 	regenerate every time you change the type).
> 
> 
> # type t = A | B | C | D of int | E of string | G;;
> type t = A | B | C | D of int | E of string | G
> 
> # let to_int x = Hashtbl.hash_param 1 1 x;;
> val to_int : 'a -> int = <fun>

Well, I'm not clear what this function computes.
I never used Hashtbl.hash_param and I do not understand the
descrption.... but:

> 
> # to_int A;; (* first constant constructor *)
> - : int = 0
> 
> # to_int B;;
> - : int = 1
> 
> # to_int C;;
> - : int = 2
> 
> # to_int (D(42));; (* first non-constant constructor *)
> - : int = 0
> 
> # to_int (E("foo"));;
> - : int = 1
> 
> # to_int G;;
> - : int = 3	


It seems to me that your solution does not create the result that was
asked for.

The value 0 and 1 in your above example are more than once
the output of the function to_int.

But as I understand it, there should never be one integer-output
more than once!

IMHO a function is searched, that creates something like a
coding table, as sociologists often do coding their questions/answers
into integers (because of a lack of good software that does this automatically;
maybe today the software is better, but it seems many sociologists
do such integer-coding tables nevertheless habitually ;-)).

But when you have an integer as an output more than once,
you go into big trouble then...


So, "to_int A;;" and "to_int (D(42));;" must not have the same output value!!!

Ciao,
   Oliver


      reply	other threads:[~2005-03-29  7:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-24 16:38 Hal Daume III
2005-03-25 11:37 ` [Caml-list] " Jean-Christophe Filliatre
2005-03-30  3:26   ` Hal Daume III
2005-03-30 22:27     ` Oliver Bandel
2005-03-31 14:33       ` Hal Daume III
2005-03-31 17:01         ` Richard Jones
2005-03-31 18:04           ` Hal Daume III
2005-03-30 22:29     ` Oliver Bandel
2005-03-31 14:33       ` Hal Daume III
2005-03-25 19:15 ` Kim Nguyen
2005-03-29  7:29   ` Oliver Bandel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050329072929.GB365@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).