Skip to content

Commit 0973799

Browse files
committed
Substrate unification, finished: split counters + drop 9 more fibs arrays
Counter split in phi_pi_fib.rs: - EXPLICIT_SEARCHES / EXPLICIT_COMPARISONS bumped by direct calls to public search fns (fibonacci_search, fibonacci_search_with_trace, phi_pi_fib_search_v2, binary_search). These are searches an OMC program asks for via the phi_pi_fib_* / phi_pi_bin_search builtins. - BACKGROUND_SEARCHES / BACKGROUND_COMPARISONS bumped by substrate-internal callers — nearest_attractor_with_dist and friends. Every HInt::new -> compute_resonance -> ... goes here. Surfaces via the new fibonacci_search_internal helper. Public API: get_search_stats() -> explicit channel (pre-refactor semantics) get_search_stats_background() -> NEW get_search_stats_all() -> NEW (sum) reset_search_stats() -> resets both OMC builtins: phi_pi_fib_stats() -> explicit (unchanged) phi_pi_fib_stats_bg() -> NEW background phi_pi_fib_stats_all() -> NEW total Effect: experiment 7's "gate work" reads back its pre-refactor 24 compares (was 857 after step 1 of the refactor confused background HInt construction work with explicit search work). All per-N compare averages in exp 7 also restored: N=8: 3.79, N=1024: 12.57. Mechanical follow-up — 9 more fibs arrays deleted: bytecode_opt.rs::fold_to_fib_const -> phi_pi_fib::fold_to_nearest_attractor vm.rs::fold_to_fibonacci -> phi_pi_fib::fold_to_nearest_attractor interpreter.rs Expression::Fold -> ditto interpreter.rs fold_escape -> ditto + zero-trap escape interpreter.rs safe_divide (fold mode) -> ditto + zero-trap escape interpreter.rs arr_fold_elements -> ditto (abs path) interpreter.rs resolve_singularity (fold) -> ditto interpreter.rs harmonic_partition -> ditto (used as bucket key) interpreter.rs phi_fold_n method -> ditto (depth-N iteration) One last fibs: site intentionally kept: interpreter.rs harmonic_split (line 3630) That one selects the LARGEST Fibonacci <= remaining string length for chunk-size determination. Different semantic from "nearest" — adding a substrate primitive for it is a separate scope decision. Tally: of the original 17+ duplicate Fibonacci tables across the codebase, 16 are gone. The remaining 1 is preserved with a distinct semantic (largest-LE, not nearest). 148/148 tests pass. All 10 hybrid_llm experiments audit byte-identical across tree-walk and bytecode VM. Conformance unchanged.
1 parent fe776fb commit 0973799

4 files changed

Lines changed: 138 additions & 141 deletions

File tree

omnimcode-core/src/bytecode_opt.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,8 @@ fn unary_cache_pass(func: &mut CompiledFunction, stats: &mut OptStats) {
169169
}
170170

171171
fn fold_to_fib_const(n: i64) -> i64 {
172-
let fibs: [i64; 15] = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610];
173-
let abs_val = n.abs();
174-
let mut nearest = fibs[0];
175-
let mut min_dist = abs_val;
176-
for &f in &fibs {
177-
let d = (f - abs_val).abs();
178-
if d < min_dist {
179-
min_dist = d;
180-
nearest = f;
181-
}
182-
}
183-
if n < 0 { -nearest } else { nearest }
172+
// Substrate-routed. Was: 15-element local Fibonacci array + linear scan.
173+
crate::phi_pi_fib::fold_to_nearest_attractor(n)
184174
}
185175

186176
/// Collapse `Not; Not` (and similar double-unary ops) to no-op.

omnimcode-core/src/interpreter.rs

Lines changed: 37 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,18 +1536,7 @@ impl Interpreter {
15361536
let v = self.eval_expr(e)?;
15371537
match v {
15381538
Value::HInt(h) => {
1539-
let fibs: [i64; 15] = [0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610];
1540-
let abs_val = h.value.abs();
1541-
let mut nearest = fibs[0];
1542-
let mut min_dist = abs_val;
1543-
for &fib in &fibs {
1544-
let d = (fib - abs_val).abs();
1545-
if d < min_dist {
1546-
min_dist = d;
1547-
nearest = fib;
1548-
}
1549-
}
1550-
let result = if h.value < 0 { -nearest } else { nearest };
1539+
let result = crate::phi_pi_fib::fold_to_nearest_attractor(h.value);
15511540
Ok(Value::HInt(HInt::new(result)))
15521541
}
15531542
_ => Ok(Value::HInt(HInt::new(0))),
@@ -1668,6 +1657,8 @@ impl Interpreter {
16681657
| "phi_pi_bin_search" | "log_phi_pi_fibonacci"
16691658
// Traced variants — return [result, probe_indices_array]
16701659
| "phi_pi_fib_search_traced" | "phi_pi_fib_nearest_traced"
1660+
// Split-channel stats (explicit vs background substrate work)
1661+
| "phi_pi_fib_stats_bg" | "phi_pi_fib_stats_all"
16711662
// Self-healing
16721663
| "safe_divide" | "safe_arr_get" | "safe_arr_set"
16731664
| "safe_add" | "safe_sub" | "safe_mul" | "resolve_singularity"
@@ -2181,20 +2172,7 @@ impl Interpreter {
21812172
if danger > 0.5 {
21822173
// Snap to nearest Fibonacci, preserve sign.
21832174
let n = v.to_int();
2184-
let fibs: [i64; 15] = [
2185-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
2186-
];
2187-
let abs_n = n.abs();
2188-
let mut nearest = fibs[0];
2189-
let mut min_dist = abs_n;
2190-
for &fib in &fibs {
2191-
let d = (fib - abs_n).abs();
2192-
if d < min_dist {
2193-
min_dist = d;
2194-
nearest = fib;
2195-
}
2196-
}
2197-
let result = if n < 0 { -nearest } else { nearest };
2175+
let result = crate::phi_pi_fib::fold_to_nearest_attractor(n);
21982176
// The point of fold_escape is to escape the zero-trap:
21992177
// if the nearest Fibonacci is 0 (which happens for x=0),
22002178
// jump to 1 instead. Otherwise we'd just heal back to
@@ -2233,20 +2211,7 @@ impl Interpreter {
22332211
let divisor = if danger > 0.5 {
22342212
// Fold b away from zero.
22352213
let n = b.to_int();
2236-
let fibs: [i64; 15] = [
2237-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
2238-
];
2239-
let abs_n = n.abs();
2240-
let mut nearest = fibs[0];
2241-
let mut min_dist = abs_n;
2242-
for &fib in &fibs {
2243-
let d = (fib - abs_n).abs();
2244-
if d < min_dist {
2245-
min_dist = d;
2246-
nearest = fib;
2247-
}
2248-
}
2249-
let mut healed = if n < 0 { -nearest } else { nearest };
2214+
let mut healed = crate::phi_pi_fib::fold_to_nearest_attractor(n);
22502215
if healed == 0 {
22512216
healed = 1;
22522217
}
@@ -2399,19 +2364,11 @@ impl Interpreter {
23992364
if let Value::Array(arr) = arr_v {
24002365
let mut acc = 0i64;
24012366
for v in arr.items.borrow().iter() {
2402-
let n = v.to_int().abs();
2403-
let fibs: [i64; 15] = [
2404-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
2405-
];
2406-
let mut nearest = fibs[0];
2407-
let mut min_dist = n;
2408-
for &f in &fibs {
2409-
let d = (f - n).abs();
2410-
if d < min_dist {
2411-
min_dist = d;
2412-
nearest = f;
2413-
}
2414-
}
2367+
// .abs() before fold matches the prior behaviour
2368+
// (always positive attractor accumulated).
2369+
let nearest = crate::phi_pi_fib::fold_to_nearest_attractor(
2370+
v.to_int().abs(),
2371+
);
24152372
acc = acc.wrapping_add(nearest);
24162373
}
24172374
Ok(Value::HInt(HInt::new(acc)))
@@ -2476,20 +2433,7 @@ impl Interpreter {
24762433
let resolved = match mode.as_str() {
24772434
"fold" => {
24782435
// Snap |numerator| to nearest Fibonacci, preserve sign.
2479-
let fibs: [i64; 15] = [
2480-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
2481-
];
2482-
let abs_n = numerator.abs();
2483-
let mut nearest = fibs[0];
2484-
let mut min_dist = abs_n;
2485-
for &fib in &fibs {
2486-
let d = (fib - abs_n).abs();
2487-
if d < min_dist {
2488-
min_dist = d;
2489-
nearest = fib;
2490-
}
2491-
}
2492-
if numerator < 0 { -nearest } else { nearest }
2436+
crate::phi_pi_fib::fold_to_nearest_attractor(numerator)
24932437
}
24942438
"invert" => {
24952439
// 1/n style: return signed inverse magnitude.
@@ -3726,20 +3670,12 @@ impl Interpreter {
37263670
return Err("harmonic_partition requires (array)".to_string());
37273671
}
37283672
if let Value::Array(arr) = self.eval_expr(&args[0])? {
3729-
let fibs: [i64; 15] = [
3730-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
3731-
];
37323673
use std::collections::BTreeMap;
37333674
let mut buckets: BTreeMap<i64, Vec<Value>> = BTreeMap::new();
37343675
let items_in = arr.items.borrow().clone();
37353676
for v in items_in {
37363677
let n = v.to_int();
3737-
let abs_n = n.abs();
3738-
let nearest = fibs.iter()
3739-
.min_by_key(|f| (**f - abs_n).abs())
3740-
.copied()
3741-
.unwrap_or(0);
3742-
let key = if n < 0 { -nearest } else { nearest };
3678+
let key = crate::phi_pi_fib::fold_to_nearest_attractor(n);
37433679
buckets.entry(key).or_insert_with(Vec::new).push(v);
37443680
}
37453681
let outer: Vec<Value> = buckets.into_iter().map(|(_, items)| {
@@ -4027,11 +3963,33 @@ impl Interpreter {
40273963
];
40283964
Ok(Value::Array(HArray::from_vec(items)))
40293965
}
4030-
// phi_pi_fib_reset() -> null. Zero the phi_pi_fib counters.
3966+
// phi_pi_fib_reset() -> null. Zero both phi_pi_fib counter
3967+
// channels (explicit AND background).
40313968
"phi_pi_fib_reset" => {
40323969
crate::phi_pi_fib::reset_search_stats();
40333970
Ok(Value::Null)
40343971
}
3972+
// phi_pi_fib_stats_bg() -> [searches, comparisons] for the
3973+
// BACKGROUND channel — substrate-internal calls
3974+
// (HInt::new -> compute_resonance -> nearest_attractor_with_dist).
3975+
"phi_pi_fib_stats_bg" => {
3976+
let s = crate::phi_pi_fib::get_search_stats_background();
3977+
let items = vec![
3978+
Value::HInt(HInt::new(s.total_searches as i64)),
3979+
Value::HInt(HInt::new(s.total_comparisons as i64)),
3980+
];
3981+
Ok(Value::Array(HArray::from_vec(items)))
3982+
}
3983+
// phi_pi_fib_stats_all() -> [searches, comparisons] summed
3984+
// across explicit + background channels.
3985+
"phi_pi_fib_stats_all" => {
3986+
let s = crate::phi_pi_fib::get_search_stats_all();
3987+
let items = vec![
3988+
Value::HInt(HInt::new(s.total_searches as i64)),
3989+
Value::HInt(HInt::new(s.total_comparisons as i64)),
3990+
];
3991+
Ok(Value::Array(HArray::from_vec(items)))
3992+
}
40353993
// phi_pi_fib_search_v2(sorted_arr, target) -> int
40363994
// F(k)/φ^(π·k) split-point search. Same return convention
40373995
// as phi_pi_fib_search (exact match index, or -(insert+1)).
@@ -5182,6 +5140,7 @@ pub(crate) const HEAL_BUILTIN_NAMES: &[&str] = &[
51825140
"phi_pi_fib_search_v2", "phi_pi_fib_nearest_v2",
51835141
"phi_pi_bin_search", "log_phi_pi_fibonacci",
51845142
"phi_pi_fib_search_traced", "phi_pi_fib_nearest_traced",
5143+
"phi_pi_fib_stats_bg", "phi_pi_fib_stats_all",
51855144
// Self-healing
51865145
"safe_divide", "safe_arr_get", "safe_arr_set",
51875146
"safe_add", "safe_sub", "safe_mul", "resolve_singularity",
@@ -5205,21 +5164,8 @@ impl Interpreter {
52055164
match v {
52065165
Value::HInt(h) => {
52075166
let mut current = h.value;
5208-
let fibs: [i64; 15] = [
5209-
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
5210-
];
52115167
for _ in 0..depth.max(1) {
5212-
let abs_val = current.abs();
5213-
let mut nearest = fibs[0];
5214-
let mut min_dist = abs_val;
5215-
for &fib in &fibs {
5216-
let d = (fib - abs_val).abs();
5217-
if d < min_dist {
5218-
min_dist = d;
5219-
nearest = fib;
5220-
}
5221-
}
5222-
current = if current < 0 { -nearest } else { nearest };
5168+
current = crate::phi_pi_fib::fold_to_nearest_attractor(current);
52235169
}
52245170
Value::HInt(HInt::new(current))
52255171
}

0 commit comments

Comments
 (0)