Changeset 5a02308 for doc/theses/fangren_yu_MMath/intro.tex
- Timestamp:
- Dec 22, 2024, 4:48:21 PM (4 weeks ago)
- Branches:
- master
- Children:
- a5e2786f
- Parents:
- 50cad32
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/intro.tex
r50cad32 r5a02308 1 1 \chapter{Introduction} 2 2 3 This thesis is exploratory work I did to understand, fix, and extend the \CFA type-system, specifically, the type resolver used to select polymorphic types among overloaded names. 3 This thesis is exploratory work I did to understand, fix, and extend the \CFA type-system, specifically, the \newterm{type-resolver} used to satisfy call-site assertions among overloaded variable and function names to allow polymorphic routine calls. 4 Assertions are the operations a function uses within its body to perform its computation. 5 For example, a polymorphic function summing an array needs a size, zero, assignment, and plus for the array element-type, and a subscript operation for the array type. 6 \begin{cfa} 7 T sum( T a[$\,$], size_t size ) { 8 @T@ total = { @0@ }; // size, 0 for type T 9 for ( size_t i = 0; i < size; i += 1 ) 10 total @+=@ a@[@i@]@; // + and subscript for T 11 return total; 12 } 13 \end{cfa} 14 In certain cases, if the resolver fails to find an exact assertion match, it attempts to find a \emph{best} match using reasonable type conversions. 15 Hence, \CFA follows the current trend of replacing nominal inheritance with traits composed of assertions for type matching. 16 The over-arching goal is to push the boundary on localized assertion matching, with advanced overloading resolution and type conversions that match programmer expectations in the C programming language. 17 Together, the resulting \CFA type-system has a number of unique features making it different from other programming languages with expressive, static, type-systems. 18 19 20 \section{Types} 21 22 All computers have multiple types because computer architects optimize the hardware around a few basic types with well defined (mathematical) operations: boolean, integral, floating-point, and occasionally strings. 23 A programming language and its compiler present ways to declare types that ultimately map into the ones provided by the underlying hardware. 24 These language types are thrust upon programmers, with their syntactic and semantic rules and restrictions. 25 These rules are used to transform a language expression to a hardware expression. 26 Modern programming-languages allow user-defined types and generalize across multiple types using polymorphism. 27 Type systems can be static, where each variable has a fixed type during execution and an expression's type is determined once at compile time, or dynamic, where each variable can change type during execution and so an expression's type is reconstructed on each evaluation. 28 Expressibility, generalization, and safety are all bound up in a language's type system, and hence, directly affect the capability, build time, and correctness of program development. 29 30 31 \section{Overloading} 32 4 33 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. 5 34 \begin{quote} … … 10 39 Depending on the language, any ambiguous cases are resolved using some form of qualification and/or casting. 11 40 12 Therefore, one of the key goals in \CFA is to push the boundary on overloading, and hence, overload resolution. 13 As well, \CFA follows the current trend of replacing nominal inheritance with traits. 14 Together, the resulting \CFA type-system has a number of unique features making it different from other programming languages with expressive, static type-systems. 15 16 17 \section{Types} 18 19 \begin{quote} 20 Some are born great, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II Scene 5, William Shakespeare 21 \end{quote} 22 23 All computers have multiple types because computer architects optimize the hardware around a few basic types with well defined (mathematical) operations: boolean, integral, floating-point, and occasionally strings. 24 A programming language and its compiler present ways to declare types that ultimately map into the ones provided by the underlying hardware. 25 These language types are \emph{thrust} upon programmers, with their syntactic and semantic rules and restrictions. 26 These rules are used to transform a language expression to a hardware expression. 27 Modern programming-languages allow user-defined types and generalize across multiple types using polymorphism. 28 Type systems can be static, where each variable has a fixed type during execution and an expression's type is determined once at compile time, or dynamic, where each variable can change type during execution and so an expression's type is reconstructed on each evaluation. 29 Expressibility, generalization, and safety are all bound up in a language's type system, and hence, directly affect the capability, build time, and correctness of program development. 30 31 32 \section{Operator Overloading} 41 42 \subsection{Operator Overloading} 33 43 34 44 Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns. 35 Like \CC, \CFA alsoallows these operators to be overloaded with user-defined types.45 Like \CC, \CFA maps operators to named functions and allows these operators to be overloaded with user-defined types. 36 46 The syntax for operator names uses the @'?'@ character to denote a parameter, \eg left and right unary operators: @?++@ and @++?@, and binary operators @?+?@ and @?<=?@. 37 47 Here, a user-defined type is extended with an addition operation with the same syntax as builtin types. … … 50 60 51 61 52 \s ection{Function Overloading}62 \subsection{Function Overloading} 53 63 54 64 Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns. … … 83 93 84 94 85 \s ection{Variable Overloading}95 \subsection{Variable Overloading} 86 96 87 97 Unlike most programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes. … … 125 135 126 136 127 \section{Constant Overloading} 128 129 \CFA is unique in providing restricted constant overloading for the values @0@ and @1@, which have special status in C, \eg the value @0@ is both an integer and a pointer literal, so its meaning depends on context. 137 \subsection{Constant Overloading} 138 139 \CFA is unique in providing restricted constant overloading for the values @0@ and @1@, which have special status in C. 140 For example, the value @0@ is both an integer and a pointer literal, so its meaning depends on context. 130 141 In addition, several operations are defined in terms of values @0@ and @1@. 131 For example, every @if@ and iteration statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result.142 For example, @if@ and iteration statements in C compare the condition with @0@, and the increment and decrement operators are semantically equivalent to adding or subtracting the value @1@. 132 143 \begin{cfa} 133 144 if ( x ) ++x; => if ( x @!= 0@ ) x @+= 1@; … … 138 149 \begin{cfa} 139 150 struct S { int i, j; }; 140 void ?{}( S & s, zero_t ) { s.[i,j] = 0; } $\C{// const ructors}$151 void ?{}( S & s, zero_t ) { s.[i,j] = 0; } $\C{// constant constructors}$ 141 152 void ?{}( S & s, one_t ) { s.[i,j] = 1; } 142 S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// assignment}$153 S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// constant assignments}$ 143 154 S ?=?( S & dst, one_t ) { dst.[i,j] = 1; return dst; } 144 155 S ?+=?( S & s, one_t ) { s.[i,j] += 1; return s; } $\C{// increment/decrement each field}$ 145 156 S ?-=?( S & s, one_t ) { s.[i,j] -= 1; return s; } 146 int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// co mparison}$157 int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// constant comparison}$ 147 158 S s = @0@; $\C{// initialization}$ 148 159 s = @0@; $\C{// assignments}$ 149 160 s = @1@; 150 if ( @s@ ) @++s@; $\C{// special,unary ++/-\,- come implicitly from +=/-=}$161 if ( @s@ ) @++s@; $\C{// unary ++/-\,- come implicitly from +=/-=}$ 151 162 \end{cfa} 152 163 Here, type @S@ is first-class with respect to the basic types, working with all existing implicit C mechanisms. … … 169 180 Note, type inferencing is different from type conversion: type inferencing \emph{discovers} a variable's type before setting its value, whereas conversion has two typed values and performs a (possibly lossy) action to convert one value to the type of the other variable. 170 181 Finally, for assignment, the current variable and expression types may not agree. 182 Discovering a variable or function type is complex and has limitations. 183 The following covers these issues, and why some schemes are not amenable with the \CFA type system. 171 184 172 185 One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}. … … 248 261 249 262 250 \s ection{Type-Inferencing Issues}263 \subsection{Type-Inferencing Issues} 251 264 252 265 Each kind of type-inferencing system has its own set of issues that flow onto the programmer in the form of convenience, restrictions, or confusions. … … 311 324 \end{cfa} 312 325 This @identity@ function can be applied to an \newterm{object type}, \ie a type with a known size and alignment, which is sufficient to stack allocate, default or copy initialize, assign, and delete. 313 The \CFA implementation passes the size and alignment for each type parameter, as well as a ny implicit/explicit constructor, copy constructor, assignment operator, and destructor.326 The \CFA implementation passes the size and alignment for each type parameter, as well as auto-generated default and copy constructors, assignment operator, and destructor. 314 327 For an incomplete \newterm{data type}, \eg pointer/reference types, this information is not needed. 315 328 \begin{cfa} … … 420 433 \end{cfa} 421 434 The implicit routines are used by the @sumable@ operator @?+=?@ for the right side of @?+=?@ and return. 435 436 If the array type is not a builtin type, an extra type parameter and assertions are required, like subscripting. 437 This case is generalized in polymorphic container-types, such as a list with @insert@ and @remove@ operations, and an element type with copy and assignment. 422 438 423 439
Note: See TracChangeset
for help on using the changeset viewer.