Skip to content

[ refactor ] make i ≢ j argument to Data.Fin.Base.punchOut irrelevant #2790

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ Non-backwards compatible changes
Minor improvements
------------------

* The type of `Relation.Nullary.Negation.Core.contradiction-irr` has been further
weakened so that the negated hypothesis `¬ A` is marked as irrelevant. This is
safe to do, in view of `Relation.Nullary.Recomputable.Properties.¬-recompute`.

* Similarly the type of `Data.Fin.Base.punchOut` has been weakened so that the
negated equational hypothesis `i ≢ j` is marked as irrelevant. This simplifies
some of the proofs of its properties, but also requires some slightly more
explicit instantiation in a couple of places.

* Refactored usages of `+-∸-assoc 1` to `∸-suc` in:
```agda
README.Data.Fin.Relation.Unary.Top
Expand All @@ -35,6 +44,11 @@ Deprecated names
* In `Algebra.Properties.CommutativeSemigroup`:
```agda
interchange ↦ medial
```

* In `Data.Fin.Properties`:
```agda
punchOut-cong′ ↦ punchOut-cong
```

New modules
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Fin/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open import Level using (0ℓ)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≢_; refl; cong)
open import Relation.Binary.Indexed.Heterogeneous.Core using (IRel)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
open import Relation.Nullary.Negation.Core using (¬_; contradiction; contradiction-irr)

private
variable
Expand Down Expand Up @@ -252,8 +252,8 @@ opposite {suc n} (suc i) = inject₁ (opposite i)
-- This is a variant of the thick function from Conor
-- McBride's "First-order unification by structural recursion".

punchOut : ∀ {i j : Fin (suc n)} → i ≢ j → Fin n
punchOut {_} {zero} {zero} i≢j = contradiction refl i≢j
punchOut : ∀ {i j : Fin (suc n)} → .(i ≢ j) → Fin n
punchOut {_} {zero} {zero} i≢j = contradiction-irr refl i≢j
punchOut {_} {zero} {suc j} _ = j
punchOut {suc _} {suc i} {zero} _ = zero
punchOut {suc _} {suc i} {suc j} i≢j = suc (punchOut (i≢j ∘ cong suc))
Expand Down
78 changes: 43 additions & 35 deletions src/Data/Fin/Permutation.agda
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ open import Data.Bool.Base using (true; false)
open import Data.Fin.Base using (Fin; suc; cast; opposite; punchIn; punchOut)
open import Data.Fin.Patterns using (0F; 1F)
open import Data.Fin.Properties using (punchInᵢ≢i; punchOut-punchIn;
punchOut-cong; punchOut-cong′; punchIn-punchOut
punchOut-cong; punchIn-punchOut
; _≟_; ¬Fin0; cast-involutive; opposite-involutive)
import Data.Fin.Permutation.Components as PC
open import Data.Nat.Base using (ℕ; suc; zero; 2+)
Expand All @@ -28,7 +28,7 @@ open import Level using (0ℓ)
open import Relation.Binary.Core using (Rel)
open import Relation.Nullary using (does; ¬_; yes; no)
open import Relation.Nullary.Decidable using (dec-yes; dec-no)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Nullary.Negation using (contradiction; contradiction-irr)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; _≢_; refl; sym; trans; subst; cong; cong₂)
open import Relation.Binary.PropositionalEquality.Properties
Expand Down Expand Up @@ -167,19 +167,30 @@ remove {m} {n} i π = permutation to from inverseˡ′ inverseʳ′

inverseʳ′ : StrictlyInverseʳ _≡_ to from
inverseʳ′ j = begin
from (to j) ≡⟨⟩
punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut _)) ⟩
punchOut {i = i} {πˡ (πʳ (punchIn i j))} _ ≡⟨ punchOut-cong i (inverseˡ π) ⟩
punchOut {i = i} {punchIn i j} _ ≡⟨ punchOut-punchIn i ⟩
j ∎
from (to j) ≡⟨⟩
punchOut {i = i} _ ≡⟨ punchOut-cong i (cong πˡ eq) ⟩
punchOut neq ≡⟨ punchOut-cong i (inverseˡ π) ⟩
punchOut {i = i} _ ≡⟨ punchOut-punchIn i ⟩
j ∎
where
eq : punchIn (πʳ i) (to j) ≡ πʳ (punchIn i j)
eq = punchIn-punchOut to-punchOut
neq : i ≢ πˡ (πʳ (punchIn i j))
neq eq = punchInᵢ≢i i j (sym (trans eq (inverseˡ π)))


inverseˡ′ : StrictlyInverseˡ _≡_ to from
inverseˡ′ j = begin
to (from j) ≡⟨⟩
punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut _)) ⟩
punchOut {i = πʳ i} {πʳ (πˡ (punchIn (πʳ i) j))} _ ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
j ∎
to (from j) ≡⟨⟩
punchOut {i = πʳ i} _ ≡⟨ punchOut-cong (πʳ i) (cong πʳ eq) ⟩
punchOut neq ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
punchOut {i = πʳ i} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
j ∎
where
eq : punchIn i (from j) ≡ πˡ (punchIn (πʳ i) j)
eq = punchIn-punchOut from-punchOut
neq : πʳ i ≢ πʳ (πˡ (punchIn (πʳ i) j))
neq = permute-≢ from-punchOut

------------------------------------------------------------------------
-- Lifting
Expand Down Expand Up @@ -233,11 +244,11 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
with j≢punchInⱼπʳpunchOuti≢k ← punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym
rewrite dec-no (j ≟ punchIn j (π ⟨$⟩ʳ punchOut i≢k)) j≢punchInⱼπʳpunchOuti≢k
= begin
punchIn i (π ⟨$⟩ˡ punchOut j≢punchInⱼπʳpunchOuti≢k) ≡⟨ cong (λ l → punchIn i (π ⟨$⟩ˡ l)) (punchOut-cong j refl)
punchIn i (π ⟨$⟩ˡ punchOut (punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym)) ≡⟨ cong (λ l → punchIn i (π ⟨$⟩ˡ l)) (punchOut-punchIn j) ⟩
punchIn i (π ⟨$⟩ˡ (π ⟨$⟩ʳ punchOut i≢k)) ≡⟨ cong (punchIn i) (inverseˡ π) ⟩
punchIn i (punchOut i≢k) ≡⟨ punchIn-punchOut i≢k ⟩
k ∎
punchIn i (π ⟨$⟩ˡ punchOut j≢punchInⱼπʳpunchOuti≢k) ≡⟨⟩
punchIn i (π ⟨$⟩ˡ punchOut (punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym)) ≡⟨ cong (punchIn i (π ⟨$⟩ˡ_)) (punchOut-punchIn j) ⟩
punchIn i (π ⟨$⟩ˡ (π ⟨$⟩ʳ punchOut i≢k)) ≡⟨ cong (punchIn i) (inverseˡ π) ⟩
punchIn i (punchOut i≢k) ≡⟨ punchIn-punchOut i≢k ⟩
k

inverseˡ′ : StrictlyInverseˡ _≡_ to from
inverseˡ′ k with j ≟ k
Expand All @@ -246,8 +257,8 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
with i≢punchInᵢπˡpunchOutj≢k ← punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym
rewrite dec-no (i ≟ punchIn i (π ⟨$⟩ˡ punchOut j≢k)) i≢punchInᵢπˡpunchOutj≢k
= begin
punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢπˡpunchOutj≢k) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-cong i refl)
punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym)) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-punchIn i) ⟩
punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢπˡpunchOutj≢k) ≡⟨⟩
punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym)) ≡⟨ cong (punchIn j (π ⟨$⟩ʳ_)) (punchOut-punchIn i) ⟩
punchIn j (π ⟨$⟩ʳ (π ⟨$⟩ˡ punchOut j≢k)) ≡⟨ cong (punchIn j) (inverseʳ π) ⟩
punchIn j (punchOut j≢k) ≡⟨ punchIn-punchOut j≢k ⟩
k ∎
Expand Down Expand Up @@ -285,8 +296,9 @@ module _ (π : Permutation (suc m) (suc n)) where
lift₀-remove p 0F = sym p
lift₀-remove p (suc i) = punchOut-zero (πʳ (suc i)) p
where
punchOut-zero : ∀ {i} (j : Fin (suc n)) {neq} → i ≡ 0F → suc (punchOut {i = i} {j} neq) ≡ j
punchOut-zero 0F {neq} p = contradiction p neq
punchOut-zero : ∀ {i} (j : Fin (suc n)) .{neq} →
i ≡ 0F → suc (punchOut {i = i} {j} neq) ≡ j
punchOut-zero 0F {neq} eq = contradiction-irr eq neq
punchOut-zero (suc j) refl = refl

↔⇒≡ : Permutation m n → m ≡ n
Expand Down Expand Up @@ -322,29 +334,25 @@ lift₀-transpose i j (suc k) with does (k ≟ i)
... | false = refl
... | true = refl

insert-punchIn : ∀ i j (π : Permutation m n) k → insert i j π ⟨$⟩ʳ punchIn i k ≡ punchIn j (π ⟨$⟩ʳ k)
insert-punchIn : ∀ i j (π : Permutation m n) k →
insert i j π ⟨$⟩ʳ punchIn i k ≡ punchIn j (π ⟨$⟩ʳ k)
insert-punchIn i j π k with i ≟ punchIn i k
... | yes i≡punchInᵢk = contradiction (sym i≡punchInᵢk) (punchInᵢ≢i i k)
... | no i≢punchInᵢk = begin
punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢk) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-cong i refl) ⟩
punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i k ∘ sym)) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-punchIn i) ⟩
punchIn j (π ⟨$⟩ʳ k) ∎
... | no i≢punchInᵢk = cong (punchIn j ∘ (π ⟨$⟩ʳ_)) (punchOut-punchIn i)

insert-remove : ∀ i (π : Permutation (suc m) (suc n)) → insert i (π ⟨$⟩ʳ i) (remove i π) ≈ π
insert-remove {m = m} {n = n} i π j with i ≟ j
insert-remove : ∀ i (π : Permutation (suc m) (suc n)) →
insert i (π ⟨$⟩ʳ i) (remove i π) ≈ π
insert-remove i π j with i ≟ j
... | yes i≡j = cong (π ⟨$⟩ʳ_) i≡j
... | no i≢j = begin
punchIn (π ⟨$⟩ʳ i) (punchOut (punchInᵢ≢i i (punchOut i≢j) ∘ sym ∘ Injection.injective (↔⇒↣ π))) ≡⟨ punchIn-punchOut _
punchIn (π ⟨$⟩ʳ i) _ ≡⟨ punchIn-punchOut (punchInᵢ≢i i (punchOut i≢j) ∘ sym ∘ Injection.injective (↔⇒↣ π)) ⟩
π ⟨$⟩ʳ punchIn i (punchOut i≢j) ≡⟨ cong (π ⟨$⟩ʳ_) (punchIn-punchOut i≢j) ⟩
π ⟨$⟩ʳ j ∎

remove-insert : ∀ i j (π : Permutation m n) → remove i (insert i j π) ≈ π
remove-insert i j π k with i ≟ i
... | no i≢i = contradiction refl i≢i
... | yes _ = begin
punchOut {i = j} _
≡⟨ punchOut-cong j (insert-punchIn i j π k) ⟩
punchOut {i = j} (punchInᵢ≢i j (π ⟨$⟩ʳ k) ∘ sym)
≡⟨ punchOut-punchIn j ⟩
π ⟨$⟩ʳ k
punchOut {i = j} _ ≡⟨ punchOut-cong j (insert-punchIn i j π k) ⟩
punchOut (punchInᵢ≢i j (π ⟨$⟩ʳ k) ∘ sym) ≡⟨ punchOut-punchIn j ⟩
π ⟨$⟩ʳ k ∎
56 changes: 29 additions & 27 deletions src/Data/Fin/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ open import Relation.Binary.PropositionalEquality.Properties as ≡
using (module ≡-Reasoning)
open import Relation.Nullary.Decidable as Dec
using (Dec; _because_; yes; no; _×-dec_; _⊎-dec_; map′)
open import Relation.Nullary.Negation.Core using (¬_; contradiction)
open import Relation.Nullary.Negation.Core
using (¬_; contradiction; contradiction-irr)
open import Relation.Nullary.Reflects using (Reflects; invert)
open import Relation.Unary as U
using (U; Pred; Decidable; _⊆_; Satisfiable; Universal)
Expand Down Expand Up @@ -877,35 +878,27 @@ punchInᵢ≢i (suc i) (suc j) = punchInᵢ≢i i j ∘ suc-injective
-- can be changed out arbitrarily (reflecting the proof-irrelevance of
-- that argument).

punchOut-cong : ∀ (i : Fin (suc n)) {j k} {i≢j : i ≢ j} {i≢k : i ≢ k} →
punchOut-cong : ∀ (i : Fin (suc n)) {j k} .{i≢j : i ≢ j} .{i≢k : i ≢ k} →
j ≡ k → punchOut i≢j ≡ punchOut i≢k
punchOut-cong {_} zero {zero} {i≢j = 0≢0} = contradiction refl 0≢0
punchOut-cong {_} zero {suc j} {zero} {i≢k = 0≢0} = contradiction refl 0≢0
punchOut-cong {_} zero {zero} {i≢j = 0≢0} = contradiction-irr refl 0≢0
punchOut-cong {_} zero {suc j} {zero} {i≢k = 0≢0} = contradiction-irr refl 0≢0
punchOut-cong {_} zero {suc j} {suc k} = suc-injective
punchOut-cong {suc n} (suc i) {zero} {zero} _ = refl
punchOut-cong {suc n} (suc i) {suc j} {suc k} = cong suc ∘ punchOut-cong i ∘ suc-injective

-- An alternative to 'punchOut-cong' in the which the new inequality
-- argument is specific. Useful for enabling the omission of that
-- argument during equational reasoning.

punchOut-cong′ : ∀ (i : Fin (suc n)) {j k} {p : i ≢ j} (q : j ≡ k) →
punchOut p ≡ punchOut (p ∘ sym ∘ trans q ∘ sym)
punchOut-cong′ i q = punchOut-cong i q
punchOut-cong {suc n} (suc i) {zero} {zero} = λ _ → refl
punchOut-cong {suc n} (suc i) {suc j} {suc k} = cong suc ∘ punchOut-cong i ∘ suc-injective

punchOut-injective : ∀ {i j k : Fin (suc n)}
(i≢j : i ≢ j) (i≢k : i ≢ k) →
.(i≢j : i ≢ j) .(i≢k : i ≢ k) →
punchOut i≢j ≡ punchOut i≢k → j ≡ k
punchOut-injective {_} {zero} {zero} {_} 0≢0 _ _ = contradiction refl 0≢0
punchOut-injective {_} {zero} {_} {zero} _ 0≢0 _ = contradiction refl 0≢0
punchOut-injective {_} {zero} {zero} {_} 0≢0 _ _ = contradiction-irr refl 0≢0
punchOut-injective {_} {zero} {_} {zero} _ 0≢0 _ = contradiction-irr refl 0≢0
punchOut-injective {_} {zero} {suc j} {suc k} _ _ pⱼ≡pₖ = cong suc pⱼ≡pₖ
punchOut-injective {suc n} {suc i} {zero} {zero} _ _ _ = refl
punchOut-injective {suc n} {suc i} {suc j} {suc k} i≢j i≢k pⱼ≡pₖ =
cong suc (punchOut-injective (i≢j ∘ cong suc) (i≢k ∘ cong suc) (suc-injective pⱼ≡pₖ))

punchIn-punchOut : ∀ {i j : Fin (suc n)} (i≢j : i ≢ j) →
punchIn-punchOut : ∀ {i j : Fin (suc n)} .(i≢j : i ≢ j) →
punchIn i (punchOut i≢j) ≡ j
punchIn-punchOut {_} {zero} {zero} 0≢0 = contradiction refl 0≢0
punchIn-punchOut {_} {zero} {zero} 0≢0 = contradiction-irr refl 0≢0
punchIn-punchOut {_} {zero} {suc j} _ = refl
punchIn-punchOut {suc m} {suc i} {zero} i≢j = refl
punchIn-punchOut {suc m} {suc i} {suc j} i≢j =
Expand All @@ -914,11 +907,7 @@ punchIn-punchOut {suc m} {suc i} {suc j} i≢j =
punchOut-punchIn : ∀ i {j : Fin n} → punchOut {i = i} {j = punchIn i j} (punchInᵢ≢i i j ∘ sym) ≡ j
punchOut-punchIn zero {j} = refl
punchOut-punchIn (suc i) {zero} = refl
punchOut-punchIn (suc i) {suc j} = cong suc (begin
punchOut (punchInᵢ≢i i j ∘ suc-injective ∘ sym ∘ cong suc) ≡⟨ punchOut-cong i refl ⟩
punchOut (punchInᵢ≢i i j ∘ sym) ≡⟨ punchOut-punchIn i ⟩
j ∎)
where open ≡-Reasoning
punchOut-punchIn (suc i) {suc j} = cong suc (punchOut-punchIn i)


------------------------------------------------------------------------
Expand Down Expand Up @@ -1035,9 +1024,12 @@ pigeonhole : m ℕ.< n → (f : Fin n → Fin m) → ∃₂ λ i j → i < j ×
pigeonhole z<s f = contradiction (f zero) λ()
pigeonhole (s<s m<n@(s≤s _)) f with any? (λ k → f zero ≟ f (suc k))
... | yes (j , f₀≡fⱼ) = zero , suc j , z<s , f₀≡fⱼ
... | no f₀≢fₖ
with i , j , i<j , fᵢ≡fⱼ ← pigeonhole m<n (λ j → punchOut (f₀≢fₖ ∘ (j ,_ )))
= suc i , suc j , s<s i<j , punchOut-injective (f₀≢fₖ ∘ (i ,_)) _ fᵢ≡fⱼ
... | no ¬∃[k]f₀≡fₛₖ =
let i , j , i<j , fᵢ≡fⱼ = pigeonhole m<n (λ k → punchOut (f₀≢fₛ k))
in suc i , suc j , s<s i<j , punchOut-injective (f₀≢fₛ i) (f₀≢fₛ j) fᵢ≡fⱼ
where
f₀≢fₛ : ∀ k → f zero ≢ f (suc k)
f₀≢fₛ k = ¬∃[k]f₀≡fₛₖ ∘ (k ,_)

injective⇒≤ : ∀ {f : Fin m → Fin n} → Injective _≡_ _≡_ f → m ℕ.≤ n
injective⇒≤ {zero} {_} {f} _ = z≤n
Expand Down Expand Up @@ -1233,3 +1225,13 @@ Please use <⇒<′ instead."
"Warning: <′⇒≺ was deprecated in v2.0.
Please use <′⇒< instead."
#-}

-- Version 2.4

punchOut-cong′ : ∀ (i : Fin (suc n)) {j k} {p : i ≢ j} (q : j ≡ k) →
punchOut p ≡ punchOut (p ∘ sym ∘ trans q ∘ sym)
punchOut-cong′ i q = punchOut-cong i q
{-# WARNING_ON_USAGE punchOut-cong′
"Warning: punchOut-cong′ was deprecated in v2.4.
Please use punchOut-cong instead."
#-}
4 changes: 2 additions & 2 deletions src/Relation/Nullary/Negation/Core.agda
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ _¬-⊎_ = [_,_]
------------------------------------------------------------------------
-- Uses of negation

contradiction-irr : .A → ¬ A → Whatever
contradiction-irr : .A → .(¬ A) → Whatever
contradiction-irr a ¬a = ⊥-elim-irr (¬a a)

contradiction : A → ¬ A → Whatever
contradiction a = contradiction-irr a
contradiction a ¬a = contradiction-irr a ¬a

contradiction₂ : A ⊎ B → ¬ A → ¬ B → Whatever
contradiction₂ (inj₁ a) ¬a ¬b = contradiction a ¬a
Expand Down