14 ms·
> Unless you go deeply in functional programming, writing a program is still writing a Recipe. ... which the compiler spends a lot of effort turning into somet
by exDM69 14y ago
> Unless you go deeply in functional programming, writing a program is still writing a Recipe.
... which the compiler spends a lot of effort turning into something more algebraic. Imperative programming is a really a pain in the ass for the compiler, but thankfully after 50 years of research in to the subject compilers are smart enough to get rid of most of the imperative parts.
Of course, ultimately, the program will be translated into a form that is a recipe for the processor or byte code interpreter. But what happens in between would be somewhat easier if programming languages were more about algebra and less about statements.
- DanWaterworth 14y agoYou took the words right out of my mouth. Many compilers use an SSA intermediate representation which is much closer to a functional language than an imperative one primarily because all data becomes immutable. Compilers will also often try to add annotations that say whether a function is pure or not, because calls to pure functions can often be lifted out of loops or optimized away entirely.
- demallien 14y agoCompilers generate assembler, which is about as imperative as it gets... There's nothing functional about LDR1 $7a5112B : ADDA
- exDM69 14y ago> Compilers generate assembler, which is about as imperative as it gets... Yes. In the very end of the compilation the compiler emits machine code or some kind of byte code, which is imperative by nature. Before that, the compiler must do a lot of analysis and optimization. All of which happens in some intermediate representation, which is as little imperative as possible. In order to get there, the compiler does different control flow and data flow analyses such as live variable analysis, the goal of which is to turn mutable variables into immutable values. It has a resemblance to functional programming in some sense (you can read research papers about SSA, single static assignment and it's equivalence to CPS, continuation passing style, if you're interested). So in the end, we get assembly or some kind of byte code, but a lot of magic happens to get there. Back in the 1970's, C was like portable assembler. These days the compiler does so much magic that it's not true any more. We could use any language as the source language for compilation but yet we stick to an ill-fitted C-based languages that are really painful for the compiler and the programmer.
- demallien 14y agoWhoa, I think you're mixing things up there. Your original claim was that compilers get rid of the imperative nature of some programming languages. I pointed out that a compiler outputs something that is about as imperative as it gets, ie the compiler has made code mode imperative, not less. The fact that compilers use s-expressions as an intermediate form says more about the recursive nature of most syntaxes than it does about the imperativeness or not of the program being compiled, and the program being output. Also, as an aside, if you take some machine code that was compiled from C, even these days it is still ridiculously easy to take that code and turn it back into C. I have quite a bit of experience at doing precisely this. The only thing that has changed is that the compiler has got better at identifying code that is recalculating the same value over and over, and gets rid of that code.
- glassx 14y agoHe's talking about intermediate representation and how some optimizations, such as SSA [1], are closer to functional style. In the end it's all imperative assembly, but during the optimization phase it can be something else. [1] http://en.wikipedia.org/wiki/Static_single_assignment_form http://en.wikipedia.org/wiki/Static_single_assignment_form
- UK-AlasGou 14y agoI was thinking "Has he programmed in assembly?" Because it defiantly does not feel functional... It's loading data into registers, and then performing operations on the registers. It feels like a turning machine changing state over time.
- pka 14y agoThe only thing that has changed is that the compiler has got better at identifying code that is recalculating the same value over and over, and gets rid of that code. i.e. the compiler identifies pure functions and tries to move them out of any loops. On the other hand, you can't really restructure impure code that much. So basically, you are confirming what your parent said - the compiler has to first analyze the AST and hack its way around any IO and side effects before turning the optimized intermediate program representation into machine code. Btw, the actual hardware could as well be a lambda calculus reducer instead of a von Neumann machine - in that case a compiler wouldn't emit anything imperative at all.