Changes in / [86841ee:2d6add4]


Ignore:
Location:
doc/theses/fangren_yu_MMath
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/fangren_yu_MMath/content1.tex

    r86841ee r2d6add4  
    1515Alternatively, use a reference when its primary purpose is to alias a value, \eg a function parameter that does not copy the argument (performance reason).
    1616Here, 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.
     17Succinctly, if the address often changes, use a pointer;
     18if the value often changes, use a reference.
     19Note, \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.
    2221
    2322The following examples shows how pointers and references are treated uniformly in \CFA.
    2423\begin{cfa}[numbers=left,numberblanklines=false]
    25 int x = 1, y = 2, z = 3;$\label{p:refexamples}$
     24int x = 1, y = 2, z = 3;
    2625int * p1 = &x, ** p2 = &p1,  *** p3 = &p2,      $\C{// pointers to x}$
    2726        @&@ r1 = x,  @&&@ r2 = r1,   @&&&@ r3 = r2;     $\C{// references to x}$
     
    3534\end{cfa}
    3635Like 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.}
    3837Usage of a reference variable automatically performs the same number of dereferences as the number of references in its declaration, \eg @r3@ becomes @***r3@.
    3938Finally, 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@.
     
    4746    \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.
    4847    \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.}
    5451        In an assignment context, the left-hand-side operand-type is always reduced to a single reference.
    5552\end{enumerate}
     
    9390\end{cfa}
    9491
    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.
     92In 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.
    9693However, 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.
    9794Without 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.
    9895This 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.
     96The 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.
    10097
    10198Moreover, 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}}:
     99For example, in line 3 of the previous example code:
    103100\begin{cfa}
    104101int @&@ r1 = x,  @&&@ r2 = r1,   @&&&@ r3 = r2; $\C{// references to x}$
     
    108105For lines 6 and 9 of the previous example code:
    109106\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}$
    111108@&@r3 = @&@y; @&&@r3 = @&&@r4;                          $\C{// change r1, r2}$
    112109\end{cfa}
     
    116113Finally, 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.
    117114Here, 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@.
     115However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, i.e., @&*@ cancel each other, giving @0p@.
    119116Therefore, the dereferencing operation does not actually happen and the expression is translated into directly initializing the reference variable with the address.
    120117Note, the same explicit reference is used in \CC to set a reference variable to null.
     
    126123When generic types were introduced to \CFA~\cite{Moss19}, some thought was given to allow reference types as type arguments.
    127124\begin{cfa}
    128 forall( T ) struct vector { T t; }; $\C{// generic type}$
     125forall( T )
     126struct vector { T t; };
    129127vector( int @&@ ) vec; $\C{// vector of references to ints}$
    130128\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).
     129While 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).
    132130Even 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.
    133131Some 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 with reference types.
     132Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax from reference types.
    135133
    136134
    137135\section{Tuple Types}
    138136
    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.
     137The 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.
    140138The 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.
     139The following examples shows the two techniques for a function returning three values.
    143140\begin{cquote}
    144141\begin{tabular}{@{}l@{\hspace{20pt}}l@{}}
     
    158155\end{tabular}
    159156\end{cquote}
    160 K-W C allows direct return of multiple values into a tuple.
     157where K-W C allows direct return of multiple values.
    161158\begin{cfa}
    162159@[int, int, int]@ foo( int p2, int p3 );
    163160@[x, y, z]@ = foo( y, z );  // return 3 values into a tuple
    164161\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.
     162Along 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.
    166163\begin{cfa}
    167164[x, y, z] = 3; $\C[2in]{// x = 3; y = 3; z = 3, where types are different}$
    168165[x, y] = [y, x]; $\C{// int tmp = x; x = y; y = tmp;}$
    169166void 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 );}$
    171168x = foo( 3, 4 )@.1@; $\C{//  int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); x = t1;}\CRT$
    172169\end{cfa}
    173 For the call to @bar@, the three results (tuple value) from @foo@ are \newterm{flattened} into individual arguments.
     170For the call to @bar@, the three results from @foo@ are \newterm{flattened} into individual arguments.
    174171Flattening is how tuples interact with parameter and subscript lists, and with other tuples, \eg:
    175172\begin{cfa}
     
    177174[ x, y, z, a, b, c] = [2, 3, 4, foo( 3, 4) ]  $\C{// flattened, where foo results are t0, t1, t2}$
    178175\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
     177Note, 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.
    183178\begin{cfa}
    184179[ int, int ] foo$\(_1\)$( int );                        $\C{// overloaded foo functions}$
    185180[ double ] foo$\(_2\)$( int );
    186181void bar( int, double, double );
    187 bar( @foo@( 3 ), @foo@( 3 ) );
     182bar( foo( 3 ), foo( 3 ) );
    188183\end{cfa}
    189184The 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.
     
    193188The programming language Go provides a similar but simplier tuple mechanism, as it does not have overloaded functions.
    194189
    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.
     190The K-W C tuples are merely syntactic sugar, as there is no mechanism to define a variable with tuple type.
     191For the tuple-returning implementation, an implicit @struct@ type is created for the returning tuple and the values are extracted by field access operations.
     192For the tuple-assignment implementation, the left-hand tuple expression is expanded into assignments of each component, creating temporary variables to avoid unexpected side effects.
     193For example, a structure is returned from @foo@ and its fields are individually assigned to the left-hand variables, @x@, @y@, @z@.
    277194
    278195In the second implementation of \CFA tuples by Rodolfo Gabriel Esteves~\cite{Esteves04}, a different strategy is taken to handle MVR functions.
     
    286203[x, y] = gives_two();
    287204\end{cfa}
    288 The Till K-W C implementation translates the program to:
     205The K-W C implementation translates the program to:
    289206\begin{cfa}
    290207struct _tuple2 { int _0; int _1; }
    291 struct _tuple2 gives_two() { ... struct _tuple2 ret = { r1, r2 }, return ret; }
     208struct _tuple2 gives_two();
    292209int x, y;
    293210struct _tuple2 _tmp = gives_two();
    294211x = _tmp._0; y = _tmp._1;
    295212\end{cfa}
    296 while the Rodolfo implementation translates it to:
     213While the Rodolfo implementation translates it to:
    297214\begin{cfa}
    298215void gives_two( int * r1, int * r2 ) { ... *r1 = ...; *r2 = ...; return; }
     
    301218\end{cfa}
    302219and 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.
     220This implementation looks more concise, and in the case of returning values having nontrivial types (\eg aggregates), this implementation saves unnecessary copying.
    304221For example,
    305222\begin{cfa}
     
    317234The downside is indirection within @gives_two@ to access values, unless values get hoisted into registers for some period of time, which is common.
    318235
    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.
     236Interestingly, 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.
     237The reason for the reversion was to make tuples become first-class types in \CFA, \ie allow tuple variables.
    321238This 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?}
    323239
    324240However, after experience gained building the \CFA runtime system, making tuple-types first-class seems to add little benefit.
     
    361277
    362278Finally, 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.
     279For 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.
    364280\VRef[Figure]{f:CVariadicMaxFunction} shows an $N$ argument @maxd@ function using the C untyped @va_list@ interface.
    365281In the example, the first argument is the number of following arguments, and the following arguments are assumed to be @double@;
    366282looping 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.
     283The @va_list@ interface is walking up (by address) the stack looking at the arguments pushed by the caller.
    368284(Magic knowledge is needed for arguments pushed using registers.)
    369285
     
    499415
    500416\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
     418C allows an anonymous aggregate type (@struct@ or @union@) to be embedded (nested) within another one, \eg a tagged union.
    504419\begin{cfa}
    505420struct S {
     
    533448
    534449As 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.}
     450In fact, all named nested aggregates are hoisted to global scope, regardless of the nesting depth.
    536451\begin{cquote}
    537452\begin{tabular}{@{}l@{\hspace{35pt}}l@{}}
     
    583498struct S s;  @s::T@.i;  @s::U@.k;
    584499\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
     501As an aside, \CFA also provides a backwards compatible \CC nested-type.
     502\begin{cfa}
     503struct S {
     504        @auto@ struct T {
     505                int i, j;
     506        };
     507        @auto@ struct U {
     508                int k, l;
     509        };
     510};
     511\end{cfa}
     512The 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@.
     513Alternatively, \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.
    586514
    587515% https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html
     
    603531} s;
    604532\end{cfa}
    605 Note, the position of the substructure is normally unimportant, unless there is some form of memory or @union@ overlay.
     533Note, the position of the substructure is normally unimportant.
    606534Like the anonymous nested types, the aggregate field names are hoisted into @struct S@, so there is direct access, \eg @s.x@ and @s.i@.
    607535However, 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  
    3434Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns.
    3535Like \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 @?+?@.
     36The syntax for operator names uses the @'?'@ character to denote a function parameter, \eg prefix, postfix, and infix increment operators: @++?@, @?++@, and @?+?@.
    3737Here, a user-defined type is extended with an addition operation with the same syntax as builtin types.
    3838\begin{cfa}
     
    4141S s1, s2;
    4242s1 = 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, converting the argument type(s) to the parameter type(s).
     43s1 = @?+?@( s1, s2 );   $\C{// direct call using operator name}\CRT$
     44\end{cfa}
     45The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments.
     46If 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).
    4747Conversions are necessary because the hardware rarely supports mix-mode operations, so both operands must be the same type.
    4848Note, without implicit conversions, programmers must write an exponential number of functions covering all possible exact-match cases among all possible types.
     
    113113\section{Type Inferencing}
    114114
    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 
    130115One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}.
    131116Here, 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.
     
    147132\end{cfa}
    148133In 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 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@.
     134Note, 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@.
    150135Inferring type constraints, by analysing the body of @f@ is possible, and these constraints must be satisfied at each call site by the argument types;
    151136in this case, parametric polymorphism can allow separate compilation.
     
    185170Not determining or writing long generic types, \eg, given deeply nested generic types.
    186171\begin{cfa}
    187 typedef T1(int).T2(float).T3(char).T @ST@;  $\C{// \CFA nested type declaration}$
    188 @ST@ x, y, x;
     172typedef T1(int).T2(float).T3(char).T ST;  $\C{// \CFA nested type declaration}$
     173ST x, y, x;
    189174\end{cfa}
    190175This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages.
     
    208193\section{Type-Inferencing Issues}
    209194
    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.
     195Each 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
     198There can be large blocks of code where all declarations are @auto@.
     199As a result, understanding and changing the code becomes almost impossible.
     200Types provide important clues as to the behaviour of the code, and correspondingly to correctly change or add new code.
     201In 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
     203The problem of unknown types becomes acute when the concrete type must be used, \eg, given:
     204\begin{cfa}
     205auto x = @...@
     206\end{cfa}
     207and the need to write a routine to compute using @x@
     208\begin{cfa}
     209void rtn( @...@ parm );
     210rtn( x );
     211\end{cfa}
     212A programmer must re-engineer the type of @x@'s initialization expression, reconstructing the possibly long generic type-name.
     213In this situation, having the type name or its short alias is essential.
     214\item
     215There is the conundrum in type inferencing of when to \emph{brand} a type.
    215216That is, when is the type of the variable/function more important than the type of its initialization expression.
    216217For 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.
     218At some point, a variable's type needs to remain constant and the expression needs to be modified or in error when it changes.
     219Often type-inferencing systems allow \newterm{branding} a variable or function type;
     220now the complier can report a mismatch on the constant.
    219221\begin{cfa}
    220222void f( @int@ x, @int@ y ) {  // brand function prototype
     
    226228\end{cfa}
    227229In 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.
    246233Type inferencing defeats this goal because there is no left-hand type.
    247 Fundamentally, type inferencing tries to magic away variable types from the programmer.
     234Fundamentally, type inferencing tries to magic away types from the programmer.
    248235However, 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.
     236Types are as important as control-flow, and should not be masked, even if it requires the programmer to think!
     237A similar example is garbage collection, where storage management is masked, resulting in poor program design and performance.
    251238The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming.
    252239Understanding space and time issues are an essential part of the programming craft.
Note: See TracChangeset for help on using the changeset viewer.