Changeset ea73caf for doc


Ignore:
Timestamp:
Apr 3, 2017, 9:01:28 AM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
36e05a2
Parents:
53f9448
Message:

squeeze text to fit on first 3 pages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/generic_types.tex

    r53f9448 rea73caf  
    5151%mathescape=true,                                                                               % LaTeX math escape in CFA code $...$
    5252escapechar=\$,                                                                                  % LaTeX escape in CFA code
    53 %keepspaces=true,                                                                               %
     53keepspaces=true,                                                                                %
    5454showstringspaces=false,                                                                 % do not show spaces with cup
    5555showlines=true,                                                                                 % show blank lines at end of code
     
    167167\begin{lstlisting}
    168168forall( otype T `| { int ?<?( T, T ); }` )      $\C{// type assertion}$
    169   void qsort( const T * arr, size_t dimension );
     169  void qsort( const T * arr, size_t dim );
    170170forall( otype T `| { int ?<?( T, T ); }` )      $\C{// type assertion}$
    171   T * bsearch( T key, const T * arr, size_t dimension );
     171  T * bsearch( T key, const T * arr, size_t dim );
    172172double vals[10] = { /* 10 floating-point values */ };
    173173qsort( vals, 10 );                                                      $\C{// sort array}$
     
    179179Crucial to the design of a new programming language are the libraries to access thousands of external features.
    180180\CFA inherits a massive compatible library-base, where other programming languages have to rewrite or provide fragile inter-language communication with C.
    181 A simple example is leveraging the existing type-unsafe C @bsearch@:
     181A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@, shown here searching a floating-point array:
    182182\begin{lstlisting}
    183183void * bsearch( const void * key, const void * base, size_t nmemb, size_t size,
    184                      int (* compar)(const void *, const void *));
     184                                int (* compar)(const void *, const void *));
    185185int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 :
    186                                          *(double *)t2 < *(double *)t1 ? 1 : 0; }
    187 double key = ...;
    188 double *v = (double *)bsearch( &key, vals, size, sizeof( vals[0] ), comp );
     186                                *(double *)t2 < *(double *)t1 ? 1 : 0; }
     187double key = 5.0;
     188double * val = (double *)bsearch( &key, vals, size, sizeof(vals[0]), comp );
    189189\end{lstlisting}
    190190but providing a type-safe \CFA overloaded wrapper.
    191191\begin{lstlisting}
    192 forall( otype T | { int ?<?( T, T ); } )
    193   T * bsearch( T key, const T * arr, size_t dimension ) {
     192forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t dim ) {
    194193        int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ }
    195         return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
    196 }
    197 forall( otype T | { int ?<?( T, T ); } )
    198   unsigned int bsearch( T key, const T * arr, size_t dimension ) {
    199         T *result = bsearch( key, arr, dimension );     $\C{// call first version}$
    200         return result ? result - arr : dimension;       $\C{// pointer subtraction includes sizeof(T)}$
    201 }
    202 double * val = bsearch( 5.0, vals, 10 );                $\C{// selection based on return type}$
     194        return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
     195}
     196forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t dim ) {
     197        T *result = bsearch( key, arr, dim );   $\C{// call first version}$
     198        return result ? result - arr : dim;             $\C{// pointer subtraction includes sizeof(T)}$
     199}
     200double * val = bsearch( 5.0, vals, 10 );        $\C{// selection based on return type}$
    203201int posn = bsearch( 5.0, vals, 10 );
    204202\end{lstlisting}
     
    209207Call-site inferencing and nested functions provide a localized form of inheritance. For example, @qsort@ only sorts in ascending order using @<@. However, it is trivial to locally change this behaviour:
    210208\begin{lstlisting}
    211 {
    212         int ?<?( double x, double y ) { return x `>` y; }       $\C{// override behaviour}$
     209{   int ?<?( double x, double y ) { return x `>` y; }   $\C{// override behaviour}$
    213210        qsort( vals, size );                                    $\C{// descending sort}$
    214211}
     
    217214Hence, programmers can easily form new local environments to maximize reuse of existing functions and types.
    218215
    219 Finally, variables can also be overloaded:
    220 
     216Finally, variables may be overloaded:
     217\lstDeleteShortInline@
     218\par\smallskip
     219\begin{tabular}{@{}l@{\hspace{\parindent}}|@{\hspace{\parindent}}l@{}}
    221220\begin{lstlisting}
    222221short int MAX = ...;
     
    224223double MAX = ...;
    225224\end{lstlisting}
     225&
    226226\begin{lstlisting}
    227227short int s = MAX;  // select correct MAX
     
    229229double d = MAX;
    230230\end{lstlisting}
    231 
     231\end{tabular}
     232\lstMakeShortInline@
     233\smallskip\par\noindent
     234Hence, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
    232235
    233236\subsection{Traits}
     
    259262};
    260263forall( otype T | summable( T ) )
    261   T sum( T a[$\,$], size_t dimension ) {
     264  T sum( T a[$\,$], size_t dim ) {
    262265        T total = { 0 };                                                        $\C{// instantiate T from 0}$
    263         for ( unsigned int i = 0; i < dimension; i += 1 )
     266        for ( unsigned int i = 0; i < dim; i += 1 )
    264267                total += a[i];                                          $\C{// select appropriate +}$
    265268        return total;
Note: See TracChangeset for help on using the changeset viewer.