- Timestamp:
- May 14, 2024, 6:14:11 PM (6 months ago)
- Branches:
- master
- Children:
- 41c4b5e
- Parents:
- ca4f2b2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/intro.tex
rca4f2b2 rccfbfd9 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@, @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{ 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{ 16 19 The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}. 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}. 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}. 20 25 \begin{quote} 21 26 enumerate (verb, transitive). … … 24 29 to specify as in a list or catalogue.~\cite{OEDenumerate} 25 30 \end{quote} 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. 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. 28 34 For example, the week, the weekdays, the weekend, and every second day of the week. 29 35 \begin{cfa}[morekeywords={in}] … … 33 39 for ( cursor in Mon, Wed, Fri, Sun } ... $\C{// every second day of week}\CRT$ 34 40 \end{cfa} 35 This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms.36 41 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. 37 42 Ordering allows iterating among the enumeration set using relational operators and advancement, \eg: … … 39 44 for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ... 40 45 \end{cfa} 41 Here the internal representation for the secondarynames are logically \emph{generated} rather than listing a subset of names.46 Here the values for the set names are logically \emph{generated} rather than listing a subset of names. 42 47 43 48 Hence, the fundamental aspects of an enumeration are: … … 45 50 \item 46 51 \begin{sloppypar} 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. 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. 50 54 \end{sloppypar} 51 55 \item 52 The secondary names are constants, which follows transitively from their binding (aliasing) to primary names, which areconstants.56 The alias names are constants, which follows transitively from their binding to other constants. 53 57 \item 54 58 Defines a type for generating instants (variables). 55 59 \item 56 For safety, an enumeration instance should be restricted to hold only its type's secondarynames.57 \item 58 There is a mechanism for \emph{enumerating} over the secondarynames, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.60 For safety, an enumeration instance should be restricted to hold only its constant names. 61 \item 62 There is a mechanism for \emph{enumerating} over the enumeration names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically. 59 63 \end{enumerate} 60 64 … … 63 67 \label{s:Terminology} 64 68 65 The term \newterm{enumeration} defines a type with a set of secondary names, and the term \newterm{enumerator} represents an arbitrary secondaryname \see{\VRef{s:CEnumeration} for the name derivation}.69 The term \newterm{enumeration} defines a type with a set of new constants, and the term \newterm{enumerator} represents an arbitrary alias name \see{\VRef{s:CEnumeration} for the name derivation}. 66 70 As well, an enumerated type can have three fundamental properties, \newterm{label}, \newterm{order}, and \newterm{value}. 67 71 \begin{cquote} … … 76 80 \end{tabular} 77 81 \end{cquote} 78 Here, the enumeration @Week@ defines the enumerator labels@Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.82 Here, the enumeration @Week@ defines the enumerator constant @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@. 79 83 The implicit ordering implies the successor of @Tue@ is @Mon@ and the predecessor of @Tue@ is @Wed@, independent of any associated enumerator values. 80 The value is the constant represented by the secondary name, which can be implicitly or explicitly set. 84 The value is the implicitly/explicitly assigned constant to support any enumeration operations; 85 the value may be hidden (opaque) or visible. 81 86 82 87 Specifying complex ordering is possible: … … 94 99 Many programming languages provide an enumeration-like mechanism, which may or may not cover the previous five fundamental enumeration aspects. 95 100 Hence, the term \emph{enumeration} can be confusing and misunderstood. 96 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which featur ingis being used.101 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which feature is being used. 97 102 This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects. 98 103 … … 101 106 \label{s:Aliasing} 102 107 103 Some languages provide simple secondaryaliasing (renaming), \eg:108 Some languages provide simple aliasing (renaming), \eg: 104 109 \begin{cfa} 105 110 const Size = 20, Pi = 3.14159, Name = "Jane"; 106 111 \end{cfa} 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. 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. 109 115 110 116 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. 111 With aliasing, each secondaryname is part of the language, and hence, participates fully, such as name overloading in the type system.117 With aliasing, each new name is part of the language, and hence, participates fully, such as name overloading in the type system. 112 118 Aliasing is not an immutable variable, \eg: 113 119 \begin{cfa} … … 121 127 122 128 Aliasing does provide readability and automatic resubstitution. 123 It also provides simple enumeration properties, but with e xtra effort.129 It also provides simple enumeration properties, but with effort. 124 130 \begin{cfa} 125 131 const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7; … … 148 154 the ADT has three variants (constructors), @A@, @B@, @C@ with associated types @Int@, @Double@, and @S@. 149 155 The constructors create an initialized value of the specific type that is bound to the immutable variables @foo@, @bar@, and @baz@. 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.156 Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to intialize and access the value using dynamic pattern-matching. 151 157 \begin{cquote} 152 158 \setlength{\tabcolsep}{15pt} … … 175 181 \end{tabular} 176 182 \end{cquote} 177 For safety, most languages require all assoc aited types to be listed or a default case with no field accesses.183 For safety, most languages require all associated types to be listed or a default case with no field accesses. 178 184 179 185 A less frequent case is multiple constructors with the same type. … … 188 194 Note, the term \newterm{variant} is often associated with ADTs. 189 195 However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}. 190 In these languages, the variant is often a union using RTTI tags , which cannot be used to simulate an enumeration.196 In these languages, the variant is often a union using RTTI tags for discrimination, which cannot be used to simulate an enumeration. 191 197 Hence, in this work the term variant is not a synonym for ADT. 192 198 … … 208 214 data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show) 209 215 \end{haskell} 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.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. 211 217 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@. 212 218 \VRef[Figure]{f:HaskellEnumeration} shows enumeration comparison and iterating (enumerating). … … 246 252 247 253 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. 248 While the enumeration is constructed using the ADT mechanism, it is so restricted it is not reallyan ADT.254 While the enumeration is constructed using the ADT mechanism, it is so restricted it is not an ADT. 249 255 Furthermore, a general ADT cannot be an enumeration because the constructors generate different values making enumerating meaningless. 250 256 While functional programming languages regularly repurpose the ADT type into an enumeration type, this process seems contrived and confusing.
Note: See TracChangeset
for help on using the changeset viewer.