source: doc/theses/mike_brooks_MMath/array.tex@ d3942b9

Last change on this file since d3942b9 was d3942b9, checked in by Michael Brooks <mlbrooks@…>, 11 months ago

Thesis, array, C typing rules, add discussion dimension hoisting.

  • Property mode set to 100644
File size: 67.5 KB
RevLine 
[bbf6a180]1\chapter{Array}
[c721105]2\label{c:Array}
[bbf6a180]3
[40ab446]4
[5a553e2]5\section{Introduction}
[40ab446]6
[5a553e2]7Arrays in C are possible the single most misunderstood and incorrectly used features in the language, resulting in the largest proportion of runtime errors and security violations.
[caa3e2c]8This chapter describes the new \CFA language and library features that introduce a length-checked array-type to the \CFA standard library~\cite{Cforall}.
9
10Specifically, a new \CFA array is declared:
11\begin{cfa}
12@array( float, 99 )@ x; $\C[2.75in]{// x contains 99 floats}$
13\end{cfa}
14using generic type @array@ with arguments @float@ and @99@.
15A function @f@ is declared with an @array@ parameter of length @42@.
[5a553e2]16\begin{cfa}
17void f( @array( float, 42 )@ & p ) {} $\C{// p accepts 42 floats}$
18f( x ); $\C{// statically rejected: types are different, 99 != 42}$
[40ab446]19
[caa3e2c]20test2.cfa:3:1 error: Invalid application of existing declaration(s) in expression.
21Applying untyped: Name: f ... to: Name: x
22\end{cfa}
23The call @f( x )@ is invalid because the @array@ lengths @99@ and @42@ do not match.
24
25Next, function @g@ introduces a @forall@ prefix on type parameter @T@ and arbitrary \emph{dimension parameter} @N@, the new feature that represents a count of elements managed by the type system.
26\begin{cfa}
27forall( T, @[N]@ )
28void g( array( T, @N@ ) & p, int i ) {
[5a553e2]29 T elem = p[i]; $\C{// dynamically checked: requires 0 <= i < N}$
[266732e]30}
[5a553e2]31g( x, 0 ); $\C{// T is float, N is 99, dynamic subscript check succeeds}$
[caa3e2c]32g( x, 1000 ); $\C{// T is float, N is 99, dynamic subscript check fails}\CRT$
33
34Cforall Runtime error: subscript 1000 exceeds dimension range [0,99) $for$ array 0x555555558020.
[5a553e2]35\end{cfa}
36The call @g( x, 0 )@ is valid because @g@ accepts any length of array, where the type system infers @float@ for @T@ and length @99@ for @N@.
37Inferring values for @T@ and @N@ is implicit without programmer involvement.
38Furthermore, the runtime subscript @x[0]@ (parameter @i@ is @0@) in @g@ is valid because @0@ is in the dimension range $[0,99)$ of argument @x@.
39The call @g( x, 1000 )@ is also valid;
40however, the runtime subscript @x[1000]@ is invalid (generates a subscript error) because @1000@ is outside the dimension range $[0,99)$ of argument @x@.
41
42The generic @array@ type is similar to the C array type, which \CFA inherits from C.
[40ab446]43Its runtime characteristics are often identical, and some features are available in both.
[5a553e2]44For example, assume a caller can instantiates @N@ with 42 in the following (details to follow).
45\begin{cfa}
[266732e]46forall( [N] )
47void declDemo() {
[caa3e2c]48 float x1[N]; $\C{// built-in type ("C array")}$
49 array(float, N) x2; $\C{// type from library}$
[266732e]50}
[5a553e2]51\end{cfa}
52Both of the locally-declared array variables, @x1@ and @x2@, have 42 elements, each element being a @float@.
53The two variables have identical size and layout; they both encapsulate 42-float, stack \vs heap allocations with no additional ``bookkeeping'' allocations or headers.
[caa3e2c]54Providing 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.
[5a553e2]55
56Admittedly, the @array@ library type (type for @x2@) is syntactically different from its C counterpart.
57A future goal (TODO xref) is to provide a built-in array type with syntax approaching C's (type for @x1@);
58then, the library @array@ type can be removed giving \CFA a largely uniform array type.
[caa3e2c]59At present, the C syntax @array@ is only partially supported, so the generic @array@ is used exclusively in the discussion;
[5a553e2]60feature support and C compatibility are revisited in Section ? TODO.
61
[caa3e2c]62Offering 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++@).
[5a553e2]63However, 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.
64Hence, 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 introduced by the C array TODO xref.
65
[caa3e2c]66My contributions in this chapter are:
[5a553e2]67\begin{enumerate}
68\item A type system enhancement that lets polymorphic functions and generic types be parameterized by a numeric value: @forall( [N] )@.
69\item Provide a length-checked array-type in the \CFA standard library, where the array's length is statically managed and dynamically valued.
70\item Provide argument/parameter passing safety for arrays and subscript safety.
[266732e]71\item TODO: general parking...
[5a553e2]72\item Identify the interesting specific abilities available by the new @array@ type.
73\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.
74\end{enumerate}
[40ab446]75
76
77\section{Definitions and design considerations}
78
79
[266732e]80\subsection{Dependent typing}
[40ab446]81
[dab9fb93]82
[5a553e2]83\section{Features added}
[bbf6a180]84
[5a553e2]85This section presents motivating examples for the new array type, demonstrating the syntax and semantics of the generic @array@.
86As stated, the core capability of the new array is tracking all dimensions in the type system, where dynamic dimensions are represented using type variables.
[bbf6a180]87
[5a553e2]88The definition of type variables preceding object declarations makes the array dimension lexically referenceable where it is needed.
[266732e]89For example, a declaration can share one length, @N@, among a pair of parameters and the return.
[b64d0f4]90\lstinput{10-17}{hello-array.cfa}
[dab9fb93]91Here, 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.
[5a553e2]92The dynamic allocation of the @ret@ array by @alloc@ uses the parameterized dimension information in its implicit @_Alignof@ and @sizeof@ determinations, and casting the return type.
93\begin{cfa}
94static inline forall( T & | sized(T) )
95T * alloc( size_t dim ) {
96 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // calloc without zero fill
97 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); // array memalign
98}
99\end{cfa}
100Here, the type system deduces from the left-hand side of the assignment the type @array(bool, N)@, and forwards it as the type variable @T@ for the generic @alloc@ function, making it available in its body.
101Hence, preexisting \CFA behaviour is leveraged here, both in the return-type polymorphism, and the @sized(T)@-aware standard-library @alloc@ routine.
102This example illustrates how the new @array@ type plugs into existing \CFA behaviour by implementing necessary @sized@ assertions needed by other types.
103(@sized@ implies a concrete \vs abstract type with a compile-time size.)
104As 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.
[bbf6a180]105
[dab9fb93]106\begin{figure}
[5a553e2]107\lstinput{30-43}{hello-array.cfa}
108\lstinput{45-48}{hello-array.cfa}
[dab9fb93]109\caption{\lstinline{f} Harness}
110\label{f:fHarness}
111\end{figure}
[bbf6a180]112
[caa3e2c]113\VRef[Figure]{f:fHarness} shows a harness that uses function @f@ to illustrate how dynamic values are fed into the @array@ type.
114Here, the dimension of arrays @x@, @y@, and @result@ is specified from a command-line value, @dim@, and these arrays are allocated on the stack.
[5a553e2]115Then 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.
116The program main is run (see figure bottom) with inputs @5@ and @7@ for sequence lengths.
[caa3e2c]117The loops follow the familiar pattern of using the variable @dim@ to iterate through the arrays.
118Most importantly, the type system implicitly captures @dim@ at the call of @f@ and makes it available throughout @f@ as @N@.
119The 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@.
[5a553e2]120Except 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.
121These benefits cannot be underestimated.
122
123In 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.
124The syntactic form is chosen to parallel other @forall@ forms:
[dab9fb93]125\begin{cfa}
[5a553e2]126forall( @[N]@ ) ... $\C[1.5in]{// array kind}$
127forall( T & ) ... $\C{// reference kind (dtype)}$
128forall( T ) ... $\C{// value kind (otype)}\CRT$
[dab9fb93]129\end{cfa}
[5a553e2]130% The notation @array(thing, N)@ is a single-dimensional case, giving a generic type instance.
[bbf6a180]131In summary:
[dab9fb93]132\begin{itemize}
133\item
[5a553e2]134@[N]@ within a forall declares the type variable @N@ to be a managed length.
[dab9fb93]135\item
[5a553e2]136The type of @N@ within code is @size_t@.
[dab9fb93]137\item
[5a553e2]138The value of @N@ within code is the acquired length derived from the usage site, \ie generic declaration or function call.
[dab9fb93]139\item
[5a553e2]140@array( thing, N0, N1, ... )@ is a multi-dimensional type wrapping $\prod_i N_i$ adjacent occurrences of @thing@ objects.
[dab9fb93]141\end{itemize}
[5a553e2]142
143\VRef[Figure]{f:TemplateVsGenericType} shows @N@ is not the same as a @size_t@ declaration in a \CC \lstinline[language=C++]{template}.
144\begin{enumerate}[leftmargin=*]
145\item
146The \CC template @N@ is a compile-time value, while the \CFA @N@ is a runtime value.
147\item
148The \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@.
149The \CFA @N@ is part of the array type and passed implicitly at the call.
150\item
151\CC cannot have an array of references, but can have an array of pointers.
152\CC has a (mistaken) belief that references are not objects, but pointers are objects.
153In the \CC example, the arrays fall back on C arrays, which have a duality with references with respect to automatic dereferencing.
[caa3e2c]154The \CFA array is a contiguous object with an address, which can be stored as a reference or pointer.
[5a553e2]155\item
156C/\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).
157\end{enumerate}
158
159\begin{figure}
160\begin{tabular}{@{}l@{\hspace{20pt}}l@{}}
[c721105]161\begin{c++}
[5a553e2]162
163@template< typename T, size_t N >@
[caa3e2c]164void copy( T ret[@N@], T x[@N@] ) {
[5a553e2]165 for ( int i = 0; i < N; i += 1 ) ret[i] = x[i];
[266732e]166}
[5a553e2]167int main() {
168 int ret[10], x[10];
169 for ( int i = 0; i < 10; i += 1 ) x[i] = i;
170 @copy<int, 10 >( ret, x );@
171 for ( int i = 0; i < 10; i += 1 )
172 cout << ret[i] << ' ';
173 cout << endl;
174}
175\end{c++}
176&
[266732e]177\begin{cfa}
[5a553e2]178int main() {
179 @forall( T, [N] )@ // nested function
[caa3e2c]180 void copy( array( T, @N@ ) & ret, array( T, @N@ ) & x ) {
[5a553e2]181 for ( i; 10 ) ret[i] = x[i];
[266732e]182 }
[5a553e2]183
184 array( int, 10 ) ret, x;
185 for ( i; 10 ) x[i] = i;
186 @copy( ret, x );@
187 for ( i; 10 )
188 sout | ret[i] | nonl;
189 sout | nl;
[266732e]190}
191\end{cfa}
[5a553e2]192\end{tabular}
193\caption{\CC \lstinline[language=C++]{template} \vs \CFA generic type }
194\label{f:TemplateVsGenericType}
195\end{figure}
196
197Continuing the discussion of \VRef[Figure]{f:fHarness}, the example has @f@ expecting two arrays of the same length.
[caa3e2c]198As stated previous, a compile-time error occurs when attempting to call @f@ with arrays of differing lengths.
[5a553e2]199% removing leading whitespace
200\lstinput[tabsize=1]{52-53}{hello-array.cfa}
201\lstinput[tabsize=1,aboveskip=0pt]{62-64}{hello-array.cfa}
[caa3e2c]202C allows casting to assert knowledge not shared with the type system.
[5a553e2]203\lstinput{70-74}{hello-array.cfa}
204
205Orthogonally, the new @array@ type works with \CFA's generic types, providing argument safety and the associated implicit communication of array length.
206Specifically, \CFA allows aggregate types to be generalized with multiple type parameters, including parameterized element types and lengths.
[4558df2]207Doing so gives a refinement of C's ``flexible array member'' pattern, allowing nesting structures with array members anywhere within the structures.
[5a553e2]208\lstinput{10-15}{hello-accordion.cfa}
[4558df2]209This structure's layout has the starting offset of @studentIds@ varying in generic parameter @C@, and the offset of @preferences@ varying in both generic parameters.
210For a function that operates on a @School@ structure, the type system handles this memory layout transparently.
[5a553e2]211\lstinput{40-45}{hello-accordion.cfa}
[4558df2]212\VRef[Figure]{f:checkHarness} shows the @School@ harness and results with different array sizes, where multidimensional arrays are discussed next.
[5a553e2]213
214\begin{figure}
[4558df2]215% super hack to get this to line up
216\begin{tabular}{@{}ll@{\hspace{25pt}}l@{}}
217\begin{tabular}{@{}p{3.25in}@{}}
218\lstinput{60-66}{hello-accordion.cfa}
219\vspace*{-3pt}
220\lstinput{73-80}{hello-accordion.cfa}
221\end{tabular}
222&
223\raisebox{0.32\totalheight}{%
224\lstinput{85-93}{hello-accordion.cfa}
225}%
226&
227\lstinput{95-109}{hello-accordion.cfa}
228\end{tabular}
229\caption{\lstinline{school} Harness and Output}
[5a553e2]230\label{f:checkHarness}
231\end{figure}
[bbf6a180]232
233
[187be97]234\section{Typing of C Arrays}
235
236Essential in giving a guarantee of accurate length is the compiler's ability
237to reject a program that presumes to mishandle length.
238By contrast, most discussion so far dealt with communicating length,
239from one party who knows it, to another who is willing to work with any given length.
240For scenarios where the concern is a mishandled length,
241the interaction is between two parties who both claim to know (something about) it.
242Such a scenario occurs in this pure C fragment, wich today's C compilers accept:
243\begin{cfa}
244 int n = @42@;
245 float x[n];
246 float (*xp)[@999@] = &x;
247 (*xp)[@500@]; // in "bound"?
248\end{cfa}
249
250Here, the array @x@ has length 42, while a pointer to it (@xp@) claims length 999.
251So, while the subscript of @xp@ at position 500 is out of bound of its referent @x@,
252the access appears in-bound of the type information available on @xp@.
253Truly, length is being mishandled in the previous step,
254where the type-carried length information on @x@ is not compatible with that of @xp@.
255
256The \CFA new-array rejects the analogous case:
257\begin{cfa}
258 int n = @42@;
259 array(float, n) x;
260 array(float, 999) * xp = x; // static rejection here
261 (*xp)[@500@]; // runtime check vs len 999
262\end{cfa}
263
264% TODO: kill the vertical whitespace around these lists
265% nothing from https://stackoverflow.com/questions/1061112/eliminate-space-before-beginitemize is working
266
267The way the \CFA array is implemented,
268the type analysis of this \CFA case reduces to a case similar to the earlier C version.
269The \CFA compiler's compatibility analysis proceeds as:
270\begin{itemize}[noitemsep,partopsep=-\parskip,parsep=0pt,leftmargin=4em]
271\item
272 Is @array(float, 999)@ type-compatible with @array(float, n)@?
273\item
274 Is @arrayX(float, char[999])@ type-compatible with @arrayX(float, char[n])@?
275 \footnote{Here, \lstinline{arrayX} represents the type that results
276 from desugaring the \lstinline{array} type
277 into a type whose generic parameters are all types.
278 This presentation elides the noisy fact that
279 \lstinline{array} is actually a macro for something bigger;
280 the reduction to \lstinline{char[-]} still proceeds as sketched.}
281\item
282 Is @char[999]@ type-compatible with @char[n]@?
283\end{itemize}
284
285I chose to achieve the necessary rejection of the \CFA case
286by adding a rejection of the corresponding C case.
287
288This decision is not backward compatible.
289There are two complementary mitigations for this incompatibility.
290
291First, a simple recourse is available to a programmer who intends to proceed
292with the statically unsound assignment.
293This situation might arise if @n@ were known to be 999,
294rather than 42, as in the introductory examples.
295The programmer can add a cast, as in:
296\begin{cfa}
297 xp = ( float (*)[999] ) & x;
298\end{cfa}
299This addition causes \CFA to accept, beacause now, the programmer has accepted blame.
300This addition is benign in plain C, because the cast is valid, just unnecessary there.
301Moreover, the addition can even be seen as appropriate ``eye candy,''
302marking where the unchecked length knowledge is used.
303Therefore, a program being onboarded to \CFA can receive a simple upgrade,
304to satisfy the \CFA rules (and arguably become clearer),
305without giving up its validity to a plain C compiler.
306
307Second, the incompatibility only affects types like pointer-to-array,
308which are are infrequently used in C.
309The more common C idiom for aliasing an array is to use the pointer-to-first-element type,
310which does not participate in the \CFA array's length checking.
311\footnote{Notably, the desugaring of the \lstinline{array@} type,
312 avoids letting any \lstinline{-[-]} type decay,
313 in order to preserve the length information that powers runtime bound checking.}
314Therefore, the frequency of needing to upgrade wild C code (as discussed in the first mitigation)
315is anticipated to be low.
316
317Because the incompatibility represents a low cost to a \CFA onboarding effort
318(with a plausible side benefit of linting the original code for a missing annotation),
319I elected not to add special measures to retain the compatibility.
320It would be possible to flag occurrences of @-[-]@ types that come from @array@ desugaring,
321treating those with stricter \CFA rules, while treating others with classic C rules.
322If future lessons from C project onboarding warrant it,
323this special compatibility measure can be added.
324
325Having allowed that both the initial C example's check
326\begin{itemize}[noitemsep,partopsep=-\parskip,parsep=0pt,leftmargin=4em]
327 \item
328 Is @float[999]@ type-compatible with @float[n]@?
329\end{itemize}
330and the second \CFA exmple's induced check
331\begin{itemize}[noitemsep,partopsep=-\parskip,parsep=0pt,leftmargin=4em]
332 \item
333 Is @char[999]@ type-compatible with @char[n]@?
334\end{itemize}
335shall have the same answer, (``no''),
336discussion turns to how I got the \CFA compiler to produce this answer.
337In its preexisting form, it produced a (buggy) approximation of the C rules.
338To implement the new \CFA rules, I took the syntactic recursion a step further, obtaining,
339in both cases:
340\begin{itemize}[noitemsep,partopsep=-\parskip,parsep=0pt,leftmargin=4em]
341 \item
342 Is @999@ TBD-compatible with @n@?
343\end{itemize}
344This compatibility question applies to a pair of expressions, where the earlier ones were to types.
345Such an expression-compatibility question is a new addition to the \CFA compiler.
346These questions only arise in the context of dimension expressions on (C) array types.
347
348TODO: ensure these compiler implementation matters are treated under \CFA compiler background:
349type unification,
350cost calculation,
351GenPoly.
352
353The relevant technical component of the \CFA compiler is,
354within the type resolver, the type unification procedure.
355I added rules for continuing this unification into expressions that occur within types.
356It is still fundamentally doing \emph{type} unification
357because it is participating in binding type variables,
358and not participating in binding any variables that stand in for expression fragments
359(for there is no such sort of variable in \CFA's analysis.)
360
361An unfortunate fact about the \CFA compiler's preexisting implementation is that
362type unification suffers from two forms of duplication.
363
364The first duplication has (many of) the unification rules stated twice.
365As a result, my additions for dimension expressions are stated twice.
366The extra statement of the rules occurs in the GenPoly module,
367where concrete types like @array(int, 5)@\footnote{
368 Again, the presentation is simplified
369 by leaving the \lstinline{array} macro unexpanded}
370are lowered into corresponding C types @struct __conc_array_1234@ (the suffix being a generated index).
371In this case, the struct's definition gives fields that hardcode the argument values of @float@ and @5@.
372The next time an @array(-,-)@ concrete instance is encountered,
373is the previous @struct __conc_array_1234@ suitable for it?
374Yes, for another occurrance of @array(int, 5)@;
375no, for either @array(rational(int), 5)@ or @array(int, 42)@.
376By the last example, this phase must ``reject''
377the hypothesis that it should reuse the dimension-5 instance's C-lowering for a dimension-42 instance.
378
379The second duplication has unification (proper) being invoked at two stages of expression resolution.
380As a result, my added rule set needs to handle more cases than the preceding discussion motivates.
381In the program
382\begin{cfa}
383 void f( double );
384 forall( T & ) void f( T & );
385 void g( int n ) {
386 array( float, n + 1 ) x;
387 f(x);
388 }
389\end{cfa}
390when resolving the function call, the first unification stage
391compares the types @T@, of the parameter, with @array( float, n + 1 )@, of the argument.
392TODO: finish.
393
394The actual rules for comparing two dimension expressions are conservative.
395To answer, ``yes, consider this pair of expressions to be matching,''
396is to imply, ``all else being equal, allow an array with length calculated by $e_1$
397to be passed to a function expecting a length-$e_2$ array.''\footnote{
398 TODO: Deal with directionality, that I'm doing exact-match, no ``at least as long as,'' no subtyping.
399 Should it be an earlier scoping principle? Feels like it should matter in more places than here.}
400So, a ``yes'' answer must represent a guarantee that both expressions will evaluate the
401same result, while a ``no'' can tolerate ``they might, but we're not sure,'
402provided that practical recourses are available
403to let programmers express their better knowledge.
404The specific rule-set that I offer with the current release is, in fact, extremely conservative.
405I chose to keep things simple,
406and allow real future needs do drive adding additional complexity,
407within the framework that I laid out.
408
409For starters, the original motivating example's rejection
410is not based on knowledge that
411the @xp@ length of (the literal) 999 is value-unequal to
412the (obvious) runtime value of the variable @n@, which is the @x@ length.
413Rather, the analysis assumes a variable's value can be anything,
414and so there can be no guarantee that its value is 999.
415So, a variable use and a literal can never match.
416
417Two occurrences of the same literal value are obviously a fine match.
418For two occurrences of the same varialbe, more information is needed.
419For example, this one is fine
420\begin{cfa}
421 void f( const int n ) {
422 float x[n];
423 float (*xp)[n] = x; // accept
424 }
425\end{cfa}
426while this one is not:
427\begin{cfa}
428 void f() {
429 int n = 42;
430 float x[n];
431 n = 999;
432 float (*xp)[n] = x; // reject
433 }
434\end{cfa}
435Furthermore, the fact that the first example sees @n@ as @const@
436is not actually a sufficent basis.
437In this example, @f@'s length expression's declaration is as @const@ as it can be,
438yet its value still changes between the two invocations:
439\begin{cfa}
440 // compile unit 1
441 void g();
442 void f( const int & const nr ) {
443 float x[nr];
444 g();
445 float (*xp)[nr] = x; // reject
446 }
447 // compile unit 2
448 static int n = 42;
449 void g() {
450 n = 99;
451 }
452 void f( const int & );
453 int main () {
454 f(n);
455 return 0;
456 }
457\end{cfa}
458The issue in this last case is,
459just because you aren't able to change something doesn't mean someone else can't.
460
461My rule set also respects a feature of the C tradition.
462In spite of the several limitations of the C rules
463accepting cases that produce different values, there are a few mismatches that C stops.
464C is quite precise when working with two static values:
465\begin{cfa}
466 enum { fortytwo = 42 };
467 float x[fortytwo];
468 float (*xp1)[42] = &x; // accept
469 float (*xp2)[999] = &x; // reject
470\end{cfa}
471My \CFA rules agree with C's on these cases.
472
473My rules classify expressions into three groups:
474\begin{description}
475\item[Statically Evaluable]
476 Expressions for which a specific value can be calculated (conservatively)
477 at compile-time.
478 A preexisting \CFA compiler module defines which expressions qualify,
479 and evaluates them.
480 Includes literals and enumeration values.
481\item[Dynamic but Stable]
482 The value of a variable declared as @const@.
483 Includes a @const@ parameter.
484\item[Potentially Unstable]
485 The catch-all category. Notable examples include:
486 any function-call result (@float x[foo()];@),
487 the particular function-call result that is a pointer dereference (@void f(const int * n) { float x[*n]; }@), and
488 any use of a reference-typed variable.
489\end{description}
490
491My \CFA rules are:
492\begin{itemize}
493\item
494 Accept a Statically Evaluable pair, if both expressions have the same value.
495 Notably, this rule allows a literal to match with an enumeration value, based on the value.
496\item
497 Accept a Dynamic but Stable pair, if both expressions are written out the same, e.g. refers to same variable declaration.
498\item
499 Otherwise, reject.
500 Notably, reject all pairs from the Potentially Unstable group.
501 Notably, reject all pairs that cross groups.
502\end{itemize}
503
504TODO: summarize the C rules and add the case-comparison table
505
[d3942b9]506The conservatism of the new rule set can leave a programmer needing a recourse,
507when needing to use a dimension expression whose stability argument
508is more subtle than current-state analysis.
509This recourse is to declare an explicit constant for the dimension value.
510Consider these two dimension expressions,
511whose reuses are rejected by the blunt current-state rules:
512\begin{cfa}
513 void f( int & nr, const int nv ) {
514 float x[nr];
515 float (*xp)[nr] = & x; // reject: nr varying (no references)
516 float y[nv + 1];
517 float (*yp)[nv + 1] = & y; // reject: ?+? unpredicable (no functions)
518 }
519\end{cfa}
520Yet, both dimension expressions are reused safely.
521(The @nr@ reference is never written, not volatile
522and control does not leave the function between the uses.
523The name @?+?@ resolves to a function that is quite predictable.)
524The programmer here can add the constant declarations:
525\begin{cfa}
526 void f( int & nr, const int nv ) {
527 @const int nx@ = nr;
528 float x[nx];
529 float (*xp)[nx] = & x; // acept
530 @const int ny@ = nv + 1;
531 float y[ny];
532 float (*yp)[ny] = & y; // accept
533 }
534\end{cfa}
535The result is the originally intended semantics,
536achieved by adding a superfluous ``snapshot it as of now'' directive.
537
538The snapshotting trick is also used by the translation, though to achieve a different outcome.
539Rather obviously, every array must be subscriptable, even a bizzarre one:
540\begin{cfa}
541 array( float, rand(10) ) x;
542 x[0]; // 10% chance of bound-check failure
543\end{cfa}
544Less obvious is that the mechanism of subscripting is a function call,
545which must communicate length accurately.
546The bound-check above (callee logic) must use the actual allocated length of @x@,
547without mistakenly reevaluating the dimension expression, @rand(10)@.
548Adjusting the example to make the function's use of length more explicit:
549\begin{cfa}
550 forall ( T * )
551 void f( T * x ) { sout | sizeof(*x); }
552 float x[ rand(10) ];
553 f( x );
554\end{cfa}
555Considering that the partly translated function declaration is, loosely,
556\begin{cfa}
557 void f( size_t __sizeof_T, void * x ) { sout | __sizeof_T; }
558\end{cfa}
559the translated call must not go like:
560\begin{cfa}
561 float x[ rand(10) ];
562 f( rand(10), &x );
563\end{cfa}
564Rather, its actual translation is like:
565\begin{cfa}
566 size_t __dim_x = rand(10);
567 float x[ __dim_x ];
568 f( __dim_x, &x );
569\end{cfa}
570The occurrence of this dimension hoisting during translation was present in the preexisting \CFA compiler.
571But its cases were buggy, particularly with determining, ``Can hoisting be skipped here?''
572For skipping this hoisting is clearly desirable in some cases,
573not the least of which is when the programmer has already done so manually.
574My work includes getting these cases right, harmonized with the accept/reject criteria, and tested.
575
576
[187be97]577
[d3942b9]578TODO: Discuss the interaction of this dimension hoisting with the challenge of extra unification for cost calculation
[187be97]579
[5a553e2]580\section{Multidimensional Arrays}
[bbf6a180]581\label{toc:mdimpl}
582
[5a553e2]583% TODO: introduce multidimensional array feature and approaches
584
585When working with arrays, \eg linear algebra, array dimensions are referred to as ``rows'' and ``columns'' for a matrix, adding ``planes'' for a cube.
586(There is little terminology for higher dimensional arrays.)
587For example, an acrostic poem\footnote{A type of poetry where the first, last or other letters in a line spell out a particular word or phrase in a vertical column.}
588can be treated as a grid of characters, where the rows are the text and the columns are the embedded keyword(s).
589Within a poem, there is the concept of a \newterm{slice}, \eg a row is a slice for the poem text, a column is a slice for a keyword.
590In general, the dimensioning and subscripting for multidimensional arrays has two syntactic forms: @m[r,c]@ or @m[r][c]@.
591
592Commonly, an array, matrix, or cube, is visualized (especially in mathematics) as a contiguous row, rectangle, or block.
593This conceptualization is reenforced by subscript ordering, \eg $m_{r,c}$ for a matrix and $c_{p,r,c}$ for a cube.
594Few programming languages differ from the mathematical subscript ordering.
595However, computer memory is flat, and hence, array forms are structured in memory as appropriate for the runtime system.
596The closest representation to the conceptual visualization is for an array object to be contiguous, and the language structures this memory using pointer arithmetic to access the values using various subscripts.
597This approach still has degrees of layout freedom, such as row or column major order, \ie juxtaposed rows or columns in memory, even when the subscript order remains fixed.
598For example, programming languages like MATLAB, Fortran, Julia and R store matrices in column-major order since they are commonly used for processing column-vectors in tabular data sets but retain row-major subscripting.
599In general, storage layout is hidden by subscripting, and only appears when passing arrays among different programming languages or accessing specific hardware.
600
[caa3e2c]601\VRef[Figure]{f:FixedVariable} shows two C90 approaches for manipulating a contiguous matrix.
[5a553e2]602Note, C90 does not support VLAs.
[caa3e2c]603The fixed-dimension approach (left) uses the type system;
[5a553e2]604however, it requires all dimensions except the first to be specified at compile time, \eg @m[][6]@, allowing all subscripting stride calculations to be generated with constants.
605Hence, every matrix passed to @fp1@ must have exactly 6 columns but the row size can vary.
[caa3e2c]606The variable-dimension approach (right) ignores (violates) the type system, \ie argument and parameters types do not match, and subscripting is performed manually using pointer arithmetic in the macro @sub@.
[bbf6a180]607
[5a553e2]608\begin{figure}
609\begin{tabular}{@{}l@{\hspace{40pt}}l@{}}
610\multicolumn{1}{c}{\textbf{Fixed Dimension}} & \multicolumn{1}{c}{\textbf{Variable Dimension}} \\
611\begin{cfa}
[bbf6a180]612
[5a553e2]613void fp1( int rows, int m[][@6@] ) {
614 ... printf( "%d ", @m[r][c]@ ); ...
615}
616int fm1[4][@6@], fm2[6][@6@]; // no VLA
617// initialize matrixes
618fp1( 4, fm1 ); // implicit 6 columns
619fp1( 6, fm2 );
620\end{cfa}
621&
622\begin{cfa}
623#define sub( m, r, c ) *(m + r * sizeof( m[0] ) + c)
624void fp2( int rows, int cols, int *m ) {
625 ... printf( "%d ", @sub( m, r, c )@ ); ...
626}
[caa3e2c]627int vm1[@4@][@4@], vm2[@6@][@8@]; // no VLA
[5a553e2]628// initialize matrixes
629fp2( 4, 4, vm1 );
630fp2( 6, 8, vm2 );
631\end{cfa}
632\end{tabular}
633\caption{C90 Fixed \vs Variable Contiguous Matrix Styles}
634\label{f:FixedVariable}
635\end{figure}
[bbf6a180]636
[5a553e2]637Many languages allow multidimensional arrays-of-arrays, \eg in Pascal or \CC.
638\begin{cquote}
639\begin{tabular}{@{}ll@{}}
640\begin{pascal}
641var m : array[0..4, 0..4] of Integer; (* matrix *)
642type AT = array[0..4] of Integer; (* array type *)
643type MT = array[0..4] of AT; (* array of array type *)
644var aa : MT; (* array of array variable *)
645m@[1][2]@ := 1; aa@[1][2]@ := 1 (* same subscripting *)
646\end{pascal}
647&
648\begin{c++}
649int m[5][5];
[bbf6a180]650
[5a553e2]651typedef vector< vector<int> > MT;
652MT vm( 5, vector<int>( 5 ) );
653m@[1][2]@ = 1; aa@[1][2]@ = 1;
654\end{c++}
655\end{tabular}
656\end{cquote}
657The language decides if the matrix and array-of-array are laid out the same or differently.
658For example, an array-of-array may be an array of row pointers to arrays of columns, so the rows may not be contiguous in memory nor even the same length (triangular matrix).
[caa3e2c]659Regardless, there is usually a uniform subscripting syntax masking the memory layout, even though a language could differentiated between the two forms using subscript syntax, \eg @m[1,2]@ \vs @aa[1][2]@.
[5a553e2]660Nevertheless, controlling memory layout can make a difference in what operations are allowed and in performance (caching/NUMA effects).
[bbf6a180]661
[5a553e2]662C also provides non-contiguous arrays-of-arrays.
663\begin{cfa}
664int m[5][5]; $\C{// contiguous}$
665int * aa[5]; $\C{// non-contiguous}$
666\end{cfa}
667both with different memory layout using the same subscripting, and both with different degrees of issues.
668The focus of this work is on the contiguous multidimensional arrays in C.
669The reason is that programmers are often forced to use the more complex array-of-array form when a contiguous array would be simpler, faster, and safer.
[caa3e2c]670Nevertheless, the C array-of-array form is still important for special circumstances.
[5a553e2]671
672\VRef[Figure]{f:ContiguousNon-contiguous} shows the extensions made in C99 for manipulating contiguous \vs non-contiguous arrays.\footnote{C90 also supported non-contiguous arrays.}
673First, VLAs are supported.
674Second, for contiguous arrays, C99 conjoins one or more of the parameters as a downstream dimension(s), \eg @cols@, implicitly using this parameter to compute the row stride of @m@.
675If the declaration of @fc@ is changed to:
676\begin{cfa}
677void fc( int rows, int cols, int m[@rows@][@cols@] ) ...
678\end{cfa}
679it is possible for C to perform bound checking across all subscripting, but it does not.
680While this contiguous-array capability is a step forward, it is still the programmer's responsibility to manually manage the number of dimensions and their sizes, both at the function definition and call sites.
681That is, the array does not automatically carry its structure and sizes for use in computing subscripts.
[caa3e2c]682While the non-contiguous style in @faa@ looks very similar to @fc@, the compiler only understands the unknown-sized array of row pointers, and it relies on the programmer to traverse the columns in a row correctly with a correctly bounded loop index.
[5a553e2]683Specifically, there is no requirement that the rows are the same length, like a poem with different length lines.
[bbf6a180]684
[5a553e2]685\begin{figure}
686\begin{tabular}{@{}ll@{}}
687\multicolumn{1}{c}{\textbf{Contiguous}} & \multicolumn{1}{c}{\textbf{ Non-contiguous}} \\
688\begin{cfa}
689void fc( int rows, @int cols@, int m[ /* rows */ ][@cols@] ) {
690 ... printf( "%d ", @m[r][c]@ ); ...
691}
692int m@[5][5]@;
693for ( int r = 0; r < 5; r += 1 ) {
[bbf6a180]694
[5a553e2]695 for ( int c = 0; c < 5; c += 1 )
696 m[r][c] = r + c;
697}
698fc( 5, 5, m );
699\end{cfa}
700&
701\begin{cfa}
702void faa( int rows, int cols, int * m[ @/* cols */@ ] ) {
703 ... printf( "%d ", @m[r][c]@ ); ...
704}
705int @* aa[5]@; // row pointers
706for ( int r = 0; r < 5; r += 1 ) {
707 @aa[r] = malloc( 5 * sizeof(int) );@ // create rows
708 for ( int c = 0; c < 5; c += 1 )
709 aa[r][c] = r + c;
710}
711faa( 5, 5, aa );
712\end{cfa}
[bbf6a180]713\end{tabular}
[5a553e2]714\caption{C99 Contiguous \vs Non-contiguous Matrix Styles}
715\label{f:ContiguousNon-contiguous}
716\end{figure}
[bbf6a180]717
718
[5a553e2]719\subsection{Multidimensional array implementation}
[bbf6a180]720
[5a553e2]721A multidimensional array implementation has three relevant levels of abstraction, from highest to lowest, where the array occupies \emph{contiguous memory}.
722\begin{enumerate}
723\item
724Flexible-stride memory:
725this model has complete independence between subscripting ordering and memory layout, offering the ability to slice by (provide an index for) any dimension, \eg slice a plane, row, or column, \eg @c[3][*][*]@, @c[3][4][*]@, @c[3][*][5]@.
726\item
727Fixed-stride memory:
728this 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).
729After which, subscripting and memory layout are independent.
730\item
731Explicit-displacement memory:
732this 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@.
733A programmer must manually build any notion of dimensions using other tools;
[caa3e2c]734hence, this style is not offering multidimensional arrays \see{\VRef[Figure]{f:FixedVariable} right example}.
[5a553e2]735\end{enumerate}
736
737There is some debate as to whether the abstraction ordering goes $\{1, 2\} < 3$, rather than my numerically-ordering.
738That is, styles 1 and 2 are at the same abstraction level, with 3 offering a limited set of functionality.
739I chose to build the \CFA style-1 array upon a style-2 abstraction.
740(Justification of the decision follows, after the description of the design.)
[bbf6a180]741
[5a553e2]742Style 3 is the inevitable target of any array implementation.
743The hardware offers this model to the C compiler, with bytes as the unit of displacement.
744C offers this model to its programmer as pointer arithmetic, with arbitrary sizes as the unit.
745Casting 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.
[bbf6a180]746
[5a553e2]747Now stepping 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 even the interface is quite low-level.
748A C/\CFA array interface includes the resulting memory layout.
749The defining requirement of a type-2 system is the ability to slice a column from a column-finest matrix.
[caa3e2c]750The required memory shape of such a slice is fixed, before any discussion of implementation.
[5a553e2]751The 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.
752TODO: do I have/need a presentation of just this layout, just the semantics of -[all]?
753
754The new \CFA standard library @array@ datatype supports richer multidimensional features than C.
755The 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.
756Beyond what C's array 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.
757
[caa3e2c]758The 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.
759\par
[5a553e2]760\mbox{\lstinput{121-126}{hello-md.cfa}}
761\par\noindent
762The memory layout is 35 contiguous elements with strictly increasing addresses.
[bbf6a180]763
[5a553e2]764A trivial form of slicing extracts a contiguous inner array, within an array-of-arrays.
765As 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.
766This 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@.
767The following is an example slicing a row.
768\lstinput{60-64}{hello-md.cfa}
769\lstinput[aboveskip=0pt]{140-140}{hello-md.cfa}
770
771However, function @print1d@ is asserting too much knowledge about its parameter @r@ for printing either a row slice or a column slice.
772Specifically, declaring the parameter @r@ with type @array@ means that @r@ is contiguous, which is unnecessarily restrictive.
773That is, @r@ need only be of a container type that offers a subscript operator (of type @ptrdiff_t@ $\rightarrow$ @float@) with managed length @N@.
[caa3e2c]774The new-array library provides the trait @ar@, so-defined.
[5a553e2]775With it, the original declaration can be generalized with the same body.
776\lstinput{43-44}{hello-md.cfa}
777\lstinput[aboveskip=0pt]{145-145}{hello-md.cfa}
778The 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.
[caa3e2c]779In a multi-dimensional subscript operation, any dimension given as @all@ is a placeholder, \ie ``not yet subscripted by a value'', waiting for such a value, implementing the @ar@ trait.
[5a553e2]780\lstinput{150-151}{hello-md.cfa}
781
782The example shows @x[2]@ and @x[[2, all]]@ both refer to the same, ``2.*'' slice.
783Indeed, 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]@.
784This 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''.
785That is:
786\begin{cquote}
787\begin{tabular}{@{}cccccl@{}}
788@x[[2,all]][3]@ & $\equiv$ & @x[2][all][3]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is redundant) \\
789@x[[all,3]][2]@ & $\equiv$ & @x[all][3][2]@ & $\equiv$ & @x[2][3]@ & (here, @all@ is effective)
790\end{tabular}
791\end{cquote}
792
793Narrating progress through each of the @-[-][-][-]@\footnote{
794The first ``\lstinline{-}'' is a variable expression and the remaining ``\lstinline{-}'' are subscript expressions.}
795expressions gives, firstly, a definition of @-[all]@, and secondly, a generalization of C's @-[i]@.
796Where @all@ is redundant:
797\begin{cquote}
798\begin{tabular}{@{}ll@{}}
799@x@ & 2-dimensional, want subscripts for coarse then fine \\
800@x[2]@ & 1-dimensional, want subscript for fine; lock coarse == 2 \\
801@x[2][all]@ & 1-dimensional, want subscript for fine \\
802@x[2][all][3]@ & 0-dimensional; lock fine == 3
803\end{tabular}
804\end{cquote}
805Where @all@ is effective:
806\begin{cquote}
807\begin{tabular}{@{}ll@{}}
808@x@ & 2-dimensional, want subscripts for coarse then fine \\
809@x[all]@ & 2-dimensional, want subscripts for fine then coarse \\
810@x[all][3]@ & 1-dimensional, want subscript for coarse; lock fine == 3 \\
811@x[all][3][2]@ & 0-dimensional; lock coarse == 2
812\end{tabular}
813\end{cquote}
[266732e]814The semantics of @-[all]@ is to dequeue from the front of the ``want subscripts'' list and re-enqueue at its back.
[5a553e2]815For example, in a two dimensional matrix, this semantics conceptually transposes the matrix by reversing the subscripts.
[266732e]816The semantics of @-[i]@ is to dequeue from the front of the ``want subscripts'' list and lock its value to be @i@.
[bbf6a180]817
[5a553e2]818Contiguous arrays, and slices of them, are all represented by the same underlying parameterized type, which includes stride information in its metatdata.
819\PAB{Do not understand this sentence: The \lstinline{-[all]} operation is a conversion from a reference to one instantiation to a reference to another instantiation.}
[266732e]820The running example's @all@-effective step, stated more concretely, is:
[5a553e2]821\begin{cquote}
822\begin{tabular}{@{}ll@{}}
823@x@ & : 5 of ( 7 of @float@ each spaced 1 @float@ apart ) each spaced 7 @floats@ apart \\
824@x[all]@ & : 7 of ( 5 of @float@ each spaced 7 @float@s apart ) each spaced 1 @float@ apart
[bbf6a180]825\end{tabular}
[5a553e2]826\end{cquote}
[bbf6a180]827
828\begin{figure}
[266732e]829\includegraphics{measuring-like-layout}
[5a553e2]830\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.
831The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual.
832The vertical shaded band highlights the location of the targeted element, 2.3.
833Any such vertical slice contains various interpretations of a single address.}
[266732e]834\label{fig:subscr-all}
[bbf6a180]835\end{figure}
836
[77328d0]837Figure~\ref{fig:subscr-all} shows one element (in the shaded band) accessed two different ways: as @x[2][3]@ and as @x[all][3][2]@.
838In both cases, value 2 selects from the coarser dimension (rows of @x@),
839while the value 3 selects from the finer dimension (columns of @x@).
[caa3e2c]840The figure illustrates the value of each subexpression, comparing how numeric subscripting proceeds from @x@, \vs from @x[all]@.
[77328d0]841Proceeding from @x@ gives the numeric indices as coarse then fine, while proceeding from @x[all]@ gives them fine then coarse.
842These two starting expressions, which are the example's only multidimensional subexpressions
843(those that received zero numeric indices so far), are illustrated with vertical steps where a \emph{first} numeric index would select.
844
[caa3e2c]845The figure's presentation offers an intuition answering to: What is an atomic element of @x[all]@?
846From there, @x[all]@ itself is simply a two-dimensional array, in the strict C sense, of these building blocks.
[77328d0]847An atom (like the bottommost value, @x[all][3][2]@), is the contained value (in the square box)
[4558df2]848and a lie about its size (the left diagonal above it, growing upward).
[caa3e2c]849An array of these atoms (like the intermediate @x[all][3]@) is just a contiguous arrangement of them, done according to their size;
850call such an array a column.
851A column is almost ready to be arranged into a matrix;
852it is the \emph{contained value} of the next-level building block, but another lie about size is required.
[4558df2]853At first, an atom needs to be arranged as if it were bigger, but now a column needs to be arranged as if it is smaller (the left diagonal above it, shrinking upward).
[77328d0]854These lying columns, arranged contiguously according to their size (as announced) form the matrix @x[all]@.
[caa3e2c]855Because @x[all]@ takes indices, first for the fine stride, then for the coarse stride, it achieves the requirement of representing the transpose of @x@.
856Yet every time the programmer presents an index, a C-array subscript is achieving the offset calculation.
[77328d0]857
858In the @x[all]@ case, after the finely strided subscript is done (column 3 is selected),
859the locations referenced by the coarse subscript options (rows 0..4) are offset by 3 floats,
860compared with where analogous rows appear when the row-level option is presented for @x@.
861
[4558df2]862For example, in \lstinline{x[all]}, the shaded band touches atoms 2.0, 2.1, 2.2, 2.3, 1.4, 1.5 and 1.6 (left diagonal).
[77328d0]863But only the atom 2.3 is storing its value there.
[4558df2]864The rest are lying about (conflicting) claims on this location, but never exercising these alleged claims.
[77328d0]865
866Lying is implemented as casting.
867The arrangement just described is implemented in the structure @arpk@.
868This structure uses one type in its internal field declaration and offers a different type as the return of its subscript operator.
869The 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]@.
[4558df2]870The subscript operator presents what is really inside, by casting to the type below the left diagonal of the lie.
[77328d0]871
[5a553e2]872% 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.
[77328d0]873
[caa3e2c]874Casting, overlapping, and lying are unsafe.
875The mission is to implement a style-1 feature in the type system for safe use by a programmer.
876The offered style-1 system is allowed to be internally unsafe,
877just 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.
878Having a style-1 system relieves the programmer from resorting to unsafe pointer arithmetic when working with noncontiguous slices.
[77328d0]879
[caa3e2c]880% PAB: repeat from previous paragraph.
881% 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.
882% My casting is unsafe, but I do not do any pointer arithmetic.
883% When a programmer works in the common-case style-2 subset (in the no-@[all]@ top of Figure~\ref{fig:subscr-all}), my casts are identities, and the C compiler is doing its usual displacement calculations.
884% 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.
[77328d0]885
[caa3e2c]886% \noindent END: Paste looking for a home
[bbf6a180]887
[266732e]888The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping.
[caa3e2c]889The @arpk@ structure and its @-[i]@ operator are defined as:
[5a553e2]890\begin{cfa}
[caa3e2c]891forall(
892 [N], $\C{// length of current dimension}$
893 S & | sized(S), $\C{// masquerading-as}$
894 Timmed &, $\C{// immediate element, often another array}$
895 Tbase & $\C{// base element, e.g. float, never array}$
[5a553e2]896) { // distribute forall to each element
897 struct arpk {
898 S strides[N]; $\C{// so that sizeof(this) is N of S}$
899 };
[caa3e2c]900 // expose Timmed, stride by S
901 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, long int i ) {
902 subcheck( a, i, 0, N );
903 return (Timmed &)a.strides[i];
[5a553e2]904 }
[bbf6a180]905}
[5a553e2]906\end{cfa}
[caa3e2c]907The private @arpk@ structure (array with explicit packing) is generic over four types: dimension length, masquerading-as, ...
908This structure's public interface is hidden behind the @array(...)@ macro and the subscript operator.
909Construction by @array@ initializes the masquerading-as type information to be equal to the contained-element information.
910Subscripting by @all@ rearranges the order of masquerading-as types to achieve, in general, nontrivial striding.
911Subscripting 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.
[bbf6a180]912
[266732e]913An 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, ...)@.
914In the base case, @array(E_base)@ is just @E_base@.
915Because this construction uses the same value for the generic parameters @S@ and @E_im@, the resulting layout has trivial strides.
[bbf6a180]916
[266732e]917Subscripting 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.
918Expressed as an operation on types, this rotation is:
[bbf6a180]919\begin{eqnarray*}
920suball( arpk(N, S, E_i, E_b) ) & = & enq( N, S, E_i, E_b ) \\
921enq( N, S, E_b, E_b ) & = & arpk( N, S, E_b, E_b ) \\
922enq( N, S, arpk(N', S', E_i', E_b), E_b ) & = & arpk( N', S', enq(N, S, E_i', E_b), E_b )
923\end{eqnarray*}
[8d76f2b]924
925
926\section{Bound checks, added and removed}
927
[266732e]928\CFA array subscripting is protected with runtime bound checks.
929Having dependent typing causes the optimizer to remove more of these bound checks than it would without them.
930This section provides a demonstration of the effect.
[8d76f2b]931
[caa3e2c]932The experiment compares the \CFA array system with the padded-room system [TODO:xref] most typically exemplified by Java arrays, but also reflected in the \CC pattern where restricted vector usage models a checked array.
[266732e]933The 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.
[caa3e2c]934The experiment compares with the \CC version to keep access to generated assembly code simple.
[8d76f2b]935
[caa3e2c]936As a control case, a simple loop (with no reused dimension sizes) is seen to get the same optimization treatment in both the \CFA and \CC versions.
[266732e]937When 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.
938But 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.
[8d76f2b]939
[dab9fb93]940TODO: paste source and assembly codes
[8d76f2b]941
[266732e]942Incorporating reuse among dimension sizes is seen to give \CFA an advantage at being optimized.
943The case is naive matrix multiplication over a row-major encoding.
[8d76f2b]944
945TODO: paste source codes
946
947
948
949
950
951\section{Comparison with other arrays}
952
[caa3e2c]953
954\subsection{Rust}
955
[266732e]956\CFA's array is the first lightweight application of dependently-typed bound tracking to an extension of C.
[caa3e2c]957Other 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.
[266732e]958These systems, therefore, ask the programmer to convince the type checker that every pointer dereference is valid.
959\CFA imposes the lighter-weight obligation, with the more limited guarantee, that initially-declared bounds are respected thereafter.
[8d76f2b]960
[266732e]961\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.
962Other 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.
963The \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.
[8d76f2b]964
965
[caa3e2c]966\subsection{Java}
[8d76f2b]967
[caa3e2c]968Java arrays are arrays-of-arrays because all objects are references \see{\VRef{toc:mdimpl}}.
969For each array, Java implicitly storages the array dimension in a descriptor, supporting array length, subscript checking, and allowing dynamically-sized array-parameter declarations.
970\begin{cquote}
[8d76f2b]971\begin{tabular}{rl}
[5a553e2]972C & @void f( size_t n, size_t m, float x[n][m] );@ \\
[caa3e2c]973Java & @void f( float x[][] );@
[8d76f2b]974\end{tabular}
[caa3e2c]975\end{cquote}
976However, in the C prototype, the parameters @n@ and @m@ are documentation only as the intended size of the first and second dimension of @x@.
977\VRef[Figure]{f:JavaVsCTriangularMatrix} compares a triangular matrix (array-of-arrays) in dynamically safe Java to unsafe C.
978Each dynamically sized row in Java stores its dimension, while C requires the programmer to manage these sizes explicitly (@rlnth@).
979All subscripting is Java has bounds checking, while C has none.
980Both Java and C require explicit null checking, otherwise there is a runtime failure.
[8d76f2b]981
[caa3e2c]982\begin{figure}
983\setlength{\tabcolsep}{15pt}
984\begin{tabular}{ll@{}}
985\begin{java}
986int m[][] = { // triangular matrix
987 new int [4],
988 new int [3],
989 new int [2],
990 new int [1],
991 null
992};
993
994for ( int r = 0; r < m.length; r += 1 ) {
995 if ( m[r] == null ) continue;
996 for ( int c = 0; c < m[r].length; c += 1 ) {
997 m[r][c] = c + r; // subscript checking
998 }
[8d76f2b]999
[caa3e2c]1000}
1001
1002for ( int r = 0; r < m.length; r += 1 ) {
1003 if ( m[r] == null ) {
1004 System.out.println( "null row" );
1005 continue;
1006 }
1007 for ( int c = 0; c < m[r].length; c += 1 ) {
1008 System.out.print( m[r][c] + " " );
1009 }
1010 System.out.println();
1011
1012}
1013\end{java}
1014&
1015\begin{cfa}
1016int * m[5] = { // triangular matrix
1017 calloc( 4, sizeof(int) ),
1018 calloc( 3, sizeof(int) ),
1019 calloc( 2, sizeof(int) ),
1020 calloc( 1, sizeof(int) ),
1021 NULL
1022};
1023int rlnth = 4;
1024for ( int r = 0; r < 5; r += 1 ) {
1025 if ( m[r] == NULL ) continue;
1026 for ( int c = 0; c < rlnth; c += 1 ) {
1027 m[r][c] = c + r; // no subscript checking
1028 }
1029 rlnth -= 1;
1030}
1031rlnth = 4;
1032for ( int r = 0; r < 5; r += 1 ) {
1033 if ( m[r] == NULL ) {
1034 printf( "null row\n" );
1035 continue;
1036 }
1037 for ( int c = 0; c < rlnth; c += 1 ) {
1038 printf( "%d ", m[r][c] );
1039 }
1040 printf( "\n" );
1041 rlnth -= 1;
1042}
1043\end{cfa}
1044\end{tabular}
1045\caption{Java (left) \vs C (right) Triangular Matrix}
1046\label{f:JavaVsCTriangularMatrix}
1047\end{figure}
1048
1049The downside of the arrays-of-arrays approach is performance due to pointer chasing versus pointer arithmetic for a contiguous arrays.
1050Furthermore, there is the cost of managing the implicit array descriptor.
1051It is unlikely that a JIT can dynamically rewrite an arrays-of-arrays form into a contiguous form.
1052
1053
1054\subsection{\CC}
1055
1056Because C arrays are difficult and dangerous, the mantra for \CC programmers is to use @std::vector@ in place of the C array.
1057While the vector size can grow and shrink dynamically, \vs a fixed-size dynamic size with VLAs, the cost of this extra feature is mitigated by preallocating the maximum size (like the VLA) at the declaration (one dynamic call) to avoid using @push_back@.
1058\begin{c++}
1059vector< vector< int > > m( 5, vector<int>(8) ); // initialize size of 5 x 8 with 6 dynamic allocations
1060\end{c++}
1061Multidimensional arrays are arrays-of-arrays with associated costs.
1062Each @vector@ array has an array descriptor contain the dimension, which allows bound checked using @x.at(i)@ in place of @x[i]@.
1063Used 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 invalidate references to contained values.
1064This scheme matches Java's safety and expressiveness exactly, but with the inherent costs.
[8d76f2b]1065
1066
1067\subsection{Levels of dependently typed arrays}
1068
1069The \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:
1070\begin{itemize}
[266732e]1071\item a \emph{zip}-style operation that consumes two arrays of equal length
1072\item a \emph{map}-style operation whose produced length matches the consumed length
1073\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
[8d76f2b]1074\end{itemize}
[266732e]1075Across 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.
1076Along the way, the \CFA array also closes the safety gap (with respect to bounds) that Java has over C.
[8d76f2b]1077
[266732e]1078Dependent type systems, considered for the purpose of bound-tracking, can be full-strength or restricted.
1079In a full-strength dependent type system, a type can encode an arbitrarily complex predicate, with bound-tracking being an easy example.
1080The tradeoff of this expressiveness is complexity in the checker, even typically, a potential for its nontermination.
1081In 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.
1082[TODO: clarify how even Idris type checking terminates]
[8d76f2b]1083
[266732e]1084Idris is a current, general-purpose dependently typed programming language.
1085Length checking is a common benchmark for full dependent type systems.
1086Here, 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.
1087[TODO: finish explaining what Data.Vect is and then the essence of the comparison]
[8d76f2b]1088
1089POINTS:
[dab9fb93]1090here is how our basic checks look (on a system that does not have to compromise);
[8d76f2b]1091it can also do these other cool checks, but watch how I can mess with its conservativeness and termination
1092
[caa3e2c]1093Two current, state-of-the-art array languages, Dex\cite{arr:dex:long} and Futhark\cite{arr:futhark:tytheory}, offer novel contributions concerning similar, restricted dependent types for tracking array length.
[266732e]1094Unlike \CFA, both are garbage-collected functional languages.
1095Because they are garbage-collected, referential integrity is built-in, meaning that the heavyweight analysis, that \CFA aims to avoid, is unnecessary.
1096So, like \CFA, the checking in question is a lightweight bounds-only analysis.
1097Like \CFA, their checks that are conservatively limited by forbidding arithmetic in the depended-upon expression.
[8d76f2b]1098
1099
1100
[266732e]1101The 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.
1102There is a particular emphasis on an existential type, enabling callee-determined return shapes.
1103
[8d76f2b]1104
1105Dex uses a novel conception of size, embedding its quantitative information completely into an ordinary type.
1106
[266732e]1107Futhark and full-strength dependently typed languages treat array sizes are ordinary values.
1108Futhark restricts these expressions syntactically to variables and constants, while a full-strength dependent system does not.
[8d76f2b]1109
[5a553e2]1110\CFA's hybrid presentation, @forall( [N] )@, has @N@ belonging to the type system, yet has no instances.
[266732e]1111Belonging to the type system means it is inferred at a call site and communicated implicitly, like in Dex and unlike in Futhark.
1112Having 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.
[8d76f2b]1113
1114\subsection{Static safety in C extensions}
1115
1116
[5a553e2]1117\section{Future work}
[8d76f2b]1118
1119\subsection{Declaration syntax}
1120
1121\subsection{Range slicing}
1122
1123\subsection{With a module system}
1124
1125\subsection{With described enumerations}
1126
[266732e]1127A project in \CFA's current portfolio will improve enumerations.
1128In the incumbent state, \CFA has C's enumerations, unmodified.
1129I 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.
1130It also has a candidate stretch goal, to adapt \CFA's @forall@ generic system to communicate generalized enumerations:
[5a553e2]1131\begin{cfa}
[266732e]1132forall( T | is_enum(T) )
1133void show_in_context( T val ) {
1134 for( T i ) {
1135 string decorator = "";
1136 if ( i == val-1 ) decorator = "< ready";
1137 if ( i == val ) decorator = "< go" ;
1138 sout | i | decorator;
1139 }
1140}
1141enum weekday { mon, tue, wed = 500, thu, fri };
1142show_in_context( wed );
[5a553e2]1143\end{cfa}
[8d76f2b]1144with output
[5a553e2]1145\begin{cfa}
[266732e]1146mon
1147tue < ready
1148wed < go
1149thu
1150fri
[5a553e2]1151\end{cfa}
[266732e]1152The details in this presentation aren't meant to be taken too precisely as suggestions for how it should look in \CFA.
1153But the example shows these abilities:
[8d76f2b]1154\begin{itemize}
[266732e]1155\item a built-in way (the @is_enum@ trait) for a generic routine to require enumeration-like information about its instantiating type
1156\item an implicit implementation of the trait whenever a user-written enum occurs (@weekday@'s declaration implies @is_enum@)
1157\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@)
1158\item a provision for looping (the @for@ form used) over the values of the type.
[8d76f2b]1159\end{itemize}
1160
1161If \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.
1162
[dab9fb93]1163[TODO: introduce Ada in the comparators]
[8d76f2b]1164
[caa3e2c]1165In 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.
[266732e]1166The 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.
[8d76f2b]1167
[266732e]1168This change of perspective also lets us remove ubiquitous dynamic bound checks.
1169[TODO: xref] discusses how automatically inserted bound checks can often be optimized away.
1170But this approach is unsatisfying to a programmer who believes she has written code in which dynamic checks are unnecessary, but now seeks confirmation.
1171To 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.
[8d76f2b]1172
1173[TODO, fix confusion: Idris has this arrangement of checks, but still the natural numbers as the domain.]
1174
[266732e]1175The 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.
1176Dex's @Ix@ is analogous the @is_enum@ proposed for \CFA above.
[5a553e2]1177\begin{cfa}
[8d76f2b]1178interface Ix n
[5a553e2]1179get_size n : Unit -> Int
1180ordinal : n -> Int
1181unsafe_from_ordinal n : Int -> n
1182\end{cfa}
[8d76f2b]1183
[266732e]1184Dex uses this foundation of a trait (as an array type's domain) to achieve polymorphism over shapes.
1185This 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.
1186Dex's example is a routine that calculates pointwise differences between two samples.
1187Done 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).
1188In 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.
[8d76f2b]1189
[266732e]1190The polymorphism plays out with the pointwise-difference routine advertising a single-dimensional interface whose domain type is generic.
1191In the audio instantiation, the duration-of-clip type argument is used for the domain.
1192In the photograph instantiation, it's the tuple-type of $ \langle \mathrm{img\_wd}, \mathrm{img\_ht} \rangle $.
1193This 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
[5a553e2]1194\begin{cfa}
[8d76f2b]1195instance {a b} [Ix a, Ix b] Ix (a & b)
[5a553e2]1196get_size = \(). size a * size b
1197ordinal = \(i, j). (ordinal i * size b) + ordinal j
1198unsafe_from_ordinal = \o.
[266732e]1199bs = size b
1200(unsafe_from_ordinal a (idiv o bs), unsafe_from_ordinal b (rem o bs))
[5a553e2]1201\end{cfa}
[8d76f2b]1202and 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
[5a553e2]1203\begin{cfa}
[266732e]1204img_trans :: (img_wd,img_ht)=>Real
1205img_trans.(i,j) = img.i.j
1206result = pairwise img_trans
[5a553e2]1207\end{cfa}
[8d76f2b]1208[TODO: cite as simplification of example from https://openreview.net/pdf?id=rJxd7vsWPS section 4]
1209
1210In 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.
1211
1212\subsection{Retire pointer arithmetic}
[82e5670]1213
1214
1215\section{\CFA}
1216
1217XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
1218moved from background chapter \\
1219XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \\
1220
1221Traditionally, fixing C meant leaving the C-ism alone, while providing a better alternative beside it.
1222(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.)
1223
1224\subsection{\CFA features interacting with arrays}
1225
1226Prior work on \CFA included making C arrays, as used in C code from the wild,
1227work, if this code is fed into @cfacc@.
1228The quality of this this treatment was fine, with no more or fewer bugs than is typical.
1229
1230More mixed results arose with feeding these ``C'' arrays into preexisting \CFA features.
1231
1232A notable success was with the \CFA @alloc@ function,
1233which type information associated with a polymorphic return type
1234replaces @malloc@'s use of programmer-supplied size information.
1235\begin{cfa}
1236// C, library
1237void * malloc( size_t );
1238// C, user
[5a553e2]1239struct tm * el1 = malloc( sizeof(struct tm) );
[82e5670]1240struct tm * ar1 = malloc( 10 * sizeof(struct tm) );
1241
1242// CFA, library
1243forall( T * ) T * alloc();
1244// CFA, user
1245tm * el2 = alloc();
1246tm (*ar2)[10] = alloc();
1247\end{cfa}
1248The alloc polymorphic return compiles into a hidden parameter, which receives a compiler-generated argument.
1249This compiler's argument generation uses type information from the left-hand side of the initialization to obtain the intended type.
1250Using a compiler-produced value eliminates an opportunity for user error.
1251
1252TODO: 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
1253
1254Bringing 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.
1255In the last example, the choice of ``pointer to array'' @ar2@ breaks a parallel with @ar1@.
1256They are not subscripted in the same way.
1257\begin{cfa}
1258ar1[5];
1259(*ar2)[5];
1260\end{cfa}
1261Using ``reference to array'' works at resolving this issue. TODO: discuss connection with Doug-Lea \CC proposal.
1262\begin{cfa}
1263tm (&ar3)[10] = *alloc();
1264ar3[5];
1265\end{cfa}
1266The implicit size communication to @alloc@ still works in the same ways as for @ar2@.
1267
1268Using proper array types (@ar2@ and @ar3@) addresses a concern about using raw element pointers (@ar1@), albeit a theoretical one.
1269TODO xref C standard does not claim that @ar1@ may be subscripted,
1270because no stage of interpreting the construction of @ar1@ has it be that ``there is an \emph{array object} here.''
1271But both @*ar2@ and the referent of @ar3@ are the results of \emph{typed} @alloc@ calls,
1272where the type requested is an array, making the result, much more obviously, an array object.
1273
1274The ``reference to array'' type has its sore spots too.
1275TODO see also @dimexpr-match-c/REFPARAM_CALL@ (under @TRY_BUG_1@)
1276
1277TODO: 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.