- Timestamp:
- Sep 29, 2024, 4:02:18 PM (5 weeks ago)
- Branches:
- master
- Children:
- 86841ee
- Parents:
- 569b118
- Location:
- doc/theses/fangren_yu_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/content1.tex
r569b118 rc82dad4 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 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. 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. 21 22 22 23 The following examples shows how pointers and references are treated uniformly in \CFA. 23 24 \begin{cfa}[numbers=left,numberblanklines=false] 24 int x = 1, y = 2, z = 3; 25 int x = 1, y = 2, z = 3;$\label{p:refexamples}$ 25 26 int * p1 = &x, ** p2 = &p1, *** p3 = &p2, $\C{// pointers to x}$ 26 27 @&@ r1 = x, @&&@ r2 = r1, @&&&@ r3 = r2; $\C{// references to x}$ … … 34 35 \end{cfa} 35 36 Like pointers, reference can be cascaded, \ie a reference to a reference, \eg @&& r2@.\footnote{ 36 \CC uses \lstinline{&&} for rvalue reference a feature for move semantics and handling the \lstinline{const} Hell problem.}37 \CC uses \lstinline{&&} for rvalue reference, a feature for move semantics and handling the \lstinline{const} Hell problem.} 37 38 Usage of a reference variable automatically performs the same number of dereferences as the number of references in its declaration, \eg @r3@ becomes @***r3@. 38 39 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@. … … 46 47 \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. 47 48 \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. 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.} 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.} 51 54 In an assignment context, the left-hand-side operand-type is always reduced to a single reference. 52 55 \end{enumerate} … … 90 93 \end{cfa} 91 94 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.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, \ie by-value or by-reference. 93 96 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. 94 97 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. 95 98 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. 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.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. 97 100 98 101 Moreover, there is also some discrepancy in how the reference types are treated in initialization and assignment expressions. 99 For example, in line 3 of the previous example code :102 For example, in line 3 of the previous example code \see{\VPageref{p:refexamples}}: 100 103 \begin{cfa} 101 104 int @&@ r1 = x, @&&@ r2 = r1, @&&&@ r3 = r2; $\C{// references to x}$ … … 105 108 For lines 6 and 9 of the previous example code: 106 109 \begin{cfa} 107 r1 = 3; r2 = 3; 110 r1 = 3; r2 = 3; r3 = 3; $\C{// change x: implicit dereference *r1, **r2, ***r3}$ 108 111 @&@r3 = @&@y; @&&@r3 = @&&@r4; $\C{// change r1, r2}$ 109 112 \end{cfa} … … 113 116 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. 114 117 Here, the expression is rewritten as @int & r1 = &(*0p)@, like the variable dereference of @x@ above. 115 However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, i.e.,@&*@ cancel each other, giving @0p@.118 However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, \ie @&*@ cancel each other, giving @0p@. 116 119 Therefore, the dereferencing operation does not actually happen and the expression is translated into directly initializing the reference variable with the address. 117 120 Note, the same explicit reference is used in \CC to set a reference variable to null. … … 123 126 When generic types were introduced to \CFA~\cite{Moss19}, some thought was given to allow reference types as type arguments. 124 127 \begin{cfa} 125 forall( T ) 126 struct vector { T t; }; 128 forall( T ) struct vector { T t; }; $\C{// generic type}$ 127 129 vector( int @&@ ) vec; $\C{// vector of references to ints}$ 128 130 \end{cfa} 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).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). 130 132 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. 131 133 Some tweaks are necessary to accommodate reference types in polymorphic contexts and it is unclear what can or cannot be achieved. 132 Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax fromreference types.134 Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax with reference types. 133 135 134 136 135 137 \section{Tuple Types} 136 138 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.139 The addition of tuples 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. 138 140 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. 139 The following examples shows the two techniques for a function returning three values. 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. 140 143 \begin{cquote} 141 144 \begin{tabular}{@{}l@{\hspace{20pt}}l@{}} … … 155 158 \end{tabular} 156 159 \end{cquote} 157 where K-W C allows direct return of multiple values.160 K-W C allows direct return of multiple values into a tuple. 158 161 \begin{cfa} 159 162 @[int, int, int]@ foo( int p2, int p3 ); 160 163 @[x, y, z]@ = foo( y, z ); // return 3 values into a tuple 161 164 \end{cfa} 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.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. 163 166 \begin{cfa} 164 167 [x, y, z] = 3; $\C[2in]{// x = 3; y = 3; z = 3, where types are different}$ 165 168 [x, y] = [y, x]; $\C{// int tmp = x; x = y; y = tmp;}$ 166 169 void bar( int, int, int ); 167 @bar@( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$170 bar( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$ 168 171 x = foo( 3, 4 )@.1@; $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); x = t1;}\CRT$ 169 172 \end{cfa} 170 For the call to @bar@, the three results from @foo@ are \newterm{flattened} into individual arguments.173 For the call to @bar@, the three results (tuple value) from @foo@ are \newterm{flattened} into individual arguments. 171 174 Flattening is how tuples interact with parameter and subscript lists, and with other tuples, \eg: 172 175 \begin{cfa} … … 174 177 [ x, y, z, a, b, c] = [2, 3, 4, foo( 3, 4) ] $\C{// flattened, where foo results are t0, t1, t2}$ 175 178 \end{cfa} 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. 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. 178 183 \begin{cfa} 179 184 [ int, int ] foo$\(_1\)$( int ); $\C{// overloaded foo functions}$ 180 185 [ double ] foo$\(_2\)$( int ); 181 186 void bar( int, double, double ); 182 bar( foo( 3 ), foo( 3 ) );187 bar( @foo@( 3 ), @foo@( 3 ) ); 183 188 \end{cfa} 184 189 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. … … 188 193 The programming language Go provides a similar but simplier tuple mechanism, as it does not have overloaded functions. 189 194 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@. 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. 194 277 195 278 In the second implementation of \CFA tuples by Rodolfo Gabriel Esteves~\cite{Esteves04}, a different strategy is taken to handle MVR functions. … … 203 286 [x, y] = gives_two(); 204 287 \end{cfa} 205 The K-W C implementation translates the program to:288 The Till K-W C implementation translates the program to: 206 289 \begin{cfa} 207 290 struct _tuple2 { int _0; int _1; } 208 struct _tuple2 gives_two() ;291 struct _tuple2 gives_two() { ... struct _tuple2 ret = { r1, r2 }, return ret; } 209 292 int x, y; 210 293 struct _tuple2 _tmp = gives_two(); 211 294 x = _tmp._0; y = _tmp._1; 212 295 \end{cfa} 213 While the Rodolfo implementation translates it to:296 while the Rodolfo implementation translates it to: 214 297 \begin{cfa} 215 298 void gives_two( int * r1, int * r2 ) { ... *r1 = ...; *r2 = ...; return; } … … 218 301 \end{cfa} 219 302 and inside the body of the function @gives_two@, the return statement is rewritten as assignments into the passed-in argument addresses. 220 This implementation looks more concise, and in the case of returning values having nontrivial types (\eg aggregates), this implementation saves unnecessary copying.303 This implementation looks more concise, and in the case of returning values having nontrivial types, \eg aggregates, this implementation saves unnecessary copying. 221 304 For example, 222 305 \begin{cfa} … … 234 317 The downside is indirection within @gives_two@ to access values, unless values get hoisted into registers for some period of time, which is common. 235 318 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 allowtuple variables.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 corresponding tuple variables. 238 321 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?} 239 323 240 324 However, after experience gained building the \CFA runtime system, making tuple-types first-class seems to add little benefit. … … 277 361 278 362 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. 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.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. 280 364 \VRef[Figure]{f:CVariadicMaxFunction} shows an $N$ argument @maxd@ function using the C untyped @va_list@ interface. 281 365 In the example, the first argument is the number of following arguments, and the following arguments are assumed to be @double@; 282 366 looping is used to traverse the argument pack from left to right. 283 The @va_list@ interface is walking up (by address) the stacklooking at the arguments pushed by the caller.367 The @va_list@ interface is walking up the stack (by address) looking at the arguments pushed by the caller. 284 368 (Magic knowledge is needed for arguments pushed using registers.) 285 369 … … 415 499 416 500 \section{\lstinline{inline} Substructure} 417 418 C allows an anonymous aggregate type (@struct@ or @union@) to be embedded (nested) within another one, \eg a tagged union. 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. 419 504 \begin{cfa} 420 505 struct S { … … 448 533 449 534 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 `@::@'. 450 In fact, all named nested aggregates are hoisted to global scope, regardless of the nesting depth. 535 \emph{In fact, all named nested aggregates are hoisted to global scope, regardless of the nesting depth.} 451 536 \begin{cquote} 452 537 \begin{tabular}{@{}l@{\hspace{35pt}}l@{}} … … 498 583 struct S s; @s::T@.i; @s::U@.k; 499 584 \end{cfa} 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. 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. 514 586 515 587 % https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html … … 531 603 } s; 532 604 \end{cfa} 533 Note, the position of the substructure is normally unimportant .605 Note, the position of the substructure is normally unimportant, unless there is some form of memory or @union@ overlay. 534 606 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@. 535 607 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
r569b118 rc82dad4 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 function parameter, \eg prefix, postfix, and infix increment operators: @++?@, @?++@, and@?+?@.36 The syntax for operator names uses the @'?'@ character to denote a parameter, \eg unary operators: @?++@, @++?@, binary operator @?+?@. 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 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 thearguments.46 If there are mixed-mode operands, @2 + 3.5@, the \CFA type system, like C/\CC, attempts (safe) conversions, converting one or more ofthe argument type(s) to the parameter type(s).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, converting 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 115 130 One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}. 116 131 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. … … 132 147 \end{cfa} 133 148 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. 134 Note, like template meta-programming, there c anbe 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@.149 Note, like template meta-programming, there could 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@. 135 150 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; 136 151 in this case, parametric polymorphism can allow separate compilation. … … 170 185 Not determining or writing long generic types, \eg, given deeply nested generic types. 171 186 \begin{cfa} 172 typedef T1(int).T2(float).T3(char).T ST; $\C{// \CFA nested type declaration}$173 STx, y, x;187 typedef T1(int).T2(float).T3(char).T @ST@; $\C{// \CFA nested type declaration}$ 188 @ST@ x, y, x; 174 189 \end{cfa} 175 190 This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages. … … 193 208 \section{Type-Inferencing Issues} 194 209 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. 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. 216 215 That is, when is the type of the variable/function more important than the type of its initialization expression. 217 216 For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors. 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. 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. 221 219 \begin{cfa} 222 220 void f( @int@ x, @int@ y ) { // brand function prototype … … 228 226 \end{cfa} 229 227 In Haskell, it is common for programmers to brand (type) function parameters. 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. 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. 233 246 Type inferencing defeats this goal because there is no left-hand type. 234 Fundamentally, type inferencing tries to magic away types from the programmer.247 Fundamentally, type inferencing tries to magic away variable types from the programmer. 235 248 However, this results in lazy programming with the potential for poor performance and safety concerns. 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.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. 238 251 The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming. 239 252 Understanding space and time issues are an essential part of the programming craft.
Note: See TracChangeset
for help on using the changeset viewer.