|
| 1 | +/- |
| 2 | +Copyright 2026 Hyphaeic SPC. |
| 3 | +
|
| 4 | +Licensed under the Hyphaeic Public License, Version 1.0 (the |
| 5 | +"License"); you may not use this file except in compliance with |
| 6 | +the License. You may obtain a copy of the License at |
| 7 | +
|
| 8 | +https://github.com/hyphaeic/hpl |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | +implied. See the License for the specific language governing |
| 14 | +permissions and limitations under the License. |
| 15 | +
|
| 16 | +# Adelic complex — driver correctness (sound representation → provably correct implementation) |
| 17 | +
|
| 18 | +The `ValuationStream` demos *observe* that the p-adic node, on the constant ledger |
| 19 | +`⟨0, n, 0, 1⟩` (the homographic encoding "value = `n`"), emits the base-`p` Hensel digits |
| 20 | +of `n`. This file *proves* it — promoting "demo that matches `Nat.digits`" into a |
| 21 | +**certified transcoder**. |
| 22 | +
|
| 23 | +The proof is an **inductive invariant over the ledger state**, not a fuel-bounded |
| 24 | +simulation. The invariant is that the ledger stays in the canonical constant form |
| 25 | +`⟨0, m, 0, 1⟩`, and a single ready→emit→`reduceOnce` step is *exactly* |
| 26 | +
|
| 27 | +> `run ⟨0, m, 0, 1⟩ … (k+1) = (m % p) :: run ⟨0, m / p, 0, 1⟩ … k` (`run_step`) |
| 28 | +
|
| 29 | +— the ledger transition step **is** the `Nat.digits` recursion `digits p m = |
| 30 | +(m % p) :: digits p (m / p)`. Pinning the fuel to `(Nat.digits p m).length` makes the |
| 31 | +correspondence exact (no trailing-zero slack): |
| 32 | +
|
| 33 | +> **`run_padic_constant_eq_digits`** : `run (padicEngine p) ⟨0, m, 0, 1⟩ [] (digits p m).length |
| 34 | +> = (Nat.digits p m).map (↑·)`. |
| 35 | +
|
| 36 | +Scope (honest): this certifies the *constant-ledger* transcoder (computing a single |
| 37 | +rational's own digits) — the case the `ValuationStream` bridge uses. The general |
| 38 | +homographic transducer (`run` on `M(x)` for a live input stream) and `bihRun` (two-stream |
| 39 | +combination) need the stronger invariant "the ledger tracks `M` applied to the absorbed |
| 40 | +prefix"; those are deferred. Leaf module. No `sorry`; axiom-clean. |
| 41 | +-/ |
| 42 | +import FdrsFormal.Modes.Adelic.PlaceEngine |
| 43 | +import Mathlib.Data.Nat.Digits.Defs |
| 44 | + |
| 45 | +namespace FdrsFormal.Modes.Adelic |
| 46 | + |
| 47 | +/-- **The ledger-invariant step lemma (the heart).** On the canonical constant ledger |
| 48 | +`⟨0, m, 0, 1⟩`, one driver step emits `m % p` and advances the ledger to `⟨0, m / p, 0, 1⟩` |
| 49 | +— the `Nat.digits` recursion, realized by the engine. Proved by computing `ready`, |
| 50 | +`candidate`, and `emitStep` on the canonical ledger (the invariant: the ledger stays |
| 51 | +`⟨0, ·, 0, 1⟩`). -/ |
| 52 | +theorem run_step (p : ℕ) [Fact p.Prime] (j fuel : ℕ) : |
| 53 | + PlaceEngine.run (padicEngine p) ⟨0, (j : ℤ), 0, 1⟩ [] (fuel + 1) |
| 54 | + = ((j % p : ℕ) : ℤ) :: |
| 55 | + PlaceEngine.run (padicEngine p) ⟨0, ((j / p : ℕ) : ℤ), 0, 1⟩ [] fuel := by |
| 56 | + have hpp : p.Prime := Fact.out |
| 57 | + haveI : NeZero p := ⟨hpp.ne_zero⟩ |
| 58 | + have hpz : (0 : ℤ) < (p : ℤ) := by exact_mod_cast hpp.pos |
| 59 | + -- the three engine computations on the canonical ledger: |
| 60 | + have hready : (padicEngine p).ready ⟨0, (j : ℤ), 0, 1⟩ = true := by |
| 61 | + simp [padicEngine, PadicLedger.ready, PadicLedger.denomUnitOnDisk] |
| 62 | + have hcand : (padicEngine p).candidate ⟨0, (j : ℤ), 0, 1⟩ = ((j % p : ℕ) : ℤ) := by |
| 63 | + simp only [padicEngine, PadicLedger.candidate, Int.cast_zero, Int.cast_one, inv_one, mul_one] |
| 64 | + rw [Int.cast_natCast, ZMod.val_natCast] |
| 65 | + have hpne : (p : ℤ) ≠ 0 := ne_of_gt hpz |
| 66 | + have key : (j : ℤ) - ((j % p : ℕ) : ℤ) = (p : ℤ) * ((j / p : ℕ) : ℤ) := by |
| 67 | + have h : (j : ℤ) = (p : ℤ) * ((j / p : ℕ) : ℤ) + ((j % p : ℕ) : ℤ) := by |
| 68 | + exact_mod_cast (Nat.div_add_mod j p).symm |
| 69 | + linarith |
| 70 | + have hemit : (padicEngine p).emit ((j % p : ℕ) : ℤ) ⟨0, (j : ℤ), 0, 1⟩ |
| 71 | + = ⟨0, ((j / p : ℕ) : ℤ), 0, 1⟩ := by |
| 72 | + show PadicLedger.emitStep (p : ℤ) ((j % p : ℕ) : ℤ) ⟨0, (j : ℤ), 0, 1⟩ = _ |
| 73 | + simp only [PadicLedger.emitStep, PadicLedger.emit, mul_zero, sub_zero, mul_one, zero_mul, |
| 74 | + one_mul, key] |
| 75 | + unfold PadicLedger.reduceOnce |
| 76 | + split_ifs with hcond |
| 77 | + · simp [Int.mul_ediv_cancel_left _ hpne, Int.ediv_self hpne] |
| 78 | + · exact absurd (by simp [Int.mul_emod_right, Int.emod_self]) hcond |
| 79 | + -- unfold one driver step and substitute |
| 80 | + simp only [PlaceEngine.run, hready, hcand, hemit, if_true] |
| 81 | + |
| 82 | +/-- **Driver correctness (constant ledger).** Running the p-adic engine on the canonical |
| 83 | +constant ledger `⟨0, m, 0, 1⟩` for exactly `(Nat.digits p m).length` steps emits *exactly* |
| 84 | +the base-`p` Hensel digits of `m` — `Nat.digits p m`. The engine `run` is a **certified |
| 85 | +transcoder**, not merely a demo that matches. -/ |
| 86 | +theorem run_padic_constant_eq_digits (p : ℕ) [Fact p.Prime] (m : ℕ) : |
| 87 | + PlaceEngine.run (padicEngine p) ⟨0, (m : ℤ), 0, 1⟩ [] (Nat.digits p m).length |
| 88 | + = (Nat.digits p m).map (fun d => (d : ℤ)) := by |
| 89 | + induction m using Nat.strong_induction_on with |
| 90 | + | _ m ih => |
| 91 | + rcases eq_or_ne m 0 with rfl | hm |
| 92 | + · simp [PlaceEngine.run] |
| 93 | + · have hp1 : 1 < p := (Fact.out (p := p.Prime)).one_lt |
| 94 | + have hdig : Nat.digits p m = (m % p) :: Nat.digits p (m / p) := |
| 95 | + Nat.digits_def' hp1 (Nat.pos_of_ne_zero hm) |
| 96 | + have hlt : m / p < m := Nat.div_lt_self (Nat.pos_of_ne_zero hm) hp1 |
| 97 | + rw [hdig, List.length_cons, run_step, ih (m / p) hlt]; rfl |
| 98 | + |
| 99 | +/-! ## Demo — the certified transcoder reproduces the `ValuationStream` numbers -/ |
| 100 | + |
| 101 | +instance : Fact (Nat.Prime 5) := ⟨by norm_num⟩ |
| 102 | + |
| 103 | +-- `run ⟨0,12,0,1⟩ … (digits 5 12).length = [2,2]` — now a *theorem*, not just an #eval. |
| 104 | +#eval PlaceEngine.run (padicEngine 5) ⟨0, 12, 0, 1⟩ [] (Nat.digits 5 12).length -- [2, 2] |
| 105 | +#eval (Nat.digits 5 12).map (fun d => (d : ℤ)) -- [2, 2] |
| 106 | + |
| 107 | +end FdrsFormal.Modes.Adelic |
0 commit comments