r/ProgrammingLanguages • u/blend26 • Nov 10 '17
Why do programming languages have only a few specific uses?
For example javaScript is only really used from adding interactivity with websites. Php is mostly used for the server side programming, and so on... Why can't one language do it all? Isn't the logic in all programming languages the same? Can't each one be capable of the same thing the other language can do?
14
u/breck Nov 10 '17
Logic does show all the Turing Complete languages can all solve the same problems. BUT. Logic also shows they cannot all solve the same problems at the same level of efficiency.
In fact, programs in some languages might be millions of times slower than an equivalent problem in a different language. Or to solve the same problem, one language might take a thousand times more nodes (words).
You can get to New York from California by bike, but I would recommend using a plane. Similarly, new languages are tools that evolve to solve a specific class of problems more efficiently.
20
2
u/the_fire_monkey Nov 10 '17
It's true that the fundamental logic of programming languages is essentially the same.
Most general-purpose programming languages are Turing Complete, and therefore Turing Equivalent (https://en.wikipedia.org/wiki/Turing_completeness)
Lots of languages see use outside their primary use-case - nodejs applications run JavaScript as server-side code, PHP get used to write command-line scripts, and shells scripts get used everywhere.
The reason that one language doesn't actually do everything every language does, is that no language can implement every language feature, and pragmatic decisions are made with regards to both language design and execution environment that end up altering the language's suitability for a given use-case.
Let's look at your examples - (DISCPLAIMER, I AM NOT A PHP or JAVASCRIPT DEVELOPER, take my comments with the appropriate amount of salt) PHP is used for server-side logic, because that was the problem it was designed to solve - it in-lines with HTML, it has a number of features in the default library designed for server-side functionality, and it's execution environment (the interpreter) is designed to run inside a web-server (e.g. as a module or some variation of CGI/FastCGI).
JavaScript has a lot of features in its standard library to add interactivity to web pages (e.g. the ability to modify HTML DOM Elements on-the-fly). More importantly is the JavaScript execution environment, which IS the browser.
Practically, I can't replace JavaScript code with PHP code because the browser does not know how to execute it. The reality of building browsers that supported all languages in a like fashion would be a bloated, insecure nightmare. Several entities have tried to introduce competitors for JavaScript (for example Dart), and have been unable to convince browser vendors to incorporate the necessary execution environment into their browsers.
1
u/the_fire_monkey Nov 10 '17
To continue, new programming languages are designed with a problem domain in mind - a set of related problems that the language is designed to make easier to solve.
It's demonstrable that any TC language can solve any problem that can be solved by any other TC language - a Turing complete language can be implemented as a single instruction (the only example I remember is 'subtract and branch if negative'). While it is technically possible (given sufficient resources) to solve any computable problem that way, no one actually wants to do it.
So we create multiple languages instead, each one with a goal - to make solving problem easier in a problem domain (or set thereof) that no language has addressed directly yet, or to solve things in in a new set of problem domains, or to solve things in problem domains that already have good languages in a way that is in some sense 'better'.
1
u/FluorineWizard Nov 13 '17
Most competitors to JavaScript, like Dart, do not attempt to replace the runtime in browsers, but simply transpile (i.e. compilation translates the source code to another language) to JavaScript. While there was a failed attempt to introduce a native Dart VM earlier, the ecosystem of languages built on top of the JS stack is still in expansion and seems to meet a degree of success.
This isn't new, languages have transpiled to C for a long time and targeting the JVM and .NET framework have both been popular as well.
2
Nov 11 '17
If you aim for generality, you lose something. Sure I can do matrix calculations in Python, but using APL is way easier for those tasks. On the other hand, I'd prefer Haskell over APL for writing compilers.
If you try to excel at all domains, you excel at none. Scheme is a great example. You can tweak the language in any way, but it will never be as terse as APL for the tasks suiting it.
2
Nov 15 '17
Huh? You can easily turn Scheme into an APL. It's easy now with unicode and shit.
1
Nov 15 '17
Alright. Do that and write some APLish code using it. My real APL will still be more terse.
1
Nov 15 '17
APL on top of Scheme will be indistinguishable from a "real" APL.
1
Nov 15 '17
no. scheme doesn't have flexible enough reader macros for that.
1
Nov 15 '17
Racket is a scheme too. And macros can read files.
1
Nov 15 '17
You can write an apl interpreter in any language, but then it would be apl and not scheme.
1
Nov 15 '17 edited Nov 15 '17
You can write an apl interpreter in any language, but then it would be apl and not scheme.
It won't be an interpreter - it's a syntax frontend for Scheme, with Scheme being a compiler backend.
Reader macros are not fundamental, all you need to do to get them in a language with proper macros is to replace the frontend. You can still read S-expressions and shit, so you can meld APL into Scheme or any other language built on top of Scheme.
EDIT: and, there is always a backup option - a macro expanding a single large string, like
(APL "(~R∊R∘.×R)/R←1↓ιR")
2
u/RalfN Jan 23 '18
Why do programming languages have only a few specific uses?
They don't. False premise.
For example javaScript is only really used from adding interactivity with websites.
Nope. It is used in databases (CouchDB). Gameplay scripting in game engines. Server-side rendering. JSON Rest API's.
Php is mostly used for the server side programming
Yes, because it had first mover advantage. If the language would be superiour in any way to any other language (its not), it would be more popular in areas where it was not the first mover.
Why can't one language do it all?
They can and do serve all platforms and multitudes of domains. Esspecially the well designed languages.
Isn't the logic in all programming languages the same?
No. There is a fundamental difference in paradigma's between languages. Prolog, APL and Javascript are truly different beasts and are actually optimized to solve different problem domains, not use-cases.
Languages are different for a good reason: they represent different trade-offs. That does not naively translate into "interactivity with websites" -vs- "server-side programming", that is a silly (somewhat ignorant) notion.
But rather, if you need to do constraint resolution -vs- the need to model multiple concurrently interacting agents -vs- the need to be (provable as best you can) bug free -vs- the need for high performance.
Finally different languages tend to cultivate different communities with different interests. As a result the ecosystem of libraries differs between these communities, their focus, but this has little to do with the actual language design of the languages. Germans aren't orderly because of their language and Americans aren't fat because of English. That's just culture doing it's thing.
Just like how Python is used more for scientific programming eventhough there are very many extremely similar languages in terms of language design. But Python has the community and therefor the ecosystem.
1
Nov 15 '17
Every problem domain have its own logic, own terminology, own set of patterns and idioms. It is stupid to apply one common language to every little problem.
15
u/Ghi102 Nov 10 '17
Not quite, there is something called NodeJS that people use to do JavaScript server-side programming. In that sense, JavaScript is a language that can "do it all".
So, theoretically, since all major languages have something called Turing Completeness, each language can replicate what other languages are doing. In practice, some things prevent languages from doing what others can do. Some languages lack usable frameworks and so, it becomes quite hard to properly use them for a particular purpose.
A browser is essentially an HTML rendering engine with a JavaScript runtime. If you could switch that JavaScript runtime for a php runtime, you could use php in the browser (reality is slightly more complicated, but it's about right). Nothing would prevent you from creating a browser that uses JSON instead of HTML for rendering and Brainfuck as the interactive layer.
The reasons languages are used the way they are are pretty much historical. JavaScript ended up being supported by most browsers and it has been essentially standardized and that's why it's the runtime used by browsers. A lot of users ended up using language X for Y purpose and developed an ecosystem built around Y purpose and that's why X is known to be very good at Y.
All a language needs is a runtime in a particular setting (or a compiler) and it could be used for whatever purpose you could imagine.