This is a classical usecase for private row types :
https://v2.ocaml.org/releases/5.0/htmlman/privatetypes.html#ss:private-rows


module type T_SUBTYPE =
sig
  type t_full = [ `A of int | `B of string | `C of int*int | `D ]
  type t = private [< t_full]
end

module OpsOnTSubtype (T: T_SUBTYPE) =
struct
  type t = T.t

  let f : t  -> int = function
    | `A i -> i
    | `B s -> String.length s
    | `C (l,r) -> l + r
    | `D -> 0
end


Le ven. 12 mai 2023 à 11:49, Frederic Fort <frederic.fort@inria.fr> a écrit :
Hello,

I want to define a functor that defines a type t and functions on that type such that t is a subtype of a polymorphic variant type.

For instance, if we have a type

type t_full = [ `A of int | `B of string | `C of int*int | `D ]

I want to be able to define a functor similar to this

module type T_SUBTYPE =
sig

type t_full = [ `A of int | `B of string | `C of int*int | `D ]

type t = [< t_full ] (* Not accepted by OCaml *)
end

module OpsOnTSubtype (T: T_SUBTYPE) =
struct
type t = T.t

let f : [> t ] -> int =
function
| `A i -> i
| `B s -> String.length s
| `C (l,r) -> l + r
| `D -> 0
end
However, I don’t know how to define the module signature T_SUBTYPE.
Is this possible in OCaml ?

Thanks in advance,
Frédéric