De: "Laurent Vibert" <laurent.vibert@gmail.com>
À: "Frederic Fort" <frederic.fort@inria.fr>
Cc: "caml-list" <caml-list@inria.fr>
Envoyé: Vendredi 12 Mai 2023 16:19:14
Objet: Re: [Caml-list] Specifying the closure of an open polymorphic variant inside a module signature
This is a classical usecase for private row types :
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
endHowever, I don’t know how to define the module signature T_SUBTYPE.
Is this possible in OCaml ?
Thanks in advance,
Frédéric