# Making a StringSet with Base

**URL:** <https://discuss.ocaml.org/t/making-a-stringset-with-base/5469>\
**Category:** Learning\
**Tags:** base\
**Created:** [April 9, 2020, 8:35am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469 "2020-04-09T08:35:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martindemello](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/martindemello/32/632_2.png) [@martindemello](https://discuss.ocaml.org/u/martindemello)\
**Post date:** [April 9, 2020, 8:35am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/1 "2020-04-09T08:35:55Z")

</div>

I have the following code to create a StringSet module in `base`:

```
open Base

type t = Set.M(String).t

let empty = Set.empty (module String)
let of_list = Set.of_list (module String)
let diff = Set.diff
let inter = Set.inter
let to_list = Set.to_list 
let union = Set.union
let union_list = Set.union_list (module String)

```

which works, but I feel like I shouldn’t have to be explicitly reexporting every function from Set that I want in my interface. With `core` it was simply

```
open Core
module S = Set.Make(String)
include S
```

---

<div class="post-metadata">

**Author:** ![remyzorg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/remyzorg/32/1965_2.png) [@remyzorg](https://discuss.ocaml.org/u/remyzorg)\
**Post date:** [April 9, 2020, 9:18am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/2 "2020-04-09T09:18:56Z")

</div>

Hi,

This discussion on stackoverflow might be an answer to your problem :

> <https://stackoverflow.com/questions/59265721/in-ocaml-using-base-how-do-you-construct-a-set-with-elements-of-type-int-int>

---

<div class="post-metadata">

**Author:** ![martindemello](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/martindemello/32/632_2.png) [@martindemello](https://discuss.ocaml.org/u/martindemello)\
**Post date:** [April 9, 2020, 10:45am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/3 "2020-04-09T10:45:17Z")

</div>

Hi Rémy,

I’m afraid that doesn’t solve my problem - using `core`, `Set.Make(String)` created a new module that satisfied the interface of `Set`, so that I could include it in my module and have it be a set. So I could say something like `StringSet.of_list xs` and it would work without needing to know that `StringSet` was derived from `Set`. In `base` since `Set.M(String)` does not return a `Set` I have to manually reexport the functions I want to use as `StringSet.foo`.

---

<div class="post-metadata">

**Author:** ![Levi\_Roth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/levi_roth/32/2268_2.png) [@Levi\_Roth](https://discuss.ocaml.org/u/Levi_Roth)\
**Post date:** [April 9, 2020, 2:13pm UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/4 "2020-04-09T14:13:01Z")

</div>

In general, in Base it shouldn’t be necessary to create a `StringSet` module the way you would in Core because (the moral equivalents of) all the values that would be in the `StringSet` module are just available directly via `Set`. What’s the use case where you need a `StringSet` module?

---

<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:** [April 9, 2020, 7:04pm UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/5 "2020-04-09T19:04:27Z")

</div>

Unfortunately, Base doesn’t provide a functorized interface for creating sets and other ordered data structures, so you have to stick with the polymorphic sets and maps. If you want a monomorphic instance of Set, then you have to use Core\_kernel or Core, where the good old `Set.Make(String)` will produce what you want.

---

<div class="post-metadata">

**Author:** ![bcc32](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bcc32/32/203_2.png) [@bcc32](https://discuss.ocaml.org/u/bcc32)\
**Post date:** [April 9, 2020, 7:21pm UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/6 "2020-04-09T19:21:00Z")

</div>

> [@martindemello](#):
>
> ```auto
> let empty = Set.empty (module String)
> let of_list = Set.of_list (module String)
> let diff = Set.diff
> let inter = Set.inter
> let to_list = Set.to_list 
> let union = Set.union
> let union_list = Set.union_list (module String)
> 
> ```

As a minor point, most of these accessors can be defined by just writing:

```ocaml
include Set

```

The only ones which need to have `(module String)` included are the creator functions, because they don’t know what comparator to use. You can include `Set.Accessors0`, `Set.Creators0`, or `Set.Creators_and_accessors0` (0 because there would be 0 type parameters) to help define your interface.

---

<div class="post-metadata">

**Author:** ![martindemello](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/martindemello/32/632_2.png) [@martindemello](https://discuss.ocaml.org/u/martindemello)\
**Post date:** [April 11, 2020, 4:21am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/7 "2020-04-11T04:21:59Z")

</div>

I have three main reasons for it:

1. I have some other string-specific functions, e.g. converting all words to lowercase, or extracting word of certain length, that I have defined on the set. I would rather be consistent about using `StringSet.foo` for all functions, not `StringSet.` for some and `Set.` for others.

2. I want to encapsulate the fact that it’s a set of strings, and not have to pass in `module String` for functions like `of_list` when using the sets in other modules.

3. I imagine monomorphic sets are more efficient, though I could be wrong about that.

It looks like my best bet is to just revert to using `core` rather than `base`.

---

<div class="post-metadata">

**Author:** ![martindemello](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/martindemello/32/632_2.png) [@martindemello](https://discuss.ocaml.org/u/martindemello)\
**Post date:** [April 11, 2020, 4:25am UTC](https://discuss.ocaml.org/t/making-a-stringset-with-base/5469/8 "2020-04-11T04:25:08Z")

</div>

thanks, I overlooked the `Set.Creators` and `Set.Accessors` modules!
