Check for type equality (unabstract abstract type)

Hi.

I want to extend the Map module with two functions, but it needs to now the type representation of a map.

Is there anything I can do to make sure that type 'a t and 'a internal are the same so I could safely use Obj.magic (or avoid it) ?

PS: Let say that I have no control over key_after and next signatures.

module type OrderedType = Map.OrderedType

module type S =
  sig
    include Map.S
    val key_after : (key -> key -> int) -> 'a t -> key
    val next : (key -> key -> int) -> 'a t -> key
  end

module Make (Ord: OrderedType) = struct

  include Map.Make (Ord)

  type 'a internal =
    | Empty
    | Node of {l:'a internal; v:key; d:'a; r:'a internal; h:int}

  let rec key_after f_compare = function
    | Empty -> raise Not_found
    | Node {l; v; r; _} ->
      let c = f_compare v in
      if c < 0 then try key_after f_compare l with Not_found -> v
      else if c > 0 then key_after f_compare r
      else v

  let key_after f_compare (x : 'a t) =
    key_after f_compare (Obj.magic x)

  let rec next x = function
    | Empty -> raise Not_found
    | Node {l; v; r; _} ->
      let c = Ord.compare x v in
      if c < 0 then try next x l with Not_found -> v else next x r

  let next k (x : 'a t) =
    next k (Obj.magic x)

end

No, there isn’t a way. If you need that kind of access I would just copy the stdlib implementation fully. That way at least your code will be safe.

Cheers,
Nicolás