Changeset 37218fc for doc/user/user.tex
- Timestamp:
- Apr 11, 2016, 11:51:07 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 37f0da8
- Parents:
- 3aba311 (diff), e55ca05 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r3aba311 r37218fc 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 %% 3 %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 %% 5 %% The contents of this file are covered under the licence agreement in the 6 %% file "LICENCE" distributed with Cforall. 7 %% 8 %% user.tex -- 9 %% 10 %% Author : Peter A. Buhr 11 %% Created On : Wed Apr 6 14:53:29 2016 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Fri Apr 8 11:40:53 2016 14 %% Update Count : 42 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 1 17 % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended 2 18 … … 5 21 6 22 % Latex packages used in the document. 7 23 \usepackage[T1]{fontenc} 24 \usepackage{textcomp} 25 \usepackage[latin1]{inputenc} 26 \usepackage{upquote} 8 27 \usepackage{fullpage,times} 9 28 \usepackage{xspace} … … 24 43 % Names used in the document. 25 44 26 \newcommand{\CFA}{C$\forall$\xspace} % set language symbolic name27 \newcommand{\CFL}{Cforall\xspace} % set language text name28 \newcommand{\CC}{C\kern-.1em\hbox{+\kern-.25em+}\xspace} % CC symbolic name29 \def\c11{ISO/IEC C} % C11 name (cannot have numbers in latex command name)30 45 \newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace} 31 46 … … 33 48 34 49 % Bespoke macros used in the document. 35 36 \makeatletter 37 % allow escape sequence in lstinline 38 %\usepackage{etoolbox} 39 %\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}} 40 41 \renewcommand\small{% 42 \@setfontsize\small{8.5}{11}% 43 \abovedisplayskip 8.5pt \@plus 3pt \@minus 4pt 44 \abovedisplayshortskip \z@ \@plus 2pt 45 \belowdisplayshortskip 4pt \@plus 2pt \@minus 2pt 46 \def\@listi{\leftmargin\leftmargini 47 \topsep 4pt \@plus 2pt \@minus 2pt 48 \parsep 2pt \@pluspt \@minuspt 49 \itemsep \parsep}% 50 \belowdisplayskip \abovedisplayskip 51 } 52 \usepackage{relsize} % must be after change to small 53 54 \renewcommand{\labelitemi}{{\raisebox{0.25ex}{\footnotesize$\bullet$}}} 55 \renewenvironment{itemize}{\begin{list}{\labelitemi}{\topsep=5pt\itemsep=5pt\parsep=0pt}}{\end{list}} 56 57 % Reduce size of section titles 58 \renewcommand\section{\@startsection{section}{1}{\z@}{-3.0ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\large\bfseries}} 59 \renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}} 60 \renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}} 61 \renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}} 62 63 % index macros 64 \newcommand{\italic}[1]{\emph{\hyperpage{#1}}} 65 \newcommand{\definition}[1]{\textbf{\hyperpage{#1}}} 66 \newcommand{\see}[1]{\emph{see} #1} 67 68 % Define some commands that produce formatted index entries suitable for cross-references. 69 % ``\spec'' produces entries for specifications of entities. ``\impl'' produces entries for their 70 % implementations, and ``\use'' for their uses. 71 72 % \newcommand{\bold}[1]{{\bf #1}} 73 % \def\spec{\@bsphack\begingroup 74 % \def\protect##1{\string##1\space}\@sanitize 75 % \@wrxref{|bold}} 76 \def\impl{\@bsphack\begingroup 77 \def\protect##1{\string##1\space}\@sanitize 78 \@wrxref{|definition}} 79 \newcommand{\indexcode}[1]{{\lstinline$#1$}} 80 \def\use{\@bsphack\begingroup 81 \def\protect##1{\string##1\space}\@sanitize 82 \@wrxref{|hyperpage}} 83 \def\@wrxref#1#2{\let\thepage\relax 84 \xdef\@gtempa{\write\@indexfile{\string 85 \indexentry{#2@{\lstinline$#2$}#1}{\thepage}}}\endgroup\@gtempa 86 \if@nobreak \ifvmode\nobreak\fi\fi\@esphack} 87 %\newcommand{\use}[1]{\index{#1@{\lstinline$#1$}}} 88 %\newcommand{\impl}[1]{\index{\protect#1@{\lstinline$\protect#1$}|definition}} 89 90 % inline text and lowercase index: \Index{inline and lowercase index text} 91 % inline text and as-in index: \Index[as-is index text]{inline text} 92 % inline text but index with different as-is text: \Index[index text]{inline text} 93 \newcommand{\Index}{\@ifstar\@sIndex\@Index} 94 \newcommand{\@Index}[2][\@empty]{\lowercase{\def\temp{#2}}#2\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi} 95 \newcommand{\@sIndex}[2][\@empty]{#2\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi} 96 97 \newcommand{\newtermFontInline}{\emph} 98 \newcommand{\newterm}{\@ifstar\@snewterm\@newterm} 99 \newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi} 100 \newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi} 101 \makeatother 102 103 % blocks and titles 104 \newenvironment{quote2}{% 105 \list{}{\lstset{resetmargins=true}\leftmargin=\parindent\rightmargin\leftmargin}% 106 \item\relax 107 }{% 108 \endlist 109 }% quote2 110 \newenvironment{rationale}{% 111 \begin{quotation}\noindent$\Box$\enspace 112 }{% 113 \hfill\enspace$\Box$\end{quotation} 114 }% 115 \newcommand{\define}[1]{\emph{#1\/}\index{#1}} 116 \newcommand{\rewrite}{\(\Rightarrow\)} 117 \newcommand{\rewriterules}{\paragraph{Rewrite Rules}~\par\noindent} 118 \newcommand{\examples}{\paragraph{Examples}~\par\noindent} 119 \newcommand{\semantics}{\paragraph{Semantics}~\par\noindent} 120 \newcommand{\constraints}{\paragraph{Constraints}~\par\noindent} 121 \newcommand{\predefined}{\paragraph{Predefined Identifiers}~\par\noindent} 122 123 % BNF macros 124 \def\syntax{\paragraph{Syntax}\trivlist\parindent=.5in\item[\hskip.5in]} 125 \let\endsyntax=\endtrivlist 126 \newcommand{\lhs}[1]{\par{\emph{#1:}}\index{#1@{\emph{#1}}|italic}} 127 \newcommand{\rhs}{\hfil\break\hbox{\hskip1in}} 128 \newcommand{\oldlhs}[1]{\emph{#1: \ldots}\index{#1@{\emph{#1}}|italic}} 129 \newcommand{\nonterm}[1]{\emph{#1\/}\index{#1@{\emph{#1}}|italic}} 130 \newcommand{\opt}{$_{opt}$\ } 131 132 % adjust varioref package with default "section" and "page" titles, and optional title with faraway page numbers 133 % \VRef{label} => Section 2.7, \VPageref{label} => page 17 134 % \VRef[Figure]{label} => Figure 3.4, \VPageref{label} => page 17 135 \renewcommand{\reftextfaceafter}{\unskip} 136 \renewcommand{\reftextfacebefore}{\unskip} 137 \renewcommand{\reftextafter}{\unskip} 138 \renewcommand{\reftextbefore}{\unskip} 139 \renewcommand{\reftextfaraway}[1]{\unskip, p.~\pageref{#1}} 140 \renewcommand{\reftextpagerange}[2]{\unskip, pp.~\pageref{#1}--\pageref{#2}} 141 \newcommand{\VRef}[2][Section]{\ifx#1\@empty\else{#1}\nobreakspace\fi\vref{#2}} 142 \newcommand{\VPageref}[2][page]{\ifx#1\@empty\else{#1}\nobreakspace\fi\pageref{#2}} 143 144 % Go programming language 145 \lstdefinelanguage{Golang}% 146 {morekeywords=[1]{package,import,func,type,struct,return,defer,panic, recover,select,var,const,iota,},% 147 morekeywords=[2]{string,uint,uint8,uint16,uint32,uint64,int,int8,int16, int32,int64, 148 bool,float32,float64,complex64,complex128,byte,rune,uintptr, error,interface},% 149 morekeywords=[3]{map,slice,make,new,nil,len,cap,copy,close,true,false, delete,append,real,imag,complex,chan,},% 150 morekeywords=[4]{for,break,continue,range,goto,switch,case,fallthrough,if, else,default,},% 151 morekeywords=[5]{Println,Printf,Error,},% 152 sensitive=true,% 153 morecomment=[l]{//},% 154 morecomment=[s]{/*}{*/},% 155 morestring=[b]',% 156 morestring=[b]",% 157 morestring=[s]{`}{`},% 158 } 159 160 % CFA based on ANSI C 161 \lstdefinelanguage{CFA}[ANSI]{C}% 162 {morekeywords={asm,_Alignas,_Alignof,_At,_Atomic,_Bool,catch,catchResume,choose,_Complex,trait,disable,dtype,enable, 163 fallthru,finally,forall,ftype,_Generic,_Imaginary,inline,lvalue,_Noreturn,otype,restrict,_Static_assert, 164 _Thread_local,throw,throwResume,try,}, 165 }% 166 167 \lstset{ 168 language=CFA, 169 columns=flexible, 170 basicstyle=\sf\relsize{-1}, 171 tabsize=4, 172 xleftmargin=\parindent, 173 escapechar=@, 174 mathescape=true, 175 keepspaces=true, 176 showstringspaces=false, 177 showlines=true, 178 }% 179 180 \makeatletter 181 % replace/adjust listings characters that look bad in sanserif 182 \lst@CCPutMacro 183 \lst@ProcessOther{"2D}{\lst@ttfamily{-{}}{{\ttfamily\upshape -}}} % replace minus 184 \lst@ProcessOther{"3C}{\lst@ttfamily{<}{\texttt{<}}} % replace less than 185 \lst@ProcessOther{"3E}{\lst@ttfamily{<}{\texttt{>}}} % replace greater than 186 \lst@ProcessOther{"5E}{\raisebox{0.4ex}{$\scriptstyle\land\,$}} % replace circumflex 187 \lst@ProcessLetter{"5F}{\lst@ttfamily{\char95}{{\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}}} % replace underscore 188 \lst@ProcessOther{"7E}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}} % replace tilde 189 %\lst@ProcessOther{"7E}{\raisebox{-.4ex}[1ex][0pt]{\textasciitilde}} % lower tilde 190 \@empty\z@\@empty 191 \makeatother 50 \input{common} 192 51 193 52 \setcounter{secnumdepth}{3} % number subsubsections … … 366 225 367 226 The command \lstinline@cfa@ is used to compile \CFA program(s). 368 This command works like the GNU \lstinline@gcc@ command, e.g.:227 This command works like the GNU \lstinline@gcc@\index{gcc} command, e.g.: 369 228 \begin{lstlisting} 370 229 cfa [ gcc-options ] C/@{\CFA}@-files [ assembler/loader-files ] 371 230 \end{lstlisting} 372 The following additional option is available: 231 By default, \CFA programs having the following \lstinline@gcc@ flags turned on: 232 \begin{description} 233 \item 234 \hspace*{-4pt}\lstinline@-std=gnu99@ 235 The 1999 C standard plus GNU extensions. 236 \end{description} 237 The following new \CFA option is available: 373 238 \begin{description} 374 239 \item … … 382 247 Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, e.g.: 383 248 \begin{lstlisting} 384 2 _147_483_648; // decimal constant249 2`_`147`_`483`_`648; // decimal constant 385 250 56_ul; // decimal unsigned long constant 386 251 0_377; // octal constant … … 451 316 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 452 317 \begin{lstlisting} 453 * int x, y; 318 `* int x, y;` 454 319 \end{lstlisting} 455 320 & … … 571 436 The point of the new syntax is to allow returning multiple values from a routine~\cite{CLU,Galletly96}, e.g.: 572 437 \begin{lstlisting} 573 [ int o1, int o2, char o3 ]f( int i1, char i2, char i3 ) {438 `[ int o1, int o2, char o3 ]` f( int i1, char i2, char i3 ) { 574 439 @\emph{routine body}@ 575 440 } … … 639 504 Because the value in the return variable is automatically returned when a \CFA routine terminates, the \lstinline@return@ statement \emph{does not} contain an expression, as in: 640 505 \begin{lstlisting} 641 [ int x ]f() {506 `[ int x ]` f() { 642 507 ... x = 0; ... x = y; ... 643 return;// implicitly return x508 `return;` // implicitly return x 644 509 } 645 510 \end{lstlisting} … … 697 562 for example, the following is incorrect: 698 563 \begin{lstlisting} 699 * [ int x ] f () fp; // routine name ``f''is not allowed564 * [ int x ] f () fp; // routine name "f" is not allowed 700 565 \end{lstlisting} 701 566 … … 864 729 \subsection{Type Nesting} 865 730 866 C allows \Index{type nesting}, but the nested types are hoisted\index{type!hoisting} (refactored) into the enclosing scope.731 \CFA allows \Index{type nesting}, and type qualification of the nested types, where as C hoists\index{type!hoisting} (refactors) nested types into the enclosing scope and has no type qualification. 867 732 \begin{quote2} 868 733 \begin{tabular}{@{}l@{\hspace{30pt}}l|l@{}} … … 919 784 920 785 int fred() { 921 s.t.c = S.R;922 struct S.T t = { S.R, 1, 2 };923 enum S.C c;924 union S.T.U u;786 s.t.c = `S.`R; // type qualification 787 struct `S.`T t = { `S.`R, 1, 2 }; 788 enum `S.`C c; 789 union `S.T.`U u; 925 790 } 926 791 \end{lstlisting} 927 792 \end{tabular} 928 793 \end{quote2} 929 930 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \CC. 931 Nested types are not hoisted and can be referenced using the field selection operator ``\lstinline@.@'', unlike the \CC scope-resolution operator ``\lstinline@::@''. 932 Given that nested types in C are equivalent to not using them, i.e., they are essentially useless, it is unlikely there are any realistic usages that break because of this incompatibility. 794 In the left example in C, types \lstinline@C@, \lstinline@U@ and \lstinline@T@ are implicitly hoisted outside of type \lstinline@S@ into the containing block scope. 795 In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``\lstinline@.@'' for type qualification, as does Java, rather than the \CC type-selection operator ``\lstinline@::@''. 933 796 934 797 … … 944 807 \begin{lstlisting} 945 808 const unsigned int size = 10; 946 int a[size]; 947 948 qsort( a, size ); // ascending order using built in ?<? 949 { // descending order by local redefinition 950 int ?<?( int a, int b ) { return a > b; } // nested routine 951 qsort( a, size ); 952 } 953 \end{lstlisting} 809 int ia[size]; 810 ... // assign values to array ia 811 qsort( ia, size ); // sort ascending order using builtin ?<? 812 { 813 `int ?<?( int x, int y ) { return x > y; }` // nested routine 814 qsort( ia, size ); // sort descending order by local redefinition 815 } 816 \end{lstlisting} 817 818 Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks; 819 the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program. 820 The following program in undefined in \CFA (and \lstinline@gcc@\index{gcc}) 821 \begin{lstlisting} 822 [* [int]( int )] foo() { // int (*foo())( int ) 823 int `i` = 7; 824 int bar( int p ) { 825 `i` += 1; // dependent on local variable 826 sout | `i` | endl; 827 } 828 return bar; // undefined because of local dependence 829 } 830 int main() { 831 * [int](int) fp = foo(); // int (*fp)(int) 832 sout | fp( 3 ) | endl; 833 } 834 \end{lstlisting} 835 because 836 837 Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine. 954 838 955 839 … … 1013 897 1014 898 \item 1015 Change: A struct is a scope in C++, not in C 1016 Rationale: Class scope is crucial to C++, and a struct is a class. 1017 Effect on original feature: Change to semantics of well-defined feature. 1018 Difficulty of converting: Semantic transformation. 1019 How widely used: C programs use struct extremely frequently, but the change is only noticeable when 1020 struct, enumeration, or enumerator names are referred to outside the struct. The latter is probably 1021 rare. 899 Change: A struct is a scope in C++, not in C \\ 900 Rationale: Class scope is crucial to C++, and a struct is a class. \\ 901 Effect on original feature: Change to semantics of well-defined feature. \\ 902 Difficulty of converting: Semantic transformation. \\ 903 How widely used: C programs use struct extremely frequently, but the change is only noticeable when struct, enumeration, or enumerator names are referred to outside the struct. 904 The latter is probably rare. 905 906 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \CC. 907 Nested types are not hoisted and can be referenced using the field selection operator ``\lstinline@.@'', unlike the \CC scope-resolution operator ``\lstinline@::@''. 908 Given that nested types in C are equivalent to not using them, i.e., they are essentially useless, it is unlikely there are any realistic usages that break because of this incompatibility. 909 1022 910 1023 911 \item … … 1185 1073 First the right-hand tuple is flattened and then the values are assigned individually. 1186 1074 Flattening is also performed on tuple types. 1187 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type lstinline@[ int, int, int, int ]@.1075 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type \lstinline@[ int, int, int, int ]@. 1188 1076 1189 1077 A \newterm{structuring coercion} is the opposite of flattening; … … 1352 1240 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1353 1241 \begin{lstlisting} 1354 L1:for ( ... ) {1355 L2:for ( ... ) {1356 L3:for ( ... ) {1357 ... break L1; ...1358 ... break L2; ...1359 ... break L3; // or break1242 `L1:` for ( ... ) { 1243 `L2:` for ( ... ) { 1244 `L3:` for ( ... ) { 1245 ... break `L1`; ... 1246 ... break `L2`; ... 1247 ... break `L3`; // or break 1360 1248 } 1361 1249 } … … 1382 1270 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1383 1271 \begin{lstlisting} 1384 L1: for ( ... ) {1385 L2: for ( ... ) {1386 L3: for ( ... ) {1387 ... continue L1; ...1388 ... continue L2; ...1389 ... continue L3; ...1272 `L1`: for ( ... ) { 1273 `L2`: for ( ... ) { 1274 `L3`: for ( ... ) { 1275 ... continue `L1`; ... 1276 ... continue `L2`; ... 1277 ... continue `L3`; ... 1390 1278 1391 1279 } … … 1623 1511 \begin{lstlisting} 1624 1512 switch ( i ) { 1625 case 1, 3, 5:1513 `case 1, 3, 5`: 1626 1514 ... 1627 case 2, 4, 6:1515 `case 2, 4, 6`: 1628 1516 ... 1629 1517 } … … 1634 1522 case 1: case 3 : case 5: 1635 1523 ... 1636 case 2: case 4 : case 6: /* even values */1524 case 2: case 4 : case 6: 1637 1525 ... 1638 1526 } … … 1655 1543 \begin{lstlisting} 1656 1544 switch ( i ) { 1657 case 1~51545 `case 1~5:` 1658 1546 ... 1659 case 10~151547 `case 10~15:` 1660 1548 ... 1661 1549 } … … 1672 1560 & 1673 1561 \begin{lstlisting} 1562 1674 1563 // 1, 2, 3, 4, 5 1675 1564 … … 2168 2057 2169 2058 2170 \section{Generics } 2059 \section{Auto Type-Inferencing} 2060 2061 Auto type-inferencing occurs in a declaration where a variable's type is inferred from its initialization expression type. 2062 \begin{quote2} 2063 \begin{tabular}{@{}l@{\hspace{30pt}}ll@{}} 2064 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CC}} & \multicolumn{1}{c}{\lstinline@gcc@}\index{gcc} \\ 2065 \begin{lstlisting} 2066 2067 auto j = 3.0 * 4; 2068 int i; 2069 auto k = i; 2070 \end{lstlisting} 2071 & 2072 \begin{lstlisting} 2073 #define expr 3.0 * i 2074 typeof(expr) j = expr; 2075 int i; 2076 typeof(i) k = i; 2077 \end{lstlisting} 2078 & 2079 \begin{lstlisting} 2080 2081 // use type of initialization expression 2082 2083 // use type of primary variable 2084 \end{lstlisting} 2085 \end{tabular} 2086 \end{quote2} 2087 The two important capabilities are: 2088 \begin{itemize} 2089 \item 2090 preventing having to determine or write out long generic types, 2091 \item 2092 ensure secondary variables, related to a primary variable, always have the same type. 2093 \end{itemize} 2094 2095 In \CFA, \lstinline@typedef@ provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name. 2096 \lstinline@gcc@ provides \lstinline@typeof@ to declare a secondary variable from a primary variable. 2097 \CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression. 2098 Only for overloaded routines with the same return type is variable type-inferencing possible. 2099 Finally, \lstinline@auto@ presents the programming problem of tracking down a type when the type is actually needed. 2100 For example, given 2101 \begin{lstlisting} 2102 auto j = `...` 2103 \end{lstlisting} 2104 and the need to write a routine to compute using \lstinline@j@ 2105 \begin{lstlisting} 2106 void rtn( `...` parm ); 2107 rtn( j ); 2108 \end{lstlisting} 2109 A programmer must work backwards to determine the type of \lstinline@j@'s initialization expression, reconstructing the possibly long generic type-name. 2110 In this situation, having the type name or a short alias is very useful. 2111 2112 There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type. 2113 That is, when is the type of the variable more important than the type of its initialization expression. 2114 For example, if a change is made in an initialization expression, it can cause hundreds or thousands of cascading type changes and/or errors. 2115 At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes. 2116 2117 Given \lstinline@typedef@ and \lstinline@typeof@ in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time. 2118 Should a significant need arise, this feature can be revisited. 2119 2120 2121 \section{Generics} 2171 2122 2172 2123 \CFA supports parametric polymorphism to allow users to define generic functions and types. … … 2457 2408 2458 2409 2459 \section{I/O Library} 2460 \label{s:IOLibrary} 2461 2462 The goal for \CFA I/O is to make I/O as simple as possible for the general case, while fully supporting polmorphism and user defined types in a consistent way. 2463 The general case is printing out a sequence of variables separated by whitespace. 2464 \begin{lstlisting} 2465 int x = 0, y = 1, z = 2; 2466 sout | x | y | z | endl; 2467 2468 cout << x << " " << y << " " << z << endl; 2469 \end{lstlisting} 2470 The \CC form takes almost twice as many characters. 2471 2472 The logical-or operator is used because it is the lowest priority overloadable operator, other than assignment. 2473 Therefore, most output expressions do not require parenthesis. 2474 \begin{lstlisting} 2475 int x = 0, y = 1, z = 2; 2476 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; 2477 2478 cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl; 2479 \end{lstlisting} 2480 2481 Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction. 2482 2483 \begin{figure} 2484 \begin{lstlisting}[mathescape=off] 2485 #include <fstream> 2486 2487 int main() { 2488 char c; 2489 short int si; 2490 unsigned short int usi; 2491 int i; 2492 unsigned int ui; 2493 long int li; 2494 unsigned long int uli; 2495 long long int lli; 2496 unsigned long long int ulli; 2497 float f; 2498 double d; 2499 long double ld; 2500 float _Complex fc; 2501 double _Complex dc; 2502 long double _Complex ldc; 2503 char s1[10], s2[10]; 2504 2505 ifstream in; 2506 open( &in, "read.data", "r" ); 2507 2508 &in | &c 2509 | &si | &usi | &i | &ui | &li | &uli | &lli | &ulli 2510 | &f | &d | &ld 2511 | &fc | &dc | &ldc 2512 | str( s1 ) | str( s2, 10 ); 2513 2514 sout | c | ' ' | endl 2515 | si | usi | i | ui | li | uli | lli | ulli | endl 2516 | f | d | ld | endl 2517 | f | "" | d | "" | ld | endl; 2518 2519 sepSet( sout, ", $" ); 2520 sout | fc | dc | ldc | endl 2521 | sepOn | s1 | sepOff | s2 | endl 2522 | s1 | "" | s2 | endl; 2523 } 2524 2525 $ cat read.data 2526 A 1 2 3 4 5 6 7 8 1.1 1.2 1.3 1.1+2.3 1.1-2.3 1.1-2.3 abc xyz 2527 $ a.out 2528 A 2529 1 2 3 4 5 6 7 8 2530 1.1 1.2 1.3 2531 1.11.21.3 2532 1.1+2.3i, $1.1-2.3i, $1.1-2.3i 2533 , $abcxyz 2534 abcxyz 2535 \end{lstlisting} 2536 \end{figure} 2537 2538 2539 \section{Standard Library} 2540 \label{s:StandardLibrary} 2541 2542 The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions. 2543 2544 2545 \subsection{malloc} 2546 2547 \begin{lstlisting} 2548 forall( otype T ) T * malloc( void ); 2549 forall( otype T ) T * malloc( char fill ); 2550 forall( otype T ) T * malloc( T * ptr, size_t size ); 2551 forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ); 2552 forall( otype T ) T * calloc( size_t size ); 2553 forall( otype T ) T * realloc( T * ptr, size_t size ); 2554 forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ); 2555 2556 forall( otype T ) T * aligned_alloc( size_t alignment ); 2557 forall( otype T ) T * memalign( size_t alignment ); // deprecated 2558 forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ); 2559 2560 forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill 2561 forall( otype T ) T * memset( T * ptr ); // remove when default value available 2562 \end{lstlisting} 2563 2564 2565 \subsection{ato/strto} 2566 2567 \begin{lstlisting} 2568 int ato( const char * ptr ); 2569 unsigned int ato( const char * ptr ); 2570 long int ato( const char * ptr ); 2571 unsigned long int ato( const char * ptr ); 2572 long long int ato( const char * ptr ); 2573 unsigned long long int ato( const char * ptr ); 2574 float ato( const char * ptr ); 2575 double ato( const char * ptr ); 2576 long double ato( const char * ptr ); 2577 float _Complex ato( const char * ptr ); 2578 double _Complex ato( const char * ptr ); 2579 long double _Complex ato( const char * ptr ); 2580 2581 int strto( const char * sptr, char ** eptr, int base ); 2582 unsigned int strto( const char * sptr, char ** eptr, int base ); 2583 long int strto( const char * sptr, char ** eptr, int base ); 2584 unsigned long int strto( const char * sptr, char ** eptr, int base ); 2585 long long int strto( const char * sptr, char ** eptr, int base ); 2586 unsigned long long int strto( const char * sptr, char ** eptr, int base ); 2587 float strto( const char * sptr, char ** eptr ); 2588 double strto( const char * sptr, char ** eptr ); 2589 long double strto( const char * sptr, char ** eptr ); 2590 float _Complex strto( const char * sptr, char ** eptr ); 2591 double _Complex strto( const char * sptr, char ** eptr ); 2592 long double _Complex strto( const char * sptr, char ** eptr ); 2593 \end{lstlisting} 2594 2595 2596 \subsection{bsearch/qsort} 2597 2598 \begin{lstlisting} 2599 forall( otype T | { int ?<?( T, T ); } ) 2600 T * bsearch( const T key, const T * arr, size_t dimension ); 2601 2602 forall( otype T | { int ?<?( T, T ); } ) 2603 void qsort( const T * arr, size_t dimension ); 2604 \end{lstlisting} 2605 2606 2607 \subsection{abs} 2608 2609 \begin{lstlisting} 2610 char abs( char ); 2611 extern "C" { 2612 int abs( int ); // use default C routine for int 2613 } // extern 2614 long int abs( long int ); 2615 long long int abs( long long int ); 2616 float abs( float ); 2617 double abs( double ); 2618 long double abs( long double ); 2619 float _Complex abs( float _Complex ); 2620 double _Complex abs( double _Complex ); 2621 long double _Complex abs( long double _Complex ); 2622 \end{lstlisting} 2623 2624 2625 \subsection{random} 2626 2627 \begin{lstlisting} 2628 void randseed( long int s ); 2629 char random(); 2630 int random(); 2631 unsigned int random(); 2632 long int random(); 2633 unsigned long int random(); 2634 float random(); 2635 double random(); 2636 float _Complex random(); 2637 double _Complex random(); 2638 long double _Complex random(); 2639 \end{lstlisting} 2640 2641 2642 \subsection{min/max/swap} 2643 2644 \begin{lstlisting} 2645 forall( otype T | { int ?<?( T, T ); } ) 2646 T min( const T t1, const T t2 ); 2647 2648 forall( otype T | { int ?>?( T, T ); } ) 2649 T max( const T t1, const T t2 ); 2650 2651 forall( otype T ) 2652 void swap( T * t1, T * t2 ); 2653 \end{lstlisting} 2410 \section{Syntactic Anomalies} 2411 2412 The number 0 and 1 are treated specially in \CFA, and can be redefined as variables. 2413 One syntactic anomaly is when a field in an structure is names 0 or 1: 2414 \begin{lstlisting} 2415 struct S { 2416 int 0, 1; 2417 } s; 2418 \end{lstlisting} 2419 The problem occurs in accesing these fields using the selection operation ``\lstinline@.@'': 2420 \begin{lstlisting} 2421 s.0 = 0; // ambiguity with floating constant .0 2422 s.1 = 1; // ambiguity with floating constant .1 2423 \end{lstlisting} 2424 To make this work, a space is required after the field selection: 2425 \begin{lstlisting} 2426 `s.@\textvisiblespace@0` = 0; 2427 `s.@\textvisiblespace@1` = 1; 2428 \end{lstlisting} 2429 While this sytact is awkward, it is unlikely many programers will name fields of a structure 0 or 1. 2430 Like the \CC lexical problem with closing template-syntax, e.g, \lstinline@Foo<Bar<int`>>`@, this issue can be solved with a more powerful lexer/parser. 2431 2432 There are several ambiguous cases with operator identifiers, e.g., \lstinline@int *?*?()@, where the string \lstinline@*?*?@ can be lexed as \lstinline@*@/\lstinline@?*?@ or \lstinline@*?@/\lstinline@*?@. 2433 Since it is common practise to put a unary operator juxtaposed to an identifier, e.g., \lstinline@*i@, users will be annoyed if they cannot do this with respect to operator identifiers. 2434 Even with this special hack, there are 5 general cases that cannot be handled. 2435 The first case is for the function-call identifier \lstinline@?()@: 2436 \begin{lstlisting} 2437 int *@\textvisiblespace@?()(); // declaration: space required after '*' 2438 *@\textvisiblespace@?()(); // expression: space required after '*' 2439 \end{lstlisting} 2440 Without the space, the string \lstinline@*?()@ is ambiguous without N character look ahead; 2441 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument/parameter list. 2442 2443 The 4 remaining cases occur in expressions: 2444 \begin{lstlisting} 2445 i++@\textvisiblespace@?i:0; // space required before '?' 2446 i--@\textvisiblespace@?i:0; // space required before '?' 2447 i@\textvisiblespace@?++i:0; // space required after '?' 2448 i@\textvisiblespace@?--i:0; // space required after '?' 2449 \end{lstlisting} 2450 In the first two cases, the string \lstinline@i++?@ is ambiguous, where this string can be lexed as \lstinline@i@ / \lstinline@++?@ or \lstinline@i++@ / \lstinline@?@; 2451 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list. 2452 In the second two cases, the string \lstinline@?++x@ is ambiguous, where this string can be lexed as \lstinline@?++@ / \lstinline@x@ or \lstinline@?@ / y\lstinline@++x@; 2453 it requires scanning ahead to determine if there is a \lstinline@'('@, which is the start of an argument list. 2654 2454 2655 2455 … … 2676 2476 2677 2477 task creates a type with implicit locking, separate stack, and a thread 2478 2678 2479 2679 2480 \subsection{Monitors} … … 3775 3576 \multicolumn{1}{c|}{\textbf{\CFA/\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ 3776 3577 \hline 3777 \begin{lstlisting} 3578 \begin{lstlisting}[boxpos=t] 3778 3579 extern "C" { 3779 3580 #include <sys/types.h> … … 3782 3583 } 3783 3584 size_t fileSize( const char *path ) { 3784 st at s;3585 struct stat s; 3785 3586 stat(path, &s); 3786 3587 return s.st_size; … … 3788 3589 \end{lstlisting} 3789 3590 & 3790 \begin{lstlisting} 3591 \begin{lstlisting}[boxpos=t] 3791 3592 /* 3792 3593 #cgo … … 3807 3608 \end{lstlisting} 3808 3609 & 3809 \begin{lstlisting} 3610 \begin{lstlisting}[boxpos=t] 3810 3611 use libc::{c_int, size_t}; 3811 3812 // The following declarations are3813 3612 // translated from sys/stat.h 3814 3613 #[repr(C)] … … 3818 3617 ... 3819 3618 } 3820 3821 3619 #[link(name = "libc")] 3822 3620 extern { … … 3824 3622 buf: *mut stat_t) -> c_int; 3825 3623 } 3826 3827 3624 fn fileSize(path: *const u8) -> size_t 3828 3625 { 3829 3626 unsafe { 3830 let mut buf: stat_t = uninit();3831 stat(path, &mut buf);3832 buf.st_size3627 let mut buf: stat_t = uninit(); 3628 stat(path, &mut buf); 3629 buf.st_size 3833 3630 } 3834 3631 } … … 3953 3750 3954 3751 3752 \begin{comment} 3955 3753 \subsubsection{Modules/Packages} 3956 3754 … … 4032 3830 } 4033 3831 \end{lstlisting} 3832 \end{comment} 3833 4034 3834 4035 3835 \subsubsection{Parallel Tasks} … … 4187 3987 \end{flushleft} 4188 3988 3989 \lstset{basicstyle=\sf\relsize{-1}} 3990 3991 4189 3992 \subsection{Summary of Language Comparison} 4190 3993 … … 4255 4058 4256 4059 4060 \appendix 4061 4062 4063 \section{I/O Library} 4064 \label{s:IOLibrary} 4065 \index{input/output library} 4066 4067 The goal for \CFA I/O is to make I/O as simple as possible for the general case, while fully supporting polmorphism and user defined types in a consistent way. 4068 The general case is printing out a sequence of variables separated by whitespace. 4069 \begin{quote2} 4070 \begin{tabular}{@{}l@{\hspace{30pt}}l@{}} 4071 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 4072 \begin{lstlisting} 4073 int x = 0, y = 1, z = 2; 4074 `sout` `|` x `|` y `|` z `| endl`; 4075 \end{lstlisting} 4076 & 4077 \begin{lstlisting} 4078 4079 cout << x << " " << y << " " << z << endl; 4080 \end{lstlisting} 4081 \end{tabular} 4082 \end{quote2} 4083 The \CFA form is half as many characters, and is similar to Python I/O with respect to implicit separators. 4084 4085 The logical-or operator is used because it is the lowest-priority overloadable operator, other than assignment. 4086 Therefore, fewer output expressions require parenthesis. 4087 \begin{quote2} 4088 \begin{tabular}{@{}ll@{}} 4089 \textbf{\CFA:} 4090 & 4091 \begin{lstlisting} 4092 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; 4093 \end{lstlisting} 4094 \\ 4095 \textbf{\CC:} 4096 & 4097 \begin{lstlisting} 4098 cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl; 4099 \end{lstlisting} 4100 \end{tabular} 4101 \end{quote2} 4102 Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction. 4103 4104 The implicit seperator\index{I/O separator} character (space/blank) is a separator not a terminator. 4105 The rules for implicitly adding the separator are: 4106 \begin{enumerate} 4107 \item 4108 A seperator does not appear at the start or end of a line. 4109 \begin{lstlisting}[belowskip=0pt] 4110 sout 1 | 2 | 3 | endl; 4111 \end{lstlisting} 4112 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4113 1 2 3 4114 \end{lstlisting} 4115 \item 4116 A seperator does not appear before or after a character literal or variable. 4117 \begin{lstlisting} 4118 sout | '1' | '2' | '3' | endl; 4119 123 4120 \end{lstlisting} 4121 \item 4122 A seperator does not appear before or after a null (empty) C string 4123 \begin{lstlisting} 4124 sout | 1 | "" | 2 | "" | 3 | endl; 4125 123 4126 \end{lstlisting} 4127 which is a local mechanism to disable insertion of the separator character. 4128 \item 4129 A seperator does not appear before a C string starting with the \Index{extended ASCII}\index{ASCII} characters: \lstinline[mathescape=off]@([{$£¥¿«@ 4130 %$ 4131 \begin{lstlisting}[mathescape=off] 4132 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x $" | 4 | "x £" | 5 | "x ¥" | 6 | "x ¿" | 7 | "x «" | 8 | endl; 4133 \end{lstlisting} 4134 %$ 4135 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4136 x (1 x [2 x {3 x $4 x £5 x ¥6 x ¿7 x «8 4137 \end{lstlisting} 4138 %$ 4139 \item 4140 A seperator does not appear after a C string ending with the extended ASCII characters: \lstinline@,.:;!?)]}%¢»@ 4141 \begin{lstlisting}[belowskip=0pt] 4142 sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 | ") x" | 8 | "] x" | 9 | "} x" 4143 | 10 | "% x" | 11 | L"¢ x" | 12 | L"» x" | endl; 4144 \end{lstlisting} 4145 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4146 1, x 2. x 3: x 4; x 5! x 6? x 7) x 8] x 9} x 10% x 11¢ 12» 4147 \end{lstlisting} 4148 \item 4149 A seperator does not appear before or after a C string begining/ending with the characters: \lstinline@\f\n\r\t\v\`'"@ 4150 \begin{lstlisting}[belowskip=0pt] 4151 sout | "x '" | 1 | "' x \`" | 2 | "\` x \"" | 3 | "\" x" | endl; 4152 \end{lstlisting} 4153 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4154 x '1' x \`2\` x "3" x 4155 \end{lstlisting} 4156 \begin{lstlisting}[showtabs=true,aboveskip=0pt] 4157 sout | "x\t" | 1 | "\tx" | endl; 4158 x 1 x 4159 \end{lstlisting} 4160 \end{enumerate} 4161 The following \CC-style \Index{manipulator}s allow further control over implicit seperation. 4162 \begin{lstlisting}[mathescape=off,belowskip=0pt] 4163 sout | sepOn | 1 | 2 | 3 | sepOn | endl; // separator at start of line 4164 \end{lstlisting} 4165 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4166 1 2 3 4167 \end{lstlisting} 4168 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4169 sout | 1 | sepOff | 2 | 3 | endl; // turn off implicit separator temporarily 4170 \end{lstlisting} 4171 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4172 12 3 4173 \end{lstlisting} 4174 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4175 sout | sepDisable | 1 | 2 | 3 | endl; // turn off implicit separation, affects all subsequent prints 4176 \end{lstlisting} 4177 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4178 123 4179 \end{lstlisting} 4180 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4181 sout | 1 | sepOn | 2 | 3 | endl; // turn on implicit separator temporarily 4182 \end{lstlisting} 4183 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4184 1 23 4185 \end{lstlisting} 4186 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] 4187 sout | sepEnable | 1 | 2 | 3 | endl; // turn on implicit separation, affects all subsequent prints 4188 \end{lstlisting} 4189 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 4190 1 2 3 4191 \end{lstlisting} 4192 \begin{lstlisting}[mathescape=off,aboveskip=0pt,aboveskip=0pt,belowskip=0pt] 4193 sepSet( sout, ", $" ); // change separator from " " to ", $" 4194 sout | 1 | 2 | 3 | endl; 4195 \end{lstlisting} 4196 %$ 4197 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt] 4198 1, $2, $3 4199 \end{lstlisting} 4200 %$ 4201 \VRef[Figure]{f:ExampleIO} shows an example of input and output I/O in \CFA. 4202 4203 \begin{figure} 4204 \begin{lstlisting}[mathescape=off] 4205 #include <fstream> 4206 4207 int main() { 4208 char c; // basic types 4209 short int si; 4210 unsigned short int usi; 4211 int i; 4212 unsigned int ui; 4213 long int li; 4214 unsigned long int uli; 4215 long long int lli; 4216 unsigned long long int ulli; 4217 float f; 4218 double d; 4219 long double ld; 4220 float _Complex fc; 4221 double _Complex dc; 4222 long double _Complex ldc; 4223 char s1[10], s2[10]; 4224 4225 ifstream in; // create / open file 4226 open( &in, "input.data", "r" ); 4227 4228 &in | &c // character 4229 | &si | &usi | &i | &ui | &li | &uli | &lli | &ulli // integral 4230 | &f | &d | &ld // floating point 4231 | &fc | &dc | &ldc // floating-point complex 4232 | cstr( s1 ) | cstr( s2, 10 ); // C string, length unchecked and checked 4233 4234 sout | c | ' ' | endl // character 4235 | si | usi | i | ui | li | uli | lli | ulli | endl // integral 4236 | f | d | ld | endl // floating point 4237 | fc | dc | ldc | endl; // complex 4238 sout | endl; 4239 sout | f | "" | d | "" | ld | endl // floating point without separator 4240 | sepDisable | fc | dc | ldc | sepEnable | endl // complex without separator 4241 | sepOn | s1 | sepOff | s2 | endl // local separator removal 4242 | s1 | "" | s2 | endl; // C string withou separator 4243 sout | endl; 4244 sepSet( sout, ", $" ); // change separator, maximum of 15 characters 4245 sout | f | d | ld | endl // floating point without separator 4246 | fc | dc | ldc | endl // complex without separator 4247 | s1 | s2 | endl; 4248 } 4249 4250 $ cat input.data 4251 A 1 2 3 4 5 6 7 8 1.1 1.2 1.3 1.1+2.3 1.1-2.3 1.1-2.3 abc xyz 4252 $ a.out 4253 A 4254 1 2 3 4 5 6 7 8 4255 1.1 1.2 1.3 4256 1.1+2.3i 1.1-2.3i 1.1-2.3i 4257 4258 1.11.21.3 4259 1.1+2.3i1.1-2.3i1.1-2.3i 4260 abcxyz 4261 abcxyz 4262 4263 1.1, $1.2, $1.3 4264 1.1+2.3i, $1.1-2.3i, $1.1-2.3i 4265 abc, $xyz 4266 \end{lstlisting} 4267 \caption{Example I/O} 4268 \label{f:ExampleIO} 4269 \end{figure} 4270 4271 4272 \section{Standard Library} 4273 \label{s:StandardLibrary} 4274 4275 The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions. 4276 4277 4278 \subsection{malloc} 4279 4280 \begin{lstlisting} 4281 forall( otype T ) T * malloc( void ); 4282 forall( otype T ) T * malloc( char fill ); 4283 forall( otype T ) T * malloc( T * ptr, size_t size ); 4284 forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ); 4285 forall( otype T ) T * calloc( size_t size ); 4286 forall( otype T ) T * realloc( T * ptr, size_t size ); 4287 forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ); 4288 4289 forall( otype T ) T * aligned_alloc( size_t alignment ); 4290 forall( otype T ) T * memalign( size_t alignment ); // deprecated 4291 forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ); 4292 4293 forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill 4294 forall( otype T ) T * memset( T * ptr ); // remove when default value available 4295 \end{lstlisting} 4296 4297 4298 \subsection{ato/strto} 4299 4300 \begin{lstlisting} 4301 int ato( const char * ptr ); 4302 unsigned int ato( const char * ptr ); 4303 long int ato( const char * ptr ); 4304 unsigned long int ato( const char * ptr ); 4305 long long int ato( const char * ptr ); 4306 unsigned long long int ato( const char * ptr ); 4307 float ato( const char * ptr ); 4308 double ato( const char * ptr ); 4309 long double ato( const char * ptr ); 4310 float _Complex ato( const char * ptr ); 4311 double _Complex ato( const char * ptr ); 4312 long double _Complex ato( const char * ptr ); 4313 4314 int strto( const char * sptr, char ** eptr, int base ); 4315 unsigned int strto( const char * sptr, char ** eptr, int base ); 4316 long int strto( const char * sptr, char ** eptr, int base ); 4317 unsigned long int strto( const char * sptr, char ** eptr, int base ); 4318 long long int strto( const char * sptr, char ** eptr, int base ); 4319 unsigned long long int strto( const char * sptr, char ** eptr, int base ); 4320 float strto( const char * sptr, char ** eptr ); 4321 double strto( const char * sptr, char ** eptr ); 4322 long double strto( const char * sptr, char ** eptr ); 4323 float _Complex strto( const char * sptr, char ** eptr ); 4324 double _Complex strto( const char * sptr, char ** eptr ); 4325 long double _Complex strto( const char * sptr, char ** eptr ); 4326 \end{lstlisting} 4327 4328 4329 \subsection{bsearch/qsort} 4330 4331 \begin{lstlisting} 4332 forall( otype T | { int ?<?( T, T ); } ) 4333 T * bsearch( const T key, const T * arr, size_t dimension ); 4334 4335 forall( otype T | { int ?<?( T, T ); } ) 4336 void qsort( const T * arr, size_t dimension ); 4337 \end{lstlisting} 4338 4339 4340 \subsection{abs} 4341 4342 \begin{lstlisting} 4343 char abs( char ); 4344 extern "C" { 4345 int abs( int ); // use default C routine for int 4346 } // extern "C" 4347 long int abs( long int ); 4348 long long int abs( long long int ); 4349 float abs( float ); 4350 double abs( double ); 4351 long double abs( long double ); 4352 float _Complex abs( float _Complex ); 4353 double _Complex abs( double _Complex ); 4354 long double _Complex abs( long double _Complex ); 4355 \end{lstlisting} 4356 4357 4358 \subsection{floor/ceil} 4359 4360 \begin{lstlisting} 4361 float floor( float ); 4362 extern "C" { 4363 double floor( double ); // use C routine for double 4364 } // extern "C" 4365 long double floor( long double ); 4366 4367 float ceil( float ); 4368 extern "C" { 4369 double ceil( double ); // use C routine for double 4370 } // extern "C" 4371 long double ceil( long double ); 4372 \end{lstlisting} 4373 4374 4375 \subsection{random} 4376 4377 \begin{lstlisting} 4378 void rand48seed( long int s ); 4379 char rand48(); 4380 int rand48(); 4381 unsigned int rand48(); 4382 long int rand48(); 4383 unsigned long int rand48(); 4384 float rand48(); 4385 double rand48(); 4386 float _Complex rand48(); 4387 double _Complex rand48(); 4388 long double _Complex rand48(); 4389 \end{lstlisting} 4390 4391 4392 \subsection{min/max/swap} 4393 4394 \begin{lstlisting} 4395 forall( otype T | { int ?<?( T, T ); } ) 4396 T min( const T t1, const T t2 ); 4397 4398 forall( otype T | { int ?>?( T, T ); } ) 4399 T max( const T t1, const T t2 ); 4400 4401 forall( otype T ) 4402 void swap( T * t1, T * t2 ); 4403 \end{lstlisting} 4404 4405 4406 \section{Rational Numbers} 4407 \label{s:RationalNumbers} 4408 4409 Rational numbers are numbers written as a ratio, i.e., as a fraction, where the numerator (top number) and the denominator (bottom number) are whole numbers. 4410 When creating and computing with rational numbers, results are constantly reduced to keep the numerator and denominator as small as possible. 4411 4412 \begin{lstlisting} 4413 // implementation 4414 struct Rational { 4415 long int numerator, denominator; // invariant: denominator > 0 4416 }; // Rational 4417 4418 // constants 4419 extern struct Rational 0; 4420 extern struct Rational 1; 4421 4422 // constructors 4423 Rational rational(); 4424 Rational rational( long int n ); 4425 Rational rational( long int n, long int d ); 4426 4427 // getter/setter for numerator/denominator 4428 long int numerator( Rational r ); 4429 long int numerator( Rational r, long int n ); 4430 long int denominator( Rational r ); 4431 long int denominator( Rational r, long int d ); 4432 4433 // comparison 4434 int ?==?( Rational l, Rational r ); 4435 int ?!=?( Rational l, Rational r ); 4436 int ?<?( Rational l, Rational r ); 4437 int ?<=?( Rational l, Rational r ); 4438 int ?>?( Rational l, Rational r ); 4439 int ?>=?( Rational l, Rational r ); 4440 4441 // arithmetic 4442 Rational -?( Rational r ); 4443 Rational ?+?( Rational l, Rational r ); 4444 Rational ?-?( Rational l, Rational r ); 4445 Rational ?*?( Rational l, Rational r ); 4446 Rational ?/?( Rational l, Rational r ); 4447 4448 // conversion 4449 double widen( Rational r ); 4450 Rational narrow( double f, long int md ); 4451 4452 // I/O 4453 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); 4454 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); 4455 \end{lstlisting} 4456 4457 4257 4458 \bibliographystyle{plain} 4258 \bibliography{ /usr/local/bibliographies/pl.bib}4459 \bibliography{cfa} 4259 4460 4260 4461
Note: See TracChangeset
for help on using the changeset viewer.