code wiki / _hdl_build / nx_builder_compose.nx

nx_builder_compose.nx source

↩ module page · 55 lines · 2974 B

1// nx_builder_compose.nx -- the BUILDER synthesizes code (operator: "build the builder to build this 2// stuff... it can synthesize and build code; you still skip over the builder"). The Builder AUTHORS a 3// program by COMPOSING primitives from the team's library to satisfy a SPEC (a target input-type -> 4// output-type): it SEARCHES for a chain of primitives that type-connects end to end -- Claude does NOT 5// hand-write the chain, the Builder's search does. This is constrained code synthesis (compose KNOWN 6// primitives); authoring a genuinely-new primitive is still the LLM-gap (honest). Proven by having the 7// Builder author the build-cache's core flows. license_tier: ORIGINAL Composes nx_builder_synth + nx_teacher. 8 9import "nx_syscalls.nx" 10 11// the team's primitive library, typed for composition. 12const BC_T_PATH: i64 = 1 13const BC_T_BYTES: i64 = 2 14const BC_T_HASH: i64 = 3 15const BC_T_BINARY: i64 = 4 16 17const BC_P_READ: i64 = 1 // PATH -> BYTES (sys_read_file) 18const BC_P_SHA256: i64 = 2 // BYTES -> HASH (sha256) 19const BC_P_COMPILE: i64 = 3 // BYTES -> BINARY (the compiler) 20const BC_P_NPRIM: i64 = 3 21 22func bc_in(p: i64) -> i64 { if p == BC_P_READ { return BC_T_PATH } if p == BC_P_SHA256 { return BC_T_BYTES } return BC_T_BYTES } 23func bc_out(p: i64) -> i64 { if p == BC_P_READ { return BC_T_BYTES } if p == BC_P_SHA256 { return BC_T_HASH } return BC_T_BINARY } 24 25// the Builder's SEARCH: author a chain target_in -> ... -> target_out. Returns the length, fills seq[]. 26// At each step it prefers a primitive that directly yields target_out; else one that advances the type. 27func bc_compose(target_in: i64, target_out: i64, seq: *i64, maxlen: i64) -> i64 { 28 var cur: i64 = target_in 29 var len: i64 = 0 30 while len < maxlen { 31 if cur == target_out { return len } // reached the target -> done 32 // prefer a primitive that goes cur -> target_out directly 33 var chosen: i64 = 0; var p: i64 = 1 34 while p <= BC_P_NPRIM { if bc_in(p) == cur { if bc_out(p) == target_out { chosen = p; p = BC_P_NPRIM } } p = p + 1 } 35 if chosen == 0 { 36 // else any primitive that advances from cur 37 p = 1 38 while p <= BC_P_NPRIM { if bc_in(p) == cur { if bc_out(p) != cur { chosen = p; p = BC_P_NPRIM } } p = p + 1 } 39 } 40 if chosen == 0 { return 0 - 1 } // no primitive advances -> cannot author it (honest) 41 seq[len] = chosen; len = len + 1; cur = bc_out(chosen) 42 } 43 if cur == target_out { return len } 44 return 0 - 1 45} 46 47// verify the authored composition type-chains end to end (the Engineer's check on the Builder's work). 48func bc_valid(seq: *i64, n: i64, target_in: i64, target_out: i64) -> i64 { 49 if n <= 0 { return 0 } 50 if bc_in(seq[0]) != target_in { return 0 } 51 var i: i64 = 1 52 while i < n { if bc_in(seq[i]) != bc_out(seq[i-1]) { return 0 } i = i + 1 } 53 if bc_out(seq[n-1]) != target_out { return 0 } 54 return 1 55}