Changeset 271326e


Ignore:
Timestamp:
Feb 15, 2018, 4:00:40 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
359f29f
Parents:
d27e340
Message:

Add name overloading subsection, rebuttal of _Generic

File:
1 edited

Legend:

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

    rd27e340 r271326e  
    189189
    190190
    191 \section{Introduction and Background}
     191\section{Introduction}
    192192
    193193The 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.
     
    226226The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance.
    227227
    228 
    229 \subsection{Polymorphic Functions}
     228\section{Polymorphic Functions}
     229
     230\CFA introduces both ad-hoc and parametric polymorphism to C, with a design originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.
     231
     232\subsection{Name Overloading}
     233
     234C 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.
     235\CFA extends the built-in operator overloading by allowing users to define overloads for any function, not just operators, and even any variable; Section~\ref{sec:libraries} includes a number of examples of how this overloading simplifies \CFA programming relative to C.
     236Code 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.
     237As an example:
     238
     239\begin{cfa}
     240int max(int a, int b) { return a < b ? b : a; }  // (1)
     241double max(double a, double b) { return a < b ? b : a; }  // (2)
     242
     243int max = INT_MAX;     // (3)
     244double max = DBL_MAX;  // (4)
     245
     246max(7, -max);   $\C{// uses (1) and (3), by matching int from constant 7}$
     247max(max, 3.14); $\C{// uses (2) and (4), by matching double from constant 3.14}$
     248
     249//max(max, -max);  $\C{// ERROR: ambiguous}$
     250int m = max(max, -max); $\C{// uses (1) once and (3) twice, by matching return type}$
     251\end{cfa}
     252
     253\Celeven did add @_Generic@ expressions, which can be used in preprocessor macros to provide a form of ad-hoc polymorphism; however, this polymorphism is both functionally and ergonomically inferior to \CFA name overloading.
     254The macro wrapping the generic expression imposes some limitations; as an example, it could not implement the example above, because the variables @max@ would collide with the functions @max@.
     255Ergonomic 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 functions dispatched to, which must all have distinct names.
     256
     257\subsection{\texorpdfstring{\LstKeywordStyle{forall} Functions}{forall Functions}}
    230258\label{sec:poly-fns}
    231259
    232 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.
    233260The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a @forall@ clause (giving the language its name):
    234261\begin{lstlisting}
     
    306333Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types.
    307334
    308 Finally, \CFA allows variable overloading:
    309 \begin{lstlisting}
    310 short int MAX = ...;   int MAX = ...;  double MAX = ...;
    311 short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$
    312 \end{lstlisting}
    313 Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
     335%% Redundant with Section~\ref{sec:libraries} %%
     336
     337% Finally, \CFA allows variable overloading:
     338% \begin{lstlisting}
     339% short int MAX = ...;   int MAX = ...;  double MAX = ...;
     340% short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$
     341% \end{lstlisting}
     342% Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
    314343
    315344\subsection{Traits}
     
    13311360\end{cfa}
    13321361
    1333 
    1334 \subsection{Exception Handling ???}
    1335 
     1362% \subsection{Exception Handling ???}
    13361363
    13371364\section{Declarations}
     
    18891916
    18901917\section{Libraries}
     1918\label{sec:libraries}
    18911919
    18921920As stated in Section~\ref{sec:poly-fns}, \CFA inherits a large corpus of library code, where other programming languages must rewrite or provide fragile inter-language communication with C.
Note: See TracChangeset for help on using the changeset viewer.