code wiki / _hdl_build / nx_research_discover_chem_gate.nx

nx_research_discover_chem_gate.nx source

↩ module page · 106 lines · 6780 B

1// nx_research_discover_chem_gate.nx -- DISCOVERY in CHEMISTRY (operator: "discover new capabilities through 2// different math/chem/physics via simulations"). The chem reproduction gate (nx_research_chem_gate) replays 3// KNOWN molar masses. This gate DISCOVERS something it was never given: the stoichiometric coefficients that 4// BALANCE a chemical reaction. 5// 6// A balanced reaction conserves every element: with reactants counted + and products counted -, the integer 7// coefficient vector x must satisfy A x = 0 for the element-count matrix A (rows=elements, cols=species). 8// That is an integer-nullspace problem. The TESTING-TEAM SEARCHES the small positive-integer lattice for the 9// fundamental solution, reduces it to the smallest whole-number ratio (gcd=1), and the VERIFIER independently 10// rechecks atom conservation per element. Liar-kill: the naive all-ones guess (and any unbalanced vector) is 11// REJECTED -- conservation can't be rubber-stamped. Discovered coefficients are compared to the textbook 12// canonical for three classic reactions. Pure integer, deterministic, reproducible. GREEN iff 6/6. 13// license_tier: ORIGINAL 14import "nx_syscalls.nx" 15 16func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 } 18func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } 19 20func gcd(a: i64, b: i64) -> i64 { var x: i64=a; var y: i64=b; if x<0{x=0-x} if y<0{y=0-y} while y!=0 { let t: i64=x%y; x=y; y=t } return x } 21// independent conservation verifier: A x == 0 for every element row. 22func balanced_ok(A: *i64, E: i64, S: i64, x: *i64) -> i64 { 23 var e: i64=0 24 while e<E { var sum: i64=0; var c: i64=0; while c<S { sum=sum+A[e*S+c]*x[c]; c=c+1 } if sum!=0 { return 0 } e=e+1 } 25 return 1 26} 27func veq(a: *i64, b: *i64, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 28 29// SEARCH the positive-integer lattice [1..B]^S (odometer, x[0] least significant) for the first vector with 30// A x = 0; reduce by gcd to the smallest whole-number ratio; write to xout. returns 1 if found. 31func balance(A: *i64, E: i64, S: i64, B: i64, xout: *i64) -> i64 { 32 let x: *i64 = sys_mmap(8*16) as *i64 33 var s: i64=0; while s<S { x[s]=1; s=s+1 } 34 var done: i64=0 35 while done==0 { 36 if balanced_ok(A,E,S,x)==1 { 37 var g: i64=x[0]; var i: i64=1; while i<S { g=gcd(g,x[i]); i=i+1 } 38 i=0; while i<S { xout[i]=x[i]/g; i=i+1 } 39 return 1 40 } 41 var p: i64=0; var carry: i64=1 42 while p<S { if carry==1 { x[p]=x[p]+1; if x[p]>B { x[p]=1 } else { carry=0 } } p=p+1 } 43 if carry==1 { done=1 } 44 } 45 return 0 46} 47func gcd_vec(x: *i64, n: i64) -> i64 { var g: i64=x[0]; var i: i64=1; while i<n { g=gcd(g,x[i]); i=i+1 } return g } 48 49func main() -> i64 { 50 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 51 g_w("=== NX-RESEARCH-DISCOVER-CHEM GATE (DISCOVERY: search balances reactions = integer nullspace, verified by conservation) ===\n") 52 53 // R1: H2 + O2 -> H2O (reactants +, products -) elements H,O 54 let A1: *i64=sys_mmap(8*16) as *i64 55 A1[0]=2; A1[1]=0; A1[2]=-2 // H: 2*H2 + 0*O2 - 2*H2O 56 A1[3]=0; A1[4]=2; A1[5]=-1 // O: 0 + 2*O2 - 1*H2O 57 let x1: *i64=sys_mmap(8*16) as *i64; let f1: i64=balance(A1,2,3,8,x1) 58 let e1: *i64=sys_mmap(8*16) as *i64; e1[0]=2; e1[1]=1; e1[2]=2 59 60 // R2: C3H8 + O2 -> CO2 + H2O elements C,H,O 61 let A2: *i64=sys_mmap(8*16) as *i64 62 A2[0]=3; A2[1]=0; A2[2]=-1; A2[3]=0 // C 63 A2[4]=8; A2[5]=0; A2[6]=0; A2[7]=-2 // H 64 A2[8]=0; A2[9]=2; A2[10]=-2; A2[11]=-1 // O 65 let x2: *i64=sys_mmap(8*16) as *i64; let f2: i64=balance(A2,3,4,8,x2) 66 let e2: *i64=sys_mmap(8*16) as *i64; e2[0]=1; e2[1]=5; e2[2]=3; e2[3]=4 67 68 // R3: Fe + O2 -> Fe2O3 elements Fe,O 69 let A3: *i64=sys_mmap(8*16) as *i64 70 A3[0]=1; A3[1]=0; A3[2]=-2 // Fe 71 A3[3]=0; A3[4]=2; A3[5]=-3 // O 72 let x3: *i64=sys_mmap(8*16) as *i64; let f3: i64=balance(A3,2,3,8,x3) 73 let e3: *i64=sys_mmap(8*16) as *i64; e3[0]=4; e3[1]=3; e3[2]=2 74 75 // LIAR-KILL: the naive all-ones guess does NOT balance H2+O2->H2O 76 let one3: *i64=sys_mmap(8*16) as *i64; one3[0]=1; one3[1]=1; one3[2]=1 77 let allone_bad: i64 = (balanced_ok(A1,2,3,one3)==0) as i64 78 // and a deliberately wrong coeff set for propane is rejected 79 let wrong2: *i64=sys_mmap(8*16) as *i64; wrong2[0]=1; wrong2[1]=5; wrong2[2]=3; wrong2[3]=3 80 let wrong_bad: i64 = (balanced_ok(A2,3,4,wrong2)==0) as i64 81 82 let m1: i64=veq(x1,e1,3); let m2: i64=veq(x2,e2,4); let m3: i64=veq(x3,e3,3) 83 let cons: i64 = balanced_ok(A1,2,3,x1)*balanced_ok(A2,3,4,x2)*balanced_ok(A3,2,3,x3) 84 var canon: i64=0 85 if gcd_vec(x1,3)==1 { if gcd_vec(x2,4)==1 { if gcd_vec(x3,3)==1 { canon=1 } } } 86 87 g_w(" R1 H2+O2->H2O discovered: "); g_n(x1[0]); g_w(",");g_n(x1[1]);g_w(",");g_n(x1[2]); g_w(" (textbook 2,1,2)\n") 88 g_w(" R2 C3H8+O2->CO2+H2O discovered: "); g_n(x2[0]);g_w(",");g_n(x2[1]);g_w(",");g_n(x2[2]);g_w(",");g_n(x2[3]); g_w(" (textbook 1,5,3,4)\n") 89 g_w(" R3 Fe+O2->Fe2O3 discovered: "); g_n(x3[0]);g_w(",");g_n(x3[1]);g_w(",");g_n(x3[2]); g_w(" (textbook 4,3,2)\n") 90 g_w(" conservation(all 3)="); g_n(cons); g_w(" canonical(gcd=1)="); g_n(canon); g_w(" liar-kill allone="); g_n(allone_bad); g_w(" wrong="); g_n(wrong_bad); g_w("\n") 91 92 var d1: i64=0; if f1==1 { if m1==1 { d1=1 } } 93 g_row("DISCOVERY R1: search balances H2+O2->H2O to (2,1,2) = textbook" as *u8, d1, pass) 94 var d2: i64=0; if f2==1 { if m2==1 { d2=1 } } 95 g_row("DISCOVERY R2: search balances C3H8+O2->CO2+H2O to (1,5,3,4) = textbook" as *u8, d2, pass) 96 var d3: i64=0; if f3==1 { if m3==1 { d3=1 } } 97 g_row("DISCOVERY R3: search balances Fe+O2->Fe2O3 to (4,3,2) = textbook" as *u8, d3, pass) 98 g_row("CONSERVATION: independent per-element atom-balance verifier confirms all 3 discovered solutions" as *u8, (cons==1) as i64, pass) 99 g_row("CANONICAL: all 3 discovered coefficient vectors are in smallest whole-number ratio (gcd=1)" as *u8, canon, pass) 100 var liar: i64=0; if allone_bad==1 { if wrong_bad==1 { liar=1 } } 101 g_row("LIAR-KILL: all-ones guess + a wrong coeff set are REJECTED by the conservation verifier" as *u8, liar, pass) 102 103 g_w("RESEARCH-DISCOVER-CHEM-GATE rows=6 pass="); g_n(pass[0]) 104 if pass[0]==6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 105 g_w(" verdict=RED\n"); sys_exit(1); return 1 106}