Changeset 38f5006 for doc


Ignore:
Timestamp:
Feb 16, 2024, 1:27:45 PM (4 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
7bb516f
Parents:
f9da761
Message:

update related work for Ada and Golang

File:
1 edited

Legend:

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

    rf9da761 r38f5006  
    22\label{s:RelatedWork}
    33
    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}
     4Enumerations 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.
     5There 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}
    99
    1010\lstnewenvironment{pascal}[1][]{% necessary
    1111\lstset{
    1212language=pascal,
    13 escapechar=\$,                          % LaTeX escape in CFA code
     13escapechar=\$,                                                  % LaTeX escape in code
    1414moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    1515}% lstset
     
    1717}{}
    1818
    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.
     19Classic Pascal has the \lstinline[language=pascal]{const} declaration binding a name to a constant literal/expression.
     20\begin{pascal}
     21const one = 0 + 1;   Vowels = set of (A,E,I,O,U);   NULL = NIL;
     22                 PI = 3.14159;   Plus = '+';   Fred = 'Fred';
     23\end{pascal}
     24The enumerator type is inferred from the constant-expression type.
     25There is no notion of an ordered set, modulo the \lstinline[language=pascal]{set of} type.
     26
     27Free Pascal is a modern, object-oriented version of classic Pascal, with a C-style enumeration type.
     28Enumerators must be in assigned in ascending numerical order with a constant expression and the range can be non-consecutive.
    2129\begin{pascal}
    2230Type EnumType = ( one, two, three, forty @= 40@, fortyone );
     
    2432Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive.
    2533The 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.:
     34The size underlying integral type can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
    2735\begin{pascal}
    2836Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
     
    3745\lstnewenvironment{ada}[1][]{% necessary
    3846\lstset{
    39 language=ada,
    40 escapechar=\$,                          % LaTeX escape in CFA code
     47language=[2005]Ada,
     48escapechar=\$,                                                  % LaTeX escape in code
    4149moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    4250}% lstset
     
    4452}{}
    4553
    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}
     54An Ada enumeration type is an ordered list of constants, called \Newterm{literals} (enumerators) of the type.
     55\begin{ada}
     56type RGB is ( @Red@, @Green@, Blue ); -- 3 literals (enumerators)
     57\end{ada}
     58No other enumerators are assignable to objects of this type.
     59Enumerators 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.
     60Like C, Ada enumerators are unscoped, \ie enumerators declared inside of an enum are visible (projected) into the enclosing scope.
     61Note, Ada is case-insensitive so names may appear in multiple forms and still be the same name (a questionable design decision).
     62The only operators on enumeration types are the ordering operators, @=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relation is given implicitly by the sequence of enumerators.
     63
     64Ada enumerators are overloadable.
     65\begin{ada}
     66type Traffic_Light is ( @Red@, Yellow, @Green@ );
     67\end{ada}
     68Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names.
     69Figure~\VRef{f:AdaEnumeration} shows how ambiguities are handled using a cast, \eg @RGB'(Red)@.
     70
     71\begin{figure}
     72\begin{ada}
     73with Ada.Text_IO; use Ada.Text_IO;
     74procedure 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;
     83begin
     84    Print( Blue );                              $\C[2in]{-- RGB}$
     85    Print( Yellow );                    $\C{-- Traffic\_Light}$
     86    Print( @RGB'(Red)@ );               $\C{-- ambiguous without cast}\CRT$
     87end test;
     88\end{ada}
     89\caption{Ada Enumeration Overload Resolution}
     90\label{f:AdaEnumeration}
     91\end{figure}
     92
    8093Like many other declarative items, enumeration literals can be renamed.
    8194In fact, such a literal is actually a function, so it has to be renamed as such:
     
    8699Renaming makes @Red@ directly visible without necessity to resort the use-clause.
    87100
     101There are four enumeration pseudo functions (attributes): @'Pos@, @'Val@, @'Image@, and @'Value@.
     102Function @Pos@ returns the enumerator position; the inverse function @Val@ returns the corresponding enumerator.
     103\begin{ada}
     104RGB'Pos(Red) = 0  -- '=' => equality
     105RGB'Val(0) = Red
     106\end{ada}
     107Funcion @Image@ returns enumerator label (in capital letters); the inverse function @Value@ returns the corresponding enumerator.
     108\begin{ada}
     109RGB'Image( Red ) = "RED"
     110RGB'Value("Red") =  Red
     111\end{ada}
     112These attributes are important for simple IO (there are more elaborate IO facilities in @Ada.Text_IO@ for enumeration types).
     113As 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
    88116Note that redeclaration as a function does not affect the staticness of the literal.
    89117
     
    109137See Ada Programming/Libraries/Standard.
    110138
    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}
     139The boolean type is defined as an enumeration:
     140\begin{ada}
     141type Boolean is (False, True); -- False / True not keywords
     142@Flag@ : Boolean;
     143\end{ada}
     144which can be used in conditions.
     145\begin{ada}
     146if @Flag@ then ...
     147\end{ada}
     148Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially  a builtin type.
     149
     150Ada provides a simple \emph{consecutive} subset (subtype) of an enumeration using \lstinline[language=ada]{range}.
     151\begin{ada}
     152type BW  is ( Cyan, Megenta, Yellow, Red, Green, Blue );
     153type CMY is new BW @range@ Cyan .. Yellow;
     154type RGB is new BW @range@ Red .. Blue;
     155\end{ada}
     156Hence, the ordering of the enumerators is crucial to provide the necessary ranges.
    142157
    143158\paragraph{Using enumerations} ~\newline
     
    175190Each 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.
    176191
    177 \paragraph{Attributes of Enumeration Types} ~\newline
    178 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 
    189192
    190193\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
     
    193196\lstset{
    194197language=[Sharp]C,
    195 escapechar=\$,                          % LaTeX escape in CFA code
     198escapechar=\$,                                                  % LaTeX escape in code
    196199moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    197200}% lstset
     
    320323\lstnewenvironment{c++}[1][]{% necessary
    321324\lstset{
    322 language=C++,
    323 escapechar=\$,                          % LaTeX escape in CFA code
     325language=[GNU]C++,
     326escapechar=\$,                                                  % LaTeX escape in code
    324327moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    325328}% lstset
     
    329332\CC is backwards compatible with C, so it inherited C's enumerations.
    330333However, the following non-backwards compatible changes have been made.
    331 \begin{quote}
     334
     335\begin{cquote}
    3323367.2 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
    333337In C, objects of enumeration type can be assigned values of any integral type. \\
     
    341345\textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
    342346\textbf{How widely used}: Common.
    343 \end{quote}
    344 \begin{quote}
     347\end{cquote}
     348
     349\begin{cquote}
    3453507.2 Change: In \CC, the type of an enumerator is its enumeration.
    346351In C, the type of an enumerator is @int@. \\
     
    357362\textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
    358363Taking the size of an enumerator is not a common C coding practice.
    359 \end{quote}
     364\end{cquote}
     365
    360366Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
    361367While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
     
    392398
    393399
    394 \section{Go}
     400\section{Golang}
    395401
    396402\lstnewenvironment{Go}[1][]{% necessary
    397403\lstset{
    398404language=Go,
    399 escapechar=\$,                          % LaTeX escape in CFA code
     405escapechar=\$,                                                  % LaTeX escape in code
    400406moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    401407}% lstset
     
    403409}{}
    404410
    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.
     411The 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.
     412Constant names are unscoped and must be unique (no overloading).
     413The first enumerator \emph{must} be explicitly initialized;
     414subsequent enumerators can be implicitly or explicitly initialized.
     415Implicit initialization is the previous (left) enumerator value.
    420416\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 }
     417const ( R = 0; G; B )                                   $\C{// implicit: 0 0 0}$
     418const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit: Fred Mary Jane}$
     419const ( H = 0; Jack = "Jack"; J, K = 3; I )     $\C{// type change, implicit/explicit: 0 Jack Jack 3 3}$
    433420\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
     422Auto-incrementing is supported by the keyword \lstinline[language=Go]{iota}, available only in the \lstinline[language=Go]{const} declaration.
     423The \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.
    435424\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 }
     425const ( R = @iota@; G; B )                              $\C{// implicit: 0 1 2}$
     426const ( C = @iota + B + 1@; G; Y )              $\C{// implicit: 3 4 5}$
    449427\end{Go}
    450 To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.
     428An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
    451429\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 }
     430const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5
    465431\end{Go}
    466 You can use the blank identifier to skip a value in a list of constants.
     432Auto-incrementing stops after an explicit initialization.
    467433\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 }
     434const ( Monday = iota; Tuesday; Wednesday; // 0, 1, 2
     435         @Thursday = 10@; Friday; Saturday; Sunday ) // 10, 10, 10, 10
    482436\end{Go}
    483 
     437Auto-incrementing can be restarted with an expression containing \emph{one} \lstinline[language=Go]{iota}.
     438\begin{Go}
     439const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@; V5 ) // 0 1 7 3 4
     440const ( Monday = iota; Tuesday; Wednesday; // 0, 1, 2
     441         @Thursday = 10;@ Friday = @iota - Wednesday + Thursday - 1@; Saturday; Sunday ) // 10, 11, 12, 13
     442\end{Go}
     443Note, \lstinline[language=Go]{iota} is advanced for the explicitly initialized enumerator, like the underscore @_@ identifier.
    484444
    485445\section{Java}
     
    495455\lstset{
    496456language=Swift,
    497 escapechar=\$,                          % LaTeX escape in CFA code
     457escapechar=\$,                                                  % LaTeX escape in code
    498458moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
    499459}% lstset
Note: See TracChangeset for help on using the changeset viewer.