Changeset 4da9142 for doc/theses
- Timestamp:
- Apr 18, 2024, 10:23:34 PM (8 months ago)
- Branches:
- master
- Children:
- 2b6db03
- Parents:
- c148966
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rc148966 r4da9142 137 137 \section{Pure Enumerators} 138 138 139 An empty enumerator type, @enum()@, implies the enumerators are pure symbols without values but set properties;139 An empty enumerator type, @enum()@, implies the enumerators are opaque symbols without values but set properties; 140 140 hence, there is no default conversion to @int@. 141 141 -
doc/theses/jiada_liang_MMath/background.tex
rc148966 r4da9142 48 48 49 49 \section{C Enumeration} 50 \label{s:CEnumeration} 50 51 51 The C enumeration has the following syntax and semantics. 52 The C enumeration has the following syntax~\cite[\S~6.7.2.2]{C11}. 53 \begin{clang}[identifierstyle=\linespread{0.9}\it] 54 $\it enum$-specifier: 55 enum identifier$\(_{opt}\)$ { enumerator-list } 56 enum identifier$\(_{opt}\)$ { enumerator-list , } 57 enum identifier 58 enumerator-list: 59 enumerator 60 enumerator-list , enumerator 61 enumerator: 62 enumeration-constant 63 enumeration-constant = constant-expression 64 \end{clang} 65 The terms \emph{enumeration} and \emph{enumerator} used in this work \see{\VRef{s:Terminology}} come from the grammar. 66 The C enumeration semantics is discussed using examples. 67 68 An unnamed enumeration is used to provide secondary renaming, like a @const@ declaration in other languages. 69 \begin{clang} 70 enum { Size = 20, Pi = 3.14159 }; // unnamed enumeration $\(\Rightarrow\)$ no ordering 71 \end{clang} 72 This declaration form is not an enumeration even though it is declared using an @enum@ because it has none of the following enumeration properties. 73 74 A \emph{named} enumeration type is an actual enumeration. 52 75 \begin{clang} 53 76 enum Weekday { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun, }; -
doc/theses/jiada_liang_MMath/intro.tex
rc148966 r4da9142 1 1 \chapter{Introduction} 2 2 3 All types in a programming language must have a set of constants, and these constants have \Newterm{primary names}, \eg integral types have constants @-1@, @17@, @12345@, \etc. 4 Constants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types. 3 All types in a programming language must have a set of constants, and these constants have \Newterm{primary names}, \eg integral types have constants @-1@, @17@, @0xff@, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}}, \etc. 4 Con\-stants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types. 5 (In \CFA, the primary constants @0@ and @1@ can be overloaded for any type.) 5 6 Hence, each primary constant has a symbolic name referring to its internal representation, and these names are dictated by language syntax related to types. 6 7 In theory, there are an infinite set of primary names per type. 7 8 8 \Newterm{Secondary naming} is a common practice in mathematics and engineering, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.9 \Newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (mega byte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc. 9 10 Many programming languages capture this important software-engineering capability through a mechanism called \Newterm{constant} or \Newterm{literal} naming, where a secondary name is aliased to a primary name. 10 In some cases, secondary naming is \Newterm{pure}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@. 11 (The names the thing.) 11 Its common purpose is to eliminate duplication of the primary constant throughout a program. 12 For example, the secondary name replaces its primary name, thereafter changing the binding of the secondary to primary name automatically distributes the rebinding throughout the program. 13 In some cases, secondary naming is \Newterm{opaque}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@. 12 14 Because a secondary name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \Newterm{rvalue}\footnote{ 13 15 The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}. … … 18 20 enumerate (verb, transitive). 19 21 To count, ascertain the number of; 20 \emph{more 21 usually, to mention (a number of things or persons) separately, as if for the 22 purpose of counting}; 23 to specify as in a list or catalogue.~\cite{OED} 22 more usually, to mention (a number of things or persons) separately, as if for the purpose of counting; 23 to specify as in a list or catalogue.~\cite{OEDenumerate} 24 24 \end{quote} 25 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are restricted to hold only the secondary names.25 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are \emph{often} restricted to hold only the secondary names. 26 26 It is possible to enumerate among set names without having an ordering among the set elements. 27 27 For example, the week, the weekdays, the weekend, and every second day of the week. … … 29 29 for ( cursor in Mon, Tue, Wed, Thu, Fri, Sat, Sun } ... $\C[3.75in]{// week}$ 30 30 for ( cursor in Mon, Tue, Wed, Thu, Fri } ... $\C{// weekday}$ 31 for ( cursor in Thu, Fri} ... $\C{// weekend}$31 for ( cursor in Sat, Sun } ... $\C{// weekend}$ 32 32 for ( cursor in Mon, Wed, Fri, Sun } ... $\C{// every second day of week}\CRT$ 33 33 \end{cfa} 34 This independence from internal representation allows multiple names to have the same representation (eight note, quaver), giving synonyms.34 This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms. 35 35 A set can have a partial or total ordering, making it possible to compare set elements, \eg Monday is before Friday and Friday is after. 36 36 Ordering allows iterating among the enumeration set using relational operators and advancement, \eg … … 38 38 for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ... 39 39 \end{cfa} 40 Here the internal representations for the secondary names are \emph{generated} rather than listing a subset of names. 40 Here the internal representations for the secondary names are logically \emph{generated} rather than listing a subset of names. 41 42 Hence, the fundamental aspects of an enumeration are: 43 \begin{enumerate} 44 \item 45 It defines a type from which instants can be generated. 46 \item 47 The type lists a finite set of secondary names, which become its primary constants. 48 This differentiates an enumeration from general types with an infinite number of primary constants. 49 \item 50 An enumeration's secondary names represent constants, which follows from their binding (aliasing) to primary names, which are constants. 51 \item 52 For safety, an enumeration instance is restricted to hold only its type's secondary names. 53 \item 54 There is a mechanism for \emph{enumerating} over the secondary names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically. 55 \end{enumerate} 41 56 42 57 43 58 \section{Terminology} 44 45 The term \Newterm{enumeration} defines the set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name. 59 \label{s:Terminology} 60 61 The term \Newterm{enumeration} defines a type with a set of secondary names, and the term \Newterm{enumerator} represents an arbitrary secondary name \see{\VRef{s:CEnumeration}}. 46 62 As well, an enumerated type has three fundamental properties, \Newterm{label}, \Newterm{order}, and \Newterm{value}. 47 63 \begin{cquote} … … 72 88 \section{Motivation} 73 89 74 Some programming languages only provide secondary renaming, which can be simulated by an enumeration without ordering. 75 \begin{cfa} 76 const Size = 20, Pi = 3.14159; 77 enum { Size = 20, Pi = 3.14159 }; // unnamed enumeration $\(\Rightarrow\)$ no ordering 78 \end{cfa} 79 In both cases, it is possible to compare the secondary names, \eg @Size < Pi@, if that is meaningful; 80 however, without an enumeration type-name, it is impossible to create an iterator cursor. 81 82 Secondary renaming can similate an enumeration, but with extra effort. 90 Some programming languages only provide direct secondary renaming. 91 \begin{cfa} 92 const Size = 20, Pi = 3.14159, Name = "Jane"; 93 \end{cfa} 94 Here, it is possible to compare the secondary names, \eg @Size < Pi@, if that is meaningful. 95 96 Secondary renaming can simulate an enumeration, but with extra effort. 83 97 \begin{cfa} 84 98 const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7; 85 99 \end{cfa} 86 Furthermore, reorderingthe enumerators requires manual renumbering.100 Any reordering of the enumerators requires manual renumbering. 87 101 \begin{cfa} 88 102 const Sun = 1, Mon = 2, Tue = 3, Wed = 4, Thu = 5, Fri = 6, Sat = 7; 89 103 \end{cfa} 90 Finally, there is no commontype to create a type-checked instance or iterator cursor.91 Hence, there is only a weak equivalence between secondary naming and enumerations, justifying the enumeration type in a programming language.92 93 A variant (algebraic) type is often promoted as a kind of enumeration, \ie a vari ent type can simulate an enumeration.94 A variant type is a tagged-union, where the possible types may beheterogeneous.95 \begin{cfa} 104 Finally, there is no type to create a type-checked instance or iterator cursor. 105 Hence, there is only a weak equivalence between secondary naming and enumerations, justifying a seperate enumeration type in a programming language. 106 107 A variant (algebraic) type is often promoted as a kind of enumeration, \ie a variant type can simulate an enumeration. 108 Fundamentally, a variant type is a tagged-union, where the tag is normally opaque and the types are usually heterogeneous. 109 \begin{cfa}[morekeywords={variant}] 96 110 @variant@ Variant { 97 111 @int tag;@ // optional/implicit: 0 => int, 1 => double, 2 => S … … 103 117 }; 104 118 \end{cfa} 105 Crucially, the union implies instance storage is shared by all of the variant types. 106 Hence, a variant is dynamically typed, as in a dynamic-typed programming-language, but the set of types is statically bound, similar to some aspects of dynamic gradual-typing~\cite{Gradual Typing}. 107 Knowing which type is in a variant instance is crucial for correctness. 108 Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary; 109 otherwise, a tag is required to denote the particular type in the variant and the tag checked at runtime using some form of type pattern-matching. 110 111 The tag can be implicitly set by the compiler on assignment, or explicitly set by the program\-mer. 112 Type pattern-matching is then used to dynamically test the tag and branch to a section of code to safely manipulate the value, \eg: 119 Crucially, the union implies instance storage is shared by all the variant types, and therefore, before a variant type can be used in a statically-typed expression, it must be dynamically discriminated to its current contained type. 120 Hence, knowing which type is in a variant instance is crucial for correctness. 121 Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary. 122 Otherwise, a tag is required to denote the particular type in the variant, and the tag is discriminated at runtime using some form of type pattern-matching, after which the value can be used in a statically-typed expression. 123 124 A less frequent variant case is multiple variants with the same type, which normally requires explicit naming of the tag to disambiguate among the common types. 125 \begin{cquote} 126 \begin{tabular}{@{}l@{\hspace{30pt}}l@{}} 127 \begin{cfa}[morekeywords={variant}] 128 variant VariantCT { 129 case @car@: int i; // explicitly typed 130 case @boat@: int i; 131 case @bridge@: int i; 132 }; 133 \end{cfa} 134 & 135 \begin{cfa}[morekeywords={variant}] 136 variant VariantCU { 137 case @car@: ; // empty or unit type 138 case @boat@: ; 139 case @bridge@: ; 140 }; 141 \end{cfa} 142 \end{tabular} 143 \end{cquote} 144 Here, the explicit tag name is used to give different meaning to the values in the common @int@ type, \eg the value 3 has different interpretations depending on the tag name. 145 It is even possible to remove the type or use the empty @unit@ type (@struct unit {}@). 146 It is this tag naming that is used to simulate an enumeration. 147 148 Normally, the variant tag is implicitly set by the compiler based on type, but with common types, a tag name is required to resolve type ambiguity. 149 \begin{cfa} 150 Variant v = 3; $\C{// implicitly set tag to 0 based on type of 3}$ 151 VariantCT ve = boats.3; $\C{// explicitly set tag to 1 using tag name}$ 152 \end{cfa} 153 Type pattern-matching is then used to dynamically test the tag and branch to a section of statically-typed code to safely manipulate the value, \eg: 154 \begin{cquote} 155 \begin{tabular}{@{}l@{\hspace{30pt}}l@{}} 113 156 \begin{cfa}[morekeywords={match}] 114 Variant v = 3; // implicitly set tag to 0 115 @match@( v ) { // know the type or test the tag 116 case int { /* only access i field in v */ } 117 case double { /* only access d field in v */ } 118 case S { /* only access s field in v */ } 157 @match@( v ) { // know type implicitly or test tag 158 case int { /* only access i field */ } 159 case double { /* only access d field */ } 160 case S { /* only access s field */ } 119 161 } 120 162 \end{cfa} 121 For safety, either all variant types must be listed or a @default@ case must exist with no field accesses. 122 123 To simulate an enumeration with a variant, the tag is \emph{re-purposed} for either ordering or value and the variant types are omitted. 124 \begin{cfa} 125 variant Weekday { 126 int tag; // implicit 0 => Mon, ..., 6 => Sun 127 @case Mon;@ // no type 163 & 164 \begin{cfa}[morekeywords={match}] 165 @match@( ve ) { 166 case car: int { /* car interpretation */ } 167 case boat: int { /* boat interpretation */ } 168 case bridge: int { /* bridge interpretation */ } 169 } 170 \end{cfa} 171 \end{tabular} 172 \end{cquote} 173 For safety, some languages require all variant types to be listed or a @default@ case with no field accesses. 174 175 To further strengthen the simulate for an enumeration with different values, each variant type can be a @const@ type or the tag becomes non-opaque, possibly taking advantage of the opaque auto-numbering. 176 \begin{cquote} 177 \begin{tabular}{@{}l@{\hspace{30pt}}l@{}} 178 \begin{cfa} 179 variant Week { 180 case Mon: const int = 0; 128 181 ... 129 @case Sun;@ 130 }; 131 \end{cfa} 132 The type system ensures tag setting and testing are correctly done. 133 However, the enumeration operations are limited to the available tag operations, \eg pattern matching. 134 \begin{cfa} 135 Week week = Mon; 136 if ( @dynamic_cast(Mon)@week ) ... // test tag == Mon 137 \end{cfa} 182 case Sat: const int = 5; 183 case Sun: const int = 10; 184 }; 185 \end{cfa} 186 & 187 \begin{cfa} 188 variant Week { 189 case Mon: ; // tag auto-numbering 190 ... 191 case Sat: ; 192 case @Sun = 10@: ; // directly set tag value 193 }; 194 \end{cfa} 195 \end{tabular} 196 \end{cquote} 197 Directly setting the tag implies restrictions, like unique values. 198 In both cases, instances of @Week@ are @const@ (immutable). 199 However, usage between these two types becomes complex. 200 \begin{cfa} 201 Week day = Week.Mon; // sets value or tag depending on type 202 if ( day == Week.Mon ) // dereference value or tag ? 203 \end{cfa} 204 Here, the dereference of @day@ should return the value of the type stored in the variant, never the tag. 205 If it does return the tag, some special meaning must be given to the empty/unit type, especially if a variant contains both regular and unit types. 206 207 208 In general, the enumeration simulation and the variant extensions to support it, are deviating from the normal use of a variant (union) type. 209 As well, the enumeration operations are limited to the available tag operations, \eg pattern matching. 138 210 While enumerating among tag names is possible: 139 211 \begin{cfa}[morekeywords={in}] 140 for ( cursor in Mon, Wed, Fri, Sun ) ... 141 \end{cfa} 142 ordering for iteration would require a \emph{magic} extension, such as a special @enum@ variant, because it has no meaning for a regular variant, \ie @int@ < @double@. 143 144 However, if a special @enum@ variant allows the tags to be heterogeneously typed, ordering must fall back on case positioning, as many types have incomparable values. 145 Iterating using tag ordering and heterogeneous types, also requires pattern matching. 146 \begin{cfa}[morekeywords={match}] 147 for ( cursor = Mon; cursor <= Fri; cursor = succ( cursor) ) { 148 match( cursor ) { 149 case Mon { /* access special type for Mon */ } 150 ... 151 case Fri { /* access special type for Fri */ } 152 default 153 } 154 } 155 \end{cfa} 156 If the variant type is changed by adding/removing types or the loop range changes, the pattern matching must be adjusted. 157 As well, if the start/stop values are dynamic, it may be impossible to statically determine if all variant types are listed. 158 159 Re-purposing the notion of enumerating into variant types is ill formed and confusing. 160 Hence, there is only a weak equivalence between an enumeration and variant type, justifying the enumeration type in a programming language. 212 for ( cursor in Week.Mon, Week.Wed, Week.Fri, Week.Sun ) ... 213 \end{cfa} 214 what is the type of @cursor@? 215 If it the tag type (@int@), how is this value used? 216 If it is the variant type, where is the instance variable, which only contains one value. 217 Hence, either enumerating with a variant enumeration is disallowed or some unusual typing rule must be invented to make it work but only in restricted contexts. 218 219 While functional programming systems regularly re-purposing variant types into enumeration types, this process seems contrived and confusing. 220 A variant tag is not an enumeration, it is a discriminant among a restricted set of types stored in a storage block. 221 Hence, there is only a weak equivalence between an enumeration and variant type, justifying a seperate enumeration type in a programming language. 161 222 162 223 -
doc/theses/jiada_liang_MMath/relatedwork.tex
rc148966 r4da9142 53 53 \lstnewenvironment{ada}[1][]{\lstset{language=[2005]Ada,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},literate={'}{\ttfamily'\!}1}\lstset{#1}}{} 54 54 55 An Ada enumeration type is an ordered list of constants, called \Newterm{literals} (enumerators). 55 An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \Newterm{literals}.\footnote{% 56 Ada 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).} 56 57 \begin{ada} 57 type RGB is ( Red, Green, Blue ); -- 3literals (enumerators)58 type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators) 58 59 \end{ada} 59 60 Object initialization and assignment are restricted to the enumerators of this type. 60 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. 61 To explicitly set enumerator values, \emph{all} enumerators must be set in \emph{ascending} order, \ie there is no auto-initialization. 61 While Ada enumerators are unscoped, like C, Ada enumerators are overloadable. 62 62 \begin{ada} 63 type RGB is ( Red, Green, Blue ); 64 @for RGB use ( Red => 10, Green => 20, Blue => 30 );@ -- ascending order 65 \end{ada} 66 Hence, the position, value, label tuples are: 67 \begin{ada} 68 (0, 10, RED) (1, 20, GREEN) (2, 30, BLUE) 69 \end{ada} 70 Note, Ada is case-\emph{insensitive} so names may appear in multiple forms and still be the same, \eg @Red@ and @RED@ (a questionable design decision). 71 72 Like C, Ada enumerators are unscoped, \ie enumerators declared inside of an enum are visible (projected) into the enclosing scope. 73 The enumeration operators are the ordering operators, @=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of enumerators, which is always ascending. 74 75 Ada enumerators are overloadable. 76 \begin{ada} 63 type RGB is ( @Red@, @Green@, Blue ); 77 64 type Traffic_Light is ( @Red@, Yellow, @Green@ ); 78 65 \end{ada} 79 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded names.66 Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded identifiers. 80 67 \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}. 81 68 … … 102 89 \end{figure} 103 90 104 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package names. 91 Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1. 92 Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization. 93 \begin{ada} 94 type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); 95 for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 ); 96 \end{ada} 97 The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators. 98 99 Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers. 105 100 \begin{ada} 106 101 OtherRed : RGB renames Red; … … 113 108 There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@, 114 109 \begin{cquote} 115 \lstDeleteShortInline@116 110 \setlength{\tabcolsep}{15pt} 117 111 \begin{tabular}{@{}ll@{}} … … 128 122 \end{ada} 129 123 \end{tabular} 130 \lstMakeShortInline@131 124 \end{cquote} 132 125 These attributes are important for IO. … … 138 131 \end{ada} 139 132 which is syntactic sugar for the label and not character literals from the predefined type @Character@. 140 The purpose is strictly readability using character literals rather than names.133 The purpose is strictly readability using character literals rather than identifiers. 141 134 \begin{ada} 142 135 Op : Operator := '+'; … … 171 164 An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a default) or iterating constructs. 172 165 \begin{cquote} 173 \lstDeleteShortInline@174 166 \setlength{\tabcolsep}{15pt} 175 167 \begin{tabular}{@{}ll@{}} … … 211 203 \end{ada} 212 204 \end{tabular} 213 \lstMakeShortInline@214 205 \end{cquote} 215 206 … … 229 220 \CC has the equivalent of Pascal typed @const@ declarations \see{\VRef{s:Pascal}}, with static and dynamic initialization. 230 221 \begin{c++} 231 const auto one = 0 + 1; $\C{// static in tialization}$222 const auto one = 0 + 1; $\C{// static initialization}$ 232 223 const auto NULL = nullptr; 233 224 const auto PI = 3.14159; … … 237 228 Sat = Fri + 1, Sun = Sat + 1; 238 229 int sa[Sun]; 239 const auto r = random(); $\C{// dynamic in tialization}$230 const auto r = random(); $\C{// dynamic initialization}$ 240 231 int da[r]; $\C{// VLA}$ 241 232 \end{c++} … … 362 353 \begin{figure} 363 354 \centering 364 \lstDeleteShortInline@365 355 \begin{tabular}{@{}l|l@{}} 366 356 \multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\ … … 414 404 \end{csharp} 415 405 \end{tabular} 416 \lstMakeShortInline@417 406 \caption{\Csharp: Free Routine Versus Class Enumeration} 418 407 \label{CsharpFreeVersusClass} … … 429 418 const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$ 430 419 \end{Go} 431 Constant names are unscoped and must be unique (no overloading).420 Constant identifiers are unscoped and must be unique (no overloading). 432 421 The first enumerator \emph{must} be explicitly initialized; 433 422 subsequent enumerators can be implicitly or explicitly initialized. … … 459 448 Basic switch and looping are possible. 460 449 \begin{cquote} 461 \lstDeleteShortInline@462 450 \setlength{\tabcolsep}{15pt} 463 451 \begin{tabular}{@{}ll@{}} … … 482 470 \end{Go} 483 471 \end{tabular} 484 \lstMakeShortInline@485 472 \end{cquote} 486 473 However, the loop prints the values from 0 to 13 because there is no actual enumeration. … … 513 500 \begin{figure} 514 501 \centering 515 \lstDeleteShortInline@516 502 \begin{tabular}{@{}l|l@{}} 517 503 \multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\ … … 553 539 \end{Java} 554 540 \end{tabular} 555 \lstMakeShortInline@556 541 \caption{Java: Free Routine Versus Class Enumeration} 557 542 \label{f:JavaFreeVersusClass} … … 607 592 \section{Rust} 608 593 \lstnewenvironment{rust}[1][]{\lstset{language=Rust,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{} 594 % https://doc.rust-lang.org/reference/items/enumerations.html 609 595 610 596 Rust provides a scoped enumeration based on variant types. … … 1010 996 1011 997 1012 \section{Python }998 \section{Python 3.13} 1013 999 \lstnewenvironment{python}[1][]{\lstset{language=Python,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{} 1014 1015 A Python enumeration is a set of symbolic names bound to \emph{unique} values. 1016 They are similar to global variables, but offer a more useful @repr()@, grouping, type-safety, and additional features. 1017 Enumerations inherits from the @Enum@ class, \eg: 1018 \begin{python} 1019 class Weekday(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 1020 class RGB(@Enum@): Red = 1; Green = 2; Blue = 3 1021 \end{python} 1022 1023 Depending on the nature of the enum a member's value may or may not be important, but either way that value can be used to get the corresponding member: 1024 \begin{python} 1025 print( repr( Weekday( 3 ) ) ) 1026 <Weekday.Wed: 3> 1027 \end{python} 1028 As you can see, the @repr()@ of a member shows the enum name, the member name, and the value. 1029 The @str()@ of a member shows only the enum name and member name: 1030 \begin{python} 1031 print( str( Weekday.Thu ), Weekday.Thu ) 1032 Weekday.Thu Weekday.Thu 1033 \end{python} 1034 The type of an enumeration member is the enum it belongs to: 1035 \begin{python} 1036 print( type( Weekday.Thu ) ) 1037 <enum 'Weekday'> 1038 print( isinstance(Weekday.Fri, Weekday) ) 1039 True 1040 \end{python} 1041 Enum members have an attribute that contains just their name: 1042 \begin{python} 1043 print(Weekday.TUESDAY.name) 1044 TUESDAY 1045 \end{python} 1046 Likewise, they have an attribute for their value: 1047 \begin{python} 1048 Weekday.WEDNESDAY.value 1049 3 1050 \end{python} 1051 1052 Unlike many languages that treat enumerations solely as name/value pairs, Python @Enum@s can have behavior added. 1053 For example, @datetime.date@ has two methods for returning the weekday: @weekday()@ and @isoweekday()@. 1054 The difference is that one of them counts from 0-6 and the other from 1-7. 1055 Rather than keep track of that ourselves we can add a method to the @Weekday@ enum to extract the day from the date instance and return the matching enum member: 1056 \begin{python} 1057 class Weekday(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = 15; Sat = 16; Sun = 17 1058 $@$classmethod 1059 def from_date(cls, date): 1060 return cls(date.isoweekday()) 1061 \end{python} 1062 Now we can find out what today is! Observe: 1063 \begin{python} 1064 >>> from datetime import date 1065 >>> Weekday.from_date(date.today()) 1066 <Weekday.TUESDAY: 2> 1067 \end{python} 1068 Of course, if you're reading this on some other day, you'll see that day instead. 1069 1070 This Weekday enum is great if our variable only needs one day, but what if we need several? Maybe we're writing a function to plot chores during a week, and don't want to use a @list@ -- we could use a different type of @Enum@: 1071 \begin{python} 1072 from enum import Flag 1073 class WeekdayF(@Flag@): Mon = @1@; Tue = @2@; Wed = @4@; Thu = @8@; Fri = @16@; Sat = @32@; Sun = @64@ 1074 \end{python} 1075 We've changed two things: we're inherited from @Flag@, and the values are all powers of 2. 1000 % https://docs.python.org/3/howto/enum.html 1001 1002 Python is a dynamically-typed reflexive programming language with multiple versions, and hence, it is possible to extend existing or build new language features within the language. 1003 As a result, discussing Python enumerations is a moving target, because if a features does not exist, if can often be created with varying levels of complexity. 1004 Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13. 1005 1006 A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values. 1007 An enumeration is not a basic type; 1008 it is a @class@ inheriting from the @Enum@ class, where the enumerators must be explicitly initialized, \eg: 1009 \begin{python} 1010 class Week(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 1011 \end{python} 1012 and/or explicitly auto initialized, \eg: 1013 \begin{python} 1014 class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@ 1015 \end{python} 1016 where @auto@ increments by 1 from the previous enumerator value. 1017 Object initialization and assignment are restricted to the enumerators of this type. 1018 An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee. 1019 \begin{python} 1020 class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @10@; Sat = @10@; Sun = @10@ 1021 \end{python} 1022 Here, the enumeration has only 4 enumerators and 3 aliases. 1023 An alias is only visible by dropping down to the @class@ level and asking for class members. 1024 @Enum@ only supports equality comparison between enumerator values; 1025 the extended class @OrderedEnum@ adds relational operators @<@, @<=@, @>@, and @>=@. 1026 1027 There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position). 1028 \begin{cquote} 1029 \setlength{\tabcolsep}{15pt} 1030 \begin{tabular}{@{}ll@{}} 1031 \begin{python} 1032 Week.Thu.value == 10; 1033 Week.Thu.name == 'Thu'; 1034 \end{python} 1035 & 1036 \begin{python} 1037 Week( 10 ) == Thu 1038 Week['Thu'].value = 10 1039 \end{python} 1040 \end{tabular} 1041 \end{cquote} 1042 1043 As an enumeration is a \lstinline[language=python]{class}, its own methods. 1044 \begin{python} 1045 class Week(Enum): 1046 Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 1047 $\\@$classmethod 1048 def today(cls, date): 1049 return cls(date.isoweekday()) 1050 print( "today:", Week.today(date.today())) 1051 today: Week.Mon 1052 \end{python} 1053 The method @today@ retrieves the day of the week and uses it as an index to print out the corresponding label of @Week@. 1076 1054 1077 1055 @Flag@ allows combining several members into a single variable: 1078 1056 \begin{python} 1079 print( repr(Week dayF.Sat | WeekdayF.Sun) )1080 <Week dayF.Sun|Sat: 96>1057 print( repr(WeekF.Sat | WeekF.Sun) ) 1058 <WeekF.Sun|Sat: 96> 1081 1059 \end{python} 1082 1060 You can even iterate over a @Flag@ variable: … … 1084 1062 for day in weekend: 1085 1063 print(day) 1086 Week day.SATURDAY1087 Week day.SUNDAY1064 WeekF.Sat 1065 WeekF.Sun 1088 1066 \end{python} 1089 1067 Okay, let's get some chores set up: 1090 1068 \begin{python} 1091 1069 >>> chores_for_ethan = { 1092 ... 'feed the cat': Week day.MONDAY | Weekday.WEDNESDAY | Weekday.FRIDAY,1093 ... 'do the dishes': Week day.TUESDAY | Weekday.THURSDAY,1094 ... 'answer SO questions': Week day.SATURDAY,1070 ... 'feed the cat': Week.MONDAY | Week.WEDNESDAY | Week.FRIDAY, 1071 ... 'do the dishes': Week.TUESDAY | Week.THURSDAY, 1072 ... 'answer SO questions': Week.SATURDAY, 1095 1073 ... } 1096 1074 \end{python} … … 1101 1079 ... if day in days: 1102 1080 ... print(chore) 1103 >>> show_chores(chores_for_ethan, Week day.SATURDAY)1081 >>> show_chores(chores_for_ethan, Week.SATURDAY) 1104 1082 answer SO questions 1105 1083 \end{python} 1106 In cases where the actual values of the members do not matter, you can save yourself some work and use @auto()@ for the values: 1107 \begin{python} 1108 >>> from enum import auto 1109 >>> class Weekday(Flag): 1110 ... MONDAY = auto() 1111 ... TUESDAY = auto() 1112 ... WEDNESDAY = auto() 1113 ... THURSDAY = auto() 1114 ... FRIDAY = auto() 1115 ... SATURDAY = auto() 1116 ... SUNDAY = auto() 1117 ... WEEKEND = SATURDAY | SUNDAY 1084 Auto incrmenet for @Flag@ is by powers of 2. 1085 \begin{python} 1086 class WeekF(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto(); \ 1087 Sat = auto(); Sun = auto(); Weekend = Sat | Sun 1088 for d in WeekF: 1089 print( f"{d.name}: {d.value}", end=" ") 1090 Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 WeekA.Weekend 1118 1091 \end{python} 1119 1092 … … 1123 1096 @Enum@ allows such access: 1124 1097 \begin{python} 1125 >>> Color(1) 1126 <Color.RED: 1> 1127 >>> Color(3) 1128 <Color.BLUE: 3> 1098 print(RGB(1), RGB(3), ) 1099 RGB.RED RGB.GREEN 1129 1100 \end{python} 1130 1101 If you want to access enum members by name, use item access: 1131 1102 \begin{python} 1132 Color['RED'] 1133 <Color.RED: 1> 1134 1135 Color['GREEN'] 1136 <Color.GREEN: 2> 1103 print( RGBa['RED'], RGBa['GREEN'] ) 1104 RGB.RED RGB.GREEN 1137 1105 \end{python} 1138 1106 If you have an enum member and need its name or value: 1139 1107 \begin{python} 1140 >>> member = Color.RED 1141 >>> member.name 1142 'RED' 1143 >>> member.value 1144 1 1145 \end{python} 1146 1147 \subsection{Duplicating enum members and values} 1148 1149 An enum member can have other names associated with it. 1150 Given two entries @A@ and @B@ with the same value (and @A@ defined first), @B@ is an alias for the member @A@. 1151 By-value lookup of the value of @A@ will return the member @A@. 1152 By-name lookup of @A@ will return the member @A@. 1153 By-name lookup of @B@ will also return the member @A@: 1154 \begin{python} 1155 class Shape(Enum): SQUARE = 2; DIAMOND = 1; CIRCLE = 3; ALIAS_FOR_SQUARE = 2 1156 >>> Shape.SQUARE 1157 <Shape.SQUARE: 2> 1158 >>> Shape.ALIAS_FOR_SQUARE 1159 <Shape.SQUARE: 2> 1160 >>> Shape(2) 1161 <Shape.SQUARE: 2> 1162 \end{python} 1163 1164 Note: Attempting to create a member with the same name as an already defined attribute (another member, a method, etc.) or attempting to create an attribute with the same name as a member is not allowed. 1108 member = RGBa.RED 1109 print( f"{member.name} {member.value}" ) 1110 RED 1 1111 \end{python} 1112 1165 1113 1166 1114 \subsection{Ensuring unique enumeration values} … … 1207 1155 >>> list(Shape) 1208 1156 [<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>] 1209 >>> list(Week day)1210 [<Week day.MONDAY: 1>, <Weekday.TUESDAY: 2>, <Weekday.WEDNESDAY: 4>, <Weekday.THURSDAY: 8>,1211 <Week day.FRIDAY: 16>, <Weekday.SATURDAY: 32>, <Weekday.SUNDAY: 64>]1212 \end{python} 1213 Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week day.WEEKEND@ aren't shown.1157 >>> list(Week) 1158 [<Week.MONDAY: 1>, <Week.TUESDAY: 2>, <Week.WEDNESDAY: 4>, <Week.THURSDAY: 8>, 1159 <Week.FRIDAY: 16>, <Week.SATURDAY: 32>, <Week.SUNDAY: 64>] 1160 \end{python} 1161 Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week.WEEKEND@ aren't shown. 1214 1162 1215 1163 The special attribute @__members__@ is a read-only ordered mapping of names to members. … … 2218 2166 2219 2167 OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage. 2220 The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, pure enumeration. 2221 2222 (I think the value of a ocaml variants are types not object, so I am not sure about this line) 2168 The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration. 2169 2223 2170 OCaml provides a variant (union) type, which is an aggregation of heterogeneous types. 2224 A basic variant is a list of nullary datatype constructors, which is like an unscoped, pure enumeration.2171 A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration. 2225 2172 \begin{ocaml} 2226 2173 type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun … … 2246 2193 type colour = Red | Green of @string@ | Blue of @int * float@ 2247 2194 \end{ocaml} 2248 A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block. 2195 A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block. 2249 2196 @colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@. 2250 2197 (Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.) … … 2259 2206 @Red, abc, 1 1.5@ 2260 2207 \end{ocaml} 2261 2262 2208 2263 2209 A variant type can have a recursive definition. … … 2280 2226 2281 2227 In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty. 2282 Therefore it is not an enumeration, except for the simple pure (nullary) case.2228 Therefore it is not an enumeration, except for the simple opaque (nullary) case. 2283 2229 2284 2230 \begin{comment} … … 2466 2412 With valediction, 2467 2413 - Gregor Richards 2414 2415 2416 Date: Tue, 16 Apr 2024 11:04:51 -0400 2417 Subject: Re: C unnamed enumeration 2418 To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> 2419 CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>, 2420 <f37yu@uwaterloo.ca> 2421 From: Gregor Richards <gregor.richards@uwaterloo.ca> 2422 2423 On 4/16/24 09:55, Peter A. Buhr wrote: 2424 > So what is a variant? Is it a set of tag names, which might be a union or is it 2425 > a union, which might have tag names? 2426 2427 Your tagless variant bears no resemblance to variants in any functional 2428 programming language. A variant is a tag AND a union. You might not need to put 2429 anything in the union, in which case it's a pointless union, but the named tag 2430 is absolutely mandatory. That's the thing that varies. 2431 2432 I was unaware of std::variant. As far as functional languages are concerned, 2433 std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for 2434 the functional language concept, because that term has no other meanings. 2435 2436 An ADT cannot not have a named tag. That's meaningless. The tag is the data 2437 constructor, which is the thing you actually define when you define an ADT. It 2438 is strictly the union that's optional. 2439 2440 With valediction, 2441 - Gregor Richards 2468 2442 \end{comment} 2469 2443 … … 2487 2461 \hline 2488 2462 \hline 2489 pure & & & & & & & & & & & & & \CM \\2463 opaque & & & & & & & & & & & & & \CM \\ 2490 2464 \hline 2491 2465 typed & & & & & & & & & & & @int@ & integral & @T@ \\
Note: See TracChangeset
for help on using the changeset viewer.