Changeset 38f5006
- Timestamp:
- Feb 16, 2024, 1:27:45 PM (8 months ago)
- Branches:
- master
- Children:
- 7bb516f
- Parents:
- f9da761
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
rf9da761 r38f5006 2 2 \label{s:RelatedWork} 3 3 4 Enumerations exist in many popular programming languages, e.g., Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp, \CC, Go~\cite{Go}, Java~\cite{Java}, Modula-3~\cite{Modula-3}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}, and the algebraic data-type in functional programming.5 There are a large set of overlapping features among these languages, but each language has its own unique restrictions and extensions.6 7 8 \section{ (Free)Pascal}4 Enumerations exist in many popular programming languages, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, \CC, Go~\cite{Go}, Java~\cite{Java}, Modula-3~\cite{Modula-3}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}, and the algebraic data-type in functional programming. 5 There are a large set of overlapping features among these languages, but each language has its own unique extensions and restrictions. 6 7 8 \section{Pascal} 9 9 10 10 \lstnewenvironment{pascal}[1][]{% necessary 11 11 \lstset{ 12 12 language=pascal, 13 escapechar=\$, % LaTeX escape in CFAcode13 escapechar=\$, % LaTeX escape in code 14 14 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 15 15 }% lstset … … 17 17 }{} 18 18 19 Free Pascal is a modern object-oriented version of the classic Pascal programming language. 20 It allows a C-style enumeration type, where enumerators must be in assigned in ascending numerical order with a constant expression and the range can be non-consecutive. 19 Classic Pascal has the \lstinline[language=pascal]{const} declaration binding a name to a constant literal/expression. 20 \begin{pascal} 21 const one = 0 + 1; Vowels = set of (A,E,I,O,U); NULL = NIL; 22 PI = 3.14159; Plus = '+'; Fred = 'Fred'; 23 \end{pascal} 24 The enumerator type is inferred from the constant-expression type. 25 There is no notion of an ordered set, modulo the \lstinline[language=pascal]{set of} type. 26 27 Free Pascal is a modern, object-oriented version of classic Pascal, with a C-style enumeration type. 28 Enumerators must be in assigned in ascending numerical order with a constant expression and the range can be non-consecutive. 21 29 \begin{pascal} 22 30 Type EnumType = ( one, two, three, forty @= 40@, fortyone ); … … 24 32 Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive. 25 33 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. 26 The size underlying integral type can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, e.g.:34 The size underlying integral type can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg: 27 35 \begin{pascal} 28 36 Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three ); … … 37 45 \lstnewenvironment{ada}[1][]{% necessary 38 46 \lstset{ 39 language= ada,40 escapechar=\$, % LaTeX escape in CFAcode47 language=[2005]Ada, 48 escapechar=\$, % LaTeX escape in code 41 49 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 42 50 }% lstset … … 44 52 }{} 45 53 46 An enumeration type is defined as a list of possible values: 47 \begin{ada} 48 type RGB is (Red, Green, Blue); 49 \end{ada} 50 Like for numeric types, where e.g., 1 is an integer literal, @Red@, @Green@ and @Blue@ are called the literals of this type. 51 There are no other values assignable to objects of this type. 52 53 \paragraph{Operators and attributes} ~\newline 54 Apart from equality (@"="@), the only operators on enumeration types are the ordering operators: @"<"@, @"<="@, @"="@, @"/="@, @">="@, @">"@, where the order relation is given implicitly by the sequence of literals: 55 Each literal has a position, starting with 0 for the first, incremented by one for each successor. 56 This position can be queried via the @'Pos@ attribute; the inverse is @'Val@, which returns the corresponding literal. In our example: 57 \begin{ada} 58 RGB'Pos (Red) = 0 59 RGB'Val (0) = Red 60 \end{ada} 61 There are two other important attributes: @Image@ and @Value@. 62 @Image@ returns the string representation of the value (in capital letters), @Value@ is the inverse: 63 \begin{ada} 64 RGB'Image ( Red ) = "RED" 65 RGB'Value ("Red") = Red 66 \end{ada} 67 These attributes are important for simple IO (there are more elaborate IO facilities in @Ada.Text_IO@ for enumeration types). 68 Note that, since Ada is case-insensitive, the string given to @'Value@ can be in any case. 69 70 \paragraph{Enumeration literals} ~\newline 71 Literals are overloadable, i.e. you can have another type with the same literals. 72 \begin{ada} 73 type Traffic_Light is (Red, Yellow, Green); 74 \end{ada} 75 Overload resolution within the context of use of a literal normally resolves which @Red@ is meant. 76 Only if you have an unresolvable overloading conflict, you can qualify with special syntax which @Red@ is meant: 77 \begin{ada} 78 RGB'(Red) 79 \end{ada} 54 An Ada enumeration type is an ordered list of constants, called \Newterm{literals} (enumerators) of the type. 55 \begin{ada} 56 type RGB is ( @Red@, @Green@, Blue ); -- 3 literals (enumerators) 57 \end{ada} 58 No other enumerators are assignable to objects of this type. 59 Enumerators without an explicitly designated constant value are auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by 1. 60 Like C, Ada enumerators are unscoped, \ie enumerators declared inside of an enum are visible (projected) into the enclosing scope. 61 Note, Ada is case-insensitive so names may appear in multiple forms and still be the same name (a questionable design decision). 62 The only operators on enumeration types are the ordering operators, @=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relation is given implicitly by the sequence of enumerators. 63 64 Ada enumerators are overloadable. 65 \begin{ada} 66 type Traffic_Light is ( @Red@, Yellow, @Green@ ); 67 \end{ada} 68 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names. 69 Figure~\VRef{f:AdaEnumeration} shows how ambiguities are handled using a cast, \eg @RGB'(Red)@. 70 71 \begin{figure} 72 \begin{ada} 73 with Ada.Text_IO; use Ada.Text_IO; 74 procedure test is 75 type RGB is ( Red, Green, Blue ); 76 type Traffic_Light is ( Red, Yellow, Green ); 77 procedure @Print@( Colour : RGB ) is begin 78 Put_Line( "Colour is " & RGB'Image( Colour ) ); 79 end Print; 80 procedure @Print@( TL : Traffic_Light ) is begin 81 Put_Line( "Light is " & Traffic_Light'Image( TL ) ); 82 end Print; 83 begin 84 Print( Blue ); $\C[2in]{-- RGB}$ 85 Print( Yellow ); $\C{-- Traffic\_Light}$ 86 Print( @RGB'(Red)@ ); $\C{-- ambiguous without cast}\CRT$ 87 end test; 88 \end{ada} 89 \caption{Ada Enumeration Overload Resolution} 90 \label{f:AdaEnumeration} 91 \end{figure} 92 80 93 Like many other declarative items, enumeration literals can be renamed. 81 94 In fact, such a literal is actually a function, so it has to be renamed as such: … … 86 99 Renaming makes @Red@ directly visible without necessity to resort the use-clause. 87 100 101 There are four enumeration pseudo functions (attributes): @'Pos@, @'Val@, @'Image@, and @'Value@. 102 Function @Pos@ returns the enumerator position; the inverse function @Val@ returns the corresponding enumerator. 103 \begin{ada} 104 RGB'Pos(Red) = 0 -- '=' => equality 105 RGB'Val(0) = Red 106 \end{ada} 107 Funcion @Image@ returns enumerator label (in capital letters); the inverse function @Value@ returns the corresponding enumerator. 108 \begin{ada} 109 RGB'Image( Red ) = "RED" 110 RGB'Value("Red") = Red 111 \end{ada} 112 These attributes are important for simple IO (there are more elaborate IO facilities in @Ada.Text_IO@ for enumeration types). 113 As well, an enumeration type, @T@, has the additional attributes, @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, and @T'Max@, producing an intuitively result based on the attribute name. 114 115 88 116 Note that redeclaration as a function does not affect the staticness of the literal. 89 117 … … 109 137 See Ada Programming/Libraries/Standard. 110 138 111 \paragraph{Booleans as enumeration literals} ~\newline 112 Also Booleans are defined as enumeration types: 113 \begin{ada} 114 type Boolean is (False, True); 115 \end{ada} 116 There is special semantics implied with this declaration in that objects and expressions of this type can be used as conditions. 117 Note that the literals @False@ and @True@ are not Ada keywords. 118 119 Thus it is not sufficient to declare a type with these literals and then hope objects of this type can be used like so: 120 \begin{ada} 121 type My_Boolean is (False, True); 122 Condition: My_Boolean; 123 124 if Condition then -- wrong, won't compile 125 \end{ada} 126 127 If you need your own Booleans (perhaps with special size requirements), you have to derive from the predefined Boolean: 128 \begin{ada} 129 type My_Boolean is new Boolean; 130 Condition: My_Boolean; 131 132 if Condition then -- OK 133 \end{ada} 134 135 \paragraph{Enumeration subtypes} ~\newline 136 You can use range to subtype an enumeration type: 137 \begin{ada} 138 subtype Capital_Letter is Character range 'A' .. 'Z'; 139 type Day_Of_Week is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); 140 subtype Working_Day is Day_Of_Week range Monday .. Friday; 141 \end{ada} 139 The boolean type is defined as an enumeration: 140 \begin{ada} 141 type Boolean is (False, True); -- False / True not keywords 142 @Flag@ : Boolean; 143 \end{ada} 144 which can be used in conditions. 145 \begin{ada} 146 if @Flag@ then ... 147 \end{ada} 148 Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type. 149 150 Ada provides a simple \emph{consecutive} subset (subtype) of an enumeration using \lstinline[language=ada]{range}. 151 \begin{ada} 152 type BW is ( Cyan, Megenta, Yellow, Red, Green, Blue ); 153 type CMY is new BW @range@ Cyan .. Yellow; 154 type RGB is new BW @range@ Red .. Blue; 155 \end{ada} 156 Hence, the ordering of the enumerators is crucial to provide the necessary ranges. 142 157 143 158 \paragraph{Using enumerations} ~\newline … … 175 190 Each of these values has a "position number" corresponding to its position in the list such that @Id1@ has position 0, @Id2@ has position 1, and the Nth value has position N-1. 176 191 177 \paragraph{Attributes of Enumeration Types} ~\newline178 An enumeration type, @T@, has the following attributes: @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, @T'Max@, @T'Image@, @T'Wide_Image@, @T'Value@, @T'Wide_Value@, @T'Pos@, and @T'Val@ (pronounced "T tick first", "T tick last", etc.).179 Most of these are illustrated in the example program given below, and most of them produce what you would intuitively expect based on their names.180 181 @T'Image@ and @T'Value@ form a complementary pair of attributes.182 The former takes a value in @T@ and returns a String representation of that value.183 The latter takes a @String@ that is a representation of a value in @T@ and returns that value.184 185 @T'Pos@ and @T'Val@ form another complementary pair.186 The former takes a value in @T@ and returns its position number.187 The latter takes a position number and returns the corresponding value of type @T@.188 189 192 190 193 \section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE … … 193 196 \lstset{ 194 197 language=[Sharp]C, 195 escapechar=\$, % LaTeX escape in CFAcode198 escapechar=\$, % LaTeX escape in code 196 199 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 197 200 }% lstset … … 320 323 \lstnewenvironment{c++}[1][]{% necessary 321 324 \lstset{ 322 language= C++,323 escapechar=\$, % LaTeX escape in CFAcode325 language=[GNU]C++, 326 escapechar=\$, % LaTeX escape in code 324 327 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 325 328 }% lstset … … 329 332 \CC is backwards compatible with C, so it inherited C's enumerations. 330 333 However, the following non-backwards compatible changes have been made. 331 \begin{quote} 334 335 \begin{cquote} 332 336 7.2 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type. 333 337 In C, objects of enumeration type can be assigned values of any integral type. \\ … … 341 345 \textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\ 342 346 \textbf{How widely used}: Common. 343 \end{quote} 344 \begin{quote} 347 \end{cquote} 348 349 \begin{cquote} 345 350 7.2 Change: In \CC, the type of an enumerator is its enumeration. 346 351 In C, the type of an enumerator is @int@. \\ … … 357 362 \textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken. 358 363 Taking the size of an enumerator is not a common C coding practice. 359 \end{quote} 364 \end{cquote} 365 360 366 Hence, the values in a \CC enumeration can only be its enumerators (without a cast). 361 367 While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@. … … 392 398 393 399 394 \section{Go }400 \section{Golang} 395 401 396 402 \lstnewenvironment{Go}[1][]{% necessary 397 403 \lstset{ 398 404 language=Go, 399 escapechar=\$, % LaTeX escape in CFAcode405 escapechar=\$, % LaTeX escape in code 400 406 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 401 407 }% lstset … … 403 409 }{} 404 410 405 What Is an Enum in Golang? 406 407 An enumerator, or enum, is a data type in Golang that consists of a set of named, constant values. While Golang doesn't support enums, it can be implemented using the identifier iota with constants. 408 409 However, in Golang, they're implemented quite differently than most other programming languages. Golang doesn't support enums directly, however, we can implement it using iota and constants. 410 411 In order to implement enums in Golang, let's first understand what iota is and how it is used. 412 413 414 What Is Iota in Golang? 415 416 iota is an identifier that is used with constant and can simplify constant definitions that use auto-increment numbers. The iota keyword represents integer constant starting from zero. 417 418 The iota keyword represents successive integer constants 0, 1, 2, \ldots. 419 It resets to 0 whenever the word const appears in the source code and increments after each const specification. 411 The Golang enumeration is similar to classical Pascal \lstinline[language=pascal]{const}, where the type of a \lstinline[language=Golang]{const} name is the type of its constant. 412 Constant names are unscoped and must be unique (no overloading). 413 The first enumerator \emph{must} be explicitly initialized; 414 subsequent enumerators can be implicitly or explicitly initialized. 415 Implicit initialization is the previous (left) enumerator value. 420 416 \begin{Go} 421 package main 422 423 import "fmt" 424 425 const ( 426 c0 = iota 427 c1 = iota 428 c2 = iota 429 ) 430 func main() { 431 fmt.Println(c0, c1, c2) //Print : 0 1 2 432 } 417 const ( R = 0; G; B ) $\C{// implicit: 0 0 0}$ 418 const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit: Fred Mary Jane}$ 419 const ( H = 0; Jack = "Jack"; J, K = 3; I ) $\C{// type change, implicit/explicit: 0 Jack Jack 3 3}$ 433 420 \end{Go} 434 You can avoid writing successive iota in front of every constant. This can be simplified as in the below code listing: 421 422 Auto-incrementing is supported by the keyword \lstinline[language=Go]{iota}, available only in the \lstinline[language=Go]{const} declaration. 423 The \lstinline[language=Go]{iota} is a \emph{per \lstinline[language=golang]{const} declaration} integer counter, starting at zero and implicitly incremented by one for each \lstinline[language=golang]{const} identifier. 435 424 \begin{Go} 436 package main 437 438 import "fmt" 439 440 const ( 441 c0 = iota 442 c1 443 c2 444 ) 445 446 func main() { 447 fmt.Println(c0, c1, c2) //Print : 0 1 2 448 } 425 const ( R = @iota@; G; B ) $\C{// implicit: 0 1 2}$ 426 const ( C = @iota + B + 1@; G; Y ) $\C{// implicit: 3 4 5}$ 449 427 \end{Go} 450 To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.428 An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}. 451 429 \begin{Go} 452 package main 453 454 import "fmt" 455 456 const ( 457 c0 = iota + 1 458 c1 459 c2 460 ) 461 462 func main() { 463 fmt.Println(c0, c1, c2) // Print : 1 2 3 464 } 430 const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5 465 431 \end{Go} 466 You can use the blank identifier to skip a value in a list of constants.432 Auto-incrementing stops after an explicit initialization. 467 433 \begin{Go} 468 package main 469 470 import "fmt" 471 472 const ( 473 c1 = iota + 1 474 _ 475 c3 476 c4 477 ) 478 479 func main() { 480 fmt.Println(c1, c3, c4) // Print : 1 3 4 481 } 434 const ( Monday = iota; Tuesday; Wednesday; // 0, 1, 2 435 @Thursday = 10@; Friday; Saturday; Sunday ) // 10, 10, 10, 10 482 436 \end{Go} 483 437 Auto-incrementing can be restarted with an expression containing \emph{one} \lstinline[language=Go]{iota}. 438 \begin{Go} 439 const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@; V5 ) // 0 1 7 3 4 440 const ( Monday = iota; Tuesday; Wednesday; // 0, 1, 2 441 @Thursday = 10;@ Friday = @iota - Wednesday + Thursday - 1@; Saturday; Sunday ) // 10, 11, 12, 13 442 \end{Go} 443 Note, \lstinline[language=Go]{iota} is advanced for the explicitly initialized enumerator, like the underscore @_@ identifier. 484 444 485 445 \section{Java} … … 495 455 \lstset{ 496 456 language=Swift, 497 escapechar=\$, % LaTeX escape in CFAcode457 escapechar=\$, % LaTeX escape in code 498 458 moredelim=**[is][\color{red}]{@}{@}, % red highlighting @...@ 499 459 }% lstset
Note: See TracChangeset
for help on using the changeset viewer.