# Check for type equality (unabstract abstract type)

**URL:** <https://discuss.ocaml.org/t/check-for-type-equality-unabstract-abstract-type/5859>\
**Category:** Learning\
**Created:** [May 28, 2020, 9:32am UTC](https://discuss.ocaml.org/t/check-for-type-equality-unabstract-abstract-type/5859 "2020-05-28T09:32:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagotch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sagotch/32/1777_2.png) [@sagotch](https://discuss.ocaml.org/u/sagotch)\
**Post date:** [May 28, 2020, 9:32am UTC](https://discuss.ocaml.org/t/check-for-type-equality-unabstract-abstract-type/5859/1 "2020-05-28T09:32:11Z")

</div>

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.

```auto
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

```

---

<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:** [May 28, 2020, 9:46am UTC](https://discuss.ocaml.org/t/check-for-type-equality-unabstract-abstract-type/5859/2 "2020-05-28T09:46:56Z")

</div>

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
