Skip to content

Commit 133bdb1

Browse files
authored
[ add ] clean version of Data.Fin.Properties.searchMinimalCounterexample (#2801)
* [ add ] clean version of `Data.Fin.Properties.searchMinimalCounterexample` * use: `Π[_]` syntax * refactor: simplify * refactor: drastic streamlining * refactor: remove redundant `import` * refactor: inlined `lemma` in `¬∀⇒∃¬`
1 parent 3a13de2 commit 133bdb1

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ Additions to existing modules
138138
≟-≡ : (eq : i ≡ j) → (i ≟ j) ≡ yes eq
139139
≟-≡-refl : (i : Fin n) → (i ≟ i) ≡ yes refl
140140
≟-≢ : (i≢j : i ≢ j) → (i ≟ j) ≡ no i≢j
141+
inject-< : inject j < i
142+
143+
record Least⟨_⟩ (P : Pred (Fin n) p) : Set p where
144+
constructor least
145+
field
146+
witness : Fin n
147+
example : P witness
148+
minimal : ∀ {j} → .(j < witness) → ¬ P j
149+
150+
search-least⟨_⟩ : Decidable P → Π[ ∁ P ] ⊎ Least⟨ P ⟩
151+
search-least⟨¬_⟩ : Decidable P → Π[ P ] ⊎ Least⟨ ∁ P ⟩
141152
```
142153

143154
* In `Data.Nat.ListAction.Properties`

src/Data/Fin/Properties.agda

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ open import Relation.Binary.PropositionalEquality.Properties as ≡
4848
open import Relation.Binary.PropositionalEquality as ≡
4949
using (≡-≟-identity; ≢-≟-identity)
5050
open import Relation.Nullary.Decidable as Dec
51-
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′)
51+
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′; decidable-stable)
5252
open import Relation.Nullary.Negation.Core
5353
using (¬_; contradiction; contradiction′)
5454
open import Relation.Nullary.Reflects using (invert)
5555
open import Relation.Unary as U
56-
using (U; Pred; Decidable; _⊆_; Satisfiable; Universal)
57-
open import Relation.Unary.Properties using (U?)
56+
using (U; Pred; Decidable; _⊆_; ∁; Satisfiable; Universal)
57+
open import Relation.Unary.Properties using (U?; ∁?)
5858

5959
private
6060
variable
@@ -471,6 +471,10 @@ toℕ-inject : ∀ {i : Fin n} (j : Fin′ i) → toℕ (inject j) ≡ toℕ j
471471
toℕ-inject {i = suc i} zero = refl
472472
toℕ-inject {i = suc i} (suc j) = cong suc (toℕ-inject j)
473473

474+
inject-< : {i : Fin n} (j : Fin′ i) inject j < i
475+
inject-< {i = suc i} zero = z<s
476+
inject-< {i = suc i} (suc j) = s<s (inject-< j)
477+
474478
------------------------------------------------------------------------
475479
-- inject₁
476480
------------------------------------------------------------------------
@@ -1036,23 +1040,47 @@ private
10361040
note P? = Dec.does (P? 0F) ∧ Dec.does (P? 1F) ∧ Dec.does (P? 2F) ∧ true
10371041
, refl
10381042

1039-
-- If a decidable predicate P over a finite set is sometimes false,
1040-
-- then we can find the smallest value for which this is the case.
1043+
------------------------------------------------------------------------
1044+
-- A decidable predicate P over a finite set is either always false, or
1045+
-- else we can find the least value for which P is true (and vice versa).
10411046

1042-
¬∀⇒∃¬-smallest : n {p} (P : Pred (Fin n) p) Decidable P
1043-
¬ ( i P i) λ i ¬ P i × ((j : Fin′ i) P (inject j))
1044-
¬∀⇒∃¬-smallest zero P P? ¬∀P = contradiction (λ()) ¬∀P
1045-
¬∀⇒∃¬-smallest (suc n) P P? ¬∀P with P? zero
1046-
... | false because [¬P₀] = (zero , invert [¬P₀] , λ ())
1047-
... | true because [P₀] = map suc (map id (∀-cons (invert [P₀])))
1048-
(¬∀⇒∃¬-smallest n (P ∘ suc) (P? ∘ suc) (¬∀P ∘ (∀-cons (invert [P₀]))))
1047+
record Least⟨_⟩ (P : Pred (Fin n) p) : Set p where
1048+
constructor least
1049+
field
1050+
witness : Fin n
1051+
example : P witness
1052+
minimal : {j} .(j < witness) ¬ P j
1053+
1054+
search-least⟨_⟩ : {P : Pred (Fin n) p} Decidable P Π[ ∁ P ] ⊎ Least⟨ P ⟩
1055+
search-least⟨_⟩ {n = zero} {P = _} P? = inj₁ λ()
1056+
search-least⟨_⟩ {n = suc _} {P = P} P? with P? zero
1057+
... | yes p₀ = inj₂ (least zero p₀ λ())
1058+
... | no ¬p₀ = Sum.map (∀-cons ¬p₀) lemma search-least⟨ P? ∘ suc ⟩
1059+
where
1060+
lemma : Least⟨ P ∘ suc ⟩ Least⟨ P ⟩
1061+
lemma (least i pₛᵢ ∀[j<i]¬P) = least (suc i) pₛᵢ λ where
1062+
{zero} _ ¬p₀
1063+
{suc _} sj<si ∀[j<i]¬P (ℕ.s<s⁻¹ sj<si)
1064+
1065+
search-least⟨¬_⟩ : {P : Pred (Fin n) p} Decidable P Π[ P ] ⊎ Least⟨ ∁ P ⟩
1066+
search-least⟨¬_⟩ {P = P} P? =
1067+
Sum.map₁ (λ ∀[¬¬P] j decidable-stable (P? j) (∀[¬¬P] j)) search-least⟨ ∁? P? ⟩
10491068

10501069
-- When P is a decidable predicate over a finite set the following
1051-
-- lemma can be proved.
1070+
-- lemmas can be proved.
1071+
1072+
¬∀⇒∃¬-smallest : n {p} (P : Pred (Fin n) p) Decidable P
1073+
¬ ( i P i) λ i ¬ P i × ((j : Fin′ i) P (inject j))
1074+
¬∀⇒∃¬-smallest n P P? ¬∀P = [ contradiction′ ¬∀P , lemma ] $ search-least⟨¬ P? ⟩
1075+
where
1076+
lemma : Least⟨ ∁ P ⟩ λ i ¬ P i × ((j : Fin′ i) P (inject j))
1077+
lemma (least i ¬pᵢ ∀[j<i]¬¬P) = i , ¬pᵢ , λ j
1078+
decidable-stable (P? (inject j)) (∀[j<i]¬¬P (inject-< j))
10521079

10531080
¬∀⇒∃¬ : n {p} (P : Pred (Fin n) p) Decidable P
10541081
¬ ( i P i) (∃ λ i ¬ P i)
1055-
¬∀⇒∃¬ n P P? ¬P = map id proj₁ (¬∀⇒∃¬-smallest n P P? ¬P)
1082+
¬∀⇒∃¬ n P P? ¬∀P =
1083+
[ contradiction′ ¬∀P , (λ (least i ¬pᵢ _) i , ¬pᵢ) ] $ search-least⟨¬ P? ⟩
10561084

10571085
-- lifting Dec over Unary subset relation
10581086

0 commit comments

Comments
 (0)