# Changeset f7b0d71 for doc/generic_types/generic_types.tex

Ignore:
Timestamp:
Apr 19, 2017, 11:10:13 AM (5 years ago)
Branches:
aaron-thesis, arm-eh, 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, resolv-new, with_gc
Children:
648afb4
Parents:
cab8cac
Message:

more cleanup

File:
1 edited

### Legend:

Unmodified
 rcab8cac belowskip=3pt, % replace/adjust listing characters that look bad in sanserif literate={-}{\makebox[1.4ex][c]{\raisebox{0.5ex}{\rule{1.2ex}{0.1ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1 literate={-}{\makebox[1.4ex][c]{\raisebox{0.5ex}{\rule{1.2ex}{0.06ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1 {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 % {}{\ttfamily\upshape\hspace*{-0.1ex}}1 {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1.4ex][c]{\raisebox{0.5ex}{\rule{1.2ex}{0.1ex}}}\kern-0.3ex\textgreater}2, {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1.4ex][c]{\raisebox{0.5ex}{\rule{1.2ex}{0.06ex}}}\kern-0.3ex\textgreater}2, moredelim=**[is][\color{red}]{}{}, }% lstset if (x) x++                                                                      $\C{// if (x != 0) x += 1;}$ \end{lstlisting} Every if and iteration statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result. Every @if@ and iteration statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result. Due to these rewrite rules, the values @0@ and @1@ have the types @zero_t@ and @one_t@ in \CFA, which allows overloading various operations for new types that seamlessly connect to all special @0@ and @1@ contexts. The types @zero_t@ and @one_t@ have special built in implicit conversions to the various integral types, and a conversion to pointer types for @0@, which allows standard C code involving @0@ and @1@ to work as normal. The \CFA translator template-expands concrete generic-types into new structure types, affording maximal inlining. To enable inter-operation among equivalent instantiations of a generic type, the translator saves the set of instantiations currently in scope and reuses the generated structure declarations where appropriate. For example, a function declaration that accepts or returns a concrete generic-type produces a declaration for the instantiated struct in the same scope, which all callers may reuse. A function declaration that accepts or returns a concrete generic-type produces a declaration for the instantiated structure in the same scope, which all callers may reuse. For example, the concrete instantiation for @pair( const char *, int )@ is: \begin{lstlisting} However, the \CFA type-system must support significantly more complex composition: \begin{lstlisting} [ int, int ] foo$$$_1$$$( int ); [ int, int ] foo$$$_1$$$( int );                        $\C{// overloaded foo functions}$ [ double ] foo$$$_2$$$( int ); void bar( int, double, double ); Since @sum@$$_0$$ does not accept any arguments, it is not a valid candidate function for the call @sum(10, 20, 30)@. In order to call @sum@$$_1$$, @10@ is matched with @x@, and the argument resolution moves on to the argument pack @rest@, which consumes the remainder of the argument list and @Params@ is bound to @[20, 30]@. The process continues, @Params@ is bound to @[]@, requiring an assertion @int sum()@, which matches @sum@$$_0$$ and terminates the recursion. The process continues unitl @Params@ is bound to @[]@, requiring an assertion @int sum()@, which matches @sum@$$_0$$ and terminates the recursion. Effectively, this algorithm traces as @sum(10, 20, 30)@ $\rightarrow$ @10 + sum(20, 30)@ $\rightarrow$ @10 + (20 + sum(30))@ $\rightarrow$ @10 + (20 + (30 + sum()))@ $\rightarrow$ @10 + (20 + (30 + 0))@. \end{lstlisting} Unlike C variadic functions, it is unnecessary to hard code the number and expected types. Furthermore, this code is extendable so any user-defined type with a @?+?@ operator. Furthermore, this code is extendable for any user-defined type with a @?+?@ operator. Summing arbitrary heterogeneous lists is possible with similar code by adding the appropriate type variables and addition operators. On the other hand, C does not have a generic collections-library in its standard distribution, resulting in frequent reimplementation of such collection types by C programmers. \CCV does not use the \CC standard template library by construction, and in fact includes the definition of @object@ and wrapper classes for @bool@, @char@, @int@, and @const char *@ in its line count, which inflates this count somewhat, as an actual object-oriented language would include these in the standard library; with their omission the \CCV line count is similar to C. with their omission, the \CCV line count is similar to C. We justify the given line count by noting that many object-oriented languages do not allow implementing new interfaces on library types without subclassing or wrapper types, which may be similarly verbose.