| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of permutation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Permutation.Propositional.Properties where |
| 10 |
|
| 11 |
open import Algebra.Bundles |
| 12 |
open import Algebra.Definitions |
| 13 |
open import Algebra.Structures |
| 14 |
open import Data.Bool.Base using (Bool; true; false) |
| 15 |
open import Data.Nat.Base using (ℕ; suc) |
| 16 |
import Data.Nat.Properties as ℕ |
| 17 |
open import Data.Product.Base using (-,_) |
| 18 |
open import Data.List.Base as List hiding (sum; product) |
| 19 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 20 |
open import Data.List.Relation.Unary.Any using (Any; here; there) |
| 21 |
open import Data.List.Relation.Unary.All using (All; []; _∷_) |
| 22 |
open import Data.List.Membership.Propositional |
| 23 |
open import Data.List.Membership.Propositional.Properties |
| 24 |
import Data.List.Properties as List |
| 25 |
open import Data.Product.Base using (_,_; _×_; ∃; ∃₂) |
| 26 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 27 |
open import Function.Base using (_∘_; _⟨_⟩_; _$_) |
| 28 |
open import Level using (Level) |
| 29 |
open import Relation.Unary as Pred using (Pred) |
| 30 |
open import Relation.Binary.Core using (Rel; _Preserves_⟶_; _Preserves₂_⟶_⟶_) |
| 31 |
open import Relation.Binary.Definitions using (_Respects_; Decidable) |
| 32 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 33 |
using (_≡_ ; refl ; cong; cong₂; _≢_) |
| 34 |
open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning) |
| 35 |
open import Relation.Nullary |
| 36 |
|
| 37 |
import Data.List.Relation.Binary.Permutation.Setoid.Properties as Permutation |
| 38 |
|
| 39 |
private |
| 40 |
variable |
| 41 |
a b p : Level |
| 42 |
A : Set a |
| 43 |
B : Set b |
| 44 |
xs ys : List A |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Permutations of empty and singleton lists |
| 48 |
|
| 49 |
↭-empty-inv : ∀ {xs : List A} → xs ↭ [] → xs ≡ [] |
| 50 |
↭-empty-inv refl = refl |
| 51 |
↭-empty-inv (trans p q) with refl ← ↭-empty-inv q = ↭-empty-inv p |
| 52 |
|
| 53 |
¬x∷xs↭[] : ∀ {x} {xs : List A} → ¬ ((x ∷ xs) ↭ []) |
| 54 |
¬x∷xs↭[] (trans s₁ s₂) with ↭-empty-inv s₂ |
| 55 |
... | refl = ¬x∷xs↭[] s₁ |
| 56 |
|
| 57 |
↭-singleton-inv : ∀ {x} {xs : List A} → xs ↭ [ x ] → xs ≡ [ x ] |
| 58 |
↭-singleton-inv refl = refl |
| 59 |
↭-singleton-inv (prep _ ρ) with refl ← ↭-empty-inv ρ = refl |
| 60 |
↭-singleton-inv (trans ρ₁ ρ₂) with refl ← ↭-singleton-inv ρ₂ = ↭-singleton-inv ρ₁ |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
-- sym |
| 64 |
|
| 65 |
↭-sym-involutive : ∀ {xs ys : List A} (p : xs ↭ ys) → ↭-sym (↭-sym p) ≡ p |
| 66 |
↭-sym-involutive refl = refl |
| 67 |
↭-sym-involutive (prep x ↭) = cong (prep x) (↭-sym-involutive ↭) |
| 68 |
↭-sym-involutive (swap x y ↭) = cong (swap x y) (↭-sym-involutive ↭) |
| 69 |
↭-sym-involutive (trans ↭₁ ↭₂) = |
| 70 |
cong₂ trans (↭-sym-involutive ↭₁) (↭-sym-involutive ↭₂) |
| 71 |
|
| 72 |
------------------------------------------------------------------------ |
| 73 |
-- Relationships to other predicates |
| 74 |
|
| 75 |
module _ {P : Pred A p} where |
| 76 |
|
| 77 |
All-resp-↭ : (All P) Respects _↭_ |
| 78 |
All-resp-↭ refl wit = wit |
| 79 |
All-resp-↭ (prep x p) (px ∷ wit) = px ∷ All-resp-↭ p wit |
| 80 |
All-resp-↭ (swap x y p) (px ∷ py ∷ wit) = py ∷ px ∷ All-resp-↭ p wit |
| 81 |
All-resp-↭ (trans p₁ p₂) wit = All-resp-↭ p₂ (All-resp-↭ p₁ wit) |
| 82 |
|
| 83 |
Any-resp-↭ : (Any P) Respects _↭_ |
| 84 |
Any-resp-↭ refl wit = wit |
| 85 |
Any-resp-↭ (prep x p) (here px) = here px |
| 86 |
Any-resp-↭ (prep x p) (there wit) = there (Any-resp-↭ p wit) |
| 87 |
Any-resp-↭ (swap x y p) (here px) = there (here px) |
| 88 |
Any-resp-↭ (swap x y p) (there (here px)) = here px |
| 89 |
Any-resp-↭ (swap x y p) (there (there wit)) = there (there (Any-resp-↭ p wit)) |
| 90 |
Any-resp-↭ (trans p p₁) wit = Any-resp-↭ p₁ (Any-resp-↭ p wit) |
| 91 |
|
| 92 |
Any-resp-[σ⁻¹∘σ] : ∀ {xs ys} (σ : xs ↭ ys) (ix : Any P xs) → |
| 93 |
Any-resp-↭ (trans σ (↭-sym σ)) ix ≡ ix |
| 94 |
Any-resp-[σ⁻¹∘σ] refl ix = refl |
| 95 |
Any-resp-[σ⁻¹∘σ] (prep _ _) (here _) = refl |
| 96 |
Any-resp-[σ⁻¹∘σ] (swap _ _ _) (here _) = refl |
| 97 |
Any-resp-[σ⁻¹∘σ] (swap _ _ _) (there (here _)) = refl |
| 98 |
Any-resp-[σ⁻¹∘σ] (trans σ₁ σ₂) ix |
| 99 |
rewrite Any-resp-[σ⁻¹∘σ] σ₂ (Any-resp-↭ σ₁ ix) |
| 100 |
rewrite Any-resp-[σ⁻¹∘σ] σ₁ ix |
| 101 |
= refl |
| 102 |
Any-resp-[σ⁻¹∘σ] (prep _ σ) (there ix) |
| 103 |
rewrite Any-resp-[σ⁻¹∘σ] σ ix |
| 104 |
= refl |
| 105 |
Any-resp-[σ⁻¹∘σ] (swap _ _ σ) (there (there ix)) |
| 106 |
rewrite Any-resp-[σ⁻¹∘σ] σ ix |
| 107 |
= refl |
| 108 |
|
| 109 |
Any-resp-[σ∘σ⁻¹] : ∀ {xs ys} (σ : xs ↭ ys) (iy : Any P ys) → |
| 110 |
Any-resp-↭ (trans (↭-sym σ) σ) iy ≡ iy |
| 111 |
Any-resp-[σ∘σ⁻¹] p |
| 112 |
with res ← Any-resp-[σ⁻¹∘σ] (↭-sym p) |
| 113 |
rewrite ↭-sym-involutive p |
| 114 |
= res |
| 115 |
|
| 116 |
∈-resp-↭ : ∀ {x : A} → (x ∈_) Respects _↭_ |
| 117 |
∈-resp-↭ = Any-resp-↭ |
| 118 |
|
| 119 |
∈-resp-[σ⁻¹∘σ] : ∀ {xs ys} {x : A} (σ : xs ↭ ys) (ix : x ∈ xs) → |
| 120 |
∈-resp-↭ (trans σ (↭-sym σ)) ix ≡ ix |
| 121 |
∈-resp-[σ⁻¹∘σ] = Any-resp-[σ⁻¹∘σ] |
| 122 |
|
| 123 |
∈-resp-[σ∘σ⁻¹] : ∀ {xs ys} {y : A} (σ : xs ↭ ys) (iy : y ∈ ys) → |
| 124 |
∈-resp-↭ (trans (↭-sym σ) σ) iy ≡ iy |
| 125 |
∈-resp-[σ∘σ⁻¹] = Any-resp-[σ∘σ⁻¹] |
| 126 |
|
| 127 |
------------------------------------------------------------------------ |
| 128 |
-- map |
| 129 |
|
| 130 |
module _ (f : A → B) where |
| 131 |
|
| 132 |
map⁺ : ∀ {xs ys} → xs ↭ ys → map f xs ↭ map f ys |
| 133 |
map⁺ refl = refl |
| 134 |
map⁺ (prep x p) = prep _ (map⁺ p) |
| 135 |
map⁺ (swap x y p) = swap _ _ (map⁺ p) |
| 136 |
map⁺ (trans p₁ p₂) = trans (map⁺ p₁) (map⁺ p₂) |
| 137 |
|
| 138 |
-- permutations preserve 'being a mapped list' |
| 139 |
↭-map-inv : ∀ {xs ys} → map f xs ↭ ys → ∃ λ ys′ → ys ≡ map f ys′ × xs ↭ ys′ |
| 140 |
↭-map-inv {[]} ρ = -, ↭-empty-inv (↭-sym ρ) , ↭-refl |
| 141 |
↭-map-inv {x ∷ []} ρ = -, ↭-singleton-inv (↭-sym ρ) , ↭-refl |
| 142 |
↭-map-inv {_ ∷ _ ∷ _} refl = -, refl , ↭-refl |
| 143 |
↭-map-inv {_ ∷ _ ∷ _} (prep _ ρ) with _ , refl , ρ′ ← ↭-map-inv ρ = -, refl , prep _ ρ′ |
| 144 |
↭-map-inv {_ ∷ _ ∷ _} (swap _ _ ρ) with _ , refl , ρ′ ← ↭-map-inv ρ = -, refl , swap _ _ ρ′ |
| 145 |
↭-map-inv {_ ∷ _ ∷ _} (trans ρ₁ ρ₂) with _ , refl , ρ₃ ← ↭-map-inv ρ₁ |
| 146 |
with _ , refl , ρ₄ ← ↭-map-inv ρ₂ = -, refl , trans ρ₃ ρ₄ |
| 147 |
|
| 148 |
------------------------------------------------------------------------ |
| 149 |
-- length |
| 150 |
|
| 151 |
↭-length : ∀ {xs ys : List A} → xs ↭ ys → length xs ≡ length ys |
| 152 |
↭-length refl = refl |
| 153 |
↭-length (prep x lr) = cong suc (↭-length lr) |
| 154 |
↭-length (swap x y lr) = cong (suc ∘ suc) (↭-length lr) |
| 155 |
↭-length (trans lr₁ lr₂) = ≡.trans (↭-length lr₁) (↭-length lr₂) |
| 156 |
|
| 157 |
------------------------------------------------------------------------ |
| 158 |
-- _++_ |
| 159 |
|
| 160 |
++⁺ˡ : ∀ xs {ys zs : List A} → ys ↭ zs → xs ++ ys ↭ xs ++ zs |
| 161 |
++⁺ˡ [] ys↭zs = ys↭zs |
| 162 |
++⁺ˡ (x ∷ xs) ys↭zs = prep x (++⁺ˡ xs ys↭zs) |
| 163 |
|
| 164 |
++⁺ʳ : ∀ {xs ys : List A} zs → xs ↭ ys → xs ++ zs ↭ ys ++ zs |
| 165 |
++⁺ʳ zs refl = refl |
| 166 |
++⁺ʳ zs (prep x ↭) = prep x (++⁺ʳ zs ↭) |
| 167 |
++⁺ʳ zs (swap x y ↭) = swap x y (++⁺ʳ zs ↭) |
| 168 |
++⁺ʳ zs (trans ↭₁ ↭₂) = trans (++⁺ʳ zs ↭₁) (++⁺ʳ zs ↭₂) |
| 169 |
|
| 170 |
++⁺ : _++_ {A = A} Preserves₂ _↭_ ⟶ _↭_ ⟶ _↭_ |
| 171 |
++⁺ ws↭xs ys↭zs = trans (++⁺ʳ _ ws↭xs) (++⁺ˡ _ ys↭zs) |
| 172 |
|
| 173 |
-- Some useful lemmas |
| 174 |
|
| 175 |
zoom : ∀ h {t xs ys : List A} → xs ↭ ys → h ++ xs ++ t ↭ h ++ ys ++ t |
| 176 |
zoom h {t} = ++⁺ˡ h ∘ ++⁺ʳ t |
| 177 |
|
| 178 |
inject : ∀ (v : A) {ws xs ys zs} → ws ↭ ys → xs ↭ zs → |
| 179 |
ws ++ [ v ] ++ xs ↭ ys ++ [ v ] ++ zs |
| 180 |
inject v ws↭ys xs↭zs = trans (++⁺ˡ _ (prep v xs↭zs)) (++⁺ʳ _ ws↭ys) |
| 181 |
|
| 182 |
shift : ∀ v (xs ys : List A) → xs ++ [ v ] ++ ys ↭ v ∷ xs ++ ys |
| 183 |
shift v [] ys = refl |
| 184 |
shift v (x ∷ xs) ys = begin |
| 185 |
x ∷ (xs ++ [ v ] ++ ys) ↭⟨ prep _ (shift v xs ys) ⟩ |
| 186 |
x ∷ v ∷ xs ++ ys ↭⟨ swap _ _ refl ⟩ |
| 187 |
v ∷ x ∷ xs ++ ys ∎ |
| 188 |
where open PermutationReasoning |
| 189 |
|
| 190 |
drop-mid-≡ : ∀ {x : A} ws xs {ys} {zs} → |
| 191 |
ws ++ [ x ] ++ ys ≡ xs ++ [ x ] ++ zs → |
| 192 |
ws ++ ys ↭ xs ++ zs |
| 193 |
drop-mid-≡ [] [] eq |
| 194 |
with refl ← List.∷-injectiveʳ eq = refl |
| 195 |
drop-mid-≡ [] (x ∷ xs) refl = shift _ xs _ |
| 196 |
drop-mid-≡ (w ∷ ws) [] refl = ↭-sym (shift _ ws _) |
| 197 |
drop-mid-≡ (w ∷ ws) (x ∷ xs) eq |
| 198 |
with refl , eq′ ← List.∷-injective eq = prep w (drop-mid-≡ ws xs eq′) |
| 199 |
|
| 200 |
drop-mid : ∀ {x : A} ws xs {ys zs} → |
| 201 |
ws ++ [ x ] ++ ys ↭ xs ++ [ x ] ++ zs → |
| 202 |
ws ++ ys ↭ xs ++ zs |
| 203 |
drop-mid ws xs p = drop-mid′ p ws xs refl refl |
| 204 |
where |
| 205 |
drop-mid′ : ∀ {as bs} → as ↭ bs → |
| 206 |
∀ {x : A} ws xs {ys zs} → |
| 207 |
as ≡ ws ++ [ x ] ++ ys → |
| 208 |
bs ≡ xs ++ [ x ] ++ zs → |
| 209 |
ws ++ ys ↭ xs ++ zs |
| 210 |
drop-mid′ refl ws xs refl eq = drop-mid-≡ ws xs eq |
| 211 |
drop-mid′ (trans p q) ws xs refl refl |
| 212 |
with h , t , refl ← ∈-∃++ (∈-resp-↭ p (∈-insert ws)) |
| 213 |
= trans (drop-mid ws h p) (drop-mid h xs q) |
| 214 |
drop-mid′ (prep x p) [] [] refl eq |
| 215 |
with refl ← List.∷-injectiveʳ eq = p |
| 216 |
drop-mid′ (prep x p) [] (x ∷ xs) refl refl = trans p (shift _ _ _) |
| 217 |
drop-mid′ (prep x p) (w ∷ ws) [] refl refl = trans (↭-sym (shift _ _ _)) p |
| 218 |
drop-mid′ (prep x p) (w ∷ ws) (x ∷ xs) refl refl = prep _ (drop-mid ws xs p) |
| 219 |
drop-mid′ (swap y z p) [] [] refl refl = prep _ p |
| 220 |
drop-mid′ (swap y z p) [] (x ∷ []) refl eq |
| 221 |
with refl , eq′ ← List.∷-injective eq |
| 222 |
with refl ← List.∷-injectiveʳ eq′ = prep _ p |
| 223 |
drop-mid′ (swap y z p) [] (x ∷ _ ∷ xs) refl refl = prep _ (trans p (shift _ _ _)) |
| 224 |
drop-mid′ (swap y z p) (w ∷ []) [] refl eq |
| 225 |
with refl ← List.∷-injectiveʳ eq = prep _ p |
| 226 |
drop-mid′ (swap y z p) (w ∷ x ∷ ws) [] refl refl = prep _ (trans (↭-sym (shift _ _ _)) p) |
| 227 |
drop-mid′ (swap y y p) (y ∷ []) (y ∷ []) refl refl = prep _ p |
| 228 |
drop-mid′ (swap y z p) (y ∷ []) (z ∷ y ∷ xs) refl refl = begin |
| 229 |
_ ∷ _ ↭⟨ prep _ p ⟩ |
| 230 |
_ ∷ (xs ++ _ ∷ _) ↭⟨ prep _ (shift _ _ _) ⟩ |
| 231 |
_ ∷ _ ∷ xs ++ _ ↭⟨ swap _ _ refl ⟩ |
| 232 |
_ ∷ _ ∷ xs ++ _ ∎ |
| 233 |
where open PermutationReasoning |
| 234 |
drop-mid′ (swap y z p) (y ∷ z ∷ ws) (z ∷ []) refl refl = begin |
| 235 |
_ ∷ _ ∷ ws ++ _ ↭⟨ swap _ _ refl ⟩ |
| 236 |
_ ∷ (_ ∷ ws ++ _) ↭⟨ prep _ (shift _ _ _) ⟨ |
| 237 |
_ ∷ (ws ++ _ ∷ _) ↭⟨ prep _ p ⟩ |
| 238 |
_ ∷ _ ∎ |
| 239 |
where open PermutationReasoning |
| 240 |
drop-mid′ (swap y z p) (y ∷ z ∷ ws) (z ∷ y ∷ xs) refl refl = swap y z (drop-mid _ _ p) |
| 241 |
|
| 242 |
|
| 243 |
-- Algebraic properties |
| 244 |
|
| 245 |
++-identityˡ : LeftIdentity {A = List A} _↭_ [] _++_ |
| 246 |
++-identityˡ xs = refl |
| 247 |
|
| 248 |
++-identityʳ : RightIdentity {A = List A} _↭_ [] _++_ |
| 249 |
++-identityʳ xs = ↭-reflexive (List.++-identityʳ xs) |
| 250 |
|
| 251 |
++-identity : Identity {A = List A} _↭_ [] _++_ |
| 252 |
++-identity = ++-identityˡ , ++-identityʳ |
| 253 |
|
| 254 |
++-assoc : Associative {A = List A} _↭_ _++_ |
| 255 |
++-assoc xs ys zs = ↭-reflexive (List.++-assoc xs ys zs) |
| 256 |
|
| 257 |
++-comm : Commutative {A = List A} _↭_ _++_ |
| 258 |
++-comm [] ys = ↭-sym (++-identityʳ ys) |
| 259 |
++-comm (x ∷ xs) ys = begin |
| 260 |
x ∷ xs ++ ys ↭⟨ prep _ (++-comm xs ys) ⟩ |
| 261 |
x ∷ ys ++ xs ↭⟨ shift x ys xs ⟨ |
| 262 |
ys ++ (x ∷ xs) ∎ |
| 263 |
where open PermutationReasoning |
| 264 |
|
| 265 |
++-isMagma : IsMagma {A = List A} _↭_ _++_ |
| 266 |
++-isMagma = record |
| 267 |
{ isEquivalence = ↭-isEquivalence |
| 268 |
; ∙-cong = ++⁺ |
| 269 |
} |
| 270 |
|
| 271 |
++-isSemigroup : IsSemigroup {A = List A} _↭_ _++_ |
| 272 |
++-isSemigroup = record |
| 273 |
{ isMagma = ++-isMagma |
| 274 |
; assoc = ++-assoc |
| 275 |
} |
| 276 |
|
| 277 |
++-isMonoid : IsMonoid {A = List A} _↭_ _++_ [] |
| 278 |
++-isMonoid = record |
| 279 |
{ isSemigroup = ++-isSemigroup |
| 280 |
; identity = ++-identity |
| 281 |
} |
| 282 |
|
| 283 |
++-isCommutativeMonoid : IsCommutativeMonoid {A = List A} _↭_ _++_ [] |
| 284 |
++-isCommutativeMonoid = record |
| 285 |
{ isMonoid = ++-isMonoid |
| 286 |
; comm = ++-comm |
| 287 |
} |
| 288 |
|
| 289 |
module _ {a} {A : Set a} where |
| 290 |
|
| 291 |
++-magma : Magma _ _ |
| 292 |
++-magma = record |
| 293 |
{ isMagma = ++-isMagma {A = A} |
| 294 |
} |
| 295 |
|
| 296 |
++-semigroup : Semigroup a _ |
| 297 |
++-semigroup = record |
| 298 |
{ isSemigroup = ++-isSemigroup {A = A} |
| 299 |
} |
| 300 |
|
| 301 |
++-monoid : Monoid a _ |
| 302 |
++-monoid = record |
| 303 |
{ isMonoid = ++-isMonoid {A = A} |
| 304 |
} |
| 305 |
|
| 306 |
++-commutativeMonoid : CommutativeMonoid _ _ |
| 307 |
++-commutativeMonoid = record |
| 308 |
{ isCommutativeMonoid = ++-isCommutativeMonoid {A = A} |
| 309 |
} |
| 310 |
|
| 311 |
-- Another useful lemma |
| 312 |
|
| 313 |
shifts : ∀ xs ys {zs : List A} → xs ++ ys ++ zs ↭ ys ++ xs ++ zs |
| 314 |
shifts xs ys {zs} = begin |
| 315 |
xs ++ ys ++ zs ↭⟨ ++-assoc xs ys zs ⟨ |
| 316 |
(xs ++ ys) ++ zs ↭⟨ ++⁺ʳ zs (++-comm xs ys) ⟩ |
| 317 |
(ys ++ xs) ++ zs ↭⟨ ++-assoc ys xs zs ⟩ |
| 318 |
ys ++ xs ++ zs ∎ |
| 319 |
where open PermutationReasoning |
| 320 |
|
| 321 |
------------------------------------------------------------------------ |
| 322 |
-- _∷_ |
| 323 |
|
| 324 |
drop-∷ : ∀ {x : A} {xs ys} → x ∷ xs ↭ x ∷ ys → xs ↭ ys |
| 325 |
drop-∷ = drop-mid [] [] |
| 326 |
|
| 327 |
------------------------------------------------------------------------ |
| 328 |
-- _∷ʳ_ |
| 329 |
|
| 330 |
∷↭∷ʳ : ∀ (x : A) xs → x ∷ xs ↭ xs ∷ʳ x |
| 331 |
∷↭∷ʳ x xs = ↭-sym (begin |
| 332 |
xs ++ [ x ] ↭⟨ shift x xs [] ⟩ |
| 333 |
x ∷ xs ++ [] ≡⟨ List.++-identityʳ _ ⟩ |
| 334 |
x ∷ xs ∎) |
| 335 |
where open PermutationReasoning |
| 336 |
|
| 337 |
------------------------------------------------------------------------ |
| 338 |
-- ʳ++ |
| 339 |
|
| 340 |
++↭ʳ++ : ∀ (xs ys : List A) → xs ++ ys ↭ xs ʳ++ ys |
| 341 |
++↭ʳ++ [] ys = ↭-refl |
| 342 |
++↭ʳ++ (x ∷ xs) ys = ↭-trans (↭-sym (shift x xs ys)) (++↭ʳ++ xs (x ∷ ys)) |
| 343 |
|
| 344 |
------------------------------------------------------------------------ |
| 345 |
-- reverse |
| 346 |
|
| 347 |
↭-reverse : (xs : List A) → reverse xs ↭ xs |
| 348 |
↭-reverse [] = ↭-refl |
| 349 |
↭-reverse (x ∷ xs) = begin |
| 350 |
reverse (x ∷ xs) ≡⟨ List.unfold-reverse x xs ⟩ |
| 351 |
reverse xs ∷ʳ x ↭⟨ ∷↭∷ʳ x (reverse xs) ⟨ |
| 352 |
x ∷ reverse xs ↭⟨ prep x (↭-reverse xs) ⟩ |
| 353 |
x ∷ xs ∎ |
| 354 |
where open PermutationReasoning |
| 355 |
|
| 356 |
------------------------------------------------------------------------ |
| 357 |
-- merge |
| 358 |
|
| 359 |
module _ {ℓ} {R : Rel A ℓ} (R? : Decidable R) where |
| 360 |
|
| 361 |
merge-↭ : ∀ xs ys → merge R? xs ys ↭ xs ++ ys |
| 362 |
merge-↭ [] [] = ↭-refl |
| 363 |
merge-↭ [] (y ∷ ys) = ↭-refl |
| 364 |
merge-↭ (x ∷ xs) [] = ↭-sym (++-identityʳ (x ∷ xs)) |
| 365 |
merge-↭ (x ∷ xs) (y ∷ ys) |
| 366 |
with does (R? x y) | merge-↭ xs (y ∷ ys) | merge-↭ (x ∷ xs) ys |
| 367 |
... | true | rec | _ = prep x rec |
| 368 |
... | false | _ | rec = begin |
| 369 |
y ∷ merge R? (x ∷ xs) ys ↭⟨ prep _ rec ⟩ |
| 370 |
y ∷ x ∷ xs ++ ys ↭⟨ shift y (x ∷ xs) ys ⟨ |
| 371 |
(x ∷ xs) ++ y ∷ ys ≡⟨ List.++-assoc [ x ] xs (y ∷ ys) ⟨ |
| 372 |
x ∷ xs ++ y ∷ ys ∎ |
| 373 |
where open PermutationReasoning |
| 374 |
|
| 375 |
------------------------------------------------------------------------ |
| 376 |
-- filter |
| 377 |
|
| 378 |
filter-↭ : ∀ {p} {P : Pred A p} (P? : Pred.Decidable P) → xs ↭ ys → filter P? xs ↭ filter P? ys |
| 379 |
filter-↭ P? refl = refl |
| 380 |
filter-↭ P? (prep x xs↭ys) with P? x |
| 381 |
... | yes _ = prep x (filter-↭ P? xs↭ys) |
| 382 |
... | no _ = filter-↭ P? xs↭ys |
| 383 |
filter-↭ P? (swap x y xs↭ys) with P? x in eqˣ | P? y in eqʸ |
| 384 |
... | yes _ | yes _ rewrite eqˣ rewrite eqʸ = swap x y (filter-↭ P? xs↭ys) |
| 385 |
... | yes _ | no _ rewrite eqˣ rewrite eqʸ = prep x (filter-↭ P? xs↭ys) |
| 386 |
... | no _ | yes _ rewrite eqˣ rewrite eqʸ = prep y (filter-↭ P? xs↭ys) |
| 387 |
... | no _ | no _ rewrite eqˣ rewrite eqʸ = filter-↭ P? xs↭ys |
| 388 |
filter-↭ P? (trans xs↭ys ys↭zs) = ↭-trans (filter-↭ P? xs↭ys) (filter-↭ P? ys↭zs) |
| 389 |
|
| 390 |
------------------------------------------------------------------------ |
| 391 |
-- catMaybes |
| 392 |
|
| 393 |
catMaybes-↭ : xs ↭ ys → catMaybes xs ↭ catMaybes ys |
| 394 |
catMaybes-↭ refl = refl |
| 395 |
catMaybes-↭ (trans xs↭ ↭ys) = trans (catMaybes-↭ xs↭) (catMaybes-↭ ↭ys) |
| 396 |
catMaybes-↭ (prep nothing xs↭) = catMaybes-↭ xs↭ |
| 397 |
catMaybes-↭ (prep (just x) xs↭) = prep x $ catMaybes-↭ xs↭ |
| 398 |
catMaybes-↭ (swap nothing nothing xs↭) = catMaybes-↭ xs↭ |
| 399 |
catMaybes-↭ (swap nothing (just y) xs↭) = prep y $ catMaybes-↭ xs↭ |
| 400 |
catMaybes-↭ (swap (just x) nothing xs↭) = prep x $ catMaybes-↭ xs↭ |
| 401 |
catMaybes-↭ (swap (just x) (just y) xs↭) = swap x y $ catMaybes-↭ xs↭ |
| 402 |
|
| 403 |
------------------------------------------------------------------------ |
| 404 |
-- mapMaybe |
| 405 |
|
| 406 |
mapMaybe-↭ : (f : A → Maybe B) → xs ↭ ys → mapMaybe f xs ↭ mapMaybe f ys |
| 407 |
mapMaybe-↭ f = catMaybes-↭ ∘ map⁺ f |
| 408 |
|
| 409 |
|
| 410 |
------------------------------------------------------------------------ |
| 411 |
-- DEPRECATED NAMES |
| 412 |
------------------------------------------------------------------------ |
| 413 |
-- Please use the new names as continuing support for the old names is |
| 414 |
-- not guaranteed. |
| 415 |
|
| 416 |
-- Version 2.3 |
| 417 |
|
| 418 |
import Data.Nat.ListAction.Properties as ℕ |
| 419 |
|
| 420 |
sum-↭ = ℕ.sum-↭ |
| 421 |
{-# WARNING_ON_USAGE sum-↭ |
| 422 |
"Warning: sum-↭ was deprecated in v2.3. |
| 423 |
Please use Data.Nat.ListAction.sum-↭ instead." |
| 424 |
#-} |
| 425 |
product-↭ = ℕ.product-↭ |
| 426 |
{-# WARNING_ON_USAGE product-↭ |
| 427 |
"Warning: product-↭ was deprecated in v2.3. |
| 428 |
Please use Data.Nat.ListAction.product-↭ instead." |
| 429 |
#-} |