code wiki / _hdl_build / nx_geo_query_gate.nx

nx_geo_query_gate.nx source

↩ module page · 79 lines · 4154 B

1// nx_geo_query_gate.nx -- GATE for GEO-009 spatial query. Proves: 2// nearest : among {LA, Boston, Philadelphia}, nearest to NYC is Philadelphia (index 2) 3// within : within 200 km of NYC = {Philadelphia} only (Boston ~306 km out, LA out) 4// bearing : due N=0, due E=90, due S=180, due W=270, NE@equator=45 (the compass quadrants) 5// 6// Evidence -> knowledge/status/geo_query.log (GEOQUERYGATE authored=organ ... verdict=GREEN). 7// license_tier: ORIGINAL 8import "nx_geo_query.nx" 9import "nx_syscalls.nx" 10import "nx_gate_verdict.nx" 11 12const GQ_LOG: *u8 = "knowledge/status/geo_query.log" 13 14func gq_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 } 15func gq_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 } 16 17func gq_near(a: i64, b: i64, tol: i64) -> i64 { var d: i64 = a - b; if d < 0 { d = 0 - d } if d <= tol { return 1 } return 0 } 18 19func gq_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 } 20 21func gq_emit(fd: i64, near: i64, wcount: i64, widx: i64, bN: i64, bE: i64, bS: i64, bW: i64, bNE: i64, ok: i64) -> i64 { 22 gq_w(fd, "GEOQUERYGATE authored=organ nearest_idx=" as *u8); gq_wn(fd, near) 23 gq_w(fd, " within200km_count=" as *u8); gq_wn(fd, wcount) 24 gq_w(fd, " within_idx0=" as *u8); gq_wn(fd, widx) 25 gq_w(fd, " bearing_N=" as *u8); gq_wn(fd, bN) 26 gq_w(fd, " bearing_E=" as *u8); gq_wn(fd, bE) 27 gq_w(fd, " bearing_S=" as *u8); gq_wn(fd, bS) 28 gq_w(fd, " bearing_W=" as *u8); gq_wn(fd, bW) 29 gq_w(fd, " bearing_NE=" as *u8); gq_wn(fd, bNE) 30 if ok == 1 { gq_w(fd, " verdict=GREEN\n" as *u8) } else { gq_w(fd, " verdict=RED\n" as *u8) } 31 return 0 32} 33 34func main() -> i64 { 35 // candidates: 0=LA, 1=Boston, 2=Philadelphia (microdegrees). 36 let pts: *i64 = sys_mmap(8 * 6) as *i64 37 gq_set(pts, 0, 34052200, 0 - 118243700) // LA 38 gq_set(pts, 1, 42360100, 0 - 71058900) // Boston 39 gq_set(pts, 2, 39952600, 0 - 75165200) // Philadelphia 40 let qlat: i64 = 40712800 // NYC 41 let qlon: i64 = 0 - 74006000 42 43 let near: i64 = geo_nearest(qlat, qlon, pts, 3) // expect 2 (Philadelphia) 44 let widx: *i64 = sys_mmap(8 * 3) as *i64 45 let wcount: i64 = geo_within(qlat, qlon, 200000, pts, 3, widx) // expect 1 (Philadelphia) 46 var w0: i64 = 0 - 1 47 if wcount >= 1 { w0 = widx[0] } 48 49 // bearings from origin (equator) for clean quadrants. 50 let bN: i64 = geo_bearing(0, 0, 1000000, 0) // due north -> 0 51 let bE: i64 = geo_bearing(0, 0, 0, 1000000) // due east -> 90 52 let bS: i64 = geo_bearing(0, 0, 0 - 1000000, 0) // due south -> 180 53 let bW: i64 = geo_bearing(0, 0, 0, 0 - 1000000) // due west -> 270 54 let bNE: i64 = geo_bearing(0, 0, 1000000, 1000000) // NE @ equator -> 45 55 56 var ok: i64 = 1 57 if near != 2 { ok = 0 } 58 if wcount != 1 { ok = 0 } 59 if w0 != 2 { ok = 0 } 60 if gq_near(bN, 0, 2) != 1 { ok = 0 } 61 if gq_near(bE, 90, 2) != 1 { ok = 0 } 62 if gq_near(bS, 180, 2) != 1 { ok = 0 } 63 if gq_near(bW, 270, 2) != 1 { ok = 0 } 64 if gq_near(bNE, 45, 2) != 1 { ok = 0 } 65 66 gq_emit(1, near, wcount, w0, bN, bE, bS, bW, bNE, ok) 67 let lf: i64 = sys_openat_append(GQ_LOG, 420) 68 if lf >= 0 { gq_emit(lf, near, wcount, w0, bN, bE, bS, bW, bNE, ok); sys_close(lf) } 69 70 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 71 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 72 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 73 let ctr__dry: *i64 = gv_ctr() 74 ctr__dry[0] = ok 75 ctr__dry[1] = 1 76 let rc__dry: i64 = gv_verdict("GEO-QUERY-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 77 sys_exit(rc__dry) 78 return rc__dry 79}