[38e20a80] | 1 | \chapter{Enumeration Traits} |
---|
| 2 | \label{c:trait} |
---|
[956299b] | 3 | |
---|
[21f4dff] | 4 | % Despite parametric polymorphism being a pivotal feature of \CFA, for a long time, there was not |
---|
| 5 | % a technique to write functions being polymorphic over enumerated types. |
---|
| 6 | \CC introduced @std::is_enum@ trait on \CC{11} and @concepts@ on \CC{20}; with the combination, users can |
---|
| 7 | write function polymorphic over enumerated type in \CC: |
---|
| 8 | \begin{cfa} |
---|
| 9 | #include <type_traits> |
---|
| 10 | |
---|
| 11 | template<typename T> |
---|
| 12 | @concept Enumerable = std::is_enum<T>::value;@ |
---|
| 13 | |
---|
| 14 | template<@Enumerable@ T> |
---|
| 15 | void f(T) {} |
---|
| 16 | \end{cfa} |
---|
| 17 | The @std::is_enum@ and other \CC @traits@ are a compile-time interfaces to query type information. |
---|
| 18 | While named the same as @trait@, it is orthogonal to \CFA trait, as the latter being defined as |
---|
| 19 | a collection of assertion to be satisfied by a polymorphic type. |
---|
[d1276f8] | 20 | |
---|
[21f4dff] | 21 | \CFA provides @CfaEnum@ and @TypedEnum@ traits to supports polymorphic functions for \CFA enumeration: |
---|
| 22 | \begin{cfa} |
---|
| 23 | forall(T | @CfaEnum(T)@) |
---|
| 24 | void f(T) {} |
---|
| 25 | \end{cfa} |
---|
| 26 | |
---|
| 27 | \section{CfaEnum and TypedEnum} |
---|
[38e20a80] | 28 | \CFA defines attribute functions @label()@ and @posn()@ for all \CFA enumerations, |
---|
| 29 | and therefore \CFA enumerations fulfills the type assertions with the combination. |
---|
| 30 | With the observation, we define trait @CfaEnum@: |
---|
[d1276f8] | 31 | \begin{cfa} |
---|
| 32 | forall( E ) trait CfaEnum { |
---|
| 33 | const char * @label@( E e ); |
---|
| 34 | unsigned int @posn@( E e ); |
---|
| 35 | }; |
---|
| 36 | \end{cfa} |
---|
| 37 | |
---|
[38e20a80] | 38 | % The trait @TypedEnum@ extends @CfaEnum@ with an additional value() assertion: |
---|
| 39 | Typed enumerations are \CFA enumeration with an additional @value@ attribute. Extending |
---|
| 40 | CfaEnum traits, TypedEnum is a subset of CFAEnum that implements attribute function @value()@, |
---|
| 41 | which includes all typed enumerations. |
---|
[d1276f8] | 42 | \begin{cfa} |
---|
| 43 | forall( E, V | CfaEnum( E ) ) trait TypedEnum { |
---|
| 44 | V @value@( E e ); |
---|
| 45 | }; |
---|
| 46 | \end{cfa} |
---|
[38e20a80] | 47 | Type parameter V of TypedEnum trait binds to return type of @value()@, which is also the base |
---|
| 48 | type for typed enumerations. CfaEnum and TypedEnum triats constitues a CfaEnum function interfaces, as well a way to define functions |
---|
| 49 | over all CfaEnum enumerations. |
---|
[d1276f8] | 50 | \begin{cfa} |
---|
[38e20a80] | 51 | // for all type E that implements value() to return type T, where T is a type that convertible to string |
---|
| 52 | forall( E, T | TypedEnum( E, T ) | { ?{}(string &, T ); } ) |
---|
| 53 | string format_enum( E e ) { return label(E) + "(" + string(value(e)) + ")"; } |
---|
[d1276f8] | 54 | |
---|
[38e20a80] | 55 | // int is convertible to string; implemented in the standard library |
---|
| 56 | enum(int) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF }; |
---|
| 57 | |
---|
| 58 | struct color_code { int R; int G; int B }; |
---|
| 59 | // Implement color_code to string conversion |
---|
| 60 | ?{}(string & this, struct color_code p ) { |
---|
| 61 | this = string(p.R) + ',' + string(p.G) + ',' + string(p.B); |
---|
[d1276f8] | 62 | } |
---|
[38e20a80] | 63 | enum(color_code) Rainbow { |
---|
| 64 | Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, |
---|
| 65 | Blue = {0, 0, 255}, Indigo = {75, 0, 130}, Purple = {148, 0, 211} |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | format_enum(RGB.Green); // "Green(65280)" |
---|
| 69 | format_enum(Rainbow.Green); // "Green(0,255,0)" |
---|
[d1276f8] | 70 | \end{cfa} |
---|
| 71 | |
---|
[38e20a80] | 72 | |
---|
| 73 | % Not only CFA enumerations can be used with CfaEnum trait, other types that satisfy |
---|
| 74 | % CfaEnum assertions are all valid. |
---|
| 75 | Types does not need be defined as \CFA enumerations to work with CfaEnum traits. CfaEnum applies to any type |
---|
| 76 | with @label()@ and @value()@ being properly defined. |
---|
| 77 | Here is an example on how to extend a C enumeration to comply CfaEnum traits: |
---|
[d1276f8] | 78 | \begin{cfa} |
---|
[38e20a80] | 79 | enum Fruit { Apple, Banana, Cherry }; $\C{// C enum}$ |
---|
[d1276f8] | 80 | const char * label( Fruit f ) { |
---|
[38e20a80] | 81 | choose( f ) { |
---|
[d1276f8] | 82 | case Apple: return "Apple"; |
---|
[38e20a80] | 83 | case Banana: return "Banana"; |
---|
[d1276f8] | 84 | case Cherry: return "Cherry"; |
---|
| 85 | } |
---|
| 86 | } |
---|
[38e20a80] | 87 | unsigned posn( Fruit f ) { return f; } |
---|
| 88 | char value( Fruit f ) { |
---|
| 89 | choose(f) { |
---|
| 90 | case Apple: return 'a'; |
---|
| 91 | case Banana: return 'b'; |
---|
| 92 | case Cherry: return 'c'; |
---|
| 93 | } |
---|
| 94 | } |
---|
[d1276f8] | 95 | |
---|
[38e20a80] | 96 | format_enum(Cherry); // "Cherry(c)" |
---|
[d1276f8] | 97 | \end{cfa} |
---|
| 98 | |
---|
[6740533e] | 99 | \section{Discussion: Static Type Information} |
---|
| 100 | @CfaEnum@ and @TypedEnum@ are approximations to \CFA Enumerations and Typed Enumerations: they are not |
---|
| 101 | assertions on a type being an enumerated type, |
---|
| 102 | but rather types being shared an interfaces with \CFA enumerations. |
---|
| 103 | \CC's @type_traits@ is fundamentally different than \CFA's traits: \CC's @type_traits@ are descriptions |
---|
| 104 | of compile time type information |
---|
| 105 | \footnote{Concepts can check if a \CC class implement a certain method, |
---|
| 106 | but it is to probe a static type information of a class having a such member.} |
---|
| 107 | , while \CFA's trait describe how a type can be used, |
---|
| 108 | which is a closer paradigm to a trait system in languages such as Scala and Rust. |
---|
| 109 | However, Scala and Rust's traits are nominative: |
---|
| 110 | a type explicitly declare a named traits to be of its type; while in \CFA, |
---|
| 111 | type implements all functions declares in a trait to implicitly be of the trait type. |
---|
| 112 | |
---|
| 113 | If to support static type information, \CFA needs new piece of syntax to distinguish static type |
---|
| 114 | query from function calls, for example: |
---|
| 115 | \begin{cfa} |
---|
| 116 | forall(T | { T::is_enum; }); |
---|
| 117 | \end{cfa} |
---|
| 118 | When to call a polymorphic function @foo(T)@ with assertions set @S@ and function call argument @a@, \CFA |
---|
| 119 | determines if there is an overloaded name @a@ that has non-zero conversion cost to all assertions in @S@. |
---|
| 120 | As a consequence, @is_enum@ can be a \CFA directive that immediately trim down the search space of @a@ to |
---|
| 121 | be some enumerated types. In fact, because \CFA stores symbols maps to enumeration in a standalone data structure. |
---|
| 122 | Limiting search space to enumeration improve on \CFA resolution speed. |
---|
| 123 | |
---|
| 124 | While assertion on static type information seems improvement on expressivity, it is a challenge to |
---|
| 125 | extend its capability without a fully functional pre-processsor that evaluate constant expression as \CC |
---|
| 126 | compilers does. The described @is_enum@ manipulate compiler behaviour, which cannot be easily extended to |
---|
| 127 | other usage cases. Therefore, \CFA currently does not support @is_enum@ and utalizes traits as a workaround. |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | \section{Bounded and Serial} |
---|
[38e20a80] | 131 | A bounded type defines a lower bound and a upper bound. |
---|
[d1276f8] | 132 | \begin{cfa} |
---|
| 133 | forall( E ) trait Bounded { |
---|
[38e20a80] | 134 | E lowerBound(); |
---|
| 135 | E lowerBound(); |
---|
[d1276f8] | 136 | }; |
---|
[38e20a80] | 137 | |
---|
[d1276f8] | 138 | \end{cfa} |
---|
[38e20a80] | 139 | Both Bounded functions are implement for \CFA enumerations, with @lowerBound()@ returning the first enumerator and @upperBound()@ returning |
---|
| 140 | the last enumerator. |
---|
[d1276f8] | 141 | \begin{cfa} |
---|
[38e20a80] | 142 | Workday day = lowerBound(); $\C{// Mon}$ |
---|
| 143 | Planet outermost = upperBound(); $\C{// NEPTUNE}$ |
---|
[d1276f8] | 144 | \end{cfa} |
---|
[38e20a80] | 145 | |
---|
| 146 | The lowerBound() and upperBound() are functions overloaded on return type only, means their type resolution solely depend on the outer context, |
---|
| 147 | including expected type as a function argument, or the left hand size of an assignment expression. |
---|
| 148 | Calling either function without a context results in a type ambiguity, except in the rare case where the type environment has only one |
---|
| 149 | type overloads the functions, including \CFA enumerations, which has Bounded functions automatic defined. |
---|
[d1276f8] | 150 | \begin{cfa} |
---|
[38e20a80] | 151 | @lowerBound();@ $\C{// ambiguous as both Workday and Planet implement Bounded}$ |
---|
| 152 | sout | @lowerBound()@; |
---|
| 153 | Workday day = first(); $\C{// day provides type Workday}$ |
---|
[d1276f8] | 154 | void foo( Planet p ); |
---|
[38e20a80] | 155 | foo( last() ); $\C{// argument provides type Planet}$ |
---|
[d1276f8] | 156 | \end{cfa} |
---|
| 157 | |
---|
[38e20a80] | 158 | @Serial@ is a subset of @Bounded@, with functions maps elements against integers, as well implements a sequential order between members. |
---|
[d1276f8] | 159 | \begin{cfa} |
---|
| 160 | forall( E | Bounded( E ) ) trait Serial { |
---|
| 161 | unsigned fromInstance( E e ); |
---|
[38e20a80] | 162 | E fromInt( unsigned int i ); |
---|
[d1276f8] | 163 | E succ( E e ); |
---|
| 164 | E pred( E e ); |
---|
[38e20a80] | 165 | unsigned Countof( E e ); |
---|
[d1276f8] | 166 | }; |
---|
| 167 | \end{cfa} |
---|
| 168 | |
---|
[38e20a80] | 169 | % A Serail type can project to an unsigned @int@ type, \ie an instance of type T has a corresponding integer value. |
---|
| 170 | Function @fromInstance()@ projects a @Bounded@ member to a number and @fromInt@ is the inverser. Function @succ()@ take an element, returns the "next" |
---|
| 171 | member in sequential order and @pred()@ returns the "last" member. |
---|
| 172 | |
---|
| 173 | A Serial type E may not be having a one-to-one mapping to integer because of bound. An integer that cannot be mapping to a member of E is called the member \newterm{out of bound}. |
---|
| 174 | Calling @succ()@ on @upperBound@ or @pred()@ on @lowerBound()@ results in out of bound. |
---|
| 175 | |
---|
| 176 | \CFA implements Serial interface for CFA enumerations with \newterm{bound check} on @fromInt()@, @succ()@ and @pred()@, and abort the program if the function call results in out of bound. |
---|
| 177 | Unlike a cast, conversion between \CFA enumeration and integer with @Serial@ interface is type safe. |
---|
| 178 | Specifically for @fromInt@, \CFA abort if input i smaller than @fromInstance(lowerBound())@ or greater than @fromInstance(upperBound())@ |
---|
| 179 | |
---|
| 180 | Function @Countof@ takes an object as a parameter and returns the number of elements in the Serial type, which is @fromInstance( upper ) - fromInstance( lower ) + 1@. |
---|
| 181 | @Countof@ does not use its arugment as procedural input; it needs |
---|
| 182 | an argument to anchor its polymorphic type T. |
---|
| 183 | |
---|
| 184 | \CFA has an expression @countof@ (lower case) that returns the number of enumerators defined for enumerations. |
---|
| 185 | \begin{cfa} |
---|
| 186 | enum RGB {Red, Green, Blue}; |
---|
| 187 | countof( RGB ); // (1) |
---|
| 188 | countof( Red ); // (2) |
---|
| 189 | \end{cfa} |
---|
| 190 | Both expressions from the previous example are converted to constant expression @3@ with no function call at runtime. |
---|
| 191 | @countof@ also works for any type T that defines @Countof@ and @lowerBound@, for which it turns into |
---|
| 192 | a function call @Countof( T )@. The resolution step on expression @countof(e)@ works as the following with priority ordered: |
---|
| 193 | \begin{enumerate} |
---|
| 194 | \item Looks for an enumeration named e, such as @enum e {... }@. |
---|
| 195 | If such an enumeration e exists, \CFA replace @countof(e)@ with constant expression with number of enumerator of e. |
---|
| 196 | \item Looks for a non-enumeration type named e that defines @Countof@ and @lowerBound@, including e being a polymorphic type, such as @forall(e)@. |
---|
| 197 | If type e exists, \CFA replaces it with @Countof(lowerBound())@, where lowerBound() is bounded to type e. |
---|
| 198 | \item Looks for an enumerator e that defined in enumeration E. If such an enumeration e exists, \CFA replace @countof(e)@ with constant expression with number of enumerator of E. |
---|
| 199 | \item Looks for a name e in the context with expression type E. If such name e exists, \CFA replace @countof(e)@ with function call @Countof(e)@. |
---|
| 200 | \item If 1-4 fail, \CFA reports a type error on expression @countof(e)@. |
---|
| 201 | \end{enumerate} |
---|
| 202 | |
---|
| 203 | With the @Bounded@ and @Serial@, a loop over enumeration can be implemented in the following ways: |
---|
| 204 | \begin{cfa} |
---|
| 205 | enum() E { ... } |
---|
| 206 | for( unsigned i = 0; i < countof(E); i++ ) { ... } |
---|
| 207 | for( E e = lowerBound(); ; e = succ(e) ) { ...; if (e == upperBound()) break; } |
---|
| 208 | |
---|
| 209 | forall( T ) { |
---|
| 210 | for( unsigned i = 0; i < countof(T); i++ ) { ... } |
---|
| 211 | for( T e = lowerBound(); ; e = succ(e) ) { ...; if (e == upperBound()) break; } |
---|
| 212 | } |
---|
| 213 | \end{cfa} |
---|
[d1276f8] | 214 | |
---|
| 215 | Finally, there is an associated trait defining comparison operators among enumerators. |
---|
| 216 | \begin{cfa} |
---|
| 217 | forall( E, T | CfaEnum( E, T ) ) { |
---|
| 218 | // comparison |
---|
| 219 | int ?==?( E l, E r ); $\C{// true if l and r are same enumerators}$ |
---|
| 220 | int ?!=?( E l, E r ); $\C{// true if l and r are different enumerators}$ |
---|
| 221 | int ?!=?( E l, zero_t ); $\C{// true if l is not the first enumerator}$ |
---|
| 222 | int ?<?( E l, E r ); $\C{// true if l is an enumerator before r}$ |
---|
| 223 | int ?<=?( E l, E r ); $\C{// true if l before or the same as r}$ |
---|
| 224 | int ?>?( E l, E r ); $\C{// true if l is an enumerator after r}$ |
---|
| 225 | int ?>=?( E l, E r ); $\C{// true if l after or the same as r}$ |
---|
| 226 | } |
---|
| 227 | \end{cfa} |
---|
| 228 | |
---|
| 229 | As an alternative, users can define the boolean conversion for CfaEnum: |
---|
| 230 | |
---|
| 231 | \begin{cfa} |
---|
| 232 | forall(E | CfaEnum(E)) |
---|
| 233 | bool ?!=?(E lhs, zero_t) { |
---|
| 234 | return posn(lhs) != 0; |
---|
| 235 | } |
---|
| 236 | \end{cfa} |
---|
| 237 | which effectively turns the first enumeration as a logical zero and non-zero for others. |
---|
| 238 | |
---|
| 239 | \begin{cfa} |
---|
| 240 | Count variable_a = First, variable_b = Second, variable_c = Third, variable_d = Fourth; |
---|
| 241 | p(variable_a); // 0 |
---|
| 242 | p(variable_b); // 1 |
---|
| 243 | p(variable_c); // "Third" |
---|
| 244 | p(variable_d); // 3 |
---|
| 245 | \end{cfa} |
---|
| 246 | |
---|
| 247 | |
---|
[956299b] | 248 | \section{Iteration and Range} |
---|
| 249 | |
---|
[f9da761] | 250 | It is convenient to iterate over a \CFA enumeration value, \eg: |
---|
[956299b] | 251 | \begin{cfa}[label=lst:range_functions] |
---|
| 252 | for ( Alphabet alph; Alphabet ) { sout | alph; } |
---|
| 253 | >>> A B C ... D |
---|
| 254 | \end{cfa} |
---|
| 255 | The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration. |
---|
| 256 | @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule. |
---|
| 257 | |
---|
| 258 | \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: |
---|
| 259 | \begin{cfa}[label=lst:range_functions] |
---|
| 260 | for ( Alphabet alph ) { sout | alph; } |
---|
| 261 | >>> A B C ... D |
---|
| 262 | \end{cfa} |
---|
| 263 | |
---|
| 264 | The following are examples for constructing for-control using an enumeration. Note that the type declaration of the iterating variable is optional, because \CFA can infer the type as EnumInstType based on the range expression, and possibly convert it to one of its attribute types. |
---|
| 265 | |
---|
| 266 | \textbullet\ H is implicit up-to exclusive range [0, H). |
---|
| 267 | \begin{cfa}[label=lst:range_function_1] |
---|
| 268 | for ( alph; Alphabet.D ) { sout | alph; } |
---|
| 269 | >>> A B C |
---|
| 270 | \end{cfa} |
---|
| 271 | |
---|
| 272 | \textbullet\ ~= H is implicit up-to inclusive range [0,H]. |
---|
| 273 | \begin{cfa}[label=lst:range_function_2] |
---|
| 274 | for ( alph; ~= Alphabet.D ) { sout | alph; } |
---|
| 275 | >>> A B C D |
---|
| 276 | \end{cfa} |
---|
| 277 | |
---|
| 278 | \textbullet\ L ~ H is explicit up-to exclusive range [L,H). |
---|
| 279 | \begin{cfa}[label=lst:range_function_3] |
---|
| 280 | for ( alph; Alphabet.B ~ Alphabet.D ) { sout | alph; } |
---|
| 281 | // for ( Alphabet alph = Alphabet.B; alph < Alphabet.D; alph += 1 ); 1 is one_t |
---|
| 282 | >>> B C |
---|
| 283 | \end{cfa} |
---|
| 284 | |
---|
| 285 | \textbullet\ L ~= H is explicit up-to inclusive range [L,H]. |
---|
| 286 | \begin{cfa}[label=lst:range_function_4] |
---|
| 287 | for ( alph; Alphabet.B ~= Alphabet.D ) { sout | alph; } |
---|
| 288 | >>> B C D |
---|
| 289 | \end{cfa} |
---|
| 290 | |
---|
| 291 | \textbullet\ L -~ H is explicit down-to exclusive range [H,L), where L and H are implicitly interchanged to make the range down-to. |
---|
| 292 | \begin{cfa}[label=lst:range_function_5] |
---|
| 293 | for ( alph; Alphabet.D -~ Alphabet.B ) { sout | alph; } |
---|
| 294 | >>> D C |
---|
| 295 | \end{cfa} |
---|
| 296 | |
---|
| 297 | \textbullet\ L -~= H is explicit down-to exclusive range [H,L], where L and H are implicitly interchanged to make the range down-to. |
---|
| 298 | \begin{cfa}[label=lst:range_function_6] |
---|
| 299 | for ( alph; Alphabet.D -~= Alphabet.B ) { sout | alph; } |
---|
| 300 | >>> D C B |
---|
| 301 | \end{cfa} |
---|
| 302 | |
---|
| 303 | A user can specify the ``step size'' of an iteration. There are two different stepping schemes of enumeration for-loop. |
---|
| 304 | \begin{cfa}[label=lst:range_function_stepping] |
---|
| 305 | enum(int) Sequence { A = 10, B = 12, C = 14, D = 16, D = 18 }; |
---|
| 306 | for ( s; Sequence.A ~= Sequence.D ~ 1 ) { sout | alph; } |
---|
| 307 | >>> 10 12 14 16 18 |
---|
| 308 | for ( s; Sequence.A ~= Sequence.D; s+=1 ) { sout | alph; } |
---|
| 309 | >>> 10 11 12 13 14 15 16 17 18 |
---|
| 310 | \end{cfa} |
---|
| 311 | The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to |
---|
| 312 | \begin{cfa}[label=lst:range_function_stepping_converted] |
---|
| 313 | for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1 ) { sout | alph; } |
---|
| 314 | >>> 10 11 12 13 14 15 16 17 18 |
---|
| 315 | \end{cfa} |
---|
| 316 | |
---|
| 317 | % \PAB{Explain what each loop does.} |
---|
| 318 | |
---|
| 319 | It is also possible to iterate over an enumeration's labels, implicitly or explicitly: |
---|
| 320 | \begin{cfa}[label=lst:range_functions_label_implicit] |
---|
| 321 | for ( char * alph; Alphabet ) |
---|
| 322 | \end{cfa} |
---|
| 323 | This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to @ch@ with type @char *@ in this case. |
---|
| 324 | If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration. |
---|
| 325 | \begin{cfa}[label=lst:range_functions_label_implicit] |
---|
| 326 | for ( char * ch; labels( Alphabet ) ) |
---|
| 327 | \end{cfa} |
---|