code wiki / _hdl_build / nx_geohash_gate.nx

nx_geohash_gate.nx source

↩ module page · 61 lines · 3127 B

1// nx_geohash_gate.nx -- GATE (runnable) for the geohash spatial index (nx_geo). Proves: 2// CANONICAL : encode (57.64911, 10.40744) -> shares a long prefix with the world-standard geohash 3// "u4pruydqqvj" (proves our encoder interoperates with every geohash tool) 4// PROXIMITY : two points ~3 m apart share a long common prefix (>=6); Denmark vs Sydney share ~0 5// -> the index property that turns O(N) fence scans into O(1) prefix bucketing 6// DETERMINISM: same input -> identical hash (pure integer, no float) 7// 8// Evidence -> knowledge/status/geohash_gate.log (GEOHASHGATE authored=organ ... verdict=GREEN). 9// license_tier: ORIGINAL 10import "nx_geo.nx" 11import "nx_syscalls.nx" 12 13const GH_LOG: *u8 = "knowledge/status/geohash_gate.log" 14 15func gh_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 } 16func gh_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 } 17 18func gh_emit(fd: i64, kat: i64, near: i64, far: i64, det: i64, hashA: *u8, ok: i64) -> i64 { 19 gh_w(fd, "GEOHASHGATE authored=organ encode=integer-no-float kat_prefix_vs_canonical=" as *u8); gh_wn(fd, kat) 20 gh_w(fd, " near_common=" as *u8); gh_wn(fd, near) 21 gh_w(fd, " far_common=" as *u8); gh_wn(fd, far) 22 gh_w(fd, " determinism_full=" as *u8); gh_wn(fd, det) 23 gh_w(fd, " hashA=" as *u8); gh_w(fd, hashA) 24 if ok == 1 { gh_w(fd, " verdict=GREEN\n" as *u8) } else { gh_w(fd, " verdict=RED\n" as *u8) } 25 return 0 26} 27 28func main() -> i64 { 29 // (57.64911, 10.40744) in microdegrees (deg * 1e6) -- the classic geohash KAT. 30 let outA: *u8 = sys_mmap(32) 31 geo_geohash(57649110, 10407440, 11, outA) 32 let kat: i64 = geo_prefix_common(outA, "u4pruydqqvj" as *u8) 33 34 // ~3 m away -> long shared prefix. 35 let outB: *u8 = sys_mmap(32) 36 geo_geohash(57649140, 10407470, 11, outB) 37 let near: i64 = geo_prefix_common(outA, outB) 38 39 // Sydney (-33.8688, 151.2093) -> essentially no shared prefix with Denmark. 40 let outC: *u8 = sys_mmap(32) 41 geo_geohash(0 - 33868800, 151209300, 11, outC) 42 let far: i64 = geo_prefix_common(outA, outC) 43 44 // determinism: re-encode the same point -> identical. 45 let outD: *u8 = sys_mmap(32) 46 geo_geohash(57649110, 10407440, 11, outD) 47 let det: i64 = geo_prefix_common(outA, outD) 48 49 var ok: i64 = 1 50 if kat < 5 { ok = 0 } // matches the world-standard geohash to >=5 chars 51 if near < 6 { ok = 0 } // 3 m apart -> deep shared prefix (the index property) 52 if far > 1 { ok = 0 } // far apart -> no shared prefix 53 if det != 11 { ok = 0 } // identical input -> identical 11-char hash 54 55 gh_emit(1, kat, near, far, det, outA, ok) 56 let lf: i64 = sys_openat_append(GH_LOG, 420) 57 if lf >= 0 { gh_emit(lf, kat, near, far, det, outA, ok); sys_close(lf) } 58 59 if ok == 1 { return 0 } 60 return 1 61}