# source:doc/theses/mike_brooks_MMath/array.tex@c721105

Last change on this file since c721105 was c721105, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

proofreading changes

• Property mode set to 100644
File size: 36.1 KB
Line
1\chapter{Array}
2\label{c:Array}
3
4\section{Introduction}
5
6This chapter describes my contribution of language and library features that provide a length-checked array type, as in:
7
8\begin{lstlisting}
9array(float, 99) x;    // x contains 99 floats
10
11void f( array(float, 42) & a ) {}
12f(x);                  // statically rejected: types are different
13
14forall( T, [N] )
15void g( array(T, N) & a, int i ) {
16        T elem = a[i];     // dynamically checked: requires 0 <= i < N
17}
18g(x, 0);               // T is float, N is 99, succeeds
19g(x, 1000);            // T is float, N is 99, dynamic check fails
20\end{lstlisting}
21
22This example first declares @x@ a variable, whose type is an instantiation of the generic type named @array@, with arguments @float@ and @99@.
23Next, it declares @f@ as a function that expects a length-42 array; the type system rejects the call's attempt to pass @x@ to @f@, because the lengths do not match.
24Next, the @forall@ annotation on function @g@ introduces @T@ as a familiar type parameter and @N@ as a \emph{dimension} parameter, a new feature that represents a count of elements, as managed by the type system.
25Because @g@ accepts any length of array; the type system accepts the calls' passing @x@ to @g@, inferring that this length is 99.
26Just as the caller's code does not need to explain that @T@ is @float@, the safe capture and communication of the value @99@ occurs without programmer involvement.
27In the case of the second call (which passes the value 1000 for @i@), within the body of @g@, the attempt to subscript @a@ by @i@ fails with a runtime error, since $@i@ \nless @N@$.
28
29The type @array@, as seen above, comes from my additions to the \CFA standard library.
30It is very similar to the built-in array type, which \CFA inherits from C.
31Its runtime characteristics are often identical, and some features are available in both.
32
33\begin{lstlisting}
34forall( [N] )
35void declDemo() {
36        float a1[N];         // built-in type ("C array")
37        array(float, N) a2;  // type from library
38}
39\end{lstlisting}
40
41If a caller instantiates @N@ with 42, then both locally-declared array variables, @a1@ and @a2@, become arrays of 42 elements, each element being a @float@.
42The two variables have identical size and layout; they both encapsulate 42-float stack allocations, no heap allocations, and no further "bookkeeping" allocations/header.
43Having the @array@ library type (that of @a2@) is a tactical measure, an early implementation that offers full feature support.
44A future goal (TODO xref) is to port all of its features into the built-in array type (that of @a1@); then, the library type could be removed, and \CFA would have only one array type.
45In present state, the built-in array has partial support for the new features.
46The fully-featured library type is used exclusively in introductory examples; feature support and C compatibility are revisited in sec TODO.
47
48Offering the @array@ type, as a distinct alternative from the the C array, is consistent with \CFA's extension philosophy (TODO xref background) to date.
49A few compatibility-breaking changes to the behaviour of the C array were also made, both as an implementation convenience, and as justified fixes to C's lax treatment.
50
51The @array@ type is an opportunity to start from a clean slate and show a cohesive selection of features.
52A clean slate was an important starting point because it meant not having to deal with every inherited complexity introduced in TODO xref background-array.
53
54
55My contributions are
56\begin{itemize}
57\item a type system enhancement that lets polymorphic functions and generic types be parameterized by a numeric value: @forall( [N] )@
58\item TODO: general parking...
59\item identify specific abilities brought by @array@
60\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
61\end{itemize}
62
63
64\section{Definitions and design considerations}
65
66
67\subsection{Dependent typing}
68
69
70\section{Features Added}
71
72The present work adds a type @array@ to the \CFA standard library~\cite{Cforall}.
73
74This array's length is statically managed and dynamically valued.
75This static management achieves argument safety and suggests a path to subscript safety as future work (TODO: cross reference).
76
77This section presents motivating examples of the new array type's usage and follows up with definitions of the notations that appear.
78
79The core of the new array management is tracking all array lengths in the type system.
80Dynamically valued lengths are represented using type variables.
81The stratification of type variables preceding object declarations makes a length referenceable everywhere that it is needed.
82For example, a declaration can share one length, @N@, among a pair of parameters and the return.
83\lstinput{10-17}{hello-array.cfa}
84Here, the function @f@ does a pointwise comparison, checking if each pair of numbers is within half a percent of each other, returning the answers in a newly allocated @bool@ array.
85
86The array type uses the parameterized length information in its @sizeof@ determination, illustrated in the example's call to @alloc@.
87That call requests an allocation of type @array(bool, N)@, which the type system deduces from the left-hand side of the initialization, into the return type of the @alloc@ call.
88Preexisting \CFA behaviour is leveraged here, both in the return-type-only polymorphism, and the @sized(T)@-aware standard-library @alloc@ routine.
89The new @array@ type plugs into this behaviour by implementing the @sized@/@sizeof@ assertion to have the intuitive meaning.
90As a result, this design avoids an opportunity for programmer error by making the size/length communication to a called routine implicit, compared with C's @calloc@ (or the low-level \CFA analog @aalloc@), which take an explicit length parameter not managed by the type system.
91
92\VRef[Figure]{f:fHarness} shows the harness to use the @f@ function illustrating how dynamic values are fed into the system.
93Here, the @a@ array is loaded with decreasing values, and the @b@ array with amounts off by a constant, giving relative differences within tolerance at first and out of tolerance later.
94The program main is run with two different inputs of sequence length.
95
96\begin{figure}
97\lstinput{30-49}{hello-array.cfa}
98\caption{\lstinline{f} Harness}
99\label{f:fHarness}
100\end{figure}
101
102The loops in the program main follow the more familiar pattern of using the ordinary variable @n@ to convey the length.
103The type system implicitly captures this value at the call site (@main@ calling @f@) and makes it available within the callee (@f@'s loop bound).
104
105The two parts of the example show @n@ adapting a variable into a type-system managed length (at @main@'s declarations of @a@, @b@, and @result@), @N@ adapting in the opposite direction (at @f@'s loop bound), and a pass-thru use of a managed length (at @f@'s declaration of @ret@).
106
107The @forall( ...[N] )@ participates in the user-relevant declaration of the name @N@, which becomes usable in parameter/return declarations and in the function @b@.
108The present form is chosen to parallel the existing @forall@ forms:
109\begin{cfa}
110forall( @[N]@ ) ... // array kind
111forall( & T  ) ...  // reference kind (dtype)
112forall( T  ) ...    // value kind (otype)
113\end{cfa}
114
115The notation @array(thing, N)@ is a single-dimensional case, giving a generic type instance.
116In summary:
117\begin{itemize}
118\item
119@[N]@ -- within a forall, declares the type variable @N@ to be a managed length
120\item
121$e$ -- a type representing the value of $e$ as a managed length, where $e$ is a @size_t@-typed expression
122\item
123N -- an expression of type @size_t@, whose value is the managed length @N@
124\item
125@array( thing, N0, N1, ... )@ -- a type wrapping $\prod_i N_i$ adjacent occurrences of @thing@ objects
126\end{itemize}
127Unsigned integers have a special status in this type system.
128Unlike how C++ allows
129\begin{c++}
130template< size_t N, char * msg, typename T >... // declarations
131\end{c++}
132\CFA does not accommodate values of any user-provided type.
133TODO: discuss connection with dependent types.
134An example of a type error demonstrates argument safety.
135The running example has @f@ expecting two arrays of the same length.
136A compile-time error occurs when attempting to call @f@ with arrays whose lengths may differ.
137\begin{cfa}
138forall( [M], [N] )
139void bad( array(float, M) &a, array(float, N) &b ) {
140        f( a, a ); // ok
141        f( b, b ); // ok
142        f( a, b ); // error
143}
144\end{cfa}
145%\lstinput{60-65}{hello-array.cfa}
146As is common practice in C, the programmer is free to cast, to assert knowledge not shared with the type system.
147\begin{cfa}
148forall( [M], [N] )
149void bad_fixed( array(float, M) & a, array(float, N) & b ) {
150        if ( M == N ) {
151            f( a, (array(float, M) &)b ); // cast b to matching type
152        }
153}
154\end{cfa}
155%\lstinput{70-75}{hello-array.cfa}
156
157Argument safety and the associated implicit communication of array length work with \CFA's generic types too.
158\CFA allows aggregate types to be generalized with multiple type parameters, including parameterized element type, so can it be defined over a parameterized length.
159Doing so gives a refinement of C's flexible array member'' pattern, that allows nesting structures with array members anywhere within other structures.
160\lstinput{10-16}{hello-accordion.cfa}
161This structure's layout has the starting offset of @cost_contribs@ varying in @Nclients@, and the offset of @total_cost@ varying in both generic parameters.
162For a function that operates on a @request@ structure, the type system handles this variation transparently.
163\lstinput{40-47}{hello-accordion.cfa}
164In the example, different runs of the program result in different offset values being used.
165\lstinput{60-76}{hello-accordion.cfa}
166The output values show that @summarize@ and its caller agree on both the offsets (where the callee starts reading @cost_contribs@ and where the callee writes @total_cost@).
167Yet the call site still says just, pass the request.''
168
169
170\section{Multidimensional implementation}
171\label{toc:mdimpl}
172
173TODO: introduce multidimensional array feature and approaches
174
175The new \CFA standard library @array@ datatype supports multidimensional uses more richly than the C array.
176The new array's multidimensional interface and implementation, follows an array-of-arrays setup, meaning, like C's @float[n][m]@ type, one contiguous object, with coarsely-strided dimensions directly wrapping finely-strided dimensions.
177This setup is in contrast with the pattern of array of pointers to other allocations representing a sub-array.
178Beyond what C's type offers, the new array brings direct support for working with a noncontiguous array slice, allowing a program to work with dimension subscripts given in a non-physical order.
179C and C++ require a programmer with such a need to manage pointer/offset arithmetic manually.
180
181Examples are shown using a $5 \times 7$ float array, @a@, loaded with increments of $0.1$ when stepping across the length-7 finely-strided dimension shown on columns, and with increments of $1.0$ when stepping across the length-5 coarsely-strided dimension shown on rows.
182%\lstinput{120-126}{hello-md.cfa}
183The memory layout of @a@ has strictly increasing numbers along its 35 contiguous positions.
184
185A trivial form of slicing extracts a contiguous inner array, within an array-of-arrays.
186Like with 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.
187This action first subscripts away the most coarsely strided dimensions, leaving a result that expects to be be subscripted by the more finely strided dimensions.
188\lstinput{60-66}{hello-md.cfa}
189\lstinput[aboveskip=0pt]{140-140}{hello-md.cfa}
190
191This function declaration is asserting too much knowledge about its parameter @c@, for it to be usable for printing either a row slice or a column slice.
192Specifically, declaring the parameter @c@ with type @array@ means that @c@ is contiguous.
193However, the function does not use this fact.
194For the function to do its job, @c@ need only be of a container type that offers a subscript operator (of type @ptrdiff_t@ $\rightarrow$ @float@), with managed length @N@.
195The new-array library provides the trait @ix@, so-defined.
196With it, the original declaration can be generalized, while still implemented with the same body, to the latter declaration:
197\lstinput{40-44}{hello-md.cfa}
198\lstinput[aboveskip=0pt]{145-145}{hello-md.cfa}
199
200Nontrivial slicing, in this example, means passing a noncontiguous slice to @print1d@.
201The new-array library provides a subscript by all'' operation for this purpose.
202In a multi-dimensional subscript operation, any dimension given as @all@ is left not yet subscripted by a value,'' implementing the @ix@ trait, waiting for such a value.
203\lstinput{150-151}{hello-md.cfa}
204
205The example has shown that @a[2]@ and @a[[2, all]]@ both refer to the same, 2.*'' slice.
206Indeed, the various @print1d@ calls under discussion access the entry with value 2.3 as @a[2][3]@, @a[[2,all]][3]@, and @a[[all,3]][2]@.
207This design preserves (and extends) C array semantics by defining @a[[i,j]]@ to be @a[i][j]@ for numeric subscripts, but also for subscripting by all''.
208That is:
209
210\begin{tabular}{cccccl}
211@a[[2,all]][3]@  &  $=$  &  @a[2][all][3]@  & $=$  &  @a[2][3]@  & (here, @all@ is redundant)  \\
212@a[[all,3]][2]@  &  $=$  &  @a[all][3][2]@  & $=$  &  @a[2][3]@  & (here, @all@ is effective)
213\end{tabular}
214
215Narrating progress through each of the @-[-][-][-]@ expressions gives, firstly, a definition of @-[all]@, and secondly, a generalization of C's @-[i]@.
216
217\noindent Where @all@ is redundant:
218
219\begin{tabular}{ll}
220@a@  & 2-dimensional, want subscripts for coarse then fine \\
221@a[2]@  & 1-dimensional, want subscript for fine; lock coarse = 2 \\
222@a[2][all]@  & 1-dimensional, want subscript for fine \\
223@a[2][all][3]@  & 0-dimensional; lock fine = 3
224\end{tabular}
225
226\noindent Where @all@ is effective:
227
228\begin{tabular}{ll}
229@a@  & 2-dimensional, want subscripts for coarse then fine \\
230@a[all]@  & 2-dimensional, want subscripts for fine then coarse \\
231@a[all][3]@  & 1-dimensional, want subscript for coarse; lock fine = 3 \\
232@a[all][3][2]@  & 0-dimensional; lock coarse = 2
233\end{tabular}
234
235The semantics of @-[all]@ is to dequeue from the front of the want subscripts'' list and re-enqueue at its back.
236The semantics of @-[i]@ is to dequeue from the front of the want subscripts'' list and lock its value to be @i@.
237
238Contiguous arrays, and slices of them, are all realized by the same underlying parameterized type.
239It includes stride information in its metatdata.
240The @-[all]@ operation is a conversion from a reference to one instantiation, to a reference to another instantiation.
241The running example's @all@-effective step, stated more concretely, is:
242
243\begin{tabular}{ll}
244@a@       & : 5 of ( 7 of float each spaced 1 float apart ) each spaced 7 floats apart \\
245@a[all]@  & : 7 of ( 5 of float each spaced 7 floats apart ) each spaced 1 float apart
246\end{tabular}
247
248\begin{figure}
249\includegraphics{measuring-like-layout}
250\caption{Visualization of subscripting by value and by \lstinline[language=CFA,basicstyle=\ttfamily]{all}, for \lstinline[language=CFA,basicstyle=\ttfamily]{a} of type \lstinline[language=CFA,basicstyle=\ttfamily]{array( float, 5, 7 )}.
251The horizontal dimension represents memory addresses while vertical layout is conceptual.}
252\label{fig:subscr-all}
253\end{figure}
254
255\noindent While the latter description implies overlapping elements, Figure \ref{fig:subscr-all} shows that the overlaps only occur with unused spaces between elements.
256Its depictions of @a[all][...]@ show the navigation of a memory layout with nontrivial strides, that is, with spaced \_ floats apart'' values that are greater or smaller than the true count of valid indices times the size of a logically indexed element.
257Reading from the bottom up, the expression @a[all][3][2]@ shows a float, that is masquerading as a @float[7]@, for the purpose of being arranged among its peers; five such occurrences form @a[all][3]@.
258The tail of flatter boxes extending to the right of a proper element represents this stretching.
259At the next level of containment, the structure @a[all][3]@ masquerades as a @float[1]@, for the purpose of being arranged among its peers; seven such occurrences form @a[all]@.
260The vertical staircase arrangement represents this compression, and resulting overlapping.
261
262The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping.
263The private @arpk@ structure (array with explicit packing) is generic over these two types (and more): the contained element, what it is masquerading as.
264This structure's public interface is the @array(...)@ construction macro and the two subscript operators.
265Construction by @array@ initializes the masquerading-as type information to be equal to the contained-element information.
266Subscripting by @all@ rearranges the order of masquerading-as types to achieve, in general, nontrivial striding.
267Subscripting by a number consumes the masquerading-as size of the contained element type, does normal array stepping according to that size, and returns there element found there, in unmasked form.
268
269The @arpk@ structure and its @-[i]@ operator are thus defined as:
270\begin{lstlisting}
271forall( ztype(N),               // length of current dimension
272        dtype(S) | sized(S),    // masquerading-as
273        dtype E_im,             // immediate element, often another array
274        dtype E_base            // base element, e.g. float, never array
275 ) {
276struct arpk {
277        S strides[N];           // so that sizeof(this) is N of S
278};
279
280// expose E_im, stride by S
281E_im & ?[?]( arpk(N, S, E_im, E_base) & a, ptrdiff_t i ) {
282        return (E_im &) a.strides[i];
283}
284}
285\end{lstlisting}
286
287An 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, ...)@.
288In the base case, @array(E_base)@ is just @E_base@.
289Because this construction uses the same value for the generic parameters @S@ and @E_im@, the resulting layout has trivial strides.
290
291Subscripting 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.
292Expressed as an operation on types, this rotation is:
293\begin{eqnarray*}
294suball( arpk(N, S, E_i, E_b) ) & = & enq( N, S, E_i, E_b ) \\
295enq( N, S, E_b, E_b ) & = & arpk( N, S, E_b, E_b ) \\
296enq( N, S, arpk(N', S', E_i', E_b), E_b ) & = & arpk( N', S', enq(N, S, E_i', E_b), E_b )
297\end{eqnarray*}
298
299
300\section{Bound checks, added and removed}
301
302\CFA array subscripting is protected with runtime bound checks.
303Having dependent typing causes the optimizer to remove more of these bound checks than it would without them.
304This section provides a demonstration of the effect.
305
306The experiment compares the \CFA array system with the padded-room system [TODO:xref] most typically exemplified by Java arrays, but also reflected in the C++ pattern where restricted vector usage models a checked array.
307The essential feature of this padded-room system is the one-to-one correspondence between array instances and the symbolic bounds on which dynamic checks are based.
308The experiment compares with the C++ version to keep access to generated assembly code simple.
309
310As a control case, a simple loop (with no reused dimension sizes) is seen to get the same optimization treatment in both the \CFA and C++ versions.
311When the programmer treats the array's bound correctly (making the subscript obviously fine''), no dynamic bound check is observed in the program's optimized assembly code.
312But when the bounds are adjusted, such that the subscript is possibly invalid, the bound check appears in the optimized assembly, ready to catch an occurrence the mistake.
313
314TODO: paste source and assembly codes
315
316Incorporating reuse among dimension sizes is seen to give \CFA an advantage at being optimized.
317The case is naive matrix multiplication over a row-major encoding.
318
319TODO: paste source codes
320
321
322
323
324
325\section{Comparison with other arrays}
326
327\CFA's array is the first lightweight application of dependently-typed bound tracking to an extension of C.
328Other extensions of C that apply dependently-typed bound tracking are heavyweight, in that the bound tracking is part of a linearly typed ownership system that further helps guarantee statically the validity of every pointer deference.
329These systems, therefore, ask the programmer to convince the type checker that every pointer dereference is valid.
330\CFA imposes the lighter-weight obligation, with the more limited guarantee, that initially-declared bounds are respected thereafter.
331
332\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.
333Other 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.
334The \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.
335
336\subsection{Safety in a padded room}
337
338Java's array [TODO:cite] is a straightforward example of assuring safety against undefined behaviour, at a cost of expressiveness for more applied properties.
339Consider the array parameter declarations in:
340
341\begin{tabular}{rl}
342C      &  @void f( size_t n, size_t m, float a[n][m] );@ \\
343Java   &  @void f( float[][] a );@
344\end{tabular}
345
346Java's safety against undefined behaviour assures the callee that, if @a@ is non-null, then @a.length@ is a valid access (say, evaluating to the number $\ell$) and if @i@ is in $[0, \ell)$ then @a[i]@ is a valid access.
347If a value of @i@ outside this range is used, a runtime error is guaranteed.
348In these respects, C offers no guarantees at all.
349Notably, the suggestion that @n@ is the intended size of the first dimension of @a@ is documentation only.
350Indeed, many might prefer the technically equivalent declarations @float a[][m]@ or @float (*a)[m]@ as emphasizing the no guarantees'' nature of an infrequently used language feature, over using the opportunity to explain a programmer intention.
351Moreover, even if @a[0][0]@ is valid for the purpose intended, C's basic infamous feature is the possibility of an @i@, such that @a[i][0]@ is not valid for the same purpose, and yet, its evaluation does not produce an error.
352
353Java's lack of expressiveness for more applied properties means these outcomes are possible:
354\begin{itemize}
355\item @a[0][17]@ and @a[2][17]@ are valid accesses, yet @a[1][17]@ is a runtime error, because @a[1]@ is a null pointer
356\item the same observation, now because @a[1]@ refers to an array of length 5
357\item execution times vary, because the @float@ values within @a@ are sometimes stored nearly contiguously, and other times, not at all
358\end{itemize}
359C's array has none of these limitations, nor do any of the array language'' comparators discussed in this section.
360
361This Java level of safety and expressiveness is also exemplified in the C family, with the commonly given advice [TODO:cite example], for C++ programmers to use @std::vector@ in place of the C++ language's array, which is essentially the C array.
362The advice is that, while a vector is also more powerful (and quirky) than an array, its capabilities include options to preallocate with an upfront size, to use an available bound-checked accessor (@a.at(i)@ in place of @a[i]@), to avoid using @push_back@, and to use a vector of vectors.
363Used with these restrictions, out-of-bound accesses are stopped, and in-bound accesses never exercise the vector's ability to grow, which is to say, they never make the program slow to reallocate and copy, and they never invalidate the program's other references to the contained values.
364Allowing this scheme the same referential integrity assumption that \CFA enjoys [TODO:xref], this scheme matches Java's safety and expressiveness exactly.
365[TODO: decide about going deeper; some of the Java expressiveness concerns have mitigations, up to even more tradeoffs.]
366
367\subsection{Levels of dependently typed arrays}
368
369The \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:
370\begin{itemize}
371\item a \emph{zip}-style operation that consumes two arrays of equal length
372\item a \emph{map}-style operation whose produced length matches the consumed length
373\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
374\end{itemize}
375Across 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.
376Along the way, the \CFA array also closes the safety gap (with respect to bounds) that Java has over C.
377
378Dependent type systems, considered for the purpose of bound-tracking, can be full-strength or restricted.
379In a full-strength dependent type system, a type can encode an arbitrarily complex predicate, with bound-tracking being an easy example.
380The tradeoff of this expressiveness is complexity in the checker, even typically, a potential for its nontermination.
381In 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.
382[TODO: clarify how even Idris type checking terminates]
383
384Idris is a current, general-purpose dependently typed programming language.
385Length checking is a common benchmark for full dependent type systems.
386Here, 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.
387[TODO: finish explaining what Data.Vect is and then the essence of the comparison]
388
389POINTS:
390here is how our basic checks look (on a system that does not have to compromise);
391it can also do these other cool checks, but watch how I can mess with its conservativeness and termination
392
393Two current, state-of-the-art array languages, Dex\cite{arr:dex:long} and Futhark\cite{arr:futhark:tytheory}, offer offer novel contributions concerning similar, restricted dependent types for tracking array length.
394Unlike \CFA, both are garbage-collected functional languages.
395Because they are garbage-collected, referential integrity is built-in, meaning that the heavyweight analysis, that \CFA aims to avoid, is unnecessary.
396So, like \CFA, the checking in question is a lightweight bounds-only analysis.
397Like \CFA, their checks that are conservatively limited by forbidding arithmetic in the depended-upon expression.
398
399
400
401The 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.
402There is a particular emphasis on an existential type, enabling callee-determined return shapes.
403
404
405Dex uses a novel conception of size, embedding its quantitative information completely into an ordinary type.
406
407Futhark and full-strength dependently typed languages treat array sizes are ordinary values.
408Futhark restricts these expressions syntactically to variables and constants, while a full-strength dependent system does not.
409
410CFA's hybrid presentation, @forall( [N] )@, has @N@ belonging to the type system, yet has no instances.
411Belonging to the type system means it is inferred at a call site and communicated implicitly, like in Dex and unlike in Futhark.
412Having 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.
413
414\subsection{Static safety in C extensions}
415
416
417\section{Future Work}
418
419\subsection{Declaration syntax}
420
421\subsection{Range slicing}
422
423\subsection{With a module system}
424
425\subsection{With described enumerations}
426
427A project in \CFA's current portfolio will improve enumerations.
428In the incumbent state, \CFA has C's enumerations, unmodified.
429I will not discuss the core of this project, which has a tall mission already, to improve type safety, maintain appropriate C compatibility and offer more flexibility about storage use.
430It also has a candidate stretch goal, to adapt \CFA's @forall@ generic system to communicate generalized enumerations:
431\begin{lstlisting}
432forall( T | is_enum(T) )
433void show_in_context( T val ) {
434        for( T i ) {
435                string decorator = "";
436                if ( i == val-1 ) decorator = "< ready";
437                if ( i == val   ) decorator = "< go"   ;
438                sout | i | decorator;
439        }
440}
441enum weekday { mon, tue, wed = 500, thu, fri };
442show_in_context( wed );
443\end{lstlisting}
444with output
445\begin{lstlisting}
446mon
447tue < ready
448wed < go
449thu
450fri
451\end{lstlisting}
452The details in this presentation aren't meant to be taken too precisely as suggestions for how it should look in \CFA.
453But the example shows these abilities:
454\begin{itemize}
455\item a built-in way (the @is_enum@ trait) for a generic routine to require enumeration-like information about its instantiating type
456\item an implicit implementation of the trait whenever a user-written enum occurs (@weekday@'s declaration implies @is_enum@)
457\item a total order over the enumeration constants, with predecessor/successor (@val-1@) available, and valid across gaps in values (@tue == 1 && wed == 500 && tue == wed - 1@)
458\item a provision for looping (the @for@ form used) over the values of the type.
459\end{itemize}
460
461If \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.
462
463[TODO: introduce Ada in the comparators]
464
465In Ada and Dex, an array is conceived as a function whose domain must satisfy only certain structural assumptions, while in C, C++, Java, Futhark and \CFA today, the domain is a prefix of the natural numbers.
466The generality has obvious aesthetic benefits for programmers working on scheduling resources to weekdays, and for programmers who prefer to count from an initial number of their own choosing.
467
468This change of perspective also lets us remove ubiquitous dynamic bound checks.
469[TODO: xref] discusses how automatically inserted bound checks can often be optimized away.
470But this approach is unsatisfying to a programmer who believes she has written code in which dynamic checks are unnecessary, but now seeks confirmation.
471To 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.
472
473[TODO, fix confusion:  Idris has this arrangement of checks, but still the natural numbers as the domain.]
474
475The 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.
476Dex's @Ix@ is analogous the @is_enum@ proposed for \CFA above.
477\begin{lstlisting}
478interface Ix n
479 get_size n : Unit -> Int
480 ordinal : n -> Int
481 unsafe_from_ordinal n : Int -> n
482\end{lstlisting}
483
484Dex uses this foundation of a trait (as an array type's domain) to achieve polymorphism over shapes.
485This 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.
486Dex's example is a routine that calculates pointwise differences between two samples.
487Done 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).
488In 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.
489
490The polymorphism plays out with the pointwise-difference routine advertising a single-dimensional interface whose domain type is generic.
491In the audio instantiation, the duration-of-clip type argument is used for the domain.
492In the photograph instantiation, it's the tuple-type of $\langle \mathrm{img\_wd}, \mathrm{img\_ht} \rangle$.
493This 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
494\begin{lstlisting}
495instance {a b} [Ix a, Ix b] Ix (a & b)
496 get_size = \(). size a * size b
497 ordinal = \(i, j). (ordinal i * size b) + ordinal j
498 unsafe_from_ordinal = \o.
499bs = size b
500(unsafe_from_ordinal a (idiv o bs), unsafe_from_ordinal b (rem o bs))
501\end{lstlisting}
502and 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
503\begin{lstlisting}
504img_trans :: (img_wd,img_ht)=>Real
505img_trans.(i,j) = img.i.j
506result = pairwise img_trans
507\end{lstlisting}
508[TODO: cite as simplification of example from https://openreview.net/pdf?id=rJxd7vsWPS section 4]
509
510In 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.
511
512\subsection{Retire pointer arithmetic}
513
514
515\section{\CFA}
516
517XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
518moved from background chapter \\
519XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
520
521Traditionally, fixing C meant leaving the C-ism alone, while providing a better alternative beside it.
522(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.)
523
524\subsection{\CFA features interacting with arrays}
525
526Prior work on \CFA included making C arrays, as used in C code from the wild,
527work, if this code is fed into @cfacc@.
528The quality of this this treatment was fine, with no more or fewer bugs than is typical.
529
530More mixed results arose with feeding these C'' arrays into preexisting \CFA features.
531
532A notable success was with the \CFA @alloc@ function,
533which type information associated with a polymorphic return type
534replaces @malloc@'s use of programmer-supplied size information.
535\begin{cfa}
536// C, library
537void * malloc( size_t );
538// C, user
539struct tm * el1 = malloc(      sizeof(struct tm) );
540struct tm * ar1 = malloc( 10 * sizeof(struct tm) );
541
542// CFA, library
543forall( T * ) T * alloc();
544// CFA, user
545tm * el2 = alloc();
546tm (*ar2)[10] = alloc();
547\end{cfa}
548The alloc polymorphic return compiles into a hidden parameter, which receives a compiler-generated argument.
549This compiler's argument generation uses type information from the left-hand side of the initialization to obtain the intended type.
550Using a compiler-produced value eliminates an opportunity for user error.
551
552TODO: 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
553
554Bringing 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.
555In the last example, the choice of pointer to array'' @ar2@ breaks a parallel with @ar1@.
556They are not subscripted in the same way.
557\begin{cfa}
558ar1[5];
559(*ar2)[5];
560\end{cfa}
561Using reference to array'' works at resolving this issue.  TODO: discuss connection with Doug-Lea \CC proposal.
562\begin{cfa}
563tm (&ar3)[10] = *alloc();
564ar3[5];
565\end{cfa}
566The implicit size communication to @alloc@ still works in the same ways as for @ar2@.
567
568Using proper array types (@ar2@ and @ar3@) addresses a concern about using raw element pointers (@ar1@), albeit a theoretical one.
569TODO xref C standard does not claim that @ar1@ may be subscripted,
570because no stage of interpreting the construction of @ar1@ has it be that there is an \emph{array object} here.''
571But both @*ar2@ and the referent of @ar3@ are the results of \emph{typed} @alloc@ calls,
572where the type requested is an array, making the result, much more obviously, an array object.
573
574The reference to array'' type has its sore spots too.
575TODO see also @dimexpr-match-c/REFPARAM_CALL@ (under @TRY_BUG_1@)
576
577TODO: I fixed a bug associated with using an array as a T.  I think.  Did I really?  What was the bug?
Note: See TracBrowser for help on using the repository browser.