Language Design Principles


Language Design Principles

Hope
Welcome back to our episode on how to succeed in language design without really trying. The central question is deceptively practical: when you have a job involving lots of data, how do you choose—or design—the right language for it?

Corey
A good example is a geology student asked to find all the large volcanic eruptions in a huge dataset. Doing that by hand sounds painful, so most computer scientists would write a program. But then comes the interesting question: which language should you use?

Hope
For that particular task, C is possible, but it is a poor fit. The speaker tried it and immediately ran into details such as remembering the argument order for fgets, whether it returned EOF or NULL, using the right scanf format, and handling strtok. The result took roughly twenty minutes or half an hour, was not very robust, and could fail on long or malformed input.

Corey
Then he wrote the same task in AWK, and it took about two seconds. It worked the first time and handled the input robustly. That contrast illustrates a major lesson in language design: notation matters enormously. A good notation matches the kind of work you are doing and lets you express the solution directly.

Hope
It also highlights two kinds of efficiency. Programmer efficiency is how quickly and easily a person can write the program; machine efficiency is how quickly the finished program runs. For a script that runs once on a modern computer, machine efficiency barely matters, so saving the programmer twenty minutes is much more important.

Corey
But that tradeoff changes for other applications. Search engines, for example, do not run in AWK because machine efficiency matters enormously at that scale. They use languages such as C or C++, or comparable tools, because the program runs repeatedly and must process enormous workloads.

Hope
The abundance of programming languages creates a kind of programming Tower of Babel. A 1961 Communications of the ACM cover already showed a surprising number of languages, only about thirteen years after the first stored-program computer. Languages such as FORTRAN, LISP, SIMULA, and BASIC have survived from that era, but the total number of languages today is far larger.

Corey
One useful, though simplified, way to view language history starts in the 1940s with absolute machine code: binary instructions entered through switches or paper tape. Assembly language followed, automating clerical tasks such as translating operation codes and resolving memory addresses.

Hope
The major leap came with high-level languages in the late 1950s and 1960s. FORTRAN addressed engineering problems, COBOL addressed business problems, and BASIC supported instruction. These languages raised the level of discussion above a particular machine and allowed more people to program without mastering a machine's assembly language.

Corey
They also improved portability. A program could be written once in a high-level language and then translated for different machines. In the 1970s, languages such as C extended that idea into the core of computing: compilers, loaders, assemblers, editors, and operating systems could be written in a high-level language.

Hope
As computers became more powerful and programs grew larger, the challenge shifted toward maintaining intellectual control over structure. That encouraged object-oriented languages, with C++ as a major example, along with stronger typing and languages such as Java.

Corey
Java also illustrates a hardware tradeoff. More powerful machines made it acceptable to interpret programs rather than always compile them directly into highly efficient machine code. Interpretation can cost speed, but it can make programming easier and portability much simpler.

Hope
Alongside mainstream languages came scripting languages. Their defining feature is that text is a fundamental data type, not an afterthought. Regular expressions are usually built into the language or provided as an integral library, associative arrays are common, types are handled loosely, and interpreters are often practical because modern hardware is fast enough.

Corey
AWK, Perl, Python, Ruby, and JavaScript fit that scripting stream in different ways. JavaScript began in 1995 and became especially dominant on the Web. Functional languages form another stream, including LISP, Haskell, and OCaml; they are relatively uncommon in everyday industry compared with mainstream languages, but their research has contributed recursion, garbage collection, type inference, and first-class functions.

Hope
Then there are domain-specific languages, or DSLs. These focus on a narrow area instead of trying to handle every programming problem. They may not even be Turing complete, but they still have formal grammars and well-defined semantics, at least in principle.

Corey
Examples include regular expressions, the shell, AWK, document-preparation languages, SQL for database retrieval, R for statistics, and AMPL for mathematical optimization. The important practical point is that designing a small language for a focused task is realistic for an individual, whereas designing the next C++ is a much larger and less reasonable ambition.

Hope
That is where notation becomes more than a matter of convenience. Benjamin Whorf argued that language shapes how we think and what we can think about. The same idea applies strongly to artificial languages: the notation we give programmers influences the solutions they can see and express.

Corey
Alan Perlis made a related point: a programming language should change the way you think about something; otherwise it is not worth worrying about. A successful language gives people a useful mental model, not merely a different spelling for the same old ideas.

Hope
AWK grew from experience with the Unix shell, which was not very good for numerical work. Al Aho brought an interest in regular expressions, Peter Weinberger brought database concerns, and the resulting language was designed for very short programs—often one-liners—for selecting data, transforming it, or generating reports.

Corey
Its core notation is pattern-action. You specify a pattern, and when input matches that pattern, the associated action runs. In grep, the pattern might be a regular expression and the action is printing matching lines; in sed, patterns trigger editing commands; in parser and lexical-analyzer generators, grammar rules or lexical patterns trigger corresponding actions.

Hope
AWK's pattern-action structure also automates the repetitive framework around the task. Conceptually, it loops over files, then lines, then patterns, but the user does not have to write those nested loops. Input is read automatically, lines are split into fields automatically, and fields can be accessed directly.

Corey
The language also avoids making users worry too much about types. A variable can hold a string or a number, and AWK tries to perform the appropriate operations. It includes regular expressions, associative arrays, and familiar C-like actions, so it combines useful new ideas with things programmers may already recognize.

Hope
Associative arrays are especially powerful because they let you organize data by meaningful keys. For example, if records list purchases such as pizza and beer with their costs, you can use the item names as keys, accumulate the totals as each line is processed, and print the final amounts. An action with no pattern runs on every input line, which makes that computation remarkably compact.

Corey
The broader lesson is that something simple in your head should be simple in the code. If the task is naturally a pattern followed by an action, or an accumulation indexed by a name, the language should make that structure visible instead of burying it under boilerplate.

Hope
AWK also teaches what happens after a language is released. If a tool is useful, people will abuse it. AWK was intended for one- or two-line programs, yet people have written programs around twenty thousand lines long, used it to generate other AWK programs, and even learned programming with it.

Corey
Machine-generated input creates another important lesson. It is much more voluminous than human input and often explores combinations that people would never think to write. The first C++ compiler produced C code with deeply nested expressions and very similar names, creating an excellent stress test for C compilers.

Hope
Language designers also face a tension between correction and stability. If a language has mistakes or needs new features, changing it may improve the design. But frequent changes can break existing programs and force users to keep relearning the language, as has happened with changing language versions such as Swift.

Corey
That is why adding features while keeping a language small and coherent is difficult. A related warning from Tony Hoare is not to include untried ideas of your own in a language—although, of course, somebody has to experiment with those ideas somewhere before they can become trusted.

Hope
AMPL shows what a different domain-specific language looks like. It targets mathematical optimization: finding variable values that satisfy constraints while minimizing or maximizing an objective function. Linear programming uses linear constraints and objectives, while related forms include integer and quadratic programming.

Corey
Before AMPL, people might generate a large coefficient matrix with a special program or use GAMS, which was described as a very cumbersome language modeled on Fortran. AMPL instead lets someone express an optimization model in notation close to ordinary mathematics, then combines that model with specific data and sends the resulting problem to a solver.

Hope
Consider the diet problem. You have foods, nutrients, the amount of each nutrient in each food, and the cost of each food. The goal is to choose quantities that provide enough nutrients, avoid excessive amounts, and minimize total cost. AMPL represents the sets, parameters, indexing, objective, and constraints directly.

Corey
AMPL is declarative at that level: it describes what the problem is, not the computational steps used to solve it. The system translates the model into a large, usually sparse matrix, passes it to an optimization solver, and then presents the results in terms of the original model. A typical industrial model might have thousands of rows and columns while having a density well below one percent.

Hope
The model is separate from the data. The same diet model can describe feeding dormitory students, buying TV dinners, or supplying a high-end restaurant; only the specific data changes. AMPL therefore includes a data language for tables and a command language for running models, displaying results, and controlling the process.

Corey
If the basic model allows fractional quantities, it might suggest buying forty-six and two-thirds packages of macaroni and cheese. Adding the word integer changes the variables to integer quantities, while the solver handles the much harder integer-programming problem. The model itself changes very little, which is a major benefit of a specialized modeling language.

Hope
AMPL became successful in a small but important market. Universities teach it, and industries use it for tasks such as airline crew scheduling, production planning, and transportation. It began as a clean declarative language, but users eventually needed loops, conditionals, and function calls, so those general programming features were added later.

Corey
That addition illustrates a recurring design problem. A language may begin with conceptual integrity—its parts fit together cleanly—but later additions can make the syntax irregular and ugly. AMPL's later programming features are an example of how extending a language can weaken its original simplicity.

Hope
AMPL is also proprietary rather than open source, and that raises a business question. A small company can sell specialized software, or it might support an open-source core through consulting, paid add-ons, or a freemium model. Consulting can work, but it is more like a retail service and is harder to scale than selling software.

Corey
Now let's shift to document preparation. EQN was designed for mathematical typesetting when ordinary formatting tools had no convenient way to express mathematics. Its notation followed the way mathematicians speak: phrases such as 'x sub i equals pi' could be translated into typesetting commands.

Hope
EQN benefited from Unix pipelines and severe hardware limits. Early Unix machines had only 64 kilobytes of memory, so separate small programs were connected together rather than placing every feature in one large program. EQN handled mathematics and passed its output to a text-formatting program.

Corey
The notation was simpler than TeX, but that simplicity made it easy to teach. Typists at Bell Labs who had no mathematical background could learn enough in about an hour to produce the standard mathematical documents of the time. EQN's parser was built using YACC, a parser generator, which made the implementation practical.

Hope
Don Knuth later drew on the same basic idea for TeX's mathematics mode, adding a much richer language and higher-quality output. That is a useful reminder that resource constraints are not always harmful; being forced to use small cooperating tools can lead to clean boundaries and appropriate notation.

Corey
Another document language was Pic, which described line drawings textually. Instead of drawing a flowchart or organization chart with a mouse, you wrote a textual description and Pic translated it into commands for Troff. A simple input-process-output diagram could be specified with very little code.

Hope
Textual descriptions are especially valuable when a drawing must be revised. A waveform could be generated once, and then a small change—such as drawing a vertical line every fourth sample—could produce a second related picture. An artist might have to redraw the image carefully, but a programmatic description makes the change trivial.

Corey
Pic also demonstrated recursive tool building. Since graphs are a specialized kind of picture, a graphing program could generate Pic code, which would generate Troff commands. If a language is useful, programs may generate input for it, and that machine-generated input can expose strengths and weaknesses that ordinary human-written examples do not.

Hope
EQN, Pic, and related tools such as Troff and TBL were useful in their era, but their ecological niche largely disappeared as TeX and LaTeX became more common. This shows that a language can fail to thrive even if its design is good: the surrounding environment or domain may simply move on.

Corey
So what helps a language succeed? First, the notation must solve a problem people care about and make the solution easy enough to write. Cultural compatibility helps too. Languages with C-like syntax—such as C++, Java, JavaScript, Perl, and AWK—benefit because programmers recognize enough of the surface form to get started.

Hope
Environmental compatibility matters as well. C++ succeeded partly because it fit naturally into the C and Unix ecosystem: it ran on Unix, worked with compatible libraries, and looked similar to C. By contrast, adopting a language such as Standard ML required more unfamiliar concepts and more investment in a new environment.

Corey
Open source can also be important because users do not have to pay to begin, and their contributions can improve the language for everyone. Proprietary languages can survive, especially in specialized markets, but they may face serious competition if an equivalent open alternative eventually appears.

Hope
Competition and timing shape success too. C faced relatively weak competition as a system language from Pascal and from typeless languages such as BCPL, which did not match the machines of the time as well. JavaScript beat Java in browsers partly because sending source code to a browser was much easier than compiling and distributing Java's compiled form.

Corey
Luck matters, but it often looks like repeated good timing. C benefited from the rise of minicomputers, then workstations, personal computers, and embedded systems. Objective-C found a niche through the NeXT workstation and later Apple platforms, even though it had previously seen little use.

Hope
Languages fail or stagnate for many reasons. Their domain may disappear, as happened to much of the Troff ecosystem. They may be too large, too complicated, too slow, or too late, as critics have argued about languages such as Algol-68, PL/I, and Perl 6. Even a language with a huge installed base, such as C++, can raise questions about whether its complexity has become too great.

Corey
Languages rarely vanish completely; more often they fail to thrive. Their user population shrinks, they stop growing, or their niche becomes less important. Philosophical choices can also limit adoption if a language demands a style of thinking that many programmers find too abstract or mathematically rigorous.

Hope
The final lesson is encouraging: there is still room to create languages that combine proven ideas with a genuinely useful new perspective. The sensible place to begin is usually a small domain-specific language, not a replacement for C++.

Corey
Choose a problem people care about, find a notation that matches how they think about it, automate the repetitive parts, build on familiar technology where possible, and keep the language coherent as it evolves. A small language can make a task dramatically easier—and designing one can be both practical and surprisingly satisfying.

Ask anything
We've read this whole study set, so questions about it get the best answers.