views:

374

answers:

10

At the risk of sounding naive, I ask this question in search of a deeper understanding of the concept of programming languages in general. I write this question for my own edification and the edification of others.

What is a useful definition of a computer programming language and what are its basic and necessary components? What are the key features that differentiate languages (functional, imperative, declarative, object oriented, scripting, etc...)?

One way to think about this question. Imagine you are looking at the hardware of a modern desktop or laptop computer. Assume, that the C language or any of its variants do not exist. How would you describe to others all the things needed to make the computer expressive and functional in terms of what we expect of personal computers today?

Tangentially related, what is it about computer languages that allow other languages to exist? For example take a scripting language like Javascript, Perl, or PHP. I assume part of the definition of these is that there is an interpreter most likely implemented in C or C++ at some level. Is it possible to write an interpreter for Javascript in Javascript? Is this a requirement for a complete language? Same for Perl, PHP, etc?

I would be satisfied with a list of concepts that can be looked up or researched further.

+4  A: 

While this doesn't directly answer your question, I am reminded of the Revenge of the Nerds essay by Paul Graham about the evolution of programming languages. It's certainly an interesting place to start your investigation.

Greg Hewgill
Thanks Greg. Looks like an interesting essay. I will be sure to read it.
Gordon Potter
Nice read, thanks
johnc
+19  A: 

Like any language, programming languages are simply a communication tool for expressing and conveying ideas. In this case, we're translating our ideas of how software should work into a structured and methodical form that computers (as well as other humans who know the language, in most cases) can read and understand.

What is a useful definition of a computer programming language and what are its basic and necessary components?

I would say the defining characteristic of a programming language is as follows: things written in that language are intended to eventually be transformed into something that is executed. Thus, pseudocode, while perhaps having the structure and rigor of a programming language, is not actually a programming language. Likewise, UML can express many powerful ideas in an abstract manner just like a programming language can, but it falls short because people don't generally write UML to be executed.

How would you describe to others all the things needed to make the computer expressive and functional in terms of what we expect of personal computers today?

Even if the word "programming language" wasn't part of the shared vocabulary of the group I was talking to, I think it would be obvious to the others that we'd need a way to communicate with the computer. Just as no one expects a car to drive itself (yet!) without external instructions in the form of interaction with the steering wheel and pedals, no one could expect the hardware to function without being told what to do. As noted above, a programming language is the conduit through which we can make that communication happen.

Tangentially related, what is it about computer languages that allow other languages to exist?

All useful programming languages have a property called Turing completeness. If one language in the Turing-complete set can do something, then any of them can; they are said to be computationally equivalent.

However, just because they're equally "powerful" doesn't mean they're equally nice to work with for humans. This is why many people are willing to sacrifice the unparalleled micromanagement you get from writing assembly code in exchange for the expressiveness and power you get with higher-level languages, like Ruby, Python, or C#.

Is it possible to write an interpreter for Javascript in Javascript? Is this a requirement for a complete language? Same for Perl, PHP, etc?

Since there is a Javascript interpreter written in C, it follows that it must be possible to write a Javascript interpreter in Javascript, since both are Turing-complete. However, again, note that Turing-completeness says nothing about how hard it is to do something in one language versus another -- only whether it is possible to begin with. Your Javascript-interpreter-inside-Javascript might well be horrendously inefficient, consume absurd amounts of memory, require enormous processing power, and be a hideously ugly hack. But Turing-completeness guarantees it can be done!

John Feminella
+1, excellent writeup!
Adam Robinson
Excellent answer! I would add (to the last point) that not only is it possible, but often languages will be created with another language (C is common) but then compilers or interpreters will be created that can compile the language itself. FORTH, LISP, Scheme, and C++ come immediately to mind - all are in this vein (though some in different ways than others).
David
I can make a javascript interpreter with javascript: eval(code); LOLOLOLOLOLOL
M28
@M28: I'm assuming you're joking here, but eval() is not actually a Javascript interpreter in a true sense. For example, `x = 5;` is valid Javascript, but you can't pass that to `eval()`; you have to quote it. But the string `"x = 5;"` doesn't have the same meaning as `x = 5`. Thus, `eval()` falls slightly short.
John Feminella
@John I was joking ._.
M28
+2  A: 

See "Programming Considered as a Human Activity." EWD 117. http://www.cs.utexas.edu/~EWD/transcriptions/EWD01xx/EWD117.html

Also See http://www.csee.umbc.edu/331/current/notes/01/01introduction.pdf

S.Lott
+1  A: 

Interesting.

I'd say the defining feature of a programming language is the ability to make decisions based on input. Effectively, if and goto. Everything else is lots and lots of syntactic sugar. This is the idea that spawned Brainfuck, which is actually remarkably fun to (try to) use.

There are places where the line blurs; for example, I doubt people would consider XSLT to really be a programming language, but it's Turing-complete. I've even solved a Project Euler problem with it. (Very, very slowly.)

Three main properties of languages come to mind:

  1. How is it run? Is it compiled to bare metal (C), compiled to mostly bare metal with some runtime lookup (C++), run on a JIT virtual machine (Java, .NET), bytecode-interpreted (Perl), or purely interpreted (uhh..)? This doesn't comment much on the language itself, but speaks to how portable the code may be, what sort of speed I might expect (and thus what broad classes of tasks would work well), and sometimes how flexible the language is.
  2. What paradigms does it support? Procedural? Functional? Is the standard library built with classes or functions? Is there reflection? Is there, ideally, support for pretty much whatever I want to do?
  3. How can I represent my data? Are there arrays, and are they fixed-size or not? How easy is it to use strings? Are there structs or hashes built in? What's the type system like? Are there objects? Are they class-based or prototype-based? Is everything an object, or are there primitives? Can I inherit from built-in objects?

I realize the last one is a very large collection of potential questions, but it's all related in my mind.

I imagine rebuilding the programming language landscape entirely from scratch would work pretty much how it did the first time: iteratively. Start with assembly, the list of direct commands the processor understands, and wrap it with something a bit easier to use. Repeat until you're happy.

Yes, you can write a Javascript interpreter in Javascript, or a Python interpreter in Python (see: PyPy), or a Python interpreter in Javascript. Such languages are called self-hosting. Have a look at Perl 6; this has been a goal for its main implementation from the start.

Ultimately, everything just has to translate to machine code, not necessarily C. You can write D or Fortran or Haskell or Lisp if you want. C just happens to be an old standard. And if you write a compiler for language Foo that can ultimately spit out machine code, by whatever means, then you can rewrite that compiler in Foo and skip the middleman. Of course, if your language is purely interpreted, this will probably result in a stack overflow...

Eevee
The term you are looking for is Turing complete. In simple terms such a language can write any program while something that is not TC can only do a limited set of task. eg SQL is not Turing complete while Basic is.
Martin Beckett
Whoops. That's a bad typo; my mistake.
Eevee
+1  A: 

Human expression which:

  • describes mathematical functions
  • makes the computer turn switches on and off
Mark Stock
+1  A: 
Norman Ramsey
Thanks for the Turing Complete explanation. I had heard of the idea but didn't quite grasp it. Your answer provided a nice clarification.
Gordon Potter
+2  A: 

One thing a lot of "IT" types forget is that there are 2 types of computer programming languages:

  1. Software programming languages: C, Java, Perl, COBAL, etc.

  2. Hardware programming languages: VHDL, Verilog, System Verilog, etc.

Anon E. Mous
+3  A: 

Slightly long rant ahead.

A computer language is actually not all that different from a human language. Both are used to express ideas and concepts in commonly understood terms. Among different human languages there are syntactic differences, but you can express the same thing in every language (does that make human languages Turing complete? :)). Some languages are better suited for expressing certain things than others.

For example, although technically not completely correct, the Inuit language seems quite suited to describe various kinds of snow. Japanese in my experience is very suitable for expressing ones feelings and state of mind thanks to a large, concise vocabulary in that area. German is pretty good for being very precise thanks to largely unambiguous grammar.

Different programming languages have different specialities as well, but they mostly differ in the level of detail required to express things. The big difference between human and programming languages is mostly that programming languages lack a lot of vocabulary and have very few "grammatical" rules. With libraries you can extend the vocabulary of a language though.

For example:

Make me coffee.

Very easy to understand for a human, but only because we know what each of the words mean.

coffee : a drink made from the roasted and ground beanlike seeds of a tropical shrub
drink : a liquid that can be swallowed
swallow : cause or allow to pass down the throat
... and so on and so on

We know all these definitions by heart, but we had to learn them at some point.

In the same way, a computer can be "taught" to "understand" words as well.

Coffee::make()->giveTo($me);

This could be a perfectly valid expression in a computer language. If the computer "knows" what Coffee, make() and giveTo() means and if $me is defined. It expresses the same idea as the English sentence, just with a different, more rigorous syntax.

In a different environment you'd have to say slightly different things to get the same outcome. In Japanese for example you'd probably say something like:

コーヒーを作ってもらっても良いですか?
Kōhī o tsukuttemoratte mo ii desu ka?

Which would roughly translate to:

if ($Person->isAgreeable('Coffee::make()')) {
    return $Person->return(Coffee::make());
}

Same idea, same outcome, but the $me is implied and if you don't check for isAgreeable first you may get a runtime error. In computer terms that would be somewhat analogous to Ruby's implied behaviour of returning the result of the last expression ("grammatical feature") and checking for available memory first (environmental necessity).

If you're talking to a really slow person with little vocabulary, you probably have to explain things in a lot more detail:

Go to the kitchen.
Take a pot.
Fill the pot with water.
...

Just like Assembler. :o)

Anyway, the point being, a programming language is actually a language just like a human language. Their syntax is different and specialized for the problem domain (logic/math) and the "listener" (computers), but they're just ways to transport ideas and concepts.

EDIT:
Another point about "optimization for the listener" is that programming languages try to eliminate ambiguity. The "make me coffee" example could, technically, be understood as "turn me into coffee". A human can tell what's meant intuitively, a computer can't. Hence in programming languages everything usually has one and one meaning only. Where it doesn't you can run into problems, the "+" operator in Javascript being a common example.

1 + 1     -> 2
'1' + '1' -> '11'
deceze
Nice answer. I like your analogy to the assembler and the need for more detail.
Gordon Potter
+1  A: 

Not a definition, but I think there are essentially two strands of development in programming languages:

  • Those working their way up from what the machine can do to something more expressive and less tied to the machine (Assembly, Fortran, C, C++, Java, ...)

  • Those going down from some mathematical or theoretical computer science concept of computation to something implementable on a real machine (Lisp, Prolog, ML, Haskell, ...)

Of course, in reality the picture is not as neat, and both strands influence each other by borrowing the best ideas.

starblue
+1  A: 

As a friend taught me about computer languages, a language is a world. A world of communication with that machine. It is world for implementing ideas, algorithms, functionality, as Alonzo and Alan described. It is the technical equivalent of the mathematical structures that the aforementioned scientists built. It is a language with epxressions and also limits. However, as Ludwig Wittgenstein said "The limits of my language mean the limits of my world", there are always limitations and that's how one chooses it's language that fits better his needs.

It is a generic answer... some thoughts actually and less an answer.

Aggelos Mpimpoudis
I like the Wittgenstein reference. Wittgenstein also said "The world is the totality of facts, not of things." Perhaps we could take this to be analogous to the way binary information works. A program is therefor a totality of "binary" facts as expressed by the programming language once compiled.
Gordon Potter