-
Notifications
You must be signed in to change notification settings - Fork 251
[ add ] clean version of Data.Fin.Properties.searchMinimalCounterexample
#2801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,16 +48,17 @@ open import Relation.Binary.PropositionalEquality.Properties as ≡ | |
open import Relation.Binary.PropositionalEquality as ≡ | ||
using (≡-≟-identity; ≢-≟-identity) | ||
open import Relation.Nullary.Decidable as Dec | ||
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′) | ||
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′; decidable-stable) | ||
open import Relation.Nullary.Negation.Core using (¬_; contradiction) | ||
open import Relation.Nullary.Recomputable using (¬-recompute) | ||
open import Relation.Nullary.Reflects using (invert) | ||
open import Relation.Unary as U | ||
using (U; Pred; Decidable; _⊆_; Satisfiable; Universal) | ||
using (U; Pred; Decidable; _⊆_; ∁; Satisfiable; Universal) | ||
open import Relation.Unary.Properties using (U?) | ||
|
||
private | ||
variable | ||
a : Level | ||
a p q : Level | ||
A : Set a | ||
m n o : ℕ | ||
i j : Fin n | ||
|
@@ -469,6 +470,10 @@ toℕ-inject : ∀ {i : Fin n} (j : Fin′ i) → toℕ (inject j) ≡ toℕ j | |
toℕ-inject {i = suc i} zero = refl | ||
toℕ-inject {i = suc i} (suc j) = cong suc (toℕ-inject j) | ||
|
||
inject-< : ∀ {i : Fin n} (j : Fin′ i) → inject j < i | ||
inject-< {i = suc i} zero = z<s | ||
inject-< {i = suc i} (suc j) = s<s (inject-< j) | ||
|
||
------------------------------------------------------------------------ | ||
-- inject₁ | ||
------------------------------------------------------------------------ | ||
|
@@ -1048,16 +1053,57 @@ private | |
note P? = Dec.does (P? 0F) ∧ Dec.does (P? 1F) ∧ Dec.does (P? 2F) ∧ true | ||
, refl | ||
|
||
-- If a decidable predicate P over a finite set is sometimes false, | ||
-- then we can find the smallest value for which this is the case. | ||
------------------------------------------------------------------------ | ||
-- A decidable predicate P over a finite set is either always true, | ||
-- or else we can find the smallest value for which P is false. | ||
|
||
module _ (P : Pred (Fin n) p) where | ||
|
||
record MinimalCounterexample : Set p where | ||
constructor μ | ||
field | ||
witness : Fin n | ||
.contra : ¬ P witness | ||
minimal : ∀ {j} → .(j < witness) → P j | ||
|
||
record MinimalExample : Set p where | ||
constructor μ | ||
field | ||
witness : Fin n | ||
example : P witness | ||
minimal : ∀ {j} → .(j < witness) → ¬ P j | ||
|
||
μ⟨_⟩ : Pred (Fin n) p → Set p | ||
μ⟨_⟩ = MinimalExample | ||
|
||
μ⟨¬_⟩ : Pred (Fin n) p → Set p | ||
μ⟨¬_⟩ = MinimalCounterexample | ||
|
||
¬¬μ⇒μ : ∀ {P : Pred (Fin n) p} → Decidable P → μ⟨¬ ∁ P ⟩ → μ⟨ P ⟩ | ||
¬¬μ⇒μ P? (μ i ¬¬pᵢ ∀[j<i]) = μ i (decidable-stable (P? i) (¬-recompute ¬¬pᵢ)) ∀[j<i] | ||
|
||
searchMinimalCounterexample : ∀ {P : Pred (Fin n) p} → Decidable P → (∀ i → P i) ⊎ μ⟨¬ P ⟩ | ||
searchMinimalCounterexample {zero} {P = _} P? = inj₁ λ() | ||
searchMinimalCounterexample {suc _} {P = P} P? with P? zero | ||
... | no ¬p₀ = inj₂ (μ zero ¬p₀ λ()) | ||
... | yes p₀ = Sum.map (∀-cons p₀) μ⁺ (searchMinimalCounterexample (P? ∘ suc)) | ||
where | ||
μ⁺ : μ⟨¬ P ∘ suc ⟩ → μ⟨¬ P ⟩ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a toplevel lemma?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm... I perhaps find that formulation compelling, if, as with all these things, the notational economy comes with an associated decompression penalty... Still, it is a direct analogue of Oh... hang on, where does |
||
μ⁺ (μ i ¬pₛᵢ ∀[j<i]P) = μ (suc i) ¬pₛᵢ | ||
λ where | ||
{zero} _ → p₀ | ||
{suc _} sj<si → ∀[j<i]P (ℕ.s<s⁻¹ sj<si) | ||
|
||
search-μ⟨¬_⟩ : ∀ {P : Pred (Fin n) p} → Decidable P → (∀ i → P i) ⊎ μ⟨¬ P ⟩ | ||
search-μ⟨¬_⟩ = searchMinimalCounterexample | ||
|
||
¬∀⟶∃¬-smallest : ∀ n {p} (P : Pred (Fin n) p) → Decidable P → | ||
¬ (∀ i → P i) → ∃ λ i → ¬ P i × ((j : Fin′ i) → P (inject j)) | ||
¬∀⟶∃¬-smallest zero P P? ¬∀P = contradiction (λ()) ¬∀P | ||
¬∀⟶∃¬-smallest (suc n) P P? ¬∀P with P? zero | ||
... | false because [¬P₀] = (zero , invert [¬P₀] , λ ()) | ||
... | true because [P₀] = map suc (map id (∀-cons (invert [P₀]))) | ||
(¬∀⟶∃¬-smallest n (P ∘ suc) (P? ∘ suc) (¬∀P ∘ (∀-cons (invert [P₀])))) | ||
¬∀⟶∃¬-smallest (suc n) P P? ¬∀P = [ flip contradiction ¬∀P , lemma ] $ search-μ⟨¬ P? ⟩ | ||
where | ||
lemma : μ⟨¬ P ⟩ → ∃ λ i → ¬ P i × ((j : Fin′ i) → P (inject j)) | ||
lemma (μ i ¬pᵢ ∀[j<i]P) = i , ¬-recompute ¬pᵢ , λ j → ∀[j<i]P (inject-< j) | ||
|
||
-- When P is a decidable predicate over a finite set the following | ||
-- lemma can be proved. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a notation for this in case you are not aware:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to this, I am going to hint that this suggestion (and the
syntax
underlying it) doesn't find universal (sic) favour with the other maintainers (see the feedback on the original #2744 from which this is derived)... and my choice ofμ⟨¬_⟩
is deliberately as a synonym/alias, and not assyntax
, as a consequence.