Changes in / [e6f1a4b:31f4837]
- Location:
- doc
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
re6f1a4b r31f4837 45 45 Statically initialized identifiers may appear in any constant-expression context, \eg @case@. 46 46 Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays on the stack. 47 Again, this form of aliasing is not an enumeration.47 Again, this form of aliasing to primary constant is not an enumeration. 48 48 49 49 -
doc/theses/jiada_liang_MMath/intro.tex
re6f1a4b r31f4837 1 1 \chapter{Introduction} 2 2 3 All types in a programming language have a set of constants (symbols), and these constants represent values, \eg integer types have constants @-1@, @17@, @0xff@ representing whole numbers, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@ representing real numbers, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}} representing (human readable) text, \etc. 4 Constants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integer and floating-point types. 5 (In \CFA, the constants @0@ and @1@ can be overloaded for any type.) 6 A constant's symbolic name is dictated by language syntax related to types. 7 In general, the representation of a constant's value is \newterm{opaque}, so the internal representation can be chosen arbitrarily. 8 In theory, there are an infinite set of constant names per type representing an infinite set of values. 9 10 It is common in mathematics, engineering and computer science to alias new constants to existing constants so they have the same value, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), K(k), M, G, T for powers of 2\footnote{Overloaded with SI powers of 10.} often prefixing bits (b) or bytes (B), \eg Gb, MB, and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc. 11 An alias can bind to another alias, which transitively binds it to the specified constant. 12 Multiple aliases can represent the same value, \eg eighth note and quaver, giving synonyms. 13 14 Many programming languages capture this important software-engineering capability through a mechanism called \newterm{constant} or \newterm{literal} naming, where a new constant is aliased to an existing constant. 15 Its purpose is for readability, replacing a constant name that directly represents a value with a name that is more symbolic and meaningful in the context of the program. 16 Thereafter, changing the aliasing of the new constant to another constant automatically distributes the rebinding, preventing errors. 17 % and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@. 18 Because an aliased 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{ 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.) 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. 7 In theory, there are an infinite set of primary constant names per type. 8 9 \newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (megabyte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc. 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. 11 Its purpose is for readability and to eliminate duplication of the primary constant throughout a program. 12 For example, a meaningful secondary name replaces a primary name throughout a program; 13 thereafter, changing the binding of the secondary to primary name automatically distributes the rebinding, preventing errors. 14 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@. 15 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{ 19 16 The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}. 20 In theory, there are an infinite set of possible aliasing, in practice, the number of aliasing per program is finite and small. 21 22 Aliased constants can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc. 23 In this case, the binding between a constant name and value can be implicit, where the values are chosen to support any set operations. 24 Many programming languages capture the aliasing and ordering through a mechanism called an \newterm{enumeration}. 17 18 Secondary names can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc. 19 Many programming languages capture these groupings through a mechanism called an \newterm{enumeration}. 25 20 \begin{quote} 26 21 enumerate (verb, transitive). … … 29 24 to specify as in a list or catalogue.~\cite{OEDenumerate} 30 25 \end{quote} 31 Within an enumeration set, the enumeration names (aliases) must be unique, and instances of an enumerated type are \emph{often} restricted to hold only these names. 32 33 It is possible to enumerate among set names without having an ordering among the set values. 26 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. 27 It is possible to enumerate among set names without having an ordering among the set elements. 34 28 For example, the week, the weekdays, the weekend, and every second day of the week. 35 29 \begin{cfa}[morekeywords={in}] … … 39 33 for ( cursor in Mon, Wed, Fri, Sun } ... $\C{// every second day of week}\CRT$ 40 34 \end{cfa} 35 This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms. 41 36 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. 42 37 Ordering allows iterating among the enumeration set using relational operators and advancement, \eg: … … 44 39 for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ... 45 40 \end{cfa} 46 Here the values for the setnames are logically \emph{generated} rather than listing a subset of names.41 Here the internal representation for the secondary names are logically \emph{generated} rather than listing a subset of names. 47 42 48 43 Hence, the fundamental aspects of an enumeration are: … … 50 45 \item 51 46 \begin{sloppypar} 52 It provides a finite set of new constants, which are implicitly or explicitly assigned values that must be appropriate for any set operations. 53 This aspect differentiates an enumeration from general types with an infinite set of constants. 47 It provides a finite set of secondary names, which become its primary constants. 48 This differentiates an enumeration from general types with an infinite set 49 of primary constants. 54 50 \end{sloppypar} 55 51 \item 56 The alias names are constants, which follows transitively from their binding to otherconstants.52 The secondary names are constants, which follows transitively from their binding (aliasing) to primary names, which are constants. 57 53 \item 58 54 Defines a type for generating instants (variables). 59 55 \item 60 For safety, an enumeration instance should be restricted to hold only its constantnames.61 \item 62 There is a mechanism for \emph{enumerating} over the enumerationnames, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.56 For safety, an enumeration instance should be restricted to hold only its type's secondary names. 57 \item 58 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. 63 59 \end{enumerate} 64 60 … … 67 63 \label{s:Terminology} 68 64 69 The term \newterm{enumeration} defines a type with a set of new constants, and the term \newterm{enumerator} represents an arbitrary aliasname \see{\VRef{s:CEnumeration} for the name derivation}.65 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} for the name derivation}. 70 66 As well, an enumerated type can have three fundamental properties, \newterm{label}, \newterm{order}, and \newterm{value}. 71 67 \begin{cquote} … … 80 76 \end{tabular} 81 77 \end{cquote} 82 Here, the enumeration @Week@ defines the enumerator constant@Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.78 Here, the enumeration @Week@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@. 83 79 The implicit ordering implies the successor of @Tue@ is @Mon@ and the predecessor of @Tue@ is @Wed@, independent of any associated enumerator values. 84 The value is the implicitly/explicitly assigned constant to support any enumeration operations; 85 the value may be hidden (opaque) or visible. 80 The value is the constant represented by the secondary name, which can be implicitly or explicitly set. 86 81 87 82 Specifying complex ordering is possible: … … 99 94 Many programming languages provide an enumeration-like mechanism, which may or may not cover the previous five fundamental enumeration aspects. 100 95 Hence, the term \emph{enumeration} can be confusing and misunderstood. 101 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which featur eis being used.96 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which featuring is being used. 102 97 This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects. 103 98 … … 106 101 \label{s:Aliasing} 107 102 108 Some languages provide simple aliasing (renaming), \eg:103 Some languages provide simple secondary aliasing (renaming), \eg: 109 104 \begin{cfa} 110 105 const Size = 20, Pi = 3.14159, Name = "Jane"; 111 106 \end{cfa} 112 The alias name is logically replaced in the program text by its matching constant. 113 It is possible to compare aliases, if the constants allow it, \eg @Size < Pi@; 114 whereas \eg @Pi < Name@ might be disallowed depending on the language. 107 The secondary name is logically replaced in the program text by its corresponding primary name. 108 Therefore, it is possible to compare the secondary names, \eg @Size < Pi@, only because the primary constants allow it, whereas \eg @Pi < Name@ might be disallowed depending on the language. 115 109 116 110 Aliasing is not macro substitution, \eg @#define Size 20@, where a name is replaced by its value \emph{before} compilation, so the name is invisible to the programming language. 117 With aliasing, each newname is part of the language, and hence, participates fully, such as name overloading in the type system.111 With aliasing, each secondary name is part of the language, and hence, participates fully, such as name overloading in the type system. 118 112 Aliasing is not an immutable variable, \eg: 119 113 \begin{cfa} … … 127 121 128 122 Aliasing does provide readability and automatic resubstitution. 129 It also provides simple enumeration properties, but with e ffort.123 It also provides simple enumeration properties, but with extra effort. 130 124 \begin{cfa} 131 125 const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7; … … 154 148 the ADT has three variants (constructors), @A@, @B@, @C@ with associated types @Int@, @Double@, and @S@. 155 149 The constructors create an initialized value of the specific type that is bound to the immutable variables @foo@, @bar@, and @baz@. 156 Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to intialize andaccess the value using dynamic pattern-matching.150 Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to access the value using dynamic pattern-matching. 157 151 \begin{cquote} 158 152 \setlength{\tabcolsep}{15pt} … … 181 175 \end{tabular} 182 176 \end{cquote} 183 For safety, most languages require all assoc iated types to be listed or a default case with no field accesses.177 For safety, most languages require all assocaited types to be listed or a default case with no field accesses. 184 178 185 179 A less frequent case is multiple constructors with the same type. … … 194 188 Note, the term \newterm{variant} is often associated with ADTs. 195 189 However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}. 196 In these languages, the variant is often a union using RTTI tags for discrimination, which cannot be used to simulate an enumeration.190 In these languages, the variant is often a union using RTTI tags, which cannot be used to simulate an enumeration. 197 191 Hence, in this work the term variant is not a synonym for ADT. 198 192 … … 214 208 data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show) 215 209 \end{haskell} 216 the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other associated types, @Eq@ allows equality comparison, and @Show@ is for printing.210 the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other type, @Eq@ allows equality comparison, and @Show@ is for printing. 217 211 The nullary constructors for the unit types are numbered left-to-right from $0$ to @maxBound@$- 1$, and provides enumerating operations @succ@, @pred@, @enumFrom@ @enumFromTo@. 218 212 \VRef[Figure]{f:HaskellEnumeration} shows enumeration comparison and iterating (enumerating). … … 252 246 253 247 The key observation is the dichotomy between an ADT and enumeration: the ADT uses the associated type resulting in a union-like data structure, and the enumeration does not use the associated type, and hence, is not a union. 254 While the enumeration is constructed using the ADT mechanism, it is so restricted it is not an ADT.248 While the enumeration is constructed using the ADT mechanism, it is so restricted it is not really an ADT. 255 249 Furthermore, a general ADT cannot be an enumeration because the constructors generate different values making enumerating meaningless. 256 250 While functional programming languages regularly repurpose the ADT type into an enumeration type, this process seems contrived and confusing. -
doc/theses/jiada_liang_MMath/relatedwork.tex
re6f1a4b r31f4837 423 423 424 424 \section{Golang} 425 \label{s:Golang}426 425 427 426 Golang has a no enumeration. … … 1069 1068 1070 1069 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. 1071 As a result, discussing Python enumerations is a moving target, because if a features does not exist, i t can often be created with varying levels of complexity within the language.1072 Nevertheless, the following is a discuss of thecore enumeration features that come with Python 3.13.1070 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. 1071 Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13. 1073 1072 1074 1073 A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values. … … 1082 1081 class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@ 1083 1082 \end{python} 1084 where @auto@ increments by 1 from the previous enumerator value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.1083 where @auto@ increments by 1 from the previous enumerator value. 1085 1084 Object initialization and assignment are restricted to the enumerators of this type. 1086 1085 An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee. … … 1109 1108 \end{cquote} 1110 1109 1111 A n enumeration \lstinline[language=python]{class} can havemethods.1110 As an enumeration is a \lstinline[language=python]{class}, its own methods. 1112 1111 \begin{python} 1113 1112 class Week(Enum):
Note: See TracChangeset
for help on using the changeset viewer.