Changeset c1c0efdb


Ignore:
Timestamp:
Aug 8, 2024, 10:02:34 PM (5 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
7568e5c
Parents:
11cced6
git-author:
Peter A. Buhr <pabuhr@…> (08/08/24 22:01:56)
git-committer:
Peter A. Buhr <pabuhr@…> (08/08/24 22:02:34)
Message:

last proofread of thesis

Location:
doc/theses/jiada_liang_MMath
Files:
6 edited

Legend:

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

    r11cced6 rc1c0efdb  
    177177
    178178
    179 \section{Auto Initialization}
    180 
    181 A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.
    182 \begin{cfa}
    183 enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13
    184 \end{cfa}
    185 The complexity of the constant expression depends on the level of computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary.
    186 
    187 If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.
    188 \begin{cfa}
    189 enum E(T) { A, B, C };
    190 \end{cfa}
    191 \begin{enumerate}
    192 \item the first enumerator, @A@, is initialized with @T@'s @zero_t@.
    193 \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.
    194 \end{enumerate}
    195 
    196 Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.
    197 It is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler to evaluate complex expressions across multiple builtin and user-defined type.
    198 Nevertheless, the necessary language concepts exist to support this feature.
     179% \section{Auto Initialization}
     180%
     181% A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.
     182% \begin{cfa}
     183% enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13
     184% \end{cfa}
     185% The complexity of the constant expression depends on the level of computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary.
     186%
     187% If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.
     188% \begin{cfa}
     189% enum E(T) { A, B, C };
     190% \end{cfa}
     191% \begin{enumerate}
     192% \item the first enumerator, @A@, is initialized with @T@'s @zero_t@.
     193% \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.
     194% \end{enumerate}
     195%
     196% Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.
     197% It is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler to evaluate complex expressions across multiple builtin and user-defined type.
     198% Nevertheless, the necessary language concepts exist to support this feature.
    199199
    200200
     
    263263posn( e2 );                     $\C[1.75in]{// 0}$
    264264enum E3 e3 = e2;        $\C{// Assignment with enumeration conversion E2 to E3}$
    265 posn( e2 );                     $\C{// 1 }$
     265posn( e2 );                     $\C{// 1 cost}$
    266266void foo( E3 e );
    267267foo( e2 );                      $\C{// Type compatible with enumeration conversion E2 to E3}$
    268268posn( (E3)e2 );         $\C{// Explicit cast with enumeration conversion E2 to E3}$
    269269E3 e31 = B;                     $\C{// No conversion: E3.B}$
    270 posn( e31 );            $\C{// 0 }\CRT$
     270posn( e31 );            $\C{// 0 cost}\CRT$
    271271\end{cfa}
    272272The last expression is unambiguous.
    273 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ need not a conversion (@zero@ cost). \CFA selects the lowest cost candidate @E3.B@.
     273While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ needs no conversion (@zero@ cost).
     274\CFA selects the lowest cost candidate @E3.B@.
    274275
    275276For the given function prototypes, the following calls are valid.
     
    449450\footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.}
    450451This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added.
    451 
    452452The array subscript operator, namely @?[?]@, is overloaded so that when a \CFA enumerator is used as an array index, it implicitly converts to its position over value to sustain data harmonization.
    453453This behaviour can be reverted by explicit overloading:
     
    455455float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; }
    456456\end{cfa}
    457 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are  
    458 unambiguous within the context. Designators in H1 are unambiguous becasue @E2@ has a @value@ cost to @int@, which
    459 is more expensive than @safe@ cost from C-Enum @E1@ to @int@. On the hand, designators in @H2@ are resolved as @E2@ because
    460 when a \CFA enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's resolution context.
     457While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context.
     458Designators in @H1@ are unambiguous becasue @E2@ has a @value@ cost to @int@, which is more expensive than @safe@ cost from C-Enum @E1@ to @int@.
     459Designators in @H2@ are resolved as @E2@ because when a \CFA enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's resolution context.
     460
    461461
    462462\section{I/O}
     463
    463464As seen in multiple examples, \CFA enumerations can be printed and the default property printed is the enumerator's label, which is similar in other programming languages.
    464465However, very few programming languages provide a mechanism to read in enumerator values.
  • doc/theses/jiada_liang_MMath/Cenum.tex

    r11cced6 rc1c0efdb  
    1515enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
    1616\end{cfa}
    17 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system-include files.
     17There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include-files.
    1818
    1919The \CFA type-system allows extensive overloading, including enumerators.
    2020Hence, most ambiguities among C enumerators are implicitly resolved by the \CFA type system, possibly without any programmer knowledge of the conflict.
    21 In addition, C Enum qualification is added, exactly like aggregate field qualification, to disambiguate.
     21In addition, C Enum qualification is added, exactly like aggregate field-qualification, to disambiguate.
    2222\VRef[Figure]{f:EnumeratorVisibility} shows how resolution, qualification, and casting are used to disambiguate situations for enumerations @E1@ and @E2@.
    2323
     
    6868
    6969\section{Type Safety}
     70\label{s:TypeSafety}
    7071
    7172As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern.
     
    101102\item[How widely used:] Common.
    102103\end{description}
    103 
    104 \begin{comment}
    105 \begin{description}[parsep=0pt]
    106 \item[Change:] In \CC, the type of an enumerator is its enumeration.
    107 In C, the type of an enumerator is @int@.
    108 Example:
    109 \begin{cfa}
    110 enum e { A };
    111 sizeof(A) == sizeof(int)        $\C{// in C}$
    112 sizeof(A) == sizeof(e)          $\C{// in \CC}$
    113 /* and sizeof(int) is not necessary equal to sizeof(e) */
    114 \end{cfa}
    115 \item[Rationale:] In \CC, an enumeration is a distinct type.
    116 \item[Effect on original feature:] Change to semantics of well-defined feature.
    117 \item[Difficulty of converting:] Semantic transformation.
    118 \item[How widely used:] Seldom. The only time this affects existing C code is when the size of an enumerator is
    119 taken. Taking the size of an enumerator is not a common C coding practice.
    120 \end{description}
    121 \end{comment}
  • doc/theses/jiada_liang_MMath/conclusion.tex

    r11cced6 rc1c0efdb  
    1313Enumeration traits define the meaning of an enumeration, allowing functions to be written that work on any enumeration, such as the reading and printing an enumeration.
    1414Using advanced duck typing, existing C enumerations can be extended so they work with all of the enumeration features, providing for legacy C code to be moved forward into the modern \CFA programming domain.
    15 Finally, I expanded the \CFA project's test suite with multiple enumeration features tests with respect to implicit conversions, control structures, inheritance, interaction with the polymorphic types, and the features built on top of enumeration traits.
     15Finally, I expanded the \CFA project's test-suite with multiple enumeration features tests with respect to implicit conversions, control structures, inheritance, interaction with the polymorphic types, and the features built on top of enumeration traits.
    1616These tests ensure future \CFA work does not accidentally break the new enumeration system.
    1717
     
    2121\section{Future Work}
    2222
     23The following are ideas to improve and extend the work in this thesis.
     24\begin{enumerate}
     25\item
    2326There are still corner cases being found in the current \CFA enumeration implementation.
    24 Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA, like compile-time constant-expression evaluation.
     27Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA. %, like compile-time constant-expression evaluation.
    2528When these changes are made, it should be straightforward to update the \CFA enumeration implementation to work with them.
    26 
     29\item
    2730Currently, some aspects of the enumeration trait system require explicitly including the file @enum.hfa@, which can lead to problems.
    2831It should be possible to have this file included implicitly by updating the \CFA prelude.
    29 
     32\item
     33There are multiple \CFA features being developed i parallel with enumerations.
     34Two closely related features are iterator and namespace.
     35Enumerations may have to be modified to dovetail with these features.
     36For example, enumerating with range loops does not align with the current iterator design, so some changes will be necessary.
     37\item
    3038C already provides @const@-style aliasing using the \emph{unnamed} enumerator \see{\VRef{s:TypeName}}, even if the name @enum@ is misleading (@const@ would be better).
    3139Given the existence of this form, it is conceivable to extend it with types other than @int@.
     
    4250enum( wchar_t * ) { Jack = L"John" };
    4351\end{cfa}
    44 There are several new features have been proposed or are developing in parallel with enumerations.
    45 Two closely related features are iterator and namespace.
    46 
    47 Enumerating features, and range loops in particular, are currently implemented as loops unique to \CFA enumeration and do not align with the
    48 general iterator pattern. They can be adapted to the iterator interface when it comes to maturity.
    49 
    50 Currently, \CFA implements a namespace feature for enumerated types only. There is recently a proposal by Andrew to
    51 generalize the concept of namespace to other types. The enumeration scope will be revisited to follow the semantics
    52 with other types. Also to improve the granularity of scope control, we propose the following extension:
     52\item
     53Currently enumeration scoping is all or nothing.
     54In some cases, it might be useful to increase the scoping granularity to individual enumerators.
    5355\begin{cfa}
    5456enum E1 { @!@A, @^@B, C };
    5557enum E2 @!@ { @!@A, @^@B, C };
    5658\end{cfa}
    57 which provides a combination of scoped and unscoped enumerators.
     59Here, @'!'@ means the enumerator is scoped, and @'^'@ means the enumerator is unscoped.
    5860For @E1@, @A@ is scoped; @B@ and @C@ are unscoped.
    5961For @E2@, @A@, and @C@ are scoped; @B@ is unscoped.
    60 Finding a use case is important to justify completing this extension.
     62Finding a use case is important to justify this extension.
     63\item
     64An extension mentioned in \VRef{s:Ada} is using @typedef@ to create an enumerator alias.
     65\begin{cfa}
     66enum(int) RGB { @Red@, @Green@, Blue };
     67enum(int) Traffic_Light { @Red@, Yellow, @Green@ };
     68typedef RGB.Red OtherRed; // alias
     69\end{cfa}
     70\end{enumerate}
  • doc/theses/jiada_liang_MMath/intro.tex

    r11cced6 rc1c0efdb  
    105105Hence, the term \emph{enumeration} can be confusing and misunderstood.
    106106Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which feature is being used.
    107 This section discusses some language features that are sometimes called enumeration but do not provide all enumeration aspects.
     107This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects.
    108108
    109109
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r11cced6 rc1c0efdb  
    1818\end{comment}
    1919
    20 Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
     20Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell} \see{discussion in \VRef{s:AlgebraicDataType}}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
    2121Among these languages, there is a large set of overlapping features, but each language has its own unique extensions and restrictions.
    2222
     
    3333The type of each constant name (enumerator) is inferred from the constant-expression type.
    3434
    35 Some dialects of Pascal introduced the enumeration type characterized by a set of ordered, unscoped identifiers (enumerators), which are not overloadable.\footnote{%
     35Pascal introduced the enumeration type characterized by a set of ordered, unscoped identifiers (enumerators), which are not overloadable.\footnote{%
    3636Pascal 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).}
    3737\begin{pascal}
     
    5656\end{cquote}
    5757
    58 Pascal provides \emph{consecutive} subtyping of an enumeration using subrange type.
     58Pascal provides \emph{consecutive} subsetting of an enumeration using a subrange type.
    5959\begin{pascal}
    6060type Week = ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
    61                                 Weekday = @Mon..Fri@;
    62                                 Weekend = @Sat..Sun@;
     61           Weekday = @Mon..Fri@;   { subtype }
     62           Weekend = @Sat..Sun@;
    6363var day : Week;
    64           wday : Weekday;
    65           wend : Weekend;
     64         wday : Weekday;
     65         wend : Weekend;
    6666\end{pascal}
    6767Hence, the ordering of the enumerators is crucial to provide the necessary ranges.
     
    8585\begin{tabular}{@{}ll@{}}
    8686\begin{pascal}
     87day := Mon;
    8788if @day@ = wday then
    8889        Writeln( day );
    8990if @day@ <= Fri then
    9091        Writeln( 'weekday');
    91 
    92 
     92Mon
     93weekday
    9394\end{pascal}
    9495&
    9596\begin{pascal}
     97
    9698case @day@ of
    9799  Mon..Fri :
     
    100102        Writeln( 'weekend')
    101103end;
     104weekday
    102105\end{pascal}
    103106\end{tabular}
     
    107110\begin{tabular}{@{}ll@{}}
    108111\begin{pascal}
    109 day := Mon;
    110 while day <= Sat do begin
     112while day <= Sun do begin
    111113        Write( day, ' ' );
    112114        day := succ( day );
    113115end;
    114 Mon Tue Wed Thu Fri Sat
     116Mon Tue Wed Thu Fri Sat Sun
    115117\end{pascal}
    116118&
    117119\begin{pascal}
    118 
    119 for day := Mon to Sat do begin
     120for day := Mon to Sun do begin
    120121        Write( day, ' ' );
    121122
    122123end;
    123 Mon Tue Wed Thu Fri Sat
     124Mon Tue Wed Thu Fri Sat Sun
    124125\end{pascal}
    125126\end{tabular}
     
    150151
    151152The underlying type is an implementation-defined integral type large enough to hold all enumerated values; it does not have to be the smallest possible type.
    152 The integral size can be explicitly specified using compiler directive \$@PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
     153The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
    153154\begin{pascal}
    154155Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
     
    160161
    161162\section{Ada}
     163\label{s:Ada}
    162164
    163165An Ada enumeration type is a set of ordered, unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{%
     
    167169\end{ada}
    168170Object initialization and assignment are restricted to the enumerators of this type.
    169 While Ada enumerators are unscoped, Ada enumerators are overloadable.
     171While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
    170172\begin{ada}
    171173type RGB is ( @Red@, @Green@, Blue );
     
    173175\end{ada}
    174176Like \CFA, Ada uses a type-resolution algorithm, including the left-hand side of the assignment, to disambiguate among overloaded identifiers.
    175 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
     177\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}.
    176178
    177179\begin{figure}
     
    194196\end{ada}
    195197\caption{Ada Enumeration Overload Resolution}
    196 \label{f:AdaEnumeration} 
     198\label{f:AdaEnumeration}
    197199\end{figure}
    198200
     
    201203\begin{ada}
    202204type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
    203 for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
     205        for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
    204206\end{ada}
    205207The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of ascending enumerators.
     
    259261if @Flag@ then ...    -- conditional
    260262\end{ada}
    261 Since only types derived from @Boolean@ can be conditional, @Boolean@ is essentially a built-in type.
    262 
    263 Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}.
     263Since only types derived from @Boolean@ can be conditional, @Boolean@ is essentially a builtin type.
     264
     265Ada provides \emph{consecutive} subsetting of an enumeration using \lstinline[language=ada]{range}.
    264266\begin{ada}
    265267type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
     
    351353whereas C @const@ declarations without @static@ are marked @R@.
    352354
    353 The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}.
    354 \begin{cquote}
    355 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
    356 In C, objects of enumeration type can be assigned values of any integral type. \\
    357 Example:
    358 \begin{c++}
    359 enum color { red, blue, green };
    360 color c = 1;                                                    $\C{// valid C, invalid c++}$
    361 \end{c++}
    362 \textbf{Rationale}: The type-safe nature of \CC. \\
    363 \textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
    364 \textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
    365 \textbf{How widely used}: Common.
    366 \end{cquote}
    367 
    368 \begin{cquote}
    369 Change: In \CC, the type of an enumerator is its enumeration.
    370 In C, the type of an enumerator is @int@. \\
     355The following \CC non-backward compatible change is made~\cite[C.1.5.7.2]{C++}, plus the safe-assignment change shown in~\VRef{s:TypeSafety}.
     356\begin{description}[parsep=0pt]
     357\item[Change:] In \CC, the type of an enumerator is its enumeration.
     358In C, the type of an enumerator is @int@.
    371359Example:
    372360\begin{c++}
    373361enum e { A };
    374362sizeof(A) == sizeof(int)                                $\C{// in C}$
    375 sizeof(A) == sizeof(e)                                  $\C{// in c++}$
     363sizeof(A) == sizeof(e)                                  $\C{// in \CC}$
    376364/* and sizeof(int) is not necessary equal to sizeof(e) */
    377365\end{c++}
    378 \textbf{Rationale}: In \CC, an enumeration is a distinct type. \\
    379 \textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
    380 \textbf{Difficulty of converting}: Semantic transformation. \\
    381 \textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
     366\item[Rationale:] In \CC, an enumeration is a distinct type.
     367\item[Effect on original feature:] Change to semantics of well-defined feature.
     368\item[Difficulty of converting:] Semantic transformation.
     369\item[How widely used:] Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
    382370Taking the size of an enumerator is not a common C coding practice.
    383 \end{cquote}
     371\end{description}
    384372Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
     373
    385374While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
    386375\begin{c++}
     
    438427\end{tabular}
    439428\end{cquote}
    440 % However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values.
    441 \CC enumerators are not relational: the only comparator defined for \CC is the value comparison. The define order of enumerator has no
    442 impact on its behaviour. As a consequence, \CC has no meaningful enumerating mechanism.
     429However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values.
    443430\begin{c++}
    444431enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
     
    4464330 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
    447434\end{c++}
    448 An approximation of enumerating an enum in \CC is to use the last enumerator value as a range. But it inevitably
    449 fails, the enumeration value does not assemble auto-initialization.
     435As a consequence, there is no meaningful enumerating mechanism.
    450436
    451437An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
    452 There is no mechanism to subtype or inherit from an enumeration.
     438There is no mechanism to subset or inherit from an enumeration.
    453439
    454440
     
    465451enum RGB { Red, Green, Blue }
    466452\end{csharp}
    467 The default underlying integral type is @int@ (no @char@), with auto-incrementing and implicit/explicit initialization.
     453The default underlying integral type is @int@, with auto-incrementing and implicit/explicit initialization.
    468454A method cannot be defined in an enumeration type (extension methods are possible).
    469455There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
     
    482468Enumerators have no defined positional meaning.
    483469\begin{csharp}
    484 day = day++ - 5;                                        $\C{// unsafe}$
     470day = day++ - 5;                                        $\C{// value manipulation}$
    485471day = day & day;
    486472\end{csharp}
    487 
    488473\begin{csharp}
    489474for ( Week d = Mon; d <= Sun; @d += 1@ ) {
     
    492477Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun
    493478\end{csharp}
     479As a consequence, there is no direct meaningful enumerating mechanism.
    494480
    495481An enumeration can be used in the @if@ and @switch@ statements.
     
    519505\end{cquote}
    520506
    521 As a compliment, \Csharp's Enum library has @Enum.GetValues@ pseudo-method that retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
     507To indirectly enumerate, \Csharp's Enum library has @Enum.GetValues@, a pseudo-method that retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
    522508\begin{csharp}
    523509foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) {
     
    526512Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13,
    527513\end{csharp}
    528 Hence, enumerating is not supplied directly by the enumeration, but indirectly through another enumerable type, array.
     514Hence, enumerating is not supplied directly by the enumeration, but indirectly through the enumerable array type.
    529515
    530516An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
    531 There is no mechanism to subtype or inherit from an enumeration.
     517There is no mechanism to subset or inherit from an enumeration.
    532518
    533519The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible.
     
    543529
    544530
    545 \section{Golang}
    546 \label{s:Golang}
    547 
    548 Golang has a no enumeration.
     531\section{Go}
     532\label{s:Go}
     533
     534Go has a no enumeration.
    549535It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
    550536\begin{Go}
     
    555541const V = 3.1;  const W = 3.1;
    556542\end{Go}
    557 Since these declarations are immutable variables, they are unscoped, and Golang has no overloading.
    558 
    559 Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
     543Since these declarations are immutable variables, they are unscoped and Go has no overloading.
     544
     545Go provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
    560546\begin{Go}
    561547const ( R = 0; G; B )                                   $\C{// implicit initialization: 0 0 0}$
     
    584570\begin{Go}
    585571const ( Mon = iota; Tue; Wed; // 0, 1, 2
    586                 @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6
     572                @Thu = 10@; Fri; Sat; @Sun = itoa@ ) $\C{// 10, 10, 10, {\color{red}6}}$
    587573\end{Go}
    588 Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing as most \emph{one} \lstinline[language=Go]{iota}.
     574Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing at most \emph{one} \lstinline[language=Go]{iota}.
    589575\begin{Go}
    590576const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
    591577const ( Mon = iota; Tue; Wed; // 0, 1, 2
    592                 @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13
     578                @Thu = 10;@ Fri = @iota@ - Wed + Thu - 1; Sat; Sun ) // 10, 11, 12, 13
    593579\end{Go}
    594580Here, @V4@ and @Fri@ restart auto-incrementing from \lstinline[language=Go]{iota} and reset \lstinline[language=Go]{iota} to 4 and 11, respectively, because of the initialization expressions containing \lstinline[language=Go]{iota}.
     
    635621A basic Java enumeration is an opaque enumeration, where the enumerators are constants.
    636622\begin{Java}
    637 enum Week {
    638         Mon, Tue, Wed, Thu, Fri, Sat, Sun;
    639 }
     623enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun; }
    640624Week day = Week.Sat;
    641625\end{Java}
     
    643627The value of an enumeration instance is restricted to its enumerators.
    644628
    645 The position (ordinal) and label are accessible but there is no value.
     629The position (ordinal) and label (name) are accessible but there is no value property.
    646630\begin{Java}
    647631System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! );
     
    649633\end{Java}
    650634Since @day@ has no value, it prints its label (name).
    651 The member @valueOf@ is the inverse of @name@, converting a string to an enumerator.
     635The member @valueOf@ is the inverse of @name@ converting a string to an enumerator.
    652636\begin{Java}
    653637day = Week.valueOf( "Wed" );
     
    839823\end{c++}
    840824An enumeration type cannot declare an array dimension nor as a subscript.
    841 There is no mechanism to subtype or inherit from an enumeration.
     825There is no mechanism to subset or inherit from an enumeration.
    842826
    843827
     
    888872\end{tabular}
    889873\end{cquote}
    890 (Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.)
     874Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.
    891875
    892876An enumeration is created when \emph{all} the enumerators are unit-type, which is like a scoped, opaque enumeration.
    893877\begin{swift}
    894 enum Week {
    895         case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
    896 };
     878enum Week { case Mon, Tue, Wed, Thu, Fri, Sat, Sun }; // unit-type
    897879var week : Week = @Week.Mon@;
    898880\end{swift}
     
    921903An enumeration can have methods.
    922904\begin{swift}
    923 enum Week: Comparable {
     905enum Week: @Comparable@ {
    924906        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
    925         func @isWeekday() -> Bool@ { return self <= .Fri }    // method
    926         func @isWeekend() -> Bool@ { return .Sat <= self }  // method
     907        func @isWeekday() -> Bool@ { return self <= .Fri }  // methods
     908        func @isWeekend() -> Bool@ { return .Sat <= self }
    927909};
    928910\end{swift}
     
    948930\end{tabular}
    949931\end{cquote}
    950 
    951932Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values.
    952933\begin{swift}
     
    954935        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
    955936};
    956 var weeki : Week = Week.Mon;
    957 if weeki <= .Fri {
    958         print( "weekday" );
    959 }
    960937for day in Week@.allCases@ {
    961938        print( day, terminator:" " )
    962939}
    963 weekday
    964940Mon Tue Wed Thu Fri Sat Sun
    965941\end{swift}
     
    1011987% https://docs.python.org/3/howto/enum.html
    1012988
    1013 Python is a dynamically typed reflexive programming language with multiple incompatible versions.
     989Python is a dynamically-typed reflexive programming language with multiple incompatible versions.
    1014990The generality of the language makes it possible to extend existing or build new language features.
    1015 As a result, discussing Python enumerations is a moving target because if a feature does not exist, it can often be created with varying levels of complexity within the language.
     991As a result, discussing Python enumerations is a moving target, because if a feature does not exist, it can often be created with varying levels of complexity within the language.
    1016992Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13.
    1017993
     
    10301006Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12!
    10311007\end{python}
    1032 where @auto@ increments by 1 from the previous @auto@ value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.
     1008where @auto@ increments by 1 from the previous @auto@ value \see{Go \lstinline[language=Go]{iota}, \VRef{s:Go}}.
    10331009@auto@ is controlled by member @_generate_next_value_()@, which can be overridden:
    10341010\begin{python}
     
    10961072class Week(!OrderedEnum!):
    10971073        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    1098         def !isWeekday(self)!:          # method
     1074        def !isWeekday(self)!:          # methods
    10991075                return Week(self.value) !<=! Week.Fri
    1100         def !isWeekend(self)!:          # method
     1076        def !isWeekend(self)!:
    11011077                return Week.Sat !<=! Week(self.value)
    11021078\end{python}
     
    12461222&
    12471223\begin{ocaml}
    1248 
    1249 
    1250 
    1251 
    1252 
    125312243
    125412253.5
    125512263 5
     1227
     1228
     1229
     1230
     1231
    12561232\end{ocaml}
    12571233\end{tabular}
     
    12981274As seen, a type tag can be used in the @if@ and \lstinline[language=ocaml]{match} statements, where \lstinline[language=ocaml]{match} must be exhaustive or have a default case.
    12991275
    1300 % Enumerating is accomplished by deriving from @enumerate@.
    1301 OCaml enumerators have an ordering following the definition order, but they are not enumerable.
    1302 To iterate over all enumerators, an OCaml type needs to derive the @enumerate@ preprocessor, which appends a list of all enumerators to the program
    1303 abstract syntax tree (AST). The list of values may not persist in the defined ordering. Given that it needs tools that are outside of the native language to facilitate,
    1304 we claim it has no capability of enumerating.
    1305 
    1306 Enumeration subtyping is allowed but inheritance is restricted to classes not types.
     1276While OCaml enumerators have an ordering following the definition order, they are not enumerable.
     1277To iterate over all enumerators, an OCaml type needs to derive from the @enumerate@ preprocessor, which appends a list of all enumerators to the program abstract syntax tree (AST).
     1278However, the list of values may not persist in the defined ordering.
     1279As a consequence, there is no meaningful enumerating mechanism.
     1280
     1281Enumeration subsetting is allowed but inheritance is restricted to classes not types.
    13071282\begin{ocaml}
    13081283type weekday = Mon | Tue | Wed | Thu | Fri
     
    15571532unique values   & \CM   & \CM           &           &           &       &      &                            & \CM               &       &           &       &     \\
    15581533\hline
    1559 auto-init               & \CM   & @all or none@   & \CM      &      &       & \CM     & \CM         & \CM               & \CM   & \CM   & \CM   & \CM   \\
     1534auto-init               & \CM   & all or none   & \CM      &      &       & \CM     & \CM           & \CM               & \CM   & \CM   & \CM   & \CM   \\
    15601535\hline
    1561 (un)Scoped              & U     & U                     & S        & S      & S         & U       & S               & S                         & S     & U             & U/S   & U/S   \\
     1536(Un)Scoped              & U     & U                     & S        & S      & S         & U       & S               & S                         & S     & U             & U/S   & U/S   \\
    15621537\hline
    15631538overload                &               & \CM                   &              &            &      &              &                 &                       &      &            &       & \CM   \\
     
    15741549
    15751550\begin{enumerate}
    1576 \item Opaque: Opaque enums' enumerator cannot be used as its underlying representation or being implemented in terms of ADT.
    1577 \item Typed: The type of value. H: heterogeneous type; values from the same enum need not be the same type.
    1578 U: uni-type; value must have the same type.
    1579 \item Safe: An enumeration variable can only hold a value from its defined enumerators.
    1580 \item Posn ordered: enumerators have defined ordering based on enumerator declaration order.
    1581 It is implied position ordered if its enumerator value must be strictly increasingly ordered.
    1582 \item Unique value: enumerators must have a unique value.
    1583 \item Auto-init: Values are auto-initializable by language specification, often being the "+1" of the predecessor.
    1584 \item (Un)Scoped: U: unscoped enumerators and did not need qualification; S: Scoped enumerators and requires qualification.
    1585 \item Overload: An enumerator label can be used without type qualification in a context where multiple enumerations have defined the label.
    1586 \item Loop: Enumerate enum members without the need to convert an enumeration to another data structure
    1587 \item Arr. dim: An enumeration can be used directly as an array dimension, and enumerators can be mapped to an array element (not a conversion to integer type).
    1588 \item Subset: Name a subset of enumerators as a new type.
    1589 \item Superset: Create a new enumeration that contains all enumerators from pre-defined enumerations.
     1551\item opaque: an enumerator cannot be used as its underlying representation or implemented in terms of an ADT.
     1552\item typed: H $\Rightarrow$ heterogeneous, \ie enumerator values may be different types. \\
     1553U $\Rightarrow$ homogenous, \ie enumerator values have the same type.
     1554\item safety: An enumeration variable can only hold a value from its defined enumerators.
     1555\item posn ordered: enumerators have defined ordering based on enumerator declaration order.
     1556Position ordered is implied if the enumerator values must be strictly increasingly.
     1557\item unique value: enumerators must have a unique value.
     1558\item auto-init: Values are auto-initializable by language specification, often being "+1" of the predecessor.
     1559\item (Un)Scoped: U $\Rightarrow$ enumerators are projected into the containing scope.
     1560S $\Rightarrow$ enumerators are contained in the enumeration scope and require qualification.
     1561\item overload: An enumerator label can be used without type qualification in a context where multiple enumerations have defined the label.
     1562\item loop: Enumerate without the need to convert an enumeration to another data structure.
     1563\item arr. dim: An enumeration can be used directly as an array dimension, and enumerators can be mapped to an array element (not a conversion to integer type).
     1564\item subset: Name a subset of enumerators as a new type.
     1565\item superset: Create a new enumeration that contains all enumerators from pre-defined enumerations.
    15901566\end{enumerate}
  • doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex

    r11cced6 rc1c0efdb  
    145145The \CFA (C-for-all) programming language is an evolutionary refinement of the C programming language.
    146146One of its distinctive features is a parametric-polymorphic generic type.
    147 However, legacy data types from C, such as enumerations, do not adapt well to the \CFA generic type system.
     147However, legacy data types from C, such as enumerations, do not adapt well to the \CFA generic type-system.
    148148
    149149This thesis extends 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.
Note: See TracChangeset for help on using the changeset viewer.