Changeset c82dad4 for doc


Ignore:
Timestamp:
Sep 29, 2024, 4:02:18 PM (5 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
86841ee
Parents:
569b118
Message:

more proofreading of intro and content1 chapters

Location:
doc/theses/fangren_yu_MMath
Files:
2 edited

Legend:

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

    r569b118 rc82dad4  
    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 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.
     17Succinctly, if the address changes often, use a pointer;
     18if the value changes often, use a reference.
     19Note, \CC made its reference address immutable starting a \emph{belief} that immutability is a fundamental aspect of a reference's pointer.
     20The 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.
    2122
    2223The following examples shows how pointers and references are treated uniformly in \CFA.
    2324\begin{cfa}[numbers=left,numberblanklines=false]
    24 int x = 1, y = 2, z = 3;
     25int x = 1, y = 2, z = 3;$\label{p:refexamples}$
    2526int * p1 = &x, ** p2 = &p1,  *** p3 = &p2,      $\C{// pointers to x}$
    2627        @&@ r1 = x,  @&&@ r2 = r1,   @&&&@ r3 = r2;     $\C{// references to x}$
     
    3435\end{cfa}
    3536Like 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.}
    3738Usage of a reference variable automatically performs the same number of dereferences as the number of references in its declaration, \eg @r3@ becomes @***r3@.
    3839Finally, 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@.
     
    4647    \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.
    4748    \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.}
    5154        In an assignment context, the left-hand-side operand-type is always reduced to a single reference.
    5255\end{enumerate}
     
    9093\end{cfa}
    9194
    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.
     95In 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.
    9396However, 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.
    9497Without 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.
    9598This 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.
     99The 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.
    97100
    98101Moreover, 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:
     102For example, in line 3 of the previous example code \see{\VPageref{p:refexamples}}:
    100103\begin{cfa}
    101104int @&@ r1 = x,  @&&@ r2 = r1,   @&&&@ r3 = r2; $\C{// references to x}$
     
    105108For lines 6 and 9 of the previous example code:
    106109\begin{cfa}
    107  r1 =  3;       r2 = 3;         r3 = 3;                         $\C{// change x: implicit dereference *r1, **r2, ***r3}$
     110 r1 =  3;       r2 = 3;   r3 = 3;                               $\C{// change x: implicit dereference *r1, **r2, ***r3}$
    108111@&@r3 = @&@y; @&&@r3 = @&&@r4;                          $\C{// change r1, r2}$
    109112\end{cfa}
     
    113116Finally, 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.
    114117Here, 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@.
     118However, the implicit @&@ needs to be cancelled for an address, which is done with the @*@, \ie @&*@ cancel each other, giving @0p@.
    116119Therefore, the dereferencing operation does not actually happen and the expression is translated into directly initializing the reference variable with the address.
    117120Note, the same explicit reference is used in \CC to set a reference variable to null.
     
    123126When generic types were introduced to \CFA~\cite{Moss19}, some thought was given to allow reference types as type arguments.
    124127\begin{cfa}
    125 forall( T )
    126 struct vector { T t; };
     128forall( T ) struct vector { T t; }; $\C{// generic type}$
    127129vector( int @&@ ) vec; $\C{// vector of references to ints}$
    128130\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).
     131While 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).
    130132Even 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.
    131133Some 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 from reference types.
     134Currently, there are contexts where \CFA programmer must use pointer types, giving up the benefits of auto-dereference operations and better syntax with reference types.
    133135
    134136
    135137\section{Tuple Types}
    136138
    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.
     139The 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.
    138140The 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.
     141Traditionally, returning multiple values is accomplished via (in/)output parameters or packing the results in a structure.
     142The following examples show these two techniques for a function returning three values.
    140143\begin{cquote}
    141144\begin{tabular}{@{}l@{\hspace{20pt}}l@{}}
     
    155158\end{tabular}
    156159\end{cquote}
    157 where K-W C allows direct return of multiple values.
     160K-W C allows direct return of multiple values into a tuple.
    158161\begin{cfa}
    159162@[int, int, int]@ foo( int p2, int p3 );
    160163@[x, y, z]@ = foo( y, z );  // return 3 values into a tuple
    161164\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.
     165Along 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.
    163166\begin{cfa}
    164167[x, y, z] = 3; $\C[2in]{// x = 3; y = 3; z = 3, where types are different}$
    165168[x, y] = [y, x]; $\C{// int tmp = x; x = y; y = tmp;}$
    166169void bar( int, int, int );
    167 @bar@( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$
     170bar( foo( 3, 4 ) ); $\C{// int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); bar( t0, t1, t2 );}$
    168171x = foo( 3, 4 )@.1@; $\C{//  int t0, t1, t2; [t0, t1, t2] = foo( 3, 4 ); x = t1;}\CRT$
    169172\end{cfa}
    170 For the call to @bar@, the three results from @foo@ are \newterm{flattened} into individual arguments.
     173For the call to @bar@, the three results (tuple value) from @foo@ are \newterm{flattened} into individual arguments.
    171174Flattening is how tuples interact with parameter and subscript lists, and with other tuples, \eg:
    172175\begin{cfa}
     
    174177[ x, y, z, a, b, c] = [2, 3, 4, foo( 3, 4) ]  $\C{// flattened, where foo results are t0, t1, t2}$
    175178\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.
     179Note, in most cases, a tuple is just compile-time syntactic-sugar for a number of individual assignments statements and possibly temporary variables.
     180Only when returning a tuple from a function is there the notion of a tuple value.
     181
     182Overloading 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.
    178183\begin{cfa}
    179184[ int, int ] foo$\(_1\)$( int );                        $\C{// overloaded foo functions}$
    180185[ double ] foo$\(_2\)$( int );
    181186void bar( int, double, double );
    182 bar( foo( 3 ), foo( 3 ) );
     187bar( @foo@( 3 ), @foo@( 3 ) );
    183188\end{cfa}
    184189The 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.
     
    188193The programming language Go provides a similar but simplier tuple mechanism, as it does not have overloaded functions.
    189194
    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@.
     195K-W C also supported tuple variables, but with a strong distinction between tuples and tuple values/variables.
     196\begin{quote}
     197Note that tuple variables are not themselves tuples.
     198Tuple variables reference contiguous areas of storage, in which tuple values are stored;
     199tuple variables and tuple values are entities which appear during program execution.
     200Tuples, on the other hand, are compile-time constructs;
     201they are lists of expressions, whose values may not be stored contiguously.~\cite[p.~30]{Till89}
     202\end{quote}
     203Fundamentally, a tuple value/variable is just a structure (contiguous areas) with an alternate (tuple) interface.
     204A 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;
     207t1 = t2;                        $\C{// tuple assignment}$
     208t1@.1@ = t2@.0@;        $\C{// position qualification}$
     209int x, y, z;
     210t1 = [ x, y, z ];       $\C{// interact with regular tuples}$
     211[ x, y, z ] = t1;
     212bar( t2 );                      $\C{// bar defined above}$
     213\end{cfa}
     214\VRef[Figure]{f:Nesting} shows The difference is nesting of structures and tuples.
     215The left \CC nested-structure is named so it is not flattened.
     216The middle C/\CC nested-structure is unnamed and flattened, causing an error because @i@ and @j@ are duplication names.
     217The right \CFA nested tuple cannot be named and is flattened.
     218C allows named nested-structures, but they have issues \see{\VRef{s:inlineSubstructure}}.
     219Note, 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}
     226struct S {
     227        struct @T@ { // not flattened
     228                int i, j;
     229        };
     230        int i, j;
     231};
     232\end{cfa}
     233&
     234\begin{cfa}
     235struct 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
     256The primary issues for tuples in the \CFA type system are polymorphism and conversions.
     257Specifically, does it make sense to have a generic (polymorphic) tuple type, as is possible for a structure?
     258\begin{cfa}
     259forall( T, S ) [ T, S ] GT; // polymorphic tuple type
     260GT( int, char ) @gt@;
     261GT( int, double ) @gt@;
     262@gt@ = [ 3, 'a' ];  // select correct gt
     263@gt@ = [ 3, 3.5 ];
     264\end{cfa}
     265and what is the cost model for C conversions across multiple values?
     266\begin{cfa}
     267gt = [ 'a', 3L ];  // select correct gt
     268\end{cfa}
     269
     270
     271\section{Tuple Implementation}
     272
     273As noted, tradition languages manipulate multiple values by in/out parameters and/or structures.
     274K-W C adopted the structure for tuple values or variables, and as needed, the fields are extracted by field access operations.
     275As 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.
     276For 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.
    194277
    195278In the second implementation of \CFA tuples by Rodolfo Gabriel Esteves~\cite{Esteves04}, a different strategy is taken to handle MVR functions.
     
    203286[x, y] = gives_two();
    204287\end{cfa}
    205 The K-W C implementation translates the program to:
     288The Till K-W C implementation translates the program to:
    206289\begin{cfa}
    207290struct _tuple2 { int _0; int _1; }
    208 struct _tuple2 gives_two();
     291struct _tuple2 gives_two() { ... struct _tuple2 ret = { r1, r2 }, return ret; }
    209292int x, y;
    210293struct _tuple2 _tmp = gives_two();
    211294x = _tmp._0; y = _tmp._1;
    212295\end{cfa}
    213 While the Rodolfo implementation translates it to:
     296while the Rodolfo implementation translates it to:
    214297\begin{cfa}
    215298void gives_two( int * r1, int * r2 ) { ... *r1 = ...; *r2 = ...; return; }
     
    218301\end{cfa}
    219302and 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.
     303This implementation looks more concise, and in the case of returning values having nontrivial types, \eg aggregates, this implementation saves unnecessary copying.
    221304For example,
    222305\begin{cfa}
     
    234317The downside is indirection within @gives_two@ to access values, unless values get hoisted into registers for some period of time, which is common.
    235318
    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.
     319Interestingly, 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.
     320The 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.
    238321This 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?}
    239323
    240324However, after experience gained building the \CFA runtime system, making tuple-types first-class seems to add little benefit.
     
    277361
    278362Finally, 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.
     363For 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.
    280364\VRef[Figure]{f:CVariadicMaxFunction} shows an $N$ argument @maxd@ function using the C untyped @va_list@ interface.
    281365In the example, the first argument is the number of following arguments, and the following arguments are assumed to be @double@;
    282366looping is used to traverse the argument pack from left to right.
    283 The @va_list@ interface is walking up (by address) the stack looking at the arguments pushed by the caller.
     367The @va_list@ interface is walking up the stack (by address) looking at the arguments pushed by the caller.
    284368(Magic knowledge is needed for arguments pushed using registers.)
    285369
     
    415499
    416500\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
     503As 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.
    419504\begin{cfa}
    420505struct S {
     
    448533
    449534As 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.}
    451536\begin{cquote}
    452537\begin{tabular}{@{}l@{\hspace{35pt}}l@{}}
     
    498583struct S s;  @s::T@.i;  @s::U@.k;
    499584\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.
    514586
    515587% https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html
     
    531603} s;
    532604\end{cfa}
    533 Note, the position of the substructure is normally unimportant.
     605Note, the position of the substructure is normally unimportant, unless there is some form of memory or @union@ overlay.
    534606Like the anonymous nested types, the aggregate field names are hoisted into @struct S@, so there is direct access, \eg @s.x@ and @s.i@.
    535607However, 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  
    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 function parameter, \eg prefix, postfix, and infix increment operators: @++?@, @?++@, and @?+?@.
     36The syntax for operator names uses the @'?'@ character to denote a parameter, \eg unary operators: @?++@, @++?@, binary operator @?+?@.
    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 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).
     43s1 = @?+?@( s1, s2 );   $\C{// direct call}\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 arguments.
     46If 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).
    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
     115Every variable has a type, but association between them can occur in different ways:
     116at the point where the variable comes into existence (declaration) and/or on each assignment to the variable.
     117\begin{cfa}
     118double x;                               $\C{// type only}$
     119float y = 3.1D;                 $\C{// type and initialization}$
     120auto z = y;                             $\C{// initialization only}$
     121z = "abc";                              $\C{// assignment}$
     122\end{cfa}
     123For type-and-initialization, the specified and initialization types may not agree.
     124Similarity, for assignment the current variable and expression types may not agree.
     125For type-only, the programmer specifies the initial type, which remains fixed for the variable's lifetime in statically typed languages.
     126In the other cases, the compiler may select the type by melding programmer and context information.
     127When the compiler participates in type selection, it is called \newterm{type inferencing}.
     128Note, 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
    115130One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}.
    116131Here, 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.
     
    132147\end{cfa}
    133148In 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 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@.
     149Note, 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@.
    135150Inferring type constraints, by analysing the body of @f@ is possible, and these constraints must be satisfied at each call site by the argument types;
    136151in this case, parametric polymorphism can allow separate compilation.
     
    170185Not determining or writing long generic types, \eg, given deeply nested generic types.
    171186\begin{cfa}
    172 typedef T1(int).T2(float).T3(char).T ST;  $\C{// \CFA nested type declaration}$
    173 ST x, y, x;
     187typedef T1(int).T2(float).T3(char).T @ST@;  $\C{// \CFA nested type declaration}$
     188@ST@ x, y, x;
    174189\end{cfa}
    175190This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages.
     
    193208\section{Type-Inferencing Issues}
    194209
    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.
     210Each 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
     212A 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
     214A restriction is the conundrum in type inferencing of when to \emph{brand} a type.
    216215That is, when is the type of the variable/function more important than the type of its initialization expression.
    217216For 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.
     217At some point, a variable's type needs to remain constant and the initializing expression needs to be modified or in error when it changes.
     218Often type-inferencing systems allow restricting (\newterm{branding}) a variable or function type, so the complier can report a mismatch with the constant initialization.
    221219\begin{cfa}
    222220void f( @int@ x, @int@ y ) {  // brand function prototype
     
    228226\end{cfa}
    229227In 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
     229A confusion is large blocks of code where all declarations are @auto@.
     230As a result, understanding and changing the code becomes almost impossible.
     231Types provide important clues as to the behaviour of the code, and correspondingly to correctly change or add new code.
     232In these cases, a programmer is forced to re-engineer types, which is fragile, or rely on a fancy IDE that can re-engineer types.
     233For example, given:
     234\begin{cfa}
     235auto x = @...@
     236\end{cfa}
     237and the need to write a routine to compute using @x@
     238\begin{cfa}
     239void rtn( @type of x@ parm );
     240rtn( x );
     241\end{cfa}
     242A programmer must re-engineer the type of @x@'s initialization expression, reconstructing the possibly long generic type-name.
     243In 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.
    233246Type inferencing defeats this goal because there is no left-hand type.
    234 Fundamentally, type inferencing tries to magic away types from the programmer.
     247Fundamentally, type inferencing tries to magic away variable types from the programmer.
    235248However, 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.
     249Types are as important as control-flow in writing a good program, and should not be masked, even if it requires the programmer to think!
     250A similar issue is garbage collection, where storage management is masked, resulting in poor program design and performance.
    238251The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming.
    239252Understanding space and time issues are an essential part of the programming craft.
Note: See TracChangeset for help on using the changeset viewer.