Documentation

CslibFrontier.Algorithms.Lean.CSP.VariableElimination

Variable elimination in a supplied order over explicit factor tables #

This module implements pure bucket (variable) elimination in an arbitrary supplied order order : List V over explicit FactorTable R V tables, together with intermediate-scope width, bucket-count, and peak live-table size bounds expressed in terms of the actual intermediate scopes and live tables produced along order. Machine-level Word-RAM running-time bounds over materialized radix-indexed factor tables live in CslibFrontier.Algorithms.WordRAM.VariableElimination.

An elimination state ElimState R V records an accumulated scalar factor scalar : R (for eliminated isolated variables), the current list of live factor tables live, and the list of variables remaining not yet summed out. Eliminating a variable x ∈ remaining partitions live into bucket := live.filter (x ∈ ·.scope) and rest := live.filter (x ∉ ·.scope). If bucket is empty (an isolated variable), scalar is multiplied by (d : R) with zero new tables; otherwise a new table on outScope := (bucket.flatMap scope).dedup.filter (· != x) is materialized by summing over 0, ..., d - 1 the product of bucket, replacing bucket in live.

Main definitions #

Main results #

Elimination state and intermediate-scope statistics #

structure Cslib.Algorithms.Lean.CSP.ElimState (R : Type u_3) (V : Type u_4) :
Type (max u_3 u_4)

State of variable elimination: accumulated scalar from isolated variables, current list of live factor tables, and variables still to be summed out.

  • scalar : R

    Scalar contribution from isolated variables eliminated so far.

  • Currently active factor tables.

  • remaining : List V

    Variables not yet eliminated.

Instances For
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.CSP.instDecidableEqElimState {R✝ : Type u_3} {V✝ : Type u_4} [DecidableEq R✝] [DecidableEq V✝] :
    def Cslib.Algorithms.Lean.CSP.instDecidableEqElimState.decEq {R✝ : Type u_3} {V✝ : Type u_4} [DecidableEq R✝] [DecidableEq V✝] (x✝ x✝¹ : ElimState R✝ V✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.Algorithms.Lean.CSP.instReprElimState {R✝ : Type u_3} {V✝ : Type u_4} [Repr R✝] [Repr V✝] :
    Repr (ElimState R✝ V✝)
    def Cslib.Algorithms.Lean.CSP.instReprElimState.repr {R✝ : Type u_3} {V✝ : Type u_4} [Repr R✝] [Repr V✝] :
    ElimState R✝ V✝ → ℕ → Std.Format

    Intermediate-scope and live-memory statistics recorded at a single elimination step.

    • bucketFactors : ℕ

      Number of live factors participating in this variable's bucket.

    • jointScopeVars : ℕ

      Distinct variables in the joint intermediate bucket scope (0 if inactive/isolated).

    • outScopeVars : ℕ

      Distinct variables in the produced table's scope (jointScopeVars - 1 if active).

    • liveFactorsAfter : ℕ

      Number of live factors after the step.

    • liveEntriesAfter : ℕ

      Total stored entries across all live tables after the step.

    • arithOps : ℕ

      Number of scalar arithmetic operations (multiplications and additions) performed at step.

    Instances For

      Evaluate a list of base-d digits aligned with a variable list into an assignment V → ℕ (defaulting to 0 outside bounds).

      Total number of stored entries across all live tables.

      Maximum single-table entry count in live.

      Maximum factor scope length in live.

      def Cslib.Algorithms.Lean.CSP.ElimState.ofFactors {R : Type u_1} {V : Type u_2} [One R] (factors : List (Logic.PL.CSP.FactorTable R V)) (declared : List V) :

      Initial elimination state for a factor list over a declared variable list.

      def Cslib.Algorithms.Lean.CSP.ElimState.eval {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (s : ElimState R V) :
      R

      Contracted value represented by an ElimState: scalar * contractionOn d live remaining.

      def Cslib.Algorithms.Lean.CSP.ElimState.result {R : Type u_1} {V : Type u_2} [Monoid R] [Zero R] (s : ElimState R V) :
      R

      Scalar product of a state once all variables have been eliminated.

      Joint deduplicated scope of the factors in a bucket.

      def Cslib.Algorithms.Lean.CSP.bucketOutScope {R : Type u_1} {V : Type u_2} [DecidableEq V] (x : V) (bucket : List (Logic.PL.CSP.FactorTable R V)) :

      Scope of the output table produced by eliminating x from bucket.

      Build the output factor table for eliminating x from a nonempty bucket at domain size d.

      def Cslib.Algorithms.Lean.CSP.elimVar {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (x : V) (s : ElimState R V) :

      Perform one elementary variable elimination step on x.

      def Cslib.Algorithms.Lean.CSP.eliminateOrder {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) :
      List V → ElimState R V → ElimState R V

      Eliminate variables sequentially along a supplied order order : List V.

      def Cslib.Algorithms.Lean.CSP.stepStats {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (x : V) (s : ElimState R V) :

      Statistics for a single elimination step elimVar d x s.

      Sequence of intermediate step statistics produced along order.

      def Cslib.Algorithms.Lean.CSP.maxJointScopeLen {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :

      Maximum joint scope size across all steps along order.

      def Cslib.Algorithms.Lean.CSP.maxOutScopeLen {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :

      Maximum output table scope size (outScope.length) across all steps along order.

      def Cslib.Algorithms.Lean.CSP.peakLiveEntries {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :

      Peak total entries stored simultaneously in live tables across order (including init).

      def Cslib.Algorithms.Lean.CSP.totalArithOps {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :

      Total arithmetic operations recorded along order.

      Correctness and algebraic invariance #

      theorem Cslib.Algorithms.Lean.CSP.eval_elimVar {R : Type u_1} {V : Type u_2} [CommSemiring R] [DecidableEq V] (d : ℕ) (x : V) (s : ElimState R V) :

      Eliminating one variable preserves ElimState.eval.

      theorem Cslib.Algorithms.Lean.CSP.eval_eliminateOrder {R : Type u_1} {V : Type u_2} [CommSemiring R] [DecidableEq V] (d : ℕ) (order : List V) (s : ElimState R V) :

      Sequential variable elimination preserves ElimState.eval for every supplied order.

      Variable elimination removes exactly the variables appearing in order from remaining.

      Once s.remaining = [], s.eval d equals the pure scalar table product s.result.

      theorem Cslib.Algorithms.Lean.CSP.result_eliminateOrder_of_subset {R : Type u_1} {V : Type u_2} [CommSemiring R] [DecidableEq V] (d : ℕ) {order : List V} {s : ElimState R V} (hcover : s.remaining.toFinset ⊆ order.toFinset) :

      If order covers all initial declared variables init.remaining, eliminating along order leaves remaining = [] and its scalar result recovers init.eval d.

      theorem Cslib.Algorithms.Lean.CSP.result_eliminateOrder_ofCSP {R : Type u_1} {V : Type u_2} {Rel : Type u_3} {D : Type u_4} [CommSemiring R] [DecidableEq V] [Fintype D] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) {F : Logic.PL.CSP Rel V} {declared order : List V} (hvars : F.vars.toFinset ⊆ declared.toFinset) (hcover : declared.toFinset ⊆ order.toFinset) :

      Contracting a weighted CSP by variable elimination in ANY supplied order order covering declared recovers F.partitionFunctionOn wt declared.

      theorem Cslib.Algorithms.Lean.CSP.result_eliminateOrder_ofCSP_vars {R : Type u_1} {V : Type u_2} {Rel : Type u_3} {D : Type u_4} [CommSemiring R] [DecidableEq V] [Fintype D] [Inhabited D] (d : ℕ) (e : D ≃ Fin d) (wt : Rel → List D → R) (F : Logic.PL.CSP Rel V) {order : List V} (hcover : F.vars.toFinset ⊆ order.toFinset) :

      Contracting a weighted CSP in ANY order covering F.vars recovers F.partitionFunction wt.

      Live-table size and intermediate-scope operation bounds #

      theorem Cslib.Algorithms.Lean.CSP.length_elimVar_live_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (x : V) (s : ElimState R V) :

      Eliminating one variable never increases the number of live factor tables.

      theorem Cslib.Algorithms.Lean.CSP.liveLength_eliminateOrder_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (s : ElimState R V) :

      The number of live factor tables never increases along any elimination order.

      theorem Cslib.Algorithms.Lean.CSP.sum_bucketFactors_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :

      Amortized bucket-count bound: the sum of bucket sizes across order is at most init.live.length + order.length.

      theorem Cslib.Algorithms.Lean.CSP.mem_traceOrder_joint_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) {st : ElimStepStats} (hst : st ∈ traceOrder d order init) :

      Every step along order has joint scope size at most maxJointScopeLen d order init.

      theorem Cslib.Algorithms.Lean.CSP.totalArithOps_le_of_intermediate {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] {d : ℕ} (hd : 0 < d) (order : List V) (init : ElimState R V) :
      totalArithOps d order init ≤ order.length + (init.live.length + 2 * order.length) * d ^ maxJointScopeLen d order init

      Total scalar arithmetic operations along order are bounded by the maximum actual intermediate bucket scope width maxJointScopeLen d order init and the amortized bucket factor sum.

      theorem Cslib.Algorithms.Lean.CSP.maxOutScopeLen_le_maxJointScopeLen {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) :
      maxOutScopeLen d order init ≤ maxJointScopeLen d order init

      Output table dimension never exceeds the joint bucket dimension along any order.

      theorem Cslib.Algorithms.Lean.CSP.length_bucketOutScope_add_one {R : Type u_1} {V : Type u_2} [DecidableEq V] {x : V} {bucket : List (Logic.PL.CSP.FactorTable R V)} (hb : bucket ≠ []) (hx : ∀ f ∈ bucket, x ∈ f.scope) :

      For a nonempty bucket of factors containing x, the output scope has one fewer variable than the joint scope.

      Every factor in live has scope length at most maxScopeLen live.

      Eliminating one variable never increases remaining.length.

      theorem Cslib.Algorithms.Lean.CSP.scope_length_elimVar_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (x : V) (s : ElimState R V) {S W : ℕ} (hs : ∀ f ∈ s.live, f.scope.length ≤ max S W) (hstep : (stepStats d x s).jointScopeVars ≤ W) (f : Logic.PL.CSP.FactorTable R V) :
      f ∈ (elimVar d x s).live → f.scope.length ≤ max S W

      One elimination step preserves the scope-length envelope max S W whenever its joint scope has at most W variables.

      theorem Cslib.Algorithms.Lean.CSP.peakLiveEntries_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] {d : ℕ} (hd : 0 < d) (order : List V) (init : ElimState R V) :
      peakLiveEntries d order init ≤ init.live.length * max (maxTableEntries init.live) (d ^ maxOutScopeLen d order init)

      Peak live-table entry count across order is bounded strictly by init.live.length times the maximum of initial single-table sizes and the maximum output table size d ^ maxOutScopeLen.

      theorem Cslib.Algorithms.Lean.CSP.liveEntries_eliminateOrder_take_le {R : Type u_1} {V : Type u_2} [Semiring R] [DecidableEq V] (d : ℕ) (order : List V) (init : ElimState R V) (k : ℕ) :

      At every prefix order.take k, live-table entry count is bounded by peakLiveEntries.