-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPermutationInstances.v
4092 lines (3499 loc) · 108 KB
/
PermutationInstances.v
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
Require Import Modulus.
Require Export PermutationsBase.
Require Import PermutationAutomation.
Require Export Prelim.
Require Export Bits.
Import Setoid.
(* Definitions of particular permutations, operations on permutations,
and their interactions *)
Local Open Scope program_scope.
Local Open Scope nat_scope.
Definition stack_perms (n0 n1 : nat) (f g : nat -> nat) : nat -> nat :=
fun n =>
if (n <? n0) then f n else
if (n <? n0 + n1) then (g (n - n0) + n0)%nat else n.
Definition tensor_perms (n0 n1 : nat) (f g : nat -> nat) : nat -> nat :=
fun n => if (n0 * n1 <=? n) then n else
(f (n / n1) * n1 + g (n mod n1)).
Definition swap_perm a b n :=
fun k => if n <=? k then k else
if k =? a then b else
if k =? b then a else k.
(* TODO: Implement things for this *)
Fixpoint insertion_sort_list n f :=
match n with
| 0 => []
| S n' => let k := (perm_inv (S n') f n') in
k :: insertion_sort_list n' (Bits.fswap f k n')
end.
Fixpoint swap_list_spec l : bool :=
match l with
| [] => true
| k :: ks => (k <? S (length ks)) && swap_list_spec ks
end.
Fixpoint perm_of_swap_list l :=
match l with
| [] => idn
| k :: ks => let n := length ks in
(swap_perm k n (S n) ∘ (perm_of_swap_list ks))%prg
end.
Fixpoint invperm_of_swap_list l :=
match l with
| [] => idn
| k :: ks => let n := length ks in
((invperm_of_swap_list ks) ∘ swap_perm k n (S n))%prg
end.
Definition perm_inv' n f :=
fun k => if n <=? k then k else perm_inv n f k.
Definition contract_perm f a :=
fun k =>
if k <? a then
if f k <? f a then f k else f k - 1
else
if f (k + 1) <? f a then f (k + 1) else f (k + 1) - 1.
Definition swap_2_perm : nat -> nat :=
swap_perm 0 1 2.
Definition rotr n m : nat -> nat :=
fun k => if n <=? k then k else (k + m) mod n.
Definition rotl n m : nat -> nat :=
fun k => if n <=? k then k else (k + (n - (m mod n))) mod n.
Definition swap_block_perm padl padm a :=
fun k =>
if k <? padl then k else
if k <? padl + a then k + (a + padm) else
if k <? padl + a + padm then k else
if k <? padl + a + padm + a then k - (a + padm) else
k.
Definition big_swap_perm p q :=
fun k =>
if k <? p then k + q else
if k <? p + q then k - p else k.
Definition reflect_perm n :=
fun k =>
if n <=? k then k else n - S k.
(** Given a permutation p over n qubits, construct a permutation over 2^n indices. *)
Definition qubit_perm_to_nat_perm n (p : nat -> nat) :=
fun k =>
if 2 ^ n <=? k then k else
funbool_to_nat n ((nat_to_funbool n k) ∘ p)%prg.
Definition kron_comm_perm p q :=
fun k => if p * q <=? k then k else
k mod p * q + k / p.
Definition perm_eq_id_mid (padl padm : nat) (f : nat -> nat) : Prop :=
forall a, a < padm -> f (padl + a) = padl + a.
Definition expand_perm_id_mid (padl padm padr : nat)
(f : nat -> nat) : nat -> nat :=
stack_perms padl (padm + padr) idn (rotr (padm + padr) padm)
∘ (stack_perms (padl + padr) padm f idn)
∘ stack_perms padl (padm + padr) idn (rotr (padm + padr) padr).
Definition contract_perm_id_mid (padl padm padr : nat)
(f : nat -> nat) : nat -> nat :=
stack_perms padl (padm + padr) idn (rotr (padm + padr) padr) ∘
f ∘ stack_perms padl (padm + padr) idn (rotr (padm + padr) padm).
#[export] Hint Unfold
stack_perms compose
rotr rotl
swap_2_perm swap_perm : perm_unfold_db.
Lemma permutation_change_dims n m (H : n = m) f :
permutation n f <-> permutation m f.
Proof.
now subst.
Qed.
Lemma perm_bounded_change_dims n m (Hnm : n = m) f (Hf : perm_bounded m f) :
perm_bounded n f.
Proof.
now subst.
Qed.
Lemma perm_eq_dim_change_if_nonzero n m f g :
perm_eq m f g -> (n <> 0 -> n = m) -> perm_eq n f g.
Proof.
intros Hfg H k Hk.
rewrite H in Hk by lia.
now apply Hfg.
Qed.
Lemma perm_eq_dim_change n m f g :
perm_eq m f g -> n = m -> perm_eq n f g.
Proof.
intros.
now apply (perm_eq_dim_change_if_nonzero n m f g).
Qed.
Lemma permutation_defn n f :
permutation n f <-> exists g,
(perm_bounded n f) /\ (perm_bounded n g) /\
(perm_eq n (f ∘ g) idn) /\ (perm_eq n (g ∘ f) idn).
Proof.
split; intros [g Hg]; exists g.
- repeat split; hnf; intros; now apply Hg.
- intros; repeat split; now apply Hg.
Qed.
Lemma permutation_of_le_permutation_idn_above n m f :
permutation n f -> m <= n -> (forall k, m <= k < n -> f k = k) ->
permutation m f.
Proof.
intros Hf Hm Hfid.
pose proof Hf as Hf'.
destruct Hf' as [finv Hfinv].
exists finv.
intros k Hk; repeat split; try (apply Hfinv; lia).
- pose proof (Hfinv k ltac:(lia)) as (?&?&?&?).
bdestructΩ (f k <? m).
specialize (Hfid (f k) ltac:(lia)).
pose proof (Hfinv (f k) ltac:(easy)) as Hfinvk.
rewrite Hfid in Hfinvk.
lia.
- pose proof (Hfinv k ltac:(lia)) as (?&?&?&?).
bdestructΩ (finv k <? m).
specialize (Hfid (finv k) ltac:(lia)).
replace -> (f (finv k)) in Hfid.
lia.
Qed.
Add Parametric Morphism n : (permutation n)
with signature perm_eq n ==> iff as permutation_perm_eq_proper.
Proof.
intros f g Hfg.
split; intros [inv Hinv];
exists inv;
intros k Hk;
[rewrite <- 2!Hfg by (destruct (Hinv k Hk); easy) |
rewrite 2!Hfg by (destruct (Hinv k Hk); easy)];
apply Hinv, Hk.
Qed.
Lemma permutation_eqb_iff {n f} a b : permutation n f ->
a < n -> b < n ->
f a =? f b = (a =? b).
Proof.
intros Hperm Hk Hfk.
bdestruct_one.
apply (permutation_is_injective n f Hperm) in H; [bdestruct_one| |]; lia.
bdestruct_one; subst; easy.
Qed.
Lemma permutation_eq_iff {n f} a b : permutation n f ->
a < n -> b < n ->
f a = f b <-> a = b.
Proof.
intros Hperm Hk Hfk.
generalize (permutation_eqb_iff _ _ Hperm Hk Hfk).
bdestructΩ'.
Qed.
Lemma perm_eq_iff_forall n (f g : nat -> nat) :
perm_eq n f g <-> forallb (fun k => f k =? g k) (seq 0 n) = true.
Proof.
rewrite forallb_seq0.
now setoid_rewrite Nat.eqb_eq.
Qed.
Lemma perm_eq_dec n (f g : nat -> nat) :
{perm_eq n f g} + {~ perm_eq n f g}.
Proof.
generalize (perm_eq_iff_forall n f g).
destruct (forallb (fun k => f k =? g k) (seq 0 n)); intros H;
[left | right]; rewrite H; easy.
Qed.
Lemma not_forallb_seq_exists f start len :
forallb f (seq start len) = false ->
exists n, n < len /\ f (n + start) = false.
Proof.
revert start; induction len; [easy|].
intros start.
simpl.
rewrite andb_false_iff.
intros [H | H].
- exists 0. split; [lia | easy].
- destruct (IHlen (S start) H) as (n & Hn & Hfn).
exists (S n); split; rewrite <- ?Hfn; f_equal; lia.
Qed.
Lemma not_forallb_seq0_exists f n :
forallb f (seq 0 n) = false ->
exists k, k < n /\ f k = false.
Proof.
intros H.
apply not_forallb_seq_exists in H.
setoid_rewrite Nat.add_0_r in H.
exact H.
Qed.
Lemma not_perm_eq_not_eq_at n (f g : nat -> nat) :
~ (perm_eq n f g) -> exists k, k < n /\ f k <> g k.
Proof.
rewrite perm_eq_iff_forall.
rewrite not_true_iff_false.
intros H.
apply not_forallb_seq0_exists in H.
setoid_rewrite Nat.eqb_neq in H.
exact H.
Qed.
Lemma perm_bounded_of_eq {n f g} :
perm_eq n g f -> perm_bounded n f ->
perm_bounded n g.
Proof.
intros Hfg Hf k Hk.
rewrite Hfg; auto.
Qed.
Lemma compose_perm_bounded n f g : perm_bounded n f -> perm_bounded n g ->
perm_bounded n (f ∘ g).
Proof.
unfold compose.
auto.
Qed.
#[export] Hint Resolve compose_perm_bounded : perm_bounded_db.
(* Section on perm_inv *)
Lemma perm_inv_linv_of_permutation n f (Hf : permutation n f) :
perm_eq n (perm_inv n f ∘ f) idn.
Proof.
exact (perm_inv_is_linv_of_permutation n f Hf).
Qed.
Lemma perm_inv_rinv_of_permutation n f (Hf : permutation n f) :
perm_eq n (f ∘ perm_inv n f) idn.
Proof.
exact (perm_inv_is_rinv_of_permutation n f Hf).
Qed.
#[export] Hint Rewrite
perm_inv_linv_of_permutation
perm_inv_rinv_of_permutation
using (solve [auto with perm_db]) : perm_inv_db.
Lemma perm_inv'_eq n f :
perm_eq n (perm_inv' n f) (perm_inv n f).
Proof.
intros k Hk.
unfold perm_inv'.
bdestructΩ'.
Qed.
#[export] Hint Extern 0
(perm_eq ?n (perm_inv' ?n ?f) ?g) =>
apply (perm_eq_trans (perm_inv'_eq n _)) : perm_inv_db.
#[export] Hint Extern 0
(perm_eq ?n ?g (perm_inv' ?n ?f)) =>
apply (fun H => perm_eq_trans
H (perm_eq_sym (perm_inv'_eq n _))) : perm_inv_db.
#[export] Hint Rewrite perm_inv'_eq : perm_inv_db.
Lemma perm_inv'_bounded n f :
perm_bounded n (perm_inv' n f).
Proof.
apply (perm_bounded_of_eq (perm_inv'_eq n f)).
auto with perm_bounded_db.
Qed.
Lemma perm_inv'_WF n f :
WF_Perm n (perm_inv' n f).
Proof.
intros k Hk;
unfold perm_inv';
bdestructΩ'.
Qed.
#[export] Hint Resolve perm_inv'_bounded : perm_bounded_db.
#[export] Hint Resolve perm_inv'_WF : WF_Perm_db.
Lemma perm_inv'_permutation n f : permutation n f ->
permutation n (perm_inv' n f).
Proof.
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv'_permutation : perm_db.
Lemma permutation_of_le_permutation_WF f m n : (m <= n)%nat -> permutation m f ->
WF_Perm m f -> permutation n f.
Proof.
intros Hmn [finv_m Hfinv_m] HWF.
exists (fun k => if m <=? k then k else finv_m k).
intros k Hk.
bdestruct (m <=? k).
- rewrite !HWF; bdestructΩ'.
- specialize (Hfinv_m _ H).
bdestructΩ'.
Qed.
Lemma perm_eq_compose_proper n (f f' g g' : nat -> nat) :
perm_bounded n g -> perm_eq n f f' -> perm_eq n g g' ->
perm_eq n (f ∘ g) (f' ∘ g').
Proof.
intros Hg Hf' Hg' k Hk.
unfold compose.
now rewrite Hf', Hg' by auto.
Qed.
#[export] Hint Resolve perm_eq_compose_proper : perm_inv_db.
Add Parametric Morphism n f : (@compose nat nat nat f) with signature
perm_eq n ==> perm_eq n as compose_perm_eq_proper_r.
Proof.
intros g g' Hg k Hk.
unfold compose.
now rewrite Hg.
Qed.
Add Parametric Morphism n : (@compose nat nat nat) with signature
perm_eq n ==>
on_predicate_relation_l (fun f => perm_bounded n f) (perm_eq n) ==>
perm_eq n as compose_perm_eq_proper_l.
Proof.
intros f f' Hf g g' [Hgbdd Hg] k Hk.
unfold compose.
rewrite <- Hg by easy.
auto.
Qed.
Lemma perm_inv_is_linv_of_permutation_compose (n : nat) (f : nat -> nat) :
permutation n f ->
perm_eq n (perm_inv n f ∘ f) idn.
Proof.
exact (perm_inv_is_linv_of_permutation n f).
Qed.
#[export] Hint Resolve
perm_inv_is_linv_of_permutation
perm_inv_is_linv_of_permutation_compose : perm_inv_db.
Lemma perm_inv_is_rinv_of_permutation_compose (n : nat) (f : nat -> nat) :
permutation n f ->
perm_eq n (f ∘ perm_inv n f) idn.
Proof.
exact (perm_inv_is_rinv_of_permutation n f).
Qed.
#[export] Hint Resolve
perm_inv_is_rinv_of_permutation
perm_inv_is_rinv_of_permutation_compose : perm_inv_db.
#[export] Hint Rewrite perm_inv_is_linv_of_permutation_compose
perm_inv_is_rinv_of_permutation_compose
using solve [auto with perm_db] : perm_inv_db.
#[export] Hint Rewrite
###perm_l -> perm_inv_is_linv_of_permutation_compose
using solve [auto with perm_bounded_db perm_db] : perm_inv_db.
#[export] Hint Rewrite
###perm_r -> perm_inv_is_linv_of_permutation_compose
using solve [auto with perm_bounded_db perm_db] : perm_inv_db.
#[export] Hint Rewrite
###perm_l -> perm_inv_is_rinv_of_permutation_compose
using solve [auto with perm_bounded_db perm_db] : perm_inv_db.
#[export] Hint Rewrite
###perm_r -> perm_inv_is_rinv_of_permutation_compose
using solve [auto with perm_bounded_db perm_db] : perm_inv_db.
Lemma perm_inv'_is_linv_of_permutation_compose (n : nat) (f : nat -> nat) :
permutation n f ->
perm_eq n (perm_inv' n f ∘ f) idn.
Proof.
intros.
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv'_is_linv_of_permutation_compose : perm_inv_db.
Lemma perm_inv'_is_rinv_of_permutation_compose (n : nat) (f : nat -> nat) :
permutation n f ->
perm_eq n (f ∘ perm_inv' n f) idn.
Proof.
intros.
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv'_is_rinv_of_permutation_compose : perm_inv_db.
Lemma permutation_iff_perm_inv'_inv n f :
permutation n f <->
perm_eq n (f ∘ perm_inv' n f) idn /\
perm_eq n (perm_inv' n f ∘ f) idn.
Proof.
split; [auto_perm|].
intros [Hrinv Hlinv].
assert (Hfbdd : perm_bounded n f). {
intros k Hk.
generalize (Hlinv k Hk).
unfold compose, perm_inv'.
bdestructΩ'.
}
exists (perm_inv' n f).
intros k Hk.
repeat split; [auto_perm.. | |].
- now apply Hlinv.
- now apply Hrinv.
Qed.
Lemma idn_WF_Perm n : WF_Perm n idn.
Proof. easy. Qed.
#[export] Hint Resolve idn_WF_Perm : WF_Perm_db.
Lemma perm_inv'_linv_of_permutation_WF n f :
permutation n f -> WF_Perm n f ->
perm_inv' n f ∘ f = idn.
Proof.
intros.
eq_by_WF_perm_eq n.
cleanup_perm_inv.
Qed.
Lemma perm_inv'_rinv_of_permutation_WF n f :
permutation n f -> WF_Perm n f ->
f ∘ perm_inv' n f = idn.
Proof.
intros.
eq_by_WF_perm_eq n.
cleanup_perm_inv.
Qed.
#[export] Hint Rewrite perm_inv'_linv_of_permutation_WF
perm_inv'_rinv_of_permutation_WF
using (solve [auto with perm_db WF_Perm_db]) : perm_inv_db.
#[export] Hint Rewrite
(###comp_l -> perm_inv'_linv_of_permutation_WF)
(###comp_r -> perm_inv'_linv_of_permutation_WF)
(###comp_l -> perm_inv'_rinv_of_permutation_WF)
(###comp_r -> perm_inv'_rinv_of_permutation_WF)
using (solve [auto with perm_db WF_Perm_db]) : perm_inv_db.
Lemma perm_eq_linv_injective n f finv finv' : permutation n f ->
is_perm_linv n f finv -> is_perm_linv n f finv' ->
perm_eq n finv finv'.
Proof.
intros Hperm Hfinv Hfinv'.
perm_eq_by_inv_inj f n.
Qed.
Lemma perm_inv_eq_inv n f finv :
(forall x : nat, x < n -> f x < n /\ finv x < n
/\ finv (f x) = x /\ f (finv x) = x)
-> perm_eq n (perm_inv n f) finv.
Proof.
intros Hfinv.
assert (Hperm: permutation n f) by (exists finv; easy).
perm_eq_by_inv_inj f n.
intros k Hk; now apply Hfinv.
Qed.
Lemma perm_inv_is_inv n f : permutation n f ->
forall k : nat, k < n -> perm_inv n f k < n /\ f k < n
/\ f (perm_inv n f k) = k /\ perm_inv n f (f k) = k.
Proof.
intros Hperm k Hk.
repeat split.
- apply perm_inv_bounded, Hk.
- destruct Hperm as [? H]; apply H, Hk.
- rewrite perm_inv_is_rinv_of_permutation; easy.
- rewrite perm_inv_is_linv_of_permutation; easy.
Qed.
Lemma perm_inv_perm_inv n f : permutation n f ->
perm_eq n (perm_inv n (perm_inv n f)) f.
Proof.
intros Hf.
perm_eq_by_inv_inj (perm_inv n f) n.
Qed.
#[export] Hint Resolve perm_inv_perm_inv : perm_inv_db.
#[export] Hint Rewrite perm_inv_perm_inv
using solve [auto with perm_db] : perm_inv_db.
Lemma perm_inv_eq_of_perm_eq' n m f g : perm_eq n f g -> m <= n ->
perm_eq n (perm_inv m f) (perm_inv m g).
Proof.
intros Heq Hm.
induction m; [easy|].
intros k Hk.
simpl.
rewrite Heq by lia.
rewrite IHm by lia.
easy.
Qed.
Lemma perm_inv_eq_of_perm_eq n f g : perm_eq n f g ->
perm_eq n (perm_inv n f) (perm_inv n g).
Proof.
intros Heq.
apply perm_inv_eq_of_perm_eq'; easy.
Qed.
#[export] Hint Resolve perm_inv_eq_of_perm_eq : perm_inv_db.
Lemma perm_inv'_eq_of_perm_eq n f g : perm_eq n f g ->
perm_inv' n f = perm_inv' n g.
Proof.
intros Heq.
eq_by_WF_perm_eq n.
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv_eq_of_perm_eq' : perm_inv_db.
Add Parametric Morphism n : (perm_inv n) with signature
perm_eq n ==> perm_eq n as perm_inv_perm_eq_proper.
Proof.
apply perm_inv_eq_of_perm_eq.
Qed.
Add Parametric Morphism n : (perm_inv' n) with signature
perm_eq n ==> eq as perm_inv'_perm_eq_to_eq_proper.
Proof.
apply perm_inv'_eq_of_perm_eq.
Qed.
Add Parametric Morphism n : (perm_inv' n) with signature
perm_eq n ==> perm_eq n as perm_inv'_perm_eq_proper.
Proof.
now intros f g ->.
Qed.
#[export] Hint Extern 20
(?f = ?g) =>
eapply eq_of_WF_perm_eq;
[solve [auto with WF_Perm_db]..|] : perm_inv_db.
#[export] Hint Extern 20
(?f ?k = ?g ?k) =>
match goal with
| Hk : k < ?n |- _ =>
let Heq := fresh in
enough (Heq : perm_eq n f g) by (exact (Heq k Hk))
end : perm_inv_db.
Lemma perm_inv'_perm_inv n f : permutation n f ->
perm_eq n (perm_inv' n (perm_inv n f)) f.
Proof.
cleanup_perm_inv.
Qed.
Lemma perm_inv_perm_inv' n f : permutation n f ->
perm_eq n (perm_inv n (perm_inv' n f)) f.
Proof.
intros Hf k Hk.
rewrite (perm_inv_eq_of_perm_eq _ _ _ (perm_inv'_eq _ _)) by easy.
cleanup_perm_inv.
Qed.
Lemma perm_inv'_perm_inv_eq n f :
permutation n f -> WF_Perm n f ->
perm_inv' n (perm_inv n f) = f.
Proof.
intros.
cleanup_perm_inv.
Qed.
Lemma perm_inv'_perm_inv' n f : permutation n f ->
perm_eq n (perm_inv' n (perm_inv' n f)) f.
Proof.
intros Hf.
rewrite (perm_inv'_eq_of_perm_eq _ _ _ (perm_inv'_eq n f)).
cleanup_perm_inv.
Qed.
Lemma perm_inv'_perm_inv'_eq n f :
permutation n f -> WF_Perm n f ->
perm_inv' n (perm_inv' n f) = f.
Proof.
rewrite (perm_inv'_eq_of_perm_eq _ _ _ (perm_inv'_eq n f)).
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv'_perm_inv
perm_inv'_perm_inv' perm_inv_perm_inv' : perm_inv_db.
#[export] Hint Rewrite perm_inv'_perm_inv_eq
perm_inv'_perm_inv'_eq
using
solve [auto with perm_db WF_Perm_db] : perm_inv_db.
Lemma permutation_compose' n f g :
permutation n f -> permutation n g ->
permutation n (fun x => f (g x)).
Proof.
apply permutation_compose.
Qed.
#[export] Hint Resolve permutation_compose permutation_compose' : perm_db.
#[export] Hint Rewrite perm_inv_is_linv_of_permutation
perm_inv_is_rinv_of_permutation : perm_inv_db.
Lemma perm_inv_eq_iff {n g} (Hg : permutation n g)
{k m} (Hk : k < n) (Hm : m < n) :
perm_inv n g k = m <-> k = g m.
Proof.
split;
[intros <- | intros ->];
rewrite ?(perm_inv_is_rinv_of_permutation _ g Hg),
?(perm_inv_is_linv_of_permutation _ g Hg);
easy.
Qed.
Lemma perm_inv_eqb_iff {n g} (Hg : permutation n g)
{k m} (Hk : k < n) (Hm : m < n) :
(perm_inv n g k =? m) = (k =? g m).
Proof.
apply Bool.eq_iff_eq_true;
rewrite 2!Nat.eqb_eq;
now apply perm_inv_eq_iff.
Qed.
Lemma perm_inv_ge n g k :
n <= perm_inv n g k -> n <= k.
Proof.
intros H.
bdestruct (n <=? k); [lia|].
specialize (perm_inv_bounded n g k); lia.
Qed.
Lemma compose_perm_inv_l n f g h
(Hf : permutation n f) (Hg : perm_bounded n g)
(Hh : perm_bounded n h) :
perm_eq n (perm_inv n f ∘ g) h <->
perm_eq n g (f ∘ h).
Proof.
split; unfold compose.
- intros H k Hk.
rewrite <- H; cleanup_perm_inv.
- intros H k Hk.
rewrite H; cleanup_perm_inv.
Qed.
Lemma compose_perm_inv_r n f g h
(Hf : permutation n f) (Hg : perm_bounded n g)
(Hh : perm_bounded n h) :
perm_eq n (g ∘ perm_inv n f) h <->
perm_eq n g (h ∘ f).
Proof.
split; unfold compose.
- intros H k Hk.
rewrite <- H; cleanup_perm_inv.
- intros H k Hk.
rewrite H; cleanup_perm_inv.
Qed.
Lemma compose_perm_inv_l' n f g h
(Hf : permutation n f) (Hg : perm_bounded n g)
(Hh : perm_bounded n h) :
perm_eq n h (perm_inv n f ∘ g) <->
perm_eq n (f ∘ h) g.
Proof.
split; intros H;
apply perm_eq_sym,
compose_perm_inv_l, perm_eq_sym;
assumption.
Qed.
Lemma compose_perm_inv_r' n f g h
(Hf : permutation n f) (Hg : perm_bounded n g)
(Hh : perm_bounded n h) :
perm_eq n h (g ∘ perm_inv n f) <->
perm_eq n (h ∘ f) g.
Proof.
split; intros H;
apply perm_eq_sym,
compose_perm_inv_r, perm_eq_sym;
assumption.
Qed.
Lemma compose_perm_inv'_l n (f g h : nat -> nat)
(Hf : permutation n f) (HWFf : WF_Perm n f) :
perm_inv' n f ∘ g = h <-> g = f ∘ h.
Proof.
split; [intros <- | intros ->];
rewrite <- compose_assoc;
cleanup_perm_inv.
Qed.
Lemma compose_perm_inv'_r n (f g h : nat -> nat)
(Hf : permutation n f) (HWFf : WF_Perm n f) :
g ∘ perm_inv' n f = h <-> g = h ∘ f.
Proof.
split; [intros <- | intros ->];
rewrite compose_assoc;
cleanup_perm_inv.
Qed.
Lemma compose_perm_inv'_l' n (f g h : nat -> nat)
(Hf : permutation n f) (HWFf : WF_Perm n f) :
h = perm_inv' n f ∘ g <-> f ∘ h = g.
Proof.
split; [intros -> | intros <-];
rewrite <- compose_assoc;
cleanup_perm_inv.
Qed.
Lemma compose_perm_inv'_r' n (f g h : nat -> nat)
(Hf : permutation n f) (HWFf : WF_Perm n f) :
h = g ∘ perm_inv' n f <-> h ∘ f = g.
Proof.
split; [intros -> | intros <-];
rewrite compose_assoc;
cleanup_perm_inv.
Qed.
Lemma perm_inv_perm_eq_iff n f g
(Hf : permutation n f) (Hg : permutation n g) :
perm_eq n (perm_inv n g) f <-> perm_eq n g (perm_inv n f).
Proof.
split; [intros <- | intros ->];
cleanup_perm_inv.
Qed.
Lemma perm_inv_compose {n f g} (Hf : permutation n f) (Hg : permutation n g) :
perm_eq n
(perm_inv n (f ∘ g))
(perm_inv n g ∘ perm_inv n f).
Proof.
apply perm_eq_sym.
perm_eq_by_inv_inj (f ∘ g) n.
rewrite !compose_assoc.
cleanup_perm_inv.
Qed.
#[export] Hint Resolve perm_inv_compose : perm_inv_db.
#[export] Hint Rewrite @perm_inv_compose
using solve [auto with perm_db] : perm_inv_db.
Lemma perm_inv_compose_alt n f g
(Hf : permutation n f) (Hg : permutation n g) :
perm_eq n
(perm_inv n (fun x => f (g x)))
(fun x => perm_inv n g (perm_inv n f x))%prg.
Proof.
now apply perm_inv_compose.
Qed.
Lemma perm_inv'_compose {n f g}
(Hf : permutation n f) (Hg : permutation n g) :
perm_inv' n (f ∘ g) =
perm_inv' n g ∘ perm_inv' n f.
Proof.
eq_by_WF_perm_eq n.
cleanup_perm_inv.
Qed.
#[export] Hint Rewrite @perm_inv'_compose
using solve [auto with perm_db] : perm_inv_db.
Lemma perm_inv_inj n f g :
permutation n f -> permutation n g ->
perm_eq n (perm_inv n f) (perm_inv n g) ->
perm_eq n f g.
Proof.
intros Hf Hg Hfg.
rewrite <- (perm_inv_perm_inv n f Hf).
rewrite Hfg.
rewrite perm_inv_perm_inv by easy.
easy.
Qed.
(* Permute bounded predicates *)
Lemma forall_lt_iff n (P Q : nat -> Prop)
(HPQ : forall k, k < n -> P k <-> Q k) :
(forall k, k < n -> P k) <-> (forall k, k < n -> Q k).
Proof.
apply forall_iff; intros k.
apply impl_iff; intros Hk.
auto.
Qed.
Lemma forall_lt_iff_permute n f (Hf : permutation n f)
(P : nat -> Prop) :
(forall k, k < n -> P k) <-> (forall k, k < n -> P (f k)).
Proof.
split; intros HP.
- intros k Hk.
apply HP.
auto with perm_db.
- intros k Hk.
generalize (HP (perm_inv n f k) (perm_inv_bounded n f k Hk)).
now rewrite perm_inv_is_rinv_of_permutation by easy.
Qed.
Lemma forall_lt_iff_of_permute_l n f (Hf : permutation n f)
(P Q : nat -> Prop) (HPQ : forall k, k < n -> P (f k) <-> Q k) :
(forall k, k < n -> P k) <-> (forall k, k < n -> Q k).
Proof.
rewrite (forall_lt_iff_permute n f Hf).
apply forall_iff; intros k.
apply impl_iff; intros Hk.
now apply HPQ.
Qed.
Lemma forall_lt_iff_of_permute_r n f (Hf : permutation n f)
(P Q : nat -> Prop) (HPQ : forall k, k < n -> P k <-> Q (f k)) :
(forall k, k < n -> P k) <-> (forall k, k < n -> Q k).
Proof.
symmetry.
apply (forall_lt_iff_of_permute_l n f Hf).
intros k Hk.
now rewrite HPQ.
Qed.
Lemma idn_inv n :
perm_eq n (perm_inv n idn) idn.
Proof.
perm_eq_by_inv_inj (fun k:nat => k) n.
Qed.
#[export] Hint Resolve idn_inv : perm_inv_db.
Lemma idn_inv' n :
perm_inv' n idn = idn.
Proof.
permutation_eq_by_WF_inv_inj (fun k:nat=>k) n.
Qed.
#[export] Hint Rewrite idn_inv' : perm_inv_db.
Lemma swap_perm_defn a b n : a < n -> b < n ->
perm_eq n (swap_perm a b n)
(fun x =>
if x =? a then b else
if x =? b then a else x).
Proof.
intros Ha Hb k Hk.
unfold swap_perm.
bdestructΩ'.
Qed.
Lemma swap_perm_same a n :
swap_perm a a n = idn.
Proof.
unfold swap_perm.
apply functional_extensionality; intros k.
bdestructΩ'.
Qed.
Lemma swap_perm_left a b n : a < n ->
swap_perm a b n a = b.
Proof.
unfold swap_perm; bdestructΩ'.
Qed.
Lemma swap_perm_right a b n : b < n ->
swap_perm a b n b = a.
Proof.
unfold swap_perm; bdestructΩ'.
Qed.
Lemma swap_perm_neither a b n x : x <> a -> x <> b ->
swap_perm a b n x = x.
Proof.
unfold swap_perm; bdestructΩ'.
Qed.
Lemma swap_perm_comm a b n :
swap_perm a b n = swap_perm b a n.
Proof.
apply functional_extensionality; intros k.
unfold swap_perm.
bdestructΩ'.
Qed.
Lemma swap_perm_WF a b n :
WF_Perm n (swap_perm a b n).
Proof.
intros k Hk.
unfold swap_perm.
bdestructΩ'.
Qed.
Lemma swap_perm_bounded a b n : a < n -> b < n ->
perm_bounded n (swap_perm a b n).
Proof.
intros Ha Hb k Hk.
unfold swap_perm.
bdestructΩ'.
Qed.
Lemma swap_perm_invol a b n : a < n -> b < n ->
(swap_perm a b n) ∘ (swap_perm a b n) = idn.
Proof.
intros Ha Hb.
unfold compose.
apply functional_extensionality; intros k.
unfold swap_perm.
bdestructΩ'.
Qed.
#[export] Hint Rewrite swap_perm_same : perm_cleanup_db.
#[export] Hint Resolve swap_perm_WF : WF_Perm_db.
#[export] Hint Resolve swap_perm_bounded : perm_bounded_db.
#[export] Hint Rewrite swap_perm_invol using lia : perm_inv_db.
Lemma swap_perm_big a b n : n <= a -> n <= b ->
perm_eq n (swap_perm a b n) idn.
Proof.
intros Ha Hb k Hk.
unfold swap_perm.
bdestructΩ'.
Qed.
#[export] Hint Rewrite swap_perm_big using lia : perm_cleanup_db.
Lemma swap_perm_big_eq a b n :
n <= a -> n <= b ->
swap_perm a b n = idn.
Proof.
intros.
eq_by_WF_perm_eq n.
cleanup_perm.
Qed.
Lemma swap_perm_permutation a b n : a < n -> b < n ->
permutation n (swap_perm a b n).
Proof.
intros Ha Hb.
perm_by_inverse (swap_perm a b n).
Qed.
Lemma swap_perm_S_permutation a n (Ha : S a < n) :
permutation n (swap_perm a (S a) n).