Changeset aa14aafe for doc/theses/jiada_liang_MMath
- Timestamp:
- Sep 10, 2024, 2:31:22 AM (2 months ago)
- Branches:
- master
- Children:
- 08e0d65
- Parents:
- 3d618a0
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/conclusion.tex
r3d618a0 raa14aafe 106 106 CFA_Codec cfa_code = CFA_Codec.VORBIS; 107 107 \end{cfa} 108 In the preceding example, the memory location of @c_code@ stores a value 9, the integral value of VORBIS.108 The memory location of @c_code@ stores a value 9, the integral value of VORBIS. 109 109 The memory of @cfa_code@ stores 5, which is the position of @CFA_Codec.VORBIS@ and can be mapped to value 9. 110 110 … … 112 112 before the relative position of VORBIS, without a re-compilation of @main.c@, variable @cfa_code@ would be mapped 113 113 to an unintented value. 114 \begin{cfa} 114 115 // libvacodec.h: v2 115 116 enum(int) C_Codec ! { -
doc/theses/jiada_liang_MMath/trait.tex
r3d618a0 raa14aafe 3 3 4 4 \CC introduced the @std::is_enum@ trait in \CC{11} and concept feature in \CC{20}. 5 With this combination, it ispossible to write a polymorphic function over an enumerated type.5 This combination makes it possible to write a polymorphic function over an enumerated type. 6 6 \begin{c++} 7 7 #include <type_traits> 8 8 template<typename T> @concept Enumerable@ = std::is_enum<T>::value; 9 template<@Enumerable@ E> E f( E e ) { $\C{// constrain ted type}$10 E w = e; $\C{// alloc tion and copy}$9 template<@Enumerable@ E> E f( E e ) { $\C{// constrained type}$ 10 E w = e; $\C{// allocation and copy}$ 11 11 cout << e << ' ' << w << endl; $\C{// value}$ 12 12 return w; $\C{// copy}$ … … 24 24 25 25 26 \section{Traits \texorpdfstring{\lstinline{CfaEnum} {CfaEnum}} and \texorpdfstring{\lstinline{TypedEnum}}{TypedEnum}}26 \section{Traits \texorpdfstring{\lstinline{CfaEnum}}{CfaEnum} and \texorpdfstring{\lstinline{TypedEnum}}{TypedEnum}} 27 27 28 28 Traits @CfaEnum@ and @TypedEnum@ define the enumeration attributes: @label@, @posn@, @value@, and @Countof@. … … 34 34 } 35 35 \end{cfa} 36 36 \newpage 37 37 Trait @CfaEnum@ defines attribute functions @label@ and @posn@ for all \CFA enumerations, and internally \CFA enumerations fulfills this assertion. 38 38 \begin{cfa} … … 107 107 \section{Discussion: Genericity} 108 108 109 At the start of this chapter, the \CC concept is introduced to constrain ttemplate types, \eg:109 At the start of this chapter, the \CC concept is introduced to constrained template types, \eg: 110 110 \begin{c++} 111 111 concept Enumerable = std::is_enum<T>::value; … … 119 119 Alternatively, languages using traits, like \CFA, Scala, Go, and Rust, are defining a restriction based on a set of operations, variables, or structure fields that must exist to match with usages in a function or aggregate type. 120 120 Hence, the \CFA enumeration traits never connected with the specific @enum@ kind. 121 Instead, anything that can look like the @enum@ kind is considered an enumeration ( ducktyping).121 Instead, anything that can look like the @enum@ kind is considered an enumeration (static structural typing). 122 122 However, Scala, Go, and Rust traits are nominative: a type explicitly declares a named traits to be of its type, while in \CFA, any type implementing all requirements declared in a trait implicitly satisfy its restrictions. 123 123 … … 125 125 For example, \VRef[Figure]{f:GeneralizedEnumerationFormatter} shows that pre-existing C enumerations can be upgraded to work and play with new \CFA enumeration facilities. 126 126 Another example is adding constructors and destructors to pre-existing C types by simply declaring them for the old C type. 127 \CC fails at certain levels of legacy extension because many of the new \CC features must appear \emph{within} an aggregate definition due to the object-oriented nature of he type system, where it is impossible to change legacy library types.127 \CC fails at certain levels of legacy extension because many of the new \CC features must appear \emph{within} an aggregate definition due to the object-oriented nature of the type system, where it is impossible to change legacy library types. 128 128 129 129 … … 134 134 forall( E ) trait Bounded { 135 135 E lowerBound(); 136 E lowerBound();136 E upperBound(); 137 137 }; 138 138 \end{cfa} … … 187 187 E upper = upperBound(); 188 188 E lower = lowerBound(); 189 return fromInstance( upper ) +fromInstance( lower ) + 1;189 return fromInstance( upper ) - fromInstance( lower ) + 1; 190 190 } 191 191 \end{cfa} … … 215 215 216 216 A for-loop consists of loop control and body. 217 The loop control is often a 3-tuple: initializers, stopping condition, and advancement.218 It is a common practice to declare one or more loop-index variables in initializers, checked these variables for stopping iteration, and updatedthe variables in advancement.217 The loop control is often a 3-tuple: initializers, looping condition, and advancement. 218 It is a common practice to declare one or more loop-index variables in initializers, determines whether the variables satisfy the loop condition, and update the variables in advancement. 219 219 Such a variable is called an \newterm{index} and is available for reading and writing within the loop body. 220 220 (Some languages make the index read-only in the loop body.) … … 240 240 \end{cfa} 241 241 Both of these loops look correct but fail because these is an additional bound check within the advancement \emph{before} the conditional test to stop the loop, resulting in a failure at the endpoints of the iteration. 242 These loops must be restructured by moving the loop test to the end of the loop (@do-while@), as in loop (2) above, which is safe because an enumeration always at least one enumerator.242 These loops must be restructured by moving the loop test to the end of the loop (@do-while@), as in loop (2) above, which is safe because an enumeration always has at least one enumerator. 243 243 244 244 … … 288 288 289 289 \CFA implements a few arithmetic operators for @CfaEnum@. 290 Unlike advancement functions in @Serial@, these operators perform direct arithmetic, so there is no implicit bound checks. 290 % Unlike advancement functions in @Serial@, these operators perform direct arithmetic, so there is no implicit bound checks. 291 Bound checks are added to these operations to ensure the outputs fulfills the @Bounded@ invariant. 291 292 \begin{cfa} 292 293 forall( E | CfaEnum( E ) | Serial( E ) ) { $\C{// distribution block}$
Note: See TracChangeset
for help on using the changeset viewer.