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
inductive Lab
| rdK | rdN | mv | tri | triC | triC2 | cmp | rc (o : Ordering) | rcB (o : Ordering)
| eg | egX | egR | egR2 | egY | vinit | vtx | pr | rend | rmv | vend | vchk | rs
| hv | hv0 | hv1 | hvB | tem0 | tsk | tem | bad | done
deriving DecidableEq
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:71
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
inductive Lab | emit (l : EmitSS.Lab) | clean | read | back | done
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:33
inductive Lab
| rdK | rdN | mv | tri | triC | triC2 | cmp | rc (o : Ordering) | rcB (o : Ordering)
| eg | egX | egR | egR2 | egY | vinit | vtx | pr | rend | rmv | vend | vchk | rs
| hv | hv0 | hv1 | hvB | tem0 | tsk | tem | bad | done
deriving DecidableEq
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:71
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 _⟩
structure SEmb {K K' : Type} (Γ : K → Type) (Γ' : K' → Type) where
/-- The stack renaming. -/
e : K → K'
/-- Distinct sub-machine stacks go to distinct host stacks. -/
inj : Injective e
/-- The alphabet of each sub-machine stack, identified with its host stack's. -/
ι : ∀ k, Γ k ≃ Γ' (e k)
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:51
@[reducible] def Γ : K → Type
| .O => Bool
| _ => Sym
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:64
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
inductive Lab
| rdK | rdN | mv | tri | triC | triC2 | cmp | rc (o : Ordering) | rcB (o : Ordering)
| eg | egX | egR | egR2 | egY | vinit | vtx | pr | rend | rmv | vend | vchk | rs
| hv | hv0 | hv1 | hvB | tem0 | tsk | tem | bad | done
deriving DecidableEq
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:71
inductive Lab | emit (l : EmitSS.Lab) | clean | read | back | done
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:33
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
inductive Lab | emit (l : EmitSS.Lab) | clean | read | back | done
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:33
@[reducible] def Γ : K → Type
| .O => Bool
| _ => Sym
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:64
def mapS : TM2.Stmt Γ Λ σ → TM2.Stmt Γ' Λ' σ
| .push k f q => .push (E.e k) (fun s => E.ι k (f s)) (mapS q)
| .peek k f q => .peek (E.e k) (fun s o => f s (o.map (E.ι k).symm)) (mapS q)
| .pop k f q => .pop (E.e k) (fun s o => f s (o.map (E.ι k).symm)) (mapS q)
| .load a q => .load a (mapS q)
| .branch f q₁ q₂ => .branch f (mapS q₁) (mapS q₂)
| .goto f => .goto fun s => φ (f s)
| .halt => .halt
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:65
def embedding : SEmb EmitSS.Γ EmitSS.Γ :=
⟨id, injective_id, fun _ => Equiv.refl _⟩
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:60
def emitLabel (l : EmitSS.Lab) : Lab :=
if l = .done then .clean else .emit l
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:64
def prog : Lab → TM2.Stmt Γ Lab Bool
| .rdK => .peek .X (fun _ o => o.isNone) <| .branch id (.goto fun _ => .bad) <|
.pop .X (fun _ o => isT o) <| .branch id
(.push .A (fun _ => U) (.goto fun _ => .rdK)) (.goto fun _ => .rdN)
| .rdN => .peek .X (fun _ o => o.isNone) <| .branch id (.goto fun _ => .bad) <|
.pop .X (fun _ o => isT o) <| .branch id
(.push .N (fun _ => U) <| .push .L (fun _ => .val false) <| .goto fun _ => .rdN)
(.goto fun _ => .mv)
| .mv => .peek .X (fun _ o => o.isNone) <| .branch id (.goto fun _ => .tri) <|
.pop .X (fun _ o => isT o) <| .push .Y (fun s => .val s) <|
.push .T (fun _ => .bit false) <| .push .T (fun s => .bit s) <|
.push .C (fun _ => U) <| .goto fun _ => .mv
⋯
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:110
def cleanS (ks : List K) (self next : Λ) : TM2.Stmt Γ Λ σ :=
.load (fun _ => Flag.tag false) <| popAll ks <|
.branch Flag.untag (.goto fun _ => self) (.goto fun _ => next)
MathlibExt/Computability/TuringMachine/Cleanup.lean:41
def read (a b : K) (d : Γ a → Option Bool) (g : Bool → Γ b)
(self good bad : Λ) : TM2.Stmt Γ Λ Bool :=
.peek a (fun _ o => isBit d o) <| .branch id
(.pop a (fun _ o => bitOf d o) <| .branch id
(.peek a (fun _ o => isBit d o) <| .branch id
(.pop a (fun _ o => bitOf d o) <| .push b g <|
.goto fun _ => self)
(.goto fun _ => bad))
(.goto fun _ => good))
(.goto fun _ => bad)
CSLibExt/Computability/Complexity/FramedCNF/Block.lean:32
inductive Sym
| bit (b : Bool) | lit (p : Bool) | cl | val (b : Bool) | dead
deriving DecidableEq
CSLibExt/Computability/Complexity/SatVerifierSpec.lean:41
def moveB (a b : K) (d : Γ a → Option Bool) (g : Bool → Γ b) (self next : Λ) :
TM2.Stmt Γ Λ σ :=
.peek a (fun _ o => Flag.tag (isBit d o))
(.branch Flag.untag
(.pop a (fun _ o => Flag.tag (bitOf d o))
(.branch Flag.untag
(.push b (fun _ => g true) (.goto fun _ => self))
(.push b (fun _ => g false) (.goto fun _ => self))))
(.goto fun _ => next))
MathlibExt/Computability/TuringMachine/BitOps.lean:86
def Sym.bit? : Sym → Option Bool
| .bit b => some b
| _ => none
CSLibExt/Computability/Complexity/SatVerifierSpec.lean:46
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
@[reducible] def Γ : K → Type
| .O => Bool
| _ => Sym
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:64
abbrev cnt {α : Type} (u : α) (n : ℕ) : List α := List.replicate n u
MathlibExt/Computability/TuringMachine/Program.lean:115
abbrev U : Sym := .cl
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:87
def RunLe (M : Λ → TM2.Stmt Γ Λ σ) (B : ℕ) (c d : TM2.Cfg Γ Λ σ) : Prop :=
∃ n, n ≤ B ∧ Run M n c d
MathlibExt/Computability/TuringMachine/Program.lean:901
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
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:69
def delimit : List Bool → List Bool
| [] => [false]
| b :: l => true :: b :: delimit l
MathlibExt/Computability/Complexity.lean:68
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
@[reducible] def Γ : K → Type
| .O => Bool
| _ => Sym
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:64
abbrev cnt {α : Type} (u : α) (n : ℕ) : List α := List.replicate n u
MathlibExt/Computability/TuringMachine/Program.lean:115
abbrev U : Sym := .cl
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:87
def RunLe (M : Λ → TM2.Stmt Γ Λ σ) (B : ℕ) (c d : TM2.Cfg Γ Λ σ) : Prop :=
∃ n, n ≤ B ∧ Run M n c d
MathlibExt/Computability/TuringMachine/Program.lean:901
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
CSLibExt/Computability/Complexity/PolynomialEncoding/UnaryNat.lean:69
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
structure Comp where
/-- Stack indices. -/
K : Type
/-- Decidable equality of stack indices. -/
[decK : DecidableEq K]
/-- Finitely many stacks. -/
[finK : Fintype K]
/-- Stack alphabets. -/
Γ : K → Type
/-- Finite alphabets. -/
[finΓ : ∀ k, Fintype (Γ k)]
/-- Labels. -/
⋯
CSLibExt/Computability/Complexity/Host.lean:53
inductive K | X | O | A | H | N | M | C | S1 | S2 | Y | Z | T | L | R | Rt
deriving DecidableEq
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:55
@[reducible] def Γ : K → Type
| .O => Bool
| _ => Sym
CSLibExt/Computability/Complexity/Karp/SubsetSumEmit.lean:64
[finΓ : ∀ k, Fintype (Γ k)]
CSLibExt/Computability/Complexity/Host.lean:63
Uses 8
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.