9
$\begingroup$

Consider a C style enum:

 enum colour {
    red,
    blue,
    green
 };

In haskell this would be:

 data colour = red | blue | green

What is the correct name for each of the possible values? or if there isn't a single correct one what is a good one?

I am sure I have seen this called an atom. Though I am unable to find the reference. I am inclined towards this definition as an atom is an indivisible unit.

Has anyone come across this?

(I think atom comes from set theory here - http://en.wikipedia.org/wiki/Urelement though you also see it in type theory - for example https://paigenorth.github.io/ross_lecture_notes.pdf)

Which of the following make (more) sense:

  • red is a value
  • red is an atom
  • red is a symbol
  • red is a unit type
  • red is a label

[I am trying to come up with a good name for the concept which is correct from the typing point of view and ideally not too overloaded. Each of the above has different implications and pros and cons.]

Wikipedia describes an enum type theoretically as:

In type theory, enumerated types are often regarded as tagged unions of unit types

Is this correct? I thought there was only one unit type? The wikipedia page for unit type is possibly ambiguous:

a unit type is a type that allows only one value

but thereafter we talk about "the unit type".

If we say an enum is a set of unit types distinguished by their tag does that imply nominal typing? That is (in pseudo-haskell):

 newtype tag = unit type + name

By this definition of univalence:

"The principle of univalence, introduced by Vladimir Voevodsky, states that two equivalent types can be freely substituted for each other. This means that if you have two types that are equal, you can treat them as interchangeable without affecting the correctness of your program."

The unit types are univalent but a "unit type + a name" (whatever we call it) is not interchangage for a unit type + a different name because it is a "new" type.


If I also defined:

 data trafficlight = red | amber | green

Is the red atom in trafficlight the same as the red atom in colour?

An implementer's inclination is to say yes. Your enum type is constructed internally as an array of the possible atoms. But of course it is still wrong to assign a variable of type colour to one of type trafficlight because they (the enum types) are distinct regardless of the atoms that make them up. Or considered another way the atoms can be re-used as members of different sets (enums).

$\endgroup$
6
  • 3
    $\begingroup$ It's called a data constructor in Haskell and related languages. $\endgroup$
    – alephalpha
    Jul 4, 2023 at 1:54
  • $\begingroup$ Ruby calls this a symbol ruby-doc.org/core-2.6.3/Symbol.html (but symbols are more general) $\endgroup$ Jul 4, 2023 at 8:24
  • $\begingroup$ I think atom comes from set theory here - en.wikipedia.org/wiki/Urelement $\endgroup$ Jul 4, 2023 at 8:24
  • $\begingroup$ Lisp and Prolog use the term "atom", but in both of those languages atoms have slightly different semantics; C has no equivalent of getprop and setprop. cs.cmu.edu/Groups/AI/html/cltl/clm/node108.html $\endgroup$
    – Pseudonym
    Aug 15, 2023 at 2:17
  • $\begingroup$ "I thought there was only one unit type?" - sure, but nothing prevents you from having a union of a type with itself. If the type is tagged different ways before that union operation, it might even be useful. $\endgroup$ Aug 15, 2023 at 8:12

6 Answers 6

8
$\begingroup$

For C-style enums where there is no further structure in the values, I would call them enum values or members of the enum. It might also make sense to call them just "values", "atoms" or "symbols" but this is less specific since these words don't imply that they belong to an enum type. It might make sense to call them "unit types" if they are indeed types (not just values), but I think that would be confusing even if it were technically true.

For Rust-style enums where enum values can have their own fields, I would call them variants, since e.g. if MyEnum::Foo(1, 2) is one of the possible values of the enum then MyEnum::Foo is not a value itself, so it wouldn't make sense to call it a value or a member of the enum type.


Wikipedia describes an enum type theoretically as:

In type theory, enumerated types are often regarded as tagged unions of unit types

Is this correct? I thought there was only one unit type?

Yes, Wikipedia is correct. It's totally possible to have distinct unit types, but also the unit types in a tagged union don't have to be distinct unit types; it is the "tag" of the tagged union which tells them apart, not the value that gets tagged. That is, in type theory 1 + 1 = 2 even if both 1s are the same, because that's just how sum types work.


If I also defined:

data trafficlight = red | amber | green

Is the red atom in trafficlight the same as the red atom in colour?

This is a matter of interpretation, so in some languages (e.g. a union of literal types or a discriminated union in Typescript) the answer could be "yes" and in others (e.g. Java or Rust) it could be "no". In languages where enum values are just names for integers, the answer might even be "yes, but only because they are both 0".

$\endgroup$
12
  • $\begingroup$ I think MyEnum::Foo is called a enum constructor. $\endgroup$
    – alephalpha
    Jul 4, 2023 at 2:01
  • $\begingroup$ @alephalpha It can be, but I think it is more general to call it a variant, since the name MyEnum::Foo can also occur in patterns where it destructs values instead of constructing them. $\endgroup$
    – kaya3
    Jul 4, 2023 at 2:03
  • $\begingroup$ MyEnum::Foo is a value by itself, though its type is some function item type instead of MyEnum. This value is called a enum constructor. In other cases, I agree that "variant" is a better name. $\endgroup$
    – alephalpha
    Jul 4, 2023 at 2:24
  • $\begingroup$ They're called cases in Swift, because of the syntax: enum MyEnum { case foo(Int, Int) }. $\endgroup$
    – Bbrk24
    Jul 4, 2023 at 2:46
  • $\begingroup$ I find rust's use of the word "enum" to mean discriminated union baffling. If you combine too things you should take the name from the more general concept and discriminated unions are definitely more general. $\endgroup$ Jul 4, 2023 at 7:44
2
$\begingroup$

The example you give is a sum type and its elements could be called summands but it is not common outside of theory papers.

data colour = red | blue | green

A record type would be a product type and its constituents are factors but they are commonly called elements or fields.

$\endgroup$
1
$\begingroup$

I usually just call them constants (for the names) and primitive values (for their value, like what true and false are usually called). Enum values could be more clear in the context of a specific language, where in most cases, all such values that are not built-in are defined in a enum.

But in the context of language design, while it may not answer the question, I usually call a enum type a "foreign key type", for the foreign keys in a relational database could give much more insights about enums than what all the languages have implemented. Their value unfortunately has the very ambiguous name in general context, a reference to a row or a record from another table.

$\endgroup$
4
  • $\begingroup$ Are you saying that in your writing, the phrase "foreign key type" means "enumerated type"? I don't understand that at all. $\endgroup$ Jul 4, 2023 at 12:40
  • $\begingroup$ If the purpose of looking for a name is to find more information and get a more concise definition about it, this may provide something without providing a good name. $\endgroup$
    – user23013
    Jul 4, 2023 at 15:17
  • $\begingroup$ I'm having a lot of trouble understanding what you're saying. I think I'm missing some of the context; I don't know whether you're talking about programming languages, databases, or both, and I know that you've mentioned both enumerated types and foreign keys, but I can't piece together the details of what you're saying about them. Could you give an example illustrating what you mean? $\endgroup$ Jul 4, 2023 at 15:35
  • $\begingroup$ I think he has interpreted my question as I am looking for a good name for an "enumeration" which is certainly part of the question. Though the type theory part is in some ways more interesting. $\endgroup$ Jul 4, 2023 at 16:49
1
$\begingroup$

Cppreference calls them "enumerators" which I think is a well fitting term.

$\endgroup$
1
$\begingroup$

In my compiler and IR, they are called enum literals. In essence, because their properties are much like those of integer literals.

Most of your proposals already imply a specific kind of interpretation of what an enum is. However, there are many variations of the concept and not all can be combined with the proposals made here. For instance, some languages allow enum constants from different types to be mixed. Some introduce the literal names in the global scope, others introduce them inside the scope of the enclosing type. The base type and ability to perform runtime type checks vary as well. Thus, using names from type or set theory should only be done for enum concepts that naturally fit those theories.

The word label is a bit odd as it is usually associated with control flow.

$\endgroup$
1
$\begingroup$

PHP calls them legal values, in the context of making invalid states unrepresentable, but declares each member with the keyword case, as it avoids adding another keyword on language.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .