OK, I’m asking this because it’s bugging me and distracting me from carrying on learning OCaml - so please help!
Here’s the signature of a polymorphic set type - yes, there should be more functions defined but we’ll get there…
open Base open Core module type PolySet = sig type 'a t val empty : 'a t val mem : 'a -> 'a t -> bool val add : 'a -> 'a t -> 'a t val elts : 'a t -> 'a list end
Now in order to implement this with a
Core list) I need to make sure that
'a has some way of testing equality. I don’t want to know what
'a is yet - I just need to make sure it can be tested for equality. So I think that should look something like:
module type Equal = sig type t val equal : t -> t -> bool end module ListSet' (M : Equal) : PolySet with type 'a t := M.t list = struct type 'a t = M.t list let empty =  let mem x xs = List.mem xs x ~equal:M.equal let add x s = x :: s let elts s = s end
Which was a bit of a struggle - but feels right… and yet:
Error: Signature mismatch: ... Values do not match: val mem : M.t -> M.t list -> bool is not included in val mem : 'a -> M.t list -> bool
Which is so close. So how do I tell the compiler that
type 'a = M.t, when it knows that
type 'a t = M.t list? What am I missing here?