Changeset 09dd830 for doc/theses/jiada_liang_MMath
- Timestamp:
- Jul 15, 2024, 1:57:15 PM (4 months ago)
- Branches:
- master
- Children:
- 1dd5fd1, 68ea8d2
- Parents:
- dc1c430
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rdc1c430 r09dd830 130 130 \end{cfa} 131 131 132 \CFA does not define attribute sfunctions for C style enumeration. But it is possilbe for users to explicitly implement132 \CFA does not define attribute functions for C style enumeration. But it is possilbe for users to explicitly implement 133 133 enumeration traits for C enum and any other types. 134 134 … … 147 147 \end{cfa} 148 148 149 A type that implement trait @CfaEnum@, \ie, a type has no @value@, is called an opaque enum.149 A type that implements trait @CfaEnum@, \ie, a type has no @value@, is called an opaque enum. 150 150 151 151 % \section{Enumerator Opaque Type} … … 164 164 }; 165 165 \end{cfa} 166 The function @first()@ and @last()@ of enumer tated type E return the first and the last enumerator declared in E, respectively. \eg:166 The function @first()@ and @last()@ of enumerated type E return the first and the last enumerator declared in E, respectively. \eg: 167 167 \begin{cfa} 168 168 Workday day = first(); $\C{// Mon}$ … … 292 292 293 293 Enumeration @Name2@ inherits all the enumerators and their values from enumeration @Names@ by containment, and a @Names@ enumeration is a subtype of enumeration @Name2@. 294 Note, enumerators must be unique in inheritance but enumerator values may be repeated.294 Note, that enumerators must be unique in inheritance but enumerator values may be repeated. 295 295 296 296 % The enumeration type for the inheriting type must be the same as the inherited type; … … 301 301 Names $\(\subset\)$ Names2 $\(\subset\)$ Names3 $\C{// enum type of Names}$ 302 302 \end{cfa} 303 A subtype can be cast ed to its super type, assigned to a super type variable, or be used as a function argument that expects the supertype.303 A subtype can be cast to its supertype, assigned to a supertype variable, or be used as a function argument that expects the supertype. 304 304 \begin{cfa} 305 305 Names fred = Name.Fred; … … 332 332 In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution). 333 333 However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts. 334 In these contexts, a programmer's initition assumes an implicit conversion to pos tion.335 336 For example, an intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implic t @break@ rather than a fall-through at the end of a @case@ clause.334 In these contexts, a programmer's initition assumes an implicit conversion to position. 335 336 For example, an intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implicit @break@ rather than a fall-through at the end of a @case@ clause. 337 337 \begin{cquote} 338 338 \begin{cfa} … … 367 367 enum Count { First, Second, Third @= First@, Fourth }; 368 368 \end{cfa} 369 which make @Third == First@ and @Fourth == Second@, causing a compilation error because of duplica se @case@ clauses.369 which make @Third == First@ and @Fourth == Second@, causing a compilation error because of duplicate @case@ clauses. 370 370 To better match with programmer intuition, \CFA toggles between value and position semantics depending on the language context. 371 For conditional clauses and switch stat ments, \CFA uses the robust position implementation.371 For conditional clauses and switch statements, \CFA uses the robust position implementation. 372 372 \begin{cfa} 373 373 choose( @position@( e ) ) { … … 392 392 for (c: -~=Alphabet ) { sout | c; } 393 393 \end{cfa} 394 The @range loop@ for enumeration is a syntax sugar that loop ing over all enumeerators and assigneach enumeration to a variable in every iteration.395 The loop control of range loop consists of two parts: a variable declaration and a @range expression@, withtype of the variable394 The @range loop@ for enumeration is a syntax sugar that loops over all enumerators and assigns each enumeration to a variable in every iteration. 395 The loop control of the range loop consists of two parts: a variable declaration and a @range expression@, with the type of the variable 396 396 can be inferred from the range expression. 397 397 398 398 The range expression is an enumeration type, optionally prefixed by @+~=@ or @-~=@. Without a prefix, or prefixed with @+~=@, the control 399 loop over all enumerator from the first to the last. With a @-~=@ prefix, the control loops backwards.399 loop over all enumerators from the first to the last. With a @-~=@ prefix, the control loops backward. 400 400 401 401 On a side note, the loop syntax … … 405 405 does not work. When d == last(), the loop control will still attempt to assign succ(d) to d, which causes an @enumBound@ exception. 406 406 407 \CFA reduces conditionals to its "if case" if the predicate not equal to ( @!=@ ) zero, and the "else case" otherwises.407 \CFA reduces conditionals to its "if case" if the predicate is not equal to ( @!=@ ) zero, and the "else case" otherwise. 408 408 Overloading the @!=@ operator with an enumeration type against the zero defines a conceptual conversion from 409 409 enum to boolean, which can be used as predicates. … … 416 416 \end{cfa} 417 417 418 In dicentally, \CFA does not define boolean conversion for enumeration. If no418 Incidentally, \CFA does not define boolean conversion for enumeration. If no 419 419 @?!=?(ErrorCode, zero_t)@ 420 420 overloading defined, 421 \CFA looks for the boolean conversion in terms of its value , and gives ancompiler error if no such conversion is available.421 \CFA looks for the boolean conversion in terms of its value and gives a compiler error if no such conversion is available. 422 422 423 423 \begin{cfa} … … 431 431 \end{cfa} 432 432 433 As an alternative ly, users can define the boolean conversion for CfaEnum:433 As an alternative, users can define the boolean conversion for CfaEnum: 434 434 435 435 \begin{cfa} … … 442 442 443 443 \section{Enumerated Arrays} 444 Enumerated array use an \CFA array as their index.444 Enumerated arrays use an \CFA array as their index. 445 445 \begin{cfa} 446 446 enum() Colour { … … 457 457 \VRef[Figure]{f:PlanetExample} shows an archetypal enumeration example illustrating most of the \CFA enumeration features. 458 458 @Planet@ is an enumeration of type @MR@. 459 Each of the planet enumeratorsis initialized to a specific mass/radius, @MR@, value.459 Each planet enumerator is initialized to a specific mass/radius, @MR@, value. 460 460 The unnamed enumeration provides the gravitational-constant enumerator @G@. 461 461 Function @surfaceGravity@ uses the @with@ clause to remove @p@ qualification from fields @mass@ and @radius@. 462 462 The program main uses the pseudo function @countof@ to obtain the number of enumerators in @Planet@, and safely converts the random value into a @Planet@ enumerator using @fromInt@. 463 463 The resulting random orbital-body is used in a @choose@ statement. 464 The enumerators in the @case@ clause use enumerator position for testing.464 The enumerators in the @case@ clause use the enumerator position for testing. 465 465 The prints use @label@ to print an enumerator's name. 466 466 Finally, a loop enumerates through the planets computing the weight on each planet for a given earth mass.
Note: See TracChangeset
for help on using the changeset viewer.