# Example use of Core\_kernel.Univ\_map

**URL:** <https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572>\
**Category:** Learning\
**Tags:** core\_kernel\
**Created:** [October 24, 2019, 9:05am UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572 "2019-10-24T09:05:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [October 24, 2019, 9:05am UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/1 "2019-10-24T09:05:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sid](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sid/32/1476_2.png) [@sid](https://discuss.ocaml.org/u/sid)\
**Post date:** [October 24, 2019, 9:55am UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/2 "2019-10-24T09:55:40Z")

</div>

Is the following link [https://github.com/janestreet/core\_kernel/blob/master/test/src/test\_univ\_map.ml](https://github.com/janestreet/core_kernel/blob/master/test/src/test_univ_map.ml) helpful?

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [October 24, 2019, 12:00pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/3 "2019-10-24T12:00:58Z")

</div>

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

```auto
# #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](https://erratique.ch/software/hmap/doc/Hmap) library.

---

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [October 24, 2019, 2:16pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/4 "2019-10-24T14:16:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [November 8, 2019, 11:33am UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/5 "2019-11-08T11:33:43Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [November 8, 2019, 11:40am UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/6 "2019-11-08T11:40:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [November 8, 2019, 12:27pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/7 "2019-11-08T12:27:07Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![BikalGurung](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bikalgurung/32/5105_2.png) [@BikalGurung](https://discuss.ocaml.org/u/BikalGurung)\
**Post date:** [November 8, 2019, 12:48pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/8 "2019-11-08T12:48:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [November 8, 2019, 12:53pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/9 "2019-11-08T12:53:29Z")

</div>

Note that keys also have an [integer identifier](https://github.com/dbuenzli/hmap/blob/e54eb3f7d7dbada0724fd023c334d0b5a29c8479/src/hmap.ml#L87) 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.

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [November 8, 2019, 1:41pm UTC](https://discuss.ocaml.org/t/example-use-of-core-kernel-univ-map/4572/10 "2019-11-08T13:41:37Z")

</div>

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.
