Changes in doc/user/user.tex [e55ca05:6c91065]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (34 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
re55ca05 r6c91065 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%2 %%3 %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo4 %%5 %% The contents of this file are covered under the licence agreement in the6 %% file "LICENCE" distributed with Cforall.7 %%8 %% user.tex --9 %%10 %% Author : Peter A. Buhr11 %% Created On : Wed Apr 6 14:53:29 201612 %% Last Modified By : Peter A. Buhr13 %% Last Modified On : Fri Apr 8 11:40:53 201614 %% Update Count : 4215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%16 17 1 % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended 18 2 … … 21 5 22 6 % Latex packages used in the document. 23 \usepackage[T1]{fontenc} 24 \usepackage{textcomp} 25 \usepackage[latin1]{inputenc} 26 \usepackage{upquote} 7 27 8 \usepackage{fullpage,times} 28 9 \usepackage{xspace} … … 43 24 % Names used in the document. 44 25 26 \newcommand{\CFA}{C$\forall$\xspace} % set language symbolic name 27 \newcommand{\CFL}{Cforall\xspace} % set language text name 28 \newcommand{\CC}{C\kern-.1em\hbox{+\kern-.25em+}\xspace} % CC symbolic name 29 \def\c11{ISO/IEC C} % C11 name (cannot have numbers in latex command name) 45 30 \newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace} 46 31 … … 48 33 49 34 % Bespoke macros used in the document. 50 \input{common} 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 51 192 52 193 \setcounter{secnumdepth}{3} % number subsubsections … … 225 366 226 367 The command \lstinline@cfa@ is used to compile \CFA program(s). 227 This command works like the GNU \lstinline@gcc@ \index{gcc}command, e.g.:368 This command works like the GNU \lstinline@gcc@ command, e.g.: 228 369 \begin{lstlisting} 229 370 cfa [ gcc-options ] C/@{\CFA}@-files [ assembler/loader-files ] 230 371 \end{lstlisting} 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: 372 The following additional option is available: 238 373 \begin{description} 239 374 \item … … 247 382 Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, e.g.: 248 383 \begin{lstlisting} 249 2 `_`147`_`483`_`648; // decimal constant384 2_147_483_648; // decimal constant 250 385 56_ul; // decimal unsigned long constant 251 386 0_377; // octal constant … … 316 451 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 317 452 \begin{lstlisting} 318 `* int x, y;` 453 * int x, y; 319 454 \end{lstlisting} 320 455 & … … 436 571 The point of the new syntax is to allow returning multiple values from a routine~\cite{CLU,Galletly96}, e.g.: 437 572 \begin{lstlisting} 438 `[ int o1, int o2, char o3 ]`f( int i1, char i2, char i3 ) {573 [ int o1, int o2, char o3 ] f( int i1, char i2, char i3 ) { 439 574 @\emph{routine body}@ 440 575 } … … 504 639 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: 505 640 \begin{lstlisting} 506 `[ int x ]`f() {641 [ int x ] f() { 507 642 ... x = 0; ... x = y; ... 508 `return;`// implicitly return x643 return; // implicitly return x 509 644 } 510 645 \end{lstlisting} … … 562 697 for example, the following is incorrect: 563 698 \begin{lstlisting} 564 * [ int x ] f () fp; // routine name "f"is not allowed699 * [ int x ] f () fp; // routine name ``f'' is not allowed 565 700 \end{lstlisting} 566 701 … … 729 864 \subsection{Type Nesting} 730 865 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.866 C allows \Index{type nesting}, but the nested types are hoisted\index{type!hoisting} (refactored) into the enclosing scope. 732 867 \begin{quote2} 733 868 \begin{tabular}{@{}l@{\hspace{30pt}}l|l@{}} … … 784 919 785 920 int fred() { 786 s.t.c = `S.`R; // type qualification787 struct `S.`T t = { `S.`R, 1, 2 };788 enum `S.`C c;789 union `S.T.`U u;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; 790 925 } 791 926 \end{lstlisting} 792 927 \end{tabular} 793 928 \end{quote2} 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@::@''. 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. 796 933 797 934 … … 807 944 \begin{lstlisting} 808 945 const unsigned int size = 10; 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. 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} 838 954 839 955 … … 897 1013 898 1014 \item 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 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. 910 1022 911 1023 \item … … 1073 1185 First the right-hand tuple is flattened and then the values are assigned individually. 1074 1186 Flattening is also performed on tuple types. 1075 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type \lstinline@[ int, int, int, int ]@.1187 For example, the type \lstinline@[ int, [ int, int ], int ]@ can be coerced, using flattening, into the type lstinline@[ int, int, int, int ]@. 1076 1188 1077 1189 A \newterm{structuring coercion} is the opposite of flattening; … … 1240 1352 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1241 1353 \begin{lstlisting} 1242 `L1:`for ( ... ) {1243 `L2:`for ( ... ) {1244 `L3:`for ( ... ) {1245 ... break `L1`; ...1246 ... break `L2`; ...1247 ... break `L3`; // or break1354 L1: for ( ... ) { 1355 L2: for ( ... ) { 1356 L3: for ( ... ) { 1357 ... break L1; ... 1358 ... break L2; ... 1359 ... break L3; // or break 1248 1360 } 1249 1361 } … … 1270 1382 \multicolumn{1}{c@{\hspace{30pt}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1271 1383 \begin{lstlisting} 1272 `L1`: for ( ... ) {1273 `L2`: for ( ... ) {1274 `L3`: for ( ... ) {1275 ... continue `L1`; ...1276 ... continue `L2`; ...1277 ... continue `L3`; ...1384 L1: for ( ... ) { 1385 L2: for ( ... ) { 1386 L3: for ( ... ) { 1387 ... continue L1; ... 1388 ... continue L2; ... 1389 ... continue L3; ... 1278 1390 1279 1391 } … … 1511 1623 \begin{lstlisting} 1512 1624 switch ( i ) { 1513 `case 1, 3, 5`:1625 case 1, 3, 5: 1514 1626 ... 1515 `case 2, 4, 6`:1627 case 2, 4, 6: 1516 1628 ... 1517 1629 } … … 1522 1634 case 1: case 3 : case 5: 1523 1635 ... 1524 case 2: case 4 : case 6: 1636 case 2: case 4 : case 6: /* even values */ 1525 1637 ... 1526 1638 } … … 1543 1655 \begin{lstlisting} 1544 1656 switch ( i ) { 1545 `case 1~5:`1657 case 1~5 1546 1658 ... 1547 `case 10~15:`1659 case 10~15 1548 1660 ... 1549 1661 } … … 1560 1672 & 1561 1673 \begin{lstlisting} 1562 1563 1674 // 1, 2, 3, 4, 5 1564 1675 … … 2057 2168 2058 2169 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} 2170 \section{Generics } 2122 2171 2123 2172 \CFA supports parametric polymorphism to allow users to define generic functions and types. … … 2408 2457 2409 2458 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. 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} 2454 2654 2455 2655 … … 2476 2676 2477 2677 task creates a type with implicit locking, separate stack, and a thread 2478 2479 2678 2480 2679 \subsection{Monitors} … … 3576 3775 \multicolumn{1}{c|}{\textbf{\CFA/\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ 3577 3776 \hline 3578 \begin{lstlisting} [boxpos=t]3777 \begin{lstlisting} 3579 3778 extern "C" { 3580 3779 #include <sys/types.h> … … 3583 3782 } 3584 3783 size_t fileSize( const char *path ) { 3585 st ruct stat s;3784 stat s; 3586 3785 stat(path, &s); 3587 3786 return s.st_size; … … 3589 3788 \end{lstlisting} 3590 3789 & 3591 \begin{lstlisting} [boxpos=t]3790 \begin{lstlisting} 3592 3791 /* 3593 3792 #cgo … … 3608 3807 \end{lstlisting} 3609 3808 & 3610 \begin{lstlisting} [boxpos=t]3809 \begin{lstlisting} 3611 3810 use libc::{c_int, size_t}; 3811 3812 // The following declarations are 3612 3813 // translated from sys/stat.h 3613 3814 #[repr(C)] … … 3617 3818 ... 3618 3819 } 3820 3619 3821 #[link(name = "libc")] 3620 3822 extern { … … 3622 3824 buf: *mut stat_t) -> c_int; 3623 3825 } 3826 3624 3827 fn fileSize(path: *const u8) -> size_t 3625 3828 { 3626 3829 unsafe { 3627 let mut buf: stat_t = uninit();3628 stat(path, &mut buf);3629 buf.st_size3830 let mut buf: stat_t = uninit(); 3831 stat(path, &mut buf); 3832 buf.st_size 3630 3833 } 3631 3834 } … … 3750 3953 3751 3954 3752 \begin{comment}3753 3955 \subsubsection{Modules/Packages} 3754 3956 … … 3830 4032 } 3831 4033 \end{lstlisting} 3832 \end{comment}3833 3834 4034 3835 4035 \subsubsection{Parallel Tasks} … … 3987 4187 \end{flushleft} 3988 4188 3989 \lstset{basicstyle=\sf\relsize{-1}}3990 3991 3992 4189 \subsection{Summary of Language Comparison} 3993 4190 … … 4058 4255 4059 4256 4060 \appendix4061 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 \item4108 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 34114 \end{lstlisting}4115 \item4116 A seperator does not appear before or after a character literal or variable.4117 \begin{lstlisting}4118 sout | '1' | '2' | '3' | endl;4119 1234120 \end{lstlisting}4121 \item4122 A seperator does not appear before or after a null (empty) C string4123 \begin{lstlisting}4124 sout | 1 | "" | 2 | "" | 3 | endl;4125 1234126 \end{lstlisting}4127 which is a local mechanism to disable insertion of the separator character.4128 \item4129 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 «84137 \end{lstlisting}4138 %$4139 \item4140 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 \item4149 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" x4155 \end{lstlisting}4156 \begin{lstlisting}[showtabs=true,aboveskip=0pt]4157 sout | "x\t" | 1 | "\tx" | endl;4158 x 1 x4159 \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 line4164 \end{lstlisting}4165 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]4166 1 2 34167 \end{lstlisting}4168 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]4169 sout | 1 | sepOff | 2 | 3 | endl; // turn off implicit separator temporarily4170 \end{lstlisting}4171 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]4172 12 34173 \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 prints4176 \end{lstlisting}4177 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]4178 1234179 \end{lstlisting}4180 \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt]4181 sout | 1 | sepOn | 2 | 3 | endl; // turn on implicit separator temporarily4182 \end{lstlisting}4183 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]4184 1 234185 \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 prints4188 \end{lstlisting}4189 \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]4190 1 2 34191 \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, $34199 \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 types4209 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 file4226 open( &in, "input.data", "r" );4227 4228 &in | &c // character4229 | &si | &usi | &i | &ui | &li | &uli | &lli | &ulli // integral4230 | &f | &d | &ld // floating point4231 | &fc | &dc | &ldc // floating-point complex4232 | cstr( s1 ) | cstr( s2, 10 ); // C string, length unchecked and checked4233 4234 sout | c | ' ' | endl // character4235 | si | usi | i | ui | li | uli | lli | ulli | endl // integral4236 | f | d | ld | endl // floating point4237 | fc | dc | ldc | endl; // complex4238 sout | endl;4239 sout | f | "" | d | "" | ld | endl // floating point without separator4240 | sepDisable | fc | dc | ldc | sepEnable | endl // complex without separator4241 | sepOn | s1 | sepOff | s2 | endl // local separator removal4242 | s1 | "" | s2 | endl; // C string withou separator4243 sout | endl;4244 sepSet( sout, ", $" ); // change separator, maximum of 15 characters4245 sout | f | d | ld | endl // floating point without separator4246 | fc | dc | ldc | endl // complex without separator4247 | s1 | s2 | endl;4248 }4249 4250 $ cat input.data4251 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 xyz4252 $ a.out4253 A4254 1 2 3 4 5 6 7 84255 1.1 1.2 1.34256 1.1+2.3i 1.1-2.3i 1.1-2.3i4257 4258 1.11.21.34259 1.1+2.3i1.1-2.3i1.1-2.3i4260 abcxyz4261 abcxyz4262 4263 1.1, $1.2, $1.34264 1.1+2.3i, $1.1-2.3i, $1.1-2.3i4265 abc, $xyz4266 \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 ); // deprecated4291 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 fill4294 forall( otype T ) T * memset( T * ptr ); // remove when default value available4295 \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 int4346 } // 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 double4364 } // extern "C"4365 long double floor( long double );4366 4367 float ceil( float );4368 extern "C" {4369 double ceil( double ); // use C routine for double4370 } // 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 // implementation4414 struct Rational {4415 long int numerator, denominator; // invariant: denominator > 04416 }; // Rational4417 4418 // constants4419 extern struct Rational 0;4420 extern struct Rational 1;4421 4422 // constructors4423 Rational rational();4424 Rational rational( long int n );4425 Rational rational( long int n, long int d );4426 4427 // getter/setter for numerator/denominator4428 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 // comparison4434 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 // arithmetic4442 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 // conversion4449 double widen( Rational r );4450 Rational narrow( double f, long int md );4451 4452 // I/O4453 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );4454 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );4455 \end{lstlisting}4456 4457 4458 4257 \bibliographystyle{plain} 4459 \bibliography{ cfa}4258 \bibliography{/usr/local/bibliographies/pl.bib} 4460 4259 4461 4260
Note:
See TracChangeset
for help on using the changeset viewer.