Changeset 11cced6
- Timestamp:
- Aug 8, 2024, 6:12:21 PM (3 months ago)
- Branches:
- master
- Children:
- c1c0efdb
- Parents:
- c4aca65
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/relatedwork.tex
rc4aca65 r11cced6 19 19 20 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}. 21 Among these s languages, there area large set of overlapping features, but each language has its own unique extensions and restrictions.21 Among these languages, there is a large set of overlapping features, but each language has its own unique extensions and restrictions. 22 22 23 23 … … 39 39 \end{pascal} 40 40 Object initialization and assignment are restricted to the enumerators of this type. 41 Enumerators are auto-initialized from left to right, starting at zero ,incrementing by 1.41 Enumerators are auto-initialized from left to right, starting at zero and incrementing by 1. 42 42 Enumerators \emph{cannot} be explicitly initialized. 43 43 Pascal provides a predefined type \lstinline[language=Pascal]{Boolean} defined as: … … 66 66 \end{pascal} 67 67 Hence, the ordering of the enumerators is crucial to provide the necessary ranges. 68 There is bidirectional assignment between the enumeration and its subranges.68 There is a bidirectional assignment between the enumeration and its subranges. 69 69 \begin{pascal} 70 70 day := Sat; … … 77 77 day := wend; $\C{\{ no check \}}\CRT$ 78 78 \end{pascal} 79 There should be a static/dynamic range check to verifyvalues assigned to subtypes.79 A static/dynamic range check should be performed to verify the values assigned to subtypes. 80 80 (Free Pascal does not check and aborts in certain situations, like writing an invalid enumerator.) 81 81 … … 125 125 \end{tabular} 126 126 \end{cquote} 127 Note , subtype@Weekday@ and @Weekend@ cannot be used to define a case or loop range.127 Note that subtypes @Weekday@ and @Weekend@ cannot be used to define a case or loop range. 128 128 129 129 An enumeration type can be used as an array dimension and subscript. … … 149 149 \end{pascal} 150 150 151 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.151 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 152 The integral size can be explicitly specified using compiler directive \$@PACKENUM@~$N$, where $N$ is the number of bytes, \eg: 153 153 \begin{pascal} … … 172 172 type Traffic_Light is ( @Red@, Yellow, @Green@ ); 173 173 \end{ada} 174 Like \CFA, Ada uses a type-resolution algorithm including the left-hand side of assignmenteto disambiguate among overloaded identifiers.174 Like \CFA, Ada uses a type-resolution algorithm, including the left-hand side of the assignment, to disambiguate among overloaded identifiers. 175 175 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}. 176 176 … … 203 203 for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 ); 204 204 \end{ada} 205 The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of a csending enumerators.205 The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of ascending enumerators. 206 206 207 207 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers. … … 259 259 if @Flag@ then ... -- conditional 260 260 \end{ada} 261 Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type.261 Since only types derived from @Boolean@ can be conditional, @Boolean@ is essentially a built-in type. 262 262 263 263 Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}. … … 325 325 \label{s:C++RelatedWork} 326 326 327 \CC enumeration is largely backward scompatible with C, so it inherited C's enumerations with some modifications and additions.327 \CC enumeration is largely backward compatible with C, so it inherited C's enumerations with some modifications and additions. 328 328 329 329 \CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization. … … 440 440 % However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values. 441 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 consequ nce, \CC has no meaningful enumerating mechanism.442 impact on its behaviour. As a consequence, \CC has no meaningful enumerating mechanism. 443 443 \begin{c++} 444 444 enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; … … 447 447 \end{c++} 448 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.449 fails, the enumeration value does not assemble auto-initialization. 450 450 451 451 An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript. … … 460 460 % https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums 461 461 462 \Csharp is a dynamically-typed programming -language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.462 \Csharp is a dynamically-typed programming language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}. 463 463 \begin{csharp} 464 464 enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun } 465 465 enum RGB { Red, Green, Blue } 466 466 \end{csharp} 467 The default underlying integral type is @int@ (no @char@), with auto-incrementing ,implicit/explicit initialization.467 The default underlying integral type is @int@ (no @char@), with auto-incrementing and implicit/explicit initialization. 468 468 A method cannot be defined in an enumeration type (extension methods are possible). 469 469 There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts. … … 479 479 % Relational and arithmetic operators are defined in terms of its numeric value only. 480 480 % Therefore, enumerators are not ordered and not enumerable like \CC. 481 Like \CC, \Csharp defines enumeration relational and arith emtic operators in terms of value.482 Enumerators ha sno defined positional meaning.481 Like \CC, \Csharp defines enumeration relational and arithmetic operators in terms of value. 482 Enumerators have no defined positional meaning. 483 483 \begin{csharp} 484 484 day = day++ - 5; $\C{// unsafe}$ … … 555 555 const V = 3.1; const W = 3.1; 556 556 \end{Go} 557 Since these declarations are unmutable variables, they are unscopedand Golang has no overloading.557 Since these declarations are immutable variables, they are unscoped, and Golang has no overloading. 558 558 559 559 Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization. … … 592 592 @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13 593 593 \end{Go} 594 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 in tialization expressions containing \lstinline[language=Go]{iota}.594 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}. 595 595 Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@, 596 596 at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@. … … 640 640 Week day = Week.Sat; 641 641 \end{Java} 642 The enumerator s members are scoped and cannot be made \lstinline[language=java]{public}, hence requirequalification.642 The enumerator's members are scoped and cannot be made \lstinline[language=java]{public}, hence requiring qualification. 643 643 The value of an enumeration instance is restricted to its enumerators. 644 644 … … 649 649 \end{Java} 650 650 Since @day@ has no value, it prints its label (name). 651 The member @valueOf@ is the inverse of @name@ converting a string toenumerator.651 The member @valueOf@ is the inverse of @name@, converting a string to an enumerator. 652 652 \begin{Java} 653 653 day = Week.valueOf( "Wed" ); … … 703 703 Notice enumerators in the @switch@ statement do not require qualification. 704 704 705 There are no arith emtic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.705 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}. 706 706 Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation). 707 707 \begin{Java} … … 801 801 Through 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}. 802 802 When tags represent non-unit types, Rust largely precludes accessing the tag because the semantics become meaningless. 803 Hence, the two mechanisms are largely disjoint, and on y the enumeration component is discussed.804 805 In detail, the @enum@ type has an implicit integer tag (discriminant) ,with a unique value for each variant type.806 Direct initialization is by a compile-time expression generatinga constant value.803 Hence, the two mechanisms are largely disjoint, and only the enumeration component is discussed. 804 805 In detail, the @enum@ type has an implicit integer tag (discriminant) with a unique value for each variant type. 806 Direct initialization is achieved by a compile-time expression that generates a constant value. 807 807 Indirect initialization (without initialization, @Fri@/@Sun@) is auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. 808 808 There is an explicit cast from the tag to integer. … … 966 966 The @enum.allCases@ property returns a collection of all the cases for looping over an enumeration type or variable (expensive operation). 967 967 968 A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with attribute @rawValue@.969 Type @Int@ has auto-incrementing from previous enumerator;968 A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with the attribute @rawValue@. 969 Type @Int@ has auto-incrementing from the previous enumerator; 970 970 type @String@ has auto-incrementing of the enumerator label. 971 971 \begin{cquote} … … 996 996 \end{cquote} 997 997 998 There is a bidirectional conversion from typed enumerator to @rawValue@ and vi se versa.998 There is a bidirectional conversion from typed enumerator to @rawValue@ and vice versa. 999 999 \begin{swift} 1000 1000 var weekInt : WeekInt = WeekInt.Mon; … … 1011 1011 % https://docs.python.org/3/howto/enum.html 1012 1012 1013 Python is a dynamically -typed reflexive programming language with multiple incompatible versions.1014 The generality of the language makes it ispossible to extend existing or build new language features.1015 As a result, discussing Python enumerations is a moving target , because if a featuresdoes not exist, it can often be created with varying levels of complexity within the language.1013 Python is a dynamically typed reflexive programming language with multiple incompatible versions. 1014 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. 1016 1016 Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13. 1017 1017 1018 1018 A Python enumeration is not a basic type; 1019 1019 it is a @class@ inheriting from the @Enum@ class. 1020 The @Enum@ class presents a set of scoped enumerators, where each enumerator is a pair object with a \emph{constant} string name and a rbitrary value.1020 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. 1021 1021 Hence, an enumeration instance is a fixed type (enumeration pair), and its value is the type of one of the enumerator pairs. 1022 1022 … … 1025 1025 class Week(!Enum!): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 1026 1026 \end{python} 1027 and/or explicitly auto 1027 and/or explicitly auto-initialized, \eg: 1028 1028 \begin{python} 1029 1029 class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = !auto()!; Sat = 4; Sun = !auto()! … … 1038 1038 \end{python} 1039 1039 1040 There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because thedynamic typing changes the type.1040 There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because dynamic typing changes the type. 1041 1041 \begin{python} 1042 1042 class RGB(Enum): Red = 1; Green = 2; Blue = 3 … … 1147 1147 class WeekEnd(WeekE): Sat = 6; Sun = 7 1148 1148 \end{python} 1149 Here, type @WeekE@ is an abstract type because thedynamic typing never uses it.1149 Here, type @WeekE@ is an abstract type because dynamic typing never uses it. 1150 1150 \begin{cquote} 1151 1151 \setlength{\tabcolsep}{25pt} … … 1175 1175 @Flag@ is the same as @IntFlag@ but cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@. 1176 1176 Auto increment for @IntFlag@ and @Flag@ is by powers of 2. 1177 Enumerators that are a combinations of single bit enumerators are aliases, andhence, invisible.1177 Enumerators that are combinations of single-bit enumerators are aliases and, hence, invisible. 1178 1178 The following is an example for @Flag@. 1179 1179 \begin{python} … … 1258 1258 \end{cquote} 1259 1259 (Note, after an @adtv@'s type is know, the enumerator is inferred without qualification, \eg @I(3)@.) 1260 The type names are independent from the type value,and mapped to an opaque, ascending, integral tag, starting from 0, supporting relational operators @<@, @<=@, @>@, and @>=@.1260 The type names are independent of the type value and mapped to an opaque, ascending, integral tag, starting from 0, supporting relational operators @<@, @<=@, @>@, and @>=@. 1261 1261 \begin{cquote} 1262 1262 \setlength{\tabcolsep}{10pt} … … 1299 1299 1300 1300 % Enumerating is accomplished by deriving from @enumerate@. 1301 OCaml enumerators ha san ordering following the definition order, but they are not enumerable.1302 To iterate over all enumerators, an OCaml type needs to derive @enumerate@ proprocessor, which appends a list of all enumerators to the program1303 abstract syntax tree (AST). The list of value may not persist the define ordering. Given that it needs tools that is outside of the native language to facilate,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 1304 we claim it has no capability of enumerating. 1305 1305 … … 1533 1533 1534 1534 \VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages with the explaination of categories below. 1535 The features are high 1535 The features are high-level and may not capture nuances within a particular language. 1536 1536 1537 1537 \begin{table} … … 1577 1577 \item Typed: The type of value. H: heterogeneous type; values from the same enum need not be the same type. 1578 1578 U: uni-type; value must have the same type. 1579 \item Safe: A enumeration variable can only hold a value from its defined enumerators.1580 \item Posn ordered: enumerators have defined ordering based on enu emrator declaration order.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 1581 It is implied position ordered if its enumerator value must be strictly increasingly ordered. 1582 \item Unique value: enumerators must have unique value.1583 \item Auto-init: Value are autoinitializable by language specification, often being the "+1" of the predecessor.1584 \item (Un)Scoped: U: unscoped enu emrators and doesnot need qualification; S: Scoped enumerators and requires qualification.1585 \item Overload: An enumerator label can be used without type qualification in a context where the label has defined by multiple enumerations.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 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 a rray dimensionand enumerators can be mapped to an array element (not a conversion to integer type).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 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 enu emrations.1589 \item Superset: Create a new enumeration that contains all enumerators from pre-defined enumerations. 1590 1590 \end{enumerate}
Note: See TracChangeset
for help on using the changeset viewer.