code wiki / (root) / nx_caplab_supply.nx

nx_caplab_supply.nx source

↩ module page · 56 lines · 2221 B

1// nx_caplab_supply.nx -- Capitalism Lab R2: production / supply chains. 2// 3// Firms don't conjure finished goods at a magic unit cost -- they convert raw 4// materials through factories into finished products, and the input costs ROLL 5// UP into the unit cost that R0/R1 price against (CLAUDE.md "no magic numbers"). 6// This rung adds: (1) unit-cost rollup across a recipe (inputs + processing), 7// composable stage-by-stage into a chain (raw -> intermediate -> finished), and 8// (2) throughput limited by the SCARCEST input (the bottleneck). Integer, pure. 9// 10// MODEL: 11// unit_cost = processing_cost + sum_i (qty_per[i] * input_price[i]) 12// throughput = min_i ( stock[i] / qty_per[i] ) over inputs with qty_per>0 13// chain = feed a stage's unit_cost in as the next stage's input price 14// 15// nx_safety_envelope: 16// intended_use: capitalism-lab production core (pure functions, no I/O) 17// sil_target: SIL1 18// verdict: NOT_YET_EVALUATED 19// 20// genealogy_id: capitalism_lab_supplychain_canon 21// lineage_id: nx_caplab_supply_v1 22 23import "nx_syscalls.nx" 24import "nx_tier.nx" 25 26// Cost to produce ONE unit of output from a recipe: processing cost plus, for 27// each input, the per-unit quantity times that input's unit price. Chain it by 28// passing a stage's returned cost as the next stage's input price. 29func nx_clab_unit_cost(n_inputs: nx_int, qty_per: *i64, price: *i64, 30 processing_cost: nx_int) -> nx_int { 31 var c: nx_int = processing_cost 32 var i: nx_int = 0 33 while i < n_inputs { 34 c = c + qty_per[i] * price[i] 35 i = i + 1 36 } 37 return c 38} 39 40// Max output units producible from on-hand input stocks: the bottleneck input 41// (smallest stock/qty_per) caps the run. Inputs with qty_per==0 are not 42// constraints (and never divide by zero). 43func nx_clab_throughput(n_inputs: nx_int, qty_per: *i64, stock: *i64) -> nx_int { 44 var best: nx_int = 0 - 1 45 var i: nx_int = 0 46 while i < n_inputs { 47 if qty_per[i] > 0 { 48 let cap: nx_int = stock[i] / qty_per[i] 49 if best < 0 { best = cap } 50 else { if cap < best { best = cap } } 51 } 52 i = i + 1 53 } 54 if best < 0 { return 0 } 55 return best 56}