Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/Paper.tex

    r48b9b36 rc8ad5d9  
    5454\newcommand{\TODO}[1]{} % TODO elided
    5555
    56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    57 
    5856% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    5957% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
     
    6260\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
    6361
    64 \renewcommand*{\thefootnote}{\Alph{footnote}} % hack because fnsymbol does not work
    65 %\renewcommand*{\thefootnote}{\fnsymbol{footnote}}
    66 
    6762\makeatletter
    6863% parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
     
    7974\setlength{\gcolumnposn}{3.5in}
    8075\setlength{\columnposn}{\gcolumnposn}
    81 
    8276\newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\lst@basicstyle{\LstCommentStyle{#2}}}}
    8377\newcommand{\CRT}{\global\columnposn=\gcolumnposn}
     
    165159literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.1ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptstyle\land\,$}}1
    166160        {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 % {`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
    167         {<}{\textrm{\textless}}1 {>}{\textrm{\textgreater}}1
    168         {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1ex][c]{\raisebox{0.5ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex{\textrm{\textgreater}}}2,
     161        {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex{\textgreater}}2,
    169162moredelim=**[is][\color{red}]{`}{`},
    170163}% lstset
     
    180173\lstMakeShortInline@%
    181174
    182 \let\OLDthebibliography\thebibliography
    183 \renewcommand\thebibliography[1]{
    184   \OLDthebibliography{#1}
    185   \setlength{\parskip}{0pt}
    186   \setlength{\itemsep}{4pt plus 0.3ex}
    187 }
    188175
    189176\title{\texorpdfstring{\protect\CFA : Adding Modern Programming Language Features to C}{Cforall : Adding Modern Programming Language Features to C}}
     
    203190The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems to hobby projects.
    204191This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more.
    205 Nevertheless, C, first standardized almost forty years ago, lacks many features that make programming in more modern languages safer and more productive.
    206 
    207 The goal of the \CFA project (pronounced ``C-for-all'') is to create an extension of C that provides modern safety and productivity features while still ensuring strong backwards compatibility with C and its programmers.
    208 Prior projects have attempted similar goals but failed to honour C programming-style;
    209 for instance, adding object-oriented or functional programming with garbage collection is a non-starter for many C developers.
     192Nevertheless, C, first standardized over thirty years ago, lacks many features that make programming in more modern languages safer and more productive.
     193
     194The goal of the \CFA project is to create an extension of C that provides modern safety and productivity features while still ensuring strong backwards compatibility with C and its programmers.
     195Prior projects have attempted similar goals but failed to honour C programming-style; for instance, adding object-oriented or functional programming with garbage collection is a non-starter for many C developers.
    210196Specifically, \CFA is designed to have an orthogonal feature-set based closely on the C programming paradigm, so that \CFA features can be added \emph{incrementally} to existing C code-bases, and C programmers can learn \CFA extensions on an as-needed basis, preserving investment in existing code and programmers.
    211197This paper presents a quick tour of \CFA features showing how their design avoids shortcomings of similar features in C and other C-like languages.
     
    241227Love it or hate it, C is extremely popular, highly used, and one of the few systems languages.
    242228In many cases, \CC is often used solely as a better C.
    243 Nevertheless, C, first standardized almost forty years ago~\cite{ANSI89:C}, lacks many features that make programming in more modern languages safer and more productive.
    244 
    245 \CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that adds modern language-features to C, while maintaining both source and runtime compatibility with C and a familiar programming model for programmers.
     229Nevertheless, C, first standardized over thirty years ago, lacks many features that make programming in more modern languages safer and more productive.
     230
     231\CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that aims to add modern language features to C, while maintaining both source and runtime compatibility with C and a familiar programming model for programmers.
    246232The four key design goals for \CFA~\cite{Bilson03} are:
    247233(1) The behaviour of standard C code must remain the same when translated by a \CFA compiler as when translated by a C compiler;
     
    252238\CC is used similarly, but has the disadvantages of multiple legacy design-choices that cannot be updated and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project.
    253239
    254 All languages features discussed in this paper are working, except some advanced exception-handling features.
    255 Not discussed in this paper are the integrated concurrency-constructs and user-level threading-library~\cite{Delisle18}.
    256 \CFA is an \emph{open-source} project implemented as a source-to-source translator from \CFA to the gcc-dialect of C~\cite{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by gcc, meeting goals (1)--(3).
     240\CFA is currently implemented as a source-to-source translator from \CFA to the gcc-dialect of C~\cite{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by gcc, meeting goals (1)--(3).
    257241Ultimately, a compiler is necessary for advanced features and optimal performance.
    258 % @plg2[9]% cd cfa-cc/src; cloc ArgTweak CodeGen CodeTools Common Concurrency ControlStruct Designators GenPoly InitTweak MakeLibCfa.cc MakeLibCfa.h Parser ResolvExpr SymTab SynTree Tuples driver prelude main.cc
    259 % -------------------------------------------------------------------------------
    260 % Language                     files          blank        comment           code
    261 % -------------------------------------------------------------------------------
    262 % C++                            108           5420           5232          34961
    263 % C/C++ Header                    86           2379           2450           8464
    264 % Teamcenter def                   2            115             65           1387
    265 % make                             5            168             87           1052
    266 % C                               20            109            403            488
    267 % awk                              1             12             26            121
    268 % sed                              1              0              0              6
    269 % -------------------------------------------------------------------------------
    270 % SUM:                           223           8203           8263          46479
    271 % -------------------------------------------------------------------------------
    272 The \CFA translator is 200+ files and 46,000+ lines of code written in C/\CC.
    273 Starting with a translator versus a compiler makes it easier and faster to generate and debug C object-code rather than intermediate, assembler or machine code.
    274 The translator design is based on the \emph{visitor pattern}, allowing multiple passes over the abstract code-tree, which works well for incrementally adding new feature through additional visitor passes.
    275 At the heart of the translator is the type resolver, which handles the polymorphic routine/type overload-resolution.
    276 % @plg2[8]% cd cfa-cc/src; cloc libcfa
    277 % -------------------------------------------------------------------------------
    278 % Language                     files          blank        comment           code
    279 % -------------------------------------------------------------------------------
    280 % C                               35           1256           1240           9116
    281 % C/C++ Header                    54            358           1106           1198
    282 % make                             2            201            325           1167
    283 % C++                              3             18             17            124
    284 % Assembly                         3             56             97            111
    285 % Bourne Shell                     2              2              0             25
    286 % awk                              1              4              0             22
    287 % -------------------------------------------------------------------------------
    288 % SUM:                           100           1895           2785          11763
    289 % -------------------------------------------------------------------------------
    290 The \CFA runtime system is 100+ files and 11,000+ lines of code, written in \CFA.
    291 Currently, the \CFA runtime is the largest \emph{user} of \CFA providing a vehicle to test the language features and implementation.
    292 % @plg2[6]% cd cfa-cc/src; cloc tests examples benchmark
    293 % -------------------------------------------------------------------------------
    294 % Language                     files          blank        comment           code
    295 % -------------------------------------------------------------------------------
    296 % C                              237          12260           2869          23286
    297 % make                             8            464            245           2838
    298 % C/C++ Header                    22            225            175            785
    299 % Python                           5            131             93            420
    300 % C++                             10             48              5            201
    301 % Lua                              2             31              4            126
    302 % Java                             4              5              0             80
    303 % Go                               2             11              9             40
    304 % -------------------------------------------------------------------------------
    305 % SUM:                           290          13175           3400          27776
    306 % -------------------------------------------------------------------------------
    307 The \CFA tests are 290+ files and 27,000+ lines of code.
    308 The tests illustrate syntactic and semantic features in \CFA, plus a growing number of runtime benchmarks.
    309 The tests check for correctness and are used for daily regression testing of 3800+ commits.
     242All features discussed in this paper are working, unless otherwise stated as under construction.
    310243
    311244Finally, it is impossible to describe a programming language without usages before definitions.
     
    328261There are only two hard things in Computer Science: cache invalidation and \emph{naming things} -- Phil Karlton
    329262\end{quote}
    330 \vspace{-9pt}
    331263C already has a limited form of ad-hoc polymorphism in the form of its basic arithmetic operators, which apply to a variety of different types using identical syntax.
    332264\CFA extends the built-in operator overloading by allowing users to define overloads for any function, not just operators, and even any variable;
     
    334266Code generation for these overloaded functions and variables is implemented by the usual approach of mangling the identifier names to include a representation of their type, while \CFA decides which overload to apply based on the same ``usual arithmetic conversions'' used in C to disambiguate operator overloads.
    335267As an example:
    336 \begin{cfa}
    337 int max = 2147483647;                                           $\C[4in]{// (1)}$
    338 double max = 1.7976931348623157E+308;           $\C{// (2)}$
     268
     269\begin{cfa}
     270int max = 2147483647;                                   $\C[4in]{// (1)}$
     271double max = 1.7976931348623157E+308;   $\C{// (2)}$
    339272int max( int a, int b ) { return a < b ? b : a; }  $\C{// (3)}$
    340273double max( double a, double b ) { return a < b ? b : a; }  $\C{// (4)}\CRT$
    341 max( 7, -max );                                         $\C{// uses (3) and (1), by matching int from constant 7}$
     274max( 7, -max );                                         $\C[2.75in]{// uses (3) and (1), by matching int from constant 7}$
    342275max( max, 3.14 );                                       $\C{// uses (4) and (2), by matching double from constant 3.14}$
    343 max( max, -max );                                       $\C{// ERROR, ambiguous}$
    344 int m = max( max, -max );                       $\C{// uses (3) and (1) twice, by matching return type}$
     276max( max, -max );                                       $\C{// ERROR: ambiguous}$
     277int m = max( max, -max );                       $\C{// uses (3) and (1) twice, by matching return type}\CRT$
    345278\end{cfa}
    346279
     
    351284As is shown later, there are a number of situations where \CFA takes advantage of available type information to disambiguate, where other programming languages generate ambiguities.
    352285
    353 \Celeven added @_Generic@ expressions~\cite[\S~6.5.1.1]{C11}, which is used with preprocessor macros to provide ad-hoc polymorphism;
     286\Celeven added @_Generic@ expressions, which is used in preprocessor macros to provide a form of ad-hoc polymorphism;
    354287however, this polymorphism is both functionally and ergonomically inferior to \CFA name overloading.
    355288The macro wrapping the generic expression imposes some limitations;
    356289\eg, it cannot implement the example above, because the variables @max@ are ambiguous with the functions @max@.
    357290Ergonomic limitations of @_Generic@ include the necessity to put a fixed list of supported types in a single place and manually dispatch to appropriate overloads, as well as possible namespace pollution from the dispatch functions, which must all have distinct names.
    358 \CFA supports @_Generic@ expressions for backwards compatibility, but it is an unnecessary mechanism. \TODO{actually implement that}
     291For backwards compatibility, \CFA supports @_Generic@ expressions, but it is an unnecessary mechanism. \TODO{actually implement that}
    359292
    360293% http://fanf.livejournal.com/144696.html
     
    384317\begin{cfa}
    385318forall( otype T `| { T ?+?(T, T); }` ) T twice( T x ) { return x `+` x; }  $\C{// ? denotes operands}$
    386 int val = twice( twice( 3.7 ) );  $\C{// val == 14}$
     319int val = twice( twice( 3.7 ) );
    387320\end{cfa}
    388321which works for any type @T@ with a matching addition operator.
    389322The polymorphism is achieved by creating a wrapper function for calling @+@ with @T@ bound to @double@, then passing this function to the first call of @twice@.
    390323There is now the option of using the same @twice@ and converting the result to @int@ on assignment, or creating another @twice@ with type parameter @T@ bound to @int@ because \CFA uses the return type~\cite{Cormack81,Baker82,Ada} in its type analysis.
    391 The first approach has a late conversion from @double@ to @int@ on the final assignment, while the second has an early conversion to @int@.
     324The first approach has a late conversion from @double@ to @int@ on the final assignment, while the second has an eager conversion to @int@.
    392325\CFA minimizes the number of conversions and their potential to lose information, so it selects the first approach, which corresponds with C-programmer intuition.
    393326
     
    396329A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted float array:
    397330\begin{cfa}
    398 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size,
    399                                 int (* compar)( const void *, const void * ));
     331void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (* compar)( const void *, const void * ));
    400332int comp( const void * t1, const void * t2 ) {
    401333         return *(double *)t1 < *(double *)t2 ? -1 : *(double *)t2 < *(double *)t1 ? 1 : 0;
     
    423355
    424356\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations (see Section~\ref{sec:libraries}).
    425 For example, it is possible to write a type-safe \CFA wrapper @malloc@ based on the C @malloc@, where the return type supplies the type/size of the allocation, which is impossible in most type systems.
     357For example, it is possible to write a type-safe \CFA wrapper @malloc@ based on the C @malloc@:
    426358\begin{cfa}
    427359forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    428 // select type and size from left-hand side
    429 int * ip = malloc();  double * dp = malloc();  struct S {...} * sp = malloc();
    430 \end{cfa}
     360int * ip = malloc();                                            $\C{// select type and size from left-hand side}$
     361double * dp = malloc();
     362struct S {...} * sp = malloc();
     363\end{cfa}
     364where the return type supplies the type/size of the allocation, which is impossible in most type systems.
    431365
    432366Call-site inferencing and nested functions provide a localized form of inheritance.
     
    435369\begin{cfa}
    436370forall( otype T | { int ?<?( T, T ); } ) void qsort( const T * arr, size_t size ) { /* use C qsort */ }
    437 int main() {
     371{
    438372        int ?<?( double x, double y ) { return x `>` y; } $\C{// locally override behaviour}$
    439         qsort( vals, 10 );                                                      $\C{// descending sort}$
    440 }
    441 \end{cfa}
    442 The local version of @?<?@ performs @?>?@ overriding the built-in @?<?@ so it is passed to @qsort@.
     373        qsort( vals, size );                                    $\C{// descending sort}$
     374}
     375\end{cfa}
     376Within the block, the nested version of @?<?@ performs @?>?@ and this local version overrides the built-in @?<?@ so it is passed to @qsort@.
    443377Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types.
    444378
    445 To reduce duplication, it is possible to distribute a group of @forall@ (and storage-class qualifiers) over functions/types, so each block declaration is prefixed by the group (see example in Appendix~\ref{s:CforallStack}).
    446 \begin{cfa}
    447 forall( otype `T` ) {                                                   $\C{// distribution block, add forall qualifier to declarations}$
     379Under construction is a mechanism to distribute @forall@ over routines/types, where each block declaration is prefixed with the initial @forall@ clause significantly reducing duplication (see @stack@ examples in Section~\ref{sec:eval}):
     380\begin{cfa}
     381forall( otype `T` ) {                                                   $\C{// forall block}$
    448382        struct stack { stack_node(`T`) * head; };       $\C{// generic type}$
    449         inline {                                                                        $\C{// nested distribution block, add forall/inline to declarations}$
    450                 void push( stack(`T`) & s, `T` value ) ...      $\C{// generic operations}$
    451         }
    452 }
    453 \end{cfa}
    454 
    455 
    456 \vspace*{-2pt}
     383        void push( stack(`T`) & s, `T` value ) ...      $\C{// generic operations}$
     384        T pop( stack(`T`) & s ) ...
     385}
     386\end{cfa}
     387
     388
    457389\subsection{Traits}
    458390
    459391\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:
    460 
    461 \begin{cquote}
    462 \lstDeleteShortInline@%
    463 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}}
    464 \begin{cfa}
    465 trait `sumable`( otype T ) {
    466         void `?{}`( T &, zero_t ); // 0 literal constructor
    467         T ?+?( T, T );                   // assortment of additions
    468         T `?+=?`( T &, T );
    469         T ++?( T & );
    470         T ?++( T & );
     392\begin{cfa}
     393trait `summable`( otype T ) {
     394        void ?{}( T *, zero_t );                                $\C{// constructor from 0 literal}$
     395        T ?+?( T, T );                                                  $\C{// assortment of additions}$
     396        T ?+=?( T *, T );
     397        T ++?( T * );
     398        T ?++( T * );
    471399};
    472 \end{cfa}
    473 &
    474 \begin{cfa}
    475 forall( otype T `| sumable( T )` ) // use trait
    476 T sum( T a[$\,$], size_t size ) {
    477         `T` total = { `0` };  // initialize by 0 constructor
    478         for ( size_t i = 0; i < size; i += 1 )
    479                 total `+=` a[i]; // select appropriate +
     400forall( otype T `| summable( T )` ) T sum( T a[$\,$], size_t size ) {$\C{// use trait}$
     401        `T` total = { `0` };                                    $\C{// instantiate T from 0 by calling its constructor}$
     402        for ( unsigned int i = 0; i < size; i += 1 ) total `+=` a[i]; $\C{// select appropriate +}$
    480403        return total;
    481404}
    482405\end{cfa}
    483 \end{tabular}
    484 \lstMakeShortInline@%
    485 \end{cquote}
    486 
    487 Note, the @sumable@ trait does not include a copy constructor needed for the right side of @?+=?@ and return;
    488 it is provided by @otype@, which is syntactic sugar for the following trait:
     406
     407In fact, the set of @summable@ trait operators is incomplete, as it is missing assignment for type @T@, but @otype@ is syntactic sugar for the following implicit trait:
    489408\begin{cfa}
    490409trait otype( dtype T | sized(T) ) {  // sized is a pseudo-trait for types with known size and alignment
     
    504423Instead, each polymorphic function (or generic type) defines the structural type needed for its execution (polymorphic type-key), and this key is fulfilled at each call site from the lexical environment, which is similar to Go~\cite{Go} interfaces.
    505424Hence, new lexical scopes and nested functions are used extensively to create local subtypes, as in the @qsort@ example, without having to manage a nominal-inheritance hierarchy.
    506 % (Nominal inheritance can be approximated with traits using marker variables or functions, as is done in Go.)
     425(Nominal inheritance can be approximated with traits using marker variables or functions, as is done in Go.)
    507426
    508427% Nominal inheritance can be simulated with traits using marker variables or functions:
     
    543462
    544463\CC, Java, and other languages use \newterm{generic types} to produce type-safe abstract data-types.
    545 \CFA generic types integrate efficiently and naturally with the existing polymorphic functions, while retaining backwards compatibility with C and providing separate compilation.
     464\CFA also implements generic types that integrate efficiently and naturally with the existing polymorphic functions, while retaining backwards compatibility with C and providing separate compilation.
    546465However, for known concrete parameters, the generic-type definition can be inlined, like \CC templates.
    547466
    548467A 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:
    549 \begin{cquote}
    550 \lstDeleteShortInline@%
    551 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    552 \begin{cfa}
    553 `forall( otype R, otype S )` struct pair {
    554         R first;        S second;
     468\begin{cfa}
     469forall( otype R, otype S ) struct pair {
     470        R first;
     471        S second;
    555472};
    556 `forall( otype T )` // dynamic
    557 T value( pair(const char *, T) p ) { return p.second; }
    558 `forall( dtype F, otype T )` // dtype-static (concrete)
    559 T value( pair(F *, T * ) p) { return *p.second; }
    560 \end{cfa}
    561 &
    562 \begin{cfa}
    563 pair(const char *, int) p = {"magic", 42}; // concrete
     473forall( otype T ) T value( pair( const char *, T ) p ) { return p.second; } $\C{// dynamic}$
     474forall( dtype F, otype T ) T value( pair( F *, T * ) p ) { return *p.second; } $\C{// dtype-static (concrete)}$
     475
     476pair( const char *, int ) p = { "magic", 42 }; $\C{// concrete}$
    564477int i = value( p );
    565 pair(void *, int *) q = { 0, &p.second }; // concrete
     478pair( void *, int * ) q = { 0, &p.second }; $\C{// concrete}$
    566479i = value( q );
    567480double d = 1.0;
    568 pair(double *, double *) r = { &d, &d }; // concrete
     481pair( double *, double * ) r = { &d, &d }; $\C{// concrete}$
    569482d = value( r );
    570483\end{cfa}
    571 \end{tabular}
    572 \lstMakeShortInline@%
    573 \end{cquote}
    574484
    575485\CFA classifies generic types as either \newterm{concrete} or \newterm{dynamic}.
    576486Concrete types have a fixed memory layout regardless of type parameters, while dynamic types vary in memory layout depending on their type parameters.
    577487A \newterm{dtype-static} type has polymorphic parameters but is still concrete.
    578 Polymorphic pointers are an example of dtype-static types;
    579 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.
     488Polymorphic pointers are an example of dtype-static types, \eg @forall(dtype T) T *@ is a polymorphic type, but for any @T@, @T *@  is a fixed-sized pointer, and therefore, can be represented by a @void *@ in code generation.
    580489
    581490\CFA generic types also allow checked argument-constraints.
     
    594503\begin{cfa}
    595504struct _pair_conc0 {
    596         const char * first;  int second;
     505        const char * first;
     506        int second;
    597507};
    598508\end{cfa}
     
    602512\begin{cfa}
    603513struct _pair_conc1 {
    604         void * first, * second;
     514        void * first;
     515        void * second;
    605516};
    606517\end{cfa}
     
    657568Another useful pattern enabled by reused dtype-static type instantiations is zero-cost \newterm{tag-structures}.
    658569Sometimes information is only used for type-checking and can be omitted at runtime, \eg:
    659 \begin{cquote}
    660 \lstDeleteShortInline@%
    661 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    662570\begin{cfa}
    663571forall( dtype Unit ) struct scalar { unsigned long value; };
    664572struct metres {};
    665573struct litres {};
     574
    666575forall( dtype U ) scalar(U) ?+?( scalar(U) a, scalar(U) b ) {
    667576        return (scalar(U)){ a.value + b.value };
    668577}
    669 \end{cfa}
    670 &
    671 \begin{cfa}
    672 scalar(metres) half_marathon = { 21_098 };
    673 scalar(litres) pool = { 2_500_000 };
    674 scalar(metres) marathon = half_marathon +
    675                                                         half_marathon;
    676 scalar(litres) two_pools = pool + pool;
    677 `marathon + pool;`      // ERROR, mismatched types
    678 \end{cfa}
    679 \end{tabular}
    680 \lstMakeShortInline@%
    681 \end{cquote}
     578scalar(metres) half_marathon = { 21_093 };
     579scalar(litres) swimming_pool = { 2_500_000 };
     580scalar(metres) marathon = half_marathon + half_marathon;
     581scalar(litres) two_pools = swimming_pool + swimming_pool;
     582marathon + swimming_pool;                                       $\C{// compilation ERROR}$
     583\end{cfa}
    682584@scalar@ is a dtype-static type, so all uses have a single structure definition, containing @unsigned long@, and can share the same implementations of common functions like @?+?@.
    683585These implementations may even be separately compiled, unlike \CC template functions.
     
    961863}
    962864\end{cfa}
    963 One more step permits the summation of any sumable type with all arguments of the same type:
    964 \begin{cfa}
    965 trait sumable( otype T ) {
     865One more step permits the summation of any summable type with all arguments of the same type:
     866\begin{cfa}
     867trait summable( otype T ) {
    966868        T ?+?( T, T );
    967869};
    968 forall( otype R | sumable( R ) ) R sum( R x, R y ) {
     870forall( otype R | summable( R ) ) R sum( R x, R y ) {
    969871        return x + y;
    970872}
    971 forall( otype R, ttype Params | sumable(R) | { R sum(R, Params); } ) R sum(R x, R y, Params rest) {
     873forall( otype R, ttype Params | summable(R) | { R sum(R, Params); } ) R sum(R x, R y, Params rest) {
    972874        return sum( x + y, rest );
    973875}
     
    1020922\begin{cfa}
    1021923forall( dtype T0, dtype T1 | sized(T0) | sized(T1) ) struct _tuple2 {
    1022         T0 field_0;  T1 field_1;                                        $\C{// generated before the first 2-tuple}$
     924        T0 field_0;                                                             $\C{// generated before the first 2-tuple}$
     925        T1 field_1;
    1023926};
    1024927_tuple2(int, int) f() {
    1025928        _tuple2(double, double) x;
    1026929        forall( dtype T0, dtype T1, dtype T2 | sized(T0) | sized(T1) | sized(T2) ) struct _tuple3 {
    1027                 T0 field_0;  T1 field_1;  T2 field_2;   $\C{// generated before the first 3-tuple}$
     930                T0 field_0;                                                     $\C{// generated before the first 3-tuple}$
     931                T1 field_1;
     932                T2 field_2;
    1028933        };
    1029934        _tuple3(int, double, int) y;
    1030935}
    1031936\end{cfa}
    1032 Tuple expressions are then converted directly into compound literals, \eg @[5, 'x', 1.24]@ becomes @(_tuple3(int, char,@ @double)){ 5, 'x', 1.24 }@.
     937{\sloppy
     938Tuple expressions are then simply converted directly into compound literals, \eg @[5, 'x', 1.24]@ becomes @(_tuple3(int, char, double)){ 5, 'x', 1.24 }@.
     939\par}%
    1033940
    1034941\begin{comment}
     
    11151022\lstDeleteShortInline@%
    11161023\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    1117 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1024\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    11181025\begin{cfa}
    11191026case 2, 10, 34, 42:
     
    11301037\lstDeleteShortInline@%
    11311038\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    1132 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1039\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    11331040\begin{cfa}
    11341041case 2~42:
     
    11831090\centering
    11841091\lstDeleteShortInline@%
    1185 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    1186 \multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1092\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     1093\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    11871094\begin{cfa}
    11881095`choose` ( day ) {
    11891096  case Mon~Thu:  // program
    11901097
    1191   case Fri:    // program
     1098  case Fri:  // program
    11921099        wallet += pay;
    11931100        `fallthrough;`
    1194   case Sat:   // party
     1101  case Sat:  // party
    11951102        wallet -= party;
    11961103
    11971104  case Sun:  // rest
    11981105
    1199   default:    // print error
     1106  default:  // error
    12001107}
    12011108\end{cfa}
     
    12051112  case Mon: case Tue: case Wed: case Thu:  // program
    12061113        `break;`
    1207   case Fri:    // program
     1114  case Fri:  // program
    12081115        wallet += pay;
    12091116
    1210   case Sat:   // party
     1117  case Sat:  // party
    12111118        wallet -= party;
    12121119        `break;`
    12131120  case Sun:  // rest
    12141121        `break;`
    1215   default:    // print error
     1122  default:  // error
    12161123}
    12171124\end{cfa}
     
    12291136\centering
    12301137\lstDeleteShortInline@%
    1231 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    1232 \multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{non-terminator}}       & \multicolumn{1}{c@{}}{\textbf{target label}}  \\
     1138\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     1139\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{non-terminator}}  & \multicolumn{1}{c}{\textbf{target label}}     \\
    12331140\begin{cfa}
    12341141choose ( ... ) {
     
    12731180\begin{figure}
    12741181\lstDeleteShortInline@%
    1275 \begin{tabular}{@{\hspace{\parindentlnth}}l|@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
    1276 \multicolumn{1}{@{\hspace{\parindentlnth}}c|@{\hspace{\parindentlnth}}}{\textbf{\CFA}}  & \multicolumn{1}{@{\hspace{\parindentlnth}}c@{}}{\textbf{C}}   \\
     1182\begin{tabular}{@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
     1183\multicolumn{1}{@{\hspace{\parindentlnth}}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}   & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
    12771184\begin{cfa}
    12781185`LC:` {
     
    12821189                `LIF:` if ( ... ) {
    12831190                        `LF:` for ( ... ) {
    1284                                 ... break `LC`; ...
    1285                                 ... break `LS`; ...
    1286                                 ... break `LIF`; ...
    1287                                 ... continue `LF;` ...
    1288                                 ... break `LF`; ...
     1191                                `LW:` while ( ... ) {
     1192                                        ... break `LC`; ...
     1193                                        ... break `LS`; ...
     1194                                        ... break `LIF`; ...
     1195                                        ... continue `LF;` ...
     1196                                        ... break `LF`; ...
     1197                                        ... continue `LW`; ...
     1198                                        ... break `LW`; ...
     1199                                } // while
    12891200                        } // for
    12901201                } else {
     
    13021213                if ( ... ) {
    13031214                        for ( ... ) {
    1304                                 ... goto `LC`; ...
    1305                                 ... goto `LS`; ...
    1306                                 ... goto `LIF`; ...
    1307                                 ... goto `LFC`; ...
    1308                                 ... goto `LFB`; ...
     1215                                while ( ... ) {
     1216                                        ... goto `LC`; ...
     1217                                        ... goto `LS`; ...
     1218                                        ... goto `LIF`; ...
     1219                                        ... goto `LFC`; ...
     1220                                        ... goto `LFB`; ...
     1221                                        ... goto `LWC`; ...
     1222                                        ... goto `LWB`; ...
     1223                                  `LWC`: ; } `LWB:` ;
    13091224                          `LFC:` ; } `LFB:` ;
    13101225                } else {
     
    13281243// continue loop
    13291244// terminate loop
     1245// continue loop
     1246// terminate loop
    13301247
    13311248
    13321249
    13331250// terminate if
     1251
     1252
    13341253
    13351254\end{cfa}
     
    13581277\subsection{Exception Handling}
    13591278
    1360 The following framework for \CFA exception-handling is in place, excluding some runtime type-information and virtual functions.
     1279The following framework for \CFA exception handling is in place, excluding some runtime type-information and virtual functions.
    13611280\CFA provides two forms of exception handling: \newterm{fix-up} and \newterm{recovery} (see Figure~\ref{f:CFAExceptionHandling})~\cite{Buhr92b,Buhr00a}.
    13621281Both mechanisms provide dynamic call to a handler using dynamic name-lookup, where fix-up has dynamic return and recovery has static return from the handler.
     
    13691288\begin{cquote}
    13701289\lstDeleteShortInline@%
    1371 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    1372 \multicolumn{1}{@{}c|@{\hspace{\parindentlnth}}}{\textbf{Resumption}}   & \multicolumn{1}{c@{}}{\textbf{Termination}}   \\
     1290\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     1291\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{Resumption}}      & \multicolumn{1}{c}{\textbf{Termination}}      \\
    13731292\begin{cfa}
    13741293`exception R { int fix; };`
     
    14361355resume( $\emph{alternate-stack}$ )
    14371356\end{cfa}
    1438 These overloads of @resume@ raise the specified exception or the currently propagating exception (reresume) at another \CFA coroutine or task~\cite{Delisle18}.
     1357These overloads of @resume@ raise the specified exception or the currently propagating exception (reresume) at another \CFA coroutine or task\footnote{\CFA coroutine and concurrency features are discussed in a separately submitted paper.}~\cite{Delisle18}.
    14391358Nonlocal raise is restricted to resumption to provide the exception handler the greatest flexibility because processing the exception does not unwind its stack, allowing it to continue after the handler returns.
    14401359
     
    14861405If an exception is raised and caught, the handler is run before the finally clause.
    14871406Like a destructor (see Section~\ref{s:ConstructorsDestructors}), a finally clause can raise an exception but not if there is an exception being propagated.
    1488 Mimicking the @finally@ clause with mechanisms like RAII is non-trivial when there are multiple types and local accesses.
     1407Mimicking the @finally@ clause with mechanisms like RAII is non-trivially when there are multiple types and local accesses.
    14891408
    14901409
     
    14921411\label{s:WithStatement}
    14931412
    1494 Heterogeneous data is often aggregated into a structure/union.
    1495 To reduce syntactic noise, \CFA provides a @with@ statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate field-qualification by opening a scope containing the field identifiers.
    1496 \begin{cquote}
    1497 \vspace*{-\baselineskip}%???
    1498 \lstDeleteShortInline@%
    1499 \begin{cfa}
    1500 struct S { char c; int i; double d; };
     1413Grouping heterogeneous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers:
     1414\begin{cfa}
     1415struct S {                                                                      $\C{// aggregate}$
     1416        char c;                                                                 $\C{// fields}$
     1417        int i;
     1418        double d;
     1419};
     1420S s, as[10];
     1421\end{cfa}
     1422However, functions manipulating aggregates must repeat the aggregate name to access its containing fields:
     1423\begin{cfa}
     1424void f( S s ) {
     1425        `s.`c; `s.`i; `s.`d;                                    $\C{// access containing fields}$
     1426}
     1427\end{cfa}
     1428which extends to multiple levels of qualification for nested aggregates.
     1429A similar situation occurs in object-oriented programming, \eg \CC:
     1430\begin{C++}
     1431struct S {
     1432        char c;                                                                 $\C{// fields}$
     1433        int i;
     1434        double d;
     1435        void f() {                                                              $\C{// implicit ``this'' aggregate}$
     1436                `this->`c; `this->`i; `this->`d;        $\C{// access containing fields}$
     1437        }
     1438}
     1439\end{C++}
     1440Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping.
     1441However, for other aggregate parameters, qualification is necessary:
     1442\begin{cfa}
    15011443struct T { double m, n; };
    1502 // multiple aggregate parameters
    1503 \end{cfa}
    1504 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}}
    1505 \begin{cfa}
    1506 void f( S & s, T & t ) {
    1507         `s.`c; `s.`i; `s.`d;
    1508         `t.`m; `t.`n;
    1509 }
    1510 \end{cfa}
    1511 &
    1512 \begin{cfa}
    1513 void f( S & s, T & t ) `with ( s, t )` {
    1514         c; i; d;                // no qualification
    1515         m; n;
    1516 }
    1517 \end{cfa}
    1518 \end{tabular}
    1519 \lstMakeShortInline@%
    1520 \end{cquote}
    1521 Object-oriented programming languages only provide implicit qualification for the receiver.
     1444int S::f( T & t ) {                                                     $\C{// multiple aggregate parameters}$
     1445        c; i; d;                                                                $\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}$
     1446        `t.`m; `t.`n;                                                   $\C{// must qualify}$
     1447}
     1448\end{cfa}
     1449
     1450To simplify the programmer experience, \CFA provides a @with@ statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers.
     1451Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block.
     1452\begin{cfa}
     1453void f( S & this ) `with ( this )` {            $\C{// with statement}$
     1454        c; i; d;                                                                $\C{\color{red}// this.c, this.i, this.d}$
     1455}
     1456\end{cfa}
     1457with the generality of opening multiple aggregate-parameters:
     1458\begin{cfa}
     1459void f( S & s, T & t ) `with ( s, t )` {                $\C{// multiple aggregate parameters}$
     1460        c; i; d;                                                                $\C{\color{red}// s.c, s.i, s.d}$
     1461        m; n;                                                                   $\C{\color{red}// t.m, t.n}$
     1462}
     1463\end{cfa}
    15221464
    15231465In detail, the @with@ statement has the form:
     
    15321474The object is the implicit qualifier for the open structure-fields.
    15331475
    1534 All expressions in the expression list are open in parallel within the compound statement, which is different from Pascal, which nests the openings from left to right.
     1476All expressions in the expression list are open in parallel within the compound statement.
     1477This semantic is different from Pascal, which nests the openings from left to right.
    15351478The difference between parallel and nesting occurs for fields with the same name and type:
    15361479\begin{cfa}
     
    15391482with ( s, t ) {
    15401483        j + k;                                                                  $\C{// unambiguous, s.j + t.k}$
    1541         m = 5.0;                                                                $\C{// unambiguous, s.m = 5.0}$
    1542         m = 1;                                                                  $\C{// unambiguous, t.m = 1}$
    1543         int a = m;                                                              $\C{// unambiguous, a = t.m }$
    1544         double b = m;                                                   $\C{// unambiguous, b = s.m}$
     1484        m = 5.0;                                                                $\C{// unambiguous, t.m = 5.0}$
     1485        m = 1;                                                                  $\C{// unambiguous, s.m = 1}$
     1486        int a = m;                                                              $\C{// unambiguous, a = s.i }$
     1487        double b = m;                                                   $\C{// unambiguous, b = t.m}$
    15451488        int c = s.i + t.i;                                              $\C{// unambiguous, qualification}$
    1546         (double)m;                                                              $\C{// unambiguous, cast s.m}$
     1489        (double)m;                                                              $\C{// unambiguous, cast}$
    15471490}
    15481491\end{cfa}
     
    15681511and implicitly opened \emph{after} a function-body open, to give them higher priority:
    15691512\begin{cfa}
    1570 void ?{}( S & s, int `i` ) with ( s ) `{` `with( $\emph{\color{red}params}$ )` {
     1513void ?{}( S & s, int `i` ) with ( s ) `with( $\emph{\color{red}params}$ )` {
    15711514        s.i = `i`; j = 3; m = 5.5;
    1572 } `}`
     1515}
    15731516\end{cfa}
    15741517Finally, a cast may be used to disambiguate among overload variables in a @with@ expression:
     
    16381581\lstDeleteShortInline@%
    16391582\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{}}
    1640 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1583\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    16411584\begin{cfa}
    16421585`[5] *` int x1;
     
    16661609\lstDeleteShortInline@%
    16671610\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    1668 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1611\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    16691612\begin{cfa}
    16701613`*` int x, y;
    1671 int z;
    1672 \end{cfa}
    1673 &
    1674 \begin{cfa}
    1675 int `*`x, `*`y, z;
     1614int y;
     1615\end{cfa}
     1616&
     1617\begin{cfa}
     1618int `*`x, `*`y;
    16761619
    16771620\end{cfa}
     
    16791622\lstMakeShortInline@%
    16801623\end{cquote}
    1681 % The downside of the \CFA semantics is the need to separate regular and pointer declarations.
    1682 The separation of regular and pointer declarations by \CFA declarations enforces greater clarity with only slightly more syntax.
     1624The downside of the \CFA semantics is the need to separate regular and pointer declarations.
    16831625
    16841626\begin{comment}
     
    16871629\lstDeleteShortInline@%
    16881630\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{}}
    1689 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{C}}     \\
     1631\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{C}}     \\
    16901632\begin{cfa}
    16911633[ 5 ] int z;
     
    17291671\lstDeleteShortInline@%
    17301672\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{}}
    1731 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{C}}     \\
     1673\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{C}}     \\
    17321674\begin{cfa}
    17331675extern const * const int x;
     
    17541696\lstDeleteShortInline@%
    17551697\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    1756 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1698\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    17571699\begin{cfa}
    17581700y = (* int)x;
     
    17821724\lstDeleteShortInline@%
    17831725\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    1784 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     1726\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    17851727\begin{cfa}
    17861728[double] foo(), foo( int ), foo( double ) {...}
     
    18001742* [ * int ] ( int y ) gp;               $\C{// pointer to function returning pointer to int with int parameter}$
    18011743* [ ] ( int, char ) hp;                 $\C{// pointer to function returning no result with int and char parameters}$
    1802 * [ * int, int ] ( int ) jp;    $\C{// pointer to function returning pointer to int and int with int parameter}\CRT$
    1803 \end{cfa}
    1804 Note, the name of the function pointer is specified last, as for other variable declarations.
     1744* [ * int, int ] ( int ) jp;    $\C{// pointer to function returning pointer to int and int with int parameter}$
     1745\end{cfa}
     1746Note, a function name cannot be specified:
     1747\begin{cfa}
     1748* [ int x ] f () fp;                    $\C{// function name "f" is disallowed}\CRT$
     1749\end{cfa}
    18051750
    18061751Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration.
     
    18591804This provides a much more orthogonal design for library implementors, obviating the need for workarounds such as @std::reference_wrapper@.
    18601805Secondly, \CFA references are rebindable, whereas \CC references have a fixed address.
     1806\newsavebox{\LstBox}
     1807\begin{lrbox}{\LstBox}
     1808\lstset{basicstyle=\footnotesize\linespread{0.9}\sf}
     1809\begin{cfa}
     1810int & r = *new( int );
     1811...                                                                                     $\C{// non-null reference}$
     1812delete &r;                                                                      $\C{// unmanaged (programmer) memory-management}$
     1813r += 1;                                                                         $\C{// undefined reference}$
     1814\end{cfa}
     1815\end{lrbox}
    18611816Rebinding allows \CFA references to be default-initialized (\eg to a null pointer\footnote{
    1862 While effort has been made into non-null reference checking in \CC and Java, the exercise seems moot for any non-managed languages (C/\CC), given that it only handles one of many different error situations, \eg using a pointer after its storage is deleted.}) and point to different addresses throughout their lifetime, like pointers.
     1817While effort has been made into non-null reference checking in \CC and Java, the exercise seems moot for any non-managed languages (C/\CC), given that it only handles one of many different error situations:
     1818\begin{cquote}
     1819\usebox{\LstBox}
     1820\end{cquote}
     1821}%
     1822) and point to different addresses throughout their lifetime, like pointers.
    18631823Rebinding is accomplished by extending the existing syntax and semantics of the address-of operator in C.
    18641824
     
    18721832\begin{itemize}
    18731833\item
    1874 if @R@ is an rvalue of type {@T &@$_1 \cdots$@ &@$_r$} where $r \ge 1$ references (@&@ symbols) then @&R@ has type {@T `*`&@$_{\color{red}2} \cdots$@ &@$_{\color{red}r}$}, \\ \ie @T@ pointer with $r-1$ references (@&@ symbols).
     1834if @R@ is an rvalue of type {@T &@$_1 \cdots$@ &@$_r$} where $r \ge 1$ references (@&@ symbols) than @&R@ has type {@T `*`&@$_{\color{red}2} \cdots$@ &@$_{\color{red}r}$}, \\ \ie @T@ pointer with $r-1$ references (@&@ symbols).
    18751835       
    18761836\item
     
    19061866\end{cfa}
    19071867This allows complex values to be succinctly and efficiently passed to functions, without the syntactic overhead of explicit definition of a temporary variable or the runtime cost of pass-by-value.
    1908 \CC allows a similar binding, but only for @const@ references; the more general semantics of \CFA are an attempt to avoid the \newterm{const poisoning} problem~\cite{Taylor10}, in which addition of a @const@ qualifier to one reference requires a cascading chain of added qualifiers.
     1868\CC allows a similar binding, but only for @const@ references; the more general semantics of \CFA are an attempt to avoid the \newterm{const hell} problem, in which addition of a @const@ qualifier to one reference requires a cascading chain of added qualifiers.
    19091869
    19101870
     
    19201880\begin{tabular}{@{}l@{\hspace{3em}}l|l@{}}
    19211881\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}}      & \multicolumn{1}{c|}{\textbf{C Implicit Hoisting}}     & \multicolumn{1}{c}{\textbf{\CFA}}     \\
     1882\hline
    19221883\begin{cfa}
    19231884struct S {
     
    19991960The symbol \lstinline+^+ is used for the destructor name because it was the last binary operator that could be used in a unary context.}.
    20001961The name @{}@ comes from the syntax for the initializer: @struct S { int i, j; } s = `{` 2, 3 `}`@.
    2001 Like other \CFA operators, these names represent the syntax used to explicitly call the constructor or destructor, \eg @s{...}@ or @^s{...}@.
     1962Like other \CFA operators, these names represent the syntax used to call the constructor or destructor, \eg @?{}(x, ...)@ or @^{}(x, ...)@.
    20021963The constructor and destructor have return type @void@, and the first parameter is a reference to the object type to be constructed or destructed.
    20031964While the first parameter is informally called the @this@ parameter, as in object-oriented languages, any variable name may be used.
    2004 Both constructors and destructors allow additional parameters after the @this@ parameter for specifying values for initialization/de-initialization\footnote{
     1965Both constructors and destructors allow additional parametes after the @this@ parameter for specifying values for initialization/de-initialization\footnote{
    20051966Destruction parameters are useful for specifying storage-management actions, such as de-initialize but not deallocate.}.
    20061967\begin{cfa}
    2007 struct VLA { int len, * data; };                        $\C{// variable length array of integers}$
     1968struct VLA { int len, * data; };
    20081969void ?{}( VLA & vla ) with ( vla ) { len = 10;  data = alloc( len ); }  $\C{// default constructor}$
    20091970void ^?{}( VLA & vla ) with ( vla ) { free( data ); } $\C{// destructor}$
    20101971{
    2011         VLA x;                                                                  $\C{// implicit:\ \ x\{\};}$
    2012 }                                                                                       $\C{// implicit:\ \textasciicircum{}x\{\};}$
     1972        VLA x;                                                                  $\C{// implicit:  ?\{\}( x );}$
     1973}                                                                                       $\C{// implicit:  ?\^{}\{\}( x );}$
    20131974\end{cfa}
    20141975@VLA@ is a \newterm{managed type}\footnote{
     
    20351996appropriate care is taken to not recursively call the copy constructor when initializing the second parameter.
    20361997
    2037 \CFA constructors may be explicitly called, like Java, and destructors may be explicitly called, like \CC.
     1998\CFA constructors may be explicitly call, like Java, and destructors may be explicitly called, like \CC.
    20381999Explicit calls to constructors double as a \CC-style \emph{placement syntax}, useful for construction of member fields in user-defined constructors and reuse of existing storage allocations.
    2039 Like the other operators in \CFA, there is a concise syntax for constructor/destructor function calls:
     2000While existing call syntax works for explicit calls to constructors and destructors, \CFA also provides a more concise \newterm{operator syntax} for both:
    20402001\begin{cfa}
    20412002{
    20422003        VLA  x,            y = { 20, 0x01 },     z = y; $\C{// z points to y}$
    2043         //    x{};         y{ 20, 0x01 };          z{ z, y };
     2004        //      ?{}( x );   ?{}( y, 20, 0x01 );   ?{}( z, y );
    20442005        ^x{};                                                                   $\C{// deallocate x}$
    20452006        x{};                                                                    $\C{// reallocate x}$
     
    20482009        y{ x };                                                                 $\C{// reallocate y, points to x}$
    20492010        x{};                                                                    $\C{// reallocate x, not pointing to y}$
    2050         //  ^z{};  ^y{};  ^x{};
     2011        // ^?{}(z);  ^?{}(y);  ^?{}(x);
    20512012}
    20522013\end{cfa}
     
    20692030In these cases, \CFA provides the initialization syntax \lstinline|S x `@=` {}|, and the object becomes unmanaged, so implicit constructor and destructor calls are not generated.
    20702031Any C initializer can be the right-hand side of an \lstinline|@=| initializer, \eg \lstinline|VLA a @= { 0, 0x0 }|, with the usual C initialization semantics.
    2071 The same syntax can be used in a compound literal, \eg \lstinline|a = (VLA)`@`{ 0, 0x0 }|, to create a C-style literal.
     2032The same syntax can be used in a compound literal, \eg \lstinline|a = VLA`@`{ 0, 0x0 }|, to create a C-style literal.
    20722033The point of \lstinline|@=| is to provide a migration path from legacy C code to \CFA, by providing a mechanism to incrementally convert to implicit initialization.
    20732034
     
    20852046
    20862047
    2087 \begin{comment}
    20882048\subsection{Integral Suffixes}
    20892049
     
    21192079\lstMakeShortInline@%
    21202080\end{cquote}
    2121 \end{comment}
    21222081
    21232082
     
    21252084
    21262085In C, @0@ has the special property that it is the only ``false'' value;
    2127 by the standard, any value that compares equal to @0@ is false, while any value that compares unequal to @0@ is true.
     2086from the standard, any value that compares equal to @0@ is false, while any value that compares unequal to @0@ is true.
    21282087As such, an expression @x@ in any boolean context (such as the condition of an @if@ or @while@ statement, or the arguments to @&&@, @||@, or @?:@\,) can be rewritten as @x != 0@ without changing its semantics.
    21292088Operator overloading in \CFA provides a natural means to implement this truth-value comparison for arbitrary types, but the C type system is not precise enough to distinguish an equality comparison with @0@ from an equality comparison with an arbitrary integer or pointer.
     
    21602119\lstDeleteShortInline@%
    21612120\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{}}
    2162 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{postfix function}}     & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{constant}}      & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{variable/expression}}   & \multicolumn{1}{c@{}}{\textbf{postfix pointer}}       \\
     2121\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{postfix function}}        & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{constant}}      & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{variable/expression}}   & \multicolumn{1}{c}{\textbf{postfix pointer}}  \\
    21632122\begin{cfa}
    21642123int |?`h|( int s );
     
    22052164\lstset{language=CFA,moredelim=**[is][\color{red}]{|}{|},deletedelim=**[is][]{`}{`}}
    22062165\lstDeleteShortInline@%
    2207 \begin{tabular}{@{}l@{\hspace{1.25\parindentlnth}}l@{}}
    2208 \multicolumn{1}{@{}c@{\hspace{1.25\parindentlnth}}}{\textbf{\CFA}}      & \multicolumn{1}{c@{}}{\textbf{\CC}}   \\
     2166\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     2167\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{\CC}}      \\
    22092168\begin{cfa}
    22102169struct W {
     
    22502209        W w, heavy = { 20 };
    22512210        w = 155|_lb|;
    2252         // binary unsupported
     2211        w = 0b1111|_lb|;       // error, binary unsupported
    22532212        w = 0${\color{red}\LstBasicStyle{'}}$233|_lb|;          // quote separator
    22542213        w = 0x9b|_kg|;
     
    22802239\lstDeleteShortInline@%
    22812240\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2282 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}   & \multicolumn{1}{c@{}}{\textbf{Usage}} \\
     2241\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}      & \multicolumn{1}{c}{\textbf{Usage}}    \\
    22832242\begin{cfa}
    22842243const short int `MIN` = -32768;
     
    22982257\begin{cquote}
    22992258\lstDeleteShortInline@%
    2300 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
    2301 \multicolumn{1}{@{}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}  & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     2259\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     2260\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    23022261\begin{cfa}
    23032262MIN
     
    23082267&
    23092268\begin{cfa}
    2310 CHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN, DBL_MIN, LDBL_MIN
    2311 UCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX, FLT_MAX, DBL_MAX, LDBL_MAX
     2269SCHAR_MIN, CHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN, DBL_MIN, LDBL_MIN
     2270SCHAR_MAX, UCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX, FLT_MAX, DBL_MAX, LDBL_MAX
    23122271M_PI, M_PIl
    23132272M_E, M_El
     
    23252284\lstDeleteShortInline@%
    23262285\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2327 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}   & \multicolumn{1}{c@{}}{\textbf{Usage}} \\
     2286\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}      & \multicolumn{1}{c}{\textbf{Usage}}    \\
    23282287\begin{cfa}
    23292288float `log`( float x );
     
    23442303\lstDeleteShortInline@%
    23452304\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2346 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     2305\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    23472306\begin{cfa}
    23482307log
     
    23722331\lstDeleteShortInline@%
    23732332\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2374 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}   & \multicolumn{1}{c@{}}{\textbf{Usage}} \\
     2333\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{Definition}}      & \multicolumn{1}{c}{\textbf{Usage}}    \\
    23752334\begin{cfa}
    23762335unsigned int `abs`( int );
     
    23912350\lstDeleteShortInline@%
    23922351\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2393 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     2352\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
    23942353\begin{cfa}
    23952354abs
     
    24142373an allocation with a specified character.
    24152374\item[resize]
    2416 an existing allocation to decrease or increase its size.
     2375an existing allocation to decreased or increased its size.
    24172376In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied.
    24182377For an increase in storage size, new storage after the copied data may be filled.
     
    24282387
    24292388\begin{table}
    2430 \caption{Storage-Management Operations}
    2431 \label{t:StorageManagementOperations}
    24322389\centering
    24332390\lstDeleteShortInline@%
     
    24492406\lstDeleteShortInline~%
    24502407\lstMakeShortInline@%
     2408\caption{Storage-Management Operations}
     2409\label{t:StorageManagementOperations}
    24512410\end{table}
    24522411
    24532412\begin{figure}
    24542413\centering
    2455 \begin{cfa}[aboveskip=0pt,xleftmargin=0pt]
     2414\begin{cquote}
     2415\begin{cfa}[aboveskip=0pt]
    24562416size_t  dim = 10;                                                       $\C{// array dimension}$
    24572417char fill = '\xff';                                                     $\C{// initialization fill value}$
     
    24592419\end{cfa}
    24602420\lstDeleteShortInline@%
    2461 \begin{tabular}{@{}l@{\hspace{\parindentlnth}}l@{}}
    2462 \multicolumn{1}{@{}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}  & \multicolumn{1}{c@{}}{\textbf{C}}     \\
    2463 \begin{cfa}[xleftmargin=-10pt]
     2421\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
     2422\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{C}}        \\
     2423\begin{cfa}
    24642424ip = alloc();
    24652425ip = alloc( fill );
     
    24762436&
    24772437\begin{cfa}
    2478 ip = (int *)malloc( sizeof(int) );
    2479 ip = (int *)malloc( sizeof(int) ); memset( ip, fill, sizeof(int) );
    2480 ip = (int *)malloc( dim * sizeof(int) );
    2481 ip = (int *)malloc( sizeof(int) ); memset( ip, fill, dim * sizeof(int) );
    2482 ip = (int *)realloc( ip, 2 * dim * sizeof(int) );
    2483 ip = (int *)realloc( ip, 4 * dim * sizeof(int) ); memset( ip, fill, 4 * dim * sizeof(int));
    2484 
    2485 ip = memalign( 16, sizeof(int) );
    2486 ip = memalign( 16, sizeof(int) ); memset( ip, fill, sizeof(int) );
    2487 ip = memalign( 16, dim * sizeof(int) );
    2488 ip = memalign( 16, dim * sizeof(int) ); memset( ip, fill, dim * sizeof(int) );
     2438ip = (int *)malloc( sizeof( int ) );
     2439ip = (int *)malloc( sizeof( int ) ); memset( ip, fill, sizeof( int ) );
     2440ip = (int *)malloc( dim * sizeof( int ) );
     2441ip = (int *)malloc( sizeof( int ) ); memset( ip, fill, dim * sizeof( int ) );
     2442ip = (int *)realloc( ip, 2 * dim * sizeof( int ) );
     2443ip = (int *)realloc( ip, 4 * dim * sizeof( int ) ); memset( ip, fill, 4 * dim * sizeof( int ) );
     2444
     2445ip = memalign( 16, sizeof( int ) );
     2446ip = memalign( 16, sizeof( int ) ); memset( ip, fill, sizeof( int ) );
     2447ip = memalign( 16, dim * sizeof( int ) );
     2448ip = memalign( 16, dim * sizeof( int ) ); memset( ip, fill, dim * sizeof( int ) );
    24892449\end{cfa}
    24902450\end{tabular}
    24912451\lstMakeShortInline@%
     2452\end{cquote}
    24922453\caption{\CFA versus C Storage-Allocation}
    24932454\label{f:StorageAllocation}
     
    25022463S * as = anew( dim, 2, 3 );                                     $\C{// each array element initialized to 2, 3}$
    25032464\end{cfa}
    2504 Note, \CC can only initialize array elements via the default constructor.
     2465Note, \CC can only initialization array elements via the default constructor.
    25052466
    25062467Finally, the \CFA memory-allocator has \newterm{sticky properties} for dynamic storage: fill and alignment are remembered with an object's storage in the heap.
     
    25192480\lstDeleteShortInline@%
    25202481\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{}}
    2521 \multicolumn{1}{@{}c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{\CC}}   \\
     2482\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{c}{\textbf{\CC}}      \\
    25222483\begin{cfa}
    25232484int x = 1, y = 2, z = 3;
     
    25742535\end{cquote}
    25752536There is a weak similarity between the \CFA logical-or operator and the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output.
    2576 \begin{comment}
     2537
    25772538The implicit separator character (space/blank) is a separator not a terminator.
    25782539The rules for implicitly adding the separator are:
     
    25932554}%
    25942555\end{itemize}
    2595 \end{comment}
    25962556There are functions to set and get the separator string, and manipulators to toggle separation on and off in the middle of output.
    25972557
     
    26082568\centering
    26092569\lstDeleteShortInline@%
    2610 \begin{tabular}{@{}l@{\hspace{3\parindentlnth}}l@{}}
    2611 \multicolumn{1}{@{}c@{\hspace{3\parindentlnth}}}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}}     \\
     2570\begin{tabular}{@{}l@{\hspace{2\parindentlnth}}@{\hspace{2\parindentlnth}}l@{}}
     2571\multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{\CFA}}    & \multicolumn{1}{@{\hspace{2\parindentlnth}}c}{\textbf{C}}     \\
    26122572\begin{cfa}
    26132573#include <gmp>
     
    26422602
    26432603
    2644 \section{Polymorphism Evaluation}
     2604\section{Evaluation}
    26452605\label{sec:eval}
    26462606
    2647 \CFA adds parametric polymorphism to C.
    2648 A runtime evaluation is performed to compare the cost of alternative styles of polymorphism.
    2649 The goal is to compare just the underlying mechanism for implementing different kinds of polymorphism.
    2650 % Though \CFA provides significant added functionality over C, these features have a low runtime penalty.
    2651 % In fact, it is shown that \CFA's generic programming can enable faster runtime execution than idiomatic @void *@-based C code.
    2652 The experiment is a set of generic-stack micro-benchmarks~\cite{CFAStackEvaluation} in C, \CFA, and \CC (see implementations in Appendix~\ref{sec:BenchmarkStackImplementations}).
     2607Though \CFA provides significant added functionality over C, these features have a low runtime penalty.
     2608In fact, \CFA's features for generic programming can enable faster runtime execution than idiomatic @void *@-based C code.
     2609This claim is demonstrated through a set of generic-code-based micro-benchmarks in C, \CFA, and \CC (see stack implementations in Appendix~\ref{sec:BenchmarkStackImplementation}).
    26532610Since all these languages share a subset essentially comprising standard C, maximal-performance benchmarks should show little runtime variance, differing only in length and clarity of source code.
    26542611A more illustrative comparison measures the costs of idiomatic usage of each language's features.
     
    26812638\end{figure}
    26822639
    2683 The structure of each benchmark implemented is: C with @void *@-based polymorphism, \CFA with parametric polymorphism, \CC with templates, and \CC using only class inheritance for polymorphism, called \CCV.
     2640The structure of each benchmark implemented is: C with @void *@-based polymorphism, \CFA with the presented features, \CC with templates, and \CC using only class inheritance for polymorphism, called \CCV.
    26842641The \CCV variant illustrates an alternative object-oriented idiom where all objects inherit from a base @object@ class, mimicking a Java-like interface;
    26852642hence runtime checks are necessary to safely down-cast objects.
    26862643The most notable difference among the implementations is in memory layout of generic types: \CFA and \CC inline the stack and pair elements into corresponding list and pair nodes, while C and \CCV lack such a capability and instead must store generic objects via pointers to separately-allocated objects.
    2687 Note, the C benchmark uses unchecked casts as C has no runtime mechanism to perform such checks, while \CFA and \CC provide type-safety statically.
     2644Note that the C benchmark uses unchecked casts as there is no runtime mechanism to perform such checks, while \CFA and \CC provide type-safety statically.
    26882645
    26892646Figure~\ref{fig:eval} and Table~\ref{tab:eval} show the results of running the benchmark in Figure~\ref{fig:BenchmarkTest} and its C, \CC, and \CCV equivalents.
    26902647The graph plots the median of 5 consecutive runs of each program, with an initial warm-up run omitted.
    2691 All code is compiled at \texttt{-O2} by gcc or g++ 6.4.0, with all \CC code compiled as \CCfourteen.
     2648All code is compiled at \texttt{-O2} by gcc or g++ 6.3.0, with all \CC code compiled as \CCfourteen.
    26922649The benchmarks are run on an Ubuntu 16.04 workstation with 16 GB of RAM and a 6-core AMD FX-6300 CPU with 3.5 GHz maximum clock frequency.
    26932650
     
    27002657
    27012658\begin{table}
     2659\centering
    27022660\caption{Properties of benchmark code}
    27032661\label{tab:eval}
    2704 \centering
    27052662\newcommand{\CT}[1]{\multicolumn{1}{c}{#1}}
    27062663\begin{tabular}{rrrrr}
    27072664                                                                        & \CT{C}        & \CT{\CFA}     & \CT{\CC}      & \CT{\CCV}             \\ \hline
    27082665maximum memory usage (MB)                       & 10,001        & 2,502         & 2,503         & 11,253                \\
    2709 source code size (lines)                        & 201           & 191           & 125           & 294                   \\
     2666source code size (lines)                        & 196           & 186           & 125           & 290                   \\
    27102667redundant type annotations (lines)      & 27            & 0                     & 2                     & 16                    \\
    27112668binary size (KB)                                        & 14            & 257           & 14            & 37                    \\
     
    27152672The C and \CCV variants are generally the slowest with the largest memory footprint, because of their less-efficient memory layout and the pointer-indirection necessary to implement generic types;
    27162673this inefficiency is exacerbated by the second level of generic types in the pair benchmarks.
    2717 By contrast, the \CFA and \CC variants run in roughly equivalent time for both the integer and pair because of equivalent storage layout, with the inlined libraries (\ie no separate compilation) and greater maturity of the \CC compiler contributing to its lead.
     2674By contrast, the \CFA and \CC variants run in roughly equivalent time for both the integer and pair of @short@ and @char@ because the storage layout is equivalent, with the inlined libraries (\ie no separate compilation) and greater maturity of the \CC compiler contributing to its lead.
    27182675\CCV is slower than C largely due to the cost of runtime type-checking of down-casts (implemented with @dynamic_cast@);
    2719 The outlier for \CFA, pop @pair@, results from the complexity of the generated-C polymorphic code.
    2720 The gcc compiler is unable to optimize some dead code and condense nested calls;
    2721 a compiler designed for \CFA could easily perform these optimizations.
     2676The outlier in the graph for \CFA, pop @pair@, results from the complexity of the generated-C polymorphic code.
     2677The gcc compiler is unable to optimize some dead code and condense nested calls; a compiler designed for \CFA could easily perform these optimizations.
    27222678Finally, the binary size for \CFA is larger because of static linking with the \CFA libraries.
    27232679
     
    27312687Line-count is a fairly rough measure of code complexity;
    27322688another important factor is how much type information the programmer must specify manually, especially where that information is not compiler-checked.
    2733 Such unchecked type information produces a heavier documentation burden and increased potential for runtime bugs, and is much less common in \CFA than C, with its manually specified function pointer arguments and format codes, or \CCV, with its extensive use of un-type-checked downcasts, \eg @object@ to @integer@ when popping a stack.
     2689Such unchecked type information produces a heavier documentation burden and increased potential for runtime bugs, and is much less common in \CFA than C, with its manually specified function pointer arguments and format codes, or \CCV, with its extensive use of untype-checked downcasts, \eg @object@ to @integer@ when popping a stack.
    27342690To quantify this manual typing, the ``redundant type annotations'' line in Table~\ref{tab:eval} counts the number of lines on which the type of a known variable is respecified, either as a format specifier, explicit downcast, type-specific function, or by name in a @sizeof@, struct literal, or @new@ expression.
    27352691The \CC benchmark uses two redundant type annotations to create a new stack nodes, while the C and \CCV benchmarks have several such annotations spread throughout their code.
    27362692The \CFA benchmark is able to eliminate all redundant type annotations through use of the polymorphic @alloc@ function discussed in Section~\ref{sec:libraries}.
    27372693
    2738 We conjecture these results scale across most generic data-types as the underlying polymorphism implement is constant.
    2739 
    27402694
    27412695\section{Related Work}
     
    27432697
    27442698\subsection{Polymorphism}
    2745 
    2746 ML~\cite{ML} was the first language to support parametric polymorphism.
    2747 Like \CFA, it supports universal type parameters, but not the use of assertions and traits to constrain type arguments.
    2748 Haskell~\cite{Haskell10} combines ML-style polymorphism, polymorphic data types, and type inference with the notion of type classes, collections of overloadable methods that correspond in intent to traits in \CFA.
    2749 Unlike \CFA, Haskell requires an explicit association between types and their classes that specifies the implementation of operations.
    2750 These associations determine the functions that are assertion arguments for particular combinations of class and type, in contrast to \CFA where the assertion arguments are selected at function call sites based upon the set of operations in scope at that point.
    2751 Haskell also severely restricts the use of overloading: an overloaded name can only be associated with a single class, and methods with overloaded names can only be defined as part of instance declarations.
    27522699
    27532700\CC provides three disjoint polymorphic extensions to C: overloading, inheritance, and templates.
     
    28032750Go does not have tuples but supports MRVF.
    28042751Java's variadic functions appear similar to C's but are type-safe using homogeneous arrays, which are less useful than \CFA's heterogeneously-typed variadic functions.
    2805 Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML~\cite{sml}, Haskell, and Scala~\cite{Scala}, which decompose tuples using pattern matching.
     2752Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML~\cite{sml} and~\cite{Scala}, which decompose tuples using pattern matching.
    28062753
    28072754
     
    28202767data-parallel features have not yet been added to \CFA, but are easily incorporated within its design, while concurrency primitives similar to those in $\mu$\CC have already been added~\cite{Delisle18}.
    28212768Finally, CCured~\cite{Necula02} and Ironclad \CC~\cite{DeLozier13} attempt to provide a more memory-safe C by annotating pointer types with garbage collection information; type-checked polymorphism in \CFA covers several of C's memory-safety issues, but more aggressive approaches such as annotating all pointer types with their nullability or requiring runtime garbage collection are contradictory to \CFA's backwards compatibility goals.
     2769
     2770
     2771\begin{comment}
     2772\subsection{Control Structures / Declarations / Literals}
     2773
     2774Java has default fall through like C/\CC.
     2775Pascal/Ada/Go/Rust do not have default fall through.
     2776\Csharp does not have fall through but still requires a break.
     2777Python uses dictionary mapping. \\
     2778\CFA choose is like Rust match.
     2779
     2780Java has labelled break/continue. \\
     2781Languages with and without exception handling.
     2782
     2783Alternative C declarations. \\
     2784Different references \\
     2785Constructors/destructors
     2786
     27870/1 Literals \\
     2788user defined: D, Objective-C
     2789\end{comment}
    28222790
    28232791
     
    28342802Finally, we demonstrate that \CFA performance for some idiomatic cases is better than C and close to \CC, showing the design is practically applicable.
    28352803
    2836 While all examples in the paper compile and run, a public beta-release of \CFA will take 6--8 months to reduce compilation time, provide better debugging, and add a few more libraries.
    2837 There is also new work on a number of \CFA features, including arrays with size, runtime type-information, virtual functions, user-defined conversions, and modules.
    2838 While \CFA polymorphic functions use dynamic virtual-dispatch with low runtime overhead (see Section~\ref{sec:eval}), it is not as low as \CC template-inlining.
    2839 Hence it may be beneficial to provide a mechanism for performance-sensitive code.
     2804There is ongoing work on a wide range of \CFA features, including arrays with size, runtime type-information, virtual functions, user-defined conversions, concurrent primitives, and modules.
     2805While all examples in the paper compile and run, a public beta-release of \CFA will take another 8--12 months to finalize these extensions.
     2806There are also interesting future directions for the polymorphism design.
     2807Notably, \CC template functions trade compile time and code bloat for optimal runtime of individual instantiations of polymorphic functions.
     2808\CFA polymorphic functions use dynamic virtual-dispatch;
     2809the runtime overhead of this approach is low, but not as low as inlining, and it may be beneficial to provide a mechanism for performance-sensitive code.
    28402810Two promising approaches are an @inline@ annotation at polymorphic function call sites to create a template-specialization of the function (provided the code is visible) or placing an @inline@ annotation on polymorphic function-definitions to instantiate a specialized version for some set of types (\CC template specialization).
    28412811These approaches are not mutually exclusive and allow performance optimizations to be applied only when necessary, without suffering global code-bloat.
     
    28462816
    28472817The authors would like to recognize the design assistance of Glen Ditchfield, Richard Bilson, Thierry Delisle, Andrew Beach and Brice Dobry on the features described in this paper, and thank Magnus Madsen for feedback on the writing.
    2848 Funding for this project has been provided by Huawei Ltd.\ (\url{http://www.huawei.com}), and Aaron Moss and Peter Buhr are partially funded by the Natural Sciences and Engineering Research Council of Canada.
    2849 
    2850 {%
    2851 \fontsize{9bp}{12bp}\selectfont%
     2818This work is supported by a corporate partnership with Huawei Ltd.\ (\url{http://www.huawei.com}), and Aaron Moss and Peter Buhr are partially funded by the Natural Sciences and Engineering Research Council of Canada.
     2819
     2820
    28522821\bibliography{pl}
    2853 }%
     2822
    28542823
    28552824\appendix
    28562825
    2857 \section{Benchmark Stack Implementations}
    2858 \label{sec:BenchmarkStackImplementations}
    2859 
    2860 Throughout, @/***/@ designates a counted redundant type annotation; code reformatted slightly for brevity.
    2861 
    2862 
    2863 \subsection{C}
    2864 
    2865 \begin{flushleft}
    2866 \lstDeleteShortInline@%
    2867 \begin{tabular}{@{}l@{\hspace{1.8\parindentlnth}}|@{\hspace{\parindentlnth}}l@{}}
    2868 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
    2869 typedef struct node {
     2826\section{Benchmark Stack Implementation}
     2827\label{sec:BenchmarkStackImplementation}
     2828
     2829Throughout, @/***/@ designates a counted redundant type annotation; code reformatted for brevity.
     2830
     2831\smallskip\noindent
     2832C
     2833\begin{cfa}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
     2834struct stack_node {
    28702835        void * value;
    2871         struct node * next;
    2872 } node;
    2873 typedef struct stack {
    2874         struct node * head;
    2875 } stack;
    2876 void copy_stack( stack * s, const stack * t,
    2877                                 void * (*copy)( const void * ) ) {
    2878         node ** cr = &s->head;
    2879         for (node * nx = t->head; nx; nx = nx->next) {
    2880                 *cr = malloc( sizeof(node) ); /***/
    2881                 (*cr)->value = copy( nx->value );
    2882                 cr = &(*cr)->next;
    2883         }
    2884         *cr = NULL;
    2885 }
    2886 void clear_stack( stack * s, void (* free_el)( void * ) ) {
    2887         for ( node * nx = s->head; nx; ) {
    2888                 node * cr = nx;
    2889                 nx = cr->next;
    2890                 free_el( cr->value );
    2891                 free( cr );
     2836        struct stack_node * next;
     2837};
     2838struct stack { struct stack_node* head; };
     2839void clear_stack( struct stack * s, void (*free_el)( void * ) ) {
     2840        for ( struct stack_node * next = s->head; next; ) {
     2841                struct stack_node * crnt = next;
     2842                next = crnt->next;
     2843                free_el( crnt->value );
     2844                free( crnt );
    28922845        }
    28932846        s->head = NULL;
    28942847}
    2895 \end{cfa}
    2896 &
    2897 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
    2898 stack new_stack() {
    2899         return (stack){ NULL }; /***/
    2900 }
    2901 stack * assign_stack( stack * s, const stack * t,
    2902                                 void * (*copy_el)( const void * ),
    2903                                 void (*free_el)( void * ) ) {
     2848struct stack new_stack() { return (struct stack){ NULL }; /***/ }
     2849void copy_stack( struct stack * s, const struct stack * t, void * (*copy)( const void * ) ) {
     2850        struct stack_node ** crnt = &s->head;
     2851        for ( struct stack_node * next = t->head; next; next = next->next ) {
     2852                *crnt = malloc( sizeof(struct stack_node) ); /***/
     2853                (*crnt)->value = copy( next->value );
     2854                crnt = &(*crnt)->next;
     2855        }
     2856        *crnt = NULL;
     2857}
     2858struct stack * assign_stack( struct stack * s, const struct stack * t,
     2859                void * (*copy_el)( const void * ), void (*free_el)( void * ) ) {
    29042860        if ( s->head == t->head ) return s;
    29052861        clear_stack( s, free_el ); /***/
     
    29072863        return s;
    29082864}
    2909 _Bool stack_empty( const stack * s ) {
    2910         return s->head == NULL;
    2911 }
    2912 void push_stack( stack * s, void * v ) {
    2913         node * n = malloc( sizeof(node) ); /***/
    2914         *n = (node){ v, s->head }; /***/
     2865_Bool stack_empty( const struct stack * s ) { return s->head == NULL; }
     2866void push_stack( struct stack * s, void * v ) {
     2867        struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/
     2868        *n = (struct stack_node){ v, s->head }; /***/
    29152869        s->head = n;
    29162870}
    2917 void * pop_stack( stack * s ) {
    2918         node * n = s->head;
     2871void * pop_stack( struct stack * s ) {
     2872        struct stack_node * n = s->head;
    29192873        s->head = n->next;
    29202874        void * v = n->value;
     
    29232877}
    29242878\end{cfa}
    2925 \end{tabular}
    2926 \lstMakeShortInline@%
    2927 \end{flushleft}
    2928 
    2929 
    2930 \subsection{\CFA}
    2931 \label{s:CforallStack}
    2932 
    2933 \begin{flushleft}
    2934 \lstDeleteShortInline@%
    2935 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    2936 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
     2879
     2880\medskip\noindent
     2881\CFA
     2882\begin{cfa}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
     2883forall( otype T ) struct stack_node {
     2884        T value;
     2885        stack_node(T) * next;
     2886};
     2887forall( otype T ) struct stack { stack_node(T) * head; };
     2888forall( otype T ) void clear( stack(T) & s ) with( s ) {
     2889        for ( stack_node(T) * next = head; next; ) {
     2890                stack_node(T) * crnt = next;
     2891                next = crnt->next;
     2892                ^(*crnt){};
     2893                free(crnt);
     2894        }
     2895        head = 0;
     2896}
     2897forall( otype T ) void ?{}( stack(T) & s ) { (s.head){ 0 }; }
     2898forall( otype T ) void ?{}( stack(T) & s, stack(T) t ) {
     2899        stack_node(T) ** crnt = &s.head;
     2900        for ( stack_node(T) * next = t.head; next; next = next->next ) {
     2901                *crnt = alloc();
     2902                ((*crnt)->value){ next->value };
     2903                crnt = &(*crnt)->next;
     2904        }
     2905        *crnt = 0;
     2906}
     2907forall( otype T ) stack(T) ?=?( stack(T) & s, stack(T) t ) {
     2908        if ( s.head == t.head ) return s;
     2909        clear( s );
     2910        s{ t };
     2911        return s;
     2912}
     2913forall( otype T ) void ^?{}( stack(T) & s) { clear( s ); }
     2914forall( otype T ) _Bool empty( const stack(T) & s ) { return s.head == 0; }
     2915forall( otype T ) void push( stack(T) & s, T value ) with( s ) {
     2916        stack_node(T) * n = alloc();
     2917        (*n){ value, head };
     2918        head = n;
     2919}
     2920forall( otype T ) T pop( stack(T) & s ) with( s ) {
     2921        stack_node(T) * n = head;
     2922        head = n->next;
     2923        T v = n->value;
     2924        ^(*n){};
     2925        free( n );
     2926        return v;
     2927}
     2928\end{cfa}
     2929
     2930\begin{comment}
    29372931forall( otype T ) {
    2938         struct node {
     2932        struct stack_node {
    29392933                T value;
    2940                 node(T) * next;
     2934                stack_node(T) * next;
    29412935        };
    2942         struct stack { node(T) * head; };
    2943         void ?{}( stack(T) & s, stack(T) t ) { // copy
    2944                 node(T) ** cr = &s.head;
    2945                 for ( node(T) * nx = t.head; nx; nx = nx->next ) {
    2946                         *cr = alloc();
    2947                         ((*cr)->value){ nx->value };
    2948                         cr = &(*cr)->next;
    2949                 }
    2950                 *cr = 0;
    2951         }
     2936        struct stack { stack_node(T) * head; };
    29522937        void clear( stack(T) & s ) with( s ) {
    2953                 for ( node(T) * nx = head; nx; ) {
    2954                         node(T) * cr = nx;
    2955                         nx = cr->next;
    2956                         ^(*cr){};
    2957                         free( cr );
     2938                for ( stack_node(T) * next = head; next; ) {
     2939                        stack_node(T) * crnt = next;
     2940                        next = crnt->next;
     2941                        ^(*crnt){};
     2942                        free(crnt);
    29582943                }
    29592944                head = 0;
    29602945        }
    2961 
    2962 \end{cfa}
    2963 &
    2964 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
    29652946        void ?{}( stack(T) & s ) { (s.head){ 0 }; }
    2966         void ^?{}( stack(T) & s) { clear( s ); }
     2947        void ?{}( stack(T) & s, stack(T) t ) {
     2948                stack_node(T) ** crnt = &s.head;
     2949                for ( stack_node(T) * next = t.head; next; next = next->next ) {
     2950                        *crnt = alloc();
     2951                        ((*crnt)->value){ next->value };
     2952                        crnt = &(*crnt)->next;
     2953                }
     2954                *crnt = 0;
     2955        }
    29672956        stack(T) ?=?( stack(T) & s, stack(T) t ) {
    29682957                if ( s.head == t.head ) return s;
     
    29712960                return s;
    29722961        }
    2973         _Bool empty( const stack(T) & s ) {
    2974                 return s.head == 0;
    2975         }
     2962        void ^?{}( stack(T) & s) { clear( s ); }
     2963        _Bool empty( const stack(T) & s ) { return s.head == 0; }
    29762964        void push( stack(T) & s, T value ) with( s ) {
    2977                 node(T) * n = alloc();
     2965                stack_node(T) * n = alloc();
    29782966                (*n){ value, head };
    29792967                head = n;
    29802968        }
    29812969        T pop( stack(T) & s ) with( s ) {
    2982                 node(T) * n = head;
     2970                stack_node(T) * n = head;
    29832971                head = n->next;
    29842972                T v = n->value;
     
    29882976        }
    29892977}
    2990 \end{cfa}
    2991 \end{tabular}
    2992 \lstMakeShortInline@%
    2993 \end{flushleft}
    2994 
    2995 
    2996 \subsection{\CC}
    2997 
    2998 \begin{flushleft}
    2999 \lstDeleteShortInline@%
    3000 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    3001 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
     2978\end{comment}
     2979
     2980\medskip\noindent
     2981\CC
     2982\begin{cfa}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
    30022983template<typename T> struct stack {
    30032984        struct node {
    30042985                T value;
    30052986                node * next;
    3006                 node( const T & v, node * n = nullptr ) :
    3007                         value( v ), next( n ) {}
     2987                node( const T & v, node * n = nullptr ) : value( v ), next( n ) {}
    30082988        };
    30092989        node * head;
    3010         void copy( const stack<T> & o ) {
    3011                 node ** cr = &head;
    3012                 for ( node * nx = o.head; nx; nx = nx->next ) {
    3013                         *cr = new node{ nx->value }; /***/
    3014                         cr = &(*cr)->next;
    3015                 }
    3016                 *cr = nullptr;
    3017         }
     2990        stack() : head( nullptr ) {}
     2991        stack( const stack<T> & o ) { copy( o ); }
    30182992        void clear() {
    3019                 for ( node * nx = head; nx; ) {
    3020                         node * cr = nx;
    3021                         nx = cr->next;
    3022                         delete cr;
     2993                for ( node * next = head; next; ) {
     2994                        node * crnt = next;
     2995                        next = crnt->next;
     2996                        delete crnt;
    30232997                }
    30242998                head = nullptr;
    30252999        }
    3026 \end{cfa}
    3027 &
    3028 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
    3029         stack() : head( nullptr ) {}
    3030         stack( const stack<T> & o ) { copy( o ); }
     3000        void copy( const stack<T> & o ) {
     3001                node ** crnt = &head;
     3002                for ( node * next = o.head; next; next = next->next ) {
     3003                        *crnt = new node{ next->value }; /***/
     3004                        crnt = &(*crnt)->next;
     3005                }
     3006                *crnt = nullptr;
     3007        }
    30313008        ~stack() { clear(); }
    3032         stack & operator=( const stack<T> & o ) {
     3009        stack & operator= ( const stack<T> & o ) {
    30333010                if ( this == &o ) return *this;
    30343011                clear();
     
    30363013                return *this;
    30373014        }
    3038         bool empty() const {
    3039                 return head == nullptr;
    3040         }
    3041         void push( const T & value ) {
    3042                 head = new node{ value, head };  /***/
    3043         }
     3015        bool empty() const { return head == nullptr; }
     3016        void push( const T & value ) { head = new node{ value, head };  /***/ }
    30443017        T pop() {
    30453018                node * n = head;
     
    30503023        }
    30513024};
    3052 
    3053 \end{cfa}
    3054 \end{tabular}
    3055 \lstMakeShortInline@%
    3056 \end{flushleft}
    3057 
    3058 
    3059 \subsection{\CCV}
    3060 
    3061 \begin{flushleft}
    3062 \lstDeleteShortInline@%
    3063 \begin{tabular}{@{}l|@{\hspace{\parindentlnth}}l@{}}
    3064 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
     3025\end{cfa}
     3026
     3027\medskip\noindent
     3028\CCV
     3029\begin{cfa}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
    30653030struct stack {
    30663031        struct node {
    30673032                ptr<object> value;
    30683033                node * next;
    3069                 node( const object & v, node * n = nullptr ) :
    3070                                 value( v.new_copy() ), next( n ) {}
     3034                node( const object & v, node * n = nullptr ) : value( v.new_copy() ), next( n ) {}
    30713035        };
    30723036        node * head;
    3073         void copy( const stack & o ) {
    3074                 node ** cr = &head;
    3075                 for ( node * nx = o.head; nx; nx = nx->next ) {
    3076                         *cr = new node{ *nx->value }; /***/
    3077                         cr = &(*cr)->next;
    3078                 }
    3079                 *cr = nullptr;
    3080         }
    30813037        void clear() {
    3082                 for ( node * nx = head; nx; ) {
    3083                         node * cr = nx;
    3084                         nx = cr->next;
    3085                         delete cr;
     3038                for ( node * next = head; next; ) {
     3039                        node * crnt = next;
     3040                        next = crnt->next;
     3041                        delete crnt;
    30863042                }
    30873043                head = nullptr;
    30883044        }
    3089 \end{cfa}
    3090 &
    3091 \begin{cfa}[xleftmargin=0pt,aboveskip=0pt,belowskip=0pt]
     3045        void copy( const stack & o ) {
     3046                node ** crnt = &head;
     3047                for ( node * next = o.head; next; next = next->next ) {
     3048                        *crnt = new node{ *next->value }; /***/
     3049                        crnt = &(*crnt)->next;
     3050                }
     3051                *crnt = nullptr;
     3052        }
    30923053        stack() : head( nullptr ) {}
    30933054        stack( const stack & o ) { copy( o ); }
    30943055        ~stack() { clear(); }
    3095         stack & operator=( const stack & o ) {
     3056        stack & operator= ( const stack & o ) {
    30963057                if ( this == &o ) return *this;
    30973058                clear();
     
    30993060                return *this;
    31003061        }
    3101         bool empty() const {
    3102                 return head == nullptr;
    3103         }
    3104         void push( const object & value ) {
    3105                 head = new node{ value, head }; /***/
    3106         }
     3062        bool empty() const { return head == nullptr; }
     3063        void push( const object & value ) { head = new node{ value, head }; /***/ }
    31073064        ptr<object> pop() {
    31083065                node * n = head;
     
    31133070        }
    31143071};
    3115 
    3116 \end{cfa}
    3117 \end{tabular}
    3118 \lstMakeShortInline@%
    3119 \end{flushleft}
     3072\end{cfa}
    31203073
    31213074
Note: See TracChangeset for help on using the changeset viewer.