On Thursday, 26 February 2015 at 00:56:02 UTC, Xinok wrote:
On Wednesday, 25 February 2015 at 21:25:49 UTC, Daniel N wrote:
Just throwing an idea out there... How about using annotations to teach the compiler which functions are inverses of each-other, in order to facilitate optimizing away certain redundant operations even if they are located inside a library(i.e. no source).

A little pseudo-code for illustrational purposes, in case my above text is incomprehensible:

void inc() pure nothrow @inverse(dec)
void dec() pure nothrow @inverse(inc)

void swap(T)(ref T lhs, ref T rhs) pure nothrow @inverse(swap!T)

I like the idea but feel that it's application is too narrow. I prefer features which are more general and offer greater flexibility. I believe I've read somewhere that some [functional] languages define common patterns and equivalent substitutions for optimization purposes.

inc(dec(x)) -> x
dec(inc(x)) -> x
cos(x)^^2 + sin(x)^^2 -> 1

Which would exactly be the result of what Daniel is talking about except you are adding invariance which is a harder problem yet can be taken care of by the programmer(you would never intentionally write cos(x)^2 + sin(x)^2 for anything since it is equal to 1 and 1 is more efficient to compute).

The problem is one of composition and it is difficult in real circumstances since compositions may not be simply ordered.

e.g., what if you have

inc(foo(dec(x))

?

In this case one can't simplify because one doesn't know what foo does.

Hence, to do it properly one would have to create a whole compositional system. e.g., @linear, @nonlinear, @additive, @commutative, etc...

e.g., if we new foo was linear then we could simplify the above to foo(x).

...and, as you hinted at, most functions are non-linear and therefor will make @inverse nearly useless.


I suppose, though, one might be able to do something like setup @inverse functions for actions.

e.g., user clicks on button X. The inverse then is sort of an "undo" of that.

In an undo system one expects every action to be "linear"(have an inverse)... Hence it might be useful in such circumstances.

Reply via email to