Changeset 7042c60 for doc/theses


Ignore:
Timestamp:
Apr 25, 2024, 3:48:17 PM (21 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
eb7586e
Parents:
cf191ac (diff), 55c97e4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

resolve conflict

Location:
doc/theses/jiada_liang_MMath
Files:
7 edited

Legend:

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

    rcf191ac r7042c60  
    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/Makefile

    rcf191ac r7042c60  
    1313BibSRC = ${wildcard *.bib}
    1414
    15 TeXLIB = .:${LaTMac}:${Build}:
    16 BibLIB = .:${BibRep}:
     15TeXLIB = .:${LaTMac}:${Build}:                  # common latex macros
     16BibLIB = .:${BibRep}:                           # common citation repository
    1717
    1818MAKEFLAGS = --no-print-directory # --silent
  • doc/theses/jiada_liang_MMath/background.tex

    rcf191ac r7042c60  
    11\chapter{Background}
    2 \lstnewenvironment{clang}[1][]{\lstset{language=[ANSI]C,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    32
    43\CFA is a backwards-compatible extension of the C programming language.
     
    4847
    4948\section{C Enumeration}
     49\label{s:CEnumeration}
    5050
    51 The C enumeration has the following syntax and semantics.
     51The C enumeration has the following syntax~\cite[\S~6.7.2.2]{C11}.
     52\begin{clang}[identifierstyle=\linespread{0.9}\it]
     53$\it enum$-specifier:
     54        enum identifier$\(_{opt}\)$ { enumerator-list }
     55        enum identifier$\(_{opt}\)$ { enumerator-list , }
     56        enum identifier
     57enumerator-list:
     58        enumerator
     59        enumerator-list , enumerator
     60enumerator:
     61        enumeration-constant
     62        enumeration-constant = constant-expression
     63\end{clang}
     64The terms \emph{enumeration} and \emph{enumerator} used in this work \see{\VRef{s:Terminology}} come from the grammar.
     65The C enumeration semantics is discussed using examples.
     66
     67An unnamed enumeration is used to provide secondary renaming, like a @const@ declaration in other languages.
     68\begin{clang}
     69enum { Size = 20, Pi = 3.14159 };   // unnamed enumeration $\(\Rightarrow\)$ no ordering
     70\end{clang}
     71This declaration form is not an enumeration even though it is declared using an @enum@ because it has none of the following enumeration properties.
     72
     73A \emph{named} enumeration type is an actual enumeration.
    5274\begin{clang}
    5375enum Weekday { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun, };
  • doc/theses/jiada_liang_MMath/intro.tex

    rcf191ac r7042c60  
    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.
    6 In theory, there are an infinite set of primary names per type.
    7 
    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.
     7In theory, there are an infinite set of primary constant names per type.
     8
     9\Newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (megabyte, 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 purpose is for readability and to eliminate duplication of the primary constant throughout a program.
     12For example, a meaningful secondary name replaces a primary name throughout a program;
     13thereafter, changing the binding of the secondary to primary name automatically distributes the rebinding, preventing errors.
     14In 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@.
    1215Because 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{
    1316The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}.
    1417
    15 Secondary names can form an (ordered) set, \eg days of the week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc.
     18Secondary names can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc.
    1619Many programming languages capture these groupings through a mechanism called an \Newterm{enumeration}.
    1720\begin{quote}
    1821enumerate (verb, transitive).
    1922To 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}
     23more usually, to mention (a number of things or persons) separately, as if for the purpose of counting;
     24to specify as in a list or catalogue.~\cite{OEDenumerate}
    2425\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.
     26Within 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.
    2627It is possible to enumerate among set names without having an ordering among the set elements.
    2728For example, the week, the weekdays, the weekend, and every second day of the week.
     
    2930for ( cursor in Mon, Tue, Wed, Thu, Fri, Sat, Sun } ... $\C[3.75in]{// week}$
    3031for ( cursor in Mon, Tue, Wed, Thu, Fri } ...   $\C{// weekday}$
    31 for ( cursor in Thu, Fri } ...                                  $\C{// weekend}$
     32for ( cursor in Sat, Sun } ...                                  $\C{// weekend}$
    3233for ( cursor in Mon, Wed, Fri, Sun } ...                $\C{// every second day of week}\CRT$
    3334\end{cfa}
    34 This independence from internal representation allows multiple names to have the same representation (eight note, quaver), giving synonyms.
     35This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms.
    3536A set can have a partial or total ordering, making it possible to compare set elements, \eg Monday is before Friday and Friday is after.
    36 Ordering allows iterating among the enumeration set using relational operators and advancement, \eg
     37Ordering allows iterating among the enumeration set using relational operators and advancement, \eg:
    3738\begin{cfa}
    3839for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ...
    3940\end{cfa}
    40 Here the internal representations for the secondary names are \emph{generated} rather than listing a subset of names.
     41Here the internal representation for the secondary names are logically \emph{generated} rather than listing a subset of names.
     42
     43Hence, the fundamental aspects of an enumeration are:
     44\begin{enumerate}
     45\item
     46\begin{sloppypar}
     47It provides a finite set of secondary names, which become its primary constants.
     48This differentiates an enumeration from general types with an infinite set
     49of primary constants.
     50\end{sloppypar}
     51\item
     52The secondary names are constants, which follows transitively from their binding (aliasing) to primary names, which are constants.
     53\item
     54Defines a type for generating instants (variables).
     55\item
     56For safety, an enumeration instance should be restricted to hold only its type's secondary names.
     57\item
     58There is a mechanism for \emph{enumerating} over the secondary names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.
     59\end{enumerate}
    4160
    4261
    4362\section{Terminology}
    44 
    45 The term \Newterm{enumeration} defines the set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name.
    46 As well, an enumerated type has three fundamental properties, \Newterm{label}, \Newterm{order}, and \Newterm{value}.
     63\label{s:Terminology}
     64
     65The 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} for the name derivation}.
     66As well, an enumerated type can have three fundamental properties, \Newterm{label}, \Newterm{order}, and \Newterm{value}.
    4767\begin{cquote}
    4868\sf\setlength{\tabcolsep}{3pt}
    4969\begin{tabular}{rcccccccr}
    5070\it\color{red}enumeration       & \multicolumn{8}{c}{\it\color{red}enumerators} \\
    51 $\downarrow$\hspace*{25pt}      & \multicolumn{8}{c}{$\downarrow$}                              \\
    52 @enum@ Week \{                          & Mon,  & Tue,  & Wed,  & Thu,  & Fri,  & Sat,  & Sun = 42      & \};   \\
     71$\downarrow$\hspace*{15pt}      & \multicolumn{8}{c}{$\downarrow$}                              \\
     72@enum@ Week \{                          & Mon,  & Tue,  & Wed,  & Thu,  & Fri,  & Sat,  & Sun {\color{red}= 42} & \};   \\
    5373\it\color{red}label                     & Mon   & Tue   & Wed   & Thu   & Fri   & Sat   & Sun           &               \\
    5474\it\color{red}order                     & 0             & 1             & 2             & 3             & 4             & 5             & 6                     &               \\
    55 \it\color{red}value                     & 0             & 1             & 2             & 3             & 4             & 5             & 42            &
     75\it\color{red}value                     & 0             & 1             & 2             & 3             & 4             & 5             & {\color{red}42}               &
    5676\end{tabular}
    5777\end{cquote}
     
    7292\section{Motivation}
    7393
    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.
     94Many programming languages provide an enumeration-like mechanism, which may or may not cover the previous five fundamental enumeration aspects.
     95Hence, the term \emph{enumeration} can be confusing and misunderstood.
     96Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which featuring is being used.
     97This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects.
     98
     99
     100\subsection{Aliasing}
     101
     102Some languages provide simple secondary aliasing (renaming), \eg:
     103\begin{cfa}
     104const Size = 20, Pi = 3.14159, Name = "Jane";
     105\end{cfa}
     106The secondary name is logically replaced in the program text by its corresponding primary name.
     107Therefore, it is possible to compare the secondary names, \eg @Size < Pi@, only because the primary constants allow it, whereas \eg @Pi < Name@ might be disallowed depending on the language.
     108
     109Aliasing is not macro substitution, \eg @#define Size 20@, where a name is replaced by its value \emph{before} compilation, so the name is invisible to the programming language.
     110With aliasing, each secondary name is part of the language, and hence, participates fully, such as name overloading in the type system.
     111Aliasing is not an immutable variable, \eg:
     112\begin{cfa}
     113extern @const@ int Size = 20;
     114extern void foo( @const@ int @&@ size );
     115foo( Size ); // take the address of (reference) Size
     116\end{cfa}
     117Taking the address of an immutable variable makes it an \Newterm{lvalue}, which implies it has storage.
     118With separate compilation, it is necessary to choose one translation unit to perform the initialization.
     119If aliasing does require storage, its address and initialization are opaque (compiler only), similar to \CC rvalue reference @&&@.
     120
     121Aliasing does provide readability and automatic resubstitution.
     122It also provides simple enumeration properties, but with extra effort.
    83123\begin{cfa}
    84124const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7;
    85125\end{cfa}
    86 Furthermore, reordering the enumerators requires manual renumbering.
     126Any reordering of the enumerators requires manual renumbering.
    87127\begin{cfa}
    88128const Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7;
    89129\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}
    96 @variant@ Variant {
    97         @int tag;@  // optional/implicit: 0 => int, 1 => double, 2 => S
    98         @union {@ // implicit
    99                 case int i;
    100                 case double d;
    101                 case struct S { int i, j; } s;
    102         @};@
    103 };
    104 \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:
    113 \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 */ }
    119 }
    120 \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
    128         ...
    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}
    138 While enumerating among tag names is possible:
    139 \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.
     130For these reasons, aliasing is sometimes called an enumeration.
     131However, there is no type to create a type-checked instance or iterator cursor, so there is no ability for enumerating.
     132Hence, there are multiple enumeration aspects not provided by aliasing, justifying a separate enumeration type in a programming language.
     133
     134
     135\subsection{Algebraic Data Type}
     136
     137An algebraic data type (ADT)\footnote{ADT is overloaded with abstract data type.} is another language feature often linked with enumeration, where an ADT conjoins an arbitrary type, possibly a \lstinline[language=C++]{class} or @union@, and a named constructor.
     138For example, in Haskell:
     139\begin{haskell}
     140data S = S { i::Int, d::Double }                $\C{// structure}$
     141data @Foo@ = A Int | B Double | C S             $\C{// ADT, composed of three types}$
     142foo = A 3;                                                              $\C{// type Foo is inferred}$
     143bar = B 3.5
     144baz = C S{ i = 7, d = 7.5 }
     145\end{haskell}
     146the ADT has three variants (constructors), @A@, @B@, @C@ with associated types @Int@, @Double@, and @S@.
     147The constructors create an initialized value of the specific type that is bound to the immutable variables @foo@, @bar@, and @baz@.
     148Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to access the value using dynamic pattern-matching.
     149\begin{cquote}
     150\setlength{\tabcolsep}{15pt}
     151\begin{tabular}{@{}ll@{}}
     152\begin{haskell}
     153prtfoo val = -- function
     154    -- pattern match on constructor
     155    case val of
     156      @A@ a -> print a
     157      @B@ b -> print b
     158      @C@ (S i d) -> do
     159          print i
     160          print d
     161\end{haskell}
     162&
     163\begin{haskell}
     164main = do
     165    prtfoo foo
     166    prtfoo bar
     167    prtfoo baz
     1683
     1693.5
     1707
     1717.5
     172\end{haskell}
     173\end{tabular}
     174\end{cquote}
     175For safety, most languages require all assocaited types to be listed or a default case with no field accesses.
     176
     177A less frequent case is multiple constructors with the same type.
     178\begin{haskell}
     179data Bar = X Int | Y Int | Z Int;
     180foo = X 3;
     181bar = Y 3;
     182baz = Z 5;
     183\end{haskell}
     184Here, the constructor name gives different meaning to the values in the common \lstinline[language=Haskell]{Int} type, \eg the value @3@ has different interpretations depending on the constructor name in the pattern matching.
     185
     186Note, the term \Newterm{variant} is often associated with ADTs.
     187However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}.
     188In these languages, the variant is often a union using RTTI tags, which cannot be used to simulate an enumeration.
     189Hence, in this work the term variant is not a synonym for ADT.
     190
     191% https://downloads.haskell.org/ghc/latest/docs/libraries/base-4.19.1.0-179c/GHC-Enum.html
     192% https://hackage.haskell.org/package/base-4.19.1.0/docs/GHC-Enum.html
     193
     194The association between ADT and enumeration occurs if all the constructors have a unit (empty) type, \eg @struct unit {}@.
     195Note, the unit type is not the same as \lstinline{void}, \eg:
     196\begin{cfa}
     197void foo( void );
     198struct unit {} u;  // empty type
     199unit bar( unit );
     200foo( foo() );        // void argument does not match with void parameter
     201bar( bar( u ) );   // unit argument does match with unit parameter
     202\end{cfa}
     203
     204For example, in the Haskell ADT:
     205\begin{haskell}
     206data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show)
     207\end{haskell}
     208the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other type, @Eq@ allows equality comparison, and @Show@ is for printing.
     209The nullary constructors for the unit types are numbered left-to-right from $0$ to @maxBound@$- 1$, and provides enumerating operations @succ@, @pred@, @enumFrom@ @enumFromTo@.
     210\VRef[Figure]{f:HaskellEnumeration} shows enumeration comparison and iterating (enumerating).
     211
     212\begin{figure}
     213\begin{cquote}
     214\setlength{\tabcolsep}{15pt}
     215\begin{tabular}{@{}ll@{}}
     216\begin{haskell}
     217day = Tue
     218main = do
     219    if day == Tue then
     220        print day
     221    else
     222        putStr "not Tue"
     223    print (enumFrom Mon)            -- week
     224    print (enumFromTo Mon Fri)   -- weekday
     225    print (enumFromTo Sat Sun)  -- weekend
     226\end{haskell}
     227&
     228\begin{haskell}
     229Tue
     230[Mon,Tue,Wed,Thu,Fri,Sat,Sun]
     231[Mon,Tue,Wed,Thu,Fri]
     232[Sat,Sun]
     233
     234
     235
     236
     237
     238\end{haskell}
     239\end{tabular}
     240\end{cquote}
     241\caption{Haskell Enumeration}
     242\label{f:HaskellEnumeration}
     243\end{figure}
     244
     245The key observation is the dichotomy between an ADT and enumeration: the ADT uses the associated type resulting in a union-like data structure, and the enumeration does not use the associated type, and hence, is not a union.
     246While the enumeration is constructed using the ADT mechanism, it is so restricted it is not really an ADT.
     247Furthermore, a general ADT cannot be an enumeration because the constructors generate different values making enumerating meaningless.
     248While functional programming languages regularly repurpose the ADT type into an enumeration type, this process seems contrived and confusing.
     249Hence, there is only a weak equivalence between an enumeration and ADT, justifying a separate enumeration type in a programming language.
    161250
    162251
    163252\section{Contributions}
    164253
    165 The goal of this work is to to extend the simple and unsafe enumeration type in the C programming-language into a sophisticated and safe type in the \CFA programming-language, while maintain backwards compatibility with C.
     254The goal of this work is to to extend the simple and unsafe enumeration type in the C programming-language into a complex and safe enumeration type in the \CFA programming-language, while maintaining backwards compatibility with C.
    166255On the surface, enumerations seem like a simple type.
    167256However, when extended with advanced features, enumerations become complex for both the type system and the runtime implementation.
    168257
     258The contribution of this work are:
    169259\begin{enumerate}
    170260\item
     
    175265typing
    176266\item
    177 subset
     267subseting
    178268\item
    179269inheritance
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    rcf191ac r7042c60  
    2323\section{Pascal}
    2424\label{s:Pascal}
    25 \lstnewenvironment{pascal}[1][]{\lstset{language=pascal,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    2625
    2726Classic Pascal has the \lstinline[language=pascal]{const} declaration binding a name to a constant literal/expression.
     
    5150
    5251\section{Ada}
    53 \lstnewenvironment{ada}[1][]{\lstset{language=[2005]Ada,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},literate={'}{\ttfamily'\!}1}\lstset{#1}}{}
    54 
    55 An Ada enumeration type is an ordered list of constants, called \Newterm{literals} (enumerators).
     52
     53An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \Newterm{literals}.\footnote{%
     54Ada 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).}
    5655\begin{ada}
    57 type RGB is ( Red, Green, Blue ); -- 3 literals (enumerators)
     56type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators)
    5857\end{ada}
    5958Object 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.
     59While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
    6260\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}
     61type RGB is ( @Red@, @Green@, Blue );
    7762type Traffic_Light is ( @Red@, Yellow, @Green@ );
    7863\end{ada}
    79 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names.
     64Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded identifiers.
    8065\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
    8166
     
    10287\end{figure}
    10388
    104 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package names.
     89Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
     90Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
     91\begin{ada}
     92type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
     93for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
     94\end{ada}
     95The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.
     96
     97Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
    10598\begin{ada}
    10699OtherRed : RGB renames Red;
     
    113106There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@,
    114107\begin{cquote}
    115 \lstDeleteShortInline@
    116108\setlength{\tabcolsep}{15pt}
    117109\begin{tabular}{@{}ll@{}}
     
    128120\end{ada}
    129121\end{tabular}
    130 \lstMakeShortInline@
    131122\end{cquote}
    132123These attributes are important for IO.
     
    138129\end{ada}
    139130which 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.
     131The purpose is strictly readability using character literals rather than identifiers.
    141132\begin{ada}
    142133Op : Operator := '+';
     
    171162An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a default) or iterating constructs.
    172163\begin{cquote}
    173 \lstDeleteShortInline@
    174164\setlength{\tabcolsep}{15pt}
    175165\begin{tabular}{@{}ll@{}}
     
    211201\end{ada}
    212202\end{tabular}
    213 \lstMakeShortInline@
    214203\end{cquote}
    215204
     
    225214\section{\CC}
    226215\label{s:C++RelatedWork}
    227 \lstnewenvironment{c++}[1][]{\lstset{language=[GNU]C++,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    228216
    229217\CC has the equivalent of Pascal typed @const@ declarations \see{\VRef{s:Pascal}}, with static and dynamic initialization.
    230218\begin{c++}
    231 const auto one = 0 + 1;                                 $\C{// static intialization}$
     219const auto one = 0 + 1;                                 $\C{// static initialization}$
    232220const auto NULL = nullptr;
    233221const auto PI = 3.14159;
     
    237225                                Sat = Fri + 1, Sun = Sat + 1;
    238226int sa[Sun];
    239 const auto r = random();                                $\C{// dynamic intialization}$
     227const auto r = random();                                $\C{// dynamic initialization}$
    240228int da[r];                                                              $\C{// VLA}$
    241229\end{c++}
     
    319307\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
    320308\label{s:Csharp}
    321 \lstnewenvironment{csharp}[1][]{\lstset{language=[Sharp]C,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    322309
    323310% https://www.tutorialsteacher.com/codeeditor?cid=cs-mk8Ojx
     
    362349\begin{figure}
    363350\centering
    364 \lstDeleteShortInline@
    365351\begin{tabular}{@{}l|l@{}}
    366352\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
     
    414400\end{csharp}
    415401\end{tabular}
    416 \lstMakeShortInline@
    417402\caption{\Csharp: Free Routine Versus Class Enumeration}
    418403\label{CsharpFreeVersusClass}
     
    421406
    422407\section{Golang}
    423 \lstnewenvironment{Go}[1][]{\lstset{language=Go,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    424408
    425409Golang provides pseudo-enumeration similar to classic Pascal \lstinline[language=pascal]{const}, binding a name to a constant literal/expression.
     
    429413const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
    430414\end{Go}
    431 Constant names are unscoped and must be unique (no overloading).
     415Constant identifiers are unscoped and must be unique (no overloading).
    432416The first enumerator \emph{must} be explicitly initialized;
    433417subsequent enumerators can be implicitly or explicitly initialized.
     
    459443Basic switch and looping are possible.
    460444\begin{cquote}
    461 \lstDeleteShortInline@
    462445\setlength{\tabcolsep}{15pt}
    463446\begin{tabular}{@{}ll@{}}
     
    482465\end{Go}
    483466\end{tabular}
    484 \lstMakeShortInline@
    485467\end{cquote}
    486468However, the loop prints the values from 0 to 13 because there is no actual enumeration.
     
    488470
    489471\section{Java}
    490 \lstnewenvironment{Java}[1][]{\lstset{language=Java,morekeywords={enum,assert,strictfp},
    491         escapechar=\$,moredelim=**[is][\color{red}]{!}{!},}\lstset{#1}}{}
    492472
    493473Every enumeration in Java is an enumeration class.
     
    513493\begin{figure}
    514494\centering
    515 \lstDeleteShortInline@
    516495\begin{tabular}{@{}l|l@{}}
    517496\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
     
    553532\end{Java}
    554533\end{tabular}
    555 \lstMakeShortInline@
    556534\caption{Java: Free Routine Versus Class Enumeration}
    557535\label{f:JavaFreeVersusClass}
     
    606584
    607585\section{Rust}
    608 \lstnewenvironment{rust}[1][]{\lstset{language=Rust,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     586% https://doc.rust-lang.org/reference/items/enumerations.html
    609587
    610588Rust provides a scoped enumeration based on variant types.
     
    652630
    653631\section{Swift}
    654 \lstnewenvironment{swift}[1][]{\lstset{language=Swift,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    655632
    656633% https://www.programiz.com/swift/online-compiler
     
    1010987
    1011988
    1012 \section{Python}
    1013 \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.
     989\section{Python 3.13}
     990% https://docs.python.org/3/howto/enum.html
     991
     992Python 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.
     993As 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.
     994Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13.
     995
     996A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values.
     997An enumeration is not a basic type;
     998it is a @class@ inheriting from the @Enum@ class, where the enumerators must be explicitly initialized, \eg:
     999\begin{python}
     1000class Week(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
     1001\end{python}
     1002and/or explicitly auto initialized, \eg:
     1003\begin{python}
     1004class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@
     1005\end{python}
     1006where @auto@ increments by 1 from the previous enumerator value.
     1007Object initialization and assignment are restricted to the enumerators of this type.
     1008An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee.
     1009\begin{python}
     1010class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @10@; Sat = @10@; Sun = @10@
     1011\end{python}
     1012Here, the enumeration has only 4 enumerators and 3 aliases.
     1013An alias is only visible by dropping down to the @class@ level and asking for class members.
     1014@Enum@ only supports equality comparison between enumerator values;
     1015the extended class @OrderedEnum@ adds relational operators @<@, @<=@, @>@, and @>=@.
     1016
     1017There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).
     1018\begin{cquote}
     1019\setlength{\tabcolsep}{15pt}
     1020\begin{tabular}{@{}ll@{}}
     1021\begin{python}
     1022Week.Thu.value == 10;
     1023Week.Thu.name == 'Thu';
     1024\end{python}
     1025&
     1026\begin{python}
     1027Week( 10 ) == Thu
     1028Week['Thu'].value = 10
     1029\end{python}
     1030\end{tabular}
     1031\end{cquote}
     1032
     1033As an enumeration is a \lstinline[language=python]{class}, its own methods.
     1034\begin{python}
     1035class Week(Enum):
     1036        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
     1037        $\\@$classmethod
     1038        def today(cls, date):
     1039                return cls(date.isoweekday())
     1040print( "today:", Week.today(date.today()))
     1041today: Week.Mon
     1042\end{python}
     1043The method @today@ retrieves the day of the week and uses it as an index to print out the corresponding label of @Week@.
    10761044
    10771045@Flag@ allows combining several members into a single variable:
    10781046\begin{python}
    1079 print( repr(WeekdayF.Sat | WeekdayF.Sun) )
    1080 <WeekdayF.Sun|Sat: 96>
     1047print( repr(WeekF.Sat | WeekF.Sun) )
     1048<WeekF.Sun|Sat: 96>
    10811049\end{python}
    10821050You can even iterate over a @Flag@ variable:
     
    10841052for day in weekend:
    10851053        print(day)
    1086 Weekday.SATURDAY
    1087 Weekday.SUNDAY
     1054WeekF.Sat
     1055WeekF.Sun
    10881056\end{python}
    10891057Okay, let's get some chores set up:
    10901058\begin{python}
    10911059>>> 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,
     1060...    'feed the cat': Week.MONDAY | Week.WEDNESDAY | Week.FRIDAY,
     1061...    'do the dishes': Week.TUESDAY | Week.THURSDAY,
     1062...    'answer SO questions': Week.SATURDAY,
    10951063...    }
    10961064\end{python}
     
    11011069...        if day in days:
    11021070...            print(chore)
    1103 >>> show_chores(chores_for_ethan, Weekday.SATURDAY)
     1071>>> show_chores(chores_for_ethan, Week.SATURDAY)
    11041072answer SO questions
    11051073\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
     1074Auto incrmenet for @Flag@ is by powers of 2.
     1075\begin{python}
     1076class WeekF(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto();  \
     1077                                                        Sat = auto(); Sun = auto(); Weekend = Sat | Sun
     1078for d in WeekF:
     1079        print( f"{d.name}: {d.value}", end=" ")
     1080Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 WeekA.Weekend
    11181081\end{python}
    11191082
     
    11231086@Enum@ allows such access:
    11241087\begin{python}
    1125 >>> Color(1)
    1126 <Color.RED: 1>
    1127 >>> Color(3)
    1128 <Color.BLUE: 3>
     1088print(RGB(1), RGB(3), )
     1089RGB.RED RGB.GREEN
    11291090\end{python}
    11301091If you want to access enum members by name, use item access:
    11311092\begin{python}
    1132 Color['RED']
    1133 <Color.RED: 1>
    1134 
    1135 Color['GREEN']
    1136 <Color.GREEN: 2>
     1093print( RGBa['RED'], RGBa['GREEN'] )
     1094RGB.RED RGB.GREEN
    11371095\end{python}
    11381096If you have an enum member and need its name or value:
    11391097\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.
     1098member = RGBa.RED
     1099print( f"{member.name} {member.value}" )
     1100RED 1
     1101\end{python}
     1102
    11651103
    11661104\subsection{Ensuring unique enumeration values}
     
    12071145>>> list(Shape)
    12081146[<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.
     1147>>> list(Week)
     1148[<Week.MONDAY: 1>, <Week.TUESDAY: 2>, <Week.WEDNESDAY: 4>, <Week.THURSDAY: 8>,
     1149<Week.FRIDAY: 16>, <Week.SATURDAY: 32>, <Week.SUNDAY: 64>]
     1150\end{python}
     1151Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week.WEEKEND@ aren't shown.
    12141152
    12151153The special attribute @__members__@ is a read-only ordered mapping of names to members.
     
    22122150
    22132151\section{OCaml}
    2214 \lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
    22152152
    22162153% https://ocaml.org/docs/basic-data-types#enumerated-data-types
     
    22182155
    22192156OCaml 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)
     2157The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
     2158
    22232159OCaml 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.
     2160A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
    22252161\begin{ocaml}
    22262162type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
     
    22462182type colour = Red | Green of @string@ | Blue of @int * float@
    22472183\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. 
     2184A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
    22492185@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
    22502186(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
     
    22592195@Red, abc, 1 1.5@
    22602196\end{ocaml}
    2261 
    22622197
    22632198A variant type can have a recursive definition.
     
    22802215
    22812216In 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.
     2217Therefore it is not an enumeration, except for the simple opaque (nullary) case.
    22832218
    22842219\begin{comment}
     
    24662401With valediction,
    24672402  - Gregor Richards
     2403
     2404
     2405Date: Tue, 16 Apr 2024 11:04:51 -0400
     2406Subject: Re: C unnamed enumeration
     2407To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
     2408CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
     2409        <f37yu@uwaterloo.ca>
     2410From: Gregor Richards <gregor.richards@uwaterloo.ca>
     2411
     2412On 4/16/24 09:55, Peter A. Buhr wrote:
     2413> So what is a variant? Is it a set of tag names, which might be a union or is it
     2414> a union, which might have tag names?
     2415
     2416Your tagless variant bears no resemblance to variants in any functional
     2417programming language. A variant is a tag AND a union. You might not need to put
     2418anything in the union, in which case it's a pointless union, but the named tag
     2419is absolutely mandatory. That's the thing that varies.
     2420
     2421I was unaware of std::variant. As far as functional languages are concerned,
     2422std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for
     2423the functional language concept, because that term has no other meanings.
     2424
     2425An ADT cannot not have a named tag. That's meaningless. The tag is the data
     2426constructor, which is the thing you actually define when you define an ADT. It
     2427is strictly the union that's optional.
     2428
     2429With valediction,
     2430  - Gregor Richards
    24682431\end{comment}
    24692432
     
    24872450\hline
    24882451\hline
    2489 pure                    &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
     2452opaque                  &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
    24902453\hline
    24912454typed                   &               &               &               &               &               &               &               &               &               &               & @int@ & integral      & @T@   \\
  • doc/theses/jiada_liang_MMath/uw-ethesis.bib

    rcf191ac r7042c60  
    22% For use with BibTeX
    33
     4Oxford English Dictionary, s.v. ``enumerate (v.), sense 3,'' September 2023, https://doi.org/10.1093/OED/1113960777.
     5@misc{OEDenumerate,
     6    keywords    = {enumerate},
     7    key         = {enumerate},
     8    title       = {enumerate (v.), sense 3},
     9    author      = {Oxford English Dictionary},
     10    howpublished= {\url{https://doi.org/10.1093/OED/1113960777}},
     11    month       = sep,
     12    year        = 2023,
     13}
  • doc/theses/jiada_liang_MMath/uw-ethesis.tex

    rcf191ac r7042c60  
    9595\CFAStyle                                               % CFA code-style
    9696\lstset{language=cfa,belowskip=-1pt} % set default language to CFA
     97\lstnewenvironment{ada}[1][]{\lstset{language=[2005]Ada,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},literate={'}{\ttfamily'\!}1}\lstset{#1}}{}
     98\lstnewenvironment{c++}[1][]{\lstset{language=[GNU]C++,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     99\lstnewenvironment{pascal}[1][]{\lstset{language=pascal,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     100\lstnewenvironment{csharp}[1][]{\lstset{language=[Sharp]C,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     101\lstnewenvironment{clang}[1][]{\lstset{language=[ANSI]C,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     102\lstnewenvironment{Go}[1][]{\lstset{language=Go,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     103\lstnewenvironment{haskell}[1][]{\lstset{language=Haskell,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     104\lstnewenvironment{Java}[1][]{\lstset{language=Java,morekeywords={enum,assert,strictfp},
     105        escapechar=\$,moredelim=**[is][\color{red}]{!}{!},}\lstset{#1}}{}
     106\lstnewenvironment{rust}[1][]{\lstset{language=Rust,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     107\lstnewenvironment{swift}[1][]{\lstset{language=Swift,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     108\lstnewenvironment{python}[1][]{\lstset{language=Python,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     109\lstnewenvironment{ocaml}[1][]{\lstset{language=OCaml,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{}
     110
     111\newsavebox{\myboxA}
     112\newsavebox{\myboxB}
    97113
    98114\newcommand{\newtermFont}{\emph}
Note: See TracChangeset for help on using the changeset viewer.