Changes in / [81da3da4:1c85ffc]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    r81da3da4 r1c85ffc  
    135135\section{\CFA-Style Enum}
    136136
    137 A \CFA enumeration is parameterized by a type specifying each enumerator's type.
     137A \CFA enumeration is parameterized by a type, which specifies the type for each enumerator.
    138138\CFA allows any object type for the enumerators, and values assigned to enumerators must be from the declared type.
    139139\begin{lstlisting}[label=lst:color]
     
    154154\end{lstlisting}
    155155where the @'!'@ implies the enumerators are \emph{not} projected.
    156 The enumerators of a scoped enumeration are accessed using qualifications, like the fields of an aggregate.
     156The enumerators of a scoped enumeration are accessed using qualification, like the fields of an aggregate.
    157157% The syntax of $qualified\_expression$ for \CFA-enum is the following:
    158158% $$<qualified\_expression> := <enum\_type>.<enumerator>$$
     
    162162\end{lstlisting}
    163163
    164 \section{Enumeration Pseudo-functions}
    165 Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@. Instead, the call to functions will be substituted into other expressions in compilation time.
    166 
    167164\subsection{Enumerator Attributes}
    168 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@.
     165
     166The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@, i.e., like @sizeof@.
    169167\begin{lstlisting}
    170168int green_pos = @position@( Colour.Green );     // 1
    171 char * green_value = @value@( Colour.Green ); / "G"
    172 char * green_label = @label@( Colour.Green ); // "Green"
    173 \end{lstlisting}
    174 
    175 \subsection{enumerate()}
    176 \begin{lstlisting}[label=lst:c_switch]
    177 enum(int) C_ENUM { First, Second, Third = First, Fourth };
    178 int v(C_ENUM e) {
    179     switch( e ) {
    180         case First: return 0; break;
    181         case Second: return 1; break;
    182         // case Thrid: return 2; break;
    183         // case Fourth: return 3; break;
    184     };
    185 };
    186 \end{lstlisting}
    187 In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@. Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value. Case First and Third, or Second and Fourth, has duplicate case values.
    188 
    189 @enumerate()@ is a pseudo-function that makes the switch statement match by an enumerator instead.
    190 \begin{lstlisting}[label=lst:c_switch_enumerate]
    191 enum(double) C_ENUM { First, Second, Third = First, Fourth };
    192 C_ENUM variable_a = First, variable_b = Second, variable_c = Thrid, variable_d = Fourth;
    193 int v(C_ENUM e) {
    194     switch( enumeratate( e ) ) {
    195         case First: return e; break;
    196         case Second: return value( e ); break;
    197         case Thrid: return label( e ); break;
    198         case Fourth: return position( e ); break;
    199     };
    200 };
    201 p(variable_a); // 0
    202 p(variable_b); // 1
    203 p(variable_c); // "Third"
    204 p(variable_d); // 3
    205 \end{lstlisting}
    206 
    207 \section{Enumeration Characteristic}
     169char * green_value = @value@( Colour.Green );   // "G"
     170char * green_label = @label@( Colour.Green );   // "Green"
     171\end{lstlisting}
     172There are implicit conversions from an enumerator to its attributes.
     173\begin{lstlisting}[label=lst:enum_inst_assign_int]
     174int green_pos = Colour.Green;  // 1
     175char * green_value = Colour.Green;  // ambiguous
     176char * green_label = Colour.Green;  // ambiguous
     177\end{lstlisting}
     178where a conversion is ambiguous, if the enumerator's type is same as an attribute's type.
     179For example, @value( Colour.Green )@ and @label( Colour.Green )@ both have type @char *@.
     180Further examples are:
     181\begin{cquote}
     182\begin{tabular}{ll}
     183\begin{lstlisting}
     184int monday_pos = Monday;  // ambiguous
     185int monday_value = Monday;  // ambiguous
     186char * monday_label = Monday;  // "Monday"
     187
     188\end{lstlisting}
     189&
     190\begin{lstlisting}
     191enum(double) Math { PI = 3.14159, E = 2.718 };
     192int pi_pos = PI;  // 0
     193double pi_value = PI;  // 3.14159
     194char * pi_label = PI;  // "PI"
     195\end{lstlisting}
     196\end{tabular}
     197\end{cquote}
     198Here, @position( Monday )@ and @value( Monday )@ both have type @int@, while all attribute types are unique for enumerator @PI@.
     199
     200When a resolution is ambiguous, a \textit{resolution precedence} applies: $$value > position > label$$
     201\CFA uses resolution distance to describe if one type can be used as another. While \CFA calculates the resolution distance between the expected type and types of all three attributes, it would not choose the attribute with the closest distance. Instead, when resolving an enumeration constant, \CFA always chooses value whenever it is a possible resolution (resolution distance is not infinite), followed by position, then label.
     202\begin{lstlisting}[label=lst:enum_inst_precedence]
     203enum(double) Foo { Bar };
     204int tee = Foo.Bar; // value( Bar );
     205\end{lstlisting}
     206In the example~\ref{lst:enum_inst_precedence}, while $position( Bar )$ has the closest resolution among the three attributes, $Foo.Bar$ is resolved as $value( Bar )$ because of the resolution precedence.
     207
     208\PAB{Not sure this is going to work.}
    208209
    209210\subsection{Enumerator Storage}
     
    216217a \CFA enumeration is always statically typed;
    217218\item
    218 it is always resolved as one of its attributes regarding real usage.
     219it is always resolved as one of its attributes in terms of real usage.
    219220\end{enumerate}
    220221When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1.
     
    231232These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
    232233
    233 \subsection{Enumeration as Value}
    234 An \CFA enumeration with base type T can be used seamlessly as T.
    235 \begin{lstlisting}[label=lst:implicit_conversion]
    236 char * green_value = Colour.Green; // "G"
    237 // Is equivalent to
    238 char * green_value = value( Color.Green ); "G"
    239 \end{lstlisting}
    240 \CFA recognizes @Colour.Green@ as an Expression with enumeration type. [reference to resolution distance] An enumeration type can be safely converted into its value type T, @char *@ in the example. When assigning @Colour.Green@ to a reference @green_value@, which has type @char *@, the compiler adds the distance between an enumeration and type T, and the distance between type T and @char *@. If the distance is safe, \CFA will replace the expression @Colour.Green@ with @value( Colour.Green )@.
    241 
    242 \subsection{Variable Overloading}
    243 \begin{lstlisting}[label=lst:variable_overload]
    244 void foo(Colour c) { return value( c ); }
    245 void bar(char * s) { return s; }
    246 Colour green = Colour.Green; // "G"
    247 char * green = "Green";
    248 foo( green ); // "G"
    249 bar( green ); // "Green"
    250 \end{lstlisting}
    251 
    252 \subsection{Function Overloading}
    253 \begin{lstlisting}[label=lst:function_overload]
    254 void foo(Colour c) { return "It is an enum"; }
    255 void foo(char * s) { return "It is a string"; }
    256 foo( green ); // "It is an enum"
    257 \end{lstlisting}
    258 
    259 As a consequence, the semantics of using \CFA enumeration as a flag for selection is identical to C enumeration.
    260 
     234\subsection{Companion Object and Companion Function}
     235
     236\begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
     237forall( T )
     238struct Companion {
     239        const T * const values;
     240        const char ** const labels;
     241        int length;
     242};
     243\end{lstlisting}
     244\CFA creates a @Companion@ object for every \CFA enumeration.
     245A companion object has the same name as the enumeration is defined for.
     246A companion object stores values and labels of enumeration constants, in the order of the constants defined in the enumeration.
     247
     248\CFA generates the definition of companion functions.
     249Because \CFA implicitly stores enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
     250Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
     251\begin{lstlisting}[label=lst:companion_definition]
     252int position( Companion o, int pos ) { return pos; }
     253T value( Companion o, int pos ) { return o.values[ pos ]; }
     254char * label( Companion o, int pos ) { return o.labels[ pos ]; }
     255\end{lstlisting}
     256Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
     257A user can retrieve values and labels defined in an enumeration by accessing the values and labels directly, or indirectly by calling @Companion@ functions @values@ and @labels@
     258\begin{lstlisting}[label=lst:companion_definition_values_labels]
     259Colour.values; // read the Companion's values
     260values( Colour ); // same as Colour.values
     261\end{lstlisting}
     262
     263\subsection{User Define Enumeration Functions}
     264
     265Companion objects make extending features for \CFA enumeration easy.
     266\begin{lstlisting}[label=lst:companion_user_definition]
     267char * charastic_string( Companion o, int position ) {
     268        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
     269}
     270printf( charactic_string ( Color, 1 ) );
     271>>> Label: Green; Value: G
     272\end{lstlisting}
     273Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
     274
     275The \CFA compiler turns a function call that takes an enumeration instance as a parameter into a function call with a companion object plus a position.
     276Therefore, a user can use the syntax with a user-defined enumeration function call:
     277\begin{lstlisting}[label=lst:companion_user_definition]
     278charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
     279>>> Label: Green; Value: G
     280\end{lstlisting}
     281Similarly, the user can work with the enumeration type itself: (see section ref...)
     282\begin{lstlisting}[ label=lst:companion_user_definition]
     283void print_enumerators ( Companion o ) {
     284        for ( c : Companion o ) {
     285                sout | label (c) | value( c ) ;
     286        }
     287}
     288print_enumerators( Colour );
     289\end{lstlisting}
     290
     291% \subsection{Runtime Enumeration}
     292
     293% The companion structure definition is visible to users, and users can create an instance of companion object themselves, which effectively constructs a \textit{Runtime Enumeration}.
     294% \begin{lstlisting}[ label=lst:runtime_enum ]
     295% const char values[$\,$] = { "Hello", "World" };
     296% const char labels[$\,$] = { "First", "Second" };
     297% Companion(char *) MyEnum = { .values: values, .labels: labels, .length: 2 };
     298% \end{lstlisting}
     299% A runtime enumeration can be used to call enumeration functions.
     300% \begin{lstlisting}[ label=lst:runtime_enum_usage ]
     301% sout | charatstic_string( MyEnum, 1 );
     302% >>> Label: Second; Value: World
     303% \end{lstlisting}
     304% However, a runtime enumeration cannot create an enumeration instance, and it does not support enum-qualified syntax.
     305% \begin{lstlisting}[ label=lst:runtime_enum_usage ]
     306% MyEnum e = MyEnum.First; // Does not work: cannot create an enumeration instance e,
     307%                                   // and MyEnum.First is not recognizable
     308% \end{lstlisting}
     309% During the compilation, \CFA adds enumeration declarations to an enumeration symbol table and creates specialized function definitions for \CFA enumeration.
     310% \CFA does not recognize runtime enumeration during compilation and would not add them to the enumeration symbol table, resulting in a lack of supports for runtime enumeration.
     311
     312% \PAB{Not sure how useful this feature is.}
    261313
    262314% \section{Enumeration Features}
     
    283335trait AutoInitializable {
    284336        void ?()( T & t, zero_t );
    285         S ?++( T & t);
     337        void ?()( T & t, one_t );
     338        S ?+?( T & t, one_t );
    286339};
    287340\end{lstlisting}
     
    290343struct Odd { int i; };
    291344void ?()( Odd & t, zero_t ) { t.i = 1; };
    292 Odd ?++( Odd t1 ) { return Odd( t1.i + 2); };
     345void ?()( Odd & t, one_t ) { t.i = 2; };
     346Odd ?+?( Odd t1, Odd t2 ) { return Odd( t1.i + t2.i); };
    293347\end{lstlisting}
    294348When the type of an enumeration is @AutoInitializable@, implicit initialization is available.
     
    298352};
    299353\end{lstlisting}
    300 In the example, no initializer is specified for the first enumeration constant @A@, so \CFA initializes it with the value of @zero_t@, which is 1.
    301 @B@ and @D@ have the values of their $predecessor++$, where @one_t@ has the value 2.
     354In the example, there is no initializer specified for the first enumeration constant @A@, so \CFA initializes it with the value of @zero_t@, which is 1.
     355@B@ and @D@ have the values of their $predecessor + one_t$, where @one_t@ has the value 2.
    302356Therefore, the enumeration is initialized as follows:
    303357\begin{lstlisting}[label=lst:sample_auto_Initializable_usage_gen]
     
    456510In the later discussion, we will use @EnumDecl<T>@ to symbolize a @EnumDecl@ parameterized by type T, and @EnumInstType<T>@ is a declared instance of @EnumDecl<T>@.
    457511
    458 \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_data]
    459 const T * const values;
    460 const char * label;
    461 int length;
    462 \end{lstlisting}
    463 Companion data are necessary information to represent an enumeration. They are stored as standalone pieces, rather than a structure. Those data will be loaded "on demand".
    464 Companion data are needed only if the according pseudo-functions are called. For example, the value of the enumeration Workday is loaded only if there is at least one compilation that has call $value(Workday)$. Once the values are loaded, all compilations share these values array to reduce memory usage.
    465 
    466 <Investiage: how to implement this is huge>
    467 
    468 \subsection{(Rework) Companion Object and Companion Function}
    469 
    470 \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
    471 forall( T )
    472 struct Companion {
    473         const T * const values;
    474         const char * label;
    475         int length;
    476 };
    477 \end{lstlisting}
    478 \CFA generates companion objects, an instance of structure that encloses @necessary@ data to represent an enumeration. The size of the companion is unknown at the compilation time, and it "grows" in size to compensate for the @usage@.
    479 
    480 The companion object is singleton across the compilation (investigation). 
    481 
    482 \CFA generates the definition of companion functions.
    483 Because \CFA implicitly stores an enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
    484 Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
    485 \begin{lstlisting}[label=lst:companion_definition]
    486 int position( Companion o, int pos ) { return pos; }
    487 T value( Companion o, int pos ) { return o.values[ pos ]; }
    488 char * label( Companion o, int pos ) { return o.labels[ pos ]; }
    489 \end{lstlisting}
    490 Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
    491 A user can retrieve values and labels defined in an enumeration by accessing the values and labels directly, or indirectly by calling @Companion@ functions @values@ and @labels@
    492 \begin{lstlisting}[label=lst:companion_definition_values_labels]
    493 Colour.values; // read the Companion's values
    494 values( Colour ); // same as Colour.values
    495 \end{lstlisting}
    496 
    497 \subsection{Companion Traits (experimental)}
    498 Not sure its semantics yet, and it might replace a companion object.
    499 \begin{lstlisting}[label=lst:companion_trait]
    500 forall(T1) {
    501     trait Companion(otype T2<otype T1>) {
    502         T1 value((otype T2<otype T1> const &);
    503         int position(otype T2<otype T1> const &);
    504         char * label(otype T2<otype T1> const &);
    505     }
    506 }
    507 \end{lstlisting}
    508 All enumerations implicitly implement the Companion trait, an interface to access attributes. The Companion can be a data type because it fulfills to requirements to have concrete instances, which are:
    509 
    510 \begin{enumerate}
    511   \item The instance of enumeration has a single polymorphic type.
    512   \item Each assertion should use the type once as a parameter.
    513 \end{enumerate}
    514 
    515 \begin{lstlisting}
    516 enum(int) Weekday {
    517     Monday=10, Tuesday, ...
    518 };
    519 
    520 T value( enum Weekday<T> & this);
    521 int position( enum Weekday<T> & this )
    522 char * label( enum Weekday<T> & this )
    523 
    524 trait Companion obj = (enum(int)) Workday.Weekday;
    525 value(obj); // 10
    526 \end{lstlisting}
    527 The enumeration comes with default implementation to the Companion traits functions. The usage of Companion functions would make \CFA allocates and initializes the necessary companion arrays, and return the data at the position represented by the enumeration.
    528 (...)
    529 
    530 \subsection{User Define Enumeration Functions}
    531 
    532 Companion objects make extending features for \CFA enumeration easy.
    533 \begin{lstlisting}[label=lst:companion_user_definition]
    534 char * charastic_string( Companion o, int position ) {
    535         return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
    536 }
    537 printf( charactic_string ( Color, 1 ) );
    538 >>> Label: Green; Value: G
    539 \end{lstlisting}
    540 Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
    541 
    542 The \CFA compiler turns a function call that takes an enumeration instance as a parameter into a function call with a companion object plus a position.
    543 Therefore, a user can use the syntax with a user-defined enumeration function call:
    544 \begin{lstlisting}[label=lst:companion_user_definition]
    545 charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
    546 >>> Label: Green; Value: G
    547 \end{lstlisting}
    548 Similarly, the user can work with the enumeration type itself: (see section ref...)
    549 \begin{lstlisting}[ label=lst:companion_user_definition]
    550 void print_enumerators ( Companion o ) {
    551         for ( c : Companion o ) {
    552                 sout | label (c) | value( c ) ;
    553         }
    554 }
    555 print_enumerators( Colour );
    556 \end{lstlisting}
    557 
     512% \subsection{Preluede}
     513% \CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
    558514
    559515\subsection{Declaration}
     
    773729\end{lstlisting}
    774730
    775 
    776731\end{document}
    777732
Note: See TracChangeset for help on using the changeset viewer.