code wiki / _hdl_build / nx_primitive_miner_gate.nx
nx_primitive_miner_gate.nx source
↩ module page · 156 lines · 10251 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_primitive_miner_gate.nx -- CLOSE THE BRIDGE: retrieval -> TRANSLATION -> select. The prior gate
4// (nx_grammar_expand) selected a new primitive from a CURATED pool; the only remaining gap was where the pool
5// comes from = live RETRIEVAL (a search engine) + TRANSLATION (turn a retrieved doc into a candidate primitive).
6// Retrieval is already sovereign (the researcher's external-fetch). This gate builds the TRANSLATION: a MINER
7// that PARSES retrieved structured text into candidate primitives -- NO LLM -- then runs the select+verify loop.
8// Deterministic on a retrieved-text FIXTURE (the live fetch is an I/O swap, operator-gated for real requests).
9//
10// T1 PARSE: the miner extracts power-sum exponents {3,4,5} from retrieved text, and NOTHING from a distractor
11// (no hallucinated primitive -- it only extracts what the text actually contains).
12// T2 SELECT+VERIFY: of the PARSED candidates {S3,S4,S5}, only S3 makes x^4 solvable+generalizing -> kept; others rejected.
13// T3 END-TO-END: retrieved-text -> parse -> select -> the expanded grammar solves x^4 (held-out 625/1296), NO LLM.
14// T4 PROVENANCE/NO-FAKE: the kept primitive's exponent (3) appears in the retrieved text; and EMPTY/irrelevant
15// retrieval yields 0 candidates -> no expansion (honest escalate, no fabrication).
16// T5 THE BRIDGE: translation (structured text -> primitive) done by a parser/miner, no LLM. Honest scope below.
17// expect_exit: 0 license_tier: ORIGINAL
18import "nx_syscalls.nx"
19
20func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
21" as *u8); return ok }
22func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
23func lcg_next(st: *i64) -> i64 { st[0]=((1103515245*st[0])+12345)&2147483647; return st[0] }
24func rand_coef(st: *i64) -> i64 { return (0-9)+(lcg_next(st)%19) }
25
26// THE MINER (the translation step): parse retrieved text for "i^<digit>" -> candidate power-sum exponents. NO LLM.
27func parse_exps(t: *u8, tlen: i64, out: *i64) -> i64 {
28 var cnt: i64=0; var i: i64=0
29 while (i+2)<tlen {
30 if t[i]==(105 as u8) { // 'i'
31 if t[i+1]==(94 as u8) { // '^'
32 let d: *u8=((t as i64)+i+2) as *u8
33 if d[0]>=(48 as u8) { if d[0]<=(57 as u8) {
34 let k: i64=(d[0] as i64)-48
35 var found: i64=0; var j: i64=0; while j<cnt { if out[j]==k { found=1 } j=j+1 }
36 if found==0 { out[cnt]=k; cnt=cnt+1 }
37 } }
38 }
39 }
40 i=i+1
41 }
42 return cnt
43}
44
45// power-sum basis: powsum(k,x) = sum_{i=1}^x i^k. pow_i = i^k.
46func pow_i(i: i64, k: i64) -> i64 { var r: i64=1; var c: i64=0; while c<k { r=r*i; c=c+1 } return r }
47func powsum(k: i64, x: i64) -> i64 { var s: i64=0; var i: i64=1; while i<=x { s=s+pow_i(i,k); i=i+1 } return s }
48// model: base {1, x, S1, S2} (+ c4*powsum(candk) if candk>0). candk=0 -> base grammar.
49func model(c: *i64, candk: i64, x: i64) -> i64 {
50 var v: i64=(c[0])+(c[1]*x)+(c[2]*powsum(1,x))+(c[3]*powsum(2,x))
51 if candk>0 { v=v+(c[4]*powsum(candk,x)) }
52 return v
53}
54func merr(c: *i64, candk: i64, xs: *i64, ys: *i64, n: i64) -> i64 {
55 var e: i64=0; var i: i64=0
56 while i<n { var d: i64=model(c,candk,xs[i])-ys[i]; if d<0 { d=0-d } e=e+d; i=i+1 }
57 return e
58}
59// iterated local search over the coefficients (no LLM). candk=0 -> 4 coeffs, else 5.
60func fit(candk: i64, xs: *i64, ys: *i64, n: i64, seed: i64, best: *i64) -> i64 {
61 var ng: i64=4; if candk>0 { ng=5 }
62 let st: *i64=sys_mmap(16) as *i64; st[0]=seed
63 let par: *i64=sys_mmap(64) as *i64; let ch: *i64=sys_mmap(64) as *i64; let bev: *i64=sys_mmap(64) as *i64
64 var gi: i64=0; while gi<ng { par[gi]=rand_coef(st); gi=gi+1 }
65 var perr: i64=merr(par,candk,xs,ys,n); var ec: i64=1
66 var ber: i64=perr; var zz: i64=0; while zz<ng { bev[zz]=par[zz]; zz=zz+1 }
67 var stag: i64=0; var stop: i64=0
68 while stop==0 {
69 var l: i64=0
70 while l<16 {
71 var c: i64=0; while c<ng { ch[c]=par[c]; c=c+1 }
72 ch[lcg_next(st)%ng]=rand_coef(st)
73 if (lcg_next(st)%3)==0 { ch[lcg_next(st)%ng]=rand_coef(st) }
74 let ce: i64=merr(ch,candk,xs,ys,n); ec=ec+1
75 if ce<perr { var c2: i64=0; while c2<ng { par[c2]=ch[c2]; c2=c2+1 } perr=ce }
76 if ce==0 { l=16 } else { l=l+1 }
77 }
78 if perr<ber { ber=perr; var b2: i64=0; while b2<ng { bev[b2]=par[b2]; b2=b2+1 } stag=0 } else { stag=stag+1 }
79 if stag>50 { var r: i64=0; while r<ng { par[r]=rand_coef(st); r=r+1 } perr=merr(par,candk,xs,ys,n); ec=ec+1; stag=0 }
80 if ber==0 { stop=1 }
81 if ec>=300000 { stop=1 }
82 }
83 var b: i64=0; while b<ng { best[b]=bev[b]; b=b+1 }
84 if ber==0 { return 1 }
85 return 0
86}
87// test a candidate exponent end-to-end: fit + held-out generalization (x=5->625, x=6->1296). 1=keep.
88func keep_candidate(candk: i64, xs: *i64, ys: *i64, n: i64) -> i64 {
89 let bb: *i64=sys_mmap(64) as *i64
90 if fit(candk,xs,ys,n,12345,bb)==1 { if model(bb,candk,5)==625 { if model(bb,candk,6)==1296 { return 1 } } }
91 return 0
92}
93
94func main() -> i64 {
95 gw("=== nx_primitive_miner: retrieval -> TRANSLATION (parse) -> select -- close the bridge, no LLM ===\n" as *u8)
96 var pass: i64=0; var total: i64=0
97
98 // the RETRIEVED text (what a search for power-sum/sequence formulas returns; live fetch = researcher external-fetch).
99 let doc: *u8="Faulhaber power sums (ref OEIS): sum i^3 = (x(x+1)/2)^2 ; sum i^4 ; sum i^5 . Note: the year 2019 had 365 days." as *u8
100 let distractor: *u8="Breaking news: stock index up 3 percent; no formulas here, just prose and numbers 42 and 7." as *u8
101
102 // TARGET = x^4 (out of base grammar). train x=0..4, held {5->625, 6->1296}.
103 let xs: *i64=sys_mmap(64) as *i64; let ys: *i64=sys_mmap(64) as *i64
104 xs[0]=0; xs[1]=1; xs[2]=2; xs[3]=3; xs[4]=4
105 ys[0]=0; ys[1]=1; ys[2]=16; ys[3]=81; ys[4]=256
106 let n: i64=5
107
108 // T1: PARSE the retrieved text -> candidate exponents; parse the distractor -> nothing.
109 let exps: *i64=sys_mmap(128) as *i64
110 let cnt: i64=parse_exps(doc,slen(doc),exps)
111 let dexps: *i64=sys_mmap(128) as *i64
112 let dcnt: i64=parse_exps(distractor,slen(distractor),dexps)
113 var got345: i64=0; if cnt==3 { if exps[0]==3 { if exps[1]==4 { if exps[2]==5 { got345=1 } } } }
114 total=total+1; if got345==1 { if dcnt==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
115 gw("T1 PARSE: miner extracted " as *u8); gn(cnt); gw(" exponents from retrieved text {" as *u8); var p: i64=0; while p<cnt { gn(exps[p]); if p<cnt-1 { gw("," as *u8) } p=p+1 } gw("}, and " as *u8); gn(dcnt); gw(" from the distractor (no hallucinated primitive)\n" as *u8)
116
117 // T2: SELECT+VERIFY over the PARSED candidates -- only S3 (k=3) is kept.
118 var selected: i64=0; var nkept: i64=0
119 var ci: i64=0
120 while ci<cnt {
121 let k: i64=exps[ci]
122 let keep: i64=keep_candidate(k,xs,ys,n)
123 gw(" parsed candidate S" as *u8); gn(k); gw(" (i^" as *u8); gn(k); gw("): kept=" as *u8); gn(keep); gw("\n" as *u8)
124 if keep==1 { selected=k; nkept=nkept+1 }
125 ci=ci+1
126 }
127 total=total+1; if selected==3 { if nkept==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
128 gw("T2 SELECT+VERIFY: of parsed {S3,S4,S5} kept exactly " as *u8); gn(nkept); gw(" -> selected S" as *u8); gn(selected); gw(", no LLM\n" as *u8)
129
130 // T3: END-TO-END -- the expanded grammar (base+S3) solves x^4 + generalizes.
131 let bb: *i64=sys_mmap(64) as *i64
132 let okfit: i64=fit(3,xs,ys,n,12345,bb)
133 let h5: i64=model(bb,3,5); let h6: i64=model(bb,3,6)
134 total=total+1; if okfit==1 { if h5==625 { if h6==1296 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
135 gw("T3 END-TO-END: retrieved->parse->select-> base+S3 fits x^4=[" as *u8); gn(bb[0]); gw("," as *u8); gn(bb[1]); gw("," as *u8); gn(bb[2]); gw("," as *u8); gn(bb[3]); gw("," as *u8); gn(bb[4]); gw("] held-out x=5->" as *u8); gn(h5); gw(" x=6->" as *u8); gn(h6); gw("\n" as *u8)
136
137 // T4: PROVENANCE + EMPTY-RETRIEVAL no-fake.
138 var prov: i64=0; var pj: i64=0; while pj<cnt { if exps[pj]==selected { prov=1 } pj=pj+1 }
139 let empty: *u8="no math here" as *u8
140 let ee: *i64=sys_mmap(64) as *i64
141 let ecnt: i64=parse_exps(empty,slen(empty),ee)
142 total=total+1; if prov==1 { if ecnt==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
143 gw("T4 PROVENANCE/NO-FAKE: selected S" as *u8); gn(selected); gw(" came from the retrieved text (prov=" as *u8); gn(prov); gw("); empty retrieval -> " as *u8); gn(ecnt); gw(" candidates -> no expansion (escalate, no fabrication)\n" as *u8)
144
145 // T5: THE BRIDGE CLOSED.
146 total=total+1; if got345==1 { if selected==3 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
147 gw("T5 BRIDGE: translation (retrieved structured text -> candidate primitive) done by a PARSER/miner, NO LLM, then env-selected\n" as *u8)
148
149 gw("\n THE BRIDGE IS CLOSED (for structured retrieval): retrieval[fixture; live=researcher external-fetch] -> TRANSLATION[parser/miner]\n" as *u8)
150 gw(" -> SELECT+VERIFY[environment, no-fake-green] -> grammar EXPANDED with a new primitive, ZERO LLM end-to-end. HONEST SCOPE: the\n" as *u8)
151 gw(" miner parses STRUCTURED sources (formula lists/tables); free-form natural-language docs still need general translation (the LLM's\n" as *u8)
152 gw(" trick, or a much harder NLP miner). Live fetch is an I/O swap (operator-gated). = the self-reliant synthesis loop, end to end.\n" as *u8)
153 gw("PRIMITIVE-MINER verdict=" as *u8)
154 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- retrieval->translation->select closed sovereignly, no LLM (for structured sources)\n" as *u8); sys_exit(0); return 0 }
155 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
156}