9
9
module Data.Fin.Permutation where
10
10
11
11
open import Data.Bool.Base using (true; false)
12
+ <<<<<<< refactor-Fin-properties
12
13
open import Data.Fin.Base using (Fin; suc; opposite; punchIn; punchOut)
13
14
open import Data.Fin.Patterns using (0F)
14
15
open import Data.Fin.Properties
15
16
using (¬Fin0; _≟_; ≟-diag-refl; ≟-off-diag
16
17
; opposite-involutive
17
18
; punchInᵢ≢i; punchOut-punchIn; punchIn-punchOut
18
19
; punchOut-cong; punchOut-cong′)
20
+ =======
21
+ open import Data.Fin.Base using (Fin; suc; cast; opposite; punchIn; punchOut)
22
+ open import Data.Fin.Patterns using (0F; 1F)
23
+ open import Data.Fin.Properties using (punchInᵢ≢i; punchOut-punchIn;
24
+ punchOut-cong; punchOut-cong′; punchIn-punchOut; _≟_; ¬Fin0; cast-involutive)
25
+ >>>>>>> master
19
26
import Data.Fin.Permutation.Components as PC
20
27
open import Data.Nat.Base using (ℕ; suc; zero)
21
28
open import Data.Product.Base using (_,_)
@@ -25,7 +32,7 @@ open import Function.Construct.Identity using (↔-id)
25
32
open import Function.Construct.Symmetry using (↔-sym)
26
33
open import Function.Definitions using (StrictlyInverseˡ; StrictlyInverseʳ)
27
34
open import Function.Properties.Inverse using (↔⇒↣)
28
- open import Function.Base using (_∘_)
35
+ open import Function.Base using (_∘_; _∘′_ )
29
36
open import Level using (0ℓ)
30
37
open import Relation.Binary.Core using (Rel)
31
38
open import Relation.Nullary.Decidable.Core using (does; yes; no)
@@ -59,11 +66,15 @@ Permutation′ n = Permutation n n
59
66
------------------------------------------------------------------------
60
67
-- Helper functions
61
68
62
- permutation : ∀ (f : Fin m → Fin n) (g : Fin n → Fin m) →
63
- StrictlyInverseˡ _≡_ f g → StrictlyInverseʳ _≡_ f g → Permutation m n
69
+ permutation : ∀ (f : Fin m → Fin n)
70
+ (g : Fin n → Fin m) →
71
+ StrictlyInverseˡ _≡_ f g →
72
+ StrictlyInverseʳ _≡_ f g →
73
+ Permutation m n
64
74
permutation = mk↔ₛ′
65
75
66
76
infixl 5 _⟨$⟩ʳ_ _⟨$⟩ˡ_
77
+
67
78
_⟨$⟩ʳ_ : Permutation m n → Fin m → Fin n
68
79
_⟨$⟩ʳ_ = Inverse.to
69
80
@@ -77,44 +88,66 @@ inverseʳ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ʳ (π ⟨$⟩ˡ i) ≡
77
88
inverseʳ π = Inverse.inverseˡ π refl
78
89
79
90
------------------------------------------------------------------------
80
- -- Equality
91
+ -- Equality over permutations
81
92
82
93
infix 6 _≈_
94
+
83
95
_≈_ : Rel (Permutation m n) 0ℓ
84
96
π ≈ ρ = ∀ i → π ⟨$⟩ʳ i ≡ ρ ⟨$⟩ʳ i
85
97
86
98
------------------------------------------------------------------------
87
- -- Example permutations
99
+ -- Permutation properties
88
100
89
- -- Identity
90
-
91
- id : Permutation′ n
101
+ id : Permutation n n
92
102
id = ↔-id _
93
103
94
- -- Transpose two indices
95
-
96
- transpose : Fin n → Fin n → Permutation′ n
97
- transpose i j = permutation (PC.transpose i j) (PC.transpose j i) (λ _ → PC.transpose-inverse _ _) (λ _ → PC.transpose-inverse _ _)
104
+ flip : Permutation m n → Permutation n m
105
+ flip = ↔-sym
98
106
99
- -- Reverse the order of indices
107
+ infixr 9 _∘ₚ_
100
108
109
+ <<<<<<< refactor-Fin-properties
101
110
reverse : Permutation′ n
102
111
reverse = permutation opposite opposite opposite-involutive opposite-involutive
112
+ =======
113
+ _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
114
+ π₁ ∘ₚ π₂ = π₂ ↔-∘ π₁
115
+ >>>>>>> master
103
116
104
117
------------------------------------------------------------------------
105
- -- Operations
118
+ -- Non-trivial identity
106
119
107
- -- Composition
120
+ cast-id : .(m ≡ n) → Permutation m n
121
+ cast-id m≡n = permutation
122
+ (cast m≡n)
123
+ (cast (sym m≡n))
124
+ (cast-involutive m≡n (sym m≡n))
125
+ (cast-involutive (sym m≡n) m≡n)
108
126
109
- infixr 9 _∘ₚ_
110
- _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
111
- π₁ ∘ₚ π₂ = π₂ ↔-∘ π₁
127
+ ------------------------------------------------------------------------
128
+ -- Transposition
112
129
113
- -- Flip
130
+ -- Transposes two elements in the permutation, keeping the remainder
131
+ -- of the permutation the same
132
+ transpose : Fin n → Fin n → Permutation n n
133
+ transpose i j = permutation
134
+ (PC.transpose i j)
135
+ (PC.transpose j i)
136
+ (λ _ → PC.transpose-inverse _ _)
137
+ (λ _ → PC.transpose-inverse _ _)
114
138
115
- flip : Permutation m n → Permutation n m
116
- flip = ↔-sym
139
+ ------------------------------------------------------------------------
140
+ -- Reverse
117
141
142
+ -- Reverses a permutation
143
+ reverse : Permutation n n
144
+ reverse = permutation
145
+ opposite
146
+ opposite
147
+ PC.reverse-involutive
148
+ PC.reverse-involutive
149
+
150
+ ------------------------------------------------------------------------
118
151
-- Element removal
119
152
--
120
153
-- `remove k [0 ↦ i₀, …, k ↦ iₖ, …, n ↦ iₙ]` yields
@@ -169,8 +202,14 @@ remove {m} {n} i π = permutation to from inverseˡ′ inverseʳ′
169
202
≡⟨ punchOut-punchIn (πʳ i) ⟩
170
203
j ∎
171
204
172
- -- lift: takes a permutation m → n and creates a permutation (suc m) → (suc n)
205
+ ------------------------------------------------------------------------
206
+ -- Lifting
207
+
208
+ -- Takes a permutation m → n and creates a permutation (suc m) → (suc n)
173
209
-- by mapping 0 to 0 and applying the input permutation to everything else
210
+ --
211
+ -- Note: should be refactored as a special-case when we add the
212
+ -- concatenation of two permutations
174
213
lift₀ : Permutation m n → Permutation (suc m) (suc n)
175
214
lift₀ {m} {n} π = permutation to from inverseˡ′ inverseʳ′
176
215
where
@@ -190,6 +229,9 @@ lift₀ {m} {n} π = permutation to from inverseˡ′ inverseʳ′
190
229
inverseˡ′ 0F = refl
191
230
inverseˡ′ (suc j) = cong suc (inverseʳ π)
192
231
232
+ ------------------------------------------------------------------------
233
+ -- Insertion
234
+
193
235
-- insert i j π is the permutation that maps i to j and otherwise looks like π
194
236
-- it's roughly an inverse of remove
195
237
insert : ∀ {m n} → Fin (suc m) → Fin (suc n) → Permutation m n → Permutation (suc m) (suc n)
@@ -239,6 +281,18 @@ insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
239
281
≡⟨ punchIn-punchOut j≢k ⟩
240
282
k ∎
241
283
284
+ ------------------------------------------------------------------------
285
+ -- Swapping
286
+
287
+ -- Takes a permutation m → n and creates a permutation
288
+ -- suc (suc m) → suc (suc n) by mapping 0 to 1 and 1 to 0 and
289
+ -- then applying the input permutation to everything else
290
+ --
291
+ -- Note: should be refactored as a special-case when we add the
292
+ -- concatenation of two permutations
293
+ swap : Permutation m n → Permutation (suc (suc m)) (suc (suc n))
294
+ swap π = transpose 0F 1F ∘ₚ lift₀ (lift₀ π)
295
+
242
296
------------------------------------------------------------------------
243
297
-- Other properties
244
298
0 commit comments