Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    r2d373440 r66d92e3  
    289289\end{lstlisting}
    290290
    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.}
    313 
    314 % \section{Enumeration Features}
     291\subsection{Runtime Enumeration}
     292
     293The 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 ]
     295const char values[$\,$] = { "Hello", "World" };
     296const char labels[$\,$] = { "First", "Second" };
     297Companion(char *) MyEnum = { .values: values, .labels: labels, .length: 2 };
     298\end{lstlisting}
     299A runtime enumeration can be used to call enumeration functions.
     300\begin{lstlisting}[ label=lst:runtime_enum_usage ]
     301sout | charatstic_string( MyEnum, 1 );
     302>>> Label: Second; Value: World
     303\end{lstlisting}
     304However, a runtime enumeration cannot create an enumeration instance, and it does not support enum-qualified syntax.
     305\begin{lstlisting}[ label=lst:runtime_enum_usage ]
     306MyEnum e = MyEnum.First; // Does not work: cannot create an enumeration instance e,
     307                                    // and MyEnum.First is not recognizable
     308\end{lstlisting}
     309During 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.}
     313
     314\section{Enumeration Features}
    315315
    316316A trait is a collection of constraints in \CFA that can be used to describe types.
    317317The \CFA standard library defines traits to categorize types with related enumeration features.
    318318
    319 \section{Enumerator Initialization}
    320 An enumerator must have a deterministic immutable value, either be explicitly initialized in the enumeration definition, or implicitly initialized by rules.
    321 
    322 \subsection{C Enumeration Rule}
    323 A C enumeration has an integral type. If not initialized, the first enumerator implicitly has the integral value 0, and other enumerators have a value equal to its $predecessor + 1$.
    324 
    325319\subsection{Auto Initializable}
    326320\label{s:AutoInitializable}
    327321
    328 
     322TODO: make the initialization rule a separate section.
     323
     324If no explicit initializer is given to an enumeration constant, C initializes the first enumeration constant with value 0, and the next enumeration constant has a value equal to its $predecessor + 1$.
    329325\CFA enumerations have the same rule in enumeration constant initialization.
    330326However, only \CFA types that have defined traits for @zero_t@, @one_t@, and an addition operator can be automatically initialized by \CFA.
     
    360356};
    361357\end{lstlisting}
    362 Note that there is no mechanism to prevent an even value for the direct initialization, such as @C = 6@.
     358Note, there is no mechanism to prevent an even value for the direct initialization, such as @C = 6@.
    363359
    364360In \CFA, character, integral, float, and imaginary types are all @AutoInitialiable@.
     
    371367>>> F, o, z
    372368\end{lstlisting}
    373 \section{Enumeration Features}
     369
    374370\subsection{Iteration and Range}
    375371
    376 It is convenient to iterate over a \CFA enumeration value, e.g.:
     372It is convenient to iterate over a \CFA enumeration, e.g.:
    377373\begin{lstlisting}[label=lst:range_functions]
    378 for ( Alphabet alph; Alphabet ) { sout | alph; }
    379 >>> A B C ... D
     374for ( Alphabet alph; Alphabet ) {
     375        printf( "%d ", alph );
     376}
     377>>> A B C ...
    380378\end{lstlisting}
    381379The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration.
    382380@alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
    383381
    384 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants:
     382\CFA offers a shorthand for iterating all enumeration constants:
    385383\begin{lstlisting}[label=lst:range_functions]
    386 for ( Alphabet alph ) { sout | alph; }
    387 >>> A B C ... D
    388 \end{lstlisting}
    389 
    390 The following are examples for constructing for-control using an enumeration. Note that the type declaration of the iterating variable is optional, because \CFA can infer the type as EnumInstType based on the range expression, and possibly convert it to one of its attribute types.
    391 
    392 \textbullet\ H is implicit up-to exclusive range [0, H).
    393 \begin{lstlisting}[label=lst:range_function_1]
    394 for ( alph; Alphabet.D ) { sout | alph; }
    395 >>> A B C
    396 \end{lstlisting}
    397 
    398 \textbullet\ ~= H is implicit up-to inclusive range [0,H].
    399 \begin{lstlisting}[label=lst:range_function_2]
    400 for ( alph; ~= Alphabet.D ) { sout | alph; }
    401 >>> A B C D
    402 \end{lstlisting}
    403 
    404 \textbullet\ L ~ H is explicit up-to exclusive range [L,H).
    405 \begin{lstlisting}[label=lst:range_function_3]
    406 for ( alph; Alphabet.B ~ Alphabet.D  ) { sout | alph; }
    407 // for ( Alphabet alph = Alphabet.B; alph < Alphabet.D; alph += 1  ); 1 is one_t
    408 >>> B C
    409 \end{lstlisting}
    410 
    411 \textbullet\ L ~= H is explicit up-to inclusive range [L,H].
    412 \begin{lstlisting}[label=lst:range_function_4]
    413 for ( alph; Alphabet.B ~= Alphabet.D  ) { sout | alph; }
    414 >>> B C D
    415 \end{lstlisting}
    416 
    417 \textbullet\ L -~ H is explicit down-to exclusive range [H,L), where L and H are implicitly interchanged to make the range down-to.
    418 \begin{lstlisting}[label=lst:range_function_5]
    419 for ( alph; Alphabet.D -~ Alphabet.B  ) { sout | alph; }
    420 >>> D C
    421 \end{lstlisting}
    422 
    423 \textbullet\ L -~= H is explicit down-to exclusive range [H,L], where L and H are implicitly interchanged to make the range down-to.
    424 \begin{lstlisting}[label=lst:range_function_6]
    425 for ( alph; Alphabet.D -~= Alphabet.B  ) { sout | alph; }
    426 >>> D C B
    427 \end{lstlisting}
    428 
    429 A user can specify the ``step size'' of an iteration. There are two different stepping schemes of enumeration for-loop.
    430 \begin{lstlisting}[label=lst:range_function_stepping]
    431 enum(int) Sequence { A = 10, B = 12, C = 14, D = 16, D  = 18 };
    432 for ( s; Sequence.A ~= Sequence.D ~ 1  ) { sout | alph; }
    433 >>> 10 12 14 16 18
    434 for ( s; Sequence.A ~= Sequence.D; s+=1  ) { sout | alph; }
    435 >>> 10 11 12 13 14 15 16 17 18
    436 \end{lstlisting}
    437 The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to
    438 \begin{lstlisting}[label=lst:range_function_stepping_converted]
    439 for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1  ) { sout | alph; }
    440 >>> 10 11 12 13 14 15 16 17 18
    441 \end{lstlisting}
    442 
    443 % \PAB{Explain what each loop does.}
     384for ( Alphabet alph ) {
     385        printf( "%d ", alph );
     386}
     387>>> A B C ...
     388\end{lstlisting}
     389The following different loop-control syntax is supported:
     390\begin{lstlisting}[label=lst:range_functions]
     391for ( Alphabet.D )
     392for ( alph; Alphabet.g ~ Alphabet.z )
     393for ( Alphabet alph; Alphabet.R ~ Alphabet.X ~ 2 )
     394\end{lstlisting}
     395\PAB{Explain what each loop does.}
     396Notably, the meaning of ``step'' for an iteration has changed for enumeration.
     397Consider the following example:
     398\begin{lstlisting}[label=lst:range_functions]
     399enum(int) Sequence {
     400        A = 10, B = 12, C = 14; 
     401}
     402for ( s; Sequence.A ~ Sequence.C ) {
     403        printf( "%d ", s );
     404}
     405>>> 10 12 14
     406
     407for ( s; Sequence.A ~ Sequence.A ~ 2 ) {
     408        printf( "%d ", s );
     409}
     410>>> 10 14
     411\end{lstlisting}
     412The range iteration of enumeration does not return the @current_value++@ until it reaches the upper bound.
     413The semantics is to return the next enumeration constant.
     414If a stepping is specified, 2 for example, it returns the 2 enumeration constant after the current one, rather than the @current+2@.
    444415
    445416It is also possible to iterate over an enumeration's labels, implicitly or explicitly:
     
    453424\end{lstlisting}
    454425
    455 
    456 % \subsection{Non-uniform Type}
    457 % TODO: Working in Progress, might need to change other sections. Conflict with the resolution right now.
    458 
    459 % \begin{lstlisting}
    460 % enum T( int, char * ) {
    461 %     a=42, b="Hello World"
    462 % };
    463 % \end{lstlisting}
    464 % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types.
    465 
    466 \subsection{Enumeration Inheritance}
    467 
    468 \begin{lstlisting}[label=lst:EnumInline]
    469 enum( char * ) Name { Jack = "Jack", Jill = "Jill" };
    470 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
    471 \end{lstlisting}
    472 \lstinline{Inline} allows Enumeration Name2 to inherit enumerators from Name1 by containment, and a Name enumeration is a subtype of enumeration Name2. An enumeration instance of type Name can be used where an instance of Name2 is expected.
    473 \begin{lstlisting}[label=lst:EnumInline]
    474 Name Fred;
    475 void f( Name2 );
    476 f( Fred );
    477 \end{lstlisting}
    478 If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum".
    479 
    480 An enumeration can inline at most one other enumeration. The inline declaration must be placed before the first enumerator of the inlining enum. The inlining enum has all the enumerators from the inlined enum, with the same labels, values, and position.
    481 \begin{lstlisting}[label=lst:EnumInline]
    482 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
    483 // is equivalent to enum Name2 { Jack = "Jack", Jill="Jill", Sue = "Sue", Tom = "Tom" };
    484 \end{lstlisting}
    485 Name.Jack is equivalent to Name2.Jack. Their attributes are all identical. Opening both Name and Name2 in the same scope will not introduce ambiguity.
    486 \begin{lstlisting}[label=lst:EnumInline]
    487 with( Name, Name2 ) { Jack; } // Name.Jack and Name2.Jack are equivalent. No ambiguity
    488 \end{lstlisting}
    489 
    490426\section{Implementation}
    491427
    492 \subsection{Compiler Representation}
    493 The definition of an enumeration is represented by an internal type called @EnumDecl@. At the minimum, it stores all the information needed to construct the companion object. Therefore, an @EnumDecl@ can be represented as the following:
    494 \begin{lstlisting}[label=lst:EnumDecl]
    495 forall(T)
    496 class EnumDecl {
    497     T* values;
    498     char** label;
    499 };
    500 \end{lstlisting}
    501 
    502 The internal representation of an enumeration constant is @EnumInstType@.
    503 An @EnumInstType@ has a reference to the \CFA-enumeration declaration and the position of the enumeration constant.
    504 \begin{lstlisting}[label=lst:EnumInstType]
    505 class EnumInstType {
    506     EnumDecl enumDecl;
    507     int position;
    508 };
    509 \end{lstlisting}
    510 In 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>@.
    511 
    512 % \subsection{Preluede}
    513 % \CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
     428\CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
    514429
    515430\subsection{Declaration}
     
    564479If the @aggregation_name@ is identified as a \CFA enumeration, the compiler checks if @field@ presents in the declared \CFA enumeration.
    565480
    566 \subsection{\lstinline{with} Clause/Statement}
     481\subsection{\lstinline{with} Statement}
     482
     483\emph{Work in Progress}
     484
    567485Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible.
     486
     487\subsection{Instance Declaration}
     488
     489\emph{Work in Progress}
     490
    568491\begin{lstlisting}[label=lst:declaration]
    569 enum Color( char * ) { Red="R", Green="G", Blue="B" };
    570 enum Animal( int ) { Cat=10, Dog=20 };
    571 with ( Color, Animal ) {
    572     char * red_string = Red; // value( Color.Red )
    573     int cat = Cat; // value( Animal.Cat )
    574 }
    575 \end{lstlisting}
    576 The \lstinline{with} might introduce ambiguity to a scope. Consider the example:
     492enum Sample s1;
     493Sample s2;
     494\end{lstlisting}
     495A declaration of \CFA enumeration instance that has no difference than a C enumeration or other \CFA aggregation.
     496The compiler recognizes the type of a variable declaration by searching the name in all possible types.
     497The @enum@ keyword is not necessary but helps to disambiguate types (questionable).
     498The generated code for a \CFA enumeration declaration is utterly an integer, which is meant to store the position.
    577499\begin{lstlisting}[label=lst:declaration]
    578 enum Color( char * ) { Red="R", Green="G", Blue="B" };
    579 enum RGB( int ) { Red=0, Green=1, Blue=2 };
    580 with ( Color, RGB ) {
    581     // int red = Red;
    582 }
    583 \end{lstlisting}
    584 \CFA will not try to resolve the expression with ambiguity. It would report an error. In this case, it is necessary to qualify @Red@ even inside of the \lstinline{with} clause.
    585 
    586 \subsection{Instance Declaration}
    587 
    588 
    589 \begin{lstlisting}[label=lst:var_declaration]
    590 enum Sample s1;
    591 \end{lstlisting}
    592 
    593 The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType.
    594 \begin{lstlisting}[label=lst:declaration_code]
    595500int s1;
    596 \end{lstlisting}
    597 The generated code for an enumeration instance is simply an int. It is to hold the position of an enumeration. And usage of variable @s1@ will be converted to return one of its attributes: label, value, or position, with respect to the @Unification@ rule
     501int s2;
     502\end{lstlisting}
     503
     504\subsection{Compiler Representation}
     505
     506\emph{Work in Progress}
     507
     508The internal representation of an enumeration constant is @EnumInstType@.
     509The minimum information an @EnumInstType@ stores is a reference to the \CFA-enumeration declaration and the position of the enumeration constant.
     510\begin{lstlisting}[label=lst:EnumInstType]
     511class EnumInstType {
     512        EnumDecl enumDecl;
     513        int position;
     514};
     515\end{lstlisting}
    598516
    599517\subsection{Unification and Resolution }
    600518
     519\emph{Work in Progress}
    601520
    602521\begin{lstlisting}
     
    685604@unification( EnumInstType<Colour>, int )@: @position( EnumInstType< Colour > )@
    686605\item
    687 return the enumeration constant at position 1
     606return the enumeration constant at the position 1
    688607\end{enumerate}
    689608\begin{lstlisting}
     
    703622\end{enumerate}
    704623The downside of the precedence rule: @EnumInstType@ $\Rightarrow$ @int ( value )@ $\Rightarrow$ @EnumInstType@ may return a different @EnumInstType@ because the value can be repeated and there is no way to know which one is expected $\Rightarrow$ want uniqueness
    705 
    706 \subsection{Casting}
    707 Casting an EnumInstType to some other type T works similarly to unify the EnumInstType with T. For example:
    708 \begin{lstlisting}
    709 enum( int ) Foo { A = 10, B = 100, C = 1000 };
    710 (int) Foo.A;
    711 \end{lstlisting}
    712 The \CFA-compiler unifies @EnumInstType<int>@ with int, with returns @value( Foo.A )@, which has statically known value 10. In other words, \CFA-compiler is aware of a cast expression, and it forms the context for EnumInstType resolution. The expression with type @EnumInstType<int>@ can be replaced by the compile with a constant expression 10, and optionally discard the cast expression.
    713 
    714 \subsection{Value Conversion}
    715 As discussed in section~\ref{lst:var_declaration}, \CFA only saves @position@ as the necessary information. It is necessary for \CFA to generate intermediate code to retrieve other attributes.
    716 
    717 \begin{lstlisting}
    718 Foo a; // int a;
    719 int j = a;
    720 char * s = a;
    721 \end{lstlisting}
    722 Assume stores a value x, which cannot be statically determined. When assigning a to j in line 2, the compiler @Unify@ j with a, and returns @value( a )@. The generated code for the second line will be
    723 \begin{lstlisting}
    724 int j = value( Foo, a )
    725 \end{lstlisting}
    726 Similarly, the generated code for the third line is
    727 \begin{lstlisting}
    728 char * j = label( Foo, a )
    729 \end{lstlisting}
    730624
    731625\end{document}
Note: See TracChangeset for help on using the changeset viewer.