# Core\_kernel Comparator and polymorphism

**URL:** <https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173>\
**Category:** Learning\
**Tags:** core\
**Created:** [July 31, 2020, 4:03pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173 "2020-07-31T16:03:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@milo](https://discuss.ocaml.org/u/milo)\
**Post date:** [July 31, 2020, 4:03pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173/1 "2020-07-31T16:03:27Z")

</div>

OCaml newbie here. I’ve run into a problem I’m struggling to resolve.

I’m working on an existing codebase, and I’d like to refactor one of its functions, which has the interface:

```auto
let do_something (l : 'a list) : 'a list = ...

```

This function is called with arguments of several different types, and its current implementation depends on polymorphic comparison.

I’d like to refactor it to call [Core\_kernel.Set’s stable\_dedup\_list](https://ocaml.janestreet.com/ocaml-core/109.55.00/tmp/core_kernel/Set.html), which takes a `comparator` argument of type [('a, 'b) Comparator.t.](https://ocaml.janestreet.com/ocaml-core/109.55.00/tmp/core_kernel/Comparator.html#TYPEt)

My question: is there a way to pass a polymorphic Comparator here so that do\_something can keep the same polymorphic interface? I’m imagining something that would be used like this:

```auto
Core_kernel.Set.stable_dedup_list (module Comparator.Poly(??)) my_list;;

```

Or is there a better way to go about this?  
Ideally I’d like to get rid of the polymorphic comparison someday, which I assume would require passing down the appropriate comparator at every callsite. But I’d like to avoid that for now, if possible.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [July 31, 2020, 4:18pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173/2 "2020-07-31T16:18:51Z")

</div>

Just looking at the signature it looks like you could use

```auto
Core_kernel.Set.stable_dedup_list ~comparator:Comparator.Poly.comparator my_list

```

Cheers,  
Nicolás

---

<div class="post-metadata">

**Author:** ![milo](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@milo](https://discuss.ocaml.org/u/milo)\
**Post date:** [July 31, 2020, 6:05pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173/3 "2020-07-31T18:05:40Z")

</div>

Thanks for the reply. When I pass Comparator.Poly.comparator, I get the error

```auto
Error: This expression has type
         ('a, Comparator.Poly.comparator_witness) Comparator.t
       but an expression was expected of type
         ('b, 'c) Set.comparator =
           (module Core_kernel__.Comparator.S with type comparator_witness = 'c and type t = 'b)

```

My apologies, I may have been looking at the wrong docs when I quoted the type for comparator, which is actually `('a, 'b) Set.comparator `.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [July 31, 2020, 6:42pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173/4 "2020-07-31T18:42:18Z")

</div>

OK, two options:

1. use `Core_kernel.Using_comparator.stable_dedup_list`: this function takes a `_ Comparator.t` as the `~comparator` argument, or
2. to use `Core_kernel.Set.stable_dedup_list`, which takes a `Comparator.S` as the `~comparator` argument, do something along the lines:

```auto
open Core_kernel
let do_something (type a) (l : a list) =
  let module C = struct include Comparator.Poly type t = a end in
  Set.stable_dedup_list (module C) l;;

```

Cheers,  
Nicolás

---

<div class="post-metadata">

**Author:** ![milo](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@milo](https://discuss.ocaml.org/u/milo)\
**Post date:** [July 31, 2020, 7:19pm UTC](https://discuss.ocaml.org/t/core-kernel-comparator-and-polymorphism/6173/5 "2020-07-31T19:19:25Z")

</div>

That works. Thanks so much for your help!!

I hadn’t yet seen locally abstract types, so thanks for introducing me to them–gonna go read about them in Real World OCaml now 🙂
