[I’m hacking with the modular implicits variant, though this question is more generic.]
I’m trying to figure out the idiom for a particular kind of concatenation of signatures, and having no luck. Does anybody know if there’s a trick for this? [other than, y’know, manually copying the relevant bits?]
Suppose I have a signature:
module type ZERO = sig type t val zero : unit -> t end
and another one
module type SUBSCRIPTABLE = sig type item_t type t val sub : t -> int -> item_t val make : item_t -> item_t list -> t end
I’d like to make one that is the “sum” of these two signatures, where
Z.t = S.item_t. So I’d like the signature
module type ZERO_SUBSCRIPTABLE = sig type item_t type t val zero : unit -> item_t val sub : t -> int -> item_t val make : item_t -> item_t list -> t end
Now why would I like this? Well, there are types that enjoy a
zero value. And types that don’t. For types that enjoy a zero value, I might want to have the
make that builds a vector automatically use that zero value to initialize the vector, where for types that don’t have a zero value, the caller would have to provide the initializer value.
More generally, in Rust, often in places where you can provide a type, you can provide a type that enjoys some collection of traits, and these are implicitly somewhat like what I describe above: the concatenation of signatures, with some renaming of types. And in some cases, the collection of traits that a type enjoys can be … significant, e.g.
pub fn spmat_dot_densevec<T>(sp_mat : &CsMatI<T, u64>, v: &ArrayView<T, Dim<[usize; 1]>>) -> Array<T, Dim<[usize; 1]>> where T : Zero + Sum + Copy + MulAdd + MulAdd<Output = T> + Send + Sync, for<'r> &'r T: Mul<&'r T, Output = T>,
which says that the item-type
- has a zero value
- has an addition operation
- can be copied
- has a multiply-add operation
- that multiply-add operation produces the same type T
- and has some good multi-threading properties
Maybe this is just something that isn’t doable in OCaml, simply due to cumbersome syntax. That’d be OK – I just want to figure out the answer.