To: J3 J3/19-150
From: Malcolm Cohen
Subject: Rank-agnostic array element and section denotation
Date: 2019-February-13
1. Introduction
Generic programming is facilitated not merely by being able to write code
that is the same for different types and kinds, but also being able to
write code that is the same for different ranks.
Another paper addresses the topic of declaration.
This paper addresses the topic of denotation. Whole array operations are
already fine, but individual element accesses, and array sectioning, have
notation that is different for every rank.
2. Array element notation
The idea is simple: allow a vector of subscript values to be provided
instead of a list of scalar values. A new notation is needed because
otherwise there is an ambiguity issue with vector subscripts.
Illustrative syntax:
to access element A ( V(1), V(2), ... V(n) )
use the syntax
A @( V )
where SIZE(V) == RANK(A).
This can also be convenient for accessing the first/last/minimum/maximum/
"special" location, e.g.
A @( MINLOC(A) )
access the minimum element.
3. Array section notation
To access a specific column of an array of arbitrary rank, the following
illustrative syntax could be used:
A @( :, V )
where SIZE(V) == RANK(A) - 1.
A section in the middle, perhaps every second element of dimension N,
could be
A @( V1, ::2, V2 )
where SIZE(V1)+SIZE(V2)+1 == RANK(A).
4. Array traversal
A rank-agnostic array traversal can be written using this notation, e.g.
V = 1
DO
process element A @( V )
V(1) = V(1) + 1
IF (V(1)>UBOUND(A,1)) THEN
code to step the bounds omitted;
when the final dimension has its upper bound reached, EXIT
END IF
END DO
This is completely general, but may be less efficient than having some
built-in mechanism for whole array traversal (at least until the compilers
start to recognise this kind of idiom).
5. Further possible extension
This kind of notation could conceivably be used for accessing elements of
an assumed-rank array. Further investigation may be warranted.
===END===