code wiki / _hdl_build / nx_market_cmp.nx

nx_market_cmp.nx source

↩ module page · 202 lines · 7826 B

1// nx_market_cmp.nx -- CROSS-MARKET COMPARISON library (arc=ANALYST rung AN10): the operator's 2// "wholesale feedback, overseas feedback -- ALL the information so it is irrefutable". The 3// irrefutability move: the v2 explanation ("the price is the delivery STRUCTURE, not the machine") 4// makes a TESTABLE cross-border prediction -- equipment is globally traded so its per-ton price 5// should be roughly comparable across markets, while the structure layer (labor/licensing/margin) 6// is local and should vary wildly. This organ MEASURES both spreads from a .cmp data file and 7// issues a deterministic verdict: STRUCTURE-DOMINANT iff the structure-per-ton spread exceeds the 8// equipment-per-ton spread. Same independence rule as .led: a market backed by <2 distinct 9// incentive classes is PROVISIONAL (named, never silently trusted). 10// Grammar (integers only; '#'/blank = comment): 11// T <ref_market> (exactly one; ratios are vs this market) 12// S <rel> <cred> <party> <method> <class> (source; idx = order) 13// M <price_cents> <ton_x10> <equip_cents> (market; idx = order) 14// B <market> <src> (backing edge) 15// Composes nx_ledger_data (int/line parsing helpers) + nx_source_grade (grades, independence). 16// LAWS: struct-free, integer-only, flat ifs, <=6 args. license_tier: ORIGINAL 17import "nx_ledger_data.nx" 18import "nx_syscalls.nx" 19const MC_MAGIC_1024: i64 = 1024 20 21const MC_MAX_SRC: i64 = 32 22const MC_MAX_MKT: i64 = 16 23const MC_MAX_EDGE: i64 = 64 24 25// state-table slots 26const MC_REL: i64 = 0 27const MC_CRED: i64 = 1 28const MC_PARTY: i64 = 2 29const MC_METHOD: i64 = 3 30const MC_CLASS: i64 = 4 31const MC_PRICE: i64 = 5 32const MC_TON: i64 = 6 33const MC_EQUIP: i64 = 7 34const MC_BMKT: i64 = 8 35const MC_BSRC: i64 = 9 36const MC_NS: i64 = 10 37const MC_NM: i64 = 11 38const MC_NB: i64 = 12 39const MC_REF: i64 = 13 40const MC_NT: i64 = 14 41 42func mc_alloc(st: *i64) -> i64 { 43 st[MC_REL] = sys_mmap(8*MC_MAX_SRC) as i64 44 st[MC_CRED] = sys_mmap(8*MC_MAX_SRC) as i64 45 st[MC_PARTY] = sys_mmap(8*MC_MAX_SRC) as i64 46 st[MC_METHOD] = sys_mmap(8*MC_MAX_SRC) as i64 47 st[MC_CLASS] = sys_mmap(8*MC_MAX_SRC) as i64 48 st[MC_PRICE] = sys_mmap(8*MC_MAX_MKT) as i64 49 st[MC_TON] = sys_mmap(8*MC_MAX_MKT) as i64 50 st[MC_EQUIP] = sys_mmap(8*MC_MAX_MKT) as i64 51 st[MC_BMKT] = sys_mmap(8*MC_MAX_EDGE) as i64 52 st[MC_BSRC] = sys_mmap(8*MC_MAX_EDGE) as i64 53 st[MC_NS]=0; st[MC_NM]=0; st[MC_NB]=0; st[MC_REF]=0; st[MC_NT]=0 54 return 0 55} 56 57func mc_line(b: *u8, i: i64, e: i64, st: *i64) -> i64 { 58 let s: i64 = ld_skip(b, i, e) 59 if s >= e { return 1 } 60 let c: i64 = b[s] as i64 61 if c == 35 { return 1 } 62 let v: *i64 = sys_mmap(8*8) as *i64 63 if c == 84 { // 'T' 64 if ld_ints(b, s+1, e, 1, v) == 0 { return 0 } 65 st[MC_REF] = v[0] 66 st[MC_NT] = st[MC_NT] + 1 67 return 1 68 } 69 if c == 83 { // 'S' 70 if ld_ints(b, s+1, e, 5, v) == 0 { return 0 } 71 if st[MC_NS] >= MC_MAX_SRC { return 0 } 72 if sg_valid(v[0], v[1], v[2], v[3]) == 0 { return 0 } 73 let a0: *i64 = st[MC_REL] as *i64 74 let a1: *i64 = st[MC_CRED] as *i64 75 let a2: *i64 = st[MC_PARTY] as *i64 76 let a3: *i64 = st[MC_METHOD] as *i64 77 let a4: *i64 = st[MC_CLASS] as *i64 78 a0[st[MC_NS]]=v[0]; a1[st[MC_NS]]=v[1]; a2[st[MC_NS]]=v[2]; a3[st[MC_NS]]=v[3]; a4[st[MC_NS]]=v[4] 79 st[MC_NS] = st[MC_NS] + 1 80 return 1 81 } 82 if c == 77 { // 'M' 83 if ld_ints(b, s+1, e, 3, v) == 0 { return 0 } 84 if st[MC_NM] >= MC_MAX_MKT { return 0 } 85 if v[0] <= 0 { return 0 } // price must be positive 86 if v[1] <= 0 { return 0 } // tonnage must be positive 87 if v[2] > v[0] { return 0 } // equipment cannot exceed the installed price 88 let p: *i64 = st[MC_PRICE] as *i64 89 let tn: *i64 = st[MC_TON] as *i64 90 let eq: *i64 = st[MC_EQUIP] as *i64 91 p[st[MC_NM]]=v[0]; tn[st[MC_NM]]=v[1]; eq[st[MC_NM]]=v[2] 92 st[MC_NM] = st[MC_NM] + 1 93 return 1 94 } 95 if c == 66 { // 'B' 96 if ld_ints(b, s+1, e, 2, v) == 0 { return 0 } 97 if st[MC_NB] >= MC_MAX_EDGE { return 0 } 98 if v[0] >= st[MC_NM] { return 0 } 99 if v[1] >= st[MC_NS] { return 0 } 100 let bm: *i64 = st[MC_BMKT] as *i64 101 let bs: *i64 = st[MC_BSRC] as *i64 102 bm[st[MC_NB]]=v[0]; bs[st[MC_NB]]=v[1] 103 st[MC_NB] = st[MC_NB] + 1 104 return 1 105 } 106 return 0 107} 108 109func mc_parse(b: *u8, n: i64, st: *i64) -> i64 { 110 mc_alloc(st) 111 var i: i64 = 0 112 while i < n { 113 let e: i64 = ld_line_end(b, i, n) 114 if mc_line(b, i, e, st) == 0 { return 0 } 115 i = e + 1 116 } 117 if st[MC_NT] != 1 { return 0 } 118 if st[MC_NM] < 2 { return 0 } // a comparison needs at least two markets 119 if st[MC_REF] >= st[MC_NM] { return 0 } 120 return 1 121} 122 123func mc_parse_file(path: *u8, st: *i64) -> i64 { 124 let lenp: *i64 = sys_mmap(16) as *i64 125 let b: *u8 = sys_read_file(path, lenp) 126 if lenp[0] <= 0 { return 0 } 127 return mc_parse(b, lenp[0], st) 128} 129 130// distinct backing classes of market m (the independence rule on markets) 131func mc_distinct_classes(st: *i64, m: i64) -> i64 { 132 let bm: *i64 = st[MC_BMKT] as *i64 133 let bs: *i64 = st[MC_BSRC] as *i64 134 let cl: *i64 = st[MC_CLASS] as *i64 135 let cls: *i64 = sys_mmap(8*(MC_MAX_EDGE+2)) as *i64 136 var nc: i64 = 0 137 var i: i64 = 0 138 while i < st[MC_NB] { 139 var c: i64 = 0 140 var hit: i64 = 0 141 if bm[i] == m { hit = 1; c = cl[bs[i]] } 142 var fresh: i64 = 0 143 if hit == 1 { 144 var seen: i64 = 0 145 var j: i64 = 0 146 while j < nc { if cls[j] == c { seen = 1 } j = j + 1 } 147 if seen == 0 { fresh = 1 } 148 } 149 if fresh == 1 { cls[nc] = c; nc = nc + 1 } 150 i = i + 1 151 } 152 return nc 153} 154 155// ---- per-market metrics (integer permil/q10, deterministic) ---- 156func mc_per_ton(price: i64, ton_x10: i64) -> i64 { return (price * 10) / ton_x10 } 157func mc_equip_share_permil(price: i64, equip: i64) -> i64 { return (equip * 1000) / price } 158func mc_structure(price: i64, equip: i64) -> i64 { return price - equip } 159func mc_ratio_q10(a: i64, b: i64) -> i64 { if b <= 0 { return 0 } return (a * MC_MAGIC_1024) / b } 160 161// spread of a per-ton metric across markets: max*1024/min (q10). vals computed by the caller. 162func mc_spread_q10(vals: *i64, n: i64) -> i64 { 163 var mx: i64 = vals[0] 164 var mn: i64 = vals[0] 165 var i: i64 = 1 166 while i < n { 167 if vals[i] > mx { mx = vals[i] } 168 if vals[i] < mn { mn = vals[i] } 169 i = i + 1 170 } 171 if mn <= 0 { return 0 } 172 return (mx * MC_MAGIC_1024) / mn 173} 174 175// full evaluation. out: 0=struct_spread_q10 1=equip_spread_q10 2=dominance_q10 176// 3=verdict(1=STRUCTURE-DOMINANT 0=not) 4=provisional_markets (backed by <2 classes) 177func mc_eval(st: *i64, out: *i64) -> i64 { 178 let p: *i64 = st[MC_PRICE] as *i64 179 let tn: *i64 = st[MC_TON] as *i64 180 let eq: *i64 = st[MC_EQUIP] as *i64 181 let nm: i64 = st[MC_NM] 182 let spt: *i64 = sys_mmap(8*(MC_MAX_MKT+2)) as *i64 183 let ept: *i64 = sys_mmap(8*(MC_MAX_MKT+2)) as *i64 184 var i: i64 = 0 185 while i < nm { 186 spt[i] = mc_per_ton(mc_structure(p[i], eq[i]), tn[i]) 187 ept[i] = mc_per_ton(eq[i], tn[i]) 188 i = i + 1 189 } 190 let ss: i64 = mc_spread_q10(spt, nm) 191 let es: i64 = mc_spread_q10(ept, nm) 192 var prov: i64 = 0 193 var m: i64 = 0 194 while m < nm { if mc_distinct_classes(st, m) < 2 { prov = prov + 1 } m = m + 1 } 195 out[0] = ss 196 out[1] = es 197 out[2] = mc_ratio_q10(ss, es) 198 out[3] = 0 199 if ss > es { out[3] = 1 } 200 out[4] = prov 201 return 1 202}