# The Base Module and Polymorphic Functions

**URL:** <https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041>\
**Category:** Learning\
**Created:** [December 12, 2018, 2:35am UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041 "2018-12-12T02:35:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deividvale](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/deividvale/32/1345_2.png) [@deividvale](https://discuss.ocaml.org/u/deividvale)\
**Post date:** [December 12, 2018, 2:35am UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041/1 "2018-12-12T02:35:12Z")

</div>

So, I’m testing some aspects of the language (as a beginner), and I cross with something particularly interesting to me.  
While seeing about polymorphism functions I have the following polymorphic function:

```
(* A function to remove duplicating elements from a list *)
let rec delDuplicates =
function
    | [] -> []
    | x :: [] -> x :: []
    | x :: y :: rest ->
        if x = y then delDuplicates (y :: rest)
        else x :: delDuplicates (y :: rest);;

```

This is a function to remove duplicate entries in a list. The compiler says it has the type:

`val delDuplicates : 'a list -> 'a list = <fun>`

So, I can evaluate the following cases:

`delDuplicates [1; 1; 1; 2; 3; 3];;`

`delDuplicates ["A"; "A"];;`

And I get the expected result from these two evaluations.

Interesting enough, if I call the Base module this function turns to a polymorphic to a non-polymorphic one as it now has the type:

`val delDuplicates : int list -> int list = <fun>`

So, evaluating `delDuplicates ["A"; "A"];;` gives me a type error.

This happens only if I use the Base module.

Someone can help me understand what is happening here? I plan to implement some mathematical notions in OCaml and it would be nice to have some functions from the base module and still have polymorphism for my functions.

I have called the module in this way:

```
#require "base";;
open Base;;
```

---

<div class="post-metadata">

**Author:** ![mc10](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@mc10](https://discuss.ocaml.org/u/mc10)\
**Post date:** [December 12, 2018, 3:14am UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041/2 "2018-12-12T03:14:05Z")

</div>

This is the same issue that was mentioned in [Possible bug in Base.List.map](https://discuss.ocaml.org/t/possible-bug-in-base-list-map/2987). In short, the standard `(=)` operator is polymorphic, but Base redefines it to be only defined for ints, since polymorphic compare is generally not what you want. See also: [Removing polymorphic compare from Core](https://discuss.ocaml.org/t/removing-polymorphic-compare-from-core/2994).

---

<div class="post-metadata">

**Author:** ![threepwood](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@threepwood](https://discuss.ocaml.org/u/threepwood)\
**Post date:** [December 13, 2018, 1:58pm UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041/3 "2018-12-13T13:58:00Z")

</div>

As @mc10 said, this is due to Base redefining `(=)` to hide the standard `(=)` function.

Here is what you can do:

1. Do not actually open Base. This way you keep the standard polymorphic operator. Note that there are reasons to not do that, as mentioned in the discussion @mc10 linked. If you want to call functions from Base without opening it, you can use their qualified names; for instance `Base.List.map`.

2. If you do open Base, you can still refer to the polymorphic operator as `Poly.(=)` (and there is also `Poly.equal`). So for instance in your function you could have : `if Poly.equal x y then ...`  
Or: `if Poly.(x = y) then ...` (this is called a local open)

3. One way to not use polymorphic comparison at all is to parametrise your code over the equality function. The simplest way to go about it is to have any function that has to be polymorphic and do equality tests take the equality function as an argument. For instance your function becomes :

```auto
let rec delDuplicates equal = function
   ...
       if equal x y then ...

```

Then your function has type:

```auto
val delDuplicates : ('a -> 'a -> bool) -> 'a list -> 'a list

```

And you call it as:

```auto
delDuplicates Int.equal [1; 1; 1; 2; 3; 3]

```

---

<div class="post-metadata">

**Author:** ![smolkaj](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/smolkaj/32/266_2.png) [@smolkaj](https://discuss.ocaml.org/u/smolkaj)\
**Post date:** [December 13, 2018, 5:24pm UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041/4 "2018-12-13T17:24:58Z")

</div>

Just to add to @threepwood’s answer: I would recommend making equal a named parameter. This will make your code self-documenting and much more readable.

```auto
 let delDuplicates ~equal = ...

```

EDIT: Yet another good pattern, if you’re not too concerned about safety, is to make `equal` an optional parameter that defaults to polymorphic compare:

```auto
let delDuplicates ?(equal=Poly.equal) = ...

```

This gives you both a self-documenting signature,

```auto
val delDuplicates : ?equal:('a -> 'a -> bool) -> 'a list -> 'a list = <fun>

```

as well as the convenience to write

```auto
delDuplicates [1; 1; 2; 2; 3]

```

as before. Plus you have the flexibility to do something like this:

```auto
delDuplicates ~equal:(fun x y -> x%2 = y%2) [1; 3; 5; 7]

```

---

<div class="post-metadata">

**Author:** ![deividvale](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/deividvale/32/1345_2.png) [@deividvale](https://discuss.ocaml.org/u/deividvale)\
**Post date:** [December 13, 2018, 9:51pm UTC](https://discuss.ocaml.org/t/the-base-module-and-polymorphic-functions/3041/5 "2018-12-13T21:51:13Z")

</div>

That is exactly what I need for my implementation. Thank you all for your answers.
