I need to use hashtable of mutable variable in Ocaml, but it doesn’t work out.
let link = Hashtbl.create 3;;
let a = ref [1;2];;
let b = ref [3;4];;
Hashtbl.add link a b;;
# Hashtbl.mem link a;;
- : bool = true
# a := 5::!a;;
- : unit = ()
# Hashtbl.mem link a;;
- : bool = false
Is there any way to make it works?
Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the physical equality operator (
==). However, OCaml doesn’t provide anything better than equality, it doesn’t provide a nontrivial hash function or order on references, so the only data structure you can build to store references is an association list of some form, with $\Theta(n)$ access time for most uses.(You can actually get at the underlying pointer if you play dirty. But the pointer can move under your feet. There is a way to make use of it nonetheless, but if you need to ask, you shouldn’t use it. And you aren’t desperate enough for that anyway.)
It would be easy to compare references if two distinct references had a distinct content. So make it so! Add a unique identifier to your references. Keep a global counter, increment it by 1 each time you create a reference, and store the counter value with the data. Now your references can be indexed by their counter value.
For better type safety and improved efficiency, define a data structure containing a counter value and an item.
You can put the code above in a module and make the
countertype abstract. Also, you can define a hash table module using theHashtblfunctorial interface. Here’s another way to define variables and a hash table structure on them with a cleaner, more modular structure.We need the intermediate module
Variablebecause the typevariableis parametric and the standard library data structure functors (Hashtbl.Make,Set.Make,Map.Make) are only defined for type constructors with no argument. Here’s an interface that exposes both the polymorphic interface (with the associated functions, but no data structures) and a functor to build any number of monomorphic instances, with an associated hash table (and set, and map) type.Note that if you expect that your program may generate more than 2^30 variables during a run, an
intwon’t cut it. You need twointvalues to make a 2^60-bit value, or anInt64.t.Note that if your program is multithreaded, you need a lock around the counter, to make the incrementation and lookup atomic.