Skip to content

Commit e32aee5

Browse files
gallaisjamesmckinna
authored andcommitted
[ fix agda#1354 ] Refactoring Permutation.Propositional
1 parent f443f9d commit e32aee5

File tree

8 files changed

+185
-162
lines changed

8 files changed

+185
-162
lines changed

doc/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

+10-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ open import Data.List.Membership.Propositional.Properties
2323
open import Data.List.Relation.Binary.Subset.Propositional.Properties
2424
using (⊆-preorder)
2525
open import Data.List.Relation.Binary.Permutation.Propositional
26+
using (_↭_; ↭-refl; ↭-sym; module PermutationReasoning)
2627
open import Data.List.Relation.Binary.Permutation.Propositional.Properties
2728
open import Data.Product.Base as Prod hiding (map)
2829
import Data.Product.Function.Dependent.Propositional as Σ
@@ -54,6 +55,7 @@ private
5455
x y : A
5556
ws xs ys zs : List A
5657

58+
5759
------------------------------------------------------------------------
5860
-- Definitions
5961

@@ -142,8 +144,8 @@ module _ {k} {f g : A → B} {xs ys} where
142144
helper y = mk↔ₛ′
143145
(λ x≡fy ≡.trans x≡fy ( f≗g y))
144146
(λ x≡gy ≡.trans x≡gy (≡.sym $ f≗g y))
145-
(λ { ≡.refl ≡.trans-symˡ (f≗g y) })
146-
λ { ≡.refl ≡.trans-symʳ (f≗g y) }
147+
(λ { refl ≡.trans-symˡ (f≗g y) })
148+
λ { refl ≡.trans-symʳ (f≗g y) }
147149

148150
------------------------------------------------------------------------
149151
-- _++_
@@ -282,7 +284,6 @@ empty-unique {xs = _ ∷ _} ∷∼[] with ⇒→ ∷∼[] (here refl)
282284
MP.right-distributive xs₁ xs₂ (pure ∘ f)) ⟩
283285
(fs >>= λ f (xs₁ >>= pure ∘ f) ++
284286
(xs₂ >>= pure ∘ f)) ≈⟨ >>=-left-distributive fs ⟩
285-
286287
(fs >>= λ f xs₁ >>= pure ∘ f) ++
287288
(fs >>= λ f xs₂ >>= pure ∘ f) ≡⟨ ≡.cong₂ _++_ (Applicative.unfold-⊛ fs xs₁) (Applicative.unfold-⊛ fs xs₂) ⟨
288289

@@ -296,7 +297,8 @@ private
296297

297298
¬-drop-cons : {x : A}
298299
¬ ( {xs ys} x ∷ xs ∼[ set ] x ∷ ys xs ∼[ set ] ys)
299-
¬-drop-cons {x = x} drop-cons with Equivalence.to x∼[] (here refl)
300+
¬-drop-cons {x = x} drop-cons
301+
with Equivalence.to x∼[] (here refl)
300302
where
301303
x,x≈x : (x ∷ x ∷ []) ∼[ set ] [ x ]
302304
x,x≈x = ++-idempotent [ x ]
@@ -357,7 +359,7 @@ drop-cons {x = x} {xs} {ys} x∷xs≈x∷ys =
357359
(∃ λ z z ∈ xs) ↔⟨ Σ.cong K-refl (∈-index xs) ⟩
358360
(∃ λ z λ i z ≡ lookup xs i) ↔⟨ ∃∃↔∃∃ _ ⟩
359361
(∃ λ i λ z z ≡ lookup xs i) ↔⟨ Σ.cong K-refl (mk↔ₛ′ _ (λ _ _ , refl) (λ _ refl) (λ { (_ , refl) refl })) ⟩
360-
(Fin (length xs) × ⊤) ↔⟨ ×-identityʳ _ _ ⟩
362+
(Fin (length xs) × ⊤) ↔⟨ ×-identityʳ _ _ ⟩
361363
Fin (length xs) ∎
362364
where
363365
open Related.EquationalReasoning
@@ -571,24 +573,16 @@ drop-cons {x = x} {xs} {ys} x∷xs≈x∷ys =
571573
from xs↭ys = Any-resp-↭ (↭-sym xs↭ys)
572574

573575
from∘to : {xs ys} (p : xs ↭ ys) (q : v ∈ xs) from p (to p q) ≡ q
574-
from∘to refl v∈xs = refl
575-
from∘to (prep _ _) (here refl) = refl
576-
from∘to (prep _ p) (there v∈xs) = ≡.cong there (from∘to p v∈xs)
577-
from∘to (swap x y p) (here refl) = refl
578-
from∘to (swap x y p) (there (here refl)) = refl
579-
from∘to (swap x y p) (there (there v∈xs)) = ≡.cong (there ∘ there) (from∘to p v∈xs)
580-
from∘to (trans p₁ p₂) v∈xs
581-
rewrite from∘to p₂ (Any-resp-↭ p₁ v∈xs)
582-
| from∘to p₁ v∈xs = refl
576+
from∘to = Any-resp-[σ⁻¹∘σ]
583577

584578
to∘from : {xs ys} (p : xs ↭ ys) (q : v ∈ ys) to p (from p q) ≡ q
585579
to∘from p with from∘to (↭-sym p)
586580
... | res rewrite ↭-sym-involutive p = res
587581

588582
∼bag⇒↭ : _∼[ bag ]_ ⇒ _↭_ {A = A}
589583
∼bag⇒↭ {A = A} {[]} eq with empty-unique (↔-sym eq)
590-
... | refl = refl
591-
∼bag⇒↭ {A = A} {x ∷ xs} eq with ∈-∃++ (Inverse.to (eq {x}) (here ≡.refl))
584+
... | refl = ↭-refl
585+
∼bag⇒↭ {A = A} {x ∷ xs} eq with ∈-∃++ (Inverse.to (eq {x}) (here refl))
592586
... | zs₁ , zs₂ , p rewrite p = begin
593587
x ∷ xs <⟨ ∼bag⇒↭ (drop-cons (↔-trans eq (comm zs₁ (x ∷ zs₂)))) ⟩
594588
x ∷ (zs₂ ++ zs₁) <⟨ ++-comm zs₂ zs₁ ⟩

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

+29-29
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
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.Base as Pointwise
13-
using (Pointwise)
14-
open import Data.List.Relation.Binary.Pointwise.Properties as Pointwise
15-
using (symmetric)
1612
open import Level using (Level; _⊔_)
1713
open import Relation.Binary.Core using (Rel; _⇒_)
1814
open import Relation.Binary.Bundles using (Setoid)
@@ -21,41 +17,45 @@ open import Relation.Binary.Definitions using (Reflexive; Symmetric)
2117

2218
private
2319
variable
24-
a r s : Level
20+
a pr r ps s : Level
2521
A : Set a
2622

27-
data Permutation {A : Set a} (R : Rel A r) : Rel (List A) (a ⊔ r) where
28-
refl : {xs ys} Pointwise R xs ys Permutation R xs ys
29-
prep : {xs ys x y} (eq : R x y) Permutation R xs ys Permutation R (x ∷ xs) (y ∷ ys)
30-
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)
31-
trans : {xs ys zs} Permutation R xs ys Permutation R ys zs Permutation R xs zs
23+
-- PR is morally the pointwise lifting of R to lists but it need not be intensionally
24+
data Permutation {A : Set a} (PR : Rel (List A) pr) (R : Rel A r) : Rel (List A) (a ⊔ pr ⊔ r) where
25+
refl : {xs ys} PR xs ys Permutation PR R xs ys
26+
prep : {xs ys x y} (eq : R x y) Permutation PR R xs ys Permutation PR R (x ∷ xs) (y ∷ ys)
27+
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)
28+
trans : {xs ys zs} Permutation PR R xs ys Permutation PR R ys zs Permutation PR R xs zs
3229

3330
------------------------------------------------------------------------
3431
-- The Permutation relation is an equivalence
3532

36-
module _ {R : Rel A r} where
33+
module _ {PR : Rel (List A) pr} {R : Rel A r} where
3734

38-
sym : Symmetric R Symmetric (Permutation R)
39-
sym R-sym (refl xs∼ys) = refl (Pointwise.symmetric R-sym xs∼ys)
40-
sym R-sym (prep x∼x′ xs↭ys) = prep (R-sym x∼x′) (sym R-sym xs↭ys)
41-
sym R-sym (swap x∼x′ y∼y′ xs↭ys) = swap (R-sym y∼y′) (R-sym x∼x′) (sym R-sym xs↭ys)
42-
sym R-sym (trans xs↭ys ys↭zs) = trans (sym R-sym ys↭zs) (sym R-sym xs↭ys)
35+
sym : Symmetric PR Symmetric R Symmetric (Permutation PR R)
36+
sym PR-sym R-sym (refl xs∼ys) = refl (PR-sym xs∼ys)
37+
sym PR-sym R-sym (prep x∼x′ xs↭ys) = prep (R-sym x∼x′) (sym PR-sym R-sym xs↭ys)
38+
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)
39+
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)
4340

44-
isEquivalence : Reflexive R Symmetric R IsEquivalence (Permutation R)
45-
isEquivalence R-refl R-sym = record
46-
{ refl = refl (Pointwise.refl R-refl)
47-
; sym = sym R-sym
41+
isEquivalence : Reflexive PR
42+
Symmetric PR Symmetric R
43+
IsEquivalence (Permutation PR R)
44+
isEquivalence PR-refl PR-sym R-sym = record
45+
{ refl = refl PR-refl
46+
; sym = sym PR-sym R-sym
4847
; trans = trans
4948
}
5049

51-
setoid : Reflexive R Symmetric R Setoid _ _
52-
setoid R-refl R-sym = record
53-
{ isEquivalence = isEquivalence R-refl R-sym
50+
setoid : Reflexive PR Symmetric PR Symmetric R Setoid _ _
51+
setoid PR-refl PR-sym R-sym = record
52+
{ isEquivalence = isEquivalence PR-refl PR-sym R-sym
5453
}
5554

56-
map : {R : Rel A r} {S : Rel A s}
57-
(R ⇒ S) (Permutation R ⇒ Permutation S)
58-
map R⇒S (refl xs∼ys) = refl (Pointwise.map R⇒S xs∼ys)
59-
map R⇒S (prep e xs∼ys) = prep (R⇒S e) (map R⇒S xs∼ys)
60-
map R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map R⇒S xs∼ys)
61-
map R⇒S (trans xs∼ys ys∼zs) = trans (map R⇒S xs∼ys) (map R⇒S ys∼zs)
55+
map : {PR : Rel (List A) pr} {PS : Rel (List A) ps}
56+
{R : Rel A r} {S : Rel A s}
57+
(PR ⇒ PS) (R ⇒ S) (Permutation PR R ⇒ Permutation PS S)
58+
map PR⇒PS R⇒S (refl xs∼ys) = refl (PR⇒PS xs∼ys)
59+
map PR⇒PS R⇒S (prep e xs∼ys) = prep (R⇒S e) (map PR⇒PS R⇒S xs∼ys)
60+
map PR⇒PS R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map PR⇒PS R⇒S xs∼ys)
61+
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

+19-22
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,38 @@ open import Relation.Binary.Core using (Rel; _⇒_)
1414
open import Relation.Binary.Bundles using (Setoid)
1515
open import Relation.Binary.Structures using (IsEquivalence)
1616
open import Relation.Binary.Definitions using (Reflexive; Transitive)
17-
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
18-
import Relation.Binary.Reasoning.Setoid as EqReasoning
17+
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
18+
import Data.List.Relation.Binary.Permutation.Homogeneous as Homogeneous
19+
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
1920
open import Relation.Binary.Reasoning.Syntax
2021

21-
------------------------------------------------------------------------
22-
-- An inductive definition of permutation
2322

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

3026
infix 3 _↭_
3127

32-
data _↭_ : Rel (List A) a where
33-
refl : {xs} xs ↭ xs
34-
prep : {xs ys} x xs ↭ ys x ∷ xs ↭ x ∷ ys
35-
swap : {xs ys} x y xs ↭ ys x ∷ y ∷ xs ↭ y ∷ x ∷ ys
36-
trans : {xs ys zs} xs ↭ ys ys ↭ zs xs ↭ zs
28+
_↭_ : Rel (List A) a
29+
_↭_ = Homogeneous.Permutation _≡_ _≡_
30+
31+
pattern refl = Homogeneous.refl ≡.refl
32+
pattern prep {xs} {ys} x tl = Homogeneous.prep {xs = xs} {ys = ys} {x = x} ≡.refl tl
33+
pattern swap {xs} {ys} x y tl = Homogeneous.swap {xs = xs} {ys = ys} {x = x} {y = y} ≡.refl ≡.refl tl
34+
35+
open Homogeneous public
36+
using (trans)
3737

3838
------------------------------------------------------------------------
3939
-- _↭_ is an equivalence
4040

4141
↭-reflexive : _≡_ ⇒ _↭_
42-
↭-reflexive refl = refl
42+
↭-reflexive ≡.refl = refl
4343

4444
↭-refl : Reflexive _↭_
4545
↭-refl = refl
4646

4747
↭-sym : {xs ys} xs ↭ ys ys ↭ xs
48-
↭-sym refl = refl
49-
↭-sym (prep x xs↭ys) = prep x (↭-sym xs↭ys)
50-
↭-sym (swap x y xs↭ys) = swap y x (↭-sym xs↭ys)
51-
↭-sym (trans xs↭ys ys↭zs) = trans (↭-sym ys↭zs) (↭-sym xs↭ys)
48+
↭-sym = Homogeneous.sym ≡.sym ≡.sym
5249

5350
-- A smart version of trans that avoids unnecessary `refl`s (see #1113).
5451
↭-trans : Transitive _↭_
@@ -74,7 +71,7 @@ data _↭_ : Rel (List A) a where
7471

7572
module PermutationReasoning where
7673

77-
private module Base = EqReasoning ↭-setoid
74+
private module Base = ≈-Reasoning ↭-setoid
7875

7976
open Base public
8077
hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨)
@@ -89,12 +86,12 @@ module PermutationReasoning where
8986
-- Skip reasoning on the first element
9087
step-prep : x xs {ys zs : List A} (x ∷ ys) IsRelatedTo zs
9188
xs ↭ ys (x ∷ xs) IsRelatedTo zs
92-
step-prep x xs rel xs↭ys = relTo (trans (prep x xs↭ys) (begin rel))
89+
step-prep x xs rel xs↭ys = ↭-go (prep x xs↭ys) rel
9390

9491
-- Skip reasoning about the first two elements
9592
step-swap : x y xs {ys zs : List A} (y ∷ x ∷ ys) IsRelatedTo zs
9693
xs ↭ ys (x ∷ y ∷ xs) IsRelatedTo zs
97-
step-swap x y xs rel xs↭ys = relTo (trans (swap x y xs↭ys) (begin rel))
94+
step-swap x y xs rel xs↭ys = ↭-go (swap x y xs↭ys) rel
9895

9996
syntax step-prep x xs y↭z x↭y = x ∷ xs <⟨ x↭y ⟩ y↭z
10097
syntax step-swap x y xs y↭z x↭y = x ∷ y ∷ xs <<⟨ x↭y ⟩ y↭z

0 commit comments

Comments
 (0)