At the Wolfram Technology Conference 2016 there were two presentations that imply that with the next-generation Wolfram Compiler there will be some limited ability to have code optimize code.
Presentation 1: Replacing your code with a more efficient version
The compiler runs an analysis on your code looking for patterns that can be optimized. For example, the following code squares the elements in a list and then adds 3:
foo=#+3&;
bar=#^2&;
Map[foo, Map[bar, Range@10]]
Because foo and bar have no side effects, the compiler symbolically rewrites the code to use "loop fusion" and expresses line 3 as:
Map[foo@*bar, Range@10]
This new expression is shorter and traverses over the list only once.
In the demonstration at the conference they showed that there is intermediate step which contains the loop-fused expression. Thus it would be possible replace your original code with the new optimized expression, which is more effective.
Presentation 2: Deconvoluting Code
There has been significant effort invested in trying to understand if two code segments are equivalent. The presentation talked about an online programming challenge system that evaluated not just test cases, but true code equivalence.
The system the presenter demonstrated used systems of transformation rules to change code into a canonical form that could be compared to a "correct" solution.
For example, his system would take either of the following expressions:
Range@10
Table[i,{i,1,10}]
And through rule transformations convert them into the "canonical form"
Table[i,{i,1,10,1}]
Which is equivalent to the previous expressions.
Since this technology exists, it would also be possible to reverse the rules, and take the rather convoluted expression
Table[i,{i,1,10,1}]
And apply:
Table[a___,{i_, b_, c_, 1}]->Table[a,{i,b,c}]
Table[i,{i_, 1, n_,}]->Range[n]
To get the clearer Range[10] function back out.
Conclusion
There are indeed programs that attempt to optimize code and there exists efforts underway that would allow for more advanced code deconvolution. Many of these methods however relied on freedom from 'side effects' common in state-based procedural programming. Furthermore these projects have not yet developed direct/user-friendly interfaces to give the feedback to the user.
Experimental`OptimizeExpression
. $\endgroup$F1, F2, F3, ... , FN
possibly each with its own arguments, a rule to reduce the compositionFN@* ... @*F4@*F3@*F2@*F1
to a single builtinG
could be constructed- even in some appropriately general case if you're lucky. I'm pretty comfortable that I'll never need an auto-optimizer, but what would definitely be useful is a code-readability optimizer/beautifier for all those awful huge differential equations we get on here. $\endgroup$