Changeset 41fb996 for doc/theses/jiada_liang_MMath/intro.tex
- Timestamp:
- Mar 25, 2024, 9:02:18 AM (3 months ago)
- Branches:
- master
- Children:
- 051aec4
- Parents:
- 6a8c773
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/intro.tex
r6a8c773 r41fb996 1 1 \chapter{Introduction} 2 2 3 All types in a programming language must have a set of constants, and these constants have primary names, \eg integral types have constants @-1@, @17@, @12345@, \etc.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 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. 5 5 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 6 In theory, there are an infinite set of primary names per type. 7 7 8 Secondary namingis 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.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 9 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 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 12 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{ 12 13 The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}. … … 22 23 to specify as in a list or catalogue.~\cite{OED} 23 24 \end{quote} 24 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are restricted to itssecondary names.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 26 It is possible to enumerate among set names without having an ordering among the set elements. 26 27 For example, the week, the weekdays, the weekend, and every second day of the week. … … 49 50 \it\color{red}enumeration & \multicolumn{8}{c}{\it\color{red}enumerators} \\ 50 51 $\downarrow$\hspace*{25pt} & \multicolumn{8}{c}{$\downarrow$} \\ 51 @enum@ Week day \{& Mon, & Tue, & Wed, & Thu, & Fri, & Sat, & Sun = 42 & \}; \\52 @enum@ Week \{ & Mon, & Tue, & Wed, & Thu, & Fri, & Sat, & Sun = 42 & \}; \\ 52 53 \it\color{red}label & Mon & Tue & Wed & Thu & Fri & Sat & Sun & \\ 53 54 \it\color{red}order & 0 & 1 & 2 & 3 & 4 & 5 & 6 & \\ … … 55 56 \end{tabular} 56 57 \end{cquote} 57 Here, the enumeration @Week day@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.58 Here, the enumeration @Week@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@. 58 59 The implicit ordering implies the successor of @Tue@ is @Mon@ and the predecessor of @Tue@ is @Wed@, independent of any associated enumerator values. 59 60 The value is the constant represented by the secondary name, which can be implicitly or explicitly set. … … 105 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}. 106 107 Knowing which type is in a variant instance is crucial for correctness. 107 Occasionally, it is possible to statically determine ,all regions where each variant type is used, so a tag and runtime checking is unnecessary;108 Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary; 108 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. 109 110 … … 120 121 For safety, either all variant types must be listed or a @default@ case must exist with no field accesses. 121 122 122 To simulate an enumeration with a variant, the tag is re-purposedfor either ordering or value and the variant types are omitted.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. 123 124 \begin{cfa} 124 125 variant Weekday { … … 129 130 }; 130 131 \end{cfa} 131 The type system ensures tag setting and testing are correct .132 The type system ensures tag setting and testing are correctly done. 132 133 However, the enumeration operations are limited to the available tag operations, \eg pattern matching. 133 134 \begin{cfa} 134 Week day weekday= Mon;135 if ( @dynamic_cast(Mon)@week day) ... // test tag == Mon135 Week week = Mon; 136 if ( @dynamic_cast(Mon)@week ) ... // test tag == Mon 136 137 \end{cfa} 137 138 While enumerating among tag names is possible: … … 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. 144 145 Iterating using tag ordering and heterogeneous types, also requires pattern matching. 145 \begin{cfa} 146 \begin{cfa}[morekeywords={match}] 146 147 for ( cursor = Mon; cursor <= Fri; cursor = succ( cursor) ) { 147 switch( cursor ) {148 match( cursor ) { 148 149 case Mon { /* access special type for Mon */ } 149 150 ... 150 151 case Fri { /* access special type for Fri */ } 152 default 151 153 } 152 154 } 153 155 \end{cfa} 154 If the variant type adds/removestypes or the loop range changes, the pattern matching must be adjusted.155 As well, if the start/stop values are dynamic, it isimpossible to statically determine if all variant types are listed.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. 156 158 157 Forcing the notion of enumerating into variant types is ill formed and confusing.159 Re-purposing the notion of enumerating into variant types is ill formed and confusing. 158 160 Hence, there is only a weak equivalence between an enumeration and variant type, justifying the enumeration type in a programming language. 159 161 … … 163 165 The goal of this work is to to extend the simple and unsafe enumeration type in the C programming-language into a sophisticated and safe type in the \CFA programming-language, while maintain backwards compatibility with C. 164 166 On the surface, enumerations seem like a simple type. 165 However, when extended with advanced features, enumerations become complex for both the type system and the implementation.167 However, when extended with advanced features, enumerations become complex for both the type system and the runtime implementation. 166 168 167 169 \begin{enumerate}
Note: See TracChangeset
for help on using the changeset viewer.