r/ocaml•
wfdctrl

Type level OR

I'm using phantom types to track whether the elements of a sorted list are unique. The signature of the module is as follows:

type nonuniq
type uniq

type elt
type _ t = private elt list

val of_list: elt list -> nonuniq t
val inter: ? t -> ? t -> ? t
val uniq: _ t -> uniq t
...

The problem is how to specify the signature of the intersection. val inter: 'state t -> 'state t -> 'state t is not good enough because an intersection between a list with unique and a list with non-unique elements makes sense and results in a list with unique elements. Basically the return type would need to be a result of a boolean OR function:

uniq -> uniq -> uniq
uniq -> nonuniq -> uniq
nonuniq -> uniq -> uniq
nonuniq -> nonuniq -> nonuniq

An obvious solution is to split the signature into two based on the first argument, however it is not the most elegant:

val inter: uniq t -> _ t -> uniq t
val inter': nonuniq t -> 'state t -> 'state t

I found a potentially better solution that almost works (based on link):

 type nonuniq = [`NonUniq]
 type uniq = [nonuniq | `Uniq]

 type elt
 type _ t
 
 val of_list: elt list -> [> nonuniq] t
 val inter: 'state t -> 'state t -> 'state t
 val uniq: _ t -> uniq t
 ...
 
 val is_uniq: uniq t -> unit (* for testing *)
 val is_nonuniq: nonuniq t -> unit

The problem is however that [> nonuniq] t unifies with uniq t, so is_uniq always type checks (is_nonuniq works as expected, though). Is there a way to write a function that only accepts uniq t (and not [> nonuniq] t)?

Posted Oct 8, 2021, 7:49 PM UTC · edited

134
gasche•

A general solution, using GADTs, is to encode the type-level operations as relations through GADT indices, effectively embedding a Prolog-style program in the type declaration. This is not implicit at the usage site: users need to provide a "proof" of the computation.

type uniq = Uniq
type nonu = Nonu

(* (a, b, c) inter means (inter(a,b) = c) *)
type (_, _, _) inter =
| UA : (uniq,   'a, uniq) inter
| AU : (  'a, uniq, uniq) inter
| NN : (nonu, nonu, nonu) inter

val iter : ('a, 'b, 'c) inter -> 'a t -> 'b t -> 'c t
7
wfdctrlOP•

Thanks, I was actually aware of this approach, but I didn't consider it to be much better than splitting the signature, it's still on the user to choose the right alternative. My other failed attempt was to try to pack the operation with the data, but I couldn't figure out how to combine two of them together (and get a properly polymorphic result). I tryed something like this:

type uniq = U
type nonuniq = N

type (_, _, _) or_ =
  | Uniq : (uniq, _, uniq) or_
  | NonUniq : (nonuniq, 'x, 'x) or_

let or_ : type a b c. (a, b, c) or_ -> (b, _, _) or_ -> (c, _, _) or_ =
  fun x y ->
  match x, y with
  | Uniq, Uniq -> Uniq   
  | NonUniq, Uniq -> Uniq
  | Uniq, NonUniq -> Uniq
  | NonUniq, NonUniq -> NonUniq

There is no way to make this work, right? I guess considering there are no implicit features in the language, there really is no better way to do this...

1
isaacDeFrain•

I used a slightly different approach incorporating both GADTs and polymorphic variants.

type elt
type uniq = [`U]
type nonu = [`N]
type _ t = private
  | U : elt list -> [> uniq] t
  | N : elt list -> [> nonu] t
val of_list : elt list -> [nonu | uniq] t
val inter : 'a t -> 'b t -> [nonu | uniq] t
val uniq : 'a t -> uniq t
val nonu : 'a t -> nonu t
val is_uniq : uniq t -> unit
val is_nonu : nonu t -> unit

In terms of typing, I believe this is the best we can do for inter considering that it can return both unique and non-unique lists.

This was a fun problem to solve (I would include the entire implementation, but reddit is not allowing me to paste atm...). I just wanted to say that I got to this solution starting from something like you suggested and running into the compiler complaining about types not being compatible. After fixing those errors, I arrived at this solution.

One thing I noticed in writing this up is that there seems to be an assumption that the intersection of two lists with non-unique elements must have non-unique elements. This simply isn't true. For example, inter [1;1] [1;2;2] = [1] (the types here aren't correct, but hopefully the point is clear). So even if both lists are built from the N constructor, the intersection may need to given the U constructor (if we want those constructors to actually tell us about the elements in the list).

2
wfdctrlOP•

Was your solution meant to be used like this:

is_uniq (uniq (inter (uniq (of_list [1; 2; 3])) (nonuniq (of_list [2; 3; 4]))))

If so, it's not quite what I had in mind. I'm using the uniq phantom type to track whether the uniqueness is preserved between the operations. That way I can avoid running the uniq function (which removes the duplicates) when it is not necessary. Maybe the poor naming was the cause of the confusion, uniq is supposed to mean: "guaranteed to be uniqe" and nonuniq: "not guaranteed to be unique" (not literately non-unique). Sorry about that.

There's also a chance I did something wrong when trying to re-implement your solution. In any case thank you for the effort :)

1