my ideas for array operations

Bruce Adams tortoise_74 at yeah.who.co.uk
Sat Oct 6 01:12:35 PDT 2007


dennis luehring Wrote:

> > Agreed. Votes--. Pointless.
> 
> but why do we have arrays and slices and ~ as operations in language?
> why do D overcomes the C++ stage of using templates for every natural 
> stuff, and why should we halt to integrate some other natural feature 
> for arrays into D (don't look at my syntax - just the idea behind)
> 
> i know:
> -my syntax is shit
> -the syntax must be clear, easy and 100% natural to use
> -array operations are heavily in use
> -compilers can't easily optimize huge amount of nested foreaches
> ...
> 
> but we can get:
> -better optimisation by the compiler (think of D complex support - for 
> walter; even the optimiation of a non use real part is a reason for 
> integration)
> -(maybe) autoparallelisation
> -cleaner implementations
> ...
> 
> > Besides, operators should avoid being O(n).
> 
> ok array1 OP array2 is bad
> 
> but ~OP and @OP are (my) operator-specialisations; they must be O(n) :-}
> 
> ciao dennis

There seems to be a tendency by some posters to opt for every possible new language syntactic feature under the sun. A good language should be feature rich without being verbose. Whenever there is a new problem to solve first see how the existing tools can solve it. If they can't you may need a new feature. Most of the time they can. Sometimes the code is inelegant and some new syntactic sugar helps. 
So first of all what are the kinds of problem we want to solve.

* code should be easily parallelisable by the compiler on platforms that support it.

* assign values to contents of an array.

I believe we have already borrowed array assignment from Fortran (90?)

int foo[4][4] = 5;
foo = 5;  // assign 5 to all elements in the matrix

It would be useful to do this algorithmically.
i.e.
* assign values to contents of an array based on index using a function.

pure int valueFunctor(int x,int y) {
   return x+y;
}

assignWithFunctor!(foo, valueFunctor);

sets foo to:
    0 1 2 3
    1 2 3 4
    2 3 4 5
    3 4 5 6

Similarly we would want array operations equivalent to:
     
UpdateValue!(foo, updateFunctor);    

int IncrementFunctor(int value) {
   value++;
}

int myIncrementWithIndicesFunctor(int value, int xIndex, int yIndex) {
   value+=x+y;
}

We also want to combine values somehow.

Some kind of "forall!" template. A classic usage would be calculating the determinant of a matrix.

We want something to operate on two arrays with the same dimensions so that.

int foo[5];
int bar[5];

foo + bar  and foo +=bar do what we expect even if we have to use a template for now.

These are easily done as library templates but the here's the catch. How do you make it so that compiler vendors can make that library exploit native parallism on machines that support it?
For a function the implementation can be hidden. I'm not sure if D lets you do this with something declared a template. The object format would have to support this or at least the export of explict specialisations such as:

  assignWithFunctor!(int[][], int delegate(int,int));

Does anyone know the answer to this.

The foreach loop already unwinds array slices and theorhetically is parallelisable. I note that Cray's Chapel language has two variants of this. One where the order of operations is guaranteed and one where
it is order dependent and therefore distributable.

One more thing we want is a template allowing us write a functor for matrix multiplication

int foo[2];
int bar[2];
int foobar[2][2];

// need to find or invent the proper generic name for this
somekindofproduct!(out int[][], int[], int[], int delegate(????))

such that with the delegate set to "matrixMultiply!"

somekindofproduct!(foobar , foo, bar, matrixMultiply!);

implements matrix multiplication.
The implication here could be that we might need "template delegates" but probably not with a little thought.

syntactic sugar for operator overloads allowing:

foobar = foo * bar;  would be nice but we can wait for things to mature first. 
The cleverness needed here is overloading based on the dimensions of the arrays and picking up and reporting sizes mismatches at compile time. Guess what D templates aleady let us do this. The implementation is left as an exercise :).

Bruce.

hint: I suspect we also need a library template for compileTimeError!




More information about the Digitalmars-d mailing list