Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:04:35+00:00 2026-06-02T19:04:35+00:00

I need to use hashtable of mutable variable in Ocaml, but it doesn’t work

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:04:36+00:00Added an answer on June 2, 2026 at 7:04 pm

    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.

    let counter = ref 0
    let new_var x = incr counter; ref (!counter, x)
    let var_value v = snd !v
    let update_var v x = v := (fst !v, x)
    let hash_var v = Hashtbl.hash (fst !v)
    

    For better type safety and improved efficiency, define a data structure containing a counter value and an item.

    let counter = ref 0
    type counter = int
    type 'a variable = {
        key : counter;
        mutable data : 'a;
    }
    let new_var x = incr counter; {key = !counter; data = x}
    let hash_var v = Hashtbl.hash v.key
    

    You can put the code above in a module and make the counter type abstract. Also, you can define a hash table module using the Hashtbl functorial interface. Here’s another way to define variables and a hash table structure on them with a cleaner, more modular structure.

    module Counter = (struct
      type t = int
      let counter = ref 0
      let next () = incr counter; !counter
      let value c = c
    end : sig
      type t
      val next : unit -> t
      val value : t -> int
    end)
    module Variable = struct
      type 'a variable = {
          mutable data : 'a;
          key : Counter.t;
      }
      let make x = {key = Counter.next(); data = x}
      let update v x = v.data <- x
      let get v = v.data
      let equal v1 v2 = v1 == v2
      let hash v = Counter.value v.key
      let compare v1 v2 = Counter.value v2.key - Counter.value v1.key
    end
    module Make = functor(A : sig type t end) -> struct
      module M = struct
        type t = A.t Variable.variable
        include Variable
      end
      module Hashtbl = Hashtbl.Make(M)
      module Set = Set.Make(M)
      module Map = Map.Make(M)
    end
    

    We need the intermediate module Variable because the type variable is 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.

    module Variable : sig
      type 'a variable
      val make : 'a -> 'a variable
      val update : 'a variable -> 'a -> unit
      val get : 'a variable -> 'a
      val equal : 'a -> 'a -> bool
      val hash : 'a variable -> int
      val compare : 'a variable -> 'b variable -> int
    end
    module Make : functor(A : sig type t end) -> sig
      module M : sig
        type t = A.t variable.variable
        val make : A.t -> t
        val update : t -> A.t -> unit
        val get : t -> A.t
        val equal : t -> t -> bool
        val hash : t -> int
        val compare : t -> t -> int
      end
      module Hashtbl : Hashtbl.S with type key = M.t
      module Set : Set.S with type key = M.t
      module Map : Map.S with type key = M.t
    end
    

    Note that if you expect that your program may generate more than 2^30 variables during a run, an int won’t cut it. You need two int values to make a 2^60-bit value, or an Int64.t.

    Note that if your program is multithreaded, you need a lock around the counter, to make the incrementation and lookup atomic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax
I need to use something like get_or_create() but the problem is that I have
So, I need use this event so I can navigate trought blog posts. I
Need to use own imaged markers instead built-in pins. I have several questions. 1.
i need to use the animate property for a less than usual activity. i
I need to use scp update some directory at another server. It is similar
I need to use some classes inside the app_code folder of a website project
I need to use Twitter api from iPhone 3.0 (SDK 3.0). can i use
I need to use system-specific functions, e.g. ftello() (defined in stdio.h as per POSIX
I need to use a function inside of another function, how can I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.