Changeset acb33f15 for doc/theses/jiada_liang_MMath
- Timestamp:
- May 13, 2024, 10:26:52 AM (7 months ago)
- Branches:
- master
- Children:
- 31f4837
- Parents:
- 41c8312
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r41c8312 racb33f15 70 70 As in Section~\ref{s:EnumeratorUnscoping}, opening multiple scoped enumerations in a @with@ can result in duplicate enumeration names, but \CFA implicit type resolution and explicit qualification/casting handles ambiguities. 71 71 72 73 \section{Enumeration Pseudo-functions} 74 75 Pseudo-functions are function-like operators that do not result in any run-time computations, \ie like @sizeof@, @alignof@, @typeof@. 76 A pseudo-function call is often substituted with information extracted from the compilation symbol-table, like storage size or alignment associated with the underlying architecture. 77 78 The attributes of an enumerator are accessed by pseudo-functions @posE@, @valueE@, and @labelE@. 79 \begin{cfa} 80 int jane_pos = @posE@( Names.Jane ); $\C{// 2}$ 81 char * jane_value = @valueE@( Names.Jane ); $\C{// "JANE"}$ 82 char * jane_label = @labelE@( Names.Jane ); $\C{// "Jane"}$ 83 sout | posE( Names.Jane) | labelE( Names.Jane ) | valueE( Names.Jane ); 84 \end{cfa} 85 Note the ability to print all of an enumerator's properties. 72 \section{Enumeration Trait} 73 74 The header file \lstinline[deletekeywords=enum]{<enum.hfa>} defines the set of traits containing operators and helper functions for @enum@. 75 A \CFA enumeration satisfies all of these traits allowing it to interact with runtime features in \CFA. 76 Each trait is discussed in detail. 77 78 The trait @Bounded@: 79 \begin{cfa} 80 forall( E ) trait Bounded { 81 E first(); 82 E last(); 83 }; 84 \end{cfa} 85 defines the bounds of the enumeration, where @first()@ returns the first enumerator and @last()@ returns the last, \eg: 86 \begin{cfa} 87 Workday day = first(); $\C{// Mon}$ 88 Planet outermost = last(); $\C{// NEPTUNE}$ 89 \end{cfa} 90 @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. 91 Calling either functions without a context results in a type ambiguity, except in the rare case where the type environment has only one enumeration. 92 \begin{cfa} 93 @first();@ $\C{// ambiguous Workday and Planet implement Bounded}$ 94 sout | @last()@; 95 Workday day = first(); $\C{// day provides type Workday}$ 96 void foo( Planet p ); 97 foo( last() ); $\C{// parameter provides type Planet}$ 98 \end{cfa} 99 100 The trait @Serial@: 101 \begin{cfa} 102 forall( E | Bounded( E ) ) trait Serial { 103 unsigned fromInstance( E e ); 104 E fromInt( unsigned int posn ); 105 E succ( E e ); 106 E pred( E e ); 107 }; 108 \end{cfa} 109 is a @Bounded@ trait, where elements can be mapped to an integer sequence. 110 A type @T@ matching @Serial@ can project to an unsigned @int@ type, \ie an instance of type T has a corresponding integer value. 111 %However, the inverse may not be possible, and possible requires a bound check. 112 The mapping from a serial type to integer is defined by @fromInstance@, which returns the enumerator's position. 113 The inverse operation is @fromInt@, which performs a bound check using @first()@ and @last()@ before casting the integer into an enumerator. 114 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@. 115 116 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. 117 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()$. 118 The exception @enumRange@ is raised if the result of either operation is outside the range of type @E@. 119 120 The trait @TypedEnum@: 121 \begin{cfa} 122 forall( E, T ) trait TypedEnum { 123 T valueE( E e ); 124 char * labelE( E e ); 125 unsigned int posE( E e ); 126 }; 127 \end{cfa} 128 captures three basic attributes of an enumeration type: value, label, and position. 129 @TypedEnum@ asserts two types @E@ and @T@, with @T@ being the base type of the enumeration @E@, \eg @enum( T ) E { ... };@. 130 Implementing general functions across all enumeration types is possible by asserting @TypeEnum( E, T )@, \eg: 131 \begin{cfa} 132 forall( E, T | TypeEnum( E, T ) ) 133 void printEnum( E e ) { 134 sout | "Enum "| labelE( e ); 135 } 136 printEunm( MARS ); 137 \end{cfa} 138 139 Finally, there is an associated trait defining comparison operators among enumerators. 140 \begin{cfa} 141 forall( E, T | TypedEnum( E, T ) ) { 142 // comparison 143 int ?==?( E l, E r ); $\C{// true if l and r are same enumerators}$ 144 int ?!=?( E l, E r ); $\C{// true if l and r are different enumerators}$ 145 int ?!=?( E l, zero_t ); $\C{// true if l is not the first enumerator}$ 146 int ?<?( E l, E r ); $\C{// true if l is an enumerator before r}$ 147 int ?<=?( E l, E r ); $\C{// true if l before or the same as r}$ 148 int ?>?( E l, E r ); $\C{// true if l is an enumerator after r}$ 149 int ?>=?( E l, E r ); $\C{// true if l after or the same as r}$ 150 } 151 \end{cfa} 152 Note, the overloaded operators are defined only when the header @<enum.hfa>@ is included. 153 If not, the compiler converts an enumerator to its value, and applies the operators defined for the value type @E@, \eg: 154 \begin{cfa} 155 // if not include <enum.hfa> 156 enum( int ) Fruits { APPLE = 2, BANANA = 10, CHERRY = 2 }; 157 APPLE == CHERRY; // true because valueE( APPLE ) == valueE( CHERRY ) 158 159 #include <enum.hfa> 160 APPLE == CHERRY; // false because posE( APPLE ) != posE( CHERRY ) 161 \end{cfa} 162 An enumerator returns its @position@ by default. 163 In particular, @printf( ... )@ from @<stdio.h>@ functions provides no context to its parameter type, so it prints @position@. 164 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@. 165 \begin{cfa} 166 printf( "Position of BANANA is \%d", BANANA ); // Position of BANANA is 1 167 sout | "Value of BANANA is " | BANANA; // Value of BANANA is 10 168 \end{cfa} 169 Programmers can overwrite this behaviour by overloading the pipeline operator themselves. 170 \PAB{This needs discussing because including \lstinline{<enum.hfa>} can change the entire meaning of a program.} 171 172 173 % \section{Enumeration Pseudo-functions} 174 175 % Pseudo-functions are function-like operators that do not result in any run-time computations, \ie like @sizeof@, @alignof@, @typeof@. 176 % A pseudo-function call is often substituted with information extracted from the compilation symbol-table, like storage size or alignment associated with the underlying architecture. 177 178 % The attributes of an enumerator are accessed by pseudo-functions @posE@, @valueE@, and @labelE@. 179 % \begin{cfa} 180 % int jane_pos = @posE@( Names.Jane ); $\C{// 2}$ 181 % char * jane_value = @valueE@( Names.Jane ); $\C{// "JANE"}$ 182 % char * jane_label = @labelE@( Names.Jane ); $\C{// "Jane"}$ 183 % sout | posE( Names.Jane) | labelE( Names.Jane ) | valueE( Names.Jane ); 184 % \end{cfa} 185 % Note the ability to print all of an enumerator's properties. 186 86 187 87 188 … … 270 371 271 372 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 373 374 \section{Enumerated Arrays} 375 Enumerated array use an \CFA array as their index. 376 \begin{cfa} 377 enum() Colour { 378 Red, Orange, Yellow, Green, Blue, Indigo, Violet 379 }; 380 381 string colourCode[Colour] = { "#e81416", "#ffa500", "#ffa500", "#ffa500", "#487de7", "#4b369d", "#70369d" }; 382 sout | "Colour Code of Orange is " | colourCode[Orange]; 383 \end{cfa} 372 384 373 385 \section{Planet Example}
Note: See TracChangeset
for help on using the changeset viewer.