Lean library · module TopologyByExample.Slices.Sparse

Sparse certificates for candidate rows

Source file

A decision tree covers all assignments by splitting on coordinates. A leaf either refutes one of the supplied clauses or identifies a supplied candidate row on selected coordinates. Checking a tree visits its nodes, not the list of all assignments. This only certifies an upper bound on possible projected rows; candidates still need witnesses or further refutations before a classification is complete.

This file was written by Codex (OpenAI's AI model) working under the direction of David R. MacIver, who reviews it and is responsible for it.

Imports

definition Literal

TopologyByExample.Slices.Sparse.Literal

A property coordinate together with its required truth value.

abbrev Literal (n : ℕ)
:= Fin n × Bool

definition Agrees

TopologyByExample.Slices.Sparse.Agrees

A row extends all literals in a partial assignment.

def Agrees {n : ℕ} (cube : List (Literal n)) (r : Row n) : Prop
:=
  ∀ l ∈ cube, r l.1 = l.2

definition Satisfies

TopologyByExample.Slices.Sparse.Satisfies

A disjunctive clause is satisfied if at least one of its literals holds.

def Satisfies {n : ℕ} (clause : List (Literal n)) (r : Row n) : Prop
:=
  ∃ l ∈ clause, r l.1 = l.2

inductive type Certificate

TopologyByExample.Slices.Sparse.Certificate

A finite decision tree referencing candidate rows and clauses by bounded indices.

inductive Certificate (n m k : ℕ) where
  /-- The current partial assignment forces a supplied candidate row. -/
  | accept (row : Fin m)
  /-- The current partial assignment falsifies a supplied clause. -/
  | reject (clause : Fin k)
  /-- Split into the false branch and the true branch at a coordinate. -/
  | split (coordinate : Fin n) (no yes : Certificate n m k)
variable {n m k p : ℕ}

definition check

TopologyByExample.Slices.Sparse.Certificate.check

Check a certificate locally at every node, without enumerating all rows.

def check (projection : Fin p → Fin n) (rows : Fin m → Row p)
    (clauses : Fin k → List (Literal n))
    (cube : List (Literal n)) : Certificate n m k → Bool
Definition
  | .accept j => decide (∀ i, (projection i, rows j i) ∈ cube)
  | .reject j => decide (∀ l ∈ clauses j, (l.1, !l.2) ∈ cube)
  | .split i no yes => check projection rows clauses ((i, false) :: cube) no &&
      check projection rows clauses ((i, true) :: cube) yes

theorem check_sound

TopologyByExample.Slices.Sparse.Certificate.check_sound

A checked subtree covers every clause-satisfying extension of its partial assignment.

theorem check_sound (projection : Fin p → Fin n) (rows : Fin m → Row p)
    (clauses : Fin k → List (Literal n))
    (cert : Certificate n m k) :
    ∀ cube, check projection rows clauses cube cert = true →
      ∀ r, Agrees cube r → (∀ j, Satisfies (clauses j) r) →
        ∃ j, (fun i ↦ r (projection i)) = rows j
Proof
:= by
  induction cert with
  | accept j =>
    intro cube hc r hr _
    simp only [check, decide_eq_true_eq] at hc
    exact ⟨j, funext fun i ↦ hr (projection i, rows j i) (hc i)⟩
  | reject j =>
    intro cube hc r hr ht
    simp only [check, decide_eq_true_eq] at hc
    obtain ⟨⟨i, b⟩, hl, hb⟩ := ht j
    have hn := hr (i, !b) (hc (i, b) hl)
    have he : b = !b := hb.symm.trans hn
    cases b <;> simp at he
  | split i no yes ihno ihyes =>
    intro cube hc r hr ht
    simp only [check, Bool.and_eq_true] at hc
    cases hi : r i with
    | false =>
      apply ihno _ hc.1 r _ ht
      intro l hl
      rcases List.mem_cons.mp hl with rfl | hl
      · exact hi
      · exact hr l hl
    | true =>
      apply ihyes _ hc.2 r _ ht
      intro l hl
      rcases List.mem_cons.mp hl with rfl | hl
      · exact hi
      · exact hr l hl

theorem covers

TopologyByExample.Slices.Sparse.Certificate.covers

A certificate checked at the empty assignment covers every model of the clauses.

theorem covers (projection : Fin p → Fin n) (rows : Fin m → Row p)
    (clauses : Fin k → List (Literal n))
    (cert : Certificate n m k) (h : check projection rows clauses [] cert = true)
    (r : Row n) (hr : ∀ j, Satisfies (clauses j) r) : ∃ j, (fun i ↦ r (projection i)) = rows j
Proof
:=
  check_sound projection rows clauses cert [] h r (by simp [Agrees]) hr

definition positiveCertificate

TopologyByExample.Slices.Sparse.Certificate.positiveCertificate

A small certificate family used to exercise the checker on many coordinates.

private def positiveCertificate (indices : List (Fin n)) : Certificate n 1 n
:=
  indices.foldr (fun i rest ↦ .split i (.reject i) rest) (.accept 0)

check Only 57 tree nodes are needed for 28 positive unit clauses.

example : check (id : Fin 28 → Fin 28) (fun _ : Fin 1 ↦ fun _ ↦ true)
    (fun i ↦ [(i, true)]) [] (positiveCertificate (List.finRange 28)) = true
Proof
:= by
  decide +kernel

check Hidden coordinates need not be assigned when the selected coordinate is fixed.

example : check (fun _ : Fin 1 ↦ (0 : Fin 3)) (fun _ : Fin 1 ↦ fun _ ↦ true)
    (fun _ : Fin 1 ↦ [((0 : Fin 3), true)]) []
    (.split 0 (.reject 0) (.accept 0)) = true
Proof
:= by decide

check An incorrect rejection cannot pass, even with no candidates to choose from.

example : check (id : Fin 1 → Fin 1) (fun i : Fin 0 ↦ i.elim0)
    (fun _ : Fin 1 ↦ [((0 : Fin 1), true)]) [] (.reject 0) = false
Proof
:= by decide

check An incorrect candidate cannot pass without its coordinate being forced.

example : check (id : Fin 1 → Fin 1) (fun _ : Fin 1 ↦ fun _ ↦ true)
    (fun i : Fin 0 ↦ i.elim0) [] (.accept 0) = false
Proof
:= by decide