1 // Package tao encodes the Tao Te Ching as a ternary state system.
2 //
3 // The 81 chapters form a 3⁴ state space across four axes:
4 //
5 // Axis 0: Form — 形 xíng (empty / becoming / manifest)
6 // Axis 1: Function — 用 yòng (passive / adaptive / active)
7 // Axis 2: Meaning — 意 yì (implicit / emergent / explicit)
8 // Axis 3: Bond — 結 jié (dissolved / forming / crystallized)
9 //
10 // Each axis has three values (0, 1, 2), giving 3⁴ = 81 positions.
11 // The ternary encoding extends the binary I Ching with a middle
12 // ground — the "becoming" state between yin and yang.
13 package tao
14 15 // Trit is a ternary digit: 0, 1, or 2.
16 type Trit uint8
17 18 const (
19 Yin Trit = 0 // 陰 — receptive, empty, passive
20 Becoming Trit = 1 // 化 — transitional, becoming, adaptive
21 Yang Trit = 2 // 陽 — creative, manifest, active
22 )
23 24 // Axis identifies one of the four ternary dimensions.
25 type Axis uint8
26 27 const (
28 AxisForm Axis = 0 // 形 xíng — structural manifestation
29 AxisFunction Axis = 1 // 用 yòng — operational mode
30 AxisMeaning Axis = 2 // 意 yì — semantic clarity
31 AxisBond Axis = 3 // 結 jié — attachment strength
32 )
33 34 // Position is a point in the 81-state ternary space.
35 // Encoded as form + 3*function + 9*meaning + 27*bond.
36 type Position uint8
37 38 // Encode computes the position from four trits.
39 func Encode(form, function, meaning, bond Trit) Position {
40 return Position(form) + Position(function)*3 + Position(meaning)*9 + Position(bond)*27
41 }
42 43 // Decode unpacks a position into its four trits.
44 func (p Position) Decode() (form, function, meaning, bond Trit) {
45 v := uint8(p)
46 form = Trit(v % 3)
47 v /= 3
48 function = Trit(v % 3)
49 v /= 3
50 meaning = Trit(v % 3)
51 v /= 3
52 bond = Trit(v % 3)
53 return
54 }
55 56 // Trit returns the value along one axis.
57 func (p Position) Trit(axis Axis) Trit {
58 return Trit((uint8(p) / pow3(uint8(axis))) % 3)
59 }
60 61 // Set returns a new position with the given axis set to the given trit.
62 func (p Position) Set(axis Axis, t Trit) Position {
63 f, fn, m, b := p.Decode()
64 switch axis {
65 case AxisForm:
66 f = t
67 case AxisFunction:
68 fn = t
69 case AxisMeaning:
70 m = t
71 case AxisBond:
72 b = t
73 }
74 return Encode(f, fn, m, b)
75 }
76 77 // Advance moves one axis one step: 0→1, 1→2, 2→0 (cyclic).
78 func (p Position) Advance(axis Axis) Position {
79 t := (p.Trit(axis) + 1) % 3
80 return p.Set(axis, t)
81 }
82 83 // Retreat moves one axis back one step: 0→2, 1→0, 2→1 (cyclic).
84 func (p Position) Retreat(axis Axis) Position {
85 t := (p.Trit(axis) + 2) % 3
86 return p.Set(axis, t)
87 }
88 89 func pow3(n uint8) uint8 {
90 r := uint8(1)
91 for i := uint8(0); i < n; i++ {
92 r *= 3
93 }
94 return r
95 }
96 97 // Chapter holds one of the 81 chapters of the Tao Te Ching.
98 type Chapter struct {
99 Number int // 1-81 (King Wen traditional order)
100 Position Position // ternary encoding (0-80)
101 Chinese string // original Chinese text
102 Translation string // fresh English translation
103 }
104 105 // Chapters returns all 81 chapters with Chinese text and fresh translation.
106 func Chapters() [81]Chapter {
107 return chapters
108 }
109 110 // ChapterAt returns the chapter at the given ternary position.
111 func ChapterAt(p Position) Chapter {
112 if p > 80 {
113 return Chapter{}
114 }
115 return chapters[p]
116 }
117