Changes in / [35897fb:69867ad9]


Ignore:
File:
1 edited

Legend:

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

    r35897fb r69867ad9  
    229229\end{tabular}
    230230\end{cquote}
    231 Here, the intuitive code on the left is implicitly transformed into the standard implementation on the right, using the value of the enumeration variable and enumerators.
     231Here, the intuitive code on the left is implicitly transformed into the statndard implementation on the right, using the value of the enumeration variable and enumerators.
    232232However, this implementation is fragile, \eg if the enumeration is changed to:
    233233\begin{cfa}
     
    333333\label{f:PlanetExample}
    334334\end{figure}
    335 
    336 \section{Enum Trait}
    337 A typed enum comes with traits capture enumeration charastics and helper functions.
    338 
    339 \begin{cfa}
    340 forall(E) trait Bounded {
    341         E lowerBound();
    342         E upperBound();
    343 };
    344 \end{cfa}
    345 \CFA enums satisfy Bounded trait thanks to the compiler implementing lowerBound() and upperBound(), with
    346 lowerBound() returning the first enumerator and upperBound() return the last.
    347 
    348 \begin{cfa}
    349 Workday day1 = lowerBound(); // Monday
    350 Planet lastPlanet = upperBound(); // NEPTUNE
    351 \end{cfa}
    352 
    353 Because lowerBound() and upperBound() are overloaded with return types only, calling either functions
    354 in a null context cause type ambiguity if than one type implementing Bounded traits, including typed enumerations.
    355 \begin{cfa}
    356 Workday day1 = lowerBound(); // Okay because rhs hints lowerBound() to return a Workday
    357 void foo(Planet p);
    358 foo( upperBound() ); Okay because foo's parameter give type hint
    359 // lowerBound(); // Error because both Planet and Workday implements Bounded
    360 \end{cfa}
    361 
    362 \begin{cfa}
    363 forall(E | Bounded(E)) trait Serial {
    364         unsigned fromInstance(E e);
    365         E fromInt(unsigned i);
    366         E succ(E e);
    367         E pred(E e);
    368 };
    369 \end{cfa}
    370 A Serial type can be mapped to a sequnce of integer. For enum types, fromInstance(E e) is equivalent to
    371 posE(E e). Enumerations implement fromInt(), succ(), and pred() with bound() check.
    372 For an enum declares N enumerators, fromInt(i) returns the ith enumerator of type E if $0 \leq i < N$.
    373 If e is the i-th enumerator, succ(e) returns the i+1-th enumerator if $e != upperBound()$ and pred(e)
    374 returns the i-1-th enumerator $e != lowerBound()$. \CFA compile gives an error if bound check fails.
    375 
    376 \begin{cfa}
    377 forall(E, T) trait TypedEnum {
    378         T valueE(E e);
    379         char * labelE(E e);
    380         unsigned int posE(E e);
    381 };
    382 \end{cfa}
    383 
    384 The TypedEnum trait capture three basic attributes of type enums. TypedEnum asserts two types E and T, with T being the base type of enumeration E.
    385 With an assertion on TypedEnum, we can implement functions for all type enums.
    386 
    387 \begin{cfa}
    388 forall( E, T | TypeEnum(E, T))
    389 void printEnum(E e) {
    390         sout | "Enum "| labelE(e);
    391 }
    392 printEunm(MARS);
    393 \end{cfa}
    394 
    395 @<enum.hfa>@ overwrites comparison operators for type enums.
    396 \begin{cfa}
    397 forall(E, T| TypedEnum(E, T)) {
    398         // comparison
    399         int ?==?(E l, E r);
    400         int ?!=?(E l, E r);
    401         int ?!=?(E l, zero_t);
    402         int ?<?(E l, E r);
    403         int ?<=?(E l, E r);
    404         int ?>?(E l, E r);
    405         int ?>=?(E l, E r);
    406 }
    407 \end{cfa}
    408 These overloaded operators are not defined if the file is not included.
    409 In this case, the compiler converts an enumerator to its value, and applies the operators
    410 if they are defined for the value type T.
    411 
    412 \begin{cfa}
    413 // if not include <enum.hfa>
    414 enum(int) Fruits {
    415         APPLE = 2, BANANA=1, CHERRY=2
    416 };
    417 APPLE == CHERRY; // True because they have the same Value
    418 #include <enum.hfa>
    419 APPLE == CHERRY; // False because they are different enumerator
    420 \end{cfa}
Note: See TracChangeset for help on using the changeset viewer.