Debian Codegraph @

src/Data/Fin/Permutation.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Bijections on finite sets (i.e. permutations).
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Fin.Permutation where
10
11 open import Data.Bool.Base using (true; false)
12 open import Data.Fin.Base using (Fin; suc; cast; opposite; punchIn; punchOut)
13 open import Data.Fin.Patterns using (0F; 1F)
14 open import Data.Fin.Properties using (punchInᵢ≢i; punchOut-punchIn;
15 punchOut-cong; punchOut-cong′; punchIn-punchOut
16 ; _≟_; ¬Fin0; cast-involutive; opposite-involutive)
17 import Data.Fin.Permutation.Components as PC
18 open import Data.Nat.Base using (ℕ; suc; zero; 2+)
19 open import Data.Product.Base using (_,_; proj₂)
20 open import Function.Bundles using (_↔_; Injection; Inverse; mk↔ₛ′)
21 open import Function.Construct.Composition using (_↔-∘_)
22 open import Function.Construct.Identity using (↔-id)
23 open import Function.Construct.Symmetry using (↔-sym)
24 open import Function.Definitions using (StrictlyInverseˡ; StrictlyInverseʳ)
25 open import Function.Properties.Inverse using (↔⇒↣)
26 open import Function.Base using (_∘_; _∘′_)
27 open import Level using (0ℓ)
28 open import Relation.Binary.Core using (Rel)
29 open import Relation.Nullary using (does; ¬_; yes; no)
30 open import Relation.Nullary.Decidable using (dec-yes; dec-no)
31 open import Relation.Nullary.Negation using (contradiction)
32 open import Relation.Binary.PropositionalEquality.Core
33 using (_≡_; _≢_; refl; sym; trans; subst; cong; cong₂)
34 open import Relation.Binary.PropositionalEquality.Properties
35 using (module ≡-Reasoning)
36 open ≡-Reasoning
37
38 private
39 variable
40 m n o : ℕ
41
42 ------------------------------------------------------------------------
43 -- Types
44
45 -- A bijection between finite sets of potentially different sizes.
46 -- There only exist inhabitants of this type if in fact m = n, however
47 -- it is often easier to prove the existence of a bijection without
48 -- first proving that the sets have the same size. Indeed such a
49 -- bijection is a useful way to prove that the sets are in fact the same
50 -- size. See '↔-≡' below.
51
52 Permutation : ℕ → ℕ → Set
53 Permutation m n = Fin m ↔ Fin n
54
55 Permutation′ : ℕ → Set
56 Permutation′ n = Permutation n n
57
58 ------------------------------------------------------------------------
59 -- Helper functions
60
61 permutation : ∀ (f : Fin m → Fin n)
62 (g : Fin n → Fin m) →
63 StrictlyInverseˡ _≡_ f g →
64 StrictlyInverseʳ _≡_ f g →
65 Permutation m n
66 permutation = mk↔ₛ′
67
68 infixl 5 _⟨$⟩ʳ_ _⟨$⟩ˡ_
69
70 _⟨$⟩ʳ_ : Permutation m n → Fin m → Fin n
71 _⟨$⟩ʳ_ = Inverse.to
72
73 _⟨$⟩ˡ_ : Permutation m n → Fin n → Fin m
74 _⟨$⟩ˡ_ = Inverse.from
75
76 inverseˡ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ˡ (π ⟨$⟩ʳ i) ≡ i
77 inverseˡ π = Inverse.inverseʳ π refl
78
79 inverseʳ : ∀ (π : Permutation m n) {i} → π ⟨$⟩ʳ (π ⟨$⟩ˡ i) ≡ i
80 inverseʳ π = Inverse.inverseˡ π refl
81
82 ------------------------------------------------------------------------
83 -- Equality over permutations
84
85 infix 6 _≈_
86
87 _≈_ : Rel (Permutation m n) 0ℓ
88 π ≈ ρ = ∀ i → π ⟨$⟩ʳ i ≡ ρ ⟨$⟩ʳ i
89
90 ------------------------------------------------------------------------
91 -- Permutation properties
92
93 id : Permutation n n
94 id = ↔-id _
95
96 flip : Permutation m n → Permutation n m
97 flip = ↔-sym
98
99 infixr 9 _∘ₚ_
100
101 _∘ₚ_ : Permutation m n → Permutation n o → Permutation m o
102 π₁ ∘ₚ π₂ = π₂ ↔-∘ π₁
103
104 ------------------------------------------------------------------------
105 -- Non-trivial identity
106
107 cast-id : .(m ≡ n) → Permutation m n
108 cast-id m≡n = permutation
109 (cast m≡n)
110 (cast (sym m≡n))
111 (cast-involutive m≡n (sym m≡n))
112 (cast-involutive (sym m≡n) m≡n)
113
114 ------------------------------------------------------------------------
115 -- Transposition
116
117 -- Transposes two elements in the permutation, keeping the remainder
118 -- of the permutation the same
119 transpose : Fin n → Fin n → Permutation n n
120 transpose i j = permutation
121 (PC.transpose i j)
122 (PC.transpose j i)
123 (λ _ → PC.transpose-inverse _ _)
124 (λ _ → PC.transpose-inverse _ _)
125
126 ------------------------------------------------------------------------
127 -- Reverse
128
129 -- Reverses a permutation
130 reverse : Permutation n n
131 reverse = permutation
132 opposite
133 opposite
134 opposite-involutive
135 opposite-involutive
136
137 ------------------------------------------------------------------------
138 -- Element removal
139 --
140 -- `remove k [0 ↦ i₀, …, k ↦ iₖ, …, n ↦ iₙ]` yields
141 --
142 -- [0 ↦ i₀, …, k-1 ↦ iₖ₋₁, k ↦ iₖ₊₁, k+1 ↦ iₖ₊₂, …, n-1 ↦ iₙ]
143
144 remove : Fin (suc m) → Permutation (suc m) (suc n) → Permutation m n
145 remove {m} {n} i π = permutation to from inverseˡ′ inverseʳ′
146 where
147 πʳ = π ⟨$⟩ʳ_
148 πˡ = π ⟨$⟩ˡ_
149
150 permute-≢ : ∀ {i j} → i ≢ j → πʳ i ≢ πʳ j
151 permute-≢ p = p ∘ Injection.injective (↔⇒↣ π)
152
153 to-punchOut : ∀ {j : Fin m} → πʳ i ≢ πʳ (punchIn i j)
154 to-punchOut = permute-≢ (punchInᵢ≢i _ _ ∘ sym)
155
156 from-punchOut : ∀ {j : Fin n} → i ≢ πˡ (punchIn (πʳ i) j)
157 from-punchOut {j} p = punchInᵢ≢i (πʳ i) j (sym (begin
158 πʳ i ≡⟨ cong πʳ p ⟩
159 πʳ (πˡ (punchIn (πʳ i) j)) ≡⟨ inverseʳ π ⟩
160 punchIn (πʳ i) j ∎))
161
162 to : Fin m → Fin n
163 to j = punchOut (to-punchOut {j})
164
165 from : Fin n → Fin m
166 from j = punchOut {j = πˡ (punchIn (πʳ i) j)} from-punchOut
167
168 inverseʳ′ : StrictlyInverseʳ _≡_ to from
169 inverseʳ′ j = begin
170 from (to j) ≡⟨⟩
171 punchOut {i = i} {πˡ (punchIn (πʳ i) (punchOut to-punchOut))} _ ≡⟨ punchOut-cong′ i (cong πˡ (punchIn-punchOut _)) ⟩
172 punchOut {i = i} {πˡ (πʳ (punchIn i j))} _ ≡⟨ punchOut-cong i (inverseˡ π) ⟩
173 punchOut {i = i} {punchIn i j} _ ≡⟨ punchOut-punchIn i ⟩
174 j ∎
175
176 inverseˡ′ : StrictlyInverseˡ _≡_ to from
177 inverseˡ′ j = begin
178 to (from j) ≡⟨⟩
179 punchOut {i = πʳ i} {πʳ (punchIn i (punchOut from-punchOut))} _ ≡⟨ punchOut-cong′ (πʳ i) (cong πʳ (punchIn-punchOut _)) ⟩
180 punchOut {i = πʳ i} {πʳ (πˡ (punchIn (πʳ i) j))} _ ≡⟨ punchOut-cong (πʳ i) (inverseʳ π) ⟩
181 punchOut {i = πʳ i} {punchIn (πʳ i) j} _ ≡⟨ punchOut-punchIn (πʳ i) ⟩
182 j ∎
183
184 ------------------------------------------------------------------------
185 -- Lifting
186
187 -- Takes a permutation m → n and creates a permutation (suc m) → (suc n)
188 -- by mapping 0 to 0 and applying the input permutation to everything else
189 --
190 -- Note: should be refactored as a special-case when we add the
191 -- concatenation of two permutations
192 lift₀ : Permutation m n → Permutation (suc m) (suc n)
193 lift₀ {m} {n} π = permutation to from inverseˡ′ inverseʳ′
194 where
195 to : Fin (suc m) → Fin (suc n)
196 to 0F = 0F
197 to (suc i) = suc (π ⟨$⟩ʳ i)
198
199 from : Fin (suc n) → Fin (suc m)
200 from 0F = 0F
201 from (suc i) = suc (π ⟨$⟩ˡ i)
202
203 inverseʳ′ : StrictlyInverseʳ _≡_ to from
204 inverseʳ′ 0F = refl
205 inverseʳ′ (suc j) = cong suc (inverseˡ π)
206
207 inverseˡ′ : StrictlyInverseˡ _≡_ to from
208 inverseˡ′ 0F = refl
209 inverseˡ′ (suc j) = cong suc (inverseʳ π)
210
211 ------------------------------------------------------------------------
212 -- Insertion
213
214 -- insert i j π is the permutation that maps i to j and otherwise looks like π
215 -- it's roughly an inverse of remove
216 insert : ∀ {m n} → Fin (suc m) → Fin (suc n) → Permutation m n → Permutation (suc m) (suc n)
217 insert {m} {n} i j π = permutation to from inverseˡ′ inverseʳ′
218 where
219 to : Fin (suc m) → Fin (suc n)
220 to k with i ≟ k
221 ... | yes i≡k = j
222 ... | no i≢k = punchIn j (π ⟨$⟩ʳ punchOut i≢k)
223
224 from : Fin (suc n) → Fin (suc m)
225 from k with j ≟ k
226 ... | yes j≡k = i
227 ... | no j≢k = punchIn i (π ⟨$⟩ˡ punchOut j≢k)
228
229 inverseʳ′ : StrictlyInverseʳ _≡_ to from
230 inverseʳ′ k with i ≟ k
231 ... | yes i≡k rewrite proj₂ (dec-yes (j ≟ j) refl) = i≡k
232 ... | no i≢k
233 with j≢punchInⱼπʳpunchOuti≢k ← punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym
234 rewrite dec-no (j ≟ punchIn j (π ⟨$⟩ʳ punchOut i≢k)) j≢punchInⱼπʳpunchOuti≢k
235 = begin
236 punchIn i (π ⟨$⟩ˡ punchOut j≢punchInⱼπʳpunchOuti≢k) ≡⟨ cong (λ l → punchIn i (π ⟨$⟩ˡ l)) (punchOut-cong j refl) ⟩
237 punchIn i (π ⟨$⟩ˡ punchOut (punchInᵢ≢i j (π ⟨$⟩ʳ punchOut i≢k) ∘ sym)) ≡⟨ cong (λ l → punchIn i (π ⟨$⟩ˡ l)) (punchOut-punchIn j) ⟩
238 punchIn i (π ⟨$⟩ˡ (π ⟨$⟩ʳ punchOut i≢k)) ≡⟨ cong (punchIn i) (inverseˡ π) ⟩
239 punchIn i (punchOut i≢k) ≡⟨ punchIn-punchOut i≢k ⟩
240 k ∎
241
242 inverseˡ′ : StrictlyInverseˡ _≡_ to from
243 inverseˡ′ k with j ≟ k
244 ... | yes j≡k rewrite proj₂ (dec-yes (i ≟ i) refl) = j≡k
245 ... | no j≢k
246 with i≢punchInᵢπˡpunchOutj≢k ← punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym
247 rewrite dec-no (i ≟ punchIn i (π ⟨$⟩ˡ punchOut j≢k)) i≢punchInᵢπˡpunchOutj≢k
248 = begin
249 punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢπˡpunchOutj≢k) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-cong i refl) ⟩
250 punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i (π ⟨$⟩ˡ punchOut j≢k) ∘ sym)) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-punchIn i) ⟩
251 punchIn j (π ⟨$⟩ʳ (π ⟨$⟩ˡ punchOut j≢k)) ≡⟨ cong (punchIn j) (inverseʳ π) ⟩
252 punchIn j (punchOut j≢k) ≡⟨ punchIn-punchOut j≢k ⟩
253 k ∎
254
255 ------------------------------------------------------------------------
256 -- Swapping
257
258 -- Takes a permutation m → n and creates a permutation
259 -- suc (suc m) → suc (suc n) by mapping 0 to 1 and 1 to 0 and
260 -- then applying the input permutation to everything else
261 --
262 -- Note: should be refactored as a special-case when we add the
263 -- concatenation of two permutations
264 swap : Permutation m n → Permutation (2+ m) (2+ n)
265 swap π = transpose 0F 1F ∘ₚ lift₀ (lift₀ π)
266
267 ------------------------------------------------------------------------
268 -- Other properties
269
270 module _ (π : Permutation (suc m) (suc n)) where
271 private
272 πʳ = π ⟨$⟩ʳ_
273 πˡ = π ⟨$⟩ˡ_
274
275 punchIn-permute : ∀ i j → πʳ (punchIn i j) ≡ punchIn (πʳ i) (remove i π ⟨$⟩ʳ j)
276 punchIn-permute i j = sym (punchIn-punchOut _)
277
278 punchIn-permute′ : ∀ i j → πʳ (punchIn (πˡ i) j) ≡ punchIn i (remove (πˡ i) π ⟨$⟩ʳ j)
279 punchIn-permute′ i j = begin
280 πʳ (punchIn (πˡ i) j) ≡⟨ punchIn-permute _ _ ⟩
281 punchIn (πʳ (πˡ i)) (remove (πˡ i) π ⟨$⟩ʳ j) ≡⟨ cong₂ punchIn (inverseʳ π) refl ⟩
282 punchIn i (remove (πˡ i) π ⟨$⟩ʳ j) ∎
283
284 lift₀-remove : πʳ 0F ≡ 0F → lift₀ (remove 0F π) ≈ π
285 lift₀-remove p 0F = sym p
286 lift₀-remove p (suc i) = punchOut-zero (πʳ (suc i)) p
287 where
288 punchOut-zero : ∀ {i} (j : Fin (suc n)) {neq} → i ≡ 0F → suc (punchOut {i = i} {j} neq) ≡ j
289 punchOut-zero 0F {neq} p = contradiction p neq
290 punchOut-zero (suc j) refl = refl
291
292 ↔⇒≡ : Permutation m n → m ≡ n
293 ↔⇒≡ {zero} {zero} π = refl
294 ↔⇒≡ {zero} {suc n} π = contradiction (π ⟨$⟩ˡ 0F) ¬Fin0
295 ↔⇒≡ {suc m} {zero} π = contradiction (π ⟨$⟩ʳ 0F) ¬Fin0
296 ↔⇒≡ {suc m} {suc n} π = cong suc (↔⇒≡ (remove 0F π))
297
298 fromPermutation : Permutation m n → Permutation′ m
299 fromPermutation π = subst (Permutation _) (sym (↔⇒≡ π)) π
300
301 refute : m ≢ n → ¬ Permutation m n
302 refute m≢n π = contradiction (↔⇒≡ π) m≢n
303
304 lift₀-id : (i : Fin (suc n)) → lift₀ id ⟨$⟩ʳ i ≡ i
305 lift₀-id 0F = refl
306 lift₀-id (suc i) = refl
307
308 lift₀-comp : ∀ (π : Permutation m n) (ρ : Permutation n o) →
309 lift₀ π ∘ₚ lift₀ ρ ≈ lift₀ (π ∘ₚ ρ)
310 lift₀-comp π ρ 0F = refl
311 lift₀-comp π ρ (suc i) = refl
312
313 lift₀-cong : ∀ (π ρ : Permutation m n) → π ≈ ρ → lift₀ π ≈ lift₀ ρ
314 lift₀-cong π ρ f 0F = refl
315 lift₀-cong π ρ f (suc i) = cong suc (f i)
316
317 lift₀-transpose : ∀ (i j : Fin n) → transpose (suc i) (suc j) ≈ lift₀ (transpose i j)
318 lift₀-transpose i j 0F = refl
319 lift₀-transpose i j (suc k) with does (k ≟ i)
320 ... | true = refl
321 ... | false with does (k ≟ j)
322 ... | false = refl
323 ... | true = refl
324
325 insert-punchIn : ∀ i j (π : Permutation m n) k → insert i j π ⟨$⟩ʳ punchIn i k ≡ punchIn j (π ⟨$⟩ʳ k)
326 insert-punchIn i j π k with i ≟ punchIn i k
327 ... | yes i≡punchInᵢk = contradiction (sym i≡punchInᵢk) (punchInᵢ≢i i k)
328 ... | no i≢punchInᵢk = begin
329 punchIn j (π ⟨$⟩ʳ punchOut i≢punchInᵢk) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-cong i refl) ⟩
330 punchIn j (π ⟨$⟩ʳ punchOut (punchInᵢ≢i i k ∘ sym)) ≡⟨ cong (λ l → punchIn j (π ⟨$⟩ʳ l)) (punchOut-punchIn i) ⟩
331 punchIn j (π ⟨$⟩ʳ k) ∎
332
333 insert-remove : ∀ i (π : Permutation (suc m) (suc n)) → insert i (π ⟨$⟩ʳ i) (remove i π) ≈ π
334 insert-remove {m = m} {n = n} i π j with i ≟ j
335 ... | yes i≡j = cong (π ⟨$⟩ʳ_) i≡j
336 ... | no i≢j = begin
337 punchIn (π ⟨$⟩ʳ i) (punchOut (punchInᵢ≢i i (punchOut i≢j) ∘ sym ∘ Injection.injective (↔⇒↣ π))) ≡⟨ punchIn-punchOut _ ⟩
338 π ⟨$⟩ʳ punchIn i (punchOut i≢j) ≡⟨ cong (π ⟨$⟩ʳ_) (punchIn-punchOut i≢j) ⟩
339 π ⟨$⟩ʳ j ∎
340
341 remove-insert : ∀ i j (π : Permutation m n) → remove i (insert i j π) ≈ π
342 remove-insert i j π k with i ≟ i
343 ... | no i≢i = contradiction refl i≢i
344 ... | yes _ = begin
345 punchOut {i = j} _
346 ≡⟨ punchOut-cong j (insert-punchIn i j π k) ⟩
347 punchOut {i = j} (punchInᵢ≢i j (π ⟨$⟩ʳ k) ∘ sym)
348 ≡⟨ punchOut-punchIn j ⟩
349 π ⟨$⟩ʳ k
350 ∎