code wiki / _hdl_build / nx_impact.nx
nx_impact.nx source
↩ module page · 129 lines · 9657 B
1// nx_impact.nx -- LIB: the CLEAN + ENVIRONMENTAL IMPACT + VALUE model for the food arc (operator: "measure
2// clean and environmental impact while getting costs LOWER to the customer -- they invest in improving their
3// lives, we gain MORE profit and they spend LESS; starting with strawberries (heavy pesticide conventionally)
4// where ours is the OPPOSITE"). Per food item, per SOURCE (conv / nishi / ...), we store MEASURED impact:
5// pesticide load, carbon, water, soil health, price, and our cost-of-goods. The triple win is then computed
6// and PROVEN, not claimed: cleaner + lower-impact + cheaper for the customer + higher margin for us (vertical
7// integration). Clean = MEASURED from pesticide load, so greenwashing (a clean CLAIM with high residue) is
8// caught. Integer cents / integer indices (no float). Each record CITEs research. license_tier: ORIGINAL
9// Schema: food:impact:<item>:<source> -> name <t> pesticide(0-100) <t> carbon_g_per_kg <t> water_L_per_kg <t>
10// soil_health(0-10, higher=regenerative) <t> price_cents <t> cogs_cents <t> cite
11import "nx_food_science.nx"
12import "nx_food_provenance.nx"
13import "nx_seg_store.nx"
14import "nx_syscalls.nx"
15
16func im_money(dst: *u8, off: i64, cents: i64) -> i64 {
17 var o: i64 = off
18 dst[o] = 36 as u8; o = o + 1
19 o = fd_apnum(dst, o, cents / 100)
20 dst[o] = 46 as u8; o = o + 1
21 let r: i64 = cents % 100
22 dst[o] = (48 + (r / 10)) as u8; o = o + 1
23 dst[o] = (48 + (r % 10)) as u8; o = o + 1
24 return o
25}
26func im_key(item: *u8, source: *u8, out: *u8) -> i64 {
27 var o: i64 = 0
28 o = as_append(out, o, "food:impact:" as *u8)
29 o = as_append(out, o, item)
30 out[o] = 58 as u8; o = o + 1
31 o = as_append(out, o, source)
32 out[o] = 0 as u8
33 return o
34}
35func im_field_int(prefix: *u8, item: *u8, source: *u8, f: i64) -> i64 {
36 let key: *u8 = sys_mmap(96); im_key(item, source, key)
37 let pq: *i64 = sys_mmap(16) as *i64
38 let lq: *i64 = sys_mmap(16) as *i64
39 if ss_get(prefix, key, pq, lq) != 1 { return 0 - 1 }
40 let buf: *u8 = sys_mmap(32)
41 let l: i64 = fd_field(pq[0] as *u8, lq[0], f, buf)
42 return fd_atoi(buf, l)
43}
44func im_field_str(prefix: *u8, item: *u8, source: *u8, f: i64, out: *u8) -> i64 {
45 let key: *u8 = sys_mmap(96); im_key(item, source, key)
46 let pq: *i64 = sys_mmap(16) as *i64
47 let lq: *i64 = sys_mmap(16) as *i64
48 if ss_get(prefix, key, pq, lq) != 1 { out[0] = 0 as u8; return 0 }
49 return fd_field(pq[0] as *u8, lq[0], f, out)
50}
51func im_pesticide(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 1) }
52func im_carbon(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 2) }
53func im_water(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 3) }
54func im_soil(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 4) }
55func im_price(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 5) }
56func im_cogs(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_field_int(prefix, item, src, 6) }
57
58// CLEAN score = 100 - measured pesticide load (cleaner = higher). MEASURED, not claimed -> greenwash is caught.
59func im_clean(prefix: *u8, item: *u8, src: *u8) -> i64 { return 100 - im_pesticide(prefix, item, src) }
60// 1 if A is environmentally better than B on EVERY axis (less carbon, less water, MORE soil health = regenerative)
61func im_greener(prefix: *u8, item: *u8, a: *u8, b: *u8) -> i64 {
62 if im_carbon(prefix, item, a) >= im_carbon(prefix, item, b) { return 0 }
63 if im_water(prefix, item, a) >= im_water(prefix, item, b) { return 0 }
64 if im_soil(prefix, item, a) <= im_soil(prefix, item, b) { return 0 }
65 return 1
66}
67// customer savings (cents) buying `a` instead of `b`
68func im_savings(prefix: *u8, item: *u8, a: *u8, b: *u8) -> i64 { return im_price(prefix, item, b) - im_price(prefix, item, a) }
69// our margin (cents) on `src` = price - cost of goods
70func im_margin(prefix: *u8, item: *u8, src: *u8) -> i64 { return im_price(prefix, item, src) - im_cogs(prefix, item, src) }
71// THE TRIPLE WIN: nishi dominates conv on clean + every env axis + price, AND our margin beats the conv margin.
72func im_triple_win(prefix: *u8, item: *u8) -> i64 {
73 if im_clean(prefix, item, "nishi" as *u8) <= im_clean(prefix, item, "conv" as *u8) { return 0 }
74 if im_greener(prefix, item, "nishi" as *u8, "conv" as *u8) != 1 { return 0 }
75 if im_price(prefix, item, "nishi" as *u8) >= im_price(prefix, item, "conv" as *u8) { return 0 }
76 if im_margin(prefix, item, "nishi" as *u8) <= im_margin(prefix, item, "conv" as *u8) { return 0 }
77 return 1
78}
79
80func im_seed(prefix: *u8) -> i64 {
81 let keys: *i64 = sys_mmap(8 * 8) as *i64
82 let vals: *i64 = sys_mmap(8 * 8) as *i64
83 var n: i64 = 0
84 // Conventional strawberries: top of the pesticide list, long-haul, soil-degrading, retail-marked-up.
85 keys[n] = "food:impact:strawberry:conv" as *u8 as i64
86 vals[n] = "Conventional strawberries\t90\t1500\t300\t3\t600\t450\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1
87 // Nishi strawberries: no-spray/IPM, local + regenerative (BUILDS soil), low cost-of-goods (we grow them).
88 keys[n] = "food:impact:strawberry:nishi" as *u8 as i64
89 vals[n] = "Nishi strawberries\t5\t300\t180\t9\t450\t150\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1
90 // Greenwash control: an "eco" CLAIM with high actual pesticide -> caught by the MEASURED clean score.
91 keys[n] = "food:impact:strawberry:greenwash" as *u8 as i64
92 vals[n] = "EcoBrand (greenwashed) strawberries\t80\t1400\t290\t3\t580\t440\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1
93
94 let w: *i64 = ss_begin()
95 var tw: i64 = 0
96 var i: i64 = 0
97 while i < n {
98 let k: *u8 = keys[i] as *u8
99 let v: *u8 = vals[i] as *u8
100 let vl: i64 = as_len(v)
101 if fd_streq_store(prefix, k, v, vl) == 0 { ss_add(w, 1, k, v, vl); tw = tw + 1 }
102 i = i + 1
103 }
104 if tw > 0 { let seg: i64 = fd_seg_next(prefix); ss_commit(prefix, w, seg) }
105 return tw
106}
107
108// render the ours-vs-conventional comparison (NUL-terminated); returns length.
109func im_render_compare(prefix: *u8, item: *u8, out: *u8) -> i64 {
110 var o: i64 = 0
111 o = as_append(out, o, "<!doctype html><html lang='en'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'><title>Cleaner, greener, cheaper</title><style>body{margin:0;font-family:system-ui,sans-serif;color:#1c2a1d;background:#f1f6ee;line-height:1.55}header{background:#2e6b34;color:#fff;padding:26px 22px}header h1{margin:0;font-size:1.5rem}main{max-width:680px;margin:0 auto;padding:18px}table{border-collapse:collapse;width:100%;background:#fff;border-radius:12px;overflow:hidden}th,td{padding:9px 12px;text-align:left;border-top:1px solid #eef3ea}th{background:#e6efe1;color:#33503f}td.n{text-align:right;font-variant-numeric:tabular-nums}.win{color:#1f7a44;font-weight:700}.bad{color:#a23a2a}.big{background:#fff;border-left:5px solid #2e6b34;border-radius:8px;padding:16px;margin-top:16px;font-size:1.05rem}.muted{color:#5b6a52}</style></head><body><header><h1>Nishi vs conventional — the opposite by the numbers</h1></header><main><table><tr><th>Measure</th><th>Conventional</th><th>Nishi</th></tr>" as *u8)
112 // pesticide / clean
113 o = as_append(out, o, "<tr><td>Pesticide load (lower better)</td><td class='n bad'>" as *u8); o = fd_apnum(out, o, im_pesticide(prefix, item, "conv" as *u8))
114 o = as_append(out, o, "</td><td class='n win'>" as *u8); o = fd_apnum(out, o, im_pesticide(prefix, item, "nishi" as *u8)); o = as_append(out, o, "</td></tr>" as *u8)
115 o = as_append(out, o, "<tr><td>Carbon (g CO2/kg)</td><td class='n bad'>" as *u8); o = fd_apnum(out, o, im_carbon(prefix, item, "conv" as *u8))
116 o = as_append(out, o, "</td><td class='n win'>" as *u8); o = fd_apnum(out, o, im_carbon(prefix, item, "nishi" as *u8)); o = as_append(out, o, "</td></tr>" as *u8)
117 o = as_append(out, o, "<tr><td>Water (L/kg)</td><td class='n bad'>" as *u8); o = fd_apnum(out, o, im_water(prefix, item, "conv" as *u8))
118 o = as_append(out, o, "</td><td class='n win'>" as *u8); o = fd_apnum(out, o, im_water(prefix, item, "nishi" as *u8)); o = as_append(out, o, "</td></tr>" as *u8)
119 o = as_append(out, o, "<tr><td>Soil health (higher = regenerative)</td><td class='n bad'>" as *u8); o = fd_apnum(out, o, im_soil(prefix, item, "conv" as *u8))
120 o = as_append(out, o, "</td><td class='n win'>" as *u8); o = fd_apnum(out, o, im_soil(prefix, item, "nishi" as *u8)); o = as_append(out, o, "</td></tr>" as *u8)
121 o = as_append(out, o, "<tr><td>Your price</td><td class='n bad'>" as *u8); o = im_money(out, o, im_price(prefix, item, "conv" as *u8))
122 o = as_append(out, o, "</td><td class='n win'>" as *u8); o = im_money(out, o, im_price(prefix, item, "nishi" as *u8)); o = as_append(out, o, "</td></tr></table>" as *u8)
123 // the win-win-win
124 o = as_append(out, o, "<div class='big'>You save <b>" as *u8); o = im_money(out, o, im_savings(prefix, item, "nishi" as *u8, "conv" as *u8))
125 o = as_append(out, o, "</b> AND get the cleaner, soil-building berry. When you spend less, you invest in your health and the planet — and because we grow them right, we earn more doing it. Cleaner, greener, cheaper: the opposite of conventional.</div>" as *u8)
126 o = as_append(out, o, "<p class='muted'>Impact figures are Nishi planning estimates grounded in the regenerative-agriculture corpus; strawberries lead conventional produce for pesticide residue.</p></main></body></html>" as *u8)
127 out[o] = 0 as u8
128 return o
129}