Re: hashtables for mutable records

From: Jacques Garrigue (garrigue@kurims.kyoto-u.ac.jp)
Date: Fri Apr 28 2000 - 03:12:27 MET DST

  • Next message: John Max Skaller: "Re: what does "32-bit integer" mean?"

    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>



    This archive was generated by hypermail 2b29 : Fri Apr 28 2000 - 12:10:57 MET DST