Ignore:
Timestamp:
Sep 10, 2024, 2:31:22 AM (5 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
08e0d65
Parents:
3d618a0
Message:

Update thesis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/trait.tex

    r3d618a0 raa14aafe  
    33
    44\CC introduced the @std::is_enum@ trait in \CC{11} and concept feature in \CC{20}.
    5 With this combination, it is possible to write a polymorphic function over an enumerated type.
     5This combination makes it possible to write a polymorphic function over an enumerated type.
    66\begin{c++}
    77#include <type_traits>
    88template<typename T>  @concept Enumerable@  =  std::is_enum<T>::value;
    9 template<@Enumerable@ E>  E  f( E e ) { $\C{// constrainted type}$
    10         E w = e;                                                        $\C{// alloction and copy}$
     9template<@Enumerable@ E>  E  f( E e ) { $\C{// constrained type}$
     10        E w = e;                                                        $\C{// allocation and copy}$
    1111        cout << e << ' ' << w << endl;          $\C{// value}$
    1212        return w;                                                       $\C{// copy}$
     
    2424
    2525
    26 \section{Traits \texorpdfstring{\lstinline{CfaEnum}{CfaEnum}} and \texorpdfstring{\lstinline{TypedEnum}}{TypedEnum}}
     26\section{Traits \texorpdfstring{\lstinline{CfaEnum}}{CfaEnum} and \texorpdfstring{\lstinline{TypedEnum}}{TypedEnum}}
    2727
    2828Traits @CfaEnum@ and @TypedEnum@ define the enumeration attributes: @label@, @posn@, @value@, and @Countof@.
     
    3434}
    3535\end{cfa}
    36 
     36\newpage
    3737Trait @CfaEnum@ defines attribute functions @label@ and @posn@ for all \CFA enumerations, and internally \CFA enumerations fulfills this assertion.
    3838\begin{cfa}
     
    107107\section{Discussion: Genericity}
    108108
    109 At the start of this chapter, the \CC concept is introduced to constraint template types, \eg:
     109At the start of this chapter, the \CC concept is introduced to constrained template types, \eg:
    110110\begin{c++}
    111111concept Enumerable = std::is_enum<T>::value;
     
    119119Alternatively, 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.
    120120Hence, 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 (duck typing).
     121Instead, anything that can look like the @enum@ kind is considered an enumeration (static structural typing).
    122122However, 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.
    123123
     
    125125For example, \VRef[Figure]{f:GeneralizedEnumerationFormatter} shows that pre-existing C enumerations can be upgraded to work and play with new \CFA enumeration facilities.
    126126Another 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.
    128128
    129129
     
    134134forall( E ) trait Bounded {
    135135        E lowerBound();
    136         E lowerBound();
     136        E upperBound();
    137137};
    138138\end{cfa}
     
    187187        E upper = upperBound();
    188188        E lower = lowerBound();
    189         return fromInstance( upper ) + fromInstance( lower ) + 1;
     189        return fromInstance( upper ) - fromInstance( lower ) + 1;
    190190}
    191191\end{cfa}
     
    215215
    216216A 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 updated the variables in advancement.
     217The loop control is often a 3-tuple: initializers, looping condition, and advancement.
     218It 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.
    219219Such a variable is called an \newterm{index} and is available for reading and writing within the loop body.
    220220(Some languages make the index read-only in the loop body.)
     
    240240\end{cfa}
    241241Both 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.
     242These 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.
    243243
    244244
     
    288288
    289289\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.
     291Bound checks are added to these operations to ensure the outputs fulfills the @Bounded@ invariant.
    291292\begin{cfa}
    292293forall( E | CfaEnum( E ) | Serial( E ) ) { $\C{// distribution block}$
Note: See TracChangeset for help on using the changeset viewer.