caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] getting the type of a polymorphic data ?
@ 2004-08-06 15:05 Diego Olivier Fernandez Pons
  0 siblings, 0 replies; 10+ messages in thread
From: Diego Olivier Fernandez Pons @ 2004-08-06 15:05 UTC (permalink / raw)
  To: caml-list

    Bonjour,

In a branch and bound program I give the user a generic polymorphic
data structure to represent decision variables, namely a set of
intervals - (domains decrease monotonically)

x = [0 .. 1] U [2 .. 3[
y = [3,5  14,8]

As usual in branch and bound, a value is tried (x = 1) and removed of
the domain if the corresponding branch raises a 'No solution'
exception (x = [0 .. 1[ U [2 .. 3[)

In a preprocessing phase I would like to change the representation of
the decision variables according to their type to avoid degenerancies
like

[0 .. 1[ U ]2 .. 3[ U ]4 .. 5[ -> [0] in the integer case but not with
floats

The problem is that I don't know how to catch the type of a
polymorphic data structure (is this even possible with some magic ?)
to be able to write something like

let x' = match (somemagic.type_of x) with
           | INT -> IntSet.from_poly x
           | FLOAT -> FloatIntervalSet.from_poly x
           | _ -> x

Any hints ?


         Diego Olivier

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread
[parent not found: <Pine.A41.4.44.0408121350120.557174-100000@ibm1>]
[parent not found: <20040812143443.2aba80c1@babasse>]
* Re: [Caml-list] getting the type of a polymorphic data ?
@ 2004-08-12 18:18 Jean-Baptiste Rouquier
  2004-08-13  8:15 ` Diego Olivier Fernandez Pons
  2004-08-13  8:59 ` Diego Olivier Fernandez Pons
  0 siblings, 2 replies; 10+ messages in thread
From: Jean-Baptiste Rouquier @ 2004-08-12 18:18 UTC (permalink / raw)
  To: caml-list

> 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-08-13  9:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-06 15:05 [Caml-list] getting the type of a polymorphic data ? 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
     [not found] <20040812143443.2aba80c1@babasse>
2004-08-12 12:47 ` Diego Olivier Fernandez Pons
2004-08-12 18:18 Jean-Baptiste Rouquier
2004-08-13  8:15 ` Diego Olivier Fernandez Pons
2004-08-13  8:59 ` Diego Olivier Fernandez Pons

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).