code wiki / _hdl_build / nx_math_genealogist.nx
nx_math_genealogist.nx source
↩ module page · 279 lines · 14085 B
1// nx_math_genealogist.nx -- the MATH-DOMAIN FAMILY-SEARCH (operator 2026-06-13:
2// "we need math and other types of genealogist family search beyond just the overall
3// family-search map ... so we can trace how the math scales and can be rebuilt without
4// floating"). The whole-ecosystem lineage.tsv is too coarse to answer "in what ORDER,
5// from the hardware rung up, do I rebuild the math engine with ZERO floating, and what
6// is each rung GRADED?". This organ is the domain-scoped genealogist for that question.
7//
8// It is organ-AUTHORED (not hand-drawn): it COMPOSES three live surfaces and computes
9// the family tree --
10// knowledge/specs/math_rungs.tsv -> the REBUILD RECIPE per function
11// (spec-emitter -> kernel-emitter -> test ->
12// oracle-gate -> f64-gate), the "how to rebuild"
13// knowledge/status/math_engine.log -> the GRADE per rung (last RUNGRUN rung=<n>
14// verdict), and the foundation grades
15// the two FOUNDATIONS (DATA below) -> ME0 nx_f64 (IEEE754 binary64 bits-up, the
16// hardware rung) and ME3 nx_bigfloat120 (the
17// 120-bit sovereign oracle every gate measures
18// against). EVERY function rests on both.
19//
20// The genealogy is a clean 3-layer rebuild DAG (verified, not asserted):
21// layer 0 ME0 nx_f64 (founds-on: - ) the hardware rung
22// layer 1 ME3 nx_bigfloat120 (founds-on: nx_f64) the oracle
23// layer 2 every function rung (founds-on: nx_f64, nx_bigfloat120)
24// FLOATING (math sense) = a rung GRADED GREEN that rests on a foundation NOT GREEN -- a
25// proven function standing on an unverified base (an impossible-to-rebuild claim). The
26// no-float LAW for math: zero floating => the whole engine is reconstructible bottom-up.
27//
28// Emits: knowledge/registry/math_genealogy.tsv (machine table, layer-ascending) + a
29// human REBUILD-ORDER tree to stdout + a MATHGENE coherence gate to
30// knowledge/status/math_genealogy.log. SELF-VALIDATING (no false-green): baked pos/neg
31// controls (green rung on green base -> grounded; green rung on a RED base -> floating;
32// grade-eval GREEN on green log / not-green on red) run BEFORE the real surfaces.
33//
34// module: nishi-core.math.genealogist
35// depends: nishi-core.sys.syscalls
36// capability: MATH_FAMILY_SEARCH_NO_FLOAT
37// license_tier: ORIGINAL
38import "nx_syscalls.nx"
39const MG_MAGIC_1048576: i64 = 1048576
40const MG_MAGIC_65536: i64 = 65536
41
42const MG_RUNGS: *u8 = "knowledge/specs/math_rungs.tsv"
43const MG_LOG: *u8 = "knowledge/status/math_engine.log"
44const MG_MAP: *u8 = "knowledge/registry/math_genealogy.tsv"
45const MG_GATE: *u8 = "knowledge/status/math_genealogy.log"
46
47func mg_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
48func mg_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
49func mg_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
50func mg_cpy(dst: *u8, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[i]=s[i]; i=i+1} dst[i]=0 as u8; return i }
51func mg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i]; i=i+1} return off+i }
52
53func mg_at(buf: *u8, n: i64, i: i64, pat: *u8, pl: i64) -> i64 {
54 if i + pl > n { return 0 }
55 var k: i64 = 0
56 while k < pl { if buf[i+k] != pat[k] { return 0 } k = k + 1 }
57 return 1
58}
59
60func mg_read(path: *u8, buf: *u8, cap: i64) -> i64 {
61 let fd: i64 = sys_openat_rd(path)
62 if fd < 0 { return 0 }
63 var n: i64 = 0
64 var r: i64 = sys_read(fd, buf, cap - 1)
65 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
66 sys_close(fd)
67 return n
68}
69
70// last line of <buf> containing <anchor> also contains "verdict=GREEN"? 1 / 0 / -1 absent.
71func mg_grade(buf: *u8, n: i64, anchor: *u8) -> i64 {
72 let al: i64 = mg_len(anchor)
73 var found: i64 = 0
74 var ls: i64 = 0
75 var fs2: i64 = 0
76 var fe2: i64 = 0
77 var i: i64 = 0
78 while i <= n {
79 var eol: i64 = 0
80 if i == n { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } }
81 if eol == 1 {
82 var j: i64 = ls
83 while j < i {
84 if mg_at(buf, n, j, anchor, al) == 1 { found = 1; fs2 = ls; fe2 = i; j = i } else { j = j + 1 }
85 }
86 ls = i + 1
87 }
88 i = i + 1
89 }
90 if found == 0 { return 0 - 1 }
91 var q: i64 = fs2
92 while q < fe2 {
93 if mg_at(buf, n, q, "verdict=GREEN" as *u8, 13) == 1 { return 1 }
94 q = q + 1
95 }
96 return 0
97}
98
99// copy tab-field f of line [ls,le) into dst (NUL-terminated). missing field -> empty.
100func mg_field(buf: *u8, ls: i64, le: i64, f: i64, dst: *u8, cap: i64) -> i64 {
101 var col: i64 = 0
102 var p: i64 = ls
103 var k: i64 = 0
104 while p < le {
105 if buf[p] == (9 as u8) {
106 if col == f { dst[k] = 0 as u8; return k }
107 col = col + 1
108 if col == f { k = 0 }
109 } else {
110 if col == f { if k < cap - 1 { dst[k] = buf[p]; k = k + 1 } }
111 }
112 p = p + 1
113 }
114 dst[k] = 0 as u8
115 return k
116}
117
118// math-sense float: a rung graded GREEN that rests on a base NOT all-GREEN is FLOATING.
119func mg_floating(grade_green: i64, base_all_green: i64) -> i64 {
120 if grade_green == 1 { if base_all_green == 0 { return 1 } }
121 return 0
122}
123
124func mg_grade_str(g: i64) -> *u8 {
125 if g == 1 { return "GREEN" as *u8 }
126 if g == 0 { return "RED" as *u8 }
127 return "ABSENT" as *u8
128}
129
130// ---- baked self-tests (pure in-memory; no false-green) ----
131func mg_selftest() -> i64 {
132 let g: *u8 = sys_mmap(256)
133 mg_cpy(g, "RUNGRUN rung=t stages=x verdict=GREEN\n")
134 if mg_grade(g, mg_len(g), "RUNGRUN rung=t " as *u8) != 1 { return 1 }
135 mg_cpy(g, "RUNGRUN rung=t verdict=GREEN\nRUNGRUN rung=t verdict=RED\n")
136 if mg_grade(g, mg_len(g), "RUNGRUN rung=t " as *u8) != 0 { return 2 }
137 mg_cpy(g, "nothing\n")
138 if mg_grade(g, mg_len(g), "RUNGRUN rung=t " as *u8) != (0 - 1) { return 3 }
139 // erf must NOT match erfc (trailing-space boundary)
140 mg_cpy(g, "RUNGRUN rung=erfc verdict=GREEN\n")
141 if mg_grade(g, mg_len(g), "RUNGRUN rung=erf " as *u8) != (0 - 1) { return 4 }
142 // float logic: green rung on green base = grounded; on red base = floating
143 if mg_floating(1, 1) != 0 { return 5 }
144 if mg_floating(1, 0) != 1 { return 6 }
145 if mg_floating(0, 0) != 0 { return 7 }
146 // field extractor
147 mg_cpy(g, "gamma\tnx_mathspec_gamma\tdemo\ttest\tog\tug\tkf\t5\n")
148 let fb: *u8 = sys_mmap(128)
149 mg_field(g, 0, mg_len(g), 1, fb, 128)
150 if mg_at(fb, mg_len(fb), 0, "nx_mathspec_gamma" as *u8, 17) != 1 { return 8 }
151 return 0
152}
153
154func main() -> i64 {
155 let epoch: i64 = sys_now_realtime_sec()
156 let st: i64 = mg_selftest()
157 if st != 0 {
158 mg_w(1, "MATHGENE verdict=RED reason=selftest-failed code=" as *u8); mg_wn(1, st); mg_w(1, "\n" as *u8)
159 let lf0: i64 = sys_openat_append(MG_GATE, 0x1a4)
160 if lf0 >= 0 { mg_w(lf0, "MATHGENE verdict=RED reason=selftest code=" as *u8); mg_wn(lf0, st); mg_w(lf0, "\n" as *u8); sys_close(lf0) }
161 sys_exit(10); return 10
162 }
163
164 let lbuf: *u8 = sys_mmap(MG_MAGIC_1048576)
165 let ln: i64 = mg_read(MG_LOG, lbuf, MG_MAGIC_1048576)
166 let rbuf: *u8 = sys_mmap(MG_MAGIC_65536)
167 let rn: i64 = mg_read(MG_RUNGS, rbuf, MG_MAGIC_65536)
168 if rn <= 0 { mg_w(1, "MATHGENE verdict=RED reason=no-rungs-table\n" as *u8); sys_exit(11); return 11 }
169
170 // ---- foundation grades (the two bases every function rests on) ----
171 let g_f64: i64 = mg_grade(lbuf, ln, "F64GATE " as *u8) // ME0 hardware rung
172 let g_bf: i64 = mg_grade(lbuf, ln, "BFREGEN " as *u8) // ME3 oracle
173 var base_all_green: i64 = 0
174 if g_f64 == 1 { if g_bf == 1 { base_all_green = 1 } }
175
176 let mfd: i64 = sys_openat_wr(MG_MAP, 0x1a4)
177 if mfd >= 0 {
178 mg_w(mfd, "# AUTHORED BY nx_math_genealogist -- math-domain family-search, rebuild order from the hardware rung up. layer 0 = ME0 nx_f64 (IEEE754 binary64 bits-up), layer 1 = ME3 nx_bigfloat120 (120-bit sovereign oracle), layer 2 = every function rung (founds on both). GRADE from math_engine.log; FLOAT = green rung on a non-green base (no-float law: zero).\n" as *u8)
179 mg_w(mfd, "# columns: layer\tid\tkind\tgrade\tfounds_on\tfloat\trebuild_chain\n" as *u8)
180 mg_w(mfd, "0\tnx_f64\tFOUNDATION-ISA\t" as *u8); mg_w(mfd, mg_grade_str(g_f64)); mg_w(mfd, "\t-\tgrounded\tIEEE754-binary64-RNE(add/sub/mul/div/sqrt/cvt)-bits-up-vs-hardware-oracle\n" as *u8)
181 mg_w(mfd, "1\tnx_bigfloat120\tFOUNDATION-ORACLE\t" as *u8); mg_w(mfd, mg_grade_str(g_bf)); mg_w(mfd, "\tnx_f64\t" as *u8)
182 if mg_floating(g_bf, g_f64) == 1 { mg_w(mfd, "FLOATING" as *u8) } else { mg_w(mfd, "grounded" as *u8) }
183 mg_w(mfd, "\tMachin-pi+Newton-sqrt+factor-division(consts-self-computed)\n" as *u8)
184 }
185
186 // human tree header
187 mg_w(1, "MATH FAMILY-SEARCH -- rebuild from the hardware rung up (no floating)\n" as *u8)
188 mg_w(1, " L0 ME0 nx_f64 grade=" as *u8); mg_w(1, mg_grade_str(g_f64)); mg_w(1, " (IEEE754 binary64 bits-up -- the hardware rung)\n" as *u8)
189 mg_w(1, " L1 ME3 nx_bigfloat120 grade=" as *u8); mg_w(1, mg_grade_str(g_bf)); mg_w(1, " founds-on nx_f64 (120-bit sovereign oracle every gate measures against)\n" as *u8)
190 mg_w(1, " L2 function rungs founds-on { nx_f64, nx_bigfloat120 }:\n" as *u8)
191
192 // ---- per-function rows from math_rungs.tsv ----
193 let name: *u8 = sys_mmap(128)
194 let f_spec: *u8 = sys_mmap(128)
195 let f_demo: *u8 = sys_mmap(128)
196 let f_test: *u8 = sys_mmap(128)
197 let f_og: *u8 = sys_mmap(128)
198 let f_ug: *u8 = sys_mmap(128)
199 let anchor: *u8 = sys_mmap(160)
200 let chain: *u8 = sys_mmap(640)
201 var funcs: i64 = 0
202 var greens: i64 = 0
203 var floats: i64 = 0
204 var ls: i64 = 0
205 var i: i64 = 0
206 while i <= rn {
207 var eol: i64 = 0
208 if i == rn { eol = 1 } else { if rbuf[i] == (10 as u8) { eol = 1 } }
209 if eol == 1 {
210 if i > ls {
211 mg_field(rbuf, ls, i, 0, name, 128)
212 if mg_len(name) > 0 {
213 funcs = funcs + 1
214 mg_field(rbuf, ls, i, 1, f_spec, 128)
215 mg_field(rbuf, ls, i, 2, f_demo, 128)
216 mg_field(rbuf, ls, i, 3, f_test, 128)
217 mg_field(rbuf, ls, i, 4, f_og, 128)
218 mg_field(rbuf, ls, i, 5, f_ug, 128)
219 // grade via the uniform per-rung RUNGRUN line (trailing space = boundary)
220 var ao: i64 = 0
221 ao = mg_cat(anchor, ao, "RUNGRUN rung=" as *u8); ao = mg_cat(anchor, ao, name); ao = mg_cat(anchor, ao, " " as *u8); anchor[ao] = 0 as u8
222 let gr: i64 = mg_grade(lbuf, ln, anchor)
223 if gr == 1 { greens = greens + 1 }
224 let fl: i64 = mg_floating(gr, base_all_green)
225 if fl == 1 { floats = floats + 1 }
226 // rebuild chain string
227 var co: i64 = 0
228 co = mg_cat(chain, co, "spec=" as *u8); co = mg_cat(chain, co, f_spec)
229 co = mg_cat(chain, co, " kernel=" as *u8); co = mg_cat(chain, co, f_demo)
230 co = mg_cat(chain, co, " test=" as *u8); co = mg_cat(chain, co, f_test)
231 co = mg_cat(chain, co, " ogate=" as *u8); co = mg_cat(chain, co, f_og)
232 co = mg_cat(chain, co, " ugate=" as *u8); co = mg_cat(chain, co, f_ug)
233 chain[co] = 0 as u8
234 // map row
235 if mfd >= 0 {
236 mg_w(mfd, "2\t" as *u8); mg_w(mfd, name); mg_w(mfd, "\tFUNCTION\t" as *u8)
237 mg_w(mfd, mg_grade_str(gr)); mg_w(mfd, "\tnx_f64,nx_bigfloat120\t" as *u8)
238 if fl == 1 { mg_w(mfd, "FLOATING" as *u8) } else { mg_w(mfd, "grounded" as *u8) }
239 mg_w(mfd, "\t" as *u8); mg_w(mfd, chain); mg_w(mfd, "\n" as *u8)
240 }
241 // human tree leaf
242 mg_w(1, " - " as *u8); mg_w(1, name)
243 mg_w(1, " grade=" as *u8); mg_w(1, mg_grade_str(gr))
244 if fl == 1 { mg_w(1, " [FLOATING]" as *u8) } else { mg_w(1, " grounded" as *u8) }
245 mg_w(1, " via " as *u8); mg_w(1, f_spec); mg_w(1, "->" as *u8); mg_w(1, f_demo); mg_w(1, "->gate\n" as *u8)
246 }
247 }
248 ls = i + 1
249 }
250 i = i + 1
251 }
252 if mfd >= 0 { sys_close(mfd) }
253
254 // ---- coherence gate ----
255 var ok: i64 = 1
256 if base_all_green != 1 { ok = 0 }
257 if greens != funcs { ok = 0 }
258 if floats != 0 { ok = 0 }
259 var p2: i64 = 0
260 while p2 < 2 {
261 var fd: i64 = 1
262 if p2 == 1 { fd = sys_openat_append(MG_GATE, 0x1a4) }
263 if fd >= 0 {
264 mg_w(fd, "MATHGENE authored=organ foundations=" as *u8)
265 mg_w(fd, "f64:" as *u8); mg_w(fd, mg_grade_str(g_f64))
266 mg_w(fd, ",bigfloat:" as *u8); mg_w(fd, mg_grade_str(g_bf))
267 mg_w(fd, " functions=" as *u8); mg_wn(fd, funcs)
268 mg_w(fd, " green=" as *u8); mg_wn(fd, greens)
269 mg_w(fd, " floating=" as *u8); mg_wn(fd, floats)
270 if ok == 1 { mg_w(fd, " rebuildable_no_float=YES" as *u8) } else { mg_w(fd, " rebuildable_no_float=NO" as *u8) }
271 mg_w(fd, " epoch=" as *u8); mg_wn(fd, epoch)
272 if ok == 1 { mg_w(fd, " verdict=GREEN\n" as *u8) } else { mg_w(fd, " verdict=RED\n" as *u8) }
273 if p2 == 1 { sys_close(fd) }
274 }
275 p2 = p2 + 1
276 }
277 if ok == 1 { sys_exit(0) } else { sys_exit(1) }
278 return 0
279}