code wiki / (root) / nx_stmt_gate.nx

nx_stmt_gate.nx source

↩ module page · 188 lines · 8132 B

1// nx_stmt_gate.nx -- F985 INDEPENDENT GATE: statements derived from the ledger. 2// Builds a real business on the ledger (owner capital in, cash spent, revenue earned) and proves the 3// accounting equation holds BY DERIVATION at every step -- nobody types a balance-sheet figure -- and 4// that an unresolvable account REFUSES the whole statement rather than quietly dropping it. 5// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 6 7import "nx_stmt_lib.nx" 8 9func sg_puts(s: *u8) -> i64 { 10 var n: i64 = 0 11 while s[n] != (0 as u8) { n = n + 1 } 12 sys_write(1, s, n) 13 return 0 14} 15func sg_putn(v: i64) -> i64 { 16 let t: *u8 = sys_mmap(32) 17 var o: i64 = 0 18 var m: i64 = v 19 if m < 0 { t[o] = 45 as u8; o = o + 1; m = 0 - m } 20 let d: *u8 = sys_mmap(32) 21 var k: i64 = 0 22 if m == 0 { d[0] = 48 as u8; k = 1 } 23 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 24 var i: i64 = 0 25 while i < k { t[o] = d[k - 1 - i]; o = o + 1; i = i + 1 } 26 sys_write(1, t, o) 27 return 0 28} 29func sg_ck(cnt: *i64, name: *u8, got: i64, want: i64) -> i64 { 30 if got == want { 31 cnt[0] = cnt[0] + 1 32 sg_puts(" PASS " as *u8); sg_puts(name); sg_puts(" = " as *u8); sg_putn(got); sg_puts("\n" as *u8) 33 return 1 34 } 35 cnt[1] = cnt[1] + 1 36 sg_puts(" FAIL " as *u8); sg_puts(name); sg_puts(" got " as *u8); sg_putn(got) 37 sg_puts(" want " as *u8); sg_putn(want); sg_puts("\n" as *u8) 38 return 0 39} 40func sg_id(tag: *u8, nonce: i64, out: *u8) -> i64 { 41 var o: i64 = mt_catcopy(out, 0, tag) 42 o = mt_catn(out, o, nonce) 43 out[o] = 0 as u8 44 return o 45} 46 47func main(argc: i64, argv: *i64) -> i64 { 48 // FIXTURE MOVED OUT OF THE SWEPT STORE (2026-08-07). knowledge/store/ is walked every 600s by 49 // the nx_segguard beat; this gate's fixture is one of TEN measured as actually folded by it. A 50 // fold landing mid-run rewrites the manifest under the code being measured, so a RED could not 51 // be attributed. Proven on the sibling defect: the SAME code went RED on a knowledge/store 52 // fixture and GREEN 24/24 on a /tmp one -- the RED tracked the FIXTURE, not the code. 53 // Created at SETUP, not teardown: a teardown does not run when a run crashes. 54 sys_mkdir("/tmp/stmtgate\x00" as *u8, 0x1ed) 55 let lp: *u8 = "/tmp/stmtgate/stmtgate-led-" as *u8 56 let cp: *u8 = "/tmp/stmtgate/stmtgate-coa-" as *u8 57 let nonce: i64 = sys_now_us() 58 let cnt: *i64 = sys_mmap(16) as *i64 59 cnt[0] = 0 60 cnt[1] = 0 61 62 let cash: *u8 = sys_mmap(64) 63 let note: *u8 = sys_mmap(64) 64 let cap: *u8 = sys_mmap(64) 65 let rev: *u8 = sys_mmap(64) 66 let exp: *u8 = sys_mmap(64) 67 let ghost: *u8 = sys_mmap(64) 68 sg_id("cash_" as *u8, nonce, cash) 69 sg_id("note_" as *u8, nonce, note) 70 sg_id("capital_" as *u8, nonce, cap) 71 sg_id("revenue_" as *u8, nonce, rev) 72 sg_id("rent_" as *u8, nonce, exp) 73 sg_id("ghost_" as *u8, nonce, ghost) 74 75 let t1: *u8 = sys_mmap(64) 76 let t2: *u8 = sys_mmap(64) 77 let t3: *u8 = sys_mmap(64) 78 let t4: *u8 = sys_mmap(64) 79 sg_id("s1_" as *u8, nonce, t1) 80 sg_id("s2_" as *u8, nonce, t2) 81 sg_id("s3_" as *u8, nonce, t3) 82 sg_id("s4_" as *u8, nonce, t4) 83 84 // the data-driven side map 85 coa_type_put(cp, "asset" as *u8, "debit" as *u8) 86 coa_type_put(cp, "liability" as *u8, "credit" as *u8) 87 coa_type_put(cp, "equity" as *u8, "credit" as *u8) 88 coa_type_put(cp, "revenue" as *u8, "credit" as *u8) 89 coa_type_put(cp, "expense" as *u8, "debit" as *u8) 90 coa_put(cp, cash, "asset" as *u8) 91 coa_put(cp, note, "liability" as *u8) 92 coa_put(cp, cap, "equity" as *u8) 93 coa_put(cp, rev, "revenue" as *u8) 94 coa_put(cp, exp, "expense" as *u8) 95 96 let bs: *i64 = sys_mmap(8 * 8) as *i64 97 let inc: *i64 = sys_mmap(8 * 8) as *i64 98 let tb: *i64 = sys_mmap(8 * 8) as *i64 99 100 let accts: *i64 = sys_mmap(8 * 5) as *i64 101 accts[0] = cash as i64 102 accts[1] = note as i64 103 accts[2] = cap as i64 104 accts[3] = rev as i64 105 accts[4] = exp as i64 106 107 sg_puts("NISHI-STMT-GATE (F985 statements DERIVED from the ledger, never typed)\n" as *u8) 108 109 // 1) owner puts in $5000.00 capital -> debit cash, credit capital 110 led_xfer(lp, t1, cash, cap, 500000, "posted" as *u8) 111 // 2) borrow $2000.00 on a note -> debit cash, credit note payable 112 led_xfer(lp, t2, cash, note, 200000, "posted" as *u8) 113 114 // ---- S1: the accounting equation holds with ZERO figures typed ---- 115 sg_ck(cnt, "S1 balance sheet derived AND balanced (1)" as *u8, 116 stmt_balance_sheet(lp, cp, accts, 5, bs), 1) 117 sg_ck(cnt, "S1a assets $7000.00" as *u8, bs[0], 700000) 118 sg_ck(cnt, "S1b liabilities $2000.00" as *u8, bs[1], 200000) 119 sg_ck(cnt, "S1c equity $5000.00" as *u8, bs[2], 500000) 120 sg_ck(cnt, "S1d variance assets-(liab+equity)" as *u8, bs[3], 0) 121 122 // 3) earn $1500.00 revenue in cash -> debit cash, credit revenue 123 led_xfer(lp, t3, cash, rev, 150000, "posted" as *u8) 124 // 4) pay $400.00 rent -> debit rent expense, credit cash 125 led_xfer(lp, t4, exp, cash, 40000, "posted" as *u8) 126 127 // ---- S2: income statement derived ---- 128 sg_ck(cnt, "S2 income statement derived (1)" as *u8, stmt_income(lp, cp, accts, 5, inc), 1) 129 sg_ck(cnt, "S2a revenue $1500.00" as *u8, inc[0], 150000) 130 sg_ck(cnt, "S2b expenses $400.00" as *u8, inc[1], 40000) 131 sg_ck(cnt, "S2c NET INCOME $1100.00" as *u8, inc[2], 110000) 132 133 // ---- S3: cash is the sum of every cash movement, derived ---- 134 let one: *i64 = sys_mmap(16) as *i64 135 coa_natural(lp, cp, cash, one) 136 sg_ck(cnt, "S3 cash = 5000+2000+1500-400 = $8100.00" as *u8, one[0], 810000) 137 138 // ---- S4: THE LINK. Assets now exceed liabilities+equity by EXACTLY net income, ---- 139 // because revenue/expense have not been closed to equity yet. This is the real 140 // articulation between the two statements, and it is DERIVED, not asserted. 141 stmt_balance_sheet(lp, cp, accts, 5, bs) 142 sg_ck(cnt, "S4 pre-close variance EQUALS net income (statement articulation)" as *u8, 143 bs[3], inc[2]) 144 145 // ---- S5: close the books -- move net income into equity, equation returns to 0 ---- 146 let tc: *u8 = sys_mmap(64) 147 sg_id("close_" as *u8, nonce, tc) 148 led_xfer(lp, tc, rev, cap, 150000, "posted" as *u8) 149 let tc2: *u8 = sys_mmap(64) 150 sg_id("close2_" as *u8, nonce, tc2) 151 led_xfer(lp, tc2, cap, exp, 40000, "posted" as *u8) 152 sg_ck(cnt, "S5 after closing entries the equation BALANCES again" as *u8, 153 stmt_balance_sheet(lp, cp, accts, 5, bs), 1) 154 sg_ck(cnt, "S5a variance back to zero" as *u8, bs[3], 0) 155 sg_ck(cnt, "S5b equity absorbed net income: $5000 + $1100" as *u8, bs[2], 610000) 156 157 // ---- S6: trial balance -- total debits == total credits across the book ---- 158 sg_ck(cnt, "S6 trial balance: debits == credits (1)" as *u8, 159 stmt_trial_balance(lp, accts, 5, tb), 1) 160 161 // ---- S7: FAIL-CLOSED. An unresolvable account REFUSES the whole statement. ---- 162 // A balance sheet that silently drops an account it did not understand is the worst 163 // possible failure: authoritative-looking and wrong. 164 let accts2: *i64 = sys_mmap(8 * 6) as *i64 165 accts2[0] = cash as i64 166 accts2[1] = note as i64 167 accts2[2] = cap as i64 168 accts2[3] = rev as i64 169 accts2[4] = exp as i64 170 accts2[5] = ghost as i64 171 bs[3] = 999999 172 sg_ck(cnt, "S7 unregistered account REFUSES the balance sheet (0)" as *u8, 173 stmt_balance_sheet(lp, cp, accts2, 6, bs), 0) 174 sg_ck(cnt, "S7a refusal wrote NO variance (not silently dropped)" as *u8, bs[3], 999999) 175 sg_ck(cnt, "S7b unregistered account REFUSES the income statement too" as *u8, 176 stmt_income(lp, cp, accts2, 6, inc), 0) 177 178 sg_puts("nx_stmt_gate: pass=" as *u8); sg_putn(cnt[0]) 179 sg_puts(" fail=" as *u8); sg_putn(cnt[1]); sg_puts("\n" as *u8) 180 if cnt[1] == 0 { 181 sg_puts("F985 nx_stmt: VERDICT=GREEN (equation derived not typed, articulation proven, fail-closed on omission)\n" as *u8) 182 sys_exit(0) 183 return 0 184 } 185 sg_puts("F985 nx_stmt: VERDICT=RED\n" as *u8) 186 sys_exit(1) 187 return 1 188}