Changes in / [6abb6dc:2ca7fc2]


Ignore:
Files:
7 added
3 deleted
20 edited

Legend:

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

    r6abb6dc r2ca7fc2  
    1 \chapter{\texorpdfstring{\CFA}{Cforall} Enumeration}
    2 
    3 \CFA extends C-Style enumeration by adding a number of new features that bring enumerations in line with other modern programming languages.
    4 Any enumeration extensions must be intuitive to C programmers in syntax and semantics.
    5 The following sections detail my new contributions to enumerations in \CFA.
    6 
    7 
    8 \section{Syntax}
    9 
    10 \CFA extends the C enumeration declaration \see{\VRef{s:CEnumeration}} by parameterizing with a type (like a generic type) and adding Plan-9 inheritance \see{\VRef{s:CFAInheritance}} using an @inline@ to another enumeration type.
     1\chapter{\CFA Enumeration}
     2
     3\CFA extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
     4Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
     5The following sections detail all of my new contributions to enumerations in \CFA.
     6
     7
     8\section{Enumeration Syntax}
     9
     10\CFA extends the C enumeration declaration \see{\VRef{s:CEnumeration}} by parameterizing with a type (like a generic type), and adding Plan-9 inheritance \see{\VRef{s:EnumerationInheritance}} using an @inline@ to another enumeration type.
    1111\begin{cfa}[identifierstyle=\linespread{0.9}\it]
    1212$\it enum$-specifier:
     
    2424
    2525
    26 \section{Operations}
     26\section{Enumeration Operations}
    2727
    2828\CFA enumerations have access to the three enumerations properties \see{\VRef{s:Terminology}}: label, order (position), and value via three overloaded functions @label@, @posn@, and @value@ \see{\VRef{c:trait} for details}.
     
    4343A A @0@ 3
    4444\end{cfa}
    45 Finally, \CFA introduces an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration.
    46 \begin{cfa}
    47 enum(int) E { A, B, C, D } e;
    48 countof( E );  // 4, type argument
    49 countof( e );  // 4, variable argument
    50 \end{cfa}
    51 This built-in function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}.
     45Finally, there is an additional enumeration routine @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration.
     46\begin{cfa}
     47enum(int) E { A, B, C, D };
     48countof( E );  // 4
     49\end{cfa}
     50This auto-generated function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}.
    5251\begin{cfa}
    5352enum E { A, B, C, D, @N@ };  // N == 4
     
    5655The underlying representation of \CFA enumeration object is its position, saved as an integral type.
    5756Therefore, the size of a \CFA enumeration is consistent with a C enumeration.
    58 Attribute function @posn@ performs type substitution on an expression from \CFA type to an integral type.
    59 The label and value of an enumerator are stored in a global data structure for each enumeration, where attribute functions @label@/@value@ map an \CFA enumeration object to the corresponding data.
    60 These operations do not apply to C Enums because backward compatibility means the necessary backing data structures cannot be supplied.
    61 
     57Attribute function @posn@ performs type substitution on an expression from \CFA type to integral type.
     58The label and value of an enumerator is stored in a global data structure for each enumeration, where attribute functions @label@/@value@ map an \CFA enumeration object to the corresponding data.
     59These operations do not apply to C Enums because backwards compatibility means the necessary backing data structures cannot be supplied.
    6260
    6361\section{Opaque Enumeration}
    6462\label{s:OpaqueEnum}
    6563
    66 When an enumeration type is empty. it is an \newterm{opaque} enumeration.
     64When an enumeration type is empty is it an \newterm{opaque} enumeration.
    6765\begin{cfa}
    6866enum@()@ Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
    6967\end{cfa}
    70 Here, the compiler chooses the internal representation, which is hidden, so the enumerators cannot be initialized.
    71 Compared to the C enum, opaque enums are more restrictive regarding typing and cannot be implicitly converted to integers.
     68Here, the internal representation is chosen by the compiler and hidden, so the enumerators cannot be initialized.
     69Compared to the C enum, opaque enums are more restrictive in terms of typing and cannot be implicitly converted to integers.
    7270\begin{cfa}
    7371Mode mode = O_RDONLY;
    7472int www @=@ mode;                                               $\C{// disallowed}$
    7573\end{cfa}
    76 Opaque enumerations have only two attribute properties, @label@ and @posn@.
     74Opaque enumerations have only two attribute properties @label@ and @posn@.
    7775\begin{cfa}
    7876char * s = label( O_TRUNC );                    $\C{// "O\_TRUNC"}$
    7977int open = posn( O_WRONLY );                    $\C{// 1}$
    8078\end{cfa}
    81 Equality and relational operations are available.
     79The equality and relational operations are available.
    8280\begin{cfa}
    8381if ( mode @==@ O_CREAT ) ...
     
    8987\label{s:EnumeratorTyping}
    9088
    91 When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertible to that type.
    92 Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be used with an enumeration, and each type's values are used to set the enumerator constants.
    93 Note the use of the synonyms @Liz@ and @Beth@ in the last declaration.
     89When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertable to that type.
     90Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's values used to set the enumerator constants.
     91Note, the use of the synonyms @Liz@ and @Beth@ in the last declaration.
    9492Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are logically rewritten with @const@.
    9593
     
    132130};
    133131\end{cfa}
    134 Note that the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures.
     132Note, the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures.
    135133
    136134While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are \emph{not} used to evaluate an enumerator's value.
     
    164162bar( x );                                       $\C{// costs (1, 0, 0, 0, 0, 0, 0, 0) or (0, 1, 0, 0, 0, 0, 0, 0)}$
    165163\end{cfa}
    166 Here, the candidate (1) has a @value@ conversion cost to convert to the base type, while the candidate (2) has an @unsafe@ conversion from @double@ to @int@,
    167 which is a more expensive conversion.
     164Here, candidate (1) has a value conversion cost to convert to the base type, while candidate (2) has an unsafe conversion from @double@ to @int@.
    168165Hence, @bar( x )@ resolves @x@ as type @Math@.
    169166
     
    179176
    180177\section{Auto Initialization}
    181 \CFA extends C's auto-initialization scheme to \CFA enumeration. For an enumeration type with base type T, the initialization scheme is the following:
     178
     179A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.
     180\begin{cfa}
     181enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13
     182\end{cfa}
     183The 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.
     184
     185If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.
     186\begin{cfa}
     187enum E(T) { A, B, C };
     188\end{cfa}
    182189\begin{enumerate}
    183 \item the first enumerator is initialized with @T@'s @zero_t@.
    184 \item Every other enumerator is initialized with its previous enumerator's value "+1", where "+1" is defined in terms of overloaded operator @?+?(T, one_t)@.
     190\item the first enumerator, @A@, is initialized with @T@'s @zero_t@.
     191\item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.
    185192\end{enumerate}
    186193
    187 \begin{cfa}
    188 struct S { int i; };
    189 S ?+?( S & s, one_t ) { return s.i++; }
    190 void ?{}( S & s, zero_t ) { s.i = 0; }
    191 enum(S) E { A, B, C, D };
    192 \end{cfa}
    193 
    194 \section{Subset}
    195 
    196 An enumeration's type can be another enumeration.
    197 \begin{cfa}
    198 enum( char ) Letter { A = 'A', ..., Z = 'Z' };
    199 enum( @Letter@ ) Greek { Alph = @A@, Beta = @B@, Gamma = @G@, ..., Zeta = @Z@ }; // alphabet intersection
    200 \end{cfa}
    201 Enumeration @Greek@ may have more or less enumerators than @Letter@, but its enumerator values \emph{must} be from @Letter@.
    202 Therefore, the set of @Greek@ enumerator values in a subset of the @Letter@ enumerator values.
    203 @Letter@ is type compatible with enumeration @Letter@ because value conversions are inserted whenever @Letter@ is used in place of @Greek@.
    204 \begin{cfa}
    205 Letter l = A;                                           $\C{// allowed}$
    206 Greek g = Alph;                                         $\C{// allowed}$
    207 l = Alph;                                                       $\C{// allowed, conversion to base type}$
    208 g = A;                                                          $\C{// {\color{red}disallowed}}$
    209 void foo( Letter );
    210 foo( Beta );                                            $\C{// allowed, conversion to base type}$
    211 void bar( Greek );
    212 bar( A );                                                       $\C{// {\color{red}disallowed}}$
    213 \end{cfa}
    214 Hence, @Letter@ enumerators are not type-compatible with the @Greek@ enumeration, but the reverse is true.
    215 
    216 
    217 \section{Inheritance}
    218 \label{s:CFAInheritance}
     194Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.
     195It 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.
     196Nevertheless, the necessary language concepts exist to support this feature.
     197
     198
     199\section{Enumeration Inheritance}
     200\label{s:EnumerationInheritance}
    219201
    220202\CFA Plan-9 inheritance may be used with \CFA enumerations, where Plan-9 inheritance is containment inheritance with implicit unscoping (like a nested unnamed @struct@/@union@ in C).
    221203Containment is nominative: an enumeration inherits all enumerators from another enumeration by declaring an @inline statement@ in its enumerator lists.
    222204\begin{cfa}
    223 enum( char * ) Names { /* $\see{\VRef[Figure]{f:EumeratorTyping}}$ */ };
     205enum( char * ) Names { /* $\see{\VRef[Figure]{s:EnumerationInheritance}}$ */ };
    224206enum( char * ) Names2 { @inline Names@, Jack = "JACK", Jill = "JILL" };
    225207enum( char * ) Names3 { @inline Names2@, Sue = "SUE", Tom = "TOM" };
     
    234216
    235217Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy.
    236 However, the uniqueness of the enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name an enumerator with the same label as its subtype's members or inherits
    237 from multiple enumeration that has overlapping enumerator labels. Consequently, a new type cannot inherit from an enumeration and its supertype or two enumerations with a
    238 common supertype (the diamond problem) since such would unavoidably introduce duplicate enumerator labels.
     218However, the uniqueness of enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members, or inherits
     219from multiple enumeration that has overlapping enumerator label. As a consequence, a new type cannot inherits from both an enumeration and its supertype, or two enumerations with a
     220common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels.
    239221
    240222The base type must be consistent between subtype and supertype.
    241 When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto-initialized.
     223When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto initialized.
    242224However, the position of the underlying representation is the order of the enumerator in the new enumeration.
    243225\begin{cfa}
    244 enum() E1 { B };                                                                        $\C{// B}$                                             
    245 enum() E2 { C, D };                                             $\C{// C D}$
    246 enum() E3 { inline E1, inline E2, E };  $\C{// {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E}$
    247 enum() E4 { A, inline E3, F};                   $\C{// A {\color{blue}[\(_{E3}\)} {\color{red}[\(_{E1}\)} B {\color{red}]} {\color{red}[\(_{E2}\)} C D {\color{red}]} E {\color{blue}]} F}$
    248 \end{cfa}
    249 In the example, @B@ is at position 0 in @E1@ and @E3@, but position 1 in @E4@ as @A@ takes position 0 in @E4@.
    250 @C@ is at position 0 in @E2@, 1 in @E3@, and 2 in @E4@.
    251 @D@ is at position 1 in @E2@, 2 in @E3@, and 3 in @E4@.
    252 
    253 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost, called \newterm{enumeration conversion}.
     226enum() E1 { A };
     227enum() E2 { B, C };
     228enum() E3 { inline E1, inline E2, D };
     229\end{cfa}
     230Here, @A@ has position 0 in @E1@ and @E3@.
     231@B@ has position 0 in @E2@ and 1 in @E3@.
     232@C@ has position 1 in @E2@ and position 2 in @E3@.
     233@D@ has position 3 in @E3@.
     234
     235A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost.
    254236\begin{cfa}
    255237enum E2 e2 = C;
    256 posn( e2 );                     $\C[1.75in]{// 0}$
    257 enum E3 e3 = e2;        $\C{// Assignment with enumeration conversion E2 to E3}$
    258 posn( e2 );                     $\C{// 1 cost}$
     238posn( e2 );                     $\C[1.75in]{// 1}$
     239enum E3 e3 = e2;
     240posn( e2 );                     $\C{// 2}$
    259241void foo( E3 e );
    260 foo( e2 );                      $\C{// Type compatible with enumeration conversion E2 to E3}$
    261 posn( (E3)e2 );         $\C{// Explicit cast with enumeration conversion E2 to E3}$
    262 E3 e31 = B;                     $\C{// No conversion: E3.B}$
    263 posn( e31 );            $\C{// 0 cost}\CRT$
     242foo( e2 );
     243posn( (E3)e2 );         $\C{// 2}$
     244E3 e31 = B;
     245posn( e31 );            $\C{// 1}\CRT$
    264246\end{cfa}
    265247The last expression is unambiguous.
    266 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ needs no conversion (@zero@ cost).
    267 \CFA selects the lowest cost candidate @E3.B@.
     248While both @E2.B@ and @E3.B@ are valid candidate, @E2.B@ has an associated safe cost and \CFA selects the zero cost candidate @E3.B@.
     249Hence, as discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of the \CFA enum.
     250Therefore, conversion involves both a change of type and possibly position.
     251
     252When converting a subtype to a supertype, its position can only be a larger value.
     253The difference between the position in the subtype and in the supertype is its \newterm{offset}.
     254\VRef[Figure]{s:OffsetSubtypeSuperType} show the algorithm to determine the offset for an subtype enumerator to its super type.
     255\PAB{You need to explain the algorithm.}
     256
     257\begin{figure}
     258\begin{cfa}
     259struct Enumerator;
     260struct CFAEnum {
     261        vector<variant<CFAEnum, Enumerator>> members;
     262};
     263pair<bool, int> calculateEnumOffset( CFAEnum dst, Enumerator e ) {
     264        int offset = 0;
     265        for ( auto v: dst.members ) {
     266                if ( v.holds_alternative<Enumerator>() ) {
     267                        auto m = v.get<Enumerator>();
     268                        if ( m == e ) return make_pair( true, 0 );
     269                        offset++;
     270                } else {
     271                        auto p = calculateEnumOffset( v, e );
     272                        if ( p.first ) return make_pair( true, offset + p.second );
     273                        offset += p.second;
     274                }
     275        }
     276        return make_pair( false, offset );
     277}
     278\end{cfa}
     279\caption{Compute Offset from Subtype Enumerator to Super Type}
     280\label{s:OffsetSubtypeSuperType}
     281\end{figure}
    268282
    269283For the given function prototypes, the following calls are valid.
     
    288302
    289303
    290 \subsection{Offset Calculation}
    291 
    292 As discussed in \VRef{s:OpaqueEnum}, \CFA chooses position as a representation of a \CFA enumeration variable.
    293 When a cast or implicit conversion moves an enumeration from subtype to supertype, the position can be unchanged or increase.
    294 \CFA determines the position offset with an \newterm{offset calculation} function.
    295 
    296 \begin{figure}
    297 \begin{cfa}
    298 struct Enumerator;
    299 struct CFAEnum { vector<variant<CFAEnum, Enumerator>> members; string name; };
    300 inline static bool operator==(CFAEnum& lhs, CFAEnum& rhs) { return lhs.name == rhs.name; }
    301 pair<bool, int> calculateEnumOffset(CFAEnum src, CFAEnum dst) {
    302         int offset = 0;
    303         if ( src == dst ) return make_pair(true, 0);
    304         for ( auto v : dst.members ) {
    305                 if ( holds_alternative<Enumerator>(v) ) {
    306                         offset++;
    307                 } else {
    308                         auto m = get<CFAEnum>(v);
    309                         if ( m == src ) @return@ make_pair( true, offset );
    310                         auto dist = calculateEnumOffset( src, m );
    311                         if ( dist.first ) {
    312                                 @return@ make_pair( true, offset + dist.second );
    313                         } else {
    314                                 offset += dist.second;
    315                         }
    316                 }
    317         }
    318         @return@ make_pair( false, offset );
    319 }
    320 \end{cfa}
    321 \caption{Compute Offset from Subtype Enumeration to a Supertype}
    322 \label{s:OffsetSubtypeSuperType}
    323 \end{figure}
    324 
    325 Figure~\ref{s:OffsetSubtypeSuperType} shows an outline of the offset calculation in \CC.
    326 Structure @CFAEnum@ represents the \CFA enumeration with a vector of variants of @CFAEnum@ or @Enumerator@.
    327 The algorithm takes two @CFAEnums@ parameters, @src@ and @dst@, with @src@ being the type of expression the conversion applies to, and @dst@ being the type the expression is cast to.
    328 The algorithm iterates over the members in @dst@ to find @src@.
    329 If a member is an enumerator of @dst@, the positions of all subsequent members are incremented by one.
    330 If the current member is @dst@, the function returns true indicating \emph{found} and the accumulated offset.
    331 Otherwise, the algorithm recurses into the current @CFAEnum@ @m@ to check if its @src@ is convertible to @m@.
    332 If @src@ is convertible to the current member @m@, this means @src@ is a subtype-of-subtype of @dst@.
    333 The offset between @src@ and @dst@ is the sum of the offset of @m@ in @dst@ and the offset of @src@ in @m@.
    334 If @src@ is not a subtype of @m@, the loop continues but with the offset shifted by the size of @m@.
    335 If the loop ends, than @src@ is not convertible to @dst@, and false is returned.
    336 
    337 
    338 \section{Control Structures}
     304\section{Enumerator Control Structures}
    339305
    340306Enumerators can be used in multiple contexts.
     
    426392
    427393
    428 \section{Dimension}
     394\section{Enumeration Dimension}
    429395
    430396\VRef{s:EnumeratorTyping} introduces the harmonizing problem between an enumeration and secondary information.
     
    443409\footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.}
    444410This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added.
     411
    445412The 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.
    446413This behaviour can be reverted by explicit overloading:
     
    448415float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; }
    449416\end{cfa}
    450 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context.
    451 Designators 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@.
    452 Designators 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.
    453 
    454 
    455 \section{I/O}
    456 
    457 As 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.
     417When an enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's context.
     418As a result, @H2@'s array destinators @A@, @B@ and @C@ are resolved unambiguously to type @E2@.
     419(@H1@'s destinators are also resolved unambiguously to @E1@ because @E2@ has a @value@ cost.)
     420
     421
     422\section{Enumeration I/O}
     423
     424As seen in multiple examples, enumerations can be printed and the default property printed is the enumerator's label, which is similar in other programming languages.
    458425However, very few programming languages provide a mechanism to read in enumerator values.
    459426Even the @boolean@ type in many languages does not have a mechanism for input using the enumerators @true@ or @false@.
    460427\VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels.
    461428When the enumerator labels are packed together in the input stream, the input algorithm scans for the longest matching string.
    462 For basic types in \CFA, the rule is that the same constants used to initialize a variable in a program are available to initialize a variable using input, where string constants can be quoted or unquoted.
     429For basic types in \CFA, the rule is that the same constants used to initialize a variable in a program are available to initialize a variable using input, where strings constants can be quoted or unquoted.
    463430
    464431\begin{figure}
     
    506473\label{f:EnumerationI/O}
    507474\end{figure}
     475
    508476
    509477
  • doc/theses/jiada_liang_MMath/background.tex

    r6abb6dc r2ca7fc2  
    11\chapter{Background}
    22
    3 This chapter covers background material for C enumerations and \CFA features used in later discussions.
     3This chapter covers background material for C enumerations and \CFA features used in later discussion.
    44
    55
     
    1414\begin{enumerate}[leftmargin=*]
    1515\item
    16 For @#define@, the programmer must explicitly manage the constant name and value.
    17 Furthermore, these C preprocessor macro names are outside the C type system and can incorrectly change random text in a program.
     16For @#define@, the programmer has to explicitly manage the constant name and value.
     17Furthermore, these C preprocessor macro names are outside of the C type-system and can incorrectly change random text in a program.
    1818\item
    1919The same explicit management is true for the @const@ declaration, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{
    20 C allows variable-length array declarations (VLA), so this case does work. Still, it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate oper\-ands of assembler instructions and occupies storage.
     20C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate oper\-ands of assembler instructions, and occupies storage.
    2121\begin{clang}
    2222$\$$ nm test.o
     
    2828
    2929
    30 \subsection{C \texorpdfstring{\lstinline{const}}{const}}
     30\subsection{C \lstinline{const}}
    3131\label{s:Cconst}
    3232
     
    8787enum { Size = 20, Max = 10, MaxPlus10 = Max + 10, @Max10Plus1@, Fred = -7 };
    8888\end{clang}
    89 Here, the aliased constants are 20, 10, 20, 21, and -7.
    90 Direct initialization is achieved by a compile-time expression that generates a constant value.
    91 Indirect initialization (without an initializer, @Max10Plus1@) is called \newterm{auto-initialization}, where enumerators are assigned values from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
     89Here, the aliased constants are: 20, 10, 20, 21, and -7.
     90Direct initialization is by a compile-time expression generating a constant value.
     91Indirect initialization (without initializer, @Max10Plus1@) is called \newterm{auto-initialization}, where enumerators are assigned values from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
    9292Because multiple independent enumerators can be combined, enumerators with the same values can occur.
    93 The enumerators are @rvalues@, so the assignment is disallowed.
     93The enumerators are rvalues, so assignment is disallowed.
    9494Finally, enumerators are \newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) outside into the enclosing scope of the @enum@ type.
    95 This semantic is required for unnamed enumerations because there is no type name for scoped qualification.
    96 
    97 As noted, this aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C.
     95For unnamed enumerations, this semantic is required because there is no type name for scoped qualification.
     96
     97As noted, this kind of aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C.
    9898While the semantics is misleading, this enumeration form matches with aggregate types:
    9999\begin{cfa}
     
    112112enum @Week@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun };
    113113\end{clang}
    114 and adopts the same semantics as direct and auto initialization.
     114and adopts the same semantics with respect to direct and auto intialization.
    115115For example, @Mon@ to @Wed@ are implicitly assigned with constants @0@--@2@, @Thu@ is explicitly set to constant @10@, and @Fri@ to @Sun@ are implicitly assigned with constants @11@--@13@.
    116116As well, initialization may occur in any order.
     
    121121};
    122122\end{clang}
    123 Note the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.\footnote{
     123Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.\footnote{
    124124A terminating comma appears in other C syntax, \eg the initializer list.}
    125 This feature allows enumerator lines to be interchanged without moving a comma.
     125This feature allow enumerator lines to be interchanged without moving a comma.
    126126Named enumerators are also unscoped.
    127127
     
    130130\label{s:CenumImplementation}
    131131
    132 Theoretically, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
     132In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
    133133In practice, C defines @int@~\cite[\S~6.4.4.3]{C11} as the underlying type for enumeration variables, restricting initialization to integral constants, which have type @int@ (unless qualified with a size suffix).
    134134However, type @int@ is defined as:
     
    136136A ``plain'' @int@ object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range @INT_MIN@ to @INT_MAX@ as defined in the header @<limits.h>@).~\cite[\S~6.2.5(5)]{C11}
    137137\end{quote}
    138 However, @int@ means 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture.
    139 Whereas @long int@ means 4 bytes on a 32-bit and 8 bytes on 64-bit architectures, and @long long int@ means 8 bytes on both 32/64-bit architectures, where 64-bit operations are simulated on 32-bit architectures.
    140 \VRef[Figure]{f:gccEnumerationStorageSize} shows both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based on its initialization.
    141 Hence, initialization in the range @INT_MIN@..@INT_MAX@ results in a 4-byte enumerator, and outside this range, the enumerator is 8 bytes.
     138However, @int@ means a 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture.
     139Whereas, @long int@ means 4 bytes on a 32-bit and 8 bytes on 64-bit architectures, and @long long int@ means 8 bytes on both 32/64-bit architectures, where 64-bit operations are simulated on 32-bit architectures.
     140\VRef[Figure]{f:gccEnumerationStorageSize} shows both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization.
     141Hence, initialization in the range @INT_MIN@..@INT_MAX@ results in a 4-byte enumerator, and outside this range the enumerator is 8 bytes.
    142142Note that @sizeof( typeof( IMin ) ) != sizeof( E )@, making the size of an enumerator different than is containing enumeration type, which seems inconsistent, \eg @sizeof( typeof( 3 ) ) == sizeof( int )@.
    143143
     
    168168\label{s:Usage}
    169169
    170 C proves an implicit \emph{bidirectional} conversion between an enumeration and its integral type and between two different enumerations.
     170C proves an implicit \emph{bidirectional} conversion between an enumeration and its integral type, and between two different enumerations.
    171171\begin{clang}
    172172enum Week week = Mon;                           $\C{// week == 0}$
     
    178178@week = Winter;@                                        $\C{// UNDEFINED! implicit conversion to Week}$
    179179\end{clang}
    180 While converting an enumerator to its underlying type is sound, the implicit conversion from the base or another enumeration type to an enumeration is a common source of error.
     180While converting an enumerator to its underlying type is useful, the implicit conversion from the base or another enumeration type to an enumeration is a common source of error.
    181181
    182182Enumerators can appear in @switch@ and looping statements.
     
    194194\end{cfa}
    195195For iterating using arithmetic to make sense, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values.
    196 For example, a previous gap introduced by @Thu = 10@ results in iterating over the values 0--13, where values 3--9 are not @Week@ values.
    197 Note that the bidirectional conversion allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@.
     196For example, a previous gap introduced by @Thu = 10@, results in iterating over the values 0--13, where values 3--9 are not @Week@ values.
     197Note, it is the bidirectional conversion that allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@.
    198198For safety, \CC does not support the bidirectional conversion, and hence, an unsafe cast is necessary to increment @day@: @day = (Week)(day + 1)@.
    199199
    200 There is a C idiom that computes the number of enumerators in an enumeration automatically.
     200There is a C idiom to automatically compute the number of enumerators in an enumeration.
    201201\begin{cfa}
    202202enum E { A, B, C, D, @N@ };  // N == 4
    203203for ( enum E e = A; e < @N@; e += 1 ) ...
    204204\end{cfa}
    205 Serendipitously, the auto-incrementing counts the number of enumerators and puts the total into the last enumerator @N@.
    206 This @N@ is often used as the dimension for an array associated with the enumeration.
     205Here, serendipitously the auto-incrementing counts the number of enumerators and puts the total into the last enumerator @N@.
     206This @N@ is often used as the dimension for an array assocated with the enumeration.
    207207\begin{cfa}
    208208E array[@N@];
     
    226226\end{cfa}
    227227However, the companion idiom results in the \emph{harmonizing} problem because an update to the enumeration @Integral_Type@ often requires a corresponding update to the companion array \snake{Integral_Name}.
    228 The requirement to harmonize is, at best, indicated by a comment before the enumeration.
     228The requirement to harmonize is at best indicated by a comment before the enumeration.
    229229This issue is exacerbated if enumeration and companion array are in different translation units.
    230230
    231231\bigskip
    232 While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide helpful/advanced enumeration features in other programming languages.
    233 
    234 
    235 \section{\texorpdfstring{\CFA}{Cforall}}
    236 
    237 \CFA in \emph{not} an object-oriented programming language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an implicit first (\lstinline[language=C++]{this}) parameter.
     232While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide useful/advanced enumeration features found in other programming languages.
     233
     234\section{\CFA Polymorphism}
     235
     236\subsection{Function Overloading}
     237Function overloading is programming languages feature wherein functions may share the same name, but with different function signatures. In both C++ and \CFA, function names can be overloaded
     238with different entities as long as they are different in terms of the number and type of parameters.
     239
     240\section{\CFA}
     241
     242\CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an  implicit first (\lstinline[language=C++]{this}) parameter.
    238243The following sections provide short descriptions of \CFA features needed further in the thesis.
    239 Other \CFA features are presented in situ with short or no explanation because the feature is obvious to C programmers.
     244Other \CFA features are presented in-situ with short explanations, or no explanation because the feature is obvious to C programmers.
    240245
    241246
    242247\subsection{Overloading}
    243248
    244 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources like included files.
     249Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
    245250\begin{quote}
    246251There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
     
    248253Experience from \CC and \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded (variables and) functions.
    249254In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process.
    250 Depending on the language, ambiguous cases are resolved using some form of qualification and/or casting.
     255Depending on the language, ambiguous cases are resolved using some form of qualification or casting.
    251256
    252257
     
    263268s1 = @?+?@( s1, s2 );   $\C{// direct call}\CRT$
    264269\end{cfa}
    265 The type system examines each call size and selects the best matching overloaded function based on the number and types of arguments.
    266 If there are mixed-mode operands, @2 + 3.5@, the type system, like in C/\CC, attempts (safe) conversions, converting the argument type(s) to the parameter type(s).
     270The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments.
     271If there are intermixed operands, @2 + 3.5@, the type system attempts (safe) conversions changing the arguments to one or more of the parameter type(s).
    267272
    268273
    269274\subsection{Function Overloading}
    270275
    271 Both \CFA and \CC allow function names to be overloaded as long as their prototypes differ in the number and type of parameters and returns.
     276Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns.
    272277\begin{cfa}
    273278void f( void );                 $\C[1.75in]{// (1): no parameter}$
     
    277282\end{cfa}
    278283In this case, the name @f@ is overloaded depending on the number and parameter types.
    279 The type system examines each call size and selects the best match based on the number and types of arguments.
    280 Here, the call @f( 'A' )@ is a perfect match for the number and parameter type of function (2).
    281 
    282 Ada, Scala, and \CFA type-systems also use the return type to pinpoint the best-overloaded name in resolving a call.
     284The type system examines each call size and selects the best matching based on the number and types of the arguments.
     285Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
     286
     287Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
    283288\begin{cfa}
    284289int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
     
    302307}
    303308\end{cfa}
    304 The \CFA type system treats overloaded variables as an overloaded function returning a value with no parameters.
     309The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
    305310Hence, no significant effort is required to support this feature.
    306311
     
    308313\subsection{Constructor and Destructor}
    309314
    310 While \CFA is not object-oriented, it adopts many language features commonly used in object-oriented languages;
    311 these features are independent of object-oriented programming.
     315While \CFA in not object oriented, it adopts many language features commonly used in object-oriented languages;
     316these features are independent from object-oriented programming.
    312317
    313318All objects in \CFA are initialized by @constructors@ \emph{after} allocation and de-initialized \emph{before} deallocation.
    314 \CC cannot have constructors for basic types because they have no aggregate type \lstinline[language=C++]{struct/class} in which to insert a constructor definition.
     319\CC cannot have constructors for basic-types because they have no aggregate type \lstinline[language=C++]{struct/class} in which to insert a constructor definition.
    315320Like \CC, \CFA has multiple auto-generated constructors for every type.
    316321
    317322The prototype for the constructor/destructor are @void ?{}( T &, ... )@ and @void ^?{}( T &, ... )@, respectively.
    318 The first parameter is logically the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages and implicitly passed.
     323The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages, and implicitly passed.
    319324\VRef[Figure]{f:CFAConstructorDestructor} shows an example of creating and using a constructor and destructor.
    320325Both constructor and destructor can be explicitly called to reuse a variable.
     
    350355
    351356The C constants @0@ and @1@ have special meaning.
    352 @0@ is the null pointer and is used in conditional expressions, where @if ( p )@ is rewritten @if ( p != 0 )@;
     357@0@ is the null pointer and used in conditional expressions, where @if ( p )@ is rewritten @if ( p != 0 )@;
    353358@1@ is an additive identity in unary operators @++@ and @--@.
    354359Aware of their significance, \CFA provides a special type @zero_t@ and @one_t@ for custom types.
     
    378383At the call size, the type parameter @T@ is bounded to @int@ from the argument @42@.
    379384
    380 For polymorphic functions to be useful, the @forall@ clause needs \newterm{type assertion}s that restrict the polymorphic types it accepts.
     385For polymorphic functions to be useful, the @forall@ clause needs \newterm{type assertion}s that restricts the polymorphic types it accepts.
    381386\begin{cfa}
    382387forall( T @| { void foo( T ); }@ ) void bar( T t ) { @foo( t );@ }
     
    385390bar( s );
    386391\end{cfa}
    387 The assertion on @T@ restricts the range of types that can be manipulated by @bar@ to only those that implement @foo@ with the matching signature, allowing @bar@'s call to @foo@ in its body.
    388 Unlike templates in \CC, which are macro expansions at the call site, \CFA polymorphic functions are compiled, passing the call-site assertion functions as hidden parameters.
    389 
     392The assertion on @T@ restricts the range of types that can be manipulated by @bar@ to only those that have an implementation of @foo@ with the matching signature, allowing @bar@'s call to @foo@ in its body.
    390393
    391394\subsection{Trait}
     395A @forall@ clause can asserts on multiple types and with multiple asserting functions. A common practice in \CFA is to group
     396the asserting functions in to a named \newterm{trait}.
     397
     398\subsection{Trait}
    392399
    393400A @forall@ clause can assert many restrictions on multiple types.
    394 A common practice is refactoring the assertions into a named \newterm{trait}, similar to other languages like Go and Rust.
     401A common practice is to refactor the assertions into a named \newterm{trait}.
    395402\begin{cfa}
    396403forall(T) trait @Bird@ {
     
    407414bird_fly( 23, robin );
    408415\end{cfa}
    409 Grouping type assertions into a named trait effectively creates a reusable interface for parametric polymorphic types.
     416Grouping type assertions into a named trait effectively creates a reusable interface for parametric-polymorphic types.
    410417
    411418
     
    416423When multiple best matches exist, the resolution is ambiguous.
    417424
    418 The \CFA resolver attempts to identify the best candidate based on: first, the number of parameters and types, and second, when no exact match exists, the fewest implicit conversions and polymorphic variables.
    419 Finding an exact match is not discussed here, because the mechanism is fairly straightforward, even when the search space is ample;
     425The \CFA resolver attempts to identity a best candidate based on: first, the number of parameters and types, and second, when no exact match exists, the fewest implicit conversions and polymorphic variables.
     426Finding an exact match is not discussed here, because the mechanism is fairly straightforward, even when the search space is large;
    420427only finding a non-exact match is discussed in detail.
    421428
     
    425432
    426433Most programming languages perform some implicit conversions among basic types to facilitate mixed-mode arithmetic;
    427 otherwise, the program becomes littered with many explicit casts which do not match the programmer's expectations.
    428 C is an aggressive language, providing conversions among almost all basic types, even when the conversion is potentially unsafe or not meaningful, \ie @float@ to @bool@.
     434otherwise, the program becomes littered with many explicit casts, which is not match programmer expectation.
     435C is an aggressive language as it provides conversions among almost all of the basic types, even when the conversion is potentially unsafe or not meaningful, \ie @float@ to @bool@.
    429436C defines the resolution pattern as ``usual arithmetic conversion''~\cite[\S~6.3.1.8]{C11}, in which C looks for a \newterm{common type} between operands, and converts one or both operands to the common type.
    430 A common type is the smallest type in terms of the size of representation that both operands can be converted into without losing their precision, called a \newterm{widening} or \newterm{safe conversion}.
     437Loosely defined, a common type is a the smallest type in terms of size of representation that both operands can be converted into without losing their precision, called a \newterm{widening} or \newterm{safe conversion}.
    431438
    432439\CFA generalizes ``usual arithmetic conversion'' to \newterm{conversion cost}.
     
    438445@poly@ is the number of polymorphic function parameters, and
    439446\item
    440 @safe@ is the sum of the degree of safe (widening) conversions.
     447@safe@ is sum of the degree of safe (widening) conversions.
    441448\end{enumerate}
    442449Sum of degree is a method to quantify C's integer and floating-point rank.
    443 Every pair of widening conversion types is assigned a \newterm{distance}, and the distance between the two same types is 0.
    444 For example, the distance from @char@ to @int@ is 2, from @int@ to @long@ is 1, and from @int@ to @long long int@ is 2.
     450Every pair of widening conversion types is assigned a \newterm{distance}, and distance between the two same type is 0.
     451For example, the distance from @char@ to @int@ is 2, distance from @int@ to @long@ is 1, and distance from @int@ to @long long int@ is 2.
    445452This distance does not mirror C's rank system.
    446 For example, the @char@ and @signed char@ ranks are the same in C, but the distance from @char@ to @signed char@ is assigned 1.
    447 @safe@ cost is summing all pairs of arguments to parameter safe conversion distances.
    448 Among the three costs in Bilson's model, @unsafe@ is the most significant cost, and @safe@ is the least significant, implying that \CFA always chooses a candidate with the lowest @unsafe@, if possible.
    449 
    450 For example, assume the overloaded function @foo@ is called with two @int@ parameters.
    451 The cost for every overloaded @foo@ has been listed along with the following:
     453For example, the rank of @char@ and @signed char@ are the same in C, but the distance from @char@ to @signed char@ is assigned 1.
     454@safe@ cost is summing all pairs of argument to parameter safe conversion distances.
     455Among the three costs in Bilson's model, @unsafe@ is the most significant cost and @safe@ is the least significant, with an implication that \CFA always choose a candidate with the lowest @unsafe@, if possible.
     456
     457For example, assume the overloaded function @foo@ is called with two @int@ parameter.
     458The cost for every overloaded @foo@ has been list along:
    452459\begin{cfa}
    453460void foo( char, char );                         $\C[2.5in]{// (1) (2, 0, 0)}$
     
    462469foo( i, j );                                            $\C{// convert j to long and call (8)}\CRT$
    463470\end{cfa}
    464 The overloaded instances are ordered from the highest to the lowest cost, and \CFA selects the last candidate (8).
     471The overloaded instances are ordered from the highest to the lowest cost, and \CFA select the last candidate (8).
    465472
    466473In the next iteration of \CFA, Schluntz and Aaron~\cite{Moss18} expanded conversion cost to a 7-tuple with 4 additional categories, @(unsafe, poly, safe, sign, vars, specialization, reference)@, with the following interpretations:
     
    469476\item \textit{Poly}
    470477\item \textit{Safe}
    471 \item \textit{Sign} is the number of sign/unsign variable conversions.
    472 \item \textit{Vars} is the number of polymorphic type variables.
    473 \item \textit{Specialization} is the negative value of the number of type assertions.
     478\item \textit{Sign} is the number of sign/unsign variable conversion.
     479\item \textit{Vars} is the number of polymorphics type variable.
     480\item \textit{Specialization} is negative value of the number of type assertion.
    474481\item \textit{Reference} is number of reference-to-rvalue conversion.
    475482\end{itemize}
    476483The extended conversion-cost model looks for candidates that are more specific and less generic.
    477484@vars@ disambiguates @forall( T, V ) foo( T, V )@ and @forall( T ) void foo( T, T )@, where the extra type parameter @V@ makes is more generic.
    478 A more generic type means fewer constraints on its parameter types.
     485A more generic type means less constraints on its parameter types.
    479486\CFA favours candidates with more restrictions on polymorphism, so @forall( T ) void foo( T, T )@ has lower cost.
    480487@specialization@ is an arbitrary count-down value starting at zero.
    481 For every type assertion in the @forall@ clause (no assertions in the above example), \CFA subtracts one from @specialization@.
    482 More type assertions mean more constraints on argument types, making the function less generic.
     488For every type assertion in @forall@ clause (no assertions in the above example), \CFA subtracts one from @specialization@.
     489More type assertions means more constraints on argument types, making the function less generic.
    483490
    484491\CFA defines two special cost values: @zero@ and @infinite@.
    485 A conversion cost is @zero@ when the argument and parameter have an exact match, and a conversion cost is @infinite@ when there is no defined conversion between the two types.
     492A conversion cost is @zero@ when argument and parameter has an exact match, and a conversion cost is @infinite@ when there is no defined conversion between two types.
    486493For example, the conversion cost from @int@ to a @struct S@ is @infinite@.
    487494
    488 In \CFA, the meaning of a C-style cast is determined by its @Cast Cost@.
    489 For most cast-expression resolutions, a cast cost equals a conversion cost.
    490 Cast cost exists as an independent matrix for conversion that cannot happen implicitly while being possible with an explicit cast.
    491 These conversions are often defined as having an infinite conversion cost and a non-infinite cast cost.
     495In \CFA, the meaning of a C style cast is determined by its @Cast Cost@. For most cast expression resolution, a cast cost is equal to a conversion cost.
     496Cast cost exists as an independent matrix for conversion that cannot happen implcitly, while being possible with an explicit cast. These conversions are often defined to have
     497infinite conversion cost and non-infinite cast cost.
  • doc/theses/jiada_liang_MMath/conclusion.tex

    r6abb6dc r2ca7fc2  
    22\label{c:conclusion}
    33
    4 The goal of this work is 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 backward compatibility with C.
    5 Within this goal, the new \CFA enumeration should align with the analogous enumeration features in other languages to match modern programming expectations.
    6 Hence, the \CFA enumeration features are borrowed from a number of programming languages, but engineered to work and play with \CFA's type system and feature set.
     4The goal of this thesis is to adapt enumeration in \CFA to be aligned with the analogous features in
     5other languages while being backward-compatiable to C.
     6The presented features are based off on tools and techniques that widely used in
     7other languages but they were adapted to better fix \CFA's feature set. Additionally, the thesis provides
     8an improvement on safety and productivity of C enumeration, including enumerator overloading,
     9name scoping and type checking.
    710
    8 Strong type-checking of enumeration initialization and assignment provides additional safety, ensuring an enumeration only contains its enumerators.
    9 Overloading and scoping of enumerators significantly reduces the naming problem, providing a better software-engineering environment, with fewer name clashes and the ability to disambiguate those that cannot be implicitly resolved.
    10 Typed enumerations solve the data-harmonization problem increasing safety through better software engineering.
    11 Moreover, integrating enumerations with existing control structures provides a consistent upgrade for programmers and a succinct and secure mechanism to enumerate with the new loop-range feature.
    12 Generalization and reuse are supported by incorporating the new enumeration type using the \CFA trait system.
    13 Enumeration 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.
    14 Using 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.
    16 These tests ensure future \CFA work does not accidentally break the new enumeration system.
     11To further explores the potential of enumerated types, this thesis presents a new \CFA enumeration
     12that is independent on C enumeration. The \CFA enumeration aims to solve the data harmonization problem
     13and have natural support to \CFA generic type, along with some new features that fit with \CFA's
     14programming pattern, such as enumerator conctrol structures.
    1715
    18 The conclusion is that the new \CFA enumeration mechanisms achieve the initial goals, providing C programmers with an intuitive enumeration mechanism for handling modern programming requirements.
     16The \CFA project's test suite has been expanded to test the enumerations with respect to its
     17implicit conversions, inheritance, interaction with the polymorphic types, and the features
     18built on top of enumeration traits.
    1919
     20The enumerated type is an attempt to adapt classic data types into \CFA unique type system. It brings
     21valuable new feature to \CFA in its own right, but also serve as a motivation to adapt other data types
     22in \CFA.
    2023
    21 \section{Future Work}
     24% \section{Future Work}
    2225
    23 The following are ideas to improve and extend the work in this thesis.
    24 \begin{enumerate}
    25 \item
    26 There are still corner cases being found in the current \CFA enumeration implementation.
    27 Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA. %, like compile-time constant-expression evaluation.
    28 When these changes are made, it should be straightforward to update the \CFA enumeration implementation to work with them.
    29 \item
    30 Currently, some aspects of the enumeration trait system require explicitly including the file @enum.hfa@, which can lead to problems.
    31 It should be possible to have this file included implicitly by updating the \CFA prelude.
    32 \item
    33 There are multiple \CFA features being developed in parallel with enumerations.
    34 Two closely related features are iterator and namespace.
    35 Enumerations may have to be modified to dovetail with these features.
    36 For example, enumerating with range loops does not align with the current iterator design, so some changes will be necessary.
    37 \item
    38 C 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).
    39 Given the existence of this form, it is conceivable to extend it with types other than @int@.
    40 \begin{cfa}
    41 enum { Size = 20u, PI = 3.14159L, Jack = L"John" };
    42 \end{cfa}
    43 which matches with @const@ aliasing in other programming languages.
    44 Here, the type of the enumerator is the type of the initialization constant, \eg @typeof( 20u )@ for @Size@ implies @unsigned int@.
    45 Auto-initialization is restricted to the case where all constants are @int@, matching with C.
    46 As seen in \VRef{s:EnumeratorTyping}, this feature is just a shorthand for multiple typed-enumeration declarations.
    47 \begin{cfa}
    48 enum( unsigned int ) { Size = 20u };
    49 enum( long double ) { PI = 3.14159L };
    50 enum( wchar_t * ) { Jack = L"John" };
    51 \end{cfa}
    52 \item
    53 Currently enumeration scoping is all or nothing. In some cases, it might be useful to
    54 increase the scoping granularity to individual enumerators.
    55 \begin{cfa}
    56 enum E1 { @!@A, @^@B, C };
    57 enum E2 @!@ { @!@A, @^@B, C };
    58 \end{cfa}
    59 Here, @'!'@ means the enumerator is scoped, and @'^'@ means the enumerator is unscoped.
    60 For @E1@, @A@ is scoped; @B@ and @C@ are unscoped.
    61 For @E2@, @A@, and @C@ are scoped; @B@ is unscoped.
    62 Finding a use case is important to justify this extension.
    63 \item
    64 An extension mentioned in \VRef{s:Ada} is using @typedef@ to create an enumerator alias.
    65 \begin{cfa}
    66 enum(int) RGB { @Red@, @Green@, Blue };
    67 enum(int) Traffic_Light { @Red@, Yellow, @Green@ };
    68 typedef RGB.Red OtherRed; // alias
    69 \end{cfa}
    70 \end{enumerate}
  • doc/theses/jiada_liang_MMath/intro.tex

    r6abb6dc r2ca7fc2  
    11\chapter{Introduction}
    22
    3 All basic types in a programming language have a set of constants (symbols), and these constants represent computable values, \eg integer types have constants @-1@, @17@, @0xff@ representing whole numbers, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@ representing real numbers, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}} representing (human readable) text, \etc.
     3All basic types in a programming language have a set of constants (symbols), and these constants represent computable values, \eg integer types have constants @-1@, @17@, @0xff@ representing whole numbers, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@ representing  real numbers, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}} representing (human readable) text, \etc.
    44Constants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integer and floating-point types.
    55(In \CFA, the constants @0@ and @1@ can be overloaded for any type.)
     
    1212A constant's symbolic name is dictated by language syntax related to types, \eg @5.@ (double), @5.0f@ (float), @5l@ (long double).
    1313In general, the representation of a constant's value is \newterm{opaque}, so the internal representation can be chosen arbitrarily, \eg two's complement, IEEE floating-point.
    14 In theory, there is an infinite set of constant names per type representing an infinite set of values.
     14In theory, there are an infinite set of constant names per type representing an infinite set of values.
    1515
    1616It is common in mathematics, engineering, and computer science to alias new constants to existing constants so they have the same value, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), K(k), M, G, T for powers of 2\footnote{Overloaded with SI powers of 10.} often prefixing bits (b) or bytes (B), \eg Gb, MB, and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.
     
    2323Because an aliased 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{
    2424The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}.
    25 In theory, there is an infinite set of possible aliasing; in practice, the number of aliasing per program is finite and small.
     25In theory, there are an infinite set of possible aliasing, in practice, the number of aliasing per program is finite and small.
    2626
    2727Aliased constants 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.
     
    5959\end{sloppypar}
    6060\item
    61 The alias names are constants, which follow transitively from their binding to other constants.
     61The alias names are constants, which follows transitively from their binding to other constants.
    6262\item
    6363Defines a type for generating instants (variables).
     
    140140\end{cfa}
    141141For these reasons, aliasing is sometimes called an enumeration.
    142 However, there is no type to create a type-checked instance or iterator cursor, so there is no ability to enumerate.
     142However, there is no type to create a type-checked instance or iterator cursor, so there is no ability for enumerating.
    143143Hence, there are multiple enumeration aspects not provided by aliasing, justifying a separate enumeration type in a programming language.
    144144
     
    158158the ADT has three variants (constructors), @A@, @B@, @C@, with associated types @Int@, @Double@, and @S@.
    159159The constructors create an initialized value of the specific type that is bound to the immutable variables @foo@, @bar@, and @baz@.
    160 Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to initialize and access the value using dynamic pattern-matching.
     160Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to intialize and access the value using dynamic pattern-matching.
    161161\begin{cquote}
    162162\setlength{\tabcolsep}{20pt}
     
    194194baz = Z 5;
    195195\end{haskell}
    196 Here, the constructor name gives different meanings 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.
     196Here, 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.
    197197
    198198Note, the term \newterm{variant} is often associated with ADTs.
    199199However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}.
    200 Here, the type (and possibly the position for equivalent types) is used to discriminate the specific \emph{variant} within the variant instance.
     200Here, the type (and possibly the position for equivalent types) is used to discriminant the specific \emph{variant} within the variant instance.
    201201For example, \VRef[Figure]{f:C++variant} shows the \CC equivalent of the two Haskell ADT types using variant types.
    202202In these languages, the variant cannot be used to simulate an enumeration.
     
    246246data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show)
    247247\end{haskell}
    248 the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other associated types. The @Eq@ allows equality comparison, and @Show@ is for printing.
    249 The nullary constructors for the unit types are numbered left-to-right from $0$ to @maxBound@$- 1$, and provide enumerating operations @succ@, @pred@, @enumFrom@, @enumFromTo@.
     248the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other associated types, @Eq@ allows equality comparison, and @Show@ is for printing.
     249The nullary constructors for the unit types are numbered left-to-right from $0$ to @maxBound@$- 1$, and provides enumerating operations @succ@, @pred@, @enumFrom@, @enumFromTo@.
    250250\VRef[Figure]{f:HaskellEnumeration} shows enumeration comparison and iterating (enumerating).
    251251
     
    296296However, when extended with advanced features, enumerations become complex for both the type system and the runtime implementation.
    297297
    298 The contributions of this work are:
     298The contribution of this work are:
    299299\begin{enumerate}
    300300\item
    301 safety: Define a safe enumeration conversion scheme, both for C and \CFA, and replace ad-hoc C idioms with safer software-engineering approaches.
    302 \item
    303 overloading: Provide a pattern to overload functions, literals, and variables for polymorphic enumerations using the \CFA type system.
    304 \item
    305 scoping: Add a namespace for enumerations and qualified access into the namespace to deal with the naming problem.
    306 \item
    307 generalization: Support all language types for enumerators with associated values providing enumeration constants for any type.
    308 \item
    309 reuse: Implement subset and containment inheritance for enumerations.
    310 \item
    311 control flow: Extend control-flow structures making it safer and easier to enumerate over an enumeration.
    312 \item
    313 I/O: Provide input and output of enumerations based on enumerator names.
     301overloading: provides a pattern to overload functions, literal, and variable for polymorphic enumerations.
     302\item
     303scoping: adds name space for enumerations.
     304\item
     305safety: defines a safe enumeration conversion scheme.
     306\item
     307harmonization: allows enumeration to be mapped with data.
     308\item
     309inheritance: implements containment inheritance for enumerations.
    314310\end{enumerate}
    315311
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r6abb6dc r2ca7fc2  
    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} \see{discussion in \VRef{s:AlgebraicDataType}}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
    21 Among these languages, there is a large set of overlapping features, but each language has its own unique extensions and restrictions.
     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}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
     21Among theses languages, there are a large set of overlapping features, but each language has its own unique extensions and restrictions.
    2222
    2323
     
    3939\end{pascal}
    4040Object initialization and assignment are restricted to the enumerators of this type.
    41 Enumerators are auto-initialized from left to right, starting at zero and incrementing by 1.
     41Enumerators are auto-initialized from left to right, starting at zero, incrementing by 1.
    4242Enumerators \emph{cannot} be explicitly initialized.
    4343Pascal provides a predefined type \lstinline[language=Pascal]{Boolean} defined as:
     
    5656\end{cquote}
    5757
    58 Pascal provides \emph{consecutive} subsetting of an enumeration using a subrange type.
     58Pascal provides \emph{consecutive} subtyping of an enumeration using subrange type.
    5959\begin{pascal}
    6060type Week = ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
    61            Weekday = @Mon..Fri@;   { subtype }
    62            Weekend = @Sat..Sun@;
     61                                Weekday = @Mon..Fri@;
     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.
    68 There is a bidirectional assignment between the enumeration and its subranges.
     68There is bidirectional assignment between the enumeration and its subranges.
    6969\begin{pascal}
    7070day := Sat;
     
    7777day := wend;                    $\C{\{ no check \}}\CRT$
    7878\end{pascal}
    79 A static/dynamic range check should be performed to verify the values assigned to subtypes.
     79There should be a static/dynamic range check to verify values assigned to subtypes.
    8080(Free Pascal does not check and aborts in certain situations, like writing an invalid enumerator.)
    8181
     
    8585\begin{tabular}{@{}ll@{}}
    8686\begin{pascal}
    87 day := Mon;
    8887if @day@ = wday then
    8988        Writeln( day );
    9089if @day@ <= Fri then
    9190        Writeln( 'weekday');
    92 Mon
    93 weekday
     91
     92
    9493\end{pascal}
    9594&
    9695\begin{pascal}
    97 
    9896case @day@ of
    9997  Mon..Fri :
     
    102100        Writeln( 'weekend')
    103101end;
    104 weekday
    105102\end{pascal}
    106103\end{tabular}
     
    110107\begin{tabular}{@{}ll@{}}
    111108\begin{pascal}
    112 while day <= Sun do begin
     109day := Mon;
     110while day <= Sat do begin
    113111        Write( day, ' ' );
    114112        day := succ( day );
    115113end;
    116 Mon Tue Wed Thu Fri Sat Sun
     114Mon Tue Wed Thu Fri Sat
    117115\end{pascal}
    118116&
    119117\begin{pascal}
    120 for day := Mon to Sun do begin
     118
     119for day := Mon to Sat do begin
    121120        Write( day, ' ' );
    122121
    123122end;
    124 Mon Tue Wed Thu Fri Sat Sun
     123Mon Tue Wed Thu Fri Sat
    125124\end{pascal}
    126125\end{tabular}
    127126\end{cquote}
    128 Note that subtypes @Weekday@ and @Weekend@ cannot be used to define a case or loop range.
     127Note, subtype @Weekday@ and @Weekend@ cannot be used to define a case or loop range.
    129128
    130129An enumeration type can be used as an array dimension and subscript.
     
    150149\end{pascal}
    151150
    152 The 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.
     151The 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.
    153152The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
    154153\begin{pascal}
     
    161160
    162161\section{Ada}
    163 \label{s:Ada}
    164162
    165163An Ada enumeration type is a set of ordered, unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{%
     
    174172type Traffic_Light is ( @Red@, Yellow, @Green@ );
    175173\end{ada}
    176 Like \CFA, Ada uses a type-resolution algorithm, including the left-hand side of the assignment, to disambiguate among overloaded identifiers.
    177 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}.
     174Like \CFA, Ada uses a type-resolution algorithm including the left-hand side of assignmente to disambiguate among overloaded identifiers.
     175\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
    178176
    179177\begin{figure}
     
    196194\end{ada}
    197195\caption{Ada Enumeration Overload Resolution}
    198 \label{f:AdaEnumeration}
     196\label{f:AdaEnumeration} 
    199197\end{figure}
    200198
     
    203201\begin{ada}
    204202type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
    205         for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
    206 \end{ada}
    207 The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of ascending enumerators.
     203for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
     204\end{ada}
     205The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.
    208206
    209207Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
     
    261259if @Flag@ then ...    -- conditional
    262260\end{ada}
    263 Since only types derived from @Boolean@ can be conditional, @Boolean@ is essentially a builtin type.
    264 
    265 Ada provides \emph{consecutive} subsetting of an enumeration using \lstinline[language=ada]{range}.
     261Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type.
     262
     263Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}.
    266264\begin{ada}
    267265type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
     
    327325\label{s:C++RelatedWork}
    328326
    329 \CC enumeration is largely backward compatible with C, so it inherited C's enumerations with some modifications and additions.
     327\CC enumeration is largely backwards compatible with C, so it inherited C's enumerations with some modifications and additions.
    330328
    331329\CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization.
     
    353351whereas C @const@ declarations without @static@ are marked @R@.
    354352
    355 The 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.
    358 In C, the type of an enumerator is @int@.
     353The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}.
     354\begin{cquote}
     355Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
     356In C, objects of enumeration type can be assigned values of any integral type. \\
     357Example:
     358\begin{c++}
     359enum color { red, blue, green };
     360color 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}
     369Change: In \CC, the type of an enumerator is its enumeration.
     370In C, the type of an enumerator is @int@. \\
    359371Example:
    360372\begin{c++}
    361373enum e { A };
    362374sizeof(A) == sizeof(int)                                $\C{// in C}$
    363 sizeof(A) == sizeof(e)                                  $\C{// in \CC}$
     375sizeof(A) == sizeof(e)                                  $\C{// in c++}$
    364376/* and sizeof(int) is not necessary equal to sizeof(e) */
    365377\end{c++}
    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.
     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.
    370382Taking the size of an enumerator is not a common C coding practice.
    371 \end{description}
     383\end{cquote}
    372384Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
    373 
    374385While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
    375386\begin{c++}
     
    4334440 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
    434445\end{c++}
    435 As a consequence, there is no meaningful enumerating mechanism.
    436 
    437446An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
    438 There is no mechanism to subset or inherit from an enumeration.
    439 
    440 
    441 \section{C\texorpdfstring{\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace}{Csharp}} % latex bug: cannot use \relsize{2} so use \LARGE
     447There is no mechanism to subtype or inherit from an enumeration.
     448
     449
     450\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
    442451\label{s:Csharp}
    443452
     
    446455% https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums
    447456
    448 \Csharp is a dynamically-typed programming language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
     457\Csharp is a dynamically-typed programming-language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
    449458\begin{csharp}
    450 enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }
     459enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun@,@ } // terminating comma
    451460enum RGB { Red, Green, Blue }
    452461\end{csharp}
    453 The default underlying integral type is @int@, with auto-incrementing and implicit/explicit initialization.
     462The default underlying integral type is @int@ (no @char@), with auto-incrementing, implicit/explicit initialization, and terminating comma.
    454463A method cannot be defined in an enumeration type (extension methods are possible).
    455464There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
     
    462471Console.WriteLine( Week.Fri );          $\C{// print label Fri}$
    463472\end{csharp}
    464 % The majority of the integral operators (relational and arithmetic) work with enumerations, except @*@ and @/@.
    465 % Relational and arithmetic operators are defined in terms of its numeric value only.
    466 % Therefore, enumerators are not ordered and not enumerable like \CC.
    467 Like \CC, \Csharp defines enumeration relational and arithmetic operators in terms of value.
    468 Enumerators have no defined positional meaning.
     473The majority of the integral operators (relational and arithmetic) work with enumerations, except @*@ and @/@.
    469474\begin{csharp}
    470 day = day++ - 5;                                        $\C{// value manipulation}$
     475day = day++ - 5;                                        $\C{// unsafe}$
    471476day = day & day;
    472477\end{csharp}
    473 \begin{csharp}
    474 for ( Week d = Mon; d <= Sun; @d += 1@ ) {
    475         Console.Write( d + " " );
    476 }
    477 Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun
    478 \end{csharp}
    479 As a consequence, there is no direct meaningful enumerating mechanism.
    480478
    481479An enumeration can be used in the @if@ and @switch@ statements.
     
    504502\end{tabular}
    505503\end{cquote}
    506 
    507 To 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).
     504However, there is no mechanism to iterate through an enumeration without an unsafe cast to increment and positions versus values is not handled.
     505\begin{csharp}
     506for ( Week d = Mon; d <= Sun; @d += 1@ ) {
     507        Console.Write( d + " " );
     508}
     509Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun
     510\end{csharp}
     511The @Enum.GetValues@ pseudo-method retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
    508512\begin{csharp}
    509513foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) {
     
    512516Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13,
    513517\end{csharp}
    514 Hence, enumerating is not supplied directly by the enumeration, but indirectly through the enumerable array type.
     518Hence, enumerating is not supplied directly by the enumeration, but indirectly through another enumerable type, array.
    515519
    516520An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
    517 There is no mechanism to subset or inherit from an enumeration.
     521There is no mechanism to subtype or inherit from an enumeration.
    518522
    519523The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible.
     
    529533
    530534
    531 \section{Go}
    532 \label{s:Go}
    533 
    534 Go has a no enumeration.
     535\section{Golang}
     536\label{s:Golang}
     537
     538Golang has a no enumeration.
    535539It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
    536540\begin{Go}
     
    541545const V = 3.1;  const W = 3.1;
    542546\end{Go}
    543 Since these declarations are immutable variables, they are unscoped and Go has no overloading.
    544 
    545 Go provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
     547Since these declarations are unmutable variables, they are unscoped and Golang has no overloading.
     548
     549Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
    546550\begin{Go}
    547551const ( R = 0; G; B )                                   $\C{// implicit initialization: 0 0 0}$
     
    570574\begin{Go}
    571575const ( Mon = iota; Tue; Wed; // 0, 1, 2
    572                 @Thu = 10@; Fri; Sat; @Sun = itoa@ ) $\C{// 10, 10, 10, {\color{red}6}}$
     576                @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6
    573577\end{Go}
    574 Auto-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}.
     578Auto-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}.
    575579\begin{Go}
    576580const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
    577581const ( Mon = iota; Tue; Wed; // 0, 1, 2
    578                 @Thu = 10;@ Fri = @iota@ - Wed + Thu - 1; Sat; Sun ) // 10, 11, 12, 13
     582                @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13
    579583\end{Go}
    580 Here, @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}.
     584Here, @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 intialization expressions containing \lstinline[language=Go]{iota}.
    581585Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@,
    582586at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@.
     
    621625A basic Java enumeration is an opaque enumeration, where the enumerators are constants.
    622626\begin{Java}
    623 enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun; }
     627enum Week {
     628        Mon, Tue, Wed, Thu, Fri, Sat, Sun;
     629}
    624630Week day = Week.Sat;
    625631\end{Java}
    626 The enumerator's members are scoped and cannot be made \lstinline[language=java]{public}, hence requiring qualification.
     632The enumerators members are scoped and cannot be made \lstinline[language=java]{public}, hence require qualification.
    627633The value of an enumeration instance is restricted to its enumerators.
    628634
    629 The position (ordinal) and label (name) are accessible but there is no value property.
     635The position (ordinal) and label are accessible but there is no value.
    630636\begin{Java}
    631637System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! );
     
    633639\end{Java}
    634640Since @day@ has no value, it prints its label (name).
    635 The member @valueOf@ is the inverse of @name@ converting a string to an enumerator.
     641The member @valueOf@ is the inverse of @name@ converting a string to enumerator.
    636642\begin{Java}
    637643day = Week.valueOf( "Wed" );
     
    687693Notice enumerators in the @switch@ statement do not require qualification.
    688694
    689 There are no arithmetic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.
     695There are no arithemtic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.
    690696Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation).
    691697\begin{Java}
     
    776782#[repr(u8)]
    777783enum ADT {
    778         I(isize) @= 5@,
     784        I(isize) @= 5@,  // ???
    779785        F(f64) @= 10@,
    780786        S(S) @= 0@,
     
    785791Through this integral tag, it is possible to enumerate, and when all tags represent the unit type, it behaves like \CC \lstinline[language=C++]{enum class}.
    786792When tags represent non-unit types, Rust largely precludes accessing the tag because the semantics become meaningless.
    787 Hence, the two mechanisms are largely disjoint, and only the enumeration component is discussed.
    788 
    789 In detail, the @enum@ type has an implicit integer tag (discriminant) with a unique value for each variant type.
    790 Direct initialization is achieved by a compile-time expression that generates a constant value.
     793Hence, the two mechanisms are largely disjoint, and ony the enumeration component is discussed.
     794
     795In detail, the @enum@ type has an implicit integer tag (discriminant), with a unique value for each variant type.
     796Direct initialization is by a compile-time expression generating a constant value.
    791797Indirect initialization (without initialization, @Fri@/@Sun@) is auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
    792798There is an explicit cast from the tag to integer.
     
    823829\end{c++}
    824830An enumeration type cannot declare an array dimension nor as a subscript.
    825 There is no mechanism to subset or inherit from an enumeration.
     831There is no mechanism to subtype or inherit from an enumeration.
    826832
    827833
     
    872878\end{tabular}
    873879\end{cquote}
    874 Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.
     880(Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.)
    875881
    876882An enumeration is created when \emph{all} the enumerators are unit-type, which is like a scoped, opaque enumeration.
    877883\begin{swift}
    878 enum Week { case Mon, Tue, Wed, Thu, Fri, Sat, Sun }; // unit-type
     884enum Week {
     885        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
     886};
    879887var week : Week = @Week.Mon@;
    880888\end{swift}
     
    903911An enumeration can have methods.
    904912\begin{swift}
    905 enum Week: @Comparable@ {
     913enum Week: Comparable {
    906914        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
    907         func @isWeekday() -> Bool@ { return self <= .Fri }  // methods
    908         func @isWeekend() -> Bool@ { return .Sat <= self }
     915        func @isWeekday() -> Bool@ { return self <= .Fri }    // method
     916        func @isWeekend() -> Bool@ { return .Sat <= self }  // method
    909917};
    910918\end{swift}
     
    930938\end{tabular}
    931939\end{cquote}
     940
    932941Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values.
    933942\begin{swift}
     
    935944        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type
    936945};
     946var weeki : Week = Week.Mon;
     947if weeki <= .Fri {
     948        print( "weekday" );
     949}
    937950for day in Week@.allCases@ {
    938951        print( day, terminator:" " )
    939952}
     953weekday
    940954Mon Tue Wed Thu Fri Sat Sun
    941955\end{swift}
    942956The @enum.allCases@ property returns a collection of all the cases for looping over an enumeration type or variable (expensive operation).
    943957
    944 A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with the attribute @rawValue@.
    945 Type @Int@ has auto-incrementing from the previous enumerator;
     958A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with attribute @rawValue@.
     959Type @Int@ has auto-incrementing from previous enumerator;
    946960type @String@ has auto-incrementing of the enumerator label.
    947961\begin{cquote}
     
    972986\end{cquote}
    973987
    974 There is a bidirectional conversion from typed enumerator to @rawValue@ and vice versa.
     988There is a bidirectional conversion from typed enumerator to @rawValue@ and vise versa.
    975989\begin{swift}
    976990var weekInt : WeekInt = WeekInt.Mon;
     
    9881002
    9891003Python is a dynamically-typed reflexive programming language with multiple incompatible versions.
    990 The generality of the language makes it possible to extend existing or build new language features.
    991 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.
     1004The generality of the language makes it is possible to extend existing or build new language features.
     1005As a result, discussing Python enumerations is a moving target, because if a features does not exist, it can often be created with varying levels of complexity within the language.
    9921006Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13.
    9931007
    9941008A Python enumeration is not a basic type;
    9951009it is a @class@ inheriting from the @Enum@ class.
    996 The @Enum@ class presents a set of scoped enumerators, where each enumerator is a pair object with a \emph{constant} string name and an arbitrary value.
     1010The @Enum@ class presents a set of scoped enumerators, where each enumerator is a pair object with a \emph{constant} string name and arbitrary value.
    9971011Hence, an enumeration instance is a fixed type (enumeration pair), and its value is the type of one of the enumerator pairs.
    9981012
     
    10011015class Week(!Enum!): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    10021016\end{python}
    1003 and/or explicitly auto-initialized, \eg:
     1017and/or explicitly auto initialized, \eg:
    10041018\begin{python}
    10051019class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = !auto()!; Sat = 4; Sun = !auto()!
    10061020Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12!
    10071021\end{python}
    1008 where @auto@ increments by 1 from the previous @auto@ value \see{Go \lstinline[language=Go]{iota}, \VRef{s:Go}}.
     1022where @auto@ increments by 1 from the previous @auto@ value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.
    10091023@auto@ is controlled by member @_generate_next_value_()@, which can be overridden:
    10101024\begin{python}
     
    10141028\end{python}
    10151029
    1016 There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because dynamic typing changes the type.
     1030There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because the dynamic typing changes the type.
    10171031\begin{python}
    10181032class RGB(Enum): Red = 1; Green = 2; Blue = 3
     
    10721086class Week(!OrderedEnum!):
    10731087        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    1074         def !isWeekday(self)!:          # methods
     1088        def !isWeekday(self)!:          # method
    10751089                return Week(self.value) !<=! Week.Fri
    1076         def !isWeekend(self)!:
     1090        def !isWeekend(self)!:          # method
    10771091                return Week.Sat !<=! Week(self.value)
    10781092\end{python}
     
    11231137class WeekEnd(WeekE): Sat = 6; Sun = 7
    11241138\end{python}
    1125 Here, type @WeekE@ is an abstract type because dynamic typing never uses it.
     1139Here, type @WeekE@ is an abstract type because the dynamic typing never uses it.
    11261140\begin{cquote}
    11271141\setlength{\tabcolsep}{25pt}
     
    11511165@Flag@ is the same as @IntFlag@ but cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@.
    11521166Auto increment for @IntFlag@ and @Flag@ is by powers of 2.
    1153 Enumerators that are combinations of single-bit enumerators are aliases and, hence, invisible.
     1167Enumerators that are a combinations of single bit enumerators are aliases, and hence, invisible.
    11541168The following is an example for @Flag@.
    11551169\begin{python}
     
    12341248\end{cquote}
    12351249(Note, after an @adtv@'s type is know, the enumerator is inferred without qualification, \eg @I(3)@.)
    1236 The type names are independent of the type value and mapped to an opaque, ascending, integral tag, starting from 0, supporting relational operators @<@, @<=@, @>@, and @>=@.
     1250The type names are independent from the type value, and mapped to an opaque, ascending, integral tag, starting from 0, supporting relational operators @<@, @<=@, @>@, and @>=@.
    12371251\begin{cquote}
    12381252\setlength{\tabcolsep}{10pt}
     
    12741288As 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.
    12751289
    1276 While OCaml enumerators have an ordering following the definition order, they are not enumerable.
    1277 To 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).
    1278 However, the list of values may not persist in the defined ordering.
    1279 As a consequence, there is no meaningful enumerating mechanism.
    1280 
    1281 Enumeration subsetting is allowed but inheritance is restricted to classes not types.
     1290Enumerating is accomplished by deriving from @enumerate@.
     1291
     1292Enumeration subtyping is allowed but inheritance is restricted to classes not types.
    12821293\begin{ocaml}
    12831294type weekday = Mon | Tue | Wed | Thu | Fri
     
    15071518\section{Comparison}
    15081519
    1509 \VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages with the explaination of categories below.
    1510 The features are high-level and may not capture nuances within a particular language.
     1520\VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages.
     1521The features are high level and may not capture nuances within a particular language
     1522The @const@ feature is simple macros substitution and not a typed enumeration.
    15111523
    15121524\begin{table}
     
    15171529\newcommand{\CM}{\checkmark}
    15181530\begin{tabular}{r|c|c|c|c|c|c|c|c|c|c|c|c|c}
    1519                                 &Pascal & Ada                   &\Csharp   & OCaml  & Java      &Golang   & Rust                & Swift                 & Python& C             & \CC   & \CFA  \\
     1531                                &Pascal & Ada   &\Csharp& OCaml & Java  &Modula-3&Golang& Rust  & Swift & Python& C             & \CC   & \CFA  \\
    15201532\hline
    1521 enum                    &Dialect& \CM                   & \CM      & ADT    & \CM   & @const@ &ADT/\CM      &ADT/\CM            & \CM   &\CM    &\CM   &\CM\\
     1533@const@                 & \CM   &               &               &               &               &               & \CM   &               &               &               &               & \CM   &               \\
    15221534\hline
    15231535\hline
    1524 opaque                  & \CM    &                              &                  & \CM    & \CM   &             & \CM         & \CM                   &               &               &               & \CM   \\
     1536opaque                  &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
    15251537\hline
    1526 typed                   & Int    & Int                  & Integral  & H     & U     & H       & U/H         & U/H           & H     & Int       & Integral& U   \\
     1538typed                   &               &               &               &               &               &               &               &               &               &               & @int@ & integral      & @T@   \\
    15271539\hline
    1528 safety          & \CM   & \CM                   &          & \CM        & \CM   &                 & \CM                 & \CM                   &               &               & \CM   & \CM   \\
     1540safe                    &               &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   \\
    15291541\hline
    1530 posn ordered    & Implied & Implied     &          & \CM    &       &             &                         &                   &       &           &       & \CM       \\
     1542ordered                 &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
    15311543\hline
    1532 unique values   & \CM   & \CM           &           &           &       &      &                            & \CM               &       &           &       &     \\
     1544dup. values             &               &               &               &               &               &               &               &               &               & alias & \CM   & \CM   & \CM   \\
    15331545\hline
    1534 auto-init               & \CM   & all or none   & \CM      &      &       & \CM     & \CM           & \CM               & \CM   & \CM   & \CM   & \CM   \\
     1546setable                 &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
    15351547\hline
    1536 (Un)Scoped              & U     & U                     & S        & S      & S         & U       & S               & S                         & S     & U             & U/S   & U/S   \\
     1548auto-init               &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
    15371549\hline
    1538 overload                &               & \CM                   &              &            &      &              &                 &                       &      &            &       & \CM   \\
     1550(Un)Scoped              &               &               &               &               &               &               &               &               &               &               & U             & U/S   & U/S   \\
    15391551\hline
    1540 loop                    & \CM   & \CM                   &                  &            &               &                 &                         &                   & \CM   &               &               & \CM   \\
     1552overload                &               & \CM   &               &               &               &               &               &               &               &               &               & \CM   & \CM   \\
    15411553\hline
    1542 arr. dim.               & \CM   & \CM           &                  &              &                 &           &                 &                         &            &      &               & \CM \\
     1554switch                  &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
    15431555\hline
    1544 subset                  & \CM   & \CM                   &         & \CM     &           &                 &                         &                   &               &               &               & \CM   \\
     1556loop                    &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
    15451557\hline
    1546 superset                &               &                               &                 &                 &           &                 &                         &               &           &               &               & \CM   \\
     1558array/subscript &               &               &               &               &               &               &               &               &               &               & \CM   &               & \CM   \\
     1559\hline
     1560subtype                 &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
     1561\hline
     1562inheritance             &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
    15471563\end{tabular}
    15481564\end{table}
    1549 
    1550 \begin{enumerate}
    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. \\
    1553 U $\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.
    1556 Position 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.
    1560 S $\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.
    1566 \end{enumerate}
  • doc/theses/jiada_liang_MMath/test.adb

    r6abb6dc r2ca7fc2  
    22-- with Ada.Standard; use Ada.Standard;
    33procedure test is
    4         type GBR is (  Green, Blue, Red );
    54        type RGB is ( Red, Green, Blue );
    6         for RGB use ( Red => 10, Green => 20, Blue => 21 );
     5        for RGB use ( Red => 10, Green => 20, Blue => 30 );
    76        Colour : RGB := Red;
    87       
     
    9695       
    9796        if B then null; end if;
    98 
    99         B := False;
    100         Colour := Green;
    101 
    102         Put_Line ( Boolean'Image( B ) & " " );
    103         Put_Line ( RGB'Image( RGB'Enum_Val( 10 ) ) & " " );
    10497end test;
    10598
  • doc/theses/jiada_liang_MMath/test.cc

    r6abb6dc r2ca7fc2  
    6161        eca[A] = EC::A;
    6262
    63         enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat = 8, Sun };
    64         if ( Fri < Sat ) cout << "hmm" << endl;
    65         else cout << "ahh" << std::endl;
     63        enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
    6664        Week day = Mon;
    6765        if ( day <= Fri ) cout << "weekday" << endl;
  • doc/theses/jiada_liang_MMath/test.go

    r6abb6dc r2ca7fc2  
    2020
    2121
    22 const ( R = 0; G = 3; B = 3; TT = 3 ) // implicit: 0 3 3
     22const ( R = 0; G = 3; B ) // implicit: 0 0 0
    2323const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) // Fred Mary Jane
    2424const ( H = 0; Jack = "Jack"; J; K = 0; I ) // type change, implicit: 0 Jack Jack
    2525const ( C = iota + G; M = iota; Y )
    2626const ( Mon = iota; Tue; Wed; // 0, 1, 2
    27         Thu = 10; Fri = iota - Wed + Thu - 1; Sat; Sun = 0 ) // 10, 11, 12, 13
     27        Thu = 10; Fri = iota - Wed + Thu - 1; Sat; Sun = iota ) // 10, 11, 12, 13
    2828const ( O1 = iota + 1; _; O3; _; O5 ) // 1, 3, 5
    2929const ( V1 = iota; V2; V3 = 7; V4 = iota + 1; V5 )
     
    3434
    3535func main() {
    36         fmt.Println( "Go:")
    3736        if 3 == R {};
    3837        fmt.Println( R, G, B )
     
    4645
    4746        day := Mon;
    48         day = Sun;
    49 
    5047        switch day {
    5148          case Mon, Tue, Wed, Thu, Fri:
    5249                fmt.Println( "weekday" );
    53           case Sat:
     50          case Sat, Sun:
    5451                fmt.Println( "weekend" );
    5552        }
     
    5754            fmt.Println( i )
    5855        }
    59         fmt.Println(B < TT);
     56
     57        var ar[Sun] int
     58        ar[Mon] = 3
    6059} // main
    61 
    62 // go build test.go
  • doc/theses/jiada_liang_MMath/test.pas

    r6abb6dc r2ca7fc2  
    44        Weekday = Mon..Fri;
    55        Weekend = Sat..Sun;
    6 type Count = ( Zero, One, Two, Ten = 10, Eleven=10 );
    7 type RR = ( A, B, C );
    8 
     6type Count = ( Zero, One, Two, Ten = 10, Eleven );
    97var day   : Week;
    108        wday  : Weekday;
     
    1210        lunch : array[Week] of Integer;
    1311        cnt       :  Count;
    14 // procedure P1(v:Week);
    15 // begin
    16 //      Writeln('Week');
    17 // end;
    18 procedure P1(v:Weekday);
    19 begin
    20         Writeln('Weekday');
    21 end;
    22 procedure P1(v:RR);
    23 begin
    24         Writeln('RR');
    25 end;
    2612begin
    2713        day := Sat;
     
    5440        Writeln();
    5541        for day := Mon to Sat do
    56                 lunch[day] := ord(day) * 10;
    57         for day := Mon to Sun do
    5842                Write( lunch[day], ' ' );
    5943        Writeln();
     
    6145                Write( ord( cnt ), ' ' );
    6246        end;
    63         day := Tue;
    64         P1( day );
    6547        Writeln();
    66 
    67         case (day) of
    68                 Mon: writeln('Excellent!' );
    69                 Tue: writeln('Well done' );
    70         end;
    7148end.
    7249
  • doc/theses/jiada_liang_MMath/test.py

    r6abb6dc r2ca7fc2  
    2525#       Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 10; Sat = 16; Sun = 17
    2626class Week(OrderedEnum):
    27         Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 0
     27        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
    2828        def isWeekday(self):
    2929                return Week(self.value) <= Week.Fri
     
    3434                return cls(date.isoweekday())
    3535
    36 day : Week = Week.Tue
     36day : Week = Week.Tue;
    3737print( "weekday:", day.isWeekday() )
    3838print( "weekend:", day.isWeekend() )
    3939print( "today:", Week.today(date.today()))
    4040
    41 print( Week.Thu.value == 4 )
    42 print( Week.Thu.name == "Thu" )
    43 print( Week( 4 ) == Week.Thu )
    44 print( Week["Thu"].value == 4 )
     41print( Week.Thu.value == 4 );
     42print( Week.Thu.name == "Thu" );
     43print( Week( 4 ) == Week.Thu );
     44print( Week["Thu"].value == 4 );
    4545
    4646if day <= Week.Fri :
    47         print( "weekday" )
     47        print( "weekday" );
    4848match day:
    4949        case Week.Mon | Week.Tue | Week.Wed | Week.Thu | Week.Fri:
    50                 print( "weekday" )
     50                print( "weekday" );
    5151        case Week.Sat | Week.Sun:
    52                 print( "weekend" )
     52                print( "weekend" );
    5353
    5454for day in Week:
     
    8080print( isinstance(Week.Fri, Week) )
    8181
    82 class WeekE(OrderedEnum): pass
     82class WeekE(OrderedEnum): pass;
    8383class WeekDay(WeekE): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5;
    8484class WeekEnd(WeekE): Sat = 6; Sun = 7
     
    121121      Weekend = Sat | Sun
    122122print( f"0x{repr(WeekF.Weekday.value)} 0x{repr(WeekF.Weekend.value)}" )
    123 day : WeekF = WeekF.Mon | WeekF.Tue
     123day : WeekF = WeekF.Mon | WeekF.Tue;
    124124print( type(day) )
    125125for day in WeekF:
     
    164164match diffval:
    165165        case Diff.Int:
    166                 print( "diffval", diffval.value )
     166                print( "diffval", diffval.value );
    167167        case Diff.Float:
    168                 print( "diffval", diffval.value )
     168                print( "diffval", diffval.value );
    169169        case Diff.Str:
    170                 print( "diffval", diffval.value )
     170                print( "diffval", diffval.value );
    171171for i in Diff:
    172172        print( f"Diff type {type(i)}, {i}, {i.name}, {i.value} : " )
     
    197197                return G * self.mass / (self.radius * self.radius)
    198198        def surfaceWeight(self, otherMass):
    199                 return otherMass * self.surfaceGravity()
    200 
    201 class Cats(Enum):
    202         pass
    203 
     199                return otherMass * self.surfaceGravity();
    204200
    205201earthWeight : float = 100
    206 earthMass : float = earthWeight / ( Planet.EARTH.surfaceGravity() )
     202earthMass : float = earthWeight / ( Planet.EARTH.surfaceGravity() );
    207203
    208204p = by_position( Planet, random.randrange(8) ) # select a random orbiting body
  • doc/theses/jiada_liang_MMath/test.swift

    r6abb6dc r2ca7fc2  
    5959
    6060enum WeekInt: Int, CaseIterable {
    61         case Mon, Tue, Wed, Thu = 10, Fri = 14,
     61        case Mon, Tue, Wed, Thu = 10, Fri,
    6262                        Sat = 4, Sun // auto-incrementing
    6363};
  • doc/theses/jiada_liang_MMath/test1.cfa

    r6abb6dc r2ca7fc2  
    11#include <fstream.hfa>                                                                  // sout
    22#include <stdlib.hfa>                                                                   // ato
    3 #include <enum.hfa>
    43
    54// integral
     
    109enum( Letter ) Greek { Alph = A, Beta = B, Gamma = G, /* more enums */ Zeta = Z }; // alphabet intersection
    1110
    12 // integral
    1311enum( char ) Currency { Dollar = '$', Cent = '¢', Yen = '¥', Pound = '£', Euro = 'E' }; // iso-latin-1
    1412enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound };
     
    3533
    3634enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
    37 Mode mode = O_RDONLY;
    38 void opaque() {
    39 bool b = mode == O_RDONLY || mode < O_APPEND;   // disallowed
    40 //int www = mode;       // disallowed
    41 }
     35Mode iomode = O_RDONLY;
     36//bool b = iomode == O_RDONLY || iomode < O_APPEND;     // disallowed
     37//int www = iomode;     // disallowed
    4238
    4339enum( char * ) Colour { Red = "red", Green = "green", Blue = "blue"  };
    44 
    45 enum E1 { A1, B1, C1 = A1, D1 = B1 };
    46 enum(float) E2 { A2 = 3.5, B2 = 4.5, C2 = A, D2 = B };
    4740
    4841void fred() {
     
    5245//greek = A;                                                            // disallowed
    5346
    54         for ( Greek l = Alph; posn(l) < posn(Gamma); l = succ( l ) ) {
     47        for ( Greek l = Alph; posn(l) <= posn(Gamma); l = succ( l ) ) {
    5548                printf( "%s %c %d\n", label( l ), value( l ), posn( l ) );
    5649        }
    57         for ( Currency c = Dollar; posn(c) < posn(Currency.Euro); c = succ( c ) ) {
     50        for ( Currency c = Dollar; posn(c) <= posn(Currency.Euro); c = succ( c ) ) {
    5851                printf( "%s %c %d\n", label( c ), value( c ), posn( c ) );
    5952        }
    6053}
    6154
    62 enum( const char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
    63 enum( const char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" };
    64 enum( const char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" };
    65 void bar() {
    66         Names fred = Names.Fred;
    67         (Names2)fred;  (Names3)fred;  (Names3)Names2.Jack;  // cast to super type
    68         Names2 fred2 = fred;  Names3 fred3 = fred2; // assign to super type
    69         const char * name = fred;
    70         Names name = Fred;
    71         sout | name | label( name ) | posn( name ) | value( name );
    72 }
     55
     56enum( char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
     57enum( char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" };
     58enum( char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" };
     59
    7360void f( Names n ) { sout | "Name" | posn( n ); }
    7461void g( Names2 );
     
    7663void j( char * );
    7764
    78 enum CColour { Red, Blue, Green };
    79 CColour c0 = 0;
    80 CColour c1 = 1;
    81 CColour c = 2;
    82 int w = Red;
    83 
    84 void coo() {
    85         enum(int) Color { Red, Blue, Green };
    86         Colour c = Red;
    87         sout | countof( Colour ) | Countof( c );
    88 //      sout | Countof( Colour );
    89         sout | countof( c );
    90 }
     65enum color { red, blue, green };
     66//color c = 0;
     67//color c = 1;
     68color c = 2;
     69int w = red;
    9170
    9271// enum(int) Week ! { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
     
    10079// }
    10180
    102 void baz() {
    103         enum(int) Count { First, Second, Third/* = First*/, Fourth/* = Second*/ };
    104         enum CCount { First, Second, Third/* = First*/, Fourth/* = Second*/ };
    105         Count cnt = Second;
    106         CCount ccnt = Second;
    107         if ( cnt < Third ) sout | "less than Third";
    108         if ( cnt ) sout | "XXX";
    109         if ( ccnt ) sout | "YYY";
    110         enum(float) F {WWW = 0.0};
    111         F f;
    112         if ( f ) sout | "FFF";
    113         bool ?!=?( Name n, zero_t ) { sout | "DDD";  return n != Fred; }
    114         Name n = Mary;
    115         if ( n ) sout | "NAME";
    116         choose( cnt ) {
    117                 case First: sout | "First";
    118                 case Second: sout | "Second";
    119                 case Third: sout | "Third";
    120                 case Fourth: sout | "Fourth";
    121         }
    122 //      for (d; Week) { sout | d; }
    123 //      for (p; +~=Planet) { sout | p; }
    124         for ( cx; Count ) { sout | cx | nonl; } sout | nl;
    125         for ( cx; +~= Count ) { sout | cx | nonl; } sout | nl;
    126         for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl;
    127         for ( Count cx = lowerBound();; ) {
    128                 sout | cx | nonl;
    129           if ( cx == upperBound() ) break;
    130                 cx = succ( cx );
    131         }
    132         sout | nl;
    133 }
    134 
    13581int main() {
    13682        fred();
    137         Names name = Names.Fred;
     83        Names name = Fred;
    13884//      f( name );
    13985
    14086        int jane_pos = posn( Names.Jane );
    141         const char * jane_value = value( Names.Jane );
    142         const char * jane_label = label( Names.Jane );
     87        char * jane_value = value( Names.Jane );
     88        char * jane_label = label( Names.Jane );
    14389        sout | Names.Jane | posn( Names.Jane) | label( Names.Jane ) | value( Names.Jane );
    144 
    145         bar();
    146         baz();
    147         coo();
    148 
    149         enum Ex { Ax, Bx, Cx, Nx };
    150         float H1[Nx] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // C
    151 //      float H2[Ex] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // CFA
    152 
    153         enum(int) E { A = 3 } e = A;
    154         sout | A | label( A ) | posn( A ) | value( A );
    155         sout | e | label( e ) | posn( e ) | value( e );
    15690}
  • doc/theses/jiada_liang_MMath/test1.java

    r6abb6dc r2ca7fc2  
    33public class test1 {
    44        enum Weekday {
    5                 Mon(7), Tue(6), Wed(5), Thu(3), Fri(4), Sat(3), Sun(7); // must appear first
     5                Mon(7), Tue(6), Wed(5), Thu(3), Fri(3), Sat(3), Sun(1); // must appear first
    66                private long day;
    77                private Weekday( long d ) { day = d; }
     
    2727                }
    2828                for ( Weekday icday : Weekday.values() ) { // position
    29                         System.out.println( icday + " " + icday.ordinal() + " " + icday.day + " " +  icday.name() + ",  " ); // label
     29                        System.out.print( icday + " " + icday.ordinal() + " " + icday.day + " " +  icday.name() + ",  " ); // label
    3030                }
    3131                System.out.println();
    32 
    33                 if (Weekday.Fri == Weekday.Sat) {
    34                         System.out.println( "Alias ");
    35                 }
    3632        }
    3733}
  • doc/theses/jiada_liang_MMath/trait.tex

    r6abb6dc r2ca7fc2  
    22\label{c:trait}
    33
    4 \CC introduced the @std::is_enum@ trait in \CC{11} and concept feature in \CC{20}.
    5 With this combination, it is possible to write a polymorphic function over an enumerated type.
    6 \begin{c++}
     4% Despite parametric polymorphism being a pivotal feature of \CFA, for a long time, there was not
     5% a technique to write functions being polymorphic over enumerated types.
     6\CC introduced @std::is_enum@ trait on \CC{11} and @concepts@ on \CC{20}; with the combination, users can
     7write function polymorphic over enumerated type in \CC:
     8\begin{cfa}
    79#include <type_traits>
    8 template<typename T>  @concept Enumerable@  =  std::is_enum<T>::value;
    9 template<@Enumerable@ E>  E  f( E e ) { $\C{// constrainted type}$
    10         E w = e;                                                        $\C{// alloction and copy}$
    11         cout << e << ' ' << w << endl;          $\C{// value}$
    12         return w;                                                       $\C{// copy}$
    13 }
    14 int main() {
    15         enum E { A = 42, B, C } e = C;
    16         e = f( e );
    17 }
    18 44 44
    19 \end{c++}
    20 The @std::is_enum@ and other \CC @traits@ are a compile-time interfaces to query type information.
    21 While named the same as @trait@ in other programming languages, it is orthogonal to the \CFA trait, with the latter being defined as a collection of assertion to be satisfied by a polymorphic type.
    22 
    23 The following sections cover the underlying implementation features I created to generalize and restrict enumerations in the \CFA type-system using the @trait@ mechanism.
    24 
    25 
    26 \section{Traits \texorpdfstring{\lstinline{CfaEnum}{CfaEnum}} and \texorpdfstring{\lstinline{TypedEnum}}{TypedEnum}}
    27 
    28 Traits @CfaEnum@ and @TypedEnum@ define the enumeration attributes: @label@, @posn@, @value@, and @Countof@.
    29 These traits support polymorphic functions for \CFA enumeration, \eg:
    30 \begin{cfa}
    31 forall( E ) | @CfaEnum( E )@ )
    32 void f( E e ) {
    33         // access enumeration properties for e
    34 }
    35 \end{cfa}
    36 
    37 Trait @CfaEnum@ defines attribute functions @label@ and @posn@ for all \CFA enumerations, and internally \CFA enumerations fulfills this assertion.
     10
     11template<typename T>
     12@concept Enumerable = std::is_enum<T>::value;@
     13
     14template<@Enumerable@ T>
     15void f(T) {}
     16\end{cfa}
     17The @std::is_enum@ and other \CC @traits@ are a compile-time interfaces to query type information.
     18While named the same as @trait@, it is orthogonal to \CFA trait, as the latter being defined as
     19a collection of assertion to be satisfied by a polymorphic type.
     20
     21\CFA provides @CfaEnum@ and @TypedEnum@ traits to supports polymorphic functions for \CFA enumeration:
     22\begin{cfa}
     23forall(T | @CfaEnum(T)@)
     24void f(T) {}
     25\end{cfa}
     26
     27\section{CfaEnum and TypedEnum}
     28\CFA defines attribute functions @label()@ and @posn()@ for all \CFA enumerations,
     29and therefore \CFA enumerations fulfills the type assertions with the combination.
     30With the observation, we define trait @CfaEnum@:
    3831\begin{cfa}
    3932forall( E ) trait CfaEnum {
     
    4235};
    4336\end{cfa}
    44 This trait covers opaque enumerations that do not have an explicit @value@.
    45 
    46 The trait @TypedEnum@ extends @CfaEnum@ with the @value@ assertion for typed enumerations.
     37
     38% The trait @TypedEnum@ extends @CfaEnum@ with an additional value() assertion:
     39Typed enumerations are \CFA enumeration with an additional @value@ attribute. Extending
     40CfaEnum traits, TypedEnum is a subset of CFAEnum that implements attribute function @value()@,
     41which includes all typed enumerations.
    4742\begin{cfa}
    4843forall( E, V | CfaEnum( E ) ) trait TypedEnum {
     
    5045};
    5146\end{cfa}
    52 Here, the associate type-parameter @V@ is the base type of the typed enumeration, and hence, the return type of @value@.
    53 These two traits provide a way to define functions over all \CFA enumerations.
    54 
    55 For example, \VRef[Figure]{f:GeneralizedEnumerationFormatter} shows a generalized enumeration formatter for any enumeration type.
    56 The formatter prints an enumerator name and its value in the form @"label( value )"@.
    57 The trait for @format_enum@ requires a function named @str@ for printing the value (payload) of the enumerator.
    58 Hence, enumeration defines how its value appear and @format_enum@ displays this value within the label name.
    59 
    60 \begin{figure}
    61 \begin{cfa}
    62 forall( @E, V | TypedEnum( E, V )@ | { string str( V ); } ) $\C{// format any enumeration}$
    63 string format_enum( E e ) {
    64         return label( e ) + '(' + str( value( e ) ) + ')'; $\C{// "label( value )"}$
    65 }
    66 enum(size_t) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF };
    67 // string library has conversion function str from size_t to string
    68 
    69 struct color_code { int R, G, B; };
     47Type parameter V of TypedEnum trait binds to return type of @value()@, which is also the base
     48type for typed enumerations. CfaEnum and TypedEnum triats constitues a CfaEnum function interfaces, as well a way to define functions
     49over all CfaEnum enumerations.
     50\begin{cfa}
     51// for all type E that implements value() to return type T, where T is a type that convertible to string
     52forall(  E, T | TypedEnum( E, T ) | { ?{}(string &, T ); } )
     53string format_enum( E e ) { return label(E) + "(" + string(value(e)) + ")"; }
     54
     55// int is convertible to string; implemented in the standard library
     56enum(int) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF };
     57
     58struct color_code { int R; int G; int B };
     59// Implement color_code to string conversion
     60?{}(string & this, struct color_code p ) {
     61        this = string(p.R) + ',' + string(p.G) + ',' + string(p.B);
     62}
    7063enum(color_code) Rainbow {
    71         Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ...
     64        Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0},
     65        Blue = {0, 0, 255}, Indigo = {75, 0, 130}, Purple = {148, 0, 211}
    7266};
    73 string str( color_code cc ) with( cc ) {        $\C{// format payload, "ddd,ddd,ddd"}$
    74         return str( R ) + ',' + str( G ) + ',' + str( B ); $\C{// "R,G,B"}$
    75 }
    76 int main() {
    77         sout | format_enum( RGB.Green );                $\C{// "Green(65280)"}$
    78         sout | format_enum( Rainbow.Green );    $\C{// "Green(0,255,0)"}$
    79 }
    80 \end{cfa}
    81 \caption{Generalized Enumeration Formatter}
    82 \label{f:GeneralizedEnumerationFormatter}
    83 \end{figure}
    84 
    85 Other types may work with traits @CfaEnum@ and @TypedEnum@, by supplying appropriate @label@, @posn@, and @value@ functions.
    86 For example, \VRef[Figure]{f:ExtendCEnumeration} extends a (possibly predefined) C enumeration to work with all the \CFA extensions.
    87 
    88 \begin{figure}
    89 \begin{cfa}
    90 enum Fruit { Apple, Banana, Cherry };           $\C{// C enum}$
    91 const char * @label@( Fruit f ) {
    92         static const char * labels[] = { "Apple", "Banana", "Cherry" };
    93         return labels[f];
    94 }
    95 int @posn@( Fruit f ) { return f; }
    96 int @value@( Fruit f ) {
    97         static const char values[] = { 'a', 'b', 'c' };
    98         return values[f];
    99 }
    100 sout | format_enum( Cherry );                           $\C{// "Cherry(c)"}$
    101 \end{cfa}
    102 \caption{Extend C Enumeration to \CFA Enumeration}
    103 \label{f:ExtendCEnumeration}
    104 \end{figure}
    105 
    106 
    107 \section{Discussion: Genericity}
    108 
    109 At the start of this chapter, the \CC concept is introduced to constraint template types, \eg:
    110 \begin{c++}
    111 concept Enumerable = std::is_enum<T>::value;
    112 \end{c++}
    113 Here, @concept@ is referring directly to types with kind @enum@;
    114 other @concept@s can refer to all types with kind @int@ with @long@ or @long long@ qualifiers, \etc.
    115 Hence, the @concept@ is a first level of restriction allowing only the specified kinds of types and rejecting others.
    116 The template expansion is the second level of restriction verifying if the type passing the @concept@ test provides the necessary functionality.
    117 Hence, a @concept@ is querying precise aspects of the programming language set of types.
    118 
    119 Alternatively, languages using traits, like \CFA, Scala, Go, and Rust, are defining a restriction based on a set of operations, variables, or structure fields that must exist to match with usages in a function or aggregate type.
    120 Hence, the \CFA enumeration traits never connected with the specific @enum@ kind.
    121 Instead, anything that can look like the @enum@ kind is considered an enumeration (duck typing).
    122 However, Scala, Go, and Rust traits are nominative: a type explicitly declares a named traits to be of its type, while in \CFA, any type implementing all requirements declared in a trait implicitly satisfy its restrictions.
    123 
    124 One of the key differences between concepts and traits, which is leveraged heavily by \CFA, is the ability to apply new \CFA features to C legacy code.
    125 For example, \VRef[Figure]{f:GeneralizedEnumerationFormatter} shows that pre-existing C enumerations can be upgraded to work and play with new \CFA enumeration facilities.
    126 Another example is adding constructors and destructors to pre-existing C types by simply declaring them for the old C type.
    127 \CC fails at certain levels of legacy extension because many of the new \CC features must appear \emph{within} an aggregate definition due to the object-oriented nature of he type system, where it is impossible to change legacy library types.
     67
     68format_enum(RGB.Green); // "Green(65280)"
     69format_enum(Rainbow.Green); // "Green(0,255,0)"
     70\end{cfa}
     71
     72
     73% Not only CFA enumerations can be used with CfaEnum trait, other types that satisfy
     74% CfaEnum assertions are all valid.
     75Types does not need be defined as \CFA enumerations to work with CfaEnum traits. CfaEnum applies to any type
     76with @label()@ and @value()@ being properly defined.
     77Here is an example on how to extend a C enumeration to comply CfaEnum traits:
     78\begin{cfa}
     79enum Fruit { Apple, Banana, Cherry };                   $\C{// C enum}$
     80const char * label( Fruit f ) {
     81        choose( f ) {
     82                case Apple: return "Apple";
     83                case Banana: return "Banana";
     84                case Cherry: return "Cherry";
     85        }
     86}
     87unsigned posn( Fruit f ) { return f; }
     88char value( Fruit f ) {
     89        choose(f)  {
     90                case Apple: return 'a';
     91                case Banana: return 'b';
     92                case Cherry: return 'c';
     93        }
     94}
     95
     96format_enum(Cherry); // "Cherry(c)"
     97\end{cfa}
     98
     99\section{Discussion: Static Type Information}
     100@CfaEnum@ and @TypedEnum@ are approximations to \CFA Enumerations and Typed Enumerations: they are not
     101assertions on a type being an enumerated type,
     102but rather types being shared an interfaces with \CFA enumerations.
     103\CC's @type_traits@ is fundamentally different than \CFA's traits: \CC's @type_traits@ are descriptions
     104of compile time type information
     105\footnote{Concepts can check if a \CC class implement a certain method,
     106but it is to probe a static type information of a class having a such member.}
     107, while \CFA's trait describe how a type can be used,
     108which is a closer paradigm to a trait system in languages such as Scala and Rust.
     109However, Scala and Rust's traits are nominative:
     110a type explicitly declare a named traits to be of its type; while in \CFA,
     111type implements all functions declares in a trait to implicitly be of the trait type.
     112
     113If to support static type information, \CFA needs new piece of syntax to distinguish static type
     114query from function calls, for example:
     115\begin{cfa}
     116forall(T | { T::is_enum; });
     117\end{cfa}
     118When to call a polymorphic function @foo(T)@ with assertions set @S@ and function call argument @a@, \CFA
     119determines if there is an overloaded name @a@ that has non-zero conversion cost to all assertions in @S@.
     120As a consequence, @is_enum@ can be a \CFA directive that immediately trim down the search space of @a@ to
     121be some enumerated types. In fact, because \CFA stores symbols maps to enumeration in a standalone data structure.
     122Limiting search space to enumeration improve on \CFA resolution speed.
     123
     124While assertion on static type information seems improvement on expressivity, it is a challenge to
     125extend its capability without a fully functional pre-processsor that evaluate constant expression as \CC
     126compilers does. The described @is_enum@ manipulate compiler behaviour, which cannot be easily extended to
     127other usage cases. Therefore, \CFA currently does not support @is_enum@ and utalizes traits as a workaround.
    128128
    129129
    130130\section{Bounded and Serial}
    131 
    132 A bounded trait defines a lower and upper bound for a type.
     131A bounded type defines a lower bound and a upper bound.
    133132\begin{cfa}
    134133forall( E ) trait Bounded {
     
    136135        E lowerBound();
    137136};
    138 \end{cfa}
    139 Both functions are necessary for the implementation of \CFA enumeration, with @lowerBound@ returning the first enumerator and @upperBound@ returning the last enumerator.
    140 \begin{cfa}
    141 enum(int) Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
    142 enum(int) Fruit { Apple, Banana, Cherry };
    143 Week first_day = lowerBound();                          $\C{// Mon}$
    144 Fruit last_fruit = upperBound();                        $\C{// Cherry}$
    145 \end{cfa}
    146 The @lowerBound@ and @upperBound@ are functions overloaded on return type only, meaning their type resolution depends solely on the call-site context, such as the parameter type for a function argument or the left hand size of an assignment expression.
    147 Calling either function without a context results in a type ambiguity, unless the type environment has only one type overloading the functions.
    148 \begin{cfa}
    149 sout | @lowerBound()@;      $\C[2.5in]{// ambiguous as Week and Fruit implement Bounded}$
    150 void foo( Fruit );
    151 foo( lowerBound() );            $\C{// parameter provides type Fruit}$
    152 Week day = upperBound();        $\C{// day provides type Week}\CRT$
    153 \end{cfa}
    154 
    155 Trait @Serial@ is a subset of @Bounded@, with functions mapping enumerators to integers, and implementing a sequential order between enumerators.
     137
     138\end{cfa}
     139Both Bounded functions are implement for \CFA enumerations, with @lowerBound()@ returning the first enumerator and @upperBound()@ returning
     140the last enumerator.
     141\begin{cfa}
     142Workday day = lowerBound();                                     $\C{// Mon}$
     143Planet outermost = upperBound();                                $\C{// NEPTUNE}$
     144\end{cfa}
     145
     146The lowerBound() and upperBound() are functions overloaded on return type only, means their type resolution solely depend on the outer context,
     147including expected type as a function argument, or the left hand size of an assignment expression.
     148Calling either function without a context results in a type ambiguity, except in the rare case where the type environment has only one
     149type overloads the functions, including \CFA enumerations, which has Bounded functions automatic defined.
     150\begin{cfa}
     151@lowerBound();@                 $\C{// ambiguous as both Workday and Planet implement Bounded}$
     152sout | @lowerBound()@;
     153Workday day = first();          $\C{// day provides type Workday}$
     154void foo( Planet p );
     155foo( last() );                      $\C{// argument provides type Planet}$
     156\end{cfa}
     157
     158@Serial@ is a subset of @Bounded@, with functions maps elements against integers, as well implements a sequential order between members.
    156159\begin{cfa}
    157160forall( E | Bounded( E ) ) trait Serial {
    158         int fromInstance( E e );
     161        unsigned fromInstance( E e );
    159162        E fromInt( unsigned int i );
     163        E succ( E e );
    160164        E pred( E e );
    161         E succ( E e );
    162         unsigned Countof( E );
     165        unsigned Countof( E e );
    163166};
    164167\end{cfa}
    165 Function @fromInstance@ projects a @Bounded@ member to a number and @fromInt@ is the inverse.
    166 Function @pred@ and @succ@ are advancement functions:
    167 @pred@ takes an enumerator and returns the previous enumerator, if there is one, in sequential order, and @succ@ returns the next enumerator.
    168 \begin{cfa}
    169 sout | fromInstance( Wed ) | fromInt( 2 ) | succ( Wed ) | pred( Wed );
    170 2 Wed Thu Tue
    171 \end{cfa}
    172 Bound checking is provided for @fromInt@, @pred@, and @succ@, and the program is terminated if the lower or upper bound is exceeded, \eg:
    173 \begin{cfa}
    174 fromInt( 100 );
    175 Cforall Runtime error: call to fromInt has index 100 outside of enumeration range 0-6.
    176 \end{cfa}
    177 Function @fromInstance@ or a position cast using @(int)@ is always safe, \ie within the enumeration range.
    178 
    179 Function @Countof@ is the generic counterpart to the builtin pseudo-function @countof@.
    180 @countof@ only works on enumeration types and instances, so it is locked into the language type system;
    181 as such, @countof( enum-type)@ becomes a compile-time constant.
    182 @Countof@ works on an any type that matches the @Serial@ trait.
    183 Hence, @Countof@ does not use its argument;
    184 only the parameter type is needed to compute the range size.
    185 \begin{cfa}
    186 int Countof( E ) {
    187         E upper = upperBound();
    188         E lower = lowerBound();
    189         return fromInstance( upper ) + fromInstance( lower ) + 1;
    190 }
    191 \end{cfa}
    192 
    193 @countof@ also works for any type @E@ that defines @Countof@ and @lowerBound@, becoming a call to @Countof( E )@.
    194 The resolution step on expression @countof( E )@ are:
     168
     169% A Serail type can project to an unsigned @int@ type, \ie an instance of type T has a corresponding integer value.
     170Function @fromInstance()@ projects a @Bounded@ member to a number and @fromInt@ is the inverser. Function @succ()@ take an element, returns the "next"
     171member in sequential order and @pred()@ returns the "last" member.
     172
     173A Serial type E may not be having a one-to-one mapping to integer because of bound. An integer that cannot be mapping to a member of E is called the member \newterm{out of bound}.
     174Calling @succ()@ on @upperBound@ or @pred()@ on @lowerBound()@ results in out of bound.
     175
     176\CFA implements Serial interface for CFA enumerations with \newterm{bound check} on @fromInt()@, @succ()@ and @pred()@, and abort the program if the function call results in out of bound.
     177Unlike a cast, conversion between \CFA enumeration and integer with @Serial@ interface is type safe.
     178Specifically for @fromInt@, \CFA abort if input i smaller than @fromInstance(lowerBound())@ or greater than @fromInstance(upperBound())@
     179
     180Function @Countof@ takes an object as a parameter and returns the number of elements in the Serial type, which is @fromInstance( upper ) - fromInstance( lower ) + 1@.
     181@Countof@ does not use its arugment as procedural input; it needs
     182an argument to anchor its polymorphic type T.
     183
     184\CFA has an expression @countof@ (lower case) that returns the number of enumerators defined for enumerations.
     185\begin{cfa}
     186enum RGB {Red, Green, Blue};
     187countof( RGB );
     188countof( Red );
     189\end{cfa}
     190Both expressions from the previous example are converted to constant expression @3@ with no function call at runtime.
     191@countof@ also works for any type T that defines @Countof@ and @lowerBound@, for which it turns into
     192a function call @Countof( T )@. The resolution step on expression @countof(e)@ works as the following with priority ordered:
    195193\begin{enumerate}
    196 \item Look for an enumeration named @E@, such as @enum E {... }@.
    197 If such an enumeration @E@ exists, replace @countof( E )@  with the number of enumerators.
    198 \item Look for a non-enumeration type named @E@ that defines @Countof@ and @lowerBound@, including @E@ being a polymorphic type, such as @forall( E )@.
    199 If type @E@ exists, replaces it with @Countof(lowerBound())@, where @lowerBound@ is defined for type @E@.
    200 \item Look for an enumerator @A@ defined in enumeration @E@.
    201 If such an enumerator @A@ exists, replace @countof( A )@ with the number of enumerators in @E@.
    202 \item Look for a name @A@ in the lexical context with type @E@.
    203 If such name @A@ exists, replace @countof( A )@ with function call @Countof( E )@.
    204 \item If 1-4 fail, report a type error on expression @countof( E )@.
     194\item Looks for an enumeration named e, such as @enum e {... }@.
     195If such an enumeration e exists, \CFA replace @countof(e)@  with constant expression with number of enumerator of e.
     196\item Looks for a non-enumeration type named e that defines @Countof@ and @lowerBound@, including e being a polymorphic type, such as @forall(e)@.
     197If type e exists, \CFA replaces it with @Countof(lowerBound())@, where lowerBound() is bounded to type e. 
     198\item Looks for an enumerator e that defined in enumeration E. If such an enumeration e exists, \CFA replace @countof(e)@ with constant expression with number of enumerator of E.
     199\item Looks for a name e in the context with expression type E. If such name e exists, \CFA replace @countof(e)@ with function call @Countof(e)@.
     200\item If 1-4 fail, \CFA reports a type error on expression @countof(e)@.
    205201\end{enumerate}
    206202
    207 
    208 \section{Enumerating}
    209 
    210 The fundamental aspect of an enumeration type is the ability to enumerate over its enumerators.
    211 \CFA supports \newterm{for} loops, \newterm{while} loop, and \newterm{range} loop. This section covers @for@ loops and @range@ loops for enumeration, but the concept transition to @while@ loop.
    212 
     203\section{Iterating Over An Enumeration}
     204An fundamental aspect of an enumerated type is to be able to enumerate over its enumerators. \CFA supports \newterm{for} loops,
     205\newterm{while} loop, and \newterm{range} loop. This section covers @for@ loops and @range@ loops for
     206enumeration, but the concept transition to @while@ loop.
    213207
    214208\subsection{For Loop}
    215 
    216 A for-loop consists of loop control and body.
    217 The loop control is often a 3-tuple: initializers, stopping condition, and advancement.
    218 It is a common practice to declare one or more loop-index variables in initializers, checked these variables for stopping iteration, and updated the variables in advancement.
    219 Such a variable is called an \newterm{index} and is available for reading and writing within the loop body.
    220 (Some languages make the index read-only in the loop body.)
    221 This style of iteration can be written for an enumeration using functions from the @Bounded@ and @Serial@ traits:
    222 \begin{cfa}
    223 enum() E { A, B, C, D };
    224 for ( unsigned int i = 0; i < countof(E); i += 1 ) $\C{// (1)}$
    225         sout | label( fromInt( i ) ) | nonl;
    226 sout | nl;
    227 for ( E e = lowerBound(); ; e = succ(e) ) {     $\C{// (2)}$
    228         sout | label(e) | nonl;
    229   if (e == upperBound()) break;
    230 }
    231 sout | nl;
    232 A B C D
    233 A B C D
    234 \end{cfa}
    235 
    236 A caveat in writing loop control using @pred@ and @succ@ is unintentionally exceeding the range.
    237 \begin{cfa}
    238 for ( E e = upperBound(); e >= lowerBound(); e = pred( e ) ) {}
    239 for ( E e = lowerBound(); e <= upperBound(); e = succ( e ) ) {}
    240 \end{cfa}
    241 Both of these loops look correct but fail because these is an additional bound check within the advancement \emph{before} the conditional test to stop the loop, resulting in a failure at the endpoints of the iteration.
    242 These loops must be restructured by moving the loop test to the end of the loop (@do-while@), as in loop (2) above, which is safe because an enumeration always at least one enumerator.
     209A for loop is constitued by a loop control and a loop body.
     210A loop control is often a 3-tuple, separated by semicolons: initializers, condition, and an expression. It is a common practice to declare
     211a variable, often in initializers, that be used in the condition and updated in the expression or loop body. Such variable is called \newterm{index}.
     212
     213% With implemented @Bounded@ and @Serial@ interface for \CFA enumeration, a @for@ loop that iterates over \CFA enumeration can be implemented as the following:
     214A @for@ loop iterates an enumeration can be written with functions from @Bounded@ and @Serial@ interfaces:
     215\begin{cfa}
     216enum() Chars { A, B, C, D };
     217for( unsigned i = 0; i < countof(E); i++ ) { sout | label(e); }         $\C{// (1) A B C D}$
     218for( Chars e = lowerBound(); ; e = succ(e) ) { $\C{// (2)}$
     219        sout |label((Chars)fromInt(i)) |' ';
     220        if (e == upperBound()) break;
     221}
     222\end{cfa}
     223
     224A caveat in writing loop using finite number as index is that the number can unintentionally be out the range:
     225\begin{cfa}
     226for( unsigned i = countof(Chars) - 1; i >= 0; i-- ) {}                          $\C{// 3}$
     227for( Chars e = lowerBound(); e <= upperBound(); e = succ(e) ) {}        $\C{// 4}$
     228for( Chars e = upperBound(); e >= lowerBound(); e = pred(e) ) {}        $\C{// 5}$
     229\end{cfa}
     230Loop (3) is a value underflow: when @i@ reaches to @0@, decrement statement will still execute and cause
     231the @unsigned int@ value @i@ wraps to @UINT_MAX@, which fails to loop test and the loop cannot terminate.
     232
     233In loop (4) and (5), when @e@ is at the @Bound@ (@upperBound@/@lowerBound@) and @succ@/@pred@ will result in @out of bounded@, \CFA
     234aborts its execution. Therefore, it is necessary to implement the condtional break within the loop body.
    243235
    244236
    245237\subsection{Range Loop}
    246 
    247 Instead of writing the traditional 3-tuple loop control, \CFA supports a \newterm{range loop}.
    248 \begin{cfa}
    249 for ( @E e; A ~= D@ ) { sout | label( e ) | nonl; } sout | nl;
    250 for ( @e; A ~= D@ ) { sout | label( e ) | nonl; } sout | nl;
    251 for ( @E e; A -~= D@ ) { sout | label( e ) | nonl; } sout | nl;
    252 for ( @e; A -~= D ~ 2@ ) { sout | label( e ) | nonl; } sout | nl;
    253 \end{cfa}
    254 Every range loop above has an index declaration and a @range@ bounded by \newterm{left bound} @A@ and \newterm{right bound} @D@.
    255 If the index declaration-type is omitted, the index type is the type of the lower bound (@typeof( A )@).
    256 If a range is joined by @~=@ (range up equal) operator, the index variable is initialized by the left bound and advanced by 1 until it is greater than the right bound.
    257 If a range is joined by @-~=@ (range down equal) operator, the index variable is initialized by the right bound and advanced by -1 until it is less than the left bound.
    258 (Note, functions @pred@ and @succ@ are not used for advancement, so the advancement problem does not occur.)
    259 A range can be suffixed by a positive \newterm{step}, \eg @~ 2@, so advancement is incremented/decremented by step.
    260 
    261 Finally, a shorthand for enumerating over the entire set of enumerators (the most common case) is using the enumeration type for the range.
    262 \begin{cfa}
    263 for ( e; @E@ ) sout | label( e ) | nonl; sout | nl; $\C{// A B C D}$
    264 for ( e; @-~= E@ ) sout | label( e ) | nonl; sout | nl; $\C{// D C B A}$
    265 \end{cfa}
    266 For a \CFA enumeration, the loop enumerates over all enumerators of the enumeration.
    267 For a type matching the @Serial@ trait: the index variable is initialized to @lowerBound@ and loop control checks the index's value for greater than the @upperBound@.
    268 If the range type is not a \CFA enumeration or does not match trait @Serial@, it is compile-time error.
    269 
     238Instead of specifying condition, \CFA supports @range loops@, for which a user specifies a range of values.
     239\begin{cfa}[label=lst:range_functions_2]
     240for ( Chars e; A ~= D ) { sout | label(e); }            $\C{// (6)}$
     241for ( e; A ~= D ) { sout | label(e); }                          $\C{// (7)}$
     242for ( Chars e; A -~= D ) { sout | label(e); }           $\C{// (8) D C B A}$
     243for ( e; A -~=D ~ 2 ) { sout | label(e); }              $\C{// (9) D B }$
     244\end{cfa}
     245Every range loop above has an index declaration, and a @range@ bounded by \newterm{left bound} @A@ and \newterm{right bound} @D@.
     246An index declaration can have an optional type declaration, without which \CFA
     247implicitly declares index variable to be the type of the bounds (@typeof(A)@).
     248If a range is joined by @~=@ (range up equal) operator, the index variable will be initialized by
     249the @left bound@, and change incrementally until its position exceeds @right bound@.
     250On the other hand, if a range is defined with @-~=@ (range down equal) operation, the index variable will
     251have the value of the @right bound@. It change decrementally until its position is less than the @left bound@.
     252A range can be suffixed by an positive integal \newterm{step}, joined by @~@. The loop @(9)@ declares a step size of 2 so that
     253e updates @pred(pred(e))@ in every iteration.
     254
     255\CFA manipulates the position of the index variable and breaks the loop if an index update can result in @out of range@.
     256
     257\CFA provides a shorthand for range loop over a \CFA enumeration or a @Serial@:
     258\begin{cfa}[label=lst:range_functions_2]
     259for ( e; Chars ) { sout | label(e); }           $\C{// (10) A B C D}$
     260for ( e; E ) {                                                          $\C{// forall(E | CfaEnum(E) | Serial(E)) }$
     261    sout | label(e);
     262}
     263\end{cfa}
     264The shorthand syntax has a type name expression (@Char@ and @E@) as its range. If the range expression does not name
     265a \CFA enumeration or a @Serial@, \CFA reports a compile time error. When type name as range is a \CFA enumeration,
     266it turns into a loop that iterates all enumerators of the type. If the type name is a @Serial@, the index variable
     267will be initialized as the @lowerBound@. The loop control checks the index's position against the position of @upperBound@,
     268and terminate the loop when the index has a position greater than the @upperBound@. \CFA does not update the index with
     269@succ@ but manipulate its position directly to avoid @out of bound@.
    270270
    271271\section{Overload Operators}
    272 
    273 \CFA overloads the comparison operators for \CFA enumeration satisfying traits @Serial@ and @CfaEnum@.
    274 These definitions require the operand types be the same and the appropriate comparison is made using the the positions of the operands.
    275 \begin{cfa}
    276 forall( E | CfaEnum( E ) | Serial( E ) ) @{@ $\C{// distribution block}$
     272% Finally, there is an associated trait defining comparison operators among enumerators.
     273\CFA preemptively overloads comparison operators for \CFA enumeration with @Serial@ and @CfaEnum@.
     274They defines that two enumerators are equal only if the are the same enumerator, and compartors are
     275define for order comparison.
     276\begin{cfa}
     277forall( E | CfaEnum( E ) | Serial( E ) ) {
    277278        // comparison
    278279        int ?==?( E l, E r );           $\C{// true if l and r are same enumerators}$
     
    282283        int ?>?( E l, E r );            $\C{// true if l is an enumerator after r}$
    283284        int ?>=?( E l, E r );           $\C{// true if l after or the same as r}$
    284 @}@
    285 \end{cfa}
    286 (Note, all the function prototypes are wrapped in a distribution block, where all qualifiers preceding the block are distributed to each declaration with the block, which eliminated tedious repeated qualification.
    287 Distribution blocks can be nested.)
    288 
    289 \CFA implements a few arithmetic operators for @CfaEnum@.
    290 Unlike advancement functions in @Serial@, these operators perform direct arithmetic, so there is no implicit bound checks.
    291 \begin{cfa}
    292 forall( E | CfaEnum( E ) | Serial( E ) ) { $\C{// distribution block}$
     285}
     286\end{cfa}
     287
     288\CFA implements few arithmetic operators for @CfaEnum@. Unlike update functions in @Serial@, these
     289operator does not have bound checks, which rely on @upperBound@ and @lowerBound@. These operators directly manipulate
     290position, the underlying representation of a \CFA enumeration.
     291\begin{cfa}
     292forall( E | CfaEnum( E ) | Serial( E ) ) {
    293293        // comparison
    294294        E ++?( E & l );
     
    303303\end{cfa}
    304304
    305 Lastly, \CFA does not define @zero_t@ for \CFA enumeration.
    306 Users can define the boolean @false@ for \CFA enumerations on their own, \eg:
    307 \begin{cfa}
    308 forall( E | CfaEnum( E ) )
    309 int ?!=?( E lhs, zero_t ) {
    310         return posn( lhs ) != 0;
    311 }
    312 \end{cfa}
    313 which effectively turns the first enumeration to a logical @false@ and @true@ for the others.
     305Lastly, \CFA does not define @zero_t@ for \CFA enumeration. Users can define the boolean false for
     306\CFA enumerations on their own. Here is an example:
     307\begin{cfa}
     308forall(E | CfaEnum(E))
     309bool ?!=?(E lhs, zero_t) {
     310        return posn(lhs) != 0;
     311}
     312\end{cfa}
     313which effectively turns the first enumeration as a logical false and true for others.
     314
     315% \begin{cfa}
     316% Count variable_a = First, variable_b = Second, variable_c = Third, variable_d = Fourth;
     317% p(variable_a); // 0
     318% p(variable_b); // 1
     319% p(variable_c); // "Third"
     320% p(variable_d); // 3
     321% \end{cfa}
     322
     323
     324% \section{Iteration and Range}
     325
     326
     327
     328% % It is convenient to iterate over a \CFA enumeration value, \eg:
     329% \CFA implements \newterm{range loop} for \CFA enumeration using the enumerated traits. The most basic form of @range loop@ is the follows:
     330% \begin{cfa}[label=lst:range_functions]
     331% for ( Alphabet alph; Alphabet ) { sout | alph; }
     332% >>> A B C ... D
     333% \end{cfa}
     334% % The @range loops@ iterates through all enumerators in the order defined in the enumeration.
     335% % @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
     336% Enumerated @range loop@ extends the \CFA grammar as it allows a type name @Alphabet@
     337
     338% \textbullet\ \CFA offers a shorthand for iterating all enumeration constants:
     339% \begin{cfa}[label=lst:range_functions]
     340% for ( Alphabet alph ) { sout | alph; }
     341% >>> A B C ... D
     342% \end{cfa}
     343
     344% The following are examples for constructing for-control using an enumeration. Note that the type declaration of the iterating variable is optional, because \CFA can infer the type as EnumInstType based on the range expression, and possibly convert it to one of its attribute types.
     345
     346% \textbullet\ H is implicit up-to exclusive range [0, H).
     347% \begin{cfa}[label=lst:range_function_1]
     348% for ( alph; Alphabet.D ) { sout | alph; }
     349% >>> A B C
     350% \end{cfa}
     351
     352% \textbullet\ ~= H is implicit up-to inclusive range [0,H].
     353% \begin{cfa}[label=lst:range_function_2]
     354% for ( alph; ~= Alphabet.D ) { sout | alph; }
     355% >>> A B C D
     356% \end{cfa}
     357
     358% \textbullet\ L ~ H is explicit up-to exclusive range [L,H).
     359% \begin{cfa}[label=lst:range_function_3]
     360% for ( alph; Alphabet.B ~ Alphabet.D  ) { sout | alph; }
     361% // for ( Alphabet alph = Alphabet.B; alph < Alphabet.D; alph += 1  ); 1 is one_t
     362% >>> B C
     363% \end{cfa}
     364
     365% \textbullet\ L ~= H is explicit up-to inclusive range [L,H].
     366% \begin{cfa}[label=lst:range_function_4]
     367% for ( alph; Alphabet.B ~= Alphabet.D  ) { sout | alph; }
     368% >>> B C D
     369% \end{cfa}
     370
     371% \textbullet\ L -~ H is explicit down-to exclusive range [H,L), where L and H are implicitly interchanged to make the range down-to.
     372% \begin{cfa}[label=lst:range_function_5]
     373% for ( alph; Alphabet.D -~ Alphabet.B  ) { sout | alph; }
     374% >>> D C
     375% \end{cfa}
     376
     377% \textbullet\ L -~= H is explicit down-to exclusive range [H,L], where L and H are implicitly interchanged to make the range down-to.
     378% \begin{cfa}[label=lst:range_function_6]
     379% for ( alph; Alphabet.D -~= Alphabet.B  ) { sout | alph; }
     380% >>> D C B
     381% \end{cfa}
     382
     383% A user can specify the ``step size'' of an iteration. There are two different stepping schemes of enumeration for-loop.
     384% \begin{cfa}[label=lst:range_function_stepping]
     385% enum(int) Sequence { A = 10, B = 12, C = 14, D = 16, D  = 18 };
     386% for ( s; Sequence.A ~= Sequence.D ~ 1  ) { sout | alph; }
     387% >>> 10 12 14 16 18
     388% for ( s; Sequence.A ~= Sequence.D; s+=1  ) { sout | alph; }
     389% >>> 10 11 12 13 14 15 16 17 18
     390% \end{cfa}
     391% The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to
     392% \begin{cfa}[label=lst:range_function_stepping_converted]
     393% for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1  ) { sout | alph; }
     394% >>> 10 11 12 13 14 15 16 17 18
     395% \end{cfa}
     396
     397% % \PAB{Explain what each loop does.}
     398
     399% It is also possible to iterate over an enumeration's labels, implicitly or explicitly:
     400% \begin{cfa}[label=lst:range_functions_label_implicit]
     401% for ( char * alph; Alphabet )
     402% \end{cfa}
     403% This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to @ch@ with type @char *@ in this case.
     404% If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration.
     405% \begin{cfa}[label=lst:range_functions_label_implicit]
     406% for ( char * ch; labels( Alphabet ) )
     407% \end{cfa}
  • doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex

    r6abb6dc r2ca7fc2  
    141141One of the key properties of an enumeration is the ability to enumerate (iterate) through the constants, and hence, access their values, if present.
    142142C restricts an enumeration to the integral type @signed int@, while \CC extends enumerations to all integral types, meaning enumeration names must bind to integer constants.
    143 Other modern programming languages provide bindings to any type and additional features to extend enumeration capabilities for better software engineering practices.
    144 
    145 The \CFA (C-for-all) programming language is an evolutionary refinement of the C programming language.
    146 One 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.
     143Other modern programming languages provide bindings to any type and additional features to extend enumeration capabilities for better software-engineering practices.
     144
     145The \CFA (C-for-all) programming language is an evolutionary refinement of the C programing language.
     146One of its distinctive feature is a parametric-polymorphic generic type.
     147However, legacy data types from C, such as enumerations, do not adapt well into 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.
    150150The major contribution is an adaptation of enumerated types with the \CFA type-system in a way that integrates naturally with the generic types.
    151 This thesis also presents several smaller refinements to the \CFA overload resolution rules for enumerated types, each of which improves the intuitive nature of enumeration name resolution by the compiler.
     151This thesis also presents a number of smaller refinement to the \CFA overload resolution rules for enumerated types, each of which improves the intuitive nature of enumeration name resolution by the compiler.
    152152Finally, this work adds other useful features to enumerations that better support software-engineering practices and simplify program development.
    153153
     
    166166Thanks to Gregor Richards and Yzihou Zhang for reading my thesis.
    167167
    168 Special thanks to Andrew James Beach for your insight on the theory development of the thesis.
     168Special thanks to Andrew James Beach for your insight on the theory development on the thesis.
    169169
    170170Thanks to Michael Brooks, Fangran Yu, Colby Parsons, Thierry Delisle, Mubeen Zulifiqar,
    171 and the entire \CFA team for the development of the \CFA language, making it the best language it can be.
     171and the entire \CFA team for development of the \CFA language, making it the best language it can be.
    172172
    173173Finally, a special thank you to Huawei Canada for funding this work.
  • libcfa/src/collections/string.hfa

    r6abb6dc r2ca7fc2  
    1010// Created On       : Fri Sep 03 11:00:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug  6 07:49:52 2024
    13 // Update Count     : 130
     12// Last Modified On : Mon Aug  5 23:06:14 2024
     13// Update Count     : 128
    1414//
    1515
     
    161161string ?+?( char c, const string & s );                                 // add a character to a copy of the string
    162162string ?+?( const string & s, const string & s2 );              // copy and concatenate both strings
    163 string ?+?( const char * s, char c );                                   // add a character to a copy of the string
    164 string ?+?( char c, const char * s );                                   // add a character to a copy of the string
    165 string ?+?( const char * c, const char * s );                   // copy and add with two NULL-terminated string
    166 string ?+?( const char * c, string & s );                               // copy and add with NULL-terminated string
    167 string ?+?( const string & s, const char * c );                 // copy and add with NULL-terminated string
     163string ?+?( const char * s, char c );                                   // copy and concatenate both strings
     164string ?+?( char c, const char * s );                                   // copy and concatenate both strings
     165string ?+?( const char * s1, const char * s2 );                 // copy and concatenate both strings
     166string ?+?( const char * s1, string & s2 );                             // copy and concatenate both strings
     167string ?+?( const string & s, const char * c );                 // copy and concatenate with NULL-terminated string
    168168
    169169static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; }
     
    184184
    185185// Comparisons
    186 int strcmp ( const string &, const string & );
    187 bool ?==?( const string &, const string & );
    188 bool ?!=?( const string &, const string & );
    189 bool ?>? ( const string &, const string & );
    190 bool ?>=?( const string &, const string & );
    191 bool ?<=?( const string &, const string & );
    192 bool ?<? ( const string &, const string & );
    193 
    194 int strcmp( const string &, const char * );
    195 bool ?==?( const string &, const char * );
    196 bool ?!=?( const string &, const char * );
    197 bool ?>? ( const string &, const char * );
    198 bool ?>=?( const string &, const char * );
    199 bool ?<=?( const string &, const char * );
    200 bool ?<? ( const string &, const char * );
    201 
    202 int strcmp( const char *, const string & );
    203 bool ?==?( const char *, const string & );
    204 bool ?!=?( const char *, const string & );
    205 bool ?>? ( const char *, const string & );
    206 bool ?>=?( const char *, const string & );
    207 bool ?<=?( const char *, const string & );
    208 bool ?<? ( const char *, const string & );
     186int strcmp ( const string &, const string &);
     187bool ?==?( const string &, const string &);
     188bool ?!=?( const string &, const string &);
     189bool ?>? ( const string &, const string &);
     190bool ?>=?( const string &, const string &);
     191bool ?<=?( const string &, const string &);
     192bool ?<? ( const string &, const string &);
     193
     194int strcmp( const string &, const char *);
     195bool ?==?( const string &, const char *);
     196bool ?!=?( const string &, const char *);
     197bool ?>? ( const string &, const char *);
     198bool ?>=?( const string &, const char *);
     199bool ?<=?( const string &, const char *);
     200bool ?<? ( const string &, const char *);
     201
     202int strcmp( const char *, const string &);
     203bool ?==?( const char *, const string &);
     204bool ?!=?( const char *, const string &);
     205bool ?>? ( const char *, const string &);
     206bool ?>=?( const char *, const string &);
     207bool ?<=?( const char *, const string &);
     208bool ?<? ( const char *, const string &);
    209209
    210210
  • libcfa/src/enum.cfa

    r6abb6dc r2ca7fc2  
    1111                // It is okay to overflow as overflow will be theoretically caught by the other bound
    1212                if ( i < fromInstance( lower ) || i > fromInstance( upper ) )
    13                         abort( "call to fromInt has index %d outside of enumeration range %d-%d.",
     13                        abort( "call to fromInt has index %d outside enumeration range %d-%d",
    1414                                   i, fromInstance( lower ), fromInstance( upper ) );
    1515                return fromInt_unsafe( i );
     
    1919                E upper = upperBound();
    2020                if ( fromInstance( e ) >= fromInstance( upper ) )
    21                         abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) );
     21                        abort( "call to succ() exceeds enumeration upper bound of %d", fromInstance( upper ) );
    2222                return succ_unsafe(e);
    2323        }
     
    2626                E lower = lowerBound();
    2727                if ( fromInstance( e ) <= fromInstance(lower ) )
    28                         abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) );
     28                        abort( "call to pred() exceeds enumeration lower bound of %d", fromInstance( lower ) );
    2929                return pred_unsafe( e );
    3030        }
    3131
    32         int Countof( E ) {
     32        int Countof( __attribute__((unused)) E e ) {
    3333                E upper = upperBound();
    3434                E lower = lowerBound();
  • libcfa/src/enum.hfa

    r6abb6dc r2ca7fc2  
    1919        E succ( E e );
    2020        E pred( E e );
    21         int Countof( E );
     21        int Countof( E e );
    2222}
    2323
  • libcfa/src/heap.cfa

    r6abb6dc r2ca7fc2  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug  7 10:14:47 2024
    13 // Update Count     : 1646
     12// Last Modified On : Sun Apr  7 21:54:29 2024
     13// Update Count     : 1644
    1414//
    1515
     
    582582                        __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText),
    583583                                                                                "CFA warning (UNIX pid:%ld) : program terminating with %td(%#tx) bytes of storage allocated but not freed.\n"
    584                                                                                 "Possible cause is mismatched allocation/deallocation calls (malloc/free), direct constructor call without a direct destructor call, or system/library routines not freeing storage.\n",
     584                                                                                "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
    585585                                                                                (long int)getpid(), allocUnfreed, allocUnfreed ); // always print the UNIX pid
    586586                } // if
  • src/Validate/ImplementEnumFunc.cpp

    r6abb6dc r2ca7fc2  
    210210                "value",
    211211                {new ast::ObjectDecl(getLocation(), "_i", new ast::EnumInstType(decl))},
    212                 {new ast::ObjectDecl(getLocation(), "_ret", decl->base)});
     212                {new ast::ObjectDecl(getLocation(), "_ret",
     213                                                        ast::deepCopy(decl->base))});
     214        // else
     215        //      return genQuasiValueProto();
    213216}
    214217
Note: See TracChangeset for help on using the changeset viewer.