List Info

Thread: "ocaml_beginners"::[] Any workaround for a generic map in OCaml?




"ocaml_beginners"::[] Any workaround for a generic map in OCaml?
user name
2006-10-03 12:48:00


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

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Any workaround for a generic map in OCaml?
user name
2006-10-03 16:33:49

On Tuesday 03 October 2006 13:48, code17 wrote:
> I'm wondering whether it is possible to define a "generic" (if I'm using
>; the right terminology) map in plain OCaml.

So List.map is:

('a -> 'b) -> 'a list -> 'b list

and you want to generalise this beyond "list" to:

('a -> 'b) -> 'a 't1 -> 'b 't2

I'm no expert on type systems (I'm a physicist) but I believe this is beyond
OCaml's type system and, moreover, although there are type systems that can
represent such "higher-order" types, they are not decidable. I believe
Haskell can represent such a function, for example.

I'm sure I'll be corrected by a computer scientist though.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Any workaround for a generic map in OCaml?
user name
2006-10-03 19:21:27

On Tue, Oct 03, 2006 at 02:48:00PM +0200, code17 wrote:
> 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.

If you allow yourself to peek inside OCaml structures using Obj.magic
or whatever then it's possible to (a) iterate over an arbitrary
structure and (b) create another arbitrary structure following the
";shape"; of the original at the same time. So this suggests that what
you want is possible, but unless very carefully designed it would also
be unsafe.

As Jon says, you certainly couldn't write or even type such a function
in pure OCaml.

Rich.

--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )