| 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 |
module Data.List.Relation.Binary.Permutation.Homogeneous where |
| 10 |
|
| 11 |
open import Data.List.Base using (List; _∷_; length) |
| 12 |
open import Data.List.Relation.Binary.Pointwise.Base as Pointwise |
| 13 |
using (Pointwise) |
| 14 |
import Data.List.Relation.Binary.Pointwise.Properties as Pointwise |
| 15 |
open import Data.Nat.Base using (ℕ; suc; _+_) |
| 16 |
open import Data.Fin.Base using (Fin; zero; suc; cast) |
| 17 |
import Data.Fin.Permutation as Fin |
| 18 |
open import Level using (Level; _⊔_) |
| 19 |
open import Function.Base using (_∘_) |
| 20 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 21 |
open import Relation.Binary.Bundles using (Setoid) |
| 22 |
open import Relation.Binary.Structures using (IsEquivalence) |
| 23 |
open import Relation.Binary.Definitions using (Reflexive; Symmetric) |
| 24 |
|
| 25 |
private |
| 26 |
variable |
| 27 |
a r s : Level |
| 28 |
A : Set a |
| 29 |
R S : Rel A r |
| 30 |
|
| 31 |
data Permutation {A : Set a} (R : Rel A r) : Rel (List A) (a ⊔ r) where |
| 32 |
refl : ∀ {xs ys} → Pointwise R xs ys → Permutation R xs ys |
| 33 |
prep : ∀ {xs ys x y} (eq : R x y) → Permutation R xs ys → Permutation R (x ∷ xs) (y ∷ ys) |
| 34 |
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) |
| 35 |
trans : ∀ {xs ys zs} → Permutation R xs ys → Permutation R ys zs → Permutation R xs zs |
| 36 |
|
| 37 |
------------------------------------------------------------------------ |
| 38 |
-- The Permutation relation is an equivalence |
| 39 |
|
| 40 |
sym : Symmetric R → Symmetric (Permutation R) |
| 41 |
sym R-sym (refl xs∼ys) = refl (Pointwise.symmetric R-sym xs∼ys) |
| 42 |
sym R-sym (prep x∼x′ xs↭ys) = prep (R-sym x∼x′) (sym R-sym xs↭ys) |
| 43 |
sym R-sym (swap x∼x′ y∼y′ xs↭ys) = swap (R-sym y∼y′) (R-sym x∼x′) (sym R-sym xs↭ys) |
| 44 |
sym R-sym (trans xs↭ys ys↭zs) = trans (sym R-sym ys↭zs) (sym R-sym xs↭ys) |
| 45 |
|
| 46 |
isEquivalence : Reflexive R → Symmetric R → IsEquivalence (Permutation R) |
| 47 |
isEquivalence R-refl R-sym = record |
| 48 |
{ refl = refl (Pointwise.refl R-refl) |
| 49 |
; sym = sym R-sym |
| 50 |
; trans = trans |
| 51 |
} |
| 52 |
|
| 53 |
setoid : Reflexive R → Symmetric R → Setoid _ _ |
| 54 |
setoid {R = R} R-refl R-sym = record |
| 55 |
{ isEquivalence = isEquivalence {R = R} R-refl R-sym |
| 56 |
} |
| 57 |
|
| 58 |
map : (R ⇒ S) → (Permutation R ⇒ Permutation S) |
| 59 |
map R⇒S (refl xs∼ys) = refl (Pointwise.map R⇒S xs∼ys) |
| 60 |
map R⇒S (prep e xs∼ys) = prep (R⇒S e) (map R⇒S xs∼ys) |
| 61 |
map R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map R⇒S xs∼ys) |
| 62 |
map R⇒S (trans xs∼ys ys∼zs) = trans (map R⇒S xs∼ys) (map R⇒S ys∼zs) |
| 63 |
|
| 64 |
-- Measures the number of constructors, can be useful for termination proofs |
| 65 |
steps : ∀ {xs ys} → Permutation R xs ys → ℕ |
| 66 |
steps (refl _) = 1 |
| 67 |
steps (prep _ xs↭ys) = suc (steps xs↭ys) |
| 68 |
steps (swap _ _ xs↭ys) = suc (steps xs↭ys) |
| 69 |
steps (trans xs↭ys ys↭zs) = steps xs↭ys + steps ys↭zs |
| 70 |
|
| 71 |
onIndices : ∀ {xs ys} → Permutation R xs ys → Fin.Permutation (length xs) (length ys) |
| 72 |
onIndices (refl ≋) = Fin.cast-id (Pointwise.Pointwise-length ≋) |
| 73 |
onIndices (prep e xs↭ys) = Fin.lift₀ (onIndices xs↭ys) |
| 74 |
onIndices (swap e f xs↭ys) = Fin.swap (onIndices xs↭ys) |
| 75 |
onIndices (trans ↭₁ ↭₂) = onIndices ↭₁ Fin.∘ₚ onIndices ↭₂ |