code wiki / _hdl_build / nx_pattern_emit_rankretrieve.nx
nx_pattern_emit_rankretrieve.nx source
↩ module page · 63 lines · 4319 B
1// nx_pattern_emit_rankretrieve.nx -- PATTERN EMITTER: RANK_RETRIEVE (shape 22). Authors a deterministic
2// term-frequency RETRIEVAL core (the researcher's internal-arm shape): score each doc by occurrences of the
3// query term, apply a BAKED relevance floor, return the top docid. The 2nd of the session's 5-emitter autonomy
4// backlog -- now TEAM-AUTHORABLE hands-off (author=emitter, no Claude logic; faithful = returns -1 when nothing
5// clears the floor, never fabricates).
6// spec[0]=min_tf (1..16 relevance floor: a doc needs >= min_tf occurrences to count as a hit)
7// Authored fn: <name>_retrieve(docs: *i64, dlens: *i64, ND: i64, term: *u8, tlen: i64, scores: *i64) -> i64
8// docs[i] = a *u8 text (stored as i64); returns the top docid (-1 if none clears the floor).
9// EMIT-TIME RAILS: min_tf outside 1..16 -> spec REFUSED, nothing written. license_tier: ORIGINAL
10import "nx_syscalls.nx"
11
12func prr_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 }
13func prr_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 as u8;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 }
14
15func prr_spec_ok(spec: *i64) -> i64 {
16 let mt: i64=spec[0]
17 if mt < 1 { return 0 }
18 if mt > 16 { return 0 }
19 return 1
20}
21
22// author the core: TF retrieval with a BAKED relevance floor (no runtime config)
23func pe_rr_emit_core(fd: i64, name: *u8, spec: *i64) -> i64 {
24 let mt: i64=spec[0]
25 prr_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: RANK_RETRIEVE) -- TF retrieval, baked relevance floor, no Claude logic\n" as *u8)
26 prr_w(fd, "import \"nx_syscalls.nx\"\n" as *u8)
27 prr_w(fd, "func " as *u8); prr_w(fd, name); prr_w(fd, "_retrieve(docs: *i64, dlens: *i64, ND: i64, term: *u8, tlen: i64, scores: *i64) -> i64 {\n" as *u8)
28 prr_w(fd, " var top: i64=0-1; var best: i64=0; var i: i64=0\n" as *u8)
29 prr_w(fd, " while i<ND {\n" as *u8)
30 prr_w(fd, " let hay: *u8=docs[i] as *u8; let hlen: i64=dlens[i]\n" as *u8)
31 prr_w(fd, " var cnt: i64=0; var p: i64=0\n" as *u8)
32 prr_w(fd, " while p+tlen<=hlen { var j: i64=0; var eq: i64=1; while j<tlen { if hay[p+j]!=term[j] { eq=0; j=tlen } else { j=j+1 } } if eq==1 { cnt=cnt+1 } p=p+1 }\n" as *u8)
33 prr_w(fd, " if cnt < " as *u8); prr_wn(fd, mt); prr_w(fd, " { cnt=0 }\n" as *u8)
34 prr_w(fd, " scores[i]=cnt\n" as *u8)
35 prr_w(fd, " if cnt>best { best=cnt; top=i }\n" as *u8)
36 prr_w(fd, " i=i+1\n }\n return top\n}\n" as *u8)
37 return 1
38}
39
40// author the test: doc0 = the term repeated min_tf times (a hit), doc1 = no term (miss) -> top=0. KAT from the spec.
41func pe_rr_emit_test(fd: i64, name: *u8, spec: *i64) -> i64 {
42 let mt: i64=spec[0]
43 prr_w(fd, "import \"" as *u8); prr_w(fd, name); prr_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8)
44 prr_w(fd, "func cps(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 }\n" as *u8)
45 prr_w(fd, "func main() -> i64 {\n" as *u8)
46 prr_w(fd, " let docs: *i64=sys_mmap(64) as *i64; let dlens: *i64=sys_mmap(64) as *i64; let sc: *i64=sys_mmap(64) as *i64\n" as *u8)
47 prr_w(fd, " let d0: *u8=sys_mmap(256); let l0: i64=cps(d0, \"" as *u8)
48 var z: i64=0; while z<mt { prr_w(fd, "zk " as *u8); z=z+1 }
49 prr_w(fd, "\" as *u8); docs[0]=d0 as i64; dlens[0]=l0\n" as *u8)
50 prr_w(fd, " let d1: *u8=sys_mmap(64); let l1: i64=cps(d1, \"qq rr ss\" as *u8); docs[1]=d1 as i64; dlens[1]=l1\n" as *u8)
51 prr_w(fd, " if " as *u8); prr_w(fd, name); prr_w(fd, "_retrieve(docs, dlens, 2, \"zk\" as *u8, 2, sc) == 0 { if sc[1]==0 { sys_exit(0) } }\n" as *u8)
52 prr_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8)
53 return 1
54}
55
56func pe_rr_author(name: *u8, modpath: *u8, testpath: *u8, spec: *i64) -> i64 {
57 if prr_spec_ok(spec) != 1 { return 0 }
58 let mf: i64=sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
59 pe_rr_emit_core(mf, name, spec); sys_close(mf)
60 let tf: i64=sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
61 pe_rr_emit_test(tf, name, spec); sys_close(tf)
62 return 1
63}