InvertedFile.lean
Lean 4 source, lean/DataMiningAsObservation/InvertedFile.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
InvertedFile.lean. Chapter 11 section 11.3, the inverted file.
An inverted file partitions the corpus into cells and answers a query by searching only the
probed cells. Probing more cells never loses a candidate already found, a true neighbour is
found exactly when its cell is probed, the fraction of the corpus scanned is the size of the
probed cells over the corpus, and with equal cells that fraction is the probe count over the
cell count. Recall at one probe against eight is the chapter's table, and the table is a
measurement.
-/
import Mathlib
namespace DMO.InvertedFile
open Finset
variable {α κ : Type*} [DecidableEq α] [DecidableEq κ]
/-- The candidates a search sees, the rows whose cell is among the probed cells `P`. -/
def scanned (corpus : Finset α) (cell : α → κ) (P : Finset κ) : Finset α :=
corpus.filter fun x => cell x ∈ P
omit [DecidableEq α] in
/-- **Probing more cells never loses a candidate.** -/
theorem scanned_mono (corpus : Finset α) (cell : α → κ) (P P' : Finset κ) (h : P ⊆ P') :
scanned corpus cell P ⊆ scanned corpus cell P' :=
Finset.monotone_filter_right corpus fun _ _ hx => h hx
omit [DecidableEq α] in
/-- **A neighbour is found exactly when its cell is probed.** -/
theorem found_iff (corpus : Finset α) (cell : α → κ) (P : Finset κ) (x : α) (hx : x ∈ corpus) :
x ∈ scanned corpus cell P ↔ cell x ∈ P := by
simp [scanned, hx]
/-- The fraction of the corpus scanned. -/
noncomputable def scannedFraction (corpus : Finset α) (cell : α → κ) (P : Finset κ) : ℝ :=
(scanned corpus cell P).card / corpus.card
omit [DecidableEq α] in
/-- **The scanned fraction lies in the unit interval** and grows with the probes. -/
theorem scannedFraction_mem_unit (corpus : Finset α) (hc : corpus.Nonempty) (cell : α → κ)
(P : Finset κ) : 0 ≤ scannedFraction corpus cell P ∧ scannedFraction corpus cell P ≤ 1 := by
unfold scannedFraction
have hpos : (0 : ℝ) < corpus.card := by exact_mod_cast hc.card_pos
refine ⟨by positivity, ?_⟩
rw [div_le_one hpos]
exact_mod_cast Finset.card_filter_le _ _
/-- **With equal cells, the scanned fraction is the probe count over the cell count.** If every
cell holds `s` rows and there are `C` cells, probing `p` of them scans `p s` of `C s` rows. -/
theorem equal_cells (p C s : ℕ) (hC : 0 < C) (hs : 0 < s) :
((p * s : ℕ) : ℝ) / ((C * s : ℕ) : ℝ) = (p : ℝ) / C := by
push_cast
have : (s : ℝ) ≠ 0 := by exact_mod_cast hs.ne'
have : (C : ℝ) ≠ 0 := by exact_mod_cast hC.ne'
field_simp
omit [DecidableEq α] in
/-- **Probing every cell scans the corpus**, which is exact search. -/
theorem scanned_univ [Fintype κ] (corpus : Finset α) (cell : α → κ) :
scanned corpus cell Finset.univ = corpus := by
simp [scanned]
end DMO.InvertedFile