source: doc/theses/mike_brooks_MMath/array.tex@ 6cbc5a62

Last change on this file since 6cbc5a62 was 8d764d4f, checked in by Peter A. Buhr <pabuhr@…>, 7 days ago

use subfloat for figure programs, inline footnote

  • Property mode set to 100644
File size: 100.3 KB
Line 
1\chapter{Array}
2\label{c:Array}
3
4Arrays in C are possibly the single most misunderstood and incorrectly used feature in the language \see{\VRef{s:Array}}, resulting in the largest proportion of runtime errors and security violations.
5This chapter describes the new \CFA language and library features that introduce a length-checked array type, @array@, to the \CFA standard library~\cite{Cforall}.
6
7Offering the @array@ type, as a distinct alternative to the C array, is consistent with \CFA's goal of backwards compatibility, \ie virtually all existing C (@gcc@) programs can be compiled by \CFA with only a small number of changes, similar to \CC (@g++@).
8However, a few compatibility-breaking changes to the behaviour of the C array are necessary, both as an implementation convenience and to fix C's lax treatment of arrays.
9Hence, the @array@ type is an opportunity to start from a clean slate and show a cohesive selection of features, making it unnecessary to deal with every inherited complexity of the C array.
10Future work is to sugar the @array@ syntax to look like C arrays \see{\VRef{f:FutureWork}}.
11
12
13\section{Introduction}
14\label{s:ArrayIntro}
15
16The new \CFA array is declared by instantiating the generic @array@ type, much like instantiating any other standard-library generic type (such as \CC @vector@), using a new style of generic parameter.
17\begin{cfa}
18@array( float, 99 )@ x; $\C[2.5in]{// x contains 99 floats}$
19\end{cfa}
20Here, the arguments to the @array@ type are @float@ (element type) and @99@ (dimension).
21When this type is used as a function parameter, the type-system requires the argument is a perfect match.
22\begin{cfa}
23void f( @array( float, 42 )@ & p ) {} $\C{// p accepts 42 floats}$
24f( x ); $\C{// statically rejected: type lengths are different, 99 != 42}$
25
26test2.cfa:3:1 error: Invalid application of existing declaration(s) in expression.
27Applying untyped: Name: f ... to: Name: x
28\end{cfa}
29Function @f@'s parameter expects an array with dimension 42, but the argument dimension 99 does not match.
30
31A function can be polymorphic over @array@ arguments using the \CFA @forall@ declaration prefix.
32\begin{cfa}
33forall( T, @[N]@ )
34void g( array( T, @N@ ) & p, int i ) {
35 T elem = p[i]; $\C{// dynamically checked: requires 0 <= i < N}$
36}
37g( x, 0 ); $\C{// T is float, N is 99, dynamic subscript check succeeds}$
38g( x, 1000 ); $\C{// T is float, N is 99, dynamic subscript check fails}$
39
40Cforall Runtime error: subscript 1000 exceeds dimension range [0,99) $for$ array 0x555555558020.
41\end{cfa}
42Function @g@ takes an arbitrary type parameter @T@ and an unsigned integer \emph{dimension} @N@.
43The dimension represents a to-be-determined number of elements, managed by the type system, where 0 represents an empty array.
44The type system implicitly infers @float@ for @T@ and @99@ for @N@.
45Furthermore, the runtime subscript @x[0]@ (parameter @i@ being @0@) in @g@ is valid because 0 is in the dimension range $[0,99)$ for argument @x@.
46The call @g( x, 1000 )@ is also accepted at compile time.
47However, the subscript, @x[1000]@, generates a runtime error, because @1000@ is outside the dimension range $[0,99)$ of argument @x@.
48In general, the @forall( ..., [N] )@ participates in the user-relevant declaration of the name @N@, which becomes usable in parameter/return declarations and within a function.
49The syntactic form is chosen to parallel other @forall@ forms:
50\begin{cfa}
51forall( @[N]@ ) ... $\C{// dimension}$
52forall( T ) ... $\C{// value datatype}$
53forall( T & ) ... $\C{// opaque datatype}$
54\end{cfa}
55% The notation @array(thing, N)@ is a single-dimensional case, giving a generic type instance.
56
57The generic @array@ type is comparable to the C array type, which \CFA inherits from C.
58Their runtime characteristics are often identical, and some features are available in both.
59For example, assume a caller has an argument that instantiates @N@ with 42.
60\begin{cfa}
61forall( [N] )
62void f( ... ) {
63 float x1[@N@]; $\C{// C array, no subscript checking}$
64 array(float, N) x2; $\C{// \CFA array, subscript checking}\CRT$
65}
66\end{cfa}
67Both of the stack declared array variables, @x1@ and @x2@, have 42 elements, each element being a @float@.
68The two variables have identical size and layout, with no additional ``bookkeeping'' allocations or headers.
69The C array, @x1@, has no subscript checking, while \CFA array, @x2@, does.
70Providing this explicit generic approach requires a significant extension to the \CFA type system to support a full-feature, safe, efficient (space and time) array-type, which forms the foundation for more complex array forms in \CFA.
71In all following discussion, ``C array'' means types like @x1@ and ``\CFA array'' means types like @x2@.
72
73A future goal is to provide the new @array@ features with syntax approaching C's (declaration style of @x1@).
74Then, the library @array@ type could be removed, giving \CFA a largely uniform array type.
75At present, the C-syntax @array@ is only partially supported, so the generic @array@ is used exclusively in the thesis.
76
77My contributions in this chapter are:
78\begin{enumerate}[leftmargin=*]
79\item A type system enhancement that lets polymorphic functions and generic types be parameterized by a numeric value: @forall( [N] )@.
80\item Provide a dimension/subscript-checked array-type in the \CFA standard library, where the array's length is statically managed and dynamically valued.
81\item Provide argument/parameter passing safety for arrays and subscript safety.
82\item Identify the interesting abilities available by the new @array@ type.
83\item Where there is a gap concerning this feature's readiness for prime-time, identification of specific workable improvements that are likely to close the gap.
84\end{enumerate}
85
86
87\begin{comment}
88\section{Dependent Typing}
89
90General dependent typing allows a type system to encode arbitrary predicates, \eg behavioural specifications for functions, which is an anti-goal for my work.
91Firstly, this application is strongly associated with pure functional languages,
92where a characterization of the return value (giving it a precise type, generally dependent upon the parameters)
93is a sufficient postcondition.
94In an imperative language like C and \CFA, it is also necessary to discuss side effects, for which an even heavier formalism, like separation logic, is required.
95Secondly: ... bash Rust.
96... cite the crap out of these claims.
97\end{comment}
98
99
100\section{Features Added}
101
102This section shows more about using the \CFA array and dimension parameters, demonstrating syntax and semantics by way of motivating examples.
103As stated, the core capability of the new array is tracking all dimensions within the type system, where dynamic dimensions are represented using type variables.
104
105By declaring type variables at the front of object declarations, an array dimension is lexically referenceable where it is needed.
106For example, a declaration can share one length, @N@, among a pair of parameters and return type, meaning the input arrays and return array are the same length.
107\lstinput{10-17}{hello-array.cfa}
108Function @f@ does a pointwise comparison of its two input arrays, checking if each pair of numbers is within half a percent of each other, returning the answers in a newly allocated @bool@ array.
109The dynamic allocation of the @ret@ array uses the library @alloc@ function,
110\begin{cfa}
111forall( T & | sized(T) )
112T * alloc() {
113 return @(T *)@malloc( @sizeof(T)@ ); // C malloc
114}
115\end{cfa}
116which captures the parameterized dimension information implicitly within its @sizeof@ determination, and casts the return type.
117Note, @alloc@ only sees the whole type for its @T@, @array(bool, N)@, where this type's size is a computation based on @N@.
118This example illustrates how the new @array@ type plugs into existing \CFA behaviour by implementing necessary \emph{sized} assertions needed by other types.
119(\emph{sized} implies a concrete \vs abstract type with a runtime-available size, exposed as @sizeof@.)
120As a result, there is significant programming safety by making the size accessible and implicit, compared with C's @calloc@ and non-array supporting @memalign@, which take an explicit length parameter not managed by the type system.
121
122\begin{figure}
123\lstinput{30-43}{hello-array.cfa}
124\lstinput{45-48}{hello-array.cfa}
125\caption{\lstinline{f} Example}
126\label{f:fExample}
127\end{figure}
128
129\VRef[Figure]{f:fExample} shows an example using function @f@, illustrating how dynamic values are fed into the @array@ type.
130Here, the dimension of arrays @x@, @y@, and @result@ is specified from a command-line value, @dim@, and these arrays are allocated on the stack.
131Then the @x@ array is initialized with decreasing values, and the @y@ array with amounts offset by constant @0.005@, giving relative differences within tolerance initially and diverging for later values.
132The program main is run (see figure bottom) with inputs @5@ and @7@ for sequence lengths.
133The loops follow the familiar pattern of using the variable @dim@ to iterate through the arrays.
134Most importantly, the type system implicitly captures @dim@ at the call of @f@ and makes it available throughout @f@ as @N@.
135The example shows @dim@ adapting into a type-system managed length at the declarations of @x@, @y@, and @result@; @N@ adapting in the same way at @f@'s loop bound; and a pass-thru use of @dim@ at @f@'s declaration of @ret@.
136Except for the lifetime-management issue of @result@, \ie explicit @free@, this program has eliminated both the syntactic and semantic problems associated with C arrays and their usage.
137The result is a significant improvement in safety and usability.
138In summary:
139\begin{itemize}[leftmargin=*]
140\item
141@[N]@ within a @forall@ declares the type variable @N@ to be a managed length.
142\item
143@N@ can be used in an expression with type @size_t@ within the function body.
144\item
145The value of an @N@-expression is the acquired length, derived from the usage site, \ie generic declaration or function call.
146\item
147@array( T, N0, N1, ... )@ is a multi-dimensional type wrapping $\prod_i N_i$ contiguous @T@-typed objects.
148\end{itemize}
149
150\VRef[Figure]{f:TemplateVsGenericType} shows @N@ is not the same as a @size_t@ declaration in a \CC \lstinline[language=C++]{template}.\footnote{
151The \CFA program requires a snapshot declaration for \lstinline{n} to compile, as described at the end of \Vref{s:ArrayTypingC}.}
152\begin{enumerate}[leftmargin=*]
153\item
154The \CC template @N@ can only be a compile-time value, while the \CFA @N@ may be a runtime value.
155\item
156\CC does not allow a template function to be nested, while \CFA allows polymorphic functions be nested.
157Hence, \CC precludes a simple form of information hiding.
158\item
159\label{p:DimensionPassing}
160The \CC template @N@ must be passed explicitly at the call, unless @N@ has a default value, even when \CC can deduct the type of @T@.
161The \CFA @N@ is part of the array type and passed implicitly at the call.
162% fixed by comparing to std::array
163% mycode/arrr/thesis-examples/check-peter/cs-cpp.cpp, v2
164\item
165\CC cannot have an array of references, but can have an array of @const@ pointers.
166\CC has a (mistaken) belief that references are not objects, but pointers are objects.
167In the \CC example, the arrays fall back on C arrays, which have a duality with references with respect to automatic dereferencing.
168The \CFA array is a contiguous object with an address, which can be stored as a reference or pointer.
169% not really about forall-N vs template-N
170% any better CFA support is how Rob left references, not what Mike did to arrays
171% https://stackoverflow.com/questions/1164266/why-are-arrays-of-references-illegal
172% https://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references
173\item
174\label{p:ArrayCopy}
175C/\CC arrays cannot be copied, while \CFA arrays can be copied, making them a first-class object (although array copy is often avoided for efficiency).
176% fixed by comparing to std::array
177% mycode/arrr/thesis-examples/check-peter/cs-cpp.cpp, v10
178\end{enumerate}
179The \CC template @std::array@ tries to accomplish a similar mechanism to \CFA @array@.
180It is an aggregate type with the same semantics as a @struct@ holding a C-style array \see{\VRef{s:ArraysCouldbeValues}}, which mitigates points \VRef[]{p:DimensionPassing} and \VRef[]{p:ArrayCopy}.
181
182\begin{figure}
183\setlength{\tabcolsep}{3pt}
184\begin{tabular}{@{}ll@{}}
185\begin{c++}
186
187@template< typename T, size_t N >@
188void copy( T ret[@N@], T x[@N@] ) {
189 for ( int i = 0; i < N; i += 1 ) ret[i] = x[i];
190}
191int main() {
192 const size_t n = 10; $\C[1.9in]{// must be constant}$
193 int ret[n], x[n]; $\C{// static size}$
194 for ( int i = 0; i < n; i += 1 ) x[i] = i;
195 @copy<int, n >( ret, x );@
196 for ( int i = 0; i < n; i += 1 ) cout << ret[i] << ' ';
197 cout << endl;
198}
199\end{c++}
200&
201\begin{cfa}
202int main() {
203 @forall( T, [N] )@ $\C{// nested function}$
204 void copy( array( T, @N@ ) & ret, array( T, @N@ ) & x ) {
205 for ( i; N ) ret[i] = x[i];
206 }
207 size_t n;
208 sin | n;
209 array( int, n ) ret, x; $\C{// dynamic-fixed size}$
210 for ( i; n ) x[i] = i; $\C{// initialize}$
211 @copy( ret, x );@ $\C{// alternative ret = x}$
212 for ( i; n ) sout | ret[i] | nonl; $\C{// print}\CRT$
213 sout | nl;
214}
215\end{cfa}
216\end{tabular}
217\caption{\lstinline{N}-style parameters, for \CC template \vs \CFA generic type }
218\label{f:TemplateVsGenericType}
219\end{figure}
220
221Just as the first example in \VRef[Section]{s:ArrayIntro} shows a compile-time rejection of a length mismatch,
222so are length mismatches stopped when they involve dimension parameters.
223While \VRef[Figure]{f:fExample} shows successfully calling a function @f@ expecting two arrays of the same length,
224\begin{cfa}
225array( bool, N ) & f( array( float, N ) &, array( float, N ) & );
226\end{cfa}
227a static rejection occurs when attempting to call @f@ with arrays of differing lengths.
228\lstinput[tabsize=1]{70-74}{hello-array.cfa}
229When the argument lengths themselves are statically unknown,
230the static check is conservative and, as always, \CFA's casting lets the programmer use knowledge not shared with the type system.
231\lstinput{90-96}{hello-array.cfa}
232This static check's rules are presented in \VRef[Section]{s:ArrayTypingC}.
233
234Orthogonally, the \CFA array type works within generic \emph{types}, \ie @forall@-on-@struct@.
235The same argument safety and the associated implicit communication of array length occurs.
236Preexisting \CFA allowed aggregate types to be generalized with type parameters, enabling parameterizing of element types.
237This feature is extended to allow parameterizing by dimension.
238Doing so gives a refinement of C's ``flexible array member''~\cite[\S~6.7.2.1.18]{C11}:
239\begin{cfa}
240struct S {
241 ...
242 double d []; // incomplete array type => flexible array member
243} * s = malloc( sizeof( struct S ) + sizeof( double [10] ) );
244\end{cfa}
245which creates a VLA of size 10 @double@s at the end of the structure.
246A C flexible array member can only occur at the end of a structure;
247\CFA allows length-parameterized array members to be nested at arbitrary locations, with intervening member declarations.
248\lstinput{10-15}{hello-accordion.cfa}
249The structure has course- and student-level metatdata (their respective field names) and a position-based preferences' matrix.
250Its layout has the starting offset of @studentIds@ varying according to the generic parameter @C@, and the offset of @preferences@ varying according to both generic parameters.
251
252\VRef[Figure]{f:checkExample} shows a program main using @School@ and results with different array sizes.
253The @school@ variable holds many students' course-preference forms.
254It is on the stack and its initialization does not use any casting or size arithmetic.
255Both of these points are impossible with a C flexible array member.
256When heap allocation is preferred, the original pattern still applies.
257\begin{cfa}
258School( classes, students ) * sp = alloc();
259\end{cfa}
260This ability to avoid casting and size arithmetic improves safety and usability over C flexible array members.
261The example program prints the courses in each student's preferred order, all using the looked-up display names.
262When a function operates on a @School@ structure, the type system handles its memory layout transparently.
263In the example, function @getPref@ returns, for the student at position @is@, what is the position of their @pref@\textsuperscript{th}-favoured class.
264Finally, inputs and outputs are given on the right for different sized schools.
265
266\begin{figure}
267\begin{lrbox}{\myboxA}
268\begin{tabular}{@{}l@{}}
269\lstinput{30-38}{hello-accordion.cfa} \\
270\lstinput{50-55}{hello-accordion.cfa} \\
271\lstinput{90-98}{hello-accordion.cfa}
272\end{tabular}
273\end{lrbox}
274
275\begin{lrbox}{\myboxB}
276\begin{tabular}{@{}l@{}}
277@$ cat school1@ \\
278\lstinputlisting{school1} \\
279@$ ./a.out < school1@ \\
280\lstinputlisting{school1.out} \\
281@$ cat school2@ \\
282\lstinputlisting{school2} \\
283@$ ./a.out < school2@ \\
284\lstinputlisting{school2.out}
285\end{tabular}
286\end{lrbox}
287
288\setlength{\tabcolsep}{10pt}
289\begin{tabular}{@{}ll@{}}
290\usebox\myboxA
291&
292\usebox\myboxB
293\end{tabular}
294
295\caption{\lstinline{School} Example, Input and Output}
296\label{f:checkExample}
297\end{figure}
298
299
300\section{Single Dimension Array Implementation}
301
302The core of the preexisting \CFA compiler already has the ``heavy equipment'' needed to provide the feature set just reviewed (up to bugs in cases not yet exercised).
303To apply this equipment in tracking array lengths, I encoded a dimension (array's length) as a type.
304The type in question does not describe any data that the program actually uses at runtime.
305It simply carries information through intermediate stages of \CFA-to-C lowering.
306And this type takes a form such that, once \emph{it} gets lowered, the result does the right thing.
307This new dimension type, encoding the array dimension within it, is the first limited \newterm{dependent type}~\cite{DependentType} added to the \CFA type-system and is used at appropriate points during type resolution.
308
309% Furthermore, the @array@ type itself is really ``icing on the cake.''
310% Presenting its full version is deferred until \VRef[Section]{s:ArrayMdImpl} (where added complexity needed for multiple dimensions is considered).
311The new array implementation is broken into single and multiple dimensions \see{\VRef[Section]{s:ArrayMdImpl}}.
312Details of the @array@ macros are sprinkles among the implementation discussion.
313The single dimension implementation begins with a simple example without @array@ macros.
314\begin{cfa}
315forall( [N] )
316struct array_1d_float {
317 float items[N]; $\C[2in]{// monomorphic type}$
318};
319forall( T, [N] )
320struct array_1d_T {
321 T items[N]; $\C{// polymorphic type}\CRT$
322};
323\end{cfa}
324These two structure patterns, plus a subscript operator, is all that @array@ provides.
325My main work is letting a programmer define such a structure (one whose type is parameterized by @[N]@) and functions that operate on it (these being similarly parameterized).
326
327The repurposed heavy equipment is
328\begin{itemize}[leftmargin=*]
329\item
330 Resolver provided values for a declaration's type-system variables, gathered from type information in scope at the usage site.
331\item
332 The box pass, encoding information about type parameters into ``extra'' regular parameters and arguments on declarations and calls.
333 Notably, it conveys the size of a type @foo@ as a @__sizeof_foo@ parameter, and rewrites the @sizeof(foo)@ expression as @__sizeof_foo@, \ie a use of the parameter.
334\end{itemize}
335
336The rules for resolution had to be restricted slightly, in order to achieve important refusal cases.
337This work is detailed in \VRef[Section]{s:ArrayTypingC}.
338However, the resolution--boxing scheme, in its preexisting state, is equipped to work on (desugared) dimension parameters.
339The following discussion explains the desugaring and how correctly lowered code results.
340
341A simpler structure, and a toy function on it, demonstrate what is needed for the encoding.
342\begin{cfa}
343forall( [@N@] ) { $\C{// [1]}$
344 struct thing {};
345 void f( thing(@N@) ) { sout | @N@; } $\C{// [2], [3]}$
346}
347int main() {
348 thing( @10@ ) x; f( x ); $\C{// prints 10, [4]}$
349 thing( @100@ ) y; f( y ); $\C{// prints 100}$
350}
351\end{cfa}
352This example has:
353\begin{enumerate}[leftmargin=*]
354\item
355 The symbol @N@ being declared as a type variable (a variable of the type system).
356\item
357 The symbol @N@ being used to parameterize a type.
358\item
359 The symbol @N@ being used as an expression (value).
360\item
361 A value like 10 being used as an argument to the parameter @N@.
362\end{enumerate}
363The chosen solution is to encode the value @N@ \emph{as a type}, so items 1 and 2 are immediately available for free.
364Item 3 needs a way to recover the encoded value from a (valid) type and to reject invalid types.
365Item 4 needs a way to produce a type that encodes the given value.
366
367Because the box pass handles a type's size as its main datum, the encoding is chosen to use it.
368The production and recovery are then straightforward.
369\begin{itemize}[leftmargin=*]
370\item
371 The value $n$ is encoded as a type whose size is $n$.
372\item
373 Given a dimension expression $e$, produce an internal type @char[@$e$@]@ to represent it.
374 If $e$ evaluates to $n$ then the encoded type has size $n$.
375\item
376 Given a type $T$ (produced by these rules), recover the value that it represents with the expression @sizeof(@$T$@)@.
377 If $T$ has size $n$ then the recovery expression evaluates to $n$.
378\end{itemize}
379
380This desugaring is applied in a translation step before the resolver.
381The ``validate'' pass hosts it, along with several other canonicalizing and desugaring transformations (the pass's name notwithstanding).
382The running example is lowered to:
383\begin{cfa}
384forall( @N *@ ) { $\C{// [1]}$
385 struct thing {};
386 void f( thing(@N@) ) { sout | @sizeof(N)@; } $\C{// [2], [3]}$
387}
388int main() {
389 thing( @char[10]@ ) x; f( x ); $\C{// prints 10, [4]}$
390 thing( @char[100]@ ) y; f( y ); $\C{// prints 100}$
391}
392\end{cfa}
393Observe:
394\begin{enumerate}[leftmargin=*]
395\item
396 @N@ is now declared to be a type.
397 It is declared to be \emph{sized} (by the @*@), meaning that the box pass shall do its @sizeof(N)@$\rightarrow$@__sizeof_N@ extra parameter and expression translation.
398\item
399 @thing(N)@ is a type; the argument to the generic @thing@ is a type (type variable).
400\item
401 The @sout...@ expression (being an application of the @?|?@ operator) has a second argument that is an ordinary expression.
402\item
403 The type of variable @x@ is another @thing(-)@ type; the argument to the generic @thing@ is a type (array type of bytes, @char[@$e$@]@).
404\end{enumerate}
405
406From this point, preexisting \CFA compilation takes over lowering it the rest of the way to C.
407Here the result shows only the relevant changes of the box pass (as informed by the resolver), leaving the rest unadulterated:
408\begin{cfa}
409// [1]
410void f( size_t __sizeof_N, @void *@ ) { sout | @__sizeof_N@; } $\C{// [2], [3]}$
411int main() {
412 struct __conc_thing_10 {} x; f( @10@, &x ); $\C{// prints 10, [4]}$
413 struct __conc_thing_100 {} y; f( @100@, &y ); $\C{// prints 100}$
414}
415\end{cfa}
416Observe:
417\begin{enumerate}[leftmargin=*]
418\item
419 The type parameter @N@ is gone.
420\item
421 The type @thing(N)@ is (replaced by @void *@, but thereby effectively) gone.
422\item
423 The @sout...@ expression has a regular variable (parameter) usage for its second argument.
424\item
425 Information about the particular @thing@ instantiation (value 10) is moved, from the type, to a regular function-call argument.
426\end{enumerate}
427At the end of the desugaring and downstream processing, the original C idiom of ``pass both a length parameter and a pointer'' has been reconstructed.
428In the programmer-written form, only the @thing@ is passed.
429The compiler's action produces the more complex form, which if handwritten, is error-prone.
430
431At the compiler front end, the parsing changes AST schema extensions and validation rules for enabling the sugared user input.
432\begin{itemize}[leftmargin=*]
433\item
434 Recognize the form @[N]@ as a type-variable declaration within a @forall@.
435\item
436 Have the new brand of type-variable, \emph{Dimension}, in the AST form of a type-variable, to represent one parsed from @[-]@.
437\item
438 Allow a type variable to occur in an expression. Validate (after parsing) that only dimension-branded type-variables are used here.
439\item
440 Allow an expression to occur in type-argument position. Brand the resulting type argument as a dimension.
441\item
442 Validate (after parsing), on a generic-type usage, \eg the type part of the declaration
443 \begin{cfa}
444 array_1d( foo, bar ) x;
445 \end{cfa}
446 \vspace*{-10pt}
447 that the brands on the generic arguments match the brands of the declared type variables.
448 Here, that @foo@ is a type and @bar@ is a dimension.
449\end{itemize}
450
451
452\section{Typing of C Arrays}
453\label{s:ArrayTypingC}
454
455Essential in giving a guarantee of accurate length is the compiler's ability to reject a program that presumes to mishandle length.
456By contrast, most discussion so far deals with communicating length, from one party who knows it, to another willing to work with any given length.
457For scenarios where the concern is a mishandled length, the interaction is between two parties who both claim to know something about it.
458
459C and \CFA can check when working with two static values.
460\begin{cfa}
461enum { n = 42 };
462float x[@n@]; $\C{// or just 42}$
463float (*xp1)[@42@] = &x; $\C{// accept}$
464float (*xp2)[@999@] = &x; $\C{// reject}$
465warning: initialization of 'float (*)[999]' from incompatible pointer type 'float (*)[42]'
466\end{cfa}
467When a variable is involved, C and \CFA take two different approaches.
468Today's C compilers accept the following without a warning.
469\begin{cfa}
470static const int n = 42;
471float x[@n@];
472float (* xp)[@999@] = &x; $\C{// should be static rejection here}$
473(*xp)[@500@]; $\C{// in "bound"?}$
474\end{cfa}
475Here, the array @x@ has length 42, while a pointer to it (@xp@) claims length 999.
476So, while the subscript of @xp@ at position 500 is out of bound with its referent @x@,
477the access appears in-bound of the type information available on @xp@.
478In fact, length is being mishandled in the previous step, where the type-carried length information on @x@ is not compatible with that of @xp@.
479
480In \CFA, I choose to reject this C example at the point where the type-carried length information on @x@ is not compatible with that of @xp@, and correspondingly, its array counterpart at the same location:
481\begin{cfa}
482static const int n = 42;
483array( float, @n@ ) x;
484array( float, @999@ ) * xp = &x; $\C{// static rejection here}$
485(*xp)[@500@]; $\C{// runtime check would pass if program compiled}$
486\end{cfa}
487The way the \CFA array is implemented, the type analysis for this case reduces to a case similar to the earlier C version.
488The \CFA compiler's compatibility analysis proceeds as:
489\begin{itemize}[leftmargin=*,parsep=0pt]
490\item
491 Is @array( float, 999 )@ type-compatible with @array( float, n )@?
492\item
493 Is desugared @array( float, char[999] )@ type-compatible with desugared @array( float, char[n] )@?
494% \footnote{
495% Here, \lstinline{arrayX} represents the type that results from desugaring the \lstinline{array} type into a type whose generic parameters are all types.
496% This presentation elides the noisy fact that \lstinline{array} is actually a macro for something bigger;
497% the reduction to \lstinline{char [-]} still proceeds as sketched.}
498\item
499 Is internal type @char[999]@ type-compatible with internal type @char[n]@?
500\end{itemize}
501The answer is false because, in general, the value of @n@ is unknown at compile time, and hence, an error is raised.
502For safety, it makes sense to reject the corresponding C case, which is a non-backwards compatible change.
503There are two mitigations for this incompatibility.
504
505First, a simple recourse is available in a situation where @n@ is \emph{known} to be 999 by using a cast.
506\begin{cfa}
507float (* xp)[999] = @(float (*)[999])@&x;
508\end{cfa}
509The cast means the programmer has accepted blame.
510Moreover, the cast is ``eye candy'' marking where the unchecked length knowledge is used.
511Therefore, a program being onboarded to \CFA requires some upgrading to satisfy the \CFA rules (and arguably become clearer), without giving up its validity to a plain C compiler.
512Second, the incompatibility only affects types like pointer-to-array, which are infrequently used in C.
513The more common C idiom for aliasing an array is to use a pointer-to-first-element type, which does not participate in the \CFA array's length checking.\footnote{
514 Notably, the desugaring of the \lstinline{array} type avoids letting any \lstinline{-[-]} type decay,
515 in order to preserve the length information that powers runtime bound-checking.}
516Therefore, the need to upgrade legacy C code is low.
517Finally, if this incompatibility is a problem onboarding C programs to \CFA, it should be possible to change the C type check to a warning rather than an error, acting as a \emph{lint} of the original code for a missing type annotation.
518
519To handle two occurrences of the same variable, more information is needed, \eg, this is fine,
520\begin{cfa}
521int n = 42;
522float x[@n@];
523float (*xp)[@n@] = x; $\C{// accept}$
524\end{cfa}
525where @n@ remains fixed across a contiguous declaration context.
526However, intervening dynamic statements can cause failures.
527\begin{cfa}
528int n = 42;
529float x[@n@];
530@n@ = 999; $\C{// dynamic change}$
531float (*xp)[@n@] = x; $\C{// reject}$
532\end{cfa}
533As well, side-effects can even occur in a contiguous declaration context.
534\begin{cquote}
535\setlength{\tabcolsep}{20pt}
536\begin{tabular}{@{}ll@{}}
537\begin{cfa}
538// compile unit 1
539extern int @n@;
540extern float g();
541void f() {
542 float x[@n@] = { @g()@ }; // side-effect
543 float (*xp)[@n@] = x; // reject
544}
545\end{cfa}
546&
547\begin{cfa}
548// compile unit 2
549int @n@ = 42;
550void g() {
551 @n@ = 999; // accept
552}
553
554
555\end{cfa}
556\end{tabular}
557\end{cquote}
558The issue here is that knowledge needed to make a correct decision is hidden by separate compilation.
559Even within a translation unit, static analysis might not be able to provide all the information.
560However, if the example uses @const@, the check is possible even though the value is unknown.
561\begin{cquote}
562\setlength{\tabcolsep}{20pt}
563\begin{tabular}{@{}ll@{}}
564\begin{cfa}
565// compile unit 1
566extern @const@ int n;
567extern float g();
568void f() {
569 float x[n] = { g() };
570 float (*xp)[n] = x; // accept
571}
572\end{cfa}
573&
574\begin{cfa}
575// compile unit 2
576@const@ int n = 42;
577void g() {
578 @n = 999@; // reject
579}
580
581
582\end{cfa}
583\end{tabular}
584\end{cquote}
585
586In summary, the new rules classify expressions into three groups:
587\begin{description}
588\item[Statically Evaluable]
589 Expressions for which a specific value can be calculated (conservatively) at compile-time.
590 A preexisting \CFA compiler module defines which literals, enumerators, and expressions qualify and evaluates them.
591\item[Dynamic but Stable]
592 The value of a variable declared as @const@, including a @const@ parameter.
593\item[Potentially Unstable]
594 The catch-all category. Notable examples include:
595 any function-call result, @float x[foo()]@, the particular function-call result that is a pointer dereference, @void f(const int * n)@ @{ float x[*n]; }@, and any use of a reference-type variable.
596\end{description}
597Within these groups, my \CFA rules are:
598\begin{itemize}[leftmargin=*]
599\item
600 Accept a Statically Evaluable pair, if both expressions have the same value.
601 Notably, this rule allows a literal to match with an enumeration value, based on the value.
602\item
603 Accept a Dynamic but Stable pair, if both expressions are written out the same, \eg refers to the same variable declaration.
604\item
605 Otherwise, reject.
606 Notably, reject all pairs from the Potentially Unstable group and all pairs that cross groups.
607\end{itemize}
608The traditional C rules are:
609\begin{itemize}[leftmargin=*]
610\item
611 Reject a Statically Evaluable pair, if the expressions have two different values.
612\item
613 Otherwise, accept.
614\end{itemize}
615\VRef[Figure]{f:DimexprRuleCompare} gives a case-by-case comparison of the consequences of these rule sets.
616It demonstrates that the \CFA false alarms occur in the same cases as C treats unsafe.
617It also shows that C-incompatibilities only occur in cases that C treats unsafe.
618
619\begin{figure}
620 \newcommand{\falsealarm}{{\color{blue}\small{*}}}
621 \newcommand{\allowmisuse}{{\color{red}\textbf{!}}}
622
623 \begin{tabular}{@{}l@{\hspace{16pt}}c@{\hspace{8pt}}c@{\hspace{16pt}}c@{\hspace{8pt}}c@{\hspace{16pt}}c}
624 & \multicolumn{2}{c}{\underline{Values Equal}}
625 & \multicolumn{2}{c}{\underline{Values Unequal}}
626 & \\
627 \textbf{Case} & C & \CFA & C & \CFA & Compat. \\
628 Both Statically Evaluable, Same Symbol & Accept & Accept & & & \cmark \\
629 Both Statically Evaluable, Different Symbols & Accept & Accept & Reject & Reject & \cmark \\
630 Both Dynamic but Stable, Same Symbol & Accept & Accept & & & \cmark \\
631 Both Dynamic but Stable, Different Symbols & Accept & Reject\,\falsealarm & Accept\,\allowmisuse & Reject & \xmark \\
632 Both Potentially Unstable, Same Symbol & Accept & Reject\,\falsealarm & Accept\,\allowmisuse & Reject & \xmark \\
633 Any other grouping, Different Symbol & Accept & Reject\,\falsealarm & Accept\,\allowmisuse & Reject & \xmark
634 \end{tabular}
635
636 \medskip
637 \noindent\textbf{Legend}
638 \begin{itemize}[leftmargin=*]
639 \item
640 Each row gives the treatment of a test harness of the form
641 \begin{cfa}
642 float x[ expr1 ];
643 float (*xp)[ expr2 ] = &x;
644 \end{cfa}
645 \vspace*{-10pt}
646 where \lstinline{expr1} and \lstinline{expr2} are meta-variables varying according to the row's Case.
647 Each row's claim applies to other harnesses too, including,
648 \begin{itemize}[leftmargin=*]
649 \item
650 calling a function with a parameter like \lstinline{x} and an argument of the \lstinline{xp} type,
651 \item
652 assignment in place of initialization,
653 \item
654 using references in place of pointers, and
655 \item
656 for the \CFA array, calling a polymorphic function on two \lstinline{T}-typed parameters with \lstinline{&x}- and \lstinline{xp}-typed arguments.
657 \end{itemize}
658 \item
659 Each case's claim is symmetric (swapping \lstinline{expr1} with \lstinline{expr2} has no effect),
660 even though most test harnesses are asymmetric.
661 \item
662 The table treats symbolic identity (Same/Different on rows)
663 apart from value equality (Equal/Unequal on columns).
664 \begin{itemize}[leftmargin=*]
665 \item
666 The expressions \lstinline{1}, \lstinline{0+1} and \lstinline{n}
667 (where \lstinline{n} is a variable with value 1),
668 are all different symbols with the value 1.
669 \item
670 The column distinction expresses ground truth about whether an omniscient analysis should accept or reject.
671 \item
672 The row distinction expresses the simple static factors used by today's analyses.
673 \end{itemize}
674 \item
675 Accordingly, every Reject under Values Equal is a false alarm (\falsealarm),
676 while every Accept under Values Unequal is an allowed misuse (\allowmisuse).
677 \end{itemize}
678
679 \caption{Case comparison for array type compatibility, given pairs of dimension expressions.}
680 \label{f:DimexprRuleCompare}
681\end{figure}
682
683\begin{comment}
684Given that the above check
685\begin{itemize}
686 \item
687 Is internal type @char[999]@ type-compatible with internal type @char[n]@?
688\end{itemize}
689answers false, discussion turns to how I got the \CFA compiler to produce this answer.
690In its preexisting form, the type system had a buggy approximation of the C rules.
691To implement the new \CFA rules, I added one further step.
692\begin{itemize}
693 \item
694 Is @999@ compatible with @n@?
695\end{itemize}
696This question applies to a pair of expressions, where the earlier question applies to types.
697An expression-compatibility question is a new addition to the \CFA compiler, and occurs in the context of dimension expressions, and possibly enumerations assigns, which must be unique.
698
699% ... ensure these compiler implementation matters are treated under \CFA compiler background: type unification, cost calculation, GenPoly.
700
701The relevant technical component of the \CFA compiler is the standard type-unification within the type resolver.
702\begin{cfa}
703example
704\end{cfa}
705I added rules for continuing this unification into expressions that occur within types.
706It is still fundamentally doing \emph{type} unification because it is participating in binding type variables, and not participating in binding any variables that stand in for expression fragments (for there is no such sort of variable in \CFA's analysis.)
707An unfortunate fact about the \CFA compiler's preexisting implementation is that type unification suffers from two forms of duplication.
708
709In detail, the first duplication has (many of) the unification rules stated twice.
710As a result, my additions for dimension expressions are stated twice.
711The extra statement of the rules occurs in the @GenPoly@ module, where concrete types like @array( int, 5 )@\footnote{
712 Again, the presentation is simplified
713 by leaving the \lstinline{array} macro unexpanded.}
714are lowered into corresponding C types @struct __conc_array_1234@ (the suffix being a generated index).
715In this case, the struct's definition contains fields that hardcode the argument values of @float@ and @5@.
716The next time an @array( -, - )@ concrete instance is encountered, it checks if the previous @struct __conc_array_1234@ is suitable for it.
717Yes, for another occurrence of @array( int, 5 )@;
718no, for examples like @array( int, 42 )@ or @array( rational(int), 5 )@.
719In the first example, it must reject the reuse hypothesis for a dimension-@5@ and a dimension-@42@ instance.
720
721The second duplication has unification (proper) being invoked at two stages of expression resolution.
722As a result, my added rule set needs to handle more cases than the preceding discussion motivates.
723In the program
724\begin{cfa}
725void @f@( double ); // overload
726forall( T & ) void @f@( T & ); // overload
727void g( int n ) {
728 array( float, n + 1 ) x;
729 f(x); // overloaded
730}
731\end{cfa}
732when resolving a function call to @g@, the first unification stage compares the type @T@ of the parameter with @array( float, n + 1 )@, of the argument.
733
734The actual rules for comparing two dimension expressions are conservative.
735To answer, ``yes, consider this pair of expressions to be matching,''
736is to imply, ``all else being equal, allow an array with length calculated by $e_1$
737to be passed to a function expecting a length-$e_2$ array.''\footnote{
738 ... Deal with directionality, that I'm doing exact-match, no ``at least as long as,'' no subtyping.
739 Should it be an earlier scoping principle? Feels like it should matter in more places than here.}
740So, a ``yes'' answer must represent a guarantee that both expressions evaluate the
741same result, while a ``no'' can tolerate ``they might, but we're not sure'',
742provided that practical recourses are available
743to let programmers express better knowledge.
744The new rule-set in the current release is, in fact, extremely conservative.
745I chose to keep things simple,
746and allow future needs to drive adding additional complexity, within the new framework.
747
748For starters, the original motivating example's rejection is not based on knowledge that the @xp@ length of (the literal) 999 is value-unequal to the (obvious) runtime value of the variable @n@, which is the @x@ length.
749Rather, the analysis assumes a variable's value can be anything, and so there can be no guarantee that its value is 999.
750So, a variable and a literal can never match.
751
752... Discuss the interaction of this dimension hoisting with the challenge of extra unification for cost calculation
753\end{comment}
754
755The conservatism of the new rule set can leave a programmer requiring a recourse, when needing to use a dimension expression whose stability argument is more subtle than current-state analysis.
756This recourse is to declare an explicit constant for the dimension value.
757Consider these two dimension expressions, rejected by the blunt current-state rules:
758\begin{cfa}
759void f( int @&@ nr, @const@ int nv ) {
760 float x[@nr@];
761 float (*xp)[@nr@] = &x; $\C[2.5in]{// reject: nr varying (no references)}$
762 float y[@nv + 1@];
763 float (*yp)[@nv + 1@] = &y; $\C{// reject: ?+? unpredictable (no functions)}\CRT$
764}
765\end{cfa}
766Yet, both dimension expressions are reused safely.
767The @nr@ reference is never written, no implicit code (load) between declarations, and control does not leave the function between the uses.
768As well, the build-in @?+?@ function is predictable.
769To make these cases work, the programmer must add the follow constant declarations (cast does not work):
770\begin{cfa}
771void f( int & nr, const int nv ) {
772 @const int nx@ = nr;
773 float x[nx];
774 float (*xp)[nx] = & x; $\C[2.5in]{// accept}$
775 @const int ny@ = nv + 1;
776 float y[ny];
777 float (*yp)[ny] = & y; $\C{// accept}\CRT$
778}
779\end{cfa}
780The result is the originally intended semantics,
781achieved by adding a superfluous ``snapshot it as of now'' directive.
782
783The snapshot trick is also used by the \CFA translation, though to achieve a different outcome.
784Rather obviously, every array must be subscriptable, even a bizarre one:
785\begin{cfa}
786array( float, @rand(10)@ ) x; $\C[2.5in]{// x[0] => no elements}$
787x[@0@]; $\C{// 10\% chance of bound-check failure}\CRT$
788\end{cfa}
789Less obvious is that the mechanism of subscripting is a function call, which must communicate length accurately.
790The bound-check above (callee logic) must use the actual allocated length of @x@, without mistakenly reevaluating the dimension expression, @rand(10)@.
791Adjusting the example to make the function's use of length more explicit:
792\begin{cfa}
793forall( T * )
794void f( T * x ) { sout | sizeof( *x ); }
795float x[ rand(10) ];
796f( x );
797\end{cfa}
798Considering that the partly translated function declaration is, loosely,
799\begin{cfa}
800void f( size_t __sizeof_T, void * x ) { sout | __sizeof_T; }
801\end{cfa}
802the translation calls the dimension argument twice:
803\begin{cfa}
804float x[ rand(10) ];
805f( rand(10), &x );
806\end{cfa}
807The correct form is:
808\begin{cfa}
809size_t __dim_x = rand(10);
810float x[ __dim_x ];
811f( __dim_x, &x );
812\end{cfa}
813Dimension hoisting already existed in the \CFA compiler.
814However, it was buggy, particularly with determining, ``Can hoisting the expression be skipped here?'', for skipping this hoisting is clearly desirable in some cases.
815For example, when a programmer has already hoisted to perform an optimization to prelude duplicate code (expression) and/or expression evaluation.
816In the new implementation, these cases are correct, harmonized with the accept/reject criteria.
817
818
819\section{Multidimensional Array Implementation}
820\label{s:ArrayMdImpl}
821
822A multidimensional array implementation has three relevant levels of abstraction, from highest to lowest, where the array occupies \emph{contiguous memory}.
823\begin{enumerate}[leftmargin=*]
824\item
825Flexible-stride memory:
826this model has complete independence between subscript ordering and memory layout, offering the ability to slice by (provide an index for) any dimension, \eg slice a row, column, or plane, \eg @c[3][4][*]@, @c[3][*][5]@, @c[3][*][*]@.
827\item
828Fixed-stride memory:
829this model binds the first subscript and the first memory layout dimension, offering the ability to slice by (provide an index for) only the coarsest dimension, @m[row][*]@ or @c[plane][*][*]@, \eg slice only by row (2D) or plane (3D).
830After which, subscripting and memory layout are independent.
831\item
832Explicit-displacement memory:
833this model has no awareness of dimensions just the ability to access memory at a distance from a reference point (base-displacement addressing), \eg @x + 23@ or @x[23}@ $\Rightarrow$ 23rd element from the start of @x@.
834A programmer must manually build any notion of dimensions using other tools;
835hence, this style is not offering multidimensional arrays \see{\VRef[Figure]{f:FixedVariable} right example}.
836\end{enumerate}
837
838There is some debate as to whether the abstraction point ordering above goes $\{1, 2\} < 3$, rather than my numerically-ordering.
839That is, styles 1 and 2 are at the same abstraction level, with 3 offering a limited set of functionality.
840I chose to build the \CFA style-1 array upon a style-2 abstraction.
841(Justification of the decision follows, after the description of the design.)
842
843Style 3 is the inevitable target of any array implementation.
844The hardware offers this model to the C compiler, with bytes as the unit of displacement.
845C offers this model to programmers as pointer arithmetic, with arbitrary sizes as the unit.
846Casting a multidimensional array as a single-dimensional array/pointer, then using @x[i]@ syntax to access its elements, is still a form of pointer arithmetic.
847
848To step into the implementation of \CFA's new type-1 multidimensional arrays in terms of C's existing type-2 multidimensional arrays, it helps to clarify that the interface is low-level, \ie a C/\CFA array interface includes the resulting memory layout.
849Specifically, the defining requirement of a type-2 system is the ability to slice a column from a column-finest matrix.
850Hence, the required memory shape of such a slice is fixed, before any discussion of implementation.
851The implementation presented here is how the \CFA array-library wrangles the C type system to make it do memory steps that are consistent with this layout while not affecting legacy C programs.
852% TODO: do I have/need a presentation of just this layout, just the semantics of -[all]?
853
854The new \CFA standard-library @array@-datatype supports richer multidimensional features than C.
855The new array implementation follows C's contiguous approach, \ie @float [r][c]@, with one contiguous object subscripted by coarsely-strided dimensions directly wrapping finely-strided dimensions.
856Beyond what C's array type offers, the new array brings direct support for working with a \emph{noncontiguous} array slice, allowing a program to work with dimension subscripts given in a non-physical order.
857
858The following examples use the matrix declaration @array( float, 5, 7 ) m@, loaded with values incremented by $0.1$, when stepping across the length-7 finely-strided column dimension, and stepping across the length-5 coarsely-strided row dimension.
859\par
860\mbox{\lstinput{121-126}{hello-md.cfa}}
861\par\noindent
862The memory layout is 35 contiguous elements with strictly increasing addresses.
863
864A trivial form of slicing extracts a contiguous inner array, within an array-of-arrays.
865As for the C array, a lesser-dimensional array reference can be bound to the result of subscripting a greater-dimensional array by a prefix of its dimensions, \eg @m[2]@, giving the third row.
866This action first subscripts away the most coarsely strided dimensions, leaving a result that expects to be subscripted by the more finely strided dimensions, \eg @m[2][3]@, giving the value @2.3@.
867The following is an example slicing a row.
868\lstinput{60-64}{hello-md.cfa}
869\lstinput[aboveskip=0pt]{140-140}{hello-md.cfa}
870
871However, function @print1d_cstyle@ is asserting too much knowledge about its parameter @r@ for printing either a row slice or a column slice.
872Specifically, declaring the parameter @r@ with type @array@ means that @r@ is contiguous, which is unnecessarily restrictive.
873That is, @r@ need only be of a container type that offers a subscript operator (of type @ptrdiff_t@ $\rightarrow$ @float@) with managed length @N@.
874The new-array library provides the trait @ar@, so-defined.
875With it, the original declaration can be generalized with the same body.
876\lstinput{43-44}{hello-md.cfa}
877\lstinput[aboveskip=0pt]{145-145}{hello-md.cfa}
878The nontrivial slicing in this example now allows passing a \emph{noncontiguous} slice to @print1d@, where the new-array library provides a ``subscript by all'' operation for this purpose.
879In a multi-dimensional subscript operation, any dimension given as @all@ is a placeholder, \ie ``not yet subscripted by a value'', waiting for a value implementing the @ar@ trait.
880\lstinput{150-151}{hello-md.cfa}
881
882The example shows @x[2]@ and @x[[2, all]]@ both refer to the same, ``2.*'' slice.
883Indeed, the various @print1d@ calls under discussion access the entry with value @2.3@ as @x[2][3]@, @x[[2,all]][3]@, and @x[[all,3]][2]@.
884This design preserves (and extends) C array semantics by defining @x[[i,j]]@ to be @x[i][j]@ for numeric subscripts, but also for ``subscripting by all''.
885That is:
886\begin{cquote}
887\begin{tabular}{@{}cccccl@{}}
888@x[[2,all]][3]@ & $\equiv$ & @x[2][all][3]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is redundant) \\
889@x[[all,3]][2]@ & $\equiv$ & @x[all][3][2]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is effective)
890\end{tabular}
891\end{cquote}
892
893Narrating progress through each of the @-[-][-][-]@\footnote{
894The first ``\lstinline{-}'' is a variable expression and the remaining ``\lstinline{-}'' are subscript expressions.}
895expressions gives, firstly, a definition of @-[all]@, and secondly, a generalization of C's @-[i]@.
896Where @all@ is redundant:
897\begin{cquote}
898\begin{tabular}{@{}ll@{}}
899@x@ & 2-dimensional, want subscripts for coarse then fine \\
900@x[2]@ & 1-dimensional, want subscript for fine; lock coarse == 2 \\
901@x[2][all]@ & 1-dimensional, want subscript for fine \\
902@x[2][all][3]@ & 0-dimensional; lock fine == 3
903\end{tabular}
904\end{cquote}
905Where @all@ is effective:
906\begin{cquote}
907\begin{tabular}{@{}ll@{}}
908@x@ & 2-dimensional, want subscripts for coarse then fine \\
909@x[all]@ & 2-dimensional, want subscripts for fine then coarse \\
910@x[all][3]@ & 1-dimensional, want subscript for coarse; lock fine == 3 \\
911@x[all][3][2]@ & 0-dimensional; lock coarse == 2
912\end{tabular}
913\end{cquote}
914The semantics of @-[all]@ is to dequeue from the front of the ``want subscripts'' list and re-enqueue at its back.
915For example, in a two dimensional matrix, this semantics conceptually transposes the matrix by reversing the subscripts.
916The semantics of @-[i]@ is to dequeue from the front of the ``want subscripts'' list and lock its value to be @i@.
917
918Contiguous arrays, and slices of them, are all represented by the same underlying parameterized type, which includes stride information in its metatdata.
919The \lstinline{-[all]} operation takes subscripts, \eg @x[2][all]@, @x[all][3]@, \etc, and converts (transposes) from the base reference @x[all]@ to a specific reference of the appropriate form.
920The running example's @all@-effective step, stated more concretely, is:
921\begin{cquote}
922\begin{tabular}{@{}ll@{}}
923@x@ & : 5 of ( 7 of @float@ each spaced 1 @float@ apart ) each spaced 7 @floats@ apart \\
924@x[all]@ & : 7 of ( 5 of @float@ each spaced 7 @float@s apart ) each spaced 1 @float@ apart
925\end{tabular}
926\end{cquote}
927
928\begin{figure}
929\includegraphics{measuring-like-layout}
930\caption{Visualization of subscripting by value and by \lstinline[language=CFA]{all}, for \lstinline{x} of type \lstinline{array( float, 5, 7 )} understood as 5 rows by 7 columns.
931The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual.
932The vertical shaded band highlights the location of the targeted element, 2.3.
933Any such vertical slice contains various interpretations of a single address.}
934\label{fig:subscr-all}
935\end{figure}
936
937\VRef[Figure]{fig:subscr-all} shows one element (in the white band) accessed two different ways: as @x[2][3]@ and as @x[all][3][2]@.
938In both cases, subscript 2 selects from the coarser dimension (rows of @x@),
939while subscript 3 selects from the finer dimension (columns of @x@).
940The figure illustrates the value of each subexpression, comparing how numeric subscripting proceeds from @x@, \vs from @x[all]@.
941Proceeding from @x@ gives the numeric indices as coarse then fine, while proceeding from @x[all]@ gives them fine then coarse.
942These two starting expressions, which are the example's only multidimensional subexpressions
943(those that received zero numeric indices so far), are illustrated with vertical steps where a \emph{first} numeric index selects.
944
945The figure's presentation offers an intuitive answer to: What is an atomic element of @x[all]@?
946\begin{enumerate}[leftmargin=*]
947\item
948@x[all]@ itself is simply a two-dimensional array, in the strict C sense, of these building blocks.
949\item
950An array of these atoms (like the intermediate @x[all][3]@) is just a contiguous arrangement of them, done according to their size;
951call such an array a column.
952A column is almost ready to be arranged into a matrix;
953it is the \emph{contained value} of the next-level building block, but another lie about size is required.
954\item
955An atom (like the bottommost value, @x[all][3][2]@), is the contained value (in the square box) and a lie about its size (the left diagonal above it, growing upward).
956\end{enumerate}
957At first, an atom needs to be arranged as if it is bigger, but now a column needs to be arranged as if it is smaller (the left diagonal above it, shrinking upward).
958These lying columns, arranged contiguously according to their size (as announced) form the matrix @x[all]@.
959Because @x[all]@ takes indices, first for the fine stride, then for the coarse stride, it achieves the requirement of representing the transpose of @x@.
960Yet every time the programmer presents an index, a C-array subscript is achieving the offset calculation.
961
962In the @x[all]@ case, after the finely strided subscript is done (column 3 is selected),
963the locations referenced by the coarse subscript options (rows 0..4) are offset by 3 floats,
964compared with where analogous rows appear when the row-level option is presented for @x@.
965For example, in \lstinline{x[all]}, the shaded band and its immediate values to the left touches atoms 2.3, 2.0, 2.1, 2.2, 1.4, 1.5 and 1.6.
966But only the atom 2.3 is storing its value there.
967The rest are lying about (conflicting) claims on this location, but never exercising these alleged claims.
968
969Lying is implemented as casting.
970The arrangement just described is implemented in the structure @arpk@.
971This structure uses one type in its internal field declaration and offers a different type as the return of its subscript operator.
972The field within is a plain-C array of the fictional type, which is 7 floats long for @x[all][3][2]@ and 1 float long for @x[all][3]@.
973The subscript operator presents what is really inside, by casting to the type below the left diagonal of the lie.
974
975% Does x[all] have to lie too? The picture currently glosses over how it it advertises a size of 7 floats. I'm leaving that as an edge case benignly misrepresented in the picture. Edge cases only have to be handled right in the code.
976
977Casting, overlapping, and lying are unsafe.
978The mission is to implement a style-1 feature in the type system for safe use by a programmer.
979The offered style-1 system is allowed to be internally unsafe,
980just as C's implementation of a style-2 system (upon a style-3 system) is unsafe within, even when the programmer is using it without casts or pointer arithmetic.
981Having a style-1 system relieves the programmer from resorting to unsafe pointer arithmetic when working with noncontiguous slices.
982
983% PAB: repeat from previous paragraph.
984% The choice to implement this style-1 system upon C's style-2 arrays, rather than its style-3 pointer arithmetic, reduces the attack surface of unsafe code.
985% My casting is unsafe, but I do not do any pointer arithmetic.
986% When a programmer works in the common-case style-2 subset (in the no-@[all]@ top of \VRef[Figure]{fig:subscr-all}), my casts are identities, and the C compiler is doing its usual displacement calculations.
987% If I had implemented my system upon style-3 pointer arithmetic, then this common case would be circumventing C's battle-hardened displacement calculations in favour of my own.
988
989% \noindent END: Paste looking for a home
990
991The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping.
992The @arpk@ structure and its @-[i]@ operator are defined as:
993\begin{cfa}
994forall(
995 [N], $\C{// length of current dimension}$
996 S & | sized(S), $\C{// masquerading-as}$
997 Timmed &, $\C{// immediate element, often another array}$
998 Tbase & $\C{// base element, \eg float, never array}$
999) { // distribute forall to each element
1000 struct arpk {
1001 S strides[N]; $\C{// so that sizeof(this) is N of S}$
1002 };
1003 // expose Timmed, stride by S
1004 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, long int i ) {
1005 subcheck( a, i, 0, N );
1006 return (Timmed &)a.strides[i];
1007 }
1008}
1009\end{cfa}
1010The private @arpk@ structure (array with explicit packing) is generic over four types: dimension length, masquerading-as, ...
1011This structure's public interface is hidden behind the @array(...)@ macro and the subscript operator.
1012Construction by @array@ initializes the masquerading-as type information to be equal to the contained-element information.
1013Subscripting by @all@ rearranges the order of masquerading-as types to achieve, in general, nontrivial striding.
1014Subscripting by a number consumes the masquerading-as size of the contained element type, does normal array stepping according to that size, and returns the element found there, in unmasked form.
1015
1016An instantiation of the @arpk@ generic is given by the @array( E_base, N0, N1, ... )@ expansion, which is @arpk( N0, Rec, Rec, E_base )@, where @Rec@ is @array( E_base, N1, ... )@.
1017In the base case, @array( E_base )@ is just @E_base@.
1018Because this construction uses the same value for the generic parameters @S@ and @E_im@, the resulting layout has trivial strides.
1019
1020Subscripting by @all@, to operate on nontrivial strides, is a dequeue-enqueue operation on the @E_im@ chain, which carries @S@ instantiations, intact, to new positions.
1021Expressed as an operation on types, this rotation is:
1022\begin{eqnarray*}
1023suball( arpk(N, S, E_i, E_b) ) & = & enq( N, S, E_i, E_b ) \\
1024enq( N, S, E_b, E_b ) & = & arpk( N, S, E_b, E_b ) \\
1025enq( N, S, arpk(N', S', E_i', E_b), E_b ) & = & arpk( N', S', enq(N, S, E_i', E_b), E_b )
1026\end{eqnarray*}
1027
1028
1029\section{Zero Overhead}
1030
1031At runtime, the \CFA array is exactly a C array.
1032\CFA array subscripting is protected with runtime bound checks.
1033The array dependent-typing provides information to the C optimizer, allowing it to remove many of the bound checks.
1034This section provides a demonstration of these effects.
1035
1036The experiment compares the \CFA array system with the simple-safety system most typically exemplified by Java arrays (\VRef[Section]{JavaCompare}), and reflected in \CC vector using member @.at@ for subscripting (\VRef[Section]{CppCompare}).
1037The essential feature is the one-to-one correspondence between array instances and the symbolic bounds on which dynamic checks are based.
1038Hence, it is the implicit \CFA checked subscripting and explicit \CC @vector@ @.at@ mechanisms being explained and not limitations of \CC optimization.
1039
1040As a control case, a simple loop receives the same optimization treatment in both the \CFA and \CC versions.
1041When the surrounding code makes it clear subscripting is always in bound, no dynamic bound check is observed in the program's optimized assembly code.
1042But when the environment is adjusted, such that the subscript is possibly invalid, the bound check appears in the optimized assembly, ready to catch a mistake.
1043
1044\VRef[Figure]{f:ovhd-ctl} gives a control-case example of summing values in an array, where each column shows the program in languages C (a,~d,~g), \CFA (b,~e,~h), and \CC (c,~f,~i).
1045The C code has no bounds checking, while the \CFA and \CC can have bounds checking.
1046The source-code functions in (a, b, c) can be compiled to have either correct or incorrect uses of bounds.
1047When compiling for correct bound use, the @BND@ macro passes its argument through, so the loops cover exactly the passed array sizes.
1048When compiling for possible incorrect bound use (a programming error), the @BND@ macro hardcodes the loops for 100 iterations, which implies out-of-bound access attempts when the passed array has fewer than 100 elements.
1049The assembly code excerpts show optimized translations for correct-bound mode in (d, e, f) and incorrect-bound mode in (g, h, i).
1050Because incorrect-bound mode hardcodes 100 iterations, the loop always executes a first time, so this mode does not have the @n == 0@ branch seen in correct-bound mode.
1051For C, this difference is the only (d--g) difference.
1052For correct bounds, the \CFA translation equals the C translation, \ie~there is no (d--e) difference, while \CC has an additional indirection to dereference the vector's auxiliary allocation.
1053
1054\begin{figure}
1055\setlength{\tabcolsep}{10pt}
1056\begin{tabular}{llll@{}}
1057\multicolumn{1}{c}{C} &
1058\multicolumn{1}{c}{\CFA} &
1059\multicolumn{1}{c}{\CC (nested vector)}
1060\\[1em]
1061\lstinput{20-28}{ar-bchk/control.c} &
1062\lstinput{20-28}{ar-bchk/control.cfa} &
1063\lstinput{20-28}{ar-bchk/control.cc}
1064\\
1065\multicolumn{1}{c}{(a)} &
1066\multicolumn{1}{c}{(b)} &
1067\multicolumn{1}{c}{(c)}
1068\\[1em]
1069\multicolumn{4}{l}{
1070 \includegraphics[page=1,trim=0 9.125in 2in 0,clip]{ar-bchk}
1071}
1072\\
1073\multicolumn{1}{c}{(d)} &
1074\multicolumn{1}{c}{(e)} &
1075\multicolumn{1}{c}{(f)}
1076\\[1em]
1077\multicolumn{4}{l}{
1078 \includegraphics[page=2,trim=0 8.75in 2in 0,clip]{ar-bchk}
1079}
1080\\
1081\multicolumn{1}{c}{(g)} &
1082\multicolumn{1}{c}{(h)} &
1083\multicolumn{1}{c}{(i)}
1084\end{tabular}
1085\caption{Overhead comparison, control case.
1086All three programs/compilers generate equally performant code when the programmer ``self-checks'' bounds via a loop's condition.
1087Yet both \CFA and \CC versions generate code that is slower and safer than C's when the programmer might overrun the bounds.}
1088\label{f:ovhd-ctl}
1089\end{figure}
1090
1091Differences arise when the bound-incorrect programs are passed an array shorter than 100.
1092The C version, (g), proceeds with undefined behaviour, reading past the end of the array.
1093The \CFA and \CC versions, (h, i), flag the mistake with the runtime check, the @i >= n@ branch.
1094This check is provided implicitly by the library types @array@ and @vector@, respectively.
1095The significant result is the \emph{absence} of runtime checks from the bound-correct translations, (e, f).
1096The code optimizer has removed them because, at the point where they would occur (immediately past @.L28@/@.L3@), it knows from the surrounding control flow either @i == 0 && 0 < n@ or @i < n@ (directly), \ie @i < n@ either way.
1097So any repeated checks, \ie the branch and its consequent code (raise error), are removable.
1098
1099% Progressing from the control case, a deliberately bound-incorrect mode is no longer informative.
1100% Rather, given a (well-typed) program that does good work on good data, the issue is whether it is (determinably) bound-correct on all data.
1101
1102When dimension sizes get reused, \CFA has an advantage over \CC-vector at getting simply written programs well optimized.
1103\VRef[Figures]{f:ovhd-treat-src} shows a simple matrix multiplication over a row-major encoding.
1104Simple means coding directly to the intuition of the mathematical definition without trying to optimize for memory layout.
1105In the assembly code of \VRef[Figure]{f:ovhd-treat-asm}, the looping pattern of \VRef[Figure]{f:ovhd-ctl} (d, e, f), ``Skip ahead on zero; loop back for next,'' occurs with three nesting levels.
1106Simultaneously, the dynamic bound-check pattern of \VRef[Figure]{f:ovhd-ctl} (h,~i), ``Get out on invalid,'' occurs, targeting @.L7@, @.L9@ and @.L8@ (bottom right).
1107Here, \VRef[Figures]{f:ovhd-treat-asm} shows the \CFA solution optimizing into practically the C solution, while the \CC solution shows added runtime bound checks.
1108Like in \VRef[Figure]{f:ovhd-ctl} (e), the significance is the \emph{absence} of library-imposed runtime checks, even though the source code is working through the the \CFA @array@ library.
1109The optimizer removed the library-imposed checks because the data structure @array@-of-@array@ is constrained by its type to be shaped correctly for the intuitive looping.
1110In \CC, the same constraint does not apply to @vector@-of-@vector@.
1111Because every individual @vector@ carries its own size, two types of bound mistakes are possible.
1112
1113\begin{figure}
1114\setlength{\tabcolsep}{10pt}
1115\begin{tabular}{llll@{}}
1116\multicolumn{1}{c}{C} &
1117\multicolumn{1}{c}{\CFA} &
1118\multicolumn{1}{c}{\CC (nested vector)}
1119\\
1120\lstinput{20-37}{ar-bchk/treatment.c} &
1121\lstinput{20-37}{ar-bchk/treatment.cfa} &
1122\lstinput{20-37}{ar-bchk/treatment.cc}
1123\end{tabular}
1124\caption{Overhead comparison, differentiation case, source.}
1125\label{f:ovhd-treat-src}
1126\end{figure}
1127
1128\begin{figure}
1129\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
1130\begin{tabular}{C{1.5in}C{1.5in}C{1.5in}p{1in}}
1131C &
1132\CFA &
1133\CC (nested vector)
1134\\[1em]
1135\multicolumn{4}{l}{
1136 \includegraphics[page=3,trim=0 4in 2in 0,clip]{ar-bchk}
1137}
1138\end{tabular}
1139\caption{Overhead comparison, differentiation case, assembly.
1140}
1141\label{f:ovhd-treat-asm}
1142\end{figure}
1143
1144In detail, the three structures received may not be matrices at all, per the obvious/dense/total interpretation; rather, any one might be ragged-right in its rows.
1145The \CFA solution guards against this possibility by encoding the minor length (number of columns) in the major element (row) type.
1146In @res@, for example, each of the @M@ rows is @array(float, N)@, guaranteeing @N@ cells within it.
1147Or more technically, guaranteeing @N@ as the basis for the imposed bound check \emph{of every row.}
1148As well, the \CC type does not impose the mathematically familiar constraint of $(M \times P) (P \times N) \rightarrow (M \times N)$.
1149Even assuming away the first issue, \eg that in @lhs@ all minor/cell counts agree, the data format allows the @rhs@ major/row count to disagree.
1150Or, the possibility that the row count @res.size()@ disagrees with the row count @lhs.size()@ illustrates this bound-mistake type in isolation.
1151The \CFA solution guards against this possibility by keeping length information separate from the array data, and therefore eligible for sharing.
1152This capability lets \CFA escape the one-to-one correspondence between array instances and symbolic bounds, where this correspondence leaves a \CC-vector programmer stuck with a matrix representation that repeats itself.
1153
1154It is important to clarify that the \CFA solution does not become unsafe (like C) in losing its dynamic checks, even though it becomes fast (as C) in losing them.
1155The dynamic checks are dismissed as unnecessary \emph{because} the program is safe to begin with.
1156To achieve the same performance, a \CC programmer must state appropriate assertions or assumptions, to allow the optimizer to dismiss the runtime checks.
1157% Especially considering that two of them are in the inner-most loop.
1158The solution requires doing the work of the inner-loop checks as a \emph{preflight step}.
1159But this step requires looping and doing it upfront gives too much separation for the optimizer to see ``has been checked already'' in the deep loop.
1160So, the programmer must restate the preflight observation within the deep loop, but this time as an unchecked assumption.
1161Such assumptions are risky because they introduce further undefined behaviour when misused.
1162Only the programmer's discipline remains to ensure this work is done without error.
1163
1164In summary, the \CFA solution lets a simply stated program have dynamic guards that catch bugs, while letting a simply stated bug-free program run as fast as the unguarded C equivalent.
1165
1166\begin{comment}
1167The ragged-right issue brings with it a source-of-truth difficulty: Where, in the \CC version, is one to find the value of $N$? $M$ can come from either @res@'s or @lhs@'s major/row count, and checking these for equality is straightforward. $P$ can come from @rhs@'s major/row count. But $N$ is only available from columns, \ie minor/cell counts, which are ragged. So any choice of initial source of truth, \eg
1168\end{comment}
1169
1170
1171\section{Array Lifecycle}
1172
1173An array, like a structure, wraps subordinate objects.
1174Any object type, like @string@, can be an array element or structure member.
1175A consequence is that the lifetime of the container must match with its subordinate objects: all elements and members must be initialized/uninitialized implicitly as part of the container's allocation/deallocation.
1176Modern programming languages implicitly perform these operations via a type's constructor and destructor.
1177Therefore, \CFA must assure that an array's subordinate objects' lifetime operations are called.
1178Preexisting \CFA mechanisms achieve this requirement, but with poor performance.
1179Furthermore, advanced array users need an exception to the basic mechanism, which does not occur with other aggregates.
1180Hence, arrays introduce subtleties in supporting an element's lifecycle.
1181
1182The preexisting \CFA support for contained-element lifecycle is based on recursive occurrences of the object-type (otype) pseudo-trait.
1183A type is an otype, if it provides a default (parameterless) constructor, copy constructor, assignment operator, and destructor (like \CC).
1184For a structure with otype members, the compiler implicitly generates implementations of the four otype functions for the outer structure.
1185Then the generated default constructor for the outer structure calls the default constructor for each member, and the other otype functions work similarly.
1186For a member that is a C array, these calls occur in a loop for each array element, which even works for VLAs.
1187This logic works the same, whether the member is a concrete type (that happens to be an otype) or if the member is a polymorphic type asserted to be an otype (which is implicit in the syntax, @forall(T)@).
1188The \CFA array has the simplified form (similar to one seen before):
1189\begin{cfa}
1190forall( T * ) // non-otype element, no lifecycle functions
1191// forall( T ) // otype element, lifecycle functions asserted
1192struct array5 {
1193 T __items[ 5 ];
1194};
1195\end{cfa}
1196Being a structure with a C-array member, the otype-form declaration @T@ causes @array5( float )@ to be an otype too.
1197But this otype-recursion pattern has a performance issue.
1198\VRef[Figure]{f:OtypeRecursionBlowup} shows the steps to find lifecycle functions, under the otype-recursion pattern, for a cube of @float@:
1199\begin{cfa}
1200array5( array5( array5( float ) ) )
1201\end{cfa}
1202The work needed for the full @float@-cube is 256 leaves.
1203Then the otype-recursion pattern generates helper functions and thunks that are exponential in the number of dimensions.
1204Anecdotal experience is annoyingly slow compile time at three dimensions and unusable at four.
1205
1206%array5(T) offers
1207%1 parameterless ctor, which asks for T to have
1208% 1 parameterless ctor
1209% 2 copy ctor
1210% 3 asgt
1211% 4 dtor
1212%2 copy ctor, which asks for T to have
1213% 1 parameterless ctor
1214% 2 copy ctor
1215% 3 asgt
1216% 4 dtor
1217%3 asgt, which asks for T to have
1218% 1 parameterless ctor
1219% 2 copy ctor
1220% 3 asgt
1221% 4 dtor
1222%4 dtor, which asks for T to have
1223% 1 parameterless ctor
1224% 2 copy ctor
1225% 3 asgt
1226% 4 dtor
1227
1228\begin{figure}
1229\centering
1230\setlength{\tabcolsep}{20pt}
1231\begin{tabular}{@{}lll@{}}
1232\begin{cfa}[deletekeywords={default}]
1233float offers
12341 default ctor
12352 copy ctor
12363 assignment
12374 dtor
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262\end{cfa}
1263&
1264\begin{cfa}[deletekeywords={default}]
1265array5( float ) has
12661 default ctor, using float$'$s
1267 1 default ctor
1268 2 copy ctor
1269 3 assignment
1270 4 dtor
12712 copy ctor, using float$'$s
1272 1 default ctor
1273 2 copy ctor
1274 3 assignment
1275 4 dtor
12763 assignment, using float$'$s
1277 1 default ctor
1278 2 copy ctor
1279 3 assignment
1280 4 dtor
12814 dtor, using float$'$s
1282 1 default ctor
1283 2 copy ctor
1284 3 assignment
1285 4 dtor
1286
1287
1288
1289
1290
1291
1292
1293
1294\end{cfa}
1295&
1296\begin{cfa}[deletekeywords={default}]
1297array5( array5( float ) ) has
12981 default ctor, using array5( float )
1299 1 default ctor, using float$'$s
1300 1 default ctor
1301 2 copy ctor
1302 3 assignment
1303 4 dtor
1304 2 copy ctor, using float$'$s
1305 1 default ctor
1306 2 copy ctor
1307 3 assignment
1308 4 dtor
1309 3 assignment, using float$'$s
1310 1 default ctor
1311 2 copy ctor
1312 3 assignment
1313 4 dtor
1314 4 dtor, using float$'$s
1315 1 default ctor
1316 2 copy ctor
1317 3 assignment
1318 4 dtor
13192 copy ctor, using array5( float )
1320 ... 4 children, 16 leaves
13213 assignment, using array5( float )
1322 ... 4 children, 16 leaves
13234 dtor, using array5( float )
1324 ... 4 children, 16 leaves
1325(64 leaves)
1326\end{cfa}
1327\end{tabular}
1328\caption{Exponential thunk generation under the otype-recursion pattern.
1329 Each time one type's function (\eg ctor) uses another type's function, the \CFA compiler generates a thunk, to capture the used function's dependencies, presented according to the using function's need.
1330 So, each non-leaf line represents a generated thunk and every line represents a search request for the resolver to find a satisfying function.}
1331\label{f:OtypeRecursionBlowup}
1332\end{figure}
1333
1334The issue is that the otype-recursion pattern uses more assertions than needed.
1335For example, @array5( float )@'s default constructor has all four lifecycle assertions about the element type, @float@.
1336However, it only needs @float@'s default constructor, as the other operations are never used.
1337Current work by the \CFA team aims to improve this situation.
1338My workaround moves from otype (value) to dtype (pointer) with a default-constructor assertion, where dtype does not generate any constructors but the assertion claws back the default otype constructor.
1339\begin{cquote}
1340\setlength{\tabcolsep}{10pt}
1341\begin{tabular}{@{}ll@{}}
1342\begin{cfa}
1343// autogenerated for otype-recursion:
1344forall( T )
1345void ?{}( array5( T ) & this ) {
1346 for ( i; 5 ) { ( this[i] ){}; }
1347}
1348forall( T )
1349void ?{}( array5( T ) & this, array5( T ) & src ) {
1350 for ( i; 5 ) { ( this[i] ){ src[i] }; }
1351}
1352forall( T )
1353void ^?{}( array5( T ) & this ) {
1354 for ( i; 5 ) { ^( this[i] ){}; }
1355}
1356\end{cfa}
1357&
1358\begin{cfa}
1359// lean, bespoke:
1360forall( T* | { void @?{}( T & )@; } )
1361void ?{}( array5( T ) & this ) {
1362 for ( i; 5 ) { ( this[i] ){}; }
1363}
1364forall( T* | { void @?{}( T &, T )@; } )
1365void ?{}( array5( T ) & this, array5( T ) & src ) {
1366 for ( i; 5 ) { ( this[i] ){ src[i] }; }
1367}
1368forall( T* | { void @?{}( T & )@; } )
1369void ^?{}( array5(T) & this ) {
1370 for (i; 5) { ^( this[i] ){}; }
1371}
1372\end{cfa}
1373\end{tabular}
1374\end{cquote}
1375Moreover, the assignment operator is skipped, to avoid hitting a lingering growth case.
1376Temporarily skipping assignment is tolerable because array assignment is not a common operation.
1377With this solution, the critical lifecycle functions are available, with linear growth in thunk creation for the default constructor.
1378
1379Finally, the intuition that a programmer using an array always wants the elements' default constructor called \emph{automatically} is simplistic.
1380Arrays exist to store different values at each position.
1381So, array initialization needs to let the programmer provide different constructor arguments to each element.
1382\begin{cfa}
1383thread worker { int id; };
1384void ?{}( worker & ) = void; $\C[2.5in]{// remove default constructor}$
1385void ?{}( worker &, int id );
1386array( worker, 5 ) ws = @{}@; $\C{// rejected; but desire is for no initialization yet}$
1387for ( i; 5 ) (ws[i]){ @i@ }; $\C{// explicit (placement) initialization for each thread, giving id}\CRT$
1388\end{cfa}
1389Note the use of the \CFA explicit constructor call, analogous to \CC's placement-@new@.
1390This call is where initialization is desired, and not at the declaration of @ws@.
1391The attempt to initialize from nothing (equivalent to dropping @= {}@ altogether) is invalid because the @worker@ type removes the default constructor.
1392The @worker@ type is designed this way to work with the threading system.
1393A thread type forks a thread at the end of each constructor and joins with it at the start of each destructor.
1394But a @worker@ cannot begin its forked-thread work without knowing its @id@.
1395Therefore, there is a conflict between the implicit actions of the builtin @thread@ type and a user's desire to defer these actions.
1396
1397Another \CFA feature for providing C backwards compatibility, at first seems viable for initializing the array @ws@, though on closer inspection is not.
1398C initialization without a constructor is specified with \lstinline|@=|, \eg \lstinline|array(worker, 5) ws @= {}| ignores all \CFA lifecycle management and uses C empty initialization.
1399This option does achieve the desired semantics on the construction side.
1400But on destruction side, the desired semantics is for implicit destructor calls to continue, to keep the join operation tied to lexical scope.
1401C initialization disables \emph{all} implicit lifecycle management, but the goal is to disable only the implicit construction.
1402
1403To fix this problem, I enhanced the \CFA standard library to provide the missing semantics, available in either form:
1404\begin{cfa}
1405array( @uninit@(worker), 5 ) ws1;
1406array( worker, 5) ws2 = { @delay_init@ };
1407\end{cfa}
1408Both cause the @ws@-construction-time implicit call chain to stop before reaching a @worker@ constructor, while leaving the implicit destruction calls intact.
1409Two forms are available, to parallel the development of this feature in \uCpp~\cite{uC++}.
1410Originally \uCpp offered only the @ws1@ form, using the class-template @uNoCtor@ equivalent to \CFA's @uninit@.
1411More recently, \uCpp was extended with the declaration macro, @uArray@, with usage similar to the @ws2@ case.
1412Based on experience piloting @uArray@ as a replacement of @uNoCtor@, it should be possible to remove the first option.
1413
1414% note to Mike, I have more fragments on some details available in push24\fragments\uNoCtor.txt
1415
1416\section{Array Comparison}
1417
1418
1419\subsection{Rust}
1420
1421A Rust array is a set of mutable or immutable (@const@) contiguous objects of the same type @T@, where the compile-time dimension(s) is part of the type signature @[T; dimension]@.
1422\begin{rust}
1423let val = 42;
1424let mut ia: [usize; 5] = [val; 5]; $\C{// mutable, repeated initialization [42, 42, 42, 42, 42]}$
1425let fval = 42.2;
1426let fa: [f64; 5] = [1.2, fval, 5.6, 7.3, 9.1]; $\C{// immutable, individual initialization}$
1427\end{rust}
1428Initialization is required even if an array is subsequently initialized.
1429Rust arrays are not VLAs, but the compile-time length can be queried using member @len()@.
1430Arrays can be assigned and passed to parameters by value or reference, if and only if, the type and dimension match, meaning a different function is needed for each array size.
1431
1432Array iteration is by a range or array variable.
1433\begin{rust}
1434for i in @0..ia.len()@ { print!("{:?} ", ia[i] ); } $\C{// 42 42 42 42 42}$
1435for fv in @fa@ { print!("{:?} ", fv ); } $\C{// 1.2 42.2 5.6 7.3 9.1}$
1436for i in 0..=1 { ia[i] = i; } $\C{// mutable changes}$
1437for iv in ia { print!("{:?} ", iv ); } $\C{// 0 1 42 42 42}$
1438\end{rust}
1439An array can be assigned and printed as a set.
1440\begin{rust}
1441ia = @[5; 5]@; println!( "{:?} {:?}", @ia@, ia.len() ); $\C{// [5, 5, 5, 5, 5] 5}$
1442ia = @[1, 2, 3, 4, 5]@; println!( "{:?} {:?}", @ia@, ia.len() ); $\C{// [1, 2, 3, 4, 5] 5}$
1443\end{rust}
1444
1445Multi-dimensional arrays use nested dimensions, resulting in columns before rows.
1446Here the outer array is a length @ROWS@ array whose elements are @f64@ arrays of length @COLS@ each.
1447\begin{cquote}
1448\setlength{\tabcolsep}{10pt}
1449\begin{tabular}{@{}ll@{}}
1450\begin{rust}
1451const ROWS: usize = 4; const COLS: usize = 6;
1452let mut fmatrix: [[f64; @COLS@]; @ROWS@] = [[0.; @COLS@]; @ROWS@];
1453for r in 0 .. ROWS {
1454 for c in 0 .. COLS { fmatrix[r][c] = r as f64 + c as f64; } }
1455\end{rust}
1456&
1457\begin{rust}
1458[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
1459[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
1460[ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
1461[ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]
1462\end{rust}
1463\end{tabular}
1464\end{cquote}
1465
1466Slices borrow a section of an array (subarray) and have type @&[T]@.
1467A slice has a dynamic size and does not implicitly coerce to an array.
1468\begin{rust}
1469println!( "{:?}", @&ia[0 .. 3]@ ); $\C{// fixed bounds, [1, 2, 3]}$
1470println!( "{:?}", @&ia[ia.len() - 2 .. ia.len()@] ); $\C{// variable bounds, [4, 5]}$
1471println!( "{:?}", @&ia[ia.len() - 2 .. ]@ ); $\C{// drop upper bound, [4, 5]}$
1472println!( "{:?}", @&ia[.. ia.len() - 2 ]@ ); $\C{// drop lower bound, [1, 2, 3]}$
1473println!( "{:?}", @&ia[..]@ ); $\C{// drop both bound, [1, 2, 3, 4, 5]}$
1474\end{rust}
1475A multi-dimensional slice can only borrow subrows because a slice requires contiguous memory.
1476Here columns 2--4 are sliced from row 3.
1477\begin{rust}
1478let mut slice2: &[f64] = &fmatrix[3][@2 ..= 4@];
1479println!( "{:?}", slice2 ); $\C{// [5.0, 6.0, 7.0]}$
1480\end{rust}
1481Here row 2 is sliced from the sub-matrix formed from rows 1--3.
1482\begin{rust}
1483slice2 = &fmatrix[@1 ..= 3@][1];
1484println!( "{:?}", slice2 ); $\C{// [3.0, 4.0, 5.0, 6.0, 7.0, 8.0]}$
1485\end{rust}
1486A slice can be explicitly converted into an array or reference to an array.
1487\begin{rust}
1488let slice: &[f64];
1489slice = &fa[0 ..= 1]; $\C{// create slice, [1.2, 42.2]}$
1490let mut fa2: [f64; 2] = @<[f64; 2]>::try_from( slice ).unwrap()@; $\C{// convert slice to array, [1.2, 42.2]}$
1491fa2 = @(& fa[2 ..= 3]).try_into().unwrap()@; $\C{// convert slice to array, [5.6, 7.3]}$
1492\end{rust}
1493
1494The @vec@ macro (using @Vec@ type) provides dynamically-size dynamically-growable arrays of arrays only using the heap (\CC @vector@ type).
1495\begin{cquote}
1496\setlength{\tabcolsep}{10pt}
1497\begin{tabular}{@{}ll@{}}
1498\multicolumn{1}{c}{Rust} &\multicolumn{1}{c}{\CC} \\
1499\begin{rust}
1500let (rows, cols) = (4, 6);
1501let mut matrix = vec![vec![0; cols]; rows];
1502... matrix[r][c] = r + c; // fill matrix
1503\end{rust}
1504&
1505\begin{c++}
1506int rows = 4, cols = 6;
1507vector<vector<int>> matrix( rows, vector<int>(cols) );
1508... matrix[r][c] = r + c; // fill matrix}
1509\end{c++}
1510\end{tabular}
1511\end{cquote}
1512A dynamically-size array is sliced the same as a fixed-size one.
1513\begin{rust}
1514let mut slice3: &[usize] = &matrix[3][2 ..= 4]; $\C{// [5, 6, 7]}$
1515slice3 = &matrix[1 ..= 3][1]; $\C{// [2, 3, 4, 5, 6, 7]}$
1516\end{rust}
1517Finally, to mitigate the restriction of matching array dimensions between argument and parameter types, it is possible for a parameter to be a slice.
1518\begin{rust}
1519fn zero( arr: @& mut [usize]@ ){
1520 for i in 0 .. arr.len() { arr[i] = 0; }
1521}
1522zero( & mut ia[0..5] ); // arbitrary sized slice
1523zero( & mut ia[0..3] );
1524\end{rust}
1525Or write a \emph{generic} function taking an array of fixed-element type and any size.
1526\begin{rust}
1527fn aprint<@const N: usize@>( arg: [usize; N] ) {
1528 for r in 0 .. arg.len() { print!( "{} ", arg[r] ); }
1529}
1530aprint( ia );
1531\end{rust}
1532
1533
1534\subsection{Java}
1535\label{JavaCompare}
1536
1537Java arrays are references, so multi-dimension arrays are arrays-of-arrays \see{\VRef{toc:mdimpl}}.
1538For each array, Java implicitly storages the array dimension in a descriptor, supporting array length, subscript checking, and allowing dynamically-sized array-parameter declarations.
1539\begin{cquote}
1540\begin{tabular}{rl}
1541C & @void f( size_t n, size_t m, float x[n][m] );@ \\
1542Java & @void f( float x[][] );@
1543\end{tabular}
1544\end{cquote}
1545However, in the C prototype, the parameters @n@ and @m@ are manually managed, \ie there is no guarantee they match with the argument matrix for parameter @x@.
1546\VRef[Figure]{f:JavaVsCTriangularMatrix} compares a triangular matrix (array-of-arrays) in dynamically safe Java to unsafe C.
1547Each dynamically sized row in Java stores its dimension, while C requires the programmer to manage these sizes explicitly (@rlnth@).
1548All subscripting is Java has bounds checking, while C has none.
1549Both Java and C require explicit null checking, otherwise there is a runtime failure.
1550
1551\begin{figure}
1552\centering
1553\begin{lrbox}{\myboxA}
1554\begin{java}
1555int m[][] = { // triangular matrix
1556 new int [4],
1557 new int [3],
1558 new int [2],
1559 new int [1],
1560 null
1561};
1562
1563for ( int r = 0; r < m.length; r += 1 ) {
1564 if ( m[r] == null ) continue;
1565 for ( int c = 0; c < m[r].length; c += 1 ) {
1566 m[r][c] = c + r; // subscript checking
1567 }
1568
1569}
1570
1571for ( int r = 0; r < m.length; r += 1 ) {
1572 if ( m[r] == null ) {
1573 System.out.println( "null row" );
1574 continue;
1575 }
1576 for ( int c = 0; c < m[r].length; c += 1 ) {
1577 System.out.print( m[r][c] + " " );
1578 }
1579 System.out.println();
1580
1581}
1582\end{java}
1583\end{lrbox}
1584
1585\begin{lrbox}{\myboxB}
1586\begin{cfa}
1587int * m[5] = { // triangular matrix
1588 calloc( 4, sizeof(int) ),
1589 calloc( 3, sizeof(int) ),
1590 calloc( 2, sizeof(int) ),
1591 calloc( 1, sizeof(int) ),
1592 NULL
1593};
1594int rlnth = 4;
1595for ( int r = 0; r < 5; r += 1 ) {
1596 if ( m[r] == NULL ) continue;
1597 for ( int c = 0; c < rlnth; c += 1 ) {
1598 m[r][c] = c + r; // no subscript checking
1599 }
1600 rlnth -= 1;
1601}
1602rlnth = 4;
1603for ( int r = 0; r < 5; r += 1 ) {
1604 if ( m[r] == NULL ) {
1605 printf( "null row\n" );
1606 continue;
1607 }
1608 for ( int c = 0; c < rlnth; c += 1 ) {
1609 printf( "%d ", m[r][c] );
1610 }
1611 printf( "\n" );
1612 rlnth -= 1;
1613}
1614\end{cfa}
1615\end{lrbox}
1616
1617\subfloat[Java]{\label{f:JavaTriangularMatrix}\usebox\myboxA}
1618\hspace{6pt}
1619\vrule
1620\hspace{6pt}
1621\subfloat[C]{\label{f:CTriangularMatrix}\usebox\myboxB}
1622\hspace{6pt}
1623
1624\caption{Triangular Matrix}
1625\label{f:JavaVsCTriangularMatrix}
1626\end{figure}
1627
1628The downside of the arrays-of-arrays approach is performance due to pointer chasing versus pointer arithmetic for a contiguous arrays.
1629Furthermore, there is the cost of managing the implicit array descriptor.
1630It is unlikely that a JIT can dynamically rewrite an arrays-of-arrays form into a contiguous form.
1631
1632
1633\subsection{\CC}
1634\label{CppCompare}
1635
1636Because C arrays are difficult and dangerous, the mantra for \CC programmers is to use @std::vector@ in place of the C array.
1637While the vector size can grow and shrink dynamically, \vs an unchanging dynamic size with VLAs, the cost of this extra feature is mitigated by preallocating the maximum size (like the VLA) at the declaration.
1638So, it costs one upfront dynamic allocation and avoids growing the array through pushing.
1639\begin{c++}
1640vector< vector< int > > m( 5, vector<int>(8) ); // initialize size of 5 x 8 with 6 dynamic allocations
1641\end{c++}
1642Multidimensional arrays are arrays-of-arrays with associated costs.
1643Each @vector@ array has an array descriptor containing the dimension, which allows bound checked using @x.at(i)@ in place of @x[i]@.
1644Used with these restrictions, out-of-bound accesses are caught, and in-bound accesses never exercise the vector's ability to grow, preventing costly reallocate-and-copy, and never invalidating references to contained values.
1645
1646This scheme matches a Java array's safety and expressiveness exactly, with the same inherent costs.
1647Notably, a \CC vector of vectors does not provide contiguous element storage (even when upfront allocation is done carefully) because a vector puts its elements in an auxiliary allocation.
1648So, in spite of @vector< vector< int > >@ appearing to be ``everything by value,'' it is still a first array whose elements include pointers to further arrays.
1649
1650\CC has options for working with memory-adjacent data in desirable ways, particularly in recent revisions.
1651But none makes an allocation with a dynamically given fixed size less awkward than the vector arrangement just described.
1652
1653\CC~26 adds @std::inplace_vector@ providing an interesting vector--array hybrid.
1654Like a vector, it lets a user construct elements in a loop, rather than imposing uniform construction.
1655Yet it preserves \lstinline{std::array}'s ability to be entirely stack-allocated, by avoiding an auxiliary elements' allocation.
1656However, it preserves the fundamental limit of \lstinline{std::array}, that the length, being a template parameter, must be a static value.
1657
1658\CC~20's @std::span@ is a view that unifies true arrays, vectors, static sizes and dynamic sizes, under a common API that adds bounds checking.
1659When wrapping a vector, bounds checking occurs on regular subscripting, \ie one need not remember to use @.at@.
1660When wrapping a locally declared fixed-size array, bound communication is implicit.
1661But it has a soundness gap by offering construction from pointer and user-given length.
1662
1663\CC~23's @std::mdspan@ adds multidimensional indexing and reshaping capabilities analogous to those built into \CFA's @array@.
1664Like @span@, it works over multiple underlying container types.
1665But neither @span@ nor @mdspan@ augments the available allocation options.
1666
1667Thus, these options do not offer an allocation with a dynamically given fixed size.
1668And furthermore, they do not provide any improvement to the C flexible array member pattern, for making a dynamic amount of storage contiguous with its header, as do \CFA's accordions.
1669
1670
1671\begin{comment}
1672\subsection{Levels of Dependently Typed Arrays}
1673
1674TODO: fix the position; checked c does not do linear types
1675\CFA's array is the first lightweight application of dependently-typed bound tracking to an extension of C.
1676Other extensions of C that apply dependently-typed bound tracking are heavyweight, in that the bound tracking is part of a linearly-typed ownership-system, which further helps guarantee statically the validity of every pointer deference.
1677These systems, therefore, ask the programmer to convince the type checker that every pointer dereference is valid.
1678\CFA imposes the lighter-weight obligation, with the more limited guarantee, that initially-declared bounds are respected thereafter.
1679
1680\CFA's array is also the first extension of C to use its tracked bounds to generate the pointer arithmetic implied by advanced allocation patterns.
1681Other bound-tracked extensions of C either forbid certain C patterns entirely, or address the problem of \emph{verifying} that the user's provided pointer arithmetic is self-consistent.
1682The \CFA array, applied to accordion structures [TOD: cross-reference] \emph{implies} the necessary pointer arithmetic, generated automatically, and not appearing at all in a user's program.
1683
1684The \CFA array and the field of ``array language'' comparators all leverage dependent types to improve on the expressiveness over C and Java, accommodating examples such as:
1685\begin{itemize}[leftmargin=*]
1686\item a \emph{zip}-style operation that consumes two arrays of equal length
1687\item a \emph{map}-style operation whose produced length matches the consumed length
1688\item a formulation of matrix multiplication, where the two operands must agree on a middle dimension, and where the result dimensions match the operands' outer dimensions
1689\end{itemize}
1690Across this field, this expressiveness is not just an available place to document such assumption, but these requirements are strongly guaranteed by default, with varying levels of statically/dynamically checked and ability to opt out.
1691Along the way, the \CFA array also closes the safety gap (with respect to bounds) that Java has over C.
1692
1693Dependent type systems, considered for the purpose of bound-tracking, can be full-strength or restricted.
1694In a full-strength dependent type system, a type can encode an arbitrarily complex predicate, with bound-tracking being an easy example.
1695The tradeoff of this expressiveness is complexity in the checker, even typically, a potential for its nontermination.
1696In a restricted dependent type system (purposed for bound tracking), the goal is to check helpful properties, while keeping the checker well-behaved; the other restricted checkers surveyed here, including \CFA's, always terminate.
1697[TODO: clarify how even Idris type checking terminates]
1698
1699Idris is a current, general-purpose dependently typed programming language.
1700Length checking is a common benchmark for full dependent type systems.
1701Here, the capability being considered is to track lengths that adjust during the execution of a program, such as when an \emph{add} operation produces a collection one element longer than the one on which it started.
1702[TODO: finish explaining what Data.Vect is and then the essence of the comparison]
1703
1704POINTS:
1705here is how our basic checks look (on a system that does not have to compromise);
1706it can also do these other cool checks, but watch how I can mess with its conservativeness and termination
1707
1708Two contemporary array-centric languages, Dex\cite{arr:dex:long} and Futhark\cite{arr:futhark:tytheory}, contribute similar, restricted dependent types for tracking array length.
1709Unlike \CFA, both are garbage-collected functional languages.
1710Because they are garbage-collected, referential integrity is built-in, meaning that the heavyweight analysis, that \CFA aims to avoid, is unnecessary.
1711So, like \CFA, the checking in question is a lightweight bounds-only analysis.
1712Like \CFA, their checks that are conservatively limited by forbidding arithmetic in the depended-upon expression.
1713
1714
1715
1716The Futhark work discusses the working language's connection to a lambda calculus, with typing rules and a safety theorem proven in reference to an operational semantics.
1717There is a particular emphasis on an existential type, enabling callee-determined return shapes.
1718
1719Dex uses an Ada-style conception of size, embedding quantitative information completely into an ordinary type.
1720
1721Futhark and full-strength dependently typed languages treat array sizes as ordinary values.
1722Futhark restricts these expressions syntactically to variables and constants, while a full-strength dependent system does not.
1723
1724\CFA's hybrid presentation, @forall( [N] )@, has @N@ belonging to the type system, yet no objects of type @[N]@ occur.
1725Belonging to the type system means it is inferred at a call site and communicated implicitly, like in Dex and unlike in Futhark.
1726Having no instances means there is no type for a variable @i@ that constrains @i@ to be in the range for @N@, unlike Dex, [TODO: verify], but like Futhark.
1727
1728If \CFA gets such a system for describing the list of values in a type, then \CFA arrays are poised to move from the Futhark level of expressiveness, up to the Dex level.
1729
1730[TODO: introduce Ada in the comparators]
1731
1732In Ada and Dex, an array is conceived as a function whose domain must satisfy only certain structural assumptions, while in C, \CC, Java, Futhark and \CFA today, the domain is a prefix of the natural numbers.
1733The Ada--Dex generality has aesthetic benefits for certain programmers. For those working on scheduling resources to weekdays:
1734For those who prefer to count from an initial number of their own choosing:
1735
1736This change of perspective also lets us remove ubiquitous dynamic bound checks.
1737[TODO: xref] discusses how automatically inserted bound checks can often be optimized away.
1738But this approach is unsatisfying to a programmer who believes she has written code in which dynamic checks are unnecessary, but now seeks confirmation.
1739To remove the ubiquitous dynamic checking is to say that an ordinary subscript operation is only valid when it can be statically verified to be in-bound (and so the ordinary subscript is not dynamically checked), and an explicit dynamic check is available when the static criterion is impractical to meet.
1740
1741[TODO, fix confusion: Idris has this arrangement of checks, but still the natural numbers as the domain.]
1742
1743The structural assumptions required for the domain of an array in Dex are given by the trait (there, ``interface'') @Ix@, which says that the parameter @n@ is a type (which could take an argument like @weekday@) that provides two-way conversion with the integers and a report on the number of values.
1744Dex's @Ix@ is analogous the @is_enum@ proposed for \CFA above.
1745\begin{cfa}
1746interface Ix n
1747get_size n : Unit -> Int
1748ordinal : n -> Int
1749unsafe_from_ordinal n : Int -> n
1750\end{cfa}
1751
1752Dex uses this foundation of a trait (as an array type's domain) to achieve polymorphism over shapes.
1753This flavour of polymorphism lets a function be generic over how many (and the order of) dimensions a caller uses when interacting with arrays communicated with this function.
1754Dex's example is a routine that calculates pointwise differences between two samples.
1755Done with shape polymorphism, one function body is equally applicable to a pair of single-dimensional audio clips (giving a single-dimensional result) and a pair of two-dimensional photographs (giving a two-dimensional result).
1756In both cases, but with respectively dimensioned interpretations of ``size,'' this function requires the argument sizes to match, and it produces a result of the that size.
1757
1758The polymorphism plays out with the pointwise-difference routine advertising a single-dimensional interface whose domain type is generic.
1759In the audio instantiation, the duration-of-clip type argument is used for the domain.
1760In the photograph instantiation, it's the tuple-type of $ \langle \mathrm{img\_wd}, \mathrm{img\_ht} \rangle $.
1761This use of a tuple-as-index is made possible by the built-in rule for implementing @Ix@ on a pair, given @Ix@ implementations for its elements
1762\begin{cfa}
1763instance {a b} [Ix a, Ix b] Ix (a & b)
1764get_size = \(). size a * size b
1765ordinal = \(i, j). (ordinal i * size b) + ordinal j
1766unsafe_from_ordinal = \o.
1767bs = size b
1768(unsafe_from_ordinal a (idiv o bs), unsafe_from_ordinal b (rem o bs))
1769\end{cfa}
1770% fix mike's syntax highlighter
1771and by a user-provided adapter expression at the call site that shows how to indexing with a tuple is backed by indexing each dimension at a time
1772\begin{cfa}
1773img_trans :: (img_wd,img_ht)=>Real
1774img_trans.(i,j) = img.i.j
1775result = pairwise img_trans
1776\end{cfa}
1777[TODO: cite as simplification of example from https://openreview.net/pdf?id=rJxd7vsWPS section 4]
1778
1779In the case of adapting this pattern to \CFA, my current work provides an adapter from ``successively subscripted'' to ``subscripted by tuple,'' so it is likely that generalizing my adapter beyond ``subscripted by @ptrdiff_t@'' is sufficient to make a user-provided adapter unnecessary.
1780
1781\subsection{Static Safety in C Extensions}
1782\end{comment}
1783
1784
1785\section{Future Work}
1786\label{f:FutureWork}
1787
1788% \subsection{Array Syntax}
1789
1790An important goal is to recast \CFA-style @array(...)@ syntax into C-style array syntax.
1791The proposal (which is partially implemented) is an alternate dimension and subscript syntax for multi-dimension arrays.
1792C multi-dimension and subscripting syntax uses multiple bracketed constants/expressions.
1793\begin{cfa}
1794int m@[2][3]@; // dimension
1795m@[0][1]@ = 3; // subscript
1796\end{cfa}
1797The alternative \CFA syntax is a comma separated list:
1798\begin{cfa}
1799int m@[2, 3]@; // dimension
1800m@[0, 1]@ = 3; // subscript
1801\end{cfa}
1802which should be intuitive to C programmers and is used in mathematics $M_{i,j}$ and other programing languages, \eg PL/I, Fortran.
1803With respect to the dimension expressions, C only allows an assignment expression, not a comma expression.
1804\begin{cfa}
1805 a[i, j];
1806test.c:3:16: error: expected ']' before ',' token
1807\end{cfa}
1808However, there is an ambiguity for a single dimension array, where the syntax for old and new arrays are the same, @int ar[10]@.
1809The solution is to use a terminating comma to denote a \CFA-style single-dimension array.
1810\begin{cfa}
1811int ar[2$\Huge\color{red},$]; // single dimension new array
1812\end{cfa}
1813This syntactic form is also used for the (rare) singleton tuple @[y@{\Large\color{red},}@]@.
1814The extra comma in the dimension is only mildly annoying, and acts as eye-candy differentiating old and new arrays.
1815Hence, \CFA can repurpose the comma expression in this context for a list of dimensions.
1816The ultimate goal is to replace all C arrays with \CFA arrays, establishing a higher level of safety in C programs, and eliminating the need for the terminating comma.
1817With respect to the subscript expression, the comma expression is allowed.
1818However, a comma expression in this context is rare, and is most commonly a (silent) mistake: subscripting a matrix with @m[i, j]@ instead of @m[i][j]@ selects the @j@th row not the @i, j@ element.
1819It is still possible to write @m[(i, j)]@ in the new syntax to achieve the equivalent of the old @m[i, j]@.
1820Internally, the compiler must de-sugar @[i, j, k]@ into @[i][j][k]@ to match with three calls to subscript operators.
1821Note, there is no ambiguity for subscripting a single dimensional array, as the subscript operator selects the correct form from the array type.
1822Currently, @array@ supports the old and new subscript syntax \see{\VRef[Figure]{f:ovhd-treat-src}}, including combinations of new and old, @arr[1, 2][3]@.
1823The new subscript syntax can be extended to C arrays for uniformity, but requires the non-compatible removal of the (rare) comma-expression as a subscript.
1824
1825
1826\begin{comment}
1827\subsection{Range Slicing}
1828
1829\subsection{With a Module System}
1830
1831
1832\subsection{Retire Pointer Arithmetic}
1833
1834
1835\section{\texorpdfstring{\CFA}{Cforall}}
1836
1837XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
1838moved from background chapter \\
1839XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
1840
1841Traditionally, fixing C meant leaving the C-ism alone, while providing a better alternative beside it.
1842(For later: That's what I offer with array.hfa, but in the future-work vision for arrays, the fix includes helping programmers stop accidentally using a broken C-ism.)
1843
1844\subsection{\texorpdfstring{\CFA}{Cforall} Features Interacting with Arrays}
1845
1846Prior work on \CFA included making C arrays, as used in C code from the wild,
1847work, if this code is fed into @cfacc@.
1848The quality of this this treatment was fine, with no more or fewer bugs than is typical.
1849
1850More mixed results arose with feeding these ``C'' arrays into preexisting \CFA features.
1851
1852A notable success was with the \CFA @alloc@ function,
1853which type information associated with a polymorphic return type
1854replaces @malloc@'s use of programmer-supplied size information.
1855\begin{cfa}
1856// C, library
1857void * malloc( size_t );
1858// C, user
1859struct tm * el1 = malloc( sizeof(struct tm) );
1860struct tm * ar1 = malloc( 10 * sizeof(struct tm) );
1861
1862// CFA, library
1863forall( T * ) T * alloc();
1864// CFA, user
1865tm * el2 = alloc();
1866tm (*ar2)[10] = alloc();
1867\end{cfa}
1868The alloc polymorphic return compiles into a hidden parameter, which receives a compiler-generated argument.
1869This compiler's argument generation uses type information from the left-hand side of the initialization to obtain the intended type.
1870Using a compiler-produced value eliminates an opportunity for user error.
1871
1872...someday... fix in following: even the alloc call gives bad code gen: verify it was always this way; walk back the wording about things just working here; assignment (rebind) seems to offer workaround, as in bkgd-cfa-arrayinteract.cfa
1873
1874Bringing in another \CFA feature, reference types, both resolves a sore spot of the last example, and gives a first example of an array-interaction bug.
1875In the last example, the choice of ``pointer to array'' @ar2@ breaks a parallel with @ar1@.
1876They are not subscripted in the same way.
1877\begin{cfa}
1878ar1[5];
1879(*ar2)[5];
1880\end{cfa}
1881Using ``reference to array'' works at resolving this issue. ...someday... discuss connection with Doug-Lea \CC proposal.
1882\begin{cfa}
1883tm (&ar3)[10] = *alloc();
1884ar3[5];
1885\end{cfa}
1886The implicit size communication to @alloc@ still works in the same ways as for @ar2@.
1887
1888Using proper array types (@ar2@ and @ar3@) addresses a concern about using raw element pointers (@ar1@), albeit a theoretical one.
1889...someday... xref C standard does not claim that @ar1@ may be subscripted,
1890because no stage of interpreting the construction of @ar1@ has it be that ``there is an \emph{array object} here.''
1891But both @*ar2@ and the referent of @ar3@ are the results of \emph{typed} @alloc@ calls,
1892where the type requested is an array, making the result, much more obviously, an array object.
1893
1894The ``reference to array'' type has its sore spots too.
1895...someday... see also @dimexpr-match-c/REFPARAM_CALL@ (under @TRY_BUG_1@)
1896
1897...someday... I fixed a bug associated with using an array as a T. I think. Did I really? What was the bug?
1898\end{comment}
Note: See TracBrowser for help on using the repository browser.