i trying parametrise functor using type parameter in declaration. example, suppose have following gadt declaration:
type _ basic = | string : string -> string basic | integer : int -> int basic
i want use map.make
construct maps using above type key type. since cannot leave type variable free in type t = 'a basic
, have functor make 'a basic
orderedtype
:
module orderedbasic(type : sig type tag end) : (map.orderedtype type t = type.tag basic) = struct type t = type.tag basic let compare : type a. basic -> basic -> int = fun b1 b2 -> match b1, b2 | string s1, string s2 -> string.compare s1 s2 | integer i1, integer i2 -> compare i1 i2 end
i can create maps wanted:
module orderedbasicstring = orderedbasic(struct type tag = string end) module basicstringmap = map.make(orderedbasicstring)
and name map type:
type 'v string_map = 'v basicstringmap.t
however, parametrise type of maps type parameter of basic
, i.e., like:
type ('k, 'v) basic_map = 'v map.make(orderedbasic(struct type tag = 'k end)).t
but doesn't seem allowed: syntax error @ inline struct
definition. ultimately, want embed maps, can have of above basic key types, in gadt in:
type _ data = map : 'a data map.make(orderedbasic(struct type tag = 'b end)).t -> 'c data
is there way make work?
the problem map
defined on key types of fixed arity, , arity zero.
it's easy if functor using allows arity want:
type _ basic = | string : string -> string basic | integer : int -> int basic module type indexed = sig type 'a t val equal : 'a t -> 'a t -> bool end module makealist (elt : indexed) = struct type ('i, 'v) t = nil | cons of 'i elt.t * 'v * ('i, 'v) t let rec find elt = function | nil -> none | cons (x, v, xs) -> if elt.equal elt x v else find elt xs (* etc *) end module basicalist = makealist (struct type 'a t = 'a basic let equal : type . basic -> basic -> bool = fun b1 b2 -> match b1, b2 | string s1, string s2 -> s1 = s2 | integer i1, integer i2 -> i1 = i2 end) type _ data = map : ('a data, 'b) basicalist.t -> 'c data
i won't go far suggesting reimplement map
arity want, although solve problem - there might trick i'm not aware of let reuse map
.