sourcedemos/ca-lab/ca-lab.xtl
1⍝!/usr/bin/env xetal
2⍝# A cellular automata lab. Every rule here is a lookup table: each
3⍝# cell's neighborhood becomes a number, by rotations and arithmetic
4⍝# over the whole array, and the number picks the cell's next state
5⍝# from the table (s_elect). Changing the rule is changing the table.
6⍝# Run it with "xetal run demos/ca-lab/ca-lab.xtl"; it prints Rule 30
7⍝# and Rule 90 as sixteen-generation boards, Life, Brian's Brain and
8⍝# Wireworld, each drawn with characters. The live page lets you edit
9⍝# the table and the board, one and two dimensions each their own
10⍝# view: https://softwarewrighter.github.io/X_eTaL-demos/ca-lab/
11
12⍝# -- one dimension: Wolfram's elementary rules.
13⍝# u:b_its rule: a rule number's 8 bits are the table: the next state
14⍝# for each of the 8 neighborhoods (left, center, right), numbered
15⍝# 4 * left + 2 * center + right.
16ᵘb̲its ← { rule → (rule d̲iv 2 ^ o̲ffsets 8) m̲od 2 }
17⍝# u:r_ow tbl r: the next row. Each cell's neighborhood number, by
18⍝# rotation, looks up its next state in the table (1 + because
19⍝# s_elect counts from 1).
20ᵘr̲ow ← { tbl r → (1 + (4 × -1 o̲- r) + (2 × r) + 1 o̲- r) s̲elect tbl }
21⍝# u:g_row tbl h: the history grows downward: rotate the rows up one,
22⍝# and put the next row (of the last one) at the bottom. Start with
23⍝# every row the first.
24ᵘg̲row ← { tbl h →
25 g ← f̲irst s̲hape h
26 n ← 2 s̲elect s̲hape h
27 keep ← 1 × ((o̲ffsets g) < g − 1) 'l̲eft t̲able o̲ffsets n
28 (keep × 1 o̲-₁ h) + (1 − keep) × (g c̲at n) r̲eshape tbl ᵘr̲ow g s̲elect h
29}
30
31⍝## Two dimensions: a table on (state, neighbors in state 1)
32
33⍝# u:c_ount b: each cell's number of neighbors in state 1 (alive,
34⍝# firing, an electron's head): nine rotated copies of b = 1 summed,
35⍝# minus the cell itself.
36ᵘc̲ount ← { b → ('+ r̲/₁₂ -1 0 1 o̲-₁₂ 1 × b = 1) − 1 × b = 1 }
37⍝# u:l_ook tbl b: the next state from a table with a row per state and
38⍝# a column per count: each cell's number is its state * 9 plus its
39⍝# count of live neighbors.
40ᵘl̲ook ← { tbl b → (1 + (9 × b) + ᵘc̲ount b) s̲elect tbl }
41⍝ -- end of the core ---------------------------------------------------
42
43⍝ Rule 30 from one cell: 16 generations of a 33-cell row.
44tbl ← ᵘb̲its 30
45tbl
46r0 ← 1 × (o̲ffsets 33) = 16
47h ← 15 '{ h → tbl ᵘg̲row h } p̲ower (16 c̲at 33) r̲eshape r0
48(1 + h) s̲elect ".#"
49
50⍝ Rule 90: the Sierpinski triangle.
51tbl ← ᵘb̲its 90
52h ← 15 '{ h → tbl ᵘg̲row h } p̲ower (16 c̲at 33) r̲eshape r0
53(1 + h) s̲elect ".#"
54
55⍝ Life as a table: rows are the states (dead, alive), columns the count
56⍝ of live neighbors (0 to 8). Born with 3, survives with 2 or 3.
57life ← 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0
58glider ← 6 6 r̲eshape 0 1 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
59(1 + 4 '{ b → life ᵘl̲ook b } p̲ower glider) s̲elect ".#"
60
61⍝ Brian's Brain: off (0), firing (1), dying (2). Firing always dies,
62⍝ dying always goes off, off fires with exactly 2 firing neighbors.
63brain ← 0 0 1 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0
64b0 ← 6 6 r̲eshape 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
65(1 + 3 '{ b → brain ᵘl̲ook b } p̲ower b0) s̲elect ".#o"
66
67⍝ Wireworld: empty (0), electron head (1), tail (2), conductor (3).
68⍝ Heads become tails, tails conductor; a conductor becomes a head with
69⍝ 1 or 2 heads around it. An electron runs along a wire.
70wire ← 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 1 1 3 3 3 3 3 3
71w0 ← 3 8 r̲eshape 0 0 0 0 0 0 0 0 2 1 3 3 3 3 3 3 0 0 0 0 0 0 0 0
72(1 + 3 '{ b → wire ᵘl̲ook b } p̲ower w0) s̲elect " @~-"