Documentation

Complexity.Algorithms.WordRAM.FusionTree.Neighbors

Loading and selecting sketch neighbors #

An inclusive sketch cut has at most two neighboring full keys. Clamp the two indices to a nonempty array, load both keys, and compare their exclusive or with the query. Clamping allows the same bounded program to handle cuts at either end of the array.

Main definitions #

Main results #

References #

@[reducible, inline]

Query word, preserved.

Instances For
    @[reducible, inline]

    Inclusive sketch cut, preserved.

    Instances For
      @[reducible, inline]

      Nonzero node occupancy, preserved.

      Instances For
        @[reducible, inline]

        Address of the first full key, preserved.

        Instances For
          @[reducible, inline]

          Distance between full keys, preserved.

          Instances For
            @[reducible, inline]

            Constant one.

            Instances For
              @[reducible, inline]

              Clamped predecessor index.

              Instances For
                @[reducible, inline]

                Clamped successor index.

                Instances For
                  @[reducible, inline]

                  Clamping scratch.

                  Instances For
                    @[reducible, inline]

                    Memory address scratch.

                    Instances For
                      @[reducible, inline]

                      First candidate key.

                      Instances For
                        @[reducible, inline]

                        Second candidate key.

                        Instances For
                          @[reducible, inline]

                          First candidate's exclusive or with the query.

                          Instances For
                            @[reducible, inline]

                            Second candidate's exclusive or with the query.

                            Instances For
                              @[reducible, inline]

                              Selected key.

                              Instances For
                                @[reducible, inline]

                                Selected key's exclusive or with the query.

                                Instances For
                                  @[reducible, inline]

                                  Selected array index.

                                  Instances For

                                    Clamped predecessor position.

                                    Instances For

                                      Clamped successor position, expressed using two saturating subtractions.

                                      Instances For
                                        theorem Cslib.Algorithms.WordRAM.FusionTree.NeighborProgram.indices_neighbors {n t : ℕ} (hn : 0 < n) (ht : t ≤ n) :
                                        (left t + 1 = t ∨ left t = t) ∧ (right n t + 1 = t ∨ right n t = t)

                                        Exactly one of the two optional sketch neighbors is repeated at an end cut.

                                        Index selected by the comparison in code.

                                        Instances For
                                          theorem Cslib.Algorithms.WordRAM.FusionTree.NeighborProgram.selectIndex_spec (score : ℕ → ℕ) {n t : ℕ} (hn : 0 < n) (ht : t ≤ n) :
                                          selectIndex score n t < n ∧ (selectIndex score n t + 1 = t ∨ selectIndex score n t = t) ∧ ∀ i < n, i + 1 = t ∨ i = t → score (selectIndex score n t) ≤ score i

                                          The clamped comparison chooses a valid neighbor of minimum score among all neighbors of the cut. At either end the repeated candidate causes no exception.

                                          Thirteen instructions load the candidates. Selection adds four or five instructions, including control flow, and prefers the left candidate when exclusive-or values tie.

                                          Instances For

                                            State after loading, before the one comparison selecting a candidate.

                                            Instances For

                                              Final state, choosing the smaller full-word exclusive or.

                                              Instances For
                                                theorem Cslib.Algorithms.WordRAM.FusionTree.NeighborProgram.result_spec (x : WordRAM.Data 17) (q n t a d : ℕ) :
                                                have j := selectIndex (fun (i : ℕ) => x.mem (a + i * d) ^^^ q) n t; (result x q n t a d).regs index = j ∧ (result x q n t a d).regs chosen = x.mem (a + j * d) ∧ (result x q n t a d).regs divergence = x.mem (a + j * d) ^^^ q

                                                The returned key, exclusive-or value, and index describe the same selected array entry.

                                                theorem Cslib.Algorithms.WordRAM.FusionTree.NeighborProgram.result_sketchNeighbor {w n q a d : ℕ} (x : WordRAM.Data 17) (keys : Fin n → ℕ) (hn : 0 < n) (hkeys : ∀ (i : Fin n), x.mem (a + ↑i * d) = keys i) :
                                                ∃ (j : Fin n), (result x q n (Lean.FusionTree.sketchRank w keys q) a d).regs index = ↑j ∧ (result x q n (Lean.FusionTree.sketchRank w keys q) a d).regs chosen = keys j ∧ (result x q n (Lean.FusionTree.sketchRank w keys q) a d).regs divergence = keys j ^^^ q ∧ Lean.FusionTree.IsSketchNeighbor w keys q j ∧ ∀ (i : Fin n), Lean.FusionTree.IsSketchNeighbor w keys q i → keys j ^^^ q ≤ keys i ^^^ q

                                                With an actual sketch cut and an encoded full-key array, the output identifies a sketch neighbor with minimum exclusive-or value among the neighbors. No full-key scan supplies the answer. The statement is separate from the checked execution and can be composed with it.

                                                theorem Cslib.Algorithms.WordRAM.FusionTree.NeighborProgram.exec {w q n t a d : ℕ} (x : WordRAM.Data 17) (hn : 0 < n) (ht : t ≤ n) (hq : WordRAM.Fits w q) (hnfit : WordRAM.Fits w n) (ha : WordRAM.Fits w (a + (n - 1) * d)) (hkeys : ∀ i < n, WordRAM.Fits w (x.mem (a + i * d))) (hxq : x.regs query = q) (hxt : x.regs cut = t) (hxn : x.regs count = n) (hxa : x.regs base = a) (hxd : x.regs stride = d) :
                                                WordRAM.ExecWithin w code x (result x q n t a d) 18 ∧ (result x q n t a d).mem = x.mem

                                                Loading two clamped neighbors and selecting one takes at most eighteen actual checked instructions. The address premise covers the last array key; every accessed address and intermediate product lies below it. The query, cut, occupancy, base, stride, and memory are preserved. Constant preparation and the sketch cut itself are separate.