Documentation

CslibFrontier.Algorithms.Reachability.DoubleCounting.Scan

Streaming bounded membership scans #

A branch supplies one choice per vertex in increasing order. A selection supplies only the successive destinations of its walk, with the source implicit. The end of that segment is the stop choice. The replay reads each destination once with a finite read-only edge callback. The branch and its segments are proof-level replay data. The scanner keeps only a vertex cursor, a current walk vertex, a remaining edge budget, a tally and one membership bit.

Main results #

To use Cslib.DoubleCounting.Scan.runM, supply skip/select, destination-read, and adjacency actions alongside the source, target, round index and expected tally. Use none for the walk's end marker. The scan returns none on rejection and some false for a successful negative membership test. Callback providers are responsible for the adjacency and choice encodings. The clock counts callback actions. The prefix bounds describe the abstract registers.

References #

@[reducible, inline]

Replay choices, indexed by the vertex being scanned. A selected segment contains the destinations of its edges in order, without the initial source.

Destination budgets, independent of edge validity and the terminal tally.

def Cslib.DoubleCounting.Scan.transcript {N : ℕ} (source : Fin N) (branch : Branch N) :

Recover the full vertex lists for the transcript contract.

theorem Cslib.DoubleCounting.Scan.bounded_of_passes {N : ℕ} (source : Fin N) (k : ℕ) (b : Branch N) {R : Fin N → Fin N → Prop} (h : Passes R source k (transcript source b)) :

Passing typed transcripts already meet the destination budget.

def Cslib.DoubleCounting.Scan.walkM {N : ℕ} {m : Type → Type u_1} [Monad m] (next : m (Option (Fin N))) (edge : Fin N → Fin N → m Bool) (target : Fin N) :
ℕ → Fin N → m (Option ℕ)

Check one walk by reading its next destination or end marker. The read-only edge callback is invoked once per attempted edge, provided there is still budget.

Replay the next destination from a segment, or its end marker.

theorem Cslib.DoubleCounting.Scan.walkM_replay_eq_some {N : ℕ} {edge : Fin N → Fin N → Bool} {target current : Fin N} {fuel left : ℕ} {tail : List (Fin N)} :
(walkM readNext (fun (u v : Fin N) => pure (edge u v)) target fuel current tail).1 = some left ↔ List.IsChainFromTo (fun (u v : Fin N) => edge u v = true) (current :: tail) current target ∧ left + tail.length = fuel

A replayed walk returns exactly its unused fuel when the destination list forms a path to the target within the original fuel.

A verified prefix's accumulated tally and membership bit.

  • tally : Tally

    Number and total length of all selected walks checked so far.

  • hit : Bool

    Whether the queried vertex has been selected.

The accumulator before the first vertex has been scanned.

def Cslib.DoubleCounting.Scan.Result.recordWalk (acc : Result) (budget remaining : ℕ) (hit : Bool) :

Record one selected walk in the membership tally.

def Cslib.DoubleCounting.Scan.tallyM {N : ℕ} {m : Type → Type u_1} [Monad m] (select : Fin N → m Bool) (next : m (Option (Fin N))) (edge : Fin N → Fin N → m Bool) (source target : Fin N) (k : ℕ) :

Run the bounded scan using one selection action per vertex and one destination-read action per walk step or stop marker. Adjacency is a read-only callback. Rejection stops the fold.

Replay a selection, loading its segment in the proof-level choice provider.

def Cslib.DoubleCounting.Scan.tallyPrefix {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) {n : ℕ} (hn : n ≤ N) :

Replay the first n vertices in increasing order. The input branch can be consumed one segment at a time.

theorem Cslib.DoubleCounting.Scan.tallyPrefix_zero {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) :
tallyPrefix edge source target k branch ⋯ = some Result.zero

The empty prefix has the initial membership accumulator.

theorem Cslib.DoubleCounting.Scan.tallyPrefix_succ {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) {n : ℕ} (hn : n < N) :
tallyPrefix edge source target k branch ⋯ = (tallyPrefix edge source target k branch ⋯).bind fun (acc : Result) => have v := ⟨n, hn⟩; match branch v with | none => some acc | some tail => Option.map (fun (left : ℕ) => acc.recordWalk k left (v == target)) (walkM readNext (fun (u v : Fin N) => pure (edge u v)) v k source tail).1

Extending a verified prefix skips an unselected vertex or adds its checked walk. The update consumes only the remaining budget returned by that walk.

theorem Cslib.DoubleCounting.Scan.tallyPrefix_congr {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) {n : ℕ} (hn : n ≤ N) {a b : Branch N} (h : ∀ (v : Fin N), ↑v < n → a v = b v) :
tallyPrefix edge source target k a hn = tallyPrefix edge source target k b hn

A partial tally depends only on selections below its cursor.

def Cslib.DoubleCounting.Scan.tally {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) :

Replay a scan, returning the accumulated tally and target membership if all chosen walks check.

theorem Cslib.DoubleCounting.Scan.tallyM_replay {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) :
(tallyM (readSelection branch) readNext (fun (u v : Fin N) => pure (edge u v)) source target k []).1 = tally edge source target k branch

The streaming implementation replayed from typed choices gives the same tally and bit as tally. The state monad holds the replay cursor, which belongs to the choice provider.

Read the membership bit when both observed totals match the expected tally.

def Cslib.DoubleCounting.Scan.runM {N : ℕ} {m : Type → Type u_1} [Monad m] (select : Fin N → m Bool) (next : m (Option (Fin N))) (edge : Fin N → Fin N → m Bool) (source target : Fin N) (k : ℕ) (expected : Tally) :

Run the streaming membership scan with the terminal tally check. A successful negative answer is some false; rejection is none.

def Cslib.DoubleCounting.Scan.run {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (expected : Tally) (branch : Branch N) :

Replay with the terminal tally check.

theorem Cslib.DoubleCounting.Scan.run_eq_finish {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (expected : Tally) (branch : Branch N) :
run edge source target k expected branch = (tallyPrefix edge source target k branch ⋯).bind (Result.finish expected)

The terminal scan checks the required totals after processing all candidates.

theorem Cslib.DoubleCounting.Scan.runM_replay {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (expected : Tally) (branch : Branch N) :
(runM (readSelection branch) readNext (fun (u v : Fin N) => pure (edge u v)) source target k expected []).1 = run edge source target k expected branch

Feeding the streaming scanner a typed branch implements its replay, including the terminal tally test.

theorem Cslib.DoubleCounting.Scan.run_eq_some_iff {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) (hit : Bool) :
run edge source target k (expected (fun (u v : Fin N) => edge u v = true) source k) branch = some hit ↔ Passes (fun (u v : Fin N) => edge u v = true) source k (transcript source branch) ∧ (branch target).isSome = hit

With exact round totals, success checks precisely the transcript contract and returns the membership bit for the queried vertex.

theorem Cslib.DoubleCounting.Scan.runM_eq_some_iff {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) (hit : Bool) :
(runM (readSelection branch) readNext (fun (u v : Fin N) => pure (edge u v)) source target k (expected (fun (u v : Fin N) => edge u v = true) source k) []).1 = some hit ↔ Passes (fun (u v : Fin N) => edge u v = true) source k (transcript source branch) ∧ (branch target).isSome = hit

The streaming scanner with the canonical choice provider returns a bit exactly when the corresponding transcript passes, and that bit records selection of the target.

theorem Cslib.DoubleCounting.Scan.hit_iff_reachable {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) (hit : Bool) (h : (runM (readSelection branch) readNext (fun (u v : Fin N) => pure (edge u v)) source target k (expected (fun (u v : Fin N) => edge u v = true) source k) []).1 = some hit) :
hit = true ↔ target ∈ Relation.reachableWithin (fun (u v : Fin N) => edge u v = true) source k

A successful scan with exact round totals reports bounded reachability at its target.

noncomputable def Cslib.DoubleCounting.Scan.successfulBranches {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) :
{ b : Branch N // (run edge source target k (expected (fun (u v : Fin N) => edge u v = true) source k) b).isSome = true } ≃ { w : Transcript (Fin N) // Passes (fun (u v : Fin N) => edge u v = true) source k w }

Successful typed branches are in bijection with passing transcripts. The inverse deletes the initial source of each selected walk.

theorem Cslib.DoubleCounting.Scan.existsUnique_successful_branch {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (h : ∃! w : Transcript (Fin N), Passes (fun (u v : Fin N) => edge u v = true) source k w) :
∃! b : Branch N, (run edge source target k (expected (fun (u v : Fin N) => edge u v = true) source k) b).isSome = true

At the exact round totals, a unique passing transcript gives a unique successful branch, including when the branch answers false.

theorem Cslib.DoubleCounting.Scan.prefix_registers_le {N : ℕ} (edge : Fin N → Fin N → Bool) (source target : Fin N) (k : ℕ) (branch : Branch N) {n : ℕ} (hn : n ≤ N) (r : Result) (hr : tallyPrefix edge source target k branch hn = some r) :

In any successfully processed prefix, the count is bounded by the number of processed vertices, and the length sum by that number times the walk budget. The prefix may be followed by invalid guesses.

theorem Cslib.DoubleCounting.Scan.tallyM_clock {N : ℕ} {σ : Type} (clock : σ → ℕ) (select : Fin N → StateM σ Bool) (next : StateM σ (Option (Fin N))) (edge : Fin N → Fin N → StateM σ Bool) (source target : Fin N) (k : ℕ) (hs : ∀ (v : Fin N) (s : σ), clock (select v s).2 ≤ clock s + 1) (hn : ∀ (s : σ), clock (next s).2 ≤ clock s + 1) (he : ∀ (u v : Fin N) (s : σ), clock (edge u v s).2 ≤ clock s + 1) (s : σ) :
clock (tallyM select next edge source target k s).2 ≤ clock s + N * (2 * k + 2)

All branches use at most N * (2 * k + 2) callback actions when each selection, destination read, or adjacency query advances the clock by at most one. This bound includes walks that fail an edge test or try to exceed the round budget.

theorem Cslib.DoubleCounting.Scan.runM_clock {N : ℕ} {σ : Type} (clock : σ → ℕ) (select : Fin N → StateM σ Bool) (next : StateM σ (Option (Fin N))) (edge : Fin N → Fin N → StateM σ Bool) (source target : Fin N) (k : ℕ) (expected : Tally) (hs : ∀ (v : Fin N) (s : σ), clock (select v s).2 ≤ clock s + 1) (hn : ∀ (s : σ), clock (next s).2 ≤ clock s + 1) (he : ∀ (u v : Fin N) (s : σ), clock (edge u v s).2 ≤ clock s + 1) (s : σ) :
clock (runM select next edge source target k expected s).2 ≤ clock s + N * (2 * k + 2)

The terminal tally check adds no callback action to tallyM_clock. Both a rejected scan and a successful false membership answer retain the same callback-action bound.