\chapter{Future Work}

These are some feature requests related to type system enhancement that come up during the development of \CFA language and library, but have not been implemented yet. A lot of the current implementations have to work around the limits of the language features and sometimes lead to inefficiency. 

\section{Closed trait types}

\CFA as it currently is does not have any closed types, as new functions can be added at any time. It is also possible to locally declare a function,\footnote{Local functions are not a standard feature in C but supported by mainstream C compilers such as gcc, and allowed in \CFA too.} or a function pointer variable to make a type satisfy a certain trait temporarily and be used as such in this limited scope. However, the lack of closed types in such a "duck typing" scheme proposes two problems. For library implementors, it is common to not want the defined set of operations to be overwritten and cause the behavior of polymorphic invocations to change. For the compiler, it means caching and reusing the result of resolution is not reliable as newly introduced declarations can participate in assertion resolution, making a previously invalid expression valid, or the other way around by introducing ambiguity in assertions. Sometimes those interfaces are fairly complicated, for example the I/O library traits \textbf{istream} and \textbf{ostream} each has over 20 operations. Without the ability to store and reuse assertion resolution results, each time the compiler encounters an I/O operation in the source code (mainly the pipe operator \textbf{?|?} used to represent stream operations in \CFA) it has to resolve the same set of assertions again, causing a lot of repetitive work. Previous experiments have shown that the I/O assertions often account for over half of the number of assertions resolved in a \CFA translation unit. Introducing a way to eliminate the need of doing such repetitive assertion resolutions that are very unlikely to change by new overloads can therefore provide significant improvement to the performance of the compiler.

The output stream trait in \CFA looks like this:

\begin{cfa}
forall( ostype & )
trait basic_ostream {
	// private
	bool sepPrt$\$$( ostype & );							// get separator state (on/off)
    
	void sepReset$\$$( ostype & );							// set separator state to default state
	void sepReset$\$$( ostype &, bool );					// set separator and default state
	const char * sepGetCur$\$$( ostype & );				// get current separator string
	void sepSetCur$\$$( ostype &, const char [] );			// set current separator string
	bool getNL$\$$( ostype & );							// get newline
	bool setNL$\$$( ostype &, bool );						// set newline
	bool getANL$\$$( ostype & );							// get auto newline (on/off)
	bool setANL$\$$( ostype &, bool );						// set auto newline (on/off), and return previous state
	bool getPrt$\$$( ostype & );							// get fmt called in output cascade
	bool setPrt$\$$( ostype &, bool );						// set fmt called in output cascade
	// public
	void nlOn( ostype & );								// turn auto-newline state on
	void nlOff( ostype & );								// turn auto-newline state off

	void sep( ostype & );								// turn separator state on
	void nosep( ostype & );								// turn separator state off
	bool sepOn( ostype & );								// set default state to on, and return previous state
	bool sepOff( ostype & );							// set default state to off, and return previous state
	const char * sepGet( ostype & );					// get separator string
	void sepSet( ostype &, const char [] );				// set separator to string (15 character maximum)
	const char * sepGetTuple( ostype & );				// get tuple separator string
	void sepSetTuple( ostype &, const char [] );		// set tuple separator to string (15 character maximum)

	void ends( ostype & );								// end of output statement
	int fmt( ostype &, const char format[], ... ) __attribute__(( format(printf, 2, 3) ));
}; // basic_ostream

\end{cfa}

and the overloaded output operator is declared as

\begin{cfa}
forall( ostype & | basic_ostream( ostype ) ) ostype & ?|?( ostype &, int ); // also defined for all other basic types
\end{cfa}

The \CFA polymorphic calling convention makes the output operator take all the trait functions above as implicit arguments, and they are repeatedly resolved every time an output operator is called. This creates both compile-time and run-time overheads. \CFA compilation takes the most amount of time in resolving assertions, and it is not uncommon to see in the current codebase and test suite that the majority of time spent in compiling a program is due to a series of I/O statements, while resolving those assertions for I/O operators could have been a once and done job. Repeatedly pushing tens of arguments onto the stack to call these operators at run-time can also have some impact on performance, although in the I/O case it is not as significant, since these operations involve system calls which are already slow.

An addition of closed trait type will make a \CFA trait behave similarly to a Haskell type class and require an explicit instance declaration as well. The syntax for closed trait is not settled yet, but it may look something like the following:

\begin{cfa}
    forall(ostype &)
    closed trait basic_ostream{
        // all the above declarations
    };

    struct ofstream {
        // ... 
    };

    // implementation of trait functions for ofstream

    __cfa_trait_instance(ofstream, basic_ostream);
\end{cfa}

At the point of instance declaration, all the required trait functions must be declared in scope, and they are inferred and specialized in the usual way. The list of inferred and specialized functions will then be saved into an array of function pointers, and polymorphic functions using the closed trait type can simply take the instance pointer, instead of a list of implicit parameters.

\begin{cfa}
    // original declaration
    forall( ostype & | basic_ostream( ostype ) ) ostype & ?|?( ostype & os, int i); 

    // translated code with closed trait
    void* ?|?(void* os, int i, void* _instance_basic_ostream); 
    // polymorphic pointer and reference types are erased

    // call site
    sout | 42;
    
    // generated code
    ?|?(sout, 42, __cfa_instance_ofstream__basic_ostream /* generated by __cfa_trait_instance decl */);
\end{cfa}

Introducing closed trait types also comes with the additional benefit of the ability to achieve better encapsulation. In the above @basic_ostream@ example, the functions ending with dollar sign \$ are meant to be private (internal to the library) and the client code should not be able to see or call those functions directly. This is impossible with the current trait and assertion system, because the satisfying declarations must be visible at call site for the polymorphic call to resolve. With closed traits and instance types, it will be possible to forward declare a closed trait (which would not even make sense otherwise) and the instance pointer of a type implementing the trait without exposing any details to the client, since all the information necessary to call the trait functions are now captured in a single pointer to a table of functions built at compile time, and the type of that pointer is made opaque to the client.




\section{Associated Types}

The analysis presented in section 4.3 shows that if we mandate that all type parameters have to be bound before assertion resolution, the complexity of resolving assertions become much lower as every assertion parameter can be resolved independently. By fully utilizing information from higher up the expression tree for return value overloading, most of the type bindings can be resolved. However there are certain scenarios where we would like to have some intermediate types to be involved in certain operations, that are neither input nor output types.

\CFA standard library provides a few polymorphic container types similar to those found in \CC standard template library. Naturally, we would like to have a harmonized iterator interface for different container types. The feature is still under development and has not been finalized, but for the purpose of this discussion, we will be using a similar approach to the \CC standard iterator contract. The equivalent type signatures can be given in \CFA trait as:

\begin{cfa}
    forall (Container, Iter, Elem) trait iterable {
        Iter begin(Container);
        Iter end(Container);
        Elem& *?(Iter);
        Iter ++?(Iter);
        bool ?==?(Iter,Iter);
    }
\end{cfa}

These are the exact operators required in \CC for the for-loop comprehension
@for (Elem & e: container)@. The problem with this trait declaration is that the iterator type @Iter@ has to be explicitly given in the trait parameter list, but is intended to be deduced from the @begin@ and @end@ operations on the container type; and the same can be said for the element type. The iterable trait is meant to describe a property for the container type but involves two additional types, one for the iterator type and one for the element type. If we were to disallow unbound type parameters in assertions without adding any features to the type system, we will not be able to describe this iterable property in \CFA. 

To solve this problem, I propose adding associate type declarations in addition to the existing (free) @forall@ type parameters. In the body of a trait declaration, new types can be introduced as the return type of an expression involving the type parameters. Following the above example, the iterator contract could be rewritten to
\begin{cfa}
    forall (Container) trait iterable {
        type Iter = begin(Container);
        type Elem& = *?(Iter);
        Iter end(Container);
        Iter ++?(Iter);
        bool ?==?(Iter,Iter);
    }
\end{cfa}

This matches conceptually better that the iterable trait is about one type (the container) rather than about three types. The iterator type and element type can be viewed as properties of the container types, not independent type parameters.

There remains one design decision to be made, that is whether the expressions that define associated types need to be uniquely resolved. If the associated type does not appear in parameter or return types, having such requirement seems to be reasonable, because the expression cost does not consider any conversions that occur in assertion parameter matching, which essentially means having multiple ways to resolve an associated type always results in ambiguity. A more interesting case would be that the associated type also appears in the return type, where both resolving the return type overload based on context and resolving the assertion that defines the associate type can help, and for the whole expression to resolve, they must agree on a common result. Moss gave the following example to illustrate \CFA assertion system's expressiveness that could be viewed as having an associated type as return, and can potentially vary based on the context:

\begin{cfa}
    forall(Ptr, Elem) trait pointer_like {
        Elem& *?(Ptr); // Ptr can be dereferenced to Elem
    };
    struct list {
        int value;
        list* next; // may omit struct on type names
    };
    typedef list* list_iterator;
    int& *?(list_iterator it) {
        return it->value;
    }
\end{cfa}

Note that the type @list*@ satisfies both @pointer_like(list*, int)@ and @pointer_like(list*, list)@ (the latter by the built-in pointer dereference operator) and the expression @*it@ could be either a @struct list@ or an @int@. Requiring associated types to be unique would make the @pointer_like@ trait inapplicable to @list*@ here, which is not desirable. I have not attempted to implement associated types in \CFA compiler, but based on the above discussions, one option could be to make associated type resolution and return type overloading coexist: when the associated type appears in the returns, we deduce it from the context and then verify the trait with ordinary assertion resolution; when it does not appear in the returns, we instead require the type to be uniquely determined by the expression that defines the associated type.

\section{User-defined conversions}
