Defining sets in ocaml

I had a problem creating a collection containing heterogeneous elements, in particular, the element will be structured as follows:

(a, 1), ((a, 1), 1)), ((a, 1), 1), 1), etc.

Can I do this with the Set ocaml module?

in addition, is there also some function that allows me to make a Cartesian product between sets (also heterogeneous)?

+6
set ocaml
source share
2 answers

You cannot create sets of heterogeneous elements. Of course, you can define a type to unify types if you know them beforehand. It looks like you are doing this, and it could be a recursive type defined by:

type ('a,'b) r = | L of 'a | N of (('a,'b) r * 'b) 

Thus, your examples will be built as

 N (L a,1) N ( N (L a,1),1) N ( N ( N (L a,1),1),1) 

Then you simply create an Ordered module to enable the comparison function.

In the case of creating a Cartesian product, you will not deal with heterogeneous elements at the moment, but with a tuple of the previous type. This will require a new Ordered module for comparison.

+8
source share

No, from http://caml.inria.fr/pub/docs/manual-ocaml/libref/Set.S.html you can see that the sets in the Set module are homogeneous.

Instead of dictionaries, you can use the http://alan.petitepomme.net/cwn/2010.02.09.html approach.

0
source share

All Articles