code wiki / (root) / nx_purchase_gate.nx

nx_purchase_gate.nx source

↩ module page · 81 lines · 4551 B

1// nx_purchase_gate.nx -- R4 GATE: proves the Nishi browser's "before you buy" pipeline end-to-end. A 2// product page -> detect the price -> show WHAT YOU HAVE vs WHAT IT TRULY COSTS + the OPPORTUNITY COST, 3// all exact. Scenario: a $200 gadget while carrying $20,000 of 24% debt ($600/mo), $500 discretionary, 4// $25/hr, $1,000 buffer goal. Exact anchor: putting $200 (or more) on the debt clears interest exactly 5// (R4-gated nx_debt). Exits 0 iff ALL pass. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_money.nx" 8import "nx_debt_payoff.nx" 9import "nx_adhd.nx" 10import "nx_purchase.nx" 11import "nx_price_extract.nx" 12 13func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func g_putn(v: i64) -> i64 { 15 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 16 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 17 let d: *u8 = sys_mmap(24); var k: i64 = 0 18 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 19 var j: i64 = k - 1 20 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 21 return 0 22} 23func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 24 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 25 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 26 return 0 27} 28func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 29 30func main() -> i64 { 31 let st: *i64 = sys_mmap(16) as *i64 32 st[0] = 0; st[1] = 0 33 34 let rate24: i64 = mny_rate_from_pct(24, 1) 35 let balance: i64 = 2000000 // $20,000 debt 36 let payment: i64 = 60000 // $600/mo 37 let price: i64 = 20000 // $200 gadget 38 let available: i64 = 50000 // $500 discretionary 39 40 // WHAT YOU HAVE 41 chk("affordable from discretionary ($200 <= $500)", pur_affordable(price, available), 1, st) 42 chk("a $600 buy is NOT affordable from $500", pur_affordable(60000, available), 0, st) 43 44 // OPPORTUNITY COST (exact, via nx_debt): putting $200 on the 24% debt saves real interest 45 let saved: i64 = pur_interest_saved(balance, rate24, payment, price) 46 g_puts(" [info] opportunity cost of the $200: forgone interest savings = "); g_putn(saved); g_puts("c\n") 47 var savepos: i64 = 0 48 if saved > 0 { savepos = 1 } 49 chk("opportunity cost > 0 (the $200 would have saved interest)", savepos, 1, st) 50 let truecost: i64 = pur_true_cost(price, saved) 51 g_puts(" [info] TRUE cost = sticker $200 + forgone savings = "); g_putn(truecost); g_puts("c\n") 52 var truebigger: i64 = 0 53 if truecost > price { truebigger = 1 } 54 chk("TRUE cost > sticker price (carrying debt makes it cost more)", truebigger, 1, st) 55 chk("true cost = price + opportunity", truecost, price + saved, st) 56 57 // EXACT anchor: paying off the WHOLE balance saves ALL the interest ($13,288.29 on $20k@24%@$600) 58 chk("clearing the debt saves all interest = 1328829c ($13,288.29)", pur_interest_saved(balance, rate24, payment, 3000000), 1328829, st) 59 60 // REFRAMES (counter present bias) 61 chk("$200 = 8 hours of your life ($25/hr)", pur_hours(price, 2500), 8, st) 62 chk("$200 = 20% of a $1,000 buffer goal", pur_pct_of_goal(price, 100000), 20, st) 63 64 // ADVISORY recommendation (never blocks) 65 chk("recommend = PAUSE (cooling-off)", pur_recommend(price, 1, 10000, available), ADHD_PAUSE, st) 66 67 // ---- BROWSER PIPELINE: a product page -> detect the price ---- 68 let page: *u8 = "<html><body><h1>Wireless Headphones</h1><p>Premium noise-cancelling sound.</p><div class=\"price\">$200.00</div><p>Shipping: $9.99</p><button>Add to cart</button></body></html>\x00" as *u8 69 let pn: i64 = slen(page) 70 chk("browser detects the price $200.00 (largest, not $9.99 shipping)", px_find_price(page, pn), 20000, st) 71 72 // end-to-end: page price -> the same advisory 73 let detected: i64 = px_find_price(page, pn) 74 chk("page price -> PAUSE (before-you-buy fires)", pur_recommend(detected, 1, 10000, available), ADHD_PAUSE, st) 75 g_puts(" [info] before-you-buy: you have "); g_putn(available); g_puts("c, this truly costs "); g_putn(pur_true_cost(detected, pur_interest_saved(balance, rate24, payment, detected))); g_puts("c\n") 76 77 g_puts("nx_purchase_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 78 if st[1] == 0 { g_puts("SITUATION R4 nx_purchase: GREEN\n"); return 0 } 79 g_puts("SITUATION R4 nx_purchase: RED\n") 80 return 1 81}