For an array with dimensions \(n_1,n_2,\ldots n_r\) stored in column-major order, an element with indices \(a_1,a_2,\ldots a_n\) is located at offset
\(\displaystyle\sum_{i=1}^r \left(\prod_{k=1}^{i-1} n_k\right) a_i\)
The \(\displaystyle\prod_{k=1}^{i-1} n_k\) are called strides. We can calculate them all in one go with
init $ scanl' (+) 1 dims
The cute part is that we can combine this with Num
instances for expressions
in a compiler.