Skip to content

Commit d4c0a97

Browse files
authored
[ refactor ] Data.Fin.Properties of decidable equality, plus knock-ons (#2740)
* add: lemmas for decidable equality on `Fin`, plus backport to `Nat` * refactor: knock-ons * fix: `import`s * tighten up * refactor: add new lemmas, simplify old proof * refactor: additional instantiated lemmas * refactor: further streamlining of proofs and `import`s * `CHANGELOG` * whitespace * fix: #2743 * fix: deprecation bug * fix: equational reaosning layout * fix: merge conflicts * fix: equation alignment * fix: equation alignment * refactor: `yes` and `no` * fix\; remove Fairbairn violation * rename: `transpose` lemmas * rename: decidable equality lemmas * fix: use of new names * restore: original alignment * fix: `CHANGELOG`
1 parent 35396e4 commit d4c0a97

File tree

5 files changed

+92
-55
lines changed

5 files changed

+92
-55
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,24 @@ Additions to existing modules
5656
[-x][-y]≈xy : ∀ x y → - x * - y ≈ x * y
5757
```
5858

59+
* In `Data.Fin.Permutation.Components`:
60+
```agda
61+
transpose[i,i,j]≡j : (i j : Fin n) → transpose i i j ≡ j
62+
transpose[i,j,j]≡i : (i j : Fin n) → transpose i j j ≡ i
63+
transpose[i,j,i]≡j : (i j : Fin n) → transpose i j i ≡ j
64+
transpose-transpose : transpose i j k ≡ l → transpose j i l ≡ k
65+
```
66+
67+
* In `Data.Fin.Properties`:
68+
```agda
69+
≡-irrelevant : Irrelevant {A = Fin n} _≡_
70+
≟-≡ : (eq : i ≡ j) → (i ≟ j) ≡ yes eq
71+
≟-≡-refl : (i : Fin n) → (i ≟ i) ≡ yes refl
72+
≟-≢ : (i≢j : i ≢ j) → (i ≟ j) ≡ no i≢j
73+
```
74+
5975
* In `Data.Nat.Properties`:
6076
```agda
77+
≟-≢ : (m≢n : m ≢ n) → (m ≟ n) ≡ no m≢n
6178
∸-suc : m ≤ n → suc n ∸ m ≡ suc (n ∸ m)
6279
```

src/Data/Fin/Permutation.agda

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ module Data.Fin.Permutation where
1111
open import Data.Bool.Base using (true; false)
1212
open import Data.Fin.Base using (Fin; suc; cast; opposite; punchIn; punchOut)
1313
open import Data.Fin.Patterns using (0F; 1F)
14-
open import Data.Fin.Properties using (punchInᵢ≢i; punchOut-punchIn;
15-
punchOut-cong; punchOut-cong′; punchIn-punchOut
16-
; _≟_; ¬Fin0; cast-involutive; opposite-involutive)
14+
open import Data.Fin.Properties
15+
using (¬Fin0; _≟_; ≟-≡-refl; ≟-≢
16+
; cast-involutive; opposite-involutive
17+
; punchInᵢ≢i; punchOut-punchIn; punchIn-punchOut
18+
; punchOut-cong; punchOut-cong′)
1719
import Data.Fin.Permutation.Components as PC
1820
open import Data.Nat.Base using (ℕ; suc; zero; 2+)
1921
open import Data.Product.Base using (_,_; proj₂)
@@ -26,9 +28,8 @@ open import Function.Properties.Inverse using (↔⇒↣)
2628
open import Function.Base using (_∘_; _∘′_)
2729
open import Level using (0ℓ)
2830
open import Relation.Binary.Core using (Rel)
29-
open import Relation.Nullary using (does; ¬_; yes; no)
30-
open import Relation.Nullary.Decidable using (dec-yes; dec-no)
31-
open import Relation.Nullary.Negation using (contradiction)
31+
open import Relation.Nullary.Decidable.Core using (does; yes; no)
32+
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
3233
open import Relation.Binary.PropositionalEquality.Core
3334
using (_≡_; _≢_; refl; sym; trans; subst; cong; cong₂)
3435
open import Relation.Binary.PropositionalEquality.Properties
@@ -167,19 +168,19 @@ remove {m} {n} i π = permutation to from inverseˡ′ inverseʳ′
167168

168169
inverseʳ′ : StrictlyInverseʳ _≡_ to from
169170
inverseʳ′ j = begin
170-
from (to j) ≡⟨⟩
171-
punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut _)) ⟩
172-
punchOut {i = i} {πˡ (πʳ (punchIn i j))} _ ≡⟨ punchOut-cong i (inverseˡ π) ⟩
173-
punchOut {i = i} {punchIn i j} _ ≡⟨ punchOut-punchIn i ⟩
174-
j
171+
from (to j) ≡⟨⟩
172+
punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut _)) ⟩
173+
punchOut {i = i} {πˡ (πʳ (punchIn i j))} _ ≡⟨ punchOut-cong i (inverseˡ π) ⟩
174+
punchOut {i = i} {punchIn i j} _ ≡⟨ punchOut-punchIn i ⟩
175+
j ∎
175176

176177
inverseˡ′ : StrictlyInverseˡ _≡_ to from
177178
inverseˡ′ j = begin
178-
to (from j) ≡⟨⟩
179-
punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut _)) ⟩
180-
punchOut {i = πʳ i} {πʳ (πˡ (punchIn (πʳ i) j))} _ ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
181-
punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
182-
j
179+
to (from j) ≡⟨⟩
180+
punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut _)) ⟩
181+
punchOut {i = πʳ i} {πʳ (πˡ (punchIn (πʳ i) j))} _ ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
182+
punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
183+
j ∎
183184

184185
------------------------------------------------------------------------
185186
-- Lifting
@@ -228,23 +229,23 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
228229

229230
inverseʳ′ : StrictlyInverseʳ _≡_ to from
230231
inverseʳ′ k with i ≟ k
231-
... | yes i≡k rewrite proj₂ (dec-yes (j ≟ j) refl) = i≡k
232+
... | yes i≡k rewrite ≟-≡-refl j = i≡k
232233
... | no i≢k
233234
with j≢punchInⱼπʳpunchOuti≢k punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym
234-
rewrite dec-no (j ≟ punchIn j (π ⟨$⟩ʳ punchOut i≢k)) j≢punchInⱼπʳpunchOuti≢k
235+
rewrite ≟-≢ j≢punchInⱼπʳpunchOuti≢k
235236
= begin
236237
punchIn i (π ⟨$⟩ˡ punchOut j≢punchInⱼπʳpunchOuti≢k) ≡⟨ cong (λ l punchIn i (π ⟨$⟩ˡ l)) (punchOut-cong j refl) ⟩
237238
punchIn i (π ⟨$⟩ˡ punchOut (punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym)) ≡⟨ cong (λ l punchIn i (π ⟨$⟩ˡ l)) (punchOut-punchIn j) ⟩
238-
punchIn i (π ⟨$⟩ˡ (π ⟨$⟩ʳ punchOut i≢k)) ≡⟨ cong (punchIn i) (inverseˡ π) ⟩
239-
punchIn i (punchOut i≢k) ≡⟨ punchIn-punchOut i≢k ⟩
240-
k ∎
239+
punchIn i (π ⟨$⟩ˡ (π ⟨$⟩ʳ punchOut i≢k)) ≡⟨ cong (punchIn i) (inverseˡ π) ⟩
240+
punchIn i (punchOut i≢k) ≡⟨ punchIn-punchOut i≢k ⟩
241+
k
241242

242243
inverseˡ′ : StrictlyInverseˡ _≡_ to from
243244
inverseˡ′ k with j ≟ k
244-
... | yes j≡k rewrite proj₂ (dec-yes (i ≟ i) refl) = j≡k
245+
... | yes j≡k rewrite ≟-≡-refl i = j≡k
245246
... | no j≢k
246247
with i≢punchInᵢπˡpunchOutj≢k punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym
247-
rewrite dec-no (i ≟ punchIn i (π ⟨$⟩ˡ punchOut j≢k)) i≢punchInᵢπˡpunchOutj≢k
248+
rewrite ≟-≢ i≢punchInᵢπˡpunchOutj≢k
248249
= begin
249250
punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢπˡpunchOutj≢k) ≡⟨ cong (λ l punchIn j (π ⟨$⟩ʳ l)) (punchOut-cong i refl) ⟩
250251
punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym)) ≡⟨ cong (λ l punchIn j (π ⟨$⟩ʳ l)) (punchOut-punchIn i) ⟩
@@ -339,12 +340,7 @@ insert-remove {m = m} {n = n} i π j with i ≟ j
339340
π ⟨$⟩ʳ j ∎
340341

341342
remove-insert : i j (π : Permutation m n) remove i (insert i j π) ≈ π
342-
remove-insert i j π k with i ≟ i
343-
... | no i≢i = contradiction refl i≢i
344-
... | yes _ = begin
345-
punchOut {i = j} _
346-
≡⟨ punchOut-cong j (insert-punchIn i j π k) ⟩
347-
punchOut {i = j} (punchInᵢ≢i j (π ⟨$⟩ʳ k) ∘ sym)
348-
≡⟨ punchOut-punchIn j ⟩
349-
π ⟨$⟩ʳ k
350-
343+
remove-insert i j π k rewrite ≟-≡-refl i = begin
344+
punchOut {i = j} _ ≡⟨ punchOut-cong j (insert-punchIn i j π k) ⟩
345+
punchOut {i = j} (punchInᵢ≢i j (π ⟨$⟩ʳ k) ∘ sym) ≡⟨ punchOut-punchIn j ⟩
346+
π ⟨$⟩ʳ k ∎

src/Data/Fin/Permutation/Components.agda

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,17 @@ module Data.Fin.Permutation.Components where
1111
open import Data.Bool.Base using (Bool; true; false)
1212
open import Data.Fin.Base using (Fin; suc; opposite; toℕ)
1313
open import Data.Fin.Properties
14-
using (_≟_; opposite-prop; opposite-involutive; opposite-suc)
15-
open import Data.Nat.Base as ℕ using (zero; suc; _∸_)
16-
open import Data.Product.Base using (proj₂)
17-
open import Function.Base using (_∘_)
18-
open import Relation.Nullary.Reflects using (invert)
19-
open import Relation.Nullary using (does; _because_; yes; no)
20-
open import Relation.Nullary.Decidable using (dec-true; dec-false)
14+
using (_≟_; ≟-≡; ≟-≡-refl
15+
; opposite-prop; opposite-involutive; opposite-suc)
16+
open import Relation.Nullary.Decidable.Core using (does; yes; no)
2117
open import Relation.Binary.PropositionalEquality.Core
22-
using (_≡_; refl; sym; trans)
23-
open import Relation.Binary.PropositionalEquality.Properties
24-
using (module ≡-Reasoning)
25-
open import Algebra.Definitions using (Involutive)
26-
open ≡-Reasoning
18+
using (_≡_; refl; sym)
2719

2820
------------------------------------------------------------------------
2921
-- Functions
3022
------------------------------------------------------------------------
3123

32-
-- 'tranpose i j' swaps the places of 'i' and 'j'.
24+
-- 'transpose i j' swaps the places of 'i' and 'j'.
3325

3426
transpose : {n} Fin n Fin n Fin n Fin n
3527
transpose i j k with does (k ≟ i)
@@ -42,17 +34,31 @@ transpose i j k with does (k ≟ i)
4234
-- Properties
4335
------------------------------------------------------------------------
4436

37+
transpose[i,i,j]≡j : {n} (i j : Fin n) transpose i i j ≡ j
38+
transpose[i,i,j]≡j i j with j ≟ i in j≟i
39+
... | yes j≡i = sym j≡i
40+
... | no _ rewrite j≟i = refl
41+
42+
transpose[i,j,j]≡i : {n} (i j : Fin n) transpose i j j ≡ i
43+
transpose[i,j,j]≡i i j with j ≟ i
44+
... | yes j≡i = j≡i
45+
... | no _ rewrite ≟-≡-refl j = refl
46+
47+
transpose[i,j,i]≡j : {n} (i j : Fin n) transpose i j i ≡ j
48+
transpose[i,j,i]≡j i j rewrite ≟-≡-refl i = refl
49+
50+
transpose-transpose : {n} {i j k l : Fin n}
51+
transpose i j k ≡ l transpose j i l ≡ k
52+
transpose-transpose {n} {i} {j} {k} {l} eq with k ≟ i in k≟i
53+
... | yes k≡i rewrite ≟-≡ (sym eq) = sym k≡i
54+
... | no k≢i with k ≟ j in k≟j
55+
... | yes k≡j rewrite eq | transpose[i,j,j]≡i j l = sym k≡j
56+
... | no k≢j rewrite eq | k≟j | k≟i = refl
57+
4558
transpose-inverse : {n} (i j : Fin n) {k}
4659
transpose i j (transpose j i k) ≡ k
47-
transpose-inverse i j {k} with k ≟ j
48-
... | true because [k≡j] rewrite dec-true (i ≟ i) refl = sym (invert [k≡j])
49-
... | false because [k≢j] with k ≟ i
50-
... | true because [k≡i]
51-
rewrite dec-false (j ≟ i) (invert [k≢j] ∘ trans (invert [k≡i]) ∘ sym)
52-
| dec-true (j ≟ j) refl
53-
= sym (invert [k≡i])
54-
... | false because [k≢i] rewrite dec-false (k ≟ i) (invert [k≢i])
55-
| dec-false (k ≟ j) (invert [k≢j]) = refl
60+
transpose-inverse i j = transpose-transpose refl
61+
5662

5763
------------------------------------------------------------------------
5864
-- DEPRECATED NAMES

src/Data/Fin/Properties.agda

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module Data.Fin.Properties where
1212

1313
open import Axiom.Extensionality.Propositional
14+
open import Axiom.UniquenessOfIdentityProofs using (module Decidable⇒UIP)
1415
open import Algebra.Definitions using (Involutive)
1516
open import Effect.Applicative using (RawApplicative)
1617
open import Effect.Functor using (RawFunctor)
@@ -44,10 +45,12 @@ open import Relation.Binary.PropositionalEquality.Core as ≡
4445
using (_≡_; _≢_; refl; sym; trans; cong; cong₂; subst; _≗_)
4546
open import Relation.Binary.PropositionalEquality.Properties as ≡
4647
using (module ≡-Reasoning)
48+
open import Relation.Binary.PropositionalEquality as ≡
49+
using (≡-≟-identity; ≢-≟-identity)
4750
open import Relation.Nullary.Decidable as Dec
4851
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′)
4952
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
50-
open import Relation.Nullary.Reflects using (Reflects; invert)
53+
open import Relation.Nullary.Reflects using (invert)
5154
open import Relation.Unary as U
5255
using (U; Pred; Decidable; _⊆_; Satisfiable; Universal)
5356
open import Relation.Unary.Properties using (U?)
@@ -100,6 +103,18 @@ zero ≟ suc y = no λ()
100103
suc x ≟ zero = no λ()
101104
suc x ≟ suc y = map′ (cong suc) suc-injective (x ≟ y)
102105

106+
≡-irrelevant : Irrelevant {A = Fin n} _≡_
107+
≡-irrelevant = Decidable⇒UIP.≡-irrelevant _≟_
108+
109+
≟-≡ : (eq : i ≡ j) (i ≟ j) ≡ yes eq
110+
≟-≡ = ≡-≟-identity _≟_
111+
112+
≟-≡-refl : (i : Fin n) (i ≟ i) ≡ yes refl
113+
≟-≡-refl _ = ≟-≡ refl
114+
115+
≟-≢ : (i≢j : i ≢ j) (i ≟ j) ≡ no i≢j
116+
≟-≢ = ≢-≟-identity _≟_
117+
103118
------------------------------------------------------------------------
104119
-- Structures
105120

src/Data/Nat/Properties.agda

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ m ≟ n = map′ (≡ᵇ⇒≡ m n) (≡⇒≡ᵇ m n) (T? (m ≡ᵇ n))
114114
≟-diag : (eq : m ≡ n) (m ≟ n) ≡ yes eq
115115
≟-diag = ≡-≟-identity _≟_
116116

117+
≟-≡ : (m≢n : m ≢ n) (m ≟ n) ≡ no m≢n
118+
≟-≡ = ≢-≟-identity _≟_
119+
117120
≡-isDecEquivalence : IsDecEquivalence (_≡_ {A = ℕ})
118121
≡-isDecEquivalence = record
119122
{ isEquivalence = isEquivalence

0 commit comments

Comments
 (0)