Skip to content

Commit 6aa4653

Browse files
committed
[ fix agda#1354 ] Refactoring Permutation.Propositional
1 parent cadca35 commit 6aa4653

File tree

7 files changed

+125
-122
lines changed

7 files changed

+125
-122
lines changed

README/Data/List/Relation/Binary/Permutation.agda

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ module README.Data.List.Relation.Binary.Permutation where
99
open import Algebra.Structures using (IsCommutativeMonoid)
1010
open import Data.List.Base
1111
open import Data.Nat using (ℕ; _+_)
12-
open import Relation.Binary.PropositionalEquality
13-
using (_≡_; refl; sym; cong; setoid)
1412

1513
------------------------------------------------------------------------
1614
-- Permutations

src/Data/List/Relation/Binary/BagAndSetEquality.agda

+21-21
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ open import Relation.Binary
4040
import Relation.Binary.Reasoning.Setoid as EqR
4141
import Relation.Binary.Reasoning.Preorder as PreorderReasoning
4242
open import Relation.Binary.PropositionalEquality as P
43-
using (_≡_; _≢_; _≗_; refl)
43+
using (_≡_; _≢_; _≗_)
4444
open import Relation.Nullary
4545
open import Data.List.Membership.Propositional.Properties
4646

@@ -115,7 +115,7 @@ module ⊆-Reasoning where
115115
module _ {a k} {A : Set a} {x y : A} {xs ys} where
116116

117117
∷-cong : x ≡ y xs ∼[ k ] ys x ∷ xs ∼[ k ] y ∷ ys
118-
∷-cong refl xs≈ys {y} =
118+
∷-cong P.refl xs≈ys {y} =
119119
y ∈ x ∷ xs ↔⟨ SK-sym $ ∷↔ (y ≡_) ⟩
120120
(y ≡ x ⊎ y ∈ xs) ∼⟨ (y ≡ x ∎) ⊎-cong xs≈ys ⟩
121121
(y ≡ x ⊎ y ∈ ys) ↔⟨ ∷↔ (y ≡_) ⟩
@@ -244,8 +244,8 @@ commutativeMonoid {a} k A = record
244244

245245
empty-unique : {k a} {A : Set a} {xs : List A}
246246
xs ∼[ ⌊ k ⌋→ ] [] xs ≡ []
247-
empty-unique {xs = []} _ = refl
248-
empty-unique {xs = _ ∷ _} ∷∼[] with ⇒→ ∷∼[] (here refl)
247+
empty-unique {xs = []} _ = P.refl
248+
empty-unique {xs = _ ∷ _} ∷∼[] with ⇒→ ∷∼[] (here P.refl)
249249
... | ()
250250

251251
-- _++_ is idempotent (under set equality).
@@ -278,7 +278,7 @@ empty-unique {xs = _ ∷ _} ∷∼[] with ⇒→ ∷∼[] (here refl)
278278
(fs ⊛ (xs₁ ++ xs₂)) ∼[ bag ] (fs ⊛ xs₁) ++ (fs ⊛ xs₂)
279279
⊛-left-distributive {B = B} fs xs₁ xs₂ = begin
280280
fs ⊛ (xs₁ ++ xs₂) ≡⟨⟩
281-
(fs >>= λ f xs₁ ++ xs₂ >>= return ∘ f) ≡⟨ (MP.cong (refl {x = fs}) λ f
281+
(fs >>= λ f xs₁ ++ xs₂ >>= return ∘ f) ≡⟨ (MP.cong (P.refl {x = fs}) λ f
282282
MP.right-distributive xs₁ xs₂ (return ∘ f)) ⟩
283283
(fs >>= λ f (xs₁ >>= return ∘ f) ++
284284
(xs₂ >>= return ∘ f)) ≈⟨ >>=-left-distributive fs ⟩
@@ -297,7 +297,7 @@ private
297297
¬-drop-cons : {a} {A : Set a} {x : A}
298298
¬ ( {xs ys} x ∷ xs ∼[ set ] x ∷ ys xs ∼[ set ] ys)
299299
¬-drop-cons {x = x} drop-cons
300-
with FE.Equivalence.to x∼[] ⟨$⟩ here refl
300+
with FE.Equivalence.to x∼[] ⟨$⟩ here P.refl
301301
where
302302
x,x≈x : (x ∷ x ∷ []) ∼[ set ] [ x ]
303303
x,x≈x = ++-idempotent [ x ]
@@ -336,8 +336,8 @@ drop-cons {A = A} {x} {xs} {ys} x∷xs≈x∷ys =
336336
(z ≡ x ⊎ z ∈ xs) ↔⟨ K-refl ⊎-cong ∈-index xs ⟩
337337
(z ≡ x ⊎ ∃ λ i z ≡ lookup xs i) ↔⟨ SK-sym $ inverse (λ { (zero , p) inj₁ p; (suc i , p) inj₂ (i , p) })
338338
(λ { (inj₁ p) zero , p; (inj₂ (i , p)) suc i , p })
339-
(λ { (zero , _) refl; (suc _ , _) refl })
340-
(λ { (inj₁ _) refl; (inj₂ _) refl }) ⟩
339+
(λ { (zero , _) P.refl; (suc _ , _) P.refl })
340+
(λ { (inj₁ _) P.refl; (inj₂ _) P.refl }) ⟩
341341
(∃ λ i z ≡ lookup (x ∷ xs) i) ∎
342342
where
343343
open Related.EquationalReasoning
@@ -359,7 +359,7 @@ drop-cons {A = A} {x} {xs} {ys} x∷xs≈x∷ys =
359359
Fin-length xs =
360360
(∃ λ z z ∈ xs) ↔⟨ Σ.cong K-refl (∈-index xs) ⟩
361361
(∃ λ z λ i z ≡ lookup xs i) ↔⟨ ∃∃↔∃∃ _ ⟩
362-
(∃ λ i λ z z ≡ lookup xs i) ↔⟨ Σ.cong K-refl (inverse _ (λ _ _ , refl) (λ { (_ , refl) refl }) (λ _ refl)) ⟩
362+
(∃ λ i λ z z ≡ lookup xs i) ↔⟨ Σ.cong K-refl (inverse _ (λ _ _ , P.refl) (λ { (_ , P.refl) P.refl }) (const P.refl)) ⟩
363363
(Fin (length xs) × ⊤) ↔⟨ ×-identityʳ _ _ ⟩
364364
Fin (length xs) ∎
365365
where
@@ -410,8 +410,8 @@ drop-cons {A = A} {x} {xs} {ys} x∷xs≈x∷ys =
410410
(from (Fin-length xs) ⟨$⟩ (to (Fin-length xs) ⟨$⟩ (z , p))))
411411
lemma z p with to (Fin-length xs) ⟨$⟩ (z , p)
412412
| left-inverse-of (Fin-length xs) (z , p)
413-
lemma .(lookup xs i) .(from (∈-index xs) ⟨$⟩ (i , refl)) | i | refl =
414-
refl
413+
lemma .(lookup xs i) .(from (∈-index xs) ⟨$⟩ (i , P.refl)) | i | P.refl =
414+
P.refl
415415

416416
-- Bag equivalence isomorphisms preserve index equality. Note that
417417
-- this means that, even if the underlying equality is proof
@@ -435,7 +435,7 @@ drop-cons {A = A} {x} {xs} {ys} x∷xs≈x∷ys =
435435
-- The old inspect idiom.
436436

437437
inspect : {a} {A : Set a} (x : A) ∃ (x ≡_)
438-
inspect x = x , refl
438+
inspect x = x , P.refl
439439

440440
-- A function is "well-behaved" if any "left" element which is the
441441
-- image of a "right" element is in turn not mapped to another
@@ -548,7 +548,7 @@ drop-cons {A = A} {x} {xs} {ys} x∷xs≈x∷ys =
548548
to-from (∼→⊎↔⊎ inv) {x = inj₁ p} hyp₂ ⟩
549549
index-of {xs = x ∷ xs} (to (∷↔ _) ⟨$⟩ (from (∼→⊎↔⊎ inv) ⟨$⟩ inj₁ q)) ≡⟨ P.cong index-of $
550550
right-inverse-of (∷↔ _) (from inv ⟨$⟩ here q) ⟩
551-
index-of {xs = x ∷ xs} (to (SK-sym inv) ⟨$⟩ here q) ≡⟨ index-equality-preserved (SK-sym inv) refl ⟩
551+
index-of {xs = x ∷ xs} (to (SK-sym inv) ⟨$⟩ here q) ≡⟨ index-equality-preserved (SK-sym inv) P.refl ⟩
552552
index-of {xs = x ∷ xs} (to (SK-sym inv) ⟨$⟩ here p) ≡⟨ P.cong index-of $ P.sym $
553553
right-inverse-of (∷↔ _) (from inv ⟨$⟩ here p) ⟩
554554
index-of {xs = x ∷ xs} (to (∷↔ _) ⟨$⟩ (from (∼→⊎↔⊎ inv) ⟨$⟩ inj₁ p)) ≡⟨ P.cong (index-of ∘ (to (∷↔ (_ ≡_)) ⟨$⟩_)) $
@@ -578,23 +578,23 @@ module _ {a} {A : Set a} where
578578
from xs↭ys = Any-resp-↭ (↭-sym xs↭ys)
579579

580580
from∘to : {xs ys} (p : xs ↭ ys) (q : v ∈ xs) from p (to p q) ≡ q
581-
from∘to refl v∈xs = refl
582-
from∘to (prep _ _) (here refl) = refl
583-
from∘to (prep _ p) (there v∈xs) = P.cong there (from∘to p v∈xs)
584-
from∘to (swap x y p) (here refl) = refl
585-
from∘to (swap x y p) (there (here refl)) = refl
586-
from∘to (swap x y p) (there (there v∈xs)) = P.cong (there ∘ there) (from∘to p v∈xs)
581+
from∘to refl v∈xs = P.refl
582+
from∘to (prep _ _) (here P.refl) = P.refl
583+
from∘to (prep _ p) (there v∈xs) = P.cong there (from∘to p v∈xs)
584+
from∘to (swap x y p) (here P.refl) = P.refl
585+
from∘to (swap x y p) (there (here P.refl)) = P.refl
586+
from∘to (swap x y p) (there (there v∈xs)) = P.cong (there ∘ there) (from∘to p v∈xs)
587587
from∘to (trans p₁ p₂) v∈xs
588588
rewrite from∘to p₂ (Any-resp-↭ p₁ v∈xs)
589-
| from∘to p₁ v∈xs = refl
589+
| from∘to p₁ v∈xs = P.refl
590590

591591
to∘from : {xs ys} (p : xs ↭ ys) (q : v ∈ ys) to p (from p q) ≡ q
592592
to∘from p with from∘to (↭-sym p)
593593
... | res rewrite ↭-sym-involutive p = res
594594

595595
∼bag⇒↭ : _∼[ bag ]_ ⇒ _↭_
596596
∼bag⇒↭ {[]} eq with empty-unique {A = A} (Inv.sym eq)
597-
... | refl = refl
597+
... | P.refl = refl
598598
∼bag⇒↭ {x ∷ xs} eq with ∈-∃++ (to ⟨$⟩ (here P.refl))
599599
where open Inv.Inverse (eq {x})
600600
... | zs₁ , zs₂ , p rewrite p = begin

src/Data/List/Relation/Binary/Permutation/Homogeneous.agda

+29-27
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,50 @@
99
module Data.List.Relation.Binary.Permutation.Homogeneous where
1010

1111
open import Data.List.Base using (List; _∷_)
12-
open import Data.List.Relation.Binary.Pointwise as Pointwise
13-
using (Pointwise)
1412
open import Level using (Level; _⊔_)
1513
open import Relation.Binary
1614

1715
private
1816
variable
19-
a r s : Level
17+
a pr r ps s : Level
2018
A : Set a
2119

22-
data Permutation {A : Set a} (R : Rel A r) : Rel (List A) (a ⊔ r) where
23-
refl : {xs ys} Pointwise R xs ys Permutation R xs ys
24-
prep : {xs ys x y} (eq : R x y) Permutation R xs ys Permutation R (x ∷ xs) (y ∷ ys)
25-
swap : {xs ys x y x′ y′} (eq₁ : R x x′) (eq₂ : R y y′) Permutation R xs ys Permutation R (x ∷ y ∷ xs) (y′ ∷ x′ ∷ ys)
26-
trans : {xs ys zs} Permutation R xs ys Permutation R ys zs Permutation R xs zs
20+
-- PR is morally the pointwise lifting of R to lists but it need not be intensionally
21+
data Permutation {A : Set a} (PR : Rel (List A) pr) (R : Rel A r) : Rel (List A) (a ⊔ pr ⊔ r) where
22+
refl : {xs ys} PR xs ys Permutation PR R xs ys
23+
prep : {xs ys x y} (eq : R x y) Permutation PR R xs ys Permutation PR R (x ∷ xs) (y ∷ ys)
24+
swap : {xs ys x y x′ y′} (eq₁ : R x x′) (eq₂ : R y y′) Permutation PR R xs ys Permutation PR R (x ∷ y ∷ xs) (y′ ∷ x′ ∷ ys)
25+
trans : {xs ys zs} Permutation PR R xs ys Permutation PR R ys zs Permutation PR R xs zs
2726

2827
------------------------------------------------------------------------
2928
-- The Permutation relation is an equivalence
3029

31-
module _ {R : Rel A r} where
30+
module _ {PR : Rel (List A) pr} {R : Rel A r} where
3231

33-
sym : Symmetric R Symmetric (Permutation R)
34-
sym R-sym (refl xs∼ys) = refl (Pointwise.symmetric R-sym xs∼ys)
35-
sym R-sym (prep x∼x′ xs↭ys) = prep (R-sym x∼x′) (sym R-sym xs↭ys)
36-
sym R-sym (swap x∼x′ y∼y′ xs↭ys) = swap (R-sym y∼y′) (R-sym x∼x′) (sym R-sym xs↭ys)
37-
sym R-sym (trans xs↭ys ys↭zs) = trans (sym R-sym ys↭zs) (sym R-sym xs↭ys)
32+
sym : Symmetric PR Symmetric R Symmetric (Permutation PR R)
33+
sym PR-sym R-sym (refl xs∼ys) = refl (PR-sym xs∼ys)
34+
sym PR-sym R-sym (prep x∼x′ xs↭ys) = prep (R-sym x∼x′) (sym PR-sym R-sym xs↭ys)
35+
sym PR-sym R-sym (swap x∼x′ y∼y′ xs↭ys) = swap (R-sym y∼y′) (R-sym x∼x′) (sym PR-sym R-sym xs↭ys)
36+
sym PR-sym R-sym (trans xs↭ys ys↭zs) = trans (sym PR-sym R-sym ys↭zs) (sym PR-sym R-sym xs↭ys)
3837

39-
isEquivalence : Reflexive R Symmetric R IsEquivalence (Permutation R)
40-
isEquivalence R-refl R-sym = record
41-
{ refl = refl (Pointwise.refl R-refl)
42-
; sym = sym R-sym
38+
isEquivalence : Reflexive PR
39+
Symmetric PR Symmetric R
40+
IsEquivalence (Permutation PR R)
41+
isEquivalence PR-refl PR-sym R-sym = record
42+
{ refl = refl PR-refl
43+
; sym = sym PR-sym R-sym
4344
; trans = trans
4445
}
4546

46-
setoid : Reflexive R Symmetric R Setoid _ _
47-
setoid R-refl R-sym = record
48-
{ isEquivalence = isEquivalence R-refl R-sym
47+
setoid : Reflexive PR Symmetric PR Symmetric R Setoid _ _
48+
setoid PR-refl PR-sym R-sym = record
49+
{ isEquivalence = isEquivalence PR-refl PR-sym R-sym
4950
}
5051

51-
map : {R : Rel A r} {S : Rel A s}
52-
(R ⇒ S) (Permutation R ⇒ Permutation S)
53-
map R⇒S (refl xs∼ys) = refl (Pointwise.map R⇒S xs∼ys)
54-
map R⇒S (prep e xs∼ys) = prep (R⇒S e) (map R⇒S xs∼ys)
55-
map R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map R⇒S xs∼ys)
56-
map R⇒S (trans xs∼ys ys∼zs) = trans (map R⇒S xs∼ys) (map R⇒S ys∼zs)
52+
map : {PR : Rel (List A) pr} {PS : Rel (List A) ps}
53+
{R : Rel A r} {S : Rel A s}
54+
(PR ⇒ PS) (R ⇒ S) (Permutation PR R ⇒ Permutation PS S)
55+
map PR⇒PS R⇒S (refl xs∼ys) = refl (PR⇒PS xs∼ys)
56+
map PR⇒PS R⇒S (prep e xs∼ys) = prep (R⇒S e) (map PR⇒PS R⇒S xs∼ys)
57+
map PR⇒PS R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map PR⇒PS R⇒S xs∼ys)
58+
map PR⇒PS R⇒S (trans xs∼ys ys∼zs) = trans (map PR⇒PS R⇒S xs∼ys) (map PR⇒PS R⇒S ys∼zs)

src/Data/List/Relation/Binary/Permutation/Propositional.agda

+15-18
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,38 @@ module Data.List.Relation.Binary.Permutation.Propositional
1010
{a} {A : Set a} where
1111

1212
open import Data.List.Base using (List; []; _∷_)
13+
import Data.List.Relation.Binary.Permutation.Homogeneous as Homogeneous
1314
open import Relation.Binary
14-
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
15+
open import Relation.Binary.PropositionalEquality as Eq using (_≡_)
1516
import Relation.Binary.Reasoning.Setoid as EqReasoning
1617

17-
------------------------------------------------------------------------
18-
-- An inductive definition of permutation
1918

20-
-- Note that one would expect that this would be defined in terms of
21-
-- `Permutation.Setoid`. This is not currently the case as it involves
22-
-- adding in a bunch of trivial `_≡_` proofs to the constructors which
23-
-- a) adds noise and b) prevents easy access to the variables `x`, `y`.
24-
-- This may be changed in future when a better solution is found.
19+
------------------------------------------------------------------------
20+
-- Definition
2521

2622
infix 3 _↭_
2723

28-
data _↭_ : Rel (List A) a where
29-
refl : {xs} xs ↭ xs
30-
prep : {xs ys} x xs ↭ ys x ∷ xs ↭ x ∷ ys
31-
swap : {xs ys} x y xs ↭ ys x ∷ y ∷ xs ↭ y ∷ x ∷ ys
32-
trans : {xs ys zs} xs ↭ ys ys ↭ zs xs ↭ zs
24+
_↭_ : Rel (List A) a
25+
_↭_ = Homogeneous.Permutation _≡_ _≡_
26+
27+
pattern refl = Homogeneous.refl Eq.refl
28+
pattern prep x tl = Homogeneous.prep {x = x} Eq.refl tl
29+
pattern swap x y tl = Homogeneous.swap {x = x} {y = y} Eq.refl Eq.refl tl
30+
31+
open Homogeneous public
32+
using (trans)
3333

3434
------------------------------------------------------------------------
3535
-- _↭_ is an equivalence
3636

3737
↭-reflexive : _≡_ ⇒ _↭_
38-
↭-reflexive refl = refl
38+
↭-reflexive Eq.refl = refl
3939

4040
↭-refl : Reflexive _↭_
4141
↭-refl = refl
4242

4343
↭-sym : {xs ys} xs ↭ ys ys ↭ xs
44-
↭-sym refl = refl
45-
↭-sym (prep x xs↭ys) = prep x (↭-sym xs↭ys)
46-
↭-sym (swap x y xs↭ys) = swap y x (↭-sym xs↭ys)
47-
↭-sym (trans xs↭ys ys↭zs) = trans (↭-sym ys↭zs) (↭-sym xs↭ys)
44+
↭-sym = Homogeneous.sym Eq.sym Eq.sym
4845

4946
-- A smart version of trans that avoids unnecessary `refl`s (see #1113).
5047
↭-trans : Transitive _↭_

0 commit comments

Comments
 (0)