8
8
9
9
module Data.Fin.Permutation where
10
10
11
+ open import Data.Bool using (true; false)
11
12
open import Data.Empty using (⊥-elim)
12
13
open import Data.Fin.Base
14
+ open import Data.Fin.Patterns
13
15
open import Data.Fin.Properties
14
16
import Data.Fin.Permutation.Components as PC
15
17
open import Data.Nat.Base using (ℕ; suc; zero)
16
18
open import Data.Product using (proj₂)
17
19
open import Function.Inverse as Inverse using (_↔_; Inverse; _InverseOf_)
18
20
open import Function.Equality using (_⟨$⟩_)
19
21
open import Function.Base using (_∘_)
20
- open import Relation.Nullary using (¬_; yes; no)
22
+ open import Level using (0ℓ)
23
+ open import Relation.Binary using (Rel)
24
+ open import Relation.Nullary using (does; ¬_; yes; no)
21
25
open import Relation.Nullary.Negation using (contradiction)
22
26
open import Relation.Binary.PropositionalEquality as P
23
27
using (_≡_; _≢_; refl; trans; sym; →-to-⟶; cong; cong₂)
24
28
open P.≡-Reasoning
25
29
30
+ private
31
+ variable
32
+ m n o : ℕ
33
+
26
34
------------------------------------------------------------------------
27
35
-- Types
28
36
@@ -42,48 +50,54 @@ Permutation′ n = Permutation n n
42
50
------------------------------------------------------------------------
43
51
-- Helper functions
44
52
45
- permutation : ∀ {m n} (f : Fin m → Fin n) (g : Fin n → Fin m) →
53
+ permutation : ∀ (f : Fin m → Fin n) (g : Fin n → Fin m) →
46
54
(→-to-⟶ g) InverseOf (→-to-⟶ f) → Permutation m n
47
55
permutation f g inv = record
48
56
{ to = →-to-⟶ f
49
57
; from = →-to-⟶ g
50
58
; inverse-of = inv
51
59
}
52
60
53
- _⟨$⟩ʳ_ : ∀ {m n} → Permutation m n → Fin m → Fin n
61
+ infixl 5 _⟨$⟩ʳ_ _⟨$⟩ˡ_
62
+ _⟨$⟩ʳ_ : Permutation m n → Fin m → Fin n
54
63
_⟨$⟩ʳ_ = _⟨$⟩_ ∘ Inverse.to
55
64
56
- _⟨$⟩ˡ_ : ∀ {m n} → Permutation m n → Fin n → Fin m
65
+ _⟨$⟩ˡ_ : Permutation m n → Fin n → Fin m
57
66
_⟨$⟩ˡ_ = _⟨$⟩_ ∘ Inverse.from
58
67
59
- inverseˡ : ∀ {m n} (π : Permutation m n) {i} → π ⟨$⟩ˡ (π ⟨$⟩ʳ i) ≡ i
68
+ inverseˡ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ˡ (π ⟨$⟩ʳ i) ≡ i
60
69
inverseˡ π = Inverse.left-inverse-of π _
61
70
62
- inverseʳ : ∀ {m n} (π : Permutation m n) {i} → π ⟨$⟩ʳ (π ⟨$⟩ˡ i) ≡ i
71
+ inverseʳ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ʳ (π ⟨$⟩ˡ i) ≡ i
63
72
inverseʳ π = Inverse.right-inverse-of π _
64
73
74
+ ------------------------------------------------------------------------
75
+ -- Equality
76
+
77
+ infix 6 _≈_
78
+ _≈_ : Rel (Permutation m n) 0ℓ
79
+ π ≈ ρ = ∀ i → π ⟨$⟩ʳ i ≡ ρ ⟨$⟩ʳ i
80
+
65
81
------------------------------------------------------------------------
66
82
-- Example permutations
67
83
68
84
-- Identity
69
85
70
- id : ∀ {n} → Permutation′ n
86
+ id : Permutation′ n
71
87
id = Inverse.id
72
88
73
89
-- Transpose two indices
74
90
75
- transpose : ∀ {n} → Fin n → Fin n → Permutation′ n
76
- transpose i j = permutation (PC.transpose i j) (PC.transpose j i)
77
- record
91
+ transpose : Fin n → Fin n → Permutation′ n
92
+ transpose i j = permutation (PC.transpose i j) (PC.transpose j i) record
78
93
{ left-inverse-of = λ _ → PC.transpose-inverse _ _
79
94
; right-inverse-of = λ _ → PC.transpose-inverse _ _
80
95
}
81
96
82
97
-- Reverse the order of indices
83
98
84
- reverse : ∀ {n} → Permutation′ n
85
- reverse = permutation PC.reverse PC.reverse
86
- record
99
+ reverse : Permutation′ n
100
+ reverse = permutation PC.reverse PC.reverse record
87
101
{ left-inverse-of = PC.reverse-involutive
88
102
; right-inverse-of = PC.reverse-involutive
89
103
}
@@ -93,12 +107,13 @@ reverse = permutation PC.reverse PC.reverse
93
107
94
108
-- Composition
95
109
96
- _∘ₚ_ : ∀ {m n o} → Permutation m n → Permutation n o → Permutation m o
110
+ infixr 9 _∘ₚ_
111
+ _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
97
112
π₁ ∘ₚ π₂ = π₂ Inverse.∘ π₁
98
113
99
114
-- Flip
100
115
101
- flip : ∀ {m n} → Permutation m n → Permutation n m
116
+ flip : Permutation m n → Permutation n m
102
117
flip = Inverse.sym
103
118
104
119
-- Element removal
@@ -107,10 +122,8 @@ flip = Inverse.sym
107
122
--
108
123
-- [0 ↦ i₀, …, k-1 ↦ iₖ₋₁, k ↦ iₖ₊₁, k+1 ↦ iₖ₊₂, …, n-1 ↦ iₙ]
109
124
110
- remove : ∀ {m n} → Fin (suc m) →
111
- Permutation (suc m) (suc n) → Permutation m n
112
- remove {m} {n} i π = permutation to from
113
- record
125
+ remove : Fin (suc m) → Permutation (suc m) (suc n) → Permutation m n
126
+ remove {m} {n} i π = permutation to from record
114
127
{ left-inverse-of = left-inverse-of
115
128
; right-inverse-of = right-inverse-of
116
129
}
@@ -139,47 +152,97 @@ remove {m} {n} i π = permutation to from
139
152
left-inverse-of : ∀ j → from (to j) ≡ j
140
153
left-inverse-of j = begin
141
154
from (to j) ≡⟨⟩
142
- punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut {i = πʳ i} _)) ⟩
155
+ punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut _)) ⟩
143
156
punchOut {i = i} {πˡ (πʳ (punchIn i j))} _ ≡⟨ punchOut-cong i (inverseˡ π) ⟩
144
157
punchOut {i = i} {punchIn i j} _ ≡⟨ punchOut-punchIn i ⟩
145
158
j ∎
146
159
147
160
right-inverse-of : ∀ j → to (from j) ≡ j
148
161
right-inverse-of j = begin
149
162
to (from j) ≡⟨⟩
150
- punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut {i = i} _)) ⟩
163
+ punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut _)) ⟩
151
164
punchOut {i = πʳ i} {πʳ (πˡ (punchIn (πʳ i) j))} _ ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
152
165
punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
153
166
j ∎
154
167
168
+ -- lift: takes a permutation m → n and creates a permutation (suc m) → (suc n)
169
+ -- by mapping 0 to 0 and applying the input permutation to everything else
170
+ lift₀ : Permutation m n → Permutation (suc m) (suc n)
171
+ lift₀ {m} {n} π = permutation to from record
172
+ { left-inverse-of = left-inverse-of
173
+ ; right-inverse-of = right-inverse-of
174
+ }
175
+ where
176
+ to : Fin (suc m) → Fin (suc n)
177
+ to 0F = 0F
178
+ to (suc i) = suc (π ⟨$⟩ʳ i)
179
+
180
+ from : Fin (suc n) → Fin (suc m)
181
+ from 0F = 0F
182
+ from (suc i) = suc (π ⟨$⟩ˡ i)
183
+
184
+ left-inverse-of : ∀ j → from (to j) ≡ j
185
+ left-inverse-of 0F = refl
186
+ left-inverse-of (suc j) = cong suc (inverseˡ π)
187
+
188
+ right-inverse-of : ∀ j → to (from j) ≡ j
189
+ right-inverse-of 0F = refl
190
+ right-inverse-of (suc j) = cong suc (inverseʳ π)
191
+
155
192
------------------------------------------------------------------------
156
193
-- Other properties
157
194
158
- module _ {m n} (π : Permutation (suc m) (suc n)) where
195
+ module _ (π : Permutation (suc m) (suc n)) where
159
196
private
160
197
πʳ = π ⟨$⟩ʳ_
161
198
πˡ = π ⟨$⟩ˡ_
162
199
163
200
punchIn-permute : ∀ i j → πʳ (punchIn i j) ≡ punchIn (πʳ i) (remove i π ⟨$⟩ʳ j)
164
- punchIn-permute i j = begin
165
- πʳ (punchIn i j) ≡⟨ sym (punchIn-punchOut {i = πʳ i} _) ⟩
166
- punchIn (πʳ i) (punchOut {i = πʳ i} {πʳ (punchIn i j)} _) ≡⟨⟩
167
- punchIn (πʳ i) (remove i π ⟨$⟩ʳ j) ∎
201
+ punchIn-permute i j = sym (punchIn-punchOut _)
168
202
169
203
punchIn-permute′ : ∀ i j → πʳ (punchIn (πˡ i) j) ≡ punchIn i (remove (πˡ i) π ⟨$⟩ʳ j)
170
204
punchIn-permute′ i j = begin
171
205
πʳ (punchIn (πˡ i) j) ≡⟨ punchIn-permute _ _ ⟩
172
206
punchIn (πʳ (πˡ i)) (remove (πˡ i) π ⟨$⟩ʳ j) ≡⟨ cong₂ punchIn (inverseʳ π) refl ⟩
173
207
punchIn i (remove (πˡ i) π ⟨$⟩ʳ j) ∎
174
208
175
- ↔⇒≡ : ∀ {m n} → Permutation m n → m ≡ n
209
+ lift₀-remove : πʳ 0F ≡ 0F → lift₀ (remove 0F π) ≈ π
210
+ lift₀-remove p 0F = sym p
211
+ lift₀-remove p (suc i) = punchOut-zero (πʳ (suc i)) p
212
+ where
213
+ punchOut-zero : ∀ {i} (j : Fin (suc n)) {neq} → i ≡ 0F → suc (punchOut {i = i} {j} neq) ≡ j
214
+ punchOut-zero 0F {neq} p = ⊥-elim (neq p)
215
+ punchOut-zero (suc j) refl = refl
216
+
217
+ ↔⇒≡ : Permutation m n → m ≡ n
176
218
↔⇒≡ {zero} {zero} π = refl
177
- ↔⇒≡ {zero} {suc n} π = contradiction (π ⟨$⟩ˡ zero ) ¬Fin0
178
- ↔⇒≡ {suc m} {zero} π = contradiction (π ⟨$⟩ʳ zero ) ¬Fin0
179
- ↔⇒≡ {suc m} {suc n} π = cong suc (↔⇒≡ (remove zero π))
219
+ ↔⇒≡ {zero} {suc n} π = contradiction (π ⟨$⟩ˡ 0F ) ¬Fin0
220
+ ↔⇒≡ {suc m} {zero} π = contradiction (π ⟨$⟩ʳ 0F ) ¬Fin0
221
+ ↔⇒≡ {suc m} {suc n} π = cong suc (↔⇒≡ (remove 0F π))
180
222
181
- fromPermutation : ∀ {m n} → Permutation m n → Permutation′ m
223
+ fromPermutation : Permutation m n → Permutation′ m
182
224
fromPermutation π = P.subst (Permutation _) (sym (↔⇒≡ π)) π
183
225
184
- refute : ∀ {m n} → m ≢ n → ¬ Permutation m n
226
+ refute : m ≢ n → ¬ Permutation m n
185
227
refute m≢n π = contradiction (↔⇒≡ π) m≢n
228
+
229
+ lift₀-id : (i : Fin (suc n)) → lift₀ id ⟨$⟩ʳ i ≡ i
230
+ lift₀-id 0F = refl
231
+ lift₀-id (suc i) = refl
232
+
233
+ lift₀-comp : ∀ (π : Permutation m n) (ρ : Permutation n o) →
234
+ lift₀ π ∘ₚ lift₀ ρ ≈ lift₀ (π ∘ₚ ρ)
235
+ lift₀-comp π ρ 0F = refl
236
+ lift₀-comp π ρ (suc i) = refl
237
+
238
+ lift₀-cong : ∀ (π ρ : Permutation m n) → π ≈ ρ → lift₀ π ≈ lift₀ ρ
239
+ lift₀-cong π ρ f 0F = refl
240
+ lift₀-cong π ρ f (suc i) = cong suc (f i)
241
+
242
+ lift₀-transpose : ∀ (i j : Fin n) → transpose (suc i) (suc j) ≈ lift₀ (transpose i j)
243
+ lift₀-transpose i j 0F = refl
244
+ lift₀-transpose i j (suc k) with does (k ≟ i)
245
+ ... | true = refl
246
+ ... | false with does (k ≟ j)
247
+ ... | false = refl
248
+ ... | true = refl
0 commit comments