| author | wenzelm | 
| Tue, 04 Apr 2017 22:16:42 +0200 | |
| changeset 65382 | de848ac5e0d7 | 
| parent 65342 | e32eb488c3a3 | 
| child 65552 | f533820e7248 | 
| permissions | -rw-r--r-- | 
| 41959 | 1 | (* Title: HOL/Library/Permutations.thy | 
| 2 | Author: Amine Chaieb, University of Cambridge | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 3 | *) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 4 | |
| 60500 | 5 | section \<open>Permutations, both general and specifically on finite sets.\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 6 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 7 | theory Permutations | 
| 65342 | 8 | imports Binomial Multiset Disjoint_Sets | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 9 | begin | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 10 | |
| 60500 | 11 | subsection \<open>Transpositions\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 12 | |
| 65342 | 13 | lemma swap_id_idempotent [simp]: "Fun.swap a b id \<circ> Fun.swap a b id = id" | 
| 14 | by (rule ext) (auto simp add: Fun.swap_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 15 | |
| 65342 | 16 | lemma inv_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id" | 
| 54681 | 17 | by (rule inv_unique_comp) simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 18 | |
| 65342 | 19 | lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)" | 
| 56545 | 20 | by (simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 21 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 22 | lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y" | 
| 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 23 | using surj_f_inv_f[of p] by (auto simp add: bij_def) | 
| 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 24 | |
| 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 25 | lemma bij_swap_comp: | 
| 65342 | 26 | assumes "bij p" | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 27 | shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p" | 
| 65342 | 28 | using surj_f_inv_f[OF bij_is_surj[OF \<open>bij p\<close>]] | 
| 29 | by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF \<open>bij p\<close>]) | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 30 | |
| 65342 | 31 | lemma bij_swap_compose_bij: | 
| 32 | assumes "bij p" | |
| 33 | shows "bij (Fun.swap a b id \<circ> p)" | |
| 34 | by (simp only: bij_swap_comp[OF \<open>bij p\<close>] bij_swap_iff \<open>bij p\<close>) | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 35 | |
| 54681 | 36 | |
| 60500 | 37 | subsection \<open>Basic consequences of the definition\<close> | 
| 54681 | 38 | |
| 39 | definition permutes (infixr "permutes" 41) | |
| 40 | where "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 41 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 42 | lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 43 | unfolding permutes_def by metis | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 44 | |
| 65342 | 45 | lemma permutes_not_in: "f permutes S \<Longrightarrow> x \<notin> S \<Longrightarrow> f x = x" | 
| 46 | by (auto simp: permutes_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 47 | |
| 54681 | 48 | lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S" | 
| 30488 | 49 | unfolding permutes_def | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 50 | apply (rule set_eqI) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 51 | apply (simp add: image_iff) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 52 | apply metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 53 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 54 | |
| 54681 | 55 | lemma permutes_inj: "p permutes S \<Longrightarrow> inj p" | 
| 64966 
d53d7ca3303e
added inj_def (redundant, analogous to surj_def, bij_def);
 wenzelm parents: 
64543diff
changeset | 56 | unfolding permutes_def inj_def by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 57 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 58 | lemma permutes_inj_on: "f permutes S \<Longrightarrow> inj_on f A" | 
| 65342 | 59 | by (auto simp: permutes_def inj_on_def) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 60 | |
| 54681 | 61 | lemma permutes_surj: "p permutes s \<Longrightarrow> surj p" | 
| 30488 | 62 | unfolding permutes_def surj_def by metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 63 | |
| 60601 | 64 | lemma permutes_bij: "p permutes s \<Longrightarrow> bij p" | 
| 65342 | 65 | unfolding bij_def by (metis permutes_inj permutes_surj) | 
| 60601 | 66 | |
| 59474 | 67 | lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S" | 
| 65342 | 68 | by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI) | 
| 59669 
de7792ea4090
renaming HOL/Fact.thy -> Binomial.thy
 paulson <lp15@cam.ac.uk> parents: 
59474diff
changeset | 69 | |
| 59474 | 70 | lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S" | 
| 71 | unfolding permutes_def bij_betw_def inj_on_def | |
| 72 | by auto (metis image_iff)+ | |
| 73 | ||
| 54681 | 74 | lemma permutes_inv_o: | 
| 65342 | 75 | assumes permutes: "p permutes S" | 
| 54681 | 76 | shows "p \<circ> inv p = id" | 
| 77 | and "inv p \<circ> p = id" | |
| 65342 | 78 | using permutes_inj[OF permutes] permutes_surj[OF permutes] | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 79 | unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+ | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 80 | |
| 30488 | 81 | lemma permutes_inverses: | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 82 | fixes p :: "'a \<Rightarrow> 'a" | 
| 65342 | 83 | assumes permutes: "p permutes S" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 84 | shows "p (inv p x) = x" | 
| 54681 | 85 | and "inv p (p x) = x" | 
| 65342 | 86 | using permutes_inv_o[OF permutes, unfolded fun_eq_iff o_def] by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 87 | |
| 54681 | 88 | lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 89 | unfolding permutes_def by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 90 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 91 | lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
 | 
| 65342 | 92 | by (auto simp add: fun_eq_iff permutes_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 93 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 94 | lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
 | 
| 65342 | 95 | by (simp add: fun_eq_iff permutes_def) metis (*somewhat slow*) | 
| 30488 | 96 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 97 | lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)" | 
| 65342 | 98 | by (simp add: permutes_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 99 | |
| 54681 | 100 | lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y" | 
| 101 | unfolding permutes_def inv_def | |
| 102 | apply auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 103 | apply (erule allE[where x=y]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 104 | apply (erule allE[where x=y]) | 
| 54681 | 105 | apply (rule someI_ex) | 
| 106 | apply blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 107 | apply (rule some1_equality) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 108 | apply blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 109 | apply blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 110 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 111 | |
| 54681 | 112 | lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S" | 
| 56545 | 113 | unfolding permutes_def Fun.swap_def fun_upd_def by auto metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 114 | |
| 54681 | 115 | lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T" | 
| 116 | by (simp add: Ball_def permutes_def) metis | |
| 117 | ||
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 118 | (* Next three lemmas contributed by Lukas Bulwahn *) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 119 | lemma permutes_bij_inv_into: | 
| 65342 | 120 | fixes A :: "'a set" | 
| 121 | and B :: "'b set" | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 122 | assumes "p permutes A" | 
| 65342 | 123 | and "bij_betw f A B" | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 124 | shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 125 | proof (rule bij_imp_permutes) | 
| 65342 | 126 | from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A" | 
| 127 | by (auto simp add: permutes_imp_bij bij_betw_inv_into) | |
| 128 | then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B" | |
| 129 | by (simp add: bij_betw_trans) | |
| 130 | then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B" | |
| 131 | by (subst bij_betw_cong[where g="f \<circ> p \<circ> inv_into A f"]) auto | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 132 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 133 | fix x | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 134 | assume "x \<notin> B" | 
| 65342 | 135 | then show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 136 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 137 | |
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 138 | lemma permutes_image_mset: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 139 | assumes "p permutes A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 140 | shows "image_mset p (mset_set A) = mset_set A" | 
| 65342 | 141 | using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image) | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 142 | |
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 143 | lemma permutes_implies_image_mset_eq: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 144 | assumes "p permutes A" "\<And>x. x \<in> A \<Longrightarrow> f x = f' (p x)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 145 | shows "image_mset f' (mset_set A) = image_mset f (mset_set A)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 146 | proof - | 
| 65342 | 147 | have "f x = f' (p x)" if "x \<in># mset_set A" for x | 
| 148 | using assms(2)[of x] that by (cases "finite A") auto | |
| 149 | with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)" | |
| 150 | by (auto intro!: image_mset_cong) | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 151 | also have "\<dots> = image_mset f' (image_mset p (mset_set A))" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 152 | by (simp add: image_mset.compositionality) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 153 | also have "\<dots> = image_mset f' (mset_set A)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 154 | proof - | 
| 65342 | 155 | from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A" | 
| 156 | by blast | |
| 157 | then show ?thesis by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 158 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 159 | finally show ?thesis .. | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 160 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 161 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 162 | |
| 60500 | 163 | subsection \<open>Group properties\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 164 | |
| 54681 | 165 | lemma permutes_id: "id permutes S" | 
| 65342 | 166 | by (simp add: permutes_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 167 | |
| 54681 | 168 | lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 169 | unfolding permutes_def o_def by metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 170 | |
| 54681 | 171 | lemma permutes_inv: | 
| 65342 | 172 | assumes "p permutes S" | 
| 54681 | 173 | shows "inv p permutes S" | 
| 65342 | 174 | using assms unfolding permutes_def permutes_inv_eq[OF assms] by metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 175 | |
| 54681 | 176 | lemma permutes_inv_inv: | 
| 65342 | 177 | assumes "p permutes S" | 
| 54681 | 178 | shows "inv (inv p) = p" | 
| 65342 | 179 | unfolding fun_eq_iff permutes_inv_eq[OF assms] permutes_inv_eq[OF permutes_inv[OF assms]] | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 180 | by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 181 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 182 | lemma permutes_invI: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 183 | assumes perm: "p permutes S" | 
| 65342 | 184 | and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x" | 
| 185 | and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x" | |
| 186 | shows "inv p = p'" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 187 | proof | 
| 65342 | 188 | show "inv p x = p' x" for x | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 189 | proof (cases "x \<in> S") | 
| 65342 | 190 | case True | 
| 191 | from assms have "p' x = p' (p (inv p x))" | |
| 192 | by (simp add: permutes_inverses) | |
| 193 | also from permutes_inv[OF perm] True have "\<dots> = inv p x" | |
| 194 | by (subst inv) (simp_all add: permutes_in_image) | |
| 195 | finally show ?thesis .. | |
| 196 | next | |
| 197 | case False | |
| 198 | with permutes_inv[OF perm] show ?thesis | |
| 199 | by (simp_all add: outside permutes_not_in) | |
| 200 | qed | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 201 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 202 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 203 | lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A" | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 204 | by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv]) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 205 | |
| 54681 | 206 | |
| 60500 | 207 | subsection \<open>The number of permutations on a finite set\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 208 | |
| 30488 | 209 | lemma permutes_insert_lemma: | 
| 65342 | 210 | assumes "p permutes (insert a S)" | 
| 54681 | 211 | shows "Fun.swap a (p a) id \<circ> p permutes S" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 212 | apply (rule permutes_superset[where S = "insert a S"]) | 
| 65342 | 213 | apply (rule permutes_compose[OF assms]) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 214 | apply (rule permutes_swap_id, simp) | 
| 65342 | 215 | using permutes_in_image[OF assms, of a] | 
| 54681 | 216 | apply simp | 
| 56545 | 217 | apply (auto simp add: Ball_def Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 218 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 219 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 220 | lemma permutes_insert: "{p. p permutes (insert a S)} =
 | 
| 65342 | 221 |   (\<lambda>(b, p). Fun.swap a b id \<circ> p) ` {(b, p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
 | 
| 54681 | 222 | proof - | 
| 65342 | 223 | have "p permutes insert a S \<longleftrightarrow> | 
| 224 | (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p | |
| 225 | proof - | |
| 226 | have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S" | |
| 227 | if p: "p permutes insert a S" | |
| 228 | proof - | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 229 | let ?b = "p a" | 
| 54681 | 230 | let ?q = "Fun.swap a (p a) id \<circ> p" | 
| 65342 | 231 | have *: "p = Fun.swap a ?b id \<circ> ?q" | 
| 232 | by (simp add: fun_eq_iff o_assoc) | |
| 233 | have **: "?b \<in> insert a S" | |
| 234 | unfolding permutes_in_image[OF p] by simp | |
| 235 | from permutes_insert_lemma[OF p] * ** show ?thesis | |
| 236 | by blast | |
| 237 | qed | |
| 238 | moreover have "p permutes insert a S" | |
| 239 | if bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" for b q | |
| 240 | proof - | |
| 241 | from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S" | |
| 54681 | 242 | by auto | 
| 65342 | 243 | have a: "a \<in> insert a S" | 
| 54681 | 244 | by simp | 
| 65342 | 245 | from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis | 
| 54681 | 246 | by simp | 
| 65342 | 247 | qed | 
| 248 | ultimately show ?thesis by blast | |
| 249 | qed | |
| 250 | then show ?thesis by auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 251 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 252 | |
| 54681 | 253 | lemma card_permutations: | 
| 65342 | 254 | assumes "card S = n" | 
| 255 | and "finite S" | |
| 33715 | 256 |   shows "card {p. p permutes S} = fact n"
 | 
| 65342 | 257 | using assms(2,1) | 
| 54681 | 258 | proof (induct arbitrary: n) | 
| 259 | case empty | |
| 260 | then show ?case by simp | |
| 33715 | 261 | next | 
| 262 | case (insert x F) | |
| 54681 | 263 |   {
 | 
| 264 | fix n | |
| 65342 | 265 | assume card_insert: "card (insert x F) = n" | 
| 33715 | 266 |     let ?xF = "{p. p permutes insert x F}"
 | 
| 267 |     let ?pF = "{p. p permutes F}"
 | |
| 268 |     let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
 | |
| 269 | let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)" | |
| 65342 | 270 | have xfgpF': "?xF = ?g ` ?pF'" | 
| 271 | by (rule permutes_insert[of x F]) | |
| 272 | from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1" | |
| 273 | by auto | |
| 274 | from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)" | |
| 275 | by auto | |
| 54681 | 276 | then have "finite ?pF" | 
| 59730 
b7c394c7a619
The factorial function, "fact", now has type "nat => 'a"
 paulson <lp15@cam.ac.uk> parents: 
59669diff
changeset | 277 | by (auto intro: card_ge_0_finite) | 
| 65342 | 278 | with \<open>finite F\<close> card_insert have pF'f: "finite ?pF'" | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
60601diff
changeset | 279 | apply (simp only: Collect_case_prod Collect_mem_eq) | 
| 33715 | 280 | apply (rule finite_cartesian_product) | 
| 281 | apply simp_all | |
| 282 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 283 | |
| 33715 | 284 | have ginj: "inj_on ?g ?pF'" | 
| 54681 | 285 | proof - | 
| 33715 | 286 |       {
 | 
| 54681 | 287 | fix b p c q | 
| 65342 | 288 | assume bp: "(b, p) \<in> ?pF'" | 
| 289 | assume cq: "(c, q) \<in> ?pF'" | |
| 290 | assume eq: "?g (b, p) = ?g (c, q)" | |
| 291 | from bp cq have pF: "p permutes F" and qF: "q permutes F" | |
| 54681 | 292 | by auto | 
| 65342 | 293 | from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x" | 
| 294 | by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff) | |
| 295 | also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x" | |
| 296 | by (auto simp: swap_def fun_upd_def fun_eq_iff) | |
| 297 | also from qF \<open>x \<notin> F\<close> have "\<dots> = c" | |
| 298 | by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff) | |
| 299 | finally have "b = c" . | |
| 54681 | 300 | then have "Fun.swap x b id = Fun.swap x c id" | 
| 301 | by simp | |
| 302 | with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q" | |
| 303 | by simp | |
| 65342 | 304 | then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)" | 
| 54681 | 305 | by simp | 
| 306 | then have "p = q" | |
| 307 | by (simp add: o_assoc) | |
| 65342 | 308 | with \<open>b = c\<close> have "(b, p) = (c, q)" | 
| 54681 | 309 | by simp | 
| 33715 | 310 | } | 
| 54681 | 311 | then show ?thesis | 
| 312 | unfolding inj_on_def by blast | |
| 33715 | 313 | qed | 
| 65342 | 314 | from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0" | 
| 315 | by auto | |
| 54681 | 316 | then have "\<exists>m. n = Suc m" | 
| 317 | by presburger | |
| 65342 | 318 | then obtain m where n: "n = Suc m" | 
| 54681 | 319 | by blast | 
| 65342 | 320 | from pFs card_insert have *: "card ?xF = fact n" | 
| 54681 | 321 | unfolding xfgpF' card_image[OF ginj] | 
| 60500 | 322 | using \<open>finite F\<close> \<open>finite ?pF\<close> | 
| 65342 | 323 | by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n) | 
| 54681 | 324 | from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF" | 
| 65342 | 325 | by (simp add: xfgpF' n) | 
| 326 | from * have "card ?xF = fact n" | |
| 327 | unfolding xFf by blast | |
| 33715 | 328 | } | 
| 65342 | 329 | with insert show ?case by simp | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 330 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 331 | |
| 54681 | 332 | lemma finite_permutations: | 
| 65342 | 333 | assumes "finite S" | 
| 54681 | 334 |   shows "finite {p. p permutes S}"
 | 
| 65342 | 335 | using card_permutations[OF refl assms] by (auto intro: card_ge_0_finite) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 336 | |
| 54681 | 337 | |
| 60500 | 338 | subsection \<open>Permutations of index set for iterated operations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 339 | |
| 51489 | 340 | lemma (in comm_monoid_set) permute: | 
| 341 | assumes "p permutes S" | |
| 54681 | 342 | shows "F g S = F (g \<circ> p) S" | 
| 51489 | 343 | proof - | 
| 60500 | 344 | from \<open>p permutes S\<close> have "inj p" | 
| 54681 | 345 | by (rule permutes_inj) | 
| 346 | then have "inj_on p S" | |
| 347 | by (auto intro: subset_inj_on) | |
| 348 | then have "F g (p ` S) = F (g \<circ> p) S" | |
| 349 | by (rule reindex) | |
| 60500 | 350 | moreover from \<open>p permutes S\<close> have "p ` S = S" | 
| 54681 | 351 | by (rule permutes_image) | 
| 352 | ultimately show ?thesis | |
| 353 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 354 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 355 | |
| 54681 | 356 | |
| 60500 | 357 | subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close> | 
| 54681 | 358 | |
| 359 | lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow> | |
| 360 | Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id" | |
| 56545 | 361 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 362 | |
| 54681 | 363 | lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow> | 
| 364 | Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id" | |
| 56545 | 365 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 366 | |
| 54681 | 367 | lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow> | 
| 368 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id" | |
| 56545 | 369 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 370 | |
| 54681 | 371 | |
| 60500 | 372 | subsection \<open>Permutations as transposition sequences\<close> | 
| 54681 | 373 | |
| 374 | inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
 | |
| 65342 | 375 | where | 
| 376 | id[simp]: "swapidseq 0 id" | |
| 377 | | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)" | |
| 54681 | 378 | |
| 379 | declare id[unfolded id_def, simp] | |
| 380 | ||
| 381 | definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 382 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 383 | |
| 60500 | 384 | subsection \<open>Some closure properties of the set of permutations, with lengths\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 385 | |
| 54681 | 386 | lemma permutation_id[simp]: "permutation id" | 
| 387 | unfolding permutation_def by (rule exI[where x=0]) simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 388 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 389 | declare permutation_id[unfolded id_def, simp] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 390 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 391 | lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 392 | apply clarsimp | 
| 54681 | 393 | using comp_Suc[of 0 id a b] | 
| 394 | apply simp | |
| 395 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 396 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 397 | lemma permutation_swap_id: "permutation (Fun.swap a b id)" | 
| 65342 | 398 | proof (cases "a = b") | 
| 399 | case True | |
| 400 | then show ?thesis by simp | |
| 401 | next | |
| 402 | case False | |
| 403 | then show ?thesis | |
| 404 | unfolding permutation_def | |
| 405 | using swapidseq_swap[of a b] by blast | |
| 406 | qed | |
| 407 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 408 | |
| 54681 | 409 | lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)" | 
| 410 | proof (induct n p arbitrary: m q rule: swapidseq.induct) | |
| 411 | case (id m q) | |
| 412 | then show ?case by simp | |
| 413 | next | |
| 414 | case (comp_Suc n p a b m q) | |
| 65342 | 415 | have eq: "Suc n + m = Suc (n + m)" | 
| 54681 | 416 | by arith | 
| 417 | show ?case | |
| 65342 | 418 | apply (simp only: eq comp_assoc) | 
| 54681 | 419 | apply (rule swapidseq.comp_Suc) | 
| 420 | using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3) | |
| 65342 | 421 | apply blast+ | 
| 54681 | 422 | done | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 423 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 424 | |
| 54681 | 425 | lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 426 | unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 427 | |
| 54681 | 428 | lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)" | 
| 65342 | 429 | by (induct n p rule: swapidseq.induct) | 
| 430 | (use swapidseq_swap[of a b] in \<open>auto simp add: comp_assoc intro: swapidseq.comp_Suc\<close>) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 431 | |
| 54681 | 432 | lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id" | 
| 433 | proof (induct n p rule: swapidseq.induct) | |
| 434 | case id | |
| 435 | then show ?case | |
| 436 | by (rule exI[where x=id]) simp | |
| 30488 | 437 | next | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 438 | case (comp_Suc n p a b) | 
| 54681 | 439 | from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 440 | by blast | |
| 441 | let ?q = "q \<circ> Fun.swap a b id" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 442 | note H = comp_Suc.hyps | 
| 65342 | 443 | from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (Fun.swap a b id)" | 
| 54681 | 444 | by simp | 
| 65342 | 445 | from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q" | 
| 54681 | 446 | by simp | 
| 447 | have "Fun.swap a b id \<circ> p \<circ> ?q = Fun.swap a b id \<circ> (p \<circ> q) \<circ> Fun.swap a b id" | |
| 448 | by (simp add: o_assoc) | |
| 449 | also have "\<dots> = id" | |
| 450 | by (simp add: q(2)) | |
| 65342 | 451 | finally have ***: "Fun.swap a b id \<circ> p \<circ> ?q = id" . | 
| 54681 | 452 | have "?q \<circ> (Fun.swap a b id \<circ> p) = q \<circ> (Fun.swap a b id \<circ> Fun.swap a b id) \<circ> p" | 
| 453 | by (simp only: o_assoc) | |
| 454 | then have "?q \<circ> (Fun.swap a b id \<circ> p) = id" | |
| 455 | by (simp add: q(3)) | |
| 65342 | 456 | with ** *** show ?case | 
| 54681 | 457 | by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 458 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 459 | |
| 54681 | 460 | lemma swapidseq_inverse: | 
| 65342 | 461 | assumes "swapidseq n p" | 
| 54681 | 462 | shows "swapidseq n (inv p)" | 
| 65342 | 463 | using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto | 
| 54681 | 464 | |
| 465 | lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)" | |
| 466 | using permutation_def swapidseq_inverse by blast | |
| 467 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 468 | |
| 60500 | 469 | subsection \<open>The identity map only has even transposition sequences\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 470 | |
| 54681 | 471 | lemma symmetry_lemma: | 
| 472 | assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c" | |
| 473 | and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> | |
| 474 | a = c \<and> b = d \<or> a = c \<and> b \<noteq> d \<or> a \<noteq> c \<and> b = d \<or> a \<noteq> c \<and> a \<noteq> d \<and> b \<noteq> c \<and> b \<noteq> d \<Longrightarrow> | |
| 475 | P a b c d" | |
| 476 | shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow> P a b c d" | |
| 477 | using assms by metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 478 | |
| 54681 | 479 | lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> | 
| 480 | Fun.swap a b id \<circ> Fun.swap c d id = id \<or> | |
| 481 | (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> | |
| 482 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)" | |
| 483 | proof - | |
| 65342 | 484 | assume neq: "a \<noteq> b" "c \<noteq> d" | 
| 54681 | 485 | have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow> | 
| 486 | (Fun.swap a b id \<circ> Fun.swap c d id = id \<or> | |
| 487 | (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> | |
| 488 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))" | |
| 489 | apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d]) | |
| 65342 | 490 | apply (simp_all only: swap_commute) | 
| 54681 | 491 | apply (case_tac "a = c \<and> b = d") | 
| 65342 | 492 | apply (clarsimp simp only: swap_commute swap_id_idempotent) | 
| 54681 | 493 | apply (case_tac "a = c \<and> b \<noteq> d") | 
| 65342 | 494 | apply (rule disjI2) | 
| 495 | apply (rule_tac x="b" in exI) | |
| 496 | apply (rule_tac x="d" in exI) | |
| 497 | apply (rule_tac x="b" in exI) | |
| 498 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | |
| 54681 | 499 | apply (case_tac "a \<noteq> c \<and> b = d") | 
| 65342 | 500 | apply (rule disjI2) | 
| 501 | apply (rule_tac x="c" in exI) | |
| 502 | apply (rule_tac x="d" in exI) | |
| 503 | apply (rule_tac x="c" in exI) | |
| 504 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | |
| 54681 | 505 | apply (rule disjI2) | 
| 506 | apply (rule_tac x="c" in exI) | |
| 507 | apply (rule_tac x="d" in exI) | |
| 508 | apply (rule_tac x="b" in exI) | |
| 56545 | 509 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | 
| 54681 | 510 | done | 
| 65342 | 511 | with neq show ?thesis by metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 512 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 513 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 514 | lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id" | 
| 65342 | 515 | using swapidseq.cases[of 0 p "p = id"] by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 516 | |
| 54681 | 517 | lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow> | 
| 65342 | 518 | n = 0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = Fun.swap a b id \<circ> q \<and> swapidseq m q \<and> a \<noteq> b)" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 519 | apply (rule iffI) | 
| 65342 | 520 | apply (erule swapidseq.cases[of n p]) | 
| 521 | apply simp | |
| 522 | apply (rule disjI2) | |
| 523 | apply (rule_tac x= "a" in exI) | |
| 524 | apply (rule_tac x= "b" in exI) | |
| 525 | apply (rule_tac x= "pa" in exI) | |
| 526 | apply (rule_tac x= "na" in exI) | |
| 527 | apply simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 528 | apply auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 529 | apply (rule comp_Suc, simp_all) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 530 | done | 
| 54681 | 531 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 532 | lemma fixing_swapidseq_decrease: | 
| 65342 | 533 | assumes "swapidseq n p" | 
| 534 | and "a \<noteq> b" | |
| 535 | and "(Fun.swap a b id \<circ> p) a = a" | |
| 54681 | 536 | shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)" | 
| 65342 | 537 | using assms | 
| 54681 | 538 | proof (induct n arbitrary: p a b) | 
| 539 | case 0 | |
| 540 | then show ?case | |
| 56545 | 541 | by (auto simp add: Fun.swap_def fun_upd_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 542 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 543 | case (Suc n p a b) | 
| 54681 | 544 | from Suc.prems(1) swapidseq_cases[of "Suc n" p] | 
| 545 | obtain c d q m where | |
| 546 | cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 547 | by auto | 
| 65342 | 548 | consider "Fun.swap a b id \<circ> Fun.swap c d id = id" | 
| 549 | | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y" | |
| 54681 | 550 | "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id" | 
| 65342 | 551 | using swap_general[OF Suc.prems(2) cdqm(4)] by metis | 
| 552 | then show ?case | |
| 553 | proof cases | |
| 554 | case 1 | |
| 555 | then show ?thesis | |
| 556 | by (simp only: cdqm o_assoc) (simp add: cdqm) | |
| 557 | next | |
| 558 | case prems: 2 | |
| 559 | then have az: "a \<noteq> z" | |
| 54681 | 560 | by simp | 
| 65342 | 561 | from prems have *: "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" for h | 
| 562 | by (simp add: Fun.swap_def) | |
| 54681 | 563 | from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)" | 
| 564 | by simp | |
| 565 | then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)" | |
| 65342 | 566 | by (simp add: o_assoc prems) | 
| 54681 | 567 | then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a" | 
| 568 | by simp | |
| 569 | then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a" | |
| 570 | unfolding Suc by metis | |
| 65342 | 571 | then have "(Fun.swap a z id \<circ> q) a = a" | 
| 572 | by (simp only: *) | |
| 573 | from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az this] | |
| 574 | have **: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0" | |
| 54681 | 575 | by blast+ | 
| 65342 | 576 | from \<open>n \<noteq> 0\<close> have ***: "Suc n - 1 = Suc (n - 1)" | 
| 577 | by auto | |
| 578 | show ?thesis | |
| 579 | apply (simp only: cdqm(2) prems o_assoc ***) | |
| 49739 | 580 | apply (simp only: Suc_not_Zero simp_thms comp_assoc) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 581 | apply (rule comp_Suc) | 
| 65342 | 582 | using ** prems | 
| 583 | apply blast+ | |
| 54681 | 584 | done | 
| 65342 | 585 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 586 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 587 | |
| 30488 | 588 | lemma swapidseq_identity_even: | 
| 54681 | 589 | assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)" | 
| 590 | shows "even n" | |
| 60500 | 591 | using \<open>swapidseq n id\<close> | 
| 54681 | 592 | proof (induct n rule: nat_less_induct) | 
| 65342 | 593 | case H: (1 n) | 
| 594 | consider "n = 0" | |
| 595 | | a b :: 'a and q m where "n = Suc m" "id = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b" | |
| 596 | using H(2)[unfolded swapidseq_cases[of n id]] by auto | |
| 597 | then show ?case | |
| 598 | proof cases | |
| 599 | case 1 | |
| 600 | then show ?thesis by presburger | |
| 601 | next | |
| 602 | case h: 2 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 603 | from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]] | 
| 54681 | 604 | have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)" | 
| 605 | by auto | |
| 606 | from h m have mn: "m - 1 < n" | |
| 607 | by arith | |
| 65342 | 608 | from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis | 
| 54681 | 609 | by presburger | 
| 65342 | 610 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 611 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 612 | |
| 54681 | 613 | |
| 60500 | 614 | subsection \<open>Therefore we have a welldefined notion of parity\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 615 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 616 | definition "evenperm p = even (SOME n. swapidseq n p)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 617 | |
| 54681 | 618 | lemma swapidseq_even_even: | 
| 619 | assumes m: "swapidseq m p" | |
| 620 | and n: "swapidseq n p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 621 | shows "even m \<longleftrightarrow> even n" | 
| 54681 | 622 | proof - | 
| 65342 | 623 | from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 54681 | 624 | by blast | 
| 65342 | 625 | from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis | 
| 54681 | 626 | by arith | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 627 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 628 | |
| 54681 | 629 | lemma evenperm_unique: | 
| 630 | assumes p: "swapidseq n p" | |
| 631 | and n:"even n = b" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 632 | shows "evenperm p = b" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 633 | unfolding n[symmetric] evenperm_def | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 634 | apply (rule swapidseq_even_even[where p = p]) | 
| 65342 | 635 | apply (rule someI[where x = n]) | 
| 54681 | 636 | using p | 
| 65342 | 637 | apply blast+ | 
| 54681 | 638 | done | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 639 | |
| 54681 | 640 | |
| 60500 | 641 | subsection \<open>And it has the expected composition properties\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 642 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 643 | lemma evenperm_id[simp]: "evenperm id = True" | 
| 54681 | 644 | by (rule evenperm_unique[where n = 0]) simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 645 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 646 | lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)" | 
| 54681 | 647 | by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 648 | |
| 30488 | 649 | lemma evenperm_comp: | 
| 65342 | 650 | assumes "permutation p" "permutation q" | 
| 651 | shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q" | |
| 54681 | 652 | proof - | 
| 65342 | 653 | from assms obtain n m where n: "swapidseq n p" and m: "swapidseq m q" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 654 | unfolding permutation_def by blast | 
| 65342 | 655 | have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)" | 
| 54681 | 656 | by arith | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 657 | from evenperm_unique[OF n refl] evenperm_unique[OF m refl] | 
| 65342 | 658 | and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis | 
| 54681 | 659 | by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 660 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 661 | |
| 54681 | 662 | lemma evenperm_inv: | 
| 65342 | 663 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 664 | shows "evenperm (inv p) = evenperm p" | 
| 54681 | 665 | proof - | 
| 65342 | 666 | from assms obtain n where n: "swapidseq n p" | 
| 54681 | 667 | unfolding permutation_def by blast | 
| 65342 | 668 | show ?thesis | 
| 669 | by (rule evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]]) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 670 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 671 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 672 | |
| 60500 | 673 | subsection \<open>A more abstract characterization of permutations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 674 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 675 | lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)" | 
| 64966 
d53d7ca3303e
added inj_def (redundant, analogous to surj_def, bij_def);
 wenzelm parents: 
64543diff
changeset | 676 | unfolding bij_def inj_def surj_def | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 677 | apply auto | 
| 65342 | 678 | apply metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 679 | apply metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 680 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 681 | |
| 30488 | 682 | lemma permutation_bijective: | 
| 65342 | 683 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 684 | shows "bij p" | 
| 54681 | 685 | proof - | 
| 65342 | 686 | from assms obtain n where n: "swapidseq n p" | 
| 54681 | 687 | unfolding permutation_def by blast | 
| 65342 | 688 | from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 54681 | 689 | by blast | 
| 65342 | 690 | then show ?thesis | 
| 691 | unfolding bij_iff | |
| 54681 | 692 | apply (auto simp add: fun_eq_iff) | 
| 693 | apply metis | |
| 694 | done | |
| 30488 | 695 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 696 | |
| 54681 | 697 | lemma permutation_finite_support: | 
| 65342 | 698 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 699 |   shows "finite {x. p x \<noteq> x}"
 | 
| 54681 | 700 | proof - | 
| 65342 | 701 | from assms obtain n where "swapidseq n p" | 
| 54681 | 702 | unfolding permutation_def by blast | 
| 65342 | 703 | then show ?thesis | 
| 54681 | 704 | proof (induct n p rule: swapidseq.induct) | 
| 705 | case id | |
| 706 | then show ?case by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 707 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 708 | case (comp_Suc n p a b) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 709 |     let ?S = "insert a (insert b {x. p x \<noteq> x})"
 | 
| 65342 | 710 | from comp_Suc.hyps(2) have *: "finite ?S" | 
| 54681 | 711 | by simp | 
| 65342 | 712 |     from \<open>a \<noteq> b\<close> have **: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
 | 
| 713 | by (auto simp: Fun.swap_def) | |
| 714 | show ?case | |
| 715 | by (rule finite_subset[OF ** *]) | |
| 54681 | 716 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 717 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 718 | |
| 30488 | 719 | lemma permutation_lemma: | 
| 65342 | 720 | assumes "finite S" | 
| 721 | and "bij p" | |
| 722 | and "\<forall>x. x\<notin> S \<longrightarrow> p x = x" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 723 | shows "permutation p" | 
| 65342 | 724 | using assms | 
| 54681 | 725 | proof (induct S arbitrary: p rule: finite_induct) | 
| 65342 | 726 | case empty | 
| 727 | then show ?case | |
| 728 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 729 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 730 | case (insert a F p) | 
| 54681 | 731 | let ?r = "Fun.swap a (p a) id \<circ> p" | 
| 732 | let ?q = "Fun.swap a (p a) id \<circ> ?r" | |
| 65342 | 733 | have *: "?r a = a" | 
| 56545 | 734 | by (simp add: Fun.swap_def) | 
| 65342 | 735 | from insert * have **: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x" | 
| 64966 
d53d7ca3303e
added inj_def (redundant, analogous to surj_def, bij_def);
 wenzelm parents: 
64543diff
changeset | 736 | by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3)) | 
| 65342 | 737 | have "bij ?r" | 
| 738 | by (rule bij_swap_compose_bij[OF insert(4)]) | |
| 739 | have "permutation ?r" | |
| 740 | by (rule insert(3)[OF \<open>bij ?r\<close> **]) | |
| 741 | then have "permutation ?q" | |
| 742 | by (simp add: permutation_compose permutation_swap_id) | |
| 54681 | 743 | then show ?case | 
| 744 | by (simp add: o_assoc) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 745 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 746 | |
| 30488 | 747 | lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 748 | (is "?lhs \<longleftrightarrow> ?b \<and> ?f") | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 749 | proof | 
| 65342 | 750 | assume ?lhs | 
| 751 | with permutation_bijective permutation_finite_support show "?b \<and> ?f" | |
| 54681 | 752 | by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 753 | next | 
| 54681 | 754 | assume "?b \<and> ?f" | 
| 755 | then have "?f" "?b" by blast+ | |
| 756 | from permutation_lemma[OF this] show ?lhs | |
| 757 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 758 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 759 | |
| 54681 | 760 | lemma permutation_inverse_works: | 
| 65342 | 761 | assumes "permutation p" | 
| 54681 | 762 | shows "inv p \<circ> p = id" | 
| 763 | and "p \<circ> inv p = id" | |
| 65342 | 764 | using permutation_bijective [OF assms] by (auto simp: bij_def inj_iff surj_iff) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 765 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 766 | lemma permutation_inverse_compose: | 
| 54681 | 767 | assumes p: "permutation p" | 
| 768 | and q: "permutation q" | |
| 769 | shows "inv (p \<circ> q) = inv q \<circ> inv p" | |
| 770 | proof - | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 771 | note ps = permutation_inverse_works[OF p] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 772 | note qs = permutation_inverse_works[OF q] | 
| 54681 | 773 | have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p" | 
| 774 | by (simp add: o_assoc) | |
| 775 | also have "\<dots> = id" | |
| 776 | by (simp add: ps qs) | |
| 65342 | 777 | finally have *: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" . | 
| 54681 | 778 | have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q" | 
| 779 | by (simp add: o_assoc) | |
| 780 | also have "\<dots> = id" | |
| 781 | by (simp add: ps qs) | |
| 65342 | 782 | finally have **: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" . | 
| 783 | show ?thesis | |
| 784 | by (rule inv_unique_comp[OF * **]) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 785 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 786 | |
| 54681 | 787 | |
| 65342 | 788 | subsection \<open>Relation to \<open>permutes\<close>\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 789 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 790 | lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)" | 
| 54681 | 791 | unfolding permutation permutes_def bij_iff[symmetric] | 
| 792 | apply (rule iffI, clarify) | |
| 65342 | 793 |    apply (rule exI[where x="{x. p x \<noteq> x}"])
 | 
| 794 | apply simp | |
| 54681 | 795 | apply clarsimp | 
| 796 | apply (rule_tac B="S" in finite_subset) | |
| 65342 | 797 | apply auto | 
| 54681 | 798 | done | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 799 | |
| 54681 | 800 | |
| 60500 | 801 | subsection \<open>Hence a sort of induction principle composing by swaps\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 802 | |
| 54681 | 803 | lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow> | 
| 65342 | 804 | (\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> P p \<Longrightarrow> P p \<Longrightarrow> permutation p \<Longrightarrow> P (Fun.swap a b id \<circ> p)) \<Longrightarrow> | 
| 54681 | 805 | (\<And>p. p permutes S \<Longrightarrow> P p)" | 
| 806 | proof (induct S rule: finite_induct) | |
| 807 | case empty | |
| 808 | then show ?case by auto | |
| 30488 | 809 | next | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 810 | case (insert x F p) | 
| 54681 | 811 | let ?r = "Fun.swap x (p x) id \<circ> p" | 
| 812 | let ?q = "Fun.swap x (p x) id \<circ> ?r" | |
| 813 | have qp: "?q = p" | |
| 814 | by (simp add: o_assoc) | |
| 815 | from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r" | |
| 816 | by blast | |
| 30488 | 817 | from permutes_in_image[OF insert.prems(3), of x] | 
| 54681 | 818 | have pxF: "p x \<in> insert x F" | 
| 819 | by simp | |
| 820 | have xF: "x \<in> insert x F" | |
| 821 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 822 | have rp: "permutation ?r" | 
| 65342 | 823 | unfolding permutation_permutes | 
| 824 | using insert.hyps(1) permutes_insert_lemma[OF insert.prems(3)] | |
| 54681 | 825 | by blast | 
| 65342 | 826 | from insert.prems(2)[OF xF pxF Pr Pr rp] qp show ?case | 
| 827 | by (simp only:) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 828 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 829 | |
| 54681 | 830 | |
| 60500 | 831 | subsection \<open>Sign of a permutation as a real number\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 832 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 833 | definition "sign p = (if evenperm p then (1::int) else -1)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 834 | |
| 54681 | 835 | lemma sign_nz: "sign p \<noteq> 0" | 
| 836 | by (simp add: sign_def) | |
| 837 | ||
| 838 | lemma sign_id: "sign id = 1" | |
| 839 | by (simp add: sign_def) | |
| 840 | ||
| 841 | lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 842 | by (simp add: sign_def evenperm_inv) | 
| 54681 | 843 | |
| 844 | lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q" | |
| 845 | by (simp add: sign_def evenperm_comp) | |
| 846 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 847 | lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 848 | by (simp add: sign_def evenperm_swap) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 849 | |
| 54681 | 850 | lemma sign_idempotent: "sign p * sign p = 1" | 
| 851 | by (simp add: sign_def) | |
| 852 | ||
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 853 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 854 | subsection \<open>Permuting a list\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 855 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 856 | text \<open>This function permutes a list by applying a permutation to the indices.\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 857 | |
| 65342 | 858 | definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list" | 
| 859 | where "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 860 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 861 | lemma permute_list_map: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 862 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 863 | shows "permute_list f (map g xs) = map g (permute_list f xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 864 | using permutes_in_image[OF assms] by (auto simp: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 865 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 866 | lemma permute_list_nth: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 867 |   assumes "f permutes {..<length xs}" "i < length xs"
 | 
| 65342 | 868 | shows "permute_list f xs ! i = xs ! f i" | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 869 | using permutes_in_image[OF assms(1)] assms(2) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 870 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 871 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 872 | lemma permute_list_Nil [simp]: "permute_list f [] = []" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 873 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 874 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 875 | lemma length_permute_list [simp]: "length (permute_list f xs) = length xs" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 876 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 877 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 878 | lemma permute_list_compose: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 879 |   assumes "g permutes {..<length xs}"
 | 
| 65342 | 880 | shows "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 881 | using assms[THEN permutes_in_image] by (auto simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 882 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 883 | lemma permute_list_ident [simp]: "permute_list (\<lambda>x. x) xs = xs" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 884 | by (simp add: permute_list_def map_nth) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 885 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 886 | lemma permute_list_id [simp]: "permute_list id xs = xs" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 887 | by (simp add: id_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 888 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 889 | lemma mset_permute_list [simp]: | 
| 65342 | 890 | fixes xs :: "'a list" | 
| 891 |   assumes "f permutes {..<length xs}"
 | |
| 892 | shows "mset (permute_list f xs) = mset xs" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 893 | proof (rule multiset_eqI) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 894 | fix y :: 'a | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 895 | from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 896 | using permutes_in_image[OF assms] by auto | 
| 65342 | 897 |   have "count (mset (permute_list f xs)) y = card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
 | 
| 64543 
6b13586ef1a2
remove typo in bij_swap_compose_bij theorem name; tune proof
 bulwahn parents: 
64284diff
changeset | 898 | by (simp add: permute_list_def count_image_mset atLeast0LessThan) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 899 |   also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 900 | by auto | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 901 |   also from assms have "card \<dots> = card {i. i < length xs \<and> y = xs ! i}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 902 | by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj) | 
| 65342 | 903 | also have "\<dots> = count (mset xs) y" | 
| 904 | by (simp add: count_mset length_filter_conv_card) | |
| 905 | finally show "count (mset (permute_list f xs)) y = count (mset xs) y" | |
| 906 | by simp | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 907 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 908 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 909 | lemma set_permute_list [simp]: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 910 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 911 | shows "set (permute_list f xs) = set xs" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 912 | by (rule mset_eq_setD[OF mset_permute_list]) fact | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 913 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 914 | lemma distinct_permute_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 915 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 916 | shows "distinct (permute_list f xs) = distinct xs" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 917 | by (simp add: distinct_count_atmost_1 assms) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 918 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 919 | lemma permute_list_zip: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 920 |   assumes "f permutes A" "A = {..<length xs}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 921 | assumes [simp]: "length xs = length ys" | 
| 65342 | 922 | shows "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 923 | proof - | 
| 65342 | 924 | from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i | 
| 925 | by simp | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 926 | have "permute_list f (zip xs ys) = map (\<lambda>i. zip xs ys ! f i) [0..<length ys]" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 927 | by (simp_all add: permute_list_def zip_map_map) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 928 | also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])" | 
| 65342 | 929 | by (intro nth_equalityI) (simp_all add: *) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 930 | also have "\<dots> = zip (permute_list f xs) (permute_list f ys)" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 931 | by (simp_all add: permute_list_def zip_map_map) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 932 | finally show ?thesis . | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 933 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 934 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 935 | lemma map_of_permute: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 936 | assumes "\<sigma> permutes fst ` set xs" | 
| 65342 | 937 | shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)" | 
| 938 | (is "_ = map_of (map ?f _)") | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 939 | proof | 
| 65342 | 940 | from assms have "inj \<sigma>" "surj \<sigma>" | 
| 941 | by (simp_all add: permutes_inj permutes_surj) | |
| 942 | then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x | |
| 943 | by (induct xs) (auto simp: inv_f_f surj_f_inv_f) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 944 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 945 | |
| 54681 | 946 | |
| 60500 | 947 | subsection \<open>More lemmas about permutations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 948 | |
| 65342 | 949 | text \<open>The following few lemmas were contributed by Lukas Bulwahn.\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 950 | |
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 951 | lemma count_image_mset_eq_card_vimage: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 952 | assumes "finite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 953 |   shows "count (image_mset f (mset_set A)) b = card {a \<in> A. f a = b}"
 | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 954 | using assms | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 955 | proof (induct A) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 956 | case empty | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 957 | show ?case by simp | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 958 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 959 | case (insert x F) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 960 | show ?case | 
| 65342 | 961 | proof (cases "f x = b") | 
| 962 | case True | |
| 963 | with insert.hyps | |
| 964 |     have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
 | |
| 965 | by auto | |
| 966 |     also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
 | |
| 967 | by simp | |
| 968 |     also from \<open>f x = b\<close> have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
 | |
| 969 | by (auto intro: arg_cong[where f="card"]) | |
| 970 | finally show ?thesis | |
| 971 | using insert by auto | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 972 | next | 
| 65342 | 973 | case False | 
| 974 |     then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
 | |
| 975 | by auto | |
| 976 | with insert False show ?thesis | |
| 977 | by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 978 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 979 | qed | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 980 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 981 | (* Prove image_mset_eq_implies_permutes *) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 982 | lemma image_mset_eq_implies_permutes: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 983 | fixes f :: "'a \<Rightarrow> 'b" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 984 | assumes "finite A" | 
| 65342 | 985 | and mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)" | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 986 | obtains p where "p permutes A" and "\<forall>x\<in>A. f x = f' (p x)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 987 | proof - | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 988 |   from \<open>finite A\<close> have [simp]: "finite {a \<in> A. f a = (b::'b)}" for f b by auto
 | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 989 | have "f ` A = f' ` A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 990 | proof - | 
| 65342 | 991 | from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))" | 
| 992 | by simp | |
| 993 | also have "\<dots> = f' ` set_mset (mset_set A)" | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 994 | by (metis mset_eq multiset.set_map) | 
| 65342 | 995 | also from \<open>finite A\<close> have "\<dots> = f' ` A" | 
| 996 | by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 997 | finally show ?thesis . | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 998 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 999 |   have "\<forall>b\<in>(f ` A). \<exists>p. bij_betw p {a \<in> A. f a = b} {a \<in> A. f' a = b}"
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1000 | proof | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1001 | fix b | 
| 65342 | 1002 | from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b" | 
| 1003 | by simp | |
| 1004 |     with \<open>finite A\<close> have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1005 | by (simp add: count_image_mset_eq_card_vimage) | 
| 65342 | 1006 |     then show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1007 | by (intro finite_same_card_bij) simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1008 | qed | 
| 65342 | 1009 |   then have "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
 | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1010 | by (rule bchoice) | 
| 65342 | 1011 |   then obtain p where p: "\<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}" ..
 | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1012 | define p' where "p' = (\<lambda>a. if a \<in> A then p (f a) a else a)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1013 | have "p' permutes A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1014 | proof (rule bij_imp_permutes) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1015 |     have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
 | 
| 65342 | 1016 | by (auto simp: disjoint_family_on_def) | 
| 1017 | moreover | |
| 1018 |     have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if "b \<in> f ` A" for b
 | |
| 1019 | using p that by (subst bij_betw_cong[where g="p b"]) auto | |
| 1020 | ultimately | |
| 1021 |     have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1022 | by (rule bij_betw_UNION_disjoint) | 
| 65342 | 1023 |     moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
 | 
| 1024 | by auto | |
| 1025 |     moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
 | |
| 1026 | by auto | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1027 | ultimately show "bij_betw p' A A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1028 | unfolding p'_def by (subst bij_betw_cong[where g="(\<lambda>a. p (f a) a)"]) auto | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1029 | next | 
| 65342 | 1030 | show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x" | 
| 1031 | by (simp add: p'_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1032 | qed | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1033 | moreover from p have "\<forall>x\<in>A. f x = f' (p' x)" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1034 | unfolding p'_def using bij_betwE by fastforce | 
| 65342 | 1035 | ultimately show ?thesis .. | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1036 | qed | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1037 | |
| 65342 | 1038 | lemma mset_set_upto_eq_mset_upto: "mset_set {..<n} = mset [0..<n]"
 | 
| 1039 | by (induct n) (auto simp: add.commute lessThan_Suc) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1040 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1041 | (* and derive the existing property: *) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1042 | lemma mset_eq_permutation: | 
| 65342 | 1043 | fixes xs ys :: "'a list" | 
| 1044 | assumes mset_eq: "mset xs = mset ys" | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1045 |   obtains p where "p permutes {..<length ys}" "permute_list p ys = xs"
 | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1046 | proof - | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1047 | from mset_eq have length_eq: "length xs = length ys" | 
| 65342 | 1048 | by (rule mset_eq_length) | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1049 |   have "mset_set {..<length ys} = mset [0..<length ys]"
 | 
| 65342 | 1050 | by (rule mset_set_upto_eq_mset_upto) | 
| 1051 |   with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
 | |
| 1052 |     image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
 | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1053 | by (metis map_nth mset_map) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1054 | from image_mset_eq_implies_permutes[OF _ this] | 
| 65342 | 1055 |   obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
 | 
| 1056 | by auto | |
| 1057 | with length_eq have "permute_list p ys = xs" | |
| 1058 | by (auto intro!: nth_equalityI simp: permute_list_nth) | |
| 1059 | with p show thesis .. | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1060 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1061 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1062 | lemma permutes_natset_le: | 
| 54681 | 1063 | fixes S :: "'a::wellorder set" | 
| 65342 | 1064 | assumes "p permutes S" | 
| 1065 | and "\<forall>i \<in> S. p i \<le> i" | |
| 54681 | 1066 | shows "p = id" | 
| 1067 | proof - | |
| 65342 | 1068 | have "p n = n" for n | 
| 1069 | using assms | |
| 1070 | proof (induct n arbitrary: S rule: less_induct) | |
| 1071 | case (less n) | |
| 1072 | show ?case | |
| 1073 | proof (cases "n \<in> S") | |
| 1074 | case False | |
| 1075 | with less(2) show ?thesis | |
| 1076 | unfolding permutes_def by metis | |
| 1077 | next | |
| 1078 | case True | |
| 1079 | with less(3) have "p n < n \<or> p n = n" | |
| 1080 | by auto | |
| 1081 | then show ?thesis | |
| 1082 | proof | |
| 1083 | assume "p n < n" | |
| 1084 | with less have "p (p n) = p n" | |
| 1085 | by metis | |
| 1086 | with permutes_inj[OF less(2)] have "p n = n" | |
| 1087 | unfolding inj_def by blast | |
| 1088 | with \<open>p n < n\<close> have False | |
| 1089 | by simp | |
| 1090 | then show ?thesis .. | |
| 1091 | qed | |
| 54681 | 1092 | qed | 
| 65342 | 1093 | qed | 
| 1094 | then show ?thesis by (auto simp: fun_eq_iff) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1095 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1096 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1097 | lemma permutes_natset_ge: | 
| 54681 | 1098 | fixes S :: "'a::wellorder set" | 
| 1099 | assumes p: "p permutes S" | |
| 1100 | and le: "\<forall>i \<in> S. p i \<ge> i" | |
| 1101 | shows "p = id" | |
| 1102 | proof - | |
| 65342 | 1103 | have "i \<ge> inv p i" if "i \<in> S" for i | 
| 1104 | proof - | |
| 1105 | from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S" | |
| 54681 | 1106 | by simp | 
| 1107 | with le have "p (inv p i) \<ge> inv p i" | |
| 1108 | by blast | |
| 65342 | 1109 | with permutes_inverses[OF p] show ?thesis | 
| 54681 | 1110 | by simp | 
| 65342 | 1111 | qed | 
| 1112 | then have "\<forall>i\<in>S. inv p i \<le> i" | |
| 54681 | 1113 | by blast | 
| 65342 | 1114 | from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id" | 
| 54681 | 1115 | by simp | 
| 30488 | 1116 | then show ?thesis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1117 | apply (subst permutes_inv_inv[OF p, symmetric]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1118 | apply (rule inv_unique_comp) | 
| 65342 | 1119 | apply simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1120 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1121 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1122 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1123 | lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
 | 
| 54681 | 1124 | apply (rule set_eqI) | 
| 1125 | apply auto | |
| 1126 | using permutes_inv_inv permutes_inv | |
| 65342 | 1127 | apply auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1128 | apply (rule_tac x="inv x" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1129 | apply auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1130 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1131 | |
| 30488 | 1132 | lemma image_compose_permutations_left: | 
| 65342 | 1133 | assumes "q permutes S" | 
| 1134 |   shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
 | |
| 54681 | 1135 | apply (rule set_eqI) | 
| 1136 | apply auto | |
| 65342 | 1137 | apply (rule permutes_compose) | 
| 1138 | using assms | |
| 1139 | apply auto | |
| 54681 | 1140 | apply (rule_tac x = "inv q \<circ> x" in exI) | 
| 1141 | apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o) | |
| 1142 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1143 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1144 | lemma image_compose_permutations_right: | 
| 65342 | 1145 | assumes "q permutes S" | 
| 54681 | 1146 |   shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
 | 
| 1147 | apply (rule set_eqI) | |
| 1148 | apply auto | |
| 65342 | 1149 | apply (rule permutes_compose) | 
| 1150 | using assms | |
| 1151 | apply auto | |
| 54681 | 1152 | apply (rule_tac x = "x \<circ> inv q" in exI) | 
| 1153 | apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc) | |
| 1154 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1155 | |
| 54681 | 1156 | lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
 | 
| 1157 | by (simp add: permutes_def) metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1158 | |
| 65342 | 1159 | lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
 | 
| 54681 | 1160 | (is "?lhs = ?rhs") | 
| 1161 | proof - | |
| 30036 | 1162 |   let ?S = "{p . p permutes S}"
 | 
| 65342 | 1163 | have *: "inj_on inv ?S" | 
| 54681 | 1164 | proof (auto simp add: inj_on_def) | 
| 1165 | fix q r | |
| 1166 | assume q: "q permutes S" | |
| 1167 | and r: "r permutes S" | |
| 1168 | and qr: "inv q = inv r" | |
| 1169 | then have "inv (inv q) = inv (inv r)" | |
| 1170 | by simp | |
| 1171 | with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r" | |
| 1172 | by metis | |
| 1173 | qed | |
| 65342 | 1174 | have **: "inv ` ?S = ?S" | 
| 54681 | 1175 | using image_inverse_permutations by blast | 
| 65342 | 1176 | have ***: "?rhs = sum (f \<circ> inv) ?S" | 
| 54681 | 1177 | by (simp add: o_def) | 
| 65342 | 1178 | from sum.reindex[OF *, of f] show ?thesis | 
| 1179 | by (simp only: ** ***) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1180 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1181 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1182 | lemma setum_permutations_compose_left: | 
| 30036 | 1183 | assumes q: "q permutes S" | 
| 64267 | 1184 |   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
 | 
| 54681 | 1185 | (is "?lhs = ?rhs") | 
| 1186 | proof - | |
| 30036 | 1187 |   let ?S = "{p. p permutes S}"
 | 
| 65342 | 1188 | have *: "?rhs = sum (f \<circ> (op \<circ> q)) ?S" | 
| 54681 | 1189 | by (simp add: o_def) | 
| 65342 | 1190 | have **: "inj_on (op \<circ> q) ?S" | 
| 54681 | 1191 | proof (auto simp add: inj_on_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1192 | fix p r | 
| 54681 | 1193 | assume "p permutes S" | 
| 1194 | and r: "r permutes S" | |
| 1195 | and rp: "q \<circ> p = q \<circ> r" | |
| 1196 | then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r" | |
| 1197 | by (simp add: comp_assoc) | |
| 1198 | with permutes_inj[OF q, unfolded inj_iff] show "p = r" | |
| 1199 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1200 | qed | 
| 65342 | 1201 | have "(op \<circ> q) ` ?S = ?S" | 
| 54681 | 1202 | using image_compose_permutations_left[OF q] by auto | 
| 65342 | 1203 | with * sum.reindex[OF **, of f] show ?thesis | 
| 1204 | by (simp only:) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1205 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1206 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1207 | lemma sum_permutations_compose_right: | 
| 30036 | 1208 | assumes q: "q permutes S" | 
| 64267 | 1209 |   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
 | 
| 54681 | 1210 | (is "?lhs = ?rhs") | 
| 1211 | proof - | |
| 30036 | 1212 |   let ?S = "{p. p permutes S}"
 | 
| 65342 | 1213 | have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S" | 
| 54681 | 1214 | by (simp add: o_def) | 
| 65342 | 1215 | have **: "inj_on (\<lambda>p. p \<circ> q) ?S" | 
| 54681 | 1216 | proof (auto simp add: inj_on_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1217 | fix p r | 
| 54681 | 1218 | assume "p permutes S" | 
| 1219 | and r: "r permutes S" | |
| 1220 | and rp: "p \<circ> q = r \<circ> q" | |
| 1221 | then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)" | |
| 1222 | by (simp add: o_assoc) | |
| 1223 | with permutes_surj[OF q, unfolded surj_iff] show "p = r" | |
| 1224 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1225 | qed | 
| 65342 | 1226 | from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S" | 
| 1227 | by auto | |
| 1228 | with * sum.reindex[OF **, of f] show ?thesis | |
| 1229 | by (simp only:) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1230 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1231 | |
| 54681 | 1232 | |
| 60500 | 1233 | subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1234 | |
| 64267 | 1235 | lemma sum_over_permutations_insert: | 
| 54681 | 1236 | assumes fS: "finite S" | 
| 1237 | and aS: "a \<notin> S" | |
| 64267 | 1238 |   shows "sum f {p. p permutes (insert a S)} =
 | 
| 1239 |     sum (\<lambda>b. sum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
 | |
| 54681 | 1240 | proof - | 
| 65342 | 1241 | have *: "\<And>f a b. (\<lambda>(b, p). f (Fun.swap a b id \<circ> p)) = f \<circ> (\<lambda>(b,p). Fun.swap a b id \<circ> p)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 1242 | by (simp add: fun_eq_iff) | 
| 65342 | 1243 |   have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
 | 
| 54681 | 1244 | by blast | 
| 30488 | 1245 | show ?thesis | 
| 65342 | 1246 | unfolding * ** sum.cartesian_product permutes_insert | 
| 64267 | 1247 | proof (rule sum.reindex) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1248 | let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1249 |     let ?P = "{p. p permutes S}"
 | 
| 54681 | 1250 |     {
 | 
| 1251 | fix b c p q | |
| 1252 | assume b: "b \<in> insert a S" | |
| 1253 | assume c: "c \<in> insert a S" | |
| 1254 | assume p: "p permutes S" | |
| 1255 | assume q: "q permutes S" | |
| 1256 | assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1257 | from p q aS have pa: "p a = a" and qa: "q a = a" | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32456diff
changeset | 1258 | unfolding permutes_def by metis+ | 
| 54681 | 1259 | from eq have "(Fun.swap a b id \<circ> p) a = (Fun.swap a c id \<circ> q) a" | 
| 1260 | by simp | |
| 1261 | then have bc: "b = c" | |
| 56545 | 1262 | by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def | 
| 62390 | 1263 | cong del: if_weak_cong split: if_split_asm) | 
| 54681 | 1264 | from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) = | 
| 1265 | (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp | |
| 1266 | then have "p = q" | |
| 65342 | 1267 | unfolding o_assoc swap_id_idempotent by simp | 
| 54681 | 1268 | with bc have "b = c \<and> p = q" | 
| 1269 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1270 | } | 
| 30488 | 1271 | then show "inj_on ?f (insert a S \<times> ?P)" | 
| 54681 | 1272 | unfolding inj_on_def by clarify metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1273 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1274 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1275 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1276 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1277 | subsection \<open>Constructing permutations from association lists\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1278 | |
| 65342 | 1279 | definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
 | 
| 1280 | where "list_permutes xs A \<longleftrightarrow> | |
| 1281 | set (map fst xs) \<subseteq> A \<and> | |
| 1282 | set (map snd xs) = set (map fst xs) \<and> | |
| 1283 | distinct (map fst xs) \<and> | |
| 1284 | distinct (map snd xs)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1285 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1286 | lemma list_permutesI [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1287 | assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)" | 
| 65342 | 1288 | shows "list_permutes xs A" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1289 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1290 | from assms(2,3) have "distinct (map snd xs)" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1291 | by (intro card_distinct) (simp_all add: distinct_card del: set_map) | 
| 65342 | 1292 | with assms show ?thesis | 
| 1293 | by (simp add: list_permutes_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1294 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1295 | |
| 65342 | 1296 | definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
 | 
| 1297 | where "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1298 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1299 | lemma permutation_of_list_Cons: | 
| 65342 | 1300 | "permutation_of_list ((x, y) # xs) x' = (if x = x' then y else permutation_of_list xs x')" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1301 | by (simp add: permutation_of_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1302 | |
| 65342 | 1303 | fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
 | 
| 1304 | where | |
| 1305 | "inverse_permutation_of_list [] x = x" | |
| 1306 | | "inverse_permutation_of_list ((y, x') # xs) x = | |
| 1307 | (if x = x' then y else inverse_permutation_of_list xs x)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1308 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1309 | declare inverse_permutation_of_list.simps [simp del] | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1310 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1311 | lemma inj_on_map_of: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1312 | assumes "distinct (map snd xs)" | 
| 65342 | 1313 | shows "inj_on (map_of xs) (set (map fst xs))" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1314 | proof (rule inj_onI) | 
| 65342 | 1315 | fix x y | 
| 1316 | assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1317 | assume eq: "map_of xs x = map_of xs y" | 
| 65342 | 1318 | from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'" | 
| 1319 | by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff) | |
| 1320 | moreover from x'y' have *: "(x, x') \<in> set xs" "(y, y') \<in> set xs" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1321 | by (force dest: map_of_SomeD)+ | 
| 65342 | 1322 | moreover from * eq x'y' have "x' = y'" | 
| 1323 | by simp | |
| 1324 | ultimately show "x = y" | |
| 1325 | using assms by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"]) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1326 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1327 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1328 | lemma inj_on_the: "None \<notin> A \<Longrightarrow> inj_on the A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1329 | by (auto simp: inj_on_def option.the_def split: option.splits) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1330 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1331 | lemma inj_on_map_of': | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1332 | assumes "distinct (map snd xs)" | 
| 65342 | 1333 | shows "inj_on (the \<circ> map_of xs) (set (map fst xs))" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1334 | by (intro comp_inj_on inj_on_map_of assms inj_on_the) | 
| 65342 | 1335 | (force simp: eq_commute[of None] map_of_eq_None_iff) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1336 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1337 | lemma image_map_of: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1338 | assumes "distinct (map fst xs)" | 
| 65342 | 1339 | shows "map_of xs ` set (map fst xs) = Some ` set (map snd xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1340 | using assms by (auto simp: rev_image_eqI) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1341 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1342 | lemma the_Some_image [simp]: "the ` Some ` A = A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1343 | by (subst image_image) simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1344 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1345 | lemma image_map_of': | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1346 | assumes "distinct (map fst xs)" | 
| 65342 | 1347 | shows "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1348 | by (simp only: image_comp [symmetric] image_map_of assms the_Some_image) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1349 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1350 | lemma permutation_of_list_permutes [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1351 | assumes "list_permutes xs A" | 
| 65342 | 1352 | shows "permutation_of_list xs permutes A" | 
| 1353 | (is "?f permutes _") | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1354 | proof (rule permutes_subset[OF bij_imp_permutes]) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1355 | from assms show "set (map fst xs) \<subseteq> A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1356 | by (simp add: list_permutes_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1357 | from assms have "inj_on (the \<circ> map_of xs) (set (map fst xs))" (is ?P) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1358 | by (intro inj_on_map_of') (simp_all add: list_permutes_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1359 | also have "?P \<longleftrightarrow> inj_on ?f (set (map fst xs))" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1360 | by (intro inj_on_cong) | 
| 65342 | 1361 | (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1362 | finally have "bij_betw ?f (set (map fst xs)) (?f ` set (map fst xs))" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1363 | by (rule inj_on_imp_bij_betw) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1364 | also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1365 | by (intro image_cong refl) | 
| 65342 | 1366 | (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits) | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 1367 | also from assms have "\<dots> = set (map fst xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1368 | by (subst image_map_of') (simp_all add: list_permutes_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1369 | finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" . | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1370 | qed (force simp: permutation_of_list_def dest!: map_of_SomeD split: option.splits)+ | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1371 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1372 | lemma eval_permutation_of_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1373 | "permutation_of_list [] x = x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1374 | "x = x' \<Longrightarrow> permutation_of_list ((x',y)#xs) x = y" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1375 | "x \<noteq> x' \<Longrightarrow> permutation_of_list ((x',y')#xs) x = permutation_of_list xs x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1376 | by (simp_all add: permutation_of_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1377 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1378 | lemma eval_inverse_permutation_of_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1379 | "inverse_permutation_of_list [] x = x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1380 | "x = x' \<Longrightarrow> inverse_permutation_of_list ((y,x')#xs) x = y" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1381 | "x \<noteq> x' \<Longrightarrow> inverse_permutation_of_list ((y',x')#xs) x = inverse_permutation_of_list xs x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1382 | by (simp_all add: inverse_permutation_of_list.simps) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1383 | |
| 65342 | 1384 | lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x" | 
| 1385 | by (induct xs) (auto simp: permutation_of_list_Cons) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1386 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1387 | lemma permutation_of_list_unique': | 
| 65342 | 1388 | "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y" | 
| 1389 | by (induct xs) (force simp: permutation_of_list_Cons)+ | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1390 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1391 | lemma permutation_of_list_unique: | 
| 65342 | 1392 | "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y" | 
| 1393 | by (intro permutation_of_list_unique') (simp_all add: list_permutes_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1394 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1395 | lemma inverse_permutation_of_list_id: | 
| 65342 | 1396 | "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x" | 
| 1397 | by (induct xs) auto | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1398 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1399 | lemma inverse_permutation_of_list_unique': | 
| 65342 | 1400 | "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x" | 
| 1401 | by (induct xs) (force simp: inverse_permutation_of_list.simps)+ | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1402 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1403 | lemma inverse_permutation_of_list_unique: | 
| 65342 | 1404 | "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x" | 
| 1405 | by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1406 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1407 | lemma inverse_permutation_of_list_correct: | 
| 65342 | 1408 | fixes A :: "'a set" | 
| 1409 | assumes "list_permutes xs A" | |
| 1410 | shows "inverse_permutation_of_list xs = inv (permutation_of_list xs)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1411 | proof (rule ext, rule sym, subst permutes_inv_eq) | 
| 65342 | 1412 | from assms show "permutation_of_list xs permutes A" | 
| 1413 | by simp | |
| 1414 | show "permutation_of_list xs (inverse_permutation_of_list xs x) = x" for x | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1415 | proof (cases "x \<in> set (map snd xs)") | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1416 | case True | 
| 65342 | 1417 | then obtain y where "(y, x) \<in> set xs" by auto | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1418 | with assms show ?thesis | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1419 | by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique) | 
| 65342 | 1420 | next | 
| 1421 | case False | |
| 1422 | with assms show ?thesis | |
| 1423 | by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id) | |
| 1424 | qed | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1425 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1426 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1427 | end |