So i'm taking a course on functional programming where we learn the ins and outs of Standard ML, today my instructor told me that one of the areas where such languages really shine is in compilers. A program that takes some very well defined input (C++ code for example), does optimizations on said input, and then translates it to assembly or bytecode or whatever. Apparently the mechanisms (pattern matching in particular) of functional programming languages such as ML and Haskell are very well suited for those tasks.

Yet he couldn't answer me why a compiler as big and advanced as GCC was implemented in C, and not in a functional language.

There must be a reason, enlighten me gurus.

10-11-2011

MK27

Quote:

Originally Posted by Neo1

Yet he couldn't answer me why a compiler as big and advanced as GCC was implemented in C, and not in a functional language.

Here's a guess:

- what does "well suited" mean? If it means, well suited from a programming point of view, big deal. By that standard, whatever language you are best at will be the language best suited to you writing a compiler.

- that Haskell etc. are "well suited to the task" does not mean that C is not. Generally, when I see a newer language lauded for "what it shines at", I take that to mean: these are the things where an implementation of this language rivals the performance of some widely used implementation of C (such as GCC). Not that it is better, but that it can compete with "the best" performance wise (C itself). Performance is a very objective measure. "Well suited for programming" is much more subjective.

- that the aspects of a compiler to which functional languages are well suited, are not the only aspects of a compiler.

- a Haskell compiler for a language other than Haskell is going to be hamstrung by it's dependency on whatever libraries Haskell uses, which would otherwise be totally unnecessary. GCC was/is first and foremost a C compiler.

Richard Stallman, the original author of GCC, is also a long term lisp fanatic. I've worked with RMS and he's very clear about that: he programs in either lisp or C, that's all, and really likes lisp. Yet while lisp is a functional language, GCC is in C. You could always inquire to him:

His email address is at the very bottom of the page. Be specific and succinct, he is very cantankerous but may answer your question anyway: "Why did you not use lisp for GCC? Do you consider C to be better suited to the task?"

10-13-2011

CornedBee

GCC was designed to be bootstrapped on systems that offer very little in the way of existing tools. On most such systems, you would expect some really bad C compiler to be around. The GCC C compiler core can be compiled by such a compiler; the rest of GCC is then compiled using this newly compiled mini-GCC.
Requiring a decent Lisp implementation to be around is much more demanding than requiring some kind of C compiler (especially as GCC comes from a time where there wasn't really such a thing as a decent Lisp implementation on general purpose hardware), and this low barrier to entry was undoubtedly one of the reasons for GCC's success.