# When did Core remove polymorphic comparison operators in recent OCaml?

**URL:** <https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633>\
**Category:** Ecosystem\
**Tags:** core, base\
**Created:** [April 27, 2020, 3:21pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633 "2020-04-27T15:21:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![trungta](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/trungta/32/2909_2.png) [@trungta](https://discuss.ocaml.org/u/trungta)\
**Post date:** [April 27, 2020, 3:21pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/1 "2020-04-27T15:21:36Z")

</div>

Hi,

I’m using the Core library in a project, and recently when I upgraded my OCaml from 4.08.1 to 4.10.0, plenty of compilation errors suddenly appear for comparison expressions like:  
`if (xs = []) then ...` or `if (x = true) then ...`

I saw that this change was discussed in this [thread](https://discuss.ocaml.org/t/monomorphic-comparison-operator-of-janestreet-base-library/1585) about monomorphic comparison operators in Base, but did not expect that Core would make it a default behavior.

So I’d like to ask since which version that Core removed such polymorphic comparison operators?  
(I couldn’t find it in release notes of Core)

Also, if I define a sum type like `type ternary = True | False | Unkn`, what will be a correct way to write `if (x = True) then ...` (which is allowed in the new Core)?

I can temporarily fix by writing `if (x == True) then ...`, but using `==` doesn’t seem correct, since `==` is about comparing physical objects…

Thanks for spending your time to check my question.

---

<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 27, 2020, 3:59pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/2 "2020-04-27T15:59:44Z")

</div>

The change was announced in [[ANN] v0.13 release of Jane Street packages](https://discuss.ocaml.org/t/ann-v0-13-release-of-jane-street-packages/4735), although unfortunately it doesn’t look like the CHANGES.md file was updated in the repo. I would consider the thread to be the canonical announcement.

> [@trungta](#):
>
> Also, if I defined a sum type like `type ternary = True | False | Unkn` , what will be a correct way to write `if (x = True) then ...` (which is allowed in the new Core)?

Here’s a few suggestions:

1. Define equality/compare functions using [`ppx_compare`](https://github.com/janestreet/ppx_compare)

```ocaml
type ternary = True | False | Unkn [@@deriving equal]

let f x = if (equal_ternary x True) then ...

```

1. Define equality/compare functions manually

```ocaml
let equal_ternary t1 t2 =
  match t1, t2 with
  | True, True | False, False | Unkn, Unkn -> true
  | _ -> false

```

1. Explicitly request polymorphic comparison operators using the `Poly` module:

```ocaml
let f x = if (Poly.(=) x True) then ...

```

---

<div class="post-metadata">

**Author:** ![trungta](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/trungta/32/2909_2.png) [@trungta](https://discuss.ocaml.org/u/trungta)\
**Post date:** [April 27, 2020, 4:15pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/3 "2020-04-27T16:15:59Z")

</div>

@bbc2: Thank you very much for the reference and the suggestions! 👍

---

<div class="post-metadata">

**Author:** ![trungta](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/trungta/32/2909_2.png) [@trungta](https://discuss.ocaml.org/u/trungta)\
**Post date:** [April 27, 2020, 5:15pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/4 "2020-04-27T17:15:52Z")

</div>

btw,

> [@bcc32](#):
>
> type ternary = True | False | Unkn [@@deriving equal]

should be: `[@@deriving eq]`

---

<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 27, 2020, 5:23pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/5 "2020-04-27T17:23:26Z")

</div>

> [@trungta](#):
>
> should be: `[@@deriving eq]`

That depends on which preprocessor you are using. `[@@deriving equal]` comes from ppx\_compare, whereas `[@@deriving eq]` comes from [ppx\_deriving](https://github.com/ocaml-ppx/ppx_deriving). Base/Core and the like have better support for the former, which is a Jane Street project, although you can feel free to use the latter—the naming conventions are different, so it may not be as convenient.

---

<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 27, 2020, 5:42pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/6 "2020-04-27T17:42:29Z")

</div>

Actually, I see now that `[@@deriving eq]` defines a function called `equal_ternary`, so I guess the names do, in fact, match, so they would probably be interchangeable.

---

<div class="post-metadata">

**Author:** ![trungta](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/trungta/32/2909_2.png) [@trungta](https://discuss.ocaml.org/u/trungta)\
**Post date:** [April 27, 2020, 5:49pm UTC](https://discuss.ocaml.org/t/when-did-core-remove-polymorphic-comparison-operators-in-recent-ocaml/5633/7 "2020-04-27T17:49:36Z")

</div>

> [@bcc32](#):
>
> That depends on which preprocessor you are using. `[@@deriving equal]` comes from ppx\_compare, whereas `[@@deriving eq]` comes from [ppx\_deriving](https://github.com/ocaml-ppx/ppx_deriving).

Ah, thanks for the information. I wasn’t aware that there are two `ppx` that could produce `equal`.
