Version française
Home     About     Download     Resources     Contact us    
Browse thread
hashtables for mutable records
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: hashtables for mutable records
From: "Coscoy, Yann" <yann.coscoy@icdc.caissedesdepots.fr>

> I want to do an hashtable on mutable and polymorphic records. Standard
> module Hashtbl is not suitable because:
>   - Hashtbl.HashType doesn't accept polymorphic types.
>   - Hashtbl.hash is susceptible to setups of a mutable fields.

The standard functorial solution would be to define a new functor:

type 'a polyref = {mutable data: 'a; id: int}

module PolyHash(T : sig type t end) =
  Hashtbl.Make
    (struct
      type t = T.t polyref
      let equal a b = (a.id = b.id)
      let hash a = a.id
    end)

module IntHash = PolyHash(struct type t = int end)
module BoolHash = PolyHash(struct type t = bool end)

The only dificulty with such an approach is that you must use
different functions for different hashtables.
If you want to define new polymorphic functions using these
hashtables, you must again put them in a functor.

Regards,

Jacques
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>