@inverse

Xinok via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 25 20:07:00 PST 2015


On Thursday, 26 February 2015 at 02:05:25 UTC, Sativa wrote:
> On Thursday, 26 February 2015 at 00:56:02 UTC, Xinok wrote:
>> 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).

Not intentionally, but consider that D is a very generative 
language. Templates and string mixins are used heavily. However, 
the generated code is usually pretty generic so little things 
like trig identities may appear in the code which the compiler 
doesn't know how to optimize away. Some C++ compilers make 
assumptions about standard library functions so techniques 
similar to this are not unheard of.

> 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.

That almost seems too complicated, like it would require a 
complete CAS built into the compiler. Some functions don't have 
clearly defined inverses as well, such as x^^3. Then x^^2 is only 
invertible if you limit the domain and range to x >= 0.

>
> 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.

I wouldn't use the term "linear" to mean "invertible". A linear 
function is one such that:
f(c*a + b) = c*f(a) + f(b)


More information about the Digitalmars-d mailing list