Skip to content
Handbook navigation

Chapter 1 · Java

Java: The Dream of Thinking Machines

Run this example

Edit and run the java example

Change the source and run it here. Use the output panel to compare your result with the expected highlights below.

Unlock execution

Output

Run the code to see output here.

Java - Four Thinking Machines in One Program

Nothing in it is random and every number is formatted with Locale.ROOT, so its output is byte-for-byte the same as the Python version's. It runs in a fraction of a second.


How the program is organized

public class ThinkingMachines {
    // Part 1: the six-rule adder, keyed by "state|symbol"
    record Action(String write, int move, String next) {}
    static final Map<String, Action> INCREMENT = Map.of(...);
    static Result runMachine(String word, boolean trace) { ... }   // tape is a TreeMap<Integer, String>

    // Part 2: threshold units; theta2 = 2 * threshold keeps every comparison in integers
    static int fires(int w1, int w2, int theta2, int x1, int x2) { ... }

    // Part 3: ELIZA-style rules: rank, decomposition pattern, reassembly template
    record Rule(int rank, Object[] decomposition, String reassembly) {}
    static List<List<String>> match(Object[] items, int at, List<String> tokens) { ... }

    // Part 4: enumeration sizes and failure-free-run bounds
    static long runsNeeded(double rate, double confidence) { ... }
    static double upperBound(int runs, double confidence) { ... }
}

Three details keep the two languages in step. The tape is a sorted map, so cells print left to right exactly as Python's sorted(tape) does. Thresholds are stored doubled, so no floating-point comparison can differ between the runtimes. The pattern matcher tries the shortest match for each * first in both versions, so both pick the same decomposition when several would fit.

A Java service would not embed ELIZA, but Part 4 is ordinary production code: a release gate that takes the number of clean evaluation runs and the target failure rate, and refuses to promote an agent until runsNeeded is met.


Expected output

=== Part 1: A Turing machine that adds one (rule table of 6 entries) ===
step  state  read  write  move  next    tape after the step
   0  right                             [1] 0  1  1
   1  right     1      1     R  right    1 [0] 1  1
   2  right     0      0     R  right    1  0 [1] 1
   3  right     1      1     R  right    1  0  1 [1]
   4  right     1      1     R  right    1  0  1  1 [_]
   5  right     _      _     L  carry    1  0  1 [1] _
   6  carry     1      0     L  carry    1  0 [1] 0  _
   7  carry     1      0     L  carry    1 [0] 0  0  _
   8  carry     0      1     -  done     1 [1] 0  0  _

input        value  output        value  steps  n + k + 2
1011            11  1100             12      8          8
111              7  1000              8      8          8
0                0  1                 1      3          3
1001             9  1010             10      7          7
1111111111    1023  10000000000    1024     22         22
(n = input length, k = trailing 1s: walk right, turn, carry, write.)

=== Part 2: Threshold units (McCulloch and Pitts, 1943) ===
unit     w1  w2  theta   (0,0) (0,1) (1,0) (1,1)
AND       1   1    2.0      0     0     0     1
OR        1   1    1.0      0     1     1     1
NAND     -1  -1   -1.5      1     1     1     0
NOT x1   -1   0   -0.5      1     1     0     0
Searched 1,323 settings (weights -3..3, threshold -6.5..6.5 in steps of 0.5).
Two-input functions computed by at least one setting: 14 of 16
Settings that compute XOR: 0   XNOR: 0
XOR from two layers: hidden OR and NAND, then AND
  x=(0,0)  OR=0  NAND=1  XOR=0
  x=(0,1)  OR=1  NAND=1  XOR=1
  x=(1,0)  OR=1  NAND=1  XOR=1
  x=(1,1)  OR=1  NAND=0  XOR=0

=== Part 3: Four ELIZA-style rules ===
> Men are all alike.
  IN WHAT WAY
> They're always bugging us about something or other.
  CAN YOU THINK OF A SPECIFIC EXAMPLE
> Well, my boyfriend made me come here.
  YOUR BOYFRIEND MADE YOU COME HERE
> He says I'm depressed much of the time.
  I AM SORRY TO HEAR YOU ARE DEPRESSED
> My gloop made me frimble.
  YOUR GLOOP MADE YOU FRIMBLE
> I am deeply sick of purple arithmetic.
  I AM SORRY TO HEAR YOU ARE SICK
> The quarterly figures look fine.
  PLEASE GO ON

=== Part 4: What testing can certify ===
Inputs to check if every case is enumerated:
  three true/false facts                                    8
  one 32-bit integer                            4,294,967,296
  100 tokens from a 128,256-token vocabulary         10^510.8
Failure-free runs needed to rule out a failure rate above the target:
target   95% confidence   99% confidence   rule of three (3/p)
10%                  29               44                    30
1%                  299              459                   300
0.1%              2,995            4,603                 3,000
0.01%            29,956           46,050                30,000
Upper bound on the failure rate after zero failures (95% confidence):
    115 clean runs  tau-bench retail test tasks     2.57%
    500 clean runs  SWE-bench Verified tasks        0.60%
  3,000 clean runs  an in-house regression suite    0.10%
Java — The Dream of Thinking Machines | KnowledgeOS