Changeset d69f7114 for doc/theses
- Timestamp:
- May 3, 2024, 9:33:58 AM (8 months ago)
- Branches:
- master
- Children:
- 164a6b6, 297b796
- Parents:
- 72713e5
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r72713e5 rd69f7114 155 155 156 156 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} 161 enum@()@ Planets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE }; 162 \end{cfa} 163 164 157 165 \section{Enumeration Inheritance} 158 166 … … 262 270 263 271 272 \section{Enumeration Trait} 273 274 The header file \lstinline[deletekeywords=enum]{<enum.hfa>} defines the set of traits containing operators and helper functions for @enum@. 275 A \CFA enumeration satisfies all of these traits allowing it to interact with runtime features in \CFA. 276 Each trait is discussed in detail. 277 278 The trait @Bounded@: 279 \begin{cfa} 280 forall( E ) trait Bounded { 281 E first(); 282 E last(); 283 }; 284 \end{cfa} 285 defines the bounds of the enumeration, where @first()@ returns the first enumerator and @last()@ returns the last, \eg: 286 \begin{cfa} 287 Workday day = first(); $\C{// Mon}$ 288 Planet 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. 291 Calling 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}$ 294 sout | @last()@; 295 Workday day = first(); $\C{// day provides type Workday}$ 296 void foo( Planet p ); 297 foo( last() ); $\C{// parameter provides type Planet}$ 298 \end{cfa} 299 300 The trait @Serial@: 301 \begin{cfa} 302 forall( 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} 309 is a @Bounded@ trait, where elements can be mapped to an integer sequence. 310 A 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. 312 The mapping from a serial type to integer is defined by @fromInstance@, which returns the enumerator's position. 313 The inverse operation is @fromInt@, which performs a bound check using @first()@ and @last()@ before casting the integer into an enumerator. 314 Specifically, 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 316 The @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. 317 Specifically, 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()$. 318 The exception @enumRange@ is raised if the result of either operation is outside the range of type @E@. 319 320 The trait @TypedEnum@: 321 \begin{cfa} 322 forall( E, T ) trait TypedEnum { 323 T valueE( E e ); 324 char * labelE( E e ); 325 unsigned int posE( E e ); 326 }; 327 \end{cfa} 328 captures 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 { ... };@. 330 Implementing general functions across all enumeration types is possible by asserting @TypeEnum( E, T )@, \eg: 331 \begin{cfa} 332 forall( E, T | TypeEnum( E, T ) ) 333 void printEnum( E e ) { 334 sout | "Enum "| labelE( e ); 335 } 336 printEunm( MARS ); 337 \end{cfa} 338 339 Finally, there is an associated trait defining comparison operators among enumerators. 340 \begin{cfa} 341 forall( 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} 352 Note, the overloaded operators are defined only when the header @<enum.hfa>@ is included. 353 If 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> 356 enum( int ) Fruits { APPLE = 2, BANANA = 10, CHERRY = 2 }; 357 APPLE == CHERRY; // true because valueE( APPLE ) == valueE( CHERRY ) 358 359 #include <enum.hfa> 360 APPLE == CHERRY; // false because posE( APPLE ) != posE( CHERRY ) 361 \end{cfa} 362 An enumerator returns its @position@ by default. 363 In particular, @printf( ... )@ from @<stdio.h>@ functions provides no context to its parameter type, so it prints @position@. 364 On 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} 366 printf( "Position of BANANA is \%d", BANANA ); // Position of BANANA is 1 367 sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10 368 \end{cfa} 369 Programmers 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 264 373 \section{Planet Example} 265 374 … … 270 379 Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. 271 380 The 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.381 The resulting random orbital-body is used in a \CFA @choose@ statement. 273 382 The 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.383 The prints use @labelE@ to print an enumerator's label. 384 Finally, a loop iterates through the planets computing the weight on each planet for a given earth mass. 276 385 The print statement does an equality comparison with an enumeration variable and enumerator. 277 386 … … 294 403 enum( double ) { G = 6.6743_E-11 }; $\C{// universal gravitational constant (m3 kg-1 s-2)}$ 295 404 static double surfaceGravity( Planet p ) @with( p )@ { 296 return G * mass / ( radius \ 2 u); $\C{// exponentiation}$405 return G * mass / ( radius \ 2 ); $\C{// exponentiation}$ 297 406 } 298 407 static double surfaceWeight( Planet p, double otherMass ) { … … 302 411 if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight"; 303 412 double earthWeight = convert( argv[1] ); 304 double mass = earthWeight / surfaceGravity( EARTH );413 double earthMass = earthWeight / surfaceGravity( EARTH ); 305 414 306 415 Planet p = @fromInt@( prng( @SizeE@(Planet) ) ); $\C{// select a random orbiting body}$ … … 315 424 for ( @p; Planet@ ) { 316 425 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"; 318 427 } 319 428 } … … 333 442 \label{f:PlanetExample} 334 443 \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 \small341 \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()@, with352 @lowerBound()@ returning the first enumerator and @upperBound()@ returning the last.353 354 \begin{figure}355 \small356 \begin{cfa}357 Workday day1 = lowerBound(); // Monday358 Planet lastPlanet = upperBound(); // NEPTUNE359 \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 functions365 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 will367 cause ambiguity on @lowerBound()@ and @upperBound()@ \see{\VRef{f:BoundFunctions} }.368 369 \begin{figure}370 \small371 \begin{cfa}372 // lowerBound(); // Error because both Planet and Workday implement Bounded373 Workday day1 = lowerBound(); // Okay because type of day1 hints lowerBound() to return a Workday374 void foo(Planet p);375 foo( upperBound() ); Okay because foo expects value with type Planet as parameter376 \end{cfa}377 \caption{Bound Function ambiguity}378 \label{f:BoundAmbiguity}379 \end{figure}380 381 \begin{figure}382 \small383 \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 \small411 \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 \small428 \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 \small442 \begin{cfa}443 forall(E, T| TypedEnum(E, T)) {444 // comparison445 int ?==?(E l, E r); // returns True if l and r are the same enumerator446 int ?!=?(E l, E r); // returns True if l and r are the different enumerator447 int ?!=?(E l, zero_t); // return True if l is not the first enumerator448 int ?<?(E l, E r); // return True if l is an enuemerator before r449 int ?<=?(E l, E r); // return True if l==r or l<r450 int ?>?(E l, E r); // return True if l is an enuemrator after r451 int ?>=?(E l, E r); // return True if l>r or l==r452 }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 \small462 \begin{cfa}463 // if not include <enum.hfa>464 enum(int) Fruits {465 APPLE = 2, BANANA=10, CHERRY=2466 };467 APPLE == CHERRY; // True because valueE(APPLE) == valueE(CHERRY)468 #include <enum.hfa>469 APPLE == CHERRY; // False because they are different enumerator470 \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 \CFA480 has overwritten this operator to prints enumeration @value@ over @position@.481 \begin{figure}482 \small483 \begin{cfa}484 printf("Position of BANANA is \%d", BANANA); // Postion of BANANA is 1485 sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10486 \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 218 218 219 219 \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.) 221 221 \begin{c++} 222 222 const @auto@ one = 0 + 1; $\C{// static initialization}$ … … 241 241 whereas C @const@ declarations without @static@ are marked @R@. 242 242 243 The following non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:C++}}.243 The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:C++}}. 244 244 \begin{cquote} 245 245 Change: \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.