Changeset 271326e
- Timestamp:
- Feb 15, 2018, 4:00:40 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 359f29f
- Parents:
- d27e340
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/Paper.tex
rd27e340 r271326e 189 189 190 190 191 \section{Introduction and Background}191 \section{Introduction} 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 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 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}} 230 258 \label{sec:poly-fns} 231 259 232 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.233 260 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): 234 261 \begin{lstlisting} … … 306 333 Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types. 307 334 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@. 314 343 315 344 \subsection{Traits} … … 1331 1360 \end{cfa} 1332 1361 1333 1334 \subsection{Exception Handling ???} 1335 1362 % \subsection{Exception Handling ???} 1336 1363 1337 1364 \section{Declarations} … … 1889 1916 1890 1917 \section{Libraries} 1918 \label{sec:libraries} 1891 1919 1892 1920 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.