JavaScript Is Turing Complete—Explained
JavaScript Is Turing Complete—Explained
If you start learning functional programming in JavaScript, you’ll probably hear about lambda calculus, Turing machine, Turing complete, and somehow that JavaScript is Turing complete. Let's dig into that one a bit and talk about it what it really means.
Join the DZone community and get the full member experience.
Join For FreeDeploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.
If you start learning functional programming in JavaScript, you’ll probably hear about lambda calculus, Turing machine, Turing complete, and somehow "JavaScript is Turing complete."
But, no one seems to explain, in simple terms, what it actually means. What’s the relation between a Turing "machine" and the JavaScript "language"? Also, most people use jargon to explain jargon like so:
In computability theory, a system of data-manipulation rules (such as a computer’s instruction set, a programming language, or a cellular automaton) is said to be Turing complete or computationally universal if it can be used to simulate any single-taped Turing machine. The concept is named after English mathematician Alan Turing. A classic example is lambda calculus.
So, this is my attempt at explaining these concepts simply.
Turing Machines
Back in the day, people wanted to know how to create a machine that could do all of the calculations they were doing by hand. They wanted to know how to build such a machine and how it might work.
Alan Turing came up with a hypothetical machine that could take any program of any complexity and run it. It could be implemented using a simple tape, a head that moves left and right, could store data by reading, writing, and erasing the contents of square cells. Given long enough tape and enough time, it could compute any program.
In other words, he explained how someone can build a computer. And called the computer a "Turing machine."
Trivia: Back in Alan Turing’s days, the word “Computer” meant the person who manually computes programs (not the machines). :-) |
So Powerful, Yet So Simple
Turing machines soon became very popular, and eventually a standard because, while they provided a powerful mechanism to calculate anything, they were also easy to understand. As described in the video below, Turing machines use a tape to keep track of states and run computations.
"Single" vs. "Multi" Tape Turing Machines
One other jargon you’ll hear about Turing machines is the concept of “single” tape.
The initial version of the Turing machine had just a long single tape. Later on, people came up with the concept of “multiple” tape Turing machines that used two to five tapes. Multi-tape Turing machines were not any more powerful than single-tape ones, but they helped to simplify programs.
So, explicitly saying “single” tape isn’t necessary.
This video shows that a multi-tape Turing Machine can be implemented in a single-tape Turing Machine.
A calculator is a good example of a Turing incomplete machine because it can only perform a small pre-defined subset of calculations.
However a home computer (Mac or a PC) is a Turing complete machine because it can do any calculation that a Turing machine can do if we give it enough memory and time.
"JavaScript Is Turing Complete"
If you think about it, a Turing machine is just a concept—it means that any "thing"(physical or virtual) that takes any program and runs it is essentially a Turing Machine. And if that "thing" can run every program that a "Turing Machine" can run, then it is called "Turing Complete."
Now, if you think about any modern programming language, they also take programs (written by us) as input and run them. Further, any program that can be theoretically written to run for a Turing machine can also be written in JavaScript. Thus, JavaScript is Turing complete.
That’s it!
If you like this post, please 1. click the like button and 2. share it on Twitter. You may retweet my tweet below:
Thanks for reading!
Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.
Published at DZone with permission of Raja Rao , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}