sourcedemos/life-microscope/life-microscope.xtl
1⍝!/usr/bin/env xetal
2⍝# Conway's Life under the microscope: the whole rule is one line,
3⍝# u:l_ife, and every array it builds on the way is shown here, for a
4⍝# glider on a 6 by 6 board (the edges wrap around). Run it with
5⍝# "xetal run demos/life-microscope/life-microscope.xtl"; it prints
6⍝# the board, the nine shifted copies' shape, their sum, the next
7⍝# board, and the glider four generations on (moved one cell down and
8⍝# to the right). The live page draws these same arrays as it runs:
9⍝# https://softwarewrighter.github.io/X_eTaL-demos/life-microscope/
10
11⍝## The rule
12
13⍝# u:l_ife board: the next generation of a 0/1 board, read right to
14⍝# left. Rotating ⍵ by every offset in -1 0 1 along both axes and
15⍝# summing gives S, each cell plus its eight neighbors (it counts the
16⍝# cell itself too, so "2 or 3 neighbors" becomes "S = 3, or alive and
17⍝# S = 4"); a cell lives next when S is 3, or when it is alive and S
18⍝# is 4.
19ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ }
20
21⍝## A glider, one step at a time
22
23⍝# b: a glider in a 6 by 6 board.
24b ← 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
25b
26
27⍝# r: the board rotated by every offset -1 0 1 along both axes: a 3 by
28⍝# 3 arrangement of shifted boards, one for each neighbor (and the
29⍝# cell itself). In the copy for offsets (dy, dx), each cell holds its
30⍝# neighbor at (dy, dx).
31r ← -1 0 1 o̲-₁₂ b
32s̲hape r
33
34⍝# s: the nine copies added over the two offset axes: S, each cell
35⍝# plus its eight neighbors.
36s ← '+ r̲/₁₂ r
37s
38
39⍝ A cell is alive next if S is 3 (born, or alive with 2 neighbors) ...
40s = 3
41⍝ ... or if it is alive and S is 4 (alive with 3 neighbors).
42b × s = 4
43
44⍝ Together, the next board: what the one line of u:l_ife computes.
45(s = 3) + b × s = 4
46ᵘl̲ife b
47
48⍝ Four steps later the glider has moved one cell down and to the
49⍝ right. A superscript on a function repeats it: u:l_ife^4 b is the
50⍝ same as this p_ower call, written out for a count the page computes.
514 'ᵘl̲ife p̲ower b