Ignore:
Timestamp:
Jul 29, 2024, 1:32:10 PM (5 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
ce02877
Parents:
5aeb1a9
Message:

update thesis

File:
1 edited

Legend:

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

    r5aeb1a9 r38e20a80  
    66% The following sections detail all of my new contributions to enumerations in \CFA.
    77\CFA extends the enumeration declaration by parameterizing with a type (like a generic type).
    8 \begin{clang}[identifierstyle=\linespread{0.9}\it]
     8
     9
     10\begin{cfa}[caption={CFA Enum},captionpos=b,label={l:CFAEnum}]
    911$\it enum$-specifier:
    1012        enum @(type-specifier$\(_{opt}\)$)@ identifier$\(_{opt}\)$ { cfa-enumerator-list }
     
    1820        $\it inline$ identifier
    1921        enumeration-constant = expression
    20 \end{clang}
     22\end{cfa}
     23
    2124A \newterm{\CFA enumeration}, or \newterm{\CFA enum}, has an optional type declaration in the bracket next to the @enum@ keyword.
    22 Without optional type declarations, the syntax defines "opaque enums".
    23 Otherwise, \CFA enum with type declaration are "typed enums".
     25Without optional type declarations, the syntax defines \newterm{opaque enums}.
     26Otherwise, \CFA enum with type declaration are \newterm{typed enums}.
    2427
    2528\section{Opaque Enum}
     
    2730Opaque enum is a special CFA enumeration type, where the internal representation is chosen by the compiler and hidden from users.
    2831Compared C enum, opaque enums are more restrictive in terms of typing, and cannot be implicitly converted to integers.
    29 Enumerators of opaque enum cannot have initializer. Declaring initializer in the body of opaque enum results in a syntax error.
     32Enumerators of opaque enum cannot have initializer. Declaring initializer in the body of opaque enum results in a compile error.
    3033\begin{cfa}
    3134enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE };
    3235
    3336Planet p = URANUS;
    34 @int i = VENUS; // Error, VENUS cannot be converted into an integral type@
    35 \end{cfa}
    36 Each opage enum has two @attributes@: @position@ and @label@. \CFA auto-generates @attribute functions@ @posn()@ and @label()@ for every \CFA enum to returns the respective attributes.
    37 \begin{cfa}
    38 // Auto-generated
    39 int posn(Planet p);
    40 char * s label(Planet p);
    41 \end{cfa}
     37int i = VENUS; @// Error, VENUS cannot be converted into an integral type
     38\end{cfa}
     39% Each opaque enum has two @attributes@: @position@ and @label@. \CFA auto-generates @attribute functions@ @posn()@ and @label()@ for every \CFA enum to returns the respective attributes.
     40Opaque enumerations have two defining properties: @label@ (name) and @order@ (position), exposed to users by predefined @attribute functions@ , with the following signatures:
     41\begin{cfa}
     42forall( E ) {
     43        unsigned posn(E e);
     44        const char * s label(E e);
     45};
     46\end{cfa}
     47With polymorphic type parameter E being substituted by enumeration types such as @Planet@.
    4248
    4349\begin{cfa}
     
    4652\end{cfa}
    4753
    48 % \subsection{Representation}
    49 \CFA uses chooses signed int as the underlying representation of an opaque enum variable, holding the value of enumeration position. Therefore, @posn()@ is in fact a cast that bypassing type system, converting an
    50 cfa enum to its integral representation.
    51 
    52 Labels information are stored in a global array. @label()@ is a function that maps enum position to an element of the array.
     54\subsection{Representation}
     55The underlying representation of \CFA enumeration object is its order, saved as an integral type. Therefore, the size of a \CFA enumeration is consistent with C enumeration.
     56Attribute function @posn@ performs type substitution on an expression from \CFA type to integral type.
     57Names of enumerators are stored in a global data structure, with @label@ maps \CFA enumeration object to corresponding data.
    5358
    5459\section{Typed Enum}
     
    5661
    5762\CFA extends the enumeration declaration by parameterizing with a type (like a generic type), allowing enumerators to be assigned any values from the declared type.
    58 Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's constants used to set the enumerator constants.
     63Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's values used to set the enumerator constants.
    5964Note, the synonyms @Liz@ and @Beth@ in the last declaration.
    6065Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are logically rewritten with @const@.
     
    7075        enum( @_Complex@ ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i };
    7176// pointer
    72         enum( @const char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
     77        enum( @char *@ ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" };
    7378        int i, j, k;
    7479        enum( @int *@ ) ptr { I = &i,  J = &j,  K = &k };
     
    107112
    108113
    109 \subsection{Implicit Conversion}
     114\subsection{Value Conversion}
    110115C has an implicit type conversion from an enumerator to its base type @int@.
    111 Correspondingly, \CFA has an implicit (safe) conversion from a typed enumerator to its base type.
     116Correspondingly, \CFA has an implicit conversion from a typed enumerator to its base type.
    112117\begin{cfa}
    113118char currency = Dollar;
    114 string fred = Fred;                                             $\C{// implicit conversion from char * to \CFA string type}$
    115 Person student = Beth;
    116 \end{cfa}
    117 
    118 % The implicit conversion is accomplished by the compiler adding @value()@ function calls as a candidate with safe cost. Therefore, the expression
    119 % \begin{cfa}
    120 % char currency = Dollar;
    121 % \end{cfa}
    122 % is equivalent to
    123 % \begin{cfa}
    124 % char currency = value(Dollar);
    125 % \end{cfa}
    126 % Such conversion an @additional@ safe
    127 
    128 The implicit conversion is accomplished by the resolver adding call to @value()@ functions as a resolution candidate with a @implicit@ cost.
    129 Implicit cost is an additional category to Aaron's cost model. It is more signicant than @unsafe@ to have
    130 the compiler choosing implicit conversion over the narrowing conversion; It is less signicant to @poly@
    131 so that function overloaded with enum traits will be selected over the implicit. @Enum trait@ will be discussed in the chapter.
    132 
    133 Therefore, \CFA conversion cost is 8-tuple
    134 @@(unsafe, implicit, poly, safe, sign, vars, specialization, reference)@@
     119void foo( char * );
     120foo( Fred );
     121\end{cfa}
     122% \CFA enumeration being resolved as its base type because \CFA inserts an implicit @value()@ call on an \CFA enumeration.
     123During the resolution of expression e with \CFA enumeration type, \CFA adds @value(e)@ as an additional candidate with an extra \newterm{value} cost.
     124For expression @char currency = Dollar@, the is no defined conversion from Dollar (\CFA enumeration) type to basic type and the conversion cost is @infinite@,
     125thus the only valid candidate is @value(Dollar)@.
     126
     127@Value@ is a new category in \CFA's conversion cost model. It is defined to be a more significant factor than a @unsafe@ but weight less than @poly@.
     128The resultin g conversion cost is a 8-tuple:
     129@@(unsafe, value, poly, safe, sign, vars, specialization, reference)@@.
     130
     131\begin{cfa}
     132void bar(int);
     133enum(int) Month !{
     134        January=31, February=29, March=31, April=30, May=31, June-30,
     135        July=31, August=31, September=30, October=31, November=30, December=31
     136};
     137
     138Month a = Februrary;    // (1), with cost (0, 1, 0, 0, 0, 0, 0, 0)
     139double a = 5.5;                 // (2), with cost (1, 0, 0, 0, 0, 0, 0, 0)
     140
     141bar(a);
     142\end{cfa}
     143In the previous example, candidate (1) has an value cost to parameter type int, with is lower than (2) as an unsafe conversion from double to int.
     144\CFA chooses value cost over unsafe cost and therefore @a@ of @bar(a)@ is resolved as an @Month@.
     145
     146\begin{cfa}
     147forall(T | @CfaEnum(T)@) void bar(T);
     148
     149bar(a);                                 // (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)
     150\end{cfa}
     151% @Value@ is designed to be less significant than @poly@ to allow function being generic over \CFA enumeration (see ~\ref{c:trait}).
     152Being generic over @CfaEnum@ traits (a pre-defined interface for \CFA enums) is a practice in \CFA to implement functions over \CFA enumerations, as will see in chapter~\ref{c:trait}.
     153@Value@ is a being a more significant cost than @poly@ implies if a overloaeded function defined for @CfaEnum@ (and other generic type), \CFA always
     154try to resolve it as a @CfaEnum@, rather to insert a @value@ conversion.
     155
     156\subsection{Coercion}
     157While implicit conversion from a \CFA enumeration has been disabled, a explicit coercion cast to basic type is still possible to be consistent with C. In which case,
     158\CFA converts a \CFA enumeration variable as a basic type, with the value of the @position@ of the variable.
    135159
    136160\section{Auto Initialization}
     
    145169The complexity of the constant expression depends on the level of runtime computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary.
    146170
    147 % The notion of auto-initialization can be generalized in \CFA through the trait @AutoInitializable@.
    148 % \begin{cfa}
    149 % forall(T) @trait@ AutoInitializable {
    150 %       void ?{}( T & o, T v );                         $\C{// initialization}$
    151 %       void ?{}( T & t, zero_t );                      $\C{// 0}$
    152 %       T ?++( T & t);                                          $\C{// increment}$
    153 % };
    154 % \end{cfa}
    155 % In addition, there is an implicit enumeration counter, @ecnt@ of type @T@, managed by the compiler.
    156 % For example, the type @Odd@ satisfies @AutoInitializable@:
    157 % \begin{cfa}
    158 % struct Odd { int i; };
    159 % void ?{}( Odd & o, int v ) { if ( v & 1 ) o.i = v; else /* error not odd */ ; };
    160 % void ?{}( Odd & o, zero_t ) { o.i = 1; };
    161 % Odd ?++( Odd o ) { return (Odd){ o.i + 2 }; };
    162 % \end{cfa}
    163 % and implicit initialization is available.
    164 % \begin{cfa}
    165 % enum( Odd ) { A, B, C = 7, D };                       $\C{// 1, 3, 7, 9}$
    166 % \end{cfa}
    167 % where the compiler performs the following transformation and runs the code.
    168 % \begin{cfa}
    169 % enum( Odd ) {
    170 %       ?{}( ecnt, @0@ }  ?{}( A, ecnt },       ?++( ecnt )  ?{}( B, ecnt ),
    171 %       ?{}( ecnt, 7 )  ?{}( C, ecnt ), ?++( ecnt )  ?{}( D, ecnt )
    172 % };
    173 % \end{cfa}
    174 
    175 The notion of auto-initialization is generalized in \CFA enum in the following way:
    176 Enumerator e is the first enumerator of \CFA enumeration E with base type T. If e declares no no initializer, e is auto-initialized by the $zero\_t$ constructor of T.
    177 \CFA reports a compile time error if T has no $zero\_t$ constructor.
    178 Enumerator e is an enumerator of base-type T enumeration E that position i, where $i \neq 0$. And d is the enumerator with position @i-1@, e is auto-initialized with
    179 the result of @value(d)++@. If operator @?++@ is not defined for type T, \CFA reports a compile time error.
    180 
    181 Unfortunately, auto-initialization is not implemented because \CFA is only a transpiler, relying on generated C code to perform the detail work.
     171% The notion of auto-initialization is generalized in \CFA enumertation E with base type T in the following way:
     172When an enumerator @e@ does not have a initializer, if @e@ has enumeration type @E@ with base type @T@, \CFA auto-initialize @e@ with the following scheme:
     173\begin{enumerate}
     174% \item Enumerator e is the first enumerator of \CFA enumeration E with base type T. If e declares no no initializer, e is auto-initialized by the $zero\_t$ constructor of T.
     175\item if e is first enumerator, e is initialized with T's @zero_t@.
     176\item otherwise, if d is the enumerator defined just before e, with d has has been initialized with expression @l@ (@l@ can also be an auto-generated), e is initialized with @l++@.
     177% \CFA reports a compile time error if T has no $zero\_t$ constructor.
     178% Enumerator e is an enumerator of base-type T enumeration E that position i, where $i \neq 0$. And d is the enumerator with position @i-1@, e is auto-initialized with
     179% the result of @value(d)++@. If operator @?++@ is not defined for type T, \CFA reports a compile time error.
     180
     181% Unfortunately, auto-initialization is not implemented because \CFA is only a transpiler, relying on generated C code to perform the detail work.
     182% C does not have the equivalent of \CC \lstinline[language={[GNU]C++}]{constexpr}, and it is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler.
     183% Nevertheless, the necessary language concepts exist to support this feature.
     184\end{enumerate}
     185while @?++( T )@ can be explicitly overloaded or implicitly overloaded with properly defined @one_t@ and @?+?(T, T)@.
     186
     187Unfortunately, auto-initialization with only constant expression is not enforced because \CFA is only a transpiler, relying on generated C code to perform the detail work.
    182188C does not have the equivalent of \CC \lstinline[language={[GNU]C++}]{constexpr}, and it is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler.
    183189Nevertheless, the necessary language concepts exist to support this feature.
    184 
    185 
    186190
    187191\section{Enumeration Inheritance}
     
    292296In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution).
    293297However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts.
    294 In these contexts, a programmer's initition assumes an implicit conversion to position.
     298In these contexts, a programmer's intuition assumes an implicit conversion to position.
    295299
    296300For example, an intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implicit @break@ rather than a fall-through at the end of a @case@ clause.
Note: See TracChangeset for help on using the changeset viewer.