wuxing.go raw
1 // Package wuxing encodes the Wu Xing (五行) five-phase system.
2 //
3 // The five phases — Wood, Fire, Earth, Metal, Water — form two
4 // directed cycles:
5 //
6 // Shēng (相生, generating): Wood → Fire → Earth → Metal → Water → Wood
7 // Kè (相克, overcoming): Wood → Earth → Water → Fire → Metal → Wood
8 //
9 // Together these form a complete tournament graph on 5 nodes where
10 // every pair has a directed relationship. The generating cycle is
11 // the "mother-son" nourishing relationship; the overcoming cycle is
12 // the "grandfather-grandson" controlling relationship.
13 //
14 // The quinary system (base 5, the third prime) completes the
15 // mixed-radix state space: 2⁶ × 3⁴ × 5 = 64 × 81 × 5 = 25,920.
16 package wuxing
17
18 // Phase is one of the five phases.
19 type Phase uint8
20
21 const (
22 Wood Phase = 0 // 木 mù — growth, expansion, spring
23 Fire Phase = 1 // 火 huǒ — transformation, testing, summer
24 Earth Phase = 2 // 土 tǔ — stabilization, centering, late summer
25 Metal Phase = 3 // 金 jīn — refinement, contraction, autumn
26 Water Phase = 4 // 水 shuǐ — dissolution, storage, winter
27 )
28
29 // String returns the English name.
30 func (p Phase) String() string {
31 return [5]string{"Wood", "Fire", "Earth", "Metal", "Water"}[p]
32 }
33
34 // Chinese returns the Chinese character and pinyin.
35 func (p Phase) Chinese() (character string, pinyin string) {
36 chars := [5]string{"木", "火", "土", "金", "水"}
37 pins := [5]string{"mù", "huǒ", "tǔ", "jīn", "shuǐ"}
38 return chars[p], pins[p]
39 }
40
41 // Generates returns the phase this one generates (mother → son).
42 // Wood→Fire, Fire→Earth, Earth→Metal, Metal→Water, Water→Wood.
43 func (p Phase) Generates() Phase {
44 return (p + 1) % 5
45 }
46
47 // GeneratedBy returns the phase that generates this one (son ← mother).
48 func (p Phase) GeneratedBy() Phase {
49 return (p + 4) % 5
50 }
51
52 // Overcomes returns the phase this one controls (grandfather → grandson).
53 // Wood→Earth, Earth→Water, Water→Fire, Fire→Metal, Metal→Wood.
54 func (p Phase) Overcomes() Phase {
55 return (p + 2) % 5
56 }
57
58 // OvercomeBy returns the phase that controls this one.
59 func (p Phase) OvercomeBy() Phase {
60 return (p + 3) % 5
61 }
62
63 // Correspondence holds the associations of a phase across domains.
64 type Correspondence struct {
65 Phase Phase
66 Character string // Chinese character
67 Pinyin string // romanized pronunciation
68 Direction string // cardinal direction
69 Season string // seasonal association
70 Color string // associated color
71 YinOrgan string // zàng organ (solid)
72 YangOrgan string // fǔ organ (hollow)
73 Sense string // sense organ
74 Taste string // flavor
75 Emotion string // emotional correspondence
76 Climate string // weather pattern
77 Planet string // celestial body
78 Creature string // heavenly creature
79 }
80
81 // Correspondences returns the full correspondence table for all five phases.
82 func Correspondences() [5]Correspondence {
83 return correspondences
84 }
85
86 var correspondences = [5]Correspondence{
87 {
88 Phase: Wood, Character: "木", Pinyin: "mù",
89 Direction: "East", Season: "Spring",
90 Color: "Green", YinOrgan: "Liver", YangOrgan: "Gallbladder",
91 Sense: "Eyes", Taste: "Sour", Emotion: "Anger",
92 Climate: "Wind", Planet: "Jupiter", Creature: "Azure Dragon",
93 },
94 {
95 Phase: Fire, Character: "火", Pinyin: "huǒ",
96 Direction: "South", Season: "Summer",
97 Color: "Red", YinOrgan: "Heart", YangOrgan: "Small Intestine",
98 Sense: "Tongue", Taste: "Bitter", Emotion: "Joy",
99 Climate: "Heat", Planet: "Mars", Creature: "Vermilion Bird",
100 },
101 {
102 Phase: Earth, Character: "土", Pinyin: "tǔ",
103 Direction: "Center", Season: "Late Summer",
104 Color: "Yellow", YinOrgan: "Spleen", YangOrgan: "Stomach",
105 Sense: "Mouth", Taste: "Sweet", Emotion: "Pensiveness",
106 Climate: "Dampness", Planet: "Saturn", Creature: "Yellow Dragon",
107 },
108 {
109 Phase: Metal, Character: "金", Pinyin: "jīn",
110 Direction: "West", Season: "Autumn",
111 Color: "White", YinOrgan: "Lungs", YangOrgan: "Large Intestine",
112 Sense: "Nose", Taste: "Pungent", Emotion: "Grief",
113 Climate: "Dryness", Planet: "Venus", Creature: "White Tiger",
114 },
115 {
116 Phase: Water, Character: "水", Pinyin: "shuǐ",
117 Direction: "North", Season: "Winter",
118 Color: "Black", YinOrgan: "Kidneys", YangOrgan: "Bladder",
119 Sense: "Ears", Taste: "Salty", Emotion: "Fear",
120 Climate: "Cold", Planet: "Mercury", Creature: "Black Tortoise",
121 },
122 }
123
124 // Relation describes how two phases interact.
125 type Relation uint8
126
127 const (
128 Self Relation = 0 // same phase
129 Generating Relation = 1 // shēng: this generates other
130 Generated Relation = 2 // shēng: other generates this
131 Overcoming Relation = 3 // kè: this overcomes other
132 Overcome Relation = 4 // kè: other overcomes this
133 )
134
135 // Relate returns the relationship from phase a to phase b.
136 func Relate(a, b Phase) Relation {
137 if a == b {
138 return Self
139 }
140 if a.Generates() == b {
141 return Generating
142 }
143 if a.GeneratedBy() == b {
144 return Generated
145 }
146 if a.Overcomes() == b {
147 return Overcoming
148 }
149 return Overcome
150 }
151
152 // Lifecycle maps lattice operations to phases:
153 //
154 // Wood — growth: nucleation, topology expansion
155 // Fire — testing: validation, trial execution
156 // Earth — stabilization: integration, lock-in deepening
157 // Metal — refinement: pruning, coherence strengthening
158 // Water — dissolution: recycling, solution return
159 //
160 // The generating cycle (shēng) is the natural lifecycle:
161 //
162 // grow → test → stabilize → refine → dissolve → grow
163 //
164 // The overcoming cycle (kè) is the correction mechanism:
165 //
166 // growth corrects stagnation (Wood→Earth)
167 // stabilization corrects chaos (Earth→Water)
168 // dissolution corrects rigidity (Water→Fire)
169 // testing corrects excess (Fire→Metal)
170 // refinement corrects sprawl (Metal→Wood)
171 type Lifecycle struct {
172 Current Phase
173 }
174
175 // Next advances to the next phase in the generating (shēng) cycle.
176 func (l *Lifecycle) Next() Phase {
177 l.Current = l.Current.Generates()
178 return l.Current
179 }
180
181 // Correct jumps to the phase this one overcomes (kè correction).
182 func (l *Lifecycle) Correct() Phase {
183 l.Current = l.Current.Overcomes()
184 return l.Current
185 }
186
187 // FullState combines all three systems into one mixed-radix state.
188 // Binary (I Ching): 0-63, Ternary (Tao): 0-80, Quinary (Wu Xing): 0-4.
189 // Total state space: 64 × 81 × 5 = 25,920.
190 type FullState struct {
191 Hexagram uint8 // 0-63: I Ching binary state
192 Position uint8 // 0-80: Tao Te Ching ternary position
193 Phase Phase // 0-4: Wu Xing quinary phase
194 }
195
196 // Encode packs a FullState into a single uint16.
197 // hexagram + 64*position + 64*81*phase
198 func (fs FullState) Encode() uint16 {
199 return uint16(fs.Hexagram) + uint16(fs.Position)*64 + uint16(fs.Phase)*64*81
200 }
201
202 // DecodeFullState unpacks a uint16 into a FullState.
203 func DecodeFullState(v uint16) FullState {
204 hex := uint8(v % 64)
205 v /= 64
206 pos := uint8(v % 81)
207 v /= 81
208 phase := Phase(v % 5)
209 return FullState{Hexagram: hex, Position: pos, Phase: phase}
210 }
211