Changes in / [359f29f:75e3cb2]
- File:
-
- 1 edited
-
doc/papers/general/Paper.tex (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/Paper.tex
r359f29f r75e3cb2 189 189 190 190 191 \section{Introduction }191 \section{Introduction and Background} 192 192 193 193 The 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. … … 226 226 The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance. 227 227 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 234 C 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. 236 Code 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. 237 As an example: 238 239 \begin{cfa} 240 int max(int a, int b) { return a < b ? b : a; } // (1) 241 double max(double a, double b) { return a < b ? b : a; } // (2) 242 243 int max = INT_MAX; // (3) 244 double max = DBL_MAX; // (4) 245 246 max(7, -max); $\C{// uses (1) and (3), by matching int from constant 7}$ 247 max(max, 3.14); $\C{// uses (2) and (4), by matching double from constant 3.14}$ 248 249 //max(max, -max); $\C{// ERROR: ambiguous}$ 250 int 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. 254 The 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@. 255 Ergonomic 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}} 228 229 \subsection{Polymorphic Functions} 258 230 \label{sec:poly-fns} 259 231 232 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}. 260 233 The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a @forall@ clause (giving the language its name): 261 234 \begin{lstlisting} … … 333 306 Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types. 334 307 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@. 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@. 343 314 344 315 \subsection{Traits} … … 1360 1331 \end{cfa} 1361 1332 1362 % \subsection{Exception Handling ???} 1333 1334 \subsection{Exception Handling ???} 1335 1363 1336 1364 1337 \section{Declarations} … … 1916 1889 1917 1890 \section{Libraries} 1918 \label{sec:libraries}1919 1891 1920 1892 As 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.