| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of permutations using setoid equality |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Core |
| 10 |
using (Rel; _⇒_; _Preserves_⟶_; _Preserves₂_⟶_⟶_) |
| 11 |
open import Relation.Binary.Bundles using (Setoid) |
| 12 |
open import Relation.Binary.Definitions as B hiding (Decidable) |
| 13 |
|
| 14 |
module Data.List.Relation.Binary.Permutation.Setoid.Properties |
| 15 |
{a ℓ} (S : Setoid a ℓ) |
| 16 |
where |
| 17 |
|
| 18 |
open import Algebra |
| 19 |
open import Data.Bool.Base using (true; false) |
| 20 |
open import Data.Fin.Base using (zero; suc) |
| 21 |
open import Data.List.Base as List hiding (head; tail) |
| 22 |
open import Data.List.Relation.Binary.Pointwise as Pointwise |
| 23 |
using (Pointwise; head; tail) |
| 24 |
import Data.List.Relation.Binary.Equality.Setoid as Equality |
| 25 |
import Data.List.Relation.Binary.Permutation.Setoid as Permutation |
| 26 |
open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 27 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 28 |
open import Data.List.Relation.Unary.AllPairs using (AllPairs; []; _∷_) |
| 29 |
import Data.List.Relation.Unary.Unique.Setoid as Unique |
| 30 |
import Data.List.Membership.Setoid as Membership |
| 31 |
open import Data.List.Membership.Setoid.Properties using (∈-∃++; ∈-insert) |
| 32 |
import Data.List.Properties as List |
| 33 |
open import Data.Nat.Base using (ℕ; suc; 2+; _<_; z<s; _+_) |
| 34 |
open import Data.Nat.Induction |
| 35 |
open import Data.Nat.Properties |
| 36 |
open import Data.Product.Base using (_,_; _×_; ∃; ∃₂; proj₁; proj₂) |
| 37 |
open import Function.Base using (_∘_; _⟨_⟩_; flip) |
| 38 |
open import Function.Bundles using (Inverse) |
| 39 |
open import Level using (Level; _⊔_) |
| 40 |
open import Relation.Unary using (Pred; Decidable) |
| 41 |
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning |
| 42 |
open import Relation.Binary.Properties.Setoid S using (≉-resp₂) |
| 43 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 44 |
using (_≡_ ; refl; sym; cong; cong₂; subst; _≢_) |
| 45 |
open import Relation.Nullary.Decidable using (yes; no; does) |
| 46 |
open import Relation.Nullary.Negation using (¬_; contradiction; contraposition) |
| 47 |
|
| 48 |
|
| 49 |
open Setoid S using (_≈_) |
| 50 |
renaming (Carrier to A; refl to ≈-refl; sym to ≈-sym; trans to ≈-trans) |
| 51 |
open Permutation S |
| 52 |
open Membership S |
| 53 |
open Unique S using (Unique) |
| 54 |
open module ≋ = Equality S |
| 55 |
using (_≋_; []; _∷_ |
| 56 |
; ≋-refl; ≋-sym; ≋-trans |
| 57 |
; All-resp-≋; Any-resp-≋; AllPairs-resp-≋) |
| 58 |
|
| 59 |
private |
| 60 |
variable |
| 61 |
b p r : Level |
| 62 |
x y z v w : A |
| 63 |
xs ys zs vs ws : List A |
| 64 |
P : Pred A p |
| 65 |
R : Rel A r |
| 66 |
|
| 67 |
|
| 68 |
------------------------------------------------------------------------ |
| 69 |
-- Relationships to other predicates |
| 70 |
------------------------------------------------------------------------ |
| 71 |
|
| 72 |
All-resp-↭ : P Respects _≈_ → (All P) Respects _↭_ |
| 73 |
All-resp-↭ resp (refl xs≋ys) pxs = All-resp-≋ resp xs≋ys pxs |
| 74 |
All-resp-↭ resp (prep x≈y p) (px ∷ pxs) = resp x≈y px ∷ All-resp-↭ resp p pxs |
| 75 |
All-resp-↭ resp (swap ≈₁ ≈₂ p) (px ∷ py ∷ pxs) = resp ≈₂ py ∷ resp ≈₁ px ∷ All-resp-↭ resp p pxs |
| 76 |
All-resp-↭ resp (trans p₁ p₂) pxs = All-resp-↭ resp p₂ (All-resp-↭ resp p₁ pxs) |
| 77 |
|
| 78 |
Any-resp-↭ : P Respects _≈_ → (Any P) Respects _↭_ |
| 79 |
Any-resp-↭ resp (refl xs≋ys) pxs = Any-resp-≋ resp xs≋ys pxs |
| 80 |
Any-resp-↭ resp (prep x≈y p) (here px) = here (resp x≈y px) |
| 81 |
Any-resp-↭ resp (prep x≈y p) (there pxs) = there (Any-resp-↭ resp p pxs) |
| 82 |
Any-resp-↭ resp (swap x y p) (here px) = there (here (resp x px)) |
| 83 |
Any-resp-↭ resp (swap x y p) (there (here px)) = here (resp y px) |
| 84 |
Any-resp-↭ resp (swap x y p) (there (there pxs)) = there (there (Any-resp-↭ resp p pxs)) |
| 85 |
Any-resp-↭ resp (trans p₁ p₂) pxs = Any-resp-↭ resp p₂ (Any-resp-↭ resp p₁ pxs) |
| 86 |
|
| 87 |
AllPairs-resp-↭ : Symmetric R → R Respects₂ _≈_ → (AllPairs R) Respects _↭_ |
| 88 |
AllPairs-resp-↭ sym resp (refl xs≋ys) pxs = AllPairs-resp-≋ resp xs≋ys pxs |
| 89 |
AllPairs-resp-↭ sym resp (prep x≈y p) (∼ ∷ pxs) = |
| 90 |
All-resp-↭ (proj₁ resp) p (All.map (proj₂ resp x≈y) ∼) ∷ |
| 91 |
AllPairs-resp-↭ sym resp p pxs |
| 92 |
AllPairs-resp-↭ sym resp@(rʳ , rˡ) (swap eq₁ eq₂ p) ((∼₁ ∷ ∼₂) ∷ ∼₃ ∷ pxs) = |
| 93 |
(sym (rʳ eq₂ (rˡ eq₁ ∼₁)) ∷ All-resp-↭ rʳ p (All.map (rˡ eq₂) ∼₃)) ∷ |
| 94 |
All-resp-↭ rʳ p (All.map (rˡ eq₁) ∼₂) ∷ |
| 95 |
AllPairs-resp-↭ sym resp p pxs |
| 96 |
AllPairs-resp-↭ sym resp (trans p₁ p₂) pxs = |
| 97 |
AllPairs-resp-↭ sym resp p₂ (AllPairs-resp-↭ sym resp p₁ pxs) |
| 98 |
|
| 99 |
∈-resp-↭ : (x ∈_) Respects _↭_ |
| 100 |
∈-resp-↭ = Any-resp-↭ (flip ≈-trans) |
| 101 |
|
| 102 |
Unique-resp-↭ : Unique Respects _↭_ |
| 103 |
Unique-resp-↭ = AllPairs-resp-↭ (_∘ ≈-sym) ≉-resp₂ |
| 104 |
|
| 105 |
------------------------------------------------------------------------ |
| 106 |
-- Length |
| 107 |
|
| 108 |
xs↭ys⇒|xs|≡|ys| : ∀ {xs ys} → xs ↭ ys → length xs ≡ length ys |
| 109 |
xs↭ys⇒|xs|≡|ys| (refl eq) = Pointwise.Pointwise-length eq |
| 110 |
xs↭ys⇒|xs|≡|ys| (prep eq xs↭ys) = ≡.cong suc (xs↭ys⇒|xs|≡|ys| xs↭ys) |
| 111 |
xs↭ys⇒|xs|≡|ys| (swap eq₁ eq₂ xs↭ys) = ≡.cong 2+ (xs↭ys⇒|xs|≡|ys| xs↭ys) |
| 112 |
xs↭ys⇒|xs|≡|ys| (trans xs↭ys xs↭ys₁) = ≡.trans (xs↭ys⇒|xs|≡|ys| xs↭ys) (xs↭ys⇒|xs|≡|ys| xs↭ys₁) |
| 113 |
|
| 114 |
¬x∷xs↭[] : ∀ {x xs} → ¬ (x ∷ xs ↭ []) |
| 115 |
¬x∷xs↭[] = contraposition xs↭ys⇒|xs|≡|ys| λ() |
| 116 |
|
| 117 |
------------------------------------------------------------------------ |
| 118 |
-- Core properties depending on the representation of _↭_ |
| 119 |
------------------------------------------------------------------------ |
| 120 |
|
| 121 |
shift : v ≈ w → ∀ xs ys → xs ++ [ v ] ++ ys ↭ w ∷ xs ++ ys |
| 122 |
shift {v} {w} v≈w [] ys = prep v≈w ↭-refl |
| 123 |
shift {v} {w} v≈w (x ∷ xs) ys = begin |
| 124 |
x ∷ (xs ++ [ v ] ++ ys) ↭⟨ ↭-prep x (shift v≈w xs ys) ⟩ |
| 125 |
x ∷ w ∷ xs ++ ys ↭⟨ ↭-swap x w ↭-refl ⟩ |
| 126 |
w ∷ x ∷ xs ++ ys ∎ |
| 127 |
where open PermutationReasoning |
| 128 |
|
| 129 |
------------------------------------------------------------------------ |
| 130 |
-- Relationship to `_≋_` |
| 131 |
------------------------------------------------------------------------ |
| 132 |
|
| 133 |
↭-split : ∀ v as bs → xs ↭ (as ++ [ v ] ++ bs) → |
| 134 |
∃₂ λ ps qs → xs ≋ (ps ++ [ v ] ++ qs) |
| 135 |
× (ps ++ qs) ↭ (as ++ bs) |
| 136 |
↭-split v as bs p = helper as bs p ≋-refl |
| 137 |
where |
| 138 |
helper : ∀ as bs → xs ↭ ys → ys ≋ (as ++ [ v ] ++ bs) → |
| 139 |
∃₂ λ ps qs → xs ≋ (ps ++ [ v ] ++ qs) |
| 140 |
× (ps ++ qs) ↭ (as ++ bs) |
| 141 |
helper as bs (trans xs↭ys ys↭zs) zs≋as++[v]++ys |
| 142 |
with ps , qs , eq , ↭ ← helper as bs ys↭zs zs≋as++[v]++ys |
| 143 |
with ps′ , qs′ , eq′ , ↭′ ← helper ps qs xs↭ys eq |
| 144 |
= ps′ , qs′ , eq′ , ↭-trans′ ↭′ ↭ |
| 145 |
helper [] _ (refl (x≈v ∷ xs≋vs)) (v≈y ∷ vs≋ys) |
| 146 |
= [] , _ , ≈-trans x≈v v≈y ∷ ≋-refl , refl (≋-trans xs≋vs vs≋ys) |
| 147 |
helper (a ∷ as) bs (refl (x≈v ∷ xs≋vs)) (v≈y ∷ vs≋ys) |
| 148 |
= _ ∷ as , bs , ≈-trans x≈v v≈y ∷ ≋-trans xs≋vs vs≋ys , ↭-refl |
| 149 |
helper [] bs (prep {xs = xs} x≈v xs↭vs) (v≈y ∷ vs≋ys) |
| 150 |
= [] , xs , ≈-trans x≈v v≈y ∷ ≋-refl , ↭-transʳ-≋ xs↭vs vs≋ys |
| 151 |
helper (a ∷ as) bs (prep x≈v as↭vs) (v≈y ∷ vs≋ys) |
| 152 |
with ps , qs , eq , ↭ ← helper as bs as↭vs vs≋ys |
| 153 |
= a ∷ ps , qs , ≈-trans x≈v v≈y ∷ eq , prep ≈-refl ↭ |
| 154 |
helper [] [] (swap _ _ _) (_ ∷ ()) |
| 155 |
helper [] (b ∷ _) (swap x≈v y≈w xs↭vs) (w≈z ∷ v≈y ∷ vs≋ys) |
| 156 |
= b ∷ [] , _ , ≈-trans x≈v v≈y ∷ ≈-trans y≈w w≈z ∷ ≋-refl |
| 157 |
, ↭-prep b (↭-transʳ-≋ xs↭vs vs≋ys) |
| 158 |
helper (a ∷ []) bs (swap x≈v y≈w xs↭vs) (w≈z ∷ v≈y ∷ vs≋ys) |
| 159 |
= [] , a ∷ _ , ≈-trans x≈v v≈y ∷ ≈-trans y≈w w≈z ∷ ≋-refl |
| 160 |
, ↭-prep a (↭-transʳ-≋ xs↭vs vs≋ys) |
| 161 |
helper (a ∷ b ∷ as) bs (swap x≈v y≈w as↭vs) (w≈a ∷ v≈b ∷ vs≋ys) |
| 162 |
with ps , qs , eq , ↭ ← helper as bs as↭vs vs≋ys |
| 163 |
= b ∷ a ∷ ps , qs , ≈-trans x≈v v≈b ∷ ≈-trans y≈w w≈a ∷ eq |
| 164 |
, ↭-swap _ _ ↭ |
| 165 |
|
| 166 |
------------------------------------------------------------------------ |
| 167 |
-- Core properties of lists depending on the representation of _↭_ |
| 168 |
------------------------------------------------------------------------ |
| 169 |
|
| 170 |
------------------------------------------------------------------------ |
| 171 |
-- map |
| 172 |
|
| 173 |
module _ (T : Setoid b r) where |
| 174 |
|
| 175 |
open Setoid T using () renaming (_≈_ to _≈′_) |
| 176 |
open Permutation T using () renaming (_↭_ to _↭′_) |
| 177 |
|
| 178 |
map⁺ : ∀ {f} → f Preserves _≈_ ⟶ _≈′_ → (map f) Preserves _↭_ ⟶ _↭′_ |
| 179 |
map⁺ pres (refl xs≋ys) = refl (Pointwise.map⁺ _ _ (Pointwise.map pres xs≋ys)) |
| 180 |
map⁺ pres (prep x p) = prep (pres x) (map⁺ pres p) |
| 181 |
map⁺ pres (swap x y p) = swap (pres x) (pres y) (map⁺ pres p) |
| 182 |
map⁺ pres (trans p₁ p₂) = trans (map⁺ pres p₁) (map⁺ pres p₂) |
| 183 |
|
| 184 |
------------------------------------------------------------------------ |
| 185 |
-- filter |
| 186 |
|
| 187 |
module _ (P? : Decidable P) (P≈ : P Respects _≈_) where |
| 188 |
|
| 189 |
filter⁺ : ∀ {xs ys : List A} → xs ↭ ys → filter P? xs ↭ filter P? ys |
| 190 |
filter⁺ (refl xs≋ys) = refl (≋.filter⁺ P? P≈ xs≋ys) |
| 191 |
filter⁺ (trans xs↭zs zs↭ys) = trans (filter⁺ xs↭zs) (filter⁺ zs↭ys) |
| 192 |
filter⁺ {x ∷ xs} {y ∷ ys} (prep x≈y xs↭ys) with P? x | P? y |
| 193 |
... | yes _ | yes _ = prep x≈y (filter⁺ xs↭ys) |
| 194 |
... | yes Px | no ¬Py = contradiction (P≈ x≈y Px) ¬Py |
| 195 |
... | no ¬Px | yes Py = contradiction (P≈ (≈-sym x≈y) Py) ¬Px |
| 196 |
... | no _ | no _ = filter⁺ xs↭ys |
| 197 |
filter⁺ {x ∷ w ∷ xs} {y ∷ z ∷ ys} (swap x≈z w≈y xs↭ys) with P? x | P? y |
| 198 |
filter⁺ {x ∷ w ∷ xs} {y ∷ z ∷ ys} (swap x≈z w≈y xs↭ys) | no ¬Px | no ¬Py |
| 199 |
with P? z | P? w |
| 200 |
... | _ | yes Pw = contradiction (P≈ w≈y Pw) ¬Py |
| 201 |
... | yes Pz | _ = contradiction (P≈ (≈-sym x≈z) Pz) ¬Px |
| 202 |
... | no _ | no _ = filter⁺ xs↭ys |
| 203 |
filter⁺ {x ∷ w ∷ xs} {y ∷ z ∷ ys} (swap x≈z w≈y xs↭ys) | no ¬Px | yes Py |
| 204 |
with P? z | P? w |
| 205 |
... | _ | no ¬Pw = contradiction (P≈ (≈-sym w≈y) Py) ¬Pw |
| 206 |
... | yes Pz | _ = contradiction (P≈ (≈-sym x≈z) Pz) ¬Px |
| 207 |
... | no _ | yes _ = prep w≈y (filter⁺ xs↭ys) |
| 208 |
filter⁺ {x ∷ w ∷ xs} {y ∷ z ∷ ys} (swap x≈z w≈y xs↭ys) | yes Px | no ¬Py |
| 209 |
with P? z | P? w |
| 210 |
... | no ¬Pz | _ = contradiction (P≈ x≈z Px) ¬Pz |
| 211 |
... | _ | yes Pw = contradiction (P≈ w≈y Pw) ¬Py |
| 212 |
... | yes _ | no _ = prep x≈z (filter⁺ xs↭ys) |
| 213 |
filter⁺ {x ∷ w ∷ xs} {y ∷ z ∷ ys} (swap x≈z w≈y xs↭ys) | yes Px | yes Py |
| 214 |
with P? z | P? w |
| 215 |
... | no ¬Pz | _ = contradiction (P≈ x≈z Px) ¬Pz |
| 216 |
... | _ | no ¬Pw = contradiction (P≈ (≈-sym w≈y) Py) ¬Pw |
| 217 |
... | yes _ | yes _ = swap x≈z w≈y (filter⁺ xs↭ys) |
| 218 |
|
| 219 |
------------------------------------------------------------------------ |
| 220 |
-- _++_ |
| 221 |
|
| 222 |
++⁺ʳ : ∀ zs → xs ↭ ys → xs ++ zs ↭ ys ++ zs |
| 223 |
++⁺ʳ zs (refl xs≋ys) = refl (Pointwise.++⁺ xs≋ys ≋-refl) |
| 224 |
++⁺ʳ zs (prep x ↭) = prep x (++⁺ʳ zs ↭) |
| 225 |
++⁺ʳ zs (swap x y ↭) = swap x y (++⁺ʳ zs ↭) |
| 226 |
++⁺ʳ zs (trans ↭₁ ↭₂) = trans (++⁺ʳ zs ↭₁) (++⁺ʳ zs ↭₂) |
| 227 |
|
| 228 |
------------------------------------------------------------------------ |
| 229 |
-- dropMiddleElement-≋ |
| 230 |
|
| 231 |
dropMiddleElement-≋ : ∀ {x} ws xs {ys} {zs} → |
| 232 |
ws ++ [ x ] ++ ys ≋ xs ++ [ x ] ++ zs → |
| 233 |
ws ++ ys ↭ xs ++ zs |
| 234 |
dropMiddleElement-≋ [] [] (_ ∷ eq) = ↭-reflexive-≋ eq |
| 235 |
dropMiddleElement-≋ [] (x ∷ xs) (w≈v ∷ eq) = ↭-transˡ-≋ eq (shift w≈v xs _) |
| 236 |
dropMiddleElement-≋ (w ∷ ws) [] (w≈x ∷ eq) = ↭-transʳ-≋ (↭-sym (shift (≈-sym w≈x) ws _)) eq |
| 237 |
dropMiddleElement-≋ (w ∷ ws) (x ∷ xs) (w≈x ∷ eq) = prep w≈x (dropMiddleElement-≋ ws xs eq) |
| 238 |
|
| 239 |
------------------------------------------------------------------------ |
| 240 |
-- Properties depending on the core properties of _↭_ |
| 241 |
------------------------------------------------------------------------ |
| 242 |
|
| 243 |
↭-shift : ∀ {v} xs ys → xs ++ [ v ] ++ ys ↭ v ∷ xs ++ ys |
| 244 |
↭-shift = shift ≈-refl |
| 245 |
|
| 246 |
++⁺ˡ : ∀ xs {ys zs} → ys ↭ zs → xs ++ ys ↭ xs ++ zs |
| 247 |
++⁺ˡ [] ys↭zs = ys↭zs |
| 248 |
++⁺ˡ (x ∷ xs) ys↭zs = ↭-prep _ (++⁺ˡ xs ys↭zs) |
| 249 |
|
| 250 |
++⁺ : _++_ Preserves₂ _↭_ ⟶ _↭_ ⟶ _↭_ |
| 251 |
++⁺ ws↭xs ys↭zs = ↭-trans (++⁺ʳ _ ws↭xs) (++⁺ˡ _ ys↭zs) |
| 252 |
|
| 253 |
-- Some other useful lemmas |
| 254 |
|
| 255 |
zoom : ∀ h {t xs ys} → xs ↭ ys → h ++ xs ++ t ↭ h ++ ys ++ t |
| 256 |
zoom h {t} = ++⁺ˡ h ∘ ++⁺ʳ t |
| 257 |
|
| 258 |
inject : ∀ v {ws xs ys zs} → ws ↭ ys → xs ↭ zs → |
| 259 |
ws ++ [ v ] ++ xs ↭ ys ++ [ v ] ++ zs |
| 260 |
inject v ws↭ys xs↭zs = ↭-trans (++⁺ˡ _ (↭-prep _ xs↭zs)) (++⁺ʳ _ ws↭ys) |
| 261 |
|
| 262 |
-- Algebraic properties |
| 263 |
|
| 264 |
++-identityˡ : LeftIdentity _↭_ [] _++_ |
| 265 |
++-identityˡ xs = ↭-refl |
| 266 |
|
| 267 |
++-identityʳ : RightIdentity _↭_ [] _++_ |
| 268 |
++-identityʳ xs = ↭-reflexive (List.++-identityʳ xs) |
| 269 |
|
| 270 |
++-identity : Identity _↭_ [] _++_ |
| 271 |
++-identity = ++-identityˡ , ++-identityʳ |
| 272 |
|
| 273 |
++-assoc : Associative _↭_ _++_ |
| 274 |
++-assoc xs ys zs = ↭-reflexive (List.++-assoc xs ys zs) |
| 275 |
|
| 276 |
++-comm : Commutative _↭_ _++_ |
| 277 |
++-comm [] ys = ↭-sym (++-identityʳ ys) |
| 278 |
++-comm (x ∷ xs) ys = begin |
| 279 |
x ∷ xs ++ ys ↭⟨ ↭-prep x (++-comm xs ys) ⟩ |
| 280 |
x ∷ ys ++ xs ↭⟨ ↭-shift ys xs ⟨ |
| 281 |
ys ++ (x ∷ xs) ∎ |
| 282 |
where open PermutationReasoning |
| 283 |
|
| 284 |
-- Corollary |
| 285 |
|
| 286 |
shifts : ∀ xs ys {zs} → xs ++ ys ++ zs ↭ ys ++ xs ++ zs |
| 287 |
shifts xs ys {zs} = begin |
| 288 |
xs ++ ys ++ zs ↭⟨ ++-assoc xs ys zs ⟨ |
| 289 |
(xs ++ ys) ++ zs ↭⟨ ++⁺ʳ zs (++-comm xs ys) ⟩ |
| 290 |
(ys ++ xs) ++ zs ↭⟨ ++-assoc ys xs zs ⟩ |
| 291 |
ys ++ xs ++ zs ∎ |
| 292 |
where open PermutationReasoning |
| 293 |
|
| 294 |
-- Structures |
| 295 |
|
| 296 |
++-isMagma : IsMagma _↭_ _++_ |
| 297 |
++-isMagma = record |
| 298 |
{ isEquivalence = ↭-isEquivalence |
| 299 |
; ∙-cong = ++⁺ |
| 300 |
} |
| 301 |
|
| 302 |
++-isSemigroup : IsSemigroup _↭_ _++_ |
| 303 |
++-isSemigroup = record |
| 304 |
{ isMagma = ++-isMagma |
| 305 |
; assoc = ++-assoc |
| 306 |
} |
| 307 |
|
| 308 |
++-isMonoid : IsMonoid _↭_ _++_ [] |
| 309 |
++-isMonoid = record |
| 310 |
{ isSemigroup = ++-isSemigroup |
| 311 |
; identity = ++-identity |
| 312 |
} |
| 313 |
|
| 314 |
++-isCommutativeMonoid : IsCommutativeMonoid _↭_ _++_ [] |
| 315 |
++-isCommutativeMonoid = record |
| 316 |
{ isMonoid = ++-isMonoid |
| 317 |
; comm = ++-comm |
| 318 |
} |
| 319 |
|
| 320 |
-- Bundles |
| 321 |
|
| 322 |
++-magma : Magma a (a ⊔ ℓ) |
| 323 |
++-magma = record |
| 324 |
{ isMagma = ++-isMagma |
| 325 |
} |
| 326 |
|
| 327 |
++-semigroup : Semigroup a (a ⊔ ℓ) |
| 328 |
++-semigroup = record |
| 329 |
{ isSemigroup = ++-isSemigroup |
| 330 |
} |
| 331 |
|
| 332 |
++-monoid : Monoid a (a ⊔ ℓ) |
| 333 |
++-monoid = record |
| 334 |
{ isMonoid = ++-isMonoid |
| 335 |
} |
| 336 |
|
| 337 |
++-commutativeMonoid : CommutativeMonoid a (a ⊔ ℓ) |
| 338 |
++-commutativeMonoid = record |
| 339 |
{ isCommutativeMonoid = ++-isCommutativeMonoid |
| 340 |
} |
| 341 |
|
| 342 |
------------------------------------------------------------------------ |
| 343 |
-- dropMiddleElement, dropMiddle, and inversion for _∷_ |
| 344 |
|
| 345 |
dropMiddleElement : ∀ {v} ws xs {ys zs} → |
| 346 |
ws ++ [ v ] ++ ys ↭ xs ++ [ v ] ++ zs → |
| 347 |
ws ++ ys ↭ xs ++ zs |
| 348 |
dropMiddleElement {v} ws xs {ys} {zs} p = |
| 349 |
let ps , qs , eq , ↭ = ↭-split v xs zs p |
| 350 |
in ↭-trans (dropMiddleElement-≋ ws ps eq) ↭ |
| 351 |
|
| 352 |
dropMiddle : ∀ {vs} ws xs {ys zs} → |
| 353 |
ws ++ vs ++ ys ↭ xs ++ vs ++ zs → |
| 354 |
ws ++ ys ↭ xs ++ zs |
| 355 |
dropMiddle {[]} ws xs p = p |
| 356 |
dropMiddle {v ∷ vs} ws xs p = dropMiddle ws xs (dropMiddleElement ws xs p) |
| 357 |
|
| 358 |
drop-∷ : x ∷ xs ↭ x ∷ ys → xs ↭ ys |
| 359 |
drop-∷ = dropMiddleElement [] [] |
| 360 |
|
| 361 |
------------------------------------------------------------------------ |
| 362 |
-- _∷ʳ_ |
| 363 |
|
| 364 |
∷↭∷ʳ : ∀ (x : A) xs → x ∷ xs ↭ xs ∷ʳ x |
| 365 |
∷↭∷ʳ x xs = ↭-sym (begin |
| 366 |
xs ++ [ x ] ↭⟨ ↭-shift xs [] ⟩ |
| 367 |
x ∷ xs ++ [] ≡⟨ List.++-identityʳ _ ⟩ |
| 368 |
x ∷ xs ∎) |
| 369 |
where open PermutationReasoning |
| 370 |
|
| 371 |
------------------------------------------------------------------------ |
| 372 |
-- reverse |
| 373 |
|
| 374 |
↭-reverse : (xs : List A) → reverse xs ↭ xs |
| 375 |
↭-reverse [] = ↭-refl |
| 376 |
↭-reverse (x ∷ xs) = begin |
| 377 |
reverse (x ∷ xs) ≡⟨ List.unfold-reverse x xs ⟩ |
| 378 |
reverse xs ∷ʳ x ↭⟨ ∷↭∷ʳ x (reverse xs) ⟨ |
| 379 |
x ∷ reverse xs ↭⟨ ↭-prep x (↭-reverse xs) ⟩ |
| 380 |
x ∷ xs ∎ |
| 381 |
where open PermutationReasoning |
| 382 |
|
| 383 |
------------------------------------------------------------------------ |
| 384 |
-- ʳ++ |
| 385 |
|
| 386 |
++↭ʳ++ : ∀ (xs ys : List A) → xs ++ ys ↭ xs ʳ++ ys |
| 387 |
++↭ʳ++ [] ys = ↭-refl |
| 388 |
++↭ʳ++ (x ∷ xs) ys = ↭-trans (↭-sym (↭-shift xs ys)) (++↭ʳ++ xs (x ∷ ys)) |
| 389 |
|
| 390 |
------------------------------------------------------------------------ |
| 391 |
-- partition |
| 392 |
|
| 393 |
module _ (P? : Decidable P) where |
| 394 |
|
| 395 |
partition-↭ : ∀ xs → (let ys , zs = partition P? xs) → xs ↭ ys ++ zs |
| 396 |
partition-↭ [] = ↭-refl |
| 397 |
partition-↭ (x ∷ xs) with does (P? x) |
| 398 |
... | true = ↭-prep x (partition-↭ xs) |
| 399 |
... | false = ↭-trans (↭-prep x (partition-↭ xs)) (↭-sym (↭-shift _ _)) |
| 400 |
where open PermutationReasoning |
| 401 |
|
| 402 |
------------------------------------------------------------------------ |
| 403 |
-- merge |
| 404 |
|
| 405 |
module _ (R? : B.Decidable R) where |
| 406 |
|
| 407 |
merge-↭ : ∀ xs ys → merge R? xs ys ↭ xs ++ ys |
| 408 |
merge-↭ [] [] = ↭-refl |
| 409 |
merge-↭ [] (y ∷ ys) = ↭-refl |
| 410 |
merge-↭ (x ∷ xs) [] = ↭-sym (++-identityʳ (x ∷ xs)) |
| 411 |
merge-↭ (x ∷ xs) (y ∷ ys) |
| 412 |
with does (R? x y) | merge-↭ xs (y ∷ ys) | merge-↭ (x ∷ xs) ys |
| 413 |
... | true | rec | _ = ↭-prep x rec |
| 414 |
... | false | _ | rec = begin |
| 415 |
y ∷ merge R? (x ∷ xs) ys ↭⟨ ↭-prep _ rec ⟩ |
| 416 |
y ∷ x ∷ xs ++ ys ↭⟨ ↭-shift (x ∷ xs) ys ⟨ |
| 417 |
(x ∷ xs) ++ y ∷ ys ≡⟨ List.++-assoc [ x ] xs (y ∷ ys) ⟨ |
| 418 |
x ∷ xs ++ y ∷ ys ∎ |
| 419 |
where open PermutationReasoning |
| 420 |
|
| 421 |
------------------------------------------------------------------------ |
| 422 |
-- foldr over a Commutative Monoid |
| 423 |
|
| 424 |
module _{_∙_ : Op₂ A} {ε : A} |
| 425 |
(isCommutativeMonoid : IsCommutativeMonoid _≈_ _∙_ ε) where |
| 426 |
|
| 427 |
private |
| 428 |
commutativeMonoid : CommutativeMonoid _ _ |
| 429 |
commutativeMonoid = record { isCommutativeMonoid = isCommutativeMonoid } |
| 430 |
open module CM = CommutativeMonoid commutativeMonoid |
| 431 |
using (∙-cong; ∙-congˡ; ∙-congʳ; assoc; comm) |
| 432 |
|
| 433 |
foldr-commMonoid : (foldr _∙_ ε) Preserves _↭_ ⟶ _≈_ |
| 434 |
foldr-commMonoid (refl xs≋ys) = Pointwise.foldr⁺ ∙-cong CM.refl xs≋ys |
| 435 |
foldr-commMonoid (prep x≈y xs↭ys) = ∙-cong x≈y (foldr-commMonoid xs↭ys) |
| 436 |
foldr-commMonoid (swap {xs} {ys} {x} {y} {x′} {y′} x≈x′ y≈y′ xs↭ys) = begin |
| 437 |
x ∙ (y ∙ foldr _∙_ ε xs) ≈⟨ ∙-congˡ (∙-congˡ (foldr-commMonoid xs↭ys)) ⟩ |
| 438 |
x ∙ (y ∙ foldr _∙_ ε ys) ≈⟨ assoc x y (foldr _∙_ ε ys) ⟨ |
| 439 |
(x ∙ y) ∙ foldr _∙_ ε ys ≈⟨ ∙-congʳ (comm x y) ⟩ |
| 440 |
(y ∙ x) ∙ foldr _∙_ ε ys ≈⟨ ∙-congʳ (∙-cong y≈y′ x≈x′) ⟩ |
| 441 |
(y′ ∙ x′) ∙ foldr _∙_ ε ys ≈⟨ assoc y′ x′ (foldr _∙_ ε ys) ⟩ |
| 442 |
y′ ∙ (x′ ∙ foldr _∙_ ε ys) ∎ |
| 443 |
where open ≈-Reasoning CM.setoid |
| 444 |
foldr-commMonoid (trans xs↭ys ys↭zs) = CM.trans (foldr-commMonoid xs↭ys) (foldr-commMonoid ys↭zs) |
| 445 |
|
| 446 |
onIndices-lookup : ∀ (xs↭ys : xs ↭ ys) → |
| 447 |
∀ i → lookup xs i ≈ lookup ys (Inverse.to (onIndices xs↭ys) i) |
| 448 |
onIndices-lookup (refl xs≋ys) i = Pointwise.lookup-cast xs≋ys _ i |
| 449 |
onIndices-lookup (prep eq xs↭ys) zero = eq |
| 450 |
onIndices-lookup (prep _ xs↭ys) (suc i) = onIndices-lookup xs↭ys i |
| 451 |
onIndices-lookup (swap eq _ xs↭ys) zero = eq |
| 452 |
onIndices-lookup (swap _ eq xs↭ys) (suc zero) = eq |
| 453 |
onIndices-lookup (swap _ _ xs↭ys) (suc (suc i)) = onIndices-lookup xs↭ys i |
| 454 |
onIndices-lookup (trans xs↭ys ys↭zs) i = ≈-trans (onIndices-lookup xs↭ys i) (onIndices-lookup ys↭zs _) |
| 455 |
|
| 456 |
------------------------------------------------------------------------ |
| 457 |
-- TOWARDS DEPRECATION |
| 458 |
------------------------------------------------------------------------ |
| 459 |
|
| 460 |
≋⇒↭ = ↭-reflexive-≋ |
| 461 |
|
| 462 |
-- These are easily superseded by ↭-transˡ-≋, ↭-transʳ-≋ |
| 463 |
-- But for the properties of steps which require precise measurement |
| 464 |
|
| 465 |
↭-respʳ-≋ : _↭_ Respectsʳ _≋_ |
| 466 |
↭-respʳ-≋ xs≋ys (refl zs≋xs) = refl (≋-trans zs≋xs xs≋ys) |
| 467 |
↭-respʳ-≋ (x≈y ∷ xs≋ys) (prep eq zs↭xs) = prep (≈-trans eq x≈y) (↭-respʳ-≋ xs≋ys zs↭xs) |
| 468 |
↭-respʳ-≋ (x≈y ∷ w≈z ∷ xs≋ys) (swap eq₁ eq₂ zs↭xs) = swap (≈-trans eq₁ w≈z) (≈-trans eq₂ x≈y) (↭-respʳ-≋ xs≋ys zs↭xs) |
| 469 |
↭-respʳ-≋ xs≋ys (trans ws↭zs zs↭xs) = trans ws↭zs (↭-respʳ-≋ xs≋ys zs↭xs) |
| 470 |
|
| 471 |
↭-respˡ-≋ : _↭_ Respectsˡ _≋_ |
| 472 |
↭-respˡ-≋ xs≋ys (refl ys≋zs) = refl (≋-trans (≋-sym xs≋ys) ys≋zs) |
| 473 |
↭-respˡ-≋ (x≈y ∷ xs≋ys) (prep eq zs↭xs) = prep (≈-trans (≈-sym x≈y) eq) (↭-respˡ-≋ xs≋ys zs↭xs) |
| 474 |
↭-respˡ-≋ (x≈y ∷ w≈z ∷ xs≋ys) (swap eq₁ eq₂ zs↭xs) = swap (≈-trans (≈-sym x≈y) eq₁) (≈-trans (≈-sym w≈z) eq₂) (↭-respˡ-≋ xs≋ys zs↭xs) |
| 475 |
↭-respˡ-≋ xs≋ys (trans ws↭zs zs↭xs) = trans (↭-respˡ-≋ xs≋ys ws↭zs) zs↭xs |
| 476 |
|
| 477 |
-- Properties of steps using the above |
| 478 |
|
| 479 |
0<steps : (xs↭ys : xs ↭ ys) → 0 < steps xs↭ys |
| 480 |
0<steps (refl _) = z<s |
| 481 |
0<steps (prep eq xs↭ys) = m<n⇒m<1+n (0<steps xs↭ys) |
| 482 |
0<steps (swap eq₁ eq₂ xs↭ys) = m<n⇒m<1+n (0<steps xs↭ys) |
| 483 |
0<steps (trans xs↭ys xs↭ys₁) = |
| 484 |
<-≤-trans (0<steps xs↭ys) (m≤m+n (steps xs↭ys) (steps xs↭ys₁)) |
| 485 |
|
| 486 |
steps-respˡ : (ys≋xs : ys ≋ xs) (ys↭zs : ys ↭ zs) → |
| 487 |
steps (↭-respˡ-≋ ys≋xs ys↭zs) ≡ steps ys↭zs |
| 488 |
steps-respˡ _ (refl _) = refl |
| 489 |
steps-respˡ (_ ∷ ys≋xs) (prep _ ys↭zs) = cong suc (steps-respˡ ys≋xs ys↭zs) |
| 490 |
steps-respˡ (_ ∷ _ ∷ ys≋xs) (swap _ _ ys↭zs) = cong suc (steps-respˡ ys≋xs ys↭zs) |
| 491 |
steps-respˡ ys≋xs (trans ys↭ws ws↭zs) = cong (_+ steps ws↭zs) (steps-respˡ ys≋xs ys↭ws) |
| 492 |
|
| 493 |
steps-respʳ : (xs≋ys : xs ≋ ys) (zs↭xs : zs ↭ xs) → |
| 494 |
steps (↭-respʳ-≋ xs≋ys zs↭xs) ≡ steps zs↭xs |
| 495 |
steps-respʳ _ (refl _) = refl |
| 496 |
steps-respʳ (_ ∷ ys≋xs) (prep _ ys↭zs) = cong suc (steps-respʳ ys≋xs ys↭zs) |
| 497 |
steps-respʳ (_ ∷ _ ∷ ys≋xs) (swap _ _ ys↭zs) = cong suc (steps-respʳ ys≋xs ys↭zs) |
| 498 |
steps-respʳ ys≋xs (trans ys↭ws ws↭zs) = cong (steps ys↭ws +_) (steps-respʳ ys≋xs ws↭zs) |
| 499 |
|
| 500 |
------------------------------------------------------------------------ |
| 501 |
-- DEPRECATED NAMES |
| 502 |
------------------------------------------------------------------------ |
| 503 |
-- Please use the new names as continuing support for the old names is |
| 504 |
-- not guaranteed. |
| 505 |
|
| 506 |
-- Version 2.1 |
| 507 |
|
| 508 |
split : ∀ (v : A) as bs {xs} → xs ↭ as ++ [ v ] ++ bs → |
| 509 |
∃₂ λ ps qs → xs ≋ ps ++ [ v ] ++ qs |
| 510 |
split v as bs xs↭as++[v]++bs |
| 511 |
with ps , qs , eq , _ ← ↭-split v as bs xs↭as++[v]++bs |
| 512 |
= ps , qs , eq |
| 513 |
{-# WARNING_ON_USAGE split |
| 514 |
"Warning: split was deprecated in v2.1. |
| 515 |
Please use the sharper lemma ↭-split instead." |
| 516 |
#-} |