Stackylogic is a programming language I made up in a previous challenge: Run Stackylogic. Read that post for full details and examples, but here is how it works paraphrased:
Stackylogic takes
0
's and1
's for input and outputs a single0
or1
upon completion.A program consists of lines that only contain the characters
01?
as well as exactly one<
at the end of one of the lines. Lines may not be empty and the line with the<
must have at least one0
,1
, or?
before it.Here's a sample program that computes the NAND of two bits:
1 ?< 11 ? 0
Every line in a program is considered a stack, with the bottom on the left and the top on the right. Implicitly, there is an empty stack (i.e. empty line) before the first line in a program and after the last line.
The
<
, called the cursor, marks the stack to start on when a program is run. Execution proceeds as follows:
Pop the top character off the stack the cursor is currently pointing to.
- If the character is
?
, prompt the user for a0
or a1
and act as if that was the character.- If the character is
0
, move the cursor one stack up (to the line above the current line).- If the character is
1
, move the cursor one stack down (to the line below the current line).If the stack the cursor moves to is empty, output the last value that was popped off a stack (always a
0
or1
), and end the program.Else, if the stack the cursor moves to is not empty, go back to step 1 and repeat the process.
The key thing to realize for this challenge is that all Stackylogic programs equate to a truth table. Some predetermined number of boolean values are input and exactly one boolean is deterministically output.
So your task is to produce a Stackylogic program that satisfies or simulates, i.e. has the same output as any given truth table. But it's not obvious that Stackylogic can simulate any truth table, so here's a proof by induction:
Base Case
The two 0-input truth tables are the tables that always output
0
or1
. The Stackylogic equivalents of these tables are0<
and1<
respectively.Inductive Step
Assume Stackylogic can simulate any N-input truth table. Let M = N + 1.
An M-input table, T, can be expressed as two N-input tables, T0 and T1, plus the additional input bit B. When B is 0, the result of T0 is used. When B is 1, the result of T1 is used.
For example, the 3-input truth table corresponding to the pseudocode
if B: result = x OR y else: result = x NAND y
is
B x y | result 0 0 0 | 1 0 0 1 | 1 0 1 0 | 1 0 1 1 | 0 1 0 0 | 0 1 0 1 | 1 1 1 0 | 1 1 1 1 | 1
which is really the two 2-input truth tables for NAND and OR stacked atop each other with the muxing bit B.
Let S0 and S1 be the Stackylogic programs that satisfy T0 and T1 respectively (we know these exist based on the first assumption). Program S that satisfies T can then be constructed as:
[lines of S0 excluding the cursor, with 0 appended to all lines below the cursor] ?< [lines of S1 excluding the cursor, with 1 appended to all lines above the cursor]
This arrangement effectively muxes between S0 and S1 based on the first input bit (from line
?<
). If it is0
, the cursor will ride the appended0
's up to the original cursor position of S0, which will then be bordered top and bottom by empty stacks, and thus run exactly identical to the original S0. Likewise, if1
is input, the cursor will ride the1
's down to S1's cursor position and proceed to execute it as if it were alone.For example, Stackylogic programs for OR and NAND are
? ?<
and
1 ?< 11 ? 0
They can be combined to simulate
if B: result = x OR y else: result = x NAND y
like so:
1 ? 110 ?0 00 0 ?< ?1 ?
Thus, any truth table can be simulated by a Stackylogic program.
Challenge
Write a program or function that takes in an N input truth table (N > 0) in the form of a list of 2N boolean values that represent the outputs of the table in ascending binary order.
Any reasonable input format is alright. e.g. for an OR truth table
x y | OR
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
any of these styles of inputs would be fine:
0111
0, 1, 1, 1
0
1
1
1
[False, True, True, True]
Print or return a Stackylogic program that satisfies the truth table, i.e. has the exact same output given the same input. Any finite program that satisfies that table is valid output. You do not need to follow the inductive proof's method of construction. The Stackylogic programs do not need to be optimally short.
For example, if the input were 11100111
, one valid output would be
1
?
110
?0
00
0
?<
?1
?
but there are many others.
The shortest code in bytes wins.
See the original Stackylogic challenge if you need an interpreter.