Changeset 4da9142


Ignore:
Timestamp:
Apr 18, 2024, 10:23:34 PM (3 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
2b6db03
Parents:
c148966
Message:

more proofreading on enumerations

Location:
doc/theses/jiada_liang_MMath
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    rc148966 r4da9142  
    137137\section{Pure Enumerators}
    138138
    139 An empty enumerator type, @enum()@, implies the enumerators are pure symbols without values but set properties;
     139An empty enumerator type, @enum()@, implies the enumerators are opaque symbols without values but set properties;
    140140hence, there is no default conversion to @int@.
    141141
  • doc/theses/jiada_liang_MMath/background.tex

    rc148966 r4da9142  
    4848
    4949\section{C Enumeration}
     50\label{s:CEnumeration}
    5051
    51 The C enumeration has the following syntax and semantics.
     52The C enumeration has the following syntax~\cite[\S~6.7.2.2]{C11}.
     53\begin{clang}[identifierstyle=\linespread{0.9}\it]
     54$\it enum$-specifier:
     55        enum identifier$\(_{opt}\)$ { enumerator-list }
     56        enum identifier$\(_{opt}\)$ { enumerator-list , }
     57        enum identifier
     58enumerator-list:
     59        enumerator
     60        enumerator-list , enumerator
     61enumerator:
     62        enumeration-constant
     63        enumeration-constant = constant-expression
     64\end{clang}
     65The terms \emph{enumeration} and \emph{enumerator} used in this work \see{\VRef{s:Terminology}} come from the grammar.
     66The C enumeration semantics is discussed using examples.
     67
     68An unnamed enumeration is used to provide secondary renaming, like a @const@ declaration in other languages.
     69\begin{clang}
     70enum { Size = 20, Pi = 3.14159 };   // unnamed enumeration $\(\Rightarrow\)$ no ordering
     71\end{clang}
     72This declaration form is not an enumeration even though it is declared using an @enum@ because it has none of the following enumeration properties.
     73
     74A \emph{named} enumeration type is an actual enumeration.
    5275\begin{clang}
    5376enum Weekday { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun, };
  • doc/theses/jiada_liang_MMath/intro.tex

    rc148966 r4da9142  
    11\chapter{Introduction}
    22
    3 All types in a programming language must have a set of constants, and these constants have \Newterm{primary names}, \eg integral types have constants @-1@, @17@, @12345@, \etc.
    4 Constants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types.
     3All types in a programming language must have a set of constants, and these constants have \Newterm{primary names}, \eg integral types have constants @-1@, @17@, @0xff@, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}}, \etc.
     4Con\-stants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types.
     5(In \CFA, the primary constants @0@ and @1@ can be overloaded for any type.)
    56Hence, each primary constant has a symbolic name referring to its internal representation, and these names are dictated by language syntax related to types.
    67In theory, there are an infinite set of primary names per type.
    78
    8 \Newterm{Secondary naming} is a common practice in mathematics and engineering, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.
     9\Newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (mega byte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.
    910Many programming languages capture this important software-engineering capability through a mechanism called \Newterm{constant} or \Newterm{literal} naming, where a secondary name is aliased to a primary name.
    10 In some cases, secondary naming is \Newterm{pure}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@.
    11 (The names the thing.)
     11Its common purpose is to eliminate duplication of the primary constant throughout a program.
     12For example, the secondary name replaces its primary name, thereafter changing the binding of the secondary to primary name automatically distributes the rebinding throughout the program.
     13In some cases, secondary naming is \Newterm{opaque}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@.
    1214Because a secondary name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \Newterm{rvalue}\footnote{
    1315The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}.
     
    1820enumerate (verb, transitive).
    1921To count, ascertain the number of;
    20 \emph{more
    21 usually, to mention (a number of things or persons) separately, as if for the
    22 purpose of counting};
    23 to specify as in a list or catalogue.~\cite{OED}
     22more usually, to mention (a number of things or persons) separately, as if for the purpose of counting;
     23to specify as in a list or catalogue.~\cite{OEDenumerate}
    2424\end{quote}
    25 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are restricted to hold only the secondary names.
     25Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are \emph{often} restricted to hold only the secondary names.
    2626It is possible to enumerate among set names without having an ordering among the set elements.
    2727For example, the week, the weekdays, the weekend, and every second day of the week.
     
    2929for ( cursor in Mon, Tue, Wed, Thu, Fri, Sat, Sun } ... $\C[3.75in]{// week}$
    3030for ( cursor in Mon, Tue, Wed, Thu, Fri } ...   $\C{// weekday}$
    31 for ( cursor in Thu, Fri } ...                                  $\C{// weekend}$
     31for ( cursor in Sat, Sun } ...                                  $\C{// weekend}$
    3232for ( cursor in Mon, Wed, Fri, Sun } ...                $\C{// every second day of week}\CRT$
    3333\end{cfa}
    34 This independence from internal representation allows multiple names to have the same representation (eight note, quaver), giving synonyms.
     34This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms.
    3535A set can have a partial or total ordering, making it possible to compare set elements, \eg Monday is before Friday and Friday is after.
    3636Ordering allows iterating among the enumeration set using relational operators and advancement, \eg
     
    3838for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ...
    3939\end{cfa}
    40 Here the internal representations for the secondary names are \emph{generated} rather than listing a subset of names.
     40Here the internal representations for the secondary names are logically \emph{generated} rather than listing a subset of names.
     41
     42Hence, the fundamental aspects of an enumeration are:
     43\begin{enumerate}
     44\item
     45It defines a type from which instants can be generated.
     46\item
     47The type lists a finite set of secondary names, which become its primary constants.
     48This differentiates an enumeration from general types with an infinite number of primary constants.
     49\item
     50An enumeration's secondary names represent constants, which follows from their binding (aliasing) to primary names, which are constants.
     51\item
     52For safety, an enumeration instance is restricted to hold only its type's secondary names.
     53\item
     54There is a mechanism for \emph{enumerating} over the secondary names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.
     55\end{enumerate}
    4156
    4257
    4358\section{Terminology}
    44 
    45 The term \Newterm{enumeration} defines the set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name.
     59\label{s:Terminology}
     60
     61The term \Newterm{enumeration} defines a type with a set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name \see{\VRef{s:CEnumeration}}.
    4662As well, an enumerated type has three fundamental properties, \Newterm{label}, \Newterm{order}, and \Newterm{value}.
    4763\begin{cquote}
     
    7288\section{Motivation}
    7389
    74 Some programming languages only provide secondary renaming, which can be simulated by an enumeration without ordering.
    75 \begin{cfa}
    76 const Size = 20, Pi = 3.14159;
    77 enum { Size = 20, Pi = 3.14159 };   // unnamed enumeration $\(\Rightarrow\)$ no ordering
    78 \end{cfa}
    79 In both cases, it is possible to compare the secondary names, \eg @Size < Pi@, if that is meaningful;
    80 however, without an enumeration type-name, it is impossible to create an iterator cursor.
    81 
    82 Secondary renaming can similate an enumeration, but with extra effort.
     90Some programming languages only provide direct secondary renaming.
     91\begin{cfa}
     92const Size = 20, Pi = 3.14159, Name = "Jane";
     93\end{cfa}
     94Here, it is possible to compare the secondary names, \eg @Size < Pi@, if that is meaningful.
     95
     96Secondary renaming can simulate an enumeration, but with extra effort.
    8397\begin{cfa}
    8498const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7;
    8599\end{cfa}
    86 Furthermore, reordering the enumerators requires manual renumbering.
     100Any reordering of the enumerators requires manual renumbering.
    87101\begin{cfa}
    88102const Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7;
    89103\end{cfa}
    90 Finally, there is no common type to create a type-checked instance or iterator cursor.
    91 Hence, there is only a weak equivalence between secondary naming and enumerations, justifying the enumeration type in a programming language.
    92 
    93 A variant (algebraic) type is often promoted as a kind of enumeration, \ie a varient type can simulate an enumeration.
    94 A variant type is a tagged-union, where the possible types may be heterogeneous.
    95 \begin{cfa}
     104Finally, there is no type to create a type-checked instance or iterator cursor.
     105Hence, there is only a weak equivalence between secondary naming and enumerations, justifying a seperate enumeration type in a programming language.
     106
     107A variant (algebraic) type is often promoted as a kind of enumeration, \ie a variant type can simulate an enumeration.
     108Fundamentally, a variant type is a tagged-union, where the tag is normally opaque and the types are usually heterogeneous.
     109\begin{cfa}[morekeywords={variant}]
    96110@variant@ Variant {
    97111        @int tag;@  // optional/implicit: 0 => int, 1 => double, 2 => S
     
    103117};
    104118\end{cfa}
    105 Crucially, the union implies instance storage is shared by all of the variant types.
    106 Hence, a variant is dynamically typed, as in a dynamic-typed programming-language, but the set of types is statically bound, similar to some aspects of dynamic gradual-typing~\cite{Gradual Typing}.
    107 Knowing which type is in a variant instance is crucial for correctness.
    108 Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary;
    109 otherwise, a tag is required to denote the particular type in the variant and the tag checked at runtime using some form of type pattern-matching.
    110 
    111 The tag can be implicitly set by the compiler on assignment, or explicitly set by the program\-mer.
    112 Type pattern-matching is then used to dynamically test the tag and branch to a section of code to safely manipulate the value, \eg:
     119Crucially, the union implies instance storage is shared by all the variant types, and therefore, before a variant type can be used in a statically-typed expression, it must be dynamically discriminated to its current contained type.
     120Hence, knowing which type is in a variant instance is crucial for correctness.
     121Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary.
     122Otherwise, a tag is required to denote the particular type in the variant, and the tag is discriminated at runtime using some form of type pattern-matching, after which the value can be used in a statically-typed expression.
     123
     124A less frequent variant case is multiple variants with the same type, which normally requires explicit naming of the tag to disambiguate among the common types.
     125\begin{cquote}
     126\begin{tabular}{@{}l@{\hspace{30pt}}l@{}}
     127\begin{cfa}[morekeywords={variant}]
     128variant VariantCT {
     129        case @car@: int i;  // explicitly typed
     130        case @boat@: int i;
     131        case @bridge@: int i;
     132};
     133\end{cfa}
     134&
     135\begin{cfa}[morekeywords={variant}]
     136variant VariantCU {
     137        case @car@: ;  // empty or unit type
     138        case @boat@: ;
     139        case @bridge@: ;
     140};
     141\end{cfa}
     142\end{tabular}
     143\end{cquote}
     144Here, the explicit tag name is used to give different meaning to the values in the common @int@ type, \eg the value 3 has different interpretations depending on the tag name.
     145It is even possible to remove the type or use the empty @unit@ type (@struct unit {}@).
     146It is this tag naming that is used to simulate an enumeration.
     147
     148Normally, the variant tag is implicitly set by the compiler based on type, but with common types, a tag name is required to resolve type ambiguity.
     149\begin{cfa}
     150Variant v = 3;                                                  $\C{// implicitly set tag to 0 based on type of 3}$
     151VariantCT ve = boats.3;                                 $\C{// explicitly set tag to 1 using tag name}$
     152\end{cfa}
     153Type pattern-matching is then used to dynamically test the tag and branch to a section of statically-typed code to safely manipulate the value, \eg:
     154\begin{cquote}
     155\begin{tabular}{@{}l@{\hspace{30pt}}l@{}}
    113156\begin{cfa}[morekeywords={match}]
    114 Variant v = 3;  // implicitly set tag to 0
    115 @match@( v ) {    // know the type or test the tag
    116         case int { /* only access i field in v */ }
    117         case double { /* only access d field in v */ }
    118         case S { /* only access s field in v */ }
     157@match@( v ) { // know type implicitly or test tag
     158        case int { /* only access i field */ }
     159        case double { /* only access d field */ }
     160        case S { /* only access s field */ }
    119161}
    120162\end{cfa}
    121 For safety, either all variant types must be listed or a @default@ case must exist with no field accesses.
    122 
    123 To simulate an enumeration with a variant, the tag is \emph{re-purposed} for either ordering or value and the variant types are omitted.
    124 \begin{cfa}
    125 variant Weekday {
    126         int tag; // implicit 0 => Mon, ..., 6 => Sun
    127         @case Mon;@ // no type
     163&
     164\begin{cfa}[morekeywords={match}]
     165@match@( ve ) {
     166        case car: int { /* car interpretation */ }
     167        case boat: int { /* boat interpretation */ }
     168        case bridge: int { /* bridge interpretation */ }
     169}
     170\end{cfa}
     171\end{tabular}
     172\end{cquote}
     173For safety, some languages require all variant types to be listed or a @default@ case with no field accesses.
     174
     175To further strengthen the simulate for an enumeration with different values, each variant type can be a @const@ type or the tag becomes non-opaque, possibly taking advantage of the opaque auto-numbering.
     176\begin{cquote}
     177\begin{tabular}{@{}l@{\hspace{30pt}}l@{}}
     178\begin{cfa}
     179variant Week {
     180        case Mon: const int = 0;
    128181        ...
    129         @case Sun;@
    130 };
    131 \end{cfa}
    132 The type system ensures tag setting and testing are correctly done.
    133 However, the enumeration operations are limited to the available tag operations, \eg pattern matching.
    134 \begin{cfa}
    135 Week week = Mon;
    136 if ( @dynamic_cast(Mon)@week ) ... // test tag == Mon
    137 \end{cfa}
     182        case Sat: const int = 5;
     183        case Sun: const int = 10;
     184};
     185\end{cfa}
     186&
     187\begin{cfa}
     188variant Week {
     189        case Mon: ;  // tag auto-numbering
     190        ...
     191        case Sat: ;
     192        case @Sun = 10@: ; // directly set tag value
     193};
     194\end{cfa}
     195\end{tabular}
     196\end{cquote}
     197Directly setting the tag implies restrictions, like unique values.
     198In both cases, instances of @Week@ are @const@ (immutable).
     199However, usage between these two types becomes complex.
     200\begin{cfa}
     201Week day = Week.Mon;  // sets value or tag depending on type
     202if ( day == Week.Mon )   // dereference value or tag ?
     203\end{cfa}
     204Here, the dereference of @day@ should return the value of the type stored in the variant, never the tag.
     205If it does return the tag, some special meaning must be given to the empty/unit type, especially if a variant contains both regular and unit types.
     206
     207
     208In general, the enumeration simulation and the variant extensions to support it, are deviating from the normal use of a variant (union) type.
     209As well, the enumeration operations are limited to the available tag operations, \eg pattern matching.
    138210While enumerating among tag names is possible:
    139211\begin{cfa}[morekeywords={in}]
    140 for ( cursor in Mon, Wed, Fri, Sun ) ...
    141 \end{cfa}
    142 ordering for iteration would require a \emph{magic} extension, such as a special @enum@ variant, because it has no meaning for a regular variant, \ie @int@ < @double@.
    143 
    144 However, if a special @enum@ variant allows the tags to be heterogeneously typed, ordering must fall back on case positioning, as many types have incomparable values.
    145 Iterating using tag ordering and heterogeneous types, also requires pattern matching.
    146 \begin{cfa}[morekeywords={match}]
    147 for ( cursor = Mon; cursor <= Fri; cursor = succ( cursor) ) {
    148         match( cursor ) {
    149                 case Mon { /* access special type for Mon */ }
    150                 ...
    151                 case Fri { /* access special type for Fri */ }
    152                 default
    153         }
    154 }
    155 \end{cfa}
    156 If the variant type is changed by adding/removing types or the loop range changes, the pattern matching must be adjusted.
    157 As well, if the start/stop values are dynamic, it may be impossible to statically determine if all variant types are listed.
    158 
    159 Re-purposing the notion of enumerating into variant types is ill formed and confusing.
    160 Hence, there is only a weak equivalence between an enumeration and variant type, justifying the enumeration type in a programming language.
     212for ( cursor in Week.Mon, Week.Wed, Week.Fri, Week.Sun ) ...
     213\end{cfa}
     214what is the type of @cursor@?
     215If it the tag type (@int@), how is this value used?
     216If it is the variant type, where is the instance variable, which only contains one value.
     217Hence, either enumerating with a variant enumeration is disallowed or some unusual typing rule must be invented to make it work but only in restricted contexts.
     218
     219While functional programming systems regularly re-purposing variant types into enumeration types, this process seems contrived and confusing.
     220A variant tag is not an enumeration, it is a discriminant among a restricted set of types stored in a storage block.
     221Hence, there is only a weak equivalence between an enumeration and variant type, justifying a seperate enumeration type in a programming language.
    161222
    162223
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    rc148966 r4da9142  
    5353\lstnewenvironment{ada}[1][]{\lstset{language=[2005]Ada,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},literate={'}{\ttfamily'\!}1}\lstset{#1}}{}
    5454
    55 An Ada enumeration type is an ordered list of constants, called \Newterm{literals} (enumerators).
     55An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \Newterm{literals}.\footnote{%
     56Ada is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).}
    5657\begin{ada}
    57 type RGB is ( Red, Green, Blue ); -- 3 literals (enumerators)
     58type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators)
    5859\end{ada}
    5960Object initialization and assignment are restricted to the enumerators of this type.
    60 Enumerators without an explicitly designated constant value are auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by 1.
    61 To explicitly set enumerator values, \emph{all} enumerators must be set in \emph{ascending} order, \ie there is no auto-initialization.
     61While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
    6262\begin{ada}
    63 type RGB is ( Red, Green, Blue );
    64 @for RGB use ( Red => 10, Green => 20, Blue => 30 );@ -- ascending order
    65 \end{ada}
    66 Hence, the position, value, label tuples are:
    67 \begin{ada}
    68 (0, 10, RED)  (1, 20, GREEN)  (2, 30, BLUE)
    69 \end{ada}
    70 Note, Ada is case-\emph{insensitive} so names may appear in multiple forms and still be the same, \eg @Red@ and @RED@ (a questionable design decision).
    71 
    72 Like C, Ada enumerators are unscoped, \ie enumerators declared inside of an enum are visible (projected) into the enclosing scope.
    73 The enumeration operators are the ordering operators, @=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of enumerators, which is always ascending.
    74 
    75 Ada enumerators are overloadable.
    76 \begin{ada}
     63type RGB is ( @Red@, @Green@, Blue );
    7764type Traffic_Light is ( @Red@, Yellow, @Green@ );
    7865\end{ada}
    79 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names.
     66Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded identifiers.
    8067\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
    8168
     
    10289\end{figure}
    10390
    104 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package names.
     91Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
     92Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
     93\begin{ada}
     94type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
     95for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
     96\end{ada}
     97The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.
     98
     99Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
    105100\begin{ada}
    106101OtherRed : RGB renames Red;
     
    113108There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@,
    114109\begin{cquote}
    115 \lstDeleteShortInline@
    116110\setlength{\tabcolsep}{15pt}
    117111\begin{tabular}{@{}ll@{}}
     
    128122\end{ada}
    129123\end{tabular}
    130 \lstMakeShortInline@
    131124\end{cquote}
    132125These attributes are important for IO.
     
    138131\end{ada}
    139132which is syntactic sugar for the label and not character literals from the predefined type @Character@.
    140 The purpose is strictly readability using character literals rather than names.
     133The purpose is strictly readability using character literals rather than identifiers.
    141134\begin{ada}
    142135Op : Operator := '+';
     
    171164An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a default) or iterating constructs.
    172165\begin{cquote}
    173 \lstDeleteShortInline@
    174166\setlength{\tabcolsep}{15pt}
    175167\begin{tabular}{@{}ll@{}}
     
    211203\end{ada}
    212204\end{tabular}
    213 \lstMakeShortInline@
    214205\end{cquote}
    215206
     
    229220\CC has the equivalent of Pascal typed @const@ declarations \see{\VRef{s:Pascal}}, with static and dynamic initialization.
    230221\begin{c++}
    231 const auto one = 0 + 1;                                 $\C{// static intialization}$
     222const auto one = 0 + 1;                                 $\C{// static initialization}$
    232223const auto NULL = nullptr;
    233224const auto PI = 3.14159;
     
    237228                                Sat = Fri + 1, Sun = Sat + 1;
    238229int sa[Sun];
    239 const auto r = random();                                $\C{// dynamic intialization}$
     230const auto r = random();                                $\C{// dynamic initialization}$
    240231int da[r];                                                              $\C{// VLA}$
    241232\end{c++}
     
    362353\begin{figure}
    363354\centering
    364 \lstDeleteShortInline@
    365355\begin{tabular}{@{}l|l@{}}
    366356\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
     
    414404\end{csharp}
    415405\end{tabular}
    416 \lstMakeShortInline@
    417406\caption{\Csharp: Free Routine Versus Class Enumeration}
    418407\label{CsharpFreeVersusClass}
     
    429418const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
    430419\end{Go}
    431 Constant names are unscoped and must be unique (no overloading).
     420Constant identifiers are unscoped and must be unique (no overloading).
    432421The first enumerator \emph{must} be explicitly initialized;
    433422subsequent enumerators can be implicitly or explicitly initialized.
     
    459448Basic switch and looping are possible.
    460449\begin{cquote}
    461 \lstDeleteShortInline@
    462450\setlength{\tabcolsep}{15pt}
    463451\begin{tabular}{@{}ll@{}}
     
    482470\end{Go}
    483471\end{tabular}
    484 \lstMakeShortInline@
    485472\end{cquote}
    486473However, the loop prints the values from 0 to 13 because there is no actual enumeration.
     
    513500\begin{figure}
    514501\centering
    515 \lstDeleteShortInline@
    516502\begin{tabular}{@{}l|l@{}}
    517503\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
     
    553539\end{Java}
    554540\end{tabular}
    555 \lstMakeShortInline@
    556541\caption{Java: Free Routine Versus Class Enumeration}
    557542\label{f:JavaFreeVersusClass}
     
    607592\section{Rust}
    608593\lstnewenvironment{rust}[1][]{\lstset{language=Rust,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     594% https://doc.rust-lang.org/reference/items/enumerations.html
    609595
    610596Rust provides a scoped enumeration based on variant types.
     
    1010996
    1011997
    1012 \section{Python}
     998\section{Python 3.13}
    1013999\lstnewenvironment{python}[1][]{\lstset{language=Python,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    1014 
    1015 A Python enumeration is a set of symbolic names bound to \emph{unique} values.
    1016 They are similar to global variables, but offer a more useful @repr()@, grouping, type-safety, and additional features.
    1017 Enumerations inherits from the @Enum@ class, \eg:
    1018 \begin{python}
    1019 class Weekday(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    1020 class RGB(@Enum@): Red = 1; Green = 2; Blue = 3
    1021 \end{python}
    1022 
    1023 Depending on the nature of the enum a member's value may or may not be important, but either way that value can be used to get the corresponding member:
    1024 \begin{python}
    1025 print( repr( Weekday( 3 ) ) )
    1026 <Weekday.Wed: 3>
    1027 \end{python}
    1028 As you can see, the @repr()@ of a member shows the enum name, the member name, and the value.
    1029 The @str()@ of a member shows only the enum name and member name:
    1030 \begin{python}
    1031 print( str( Weekday.Thu ), Weekday.Thu )
    1032 Weekday.Thu Weekday.Thu
    1033 \end{python}
    1034 The type of an enumeration member is the enum it belongs to:
    1035 \begin{python}
    1036 print( type( Weekday.Thu ) )
    1037 <enum 'Weekday'>
    1038 print( isinstance(Weekday.Fri, Weekday) )
    1039 True
    1040 \end{python}
    1041 Enum members have an attribute that contains just their name:
    1042 \begin{python}
    1043 print(Weekday.TUESDAY.name)
    1044 TUESDAY
    1045 \end{python}
    1046 Likewise, they have an attribute for their value:
    1047 \begin{python}
    1048 Weekday.WEDNESDAY.value
    1049 3
    1050 \end{python}
    1051 
    1052 Unlike many languages that treat enumerations solely as name/value pairs, Python @Enum@s can have behavior added.
    1053 For example, @datetime.date@ has two methods for returning the weekday: @weekday()@ and @isoweekday()@.
    1054 The difference is that one of them counts from 0-6 and the other from 1-7.
    1055 Rather than keep track of that ourselves we can add a method to the @Weekday@ enum to extract the day from the date instance and return the matching enum member:
    1056 \begin{python}
    1057 class Weekday(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 15; Sat = 16; Sun = 17
    1058 $@$classmethod
    1059 def from_date(cls, date):
    1060         return cls(date.isoweekday())
    1061 \end{python}
    1062 Now we can find out what today is! Observe:
    1063 \begin{python}
    1064 >>> from datetime import date
    1065 >>> Weekday.from_date(date.today())     
    1066 <Weekday.TUESDAY: 2>
    1067 \end{python}
    1068 Of course, if you're reading this on some other day, you'll see that day instead.
    1069 
    1070 This Weekday enum is great if our variable only needs one day, but what if we need several? Maybe we're writing a function to plot chores during a week, and don't want to use a @list@ -- we could use a different type of @Enum@:
    1071 \begin{python}
    1072 from enum import Flag
    1073 class WeekdayF(@Flag@): Mon = @1@; Tue = @2@; Wed = @4@; Thu = @8@; Fri = @16@; Sat = @32@; Sun = @64@
    1074 \end{python}
    1075 We've changed two things: we're inherited from @Flag@, and the values are all powers of 2.
     1000% https://docs.python.org/3/howto/enum.html
     1001
     1002Python is a dynamically-typed reflexive programming language with multiple versions, and hence, it is possible to extend existing or build new language features within the language.
     1003As a result, discussing Python enumerations is a moving target, because if a features does not exist, if can often be created with varying levels of complexity.
     1004Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13.
     1005
     1006A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values.
     1007An enumeration is not a basic type;
     1008it is a @class@ inheriting from the @Enum@ class, where the enumerators must be explicitly initialized, \eg:
     1009\begin{python}
     1010class Week(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
     1011\end{python}
     1012and/or explicitly auto initialized, \eg:
     1013\begin{python}
     1014class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@
     1015\end{python}
     1016where @auto@ increments by 1 from the previous enumerator value.
     1017Object initialization and assignment are restricted to the enumerators of this type.
     1018An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee.
     1019\begin{python}
     1020class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @10@; Sat = @10@; Sun = @10@
     1021\end{python}
     1022Here, the enumeration has only 4 enumerators and 3 aliases.
     1023An alias is only visible by dropping down to the @class@ level and asking for class members.
     1024@Enum@ only supports equality comparison between enumerator values;
     1025the extended class @OrderedEnum@ adds relational operators @<@, @<=@, @>@, and @>=@.
     1026
     1027There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).
     1028\begin{cquote}
     1029\setlength{\tabcolsep}{15pt}
     1030\begin{tabular}{@{}ll@{}}
     1031\begin{python}
     1032Week.Thu.value == 10;
     1033Week.Thu.name == 'Thu';
     1034\end{python}
     1035&
     1036\begin{python}
     1037Week( 10 ) == Thu
     1038Week['Thu'].value = 10
     1039\end{python}
     1040\end{tabular}
     1041\end{cquote}
     1042
     1043As an enumeration is a \lstinline[language=python]{class}, its own methods.
     1044\begin{python}
     1045class Week(Enum):
     1046        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
     1047        $\\@$classmethod
     1048        def today(cls, date):
     1049                return cls(date.isoweekday())
     1050print( "today:", Week.today(date.today()))
     1051today: Week.Mon
     1052\end{python}
     1053The method @today@ retrieves the day of the week and uses it as an index to print out the corresponding label of @Week@.
    10761054
    10771055@Flag@ allows combining several members into a single variable:
    10781056\begin{python}
    1079 print( repr(WeekdayF.Sat | WeekdayF.Sun) )
    1080 <WeekdayF.Sun|Sat: 96>
     1057print( repr(WeekF.Sat | WeekF.Sun) )
     1058<WeekF.Sun|Sat: 96>
    10811059\end{python}
    10821060You can even iterate over a @Flag@ variable:
     
    10841062for day in weekend:
    10851063        print(day)
    1086 Weekday.SATURDAY
    1087 Weekday.SUNDAY
     1064WeekF.Sat
     1065WeekF.Sun
    10881066\end{python}
    10891067Okay, let's get some chores set up:
    10901068\begin{python}
    10911069>>> chores_for_ethan = {
    1092 ...    'feed the cat': Weekday.MONDAY | Weekday.WEDNESDAY | Weekday.FRIDAY,
    1093 ...    'do the dishes': Weekday.TUESDAY | Weekday.THURSDAY,
    1094 ...    'answer SO questions': Weekday.SATURDAY,
     1070...    'feed the cat': Week.MONDAY | Week.WEDNESDAY | Week.FRIDAY,
     1071...    'do the dishes': Week.TUESDAY | Week.THURSDAY,
     1072...    'answer SO questions': Week.SATURDAY,
    10951073...    }
    10961074\end{python}
     
    11011079...        if day in days:
    11021080...            print(chore)
    1103 >>> show_chores(chores_for_ethan, Weekday.SATURDAY)
     1081>>> show_chores(chores_for_ethan, Week.SATURDAY)
    11041082answer SO questions
    11051083\end{python}
    1106 In cases where the actual values of the members do not matter, you can save yourself some work and use @auto()@ for the values:
    1107 \begin{python}
    1108 >>> from enum import auto
    1109 >>> class Weekday(Flag):
    1110 ...    MONDAY = auto()
    1111 ...    TUESDAY = auto()
    1112 ...    WEDNESDAY = auto()
    1113 ...    THURSDAY = auto()
    1114 ...    FRIDAY = auto()
    1115 ...    SATURDAY = auto()
    1116 ...    SUNDAY = auto()
    1117 ...    WEEKEND = SATURDAY | SUNDAY
     1084Auto incrmenet for @Flag@ is by powers of 2.
     1085\begin{python}
     1086class WeekF(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto();  \
     1087                                                        Sat = auto(); Sun = auto(); Weekend = Sat | Sun
     1088for d in WeekF:
     1089        print( f"{d.name}: {d.value}", end=" ")
     1090Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 WeekA.Weekend
    11181091\end{python}
    11191092
     
    11231096@Enum@ allows such access:
    11241097\begin{python}
    1125 >>> Color(1)
    1126 <Color.RED: 1>
    1127 >>> Color(3)
    1128 <Color.BLUE: 3>
     1098print(RGB(1), RGB(3), )
     1099RGB.RED RGB.GREEN
    11291100\end{python}
    11301101If you want to access enum members by name, use item access:
    11311102\begin{python}
    1132 Color['RED']
    1133 <Color.RED: 1>
    1134 
    1135 Color['GREEN']
    1136 <Color.GREEN: 2>
     1103print( RGBa['RED'], RGBa['GREEN'] )
     1104RGB.RED RGB.GREEN
    11371105\end{python}
    11381106If you have an enum member and need its name or value:
    11391107\begin{python}
    1140 >>> member = Color.RED
    1141 >>> member.name
    1142 'RED'
    1143 >>> member.value
    1144 1
    1145 \end{python}
    1146 
    1147 \subsection{Duplicating enum members and values}
    1148 
    1149 An enum member can have other names associated with it.
    1150 Given two entries @A@ and @B@ with the same value (and @A@ defined first), @B@ is an alias for the member @A@.
    1151 By-value lookup of the value of @A@ will return the member @A@.
    1152 By-name lookup of @A@ will return the member @A@.
    1153 By-name lookup of @B@ will also return the member @A@:
    1154 \begin{python}
    1155 class Shape(Enum): SQUARE = 2; DIAMOND = 1; CIRCLE = 3; ALIAS_FOR_SQUARE = 2
    1156 >>> Shape.SQUARE
    1157 <Shape.SQUARE: 2>
    1158 >>> Shape.ALIAS_FOR_SQUARE
    1159 <Shape.SQUARE: 2>
    1160 >>> Shape(2)
    1161 <Shape.SQUARE: 2>
    1162 \end{python}
    1163 
    1164 Note: Attempting to create a member with the same name as an already defined attribute (another member, a method, etc.) or attempting to create an attribute with the same name as a member is not allowed.
     1108member = RGBa.RED
     1109print( f"{member.name} {member.value}" )
     1110RED 1
     1111\end{python}
     1112
    11651113
    11661114\subsection{Ensuring unique enumeration values}
     
    12071155>>> list(Shape)
    12081156[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
    1209 >>> list(Weekday)
    1210 [<Weekday.MONDAY: 1>, <Weekday.TUESDAY: 2>, <Weekday.WEDNESDAY: 4>, <Weekday.THURSDAY: 8>,
    1211 <Weekday.FRIDAY: 16>, <Weekday.SATURDAY: 32>, <Weekday.SUNDAY: 64>]
    1212 \end{python}
    1213 Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Weekday.WEEKEND@ aren't shown.
     1157>>> list(Week)
     1158[<Week.MONDAY: 1>, <Week.TUESDAY: 2>, <Week.WEDNESDAY: 4>, <Week.THURSDAY: 8>,
     1159<Week.FRIDAY: 16>, <Week.SATURDAY: 32>, <Week.SUNDAY: 64>]
     1160\end{python}
     1161Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week.WEEKEND@ aren't shown.
    12141162
    12151163The special attribute @__members__@ is a read-only ordered mapping of names to members.
     
    22182166
    22192167OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
    2220 The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, pure enumeration.
    2221 
    2222 (I think the value of a ocaml variants are types not object, so I am not sure about this line)
     2168The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
     2169
    22232170OCaml provides a variant (union) type, which is an aggregation of heterogeneous types.
    2224 A basic variant is a list of nullary datatype constructors, which is like an unscoped, pure enumeration.
     2171A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
    22252172\begin{ocaml}
    22262173type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     
    22462193type colour = Red | Green of @string@ | Blue of @int * float@
    22472194\end{ocaml}
    2248 A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block. 
     2195A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
    22492196@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
    22502197(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
     
    22592206@Red, abc, 1 1.5@
    22602207\end{ocaml}
    2261 
    22622208
    22632209A variant type can have a recursive definition.
     
    22802226
    22812227In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty.
    2282 Therefore it is not an enumeration, except for the simple pure (nullary) case.
     2228Therefore it is not an enumeration, except for the simple opaque (nullary) case.
    22832229
    22842230\begin{comment}
     
    24662412With valediction,
    24672413  - Gregor Richards
     2414
     2415
     2416Date: Tue, 16 Apr 2024 11:04:51 -0400
     2417Subject: Re: C unnamed enumeration
     2418To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     2419CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
     2420        <f37yu@uwaterloo.ca>
     2421From: Gregor Richards <gregor.richards@uwaterloo.ca>
     2422
     2423On 4/16/24 09:55, Peter A. Buhr wrote:
     2424> So what is a variant? Is it a set of tag names, which might be a union or is it
     2425> a union, which might have tag names?
     2426
     2427Your tagless variant bears no resemblance to variants in any functional
     2428programming language. A variant is a tag AND a union. You might not need to put
     2429anything in the union, in which case it's a pointless union, but the named tag
     2430is absolutely mandatory. That's the thing that varies.
     2431
     2432I was unaware of std::variant. As far as functional languages are concerned,
     2433std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for
     2434the functional language concept, because that term has no other meanings.
     2435
     2436An ADT cannot not have a named tag. That's meaningless. The tag is the data
     2437constructor, which is the thing you actually define when you define an ADT. It
     2438is strictly the union that's optional.
     2439
     2440With valediction,
     2441  - Gregor Richards
    24682442\end{comment}
    24692443
     
    24872461\hline
    24882462\hline
    2489 pure                    &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
     2463opaque                  &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
    24902464\hline
    24912465typed                   &               &               &               &               &               &               &               &               &               &               & @int@ & integral      & @T@   \\
Note: See TracChangeset for help on using the changeset viewer.