When you talk about “languages” for computer programming, it sounds like there are many different ones. Why?
[Update: Jovana Milutinovich has translated this post into Serbo-Croatian!]
Here are two pictures:
Suppose you needed to invent a language in which you could communicate with aliens about these two items.
If the aliens were termite-like, the fact that the table is metal is probably important. In fact, they might want a lot of detail about the type of wood the chair is made from, but no detail at all about the table since it’s uninteresting to them.
If the aliens were small of build, the fact that the table is large and heavy, whereas the chair is smaller and lighter, might be important. The specific materials they’re made of might be inconsequential.
If the aliens are about the same height as you but have really short legs and a really long torso, the average heights of the chair seat and tabletop might be the most interesting points–regardless of construction materials, weight, or other properties.
Therefore, if you were designing a language for discussing these items, would you design a language that lets you say a lot about materials, a lot about weight, or a lot about height?
The answer is it would depend on what, in fact, the aliens are like. A contentious line of argument concerns whether Native Alaskans have significantly more words for “snow” than exist in English; to the extent this is true, it makes sense because those distinctions, lost in the speech of people living in temperate climates, might have had important quotidian impacts on Native Alaskans’ lives.
So it is with programming languages. The range of activities that constitutes “programming” is vast. Some programs control the operation of life-critical systems, like medical equipment, antilock brakes or the Space Shuttle’s navigation systems. Some programs let you look up cool stuff on the Web. Some programs protect your computer from evil viruses, and yet others do the “heavy lifting” of finding the best route from point A to point B on Google Maps. Different kinds of programming problems require focusing on certain aspects of a problem (and ways of solving it) at the expense of others. Different programming languages have evolved to fill those needs.
So computers “understand” all those languages?
No. At bottom, the only “language” that is “understood” by any computer is really just a description (that can be written down) of how electrical signals fly around inside the computer. This “language” is called machine code, and at its most basic level, a machine code description of the computer’s activities corresponds more or less to activiating particular electrical pathways in the computer.
Why is this a problem?
Imagine you have to explain to someone who doesn’t speak English how to drive a car. But the only concepts you know how to express in her language are very basic things like talking about the position of their limbs. In particular, you don’t know how to say “turn the steering wheel”, and in fact you don’t even know how to say “turn”. Here’s what you might say:
“Take your right hand, and put it on the round thing in front of you at a position that is about 30 degrees off of the vertical. Then take your left hand, and put it on the same round thing, but in a position that is 30 degrees off the vertical in the opposite direction. Then, while gripping the round thing, move your arms in such a manner that your right arm is making your right hand travel to the right and down, while your left arm is making your left hand travel to the right and up. If you reach the limit of your range of motion, release your right hand from gripping the round thing, cross your right arm over your left arm, and re-grip the round thing in a position that is now just a few degrees to the left of the vertical; then, while continuing to move your left arm as before, start moving your right arm in a new trajectory that takes it toward the centerline of the round thing…”
You can see how tedious this would get. If your assignment was to train a race-car driver, this would be impossible. You’d have to already know how to talk to her in terms of a steering wheel, turning, etc.
Programming a computer using machine code is like the first example. You have a complicated task to express, but you have only a simple limited vocabulary with which to express it. The result is that your explanations are cumbersome and verbose, and someone reading the explanation could easily miss the forest for the trees.
“High level” programming languages are like the second example. You can just say, “Turn the wheel clockwise”, and that single instruction gets “translated” to the right sequences of detailed motor actions to make it happen.
And, of course, if you were coaching a concert-class violinist, it wouldn’t matter if you could say “turn the wheel to the right” – but being able to say “place your hand on the neck of the violin in first position” would be very useful.
That’s the distinction between programming in machine code–”Move your arm…grip the round thing…move your other arm…release your grip…”–versus programming in a high level language: “Turn the wheel clockwise.”
OK, so machine code programming sucks, yet it is the only language “understood” by a computer as designed. Bad news. But, the good news is that those other high level languages can be translated to low-level machine language.
How is the translation done? In a weird “snake eats its own tail” scenario, the translation is done by other programs – programs whose job it is to translate one language into another. (This isn’t really that surprising. There are professional human translators whose job it is to do this for different languages.)
But when a brand-new computer is made, how do you create the “translator” programs for it in the first place?
Basically, you start by writing out “the hard way” (using the computer’s own “native tongue”, which is very esoteric and detailed and varies from computer to computer) a simple program that can only translate very simple “sentences” in a more sophisticated language. (Imagine teaching a foreigner with no English skills just a few vital English sentences. It’s enough for them to get by, but hardly enough for them to write the Great American Novel.) Then, using these sentences, a more sophisticated program can be built–which in turn allows slightly more complex “sentences” to be translated. (The foreigner now knows enough English to read the newspaper, and can therefore teach herself more.) This process continues until, after a while, we have a program that can “translate” very sophisticated “sentences” down into the machine’s native language (which, by the way, is actually called machine code). Once we have that program, we throw away all the intermediate ones, and presto! We have a language translator. This is called bootstrapping a new language, and while the description given here is a little bit of a simplification, the essential intellectual principle of the process is approximately the way I’ve described it.