| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Documentation for permutation over `List`s |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
module README.Data.List.Relation.Binary.Permutation where |
| 8 |
|
| 9 |
open import Algebra.Structures using (IsCommutativeMonoid) |
| 10 |
open import Data.List.Base |
| 11 |
open import Data.Nat using (ℕ; _+_) |
| 12 |
open import Relation.Binary.PropositionalEquality |
| 13 |
using (_≡_; refl; sym; cong; setoid) |
| 14 |
|
| 15 |
------------------------------------------------------------------------ |
| 16 |
-- Permutations |
| 17 |
|
| 18 |
-- As an alternative to pointwise equality you might consider two lists |
| 19 |
-- to be equal if they contain the same elements regardless of the order |
| 20 |
-- of the elements. This is known as either "set equality" or a |
| 21 |
-- "permutation". |
| 22 |
|
| 23 |
-- The easiest-to-use formalisation of this relation is found in the |
| 24 |
-- module: |
| 25 |
|
| 26 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 27 |
|
| 28 |
-- The permutation relation is written as `_↭_` and has four |
| 29 |
-- constructors. The first `refl` says that a list is always |
| 30 |
-- a permutation of itself, the second `prep` says that if the |
| 31 |
-- heads of the lists are the same they can be skipped, the third |
| 32 |
-- `swap` says that the first two elements of the lists can be |
| 33 |
-- swapped and the fourth `trans` says that permutation proofs |
| 34 |
-- can be chained transitively. |
| 35 |
|
| 36 |
-- For example a proof that two lists are a permutation of one |
| 37 |
-- another can be written as follows: |
| 38 |
|
| 39 |
lem₁ : 1 ∷ 2 ∷ 3 ∷ [] ↭ 3 ∷ 1 ∷ 2 ∷ [] |
| 40 |
lem₁ = trans (prep 1 (swap 2 3 refl)) (swap 1 3 refl) |
| 41 |
|
| 42 |
-- In practice it is difficult to parse the constructors in the |
| 43 |
-- proof above and hence understand why it holds. The |
| 44 |
-- `PermutationReasoning` module can be used to write this proof |
| 45 |
-- in a much more readable form: |
| 46 |
|
| 47 |
open PermutationReasoning |
| 48 |
|
| 49 |
lem₂ : 1 ∷ 2 ∷ 3 ∷ [] ↭ 3 ∷ 1 ∷ 2 ∷ [] |
| 50 |
lem₂ = begin |
| 51 |
1 ∷ 2 ∷ 3 ∷ [] ↭⟨ prep 1 (swap 2 3 refl) ⟩ |
| 52 |
1 ∷ 3 ∷ 2 ∷ [] ↭⟨ swap 1 3 refl ⟩ |
| 53 |
3 ∷ 1 ∷ 2 ∷ [] ∎ |
| 54 |
|
| 55 |
-- As might be expected, properties of the permutation relation may be |
| 56 |
-- found in: |
| 57 |
|
| 58 |
open import Data.List.Relation.Binary.Permutation.Propositional.Properties |
| 59 |
using (map⁺; ++-isCommutativeMonoid) |
| 60 |
|
| 61 |
lem₃ : ∀ (f : ℕ → ℕ) {xs ys : List ℕ} → xs ↭ ys → map f xs ↭ map f ys |
| 62 |
lem₃ = map⁺ |
| 63 |
|
| 64 |
lem₄ : IsCommutativeMonoid {A = List ℕ} _↭_ _++_ [] |
| 65 |
lem₄ = ++-isCommutativeMonoid |
| 66 |
|
| 67 |
-- Alternatively permutations using non-propositional equality can be |
| 68 |
-- found in: |
| 69 |
|
| 70 |
import Data.List.Relation.Binary.Permutation.Setoid |