# How to initialize a Base.Hashtbl on an abstract type?

**URL:** https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056
**Category:** Learning
**Tags:** base
**Created:** [May 29, 2018, 9:34am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056 "2018-05-29T09:34:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![loxs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/loxs/32/961_2.png) [@loxs](https://discuss.ocaml.org/u/loxs)
#### Post date: [May 29, 2018, 9:34am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/1 "2018-05-29T09:34:48Z")

</div>

Could someone show me how to initialize a Base.Hashtbl and how to satisfy it with a key that is an abstracted list of ints? I tried lots of things but still fail to make it work and I don’t kow where to read (yes, did read the chapter in dev.RWO and JS docs as far as I thought makes sense)

So, what I have is something like this:

```ocaml
module MyModule : sig
    type t
end = struct
    type t = int list
end

```

What do I need to do in order to make it work with `Hashtbl.create (module MyModule)`

I tried various things like including `Hashable.S` in the signature, but I failed miserably.

---

<div class="post-metadata">

### Author: ![bobbypriambodo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobbypriambodo/32/179_2.png) [@bobbypriambodo](https://discuss.ocaml.org/u/bobbypriambodo)
#### Post date: [May 29, 2018, 9:59am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/2 "2018-05-29T09:59:11Z")

</div>

Given `MyModule`, if you try to create a Hashtbl there’s this error:

```ocaml
utop # let table = Base.Hashtbl.create (module MyModule);;
Error: Signature mismatch:
       ...
       The value `hash' is required but not provided
       File "src/hashtbl_intf.ml", line 15, characters 2-21:
         Expected declaration
       The value `sexp_of_t' is required but not provided
       File "src/hashtbl_intf.ml", line 9, characters 4-49:
         Expected declaration
       The value `compare' is required but not provided
       File "src/hashtbl_intf.ml", line 8, characters 4-31:
         Expected declaration

```

Note that it requires MyModule to implement `hash`, `sexp_of_t`, and `compare` functions, i.e. implementing [`Base__.Hashtbl_intf.Key`](https://ocaml.janestreet.com/ocaml-core/latest/doc/base/Base__/Hashtbl_intf/module-type-Key/) interface.

~~That said, do you really need to have MyModule as the key? If MyModule is the value, then you don’t need to give MyModule to `create` and instead provide modules such as `Int` (if your keys are ints) or `String` (if your keys are strings).~~ Edit: sorry I reread your original post and you seem to really need it to be key, then you need to implement those functions.

---

<div class="post-metadata">

### Author: ![loxs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/loxs/32/961_2.png) [@loxs](https://discuss.ocaml.org/u/loxs)
#### Post date: [May 29, 2018, 10:06am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/3 "2018-05-29T10:06:19Z")

</div>

Yeah, I already saw the error, but I fail to satisfy it as I both have to write the proper types in the signature and in the struct and I fail to do that.

I’m sure there is a shortcut by including some module in the signature and then defining the three functions but I can’t figure it out.

---

<div class="post-metadata">

### Author: ![bobbypriambodo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobbypriambodo/32/179_2.png) [@bobbypriambodo](https://discuss.ocaml.org/u/bobbypriambodo)
#### Post date: [May 29, 2018, 10:17am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/4 "2018-05-29T10:17:49Z")

</div>

My [ppx\_deriving](https://github.com/ocaml-ppx/ppx_deriving)-fu is not that good, but afaik you can utilize [ppx\_compare](https://github.com/janestreet/ppx_compare) and [ppx\_sexp\_conv](https://github.com/janestreet/ppx_sexp_conv) so that you can write:

```auto
module MyModule : sig
    type t [@@deriving compare, sexp_of]
    val hash : t -> int
end = struct
    type t = int list [@@deriving compare, sexp_of]
    let hash v = ... (*impl*)
end

```

Where you already get `compare` and `sexp_of_t` generated automatically by the ppx.

---

<div class="post-metadata">

### Author: ![loxs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/loxs/32/961_2.png) [@loxs](https://discuss.ocaml.org/u/loxs)
#### Post date: [May 29, 2018, 10:35am UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/5 "2018-05-29T10:35:26Z")

</div>

Yeah, this works so far (already had it) but I can’t seem to derive the hash. I find some places on the web where they mention [@@deriving hash] which is available via ppx\_hash opam package, but I get error when I try to use it.

`Error: Ppxlib.Deriving: 'hash' is not a supported signature type deriving generator`

So I still don’t know what is the proper signature for my module.  
Also, if possible, I’d very much like to see a solution without using PPX, as I want to understand what is the proper typing here.

---

<div class="post-metadata">

### Author: ![bluddy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bluddy/32/104_2.png) [@bluddy](https://discuss.ocaml.org/u/bluddy)
#### Post date: [May 29, 2018, 12:57pm UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/6 "2018-05-29T12:57:21Z")

</div>

Do you have `ppx_hash` as a dependency?

There’s nothing magical here – it’s just a bunch of boilerplate, which is why it’s so useful to have the ppx’s do it. I imagine you could use a dummy `sexp_of_t` function unless you’re trying to serialize your data structure; `compare` is just the usual loop over values with comparison of each int, and `hash` is generally just going to loop over the list and `xor` with a hash function per value. In the case of integers, it might even be xor-ing the actual integer value.

---

<div class="post-metadata">

### Author: ![loxs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/loxs/32/961_2.png) [@loxs](https://discuss.ocaml.org/u/loxs)
#### Post date: [May 29, 2018, 1:01pm UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/7 "2018-05-29T13:01:00Z")

</div>

Yes, it “works” (or at least compiles) in the struct of the module, but not in the signature. Though it still doesn’t allow me to use the module as a Hashtbl key while complaining with the above errors.

---

<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: [May 29, 2018, 1:14pm UTC](https://discuss.ocaml.org/t/how-to-initialize-a-base-hashtbl-on-an-abstract-type/2056/8 "2018-05-29T13:14:29Z")

</div>

An answer specific to your question is:

```ocaml
module MyModule : sig 
    type t [@@deriving compare, hash, sexp_of]
end = struct 
    type t = int list [@@deriving compare, hash, sexp_of]
end

module Table = Hashtbl.Make(MyModule)

```

However, the idiomatic approach is to automatically derive all other data types, including maps, sets, hash\_sets, etc, instead of implementing each one manually. You can do this with the `Identifiable.Make` function, that derive a module that implements the `Identifiable.S` interface, from the provided minimal representation, e.g.,

```ocaml
module MyModule : sig 
    type t 
    include Idenfiable.S with type t := t
end = struct 
    module Base = struct 
       type t = int list [@@deriving bin_io, compare, hash, sexp]
       let of_string x = failwith "implement-me"
       let to_string x = failwith "implement-me"
       let module_name = "My_unit.MyModule"
    end
    include Base
    include Identifiable.Make(Base)
end

```

The `Identifiable.S` interface is very rich, so you may choose something smaller, like `Identifiable.S_plain`, `Comparable.S`, `Comparable.S_plain`, etc.

Also, when a deriver is not recognized, then you need to install it via opam and specify in the build system.
