<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/01/bec8514bf8ec8e4b7403d82c8845ffa0"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T17:05:16"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature"
  prev="2009/01/fa6c4452c1d8f79eeb50fa71a9e2e430"
  next="2009/01/8feeca3b384c260c274f1fff24d8962f"
  prev-in-thread="2009/01/13994d2261e2703a410b2818687311b2"
  prev-thread="2009/01/6cfb5e559cd4bf982c4960d25352bc27"
  next-thread="2009/01/bdd60304d962e4e9ac5f3fc4516fce06"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="Making a polymorphic type non-polymorphic to comply with original signature ">
<msg 
  url="2009/01/5086d82bd654331abd6d66fbfc5877f2"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T10:59:50"
  subject="Making a polymorphic type non-polymorphic to comply with original signature ">
<msg 
  url="2009/01/03af28a51b1f263e4bce45395c2a24ee"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2009-01-20T11:25:04"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
<msg 
  url="2009/01/45ebef4fa64ce8ca6d0ab81460203417"
  from="David Teller &lt;David.Teller@u...&gt;"
  author="David Teller"
  date="2009-01-20T11:37:28"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
<msg 
  url="2009/01/8291b0da375a697d829347a7a18148e7"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T11:46:45"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to	comply with original signature">
<msg 
  url="2009/01/da6cc2637b564bfd93a0b72007d53fe0"
  from="Thomas Gazagnaire &lt;ocaml@g...&gt;"
  author="Thomas Gazagnaire"
  date="2009-01-20T12:58:33"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply  with original signature">
<msg 
  url="2009/01/eb1bb346ea9ac86f67b1c2011a41bdc8"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T13:25:52"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2009/01/3faf1d84d33d7d63e5f417f0853743b6"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T11:43:13"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
</msg>
</msg>
<msg 
  url="2009/01/13994d2261e2703a410b2818687311b2"
  from="Martin Jambon &lt;martin.jambon@e...&gt;"
  author="Martin Jambon"
  date="2009-01-20T15:02:35"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
<msg 
  url="2009/01/bec8514bf8ec8e4b7403d82c8845ffa0"
  from="Hugo Ferreira &lt;hmf@i...&gt;"
  author="Hugo Ferreira"
  date="2009-01-20T17:05:16"
  subject="Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature">
</msg>
</msg>
</msg>
</thread>

<contents>
Martin,

Thanks for the alternate solution. It seems
to provide a much simpler interface i.e:
doesn't require such a convoluted way of
declaring the types.

I wonder if it has any run-time benefits
compared to the previous solution.

Regards,
Hugo F.



Martin Jambon wrote:
&gt; Hugo Ferreira wrote:
&gt;&gt; Hello,
&gt;&gt;
&gt;&gt; I have the following definition:
&gt;&gt;
&gt;&gt; module rec H :
&gt;&gt;   sig
&gt;&gt;       type 'a node =
&gt;&gt;           | Node of 'a node J.t
&gt;&gt;           | Leaf of 'a
&gt;&gt;
&gt;&gt;      type 'a t = 'a node
&gt;&gt;      val equal : 'a t -&gt; 'a t -&gt; bool
&gt;&gt;     val hash : 'a t -&gt; int
&gt;&gt;   end =
&gt;&gt; struct
&gt;&gt;     type 'a node =
&gt;&gt;         | Node of 'a node J.t
&gt;&gt;         | Leaf of 'a
&gt;&gt;
&gt;&gt;     type 'a t = 'a node
&gt;&gt;     let equa = (==)
&gt;&gt;     let hash = Hashtbl.hash
&gt;&gt; end
&gt;&gt;
&gt;&gt; and J : Hashtbl.S with type 'a key = 'a H.node = Hashtbl.Make( H )
&gt;&gt; ;;
&gt;&gt;
&gt;&gt; The data type 'a node is polymorphic. It is also a key used by the
&gt;&gt; hash-table. Note that H now does not comply with Hashtbl.HashedType
&gt;&gt; (because Hashtbl.HashedType is not polymorphic). By adding the
&gt;&gt; constraint "with type" also does not help.
&gt;&gt;
&gt;&gt; Is it possible to make H comply with Hashtbl.HashedType i.e: make
&gt;&gt; J.Key = 'a H.node ?
&gt; 
&gt; 
&gt; I just posted an implementation of polymorphic hash tables with physical
&gt; comparison, using the Obj module:
&gt; 
&gt;   http://martin.jambon.free.fr/phys.html
&gt; 
&gt; It is the following code:
&gt; 
&gt; (***** phys.mli *****)
&gt; 
&gt; type ('a, 'b) t
&gt; 
&gt; val add : ('a, 'b) t -&gt; 'a -&gt; 'b -&gt; unit
&gt; val clear : ('a, 'b) t -&gt; unit
&gt; val copy : ('a, 'b) t -&gt; ('a, 'b) t
&gt; val create : int -&gt; ('a, 'b) t
&gt; val find : ('a, 'b) t -&gt; 'a -&gt; 'b
&gt; val find_all : ('a, 'b) t -&gt; 'a -&gt; 'b list
&gt; val fold : ('a -&gt; 'b -&gt; 'c -&gt; 'c) -&gt; ('a, 'b) t -&gt; 'c -&gt; 'c
&gt; val iter : ('a -&gt; 'b -&gt; unit) -&gt; ('a, 'b) t -&gt; unit
&gt; val length : ('a, 'b) t -&gt; int
&gt; val mem : ('a, 'b) t -&gt; 'a -&gt; bool
&gt; val remove : ('a, 'b) t -&gt; 'a -&gt; unit
&gt; val replace : ('a, 'b) t -&gt; 'a -&gt; 'b -&gt; unit
&gt; 
&gt; 
&gt; 
&gt; (***** phys.ml *****)
&gt; 
&gt; (*
&gt;   This implementation uses the Obj module which allows to transgress
&gt;   the type system. It is not regular OCaml.
&gt; *)
&gt; 
&gt; module Magic_key =
&gt; struct
&gt;   type t = Obj.t
&gt;   let equal = ( == )
&gt;   let hash = Hashtbl.hash
&gt; end
&gt; 
&gt; module Magic_table = Hashtbl.Make (Magic_key)
&gt; 
&gt; 
&gt; type ('a, 'b) t = 'b Magic_table.t
&gt; 
&gt; let add tbl k v = Magic_table.add tbl (Obj.repr k) v
&gt; let clear tbl = Magic_table.clear tbl
&gt; let copy tbl = Magic_table.copy tbl
&gt; let create n = Magic_table.create n
&gt; let find tbl k = Magic_table.find tbl (Obj.repr k)
&gt; let find_all tbl k = Magic_table.find_all tbl (Obj.repr k)
&gt; let fold f tbl accu = Magic_table.fold (Obj.magic f) (Obj.magic tbl) accu
&gt; let iter f tbl = Magic_table.iter (Obj.magic f) (Obj.magic tbl)
&gt; let length tbl = Magic_table.length tbl
&gt; let mem tbl k = Magic_table.mem tbl (Obj.repr k)
&gt; let remove tbl k = Magic_table.remove tbl (Obj.repr k)
&gt; let replace tbl k v = Magic_table.replace tbl (Obj.repr k) v
&gt; 
&gt; 
&gt; (*****************)
&gt; 
&gt; 
&gt; 
&gt; Martin
&gt; 

</contents>

</message>

