| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An inductive definition for the permutation relation |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Binary.Permutation.Propositional |
| 10 |
{a} {A : Set a} where |
| 11 |
|
| 12 |
open import Data.List.Base using (List; []; _∷_) |
| 13 |
open import Data.List.Relation.Binary.Equality.Propositional using (_≋_; ≋⇒≡) |
| 14 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 15 |
open import Relation.Binary.Bundles using (Setoid) |
| 16 |
open import Relation.Binary.Structures using (IsEquivalence) |
| 17 |
open import Relation.Binary.Definitions using (Reflexive; Transitive) |
| 18 |
open import Relation.Binary.PropositionalEquality as ≡ using (_≡_; refl) |
| 19 |
import Relation.Binary.Reasoning.Setoid as EqReasoning |
| 20 |
open import Relation.Binary.Reasoning.Syntax |
| 21 |
|
| 22 |
import Data.List.Relation.Binary.Permutation.Setoid as Permutation |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
x y z v w : A |
| 27 |
ws xs ys zs : List A |
| 28 |
|
| 29 |
------------------------------------------------------------------------ |
| 30 |
-- An inductive definition of permutation |
| 31 |
|
| 32 |
-- Note that one would expect that this would be defined in terms of |
| 33 |
-- `Permutation.Setoid`. This is not currently the case as it involves |
| 34 |
-- adding in a bunch of trivial `_≡_` proofs to the constructors which |
| 35 |
-- a) adds noise and b) prevents easy access to the variables `x`, `y`. |
| 36 |
-- This may be changed in future when a better solution is found. |
| 37 |
|
| 38 |
infix 3 _↭_ |
| 39 |
|
| 40 |
data _↭_ : Rel (List A) a where |
| 41 |
refl : xs ↭ xs |
| 42 |
prep : ∀ x → xs ↭ ys → x ∷ xs ↭ x ∷ ys |
| 43 |
swap : ∀ x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys |
| 44 |
trans : xs ↭ ys → ys ↭ zs → xs ↭ zs |
| 45 |
|
| 46 |
-- Constructor aliases |
| 47 |
|
| 48 |
↭-refl : Reflexive _↭_ |
| 49 |
↭-refl = refl |
| 50 |
|
| 51 |
↭-prep : ∀ x → xs ↭ ys → x ∷ xs ↭ x ∷ ys |
| 52 |
↭-prep = prep |
| 53 |
|
| 54 |
↭-swap : ∀ x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys |
| 55 |
↭-swap = swap |
| 56 |
|
| 57 |
------------------------------------------------------------------------ |
| 58 |
-- _↭_ is an equivalence |
| 59 |
|
| 60 |
↭-reflexive : _≡_ ⇒ _↭_ |
| 61 |
↭-reflexive refl = ↭-refl |
| 62 |
|
| 63 |
↭-reflexive-≋ : _≋_ ⇒ _↭_ |
| 64 |
↭-reflexive-≋ xs≋ys = ↭-reflexive (≋⇒≡ xs≋ys) |
| 65 |
|
| 66 |
↭-sym : xs ↭ ys → ys ↭ xs |
| 67 |
↭-sym refl = refl |
| 68 |
↭-sym (prep x xs↭ys) = prep x (↭-sym xs↭ys) |
| 69 |
↭-sym (swap x y xs↭ys) = swap y x (↭-sym xs↭ys) |
| 70 |
↭-sym (trans xs↭ys ys↭zs) = trans (↭-sym ys↭zs) (↭-sym xs↭ys) |
| 71 |
|
| 72 |
-- A smart version of trans that avoids unnecessary `refl`s (see #1113). |
| 73 |
↭-trans : Transitive _↭_ |
| 74 |
↭-trans refl ρ₂ = ρ₂ |
| 75 |
↭-trans ρ₁ refl = ρ₁ |
| 76 |
↭-trans ρ₁ ρ₂ = trans ρ₁ ρ₂ |
| 77 |
|
| 78 |
↭-isEquivalence : IsEquivalence _↭_ |
| 79 |
↭-isEquivalence = record |
| 80 |
{ refl = ↭-refl |
| 81 |
; sym = ↭-sym |
| 82 |
; trans = ↭-trans |
| 83 |
} |
| 84 |
|
| 85 |
↭-setoid : Setoid _ _ |
| 86 |
↭-setoid = record |
| 87 |
{ isEquivalence = ↭-isEquivalence |
| 88 |
} |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- _↭_ is finer than `Setoid`-based permutation for any equivalence on A |
| 92 |
|
| 93 |
module _ {ℓ} {_≈_ : Rel A ℓ} (isEquivalence : IsEquivalence _≈_) where |
| 94 |
|
| 95 |
private |
| 96 |
open module ↭ₛ′ = Permutation record { isEquivalence = isEquivalence } |
| 97 |
using () |
| 98 |
renaming (_↭_ to _↭ₛ′_) |
| 99 |
|
| 100 |
↭⇒↭ₛ′ : _↭_ ⇒ _↭ₛ′_ |
| 101 |
↭⇒↭ₛ′ refl = ↭ₛ′.↭-refl |
| 102 |
↭⇒↭ₛ′ (prep x p) = ↭ₛ′.↭-prep x (↭⇒↭ₛ′ p) |
| 103 |
↭⇒↭ₛ′ (swap x y p) = ↭ₛ′.↭-swap x y (↭⇒↭ₛ′ p) |
| 104 |
↭⇒↭ₛ′ (trans p q) = ↭ₛ′.↭-trans′ (↭⇒↭ₛ′ p) (↭⇒↭ₛ′ q) |
| 105 |
|
| 106 |
|
| 107 |
------------------------------------------------------------------------ |
| 108 |
-- _↭_ is equivalent to `Setoid`-based permutation on `≡.setoid A` |
| 109 |
|
| 110 |
private |
| 111 |
open module ↭ₛ = Permutation (≡.setoid A) |
| 112 |
using () |
| 113 |
renaming (_↭_ to _↭ₛ_) |
| 114 |
|
| 115 |
↭⇒↭ₛ : _↭_ ⇒ _↭ₛ_ |
| 116 |
↭⇒↭ₛ = ↭⇒↭ₛ′ ≡.isEquivalence |
| 117 |
|
| 118 |
↭ₛ⇒↭ : _↭ₛ_ ⇒ _↭_ |
| 119 |
↭ₛ⇒↭ (↭ₛ.refl xs≋ys) = ↭-reflexive-≋ xs≋ys |
| 120 |
↭ₛ⇒↭ (↭ₛ.prep refl p) = ↭-prep _ (↭ₛ⇒↭ p) |
| 121 |
↭ₛ⇒↭ (↭ₛ.swap refl refl p) = ↭-swap _ _ (↭ₛ⇒↭ p) |
| 122 |
↭ₛ⇒↭ (↭ₛ.trans p q) = ↭-trans (↭ₛ⇒↭ p) (↭ₛ⇒↭ q) |
| 123 |
|
| 124 |
|
| 125 |
------------------------------------------------------------------------ |
| 126 |
-- A reasoning API to chain permutation proofs and allow "zooming in" |
| 127 |
-- to localised reasoning. |
| 128 |
|
| 129 |
module PermutationReasoning where |
| 130 |
|
| 131 |
private module Base = EqReasoning ↭-setoid |
| 132 |
|
| 133 |
open Base public |
| 134 |
hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨) |
| 135 |
renaming (≈-go to ↭-go) |
| 136 |
|
| 137 |
open ↭-syntax _IsRelatedTo_ _IsRelatedTo_ ↭-go ↭-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 |