Hi, there
I'm wondering whether it is possible to define a "generic" (if I'm using the
right terminology) map in plain OCaml.
Basically, List.map of OCaml maps a list of elements (all of type 'a) to a
list of elements (all of type 'b), but what I want is a function mapping
arbitrary data set (maybe as tuple, or any other data structure being capable
of representation) to another data set, given a proper worker function as
parameter.
For example. given
let to_list x = [x];
ideally, gmap will evaluate
gmap to_list (3,'a') ==> ([3],['a'])
gmap to_list (3, 3.14, Some "asdf") ==> ([3], [3.14], [Some "asdf"])
Here are some facts I understood:
1. Actually, I haven't seen a typed way for representing arbitrary data set in
sake of possibly infinite type parameters, but the generic behavior above is
definitely wanted in my case.
2. The gmap above is not legally typed in OCaml. So even if a solution
existed, it won't be as plain as the above one, that's why I'm asking for
"workaround";
3. Since I'm talking about "arbitrary" data set (arbitrary length, arbitrary
types), the solution should have some property of "infinity", solution such as
using sum type + pattern matching won't work since it's finite.
4. "Generic FP" and "pattern calculus" seem to be relevant ongoing
research. However I don't want to use anything other than native OCaml
(library is acceptable).
Thanks!
- code17
.