-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSym.lean
More file actions
1262 lines (1097 loc) · 36.9 KB
/
Sym.lean
File metadata and controls
1262 lines (1097 loc) · 36.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import data.fintype data.equiv.basic group_theory.subgroup
namespace list
@[simp] lemma length_attach {α} (L : list α) :
L.attach.length = L.length :=
length_pmap
@[simp] lemma nth_le_attach {α} (L : list α) (i) (H : i < L.attach.length) :
(L.attach.nth_le i H).1 = L.nth_le i (length_attach L ▸ H) :=
calc (L.attach.nth_le i H).1
= (L.attach.map subtype.val).nth_le i (by simpa using H) : by rw nth_le_map'
... = L.nth_le i _ : by congr; apply attach_map_val
@[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) :
nth_le (range n) i H = i :=
option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)]
attribute [simp] length_of_fn
attribute [simp] nth_le_of_fn
-- Congratulations, I proved that two things which have
-- equally few lemmas are equal.
theorem of_fn_eq_pmap {α n} {f : fin n → α} :
of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) :=
by rw [pmap_eq_map_attach]; from ext_le (by simp)
(λ i hi1 hi2, by simp at hi1; simp [nth_le_of_fn f ⟨i, hi1⟩])
theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) :
nodup (of_fn f) :=
by rw of_fn_eq_pmap; from nodup_pmap
(λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n)
end list
section fin
variables {m n : ℕ}
def fin_zero_elim {C : Sort*} : fin 0 → C :=
λ x, false.elim $ nat.not_lt_zero x.1 x.2
def fin_sum : (fin m ⊕ fin n) ≃ fin (m + n) :=
{ to_fun := λ x, sum.rec_on x
(λ y, ⟨y.1, nat.lt_of_lt_of_le y.2 $ nat.le_add_right m n⟩)
(λ y, ⟨m + y.1, nat.add_lt_add_left y.2 m⟩),
inv_fun := λ x, if H : x.1 < m
then sum.inl ⟨x.1, H⟩
else sum.inr ⟨x.1 - m, nat.lt_of_add_lt_add_left $
show m + (x.1 - m) < m + n,
from (nat.add_sub_of_le $ le_of_not_gt H).symm ▸ x.2⟩,
left_inv := λ x, sum.cases_on x
(λ y, by simp [y.2]; from fin.eq_of_veq rfl)
(λ y, have H : ¬m + y.val < m, by simp [nat.zero_le],
by simp [H, nat.add_sub_cancel_left];
from fin.eq_of_veq rfl),
right_inv := λ x, begin
by_cases H : x.1 < m,
{ dsimp; rw [dif_pos H]; simp,
exact fin.eq_of_veq rfl },
{ dsimp; rw [dif_neg H]; simp,
apply fin.eq_of_veq; simp,
rw [nat.add_sub_of_le (le_of_not_gt H)] }
end }
def fin_prod : (fin m × fin n) ≃ fin (m * n) :=
{ to_fun := λ x, ⟨x.2.1 + n * x.1.1, calc
x.2.1 + n * x.1.1 + 1
= x.1.1 * n + x.2.1 + 1 : by ac_refl
... ≤ x.1.1 * n + n : nat.add_le_add_left x.2.2 _
... = (x.1.1 + 1) * n : eq.symm $ nat.succ_mul _ _
... ≤ m * n : nat.mul_le_mul_right _ x.1.2⟩,
inv_fun := λ x, have H : n > 0,
from nat.pos_of_ne_zero $ λ H,
nat.not_lt_zero x.1 $ by subst H; from x.2,
(⟨x.1 / n, (nat.div_lt_iff_lt_mul _ _ H).2 x.2⟩,
⟨x.1 % n, nat.mod_lt _ H⟩),
left_inv := λ ⟨x, y⟩, have H : n > 0,
from nat.pos_of_ne_zero $ λ H,
nat.not_lt_zero y.1 $ H ▸ y.2,
prod.ext
(fin.eq_of_veq $ calc
(y.1 + n * x.1) / n
= y.1 / n + x.1 : nat.add_mul_div_left _ _ H
... = 0 + x.1 : by rw nat.div_eq_of_lt y.2
... = x.1 : nat.zero_add x.1)
(fin.eq_of_veq $ calc
(y.1 + n * x.1) % n
= y.1 % n : nat.add_mul_mod_self_left _ _ _
... = y.1 : nat.mod_eq_of_lt y.2),
right_inv := λ x, fin.eq_of_veq $ nat.mod_add_div _ _ }
@[simp] lemma fin.raise_val (k : fin n) :
k.raise.val = k.val :=
rfl
def fin.fall : Π i : fin (n+1), i.1 < n → fin n :=
λ i h, ⟨i.1, h⟩
@[simp] lemma fin.fall_val (k : fin (n+1)) (H : k.1 < n) :
(k.fall H).val = k.val :=
rfl
def fin.descend (pivot : fin (n+1)) : Π i : fin (n+1), i ≠ pivot → fin n :=
λ i H, if h : i.1 < pivot.1
then i.fall (lt_of_lt_of_le h $ nat.le_of_lt_succ pivot.2)
else i.pred (λ H1, H $ by subst H1;
replace h := nat.eq_zero_of_le_zero (le_of_not_gt h);
from fin.eq_of_veq h.symm)
def fin.ascend (pivot : fin (n+1)) : Π i : fin n, fin (n+1) :=
λ i, if i.1 < pivot.1 then i.raise else i.succ
theorem fin.ascend_ne (pivot : fin (n+1)) (i : fin n) :
pivot.ascend i ≠ pivot :=
λ H, begin
unfold fin.ascend at H,
split_ifs at H;
rw ← H at h;
simp [lt_irrefl, nat.lt_succ_self] at h;
cc
end
@[simp] lemma fin.ascend_descend (pivot i : fin (n+1))
(H : i ≠ pivot) : pivot.ascend (pivot.descend i H) = i :=
begin
unfold fin.descend fin.ascend,
split_ifs with H1 H2 H3; apply fin.eq_of_veq; simp at *,
{ cases pivot with p hp,
cases i with i hi,
cases i with i, { simp at * },
exfalso, apply H, apply fin.eq_of_veq,
apply le_antisymm, { apply nat.succ_le_of_lt H2 },
simpa using H1 },
{ cases pivot with p hp,
cases i with i hi,
cases i with i,
{ exfalso, apply H, apply fin.eq_of_veq, symmetry,
apply nat.eq_zero_of_le_zero H2 },
refl }
end
@[simp] lemma fin.descend_ascend (pivot : fin (n+1))
(i : fin n) (H : pivot.ascend i ≠ pivot) :
pivot.descend (pivot.ascend i) H = i :=
begin
unfold fin.descend fin.ascend,
apply fin.eq_of_veq,
by_cases h : i.val < pivot.val,
{ simp [h] },
{ unfold ite dite,
cases nat.decidable_lt ((ite (i.val < pivot.val) (fin.raise i) (fin.succ i)).val) (pivot.val) with h1 h1,
{ simp,
cases nat.decidable_lt (i.val) (pivot.val),
{ simp },
{ cc } },
{ simp,
cases nat.decidable_lt (i.val) (pivot.val) with h2 h2,
{ simp [h2] at h1,
simp at *,
exfalso, apply lt_asymm (nat.lt_succ_self i.1),
apply lt_of_lt_of_le h1 h },
{ simp } } }
end
@[simp] lemma fin.succ_pred (i : fin (n+1)) (H : i ≠ 0) :
(i.pred H).succ = i :=
begin
apply fin.eq_of_veq,
cases i with i hi,
cases i,
{ exfalso, apply H, apply fin.eq_of_veq, refl },
refl
end
@[simp] lemma fin.pred_succ (i : fin n) (H : i.succ ≠ 0) :
i.succ.pred H = i :=
by cases i; refl
instance : decidable_linear_order (fin n) :=
{ lt_iff_le_not_le := λ i j, nat.lt_iff_le_not_le,
le_refl := λ ⟨i, hi⟩, nat.le_refl i,
le_trans := λ ⟨i, hi⟩ ⟨j, hj⟩ ⟨k, hk⟩ hij hjk, nat.le_trans hij hjk,
le_antisymm := λ ⟨i, hi⟩ ⟨j, hj⟩ hij hji, fin.eq_of_veq $ nat.le_antisymm hij hji,
le_total := λ ⟨i, hi⟩ ⟨j, hj⟩, or.cases_on (@nat.le_total i j) or.inl or.inr,
decidable_le := fin.decidable_le,
.. fin.has_le, .. fin.has_lt }
instance : preorder (fin n) :=
by apply_instance
end fin
section miscellaneous
theorem nat.pred_eq_of_eq_succ {m n : ℕ}
(H : m = n.succ) : m.pred = n :=
by simp [H]
@[simp] lemma equiv.symm_apply_eq {α β} {e : α ≃ β} {x y} :
e.symm x = y ↔ x = e y :=
⟨λ H, by simp [H.symm], λ H, by simp [H]⟩
theorem finset.lt_wf {α} [decidable_eq α] :
well_founded (@has_lt.lt (finset α) _) :=
have H : subrelation (@has_lt.lt (finset α) _)
(inv_image (<) finset.card),
from λ x y hxy, finset.card_lt_card hxy,
subrelation.wf H $ inv_image.wf _ $ nat.lt_wf
def finset.min' {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅) : α :=
@option.get _ S.min $
let ⟨k, hk⟩ := finset.exists_mem_of_ne_empty H in
let ⟨b, hb⟩ := finset.min_of_mem hk in by simp at hb; simp [hb]
theorem finset.min'_mem {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅) : S.min' H ∈ S :=
finset.mem_of_min $ by simp [finset.min']
theorem finset.min'_le {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅)
(x) (H2 : x ∈ S) : S.min' H ≤ x :=
finset.le_min_of_mem H2 $ option.get_mem _
theorem finset.le_min' {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅)
(x) (H2 : ∀ y ∈ S, x ≤ y) : x ≤ S.min' H :=
H2 _ $ finset.min'_mem _ _
def finset.max' {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅) : α :=
@option.get _ S.max $
let ⟨k, hk⟩ := finset.exists_mem_of_ne_empty H in
let ⟨b, hb⟩ := finset.max_of_mem hk in by simp at hb; simp [hb]
theorem finset.max'_mem {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅) : S.max' H ∈ S :=
finset.mem_of_max $ by simp [finset.max']
theorem finset.le_max' {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅)
(x) (H2 : x ∈ S) : x ≤ S.max' H :=
finset.le_max_of_mem H2 $ option.get_mem _
theorem finset.max'_le {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅)
(x) (H2 : ∀ y ∈ S, y ≤ x) : S.max' H ≤ x :=
H2 _ $ finset.max'_mem _ _
theorem finset.min'_lt_max' {α} [decidable_linear_order α]
(S : finset α) (H : S ≠ ∅) {i j}
(H1 : i ∈ S) (H2 : j ∈ S) (H3 : i ≠ j) :
S.min' H < S.max' H :=
begin
rcases lt_trichotomy i j with H4 | H4 | H4,
{ have H5 := finset.min'_le S H i H1,
have H6 := finset.le_max' S H j H2,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 },
{ cc },
{ have H5 := finset.min'_le S H j H2,
have H6 := finset.le_max' S H i H1,
apply lt_of_le_of_lt H5,
apply lt_of_lt_of_le H4 H6 }
end
end miscellaneous
variable (n : ℕ)
def Sym : Type :=
equiv.perm (fin n)
instance : has_coe_to_fun (Sym n) :=
equiv.has_coe_to_fun
@[extensionality] theorem Sym.ext (σ τ : Sym n)
(H : ∀ i, σ i = τ i) : σ = τ :=
equiv.ext _ _ H
theorem Sym.ext_iff (σ τ : Sym n) :
σ = τ ↔ ∀ i, σ i = τ i :=
⟨λ H i, H ▸ rfl, Sym.ext _ _ _⟩
instance : decidable_eq (Sym n) :=
λ σ τ, decidable_of_iff' _ (Sym.ext_iff _ _ _)
instance : group (Sym n) :=
equiv.perm_group
variable {n}
section perm
def Sym.to_list (σ : Sym n) : list (fin n) :=
list.of_fn σ
theorem Sym.to_list_perm (σ : Sym n) :
σ.to_list ~ list.of_fn (1 : Sym n) :=
(list.perm_ext
(list.nodup_of_fn $ σ.bijective.1)
(list.nodup_of_fn $ (1 : Sym n).bijective.1)).2 $ λ f,
by rw [list.of_fn_eq_pmap, list.of_fn_eq_pmap, list.mem_pmap, list.mem_pmap]; from
⟨λ _, ⟨f.1, by simp [f.2], fin.eq_of_veq rfl⟩,
λ _, ⟨(σ⁻¹ f).1, by simp [(σ⁻¹ f).2], by convert equiv.apply_inverse_apply σ f;
from congr_arg _ (fin.eq_of_veq rfl)⟩⟩
def list.to_sym (L : list (fin n))
(HL : L ~ list.of_fn (1 : Sym n)) : Sym n :=
{ to_fun := λ f, list.nth_le L f.1 $
by rw [list.perm_length HL, list.length_of_fn]; from f.2,
inv_fun := λ f, ⟨list.index_of f L,
begin
convert list.index_of_lt_length.2 _,
{ rw [list.perm_length HL, list.length_of_fn] },
{ rw [list.mem_of_perm HL, list.mem_iff_nth_le],
refine ⟨f.1, _, _⟩,
{ rw list.length_of_fn,
exact f.2 },
{ apply list.nth_le_of_fn } }
end⟩,
left_inv := λ f, fin.eq_of_veq $ list.nth_le_index_of
((list.perm_nodup HL).2 $ list.nodup_of_fn $ λ _ _, id) _ _,
right_inv := λ f, list.index_of_nth_le $ list.index_of_lt_length.2 $
(list.mem_of_perm HL).2 $ list.mem_iff_nth_le.2 $
⟨f.1, by rw list.length_of_fn; from f.2,
list.nth_le_of_fn _ _⟩ }
@[simp] lemma list.to_sym_apply (L : list (fin n))
(HL : L ~ list.of_fn (1 : Sym n)) (i) :
(L.to_sym HL) i = L.nth_le i.1 (by simp [list.perm_length HL, i.2]) :=
rfl
@[simp] lemma Sym.to_list_to_sym (σ : Sym n) :
σ.to_list.to_sym σ.to_list_perm = σ :=
Sym.ext _ _ _ $ λ i, fin.eq_of_veq $ by simp [Sym.to_list]
end perm
namespace Sym
def equiv_0 : Sym 0 ≃ fin (0:ℕ).fact :=
{ to_fun := λ _, ⟨0, dec_trivial⟩,
inv_fun := λ _, 1,
left_inv := λ _, ext _ _ _ $ λ ⟨n, H⟩, by cases H,
right_inv := λ ⟨n, H⟩, fin.eq_of_veq $
by cases H with H1 H1; [refl, cases H1] }
def descend (σ : Sym (n+1)) : Sym n :=
{ to_fun := λ i, (σ 0).descend (σ i.succ)
(λ H, by cases i; from nat.no_confusion
(fin.veq_of_eq (σ.bijective.1 H))),
inv_fun := λ i, (σ.symm ((σ 0).ascend i)).pred $ λ H,
fin.ascend_ne (σ 0) i $ by simpa using H,
left_inv := λ i, fin.eq_of_veq $ by dsimp; rw [fin.pred_val];
apply nat.pred_eq_of_eq_succ; rw [← fin.succ_val];
apply fin.veq_of_eq; simp,
right_inv := λ i, fin.eq_of_veq $ by simp }
def ascend (σ : Sym n) (k : fin (n+1)) : Sym (n+1) :=
{ to_fun := λ i, if H : i = 0 then k
else k.ascend $ σ $ i.pred H,
inv_fun := λ i, if H : i = k then 0
else (σ.symm $ k.descend i H).succ,
left_inv := λ i, fin.eq_of_veq $ begin
dsimp,
by_cases h1 : i = 0,
{ simp [h1] },
{ rw [dif_neg h1],
rw [dif_neg (fin.ascend_ne k (σ (i.pred h1)))],
simp }
end,
right_inv := λ i, fin.eq_of_veq $ begin
dsimp,
by_cases h1 : i = k,
{ simp [h1] },
{ rw [dif_neg h1, dif_neg], { simp },
intro H,
replace H := fin.veq_of_eq H,
simp at H,
exact nat.no_confusion H }
end }
@[simp] lemma descend_ascend (σ : Sym n) (k : fin (n+1)) :
descend (ascend σ k) = σ :=
begin
ext i,
dsimp [ascend, descend],
have H : i.succ ≠ 0,
{ intro H,
replace H := fin.veq_of_eq H,
simp at H, injections },
simp [H]
end
def equiv_succ (ih : Sym n ≃ fin n.fact) :
Sym (n+1) ≃ (fin (n+1) × fin n.fact) :=
{ to_fun := λ σ, (σ 0, ih $ descend σ),
inv_fun := λ F, ascend (ih.symm F.2) F.1,
left_inv := λ σ, ext _ _ _ $ λ i, begin
dsimp, rw [equiv.inverse_apply_apply ih],
dsimp [descend, ascend],
split_ifs, {subst h},
simp
end,
right_inv := λ F, prod.ext
(fin.eq_of_veq $ by dsimp [ascend]; simp) $
fin.eq_of_veq $ by simp }
protected def equiv : Sym n ≃ fin n.fact :=
nat.rec_on n equiv_0 $ λ n ih,
calc Sym (n+1)
≃ (fin (n+1) × fin n.fact) : equiv_succ ih
... ≃ fin (n+1).fact : fin_prod
instance : fintype (Sym n) :=
fintype.of_equiv _ Sym.equiv.symm
theorem card : fintype.card (Sym n) = nat.fact n :=
(fintype.of_equiv_card Sym.equiv.symm).trans $
fintype.card_fin _
theorem Cayley (α : Type*) [group α] [fintype α] :
∃ f : α → Sym (fintype.card α), function.injective f ∧ is_group_hom f :=
nonempty.rec_on (fintype.card_eq.1 $ fintype.card_fin $ fintype.card α) $ λ φ,
⟨λ x, ⟨λ i, φ.symm (x * φ i), λ i, φ.symm (x⁻¹ * φ i),
λ i, by simp, λ i, by simp⟩,
λ x y H, have H1 : _ := congr_fun (equiv.mk.inj H).1 (φ.symm 1), by simpa using H1,
⟨λ x y, ext _ _ _ $ λ i, by simp [mul_assoc]⟩⟩
@[simp] lemma mul_apply (σ τ : Sym n) (i : fin n) :
(σ * τ) i = σ (τ i) :=
rfl
@[simp] lemma one_apply (i : fin n) :
(1 : Sym n) i = i :=
rfl
@[simp] lemma inv_apply (σ : Sym n) (i : fin n) :
σ⁻¹ i = σ.symm i :=
rfl
def swap (i j : fin n) : Sym n :=
{ to_fun := λ k, if k = i then j
else if k = j then i else k,
inv_fun := λ k, if k = i then j
else if k = j then i else k,
left_inv := λ k, by dsimp; split_ifs; cc,
right_inv := λ k, by dsimp; split_ifs; cc }
@[simp] lemma swap_left (i j : fin n) :
swap i j i = j :=
by dsimp [swap]; cc
@[simp] lemma swap_right (i j : fin n) :
swap i j j = i :=
by dsimp [swap]; split_ifs; cc
@[simp] lemma swap_mul_self (i j : fin n) :
swap i j * swap i j = 1 :=
ext _ _ _ $ λ k, by dsimp [swap]; split_ifs; cc
theorem swap_comm (i j : fin n) :
swap i j = swap j i :=
ext _ _ _ $ λ k, by dsimp [swap]; split_ifs; cc
theorem swap_canonical (i j : fin n)
(H1 H2 : ({i, j} : finset (fin n)) ≠ ∅) :
swap (finset.min' _ H1) (finset.max' _ H2) = swap i j :=
begin
have H3 := finset.min'_mem _ H1,
have H4 : finset.min' _ H1 = j ∨ finset.min' _ H1 = i,
{ simpa using H3 },
have H5 := finset.max'_mem _ H2,
have H6 : finset.max' _ H2 = j ∨ finset.max' _ H2 = i,
{ simpa using H5 },
cases H4; cases H6,
{ rw [H4, H6],
have H7 := finset.min'_le _ H1 i (by simp),
have H8 := finset.le_max' _ H2 i (by simp),
rw H4 at H7, rw H6 at H8,
have H9 := le_antisymm H7 H8,
subst H9 },
{ rw [H4, H6, swap_comm] },
{ rw [H4, H6] },
{ rw [H4, H6],
have H7 := finset.min'_le _ H1 j (by simp),
have H8 := finset.le_max' _ H2 j (by simp),
rw H4 at H7, rw H6 at H8,
have H9 := le_antisymm H7 H8,
subst H9 }
end
@[simp] theorem swap_self (i : fin n) :
swap i i = 1 :=
ext _ _ _ $ λ k, by dsimp [swap]; split_ifs; cc
def support (σ : Sym n) : finset (fin n) :=
finset.filter (λ i, σ i ≠ i) finset.univ
theorem support_def {σ : Sym n} {i : fin n} :
i ∈ σ.support ↔ σ i ≠ i :=
⟨λ H, (finset.mem_filter.1 H).2, λ H, finset.mem_filter.2 ⟨finset.mem_univ _, H⟩⟩
def support_choice (σ : Sym n) (H : σ.support ≠ ∅) :
{ i // i ∈ σ.support } :=
⟨σ.support.min' H, finset.min'_mem _ _⟩
theorem support_swap {i j : fin n} (H : i ≠ j) :
(swap i j).support = {i, j} :=
begin
ext k, split,
{ intro H1,
simp [support_def, swap] at H1,
split_ifs at H1 with h1 h2 h3 h4,
{ subst h1, simp },
{ subst h2, simp },
cc },
{ intro H1,
simp at H1,
cases H1 with H1 H1;
subst H1;
simp [support_def, swap, H.symm, H] }
end
theorem support_swap_mul {σ : Sym n} {i : fin n}
(H : i ∈ σ.support) : (swap (σ i) i * σ).support < σ.support :=
begin
split,
{ intros j h1,
simp [support_def, swap] at *,
split_ifs at h1,
{ intro h2, rw ← h2 at h, subst h, cc },
{ cc },
{ cc } },
intro H1,
specialize H1 H,
simp [support_def, swap] at H1,
apply H1
end
@[simp] lemma support_one : support (1 : Sym n) = ∅ :=
finset.eq_empty_of_forall_not_mem $ λ i H,
support_def.1 H rfl
variable (n)
@[derive decidable_eq]
structure step : Type :=
(fst : fin n)
(snd : fin n)
(lt : fst < snd)
variable {n}
instance step.fintype : fintype (step n) :=
@fintype.of_surjective { i : fin n × fin n // i.1 < i.2 } _ _ _
(λ i, (⟨i.1.1, i.1.2, i.2⟩ : step n)) $ λ s,
⟨⟨(s.1, s.2), s.3⟩, by cases s; refl⟩
instance : has_mem (fin n) (step n) :=
⟨λ i s, i = s.1 ∨ i = s.2⟩
@[extensionality] theorem step.ext (s t : step n)
(H1 : s.1 = t.1) (H2 : s.2 = t.2) : s = t :=
by cases s; cases t; congr; assumption
def step.mk' (i j : fin n) (H : i ≠ j) : step n :=
if h : i < j then ⟨i, j, h⟩ else
⟨j, i, (eq_or_lt_of_not_lt h).resolve_left H⟩
def step.eval (s : step n) : Sym n :=
swap s.1 s.2
@[simp] lemma step.eval_mul_self (s : step n) :
s.eval * s.eval = 1 :=
by simp [step.eval]
@[simp] lemma step.eval_mk' (i j : fin n) (H : i ≠ j) :
(step.mk' i j H).eval = swap i j :=
by unfold step.mk'; split_ifs; simp [step.eval, swap_comm]
theorem choice.aux (σ : Sym n)
(H : ∃ i j, i ≠ j ∧ σ = swap i j) :
σ.support ≠ ∅ :=
let ⟨i, j, h1, h2⟩ := H in by
refine finset.ne_empty_of_mem (_ : j ∈ σ.support);
rw [h2, support_swap h1];
apply finset.mem_insert_self
def choice (σ : Sym n)
(H : ∃ i j, i ≠ j ∧ σ = swap i j) : step n :=
{ fst := σ.support.min' $ choice.aux _ H,
snd := σ.support.max' $ choice.aux _ H,
lt := by rcases H with ⟨i, j, h1, h2⟩; subst h2; dsimp;
refine finset.min'_lt_max' _ _ _ _ h1;
simp [support_swap h1] }
theorem eval_choice (σ : Sym n)
(H : ∃ i j, i ≠ j ∧ σ = swap i j) :
(σ.choice H).eval = σ :=
begin
rcases H with ⟨i, j, h1, h2⟩,
subst h2, unfold step.eval choice, dsimp,
convert swap_canonical i j _ _;
simp [support_swap h1]
end
theorem choice_eval (s : step n)
(H : ∃ i j, i ≠ j ∧ s.eval = swap i j) :
s.eval.choice H = s :=
begin
ext; dsimp [step.eval, choice],
{ apply le_antisymm,
{ apply finset.min'_le,
simp [support_swap (ne_of_lt s.3)] },
{ apply finset.le_min',
intros y h1,
simp [support_swap (ne_of_lt s.3)] at h1,
cases h1; subst h1,
apply le_of_lt s.3 } },
{ apply le_antisymm,
{ apply finset.max'_le,
intros y h1,
simp [support_swap (ne_of_lt s.3)] at h1,
cases h1; subst h1,
apply le_of_lt s.3 },
{ apply finset.le_max',
simp [support_swap (ne_of_lt s.3)] } }
end
def list_step.aux : has_well_founded (Sym n) :=
{ r := inv_image (<) support,
wf := inv_image.wf _ finset.lt_wf }
local attribute [instance] list_step.aux
attribute [elab_as_eliminator] well_founded.fix
attribute [elab_as_eliminator] well_founded.induction
def list_step (σ : Sym n) : list (step n) :=
by refine well_founded.fix list_step.aux.wf _ σ; from
λ σ ih, if H : σ.support = ∅ then []
else let ⟨i, hi⟩ := σ.support_choice H in
step.mk' (σ i) i (support_def.1 hi)
:: ih (swap (σ i) i * σ) (support_swap_mul hi)
@[simp] lemma list_step_prod (σ : Sym n) :
(σ.list_step.map step.eval).prod = σ :=
well_founded.induction list_step.aux.wf σ $ λ σ ih,
begin
dsimp [list_step],
rw [well_founded.fix_eq],
split_ifs,
{ ext, by_contra H,
suffices : i ∈ (∅ : finset (fin n)),
{ simp at this, cc },
rw [← h, support_def],
exact mt eq.symm H },
cases support_choice σ h with i hi,
unfold list_step._match_1,
specialize ih _ (support_swap_mul hi),
dsimp [list_step] at ih,
rw [list.map_cons, list.prod_cons, ih, ← mul_assoc],
rw [step.eval_mk', swap_mul_self, one_mul]
end
theorem mem_step_iff_mem_support (s : step n) (i : fin n) :
i ∈ s ↔ i ∈ s.eval.support :=
begin
unfold step.eval,
simp [support_swap (ne_of_lt s.3)],
rw [or_comm], refl
end
theorem support_eq_of_mul_eq_one {σ τ : Sym n} (H : σ * τ = 1) :
σ.support = τ.support :=
begin
ext i, simp [support_def, not_iff_not],
rw [eq_comm, iff.comm],
convert equiv.symm_apply_eq,
symmetry,
rw [equiv.symm_apply_eq, ← mul_apply, H, one_apply],
end
theorem of_mem_mul_support {σ τ : Sym n} {i}
(H : i ∈ (σ * τ).support) :
i ∈ σ.support ∨ i ∈ τ.support :=
by_contradiction $ λ H2,
by simp [support_def, not_or_distrib] at H H2;
simp [H2] at H; cc
theorem of_not_mem_mul_support {σ τ : Sym n} {i}
(H : i ∉ (σ * τ).support) :
i ∈ σ.support ↔ i ∈ τ.support :=
begin
simp [support_def] at H ⊢,
split,
{ intros H2 H3, rw H3 at H, cc },
{ intros H2 H3, rw ← H3 at H,
replace H := σ.bijective.1 H,
rw H3 at H, cc }
end
theorem not_mem_mul_support {σ τ : Sym n} {i}
(H1 : i ∉ σ.support) (H2 : i ∉ τ.support) :
i ∉ (σ * τ).support :=
begin
simp [support_def] at H1 H2 ⊢,
rw [H2, H1]
end
@[simp] lemma mem_mk' {i j k : fin n} (H : i ≠ j) :
k ∈ step.mk' i j H ↔ k = i ∨ k = j :=
begin
unfold step.mk',
split_ifs,
{ refl },
{ apply or_comm }
end
-- (ab)(cd) = (cd)(ab)
theorem sgn_aux5 (s t : step n)
(H1 : s.1 ≠ t.1) (H2 : s.1 ≠ t.2)
(H3 : s.2 ≠ t.1) (H4 : s.2 ≠ t.2) :
s.eval * t.eval = t.eval * s.eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
dsimp [step.eval, swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(ac) = (bc)(ab)
theorem sgn_aux4a (s t : step n)
(H1 : s.1 = t.1) (H4 : s.2 ≠ t.2) :
s.eval * t.eval = (step.mk' s.2 t.2 H4).eval * s.eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
unfold step.eval step.mk',
simp [swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(ca) = (cb)(ab)
theorem sgn_aux4b (s t : step n) (H1 : s.1 = t.2)
(H2 : t.1 < s.2) :
s.eval * t.eval = (⟨t.1, s.2, H2⟩ : step n).eval * s.eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
have := ne_of_lt H2,
dsimp [step.eval, swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(ac) = (ac)(bc)
theorem sgn_aux4c (s t : step n)
(H1 : s.1 = t.1) (H4 : s.2 ≠ t.2) :
s.eval * t.eval = t.eval * (step.mk' s.2 t.2 H4).eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
unfold step.eval step.mk',
simp [swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(ca) = (ca)(cb)
theorem sgn_aux4d (s t : step n)
(H1 : s.1 = t.2) (H4 : t.1 < s.2) :
s.eval * t.eval = t.eval * (⟨t.1, s.2, H4⟩ : step n).eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
have := ne_of_lt H4,
simp [step.eval, swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(bc) = (bc)(ac)
theorem sgn_aux3a (s t : step n) (H1 : s.2 = t.1)
(H2 : s.1 < t.2) :
s.eval * t.eval = t.eval * (⟨s.1, t.2, H2⟩ : step n).eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
have := ne_of_lt H2,
dsimp [step.eval, swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(cb) = (cb)(ac)
theorem sgn_aux3b (s t : step n) (H1 : s.2 = t.2)
(H2 : s.1 ≠ t.1) :
s.eval * t.eval = t.eval * (step.mk' s.1 t.1 H2).eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
unfold step.eval step.mk',
simp [swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(bc) = (ac)(ab)
theorem sgn_aux3c (s t : step n) (H1 : s.2 = t.1)
(H2 : s.1 < t.2) :
s.eval * t.eval = (⟨s.1, t.2, H2⟩ : step n).eval * s.eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
have := ne_of_lt H2,
dsimp [step.eval, swap], ext k,
dsimp at *,
split_ifs; cc
end
-- (ab)(cb) = (ac)(ab)
theorem sgn_aux3d (s t : step n) (H1 : s.2 = t.2)
(H2 : s.1 ≠ t.1) :
s.eval * t.eval = (step.mk' s.1 t.1 H2).eval * s.eval :=
begin
have := ne_of_lt s.3,
have := ne_of_lt t.3,
unfold step.eval step.mk',
simp [swap], ext k,
dsimp at *,
split_ifs; cc
end
theorem sgn_aux2 (s t : step n) (i) (H : i ∈ s) :
s = t ∨ ∃ s' t' : step n, s.eval * t.eval = s'.eval * t'.eval
∧ i ∉ s' ∧ i ∈ t' :=
begin
cases H with H H; subst H,
{ by_cases H2 : s.1 = t.1,
{ by_cases H3 : s.2 = t.2,
{ left, ext; assumption },
right, -- (ab)(ac) = (bc)(ab)
refine ⟨step.mk' s.2 t.2 H3, s, _, _⟩,
{ exact sgn_aux4a _ _ H2 _ },
rw [mem_mk'],
exact ⟨λ H, or.cases_on H
(ne_of_lt s.3) (H2.symm ▸ (ne_of_lt t.3)),
or.inl rfl⟩ },
right,
by_cases H3 : s.1 = t.2,
{ -- (ab)(ca) = (cb)(ab)
have H4 : t.1 < s.2 := lt_trans t.3 (H3 ▸ s.3),
refine ⟨⟨t.1, s.2, H4⟩, s, _, _⟩,
{ exact sgn_aux4b _ _ H3 _ },
exact ⟨λ H, or.cases_on H
H2 (ne_of_lt s.3),
or.inl rfl⟩ },
by_cases H4 : s.2 = t.1,
{ -- (ab)(bc) = (bc)(ac)
have H5 : s.1 < t.2 := lt_trans s.3 (H4.symm ▸ t.3),
refine ⟨t, ⟨s.1, t.2, H5⟩, _, _⟩,
{ exact sgn_aux3a _ _ H4 _ },
exact ⟨λ H, or.cases_on H
H2 (ne_of_lt H5),
or.inl rfl⟩ },
by_cases H5 : s.2 = t.2,
{ -- (ab)(cb) = (cb)(ac)
refine ⟨t, step.mk' s.1 t.1 H2, _, _⟩,
{ exact sgn_aux3b _ _ H5 _ },
exact ⟨λ H, or.cases_on H
H2 H3,
by simp⟩ },
-- (ab)(cd) = (cd)(ab)
refine ⟨t, s, _, _⟩,
{ exact sgn_aux5 _ _ H2 H3 H4 H5 },
exact ⟨λ H, or.cases_on H
H2 H3,
or.inl rfl⟩ },
by_cases H2 : s.1 = t.1,
{ by_cases H3 : s.2 = t.2,
{ left, ext; assumption },
right, -- (ab)(ac) = (ac)(bc)
refine ⟨t, step.mk' s.2 t.2 H3, _, _⟩,
{ exact sgn_aux4c _ _ H2 _ },
rw [mem_mk'],
exact ⟨λ H, or.cases_on H
(H2 ▸ ne_of_gt s.3) H3,
or.inl rfl⟩ },
right,
by_cases H3 : s.1 = t.2,
{ -- (ab)(ca) = (ca)(cb)
have H4 : t.1 < s.2 := lt_trans t.3 (H3 ▸ s.3),
refine ⟨t, ⟨t.1, s.2, H4⟩, _, _⟩,
{ exact sgn_aux4d _ _ H3 _ },
exact ⟨λ H, or.cases_on H
(ne_of_gt H4) (H3 ▸ ne_of_gt s.3),
or.inr rfl⟩ },
by_cases H4 : s.2 = t.1,
{ -- (ab)(bc) = (ac)(ab)
have H5 : s.1 < t.2 := lt_trans s.3 (H4.symm ▸ t.3),
refine ⟨⟨s.1, t.2, H5⟩, s, _, _⟩,
{ exact sgn_aux3c _ _ H4 _ },
exact ⟨λ H, or.cases_on H
(ne_of_gt s.3) (H4.symm ▸ ne_of_lt t.3),
or.inr rfl⟩ },
by_cases H5 : s.2 = t.2,
{ -- (ab)(cb) = (ac)(ab)
refine ⟨step.mk' s.1 t.1 H2, s, _, _⟩,
{ exact sgn_aux3d _ _ H5 _ },
rw [mem_mk'],
exact ⟨λ H, or.cases_on H
(ne_of_gt s.3) (H5.symm ▸ ne_of_gt t.3),
or.inr rfl⟩ },
refine ⟨t, s, _, _⟩,
{ exact sgn_aux5 _ _ H2 H3 H4 H5 },
exact ⟨λ H, or.cases_on H
H4 H5,
or.inr rfl⟩
end
theorem sgn_aux (L1 : list (step n)) (s : step n) (L2 : list (step n)) (i : fin n)
(H1 : (L1.map step.eval).prod * s.eval * (L2.map step.eval).prod = 1)
(H2 : i ∈ s) (H3 : i ∉ (L1.map step.eval).prod.support) :
∃ (L : list (step n)),
(L.map step.eval).prod = 1
∧ L.length + 2 = L1.length + 1 + L2.length :=
begin
induction L2 with hd tl ih generalizing L1 s,
{ simp at H1,
simp [mem_step_iff_mem_support] at H2,
rw support_eq_of_mul_eq_one H1 at H3,
cc },
simp [mul_assoc] at H1,
simp [mem_step_iff_mem_support] at H2,
have H4 := H3,
rw [support_eq_of_mul_eq_one H1] at H4,
replace H4 := of_not_mem_mul_support H4,
replace H4 := H4.1 H2,
rw [← mem_step_iff_mem_support] at H2,
rcases sgn_aux2 s hd i H2 with H5 | ⟨s', t', H5, H6, H7⟩,
{ subst H5,
rw [← mul_assoc s.eval, step.eval_mul_self, one_mul] at H1,
rw [← list.prod_append, ← list.map_append] at H1,
refine ⟨_, H1, _⟩,
simp, unfold bit0, ac_refl },
specialize ih (L1 ++ [s']) t' _ H7 _,
rcases ih with ⟨L, H8, H9⟩,
refine ⟨L, H8, _⟩,
{ simp [H9] },
{ simp at H5 ⊢,
rw [mul_assoc (L1.map step.eval).prod, ← H5],
simpa [mul_assoc] using H1 },
simpa using not_mem_mul_support H3 _,
simpa [mem_step_iff_mem_support] using H6
end
theorem length_even_of_prod_one (L : list (step n))
(H : (L.map step.eval).prod = 1) :
L.length % 2 = 0 :=
begin
generalize H1 : L.length = k,
revert L,
apply nat.strong_induction_on k,
intros k ih L H H1,
cases k with k, { refl },
cases k with k,
{ exfalso,
rw list.length_eq_one at H1,
cases H1 with s H2,
subst H2,
replace H := congr_arg support H,
simp [step.eval, support_swap (ne_of_lt s.3)] at H,
exact H },
cases L with hd tl, { simp at H1, injections },
rcases sgn_aux [] hd tl hd.1 _ (or.inl rfl) _ with ⟨L, H2, H3⟩,
specialize ih k _ L H2 _,
change (k + 2) % 2 = 0,
{ rw [nat.add_mod_right, ih] },
{ constructor, constructor },
{ simp at H1 H3,
rw ← H3 at H1,
exact nat.succ_inj (nat.succ_inj H1) },
{ simpa using H },
simp
end
theorem length_mod_two_eq (L1 L2 : list (step n))
(H : (L1.map step.eval).prod = (L2.map step.eval).prod) :
L1.length % 2 = L2.length % 2 :=
have H1 : (L2.map step.eval).reverse.prod = (L2.map step.eval).prod⁻¹,
from list.rec_on L2 (by simp) $ λ hd tl ih,