Parentheses only is an esolang which consists only of parentheses.
All parentheses must be balanced. Other characters are ignored. Pair of balanced parentheses represents a group. Group can be either empty, or can contain other groups (concatenated).
For example, this is a valid program:
Global group is a group that is not inside any other group. This program has three global groups:
- If there are no global groups, then halt the program.
- If the first global group is empty and it is the only global group, then halt the program.
- If the first global group is empty and the second global group is also empty, then remove one of them.
- If the first global group is empty and the second global group is non-empty, then swap them.
- If the first global group has exactly one element, then replace the first global group with the content of that element (so, fo example,
((()()))will be replaced with
- If the first global group has two or more elements and it is the only global group, then halt the program.
If none of the above is true, select the first two global groups (we color the first one green and the second one blue):
(()((())())) (()()(())) ()
Let's call the first one
A and the second one
B. First, we remove
We first select the first element of
A (let's call it
C and color it red):
Now, we remove
C and then we remove parentheses of
Finally, for each remaining element of
A (the groups that are left green after removing
C and parentheses of
A), we recursively substitute
B. How substitution works is explained in the next paragraph.
Do all these steps iteratively until the program halts.
Given a group
X, we substitute
Z according to the following five rules (apply the first one that can be applied):
Y, replace it with
Xis empty, leave it untouched
Xhas exactly one element, then substutute
Zin that element
- If the first element of
Y, leave it untouched
- In each element of
Xstarting from the second element, substutute
Back to the example
Currently we have:
Now, we want to substutute
C (which is
B (which is
(()()(()))) in all elements that are green (and that is only one element
So, we want to substitute
((())()). We read the five rules and try to apply the first one that can be applied. The first rule cannot be applied, because
((())()) is not
(). The second rule cannot be applied, because
((())()) is not empty. The third rule also cannot be applied, because
((())()) does not have exactly one element. Fourth rule cannot be applied, because the first element of
(()), which is not equal to
(). Finally, we can apply the fifth rule, which says that in each element of
((())()) starting from the second element (that is
() only) we substitute
(()()(())). So, in order to substitute
() we can apply the first rule, so we obtain:
We continue, because we have two global groups. Now, we mark new
(()()(()))). We apply rule 5, so we substitute
()(()). On the first one we apply rule 2 and on the second one we apply rule 1. Now we have:
There is only one global group and it has three elements, so the program halts here.
Input is in the same format as the source code. We simply concatenate the input to the source code when the program starts. Both source code and the input independently must have balanced parentheses. The output is the final state of the program that cannot be reduced further.