Changes in / [a5e2786f:28c2c9d5]
- File:
-
- 1 edited
-
doc/theses/fangren_yu_MMath/intro.tex (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/intro.tex
ra5e2786f r28c2c9d5 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 \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 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. 33 4 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. 34 5 \begin{quote} … … 39 10 Depending on the language, any ambiguous cases are resolved using some form of qualification and/or casting. 40 11 41 42 \subsection{Operator Overloading} 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} 43 33 44 34 Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns. 45 Like \CC, \CFA maps operators to named functions andallows these operators to be overloaded with user-defined types.35 Like \CC, \CFA also allows these operators to be overloaded with user-defined types. 46 36 The syntax for operator names uses the @'?'@ character to denote a parameter, \eg left and right unary operators: @?++@ and @++?@, and binary operators @?+?@ and @?<=?@. 47 37 Here, a user-defined type is extended with an addition operation with the same syntax as builtin types. … … 60 50 61 51 62 \s ubsection{Function Overloading}52 \section{Function Overloading} 63 53 64 54 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. … … 93 83 94 84 95 \s ubsection{Variable Overloading}85 \section{Variable Overloading} 96 86 97 87 Unlike most programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes. … … 135 125 136 126 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. 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. 141 130 In addition, several operations are defined in terms of values @0@ and @1@. 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@.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. 143 132 \begin{cfa} 144 133 if ( x ) ++x; => if ( x @!= 0@ ) x @+= 1@; … … 149 138 \begin{cfa} 150 139 struct S { int i, j; }; 151 void ?{}( S & s, zero_t ) { s.[i,j] = 0; } $\C{// const ant constructors}$140 void ?{}( S & s, zero_t ) { s.[i,j] = 0; } $\C{// constructors}$ 152 141 void ?{}( S & s, one_t ) { s.[i,j] = 1; } 153 S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// constant assignments}$142 S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// assignment}$ 154 143 S ?=?( S & dst, one_t ) { dst.[i,j] = 1; return dst; } 155 144 S ?+=?( S & s, one_t ) { s.[i,j] += 1; return s; } $\C{// increment/decrement each field}$ 156 145 S ?-=?( S & s, one_t ) { s.[i,j] -= 1; return s; } 157 int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// co nstant comparison}$146 int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// comparison}$ 158 147 S s = @0@; $\C{// initialization}$ 159 148 s = @0@; $\C{// assignments}$ 160 149 s = @1@; 161 if ( @s@ ) @++s@; $\C{// unary ++/-\,- come implicitly from +=/-=}$150 if ( @s@ ) @++s@; $\C{// special, unary ++/-\,- come implicitly from +=/-=}$ 162 151 \end{cfa} 163 152 Here, type @S@ is first-class with respect to the basic types, working with all existing implicit C mechanisms. … … 180 169 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. 181 170 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.184 171 185 172 One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}. … … 261 248 262 249 263 \s ubsection{Type-Inferencing Issues}250 \section{Type-Inferencing Issues} 264 251 265 252 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. … … 324 311 \end{cfa} 325 312 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. 326 The \CFA implementation passes the size and alignment for each type parameter, as well as a uto-generated default and copy constructors, assignment operator, and destructor.313 The \CFA implementation passes the size and alignment for each type parameter, as well as any implicit/explicit constructor, copy constructor, assignment operator, and destructor. 327 314 For an incomplete \newterm{data type}, \eg pointer/reference types, this information is not needed. 328 315 \begin{cfa} … … 433 420 \end{cfa} 434 421 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.438 422 439 423
Note:
See TracChangeset
for help on using the changeset viewer.