Debian Codegraph @

src/Util/ListUtil/Permutation.v

raw
1 From Coq Require Import NArith.
2 From Coq Require Import Arith.
3 From Coq Require Import ZArith.
4 From Coq Require Import List.
5 From Coq Require Import Permutation.
6 From Coq Require Import Setoid.
7 From Coq Require Import Morphisms.
8 Require Import Crypto.Util.Tactics.BreakMatch.
9 Import ListNotations.
10
11 Lemma Permutation_partition {A} (l : list A) f :
12 Permutation.Permutation l (fst (partition f l) ++ snd (partition f l)).
13 Proof using Type.
14 induction l; cbn; break_match; cbn in *; eauto.
15 etransitivity. econstructor. eassumption.
16 eapply Permutation.Permutation_middle.
17 Qed.
18
19 Global Instance fold_right_Proper_commutative_associative_Permutation {A op}
20 (Hcomm : forall x y, op x y = op y x)
21 (Hassoc : forall x y z, op x (op y z) = op (op x y) z)
22 : Proper (eq ==> @Permutation A ==> eq) (fold_right op) | 1000.
23 Proof using Type.
24 intros init init' <- xs ys; induction 1; cbn;
25 rewrite ?Hassoc;
26 repeat match goal with
27 | [ |- context[op ?x ?y] ]
28 => lazymatch goal with
29 | [ |- context[op y x] ]
30 => rewrite (Hcomm x y)
31 end
32 end;
33 try reflexivity;
34 try now repeat match goal with H : _ |- _ => rewrite H; clear H end.
35 Qed.
36
37 Module Nat.
38 Global Instance fold_right_Proper_Permutation_add
39 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Nat.add) | 100.
40 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Nat.add_comm | hnf; apply Nat.add_assoc ]. Qed.
41
42 Global Instance fold_right_Proper_Permutation_mul
43 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Nat.mul) | 100.
44 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Nat.mul_comm | hnf; apply Nat.mul_assoc ]. Qed.
45
46 Global Instance fold_right_Proper_Permutation_land
47 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Nat.land) | 100.
48 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Nat.land_comm | hnf; apply Nat.land_assoc ]. Qed.
49
50 Global Instance fold_right_Proper_Permutation_lor
51 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Nat.lor) | 100.
52 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Nat.lor_comm | hnf; apply Nat.lor_assoc ]. Qed.
53
54 Global Instance fold_right_Proper_Permutation_lxor
55 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Nat.lxor) | 100.
56 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Nat.lxor_comm | hnf; intros; symmetry; apply Nat.lxor_assoc ]. Qed.
57 End Nat.
58
59 Module N.
60 Global Instance fold_right_Proper_Permutation_add
61 : Proper (eq ==> @Permutation _ ==> eq) (fold_right N.add) | 100.
62 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply N.add_comm | hnf; apply N.add_assoc ]. Qed.
63
64 Global Instance fold_right_Proper_Permutation_mul
65 : Proper (eq ==> @Permutation _ ==> eq) (fold_right N.mul) | 100.
66 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply N.mul_comm | hnf; apply N.mul_assoc ]. Qed.
67
68 Global Instance fold_right_Proper_Permutation_land
69 : Proper (eq ==> @Permutation _ ==> eq) (fold_right N.land) | 100.
70 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply N.land_comm | hnf; apply N.land_assoc ]. Qed.
71
72 Global Instance fold_right_Proper_Permutation_lor
73 : Proper (eq ==> @Permutation _ ==> eq) (fold_right N.lor) | 100.
74 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply N.lor_comm | hnf; apply N.lor_assoc ]. Qed.
75
76 Global Instance fold_right_Proper_Permutation_lxor
77 : Proper (eq ==> @Permutation _ ==> eq) (fold_right N.lxor) | 100.
78 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply N.lxor_comm | hnf; intros; symmetry; apply N.lxor_assoc ]. Qed.
79 End N.
80
81 Module Z.
82 Global Instance fold_right_Proper_Permutation_add
83 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Z.add) | 100.
84 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Z.add_comm | hnf; apply Z.add_assoc ]. Qed.
85
86 Global Instance fold_right_Proper_Permutation_mul
87 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Z.mul) | 100.
88 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Z.mul_comm | hnf; apply Z.mul_assoc ]. Qed.
89
90 Global Instance fold_right_Proper_Permutation_land
91 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Z.land) | 100.
92 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Z.land_comm | hnf; apply Z.land_assoc ]. Qed.
93
94 Global Instance fold_right_Proper_Permutation_lor
95 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Z.lor) | 100.
96 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Z.lor_comm | hnf; apply Z.lor_assoc ]. Qed.
97
98 Global Instance fold_right_Proper_Permutation_lxor
99 : Proper (eq ==> @Permutation _ ==> eq) (fold_right Z.lxor) | 100.
100 Proof using Type. apply fold_right_Proper_commutative_associative_Permutation; [ hnf; apply Z.lxor_comm | hnf; intros; symmetry; apply Z.lxor_assoc ]. Qed.
101 End Z.