Changeset d69f7114 for doc/theses


Ignore:
Timestamp:
May 3, 2024, 9:33:58 AM (8 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
164a6b6, 297b796
Parents:
72713e5
Message:

proofread section Enumeration Trait, and a few other wording changes

Location:
doc/theses/jiada_liang_MMath
Files:
2 edited

Legend:

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

    r72713e5 rd69f7114  
    155155
    156156
     157\section{Enumerator Opaque Type}
     158
     159\CFA provides a special opaque enumeration type, where the internal representation is chosen by the compiler and only equality operations are available.
     160\begin{cfa}
     161enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE };
     162\end{cfa}
     163
     164
    157165\section{Enumeration Inheritance}
    158166
     
    262270
    263271
     272\section{Enumeration Trait}
     273
     274The header file \lstinline[deletekeywords=enum]{<enum.hfa>} defines the set of traits containing operators and helper functions for @enum@.
     275A \CFA enumeration satisfies all of these traits allowing it to interact with runtime features in \CFA.
     276Each trait is discussed in detail.
     277
     278The trait @Bounded@:
     279\begin{cfa}
     280forall( E ) trait Bounded {
     281        E first();
     282        E last();
     283};
     284\end{cfa}
     285defines the bounds of the enumeration, where @first()@ returns the first enumerator and @last()@ returns the last, \eg:
     286\begin{cfa}
     287Workday day = first();                                  $\C{// Mon}$
     288Planet outermost = last();                              $\C{// NEPTUNE}$
     289\end{cfa}
     290@first()@ and @last()@ are overloaded with return types only, so in the example, the enumeration type is found on the left-hand side of the assignment.
     291Calling either functions without a context results in a type ambiguity, except in the rare case where the type environment has only one enumeration.
     292\begin{cfa}
     293@first();@                                                              $\C{// ambiguous Workday and Planet implement Bounded}$
     294sout | @last()@;
     295Workday day = first();                                  $\C{// day provides type Workday}$
     296void foo( Planet p );
     297foo( last() );                                                  $\C{// parameter provides type Planet}$
     298\end{cfa}
     299
     300The trait @Serial@:
     301\begin{cfa}
     302forall( E | Bounded( E ) ) trait Serial {
     303        unsigned fromInstance( E e );
     304        E fromInt( unsigned int posn );
     305        E succ( E e );
     306        E pred( E e );
     307};
     308\end{cfa}
     309is a @Bounded@ trait, where elements can be mapped to an integer sequence.
     310A type @T@ matching @Serial@ can project to an unsigned @int@ type, \ie an instance of type T has a corresponding integer value.
     311%However, the inverse may not be possible, and possible requires a bound check.
     312The mapping from a serial type to integer is defined by @fromInstance@, which returns the enumerator's position.
     313The inverse operation is @fromInt@, which performs a bound check using @first()@ and @last()@ before casting the integer into an enumerator.
     314Specifically, for enumerator @E@ declaring $N$ enumerators, @fromInt( i )@ returns the $i-1_{th}$ enumerator, if $0 \leq i < N$, or raises the exception @enumBound@.
     315
     316The @Serial@ trait also requires interface functions @succ( E e )@ and @pred( E e )@ be implemented for a serial type, which imply the enumeration positions are consecutive and ordinal.
     317Specifically, if @e@ is the $i_{th}$ enumerator, @succ( e )@ returns the $i+1_{th}$ enumerator when $e \ne last()$, and @pred( e )@ returns the $i-1_{th}$ enumerator when $e \ne first()$.
     318The exception @enumRange@ is raised if the result of either operation is outside the range of type @E@.
     319
     320The trait @TypedEnum@:
     321\begin{cfa}
     322forall( E, T ) trait TypedEnum {
     323        T valueE( E e );
     324        char * labelE( E e );
     325        unsigned int posE( E e );
     326};
     327\end{cfa}
     328captures three basic attributes of an enumeration type: value, label, and position.
     329@TypedEnum@ asserts two types @E@ and @T@, with @T@ being the base type of the enumeration @E@, \eg @enum( T ) E { ... };@.
     330Implementing general functions across all enumeration types is possible by asserting @TypeEnum( E, T )@, \eg:
     331\begin{cfa}
     332forall( E, T | TypeEnum( E, T ) )
     333void printEnum( E e ) {
     334        sout | "Enum "| labelE( e );
     335}
     336printEunm( MARS );
     337\end{cfa}
     338
     339Finally, there is an associated trait defining comparison operators among enumerators.
     340\begin{cfa}
     341forall( E, T | TypedEnum( E, T ) ) {
     342        // comparison
     343        int ?==?( E l, E r );           $\C{// true if l and r are same enumerators}$
     344        int ?!=?( E l, E r );           $\C{// true if l and r are different enumerators}$
     345        int ?!=?( E l, zero_t );        $\C{// true if l is not the first enumerator}$
     346        int ?<?( E l, E r );            $\C{// true if l is an enumerator before r}$
     347        int ?<=?( E l, E r );           $\C{// true if l before or the same as r}$
     348        int ?>?( E l, E r );            $\C{// true if l is an enumerator after r}$
     349        int ?>=?( E l, E r );           $\C{// true if l after or the same as r}$         
     350}
     351\end{cfa}
     352Note, the overloaded operators are defined only when the header @<enum.hfa>@ is included.
     353If not, the compiler converts an enumerator to its value, and applies the operators defined for the value type @E@, \eg:
     354\begin{cfa}
     355// if not include <enum.hfa>
     356enum( int ) Fruits { APPLE = 2, BANANA = 10, CHERRY = 2 };
     357APPLE == CHERRY; // true because valueE( APPLE ) == valueE( CHERRY )
     358
     359#include <enum.hfa>
     360APPLE == CHERRY; // false because posE( APPLE ) != posE( CHERRY )
     361\end{cfa}
     362An enumerator returns its @position@ by default.
     363In particular, @printf( ... )@ from @<stdio.h>@ functions provides no context to its parameter type, so it prints @position@.
     364On the other hand, the pipeline operator @?|?( ostream os, E enumType )@ provides type context for type @E@, and \CFA has overwritten this operator to print the enumeration @value@ over @position@.
     365\begin{cfa}
     366printf( "Position of BANANA is \%d", BANANA ); // Position of BANANA is 1
     367sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10
     368\end{cfa}
     369Programmers can overwrite this behaviour by overloading the pipeline operator themselves.
     370\PAB{This needs discussing because including \lstinline{<enum.hfa>} can change the entire meaning of a program.}
     371
     372
    264373\section{Planet Example}
    265374
     
    270379Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@.
    271380The program main uses @SizeE@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator.
    272 The resulting random orbital body is used in a @choose@ statement.
     381The resulting random orbital-body is used in a \CFA @choose@ statement.
    273382The enumerators in the @case@ clause use position for testing.
    274 The prints use @labelE@ to print the enumerators label.
    275 Finally, a loop iterates through the planets computing the weight on each planet for a given earth weight.
     383The prints use @labelE@ to print an enumerator's label.
     384Finally, a loop iterates through the planets computing the weight on each planet for a given earth mass.
    276385The print statement does an equality comparison with an enumeration variable and enumerator.
    277386
     
    294403enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$
    295404static double surfaceGravity( Planet p ) @with( p )@ {
    296         return G * mass / ( radius \ 2u ); $\C{// exponentiation}$
     405        return G * mass / ( radius \ 2 ); $\C{// exponentiation}$
    297406}
    298407static double surfaceWeight( Planet p, double otherMass ) {
     
    302411        if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight";
    303412        double earthWeight = convert( argv[1] );
    304         double mass = earthWeight / surfaceGravity( EARTH );
     413        double earthMass = earthWeight / surfaceGravity( EARTH );
    305414
    306415        Planet p = @fromInt@( prng( @SizeE@(Planet) ) ); $\C{// select a random orbiting body}$
     
    315424        for ( @p; Planet@ ) {
    316425                sout | "Your weight on" | (@p == MOON@ ? "the" : "") | labelE(p)
    317                            | "is" | wd(1,1, surfaceWeight( p, mass )) | "kg";
     426                           | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg";
    318427        }
    319428}
     
    333442\label{f:PlanetExample}
    334443\end{figure}
    335 
    336 \section{Enum Trait}
    337 The header file @<enum.hfa>@ defines traits implemented by, and sets of operators and helper functions defined for enum.
    338 
    339 \begin{figure}
    340 \small
    341 \begin{cfa}
    342 forall(E) trait Bounded {
    343         E lowerBound();
    344         E upperBound();
    345 };
    346 \end{cfa}
    347 \caption{f:Bounded Trait}
    348 \label{f:BoundedTrait}
    349 \end{figure}
    350 
    351 \CFA enums satisfy Bounded trait thanks to the default implemention of @lowerBound()@ and @upperBound()@, with
    352 @lowerBound()@ returning the first enumerator and @upperBound()@ returning the last.
    353 
    354 \begin{figure}
    355 \small
    356 \begin{cfa}
    357 Workday day1 = lowerBound(); // Monday
    358 Planet lastPlanet = upperBound(); // NEPTUNE
    359 \end{cfa}
    360 \caption{f:Bound Functions}
    361 \label{f:BoundFunctions}
    362 \end{figure}
    363 
    364 @lowerBound()@ and @upperBound()@ are overloaded with return types only. Calling either functions
    365 without a context can result in type ambiguity if more than one types implement the functions from the @Bounded@ traits.
    366 Because \CFA enum types implements @Bounded@, multiple type enumerations in the same lexical scope will
    367 cause ambiguity on @lowerBound()@ and @upperBound()@ \see{\VRef{f:BoundFunctions} }.
    368 
    369 \begin{figure}
    370 \small
    371 \begin{cfa}
    372 // lowerBound(); // Error because both Planet and Workday implement Bounded
    373 Workday day1 = lowerBound(); // Okay because type of day1 hints lowerBound() to return a Workday
    374 void foo(Planet p);
    375 foo( upperBound() ); Okay because foo expects value with type Planet as parameter
    376 \end{cfa}
    377 \caption{Bound Function ambiguity}
    378 \label{f:BoundAmbiguity}
    379 \end{figure}
    380 
    381 \begin{figure}
    382 \small
    383 \begin{cfa}
    384 forall(E | Bounded(E)) trait Serial {
    385         unsigned fromInstance(E e);
    386         E fromInt(unsigned i);
    387         E succ(E e);
    388         E pred(E e);
    389 };
    390 \end{cfa}
    391 \caption{Bounded Trait}
    392 \label{f:BoundedTrait}
    393 \end{figure}
    394 
    395 A Serial type is a @Bounded@ type where elements can be mapped to an integer sequence.
    396 A serial type @T@ can project to the integer type: an instance of type T has a corresponding integer value,
    397 but the inverse may not be possible, and possible requires a bound check.
    398 
    399 The mapping from a serial type to integer is defined as @fromInstance(E e)@.
    400 For enum types, @fromInstance(E e)@ is equivalent to getting the position of e.
    401 The inverse of @fromInstance(E e)@ is @fromInt(unsigned i)@ and \CFA implements it with a bound check.
    402 For an enum @E@ declares N enumerators, @fromInt(i)@ returns the $i-1_{th}$ enumerator if $0 \leq i < N$, or error otherwises.
    403 
    404 The Serial trait provides interface functions @succ(E e)@ and @pred(E e)@ as members of a serial type are consecutive and ordinal.
    405 If @e@ is the $i_{th}$ enumerator, @succ(e)@ returns the $i+1_{th}$ enumerator when $e != upperBound()$, and @pred(e)@
    406 returns the $i-1_{th}$ enumerator when $e != lowerBound()$.
    407 \CFA compiler gives an error if the result of the operation is out the domain of type @T@.
    408 
    409 \begin{figure}
    410 \small
    411 \begin{cfa}
    412 forall(E, T) trait TypedEnum {
    413         T valueE(E e);
    414         char * labelE(E e);
    415         unsigned int posE(E e);
    416 };
    417 \end{cfa}
    418 \caption{Type Enum Trait}
    419 \label{f:TypeEnumTrait}
    420 \end{figure}
    421 
    422 The @TypedEnum@ trait captures three basic attributes of a type enum: value, label, and position.
    423 TypedEnum asserts two types @E@ and @T@, with @T@ being the base type of enumeration @E@.
    424 Implementing functions for general type enums is possible by asserting @TypeEnum(E, T)@.
    425 
    426 \begin{figure}
    427 \small
    428 \begin{cfa}
    429 forall( E, T | TypeEnum(E, T))
    430 void printEnum(E e) {
    431         sout | "Enum "| labelE(e);
    432 }
    433 printEunm(MARS);
    434 \end{cfa}
    435 \caption{Implement Functions for Enums}
    436 \label{f:ImplementEnumFunction}
    437 \end{figure}
    438 
    439 @<enum.hfa>@ overwrites comparison operators for type enums.
    440 \begin{figure}
    441 \small
    442 \begin{cfa}
    443 forall(E, T| TypedEnum(E, T)) {
    444         // comparison
    445         int ?==?(E l, E r); // returns True if l and r are the same enumerator
    446         int ?!=?(E l, E r); // returns True if l and r are the different enumerator
    447         int ?!=?(E l, zero_t); // return True if l is not the first enumerator
    448         int ?<?(E l, E r); // return True if l is an enuemerator before r
    449         int ?<=?(E l, E r); // return True if l==r or l<r
    450         int ?>?(E l, E r); // return True if l is an enuemrator after r
    451         int ?>=?(E l, E r); // return True if l>r or l==r
    452 }
    453 \end{cfa}
    454 \caption{Enum Operators}
    455 \label{f:EnumOperators}
    456 \end{figure}
    457 The overloaded operators in \ref{f:EnumOperators} are defined when and only when the header @<enum.hfa>@ is included.
    458 If not, the compiler converts an enumerator to its value, and applies the operators defined for the value type @T@.
    459 
    460 \begin{figure}
    461 \small
    462 \begin{cfa}
    463 // if not include <enum.hfa>
    464 enum(int) Fruits {
    465         APPLE = 2, BANANA=10, CHERRY=2
    466 };
    467 APPLE == CHERRY; // True because valueE(APPLE) == valueE(CHERRY)
    468 #include <enum.hfa>
    469 APPLE == CHERRY; // False because they are different enumerator
    470 \end{cfa}
    471 \caption{Include <enum.hfa>}
    472 \label{f:IncludeHeader}
    473 \end{figure}
    474 
    475 An enumerator returns its @position@ by default. In particular,
    476 @printf(...)@ from @<stdio.h>@ functions provides no context to its parameter type,
    477 so it prints @position@.
    478 
    479 On the other hand, the pipeline operator @?|?(ostream os, E enumType)@ provides type context for type @E@, and \CFA
    480 has overwritten this operator to prints enumeration @value@ over @position@.
    481 \begin{figure}
    482 \small
    483 \begin{cfa}
    484 printf("Position of BANANA is \%d", BANANA); // Postion of BANANA is 1
    485 sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10
    486 \end{cfa}
    487 \caption{Print Enums}
    488 \label{f:PrintEnum}
    489 \end{figure}
    490 
    491 Programmers can overwrite this behaviour by overloading the pipeline operator themselve.
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r72713e5 rd69f7114  
    218218
    219219\CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization.
    220 (Note, a \CC @constexpr@ declaration is the same @const@ with the restriction that the initialization is a compile-time expression.)
     220(Note, a \CC @constexpr@ declaration is the same as @const@ with the restriction that the initialization is a compile-time expression.)
    221221\begin{c++}
    222222const @auto@ one = 0 + 1;                               $\C{// static initialization}$
     
    241241whereas C @const@ declarations without @static@ are marked @R@.
    242242
    243 The following non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:C++}}.
     243The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:C++}}.
    244244\begin{cquote}
    245245Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
Note: See TracChangeset for help on using the changeset viewer.