2

I am trying to get a deeper understanding of the concepts behind Opa. In particular, I'm curious about the decision not to provide discriminated unions like in ML or Haskell (i.e. to define several constructors which wrap zero or more values), but stay with the record types and sum types of them. This decision makes total sense to me (maybe because I am more into OOP than FP), and it seems that one can implement everything that could be done with discriminated union.

However, can we say that the language still supports algebraic data types then? This one is not a practical question but rather one about terminology.

Btw, Wikipedia says yes, maybe it should be corrected.

Milo
  • 431
  • 2
  • 4
  • Opa provides sum and product types (possibly recursive, possibly abstract) so as far as I understand the term of Algebraic Data Types, I would say, yes absolutely, Opa has them. – akoprowski Mar 04 '12 at 21:59
  • Can you give me a link of such definition of Algebraic Data Types (according to which Opa has them)? Maybe the issue is that the term is not strictly defined, for example the definition from http://foldoc.org/algebraic+data+type does state (as I understand it) that values should be defined in terms of constructor applications, which is not the case in Opa. – Milo Mar 05 '12 at 14:46
  • In my understanding Algebraic Data Types are about providing sums of product types. I didn't realize that marking sum variants with constructors was crucial; if that's the case then indeed you are right, Opa does not qualify, although I'd consider it mainly a terminology dispute as its type-system is capable of everything one can do with algebraic data types (and more). – akoprowski Mar 06 '12 at 14:36

1 Answers1

3

In Opa everything is a record, and indeed you can implement what could be done with discriminated union.

Where in ocaml you will write type tree = Leaf of int | Node of tree * tree

In Opa it will be type tree = {int leaf} or {tree left, tree right}

You may notice that, unlike with Ocaml, in Opa you can manipulate records without explicitly defining its type. This is why even if Opa uses only records, it is still simple to define discriminated union.

Btw, did you had a look at http://doc.opalang.org/manual/The-type-system ?

Cédrics
  • 1,974
  • 11
  • 13
  • Yes, I did look at the manual. What grabbed my attention was "... if several cases (i.e. records) of a sum share a same label, this label must have the same type in each cases...". But I don't argue whether it is possible to use sums of records instead of discriminated unions. It just seems to me that this type system is rather different, and I wonder whether it's correct to assume that it is still a kind of algebraic-data-type-based one. – Milo Mar 04 '12 at 18:48