Documentation

CslibFrontier.Algorithms.Reachability.DoubleCounting.SeedSearch

Ordered seed search over verified reachability controllers #

This is a composition step for #6747, not a native workspace or NL = UL theorem. The designated source's controller is run first to obtain its answer. The other listed sources are then checked in order. The list can enumerate original roots within a subdivided graph, whose auxiliary vertices remain available as walk targets. A bad controller verdict rejects this seed; a failed controller execution rejects the branch. Seeds are tried by increasing binary value, using the existing bit-string numbering and a bounded counter. In List, duplicate successful branches remain duplicate entries.

Applying the interface #

Supply a controller callback indexed by a bit seed and source. Its exact-outcome contract returns a bad radius witnessing failure of round uniqueness, or a decided Boolean with all-round uniqueness and the correct reachability answer. runController_correct and runController_replay_correct provide this boundary. The designated source is always tested, even if it is absent from the supplied source list. For graph-wide verification the list must cover every intended original source; the algorithm does not infer that coverage.

The seed is numbered by BitString.equivFin, with coordinate zero least significant. BitString.equivFin_symm_apply identifies access to a seed bit with Nat.testBit on its counter, so a later native implementation need not retain an array of seed words. The selected answer concerns the graph at the selected seed. Applying it to a fixed language also uses the reachability-preservation theorem of the weighted transport.

The outer traversal keeps a cursor and a remaining count, each at most 2 ^ length, so each register fits in length + 1 binary digits. The initial count 2 ^ length needs all of those digits. Each tested cursor is strictly below 2 ^ length and fits in length digits. The checked client verifies these bounds for the native binary encoding. The source list specifies the inner traversal. The actual replay callback in the checked client is proof-level enumeration. Native loop/state bounds, binary choice lowering, callbacks and first-halting composition remain in #6747. Validate this client with scripts/build.sh CslibFrontierTests.IsolationSeedSearch --wfail --iofail.

For implicitly indexed source lists, runSeedSearchIndexed requests the source at each position using a bounded cursor. It shares the seed loop and designated source handling with runSeedSearch. The exact equality to the list specification holds before assuming any controller contracts, so it covers rejection and repeated outcomes as well.

Main results #

References #

The first seed passing the requested source checks and its designated answer.

  • seed : Fin (2 ^ length)

    Binary value of the selected seed, with coordinate zero least significant.

  • reachable : Bool

    The controller's answer from the designated source.

Instances For
    @[instance_reducible]
    def Cslib.DoubleCounting.instDecidableEqSeedAnswer.decEq {length✝ : ℕ} (x✝ x✝¹ : SeedAnswer length✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.DoubleCounting.instReprSeedAnswer {length✝ : ℕ} :
    Repr (SeedAnswer length✝)
    def Cslib.DoubleCounting.testSeed {m : Type → Type} [AlternativeMonad m] {n : ℕ} (controller : Fin n → m (Option ControllerVerdict)) (sources : List (Fin n)) (source : Fin n) :

    Test the designated source first, then the other listed sources in their given order. none is a bad seed; controller failure is an effectful rejected branch.

    def Cslib.DoubleCounting.runSeedSearch {m : Type → Type} [AlternativeMonad m] {n length : ℕ} (controller : (Fin length → Bool) → Fin n → m (Option ControllerVerdict)) (sources : List (Fin n)) (source : Fin n) :
    m (Option (SeedAnswer length))

    Enumerate every bit seed by increasing binary value, stopping at the first seed whose designated and listed source controllers all pass. A successful negative answer also stops the search.

    def Cslib.DoubleCounting.runSeedScheduler {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    m (Option (SeedAnswer length))

    Run the seed and source counters with a callback on the binary seed number, the optional source position and retained designated answer, and the source ID. The absent position denotes the designated scan. The callback's effects preserve branch multiplicity.

    def Cslib.DoubleCounting.runSeedSchedulerFrom {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) (seed : ℕ) (phase : Option (ℕ × Bool)) :
    m (Option (SeedAnswer length))

    Resume the same monadic seed search at a binary rank. With no retained answer, start with the designated scan. A pair of cursor and answer resumes the indexed roots for that seed, after which later seeds still start with their designated scan.

    theorem Cslib.DoubleCounting.runSeedSchedulerFrom_start {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    runSeedSchedulerFrom scan sourceAt source 0 none = runSeedScheduler scan sourceAt source

    Starting at the designated scan of rank zero gives the original seed search.

    theorem Cslib.DoubleCounting.runSeedSchedulerFrom_exhausted {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) {seed : ℕ} (hs : 2 ^ length ≤ seed) (phase : Option (ℕ × Bool)) :
    runSeedSchedulerFrom scan sourceAt source seed phase = pure none

    A rank at or above the exclusive limit exhausts the seed search.

    theorem Cslib.DoubleCounting.runSeedSchedulerFrom_designated {m : Type → Type} [AlternativeMonad m] {n length : ℕ} [LawfulMonad m] {count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) {seed : ℕ} (hs : seed < 2 ^ length) (verdict : ControllerVerdict) (hscan : scan ⟨seed, hs⟩ none source = pure (some verdict)) :
    runSeedSchedulerFrom scan sourceAt source seed none = match verdict, hscan with | ControllerVerdict.bad radius, hscan => runSeedSchedulerFrom scan sourceAt source (seed + 1) none | ControllerVerdict.decided b, hscan => runSeedSchedulerFrom scan sourceAt source seed (some (0, b))

    A designated scan's pure verdict advances the seed on a tie, or starts the indexed roots with the designated answer.

    theorem Cslib.DoubleCounting.runSeedSchedulerFrom_indexed_done {m : Type → Type} [AlternativeMonad m] {n length : ℕ} [LawfulMonad m] {count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) {seed cursor : ℕ} (hs : seed < 2 ^ length) (hc : count ≤ cursor) (answer : Bool) :
    runSeedSchedulerFrom scan sourceAt source seed (some (cursor, answer)) = pure (some { seed := ⟨seed, hs⟩, reachable := answer })

    Once all indexed roots have passed, the retained designated answer is returned at the current seed.

    theorem Cslib.DoubleCounting.runSeedSchedulerFrom_indexed {m : Type → Type} [AlternativeMonad m] {n length : ℕ} [LawfulMonad m] {count : ℕ} (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) {seed cursor : ℕ} (hs : seed < 2 ^ length) (hc : cursor < count) (answer : Bool) (reply : Option ControllerVerdict) (hskip : reply.isNone = true ↔ sourceAt ⟨cursor, hc⟩ = source) (hscan : ∀ (value : ControllerVerdict), reply = some value → scan ⟨seed, hs⟩ (some (⟨cursor, hc⟩, answer)) (sourceAt ⟨cursor, hc⟩) = pure (some value)) :
    runSeedSchedulerFrom scan sourceAt source seed (some (cursor, answer)) = match reply, hskip, hscan with | some (ControllerVerdict.bad radius), hskip, hscan => runSeedSchedulerFrom scan sourceAt source (seed + 1) none | x, hskip, hscan => runSeedSchedulerFrom scan sourceAt source seed (some (cursor + 1, answer))

    An indexed step skips the designated source or consumes its own pure verdict. A tie advances to the next seed. A skip or decided verdict advances the cursor with the retained designated answer.

    def Cslib.DoubleCounting.runSeedSearchIndexed {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (controller : (Fin length → Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    m (Option (SeedAnswer length))

    Search with sources supplied by their positions. The traversal keeps a cursor and remaining count, requesting each source only after the preceding checks have passed.

    theorem Cslib.DoubleCounting.runSeedSearchIndexed_eq_scheduler {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (controller : (Fin length → Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    runSeedSearchIndexed controller sourceAt source = runSeedScheduler (fun (i : Fin (2 ^ length)) (x : Option (Fin count × Bool)) => controller ((BitString.equivFin length).symm i)) sourceAt source

    The indexed search is the specialization that reads the binary seed by its bits.

    theorem Cslib.DoubleCounting.runSeedScheduler_eq {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (scan : Fin (2 ^ length) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    runSeedScheduler (fun (i : Fin (2 ^ length)) (x : Option (Fin count × Bool)) => scan i) sourceAt source = runSeedSearchIndexed (fun (bits : Fin length → Bool) => scan ((BitString.equivFin length) bits)) sourceAt source

    A scanner that depends only on the seed number and source ID gives the same computation as the indexed search with the corresponding bit-seed callback.

    theorem Cslib.DoubleCounting.runSeedSearchIndexed_eq {m : Type → Type} [AlternativeMonad m] {n length count : ℕ} (controller : (Fin length → Bool) → Fin n → m (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) :
    runSeedSearchIndexed controller sourceAt source = runSeedSearch controller (List.ofFn sourceAt) source

    Indexed traversal gives the same ordered computation as enumerating the source list. In the list monad this preserves all outcomes and their multiplicities.

    theorem Cslib.DoubleCounting.runSeedScheduler_clock {n length : ℕ} {σ : Type} {count : ℕ} (clock : σ → ℕ) (cost : ℕ) (scan : Fin (2 ^ length) → Option (Fin count × Bool) → Fin n → OptionT (StateM σ) (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) (hc : ∀ (i : Fin (2 ^ length)) (phase : Option (Fin count × Bool)) (u : Fin n) (s : σ), clock ((scan i phase u).run s).2 ≤ clock s + cost) (s : σ) :
    clock ((runSeedScheduler scan sourceAt source).run s).2 ≤ clock s + 2 ^ length * ((1 + count) * cost)

    The indexed search spends at most one designated call and one call per source position for each candidate seed. A rejecting call still advances the measured state clock. This bounds the supplied callback cost in the rejecting state monad.

    theorem Cslib.DoubleCounting.runSeedSearchIndexed_clock {n length : ℕ} {σ : Type} {count : ℕ} (clock : σ → ℕ) (cost : ℕ) (controller : (Fin length → Bool) → Fin n → OptionT (StateM σ) (Option ControllerVerdict)) (sourceAt : Fin count → Fin n) (source : Fin n) (hc : ∀ (seed : Fin length → Bool) (u : Fin n) (s : σ), clock ((controller seed u).run s).2 ≤ clock s + cost) (s : σ) :
    clock ((runSeedSearchIndexed controller sourceAt source).run s).2 ≤ clock s + 2 ^ length * ((1 + count) * cost)

    The indexed search spends at most one designated call and one call per source position for each candidate seed, including a rejecting call.

    theorem Cslib.DoubleCounting.testSeed_correct {m : Type → Type} [AlternativeMonad m] {n : ℕ} [LawfulMonad m] {r : Fin n → Fin n → Prop} {target source : Fin n} (sources : List (Fin n)) {controller : Fin n → m (Option ControllerVerdict)} (hc : ∀ (s : Fin n), ∃ (verdict : ControllerVerdict), controller s = pure (some verdict) ∧ match verdict with | ControllerVerdict.bad j => ¬∃! w : Transcript (Fin n), Passes r s j w | ControllerVerdict.decided b => (∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes r s j w) ∧ (b = true ↔ Relation.ReflTransGen r s target)) :
    testSeed controller sources source = pure (if (∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes r source j w) ∧ ∀ s ∈ sources, ∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes r s j w then some (decide (Relation.ReflTransGen r source target)) else none)

    The all-source test has one monadic outcome under the controller's exact-outcome contract. In List this is a singleton with its multiplicity, not merely a unique value.

    theorem Cslib.DoubleCounting.runSeedSearch_correct {m : Type → Type} [AlternativeMonad m] {n length : ℕ} [LawfulMonad m] (sources : List (Fin n)) {r : (Fin length → Bool) → Fin n → Fin n → Prop} {target source : Fin n} {controller : (Fin length → Bool) → Fin n → m (Option ControllerVerdict)} (hc : ∀ (seed : Fin length → Bool) (s : Fin n), ∃ (verdict : ControllerVerdict), controller seed s = pure (some verdict) ∧ match verdict with | ControllerVerdict.bad j => ¬∃! w : Transcript (Fin n), Passes (r seed) s j w | ControllerVerdict.decided b => (∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r seed) s j w) ∧ (b = true ↔ Relation.ReflTransGen (r seed) s target)) (hex : ∃ (seed : Fin length → Bool), (∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r seed) source j w) ∧ ∀ s ∈ sources, ∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r seed) s j w) :
    ∃ (result : SeedAnswer length), runSeedSearch controller sources source = pure (some result) ∧ ((∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r ((BitString.equivFin length).symm result.seed)) source j w) ∧ ∀ s ∈ sources, ∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r ((BitString.equivFin length).symm result.seed)) s j w) ∧ (∀ i < result.seed, ¬((∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r ((BitString.equivFin length).symm i)) source j w) ∧ ∀ s ∈ sources, ∀ (j : ℕ), ∃! w : Transcript (Fin n), Passes (r ((BitString.equivFin length).symm i)) s j w)) ∧ (result.reachable = true ↔ Relation.ReflTransGen (r ((BitString.equivFin length).symm result.seed)) source target)

    Under exact controller outcomes and the existence of an all-source-good seed, the ordered search has one outcome and selects the least binary seed that passes every source. In List this asserts exactly one successful branch, including its multiplicity.