Example use of Core_kernel.Univ_map

Is there a sample/example which demonstrates how to use Core_kernel.Univ_map. If the documentation serves me right that it is a heterogenous map. However, I am unable to find a simple usage/example.

1 Like

Is the following link https://github.com/janestreet/core_kernel/blob/master/test/src/test_univ_map.ml helpful?

1 Like

It is indeed a universal, aka heterogenous associative data structure in which values can have different types, and the type of the value is denoted by the type of the key. However, the key type is fixed, and should be the 'a Type_equal.Id.t type. It is better to think of those maps as object dictionaries in Python or as extensible records, here is a simple example in the toplevel

# #use "topfind";;
# #require "core_kernel";;
# open Core_kernel;;
# let name = Type_equal.Id.create ~name:"name" sexp_of_string;;
val name : string Type_equal.Id.t = <abstr>
# let age = Type_equal.Id.create ~name:"age" sexp_of_int;;
val age : int Type_equal.Id.t = <abstr>
# let dance = Type_equal.Id.create ~name:"dance" sexp_of_opaque;;
val dance : '_weak1 Type_equal.Id.t = <abstr>
# let student = Univ_map.empty;;
val student : Core_kernel.Univ_map.t = <abstr>
# let student = Univ_map.set student name "Joe";;
val student : Core_kernel.Univ_map.t = <abstr>
# let student = Univ_map.set student age 20;;
val student : Core_kernel.Univ_map.t = <abstr>
# let student = Univ_map.set student dance @@ fun () -> 
   print_endline "Dancing, like he never danced before";;
val student : Univ_map.t = <abstr>
# Univ_map.find_exn student name;;
- : string Univ_map.data = "Joe"
# Univ_map.find_exn student age;;
- :int Univ_map.data = 20
# Univ_map.find_exn student dance ();;
Dancing, like he never danced before
- : unit = ()

See also Daniel’s hmap library.

1 Like

@ivg @sid Thanks. Just what I was looking for. :+1: It seems github search is not always that accurate.

@ivg @sid Is there a way to get the length of bindings in Univ_map? It seems it is not supported. Am I missing something?

Additionally, with regards to Hmap, when I do Hmap.Key.create() am I guaranteed to have unique key every time, i.e. will the key perhaps clash if I do a lot of Hmap.Key.create() calls?

/cc @dbuenzli

Yes, it is guaranteed. Keys are extensible variants and it is guaranteed by the language itself that the key will match only with itself. (Otherwise it is a bug in the compiler).

Thanks. That settles it. It seems Hmap is what I need.

Note that keys also have an integer identifier so if you generate more than 2^31 keys (on 32-bit platforms) or 2^63 (on 64-bit platforms) you might get into trouble.

Besides, since we’re going into details here, it is more likely that the extensible variant counter which is also 2^{31,63} (and is the same counter that is used for objects, e.g., evey object end increments it) will run out, especially on 32-bits systems. That’s why in our own implementation of hmap we’ve added an Int63.t counter to ensure that we can still work on 32-bit systems.

1 Like