caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Baptiste Rouquier <jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] getting the type of a polymorphic data ?
Date: Thu, 12 Aug 2004 20:18:51 +0200 (CEST)	[thread overview]
Message-ID: <1092334731.411bb48b56f4e@mouette.ens-lyon.fr> (raw)

> Is it reasonable to admit that if Obj.is_int is true for x and y then
Hashtbl.hash is injective ?
No.

# type foo = A | B;;
# let _ = Obj.is_int (Obj.repr 0);;
- : bool = true
# let _ = Obj.is_int (Obj.repr A);;
- : bool = true
# let _ = Hashtbl.hash A;;
- : int = 0
# let _ = Hashtbl.hash 0;;
- : int = 0

The purpose of Hashtbl.hash is exactly no to be injective ! And hashing lists
can't be injective.

        Objective Caml version 3.07+2
# let _ = Hashtbl.hash [0;1];;
- : int = 65599
# let _ = Hashtbl.hash [65599;0];;
- : int = 65599

What you are looking for is a function that associates a unique int to a
variable of any type. This is Oo.id.
As you said, hash is neither injective on int, nor does it allow to distinguish
between types isomorphs to (a subset of) int.

But let's try to first state the requirement :
- the user define it's own types, foo and bar. We can ask him to provide some
functions to handle these types.
- you want a way to print the result x (of type foo or bar) of a computation
with a user provided help message. This help message doesn't depend on the value
of x.
- you want to manipulate the types foo and bar either as int or floats (not both
for the same type), for efficiency.

My approach is to first transform the user defined type into your own good
representation, using user provided functions, then work on int and float for
the branch and bound, without type tricks, and work on more sophisticated types
for printing.
The user has to say whether his type is isomorph to int (and provide the
conversions functions) or to float (ditto). It's safer than trying to guess it
with the Obj module.
You could provide defaults for those conversions functions, based on Obj / hash
tricks, with lots of warnings.

type 'a range =
  | Single of 'a
  | Interval of 'a * 'a

type domain =
  | Discrete of int range list
  | Continuous of float range list (* is Single meaningful ? *)

Your function
[0 .. 1[ U ]2 .. 3[ U ]4 .. 5[ -> [0]
can now be written
val simplify : domain -> domain



Now printing the results. The property lists is a general solution which is good
for your problem. You can also have a specialized (trivial) mechanism, see
below. The core idea is that you must have two different types :
 - one for doing the computations. For efficiency it's int or float.
 - one for printing the results. This one holds the value (possibly converted
back to user defined type) and the help / description string.

# type 'a result = 'a * string;;
# let new_result (a:'a) description = ((a,description) : 'a result);;
val new_result : 'a -> string -> 'a result = <fun>
# let print_int ((a,d): int result) = Printf.printf "%s is %d\n%!" d a;;
val print_int : int result -> unit = <fun>
# let print_float = ...
# let x = new_result 10 "the number of elements in the knapsack";;
val x : int result = ...
# print_int x;;
the number of elements in the knapsack is 10
- : unit = ()

Jean-Baptiste.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2004-08-12 18:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-12 18:18 Jean-Baptiste Rouquier [this message]
2004-08-13  8:15 ` Diego Olivier Fernandez Pons
2004-08-13  8:59 ` Diego Olivier Fernandez Pons
     [not found] <20040812143443.2aba80c1@babasse>
2004-08-12 12:47 ` Diego Olivier Fernandez Pons
     [not found] <Pine.A41.4.44.0408121350120.557174-100000@ibm1>
2004-08-12 12:09 ` Diego Olivier Fernandez Pons
2004-08-12 13:38   ` Damien Doligez
2004-08-12 13:57     ` Diego Olivier Fernandez Pons
2004-08-12 14:13       ` Marcin 'Qrczak' Kowalczyk
2004-08-12 14:26         ` Diego Olivier Fernandez Pons
  -- strict thread matches above, loose matches on Subject: below --
2004-08-06 15:05 Diego Olivier Fernandez Pons

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=1092334731.411bb48b56f4e@mouette.ens-lyon.fr \
    --to=jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr \
    --cc=caml-list@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).