Projecting and normalizing a pair of binary naturals
InductiveStacks of the number-pair projector#✓fully proved
Pair workspace, one reusable Nat buffer, and two canonical outputs.
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
inductive K | input | left | scratch
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/PairSplit.lean:32
Uses 1 · Used by 32
InductiveControl labels of the number-pair projector#✓fully proved
Pair projection followed by normalization of the two fields.
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
inductive Lab | read | copy | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/PairSplit.lean:40
inductive Lab | read (last : Option Bool) | copy | done
deriving DecidableEq
CSLibExt/Computability/Complexity/FramedCNF/NormalizeMachine.lean:36
Uses 2 · Used by 22
DefinitionPair-frame splitter stack embedding in the number-pair projector#✓fully proved
The pair-frame component acts only on its three own stacks.
def pairEmb : SEmb (fun _ : PairSplit.K => Bool) (fun _ : K => Bool) where
e := K.pair
inj _ _ h := K.pair.inj h
ι _ := Equiv.refl Bool
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
inductive K | input | left | scratch
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/PairSplit.lean:32
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
inj : Injective e
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:55
ι : ∀ k, Γ k ≃ Γ' (e k)
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:57
Uses 3 · Used by 3
DefinitionField-normalizer stack embedding in the number-pair projector#✓fully proved
Each normalizer consumes its raw field, sharing only an empty buffer.
def normEmb (second : Bool) :
SEmb (fun _ : NormalizeMachine.K => Bool) (fun _ : K => Bool) where
e
| .input => if second then .pair .input else .pair .left
| .buffer => .buffer
| .output => if second then .right else .left
inj a b h := by cases second <;> cases a <;> cases b <;> simp_all
ι _ := Equiv.refl Bool
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
inductive K | input | buffer | output
deriving DecidableEq
CSLibExt/Computability/Complexity/FramedCNF/NormalizeMachine.lean:28
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
e : K → K'
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:53
inj : Injective e
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:55
DefinitionPair-frame splitter halt redirection in the number-pair projector#✓fully proved
Only actual pair halt labels are redirected.
def pairLabel : PairSplit.Lab → Lab
| .good => .norm false (.read none)
| .bad => .bad
| l => .pair l
inductive Lab | read | copy | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/PairSplit.lean:40
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:40
Uses 3 · Used by 3
DefinitionLabel map chaining the two field normalizers#✓fully proved
Normalizing the first field continues with the second.
def normLabel (second : Bool) : NormalizeMachine.Lab → Lab
| .done => if second then .good else .norm true (.read none)
| l => .norm second l
inductive Lab | read (last : Option Bool) | copy | done
deriving DecidableEq
CSLibExt/Computability/Complexity/FramedCNF/NormalizeMachine.lean:36
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:40
DefinitionProgram of the number-pair projector#✓fully proved
All work is performed by the two existing finite components.
def prog : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:40
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
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 pairEmb : SEmb (fun _ : PairSplit.K => Bool) (fun _ : K => Bool) where
e := K.pair
inj _ _ h := K.pair.inj h
ι _ := Equiv.refl Bool
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:57
def pairLabel : PairSplit.Lab → Lab
| .good => .norm false (.read none)
| .bad => .bad
| l => .pair l
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:73
def prog : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .read => Block.read .input .scratch some id .read .copy .bad
| .copy => moveB .scratch .left some id .copy .good
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/PairSplit.lean:48
def normEmb (second : Bool) :
SEmb (fun _ : NormalizeMachine.K => Bool) (fun _ : K => Bool) where
e
| .input => if second then .pair .input else .pair .left
| .buffer => .buffer
| .output => if second then .right else .left
inj a b h := by cases second <;> cases a <;> cases b <;> simp_all
ι _ := Equiv.refl Bool
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:63
def normLabel (second : Bool) : NormalizeMachine.Lab → Lab
| .done => if second then .good else .norm true (.read none)
| l => .norm second l
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:79
def prog : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .read last =>
.peek .input (fun _ o => o.isSome) <| .branch id
(.pop .input (fun _ o => o.getD false) <|
.push .buffer id <| .goto fun b => .read (some b))
(if last = some false then
.push .buffer (fun _ => true) (.goto fun _ => .copy)
else .goto fun _ => .copy)
| .copy => moveB .buffer .output some id .copy .done
| .done => .halt
CSLibExt/Computability/Complexity/FramedCNF/NormalizeMachine.lean:46
Uses 13 · Used by 12
DefinitionSix-stack invariant of the number-pair projector#✓fully proved
Explicit six-stack invariant.
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
Uses 2 · Used by 14
TheoremNumber-pair projector normalizes both fields of a raw pair frame#✓fully proved
Every raw pair frame is projected and normalized without changing the total Nat decoding semantics. Previous output suffixes survive.
theorem run_delimit (p r left right : List Bool) (v : Bool) :
∃ v', RunLe prog (2 * (delimit p ++ r).length + 6)
⟨some (.pair .read), v, st (delimit p ++ r) [] [] [] left right⟩
⟨some .good, v',
st [] [] [] [] (normalizeNat p ++ left) (normalizeNat r ++ right)⟩
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 : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
def delimit : List Bool → List Bool
| [] => [false]
| b :: l => true :: b :: delimit l
MathlibExt/Computability/Complexity.lean:68
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:90
def normalizeNat (bits : List Bool) : List Bool :=
if bits.getLast? = some false then bits ++ [true] else bits
CSLibExt/Computability/Complexity/FramedCNF/Normalize.lean:27
Uses 29 · Used by 1
TheoremDecoding a pair of naturals fails exactly on a malformed first frame#✓fully proved
Pair Nat decoding fails exactly when the first frame is malformed; both raw field payloads use Mathlib's total Nat decoder.
theorem decode_pair_nat (x : List Bool) :
bitDecode (α := Nat × Nat) x = (undelimit x).map (fun p =>
(Computability.decodeNat p.1, Computability.decodeNat p.2))
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
def undelimit : List Bool → Option (List Bool × List Bool)
| false :: rest => some ([], rest)
| true :: b :: input => (undelimit input).map fun p => (b :: p.1, p.2)
| _ => none
MathlibExt/Computability/Complexity.lean:74
TheoremNumber-pair projector yields both canonical payloads on success#✓fully proved
A successful pair decode produces both canonical binary payloads, with all scratch empty and arbitrary previous output suffixes preserved.
theorem run_some {x : List Bool} {m n : Nat}
(h : bitDecode x = some (m, n)) (left right : List Bool) (v : Bool) :
∃ v', RunLe prog (2 * x.length + 6)
⟨some (.pair .read), v, st x [] [] [] left right⟩
⟨some .good, v', st [] [] [] [] (Computability.encodeNat m ++ left)
(Computability.encodeNat n ++ right)⟩
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
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 : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:90
Uses 17 · Used by 3
TheoremNumber-pair projector fails on malformed framing in linear time#✓fully proved
Malformed pair framing fails in linear time. Private partial workspace contents must be cleared before another invocation.
theorem run_none {x : List Bool} (h : bitDecode (α := Nat × Nat) x = none)
(left right : List Bool) (v : Bool) :
∃ v' S, RunLe prog (x.length + 1)
⟨some (.pair .read), v, st x [] [] [] left right⟩
⟨some .bad, v', S⟩
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
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 : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:90
Uses 25 · Used by 3
AbbreviationNumber-pair projector as a host component#✓fully proved
The concrete two-Nat projector in the existing Host interface.
abbrev component : Host.Comp where
K := K
Γ := fun _ => Bool
L := Lab
prog := prog
ks := [.pair .input, .pair .left, .pair .scratch, .buffer, .left, .right]
ks_nodup := by decide
mem_ks k := by
rcases k with k | _ | _ | _ <;> try decide
cases k <;> decide
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 | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:40
def prog : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
Uses 7 · Used by 1
TheoremCanonical payloads of a decoded pair are bounded by the raw input#✓fully proved
Canonical output payload sizes are bounded by the raw input size, even when either field had a noncanonical final zero bit.
theorem decoded_payload_length_le {x : List Bool} {m n : Nat}
(h : bitDecode x = some (m, n)) :
(Computability.encodeNat m).length + (Computability.encodeNat n).length ≤
x.length + 1
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
Uses 10 · Used by 3
TheoremNumber-pair projector reaches the decoding outcome#✓fully proved
Total decoder outcome with exact successful output and empty private scratch. Failure does not promise reusable workspace contents.
theorem run (x : List Bool) (v : Bool) :
∃ v' S, RunLe prog (2 * x.length + 6)
⟨some (.pair .read), v, st x [] [] [] [] []⟩
⟨some (if (bitDecode (α := Nat × Nat) x).isSome
then .good else .bad), v', S⟩ ∧
∀ m n : Nat, bitDecode x = some (m, n) →
S = st [] [] [] [] (Computability.encodeNat m)
(Computability.encodeNat n)
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 : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:90
def bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
Uses 16 · Used by 1
TheoremEmbedded number-pair projector preserves outside stacks#✓fully proved
Callers may embed this concrete projector without disturbing outside stacks. A failed workspace must be cleared before reuse.
theorem run_embedded {J L : Type} [DecidableEq J] {Γ : J → Type}
(E : SEmb (fun _ : K => Bool) Γ) (φ : Lab → L)
{M : L → TM2.Stmt Γ L Bool} (hm : Embeds E φ prog M)
(x : List Bool) (v : Bool) (S : ∀ j, List (Γ j))
(hs : Agree E (st x [] [] [] [] []) S) :
∃ v' T, RunLe M (2 * x.length + 6)
⟨some (φ (.pair .read)), v, S⟩
⟨some (φ (if (bitDecode (α := Nat × Nat) x).isSome
then .good else .bad)), v', T⟩ ∧ Frame E S T ∧
∀ m n : Nat, bitDecode x = some (m, n) →
Agree E (st [] [] [] [] (Computability.encodeNat m)
(Computability.encodeNat n)) T
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
inductive K | pair (k : PairSplit.K) | buffer | left | right
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:29
inductive Lab | pair (l : PairSplit.Lab)
| norm (second : Bool) (l : NormalizeMachine.Lab) | good | bad
deriving DecidableEq
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:40
def Embeds (M : Λ → TM2.Stmt Γ Λ σ) (M' : Λ' → TM2.Stmt Γ' Λ' σ) : Prop :=
∀ l, M' (φ l) = mapS E φ (M l) ∨ M l = .halt
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:76
def prog : Lab → TM2.Stmt (fun _ : K => Bool) Lab Bool
| .pair l => mapS pairEmb pairLabel (PairSplit.prog l)
| .norm s l => mapS (normEmb s) (normLabel s) (NormalizeMachine.prog l)
| .good | .bad => .halt
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:84
def Agree (S : ∀ k, List (Γ k)) (S' : ∀ k', List (Γ' k')) : Prop :=
∀ k, S' (E.e k) = (S k).map (E.ι k)
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:80
def st (input first scratch buffer left right : List Bool) : K → List Bool
| .pair .input => input
| .pair .left => first
| .pair .scratch => scratch
| .buffer => buffer
| .left => left
| .right => right
CSLibExt/Computability/Complexity/PolynomialEncoding/NatPair.lean:90
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 bitDecode [BitstringEncoding α] (l : List Bool) : Option α := toEncoding.decode l
MathlibExt/Computability/Complexity.lean:58
def Frame (S' S'' : ∀ k', List (Γ' k')) : Prop :=
∀ k', (∀ k, E.e k ≠ k') → S'' k' = S' k'
MathlibExt/Computability/TuringMachine/StackEmbedding.lean:84
Uses 19
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.