Ignore:
Timestamp:
Mar 25, 2024, 9:02:18 AM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
051aec4
Parents:
6a8c773
Message:

word smithing and poking at rust enumerations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/intro.tex

    r6a8c773 r41fb996  
    11\chapter{Introduction}
    22
    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.
     3All 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.
    44Constants 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.
    55Hence, each primary constant has a symbolic name referring to its internal representation, and these names are dictated by language syntax related to types.
    66In theory, there are an infinite set of primary names per type.
    77
    8 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.
     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.
    99Many 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.
    1010In 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.)
    1112Because 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{
    1213The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}.
     
    2223to specify as in a list or catalogue.~\cite{OED}
    2324\end{quote}
    24 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are restricted to its secondary names.
     25Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are restricted to hold only the secondary names.
    2526It is possible to enumerate among set names without having an ordering among the set elements.
    2627For example, the week, the weekdays, the weekend, and every second day of the week.
     
    4950\it\color{red}enumeration       & \multicolumn{8}{c}{\it\color{red}enumerators} \\
    5051$\downarrow$\hspace*{25pt}      & \multicolumn{8}{c}{$\downarrow$}                              \\
    51 @enum@ Weekday \{                       & Mon,  & Tue,  & Wed,  & Thu,  & Fri,  & Sat,  & Sun = 42      & \};   \\
     52@enum@ Week \{                          & Mon,  & Tue,  & Wed,  & Thu,  & Fri,  & Sat,  & Sun = 42      & \};   \\
    5253\it\color{red}label                     & Mon   & Tue   & Wed   & Thu   & Fri   & Sat   & Sun           &               \\
    5354\it\color{red}order                     & 0             & 1             & 2             & 3             & 4             & 5             & 6                     &               \\
     
    5556\end{tabular}
    5657\end{cquote}
    57 Here, the enumeration @Weekday@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.
     58Here, the enumeration @Week@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.
    5859The implicit ordering implies the successor of @Tue@ is @Mon@ and the predecessor of @Tue@ is @Wed@, independent of any associated enumerator values.
    5960The value is the constant represented by the secondary name, which can be implicitly or explicitly set.
     
    105106Hence, 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}.
    106107Knowing 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;
     108Occasionally, it is possible to statically determine all regions where each variant type is used, so a tag and runtime checking is unnecessary;
    108109otherwise, 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.
    109110
     
    120121For safety, either all variant types must be listed or a @default@ case must exist with no field accesses.
    121122
    122 To simulate an enumeration with a variant, the tag is re-purposed for either ordering or value and the variant types are omitted.
     123To simulate an enumeration with a variant, the tag is \emph{re-purposed} for either ordering or value and the variant types are omitted.
    123124\begin{cfa}
    124125variant Weekday {
     
    129130};
    130131\end{cfa}
    131 The type system ensures tag setting and testing are correct.
     132The type system ensures tag setting and testing are correctly done.
    132133However, the enumeration operations are limited to the available tag operations, \eg pattern matching.
    133134\begin{cfa}
    134 Weekday weekday = Mon;
    135 if ( @dynamic_cast(Mon)@weekday ) ... // test tag == Mon
     135Week week = Mon;
     136if ( @dynamic_cast(Mon)@week ) ... // test tag == Mon
    136137\end{cfa}
    137138While enumerating among tag names is possible:
     
    143144However, 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.
    144145Iterating using tag ordering and heterogeneous types, also requires pattern matching.
    145 \begin{cfa}
     146\begin{cfa}[morekeywords={match}]
    146147for ( cursor = Mon; cursor <= Fri; cursor = succ( cursor) ) {
    147         switch( cursor ) {
     148        match( cursor ) {
    148149                case Mon { /* access special type for Mon */ }
    149150                ...
    150151                case Fri { /* access special type for Fri */ }
     152                default
    151153        }
    152154}
    153155\end{cfa}
    154 If the variant type adds/removes types or the loop range changes, the pattern matching must be adjusted.
    155 As well, if the start/stop values are dynamic, it is impossible to statically determine if all variant types are listed.
     156If the variant type is changed by adding/removing types or the loop range changes, the pattern matching must be adjusted.
     157As well, if the start/stop values are dynamic, it may be impossible to statically determine if all variant types are listed.
    156158
    157 Forcing the notion of enumerating into variant types is ill formed and confusing.
     159Re-purposing the notion of enumerating into variant types is ill formed and confusing.
    158160Hence, there is only a weak equivalence between an enumeration and variant type, justifying the enumeration type in a programming language.
    159161
     
    163165The 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.
    164166On 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.
     167However, when extended with advanced features, enumerations become complex for both the type system and the runtime implementation.
    166168
    167169\begin{enumerate}
Note: See TracChangeset for help on using the changeset viewer.