Changeset de3a579


Ignore:
Timestamp:
May 1, 2024, 1:35:48 PM (6 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
35897fb
Parents:
5c27b6a
Message:

Add trait subsection

File:
1 edited

Legend:

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

    r5c27b6a rde3a579  
    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}
     
    316316\label{f:PlanetExample}
    317317\end{figure}
     318
     319\section{Enum Trait}
     320A typed enum comes with traits capture enumeration charastics and helper functions.
     321
     322\begin{cfa}
     323forall(E) trait Bounded {
     324        E lowerBound();
     325        E upperBound();
     326};
     327\end{cfa}
     328\CFA enums satisfy Bounded trait thanks to the compiler implementing lowerBound() and upperBound(), with
     329lowerBound() returning the first enumerator and upperBound() return the last.
     330
     331\begin{cfa}
     332Workday day1 = lowerBound(); // Monday
     333Planet lastPlanet = upperBound(); // NEPTUNE
     334\end{cfa}
     335
     336Because lowerBound() and upperBound() are overloaded with return types only, calling either functions
     337in a null context cause type ambiguity if than one type implementing Bounded traits, including typed enumerations.
     338\begin{cfa}
     339Workday day1 = lowerBound(); // Okay because rhs hints lowerBound() to return a Workday
     340void foo(Planet p);
     341foo( upperBound() ); Okay because foo's parameter give type hint
     342// lowerBound(); // Error because both Planet and Workday implements Bounded
     343\end{cfa}
     344
     345\begin{cfa}
     346forall(E | Bounded(E)) trait Serial {
     347        unsigned fromInstance(E e);
     348        E fromInt(unsigned i);
     349        E succ(E e);
     350        E pred(E e);
     351};
     352\end{cfa}
     353A Serial type can be mapped to a sequnce of integer. For enum types, fromInstance(E e) is equivalent to
     354posE(E e). Enumerations implement fromInt(), succ(), and pred() with bound() check.
     355For an enum declares N enumerators, fromInt(i) returns the ith enumerator of type E if $0 \leq i < N$.
     356If e is the i-th enumerator, succ(e) returns the i+1-th enumerator if $e != upperBound()$ and pred(e)
     357returns the i-1-th enumerator $e != lowerBound()$. \CFA compile gives an error if bound check fails.
     358
     359\begin{cfa}
     360forall(E, T) trait TypedEnum {
     361        T valueE(E e);
     362        char * labelE(E e);
     363        unsigned int posE(E e);
     364};
     365\end{cfa}
     366
     367The 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.
     368With an assertion on TypedEnum, we can implement functions for all type enums.
     369
     370\begin{cfa}
     371forall( E, T | TypeEnum(E, T))
     372void printEnum(E e) {
     373        sout | "Enum "| labelE(e);
     374}
     375printEunm(MARS);
     376\end{cfa}
     377
     378@<enum.hfa>@ overwrites comparison operators for type enums.
     379\begin{cfa}
     380forall(E, T| TypedEnum(E, T)) {
     381        // comparison
     382        int ?==?(E l, E r);
     383        int ?!=?(E l, E r);
     384        int ?!=?(E l, zero_t);
     385        int ?<?(E l, E r);
     386        int ?<=?(E l, E r);
     387        int ?>?(E l, E r);
     388        int ?>=?(E l, E r);
     389}
     390\end{cfa}
     391These overloaded operators are not defined if the file is not included.
     392In this case, the compiler converts an enumerator to its value, and applies the operators
     393if they are defined for the value type T.
     394
     395\begin{cfa}
     396// if not include <enum.hfa>
     397enum(int) Fruits {
     398        APPLE = 2, BANANA=1, CHERRY=2
     399};
     400APPLE == CHERRY; // True because they have the same Value
     401#include <enum.hfa>
     402APPLE == CHERRY; // False because they are different enumerator
     403\end{cfa}
Note: See TracChangeset for help on using the changeset viewer.