code wiki / _hdl_build / nx_flip_real.nx
nx_flip_real.nx source
↩ module page · 276 lines · 13025 B
1// nx_flip_real.nx -- REAL-VALUE vs NOMINAL-PRICE discriminator + triangulated manipulation gate.
2// Value = bottom-up NET TANGIBLE (goodwill/intangibles EXCLUDED as accounting air). A balance sheet
3// IS a part list. backing_bps = net_tangible*10000/nominal ; air_bps = 10000-backing.
4// MANIPULATION GATE (feedback-market-intelligence-captain-moroni-discipline): PROVEN only on THREE
5// independent evidence sources; <3 = UNPROVEN (reported, never acted on, never amplified). No concept
6// of a group or person by construction. COVERAGE: TANGIBLE with no offsetting LIABILITY = REFUSED.
7// PRECEDENCE: manipulation(3) > incomplete(4) > value.
8//
9// CONVERTED ONTO THE SHARED BASE nx_flip_lib.nx (fx_*); only fr_ev_present (evidence-cell presence)
10// stays local. Equivalence PROVEN by nx_flip_gate staying 16/16 across the swap.
11// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
12import "nx_flip_lib.nx"
13import "nx_syscalls.nx"
14
15const FR_ARGC_MIN: i64 = 5
16const FR_ARG_PFX: i64 = 5
17const FR_EVIDENCE_N: i64 = 3
18const FR_CFG_MINBACK: i64 = 1
19const FR_CFG_REQTRI: i64 = 3
20const FR_CFG_REQLIAB: i64 = 5
21const FR_V_ASSET: i64 = 1
22const FR_V_COMP: i64 = 2
23const FR_V_KIND: i64 = 3
24const FR_V_AMT: i64 = 4
25const FR_M_ASSET: i64 = 1
26const FR_M_PRACTICE: i64 = 2
27const FR_M_EV0: i64 = 3
28
29// an evidence cell counts ONLY if non-empty and not the '-' placeholder (real-specific)
30func fr_ev_present(b: *u8, ls: i64, le: i64, idx: i64, sp: *i64) -> i64 {
31 if fx_col(b, ls, le, idx, sp) == 0 { return 0 }
32 if sp[1] <= sp[0] { return 0 }
33 if fx_slice_eqs(b, sp[0], sp[1], "-" as *u8) == 1 { return 0 }
34 return 1
35}
36
37func main(argc: i64, argv: *i64) -> i64 {
38 if argc < FR_ARGC_MIN {
39 fx_puts("usage: nx_flip_real <class> <asset> <nominal-price> <days> [plane-prefix-root]\n" as *u8)
40 fx_puts(" real value is read from <root>val- ; manipulation flags from <root>manip- ; thresholds from <root>realcfg-\n" as *u8)
41 sys_exit(FX_EXIT_USAGE)
42 return FX_EXIT_USAGE
43 }
44 let cls: *u8 = argv[1] as *u8
45 let asset: *u8 = argv[2] as *u8
46 let nominal: i64 = fx_atoi(argv[3] as *u8)
47 let days: i64 = fx_atoi(argv[4] as *u8)
48
49 var root: *u8 = "flip-" as *u8
50 if argc > FR_ARG_PFX { root = argv[FR_ARG_PFX] as *u8 }
51 let ncp: *i64 = sts_mm(FX_SCRATCH) as *i64
52 let nvp: *i64 = sts_mm(FX_SCRATCH) as *i64
53 let nmp: *i64 = sts_mm(FX_SCRATCH) as *i64
54 let bc: *u8 = fx_plane(root, "realcfg-" as *u8, ncp)
55 let bv: *u8 = fx_plane(root, "val-" as *u8, nvp)
56 let bm: *u8 = fx_plane(root, "manip-" as *u8, nmp)
57 let nc: i64 = ncp[0]
58 let nv: i64 = nvp[0]
59 let nm: i64 = nmp[0]
60
61 let sp: *i64 = sts_mm(FX_SCRATCH) as *i64
62 let cr: *i64 = sts_mm(FX_SCRATCH) as *i64
63 let cs: *i64 = sts_mm(FX_SCRATCH) as *i64
64 let pb: *u8 = sts_mm(FX_BUF)
65 var po: i64 = 0
66
67 if nc == 0 {
68 fx_puts("FLIP-RED reason=realcfg-plane-empty prefix=" as *u8); fx_puts(fx_cat2(root, "realcfg-" as *u8)); fx_puts("\n" as *u8)
69 sys_exit(FX_EXIT_REFUSED)
70 return FX_EXIT_REFUSED
71 }
72 if fx_row_by_id(bc, nc, cls, cr, sp) == 0 {
73 fx_puts("FLIP-RED reason=unknown-real-value-class class=" as *u8); fx_puts(cls); fx_puts("\n" as *u8)
74 sys_exit(FX_EXIT_REFUSED)
75 return FX_EXIT_REFUSED
76 }
77 let min_back: i64 = fx_col_atoi(bc, cr[0], cr[1], FR_CFG_MINBACK, sp)
78 let req_tri: i64 = fx_col_atoi(bc, cr[0], cr[1], FR_CFG_REQTRI, sp)
79
80 // ---- value components: the balance sheet IS a part list ----
81 var tangible: i64 = 0
82 var intangible: i64 = 0
83 var liab: i64 = 0
84 var flow: i64 = 0
85 var n_comp: i64 = 0
86 var n_liab: i64 = 0
87 // ---- DRIVER ATTRIBUTION (added 2026-08-08). The organ already said HOW MUCH of the price
88 // is real (backing_bps); it never said WHICH components drive it. "Exact feedback on what is
89 // driving value" needs a per-driver share AND a partition that reconciles -- a partition is a
90 // claim, so the parts must sum and the residual must be PRINTED, not assumed to be zero.
91 var top_share: i64 = 0
92 var have_top: i64 = 0
93 var sum_pos_bps: i64 = 0
94 var sum_neg_bps: i64 = 0
95 let tops: *i64 = sts_mm(FX_SCRATCH) as *i64
96 var i: i64 = 0
97 while i < nv {
98 let le: i64 = fx_line_end(bv, nv, i)
99 if le > i {
100 if fx_col(bv, i, le, FR_V_ASSET, sp) == 1 {
101 if fx_slice_eqs(bv, sp[0], sp[1], asset) == 1 {
102 n_comp = n_comp + 1
103 fx_col(bv, i, le, FR_V_COMP, cs)
104 let amt: i64 = fx_col_atoi(bv, i, le, FR_V_AMT, sp)
105 var kind: i64 = 0
106 if fx_col(bv, i, le, FR_V_KIND, sp) == 1 {
107 if fx_slice_eqs(bv, sp[0], sp[1], "TANGIBLE" as *u8) == 1 { kind = 1 }
108 if fx_slice_eqs(bv, sp[0], sp[1], "INTANGIBLE" as *u8) == 1 { kind = 2 }
109 if fx_slice_eqs(bv, sp[0], sp[1], "LIABILITY" as *u8) == 1 { kind = 3 }
110 if fx_slice_eqs(bv, sp[0], sp[1], "FLOW" as *u8) == 1 { kind = 4 }
111 }
112 if kind == 1 { tangible = tangible + amt }
113 if kind == 2 { intangible = intangible + amt }
114 if kind == 3 { liab = liab + amt; n_liab = n_liab + 1 }
115 if kind == 4 { flow = flow + amt }
116 // Share of the NOMINAL PRICE this component explains. TANGIBLE adds,
117 // LIABILITY subtracts, INTANGIBLE contributes ZERO to backing by design --
118 // it is the air, and saying so per-component is the whole point.
119 var share: i64 = 0
120 if nominal > 0 {
121 if kind == 1 { share = amt * FX_BPS / nominal; sum_pos_bps = sum_pos_bps + share }
122 if kind == 3 { share = 0 - (amt * FX_BPS / nominal); sum_neg_bps = sum_neg_bps + (0 - share) }
123 }
124 var mag: i64 = share
125 if mag < 0 { mag = 0 - mag }
126 if mag > top_share { top_share = mag; tops[0] = cs[0]; tops[1] = cs[1]; have_top = 1 }
127 po = fx_bcat(pb, po, "COMP " as *u8)
128 po = fx_bcatsl(pb, po, bv, cs[0], cs[1])
129 po = fx_bcat(pb, po, " amount=" as *u8); po = fx_bcatn(pb, po, amt)
130 po = fx_bcat(pb, po, " share_bps=" as *u8); po = fx_bcatn(pb, po, share)
131 po = fx_bcat(pb, po, " kind=" as *u8)
132 if kind == 1 { po = fx_bcat(pb, po, "TANGIBLE-counts" as *u8) }
133 if kind == 2 { po = fx_bcat(pb, po, "INTANGIBLE-EXCLUDED-this-is-the-air" as *u8) }
134 if kind == 3 { po = fx_bcat(pb, po, "LIABILITY-subtracts" as *u8) }
135 if kind == 4 { po = fx_bcat(pb, po, "FLOW-reported-only" as *u8) }
136 if kind == 0 { po = fx_bcat(pb, po, "UNKNOWN-KIND-ignored" as *u8) }
137 po = fx_bcat(pb, po, "\n" as *u8)
138 }
139 }
140 }
141 i = le + 1
142 }
143
144 // ---- manipulation: PROVEN only on 3 independent evidence sources ----
145 var man_proven: i64 = 0
146 var man_unproven: i64 = 0
147 i = 0
148 while i < nm {
149 let le: i64 = fx_line_end(bm, nm, i)
150 if le > i {
151 if fx_col(bm, i, le, FR_M_ASSET, sp) == 1 {
152 if fx_slice_eqs(bm, sp[0], sp[1], asset) == 1 {
153 var ev: i64 = 0
154 var k: i64 = 0
155 while k < FR_EVIDENCE_N {
156 ev = ev + fr_ev_present(bm, i, le, FR_M_EV0 + k, sp)
157 k = k + 1
158 }
159 fx_col(bm, i, le, FR_M_PRACTICE, cs)
160 po = fx_bcat(pb, po, "MANIP " as *u8)
161 po = fx_bcatsl(pb, po, bm, cs[0], cs[1])
162 po = fx_bcat(pb, po, " evidence_sources=" as *u8); po = fx_bcatn(pb, po, ev)
163 po = fx_bcat(pb, po, " status=" as *u8)
164 if ev >= FR_EVIDENCE_N {
165 man_proven = man_proven + 1
166 po = fx_bcat(pb, po, "TRIANGULATED-BLOCKS-TRADE" as *u8)
167 } else {
168 man_unproven = man_unproven + 1
169 po = fx_bcat(pb, po, "UNPROVEN-reported-never-acted-on-never-amplified" as *u8)
170 }
171 po = fx_bcat(pb, po, "\n" as *u8)
172 }
173 }
174 }
175 i = le + 1
176 }
177
178 if nominal <= 0 {
179 fx_puts("FLIP-RED reason=nominal-price-must-be-positive\n" as *u8)
180 fx_puts("verdict=REFUSED\n" as *u8)
181 sys_exit(FX_EXIT_REFUSED)
182 return FX_EXIT_REFUSED
183 }
184 if n_comp == 0 {
185 fx_puts("FLIP-RED reason=no-value-components-for-asset asset=" as *u8); fx_puts(asset); fx_puts("\n" as *u8)
186 fx_puts("verdict=REFUSED\n" as *u8)
187 fx_puts("rule=absent-analysis-is-not-clean-analysis\n" as *u8)
188 sys_exit(FX_EXIT_REFUSED)
189 return FX_EXIT_REFUSED
190 }
191
192 let net_tangible: i64 = tangible - liab
193 let backing: i64 = net_tangible * FX_BPS / nominal
194 let air: i64 = FX_BPS - backing
195
196 var blocked: i64 = 0
197 if req_tri == 1 {
198 if man_proven > 0 { blocked = 1 }
199 } else {
200 if man_proven + man_unproven > 0 { blocked = 1 }
201 }
202
203 let req_liab: i64 = fx_col_atoi(bc, cr[0], cr[1], FR_CFG_REQLIAB, sp)
204 var incomplete: i64 = 0
205 if req_liab == 1 { if n_liab == 0 { incomplete = 1 } }
206
207 var vcode: i64 = 0
208 if blocked == 1 { vcode = 3 } else { if incomplete == 1 { vcode = 4 } else { if backing >= min_back { vcode = 1 } } }
209
210 fx_puts("FLIP-REAL\n" as *u8)
211 fx_puts("class=" as *u8); fx_puts(cls); fx_puts(" asset=" as *u8); fx_puts(asset); fx_puts("\n" as *u8)
212 fx_puts("verdict=" as *u8)
213 if vcode == 3 { fx_puts("REFUSED" as *u8) }
214 if vcode == 4 { fx_puts("REFUSED" as *u8) }
215 if vcode == 1 { fx_puts("BUY" as *u8) }
216 if vcode == 0 { fx_puts("PASS" as *u8) }
217 fx_puts(" liability_rows=" as *u8); fx_putn(n_liab)
218 fx_puts(" backing_bps=" as *u8); fx_putn(backing)
219 fx_puts(" air_bps=" as *u8); fx_putn(air)
220 fx_puts(" manip_proven=" as *u8); fx_putn(man_proven)
221 fx_puts(" manip_unproven=" as *u8); fx_putn(man_unproven)
222 fx_puts("\n" as *u8)
223 fx_puts("rule=" as *u8)
224 if vcode == 3 { fx_puts("triangulated-manipulation-blocks-trade\n" as *u8) }
225 if vcode == 4 { fx_puts("incomplete-balance-sheet-tangible-with-no-offsetting-liability-overstates-real-value\n" as *u8) }
226 if vcode == 1 { fx_puts("net-tangible-value-covers-nominal-price\n" as *u8) }
227 if vcode == 0 { fx_puts("price-not-covered-by-real-value-the-rest-is-inflation\n" as *u8) }
228
229 fx_kv("components" as *u8, n_comp)
230 fx_kv("tangible" as *u8, tangible)
231 fx_kv("liabilities" as *u8, liab)
232 fx_kv("net_tangible" as *u8, net_tangible)
233 fx_kv("intangible_excluded" as *u8, intangible)
234 fx_kv("flow_reported" as *u8, flow)
235 fx_kv("nominal" as *u8, nominal)
236 fx_kv("backing_bps" as *u8, backing)
237
238 // ---- DRIVER ATTRIBUTION: the partition, and it MUST RECONCILE ----
239 // `backing` is computed ONCE from the totals; `sum_pos - sum_neg` sums the SAME quantity
240 // component-by-component. The two can differ only by per-component integer truncation, so
241 // the residual is a genuine check: a large one means a component was double-counted,
242 // dropped, or classified into a bucket that does not participate in value.
243 // NOT printed: a "total_bps" of tangible+air+residual, because that is 10000 BY ALGEBRA no
244 // matter what the data says -- a tautology that prints a number and reads as evidence.
245 let attrib_sum_bps: i64 = sum_pos_bps - sum_neg_bps
246 let residual_bps: i64 = backing - attrib_sum_bps
247 var recon_tol: i64 = n_comp + 1
248 var reconciles: i64 = 0
249 var resmag: i64 = residual_bps
250 if resmag < 0 { resmag = 0 - resmag }
251 if resmag <= recon_tol { reconciles = 1 }
252 fx_kv("attrib_tangible_bps" as *u8, sum_pos_bps)
253 fx_kv("attrib_liability_bps" as *u8, sum_neg_bps)
254 fx_kv("attrib_sum_bps" as *u8, attrib_sum_bps)
255 fx_kv("attrib_air_bps" as *u8, air)
256 fx_kv("attrib_residual_bps" as *u8, residual_bps)
257 fx_kv("attrib_reconciles" as *u8, reconciles)
258 // THE QUALITATIVE HALF, counted rather than left as prose: each practice row is a market
259 // driver that carries no dollar amount, and PROVEN vs UNPROVEN is the triangulation verdict.
260 fx_kv("qual_drivers" as *u8, man_proven + man_unproven)
261 fx_kv("qual_drivers_proven" as *u8, man_proven)
262 if have_top == 1 {
263 fx_puts("top_value_driver=" as *u8)
264 fx_putslice(bv, tops[0], tops[1])
265 fx_puts(" share_bps=" as *u8); fx_putn(top_share)
266 fx_puts("\n" as *u8)
267 }
268 fx_kv("air_bps" as *u8, air)
269 fx_kv("min_backing_bps" as *u8, min_back)
270 fx_kv("require_triangulation" as *u8, req_tri)
271 fx_kv("days" as *u8, days)
272
273 sys_write(FX_OUT, pb, po)
274 sys_exit(0)
275 return 0
276}