Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A definition for the permutation relation using setoid equality
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Bundles using (Setoid)
10
11 module Data.List.Relation.Binary.Permutation.Setoid
12 {a ℓ} (S : Setoid a ℓ) where
13
14 open import Data.List.Base using (List; _∷_)
15 import Data.List.Relation.Binary.Permutation.Homogeneous as Homogeneous
16 import Data.List.Relation.Binary.Equality.Setoid as ≋
17 open import Function.Base using (_∘′_)
18 open import Level using (_⊔_)
19 open import Relation.Binary.Core using (Rel; _⇒_)
20 open import Relation.Binary.Definitions
21 using (Reflexive; Symmetric; Transitive; LeftTrans; RightTrans)
22 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
23 import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
24 open import Relation.Binary.Reasoning.Syntax
25 open import Relation.Binary.Structures using (IsEquivalence)
26
27 open module ≈ = Setoid S using (_≈_) renaming (Carrier to A)
28 open ≋ S using (_≋_; _∷_; ≋-refl; ≋-sym; ≋-trans)
29
30 ------------------------------------------------------------------------
31 -- Definition, based on `Homogeneous`
32
33 open Homogeneous public
34 using (refl; prep; swap; trans; onIndices)
35
36 infix 3 _↭_
37
38 _↭_ : Rel (List A) (a ⊔ ℓ)
39 _↭_ = Homogeneous.Permutation _≈_
40
41 steps = Homogeneous.steps {R = _≈_}
42
43 ------------------------------------------------------------------------
44 -- Constructor aliases
45
46 -- Constructor alias
47
48 ↭-reflexive-≋ : _≋_ ⇒ _↭_
49 ↭-reflexive-≋ = refl
50
51 ↭-trans : Transitive _↭_
52 ↭-trans = trans
53
54 -- These provide aliases for `swap` and `prep` when the elements being
55 -- swapped or prepended are propositionally equal
56
57 ↭-prep : ∀ x {xs ys} → xs ↭ ys → x ∷ xs ↭ x ∷ ys
58 ↭-prep x xs↭ys = prep ≈.refl xs↭ys
59
60 ↭-swap : ∀ x y {xs ys} → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys
61 ↭-swap x y xs↭ys = swap ≈.refl ≈.refl xs↭ys
62
63 ------------------------------------------------------------------------
64 -- _↭_ is an equivalence
65
66 ↭-reflexive : _≡_ ⇒ _↭_
67 ↭-reflexive refl = ↭-reflexive-≋ ≋-refl
68
69 ↭-refl : Reflexive _↭_
70 ↭-refl = ↭-reflexive refl
71
72 ↭-sym : Symmetric _↭_
73 ↭-sym = Homogeneous.sym ≈.sym
74
75 -- As with the existing proofs `↭-respʳ-≋` and `↭-respˡ-≋` in `Setoid.Properties`
76 -- (which appeal to symmetry of the underlying equality, and its effect on the
77 -- proofs of symmetry for both pointwise equality and permutation) to the effect
78 -- that _↭_ respects _≋_ on the left and right, such arguments can be both
79 -- streamlined, and used to define a 'smart' constructor `↭-trans′` for transitivity.
80 --
81 -- The arguments below show how proofs of permutation can have all transitive
82 -- compositions with instances of `refl` pushed to the leaves of derivations,
83 -- thereby addressing the inefficiencies analysed in issue #1113
84 --
85 -- Conjecture: these transformations are `steps` invariant, but that is moot,
86 -- given their use here, and in `Setoid.Properties.↭-split` (without requiring
87 -- WF-induction on `steps`) to enable a new, sharper, analysis of lists `xs`
88 -- such that `xs ↭ ys ++ [ x ] ++ zs` in terms of `x`, `ys`, and `zs`.
89
90 ↭-transˡ-≋ : LeftTrans _≋_ _↭_
91 ↭-transˡ-≋ xs≋ys (refl ys≋zs)
92 = refl (≋-trans xs≋ys ys≋zs)
93 ↭-transˡ-≋ (x≈y ∷ xs≋ys) (prep y≈z ys↭zs)
94 = prep (≈.trans x≈y y≈z) (↭-transˡ-≋ xs≋ys ys↭zs)
95 ↭-transˡ-≋ (x≈y ∷ w≈z ∷ xs≋ys) (swap eq₁ eq₂ ys↭zs)
96 = swap (≈.trans x≈y eq₁) (≈.trans w≈z eq₂) (↭-transˡ-≋ xs≋ys ys↭zs)
97 ↭-transˡ-≋ xs≋ys (trans ys↭ws ws↭zs)
98 = trans (↭-transˡ-≋ xs≋ys ys↭ws) ws↭zs
99
100 ↭-transʳ-≋ : RightTrans _↭_ _≋_
101 ↭-transʳ-≋ (refl xs≋ys) ys≋zs
102 = refl (≋-trans xs≋ys ys≋zs)
103 ↭-transʳ-≋ (prep x≈y xs↭ys) (y≈z ∷ ys≋zs)
104 = prep (≈.trans x≈y y≈z) (↭-transʳ-≋ xs↭ys ys≋zs)
105 ↭-transʳ-≋ (swap eq₁ eq₂ xs↭ys) (x≈w ∷ y≈z ∷ ys≋zs)
106 = swap (≈.trans eq₁ y≈z) (≈.trans eq₂ x≈w) (↭-transʳ-≋ xs↭ys ys≋zs)
107 ↭-transʳ-≋ (trans xs↭ws ws↭ys) ys≋zs
108 = trans xs↭ws (↭-transʳ-≋ ws↭ys ys≋zs)
109
110 ↭-trans′ : Transitive _↭_
111 ↭-trans′ (refl xs≋ys) ys↭zs = ↭-transˡ-≋ xs≋ys ys↭zs
112 ↭-trans′ xs↭ys (refl ys≋zs) = ↭-transʳ-≋ xs↭ys ys≋zs
113 ↭-trans′ xs↭ys ys↭zs = trans xs↭ys ys↭zs
114
115 ↭-isEquivalence : IsEquivalence _↭_
116 ↭-isEquivalence = record
117 { refl = ↭-refl
118 ; sym = ↭-sym
119 ; trans = ↭-trans
120 }
121
122 ↭-setoid : Setoid _ _
123 ↭-setoid = record { isEquivalence = ↭-isEquivalence }
124
125 ------------------------------------------------------------------------
126 -- A reasoning API to chain permutation proofs
127
128 module PermutationReasoning where
129
130 private module Base = ≈-Reasoning ↭-setoid
131
132 open Base public
133 hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨)
134 renaming (≈-go to ↭-go)
135
136 open ↭-syntax _IsRelatedTo_ _IsRelatedTo_ ↭-go ↭-sym public
137 open ≋-syntax _IsRelatedTo_ _IsRelatedTo_ (↭-go ∘′ ↭-reflexive-≋) ≋-sym public
138
139 -- Some extra combinators that allow us to skip certain elements
140
141 infixr 2 step-swap step-prep
142
143 -- Skip reasoning on the first element
144 step-prep : ∀ x xs {ys zs : List A} → (x ∷ ys) IsRelatedTo zs →
145 xs ↭ ys → (x ∷ xs) IsRelatedTo zs
146 step-prep x xs rel xs↭ys = ↭-go (↭-prep x xs↭ys) rel
147
148 -- Skip reasoning about the first two elements
149 step-swap : ∀ x y xs {ys zs : List A} → (y ∷ x ∷ ys) IsRelatedTo zs →
150 xs ↭ ys → (x ∷ y ∷ xs) IsRelatedTo zs
151 step-swap x y xs rel xs↭ys = ↭-go (↭-swap x y xs↭ys) rel
152
153 syntax step-prep x xs y↭z x↭y = x ∷ xs <⟨ x↭y ⟩ y↭z
154 syntax step-swap x y xs y↭z x↭y = x ∷ y ∷ xs <<⟨ x↭y ⟩ y↭z