Changeset c1c0efdb
- Timestamp:
- Aug 8, 2024, 10:02:34 PM (5 weeks ago)
- Branches:
- master
- Children:
- 7568e5c
- Parents:
- 11cced6
- git-author:
- Peter A. Buhr <pabuhr@…> (08/08/24 22:01:56)
- git-committer:
- Peter A. Buhr <pabuhr@…> (08/08/24 22:02:34)
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r11cced6 rc1c0efdb 177 177 178 178 179 \section{Auto Initialization}180 181 A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions.182 \begin{cfa}183 enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13184 \end{cfa}185 The complexity of the constant expression depends on the level of computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary.186 187 If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows.188 \begin{cfa}189 enum E(T) { A, B, C };190 \end{cfa}191 \begin{enumerate}192 \item the first enumerator, @A@, is initialized with @T@'s @zero_t@.193 \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@.194 \end{enumerate}195 196 Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.197 It is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler to evaluate complex expressions across multiple builtin and user-defined type.198 Nevertheless, the necessary language concepts exist to support this feature.179 % \section{Auto Initialization} 180 % 181 % A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions. 182 % \begin{cfa} 183 % enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13 184 % \end{cfa} 185 % The complexity of the constant expression depends on the level of computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary. 186 % 187 % If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows. 188 % \begin{cfa} 189 % enum E(T) { A, B, C }; 190 % \end{cfa} 191 % \begin{enumerate} 192 % \item the first enumerator, @A@, is initialized with @T@'s @zero_t@. 193 % \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@. 194 % \end{enumerate} 195 % 196 % Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work. 197 % It is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler to evaluate complex expressions across multiple builtin and user-defined type. 198 % Nevertheless, the necessary language concepts exist to support this feature. 199 199 200 200 … … 263 263 posn( e2 ); $\C[1.75in]{// 0}$ 264 264 enum E3 e3 = e2; $\C{// Assignment with enumeration conversion E2 to E3}$ 265 posn( e2 ); $\C{// 1 }$265 posn( e2 ); $\C{// 1 cost}$ 266 266 void foo( E3 e ); 267 267 foo( e2 ); $\C{// Type compatible with enumeration conversion E2 to E3}$ 268 268 posn( (E3)e2 ); $\C{// Explicit cast with enumeration conversion E2 to E3}$ 269 269 E3 e31 = B; $\C{// No conversion: E3.B}$ 270 posn( e31 ); $\C{// 0 }\CRT$270 posn( e31 ); $\C{// 0 cost}\CRT$ 271 271 \end{cfa} 272 272 The last expression is unambiguous. 273 While both @E2.B@ and @E3.B@ are valid candidates, @E2.B@ has an associated safe cost and @E3.B@ need not a conversion (@zero@ cost). \CFA selects the lowest cost candidate @E3.B@. 273 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). 274 \CFA selects the lowest cost candidate @E3.B@. 274 275 275 276 For the given function prototypes, the following calls are valid. … … 449 450 \footnotetext{C uses symbol \lstinline{'='} for designator initialization, but \CFA changes it to \lstinline{':'} because of problems with tuple syntax.} 450 451 This approach is also necessary for a predefined typed enumeration (unchangeable), when additional secondary-information need to be added. 451 452 452 The 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. 453 453 This behaviour can be reverted by explicit overloading: … … 455 455 float ?[?]( float * arr, E2 index ) { return arr[ value( index ) ]; } 456 456 \end{cfa} 457 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are 458 unambiguous within the context. Designators in H1 are unambiguous becasue @E2@ has a @value@ cost to @int@, which 459 is more expensive than @safe@ cost from C-Enum @E1@ to @int@. On the hand, designators in @H2@ are resolved as @E2@ because 460 when a \CFA enumeration type is being used as an array dimension, \CFA adds the enumeration type to the initializer's resolution context. 457 While enumerator labels @A@, @B@ and @C@ are being defined twice in different enumerations, they are unambiguous within the context. 458 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@. 459 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. 460 461 461 462 462 \section{I/O} 463 463 464 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. 464 465 However, very few programming languages provide a mechanism to read in enumerator values. -
doc/theses/jiada_liang_MMath/Cenum.tex
r11cced6 rc1c0efdb 15 15 enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$ 16 16 \end{cfa} 17 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system -includefiles.17 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include-files. 18 18 19 19 The \CFA type-system allows extensive overloading, including enumerators. 20 20 Hence, most ambiguities among C enumerators are implicitly resolved by the \CFA type system, possibly without any programmer knowledge of the conflict. 21 In addition, C Enum qualification is added, exactly like aggregate field 21 In addition, C Enum qualification is added, exactly like aggregate field-qualification, to disambiguate. 22 22 \VRef[Figure]{f:EnumeratorVisibility} shows how resolution, qualification, and casting are used to disambiguate situations for enumerations @E1@ and @E2@. 23 23 … … 68 68 69 69 \section{Type Safety} 70 \label{s:TypeSafety} 70 71 71 72 As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern. … … 101 102 \item[How widely used:] Common. 102 103 \end{description} 103 104 \begin{comment}105 \begin{description}[parsep=0pt]106 \item[Change:] In \CC, the type of an enumerator is its enumeration.107 In C, the type of an enumerator is @int@.108 Example:109 \begin{cfa}110 enum e { A };111 sizeof(A) == sizeof(int) $\C{// in C}$112 sizeof(A) == sizeof(e) $\C{// in \CC}$113 /* and sizeof(int) is not necessary equal to sizeof(e) */114 \end{cfa}115 \item[Rationale:] In \CC, an enumeration is a distinct type.116 \item[Effect on original feature:] Change to semantics of well-defined feature.117 \item[Difficulty of converting:] Semantic transformation.118 \item[How widely used:] Seldom. The only time this affects existing C code is when the size of an enumerator is119 taken. Taking the size of an enumerator is not a common C coding practice.120 \end{description}121 \end{comment} -
doc/theses/jiada_liang_MMath/conclusion.tex
r11cced6 rc1c0efdb 13 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 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 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 16 These tests ensure future \CFA work does not accidentally break the new enumeration system. 17 17 … … 21 21 \section{Future Work} 22 22 23 The following are ideas to improve and extend the work in this thesis. 24 \begin{enumerate} 25 \item 23 26 There are still corner cases being found in the current \CFA enumeration implementation. 24 Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA , like compile-time constant-expression evaluation.27 Fixing some of these corner cases requires changes to the \CFA resolver or extensions to \CFA. %, like compile-time constant-expression evaluation. 25 28 When these changes are made, it should be straightforward to update the \CFA enumeration implementation to work with them. 26 29 \item 27 30 Currently, some aspects of the enumeration trait system require explicitly including the file @enum.hfa@, which can lead to problems. 28 31 It should be possible to have this file included implicitly by updating the \CFA prelude. 29 32 \item 33 There are multiple \CFA features being developed i 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 30 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). 31 39 Given the existence of this form, it is conceivable to extend it with types other than @int@. … … 42 50 enum( wchar_t * ) { Jack = L"John" }; 43 51 \end{cfa} 44 There are several new features have been proposed or are developing in parallel with enumerations. 45 Two closely related features are iterator and namespace. 46 47 Enumerating features, and range loops in particular, are currently implemented as loops unique to \CFA enumeration and do not align with the 48 general iterator pattern. They can be adapted to the iterator interface when it comes to maturity. 49 50 Currently, \CFA implements a namespace feature for enumerated types only. There is recently a proposal by Andrew to 51 generalize the concept of namespace to other types. The enumeration scope will be revisited to follow the semantics 52 with other types. Also to improve the granularity of scope control, we propose the following extension: 52 \item 53 Currently enumeration scoping is all or nothing. 54 In some cases, it might be useful to increase the scoping granularity to individual enumerators. 53 55 \begin{cfa} 54 56 enum E1 { @!@A, @^@B, C }; 55 57 enum E2 @!@ { @!@A, @^@B, C }; 56 58 \end{cfa} 57 which provides a combination of scoped and unscoped enumerators.59 Here, @'!'@ means the enumerator is scoped, and @'^'@ means the enumerator is unscoped. 58 60 For @E1@, @A@ is scoped; @B@ and @C@ are unscoped. 59 61 For @E2@, @A@, and @C@ are scoped; @B@ is unscoped. 60 Finding a use case is important to justify completing this extension. 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
r11cced6 rc1c0efdb 105 105 Hence, the term \emph{enumeration} can be confusing and misunderstood. 106 106 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which feature is being used. 107 This section discusses some language features that are sometimes called enumeration but do not provide all enumeration aspects.107 This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects. 108 108 109 109 -
doc/theses/jiada_liang_MMath/relatedwork.tex
r11cced6 rc1c0efdb 18 18 \end{comment} 19 19 20 Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell} , Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.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 21 Among these languages, there is a large set of overlapping features, but each language has its own unique extensions and restrictions. 22 22 … … 33 33 The type of each constant name (enumerator) is inferred from the constant-expression type. 34 34 35 Some dialects ofPascal introduced the enumeration type characterized by a set of ordered, unscoped identifiers (enumerators), which are not overloadable.\footnote{%35 Pascal introduced the enumeration type characterized by a set of ordered, unscoped identifiers (enumerators), which are not overloadable.\footnote{% 36 36 Pascal is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).} 37 37 \begin{pascal} … … 56 56 \end{cquote} 57 57 58 Pascal provides \emph{consecutive} sub typing of an enumeration usingsubrange type.58 Pascal provides \emph{consecutive} subsetting of an enumeration using a subrange type. 59 59 \begin{pascal} 60 60 type Week = ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); 61 Weekday = @Mon..Fri@;62 61 Weekday = @Mon..Fri@; { subtype } 62 Weekend = @Sat..Sun@; 63 63 var day : Week; 64 65 64 wday : Weekday; 65 wend : Weekend; 66 66 \end{pascal} 67 67 Hence, the ordering of the enumerators is crucial to provide the necessary ranges. … … 85 85 \begin{tabular}{@{}ll@{}} 86 86 \begin{pascal} 87 day := Mon; 87 88 if @day@ = wday then 88 89 Writeln( day ); 89 90 if @day@ <= Fri then 90 91 Writeln( 'weekday'); 91 92 92 Mon 93 weekday 93 94 \end{pascal} 94 95 & 95 96 \begin{pascal} 97 96 98 case @day@ of 97 99 Mon..Fri : … … 100 102 Writeln( 'weekend') 101 103 end; 104 weekday 102 105 \end{pascal} 103 106 \end{tabular} … … 107 110 \begin{tabular}{@{}ll@{}} 108 111 \begin{pascal} 109 day := Mon; 110 while day <= Sat do begin 112 while day <= Sun do begin 111 113 Write( day, ' ' ); 112 114 day := succ( day ); 113 115 end; 114 Mon Tue Wed Thu Fri Sat 116 Mon Tue Wed Thu Fri Sat Sun 115 117 \end{pascal} 116 118 & 117 119 \begin{pascal} 118 119 for day := Mon to Sat do begin 120 for day := Mon to Sun do begin 120 121 Write( day, ' ' ); 121 122 122 123 end; 123 Mon Tue Wed Thu Fri Sat 124 Mon Tue Wed Thu Fri Sat Sun 124 125 \end{pascal} 125 126 \end{tabular} … … 150 151 151 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. 152 The integral size can be explicitly specified using compiler directive \$@PACKENUM@~$N$, where $N$ is the number of bytes, \eg:153 The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg: 153 154 \begin{pascal} 154 155 Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three ); … … 160 161 161 162 \section{Ada} 163 \label{s:Ada} 162 164 163 165 An Ada enumeration type is a set of ordered, unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{% … … 167 169 \end{ada} 168 170 Object initialization and assignment are restricted to the enumerators of this type. 169 While Ada enumerators are unscoped, Ada enumerators are overloadable.171 While Ada enumerators are unscoped, like C, Ada enumerators are overloadable. 170 172 \begin{ada} 171 173 type RGB is ( @Red@, @Green@, Blue ); … … 173 175 \end{ada} 174 176 Like \CFA, Ada uses a type-resolution algorithm, including the left-hand side of the assignment, to disambiguate among overloaded identifiers. 175 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ ie\lstinline[language=ada]{RGB'(Red)}.177 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \eg \lstinline[language=ada]{RGB'(Red)}. 176 178 177 179 \begin{figure} … … 194 196 \end{ada} 195 197 \caption{Ada Enumeration Overload Resolution} 196 \label{f:AdaEnumeration} 198 \label{f:AdaEnumeration} 197 199 \end{figure} 198 200 … … 201 203 \begin{ada} 202 204 type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); 203 for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );205 for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 ); 204 206 \end{ada} 205 207 The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of ascending enumerators. … … 259 261 if @Flag@ then ... -- conditional 260 262 \end{ada} 261 Since only types derived from @Boolean@ can be conditional, @Boolean@ is essentially a built -in type.262 263 Ada provides \emph{consecutive} sub typing of an enumeration using \lstinline[language=ada]{range}.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}. 264 266 \begin{ada} 265 267 type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); … … 351 353 whereas C @const@ declarations without @static@ are marked @R@. 352 354 353 The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}. 354 \begin{cquote} 355 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type. 356 In C, objects of enumeration type can be assigned values of any integral type. \\ 357 Example: 358 \begin{c++} 359 enum color { red, blue, green }; 360 color c = 1; $\C{// valid C, invalid c++}$ 361 \end{c++} 362 \textbf{Rationale}: The type-safe nature of \CC. \\ 363 \textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\ 364 \textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\ 365 \textbf{How widely used}: Common. 366 \end{cquote} 367 368 \begin{cquote} 369 Change: In \CC, the type of an enumerator is its enumeration. 370 In C, the type of an enumerator is @int@. \\ 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@. 371 359 Example: 372 360 \begin{c++} 373 361 enum e { A }; 374 362 sizeof(A) == sizeof(int) $\C{// in C}$ 375 sizeof(A) == sizeof(e) $\C{// in c++}$363 sizeof(A) == sizeof(e) $\C{// in \CC}$ 376 364 /* and sizeof(int) is not necessary equal to sizeof(e) */ 377 365 \end{c++} 378 \ textbf{Rationale}: In \CC, an enumeration is a distinct type. \\379 \ textbf{Effect on original feature}: Change to semantics of well-defined feature. \\380 \ textbf{Difficulty of converting}: Semantic transformation. \\381 \ textbf{How widely used}:Seldom. The only time this affects existing C code is when the size of an enumerator is taken.366 \item[Rationale:] In \CC, an enumeration is a distinct type. 367 \item[Effect on original feature:] Change to semantics of well-defined feature. 368 \item[Difficulty of converting:] Semantic transformation. 369 \item[How widely used:] Seldom. The only time this affects existing C code is when the size of an enumerator is taken. 382 370 Taking the size of an enumerator is not a common C coding practice. 383 \end{ cquote}371 \end{description} 384 372 Hence, the values in a \CC enumeration can only be its enumerators (without a cast). 373 385 374 While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@. 386 375 \begin{c++} … … 438 427 \end{tabular} 439 428 \end{cquote} 440 % However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values. 441 \CC enumerators are not relational: the only comparator defined for \CC is the value comparison. The define order of enumerator has no 442 impact on its behaviour. As a consequence, \CC has no meaningful enumerating mechanism. 429 However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values. 443 430 \begin{c++} 444 431 enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; … … 446 433 0 1 2 @3 4 5 6 7 8 9@ 10 11 12 13 447 434 \end{c++} 448 An approximation of enumerating an enum in \CC is to use the last enumerator value as a range. But it inevitably 449 fails, the enumeration value does not assemble auto-initialization. 435 As a consequence, there is no meaningful enumerating mechanism. 450 436 451 437 An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript. 452 There is no mechanism to sub typeor inherit from an enumeration.438 There is no mechanism to subset or inherit from an enumeration. 453 439 454 440 … … 465 451 enum RGB { Red, Green, Blue } 466 452 \end{csharp} 467 The default underlying integral type is @int@ (no @char@), with auto-incrementing and implicit/explicit initialization.453 The default underlying integral type is @int@, with auto-incrementing and implicit/explicit initialization. 468 454 A method cannot be defined in an enumeration type (extension methods are possible). 469 455 There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts. … … 482 468 Enumerators have no defined positional meaning. 483 469 \begin{csharp} 484 day = day++ - 5; $\C{// unsafe}$470 day = day++ - 5; $\C{// value manipulation}$ 485 471 day = day & day; 486 472 \end{csharp} 487 488 473 \begin{csharp} 489 474 for ( Week d = Mon; d <= Sun; @d += 1@ ) { … … 492 477 Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun 493 478 \end{csharp} 479 As a consequence, there is no direct meaningful enumerating mechanism. 494 480 495 481 An enumeration can be used in the @if@ and @switch@ statements. … … 519 505 \end{cquote} 520 506 521 As a compliment, \Csharp's Enum library has @Enum.GetValues@pseudo-method that retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).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). 522 508 \begin{csharp} 523 509 foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) { … … 526 512 Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13, 527 513 \end{csharp} 528 Hence, enumerating is not supplied directly by the enumeration, but indirectly through another enumerable type, array.514 Hence, enumerating is not supplied directly by the enumeration, but indirectly through the enumerable array type. 529 515 530 516 An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript. 531 There is no mechanism to sub typeor inherit from an enumeration.517 There is no mechanism to subset or inherit from an enumeration. 532 518 533 519 The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible. … … 543 529 544 530 545 \section{Go lang}546 \label{s:Go lang}547 548 Go langhas a no enumeration.531 \section{Go} 532 \label{s:Go} 533 534 Go has a no enumeration. 549 535 It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression). 550 536 \begin{Go} … … 555 541 const V = 3.1; const W = 3.1; 556 542 \end{Go} 557 Since these declarations are immutable variables, they are unscoped , and Golanghas no overloading.558 559 Go langprovides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.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. 560 546 \begin{Go} 561 547 const ( R = 0; G; B ) $\C{// implicit initialization: 0 0 0}$ … … 584 570 \begin{Go} 585 571 const ( Mon = iota; Tue; Wed; // 0, 1, 2 586 @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6572 @Thu = 10@; Fri; Sat; @Sun = itoa@ ) $\C{// 10, 10, 10, {\color{red}6}}$ 587 573 \end{Go} 588 Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing a smost \emph{one} \lstinline[language=Go]{iota}.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}. 589 575 \begin{Go} 590 576 const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5 591 577 const ( Mon = iota; Tue; Wed; // 0, 1, 2 592 @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13578 @Thu = 10;@ Fri = @iota@ - Wed + Thu - 1; Sat; Sun ) // 10, 11, 12, 13 593 579 \end{Go} 594 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}. … … 635 621 A basic Java enumeration is an opaque enumeration, where the enumerators are constants. 636 622 \begin{Java} 637 enum Week { 638 Mon, Tue, Wed, Thu, Fri, Sat, Sun; 639 } 623 enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun; } 640 624 Week day = Week.Sat; 641 625 \end{Java} … … 643 627 The value of an enumeration instance is restricted to its enumerators. 644 628 645 The position (ordinal) and label are accessible but there is no value.629 The position (ordinal) and label (name) are accessible but there is no value property. 646 630 \begin{Java} 647 631 System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! ); … … 649 633 \end{Java} 650 634 Since @day@ has no value, it prints its label (name). 651 The member @valueOf@ is the inverse of @name@ ,converting a string to an enumerator.635 The member @valueOf@ is the inverse of @name@ converting a string to an enumerator. 652 636 \begin{Java} 653 637 day = Week.valueOf( "Wed" ); … … 839 823 \end{c++} 840 824 An enumeration type cannot declare an array dimension nor as a subscript. 841 There is no mechanism to sub typeor inherit from an enumeration.825 There is no mechanism to subset or inherit from an enumeration. 842 826 843 827 … … 888 872 \end{tabular} 889 873 \end{cquote} 890 (Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.) 874 Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@. 891 875 892 876 An enumeration is created when \emph{all} the enumerators are unit-type, which is like a scoped, opaque enumeration. 893 877 \begin{swift} 894 enum Week { 895 case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type 896 }; 878 enum Week { case Mon, Tue, Wed, Thu, Fri, Sat, Sun }; // unit-type 897 879 var week : Week = @Week.Mon@; 898 880 \end{swift} … … 921 903 An enumeration can have methods. 922 904 \begin{swift} 923 enum Week: Comparable{905 enum Week: @Comparable@ { 924 906 case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type 925 func @isWeekday() -> Bool@ { return self <= .Fri } // method926 func @isWeekend() -> Bool@ { return .Sat <= self } // method907 func @isWeekday() -> Bool@ { return self <= .Fri } // methods 908 func @isWeekend() -> Bool@ { return .Sat <= self } 927 909 }; 928 910 \end{swift} … … 948 930 \end{tabular} 949 931 \end{cquote} 950 951 932 Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values. 952 933 \begin{swift} … … 954 935 case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type 955 936 }; 956 var weeki : Week = Week.Mon;957 if weeki <= .Fri {958 print( "weekday" );959 }960 937 for day in Week@.allCases@ { 961 938 print( day, terminator:" " ) 962 939 } 963 weekday964 940 Mon Tue Wed Thu Fri Sat Sun 965 941 \end{swift} … … 1011 987 % https://docs.python.org/3/howto/enum.html 1012 988 1013 Python is a dynamically 989 Python is a dynamically-typed reflexive programming language with multiple incompatible versions. 1014 990 The generality of the language makes it possible to extend existing or build new language features. 1015 As a result, discussing Python enumerations is a moving target because if a feature does not exist, it can often be created with varying levels of complexity within the language.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. 1016 992 Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13. 1017 993 … … 1030 1006 Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12! 1031 1007 \end{python} 1032 where @auto@ increments by 1 from the previous @auto@ value \see{Go lang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.1008 where @auto@ increments by 1 from the previous @auto@ value \see{Go \lstinline[language=Go]{iota}, \VRef{s:Go}}. 1033 1009 @auto@ is controlled by member @_generate_next_value_()@, which can be overridden: 1034 1010 \begin{python} … … 1096 1072 class Week(!OrderedEnum!): 1097 1073 Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 1098 def !isWeekday(self)!: # method 1074 def !isWeekday(self)!: # methods 1099 1075 return Week(self.value) !<=! Week.Fri 1100 def !isWeekend(self)!: # method1076 def !isWeekend(self)!: 1101 1077 return Week.Sat !<=! Week(self.value) 1102 1078 \end{python} … … 1246 1222 & 1247 1223 \begin{ocaml} 1248 1249 1250 1251 1252 1253 1224 3 1254 1225 3.5 1255 1226 3 5 1227 1228 1229 1230 1231 1256 1232 \end{ocaml} 1257 1233 \end{tabular} … … 1298 1274 As 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. 1299 1275 1300 % Enumerating is accomplished by deriving from @enumerate@. 1301 OCaml enumerators have an ordering following the definition order, but they are not enumerable. 1302 To iterate over all enumerators, an OCaml type needs to derive the @enumerate@ preprocessor, which appends a list of all enumerators to the program 1303 abstract syntax tree (AST). The list of values may not persist in the defined ordering. Given that it needs tools that are outside of the native language to facilitate, 1304 we claim it has no capability of enumerating. 1305 1306 Enumeration subtyping is allowed but inheritance is restricted to classes not types. 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. 1307 1282 \begin{ocaml} 1308 1283 type weekday = Mon | Tue | Wed | Thu | Fri … … 1557 1532 unique values & \CM & \CM & & & & & & \CM & & & & \\ 1558 1533 \hline 1559 auto-init & \CM & @all or none@& \CM & & & \CM & \CM & \CM & \CM & \CM & \CM & \CM \\1534 auto-init & \CM & all or none & \CM & & & \CM & \CM & \CM & \CM & \CM & \CM & \CM \\ 1560 1535 \hline 1561 ( un)Scoped & U & U & S & S & S & U & S & S & S & U & U/S & U/S \\1536 (Un)Scoped & U & U & S & S & S & U & S & S & S & U & U/S & U/S \\ 1562 1537 \hline 1563 1538 overload & & \CM & & & & & & & & & & \CM \\ … … 1574 1549 1575 1550 \begin{enumerate} 1576 \item Opaque: Opaque enums' enumerator cannot be used as its underlying representation or being implemented in terms of ADT. 1577 \item Typed: The type of value. H: heterogeneous type; values from the same enum need not be the same type. 1578 U: uni-type; value must have the same type. 1579 \item Safe: An enumeration variable can only hold a value from its defined enumerators. 1580 \item Posn ordered: enumerators have defined ordering based on enumerator declaration order. 1581 It is implied position ordered if its enumerator value must be strictly increasingly ordered. 1582 \item Unique value: enumerators must have a unique value. 1583 \item Auto-init: Values are auto-initializable by language specification, often being the "+1" of the predecessor. 1584 \item (Un)Scoped: U: unscoped enumerators and did not need qualification; S: Scoped enumerators and requires qualification. 1585 \item Overload: An enumerator label can be used without type qualification in a context where multiple enumerations have defined the label. 1586 \item Loop: Enumerate enum members without the need to convert an enumeration to another data structure 1587 \item Arr. dim: An enumeration can be used directly as an array dimension, and enumerators can be mapped to an array element (not a conversion to integer type). 1588 \item Subset: Name a subset of enumerators as a new type. 1589 \item Superset: Create a new enumeration that contains all enumerators from pre-defined enumerations. 1551 \item opaque: an enumerator cannot be used as its underlying representation or implemented in terms of an ADT. 1552 \item typed: H $\Rightarrow$ heterogeneous, \ie enumerator values may be different types. \\ 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. 1590 1566 \end{enumerate} -
doc/theses/jiada_liang_MMath/uw-ethesis-frontpgs.tex
r11cced6 rc1c0efdb 145 145 The \CFA (C-for-all) programming language is an evolutionary refinement of the C programming language. 146 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 147 However, legacy data types from C, such as enumerations, do not adapt well to the \CFA generic type-system. 148 148 149 149 This thesis extends the simple and unsafe enumeration type in the C programming language into a complex and safe enumeration type in the \CFA programming-language, while maintaining backwards compatibility with C.
Note: See TracChangeset
for help on using the changeset viewer.