code wiki / _hdl_build / nx_perma_mine_gate.nx

nx_perma_mine_gate.nx source

↩ module page · 169 lines · 9421 B

1// nx_perma_mine_gate.nx -- GATE: grind the permaculture corpus into affordability facts. Proves: T1 corpus 2// mined (license-clean), T2 companion-planting signal (crops co-occur -> grow-together facts), T3 soil-practice 3// ranking (compost beats biochar by corpus evidence), T4 companions promoted to the garden store + readable, 4// T5 license negative control (an ARR-tagged source refused). The researcher grew from fetcher -> synthesizer. 5// license_tier: ORIGINAL 6import "nx_perma_mine.nx" 7import "nx_food_provenance.nx" 8import "nx_food_science.nx" 9import "nx_seg_store.nx" 10import "nx_syscalls.nx" 11import "nx_gate_verdict.nx" 12 13const PG_FOOD: *u8 = "knowledge/store/food-" 14 15func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16func g_i(v: i64) -> i64 { 17 let bb: *u8 = sys_mmap(28); var m: i64 = v 18 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 19 let t: *u8 = sys_mmap(28); var k: i64 = 0 20 if m == 0 { t[0] = 48 as u8; k = 1 } 21 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 22 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 23} 24 25func main() -> i64 { 26 g_p("=== nx_perma_mine_gate (grind the permaculture corpus -> companion planting + soil ranking) ===\n" as *u8) 27 fd_seed(PG_FOOD) 28 fd_seed_provenance(PG_FOOD) 29 gd_seed(PG_FOOD) 30 31 let world: *i64 = fd_world_open(PG_FOOD) 32 let n: i64 = world[0] 33 let names: *i64 = world[4] as *i64 34 let widx: *i64 = world[1] as *i64 35 let termlc: *i64 = sys_mmap(8 * 64) as *i64 36 let termlen: *i64 = sys_mmap(8 * 64) as *i64 37 fm_build_terms(world, termlc, termlen) 38 let cooc: *i64 = sys_mmap(8 * n * n) as *i64 39 let pres: *i64 = sys_mmap(8 * 64) as *i64 40 41 // soil-practice terms (already lowercase) 42 let st: *i64 = sys_mmap(8 * 8) as *i64 43 let stl: *i64 = sys_mmap(8 * 8) as *i64 44 st[0] = "compost" as *u8 as i64; st[1] = "mulch" as *u8 as i64; st[2] = "manure" as *u8 as i64; st[3] = "nitrogen" as *u8 as i64; st[4] = "biochar" as *u8 as i64 45 var si: i64 = 0; while si < 5 { stl[si] = as_len(st[si] as *u8); si = si + 1 } 46 let tcount: *i64 = sys_mmap(8 * 8) as *i64 47 48 // corpus (license-clean Wikipedia/Wikibooks = CC-BY-SA); entry 9 is an ARR negative control 49 let paths: *i64 = sys_mmap(8 * 16) as *i64 50 let lics: *i64 = sys_mmap(8 * 16) as *i64 51 paths[0] = "knowledge/fetched/perma_a6_companion_planting.raw" as *u8 as i64; lics[0] = "CCBYSA" as *u8 as i64 52 paths[1] = "knowledge/fetched/perma_a5_polyculture.raw" as *u8 as i64; lics[1] = "CCBYSA" as *u8 as i64 53 paths[2] = "knowledge/fetched/perma_a1_permaculture.raw" as *u8 as i64; lics[2] = "CCBYSA" as *u8 as i64 54 paths[3] = "knowledge/fetched/perma_b11_crop_rotation.raw" as *u8 as i64; lics[3] = "CCBYSA" as *u8 as i64 55 paths[4] = "knowledge/fetched/perma_b9_cover_crop.raw" as *u8 as i64; lics[4] = "CCBYSA" as *u8 as i64 56 paths[5] = "knowledge/fetched/perma_b12_nitrogen_fixation.raw" as *u8 as i64; lics[5] = "CCBYSA" as *u8 as i64 57 paths[6] = "knowledge/fetched/perma_a3_agroforestry.raw" as *u8 as i64; lics[6] = "CCBYSA" as *u8 as i64 58 paths[7] = "knowledge/fetched/perma_a2_forest_gardening.raw" as *u8 as i64; lics[7] = "CCBYSA" as *u8 as i64 59 paths[8] = "knowledge/fetched/perma_b7_compost.raw" as *u8 as i64; lics[8] = "CCBYSA" as *u8 as i64 60 paths[9] = "knowledge/fetched/perma_b1_soil.raw" as *u8 as i64; lics[9] = "ARR" as *u8 as i64 61 let ncorp: i64 = 10 62 63 var mined: i64 = 0 64 var skipped: i64 = 0 65 var totalbytes: i64 = 0 66 let szp: *i64 = sys_mmap(16) as *i64 67 var ci: i64 = 0 68 while ci < ncorp { 69 if fd_source_admissible(PG_FOOD, lics[ci] as *u8) == 0 { 70 g_p(" REFUSED (" as *u8); g_p(lics[ci] as *u8); g_p("): " as *u8); g_p(paths[ci] as *u8); g_p("\n" as *u8) 71 skipped = skipped + 1 72 } else { 73 let buf: *u8 = ss_readall(paths[ci] as *u8, szp) 74 let sz: i64 = szp[0] 75 if sz > 0 { 76 fm_lower(buf, sz) 77 fm_mine(world, buf, sz, 800, termlc, termlen, cooc, pres) 78 var t: i64 = 0 79 while t < 5 { tcount[t] = tcount[t] + pm_count_term(buf, sz, st[t] as *u8, stl[t]); t = t + 1 } 80 mined = mined + 1; totalbytes = totalbytes + sz 81 } else { g_p(" MISS: " as *u8); g_p(paths[ci] as *u8); g_p("\n" as *u8) } 82 } 83 ci = ci + 1 84 } 85 86 var pass: i64 = 0 87 var tot: i64 = 0 88 89 // T1 mined license-clean corpora 90 tot = tot + 1 91 if mined >= 7 { if totalbytes > 100000 { pass = pass + 1; g_p("PASS T1 mined " as *u8); g_i(mined); g_p(" license-clean permaculture sources (" as *u8); g_i(totalbytes); g_p(" bytes)\n" as *u8) } else { g_p("FAIL T1 bytes\n" as *u8) } } else { g_p("FAIL T1 mined=" as *u8); g_i(mined); g_p("\n" as *u8) } 92 93 // T2 companion-planting signal among growable crops 94 var npairs: i64 = 0 95 var i: i64 = 0 96 while i < n { 97 if gd_growable(PG_FOOD, widx[i] as *u8) == 1 { 98 var j: i64 = i + 1 99 while j < n { if gd_growable(PG_FOOD, widx[j] as *u8) == 1 { if cooc[i * n + j] > 0 { npairs = npairs + 1 } } j = j + 1 } 100 } 101 i = i + 1 102 } 103 let oi: i64 = fd_index_of(world, "onion" as *u8) 104 let cri: i64 = fd_index_of(world, "carrot" as *u8) 105 let gi: i64 = fd_index_of(world, "garlic" as *u8) 106 g_p("companion pairs (growable) found=" as *u8); g_i(npairs) 107 g_p(" e.g. onion+carrot=" as *u8); g_i(cooc[oi * n + cri]); g_p(" garlic+onion=" as *u8); g_i(cooc[gi * n + oi]); g_p("\n" as *u8) 108 // top-5 companion pairs 109 let tmp: *i64 = sys_mmap(8 * n * n) as *i64 110 var ti2: i64 = 0; while ti2 < n * n { tmp[ti2] = cooc[ti2]; ti2 = ti2 + 1 } 111 g_p(" top companions:" as *u8) 112 var tk: i64 = 0 113 while tk < 5 { 114 var bi: i64 = 0 - 1; var bj: i64 = 0 - 1; var bv: i64 = 0 115 var x: i64 = 0 116 while x < n { 117 if gd_growable(PG_FOOD, widx[x] as *u8) == 1 { 118 var y: i64 = x + 1 119 while y < n { if gd_growable(PG_FOOD, widx[y] as *u8) == 1 { if tmp[x * n + y] > bv { bv = tmp[x * n + y]; bi = x; bj = y } } y = y + 1 } 120 } 121 x = x + 1 122 } 123 if bv > 0 { g_p(" " as *u8); g_p(names[bi] as *u8); g_p("+" as *u8); g_p(names[bj] as *u8); g_p("=" as *u8); g_i(bv); tmp[bi * n + bj] = 0 } else { tk = 5 } 124 tk = tk + 1 125 } 126 g_p("\n" as *u8) 127 tot = tot + 1 128 if npairs >= 3 { pass = pass + 1; g_p("PASS T2 companion-planting signal mined from the corpus (grow-together facts)\n" as *u8) } else { g_p("FAIL T2 npairs=" as *u8); g_i(npairs); g_p("\n" as *u8) } 129 130 // T3 soil-improvement ranking (compost beats biochar by corpus evidence) 131 g_p("soil practices by corpus evidence: compost=" as *u8); g_i(tcount[0]); g_p(" mulch=" as *u8); g_i(tcount[1]); g_p(" manure=" as *u8); g_i(tcount[2]); g_p(" nitrogen=" as *u8); g_i(tcount[3]); g_p(" biochar=" as *u8); g_i(tcount[4]); g_p("\n" as *u8) 132 tot = tot + 1 133 if tcount[0] > tcount[4] { if tcount[0] > 0 { pass = pass + 1; g_p("PASS T3 soil ranking (compost out-evidences biochar = the cheapest fertility win is free compost)\n" as *u8) } else { g_p("FAIL T3 no compost signal\n" as *u8) } } else { g_p("FAIL T3 compost=" as *u8); g_i(tcount[0]); g_p(" biochar=" as *u8); g_i(tcount[4]); g_p("\n" as *u8) } 134 135 // T4 promote companions to the garden store + read back 136 let wrote: i64 = pm_promote_companions(PG_FOOD, world, cooc) 137 g_p("promoted " as *u8); g_i(wrote); g_p(" companion records\n" as *u8) 138 tot = tot + 1 139 var ok4: i64 = 1 140 var anyread: i64 = 0 141 // read back the single highest companion pair 142 var hi: i64 = 0 - 1; var hj: i64 = 0 - 1; var hv: i64 = 0 143 var ax: i64 = 0 144 while ax < n { 145 if gd_growable(PG_FOOD, widx[ax] as *u8) == 1 { 146 var ay: i64 = ax + 1 147 while ay < n { if gd_growable(PG_FOOD, widx[ay] as *u8) == 1 { if cooc[ax * n + ay] > hv { hv = cooc[ax * n + ay]; hi = ax; hj = ay } } ay = ay + 1 } 148 } 149 ax = ax + 1 150 } 151 if hi >= 0 { if pm_companion(PG_FOOD, widx[hi] as *u8, widx[hj] as *u8) == hv { anyread = 1 } } 152 if anyread != 1 { ok4 = 0 } 153 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 companions promoted durable + readable (top: " as *u8); g_p(names[hi] as *u8); g_p("+" as *u8); g_p(names[hj] as *u8); g_p("=" as *u8); g_i(hv); g_p(")\n" as *u8) } else { g_p("FAIL T4\n" as *u8) } 154 155 // T5 license negative control 156 tot = tot + 1 157 if skipped == 1 { pass = pass + 1; g_p("PASS T5 license-gated: the ARR-tagged source was REFUSED\n" as *u8) } else { g_p("FAIL T5 skipped=" as *u8); g_i(skipped); g_p("\n" as *u8) } 158 159 g_p("nx_perma_mine_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 160 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 161 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 162 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 163 let ctr__dry: *i64 = gv_ctr() 164 ctr__dry[0] = pass 165 ctr__dry[1] = tot 166 let rc__dry: i64 = gv_verdict("PERMA-MINE-GATE" as *u8, ctr__dry, "permaculture corpus ground into companion-planting + soil-ranking affordability facts)" as *u8) 167 sys_exit(rc__dry) 168 return rc__dry 169}