@inverse

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 25 17:40:31 PST 2015


On 2/25/15 4:55 PM, 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

Since you're here, do you plan to fix stable sort as recently discussed? 
-- Andrei



More information about the Digitalmars-d mailing list