code wiki / (root) / nx_caplab_finance.nx

nx_caplab_finance.nx source

↩ module page · 62 lines · 2559 B

1// nx_caplab_finance.nx -- Capitalism Lab R7: firm finances (loans/debt/interest/ 2// bankruptcy). Cash isn't infinite: a firm can BORROW (up to a credit limit) to 3// fund expansion/investment, pays INTEREST on its debt each period, can REPAY, 4// and goes BANKRUPT when its net worth falls below what its credit can cover. 5// A loan adds liquidity (cash) AND liability (debt) equally -- it does NOT raise 6// net worth; only earnings do, and interest erodes it. Integer, deterministic. 7// 8// finance record (*i64): [0]=cash [1]=debt 9// net_worth = cash - debt 10// loan(amt) : if debt+amt <= credit_limit -> cash+=amt, debt+=amt 11// interest(rate) : cash -= debt*rate/100 12// repay(amt) : pay = min(amt, debt, cash); cash-=pay, debt-=pay 13// bankrupt : net_worth <= -credit_limit (underwater beyond all credit) 14// 15// nx_safety_envelope: 16// intended_use: capitalism-lab firm finances (pure, mutate the record) 17// sil_target: SIL1 18// verdict: NOT_YET_EVALUATED 19// 20// genealogy_id: capitalism_lab_finance_canon 21// lineage_id: nx_caplab_finance_v1 22 23import "nx_syscalls.nx" 24import "nx_tier.nx" 25 26const NX_FIN_CASH: nx_int = 0 27const NX_FIN_DEBT: nx_int = 1 28 29func nx_clab_fin_networth(fin: *i64) -> nx_int { 30 return fin[NX_FIN_CASH] - fin[NX_FIN_DEBT] 31} 32 33// Borrow `amount` if it keeps total debt within the credit limit. Returns 1 if 34// granted (cash and debt both rise), 0 if denied (record unchanged). 35func nx_clab_fin_loan(fin: *i64, amount: nx_int, credit_limit: nx_int) -> nx_int { 36 if fin[NX_FIN_DEBT] + amount > credit_limit { return 0 } 37 fin[NX_FIN_CASH] = fin[NX_FIN_CASH] + amount 38 fin[NX_FIN_DEBT] = fin[NX_FIN_DEBT] + amount 39 return 1 40} 41 42// Charge one period of interest on the outstanding debt (drawn from cash). 43func nx_clab_fin_interest(fin: *i64, rate_pct: nx_int) { 44 let it: nx_int = fin[NX_FIN_DEBT] * rate_pct / 100 45 fin[NX_FIN_CASH] = fin[NX_FIN_CASH] - it 46} 47 48// Repay up to `amount`, bounded by outstanding debt and available cash. 49func nx_clab_fin_repay(fin: *i64, amount: nx_int) { 50 var pay: nx_int = amount 51 if pay > fin[NX_FIN_DEBT] { pay = fin[NX_FIN_DEBT] } 52 if pay > fin[NX_FIN_CASH] { pay = fin[NX_FIN_CASH] } 53 if pay < 0 { pay = 0 } 54 fin[NX_FIN_CASH] = fin[NX_FIN_CASH] - pay 55 fin[NX_FIN_DEBT] = fin[NX_FIN_DEBT] - pay 56} 57 58// Insolvent when net worth is underwater by more than the firm's credit can bridge. 59func nx_clab_fin_bankrupt(fin: *i64, credit_limit: nx_int) -> nx_int { 60 if nx_clab_fin_networth(fin) <= 0 - credit_limit { return 1 } 61 return 0 62}