Changeset cdbb909


Ignore:
Timestamp:
Sep 2, 2024, 2:54:53 PM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
db19e1d
Parents:
d6b7d1d
Message:

proofread introduction chapter

File:
1 edited

Legend:

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

    rd6b7d1d rcdbb909  
    66Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
    77\begin{quote}
    8 There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
     8There are only two hard things in Computer Science: cache invalidation and \emph{naming things}. --- Phil Karlton
    99\end{quote}
    1010Experience from \CC and \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded (variables and) functions.
     
    1515
    1616
    17 \subsection{Operator Overloading}
    18 
    19 Virtually all programming languages overload the arithmetic operators across the basic types using the number and type of parameters and returns.
     17\section{Types}
     18
     19\begin{quote}
     20Some are born great, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II Scene 5, William Shakespeare
     21\end{quote}
     22
     23All computers have multiple types because computer architects optimize the hardware around a few basic types with well defined (mathematical) operations: boolean, integral, floating-point, and occasionally strings.
     24A programming language and its compiler present ways to declare types that ultimately map into those provided by the underlying hardware.
     25These language types are thrust upon programmers, with their syntactic and semantic rules, and resulting restrictions.
     26A language type-system defines these rules and uses them to understand how an expression is to be evaluated by the hardware.
     27Modern programming-languages allow user-defined types and generalize across multiple types using polymorphism.
     28Type systems can be static, where each variable has a fixed type during execution and an expression's type is determined once at compile time, or dynamic, where each variable can change type during execution and so an expression's type is reconstructed on each evaluation.
     29Expressibility, generalization, and safety are all bound up in a language's type system, and hence, directly affect the capability, build time, and correctness of program development.
     30
     31
     32\section{Operator Overloading}
     33
     34Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns.
    2035Like \CC, \CFA also allows these operators to be overloaded with user-defined types.
    21 The syntax for operator names uses the @'?'@ character to denote a parameter, \eg prefix and infix increment operators: @?++@, @++?@, and @?+?@.
     36The syntax for operator names uses the @'?'@ character to denote a function parameter, \eg prefix, postfix, and infix increment operators: @++?@, @?++@, and @?+?@.
     37Here, a user-defined type is extended with an addition operation with the same syntax as builtin types.
    2238\begin{cfa}
    2339struct S { int i, j };
     
    2541S s1, s2;
    2642s1 = s1 @+@ s2;                 $\C[1.75in]{// infix call}$
    27 s1 = @?+?@( s1, s2 );   $\C{// direct call}\CRT$
     43s1 = @?+?@( s1, s2 );   $\C{// direct call using operator name}\CRT$
    2844\end{cfa}
    2945The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments.
    30 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).
    31 
    32 
    33 \subsection{Function Overloading}
     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).
     47Conversions are necessary because the hardware rarely supports mix-mode operations, so both operands must be the same type.
     48Note, without implicit conversions, programmers must write an exponential number of functions covering all possible exact-match cases among all possible types.
     49This approach does not match with programmer intuition and expectation, regardless of any \emph{safety} issues resulting from converted values.
     50
     51
     52\section{Function Overloading}
    3453
    3554Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns.
    3655\begin{cfa}
    37 void f( void );                 $\C[1.75in]{// (1): no parameter}$
     56void f( void );                 $\C[2in]{// (1): no parameter}$
    3857void f( char );                 $\C{// (2): overloaded on the number and parameter type}$
    3958void f( int, int );             $\C{// (3): overloaded on the number and parameter type}$
     
    4564
    4665Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
    47 \begin{cfa}
    48 int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
    49 double f( void );               $\C{// (5); overloaded on return type}$
    50 int i = f();                    $\C{// select (4)}$
    51 double d = f();                 $\C{// select (5)}\CRT$
    52 \end{cfa}
    53 
    54 
    55 \subsection{Variable Overloading}
    56 Unlike almost all programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes.
     66For example, in many programming languages with overloading, the following functions are ambiguous without using the return type.
     67\begin{cfa}
     68int f( int );                   $\C[2in]{// (1); overloaded on return type and parameter}$
     69double f( int );                $\C{// (2); overloaded on return type and parameter}$
     70int i = f( 3 );                 $\C{// select (1)}$
     71double d = f( 3 );              $\C{// select (2)}\CRT$
     72\end{cfa}
     73However, if the type system looks at the return type, there is an exact match for each call, which matches with programmer intuition and expectation.
     74This capability can be taken to the extreme, where there are no function parameters.
     75\begin{cfa}
     76int random( void );             $\C[2in]{// (1); overloaded on return type}$
     77double random( void );  $\C{// (2); overloaded on return type}$
     78int i = random();               $\C{// select (1)}$
     79double d = random();    $\C{// select (2)}\CRT$
     80\end{cfa}
     81Again, there is an exact match for each call.
     82If there is no exact match, a set of minimal conversions can be added to find a best match, as for operator overloading.
     83
     84
     85\section{Variable Overloading}
     86
     87Unlike most programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes.
     88(Shadow overloading is also possible for functions, if a language supports nested function declarations, \eg \CC named, nested, lambda functions.)
    5789\begin{cfa}
    5890void foo( double d );
    59 int v;                              $\C[1.75in]{// (1)}$
     91int v;                              $\C[2in]{// (1)}$
    6092double v;                               $\C{// (2) variable overloading}$
    6193foo( v );                               $\C{// select (2)}$
     
    6698}
    6799\end{cfa}
    68 The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
     100It is interesting that shadow overloading is considered a normal programming-language feature with only slight software-engineering problems, but variable overloading within a scope is often considered extremely dangerous.
     101
     102In \CFA, the type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
    69103Hence, no significant effort is required to support this feature.
     104Leveraging the return type to disambiguate is essential because variables have no parameters.
     105\begin{cfa}
     106int MAX = 2147483647;   $\C[2in]{// (1); overloaded on return type}$
     107double MAX = ...;               $\C{// (2); overloaded on return type}$
     108int i = MAX;                    $\C{// select (1)}$
     109double d = MAX;                 $\C{// select (2)}\CRT$
     110\end{cfa}
     111
     112
     113\section{Type Inferencing}
     114
     115One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}.
     116Here, 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.
     117\begin{cfa}
     118auto f() {
     119        x = 1;   y = 3.5;       $\C{// set types from constants}$
     120        x = // expression involving x, y and other local initialized variables
     121        y = // expression involving x, y and other local initialized variables
     122        return x, y;
     123}
     124auto w = f();                   $\C{// typing flows outwards}$
     125
     126void f( auto x, auto y ) {
     127        x = // expression involving x, y and other local initialized variables
     128        y = // expression involving x, y and other local initialized variables
     129}
     130s = 1;   t = 3.5;               $\C{// set types from constants}$
     131f( s, t );                              $\C{// typing flows inwards}$
     132\end{cfa}
     133In 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.
     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@.
     135Inferring type constraints, by analysing the body of @f@ is possible, and these constraints must be satisfied at each call site by the argument types;
     136in this case, parametric polymorphism can allow separate compilation.
     137In languages with type inferencing, there is often limited overloading to reduce the search space, which introduces the naming problem.
     138Return-type inferencing goes in the opposite direction to Hindley--Milner: knowing the type of the result and flowing back through an expression to help select the best possible overloads, and possibly converting the constants for a best match.
     139
     140In simpler type inferencing systems, such as C/\CC/\CFA, there are more specific usages.
     141\begin{cquote}
     142\setlength{\tabcolsep}{10pt}
     143\begin{tabular}{@{}lll@{}}
     144\multicolumn{1}{c}{\textbf{gcc / \CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\
     145\begin{cfa}
     146#define expr 3.0 * i
     147typeof(expr) x = expr;
     148int y;
     149typeof(y) z = y;
     150\end{cfa}
     151&
     152\begin{cfa}
     153
     154auto x = 3.0 * 4;
     155int y;
     156auto z = y;
     157\end{cfa}
     158&
     159\begin{cfa}
     160
     161// use type of initialization expression
     162
     163// use type of initialization expression
     164\end{cfa}
     165\end{tabular}
     166\end{cquote}
     167The two important capabilities are:
     168\begin{itemize}[topsep=0pt]
     169\item
     170Not determining or writing long generic types, \eg, given deeply nested generic types.
     171\begin{cfa}
     172typedef T1(int).T2(float).T3(char).T ST;  $\C{// \CFA nested type declaration}$
     173ST x, y, x;
     174\end{cfa}
     175This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages.
     176\item
     177Ensuring the type of secondary variables, always matches a primary variable.
     178\begin{cfa}
     179int x; $\C{// primary variable}$
     180typeof(x) y, z, w; $\C{// secondary variables match x's type}$
     181\end{cfa}
     182If the type of @x@ changes, the types of the secondary variables correspondingly update.
     183\end{itemize}
     184Note, the use of @typeof@ is more restrictive, and possibly safer, than general type-inferencing.
     185\begin{cfa}
     186int x;
     187type(x) y = ... // complex expression
     188type(x) z = ... // complex expression
     189\end{cfa}
     190Here, the types of @y@ and @z@ are fixed (branded), whereas with type inferencing, the types of @y@ and @z@ are potentially unknown.
     191
     192
     193\section{Type-Inferencing Issues}
     194
     195Each kind of type-inferencing systems 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.
     216That is, when is the type of the variable/function more important than the type of its initialization expression.
     217For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors.
     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.
     221\begin{cfa}
     222void f( @int@ x, @int@ y ) {  // brand function prototype
     223        x = // expression involving x, y and other local initialized variables
     224        y = // expression involving x, y and other local initialized variables
     225}
     226s = 1;   t = 3.5;
     227f( s, @t@ ); // type mismatch
     228\end{cfa}
     229In 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.
     233Type inferencing defeats this goal because there is no left-hand type.
     234Fundamentally, type inferencing tries to magic away types from the programmer.
     235However, this results in lazy programming with the potential for poor performance and safety concerns.
     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.
     238The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming.
     239Understanding space and time issues are an essential part of the programming craft.
     240Given @typedef@ and @typeof@ in \CFA, and the strong need to use the left-hand type in resolution, implicit type-inferencing is unsupported.
     241Should a significant need arise, this feature can be revisited.
     242
     243
     244\section{Polymorphism}
     245
     246
     247
     248\section{Contributions}
Note: See TracChangeset for help on using the changeset viewer.