Changes in / [86841ee:2d6add4]
- Location:
- doc/theses/fangren_yu_MMath
- Files:
-
- 2 edited
-
content1.tex (modified) (18 diffs)
-
intro.tex (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/content1.tex
r86841ee r2d6add4 15 15 Alternatively, use a reference when its primary purpose is to alias a value, \eg a function parameter that does not copy the argument (performance reason). 16 16 Here, manipulating the value is the primary operation, while changing the pointer address is the secondary operation. 17 Succinctly, if the address changes often, use a pointer; 18 if the value changes often, use a reference. 19 Note, \CC made its reference address immutable starting a \emph{belief} that immutability is a fundamental aspect of a reference's pointer. 20 The results is asymmetry semantics between the pointer and reference. 21 \CFA adopts a uniform policy between pointers and references where mutability is a separate property made at the declaration. 17 Succinctly, if the address often changes, use a pointer; 18 if the value often changes, use a reference. 19 Note, \CC made the reference address immutable starting a \emph{belief} that immutability is a fundamental aspect of a reference's pointer, resulting in a semantic asymmetry between the pointer and reference. 20 \CFA adopts a uniform policy between pointers and references where mutability is a settable property at the point of declaration. 22 21 23 22 The following examples shows how pointers and references are treated uniformly in \CFA. 24 23 \begin{cfa}[numbers=left,numberblanklines=false] 25 int x = 1, y = 2, z = 3; $\label{p:refexamples}$24 int x = 1, y = 2, z = 3; 26 25 int * p1 = &x, ** p2 = &p1, *** p3 = &p2, $\C{// pointers to x}$ 27 26 @&@ r1 = x, @&&@ r2 = r1, @&&&@ r3 = r2; $\C{// references to x}$ … … 35 34 \end{cfa} 36 35 Like pointers, reference can be cascaded, \ie a reference to a reference, \eg @&& r2@.\footnote{ 37 \CC uses \lstinline{&&} for rvalue reference ,a feature for move semantics and handling the \lstinline{const} Hell problem.}36 \CC uses \lstinline{&&} for rvalue reference a feature for move semantics and handling the \lstinline{const} Hell problem.} 38 37 Usage of a reference variable automatically performs the same number of dereferences as the number of references in its declaration, \eg @r3@ becomes @***r3@. 39 38 Finally, to reassign a reference's address needs a mechanism to stop the auto-referencing, which is accomplished by using a single reference to cancel all the auto-dereferencing, \eg @&r3 = &y@ resets @r3@'s address to point to @y@. … … 47 46 \item For an expression $e$ with type $T\ \&_1...\&_n$, \ie $T$ followed by $n$ references, where $T$ is not a reference type, the expression $\&T$ (address of $T$) has type $T *$ followed by $n - 1$ references. 48 47 \item For an expression $e$ with type $T *\&_1...\&_n$, \ie $T *$ followed by $n$ references, the expression $* T$ (dereference $T$) has type $T$ followed by $n + 1$ references. 49 This rule is the reverse of the previous rule, such that address-of and dereference operators are perfect inverses. 50 \item When matching argument and parameter types at a function call, the number of references on the argument type is stripped off to match the number of references on the parameter type.\footnote{ 51 \CFA handles the \lstinline{const} Hell problem by allowing rvalue expressions to be converted to reference values by implicitly creating a temporary variable, with some restrictions. 52 As well, there is a warning that the output nature of the reference is lost. 53 Hence, a single function handles \lstinline{const} and non-\lstinline{const} as constness is handled at the call site.} 48 This is the reverse of the previous rule, such that address-of and dereference operators are perfect inverses. 49 \item When matching parameter and argument types in a function call context, the number of references on the argument type is stripped off to match the number of references on the parameter type.\footnote{ 50 \CFA handles the \lstinline{const} Hell problem by allowing rvalue expressions to be converted to reference values by implicitly creating a temporary variable, with some restrictions.} 54 51 In an assignment context, the left-hand-side operand-type is always reduced to a single reference. 55 52 \end{enumerate} … … 93 90 \end{cfa} 94 91 95 In the initial \CFA reference design, the goal was to make the reference type a \emph{real} data type \vs a restricted \CC reference, which is mostly used for choosing the argument-passing method, \ieby-value or by-reference.92 In the initial \CFA reference design, the goal was to make the reference type a \emph{real} data type \vs a restricted \CC reference, which is mostly used for choosing the argument-passing method, by-value or by-reference. 96 93 However, there is an inherent ambiguity for auto-dereferencing: every argument expression involving a reference variable can potentially mean passing the reference's value or address. 97 94 Without any restrictions, this ambiguity limits the behaviour of reference types in \CFA polymorphic functions, where a type @T@ can bind to a reference or non-reference type. 98 95 This ambiguity prevents the type system treating reference types the same way as other types in many cases even if type variables could be bound to reference types. 99 The reason is that \CFA uses a common \emph{object trait}\label{p:objecttrait}(constructor, destructor and assignment operators) to handle passing dynamic concrete type arguments into polymorphic functions, and the reference types are handled differently in these contexts so they do not satisfy this common interface.96 The reason is that \CFA uses a common \emph{object} trait (constructor, destructor and assignment operators) to handle passing dynamic concrete type arguments into polymorphic functions, and the reference types are handled differently in these contexts so they do not satisfy this common interface. 100 97 101 98 Moreover, there is also some discrepancy in how the reference types are treated in initialization and assignment expressions. 102 For example, in line 3 of the previous example code \see{\VPageref{p:refexamples}}:99 For example, in line 3 of the previous example code: 103 100 \begin{cfa} 104 101 int @&@ r1 = x, @&&@ r2 = r1, @&&&@ r3 = r2; $\C{// references to x}$ … … 108 105 For lines 6 and 9 of the previous example code: 109 106 \begin{cfa} 110 r1 = 3; r2 = 3; r3 = 3; $\C{// change x: implicit dereference *r1, **r2, ***r3}$107 r1 = 3; r2 = 3; r3 = 3; $\C{// change x: implicit dereference *r1, **r2, ***r3}$ 111 108 @&@r3 = @&@y; @&&@r3 = @&&@r4; $\C{// change r1, r2}$ 112 109 \end{cfa} … … 116 113 Finally, there is an annoying issue (although purely syntactic) for setting a mutable reference to a specific address like null, @int & r1 = *0p@, which looks like dereferencing a null pointer. 117 114 Here, the expression is rewritten as @int & r1 = &(*0p)@, like the variable dereference of @x@ above. 118 However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, \ie@&*@ cancel each other, giving @0p@.115 However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, i.e., @&*@ cancel each other, giving @0p@. 119 116 Therefore, the dereferencing operation does not actually happen and the expression is translated into directly initializing the reference variable with the address. 120 117 Note, the same explicit reference is used in \CC to set a reference variable to null. … … 126 123 When generic types were introduced to \CFA~\cite{Moss19}, some thought was given to allow reference types as type arguments. 127 124 \begin{cfa} 128 forall( T ) struct vector { T t; }; $\C{// generic type}$ 125 forall( T ) 126 struct vector { T t; }; 129 127 vector( int @&@ ) vec; $\C{// vector of references to ints}$ 130 128 \end{cfa} 131 While it is possible to write a reference type as the argument to a generic type, it is disallowed in assertion checking, if the generic type requires the object trait \see{\VPageref{p:objecttrait}}for the type argument (a fairly common use case).129 While it is possible to write a reference type as the argument to a generic type, it is disallowed in assertion checking, if the generic type requires the object trait for the type argument (a fairly common use case). 132 130 Even if the object trait can be made optional, the current type system often misbehaves by adding undesirable auto-dereference on the referenced-to value rather than the reference variable itself, as intended. 133 131 Some tweaks are necessary to accommodate reference types in polymorphic contexts and it is unclear what can or cannot be achieved. 134 Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax withreference types.132 Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax from reference types. 135 133 136 134 137 135 \section{Tuple Types} 138 136 139 The addition of tuple s to \CFA can be traced back to the original design by David Till in \mbox{K-W C}~\cite{Till89,Buhr94a}, a predecessor project of \CFA.137 The addition of tuple types to \CFA can be traced back to the original design by David Till in \mbox{K-W C}~\cite{Till89,Buhr94a}, a predecessor project of \CFA. 140 138 The primary purpose of tuples is to eliminate output parameters or creating an aggregate type to return multiple values from a function, called a multiple-value-returning (MVR) function. 141 Traditionally, returning multiple values is accomplished via (in/)output parameters or packing the results in a structure. 142 The following examples show these two techniques for a function returning three values. 139 The following examples shows the two techniques for a function returning three values. 143 140 \begin{cquote} 144 141 \begin{tabular}{@{}l@{\hspace{20pt}}l@{}} … … 158 155 \end{tabular} 159 156 \end{cquote} 160 K-W C allows direct return of multiple values into a tuple.157 where K-W C allows direct return of multiple values. 161 158 \begin{cfa} 162 159 @[int, int, int]@ foo( int p2, int p3 ); 163 160 @[x, y, z]@ = foo( y, z ); // return 3 values into a tuple 164 161 \end{cfa} 165 Along with making returning multiple values a first-class feature, tuples were extended to simplify a number of other common context that normally require multiple statements and/or additional declarations, all of which reduces coding time and errors.162 Along with simplifying returning multiple values, tuples can be extended to simplify a number of other common context that normally require multiple statements and/or additional declarations, all of which reduces coding time and errors. 166 163 \begin{cfa} 167 164 [x, y, z] = 3; $\C[2in]{// x = 3; y = 3; z = 3, where types are different}$ 168 165 [x, y] = [y, x]; $\C{// int tmp = x; x = y; y = tmp;}$ 169 166 void bar( int, int, int ); 170 bar( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$167 @bar@( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$ 171 168 x = foo( 3, 4 )@.1@; $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); x = t1;}\CRT$ 172 169 \end{cfa} 173 For the call to @bar@, the three results (tuple value)from @foo@ are \newterm{flattened} into individual arguments.170 For the call to @bar@, the three results from @foo@ are \newterm{flattened} into individual arguments. 174 171 Flattening is how tuples interact with parameter and subscript lists, and with other tuples, \eg: 175 172 \begin{cfa} … … 177 174 [ x, y, z, a, b, c] = [2, 3, 4, foo( 3, 4) ] $\C{// flattened, where foo results are t0, t1, t2}$ 178 175 \end{cfa} 179 Note, in most cases, a tuple is just compile-time syntactic-sugar for a number of individual assignments statements and possibly temporary variables. 180 Only when returning a tuple from a function is there the notion of a tuple value. 181 182 Overloading in the \CFA type-system must support complex composition of tuples and C type conversions using a costing scheme giving lower cost to widening conversions that do not truncate a value. 176 177 Note, the \CFA type-system supports complex composition of tuples and C type conversions using a costing scheme giving lower cost to widening conversions that do not truncate a value. 183 178 \begin{cfa} 184 179 [ int, int ] foo$\(_1\)$( int ); $\C{// overloaded foo functions}$ 185 180 [ double ] foo$\(_2\)$( int ); 186 181 void bar( int, double, double ); 187 bar( @foo@( 3 ), @foo@( 3 ) );182 bar( foo( 3 ), foo( 3 ) ); 188 183 \end{cfa} 189 184 The type resolver only has the tuple return types to resolve the call to @bar@ as the @foo@ parameters are identical, which involves unifying the flattened @foo@ return values with @bar@'s parameter list. … … 193 188 The programming language Go provides a similar but simplier tuple mechanism, as it does not have overloaded functions. 194 189 195 K-W C also supported tuple variables, but with a strong distinction between tuples and tuple values/variables. 196 \begin{quote} 197 Note that tuple variables are not themselves tuples. 198 Tuple variables reference contiguous areas of storage, in which tuple values are stored; 199 tuple variables and tuple values are entities which appear during program execution. 200 Tuples, on the other hand, are compile-time constructs; 201 they are lists of expressions, whose values may not be stored contiguously.~\cite[p.~30]{Till89} 202 \end{quote} 203 Fundamentally, a tuple value/variable is just a structure (contiguous areas) with an alternate (tuple) interface. 204 A tuple value/variable is assignable (like structures), its fields can be accessed using position rather than name qualification, and it can interact with regular tuples. 205 \begin{cfa} 206 [ int, int, int ] t1, t2; 207 t1 = t2; $\C{// tuple assignment}$ 208 t1@.1@ = t2@.0@; $\C{// position qualification}$ 209 int x, y, z; 210 t1 = [ x, y, z ]; $\C{// interact with regular tuples}$ 211 [ x, y, z ] = t1; 212 bar( t2 ); $\C{// bar defined above}$ 213 \end{cfa} 214 \VRef[Figure]{f:Nesting} shows The difference is nesting of structures and tuples. 215 The left \CC nested-structure is named so it is not flattened. 216 The middle C/\CC nested-structure is unnamed and flattened, causing an error because @i@ and @j@ are duplication names. 217 The right \CFA nested tuple cannot be named and is flattened. 218 C allows named nested-structures, but they have issues \see{\VRef{s:inlineSubstructure}}. 219 Note, it is common in C to have an unnamed @union@ so its fields do not require qualification. 220 221 \begin{figure} 222 \setlength{\tabcolsep}{15pt} 223 \begin{tabular}{@{}ll@{\hspace{90pt}}l@{}} 224 \multicolumn{1}{c}{\CC} & \multicolumn{1}{c}{C/\CC} & \multicolumn{1}{c}{tuple} \\ 225 \begin{cfa} 226 struct S { 227 struct @T@ { // not flattened 228 int i, j; 229 }; 230 int i, j; 231 }; 232 \end{cfa} 233 & 234 \begin{cfa} 235 struct S2 { 236 struct ${\color{red}/* unnamed */}$ { // flatten 237 int i, j; 238 }; 239 int i, j; 240 }; 241 \end{cfa} 242 & 243 \begin{cfa} 244 [ 245 [ // flatten 246 1, 2 247 ] 248 1, 2 249 ] 250 \end{cfa} 251 \end{tabular} 252 \caption{Nesting} 253 \label{f:Nesting} 254 \end{figure} 255 256 The primary issues for tuples in the \CFA type system are polymorphism and conversions. 257 Specifically, does it make sense to have a generic (polymorphic) tuple type, as is possible for a structure? 258 \begin{cfa} 259 forall( T, S ) [ T, S ] GT; // polymorphic tuple type 260 GT( int, char ) @gt@; 261 GT( int, double ) @gt@; 262 @gt@ = [ 3, 'a' ]; // select correct gt 263 @gt@ = [ 3, 3.5 ]; 264 \end{cfa} 265 and what is the cost model for C conversions across multiple values? 266 \begin{cfa} 267 gt = [ 'a', 3L ]; // select correct gt 268 \end{cfa} 269 270 271 \section{Tuple Implementation} 272 273 As noted, tradition languages manipulate multiple values by in/out parameters and/or structures. 274 K-W C adopted the structure for tuple values or variables, and as needed, the fields are extracted by field access operations. 275 As well, For the tuple-assignment implementation, the left-hand tuple expression is expanded into assignments of each component, creating temporary variables to avoid unexpected side effects. 276 For example, the tuple value returned from @foo@ is a structure, and its fields are individually assigned to a left-hand tuple, @x@, @y@, @z@, or copied directly into a corresponding tuple variable. 190 The K-W C tuples are merely syntactic sugar, as there is no mechanism to define a variable with tuple type. 191 For the tuple-returning implementation, an implicit @struct@ type is created for the returning tuple and the values are extracted by field access operations. 192 For the tuple-assignment implementation, the left-hand tuple expression is expanded into assignments of each component, creating temporary variables to avoid unexpected side effects. 193 For example, a structure is returned from @foo@ and its fields are individually assigned to the left-hand variables, @x@, @y@, @z@. 277 194 278 195 In the second implementation of \CFA tuples by Rodolfo Gabriel Esteves~\cite{Esteves04}, a different strategy is taken to handle MVR functions. … … 286 203 [x, y] = gives_two(); 287 204 \end{cfa} 288 The TillK-W C implementation translates the program to:205 The K-W C implementation translates the program to: 289 206 \begin{cfa} 290 207 struct _tuple2 { int _0; int _1; } 291 struct _tuple2 gives_two() { ... struct _tuple2 ret = { r1, r2 }, return ret; }208 struct _tuple2 gives_two(); 292 209 int x, y; 293 210 struct _tuple2 _tmp = gives_two(); 294 211 x = _tmp._0; y = _tmp._1; 295 212 \end{cfa} 296 while the Rodolfo implementation translates it to:213 While the Rodolfo implementation translates it to: 297 214 \begin{cfa} 298 215 void gives_two( int * r1, int * r2 ) { ... *r1 = ...; *r2 = ...; return; } … … 301 218 \end{cfa} 302 219 and inside the body of the function @gives_two@, the return statement is rewritten as assignments into the passed-in argument addresses. 303 This implementation looks more concise, and in the case of returning values having nontrivial types , \eg aggregates, this implementation saves unnecessary copying.220 This implementation looks more concise, and in the case of returning values having nontrivial types (\eg aggregates), this implementation saves unnecessary copying. 304 221 For example, 305 222 \begin{cfa} … … 317 234 The downside is indirection within @gives_two@ to access values, unless values get hoisted into registers for some period of time, which is common. 318 235 319 Interestingly, in the third implementation of \CFA tuples by Robert Schluntz~\cite[\S~3]{Schluntz17}, the MVR functions revert back to structure based, where it remains in the current version of \CFA.320 The reason for the reversion was to have a uniform approach for tuple values/variables making tuples first-class types in \CFA, \ie allow tuples with correspondingtuple variables.236 Interestingly, in the third implementation of \CFA tuples by Robert Schluntz~\cite[\S~3]{Schluntz17}, the tuple type reverts back to structure based, where it remains in the current version of the cfa-cc translator. 237 The reason for the reversion was to make tuples become first-class types in \CFA, \ie allow tuple variables. 321 238 This extension was possible, because in parallel with Schluntz's work, generic types were being added independently by Moss~\cite{Moss19}, and the tuple variables leveraged the same implementation techniques as the generic variables. 322 \PAB{I'm not sure about the connection here. Do you have an example of what you mean?}323 239 324 240 However, after experience gained building the \CFA runtime system, making tuple-types first-class seems to add little benefit. … … 361 277 362 278 Finally, a type-safe variadic argument signature was added by Robert Schluntz~\cite[\S~4.1.2]{Schluntz17} using @forall@ and a new tuple parameter-type, denoted by the keyword @ttype @ in Schluntz's implementation, but changed to the ellipsis syntax similar to \CC's template parameter pack. 363 For C variadics, \eg @va_list@,the number and types of the arguments must be conveyed in some way, \eg @printf@ uses a format string indicating the number and types of the arguments.279 For C variadics, the number and types of the arguments must be conveyed in some way, \eg @printf@ uses a format string indicating the number and types of the arguments. 364 280 \VRef[Figure]{f:CVariadicMaxFunction} shows an $N$ argument @maxd@ function using the C untyped @va_list@ interface. 365 281 In the example, the first argument is the number of following arguments, and the following arguments are assumed to be @double@; 366 282 looping is used to traverse the argument pack from left to right. 367 The @va_list@ interface is walking up the stack (by address)looking at the arguments pushed by the caller.283 The @va_list@ interface is walking up (by address) the stack looking at the arguments pushed by the caller. 368 284 (Magic knowledge is needed for arguments pushed using registers.) 369 285 … … 499 415 500 416 \section{\lstinline{inline} Substructure} 501 \label{s:inlineSubstructure} 502 503 As mentioned \see{\VRef[Figure]{f:Nesting}}, C allows an anonymous aggregate type (@struct@ or @union@) to be embedded (nested) within another one, \eg a tagged union. 417 418 C allows an anonymous aggregate type (@struct@ or @union@) to be embedded (nested) within another one, \eg a tagged union. 504 419 \begin{cfa} 505 420 struct S { … … 533 448 534 449 As an aside, C nested \emph{named} aggregates behave in a (mysterious) way because the nesting is allowed but there is no ability to use qualification to access an inner type, like the \CC type operator `@::@'. 535 \emph{In fact, all named nested aggregates are hoisted to global scope, regardless of the nesting depth.} 450 In fact, all named nested aggregates are hoisted to global scope, regardless of the nesting depth. 536 451 \begin{cquote} 537 452 \begin{tabular}{@{}l@{\hspace{35pt}}l@{}} … … 583 498 struct S s; @s::T@.i; @s::U@.k; 584 499 \end{cfa} 585 \CFA chose to adopt the \CC non-compatible change for nested types, since \CC's change has already forced certain coding changes in C libraries that must be parsed by \CC. 500 501 As an aside, \CFA also provides a backwards compatible \CC nested-type. 502 \begin{cfa} 503 struct S { 504 @auto@ struct T { 505 int i, j; 506 }; 507 @auto@ struct U { 508 int k, l; 509 }; 510 }; 511 \end{cfa} 512 The keyword @auto@ denotes a local (scoped) declaration, and here, it implies a local (scoped) type, using dot as the type qualifier, \eg @S.T t@. 513 Alternatively, \CFA could adopt the \CC non-compatible change for nested types, since it may have already forced certain coding changes in C libraries that must be parsed by \CC. 586 514 587 515 % https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html … … 603 531 } s; 604 532 \end{cfa} 605 Note, the position of the substructure is normally unimportant , unless there is some form of memory or @union@ overlay.533 Note, the position of the substructure is normally unimportant. 606 534 Like the anonymous nested types, the aggregate field names are hoisted into @struct S@, so there is direct access, \eg @s.x@ and @s.i@. 607 535 However, like the implicit C hoisting of nested structures, the field names must be unique and the type names are now at a different scope level, unlike type nesting in \CC. -
doc/theses/fangren_yu_MMath/intro.tex
r86841ee r2d6add4 34 34 Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns. 35 35 Like \CC, \CFA also allows these operators to be overloaded with user-defined types. 36 The syntax for operator names uses the @'?'@ character to denote a parameter, \eg unary operators: @?++@, @++?@, binary operator@?+?@.36 The syntax for operator names uses the @'?'@ character to denote a function parameter, \eg prefix, postfix, and infix increment operators: @++?@, @?++@, and @?+?@. 37 37 Here, a user-defined type is extended with an addition operation with the same syntax as builtin types. 38 38 \begin{cfa} … … 41 41 S s1, s2; 42 42 s1 = s1 @+@ s2; $\C[1.75in]{// infix call}$ 43 s1 = @?+?@( s1, s2 ); $\C{// direct call }\CRT$44 \end{cfa} 45 The type system examines each call size and selects the best matching overloaded function based on the number and types of arguments.46 If there are mixed-mode operands, @2 + 3.5@, the type system, like in C/\CC, attempts (safe) conversions, convertingthe argument type(s) to the parameter type(s).43 s1 = @?+?@( s1, s2 ); $\C{// direct call using operator name}\CRT$ 44 \end{cfa} 45 The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments. 46 If there are mixed-mode operands, @2 + 3.5@, the \CFA type system, like C/\CC, attempts (safe) conversions, converting one or more of the argument type(s) to the parameter type(s). 47 47 Conversions are necessary because the hardware rarely supports mix-mode operations, so both operands must be the same type. 48 48 Note, without implicit conversions, programmers must write an exponential number of functions covering all possible exact-match cases among all possible types. … … 113 113 \section{Type Inferencing} 114 114 115 Every variable has a type, but association between them can occur in different ways:116 at the point where the variable comes into existence (declaration) and/or on each assignment to the variable.117 \begin{cfa}118 double x; $\C{// type only}$119 float y = 3.1D; $\C{// type and initialization}$120 auto z = y; $\C{// initialization only}$121 z = "abc"; $\C{// assignment}$122 \end{cfa}123 For type-and-initialization, the specified and initialization types may not agree.124 Similarity, for assignment the current variable and expression types may not agree.125 For type-only, the programmer specifies the initial type, which remains fixed for the variable's lifetime in statically typed languages.126 In the other cases, the compiler may select the type by melding programmer and context information.127 When the compiler participates in type selection, it is called \newterm{type inferencing}.128 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.129 130 115 One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}. 131 116 Here, the type resolver starts with the types of the program constants used for initialization and these constant types flow throughout the program, setting all variable and expression types. … … 147 132 \end{cfa} 148 133 In both overloads of @f@, the type system works from the constant initializations inwards and/or outwards to determine the types of all variables and functions. 149 Note, like template meta-programming, there c ouldbe a new function generated for the second @f@ depending on the types of the arguments, assuming these types are meaningful in the body of the @f@.134 Note, like template meta-programming, there can be a new function generated for the second @f@ depending on the types of the arguments, assuming these types are meaningful in the body of the @f@. 150 135 Inferring type constraints, by analysing the body of @f@ is possible, and these constraints must be satisfied at each call site by the argument types; 151 136 in this case, parametric polymorphism can allow separate compilation. … … 185 170 Not determining or writing long generic types, \eg, given deeply nested generic types. 186 171 \begin{cfa} 187 typedef T1(int).T2(float).T3(char).T @ST@; $\C{// \CFA nested type declaration}$188 @ST@x, y, x;172 typedef T1(int).T2(float).T3(char).T ST; $\C{// \CFA nested type declaration}$ 173 ST x, y, x; 189 174 \end{cfa} 190 175 This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages. … … 208 193 \section{Type-Inferencing Issues} 209 194 210 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. 211 212 A convenience is having the compiler use its overarching program knowledge to select the best type for each variable based on some notion of \emph{best}, which simplifies the programming experience. 213 214 A restriction is the conundrum in type inferencing of when to \emph{brand} a type. 195 Each kind of type-inferencing system has its own set of issues that flow onto the programmer in the form of restrictions and/or confusions. 196 \begin{enumerate}[leftmargin=*] 197 \item 198 There can be large blocks of code where all declarations are @auto@. 199 As a result, understanding and changing the code becomes almost impossible. 200 Types provide important clues as to the behaviour of the code, and correspondingly to correctly change or add new code. 201 In these cases, a programmer is forced to re-engineer types, which is fragile, or rely on a fancy IDE that can re-engineer types. 202 \item 203 The problem of unknown types becomes acute when the concrete type must be used, \eg, given: 204 \begin{cfa} 205 auto x = @...@ 206 \end{cfa} 207 and the need to write a routine to compute using @x@ 208 \begin{cfa} 209 void rtn( @...@ parm ); 210 rtn( x ); 211 \end{cfa} 212 A programmer must re-engineer the type of @x@'s initialization expression, reconstructing the possibly long generic type-name. 213 In this situation, having the type name or its short alias is essential. 214 \item 215 There is the conundrum in type inferencing of when to \emph{brand} a type. 215 216 That is, when is the type of the variable/function more important than the type of its initialization expression. 216 217 For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors. 217 At some point, a variable's type needs to remain constant and the initializing expression needs to be modified or in error when it changes. 218 Often type-inferencing systems allow restricting (\newterm{branding}) a variable or function type, so the complier can report a mismatch with the constant initialization. 218 At some point, a variable's type needs to remain constant and the expression needs to be modified or in error when it changes. 219 Often type-inferencing systems allow \newterm{branding} a variable or function type; 220 now the complier can report a mismatch on the constant. 219 221 \begin{cfa} 220 222 void f( @int@ x, @int@ y ) { // brand function prototype … … 226 228 \end{cfa} 227 229 In Haskell, it is common for programmers to brand (type) function parameters. 228 229 A confusion is large blocks of code where all declarations are @auto@. 230 As a result, understanding and changing the code becomes almost impossible. 231 Types provide important clues as to the behaviour of the code, and correspondingly to correctly change or add new code. 232 In these cases, a programmer is forced to re-engineer types, which is fragile, or rely on a fancy IDE that can re-engineer types. 233 For example, given: 234 \begin{cfa} 235 auto x = @...@ 236 \end{cfa} 237 and the need to write a routine to compute using @x@ 238 \begin{cfa} 239 void rtn( @type of x@ parm ); 240 rtn( x ); 241 \end{cfa} 242 A programmer must re-engineer the type of @x@'s initialization expression, reconstructing the possibly long generic type-name. 243 In this situation, having the type name or its short alias is essential. 244 245 \CFA's type system tries to prevent type-resolution mistakes by relying heavily on the type of the left-hand side of assignment to pinpoint the right types within an expression. 230 \end{enumerate} 231 232 \CFA's type system is trying to prevent type-resolution mistakes by relying heavily on the type of the left-hand side of assignment to pinpoint the right types for an expression computation. 246 233 Type inferencing defeats this goal because there is no left-hand type. 247 Fundamentally, type inferencing tries to magic away variabletypes from the programmer.234 Fundamentally, type inferencing tries to magic away types from the programmer. 248 235 However, this results in lazy programming with the potential for poor performance and safety concerns. 249 Types are as important as control-flow in writing a good program, and should not be masked, even if it requires the programmer to think!250 A similar issue is garbage collection, where storage management is masked, resulting in poor program design and performance.236 Types are as important as control-flow, and should not be masked, even if it requires the programmer to think! 237 A similar example is garbage collection, where storage management is masked, resulting in poor program design and performance. 251 238 The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming. 252 239 Understanding space and time issues are an essential part of the programming craft.
Note:
See TracChangeset
for help on using the changeset viewer.