3

I am learning the union bit of OCaml and I got quite confused.


In C, a union is like this

union {
    int i;
    float f;
} x;
x.i = 0x27;
printf ("%f\n", x.f);

So, does union in OCaml serve the same purpose?

Let's take an example. Below is a union definition:

enter image description here

how do I use this union like the C example above?


Also for this one

enter image description here

Is type 'a set a union definition? How about type 'a set = 'a list? and why?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

1 Answers1

8

A discriminated union in OCaml is like a C union combined with an enum. So your example of a number type could be represented like this in C:

enum number_tag {
    ZERO, INTEGER, REAL
};

union number_value {
    int i; /* Only access this if tag is INTEGER */
    float f; /* Only access this if tag is REAL */
};

struct number {
    enum number_tag tag;
    union number_value value;
};

So you can ask what a number's type is by accessing the enum and then access the appropriate field of the union based on the value of the enum. Of course C won't stop from accessing the wrong field of the union.

OCaml on the other hand eliminates the possibility of accessing the wrong field. Since you can only access the values by pattern matching, you know that you always have the right type.

Pattern matching on a value of the number type would look like this in OCaml:

match value_of_type_number with
| Zero ->
    (* Handle the case that the number is Zero *)
| Integer i ->
    (* Handle the case that the number is an Integer with the value i *)
| Float f ->
    (* Handle the case that the number is a Float with the value f *)

The equivalent of that would be this:

switch(value_of_type_number.tag) {
case ZERO:
    /* Handle the case that the number is Zero */
    break;
case INTEGER:
    int i = value_of_type_number.value.i;
    /* Handle the case that the number is an Integer with the value i */
    break;
case FLOAT:
    float f = value_of_type_number.value.f;
    /* Handle the case that the number is a Float with the value f */
    break;
}

Is type 'a set a union definition? How about type 'a set = 'a list? and why?

type 'a set is not a definition at all. It just specifies the interface. It says that an implementation of this interface must define a type named set which takes one type parameter.

type 'a set = 'a list is a definition, but not of a discriminated union. It's simply a type alias. That is it says "'a set is just another name for 'a list'. A definition of a discriminated union would have a constructor as the first thing after the equals sign. Constructors always start with capital letters.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • could you please further give an example for how to use the `number union` in OCaml? – Jackson Tale Jan 09 '13 at 20:45
  • @JacksonTale I've added an example to my answer. – sepp2k Jan 09 '13 at 21:01
  • Could you please also help me with http://stackoverflow.com/questions/14234868/what-is-the-meaning-of-abstract-in-the-interface-definition ? Although I marked the answer, but I don't think that answer is clear enough for me. – Jackson Tale Jan 09 '13 at 21:04