Changeset 5ddb8bf


Ignore:
Timestamp:
Dec 11, 2023, 4:17:57 AM (6 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
81da3da4
Parents:
2d373440
Message:

(Not really readable right now. Will clean up today and probably tomorrow) 1. Remove implicit conversion; 2. Companion trait;

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    r2d373440 r5ddb8bf  
    135135\section{\CFA-Style Enum}
    136136
    137 A \CFA enumeration is parameterized by a type, which specifies the type for each enumerator.
     137A \CFA enumeration is parameterized by a type specifying each enumerator's type.
    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 qualification, like the fields of an aggregate.
     156The enumerators of a scoped enumeration are accessed using qualifications, 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}
     165Pseudo-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
    164167\subsection{Enumerator Attributes}
    165 
    166 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@, i.e., like @sizeof@.
     168The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@.
    167169\begin{lstlisting}
    168170int green_pos = @position@( Colour.Green );     // 1
    169 char * green_value = @value@( Colour.Green );   // "G"
    170 char * green_label = @label@( Colour.Green );   // "Green"
    171 \end{lstlisting}
    172 There are implicit conversions from an enumerator to its attributes.
    173 \begin{lstlisting}[label=lst:enum_inst_assign_int]
    174 int green_pos = Colour.Green;  // 1
    175 char * green_value = Colour.Green;  // ambiguous
    176 char * green_label = Colour.Green;  // ambiguous
    177 \end{lstlisting}
    178 where a conversion is ambiguous, if the enumerator's type is same as an attribute's type.
    179 For example, @value( Colour.Green )@ and @label( Colour.Green )@ both have type @char *@.
    180 Further examples are:
    181 \begin{cquote}
    182 \begin{tabular}{ll}
    183 \begin{lstlisting}
    184 int monday_pos = Monday;  // ambiguous
    185 int monday_value = Monday;  // ambiguous
    186 char * monday_label = Monday;  // "Monday"
    187 
    188 \end{lstlisting}
    189 &
    190 \begin{lstlisting}
    191 enum(double) Math { PI = 3.14159, E = 2.718 };
    192 int pi_pos = PI;  // 0
    193 double pi_value = PI;  // 3.14159
    194 char * pi_label = PI;  // "PI"
    195 \end{lstlisting}
    196 \end{tabular}
    197 \end{cquote}
    198 Here, @position( Monday )@ and @value( Monday )@ both have type @int@, while all attribute types are unique for enumerator @PI@.
    199 
    200 When 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]
    203 enum(double) Foo { Bar };
    204 int tee = Foo.Bar; // value( Bar );
    205 \end{lstlisting}
    206 In 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.}
     171char * green_value = @value@( Colour.Green ); / "G"
     172char * green_label = @label@( Colour.Green ); // "Green"
     173\end{lstlisting}
     174
     175\subsection{enumerate()}
     176\begin{lstlisting}[label=lst:c_switch]
     177enum(int) C_ENUM { First, Second, Third = First, Fourth };
     178int 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}
     187In 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]
     191enum(double) C_ENUM { First, Second, Third = First, Fourth };
     192C_ENUM variable_a = First, variable_b = Second, variable_c = Thrid, variable_d = Fourth;
     193int 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};
     201p(variable_a); // 0
     202p(variable_b); // 1
     203p(variable_c); // "Third"
     204p(variable_d); // 3
     205\end{lstlisting}
     206
     207\section{Enumeration Characteristic}
    209208
    210209\subsection{Enumerator Storage}
     
    217216a \CFA enumeration is always statically typed;
    218217\item
    219 it is always resolved as one of its attributes in terms of real usage.
     218it is always resolved as one of its attributes regarding real usage.
    220219\end{enumerate}
    221220When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1.
     
    232231These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
    233232
    234 \subsection{Companion Object and Companion Function}
    235 
    236 \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
    237 forall( T )
    238 struct 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.
    245 A companion object has the same name as the enumeration is defined for.
    246 A 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.
    249 Because \CFA implicitly stores enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
    250 Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
    251 \begin{lstlisting}[label=lst:companion_definition]
    252 int position( Companion o, int pos ) { return pos; }
    253 T value( Companion o, int pos ) { return o.values[ pos ]; }
    254 char * label( Companion o, int pos ) { return o.labels[ pos ]; }
    255 \end{lstlisting}
    256 Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
    257 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@
    258 \begin{lstlisting}[label=lst:companion_definition_values_labels]
    259 Colour.values; // read the Companion's values
    260 values( Colour ); // same as Colour.values
    261 \end{lstlisting}
    262 
    263 \subsection{User Define Enumeration Functions}
    264 
    265 Companion objects make extending features for \CFA enumeration easy.
    266 \begin{lstlisting}[label=lst:companion_user_definition]
    267 char * charastic_string( Companion o, int position ) {
    268         return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
    269 }
    270 printf( charactic_string ( Color, 1 ) );
    271 >>> Label: Green; Value: G
    272 \end{lstlisting}
    273 Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
    274 
    275 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.
    276 Therefore, a user can use the syntax with a user-defined enumeration function call:
    277 \begin{lstlisting}[label=lst:companion_user_definition]
    278 charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
    279 >>> Label: Green; Value: G
    280 \end{lstlisting}
    281 Similarly, the user can work with the enumeration type itself: (see section ref...)
    282 \begin{lstlisting}[ label=lst:companion_user_definition]
    283 void print_enumerators ( Companion o ) {
    284         for ( c : Companion o ) {
    285                 sout | label (c) | value( c ) ;
    286         }
    287 }
    288 print_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.}
     233\subsection{Enumeration as Value}
     234An \CFA enumeration with base type T can be used seamlessly as T.
     235\begin{lstlisting}[label=lst:implicit_conversion]
     236char * green_value = Colour.Green; // "G"
     237// Is equivalent to
     238char * 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]
     244void foo(Colour c) { return value( c ); }
     245void bar(char * s) { return s; }
     246Colour green = Colour.Green; // "G"
     247char * green = "Green";
     248foo( green ); // "G"
     249bar( green ); // "Green"
     250\end{lstlisting}
     251
     252\subsection{Function Overloading}
     253\begin{lstlisting}[label=lst:function_overload]
     254void foo(Colour c) { return "It is an enum"; }
     255void foo(char * s) { return "It is a string"; }
     256foo( green ); // "It is an enum"
     257\end{lstlisting}
     258
     259As a consequence, the semantics of using \CFA enumeration as a flag for selection is identical to C enumeration.
     260
    313261
    314262% \section{Enumeration Features}
     
    335283trait AutoInitializable {
    336284        void ?()( T & t, zero_t );
    337         void ?()( T & t, one_t );
    338         S ?+?( T & t, one_t );
     285        S ?++( T & t);
    339286};
    340287\end{lstlisting}
     
    343290struct Odd { int i; };
    344291void ?()( Odd & t, zero_t ) { t.i = 1; };
    345 void ?()( Odd & t, one_t ) { t.i = 2; };
    346 Odd ?+?( Odd t1, Odd t2 ) { return Odd( t1.i + t2.i); };
     292Odd ?++( Odd t1 ) { return Odd( t1.i + 2); };
    347293\end{lstlisting}
    348294When the type of an enumeration is @AutoInitializable@, implicit initialization is available.
     
    352298};
    353299\end{lstlisting}
    354 In 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.
     300In 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.
    356302Therefore, the enumeration is initialized as follows:
    357303\begin{lstlisting}[label=lst:sample_auto_Initializable_usage_gen]
     
    510456In 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>@.
    511457
    512 % \subsection{Preluede}
    513 % \CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
     458\begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_data]
     459const T * const values;
     460const char * label;
     461int length;
     462\end{lstlisting}
     463Companion 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".
     464Companion 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]
     471forall( T )
     472struct 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
     480The companion object is singleton across the compilation (investigation). 
     481
     482\CFA generates the definition of companion functions.
     483Because \CFA implicitly stores an enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
     484Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
     485\begin{lstlisting}[label=lst:companion_definition]
     486int position( Companion o, int pos ) { return pos; }
     487T value( Companion o, int pos ) { return o.values[ pos ]; }
     488char * label( Companion o, int pos ) { return o.labels[ pos ]; }
     489\end{lstlisting}
     490Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
     491A 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]
     493Colour.values; // read the Companion's values
     494values( Colour ); // same as Colour.values
     495\end{lstlisting}
     496
     497\subsection{Companion Traits (experimental)}
     498Not sure its semantics yet, and it might replace a companion object.
     499\begin{lstlisting}[label=lst:companion_trait]
     500forall(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}
     508All 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}
     516enum(int) Weekday {
     517    Monday=10, Tuesday, ...
     518};
     519
     520T value( enum Weekday<T> & this);
     521int position( enum Weekday<T> & this )
     522char * label( enum Weekday<T> & this )
     523
     524trait Companion obj = (enum(int)) Workday.Weekday;
     525value(obj); // 10
     526\end{lstlisting}
     527The 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
     532Companion objects make extending features for \CFA enumeration easy.
     533\begin{lstlisting}[label=lst:companion_user_definition]
     534char * charastic_string( Companion o, int position ) {
     535        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
     536}
     537printf( charactic_string ( Color, 1 ) );
     538>>> Label: Green; Value: G
     539\end{lstlisting}
     540Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
     541
     542The \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.
     543Therefore, a user can use the syntax with a user-defined enumeration function call:
     544\begin{lstlisting}[label=lst:companion_user_definition]
     545charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
     546>>> Label: Green; Value: G
     547\end{lstlisting}
     548Similarly, the user can work with the enumeration type itself: (see section ref...)
     549\begin{lstlisting}[ label=lst:companion_user_definition]
     550void print_enumerators ( Companion o ) {
     551        for ( c : Companion o ) {
     552                sout | label (c) | value( c ) ;
     553        }
     554}
     555print_enumerators( Colour );
     556\end{lstlisting}
     557
    514558
    515559\subsection{Declaration}
     
    729773\end{lstlisting}
    730774
     775
    731776\end{document}
    732777
Note: See TracChangeset for help on using the changeset viewer.