code wiki / _hdl_build / nx_ledger_data_gate.nx
nx_ledger_data_gate.nx source
↩ module page · 102 lines · 6451 B
1// nx_ledger_data_gate.nx -- ENGINEER gate for LEDGER-AS-DATA (rung AN1). Proves: the .led grammar
2// parses exactly or refuses LOUDLY (malformed/unknown/bad-grade/dangling-ref/dup-T all = 0), the
3// strict independence demotion fires from DATA, narrative-with-value goes BROKEN, the SAME binary
4// yields DIFFERENT verdicts from different files (case = data, no recompile), and the REAL
5// ac_vs_pc_v2.led evaluates to the hand-computed v2 answer. license_tier: ORIGINAL
6import "nx_ledger_data.nx"
7import "nx_syscalls.nx"
8
9func lg_wf(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
10func lg_wnf(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 }
11func lg_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
12
13func main() -> i64 {
14 let r: *i64 = sys_mmap(8*48) as *i64
15 var t: i64 = 0
16 let st: *i64 = sys_mmap(8*24) as *i64
17 let out: *i64 = sys_mmap(8*12) as *i64
18 let sup: *i64 = sys_mmap(8*4) as *i64
19
20 // KAT1: minimal clean ledger -- two distinct-class (and opposed) sources back the one leaf
21 let g1: *u8 = "T 5000 600 150\nC 1 2\nS 1 1 1 5 1\nS 1 1 1 5 2\nN 0 1000 2\nN 0 1000 1\nB 1 0\nB 1 1\n" as *u8
22 r[t]=0; if ld_parse(g1, lg_len(g1), st) == 1 { r[t]=1 } t=t+1
23 r[t]=0; if st[LD_NS] == 2 { r[t]=1 } t=t+1
24 r[t]=0; if st[LD_NN] == 2 { r[t]=1 } t=t+1
25 r[t]=0; if st[LD_NB] == 2 { r[t]=1 } t=t+1
26 ld_eval(st, out)
27 r[t]=0; if out[3] == AN_EXPLAINED { r[t]=1 } t=t+1
28 r[t]=0; if out[1] == 1000 { r[t]=1 } t=t+1 // full coverage
29 r[t]=0; if out[6] == 0 { r[t]=1 } t=t+1 // no demotions
30 r[t]=0; if out[5] == SG_CONF_HIGH { r[t]=1 } t=t+1
31 r[t]=0; if ld_node_support(st, 1, sup) == 2 { r[t]=1 } t=t+1
32 r[t]=0; if sup[0] == 1 { r[t]=1 } t=t+1 // opposed pair detected from the C row
33 let vd_clean: i64 = out[3]
34
35 // KAT2: ECHO ledger -- same shape, both sources same class -> demoted from DATA, UNDERTRACED
36 let g2: *u8 = "T 5000 600 150\nS 1 1 1 5 1\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 1\nB 1 0\nB 1 1\n" as *u8
37 r[t]=0; if ld_parse(g2, lg_len(g2), st) == 1 { r[t]=1 } t=t+1
38 ld_eval(st, out)
39 r[t]=0; if out[6] == 1 { r[t]=1 } t=t+1 // the leaf was demoted
40 r[t]=0; if out[3] == AN_UNDERTRACED { r[t]=1 } t=t+1
41 // KAT3: the no-recompile proof -- same binary, different file, different verdict
42 r[t]=0; if vd_clean != out[3] { r[t]=1 } t=t+1
43
44 // KAT4: NARRATIVE node with a value -> BROKEN (common wisdom cannot ride in via data files)
45 let g3: *u8 = "T 5000 600 150\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 0\nB 1 0\n" as *u8
46 r[t]=0; if ld_parse(g3, lg_len(g3), st) == 1 { r[t]=1 } t=t+1
47 ld_eval(st, out)
48 r[t]=0; if out[3] == AN_BROKEN { r[t]=1 } t=t+1
49
50 // KAT5: LOUD refusals -- every malformation parses to 0, never a partial answer
51 let b1: *u8 = "T 5000 600 150\nX 1 2\n" as *u8
52 r[t]=0; if ld_parse(b1, lg_len(b1), st) == 0 { r[t]=1 } t=t+1 // unknown record letter
53 let b2: *u8 = "S 1 1 1 5 1\nN 0 1000 2\nN 0 1000 1\n" as *u8
54 r[t]=0; if ld_parse(b2, lg_len(b2), st) == 0 { r[t]=1 } t=t+1 // no T row
55 let b3: *u8 = "T 5000 600 150\nS 7 1 1 5 1\nN 0 1000 2\nN 0 1000 1\n" as *u8
56 r[t]=0; if ld_parse(b3, lg_len(b3), st) == 0 { r[t]=1 } t=t+1 // invalid Admiralty grade
57 let b4: *u8 = "T 5000 600 150\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 1\nB 1 5\n" as *u8
58 r[t]=0; if ld_parse(b4, lg_len(b4), st) == 0 { r[t]=1 } t=t+1 // dangling source ref
59 let b5: *u8 = "T 5000 600 150\nT 5000 600 150\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 1\n" as *u8
60 r[t]=0; if ld_parse(b5, lg_len(b5), st) == 0 { r[t]=1 } t=t+1 // duplicate T
61 let b6: *u8 = "T 5000 600\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 1\n" as *u8
62 r[t]=0; if ld_parse(b6, lg_len(b6), st) == 0 { r[t]=1 } t=t+1 // T missing a field
63 let b7: *u8 = "T 5000 600 150\nS 1 1 1 5 1\nN 0 1000 2\nN 0 1000 9\n" as *u8
64 r[t]=0; if ld_parse(b7, lg_len(b7), st) == 0 { r[t]=1 } t=t+1 // bad kind
65
66 // KAT6: the REAL v2 case file -- the whole AN8 answer, hand-computed
67 r[t]=0; if ld_parse_file("knowledge/analyst/ac_vs_pc_v2.led" as *u8, st) == 1 { r[t]=1 } t=t+1
68 r[t]=0; if st[LD_NS] == 17 { r[t]=1 } t=t+1
69 r[t]=0; if st[LD_NN] == 6 { r[t]=1 } t=t+1
70 r[t]=0; if st[LD_NB] == 15 { r[t]=1 } t=t+1
71 ld_eval(st, out)
72 r[t]=0; if out[7] == 0 { r[t]=1 } t=t+1 // ledger closes exactly
73 r[t]=0; if out[6] == 1 { r[t]=1 } t=t+1 // exactly ONE demotion (materials, still single-class)
74 r[t]=0; if out[0] == 491750 { r[t]=1 } t=t+1 // traced cents
75 r[t]=0; if out[1] == 855 { r[t]=1 } t=t+1 // coverage permil
76 r[t]=0; if out[2] == 144 { r[t]=1 } t=t+1 // residual permil
77 r[t]=0; if out[3] == AN_EXPLAINED { r[t]=1 } t=t+1 // v2 verdict: the price IS its traced costs
78 r[t]=0; if out[4] == 122 { r[t]=1 } t=t+1 // weakest admitted leg = margin's vendor leg
79 r[t]=0; if out[5] == SG_CONF_LOW { r[t]=1 } t=t+1 // confidence stays honest-LOW
80 // margin node (4): opposed classes agree; labor node (3): weaker leg = manufacturer-hours 217
81 r[t]=0; if ld_node_support(st, 4, sup) == 2 { r[t]=1 } t=t+1
82 r[t]=0; if sup[0] == 1 { r[t]=1 } t=t+1
83 ld_node_support(st, 3, sup)
84 r[t]=0; if sup[1] == 217 { r[t]=1 } t=t+1
85
86 var pass: i64 = 0
87 var i: i64 = 0
88 while i < t { pass = pass + r[i]; i = i + 1 }
89 lg_wf(1, "=== LEDGER-AS-DATA GATE (Engineer): grammar, loud refusals, data-driven verdicts ===\n" as *u8)
90 var j: i64 = 0
91 while j < t { if r[j] == 0 { lg_wf(1, " FAIL kat#" as *u8); lg_wnf(1, j); lg_wf(1, "\n" as *u8) } j = j + 1 }
92 lg_wf(1, " passed " as *u8); lg_wnf(1, pass); lg_wf(1, "/" as *u8); lg_wnf(1, t); lg_wf(1, "\n" as *u8)
93 let dfd: i64 = sys_openat_append("knowledge/status/analyst_led_gate.log" as *u8, 0x1a4)
94 if dfd >= 0 {
95 if pass == t { lg_wf(dfd, "ANALYST-LED-GATE verdict=GREEN " as *u8) }
96 if pass != t { lg_wf(dfd, "ANALYST-LED-GATE verdict=RED " as *u8) }
97 lg_wnf(dfd, pass); lg_wf(dfd, "/" as *u8); lg_wnf(dfd, t); lg_wf(dfd, "\n" as *u8)
98 sys_close(dfd)
99 }
100 if pass == t { sys_exit(0); return 0 }
101 sys_exit(1); return 1
102}