9
9
module Data.Fin.Permutation where
10
10
11
11
open import Data.Bool.Base using (true; false)
12
- open import Data.Fin.Base using (Fin; suc; opposite; punchIn; punchOut)
13
- open import Data.Fin.Patterns using (0F)
12
+ open import Data.Fin.Base using (Fin; suc; cast; opposite; punchIn; punchOut)
13
+ open import Data.Fin.Patterns using (0F; 1F )
14
14
open import Data.Fin.Properties using (punchInᵢ≢i; punchOut-punchIn;
15
- punchOut-cong; punchOut-cong′; punchIn-punchOut; _≟_; ¬Fin0)
15
+ punchOut-cong; punchOut-cong′; punchIn-punchOut; _≟_; ¬Fin0; cast-involutive )
16
16
import Data.Fin.Permutation.Components as PC
17
17
open import Data.Nat.Base using (ℕ; suc; zero)
18
18
open import Data.Product.Base using (_,_; proj₂)
@@ -22,7 +22,7 @@ open import Function.Construct.Identity using (↔-id)
22
22
open import Function.Construct.Symmetry using (↔-sym)
23
23
open import Function.Definitions using (StrictlyInverseˡ; StrictlyInverseʳ)
24
24
open import Function.Properties.Inverse using (↔⇒↣)
25
- open import Function.Base using (_∘_)
25
+ open import Function.Base using (_∘_; _∘′_ )
26
26
open import Level using (0ℓ)
27
27
open import Relation.Binary.Core using (Rel)
28
28
open import Relation.Nullary using (does; ¬_; yes; no)
@@ -57,11 +57,15 @@ Permutation′ n = Permutation n n
57
57
------------------------------------------------------------------------
58
58
-- Helper functions
59
59
60
- permutation : ∀ (f : Fin m → Fin n) (g : Fin n → Fin m) →
61
- StrictlyInverseˡ _≡_ f g → StrictlyInverseʳ _≡_ f g → Permutation m n
60
+ permutation : ∀ (f : Fin m → Fin n)
61
+ (g : Fin n → Fin m) →
62
+ StrictlyInverseˡ _≡_ f g →
63
+ StrictlyInverseʳ _≡_ f g →
64
+ Permutation m n
62
65
permutation = mk↔ₛ′
63
66
64
67
infixl 5 _⟨$⟩ʳ_ _⟨$⟩ˡ_
68
+
65
69
_⟨$⟩ʳ_ : Permutation m n → Fin m → Fin n
66
70
_⟨$⟩ʳ_ = Inverse.to
67
71
@@ -75,44 +79,61 @@ inverseʳ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ʳ (π ⟨$⟩ˡ i) ≡
75
79
inverseʳ π = Inverse.inverseˡ π refl
76
80
77
81
------------------------------------------------------------------------
78
- -- Equality
82
+ -- Equality over permutations
79
83
80
84
infix 6 _≈_
85
+
81
86
_≈_ : Rel (Permutation m n) 0ℓ
82
87
π ≈ ρ = ∀ i → π ⟨$⟩ʳ i ≡ ρ ⟨$⟩ʳ i
83
88
84
89
------------------------------------------------------------------------
85
- -- Example permutations
90
+ -- Permutation properties
86
91
87
- -- Identity
88
-
89
- id : Permutation′ n
92
+ id : Permutation n n
90
93
id = ↔-id _
91
94
92
- -- Transpose two indices
93
-
94
- transpose : Fin n → Fin n → Permutation′ n
95
- transpose i j = permutation (PC.transpose i j) (PC.transpose j i) (λ _ → PC.transpose-inverse _ _) (λ _ → PC.transpose-inverse _ _)
95
+ flip : Permutation m n → Permutation n m
96
+ flip = ↔-sym
96
97
97
- -- Reverse the order of indices
98
+ infixr 9 _∘ₚ_
98
99
99
- reverse : Permutation′ n
100
- reverse = permutation opposite opposite PC.reverse-involutive PC.reverse-involutive
100
+ _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
101
+ π₁ ∘ₚ π₂ = π₂ ↔-∘ π₁
101
102
102
103
------------------------------------------------------------------------
103
- -- Operations
104
+ -- Non-trivial identity
104
105
105
- -- Composition
106
+ cast-id : .(m ≡ n) → Permutation m n
107
+ cast-id m≡n = permutation
108
+ (cast m≡n)
109
+ (cast (sym m≡n))
110
+ (cast-involutive m≡n (sym m≡n))
111
+ (cast-involutive (sym m≡n) m≡n)
106
112
107
- infixr 9 _∘ₚ_
108
- _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
109
- π₁ ∘ₚ π₂ = π₂ ↔-∘ π₁
113
+ ------------------------------------------------------------------------
114
+ -- Transposition
110
115
111
- -- Flip
116
+ -- Transposes two elements in the permutation, keeping the remainder
117
+ -- of the permutation the same
118
+ transpose : Fin n → Fin n → Permutation n n
119
+ transpose i j = permutation
120
+ (PC.transpose i j)
121
+ (PC.transpose j i)
122
+ (λ _ → PC.transpose-inverse _ _)
123
+ (λ _ → PC.transpose-inverse _ _)
112
124
113
- flip : Permutation m n → Permutation n m
114
- flip = ↔-sym
125
+ ------------------------------------------------------------------------
126
+ -- Reverse
115
127
128
+ -- Reverses a permutation
129
+ reverse : Permutation n n
130
+ reverse = permutation
131
+ opposite
132
+ opposite
133
+ PC.reverse-involutive
134
+ PC.reverse-involutive
135
+
136
+ ------------------------------------------------------------------------
116
137
-- Element removal
117
138
--
118
139
-- `remove k [0 ↦ i₀, …, k ↦ iₖ, …, n ↦ iₙ]` yields
@@ -159,8 +180,14 @@ remove {m} {n} i π = permutation to from inverseˡ′ inverseʳ′
159
180
punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
160
181
j ∎
161
182
162
- -- lift: takes a permutation m → n and creates a permutation (suc m) → (suc n)
183
+ ------------------------------------------------------------------------
184
+ -- Lifting
185
+
186
+ -- Takes a permutation m → n and creates a permutation (suc m) → (suc n)
163
187
-- by mapping 0 to 0 and applying the input permutation to everything else
188
+ --
189
+ -- Note: should be refactored as a special-case when we add the
190
+ -- concatenation of two permutations
164
191
lift₀ : Permutation m n → Permutation (suc m) (suc n)
165
192
lift₀ {m} {n} π = permutation to from inverseˡ′ inverseʳ′
166
193
where
@@ -180,6 +207,9 @@ lift₀ {m} {n} π = permutation to from inverseˡ′ inverseʳ′
180
207
inverseˡ′ 0F = refl
181
208
inverseˡ′ (suc j) = cong suc (inverseʳ π)
182
209
210
+ ------------------------------------------------------------------------
211
+ -- Insertion
212
+
183
213
-- insert i j π is the permutation that maps i to j and otherwise looks like π
184
214
-- it's roughly an inverse of remove
185
215
insert : ∀ {m n} → Fin (suc m) → Fin (suc n) → Permutation m n → Permutation (suc m) (suc n)
@@ -221,6 +251,18 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
221
251
punchIn j (punchOut j≢k) ≡⟨ punchIn-punchOut j≢k ⟩
222
252
k ∎
223
253
254
+ ------------------------------------------------------------------------
255
+ -- Swapping
256
+
257
+ -- Takes a permutation m → n and creates a permutation
258
+ -- suc (suc m) → suc (suc n) by mapping 0 to 1 and 1 to 0 and
259
+ -- then applying the input permutation to everything else
260
+ --
261
+ -- Note: should be refactored as a special-case when we add the
262
+ -- concatenation of two permutations
263
+ swap : Permutation m n → Permutation (suc (suc m)) (suc (suc n))
264
+ swap π = transpose 0F 1F ∘ₚ lift₀ (lift₀ π)
265
+
224
266
------------------------------------------------------------------------
225
267
-- Other properties
226
268
0 commit comments