Is JMESPath Turing Complete? #150
-
OverviewI have been asked on Gitter whether JMESPath is Turing Complete? This got me interested in its own right. I’ve started this discussion to prompt the community to chime in and provide pointers and insights as I have actually no idea where to start 😱 Edit: it turns out I do, in fact, have some idea where to start. Maybe this post from the discussion is a bit too abstract or literal. A more promising thought experiment might be to rely on λ-calculus. See that post later in the discussion. From this answer, criteria for Turing completeness look like so: To check to see if something is Turing complete, see if you can implement a Turing machine inside it. In other words, check to see if it can simulate the following:
So Is JMESPath Turing Complete? Reading and writing arbitrary dataThe original JMESPath specification did not have any way to refer to arbitrary lexical scope. This JMESPath Community effort actually includes the I suspect this goes a long way towards ticking that box. Now that I think about this, maybe the fact that third-party functions can be registered – although it has not been included in the current version yet – would be a kind of escape mechanism and would allow us to do virtually anything. As for writing arbitrary data, I think that In a Turing Macine, the tape acts as both the input and output. The states, their transitions and associates conditions and instructions are encoded in the machine itself. In the case of JMESPath, this would be the expression itself. I think one of the key requirements is to be able to write into a given "cell" of the Turing machine state. While JMESPath can produce arbitrary data thanks to the two constructs mentioned above, I think a more powerful equivalent would be to hold some state and be able to write to it. JMESPath currently does not have such ability. However, a potential future Indeed, JMESPath has an internal scope object that can be initialized to an arbitrary object using the let function. The reduce function builds upon this concept and has the ability to hold a accumulator object that can be read and written to based upon some expression that is evaluated with inputs from a specified array. Maybe we need to have a more generic mechanism to access and change in the internal scope independently from the reduce function? Ability to simulate moving the read/write headJMESPath does have comparison operators and is also capable of branching so I suspect this sort of ticks this box. Ability to simulate a finite state machineI suspect that – and the next item – will be the most challenging item to cover. In essence, we need to be able to go back and perform some processing on existing "data". Basically, a first challenge would be able to create a simple counter up to a specified value for instance. Armed with an hypothetical
The expression therefore returns Note: While most examples from this page are using hypothetical functions, the reduce function is indeed implemented in the preview page and you can try this specific example there. The first argument array A "halt" stateReferences
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Maybe a more promising thought experiment is to rely on the fact that λ-calculus is capable of simulating a Turing Machine. In other words, a functional programming language is Turing complete [citation required] I have actually no idea what I’m talking about 🙊. JMESPath is already a sort of functional language. It has the built-in ability to call functions. Therefore I think adding the ability to define your own functions in a JMESPath instruction would get us most of the way towards Turing completeness. Assuming a new function like so:
We could write the following expression:
This example does not actually need the Notes:
ReferencesA Byte of Code – September 04 2022 – Why functions are Turing complete (Lambda Calculus) – YouTube |
Beta Was this translation helpful? Give feedback.
-
The consensus is that a DSL – as JMESPath is – can still be extremely useful without being Turing complete. In fact, conscious decision to avoid Turing completeness might be the way to provide more guarantees without necessarily sacrificing features. In its current state, JMESPath seems to be definitely not Turing complete. However, it is well possible that allowing registration of third-party functions is only what’s required to bootstrap Turing completeness. However, we will try and make sure we do not unnecessarily introduce complexity within the bounds of the language and the built-in function library. |
Beta Was this translation helpful? Give feedback.
The consensus is that a DSL – as JMESPath is – can still be extremely useful without being Turing complete. In fact, conscious decision to avoid Turing completeness might be the way to provide more guarantees without necessarily sacrificing features.
In its current state, JMESPath seems to be definitely not Turing complete.
I would wager a guess that JMESPath Community still is not Turing complete either.
However, it is well possible that allowing registration of third-party functions is only what’s required to bootstrap Turing completeness. However, we will try and make sure we do not unnecessarily introduce complexity within the bounds of the language and the built-in function library.