Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] getting the type of a polymorphic data ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Diego Olivier Fernandez Pons <Diego.FERNANDEZ_PONS@e...>
Subject: Re: [Caml-list] getting the type of a polymorphic data ?
    Bonjour,

> 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

I have just realized I really do not need the 'type' of the
polymorphic data, just to know if it is isomorphic to integers (to be
able to properly use Hashtbl.hash)

It seems that Obj.is_int does exactly that (determining if a type is
isomorphic to a subset of integers).

        Objective Caml version 3.08.0

# Obj.is_int (Obj.repr 'a');;
- : bool = true
# type x = A | B;;
type x = A | B
# Obj.is_int (Obj.repr A);;
- : bool = true
# Obj.is_int (Obj.repr 1);;
- : bool = true
# Obj.is_int (Obj.repr "a");;
- : bool = false
# type x = A of int * int | B of int;;
type x = A of int * int | B of int
# Obj.is_int (Obj.repr (A (1, 0)));;
- : bool = false

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

hash x = hash y => x = y (for structural polymorphic equality)

In other terms does 'Obj.is_int x' implies that x is 'perfectly
hashable' ?


        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