@@ -2104,7 +2104,7 @@ impl Interpreter {
21042104 } else if lv.is_float() || rv.is_float() {
21052105 Ok(Value::HFloat(lv.to_float() + rv.to_float()))
21062106 } else {
2107- Ok(Value::HInt(HInt::new(lv .to_int().wrapping_add(rv.to_int()))))
2107+ Ok(db_int(&lv, &rv, lv .to_int().wrapping_add(rv.to_int()), |a, b| a.wrapping_add(b )))
21082108 }
21092109 }
21102110 Expression::Sub(l, r) => {
@@ -2113,7 +2113,7 @@ impl Interpreter {
21132113 if lv.is_float() || rv.is_float() {
21142114 Ok(Value::HFloat(lv.to_float() - rv.to_float()))
21152115 } else {
2116- Ok(Value::HInt(HInt::new(lv .to_int().wrapping_sub(rv.to_int()))))
2116+ Ok(db_int(&lv, &rv, lv .to_int().wrapping_sub(rv.to_int()), |a, b| a.wrapping_sub(b )))
21172117 }
21182118 }
21192119 Expression::Mul(l, r) => {
@@ -2122,7 +2122,7 @@ impl Interpreter {
21222122 if lv.is_float() || rv.is_float() {
21232123 Ok(Value::HFloat(lv.to_float() * rv.to_float()))
21242124 } else {
2125- Ok(Value::HInt(HInt::new(lv .to_int().wrapping_mul(rv.to_int()))))
2125+ Ok(db_int(&lv, &rv, lv .to_int().wrapping_mul(rv.to_int()), |a, b| a.wrapping_mul(b )))
21262126 }
21272127 }
21282128 Expression::Div(l, r) => {
@@ -2148,7 +2148,7 @@ impl Interpreter {
21482148 context: "div".to_string(),
21492149 })
21502150 } else {
2151- Ok(Value::HInt(HInt::new(lv .to_int() / divisor) ))
2151+ Ok(db_int(&lv, &rv, lv .to_int() / divisor, |a, b| if b == 0 { 0 } else { a / b } ))
21522152 }
21532153 }
21542154 }
@@ -2167,7 +2167,7 @@ impl Interpreter {
21672167 if divisor == 0 {
21682168 Ok(Value::HInt(HInt::new(0)))
21692169 } else {
2170- Ok(Value::HInt(HInt::new(lv .to_int() % divisor) ))
2170+ Ok(db_int(&lv, &rv, lv .to_int() % divisor, |a, b| if b == 0 { 0 } else { a % b } ))
21712171 }
21722172 }
21732173 }
@@ -2181,7 +2181,7 @@ impl Interpreter {
21812181 if e < 0 {
21822182 Ok(Value::HFloat((bv.to_int() as f64).powi(e as i32)))
21832183 } else {
2184- Ok(Value::HInt(HInt::new(bv .to_int().pow(e as u32))))
2184+ Ok(db_int(&bv, &ev, bv .to_int().pow(e as u32), |a, b| a.wrapping_pow(b.max(0) as u32 )))
21852185 }
21862186 }
21872187 }
@@ -2512,6 +2512,8 @@ impl Interpreter {
25122512 | "gen_omc" | "gen_at"
25132513 // HBit dual-band gate (Phase 6) — real two-band resonance/divergence
25142514 | "hbit_harmony" | "hbit_divergence" | "band_divergence" | "band_route"
2515+ // Value-level dual-band (Phase 6 — HBit real at the Value level)
2516+ | "bands" | "value_divergence"
25152517 // Traced variants — return [result, probe_indices_array]
25162518 | "phi_pi_fib_search_traced" | "phi_pi_fib_nearest_traced"
25172519 // Split-channel stats (explicit vs background substrate work)
@@ -11666,34 +11668,64 @@ impl Interpreter {
1166611668 // observation" so subsequent ops carry both bands through
1166711669 // computation. A later harmony() check decides whether
1166811670 // the value is behaving as predicted.
11671+ // phi_shadow(value) — Phase 6: HBit real at the Value level. Attaches the harmonic
11672+ // SHADOW band β = nearest Fibonacci attractor of α. The returned value carries BOTH
11673+ // bands: α (the exact value, unchanged) and β (the "on-lattice" companion). β then
11674+ // rides alongside α through arithmetic (db_int); harmony()/band_divergence() read the
11675+ // accumulated α/β drift. Was an identity stub; now the real dual-band entry point.
1166911676 "phi_shadow" => {
1167011677 if args.is_empty() {
1167111678 return Err("phi_shadow requires (value)".to_string());
1167211679 }
1167311680 let v = self.eval_expr(&args[0])?;
11674- Ok(v)
11675- }
11676- // harmony(x) - HBit harmony reading.
11677- //
11678- // Tree-walk semantics: returns 1000 unconditionally. With
11679- // no β to compare against, harmony is trivially perfect.
11680- // The value's semantic content fits this — in tree-walk
11681- // mode, "harmony" can be read as "agreement between α and
11682- // α" which is always exact.
11683- //
11684- // Dual-band JIT semantics (omnimcode-codegen, Session G):
11685- // intercepted as an intrinsic that emits a call to the
11686- // extern Rust helper computing harmony from the two lanes.
11687- //
11688- // Return convention: i64 in [0, 1000]. 1000 = perfect
11689- // harmony, 0 = maximally divergent. Floats avoided to
11690- // keep the calling convention pure-i64.
11681+ let a = v.to_int();
11682+ let (att, _) = crate::phi_pi_fib::nearest_attractor_with_dist(a);
11683+ Ok(Value::HInt(HInt::with_beta(a, att)))
11684+ }
11685+ // harmony(x) -> int 0..1000 — Phase 6: REAL dual-band reading. If x carries a β shadow
11686+ // (from phi_shadow + dual-band arithmetic), returns 1000·harmony(α,β) = how in-tune the
11687+ // computation stayed with the attractor lattice. If single-band (no β), returns 1000
11688+ // (a value is in perfect harmony with itself). Was a constant 1000 stub.
1169111689 "harmony" => {
1169211690 if args.is_empty() {
1169311691 return Err("harmony requires (value)".to_string());
1169411692 }
11695- let _ = self.eval_expr(&args[0])?;
11696- Ok(Value::HInt(HInt::new(1000)))
11693+ let v = self.eval_expr(&args[0])?;
11694+ match v.beta_band() {
11695+ Some(b) => {
11696+ let h = crate::value::HBit::harmony(v.to_int(), b);
11697+ Ok(Value::HInt(HInt::new((h * 1000.0).round() as i64)))
11698+ }
11699+ None => Ok(Value::HInt(HInt::new(1000))),
11700+ }
11701+ }
11702+ // bands(x) -> [α, β] — Phase 6: inspect both bands of a value. β = α when single-band.
11703+ "bands" => {
11704+ if args.is_empty() {
11705+ return Err("bands requires (value)".to_string());
11706+ }
11707+ let v = self.eval_expr(&args[0])?;
11708+ let a = v.to_int();
11709+ let b = v.beta_band().unwrap_or(a);
11710+ Ok(Value::Array(HArray::from_vec(vec![
11711+ Value::HInt(HInt::new(a)),
11712+ Value::HInt(HInt::new(b)),
11713+ ])))
11714+ }
11715+ // value_divergence(x) -> int 0..1000 — Phase 6: the per-value drift = 1000·(1-harmony).
11716+ // 0 = on the lattice (trust the β skip); high = dissonant (α is the truth). The gate.
11717+ "value_divergence" => {
11718+ if args.is_empty() {
11719+ return Err("value_divergence requires (value)".to_string());
11720+ }
11721+ let v = self.eval_expr(&args[0])?;
11722+ match v.beta_band() {
11723+ Some(b) => {
11724+ let h = crate::value::HBit::harmony(v.to_int(), b);
11725+ Ok(Value::HInt(HInt::new(((1.0 - h) * 1000.0).round() as i64)))
11726+ }
11727+ None => Ok(Value::HInt(HInt::new(0))),
11728+ }
1169711729 }
1169811730 // phi_pi_fib_search_v2(sorted_arr, target) -> int
1169911731 // F(k)/φ^(π·k) split-point search. Same return convention
@@ -15633,6 +15665,23 @@ pub(crate) fn stmts_contain_return(stmts: &[Statement]) -> bool {
1563315665 false
1563415666}
1563515667
15668+ /// Dual-band integer arithmetic (Phase 6 — HBit real at the Value level). When NEITHER operand
15669+ /// carries a β shadow (the default for all ordinary values), returns exactly `HInt::new(alpha)` —
15670+ /// byte-identical to single-band behavior. When either carries β, the shadow rides alongside:
15671+ /// result β = op(lβ, rβ), with missing bands falling back to the operand's α. α is ALWAYS the exact
15672+ /// answer the caller already computed; β never alters it — it only records harmonic drift.
15673+ #[inline]
15674+ fn db_int(lv: &Value, rv: &Value, alpha: i64, op: impl Fn(i64, i64) -> i64) -> Value {
15675+ match (lv.beta_band(), rv.beta_band()) {
15676+ (None, None) => Value::HInt(HInt::new(alpha)),
15677+ (lb, rb) => {
15678+ let la = lb.unwrap_or_else(|| lv.to_int());
15679+ let ra = rb.unwrap_or_else(|| rv.to_int());
15680+ Value::HInt(HInt::with_beta(alpha, op(la, ra)))
15681+ }
15682+ }
15683+ }
15684+
1563615685/// Builtins whose evaluation has a side effect or is nondeterministic. `@memo`
1563715686/// refuses a function that directly calls any of these (best-effort purity gate).
1563815687const MEMO_IMPURE: &[&str] = &[
@@ -15979,6 +16028,7 @@ pub(crate) const HEAL_BUILTIN_NAMES: &[&str] = &[
1597916028 "gen_omc", "gen_at",
1598016029 // HBit dual-band gate (Phase 6)
1598116030 "hbit_harmony", "hbit_divergence", "band_divergence", "band_route",
16031+ "bands", "value_divergence",
1598216032 "phi_pi_fib_search_traced", "phi_pi_fib_nearest_traced",
1598316033 "phi_pi_fib_stats_bg", "phi_pi_fib_stats_all",
1598416034 // HBit dual-band intrinsics (Sessions F+G)
0 commit comments