Changeset de3a579
- Timestamp:
- May 1, 2024, 1:35:48 PM (7 months ago)
- Branches:
- master
- Children:
- 35897fb
- Parents:
- 5c27b6a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r5c27b6a rde3a579 229 229 \end{tabular} 230 230 \end{cquote} 231 Here, the intuitive code on the left is implicitly transformed into the sta tndard implementation on the right, using the value of the enumeration variable and enumerators.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. 232 232 However, this implementation is fragile, \eg if the enumeration is changed to: 233 233 \begin{cfa} … … 316 316 \label{f:PlanetExample} 317 317 \end{figure} 318 319 \section{Enum Trait} 320 A typed enum comes with traits capture enumeration charastics and helper functions. 321 322 \begin{cfa} 323 forall(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 329 lowerBound() returning the first enumerator and upperBound() return the last. 330 331 \begin{cfa} 332 Workday day1 = lowerBound(); // Monday 333 Planet lastPlanet = upperBound(); // NEPTUNE 334 \end{cfa} 335 336 Because lowerBound() and upperBound() are overloaded with return types only, calling either functions 337 in a null context cause type ambiguity if than one type implementing Bounded traits, including typed enumerations. 338 \begin{cfa} 339 Workday day1 = lowerBound(); // Okay because rhs hints lowerBound() to return a Workday 340 void foo(Planet p); 341 foo( 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} 346 forall(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} 353 A Serial type can be mapped to a sequnce of integer. For enum types, fromInstance(E e) is equivalent to 354 posE(E e). Enumerations implement fromInt(), succ(), and pred() with bound() check. 355 For an enum declares N enumerators, fromInt(i) returns the ith enumerator of type E if $0 \leq i < N$. 356 If e is the i-th enumerator, succ(e) returns the i+1-th enumerator if $e != upperBound()$ and pred(e) 357 returns the i-1-th enumerator $e != lowerBound()$. \CFA compile gives an error if bound check fails. 358 359 \begin{cfa} 360 forall(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 367 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. 368 With an assertion on TypedEnum, we can implement functions for all type enums. 369 370 \begin{cfa} 371 forall( E, T | TypeEnum(E, T)) 372 void printEnum(E e) { 373 sout | "Enum "| labelE(e); 374 } 375 printEunm(MARS); 376 \end{cfa} 377 378 @<enum.hfa>@ overwrites comparison operators for type enums. 379 \begin{cfa} 380 forall(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} 391 These overloaded operators are not defined if the file is not included. 392 In this case, the compiler converts an enumerator to its value, and applies the operators 393 if they are defined for the value type T. 394 395 \begin{cfa} 396 // if not include <enum.hfa> 397 enum(int) Fruits { 398 APPLE = 2, BANANA=1, CHERRY=2 399 }; 400 APPLE == CHERRY; // True because they have the same Value 401 #include <enum.hfa> 402 APPLE == CHERRY; // False because they are different enumerator 403 \end{cfa}
Note: See TracChangeset
for help on using the changeset viewer.