Documentation

CslibFrontier.Logics.Propositional.CSP.FactorTable

Explicit array factor tables and tensor-network correspondence #

This module provides a concrete array-backed representation FactorTable R V of weighted finite-domain constraints, together with exact bridges to both Cslib.Logic.PL.CSP (over any finite domain D of size d) and Cslib.Circuits.Frontier.ExplicitTensorNetwork / Cslib.Circuits.Frontier.TensorNetwork (at domain size d = 2).

Constraint scopes are arbitrary lists of variables: repeated variables inside a scope (for example [v, v]), empty scopes ([]), isolated declared variables, and disconnected constraint components are all handled uniformly by evaluating mixed-radix least-significant-digit indices (Nat.ofDigits) against an assignment σ : V → ℕ.

Main definitions #

Main results #

Mixed-radix least-significant-digit indexing #

Decode idx into len base-d digits in least-significant-digit-first order.

theorem Cslib.Logic.PL.CSP.ofDigits_map_zero {V : Type u_1} (d : ℕ) (xs : List V) :
Nat.ofDigits d (List.map (fun (x : V) => 0) xs) = 0

Evaluating Nat.ofDigits on an all-zero coordinate tuple yields 0.

@[simp]
theorem Cslib.Logic.PL.CSP.length_lsbDecode (d len idx : ℕ) :
(lsbDecode d len idx).length = len
theorem Cslib.Logic.PL.CSP.mem_lsbDecode_lt {d : ℕ} (hd : 0 < d) (len idx x : ℕ) (hx : x ∈ lsbDecode d len idx) :
x < d

Every decoded digit is strictly below a positive radix d.

theorem Cslib.Logic.PL.CSP.ofDigits_lt_pow_length {d : ℕ} (xs : List ℕ) (h : ∀ x ∈ xs, x < d) :

Nat.ofDigits d xs is strictly below d ^ xs.length for every d (including degenerate bases d ≤ 1) whenever every digit is below d.

theorem Cslib.Logic.PL.CSP.lsbDecode_ofDigits {d : ℕ} (xs : List ℕ) :
(∀ x ∈ xs, x < d) → lsbDecode d xs.length (Nat.ofDigits d xs) = xs

Decoding Nat.ofDigits d xs at length xs.length recovers xs when digits are below d.

theorem Cslib.Logic.PL.CSP.ofDigits_lsbDecode {d len idx : ℕ} (h : idx < d ^ len) :
Nat.ofDigits d (lsbDecode d len idx) = idx

Encoding the first len decoded digits recovers idx when idx < d ^ len.

Explicit factor tables #

structure Cslib.Logic.PL.CSP.FactorTable (R : Type u_1) (V : Type u_2) :
Type (max u_1 u_2)

A weighted constraint factor represented by an ordered variable scope and an explicit table of ring entries indexed in base-d least-significant-digit order.

  • scope : List V

    Ordered variable list of the factor (may contain duplicates or be empty).

  • entries : Array R

    Flat array of weights of expected length d ^ scope.length.

Instances For
    @[instance_reducible]
    instance Cslib.Logic.PL.CSP.instDecidableEqFactorTable {R✝ : Type u_1} {V✝ : Type u_2} [DecidableEq R✝] [DecidableEq V✝] :
    def Cslib.Logic.PL.CSP.instDecidableEqFactorTable.decEq {R✝ : Type u_1} {V✝ : Type u_2} [DecidableEq R✝] [DecidableEq V✝] (x✝ x✝¹ : FactorTable R✝ V✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.Logic.PL.CSP.instReprFactorTable {R✝ : Type u_1} {V✝ : Type u_2} [Repr R✝] [Repr V✝] :
    Repr (FactorTable R✝ V✝)
    def Cslib.Logic.PL.CSP.instReprFactorTable.repr {R✝ : Type u_1} {V✝ : Type u_2} [Repr R✝] [Repr V✝] :
    FactorTable R✝ V✝ → ℕ → Std.Format
    def Cslib.Logic.PL.CSP.FactorTable.WellFormed {R : Type u_1} {V : Type u_2} (d : ℕ) (f : FactorTable R V) :

    A factor table is well-formed at domain size d when it stores all d ^ scope.length entries.

    def Cslib.Logic.PL.CSP.FactorTable.evalAt {R : Type u_1} {V : Type u_2} (d : ℕ) [Zero R] (f : FactorTable R V) (σ : V → ℕ) :
    R

    Evaluate a factor table at a ℕ-valued assignment σ, looking up its LSB base-d index with 0 outside bounds.

    def Cslib.Logic.PL.CSP.FactorTable.vars {R : Type u_1} {V : Type u_2} [DecidableEq V] (factors : List (FactorTable R V)) :

    All variables occurring in a list of factor tables.

    def Cslib.Logic.PL.CSP.FactorTable.contractionOn {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (factors : List (FactorTable R V)) (declared : Finset V) :
    R

    Contract a list of factor tables over domain {0, ..., d - 1} for declared variables declared, fixing variables outside declared to 0.

    def Cslib.Logic.PL.CSP.FactorTable.contraction {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (factors : List (FactorTable R V)) :
    R

    Contract a list of factor tables over its occurring variables FactorTable.vars.

    theorem Cslib.Logic.PL.CSP.FactorTable.evalAt_congr {R : Type u_1} {V : Type u_2} {d : ℕ} [Zero R] {f : FactorTable R V} {σ τ : V → ℕ} (h : ∀ v ∈ f.scope, σ v = τ v) :
    evalAt d f σ = evalAt d f τ

    Factor evaluation depends only on values assigned to f.scope.

    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_nil {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (declared : Finset V) :
    contractionOn d [] declared = ↑d ^ declared.card

    The empty factor list contracts to d ^ declared.card over declared.

    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_eq_card_pow_mul_of_subset {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) {factors : List (FactorTable R V)} {S T : Finset V} (hvars : vars factors ⊆ S) (hST : S ⊆ T) :
    contractionOn d factors T = ↑d ^ (T \ S).card * contractionOn d factors S

    Enlarging the declared universe multiplies contractionOn by d ^ (T \ S).card whenever all factor variables already lie in S.

    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_append_of_disjoint {R : Type u_1} {V : Type u_2} [CommSemiring R] [DecidableEq V] (d : ℕ) {fs₁ fs₂ : List (FactorTable R V)} {d₁ d₂ : Finset V} (hdisj : Disjoint d₁ d₂) (h₁ : vars fs₁ ⊆ d₁) (h₂ : vars fs₂ ⊆ d₂) :
    contractionOn d (fs₁ ++ fs₂) (d₁ ∪ d₂) = contractionOn d fs₁ d₁ * contractionOn d fs₂ d₂

    Disconnected lists of factor tables multiply over a commutative semiring.

    theorem Cslib.Logic.PL.CSP.FactorTable.contraction_append_of_disjoint {R : Type u_1} {V : Type u_2} [CommSemiring R] [DecidableEq V] (d : ℕ) {fs₁ fs₂ : List (FactorTable R V)} (hdisj : Disjoint (vars fs₁) (vars fs₂)) :
    contraction d (fs₁ ++ fs₂) = contraction d fs₁ * contraction d fs₂

    Disconnected factor tables multiply over contraction.

    def Cslib.Logic.PL.CSP.FactorTable.relabel {R : Type u_1} {V : Type u_2} {W : Type u_3} (e : V → W) (f : FactorTable R V) :

    Relabeling factor scopes through a variable equivalence preserves contractionOn.

    @[simp]
    theorem Cslib.Logic.PL.CSP.FactorTable.relabel_scope {R : Type u_1} {V : Type u_2} {W : Type u_3} (e : V → W) (f : FactorTable R V) :
    @[simp]
    theorem Cslib.Logic.PL.CSP.FactorTable.relabel_entries {R : Type u_1} {V : Type u_2} {W : Type u_3} (e : V → W) (f : FactorTable R V) :
    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_relabel_equiv {R : Type u_1} {V : Type u_2} {W : Type u_3} [Semiring R] [DecidableEq V] [DecidableEq W] (d : ℕ) (e : V ≃ W) (factors : List (FactorTable R V)) (declared : Finset V) :
    contractionOn d (List.map (relabel ⇑e) factors) (Finset.map e.toEmbedding declared) = contractionOn d factors declared

    Bridge from CSP over any finite domain #

    def Cslib.Logic.PL.CSP.FactorTable.ofConstraint {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (c : Constraint Rel V) :

    Compile one constraint into an explicit factor table at domain size d using bijection e : D ≃ Fin d.

    def Cslib.Logic.PL.CSP.FactorTable.ofCSP {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (F : CSP Rel V) :

    Compile an entire CSP instance into a list of explicit factor tables.

    theorem Cslib.Logic.PL.CSP.FactorTable.wellFormed_ofConstraint {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (c : Constraint Rel V) :
    @[simp]
    theorem Cslib.Logic.PL.CSP.FactorTable.vars_ofCSP {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [DecidableEq V] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (F : CSP Rel V) :
    vars (ofCSP d e wt F) = F.vars.toFinset
    theorem Cslib.Logic.PL.CSP.FactorTable.evalAt_ofConstraint {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Zero R] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (c : Constraint Rel V) (σ : V → D) :
    (evalAt d (ofConstraint d e wt c) fun (v : V) => ↑(e (σ v))) = wt c.rel (List.map σ c.scope)

    Evaluating ofConstraint at fun v => (e (σ v)).1 recovers the original constraint weight, preserving arbitrary repeated variables and empty scopes.

    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_ofCSP {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Semiring R] [DecidableEq V] [Fintype D] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) {F : CSP Rel V} {declared : Finset V} (hvars : F.vars.toFinset ⊆ declared) :
    contractionOn d (ofCSP d e wt F) declared = partitionFunctionOn wt F declared

    Contracting ofCSP over declared (containing F.vars.toFinset) equals F.partitionFunctionOn wt declared.

    theorem Cslib.Logic.PL.CSP.FactorTable.contraction_ofCSP {Rel : Type u_1} {V : Type u_2} {D : Type u_3} {R : Type u_4} [Semiring R] [DecidableEq V] [Fintype D] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (F : CSP Rel V) :

    Contracting ofCSP over its occurring variables equals F.partitionFunction wt.

    Bridge to ExplicitTensorNetwork and TensorNetwork at d = 2 #

    Convert a list of factor tables on ℕ-named variables into an ExplicitTensorNetwork R with m declared edges 0, ..., m - 1.

    Convert an ExplicitTensorNetwork R into a list of FactorTable R ℕ tables.

    theorem Cslib.Logic.PL.CSP.FactorTable.contractionOn_ofDescribedNetwork {R : Type u_1} {T : Type u_2} [CommSemiring R] (eval : T → List Bool → R) (net : Circuits.Frontier.DescribedNetwork T) (heval : ∀ v ∈ net.vertices, ∀ (bs : List Bool), bs.length = v.1.length → eval v.2 bs = (Array.ofFn fun (i : Fin (2 ^ v.1.length)) => eval v.2 (List.map (fun (k : ℕ) => decide (k ≠ 0)) (lsbDecode 2 v.1.length ↑i))).getD (BitString.lsbValue bs) 0) :
    contractionOn 2 (List.map (fun (v : List ℕ × T) => { scope := v.1, entries := Array.ofFn fun (i : Fin (2 ^ v.1.length)) => eval v.2 (List.map (fun (k : ℕ) => decide (k ≠ 0)) (lsbDecode 2 v.1.length ↑i)) }) net.vertices) (Finset.range net.numEdges) = Circuits.Frontier.DescribedNetwork.contraction eval net

    Contracting a DescribedNetwork T whose vertex evaluator eval agrees with table lookup matches FactorTable.contractionOn 2.

    Contracting an arbitrary TensorNetwork R Bool V E matches FactorTable.contractionOn 2 of its explicit table representation.