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
Uses 1 · Used by 32
Used byPair-list projector main loop emits both lists in reverse · Pair-list projector as a host component · Pair-list projector emits one weight and one value frame per item · Stacks of the pair-list projector · Stack embedding of the pair normalizer in the pair-list projector · Program of the pair-list projector · Pair-list projector always terminates at the decoding outcome · Embedded pair-list projector preserves outside stacks · Pair-list projector fails on malformed input in linear time · Pair-list projector yields canonical weight and value lists · Eleven-stack invariant of the pair-list projector · Four-output pair-list decoder as a host component · List-projector stack embedding in the four-output decoder · Stacks of the four-output pair-list decoder · Final pair-projector stack embedding in the four-output decoder · Program of the four-output pair-list decoder · Four-output decoder reaches exactly the canonical decoding outcome · Embedded four-output decoder preserves outside stacks · Four-output decoder fails on malformed input · Four-output decoder emits four canonical outputs on success · Outer-splitter stack embedding in the four-output decoder · Stack invariant of the four-output pair-list decoder · Number-pair projector as a host component · Field-normalizer stack embedding in the number-pair projector · Pair-frame splitter stack embedding in the number-pair projector · Program of the number-pair projector · Number-pair projector reaches the decoding outcome · Number-pair projector normalizes both fields of a raw pair frame · Embedded number-pair projector preserves outside stacks · Number-pair projector fails on malformed framing in linear time · Number-pair projector yields both canonical payloads on success · Six-stack invariant of the number-pair projector

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
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
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
Uses 4 · Used by 2

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
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
Uses 2 · Used by 2

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
Uses 13 · Used by 12

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)⟩
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))
Uses 6 · Used by 1

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)⟩
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⟩
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
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
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)
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
Uses 19
16 fully proved

The dependency explorer needs JavaScript and its graph data.

Download the complete graph data.