Is it possible to define a polymorphic variant type as the union of other polymoprhic variant types?

If you create a couple types to capture groups of polymorphic variant constructors, as in

type group_a = [ `A | `B | `C ]
type group_b = [ `D | `E | `F ]

and you want to create a third type containing all the constructors in the two groups,

type combined = [ `A | `B | `C | `D | `E | `F ]

Is there any ocaml syntax that lets you define combined in terms of group_a and group_b? Something like

(* V is an imaginary operator meaning "union" *)
type combined = group_a V group_b

Yeps!

$ ocaml
        OCaml version 4.13.0

# type group_a = [ `A | `B | `C ];;
type group_a = [ `A | `B | `C ]
# type group_b = [ `D | `E | `F ];;
type group_b = [ `D | `E | `F ]
# type combined = [ group_a | group_b ];;
type combined = [ `A | `B | `C | `D | `E | `F ]
4 Likes

An when you will want to still pattern match on subsets the pattern syntax is #subset:

# let group_to_string : combined -> string = 
function #group_a -> "Group a" | #group_b -> "Group b";;
val group_to_string : combined -> string = <fun>
# group_to_string `B;;
- : string = "Group a"
4 Likes

Thanks! The answer was right in front of me.