@inverse

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 25 13:44:38 PST 2015


On Wed, Feb 25, 2015 at 09:25:47PM +0000, Daniel N via Digitalmars-d 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 this idea. It could help ARC by not requiring specific language
support for ARC, at least as far as eliding redundant inc/dec pairs are
concerned, but allowing a library refcounting type to hint to the
optimizer that if inc/dec of the count occurs in pairs, the compiler can
elide them if nobody else looks at the refcount in the interim..

It also helps algebra libraries where repeating a self-inverting
operation can be automatically elided, thereby simplifying complex
expressions a bit and perhaps allowing further optimizations.

For the latter, though, it would be even better if other identities are
definable, for example @reflexive for indicating that func(x,x) == true,
@symmetric for indicating func(x,y) == func(y,x), and so on.

Not sure how likely it is that this will actually make it into the
language, though. Recently there seems to be a lot of resistance against
adding new features that don't have sufficiently wide applicability.


T

-- 
People tell me that I'm paranoid, but they're just out to get me.


More information about the Digitalmars-d mailing list