Permutation.agda 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. module Data.Permutation where
  2. {-
  3. open import Prelude
  4. open import Data.Fin as Fin hiding (_==_; _<_)
  5. open import Data.Nat
  6. open import Data.Vec
  7. open import Logic.Identity
  8. open import Logic.Base
  9. import Logic.ChainReasoning
  10. -- What is a permutation?
  11. -- Answer 1: A bijection between Fin n and itself
  12. data Permutation (n : Nat) : Set where
  13. permutation :
  14. (π π⁻¹ : Fin n -> Fin n) ->
  15. (forall {i} -> π (π⁻¹ i) ≡ i) ->
  16. Permutation n
  17. module Permutation {n : Nat}(P : Permutation n) where
  18. private
  19. π' : Permutation n -> Fin n -> Fin n
  20. π' (permutation x _ _) = x
  21. π⁻¹' : Permutation n -> Fin n -> Fin n
  22. π⁻¹' (permutation _ x _) = x
  23. proof : (P : Permutation n) -> forall {i} -> π' P (π⁻¹' P i) ≡ i
  24. proof (permutation _ _ x) = x
  25. π : Fin n -> Fin n
  26. π = π' P
  27. π⁻¹ : Fin n -> Fin n
  28. π⁻¹ = π⁻¹' P
  29. module Proofs where
  30. ππ⁻¹-id : {i : Fin n} -> π (π⁻¹ i) ≡ i
  31. ππ⁻¹-id = proof P
  32. open module Chain = Logic.ChainReasoning.Poly.Homogenous _≡_ (\x -> refl) (\x y z -> trans)
  33. π⁻¹-inj : (i j : Fin n) -> π⁻¹ i ≡ π⁻¹ j -> i ≡ j
  34. π⁻¹-inj i j h =
  35. chain> i
  36. === π (π⁻¹ i) by sym ππ⁻¹-id
  37. === π (π⁻¹ j) by cong π h
  38. === j by ππ⁻¹-id
  39. -- Generalise
  40. lem : {n : Nat}(f g : Fin n -> Fin n)
  41. -> (forall i -> f (g i) ≡ i)
  42. -> (forall i -> g (f i) ≡ i)
  43. lem {zero} f g inv ()
  44. lem {suc n} f g inv i = ?
  45. where
  46. gz≠gs : {i : Fin n} -> g fzero ≢ g (fsuc i)
  47. gz≠gs {i} gz=gs = fzero≠fsuc $
  48. chain> fzero
  49. === f (g fzero) by sym (inv fzero)
  50. === f (g (fsuc i)) by cong f gz=gs
  51. === fsuc i by inv (fsuc i)
  52. z≠f-thin-gz : {i : Fin n} -> fzero ≢ f (thin (g fzero) i)
  53. z≠f-thin-gz {i} z=f-thin-gz = ?
  54. -- f (g fzero)
  55. -- = fzero
  56. -- = f (thin (g fzero) i)
  57. g' : Fin n -> Fin n
  58. g' j = thick (g fzero) (g (fsuc j)) gz≠gs
  59. f' : Fin n -> Fin n
  60. f' j = thick fzero (f (thin (g fzero) j)) ?
  61. g'f' : forall j -> g' (f' j) ≡ j
  62. g'f' = lem {n} f' g' ?
  63. π⁻¹π-id : forall {i} -> π⁻¹ (π i) ≡ i
  64. π⁻¹π-id = ?
  65. -- Answer 2: A Vec (Fin n) n with no duplicates
  66. {-
  67. infixr 40 _◅_ _↦_,_
  68. infixr 20 _○_
  69. data Permutation : Nat -> Set where
  70. ε : Permutation zero
  71. _◅_ : {n : Nat} -> Fin (suc n) -> Permutation n -> Permutation (suc n)
  72. _↦_,_ : {n : Nat}(i j : Fin (suc n)) -> Permutation n -> Permutation (suc n)
  73. fzero ↦ j , π = j ◅ π
  74. fsuc i ↦ j , j' ◅ π = thin j j' ◅ i ↦ ? , π
  75. indices : {n : Nat} -> Permutation n -> Vec (Fin n) n
  76. indices ε = []
  77. indices (i ◅ π) = i :: map (thin i) (indices π)
  78. -- permute (i ◅ π) xs with xs [!] i where
  79. -- permute₁ (i ◅ π) .(insert i x xs) (ixV x xs) = x :: permute π xs
  80. permute : {n : Nat}{A : Set} -> Permutation n -> Vec A n -> Vec A n
  81. permute (i ◅ π) xs = permute' π i xs (xs [!] i)
  82. where
  83. permute' : {n : Nat}{A : Set} -> Permutation n -> (i : Fin (suc n))(xs : Vec A (suc n)) ->
  84. IndexView i xs -> Vec A (suc n)
  85. permute' π i .(insert i x xs') (ixV x xs') = x :: permute π xs'
  86. delete : {n : Nat} -> Fin (suc n) -> Permutation (suc n) -> Permutation n
  87. delete fzero (j ◅ π) = π
  88. delete {zero} (fsuc ()) _
  89. delete {suc _} (fsuc i) (j ◅ π) = ? ◅ delete i π
  90. identity : {n : Nat} -> Permutation n
  91. identity {zero } = ε
  92. identity {suc n} = fzero ◅ identity
  93. _⁻¹ : {n : Nat} -> Permutation n -> Permutation n
  94. ε ⁻¹ = ε
  95. (i ◅ π) ⁻¹ = ?
  96. _○_ : {n : Nat} -> Permutation n -> Permutation n -> Permutation n
  97. ε ○ π₂ = ε
  98. i ◅ π₁ ○ π₂ = (indices π₂ ! i) ◅ (π₁ ○ delete i π₂)
  99. -}
  100. -}