Tentative minimal example
Is there a way to create a GADT to encode a function return type and arity, leaving the function argument type as a parameter? I would like to create a type that encodes
Equalhas two arguments and returns boolHashhas a single argument and returns int
And then use that type to create some call functions with fixed argument type. For example, a call_t function such that call_t Equal has type t -> t -> bool, call_t Hash has type t -> int and a call_u function which is the same but with t replaced by u.
Is such a thing expressible with OCaml GADTs ?
Big picture
I’m trying to build a dynamic record type, i.e. a sort of record where the full set of fields is only known at runtime, with new fields being added dynamically via function calls. I would like to define operations on the whole record (compare, equal, hash, clone…) at the field level (so whenever a field is created, it must specify how to implement these operations). Critically, the set of operations is not fixed (some records may have none, some only have equal/compare, some have even more operations…).
The solution I found is to have the user specify a GADT ’a operand when creating the record, along with a merger function combine: ‘a operand -> ‘a -> ‘a -> ‘a, then for each field creation, also specify a function operand: ‘a operand -> field^n -> ‘a. This can then be lifted into a record wide operand: ’a operand -> record^n -> ‘a. The problem is how to encode the ^n… Right now all I can think of is to have one version per desired arity (so one GADT/combine function/operand function for unary operands (hash, clone), another one for binary operands (equal, compare)…).
Is there a way to encode that arity directly in the GADT, avoiding the need for this duplication ?