caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Hugo Ferreira <hmf@inescporto.pt>
To: Martin Jambon <martin.jambon@ens-lyon.org>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Making a polymorphic type non-polymorphic to comply with original signature
Date: Tue, 20 Jan 2009 17:05:14 +0000	[thread overview]
Message-ID: <4976044A.3060407@inescporto.pt> (raw)
In-Reply-To: <4975E72C.8060709@ens-lyon.org>

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


      reply	other threads:[~2009-01-20 17:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-20 10:59 Hugo Ferreira
2009-01-20 11:24 ` [Caml-list] " Daniel Bünzli
2009-01-20 11:37   ` David Teller
2009-01-20 11:46     ` Hugo Ferreira
2009-01-20 12:56       ` Thomas Gazagnaire
2009-01-20 13:25         ` Hugo Ferreira
2009-01-20 11:43   ` Hugo Ferreira
2009-01-20 15:01 ` Martin Jambon
2009-01-20 17:05   ` Hugo Ferreira [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=4976044A.3060407@inescporto.pt \
    --to=hmf@inescporto.pt \
    --cc=caml-list@yquem.inria.fr \
    --cc=martin.jambon@ens-lyon.org \
    /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).