code wiki / _hdl_build / nx_inventory_gate.nx

nx_inventory_gate.nx source

↩ module page · 122 lines · 8566 B

1// nx_inventory_gate.nx -- GATE: the UNIVERSAL inventory substrate proven on TWO domains at once -- a kitchen 2// PANTRY and a will's ESTATE asset catalog -- with identical operations. Proves: T1 catalog+readback, T2 3// universality (same engine, two domains), T3 expiration tracking (pantry), T4 net-worth total for the will 4// (integer cents, no float), T5 by-beneficiary (owner) query, T6 idempotent catalog, T7 supply-chain consume 5// (depletion + honest insufficient-stock refusal). license_tier: ORIGINAL 6import "nx_inventory.nx" 7import "nx_syscalls.nx" 8 9const IG_PREFIX: *u8 = "knowledge/store/inv-" 10 11func 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 } 12func g_i(v: i64) -> i64 { 13 let bb: *u8 = sys_mmap(28); var m: i64 = v 14 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 15 let t: *u8 = sys_mmap(28); var k: i64 = 0 16 if m == 0 { t[0] = 48 as u8; k = 1 } 17 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 19} 20func g_money(cents: i64) -> i64 { let b: *u8 = sys_mmap(32); let o: i64 = iv_money(b, 0, cents); sys_write(1, b, o); return 0 } 21func g_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } 22func g_in(ids: *i64, n: i64, target: *u8) -> i64 { var i: i64 = 0; while i < n { if g_streq(ids[i] as *u8, target) == 1 { return 1 } i = i + 1 } return 0 } 23 24func main() -> i64 { 25 g_p("=== nx_inventory_gate (UNIVERSAL inventory: a will's estate AND a pantry, one engine) ===\n" as *u8) 26 27 // ---- catalog a PANTRY (perishables with expiry days; today = day 0) ---- 28 iv_add(IG_PREFIX, "pantry" as *u8, "chicken" as *u8, "Chicken breast" as *u8, "protein" as *u8, 2, "lb" as *u8, "fridge" as *u8, 0, 3, "" as *u8) 29 iv_add(IG_PREFIX, "pantry" as *u8, "tofu" as *u8, "Firm tofu" as *u8, "protein" as *u8, 1, "block" as *u8, "fridge" as *u8, 0, 2, "" as *u8) 30 iv_add(IG_PREFIX, "pantry" as *u8, "broccoli" as *u8, "Broccoli" as *u8, "vegetable" as *u8, 1, "head" as *u8, "fridge" as *u8, 0, 5, "" as *u8) 31 iv_add(IG_PREFIX, "pantry" as *u8, "onion" as *u8, "Yellow onion" as *u8, "vegetable" as *u8, 3, "ea" as *u8, "pantry" as *u8, 0, 20, "" as *u8) 32 iv_add(IG_PREFIX, "pantry" as *u8, "garlic" as *u8, "Garlic" as *u8, "aromatic" as *u8, 1, "bulb" as *u8, "pantry" as *u8, 0, 60, "" as *u8) 33 iv_add(IG_PREFIX, "pantry" as *u8, "ginger" as *u8, "Ginger root" as *u8, "aromatic" as *u8, 1, "knob" as *u8, "fridge" as *u8, 0, 14, "" as *u8) 34 iv_add(IG_PREFIX, "pantry" as *u8, "soy" as *u8, "Soy sauce" as *u8, "sauce" as *u8, 1, "bottle" as *u8, "pantry" as *u8, 0, 365, "" as *u8) 35 iv_add(IG_PREFIX, "pantry" as *u8, "rice" as *u8, "Jasmine rice" as *u8, "starch" as *u8, 1, "bag" as *u8, "pantry" as *u8, 0, 0, "" as *u8) 36 37 // ---- catalog an ESTATE for a will (values in CENTS, owner = beneficiary) ---- 38 iv_add(IG_PREFIX, "estate" as *u8, "clock" as *u8, "Grandfather clock" as *u8, "heirloom" as *u8, 1, "ea" as *u8, "living room" as *u8, 250000, 0, "Sarah" as *u8) 39 iv_add(IG_PREFIX, "estate" as *u8, "ring" as *u8, "Wedding ring" as *u8, "jewelry" as *u8, 1, "ea" as *u8, "safe" as *u8, 800000, 0, "Emma" as *u8) 40 iv_add(IG_PREFIX, "estate" as *u8, "truck" as *u8, "Ford F-150" as *u8, "vehicle" as *u8, 1, "ea" as *u8, "garage" as *u8, 1850000, 0, "Sam" as *u8) 41 iv_add(IG_PREFIX, "estate" as *u8, "piano" as *u8, "Steinway piano" as *u8, "instrument" as *u8, 1, "ea" as *u8, "study" as *u8, 4200000, 0, "Emma" as *u8) 42 iv_add(IG_PREFIX, "estate" as *u8, "coins" as *u8, "Coin collection" as *u8, "collectible" as *u8, 1, "set" as *u8, "safe" as *u8, 575000, 0, "Sarah" as *u8) 43 44 var pass: i64 = 0 45 var tot: i64 = 0 46 let f: *u8 = sys_mmap(128) 47 48 // T1 catalog + readback across both domains 49 tot = tot + 1 50 var ok1: i64 = 1 51 iv_field_of(IG_PREFIX, "pantry" as *u8, "chicken" as *u8, 0, f) 52 if g_streq(f, "Chicken breast" as *u8) != 1 { ok1 = 0 } 53 if iv_qty(IG_PREFIX, "pantry" as *u8, "chicken" as *u8) != 2 { ok1 = 0 } 54 iv_field_of(IG_PREFIX, "estate" as *u8, "clock" as *u8, 0, f) 55 if g_streq(f, "Grandfather clock" as *u8) != 1 { ok1 = 0 } 56 if iv_value(IG_PREFIX, "estate" as *u8, "clock" as *u8) != 250000 { ok1 = 0 } 57 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 catalog+readback (pantry item + estate asset both round-trip)\n" as *u8) } else { g_p("FAIL T1\n" as *u8) } 58 59 // T2 universality: one engine, two domains 60 let pids: *i64 = sys_mmap(8 * 256) as *i64 61 let eids: *i64 = sys_mmap(8 * 256) as *i64 62 let np: i64 = iv_list(IG_PREFIX, "pantry" as *u8, pids) 63 let ne: i64 = iv_list(IG_PREFIX, "estate" as *u8, eids) 64 g_p("pantry items=" as *u8); g_i(np); g_p(" estate items=" as *u8); g_i(ne); g_p("\n" as *u8) 65 tot = tot + 1 66 if np == 8 { if ne == 5 { pass = pass + 1; g_p("PASS T2 universal: the SAME inventory engine manages a pantry AND a will's estate\n" as *u8) } else { g_p("FAIL T2 estate count\n" as *u8) } } else { g_p("FAIL T2 pantry count\n" as *u8) } 67 68 // T3 expiration tracking (within 3 days of today=0) 69 let exp: *i64 = sys_mmap(8 * 256) as *i64 70 let nexp: i64 = iv_expiring(IG_PREFIX, "pantry" as *u8, 0, 3, exp) 71 g_p("expiring within 3 days:" as *u8) 72 var ei: i64 = 0; while ei < nexp { g_p(" " as *u8); g_p(exp[ei] as *u8); ei = ei + 1 } g_p("\n" as *u8) 73 tot = tot + 1 74 var ok3: i64 = 1 75 if nexp != 2 { ok3 = 0 } 76 if g_in(exp, nexp, "tofu" as *u8) != 1 { ok3 = 0 } 77 if g_in(exp, nexp, "chicken" as *u8) != 1 { ok3 = 0 } 78 if g_in(exp, nexp, "rice" as *u8) != 0 { ok3 = 0 } // non-perishable must NOT be flagged 79 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 expiration: tofu + chicken flagged, non-perishable rice excluded\n" as *u8) } else { g_p("FAIL T3 nexp=" as *u8); g_i(nexp); g_p("\n" as *u8) } 80 81 // T4 net worth for the will (integer cents, no float) 82 let total: i64 = iv_value_total(IG_PREFIX, "estate" as *u8) 83 g_p("estate net worth = " as *u8); g_money(total); g_p(" (" as *u8); g_i(total); g_p(" cents)\n" as *u8) 84 tot = tot + 1 85 if total == 7675000 { pass = pass + 1; g_p("PASS T4 will net-worth total exact (no float): $76,750.00\n" as *u8) } else { g_p("FAIL T4 total=" as *u8); g_i(total); g_p("\n" as *u8) } 86 87 // T5 by-beneficiary query (the will: who gets what) 88 var emma: i64 = 0 89 var i5: i64 = 0 90 while i5 < ne { 91 let ow: *u8 = sys_mmap(64) 92 iv_field_of(IG_PREFIX, "estate" as *u8, eids[i5] as *u8, 7, ow) 93 if g_streq(ow, "Emma" as *u8) == 1 { emma = emma + 1 } 94 i5 = i5 + 1 95 } 96 g_p("assets willed to Emma=" as *u8); g_i(emma); g_p("\n" as *u8) 97 tot = tot + 1 98 if emma == 2 { pass = pass + 1; g_p("PASS T5 by-beneficiary: 2 assets willed to Emma (ring + piano)\n" as *u8) } else { g_p("FAIL T5\n" as *u8) } 99 100 // T6 idempotent catalog (re-adding an unchanged asset writes nothing) 101 let re: i64 = iv_add(IG_PREFIX, "estate" as *u8, "clock" as *u8, "Grandfather clock" as *u8, "heirloom" as *u8, 1, "ea" as *u8, "living room" as *u8, 250000, 0, "Sarah" as *u8) 102 tot = tot + 1 103 if re == 0 { pass = pass + 1; g_p("PASS T6 idempotent (re-cataloging an unchanged item is a no-op)\n" as *u8) } else { g_p("FAIL T6 re=" as *u8); g_i(re); g_p("\n" as *u8) } 104 105 // T7 supply-chain consume (depletion + honest insufficient-stock refusal) 106 let q1: i64 = iv_consume(IG_PREFIX, "pantry" as *u8, "onion" as *u8, 1) 107 let q2: i64 = iv_qty(IG_PREFIX, "pantry" as *u8, "onion" as *u8) 108 let q3: i64 = iv_consume(IG_PREFIX, "pantry" as *u8, "onion" as *u8, 99) 109 g_p("onion: consumed 1 -> qty " as *u8); g_i(q2); g_p("; over-consume 99 -> " as *u8); g_i(q3); g_p("\n" as *u8) 110 tot = tot + 1 111 var ok7: i64 = 1 112 if q1 != 2 { ok7 = 0 } // 3 - 1 = 2 113 if q2 != 2 { ok7 = 0 } 114 if q3 != (0 - 1) { ok7 = 0 } // insufficient -> -1, and qty unchanged 115 if iv_qty(IG_PREFIX, "pantry" as *u8, "onion" as *u8) != 2 { ok7 = 0 } 116 if ok7 == 1 { pass = pass + 1; g_p("PASS T7 supply-chain consume (qty decremented; over-consume refused, stock preserved)\n" as *u8) } else { g_p("FAIL T7\n" as *u8) } 117 118 g_p("nx_inventory_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 119 if pass == tot { g_p(" verdict=GREEN (one universal inventory engine: will catalog + pantry + expiry + consume)\n" as *u8); return 0 } 120 g_p(" verdict=RED\n" as *u8) 121 return 1 122}