\chapter{Introduction}

This thesis is exploratory work I did to understand, fix, and extend the \CFA type-system, specifically, the \newterm{type-resolver} used to satisfy call-site assertions among overloaded variable and function names to allow polymorphic function calls.
Assertions are the operations a function uses within its body to perform its computation.
For example, a polymorphic function summing an array needs a size, zero, assignment, and plus for the array element-type, and a subscript operation for the array type.
\begin{cfa}
T sum( T a[$\,$], size_t size ) {
	@T@ total = { @0@ };  $\C[1.75in]{// size, 0 for type T}$
	for ( i; size )
		total @+=@ a@[@i@]@; $\C{// + and subscript for T}\CRT$
	return total;
}
\end{cfa}
In certain cases, if the resolver fails to find an exact assertion match, it attempts to find a \emph{best} match using reasonable type conversions.
Hence, \CFA follows the current trend of replacing nominal inheritance with traits composed of assertions for type matching.
The over-arching goal in \CFA is to push the boundary on localized assertion matching, with advanced overloading resolution and type conversions that match programmer expectations in the C programming language.
Together, the resulting type-system has a number of unique features making it different from other programming languages with expressive, static, type-systems.


\section{Types}

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/semantic rules and restrictions.
These rules are then used to transform a language expression to a unique 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 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{Overloading}
\label{s:Overloading}

\vspace*{-5pt}
\begin{quote}
There are only two hard things in Computer Science: cache invalidation and \emph{naming things}. --- Phil Karlton
\end{quote}
\vspace*{-5pt}
Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
Experience from \CC and \CFA developers shows the type system can implicitly and correctly disambiguate 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 is unaware of name clashes, as they are silently resolved, simplifying the development process.

Disambiguating among overloads is implemented by examining each call site and selecting the best matching overloaded function based on criteria like the types and number of arguments and the return context.
Since the hardware does not support mixed-mode operands, such as @2 + 3.5@, the type system must disallow it or (safely) convert the operands to a common type.
Like overloading, the majority of mixed-mode conversions are silently resolved, simplifying the development process.
This approach matches with programmer intuition and expectation, regardless of any \emph{safety} issues resulting from converted values.
Depending on the language, mix-mode conversions can be explicitly controlled using a cast.

\newterm{Namespace pollution} refers to loading the global or other namespaces with many names, resulting in paranoia that the compiler could make wrong choices for overloaded names causing failure.
This fear leads to coding styles where names are partitioned by language mechanisms and qualification is used to make names unique.
This approach defeats the purpose of overloading and places an additional coding burden on both the developer and user.
As well, many namespace systems provide a mechanism to open their scope returning to normal overloading, \ie no qualification.
While namespace mechanisms are very important and provide a number of crucial program-development features, protection from overloading is overstated.
Similarly, lexical nesting is another place where duplicate naming issues arise.
For example, in object-oriented programming, class member names \newterm{shadow} names within members.
Some programmers qualify all member names with @class::@ or @this->@ to make them unique from names defined in members.
Even nested lexical blocks result in shadowing, \eg multiple nested loop-indices called @i@, silently changing the meaning of @i@ at lower scope levels.
Again, coding styles exist requiring all variables in nested block to be unique to prevent name shadowing problems.
Depending on the language, these possible ambiguities can be reported (as warnings or errors) and resolved explicitly using some form of qualification and/or cast.
For example, if variables can be overloaded, shadowed variables of different type can produce ambiguities, indicating potential problems in lower scopes.

Formally, overloading is defined by Strachey as one kind of \newterm{ad hoc polymorphism}:
\vspace*{-5pt}
\begin{quote}
In ad hoc polymorphism there is no single systematic way of determining the type of the result from the type of the arguments.
There may be several rules of limited extent which reduce the number of cases, but these are themselves ad hoc both in scope and content.
All the ordinary arithmetic operators and functions come into this category.
It seems, moreover, that the automatic insertion of transfer functions by the compiling system is limited to this.~\cite[p.~37]{Strachey00}
\end{quote}
\vspace*{-5pt}
where a \newterm{transfer function} is an implicit conversion to help find a matching overload:
\vspace*{-5pt}
\begin{quote}
The problem of dealing with polymorphic operators is complicated by the fact that the range of types sometimes overlap.
Thus for example 3 may be an integer or a real and it may be necessary to change it from one type to the other.
The functions which perform this operation are known as transfer functions and may either be used explicitly by the programmer, or, in some systems, inserted automatically by the compiling system.~\cite[p.~35]{Strachey00}
\end{quote}
\vspace*{-5pt}
The differentiating characteristic between parametric polymorphism and overloading is often stated as: polymorphic functions use one algorithm to operate on arguments of many different types, whereas overloaded functions use a different algorithm for each type of argument.
A similar differentiation is applicable for overloading and default parameters.
\begin{cquote}
\setlength{\tabcolsep}{10pt}
\begin{tabular}{@{}lll@{}}
\multicolumn{1}{c}{\textbf{different implementations}}	& \multicolumn{1}{c}{\textbf{same implementation}} \\
\begin{cfa}
void foo( int );
void foo( int, int );
\end{cfa}
&
\begin{cfa}
void foo( int, int = 5 ); // default value

\end{cfa}
\end{tabular}
\end{cquote}
However, this distinguishing characteristic is vague.
For example, should the operation @abs@ be overloaded or polymorphic or both?
\begin{cquote}
\setlength{\tabcolsep}{10pt}
\begin{tabular}{@{}lll@{}}
\multicolumn{1}{c}{\textbf{overloading}}	& \multicolumn{1}{c}{\textbf{polymorphic}} \\
\begin{cfa}
int abs( int );
double abs( double );
\end{cfa}
&
\begin{cfa}
forall( T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
T abs( T );
\end{cfa}
\end{tabular}
\end{cquote}
Here, there are performance advantages for having specializations and code-reuse advantages for the generalization.

The Strachey definitions raise several questions.
\begin{enumerate}[leftmargin=*]
\item
Is overloading polymorphism?

\noindent
In type theory, polymorphism allows an overloaded type name to represent multiple different types.
For example, generic types overload the type name for a container type.
\begin{cfa}
@List@<int> li;    @List@<double> ld;    @List@<struct S> ls;
\end{cfa}
For subtyping, a derived type masquerades as a base type, where the base and derived names cannot be overloaded.
Instead, the mechanism relies on structural typing among the types.
In both cases, the polymorphic mechanisms apply in the type domain and the names are in the type namespace.
In the following C example:
\begin{cfa}
struct S { int S; };
struct S s;
void S( struct S S ) { S.S = 1; };
enum E { E };
enum E e = E;
\end{cfa}
the overloaded names @S@ and @E@ are separated into the type and object domain, and C uses the type kinds @struct@ and @enum@ to disambiguate the names.
In general, types are not overloaded because inferencing them is difficult to imagine in a statically typed programming language.
\begin{cquote}
\setlength{\tabcolsep}{26pt}
\begin{tabular}{@{}ll@{}}
\begin{cfa}
struct S { int i; }; // 1
struct S { int i, j }; // 2
union S { int i; double d; }; // 3
typedef int S; // 4
\end{cfa}
&
\begin{cfa}
S s;
if ( ... ) s = (S){ 3 }; // 1
else s = 3; // 4

\end{cfa}
\end{tabular}
\end{cquote}
On the other hand, in ad-hoc overloading of variables and/or functions, the names are in the object domain and each overloaded object name has an anonymous associated type.
\begin{cfa}
@double@ foo( @int@ );    @char@ foo( @void@ );    @int@ foo( @double, double@ );
@double@ foo;    @char@ foo;    @int@ foo;
\end{cfa}
Here, the associated type cannot be extracted using @typeof@/\lstinline[language={[11]C++}]{decltype} for typing purposes, as @typeof( foo )@ is always ambiguous.
To disambiguate the type of @foo@ requires additional information from the call-site or a cast.
\begin{cfa}
double d = foo( 3 );    char c = foo();    int i = foo( 3.5, 4.1 );  $\C{// exact match}$
d = foo;    c = foo;    i = foo;  $\C{// exact match}$
i = ((double (*)( int ))foo)( 3.5 ); $\C{// no exact match, explicit cast => conversions}$
\end{cfa}
Hence, depending on your perspective, overloading may not be polymorphism, as no single overloaded entity represents multiple types.

\item
Does ad-hoc polymorphism have a single systematic way of determining the type of the result from the type of the arguments?

\noindent
For exact type matches in overloading, there is a systematic way of matching arguments to parameters, and a function denotes its return type rather than using type inferencing.
This matching is just as robust as other polymorphic analysis.
The ad-hoc aspect might be the implicit transfer functions (conversions) applied to arguments to create an exact parameter type-match, as there may be multiple conversions leading to different exact matches.
Note, conversion issues apply to both non-overloaded and overloaded functions.
Here, the selection of the conversion functions can be considered the \emph{opinion} of the language (type system), even if the technique used is based on sound rules, like maximizing conversion accuracy (non-lossy).
The difference in opinion results when the language conversion rules differ from a programmer's expectations.
However, without implicit conversions, programmers may have to write an exponential number of functions covering all possible exact-match cases among all reasonable types.
\CFA's \emph{opinion} on conversions must match C's and then rely on programmers to understand the effects.
That is, let the compiler do the heavy-lifting of selecting a \emph{best} set of conversions that maximize safety.
Hence, removing implicit conversions from \CFA is not an option (as is true in most other programming languages), so it must do the best possible job to get it right.

\item
Why are there two forms of \emph{overloading} (general and parametric) in different programming languages?

\noindent
\newterm{General overloading} occurs when the type-system \emph{knows} a function's parameters and return types (or a variable's type for variable overloading).
In functional programming-languages, there is always a return type.
If a return type is specified, the compiler does not have to inference the function body.
For example, the compiler has complete knowledge about builtin types and their overloaded arithmetic operators.
In this context, there is a fixed set of overloads for a given name that are completely specified.
Overload resolution then involves finding an exact match between a call and the overload prototypes based on argument type(s) and possibly return context.
If an \emph{exact} match is not found, the call is either ill formed (ambiguous) or further attempts are made to find a \emph{best} match using transfer functions (conversions).
As a consequence, no additional runtime information is needed per call, \ie the call is a direct transfer (branch) with possibly converted arguments.

\noindent
\newterm{Parametric overloading} occurs when the type-system does not know a function's parameter and return types.
Types are inferred from constant types and/or constraint information that includes typing.
Parametric overloading starts with a universal/generic type used to define parameters and returns.
\begin{cfa}
forall( T ) T identity( T t ) { return t; }
int i = identify( 3 );
double d;
double e = identity( d );
\end{cfa}
Here, the type system is substituting the argument type for @T@, and ensuring the return type also matches.
This mechanism can be extended to overloaded parametric functions with multiple type parameters.
\begin{cfa}
forall( T ) [T, T] identity( T t1, T t2 ) { return [t1, t2 ]; } // return tuple
int j;
[i, j] = identity( 3, 4 );
forall( T, U ) [T, U] identity( T t1, U t2 ) { return [t1, t2 ]; }
[i, d] = identity( i, d );
\end{cfa}
Here, the type system is using techniques from general overloading, number and argument types, to disambiguate the overloaded parametric functions.
However, this exercise is moot because the types are so universal they have no practical operations within the function, modulo any general operations carried by every type, like copy or print.
This kind of polymorphism is restricted to moving type instances as abstract entities;
if type erasure is used, there is no way to recover the original type to perform useful actions on its value(s).
Hence, parametric overloading requires additional information about the universal types to make them useful.

This additional information often comes as a set of operations that must be supply for a type, \eg \CFA/Rust/Go have traits, \CC template has concepts, Haskell has type-classes.
These operations can then be used in the body of the function to manipulate the type's value.
Here, a type binding to @T@ must have available a @++@ operation with the specified signature.
\begin{cfa}
forall( T | @T ?++( T, T )@ ) // trait
T inc( T t ) { return t@++@; } // change type value
int i = 3
i = inc( i )
double d = inc( 3.5 );
\end{cfa}
Given a qualifying trait, are its elements inferred or declared?
In the example, the type system infers @int@ for @T@, infers it needs an appropriately typed @++@ operator, and finds it in the enclosing environment, possibly in the language's prelude defining basic types and their operations.
This implicit inferencing is expensive if matched with implicit conversions when there is no exact match.
Alternatively, types opt-in to traits via declarations.
\begin{cfa}
trait postinc { T ?@++@( T, T ) }
type int provide postinc;
\end{cfa}
In this approach, after inferring @int@ for @T@, @int@ is examined for any necessary traits required in the function, so there is no searching.
Once all traits are satisfied, the compiler can inline them, pass functions pointers directly, or pass a virtual table composing the trait.
\end{enumerate}
The following sections illustrate how overloading is provided in \CFA.

\begin{comment}
\begin{haskell}
f( int, int );  f( int, float ); -- return types to be determined
g( int, int );  g( float, int );
let x = curry f( 3, _ ); -- which f
let y = curry g( _ , 3 ); -- which g
\end{haskell}
For the currying to succeed, there cannot be overloaded function names resulting in ambiguities.
To allow currying to succeed requires an implicit disambiguating mechanism, \ie a kind of transfer function.
A type class~\cite{typeclass} is a mechanism to convert overloading into parametric polymorphism.
Parametric polymorphism has enough information to disambiguate the overloaded names because it removes the type inferencing.
\begin{cfa}
forall( T | T has + $and$ - ) T f$\(_1\)$( T );
forall( T | T has * $and$ - ) T f$\(_2\)$( T );
x = f$\(_1\)$( x ); // if x has + and - but not *
y = f$\(_2\)$( y ); // if y has * and - but not +
\end{cfa}
Here, the types of @x@ and @y@ are combined in the type-class contraints to provide secondary infomration for disambiguation.
This approach handles many overloading cases because the contraints overlap completely or are disjoint

A type class (trait) generically abstracts the set of the operations used in a function's implementation.
A type-class instance binds a specific type to the generic operations to form concrete instances, giving a name type-class.
Then Qualified types concisely express the operations required to convert an overloaded
The name type-class is used as a transfer function to convert an overloaded function into a polymorphic function that is uniquely qualified with the  name type-class.
\begin{cfa}
void foo_int_trait( special int trait for operations in this foo );
void foo_int_int_trait( special (int, int) trait for operations in this foo );
\end{cfa}

In this case, the compiler implicitly changes the overloaded function to a parametrically polymorphic one.
Hence, the programmer does specify any additional information for the overloading to work.
Explicit overloading occurs when the compiler has to be told what operations are associated with a type  programmer separately defines the associate type and subsequently associates the type with overloaded name.
\end{comment}

\begin{comment}
Date: Mon, 24 Feb 2025 11:26:12 -0500
Subject: Re: overloading
To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
CC: <f37yu@uwaterloo.ca>, <ajbeach@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
        <alvin.zhang@uwaterloo.ca>, <lseo@plg.uwaterloo.ca>,
        <j82liang@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>

Yes.

With valediction,
  - Gregor Richards

On 2/24/25 11:22, Peter A. Buhr wrote:
>      Gregor Richards <gregor.richards@uwaterloo.ca> writes:
>      In Haskell, `+` works for both because of typeclasses (inclusion
>      polymorphism), and so is also not an unresolved type.
>
> I'm making this up. The Haskell type-class is a trait, like an interface or
> abstract class, and its usage/declaration/binding creates a specific trait
> instance for bound types, which is a vtable filled with the typed functions
> instantiated/located for the trait. The vtables are present at runtime and
> passed implicitly to ad-hoc polymorphic functions allowing differentiate of
> overloaded functions based on the number of traits and their specialization.
> (Major geek talk, YA! 8-)
>
>      On 2/21/25 23:04, Fangren Yu wrote:
>      > In a statically typed language I would rather have definitions like
>      > double x = x+x be ambiguous than "an unresolved type" as the latter
>      > sounds like a weaker version of a generic type, and being able to make
>      > something generic without explicitly saying so is probably not a good
>      > idea. Giving the unspecified parameter type an arbitrary preference is
>      > the second best option IMO (does ML give you a warning on such not
>      > fully inferred types?)
>      > ------------------------------------------------------------------------
>      > *From:* Gregor Richards <gregor.richards@uwaterloo.ca>
>      > *Sent:* Wednesday, February 19, 2025 9:55:23 PM
>      > *To:* Peter Buhr <pabuhr@uwaterloo.ca>
>      > *Cc:* Andrew James Beach <ajbeach@uwaterloo.ca>; Michael Leslie Brooks
>      > <mlbrooks@uwaterloo.ca>; Fangren Yu <f37yu@uwaterloo.ca>;
>      > j82liang@uwaterloo.ca <j82liang@uwaterloo.ca>; Alvin Zhang
>      > <alvin.zhang@uwaterloo.ca>; lseo@plg.uwaterloo.ca <lseo@plg.uwaterloo.ca>
>      > *Subject:* Re: overloading
>      > Jives with what I was saying, albeit not exactly the same; it's a result
>      > of the same problem.
>      >
>      > 'doubles' refers to an unresolved 'double', and the latter can't be
>      > resolved without the former, so you can't compile 'double' unless you
>      > know what its arguments are. The solutions are:
>      >
>      > * Typeclasses make it possible by compiling with a handle. When you
>      > call a function that takes a typeclass value as an argument, it takes an
>      > extra, hidden argument internally which is the typeclass handle. That
>      > handle tells the callee how to use the typeclass functions with this
>      > particular value. And, of course, you hope that some aggressive inlining
>      > gets rid of the dynamic dispatch :). But, no per se overloading
>      > supported, only inclusion polymorphism.
>      >
>      > * If you do whole-world compilation, then you just compile what you
>      > particularly need in context. If you call 'doubles' with a
>      > float,int,int, then you compile that version. But, currying is unsolved.
>      >
>      > * If you do C++-style templating, this is a less severe problem, as
>      > you compile it with the use of 'doubles', not with the definition. But,
>      > either no currying, or you have to specify the extra types explicitly so
>      > it knows what to curry, so no inference.
>      >
>      > * If you do Java-style generics, ... just kidding.
>      >
>      > In a language like Haskell or OCaml, if you want to compile this
>      > modularly and have the code with the implementation, then naively it
>      > would have to make eight implementations. But, even that is only true if
>      > (x, y, z) is a single tuple argument. Currying is still the killer. If
>      > you call `doubles 3`, the return is supposed to be some x -> y -> (int, x,
>      > y), where 'x' and 'y' are "some type on which I can call a 'double'
>      > function, but I don't know which double function yet because I don't
>      > know what type". Even *writing* that type is hard enough, but having
>      > values of that type float around at runtime? Yikes.
>      >
>      > To put it a different way: In C++ (and presumably CFA?), you can
>      > overload all you want to, but you can't get a function pointer to an
>      > unresolved overload. The function pointer is to a *particular* overload,
>      > not the set of possible overloads. Well, in a functional language,
>      > function pointers are the lifeblood of the language.
>      >
>      > With valediction,
>      > - Gregor Richards
>      >
>      > On 2/19/25 21:25, Peter A. Buhr wrote:
>      > > In the "Type Classes" chapter I sent out, the author says the
>      > following. Does it
>      > > jive with what you are saying about currying? BTW, I do not know who
>      > wrote the
>      > > book chapter.
>      > >
>      > >
>      > ==========================================================================
>      > >
>      > > Suppose we have a language that overloads addition + and
>      > multiplication *,
>      > > providing versions that work over values of type Int and type Float.
>      > Now,
>      > > consider the double function, written in terms of the overloaded
>      > addition
>      > > operation:
>      > >
>      > > double x = x + x
>      > >
>      > > What does this definition mean? A naive interpretation would be to
>      > say that
>      > > double is also overloaded, defining one function of type Int -> Int
>      > -> Int and a
>      > > second of type Float -> Float -> Float. All seems fine, until we
>      > consider the
>      > > function
>      > >
>      > > doubles (x,y,z) = (double x, double y, double z)
>      > >
>      > > Under the proposed scheme, this definition would give rise to eight
>      > different
>      > > versions! This approach has not been widely used because of the
>      > exponential
>      > > growth in the number of versions.
>      > >
>      > > To avoid this blow-up, language designers have sometimes restricted the
>      > > definition of overloaded functions. In this approach, which was
>      > adopted in
>      > > Standard ML, basic operations can be overloaded, but not functions
>      > defined in
>      > > terms of them. Instead, the language design specifies one of the
>      > possible
>      > > versions as the meaning of the function. For example, Standard ML give
>      > > preference to the type int over real, so the type (and
>      > implementation) of the
>      > > function double would be int -> int. If the programmer wanted to
>      > define a double
>      > > function over floating point numbers, she would have to explicitly
>      > write the
>      > > type of the function in its definition and give the function a name
>      > distinct
>      > > from the double function on integers. This approach is not particularly
>      > > satisfying, because it violates a general principle of language
>      > design: giving
>      > > the compiler the ability to define features that programmers cannot.
>
>      [2:text/html Show Save:noname (10kB)]
\end{comment}


\subsection{Operator Overloading}

Many programming languages provide general overloading of the arithmetic operators~\cite{OperOverloading} across the basic computational types using the number and type of parameters and returns.
However, in some languages, arithmetic operators may not be first class, and hence, cannot be overloaded.
Like \CC, \CFA allows general operator overloading for user-defined types.
The \CFA syntax for operator names uses the @'?'@ character to denote a parameter, \eg left and right unary operators: @?++@ and @++?@, and binary operators @?+?@ and @?<=?@.
Here, a user-defined type is extended with an addition operation with the same syntax as a builtin type.
\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}
\CFA excludes overloading the comma operator, short-circuit logical operators \lstinline{&&} and \lstinline{||}, and ternary conditional operator \lstinline{?} (\eg @max = x > y ? x : y@), all of which are short-hand control structures rather than operators.
\CC \emph{does} overload the comma and short-circuit logical operators, where overloading the comma operator is esoteric and rarely used, and the short-circuit operators do not exhibit short-circuit semantics, both of which are confusing and/or inconsistent.


\subsection{Function Overloading}

Many programming languages provide general overloading for functions~\cite{FuncOverloading}, as long as their prototypes differ in the number and type of parameters.
A few programming languages also use the return type for selecting overloaded functions \see{below}.
\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}
The type system examines each call site and first looks for an exact match and then a best match using conversions.

Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
Essentially, the return types are \emph{reversed curried} into output parameters of the function.
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 uses the return type, there is an exact match for each call, which again matches with programmer intuition and expectation.
This capability can be taken to the extreme, where the only differentiating factor is the return type.
\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.
As for operator overloading, if there is no exact match, a set of minimal implicit conversions can be added to find a best match.
\begin{cfa}
short int = random();	$\C[2in]{// select (1), unsafe}$
long double = random();	$\C{// select (2), safe}\CRT$
\end{cfa}


\subsection{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, in languages supporting 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 shadowing \see{namespace pollution in \VRef{s:Overloading}} is considered a normal programming-language feature with only slight software-engineering problems.
However, variable overloading within a scope is often considered dangerous, without any evidence to corroborate this claim.
In contrast, function overloading in \CC occurs silently within the global scope from @#include@ files all the time without problems.

In \CFA, the type system simply treats an overloaded variable as an overloaded function returning a value with no parameters.
Hence, no effort is required to support this feature as it is available for differentiating among overloaded functions with 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 common constants.


\subsection{Constant Overloading}

\CFA is unique in providing restricted constant overloading for the values @0@ and @1@, which have special status in C.
For example, 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@.
For example, @if@ and iteration statements in C compare the condition with @0@, and the increment and decrement operators are semantically equivalent to adding or subtracting the value @1@.
\begin{cfa}
if ( x ) ++x;        =>    if ( x @!= 0@ ) x @+= 1@;
for ( ; x; --x )   =>    for ( ; x @!= 0@; x @-= 1@ )
\end{cfa}
To generalize this feature, both constants are given types @zero_t@ and @one_t@ in \CFA, which allows overloading various operations for new types that seamlessly work within the 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{// constant constructors}$
void ?{}( S & s, one_t ) { s.[i,j] = 1; }
S ?=?( S & dst, zero_t ) { dst.[i,j] = 0; return dst; } $\C{// constant assignments}$
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 each field}$
S ?-=?( S & s, one_t ) { s.[i,j] -= 1; return s; }
int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } $\C{// constant comparison}$
S s = @0@;			$\C{// initialization}$
s = @0@;			$\C{// assignments}$
s = @1@;
if ( @s@ ) @++s@;	$\C{// unary ++/-\,- come implicitly from +=/-=}$
\end{cfa}
Here, type @S@ is first-class with respect to the basic types, working with all existing implicit C mechanisms.


\section{Type Inferencing}
\label{s:IntoTypeInferencing}

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 requiring an implicit/explicit conversion.
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 variables and performs a (possibly lossy) value conversion from one type to the other.
Finally, for assignment, the current variable and expression types may not agree.
Discovering a variable or function type is complex and has limitations.
The following covers these issues, and why this scheme is not amenable with the \CFA type system.

One of the first and most powerful type-inferencing systems 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.
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 @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.

There are multiple ways to indirectly specify a variable's type, \eg from a prior variable or expression.
\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 * i;
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}
Here, @type(expr)@ computes the same type as @auto@ righ-hand expression.
The advantages 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 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.
There can be strong software-engineering reasons for binding the types of these variables.
\end{itemize}
Note, the use of @typeof@ is more restrictive, and possibly safer, than general type-inferencing.
\begin{cquote}
\setlength{\tabcolsep}{20pt}
\begin{tabular}{@{}ll@{}}
\begin{cfa}
int x;
type(x) y = ... // complex expression
type(x) z = ... // complex expression
\end{cfa}
&
\begin{cfa}
int x;
auto y = ... // complex expression
auto z = ... // complex expression
\end{cfa}
\end{tabular}
\end{cquote}
On the left, the types of @y@ and @z@ are fixed (branded), whereas on the right, the types of @y@ and @z@ can fluctuate.


\subsection{Type-Inferencing Issues}

Each kind of type-inferencing system has its own set of issues that affect 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(s).
For example, if a change is made in an initialization expression, it can cascade type changes producing many other changes and/or errors.
At some point, a variable's type needs to remain constant and the initializing expression needs to be modified or be in error when it changes.
Often type-inferencing systems allow restricting (\newterm{branding}) a variable or function type, so the compiler 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 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 an IDE that can re-engineer types for them.
For example, given:
\begin{cfa}
auto x = @...@
\end{cfa}
and the need to write a function 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.

\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 correct types within an expression.
Type inferencing defeats this goal because there is no left-hand type.
Fundamentally, type inferencing tries to remove explicit typing from programming.
However, writing down types is an important aspect of good programming, as it provides a check of the programmer's expected type and the actual type.
Thinking carefully about types is similar to thinking carefully about date structures, often resulting in better performance and safety.
Similarly, thinking carefully about storage management in unmanaged languages is an important aspect of good programming, versus implicit storage management (garbage collection) in managed language.\footnote{
There are full-time Java consultants, who are hired to find memory-management problems in large Java programs, \eg Monika Beckworth.}
Given @typedef@ and @typeof@, and the strong desire to use the left-hand type in resolution, no attempt has been made in \CFA to support implicit type-inferencing.
Should a significant need arise, this decision can be revisited.


\section{Polymorphism}

\CFA provides polymorphic functions and types, where a polymorphic function can constrain types using assertions based on traits.


\subsection{Polymorphic Function}

The signature feature of the \CFA type-system is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96}, 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 an \newterm{object type}, \ie a type with a known size and alignment, which is sufficient to stack allocate, default or copy initialize, assign, and delete.
The \CFA implementation passes the size and alignment for each type parameter, as well as auto-generated default and copy constructors, assignment operator, and destructor.
For an incomplete \newterm{data type}, \eg pointer/reference types, this information is not needed.
\begin{cfa}
forall( T * ) T * identity( T * val ) { return val; }
int i, * ip = identity( &i );
\end{cfa}
Unlike \CC template functions, \CFA polymorphic functions are compatible with \emph{separate compilation}, preventing compilation and code bloat.

To constrain polymorphic types, \CFA uses \newterm{type assertions}~\cite[pp.~37-44]{Alphard} to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type variable.
Here, the function @twice@ works for any type @T@ with a matching addition operator.
\begin{cfa}
forall( T @| { T ?+?(T, T); }@ ) T twice( T x ) { return x @+@ x; }
int val = twice( twice( 3 ) );  $\C{// val == 12}$
\end{cfa}
The closest approximation to parametric polymorphism and assertions in C is type-unsafe (@void *@) functions, like @qsort@ for sorting an array of unknown values.
\begin{cfa}
void qsort( void * base, size_t nmemb, size_t size, int (*cmp)( const void *, const void * ) );
\end{cfa}
Here, the polymorphism is type-erasure, and the parametric assertion is the comparison function, which is explicitly passed.
\begin{cfa}
enum { N = 5 };
double val[N] = { 5.1, 4.1, 3.1, 2.1, 1.1 };
int cmp( const void * v1, const void * v2 ) { $\C{// compare two doubles}$
	return *(double *)v1 < *(double *)v2 ? -1 : *(double *)v2 < *(double *)v1 ? 1 : 0;
}
qsort( val, N, sizeof( double ), cmp );
\end{cfa}
The equivalent type-safe version in \CFA is a wrapper over the C version.
\begin{cfa}
forall( ET | { int @?<?@( ET, ET ); } ) $\C{// type must have < operator}$
void qsort( ET * vals, size_t dim ) {
	int cmp( const void * t1, const void * t2 ) { $\C{// nested function}$
		return *(ET *)t1 @<@ *(ET *)t2 ? -1 : *(ET *)t2 @<@ *(ET *)t1 ? 1 : 0;
	}
	qsort( vals, dim, sizeof(ET), cmp ); $\C{// call C version}$
}
qsort( val, N );  $\C{// deduct type double, and pass builtin < for double}$
\end{cfa}
The nested function @cmp@ is implicitly built and provides the interface from typed \CFA to untyped (@void *@) C.
Providing a hidden @cmp@ function in \CC is awkward as lambdas do not use C calling conventions and template declarations cannot appear in block scope.
% In addition, an alternate kind of return is made available: position versus pointer to found element.
% \CC's type system cannot disambiguate between the two versions of @bsearch@ because it does not use the return type in overload resolution, nor can \CC separately compile a template @bsearch@.
Call-site inferencing and nested functions provide a localized form of inheritance.
For example, the \CFA @qsort@ can be made to sort in descending order by locally changing the behaviour of @<@.
\begin{cfa}
{
	int ?<?( double x, double y ) { return x @>@ y; } $\C{// locally override behaviour}$
	qsort( vals, 10 );							$\C{// descending sort}$
}
\end{cfa}
The local version of @?<?@ overrides the built-in @?<?@ so it is passed to @qsort@.
The local version performs @?>?@, making @qsort@ sort in descending order.
Hence, any number of assertion functions can be overridden locally to maximize the reuse of existing functions and types, without the construction of a named inheritance hierarchy.
A final example is a type-safe wrapper for C @malloc@, where the return type supplies the type/size of the allocation, which is impossible in most type systems.
\begin{cfa}
static inline forall( T & | sized(T) )
T * malloc( void ) {
	if ( _Alignof(T) <= __BIGGEST_ALIGNMENT__ ) return (T *)malloc( sizeof(T) ); // C allocation
	else return (T *)memalign( _Alignof(T), sizeof(T) );
}
// select type and size from left-hand side
int * ip = malloc();  double * dp = malloc();  [[aligned(64)]] struct S {...} * sp = malloc();
\end{cfa}
The @sized@ assertion passes size and alignment as a data object has no implicit assertions.
Both assertions are used in @malloc@ via @sizeof@ and @_Alignof@.
In practice, this polymorphic @malloc@ is unwrapped by the C compiler and the @if@ statement is elided producing a type-safe call to @malloc@ or @memalign@.

This mechanism is used to construct type-safe wrapper-libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions.
Here, existing C legacy code is leveraged as much as possible;
other programming languages must build supporting libraries from scratch, even in \CC.


\subsection{Traits}
\label{s:Traits}

\CFA provides \newterm{traits} to name a group of type assertions, where the trait name allows specifying the same set of assertions in multiple locations, preventing repetition mistakes at each function declaration.
\begin{cquote}
\begin{tabular}{@{}l|@{\hspace{10pt}}l@{}}
\begin{cfa}
forall( T ) trait @sumable@ {
	void @?{}@( T &, zero_t ); // 0 literal constructor
	T @?+=?@( T &, T );		 // assortment of additions
	T ?+?( T, T );
	T ++?( T & );
	T ?++( T & );
};
\end{cfa}
&
\begin{cfa}
forall( T @| sumable( T )@ )   // use trait
T sum( T a[$\,$], size_t size ) {
	@T@ total = 0;		  // initialize by 0 constructor
	for ( i; size )
		total @+=@ a[i];	// select appropriate +
	return total;
}
\end{cfa}
\end{tabular}
\end{cquote}
Traits are implemented by flattening them at use points, as if written in full by the programmer.
Flattening often results in overlapping assertions, \eg operator @+@.
Hence, trait names play no part in type equivalence.
In the example, type @T@ is an object type, and hence, has the implicit internal trait @otype@.
\begin{cfa}
trait otype( T & | sized(T) ) {
	void ?{}( T & );						$\C{// default constructor}$
	void ?{}( T &, T );						$\C{// copy constructor}$
	void ?=?( T &, T );						$\C{// assignment operator}$
	void ^?{}( T & );						$\C{// destructor}$
};
\end{cfa}
These implicit functions are used by the @sumable@ operator @?+=?@ for the right side of @?+=?@ and return.

If the array type is not a builtin type, an extra type parameter and assertions are required, like subscripting.
This case is generalized in polymorphic container-types, such as a list with @insert@ and @remove@ operations, and an element type with copy and assignment.


\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.
\begin{enumerate}[leftmargin=*]
\item
Write bespoke data structures for each context.
While this approach is flexible and supports integration with the C type checker and tooling, it is tedious and error prone, especially for more complex data structures.

\item
Use @void *@-based polymorphism, \eg the C standard library functions @bsearch@ and @qsort@, which allow for the reuse of code with common functionality.
However, this approach eliminates the type checker's ability to ensure argument types are properly matched, often requiring a number of extra function parameters, pointer indirection, and dynamic allocation that is otherwise unnecessary.

\item
Use an internal macro capability, like \CC @templates@, to generate code that is both generic and type checked, but errors may be difficult to interpret.
Furthermore, writing complex template macros is difficult and complex.

\item
Use an external macro capability, like M4~\cite{M4}, to generate code that is generic code, but errors may be difficult to interpret.
Like internal macros, writing and using external macros is equally difficult and complex.
\end{enumerate}

\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.
For concrete parameters, the generic-type definition can be inlined, like \CC templates, if its definition appears in a header file (\eg @static inline@).

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}
\begin{tabular}{@{}l|@{\hspace{10pt}}l@{}}
\begin{cfa}
@forall( F, S )@ struct pair {
	F first;	S second;
};
@forall( F, S )@ // object
S second( pair( F, S ) p ) { return p.second; }
@forall( F *, S * )@ // sized
S * second( pair( F *, S * ) p ) { return p.second; }
\end{cfa}
&
\begin{cfa}
pair( double, int ) dpr = { 3.5, 42 };
int i = second( dpr );
pair( void *, int * ) vipr = { 0p, &i };
int * ip = second( vipr );
double d = 1.0;
pair( int *, double * ) idpr = { &i, &d };
double * dp = second( idpr );
\end{cfa}
\end{tabular}
\end{cquote}
\label{s:GenericImplementation}
\CFA generic types are \newterm{fixed} or \newterm{dynamic} sized.
Fixed-size types have a fixed memory layout regardless of type parameters, whereas dynamic types vary in memory layout depending on the type parameters.
For example, the type variable @T *@ is fixed size and is represented by @void *@ in code generation;
whereas, the type variable @T@ is dynamic and set at the point of instantiation.
The difference between fixed and dynamic is the complexity and cost of field access.
For fixed, field offsets are computed (known) at compile time and embedded as displacements in instructions.
For dynamic, field offsets are compile-time computed at the call site, stored in an array of offset values, passed as a polymorphic parameter, and added to the structure address for each field dereference within a polymorphic function.
See~\cite[\S~3.2]{Moss19} for complete implementation details.

Currently, \CFA generic types allow assertion.
For example, the following declaration of a sorted set-type ensures the set key supports equality and relational comparison.
\begin{cfa}
forall( Elem, @Key@ | { _Bool ?==?( Key, Key ); _Bool ?<?( Key, Key ); } )
struct Sorted_Set { Elem elem; @Key@ key; ... };
\end{cfa}
However, the operations that insert/remove elements from the set should not appear as part of the generic-types assertions.
\begin{cfa}
forall( @Elem@ | /* any assertions on element type */ ) {
	void insert( Sorted_Set set, @Elem@ elem ) { ... }
	bool remove( Sorted_Set set, @Elem@ elem ) { ... } // false => element not present
	... // more set operations
} // distribution
\end{cfa}
(Note, the @forall@ clause can be distributed across multiple functions.)
For software-engineering reasons, the set assertions would be refactored into a trait to allow alternative implementations, like a Java \lstinline[language=java]{interface}.

In summation, the \CFA type system inherits \newterm{nominal typing} for concrete types from C;
however, without inheritance in \CFA, nominal typing cannot be extended to polymorphic subtyping.
Instead, \CFA adds \newterm{structural typing} and uses it to generate polymorphism.
Here, traits are like interfaces in Java or abstract base-classes in \CC, but without the nominal inheritance relationships.
Instead, each polymorphic function or generic type defines the structural requirements needed for its execution, which is fulfilled at each call site from the lexical environment, like Go~\cite{Go} or Rust~\cite{Rust} interfaces.
Hence, lexical scopes and nested functions are used extensively to mimic subtypes, as in the @qsort@ example, without managing a nominal inheritance hierarchy.


\section{Language Comparison}

\begin{table}
\caption{Overload Features in Programming Languages}
\label{t:OverloadingFeatures}
\centering
\begin{minipage}{\linewidth}
\setlength{\tabcolsep}{5pt}
\begin{tabular}{@{}r|cccccccc@{}}
Feature\,{\textbackslash}\,Language	& Ada	& \CC		& \CFA	& Java	& Scala	& Swift & Rust & Haskell	\\
\hline
general\footnote{overloadable entities: V $\Rightarrow$ variable, O $\Rightarrow$ operator, F $\Rightarrow$ function, M $\Rightarrow$ member}
						& O\footnote{except assignment}/F	& O/F/M	& V/O/F	& M\footnote{not universal}	& O/M	& O/F/M	& no	& no	\\
general constraints\footnote{T $\Rightarrow$ parameter type, \# $\Rightarrow$ parameter count, N $\Rightarrow$ parameter name; R $\Rightarrow$ return type}
						& T/\#//R\footnote{parameter names can be used to disambiguate among overloads but not create overloads}
									& T/\#	& T/\#/R	& T/\#	& T/\#/N/R	& T/\#/N/R	& T/\#/N	& T/R \\
univ. type constraints\footnote{A $\Rightarrow$ concept, T $\Rightarrow$ interface/trait/type-class, B $\Rightarrow$ type bounds}
						& no\footnote{generic cannot be overloaded}		& C		& T			& B		& B			& T			& T			& T \\
Safe/Unsafe arg. conv.	& no		& S/U\footnote{no conversions allowed during template parameter deduction}	& S/U
						& S\footnote{unsafe (narrowing) conversion only allowed in assignment or initialization to a primitive (builtin) type}	& S
						& no\footnote{literals only, Int $\rightarrow$ Double (Safe)}	& no	& no
\end{tabular}
\end{minipage}
% https://doc.rust-lang.org/rust-by-example/trait/impl_trait.html
% https://dl.acm.org/doi/10.1145/75277.75283
\end{table}

Because \CFA's type system is focused on overloading and overload resolution, \VRef[Table]{t:OverloadingFeatures} compares \CFA with a representative set of popular programming languages and their take on overloading.
The first row classifies whether there is general overloading, and what entities may be overloaded.
\begin{cquote}
\setlength{\tabcolsep}{10pt}
\begin{tabular}{@{}llll@{}}
\multicolumn{1}{c}{\textbf{variable}} & \multicolumn{1}{c}{\textbf{operator}} & \multicolumn{1}{c}{\textbf{function}} & \multicolumn{1}{c}{\textbf{member}} \\
\begin{cfa}
int foo;
double foo;


\end{cfa}
&
\begin{cfa}
int ?+?( int );
double ?+?( double );


\end{cfa}
&
\begin{cfa}
int foo( int );
double foo( int, int );


\end{cfa}
&
\begin{c++}
class C {
	int foo( int );
	double foo( int, int );
};
\end{c++}
\end{tabular}
\end{cquote}

The second row classifies the specialization mechanisms used to distinguish among the general overload capabilities.
\begin{cquote}
\begin{tabular}{@{}llll@{}}
\multicolumn{1}{c}{\textbf{type}} & \multicolumn{1}{c}{\textbf{number}} & \multicolumn{1}{c}{\textbf{name}} & \multicolumn{1}{c}{\textbf{return}} \\
\begin{swift}
func foo( _ x : Int )
func foo( _ x : Double )
foo( 3 )
foo( 3.5 )
\end{swift}
&
\begin{swift}
func foo( _ x : Int )
func foo( _ x : Int, _ y : Int )
foo( 3 )
foo( 3, 3 )
\end{swift}
&
\begin{swift}
func foo( x : Int )
func foo( y : Int )
foo( x : 3 )
foo( y : 3 );
\end{swift}
&
\begin{swift}
func foo() -> Int
func foo() -> String
var i : Int = foo()
var s : String = foo();
\end{swift}
\end{tabular}
\end{cquote}

The third row classifies if generic functions can be overloaded based on differing type variables, number of type variables, and type-variable constraints.
The following examples illustrates the first two overloading cases.
\begin{swift}
func foo<T>( a : T );  $\C[2.25in]{// foo1}$
func foo<T>( a : T,  b : T );  $\C{// foo2}$
func foo<T,U>( a : T,  b :  U );  $\C{// foo3}$
foo( a : 3.5 );  $\C{// foo1}$
foo( a : 2, b : 2 );  $\C{// foo2}$
foo( a : 2, b : 2.5 );  $\C{// foo3}\CRT$
\end{swift}
Here, the overloaded calls are disambiguated using argument types and their number.

However, the parameter operations are severely restricted because universal types have few operations.
For example, Swift provides a @print@ operation for its universal type, and the Java @Object@ class provides general methods: @toString@, @hashCode@, @equals@, @finalize@, \etc.
This restricted mechanism still supports a few useful functions, where the parameters are abstract entities, \eg:
\begin{swift}
func swap<T>( _ a : inout T, _ b : inout T ) { let temp : T = a;  a = b;  b = temp; }
var ix = 4, iy = 3;
swap( &ix, &iy );
var fx = 4.5, fy = 3.5;
swap( &fx, &fy );
\end{swift}
To make a universal function useable, an abstract description is needed for the operations used on the parameters within the function body.
Type matching these operations can be done by using techniques like \CC template expansion, or explicit stating, \eg interfaces, subtyping (inheritance), assertions (traits), type classes, type bounds.
The mechanism chosen can affect separate compilation or require runtime type information (RTTI).
\begin{description}
\item[concept] ~
\begin{c++}
concept range = requires( T& t ) {
	ranges::begin(t); // equality-preserving for forward iterators
	ranges::end (t);
};
\end{c++}
\item[traits/type-classes] ~
\begin{cfa}
forall( T | { T ?+?( T, T ) } )
\end{cfa}
\item[inheritance/type bounds] ~
\begin{scala}
class CarPark[Vehicle  >: bike  <: bus](val lot : Vehicle)
\end{scala}
\end{description}

\begin{figure}
\setlength{\tabcolsep}{12pt}
\begin{tabular}{@{}ll@{}}
\multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{Haskell}} \\
\begin{cfa}
forall( T ) trait sumable {
	void ?{}( T &, zero_t );
	T ?+=?( T &, T );  };
forall( T | sumable( T ) )
T sum( T a[], size_t size ) {
	T total = 0;
	for ( i; size ) total += a[i];
	return total;  }
struct S { int i, j; };
void ?{}( S & s, zero_t ) { s.[i, j] = 0; }
void ?{}( S & s ) { s{0}; }
void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
S ?+=?( S & l, S r ) { l.[i, j] += r.[i, j]; }
int main() {		// trait inferencing
	sout | sum( (int []){ 1, 2, 3 }, 3 );
	sout | sum( (double []){ 1.5, 2.5, 3.5 }, 3 );
	sout | sum( (S []){ {1,1}, {2,2}, {3,3} }, 3 ).[i, j];  }



\end{cfa}
&
\begin{haskell}
class Sumable a where
	szero :: a
	sadd :: a -> a -> a
ssum ::  Sumable a $=>$ [a] -> a
ssum (x:xs) = sadd x (ssum xs)
ssum [] = szero
data S = S Int Int deriving Show
@instance Sumable Int@ where
	szero = 0
	sadd = (+)
@instance Sumable Float@ where
	szero = 0.0
	sadd = (+)
@instance Sumable S@ where
	szero = S 0 0
	sadd (S l1 l2) (S r1 r2) = S (l1 + r1) (l2 + r2)
main = do
	print ( ssum [1::Int, 2, 3] )
	print ( ssum [1.5::Float, 2.5, 3.5] )
	print ( ssum [(S 1 1), (S 2 2), (S 3 3)] )
\end{haskell}
\end{tabular}
\caption{Implicit/Explicit Trait Inferencing}
\label{f:ImplicitExplicitTraitInferencing}
\end{figure}

One differentiating feature among these specialization techniques is the ability to implicitly or explicitly infer the trait information at a class site.
\VRef[Figure]{f:ImplicitExplicitTraitInferencing} compares the @sumable@ trait and polymorphic @sum@ function \see{\VRef{s:Traits}} for \CFA and Haskell.
Here, the \CFA type system inferences the trait functions at each call site, so no additional specification is necessary by the programmer.
The Haskell program requires the programmer to explicitly bind the trait and to each type that can be summed.
(The extra casts in the Haskell program are necessary to differentiate between the two kinds of integers and floating-point numbers.)
Ada also requires explicit binding, creating a new function name for each generic instantiation.
\begin{ada}
function int_Twice is new Twice( Integer, "+" => "+" );
function float_Twice is new Twice( Float, "+" => "+" );
\end{ada}
Finally, there is a belief that certain type systems cannot support general overloading, \eg Haskell.
As \VRef[Table]{t:OverloadingFeatures} shows, there are multiple languages with both general and parametric overloading, so the decision to not support general overloading is based on design choices made by the language designers not any reason in type theory.

The fourth row classifies if conversions are attempted beyond exact match.
\begin{cfa}
int foo( double ); $\C[1.75in]{// 1}$
double foo( int ); $\C{// 2}$
int i = foo( 3 ); $\C{// 1 : 3 to 3.0 argument conversion => all conversions are safe}$
double d = foo( 3.5 ); $\C{// 1 : int to double result conversion => all conversions are safe}\CRT$
\end{cfa}


\section{Contributions}

The \CFA compiler performance and type capability have been greatly improved through my development work.
\begin{enumerate}
\item
The compilation time of various \CFA library units and test programs has been reduced by an order of magnitude, from minutes to seconds \see{\VRef[Table]{t:SelectedFileByCompilerBuild}}, which made it possible to develop and test more complicated \CFA programs that utilize sophisticated type system features.
The details of compiler optimization work are covered in a previous technical report~\cite{Yu20}, which essentially forms part of this thesis.
\item
This thesis presents a systematic review of the new features added to the \CFA language and its type system.
Some of the more recent inclusions to \CFA, such as tuples and generic structure types, were not well tested during development due to the limitation of compiler performance.
Several issues coming from the interactions of various language features are identified and discussed in this thesis;
some of them I have resolved, while others are given temporary fixes and need to be reworked in the future.
\item
Finally, this thesis provides constructive ideas for fixing a number of high-level issues in the \CFA language design and implementation, and gives a path for future improvements to the language and compiler.
\end{enumerate}


\begin{comment}
From: Andrew James Beach <ajbeach@uwaterloo.ca>
To: Peter Buhr <pabuhr@uwaterloo.ca>, Michael Leslie Brooks <mlbrooks@uwaterloo.ca>,
    Fangren Yu <f37yu@uwaterloo.ca>, Jiada Liang <j82liang@uwaterloo.ca>
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}
