3 ms·
Unless you go deeply in functional programming, writing a program is still writing a Recipe. It's writing a recipe to a very, very dumb line cook. Except he's
by keyle 14y ago
Unless you go deeply in functional programming, writing a program is still writing a Recipe.
It's writing a recipe to a very, very dumb line cook. Except he's really fast, but really dumb.
Unless you're writing a pure stateless turing machine, programming is closer to sending a very detail recipe than defining the universe.
Like anything though, the computer is a tool, you can use it as an algebra machine or as a line cook. It' a bit like you can use a hammer to blow open a wall, or carefully insert a nail in a wall.
- 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