Reusable unary-count to canonical binary Nat conversion

InductiveControl labels of the unary-to-binary converter#✓fully proved

Existing emitter labels and the cleanup/unframing continuation.

inductive Lab | emit (l : EmitSS.Lab) | clean | read | back | done
  deriving DecidableEq
Uses 1 · Used by 6

DefinitionLabel enumeration of the unary-to-binary converter#✓fully proved

The finite label enumeration uses the existing emitter enumeration and two Boolean choices, without a noncomputable list conversion.

Cslib.Complexity.PolynomialEncoding.UnaryNat.labEquiv · 18 lines
def labEquiv : Lab ≃ EmitSS.Lab ⊕ Bool ⊕ Bool where
  toFun
    | .emit l => .inl l
    | .clean => .inr (.inl false)
    | .read => .inr (.inl true)
    | .back => .inr (.inr false)
    | .done => .inr (.inr true)
  invFun
    | .inl l => .emit l
    | .inr (.inl false) => .clean
    | .inr (.inl true) => .read
    | .inr (.inr false) => .back
    | .inr (.inr true) => .done
  left_inv l := by cases l <;> rfl
  right_inv l := by
    rcases l with l | b | b
    · rfl
    all_goals cases b <;> rfl
Uses 2 · Used by 1

DefinitionEmitter stack embedding in the unary-to-binary converter#✓fully proved

The component retains the existing emitter's heterogeneous alphabets.

def embedding : SEmb EmitSS.Γ EmitSS.Γ :=
  ⟨id, injective_id, fun _ => Equiv.refl _⟩
Uses 3 · Used by 3

DefinitionLabel map from the emitter halt to scratch cleanup#✓fully proved

The actual emitter halt is followed by charged scratch cleanup.

def emitLabel (l : EmitSS.Lab) : Lab :=
  if l = .done then .clean else .emit l
Uses 2 · Used by 3

DefinitionProgram of the unary-to-binary converter#✓fully proved

framed = true keeps the new frame; otherwise only that frame is removed, preserving any earlier output below it.

def prog (framed : Bool) : Lab → TM2.Stmt EmitSS.Γ Lab Bool
  | .emit l => mapS embedding emitLabel (EmitSS.prog l)
  | .clean => cleanS [.A, .H, .T] .clean (if framed then .done else .read)
  | .read => FramedCNF.Block.read .O .X some Sym.bit .read .back .done
  | .back => moveB .X .O Sym.bit? id .back .done
  | .done => .halt
Uses 14 · Used by 3

TheoremConverter turns a unary count into a canonical binary frame#✓fully proved

Emit one canonical Nat frame above any earlier output and restore all conversion scratch. The old counter is consumed; unrelated stacks, including X, are preserved. Cleanup is part of the charged run.

theorem run_framed (n : Nat) (out : List Bool) (v : Bool)
    (S : ∀ k, List (EmitSS.Γ k)) (hA : S .A = cnt EmitSS.U n)
    (hH : S .H = []) (hT : S .T = []) (hO : S .O = out) :
    ∃ U, RunLe (prog true) (708 * n + 304)
      ⟨some (.emit .hv), v, S⟩ ⟨some .done, false, U⟩ ∧
      U .O = delimit (Computability.encodeNat n) ++ out ∧
      U .A = [] ∧ U .H = [] ∧ U .T = [] ∧
      ∀ k, k ≠ .A → k ≠ .H → k ≠ .T → k ≠ .O → U k = S k
Uses 37

TheoremConverter turns a unary count into a raw canonical binary payload#✓fully proved

Convert a unary count to the canonical unframed Nat payload, preserving an earlier output suffix and clearing every used work stack.

theorem run_raw (n : Nat) (out : List Bool) (v : Bool)
    (S : ∀ k, List (EmitSS.Γ k)) (hA : S .A = cnt EmitSS.U n)
    (hH : S .H = []) (hT : S .T = []) (hO : S .O = out)
    (hX : S .X = []) :
    ∃ w U, RunLe (prog false) (710 * n + 306)
      ⟨some (.emit .hv), v, S⟩ ⟨some .done, w, U⟩ ∧
      U .O = Computability.encodeNat n ++ out ∧
      U .A = [] ∧ U .H = [] ∧ U .T = [] ∧ U .X = [] ∧
      ∀ k, k ≠ .A → k ≠ .H → k ≠ .T → k ≠ .O → k ≠ .X →
        U k = S k
Uses 39

AbbreviationUnary-to-binary converter as a host component#✓fully proved

The adapter is a finite component of the existing Host. Its input counter has n physical symbols; the bound is linear in that length, not in the bit length of an arbitrary binary-encoded numeric input.

abbrev component (framed : Bool) : Host.Comp where
  K := EmitSS.K
  Γ := EmitSS.Γ
  finΓ k := by cases k <;> infer_instance
  L := Lab
  prog := prog framed
  ks := [.X, .O, .A, .H, .N, .M, .C, .S1, .S2, .Y, .Z, .T, .L, .R, .Rt]
  ks_nodup := by decide
  mem_ks k := by cases k <;> simp
Uses 8
8 fully proved

The dependency explorer needs JavaScript and its graph data.

Download the complete graph data.