/- Released under Apache 2.0 license as described in the file LICENSE. -/ import FormalConflictRange.General /-! # A computable cell hull The general algorithm is stated with `Finset.min'`/`Finset.max'`, which are noncomputable because they take an explicit nonemptiness proof. This file replaces those choices by the executable `Finset.min`/`Finset.max` operations (with `⊤`/`⊥` as the empty-set defaults) and proves that the resulting cell representatives and hull agree with the classical definitions. The endpoint cells themselves are already computable: `cellEndpoints` is a `def`, and only the choice of a representative inside a cell used `min'`/`max'`. Once those are replaced, the cellwise algorithm with `activeComputable` is an executable `def`. -/ namespace FormalConflictRange /-- Computable least endpoint strictly above `e`, with `⊤` for the empty case. -/ def nextEndpointC (E : Finset (WithTop Key)) (e : WithTop Key) : WithTop Key := match (E.filter fun x => e < x).min with | none => ⊤ | some x => x theorem nextEndpointC_eq (E : Finset (WithTop Key)) (e : WithTop Key) : nextEndpointC E e = nextEndpoint E e := by unfold nextEndpointC nextEndpoint by_cases h : (E.filter fun x => e < x).Nonempty · rw [dite_eq_left h] have hmin : (E.filter fun x => e < x).min = ((Finset.min' _ h : WithTop Key) : WithTop (WithTop Key)) := (Finset.coe_min' h).symm rw [hmin] · have hempty : E.filter (fun x => e < x) = ∅ := Finset.not_nonempty_iff_eq_empty.mp h rw [dite_eq_right h, hempty] simp /-- Computable greatest endpoint at or below `k`, with `⊥` as a default. The `hBot` argument is kept for definitional compatibility with `cellLo`. -/ def cellLoC (E : Finset (WithTop Key)) (_hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := match (E.filter fun e => e ≤ (k : WithTop Key)).max with | some e => e | none => (([] : Key) : WithTop Key) theorem cellLoC_eq (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : cellLoC E hBot k = cellLo E hBot k := by unfold cellLoC cellLo have hne : (E.filter fun e => e ≤ (k : WithTop Key)).Nonempty := by refine ⟨(([] : Key) : WithTop Key), ?_⟩ rw [Finset.mem_filter] exact ⟨hBot, by simp [nil_le_key]⟩ have hmax : (E.filter fun e => e ≤ (k : WithTop Key)).max = ((Finset.max' _ hne : WithTop Key) : WithBot (WithTop Key)) := (Finset.coe_max' hne).symm rw [hmax] /-- Computable version of the next endpoint after the cell's lower endpoint. -/ def cellHiC (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : WithTop Key := nextEndpointC E (cellLoC E hBot k) theorem cellHiC_eq (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : cellHiC E hBot k = cellHi E hBot k := by unfold cellHiC cellHi rw [cellLoC_eq, nextEndpointC_eq] /-- Computable representative of `k`'s cell. -/ def cellRepC (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : Key × WithTop Key := let lo := cellLoC E hBot k match lo with | some l => if (k : WithTop Key) = lo then (k, keyAfter k) else (keyAfter l, nextEndpointC E lo) | none => (k, keyAfter k) theorem cellRepC_eq (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (k : Key) : cellRepC E hBot k = cellRep E hBot k := by unfold cellRepC cellRep rw [cellLoC_eq] cases hlo : cellLo E hBot k with | top => rfl | coe l => simp only by_cases hk : (k : WithTop Key) = (↑l : WithTop Key) · simp [hk] · simp [hk, nextEndpointC_eq] /-- Computable open-cell representative following a finite endpoint. -/ def openRepC (E : Finset (WithTop Key)) (e : WithTop Key) : Key × WithTop Key := match e with | some l => (keyAfter l, nextEndpointC E e) | none => (([] : Key), (([] : Key) : WithTop Key)) theorem openRepC_eq (E : Finset (WithTop Key)) (e : WithTop Key) : openRepC E e = openRep E e := by unfold openRepC openRep cases e with | top => rfl | coe l => rw [nextEndpointC_eq] /-- Computable finite set of cell representatives. -/ def cellRepsC (E : Finset (WithTop Key)) (_hBot : (([] : Key) : WithTop Key) ∈ E) (_hTop : ⊤ ∈ E) : Finset (Key × WithTop Key) := let endpoints := E.filter fun e => e ≠ ⊤ ((endpoints.image singletonRep) ∪ (endpoints.image (openRepC E))).filter fun p => (p.1 : WithTop Key) < p.2 theorem cellRepsC_eq (E : Finset (WithTop Key)) (hBot : (([] : Key) : WithTop Key) ∈ E) (hTop : ⊤ ∈ E) : cellRepsC E hBot hTop = cellReps E hBot hTop := by unfold cellRepsC cellReps have hop : openRepC E = openRep E := funext (openRepC_eq E) rw [hop] /-! ## Computable representatives and hull -/ /-- Computable active representatives, filtered by a Boolean activity oracle. -/ def activeRepsOfC (D : ConstraintDNF) (active : Key → Bool) : Finset (Key × WithTop Key) := (cellRepsC (cellEndpoints D) (cellEndpoints_bot D) (cellEndpoints_top D)).filter (fun p => active p.1) theorem activeRepsOfC_eq (D : ConstraintDNF) (active : Key → Bool) : activeRepsOfC D active = activeRepsOf D active := by unfold activeRepsOfC activeRepsOf rw [cellRepsC_eq] /-- Computable least key of a finite set of active representatives. -/ def activeBeginC (A : Finset (Key × WithTop Key)) : Key := match (A.image fun p => p.1).min with | none => [] | some k => k /-- Computable greatest upper boundary of a finite set of active representatives. -/ def activeEndC (A : Finset (Key × WithTop Key)) : WithTop Key := match (A.image fun p => p.2).max with | none => (([] : Key) : WithTop Key) | some e => e theorem activeBeginC_eq {A : Finset (Key × WithTop Key)} (h : A.Nonempty) : activeBeginC A = activeBegin A h := by unfold activeBeginC activeBegin have hne : (A.image fun p => p.1).Nonempty := h.image _ have hmin : (A.image fun p => p.1).min = ((Finset.min' _ hne : Key) : WithTop Key) := (Finset.coe_min' hne).symm rw [hmin] theorem activeEndC_eq {A : Finset (Key × WithTop Key)} (h : A.Nonempty) : activeEndC A = activeEnd A h := by unfold activeEndC activeEnd have hne : (A.image fun p => p.2).Nonempty := h.image _ have hmax : (A.image fun p => p.2).max = ((Finset.max' _ hne : WithTop Key) : WithBot (WithTop Key)) := (Finset.coe_max' hne).symm rw [hmax] /-- Computable hull of a finite set of active representatives. -/ def hullOfActiveRepsC (A : Finset (Key × WithTop Key)) : ConflictRange := ⟨(activeBeginC A : WithTop Key), activeEndC A⟩ theorem hullOfActiveRepsC_eq (A : Finset (Key × WithTop Key)) : hullOfActiveRepsC A = hullOfActiveReps A := by unfold hullOfActiveRepsC hullOfActiveReps by_cases h : A.Nonempty · rw [dite_eq_left h, activeBeginC_eq h, activeEndC_eq h] · have hA : A = ∅ := Finset.not_nonempty_iff_eq_empty.mp h rw [dite_eq_right h, hA] simp [activeBeginC, activeEndC] /-- The cellwise algorithm with a Boolean oracle, using only computable hull extraction. -/ def computableCellwise (r : RangeRead) (res : Result) (active : Key → Bool) : ConflictRange := hullOfActiveRepsC (activeRepsOfC (characterizingDNF r res) active) theorem computableCellwise_eq_cellwise (r : RangeRead) (res : Result) (active : Key → Bool) : computableCellwise r res active = conflictRangeCellwise r res active := by unfold computableCellwise conflictRangeCellwise rw [activeRepsOfC_eq, hullOfActiveRepsC_eq] end FormalConflictRange