I'm having a lot of trouble figuring out what private type abbreviations are good for. Private type abbreviations arrived as a new feature in OCaml 3.11, but I still don't know where I would want to use them. Here's a simple example of a private type abbreviation. First, let's write down a trivial module that has an int type and a couple of trivial helper functions:

  1. module Int = struct
  2. type t = int
  3. let of_int x = x
  4. let to_int x = x
  5. end

That's not very interesting on its own, but we can apply a signature to make t private:

  1. module Priv : sig =
  2. type t = private int
  3. val of_int : int -> t
  4. val to_int : t -> int
  5. end = Int

And you can do something similar to make t abstract.

  1. module Abstr : sig =
  2. type t
  3. val of_int : int -> t
  4. val to_int : t -> int
  5. end = Int

The question is, when would one prefer Priv to Abstr?

Now, I'm aware that there are some optimization differences. For example, the following code uses the slower compare_val to compare its ints:

  1. Abstr.of_int 3 > Abstr.of_int 4

whereas this code uses a more efficient specialized-to-int comparator.

  1. Priv.of_int 3 > Priv.of_int 4

But the difference here is really a weird compiler optimization difference. There's no fundamental reason that the abstract version shouldn't be able to do the same optimization as the private one. Semantically, there's nothing interesting going on here.

There is one thing that I know how to do with a private type that I can't do with an abstract type: coercions. In particular, this code compiles:

  1. let five =
  2. let x = Priv.of_int 4 in
  3. (x :> int) + 1

whereas the same code with the abstract type fails. But that doesn't seem all that interesting, since I can write the following essentially equivalent code:

  1. let five =
  2. let x = Abstr.of_int 4 in
  3. Abstr.to_int x + 1

I had some other theories about what one could do with private types, but none of them seemed to pan out. For instance, the following all fail to compile:

  1. let p = Priv.of_int 4
  2.  
  3. let _ = p + 1
  4.  
  5. let _ =
  6. match p with
  7. | 0 -> true
  8. | _ -> false
  9.  
  10. let _ =
  11. let f x = (x :> int) + 1 in
  12. f p

So is there some deeper purpose here that I'm missing?

To be clear, ordinary private types are clearly useful, since they let you have access to pattern matching and record fields, while still giving you many of the advantages of a private type. But private type abbreviations are still pretty much a mystery to me.