\chapter{Introduction} This thesis is exploratory work I did to understand, fix, and extend the \CFA type-system, specifically, the type resolver used to select polymorphic types among overloaded names. Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. \begin{quote} There are only two hard things in Computer Science: cache invalidation and \emph{naming things}. --- Phil Karlton \end{quote} Experience 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. In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process. Depending on the language, any ambiguous cases are resolved using some form of qualification and/or casting. Therefore, one of the key goals in \CFA is to push the boundary on overloading, and hence, overload resolution. As well, \CFA follows the current trend of replacing nominal inheritance with traits. Together, the resulting \CFA type-system has a number of unique features making it different from all other programming languages. \section{Types} \begin{quote} Some are born great, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II Scene 5, William Shakespeare \end{quote} All 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. A programming language and its compiler present ways to declare types that ultimately map into the ones provided by the underlying hardware. These language types are thrust upon programmers, with their syntactic and semantic rules and restrictions. These rules are used to transform a language expression to a hardware expression. Modern programming-languages allow user-defined types and generalize across multiple types using polymorphism. Type 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. Expressibility, 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. \section{Operator Overloading} Virtually all programming languages overload the arithmetic operators across the basic computational types using the number and type of parameters and returns. Like \CC, \CFA also allows these operators to be overloaded with user-defined types. The syntax for operator names uses the @'?'@ character to denote a parameter, \eg unary left and right operators: @?++@ and @++?@, and binary operators @?+?@ and @?<=?@. Here, a user-defined type is extended with an addition operation with the same syntax as builtin types. \begin{cfa} struct S { int i, j }; S @?+?@( S op1, S op2 ) { return (S){ op1.i + op2.i, op1.j + op2.j }; } S s1, s2; s1 = s1 @+@ s2; $\C[1.75in]{// infix call}$ s1 = @?+?@( s1, s2 ); $\C{// direct call}\CRT$ \end{cfa} The type system examines each call site and selects the best matching overloaded function based on the number and types of arguments. 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). Conversions are necessary because the hardware rarely supports mix-mode operations, so both operands must be the same type. Note, without implicit conversions, programmers must write an exponential number of functions covering all possible exact-match cases among all possible types. This approach does not match with programmer intuition and expectation, regardless of any \emph{safety} issues resulting from converted values. \section{Function Overloading} Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns. \begin{cfa} void f( void ); $\C[2in]{// (1): no parameter}$ void f( char ); $\C{// (2): overloaded on the number and parameter type}$ void f( int, int ); $\C{// (3): overloaded on the number and parameter type}$ f( 'A' ); $\C{// select (2)}\CRT$ \end{cfa} In this case, the name @f@ is overloaded depending on the number and parameter types. The type system examines each call size and selects the best match based on the number and types of the arguments. Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2). Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name. For example, in many programming languages with overloading, the following functions are ambiguous without using the return type. \begin{cfa} int f( int ); $\C[2in]{// (1); overloaded on return type and parameter}$ double f( int ); $\C{// (2); overloaded on return type and parameter}$ int i = f( 3 ); $\C{// select (1)}$ double d = f( 3 ); $\C{// select (2)}\CRT$ \end{cfa} Alternatively, if the type system looks at the return type, there is an exact match for each call, which matches with programmer intuition and expectation. This capability can be taken to the extreme, where there are no function parameters. \begin{cfa} int random( void ); $\C[2in]{// (1); overloaded on return type}$ double random( void ); $\C{// (2); overloaded on return type}$ int i = random(); $\C{// select (1)}$ double d = random(); $\C{// select (2)}\CRT$ \end{cfa} Again, there is an exact match for each call. If there is no exact match, a set of minimal conversions can be added to find a best match, as for operator overloading. \section{Variable Overloading} Unlike most programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes. (Shadow overloading is also possible for functions, if a language supports nested function declarations, \eg \CC named, nested, lambda functions.) \begin{cfa} void foo( double d ); int v; $\C[2in]{// (1)}$ double v; $\C{// (2) variable overloading}$ foo( v ); $\C{// select (2)}$ { int v; $\C{// (3) shadow overloading}$ double v; $\C{// (4) and variable overloading}$ foo( v ); $\C{// select (4)}\CRT$ } \end{cfa} It is interesting that shadow overloading is considered a normal programming-language feature with only slight software-engineering problems. However, variable overloading within a scope is considered extremely dangerous, without any evidence to corroborate this claim. Similarly, function overloading occurs silently within the global scope in \CC from @#include@ files all the time without problems. In \CFA, the type system simply treats overloaded variables as an overloaded function returning a value with no parameters. Hence, no significant effort is required to support this feature by leveraging the return type to disambiguate as variables have no parameters. \begin{cfa} int MAX = 2147483647; $\C[2in]{// (1); overloaded on return type}$ long int MAX = ...; $\C{// (2); overloaded on return type}$ double MAX = ...; $\C{// (3); overloaded on return type}$ int i = MAX; $\C{// select (1)}$ long int i = MAX; $\C{// select (2)}$ double d = MAX; $\C{// select (3)}\CRT$ \end{cfa} Hence, the name @MAX@ can replace all the C type-specific names, \eg @INT_MAX@, @LONG_MAX@, @DBL_MAX@, \etc. The result is a significant reduction in names to access typed constants. % Paraphrasing Shakespeare, ``The \emph{name} is the thing.''. \section{Constant Overloading} \CFA is unique in providing restricted constant overloading for the values @0@ and @1@, which have special status in C, \eg the value @0@ is both an integer and a pointer literal, so its meaning depends on context. In addition, several operations are defined in terms of values @0@ and @1@, \eg: \begin{cfa} if ( x ) ++x $\C{// if ( x != 0 ) x += 1;}$ \end{cfa} Every @if@ and iteration statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result. These two constants are given types @zero_t@ and @one_t@ in \CFA, which allows overloading various operations for new types that seamlessly connect to all special @0@ and @1@ contexts. The types @zero_t@ and @one_t@ have special builtin implicit conversions to the various integral types, and a conversion to pointer types for @0@, which allows standard C code involving @0@ and @1@ to work. \begin{cfa} struct S { int i, j; }; void ?{}( S & s, zero_t ) { s.[i,j] = 0; } $\C{// constructors}$ void ?{}( S & s, one_t ) { s.[i,j] = 1; } S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// assignment}$ S ?=?( S & dst, one_t ) { dst.[i,j] = 1; return dst; } S ?+=?( S & s, one_t ) { s.[i,j] += 1; return s; } $\C{// increment/decrement}$ S ?-=?( S & s, one_t ) { s.[i,j] -= 1; return s; } int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// comparison}$ S s = @0@; s = @0@; s = @1@; if ( @s@ ) @++s@; $\C{// unary ++/-\,- come from +=/-=}$ \end{cfa} Hence, type @S@ is first-class with respect to the basic types, working with all existing implicit C mechanisms. \section{Type Inferencing} Every variable has a type, but association between them can occur in different ways: at the point where the variable comes into existence (declaration) and/or on each assignment to the variable. \begin{cfa} double x; $\C{// type only}$ float y = 3.1D; $\C{// type and initialization}$ auto z = y; $\C{// initialization only}$ z = "abc"; $\C{// assignment}$ \end{cfa} For type-only, the programmer specifies the initial type, which remains fixed for the variable's lifetime in statically typed languages. For type-and-initialization, the specified and initialization types may not agree. For initialization-only, the compiler may select the type by melding programmer and context information. When the compiler participates in type selection, it is called \newterm{type inferencing}. 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. Finally, for assignment, the current variable and expression types may not agree. One of the first and powerful type-inferencing system is Hindley--Milner~\cite{Damas82}. 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. \begin{cfa} auto f() { x = 1; y = 3.5; $\C{// set types from constants}$ x = // expression involving x, y and other local initialized variables y = // expression involving x, y and other local initialized variables return x, y; } auto w = f(); $\C{// typing flows outwards}$ void f( auto x, auto y ) { x = // expression involving x, y and other local initialized variables y = // expression involving x, y and other local initialized variables } s = 1; t = 3.5; $\C{// set types from constants}$ f( s, t ); $\C{// typing flows inwards}$ \end{cfa} 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. 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 @f@. 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; in this case, parametric polymorphism can allow separate compilation. In languages with type inferencing, there is often limited overloading to reduce the search space, which introduces the naming problem. Note, return-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. In simpler type-inferencing systems, such as C/\CC/\CFA, there are more specific usages. \begin{cquote} \setlength{\tabcolsep}{10pt} \begin{tabular}{@{}lll@{}} \multicolumn{1}{c}{\textbf{gcc / \CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ \begin{cfa} #define expr 3.0 * i typeof(expr) x = expr; int y; typeof(y) z = y; \end{cfa} & \begin{cfa} auto x = 3.0 * 4; int y; auto z = y; \end{cfa} & \begin{cfa} // use type of initialization expression // use type of initialization expression \end{cfa} \end{tabular} \end{cquote} The two important capabilities are: \begin{itemize}[topsep=0pt] \item Not determining or writing long generic types, \eg, given deeply nested generic types. \begin{cfa} typedef T1(int).T2(float).T3(char).T @ST@; $\C{// \CFA nested type declaration}$ @ST@ x, y, x; \end{cfa} This issue is exaggerated with \CC templates, where type names are 100s of characters long, resulting in unreadable error messages. \item Ensuring the type of secondary variables, always match a primary variable. \begin{cfa} int x; $\C{// primary variable}$ typeof(x) y, z, w; $\C{// secondary variables match x's type}$ \end{cfa} If the type of @x@ changes, the type of the secondary variables correspondingly updates. \end{itemize} Note, the use of @typeof@ is more restrictive, and possibly safer, than general type-inferencing. \begin{cfa} int x; type(x) y = ... // complex expression type(x) z = ... // complex expression \end{cfa} Here, the types of @y@ and @z@ are fixed (branded), whereas with type inferencing, the types of @y@ and @z@ are potentially unknown. \section{Type-Inferencing Issues} 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. 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. A restriction is the conundrum in type inferencing of when to \emph{brand} a type. That is, when is the type of the variable/function more important than the type of its initialization expression. For example, if a change is made in an initialization expression, it can cause cascading type changes and/or errors. 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. Often type-inferencing systems allow restricting (\newterm{branding}) a variable or function type, so the complier can report a mismatch with the constant initialization. \begin{cfa} void f( @int@ x, @int@ y ) { // brand function prototype x = // expression involving x, y and other local initialized variables y = // expression involving x, y and other local initialized variables } s = 1; t = 3.5; f( s, @t@ ); // type mismatch \end{cfa} In Haskell, it is common for programmers to brand (type) function parameters. A confusion is large blocks of code where all declarations are @auto@, as is now common in \CC. As a result, understanding and changing the code becomes almost impossible. Types provide important clues as to the behaviour of the code, and correspondingly to correctly change or add new code. 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 for them. For example, given: \begin{cfa} auto x = @...@ \end{cfa} and the need to write a routine to compute using @x@ \begin{cfa} void rtn( @type of x@ parm ); rtn( x ); \end{cfa} A programmer must re-engineer the type of @x@'s initialization expression, reconstructing the possibly long generic type-name. In this situation, having the type name or its short alias is essential. The \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. Type inferencing defeats this goal because there is no left-hand type. Fundamentally, type inferencing tries to magic away variable types from the programmer. However, this results in lazy programming with the potential for poor performance and safety concerns. 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! A similar issue is garbage collection, where storage management is magicked away, often resulting in poor program design and performance.\footnote{ There are full-time Java consultants, who are hired to find memory-management problems in large Java programs.} The entire area of Computer-Science data-structures is obsessed with time and space, and that obsession should continue into regular programming. Understanding space and time issues are an essential part of the programming craft. Given @typedef@ and @typeof@ in \CFA, and the strong need to use the left-hand type in resolution, implicit type-inferencing is unsupported. Should a significant need arise, this feature can be revisited. \section{Polymorphism} \CFA provides polymorphic functions and types, where the polymorphic function can be the constraints types using assertions based on traits. \subsection{\texorpdfstring{\protect\lstinline{forall} functions}{forall functions}} \label{sec:poly-fns} The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a @forall@ clause (giving the language its name). \begin{cfa} @forall( T )@ T identity( T val ) { return val; } int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$ \end{cfa} This @identity@ function can be applied to any complete \newterm{object type} (or @otype@). The type variable @T@ is transformed into a set of additional implicit parameters encoding sufficient information about @T@ to create and return a variable of that type. The \CFA implementation passes the size and alignment of the type represented by an @otype@ parameter, as well as an assignment operator, constructor, copy constructor, and destructor. If this extra information is not needed, for instance, for a pointer, the type parameter can be declared as a \newterm{data type} (or @dtype@). In \CFA, the polymorphic runtime cost is spread over each polymorphic call, because more arguments are passed to polymorphic functions; the experiments in Section~\ref{sec:eval} show this overhead is similar to \CC virtual function calls. A design advantage is that, unlike \CC template functions, \CFA polymorphic functions are compatible with C \emph{separate compilation}, preventing compilation and code bloat. Since bare polymorphic types provide a restricted set of available operations, \CFA provides a \newterm{type assertion}~\cite[pp.~37-44]{Alphard} mechanism to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type variable. For example, the function @twice@ can be defined using the \CFA syntax for operator overloading. \begin{cfa} forall( T @| { T ?+?(T, T); }@ ) T twice( T x ) { return x @+@ x; } $\C{// ? denotes operands}$ int val = twice( twice( 3.7 ) ); $\C{// val == 14}$ \end{cfa} This works for any type @T@ with a matching addition operator. The polymorphism is achieved by creating a wrapper function for calling @+@ with the @T@ bound to @double@ and then passing this function to the first call of @twice@. There is now the option of using the same @twice@ and converting the result into @int@ on assignment or creating another @twice@ with the type parameter @T@ bound to @int@ because \CFA uses the return type~\cite{Cormack81,Baker82,Ada} in its type analysis. The first approach has a late conversion from @double@ to @int@ on the final assignment, whereas the second has an early conversion to @int@. \CFA minimizes the number of conversions and their potential to lose information; hence, it selects the first approach, which corresponds with C programmer intuition. Crucial to the design of a new programming language are the libraries to access thousands of external software features. Like \CC, \CFA inherits a massive compatible library base, where other programming languages must rewrite or provide fragile interlanguage communication with C. A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted float array. \begin{cfa} void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (* compar)( const void *, const void * )); int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 : *(double *)t2 < *(double *)t1 ? 1 : 0; } double key = 5.0, vals[10] = { /* 10 sorted float values */ }; double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); $\C{// search sorted array}$ \end{cfa} This can be augmented simply with generalized, type-safe, \CFA-overloaded wrappers. \begin{cfa} forall( T | { int ?@ y; } $\C{// locally override behavior}$ qsort( vals, 10 ); $\C{// descending sort}$ } \end{cfa} The local version of @??@ overriding the built-in @?value; } % \end{cfa} % In the example above, @(list_iterator, int)@ satisfies @pointer_like@ by the user-defined dereference function, and @(list_iterator, list)@ also satisfies @pointer_like@ by the built-in dereference operator for pointers. Given a declaration @list_iterator it@, @*it@ can be either an @int@ or a @list@, with the meaning disambiguated by context (\eg @int x = *it;@ interprets @*it@ as an @int@, while @(*it).value = 42;@ interprets @*it@ as a @list@). % While a nominal-inheritance system with associated types could model one of those two relationships by making @El@ an associated type of @Ptr@ in the @pointer_like@ implementation, few such systems could model both relationships simultaneously. \subsection{Generic Types} A significant shortcoming of standard C is the lack of reusable type-safe abstractions for generic data structures and algorithms. Broadly speaking, there are three approaches to implement abstract data structures in C. One approach is to write bespoke data structures for each context in which they are needed. While this approach is flexible and supports integration with the C type checker and tooling, it is also tedious and error prone, especially for more complex data structures. A second approach is to use @void *@-based polymorphism, \eg the C standard library functions @bsearch@ and @qsort@, which allow for the reuse of code with common functionality. However, basing all polymorphism on @void *@ eliminates the type checker's ability to ensure that argument types are properly matched, often requiring a number of extra function parameters, pointer indirection, and dynamic allocation that is otherwise not needed. A third approach to generic code is to use preprocessor macros, which does allow the generated code to be both generic and type checked, but errors may be difficult to interpret. Furthermore, writing and using preprocessor macros is unnatural and inflexible. \CC, Java, and other languages use \newterm{generic types} to produce type-safe abstract data types. \CFA generic types integrate efficiently and naturally with the existing polymorphic functions, while retaining backward compatibility with C and providing separate compilation. However, for known concrete parameters, the generic-type definition can be inlined, like \CC templates. A generic type can be declared by placing a @forall@ specifier on a @struct@ or @union@ declaration and instantiated using a parenthesized list of types after the type name. \begin{cquote} \lstDeleteShortInline@% \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}} \begin{cfa} @forall( R, S )@ struct pair { R first; S second; }; @forall( T )@ // dynamic T value( pair(const char *, T) p ) { return p.second; } @forall( dtype F, T )@ // dtype-static (concrete) T value( pair(F *, T * ) p) { return *p.second; } \end{cfa} & \begin{cfa} pair(const char *, int) p = {"magic", 42}; // concrete int i = value( p ); pair(void *, int *) q = { 0, &p.second }; // concrete i = value( q ); double d = 1.0; pair(double *, double *) r = { &d, &d }; // concrete d = value( r ); \end{cfa} \end{tabular} \lstMakeShortInline@% \end{cquote} \CFA classifies generic types as either \newterm{concrete} or \newterm{dynamic}. Concrete types have a fixed memory layout regardless of type parameters, whereas dynamic types vary in memory layout depending on their type parameters. A \newterm{dtype-static} type has polymorphic parameters but is still concrete. Polymorphic pointers are an example of dtype-static types; given some type variable @T@, @T@ is a polymorphic type, as is @T *@, but @T *@ has a fixed size and can, therefore, be represented by @void *@ in code generation. \CFA generic types also allow checked argument constraints. For example, the following declaration of a sorted set type ensures the set key supports equality and relational comparison. \begin{cfa} forall( Key | { _Bool ?==?(Key, Key); _Bool ? To: Peter Buhr , Michael Leslie Brooks , Fangren Yu , Jiada Liang Subject: Re: Haskell Date: Fri, 30 Aug 2024 16:09:06 +0000 Do you mean: one = 1 And then write a bunch of code that assumes it is an Int or Integer (which are roughly int and Int in Cforall) and then replace it with: one = 1.0 And have that crash? That is actually enough, for some reason Haskell is happy to narrow the type of the first literal (Num a => a) down to Integer but will not do the same for (Fractional a => a) and Rational (which is roughly Integer for real numbers). Possibly a compatibility thing since before Haskell had polymorphic literals. Now, writing even the first version will fire a -Wmissing-signatures warning, because it does appear to be understood that just from a documentation perspective, people want to know what types are being used. Now, if you have the original case and start updating the signatures (adding one :: Fractional a => a), you can eventually get into issues, for example: import Data.Array (Array, Ix, (!)) atOne :: (Ix a, Frational a) => Array a b -> b - - In CFA: forall(a | Ix(a) | Frational(a), b) b atOne(Array(a, b) const & array) atOne = (! one) Which compiles and is fine except for the slightly awkward fact that I don't know of any types that are both Ix and Fractional types. So you might never be able to find a way to actually use that function. If that is good enough you can reduce that to three lines and use it. Something that just occurred to me, after I did the above examples, is: Are there any classic examples in literature I could adapt to Haskell? Andrew PS, I think it is too obvious of a significant change to work as a good example but I did mock up the structure of what I am thinking you are thinking about with a function. If this helps here it is. doubleInt :: Int -> Int doubleInt x = x * 2 doubleStr :: String -> String doubleStr x = x ++ x -- Missing Signature action = doubleInt - replace with doubleStr main :: IO () main = print $ action 4 \end{comment}