Documentation

CslibFrontier.Computability.Complexity.NatCodec

Composable natural-number index codecs for hardness reductions #

Many polynomial-time and parsimonious reductions allocate fresh natural-number variable or vertex names across several disjoint roles — original vertices or variables, fixed hub/pendant gadgets, clause-splitting pairs, occurrence-splitting triples, or layered arc gadgets — and then discharge injectivity, non-collision between roles, partial decoding, binary bit-length bounds, and ComputableInPolyTime certificates under Turing.MultiTapeTM.encNat.

Cslib.Complexity.NatCodec α packages a computable numbering encode : α → ℕ together with its exact partial inverse decode? : ℕ → Option α (decode? n = some a ↔ encode a = n). Combinators build compound namespaces without ad-hoc parity or Nat.unpair arithmetic:

All combinators carry @[fun_prop] ComputableInPolyTime rules into encNat, using Turing.MultiTapeTM.ComputableInPolyTime.sumElim from PolyTime.Sum.

Main definitions #

Main results #

Sharp binary bit-length bound for Nat.pair #

theorem Nat.size_pair_le (a b : ℕ) :
(pair a b).size ≤ 2 * max a.size b.size

Pairing two natural numbers of binary length at most m uses at most 2 m binary digits.

NatCodec and its combinators #

structure Cslib.Complexity.NatCodec (α : Type u_1) :
Type u_1

A computable injective numbering encode : α → ℕ equipped with its exact partial inverse decode? : ℕ → Option α (decode? n = some a ↔ encode a = n).

  • encode : α → ℕ

    Assign a natural-number index to a.

  • decode? : ℕ → Option α

    Recover the unique a with encode a = n, or none outside the range of encode.

  • decode?_encode (a : α) : self.decode? (self.encode a) = some a

    Encoding followed by decode? recovers the input.

  • encode_of_decode? {n : ℕ} {a : α} : self.decode? n = some a → self.encode a = n

    Any some a returned by decode? n encodes back to n.

Instances For
    @[instance_reducible]
    instance Cslib.Complexity.NatCodec.instCoeFunForallNat {α : Type u_1} :
    CoeFun (NatCodec α) fun (x : NatCodec α) => α → ℕ
    @[simp]
    theorem Cslib.Complexity.NatCodec.decode?_apply {α : Type u_1} (c : NatCodec α) (a : α) :
    c.decode? (c.encode a) = some a
    @[simp]
    theorem Cslib.Complexity.NatCodec.apply_eq_iff {α : Type u_1} (c : NatCodec α) {a b : α} :
    c.encode a = c.encode b ↔ a = b

    View a NatCodec as an embedding α ↪ ℕ.

    theorem Cslib.Complexity.NatCodec.decode?_eq_some_iff {α : Type u_1} (c : NatCodec α) {n : ℕ} {a : α} :
    c.decode? n = some a ↔ c.encode a = n

    c.decode? n returns some a if and only if c a = n.

    c.decode? n succeeds if and only if n lies in Set.range c.

    c.decode? n returns none if and only if n lies outside Set.range c.

    Binary word length of c a under encNat equals (c a).size.

    Base codecs and combinators #

    Canonical codec for Fin k onto {0, …, k - 1}.

    @[simp]
    theorem Cslib.Complexity.NatCodec.fin_apply (k : ℕ) (i : Fin k) :
    (fin k).encode i = ↑i
    theorem Cslib.Complexity.NatCodec.fin_lt (k : ℕ) (i : Fin k) :
    (fin k).encode i < k
    def Cslib.Complexity.NatCodec.shift {α : Type u_1} (k : ℕ) (c : NatCodec α) :

    Shift every index of c upward by k.

    @[simp]
    theorem Cslib.Complexity.NatCodec.shift_apply {α : Type u_1} (k : ℕ) (c : NatCodec α) (a : α) :
    (shift k c).encode a = c.encode a + k
    theorem Cslib.Complexity.NatCodec.le_shift {α : Type u_1} (k : ℕ) (c : NatCodec α) (a : α) :
    k ≤ (shift k c).encode a
    def Cslib.Complexity.NatCodec.finSum {β : Type u_1} (k : ℕ) (c : NatCodec β) :

    Reserve {0, …, k - 1} for Fin k and shift c to {k, k + 1, …}.

    @[simp]
    theorem Cslib.Complexity.NatCodec.finSum_inl {β : Type u_1} (k : ℕ) (c : NatCodec β) (i : Fin k) :
    (finSum k c).encode (Sum.inl i) = ↑i
    @[simp]
    theorem Cslib.Complexity.NatCodec.finSum_inr {β : Type u_1} (k : ℕ) (c : NatCodec β) (b : β) :
    (finSum k c).encode (Sum.inr b) = c.encode b + k
    theorem Cslib.Complexity.NatCodec.finSum_inl_ne_inr {β : Type u_1} (k : ℕ) (c : NatCodec β) (i : Fin k) (b : β) :
    theorem Cslib.Complexity.NatCodec.finSum_inr_ne_inl {β : Type u_1} (k : ℕ) (c : NatCodec β) (b : β) (i : Fin k) :
    theorem Cslib.Complexity.NatCodec.disjoint_range_finSum {β : Type u_1} (k : ℕ) (c : NatCodec β) :
    Disjoint (Set.range fun (i : Fin k) => (finSum k c).encode (Sum.inl i)) (Set.range fun (b : β) => (finSum k c).encode (Sum.inr b))

    Left and right summands of finSum occupy disjoint subsets of ℕ.

    def Cslib.Complexity.NatCodec.sum {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) :
    NatCodec (α ⊕ β)

    Interleave c₁ on even indices (2 * c₁ a) and c₂ on odd indices (2 * c₂ b + 1).

    @[simp]
    theorem Cslib.Complexity.NatCodec.sum_inl {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (a : α) :
    (c₁.sum c₂).encode (Sum.inl a) = 2 * c₁.encode a
    @[simp]
    theorem Cslib.Complexity.NatCodec.sum_inr {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (b : β) :
    (c₁.sum c₂).encode (Sum.inr b) = 2 * c₂.encode b + 1
    theorem Cslib.Complexity.NatCodec.sum_inl_ne_inr {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (a : α) (b : β) :
    (c₁.sum c₂).encode (Sum.inl a) ≠ (c₁.sum c₂).encode (Sum.inr b)
    theorem Cslib.Complexity.NatCodec.sum_inr_ne_inl {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (b : β) (a : α) :
    (c₁.sum c₂).encode (Sum.inr b) ≠ (c₁.sum c₂).encode (Sum.inl a)
    theorem Cslib.Complexity.NatCodec.disjoint_range_sum {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) :
    Disjoint (Set.range fun (a : α) => (c₁.sum c₂).encode (Sum.inl a)) (Set.range fun (b : β) => (c₁.sum c₂).encode (Sum.inr b))

    Even and odd branches of NatCodec.sum occupy disjoint subsets of ℕ.

    def Cslib.Complexity.NatCodec.pair {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) :
    NatCodec (α × β)

    Product codec combining c₁ and c₂ through Nat.pair / Nat.unpair.

    @[simp]
    theorem Cslib.Complexity.NatCodec.pair_apply {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (p : α × β) :
    (c₁.pair c₂).encode p = Nat.pair (c₁.encode p.1) (c₂.encode p.2)

    Binary bit-length bounds #

    theorem Cslib.Complexity.NatCodec.size_shift_le {α : Type u_1} (k : ℕ) (c : NatCodec α) (a : α) :
    ((shift k c).encode a).size ≤ max (c.encode a).size k.size + 1
    theorem Cslib.Complexity.NatCodec.size_finSum_le {β : Type u_2} (k : ℕ) (c : NatCodec β) (x : Fin k ⊕ β) :
    ((finSum k c).encode x).size ≤ Sum.elim (fun (x : Fin k) => k.size) (fun (b : β) => max (c.encode b).size k.size + 1) x

    Prefixing Fin k and shifting c by k bounds binary bit-length by k.size on .inl and max (c b).size k.size + 1 on .inr.

    theorem Cslib.Complexity.NatCodec.size_sum_inl_le {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (a : α) :
    ((c₁.sum c₂).encode (Sum.inl a)).size ≤ (c₁.encode a).size + 1
    theorem Cslib.Complexity.NatCodec.size_sum_inr_eq {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (b : β) :
    ((c₁.sum c₂).encode (Sum.inr b)).size = (c₂.encode b).size + 1
    theorem Cslib.Complexity.NatCodec.size_sum_le {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (x : α ⊕ β) :
    ((c₁.sum c₂).encode x).size ≤ Sum.elim (fun (a : α) => (c₁.encode a).size) (fun (b : β) => (c₂.encode b).size) x + 1

    Interleaving via NatCodec.sum adds at most one binary digit to the payload index.

    theorem Cslib.Complexity.NatCodec.size_pair_le {α : Type u_1} {β : Type u_2} (c₁ : NatCodec α) (c₂ : NatCodec β) (p : α × β) :
    ((c₁.pair c₂).encode p).size ≤ 2 * max (c₁.encode p.1).size (c₂.encode p.2).size

    Pairing via NatCodec.pair uses at most twice the larger payload bit-length.

    Polynomial-time computability under encNat #