programdemos/langtons-ant/langtons-ant.xtl

Langton's ant: on a white cell it turns right, on a black cell left; it flips the cell and steps forward. From two rules comes chaos, and then, after about 10000 steps, a "highway" the ant builds forever. Here the ant is arrays: its position is a mask with a single 1, its direction a number; no indexing, no loops, just whole-array steps. Run it with "xetal run demos/langtons-ant/langtons-ant.xtl"; it prints the state's shape, the board drawn with characters (black, white, the ant), and how many cells are black. The live page runs it frame by frame and pauses when the highway forms: https://softwarewrighter.github.io/X_eTaL-demos/langtons-ant/

source

rows : Int

value · line 15

The board's size and the number of steps.

rows ← 21
Used in: row, col, a0

cols : Int

value · line 16
cols ← 21
Used in: row, col, a0

steps : Int

value · line 17
steps ← 400
Used in: %2

One step of the ant

ᵘs̲tep : (Int, Int, Int) -> (Int, Int, Int)

function · line 30

u:s_tep (b, a, d): the state is a tuple, the board (0 white, 1 black), the ant's mask (1 where it stands), and its direction (0 up, 1 right, 2 down, 3 left), taken apart by the pattern and given back as a new tuple (D7, landed); d is a plain scalar now, not a plane carrying the same number everywhere. cell looks up the color under the ant (the board times its mask, summed); turn is right (+1) on white, left (+3, that is -1) on black; flip turns the cell's color over where the mask is 1; dy, dx move the mask one row or column, by its new direction.

ᵘs̲tep ← { (b, a, d) →
  cell ← '+ r̲/ r̲avel a × b
  turn ← (d + 1 + 2 × cell) m̲od 4
  flip ← b + a × 1 − 2 × b
  dy ← (1 × turn = 0) − 1 × turn = 2
  dx ← (1 × turn = 3) − 1 × turn = 1
  step ← dy o̲-₁ dx o̲-₂ a
  (flip, step, turn)
}
Used in: %2

row : Int

value · line 42
row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
Used in: b0, a0

col : Int

value · line 43
col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols
Used in: a0

b0 : Int

value · line 44
b0 ← 0 × row
Used in: %2

a0 : Int

value · line 45
a0 ← 1 × (row = rows d̲iv 2) ∧ col = cols d̲iv 2
Used in: %2

%2 : Int

value · line 46
(b, a, d) ← steps 'ᵘs̲tep p̲ower (b0, a0, 0)

b : Int

value · line 46
(b, a, d) ← steps 'ᵘs̲tep p̲ower (b0, a0, 0)

a : Int

value · line 46
(b, a, d) ← steps 'ᵘs̲tep p̲ower (b0, a0, 0)

d

value · line 46
(b, a, d) ← steps 'ᵘs̲tep p̲ower (b0, a0, 0)
Used in: ᵘs̲tep, %2