Changes in / [69867ad9:35897fb]


Ignore:
File:
1 edited

Legend:

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

    r69867ad9 r35897fb  
    229229\end{tabular}
    230230\end{cquote}
    231 Here, 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.
     231Here, 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.
    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}
     337A typed enum comes with traits capture enumeration charastics and helper functions.
     338
     339\begin{cfa}
     340forall(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
     346lowerBound() returning the first enumerator and upperBound() return the last.
     347
     348\begin{cfa}
     349Workday day1 = lowerBound(); // Monday
     350Planet lastPlanet = upperBound(); // NEPTUNE
     351\end{cfa}
     352
     353Because lowerBound() and upperBound() are overloaded with return types only, calling either functions
     354in a null context cause type ambiguity if than one type implementing Bounded traits, including typed enumerations.
     355\begin{cfa}
     356Workday day1 = lowerBound(); // Okay because rhs hints lowerBound() to return a Workday
     357void foo(Planet p);
     358foo( 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}
     363forall(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}
     370A Serial type can be mapped to a sequnce of integer. For enum types, fromInstance(E e) is equivalent to
     371posE(E e). Enumerations implement fromInt(), succ(), and pred() with bound() check.
     372For an enum declares N enumerators, fromInt(i) returns the ith enumerator of type E if $0 \leq i < N$.
     373If e is the i-th enumerator, succ(e) returns the i+1-th enumerator if $e != upperBound()$ and pred(e)
     374returns the i-1-th enumerator $e != lowerBound()$. \CFA compile gives an error if bound check fails.
     375
     376\begin{cfa}
     377forall(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
     384The 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.
     385With an assertion on TypedEnum, we can implement functions for all type enums.
     386
     387\begin{cfa}
     388forall( E, T | TypeEnum(E, T))
     389void printEnum(E e) {
     390        sout | "Enum "| labelE(e);
     391}
     392printEunm(MARS);
     393\end{cfa}
     394
     395@<enum.hfa>@ overwrites comparison operators for type enums.
     396\begin{cfa}
     397forall(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}
     408These overloaded operators are not defined if the file is not included.
     409In this case, the compiler converts an enumerator to its value, and applies the operators
     410if they are defined for the value type T.
     411
     412\begin{cfa}
     413// if not include <enum.hfa>
     414enum(int) Fruits {
     415        APPLE = 2, BANANA=1, CHERRY=2
     416};
     417APPLE == CHERRY; // True because they have the same Value
     418#include <enum.hfa>
     419APPLE == CHERRY; // False because they are different enumerator
     420\end{cfa}
Note: See TracChangeset for help on using the changeset viewer.