| author | wenzelm | 
| Thu, 07 Nov 2024 12:35:55 +0100 | |
| changeset 81386 | bcd880130390 | 
| parent 80777 | 623d46973cbe | 
| child 82683 | 71304514891e | 
| permissions | -rw-r--r-- | 
| 73477 | 1 | (* Author: Amine Chaieb, University of Cambridge | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 2 | *) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 3 | |
| 60500 | 4 | 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 | 5 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 6 | theory Permutations | 
| 73477 | 7 | imports | 
| 8 | "HOL-Library.Multiset" | |
| 9 | "HOL-Library.Disjoint_Sets" | |
| 73623 | 10 | Transposition | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 11 | begin | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 12 | |
| 73466 | 13 | subsection \<open>Auxiliary\<close> | 
| 14 | ||
| 15 | abbreviation (input) fixpoints :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> 'a set\<close>
 | |
| 16 |   where \<open>fixpoints f \<equiv> {x. f x = x}\<close>
 | |
| 17 | ||
| 18 | lemma inj_on_fixpoints: | |
| 19 | \<open>inj_on f (fixpoints f)\<close> | |
| 20 | by (rule inj_onI) simp | |
| 21 | ||
| 22 | lemma bij_betw_fixpoints: | |
| 23 | \<open>bij_betw f (fixpoints f) (fixpoints f)\<close> | |
| 24 | using inj_on_fixpoints by (auto simp add: bij_betw_def) | |
| 25 | ||
| 26 | ||
| 73328 | 27 | subsection \<open>Basic definition and consequences\<close> | 
| 54681 | 28 | |
| 73466 | 29 | definition permutes :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> 'a set \<Rightarrow> bool\<close>  (infixr \<open>permutes\<close> 41)
 | 
| 30 | where \<open>p permutes S \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)\<close> | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 31 | |
| 73466 | 32 | lemma bij_imp_permutes: | 
| 33 | \<open>p permutes S\<close> if \<open>bij_betw p S S\<close> and stable: \<open>\<And>x. x \<notin> S \<Longrightarrow> p x = x\<close> | |
| 34 | proof - | |
| 35 | note \<open>bij_betw p S S\<close> | |
| 36 | moreover have \<open>bij_betw p (- S) (- S)\<close> | |
| 37 | by (auto simp add: stable intro!: bij_betw_imageI inj_onI) | |
| 38 | ultimately have \<open>bij_betw p (S \<union> - S) (S \<union> - S)\<close> | |
| 39 | by (rule bij_betw_combine) simp | |
| 40 | then have \<open>\<exists>!x. p x = y\<close> for y | |
| 41 | by (simp add: bij_iff) | |
| 42 | with stable show ?thesis | |
| 43 | by (simp add: permutes_def) | |
| 44 | qed | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 45 | |
| 73466 | 46 | context | 
| 47 | fixes p :: \<open>'a \<Rightarrow> 'a\<close> and S :: \<open>'a set\<close> | |
| 48 | assumes perm: \<open>p permutes S\<close> | |
| 49 | begin | |
| 50 | ||
| 51 | lemma permutes_inj: | |
| 52 | \<open>inj p\<close> | |
| 53 | using perm by (auto simp: permutes_def inj_on_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 54 | |
| 73466 | 55 | lemma permutes_image: | 
| 56 | \<open>p ` S = S\<close> | |
| 57 | proof (rule set_eqI) | |
| 58 | fix x | |
| 59 | show \<open>x \<in> p ` S \<longleftrightarrow> x \<in> S\<close> | |
| 60 | proof | |
| 61 | assume \<open>x \<in> p ` S\<close> | |
| 62 | then obtain y where \<open>y \<in> S\<close> \<open>p y = x\<close> | |
| 63 | by blast | |
| 64 | with perm show \<open>x \<in> S\<close> | |
| 65 | by (cases \<open>y = x\<close>) (auto simp add: permutes_def) | |
| 66 | next | |
| 67 | assume \<open>x \<in> S\<close> | |
| 68 | with perm obtain y where \<open>y \<in> S\<close> \<open>p y = x\<close> | |
| 69 | by (metis permutes_def) | |
| 70 | then show \<open>x \<in> p ` S\<close> | |
| 71 | by blast | |
| 72 | qed | |
| 73 | qed | |
| 74 | ||
| 75 | lemma permutes_not_in: | |
| 76 | \<open>x \<notin> S \<Longrightarrow> p x = x\<close> | |
| 77 | using perm by (auto simp: permutes_def) | |
| 78 | ||
| 79 | lemma permutes_image_complement: | |
| 80 | \<open>p ` (- S) = - S\<close> | |
| 81 | by (auto simp add: permutes_not_in) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 82 | |
| 73466 | 83 | lemma permutes_in_image: | 
| 84 | \<open>p x \<in> S \<longleftrightarrow> x \<in> S\<close> | |
| 85 | using permutes_image permutes_inj by (auto dest: inj_image_mem_iff) | |
| 86 | ||
| 87 | lemma permutes_surj: | |
| 88 | \<open>surj p\<close> | |
| 89 | proof - | |
| 90 | have \<open>p ` (S \<union> - S) = p ` S \<union> p ` (- S)\<close> | |
| 91 | by (rule image_Un) | |
| 92 | then show ?thesis | |
| 93 | by (simp add: permutes_image permutes_image_complement) | |
| 94 | qed | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 95 | |
| 73466 | 96 | lemma permutes_inv_o: | 
| 97 | shows "p \<circ> inv p = id" | |
| 98 | and "inv p \<circ> p = id" | |
| 99 | using permutes_inj permutes_surj | |
| 100 | unfolding inj_iff [symmetric] surj_iff [symmetric] by auto | |
| 101 | ||
| 102 | lemma permutes_inverses: | |
| 103 | shows "p (inv p x) = x" | |
| 104 | and "inv p (p x) = x" | |
| 105 | using permutes_inv_o [unfolded fun_eq_iff o_def] by auto | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 106 | |
| 73466 | 107 | lemma permutes_inv_eq: | 
| 108 | \<open>inv p y = x \<longleftrightarrow> p x = y\<close> | |
| 109 | by (auto simp add: permutes_inverses) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 110 | |
| 73466 | 111 | lemma permutes_inj_on: | 
| 112 | \<open>inj_on p A\<close> | |
| 113 | by (rule inj_on_subset [of _ UNIV]) (auto intro: permutes_inj) | |
| 114 | ||
| 115 | lemma permutes_bij: | |
| 116 | \<open>bij p\<close> | |
| 65342 | 117 | unfolding bij_def by (metis permutes_inj permutes_surj) | 
| 60601 | 118 | |
| 73466 | 119 | lemma permutes_imp_bij: | 
| 120 | \<open>bij_betw p S S\<close> | |
| 121 | by (simp add: bij_betw_def permutes_image permutes_inj_on) | |
| 59474 | 122 | |
| 73466 | 123 | lemma permutes_subset: | 
| 124 | \<open>p permutes T\<close> if \<open>S \<subseteq> T\<close> | |
| 125 | proof (rule bij_imp_permutes) | |
| 126 | define R where \<open>R = T - S\<close> | |
| 127 |   with that have \<open>T = R \<union> S\<close> \<open>R \<inter> S = {}\<close>
 | |
| 128 | by auto | |
| 129 | then have \<open>p x = x\<close> if \<open>x \<in> R\<close> for x | |
| 130 | using that by (auto intro: permutes_not_in) | |
| 131 | then have \<open>p ` R = R\<close> | |
| 132 | by simp | |
| 133 | with \<open>T = R \<union> S\<close> show \<open>bij_betw p T T\<close> | |
| 134 | by (simp add: bij_betw_def permutes_inj_on image_Un permutes_image) | |
| 135 | fix x | |
| 136 | assume \<open>x \<notin> T\<close> | |
| 137 | with \<open>T = R \<union> S\<close> show \<open>p x = x\<close> | |
| 138 | by (simp add: permutes_not_in) | |
| 139 | qed | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 140 | |
| 73410 | 141 | lemma permutes_imp_permutes_insert: | 
| 73466 | 142 | \<open>p permutes insert x S\<close> | 
| 143 | by (rule permutes_subset) auto | |
| 144 | ||
| 145 | end | |
| 146 | ||
| 147 | lemma permutes_id [simp]: | |
| 148 | \<open>id permutes S\<close> | |
| 149 | by (auto intro: bij_imp_permutes) | |
| 73410 | 150 | |
| 73466 | 151 | lemma permutes_empty [simp]: | 
| 152 |   \<open>p permutes {} \<longleftrightarrow> p = id\<close>
 | |
| 153 | proof | |
| 154 |   assume \<open>p permutes {}\<close>
 | |
| 155 | then show \<open>p = id\<close> | |
| 156 | by (auto simp add: fun_eq_iff permutes_not_in) | |
| 157 | next | |
| 158 | assume \<open>p = id\<close> | |
| 159 |   then show \<open>p permutes {}\<close>
 | |
| 160 | by simp | |
| 161 | qed | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 162 | |
| 73466 | 163 | lemma permutes_sing [simp]: | 
| 164 |   \<open>p permutes {a} \<longleftrightarrow> p = id\<close>
 | |
| 165 | proof | |
| 166 |   assume perm: \<open>p permutes {a}\<close>
 | |
| 167 | show \<open>p = id\<close> | |
| 168 | proof | |
| 169 | fix x | |
| 170 |     from perm have \<open>p ` {a} = {a}\<close>
 | |
| 171 | by (rule permutes_image) | |
| 172 | with perm show \<open>p x = id x\<close> | |
| 173 | by (cases \<open>x = a\<close>) (auto simp add: permutes_not_in) | |
| 174 | qed | |
| 175 | next | |
| 176 | assume \<open>p = id\<close> | |
| 177 |   then show \<open>p permutes {a}\<close>
 | |
| 178 | by simp | |
| 179 | qed | |
| 30488 | 180 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 181 | lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)" | 
| 65342 | 182 | by (simp add: permutes_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 183 | |
| 73648 | 184 | lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> transpose a b permutes S" | 
| 185 | by (rule bij_imp_permutes) (auto intro: transpose_apply_other) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 186 | |
| 73466 | 187 | lemma permutes_superset: | 
| 188 | \<open>p permutes T\<close> if \<open>p permutes S\<close> \<open>\<And>x. x \<in> S - T \<Longrightarrow> p x = x\<close> | |
| 189 | proof - | |
| 190 | define R U where \<open>R = T \<inter> S\<close> and \<open>U = S - T\<close> | |
| 191 |   then have \<open>T = R \<union> (T - S)\<close> \<open>S = R \<union> U\<close> \<open>R \<inter> U = {}\<close>
 | |
| 192 | by auto | |
| 193 | from that \<open>U = S - T\<close> have \<open>p ` U = U\<close> | |
| 194 | by simp | |
| 195 | from \<open>p permutes S\<close> have \<open>bij_betw p (R \<union> U) (R \<union> U)\<close> | |
| 196 | by (simp add: permutes_imp_bij \<open>S = R \<union> U\<close>) | |
| 197 | moreover have \<open>bij_betw p U U\<close> | |
| 198 | using that \<open>U = S - T\<close> by (simp add: bij_betw_def permutes_inj_on) | |
| 199 | ultimately have \<open>bij_betw p R R\<close> | |
| 200 |     using \<open>R \<inter> U = {}\<close> \<open>R \<inter> U = {}\<close> by (rule bij_betw_partition)
 | |
| 201 | then have \<open>p permutes R\<close> | |
| 202 | proof (rule bij_imp_permutes) | |
| 203 | fix x | |
| 204 | assume \<open>x \<notin> R\<close> | |
| 205 | with \<open>R = T \<inter> S\<close> \<open>p permutes S\<close> show \<open>p x = x\<close> | |
| 206 | by (cases \<open>x \<in> S\<close>) (auto simp add: permutes_not_in that(2)) | |
| 207 | qed | |
| 208 | then have \<open>p permutes R \<union> (T - S)\<close> | |
| 209 | by (rule permutes_subset) simp | |
| 210 | with \<open>T = R \<union> (T - S)\<close> show ?thesis | |
| 211 | by simp | |
| 212 | qed | |
| 54681 | 213 | |
| 69895 
6b03a8cf092d
more formal contributors (with the help of the history);
 wenzelm parents: 
67673diff
changeset | 214 | lemma permutes_bij_inv_into: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close> | 
| 65342 | 215 | fixes A :: "'a set" | 
| 216 | and B :: "'b set" | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 217 | assumes "p permutes A" | 
| 65342 | 218 | and "bij_betw f A B" | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 219 | 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 | 220 | proof (rule bij_imp_permutes) | 
| 65342 | 221 | from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A" | 
| 222 | by (auto simp add: permutes_imp_bij bij_betw_inv_into) | |
| 223 | then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B" | |
| 224 | by (simp add: bij_betw_trans) | |
| 225 | then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B" | |
| 226 | 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 | 227 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 228 | fix x | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 229 | assume "x \<notin> B" | 
| 65342 | 230 | 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 | 231 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 232 | |
| 69895 
6b03a8cf092d
more formal contributors (with the help of the history);
 wenzelm parents: 
67673diff
changeset | 233 | lemma permutes_image_mset: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 234 | assumes "p permutes A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 235 | shows "image_mset p (mset_set A) = mset_set A" | 
| 65342 | 236 | 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 | 237 | |
| 69895 
6b03a8cf092d
more formal contributors (with the help of the history);
 wenzelm parents: 
67673diff
changeset | 238 | lemma permutes_implies_image_mset_eq: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 239 | 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 | 240 | 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 | 241 | proof - | 
| 65342 | 242 | have "f x = f' (p x)" if "x \<in># mset_set A" for x | 
| 243 | using assms(2)[of x] that by (cases "finite A") auto | |
| 244 | with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)" | |
| 245 | by (auto intro!: image_mset_cong) | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 246 | 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 | 247 | by (simp add: image_mset.compositionality) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 248 | 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 | 249 | proof - | 
| 65342 | 250 | from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A" | 
| 251 | by blast | |
| 252 | then show ?thesis by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 253 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 254 | finally show ?thesis .. | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 255 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 256 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 257 | |
| 60500 | 258 | subsection \<open>Group properties\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 259 | |
| 54681 | 260 | 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 | 261 | unfolding permutes_def o_def by metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 262 | |
| 54681 | 263 | lemma permutes_inv: | 
| 65342 | 264 | assumes "p permutes S" | 
| 54681 | 265 | shows "inv p permutes S" | 
| 65342 | 266 | 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 | 267 | |
| 54681 | 268 | lemma permutes_inv_inv: | 
| 65342 | 269 | assumes "p permutes S" | 
| 54681 | 270 | shows "inv (inv p) = p" | 
| 65342 | 271 | 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 | 272 | by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 273 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 274 | lemma permutes_invI: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 275 | assumes perm: "p permutes S" | 
| 65342 | 276 | and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x" | 
| 277 | and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x" | |
| 278 | shows "inv p = p'" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 279 | proof | 
| 65342 | 280 | show "inv p x = p' x" for x | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 281 | proof (cases "x \<in> S") | 
| 65342 | 282 | case True | 
| 283 | from assms have "p' x = p' (p (inv p x))" | |
| 284 | by (simp add: permutes_inverses) | |
| 285 | also from permutes_inv[OF perm] True have "\<dots> = inv p x" | |
| 286 | by (subst inv) (simp_all add: permutes_in_image) | |
| 287 | finally show ?thesis .. | |
| 288 | next | |
| 289 | case False | |
| 290 | with permutes_inv[OF perm] show ?thesis | |
| 291 | by (simp_all add: outside permutes_not_in) | |
| 292 | qed | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 293 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 294 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 295 | 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 | 296 | 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 | 297 | |
| 54681 | 298 | |
| 66486 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 299 | subsection \<open>Mapping permutations with bijections\<close> | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 300 | |
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 301 | lemma bij_betw_permutations: | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 302 | assumes "bij_betw f A B" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 303 | shows "bij_betw (\<lambda>\<pi> x. if x \<in> B then f (\<pi> (inv_into A f x)) else x) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 304 |              {\<pi>. \<pi> permutes A} {\<pi>. \<pi> permutes B}" (is "bij_betw ?f _ _")
 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 305 | proof - | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 306 | let ?g = "(\<lambda>\<pi> x. if x \<in> A then inv_into A f (\<pi> (f x)) else x)" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 307 | show ?thesis | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 308 | proof (rule bij_betw_byWitness [of _ ?g], goal_cases) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 309 | case 3 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 310 | show ?case using permutes_bij_inv_into[OF _ assms] by auto | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 311 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 312 | case 4 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 313 | have bij_inv: "bij_betw (inv_into A f) B A" by (intro bij_betw_inv_into assms) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 314 |     {
 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 315 | fix \<pi> assume "\<pi> permutes B" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 316 | from permutes_bij_inv_into[OF this bij_inv] and assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 317 | have "(\<lambda>x. if x \<in> A then inv_into A f (\<pi> (f x)) else x) permutes A" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 318 | by (simp add: inv_into_inv_into_eq cong: if_cong) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 319 | } | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 320 | from this show ?case by (auto simp: permutes_inv) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 321 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 322 | case 1 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 323 | thus ?case using assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 324 | by (auto simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_left | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 325 | dest: bij_betwE) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 326 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 327 | case 2 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 328 | moreover have "bij_betw (inv_into A f) B A" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 329 | by (intro bij_betw_inv_into assms) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 330 | ultimately show ?case using assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 331 | by (auto simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_right | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 332 | dest: bij_betwE) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 333 | qed | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 334 | qed | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 335 | |
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 336 | lemma bij_betw_derangements: | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 337 | assumes "bij_betw f A B" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 338 | shows "bij_betw (\<lambda>\<pi> x. if x \<in> B then f (\<pi> (inv_into A f x)) else x) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 339 |              {\<pi>. \<pi> permutes A \<and> (\<forall>x\<in>A. \<pi> x \<noteq> x)} {\<pi>. \<pi> permutes B \<and> (\<forall>x\<in>B. \<pi> x \<noteq> x)}" 
 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 340 | (is "bij_betw ?f _ _") | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 341 | proof - | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 342 | let ?g = "(\<lambda>\<pi> x. if x \<in> A then inv_into A f (\<pi> (f x)) else x)" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 343 | show ?thesis | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 344 | proof (rule bij_betw_byWitness [of _ ?g], goal_cases) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 345 | case 3 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 346 | have "?f \<pi> x \<noteq> x" if "\<pi> permutes A" "\<And>x. x \<in> A \<Longrightarrow> \<pi> x \<noteq> x" "x \<in> B" for \<pi> x | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 347 | using that and assms by (metis bij_betwE bij_betw_imp_inj_on bij_betw_imp_surj_on | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 348 | inv_into_f_f inv_into_into permutes_imp_bij) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 349 | with permutes_bij_inv_into[OF _ assms] show ?case by auto | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 350 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 351 | case 4 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 352 | have bij_inv: "bij_betw (inv_into A f) B A" by (intro bij_betw_inv_into assms) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 353 | have "?g \<pi> permutes A" if "\<pi> permutes B" for \<pi> | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 354 | using permutes_bij_inv_into[OF that bij_inv] and assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 355 | by (simp add: inv_into_inv_into_eq cong: if_cong) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 356 | moreover have "?g \<pi> x \<noteq> x" if "\<pi> permutes B" "\<And>x. x \<in> B \<Longrightarrow> \<pi> x \<noteq> x" "x \<in> A" for \<pi> x | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 357 | using that and assms by (metis bij_betwE bij_betw_imp_surj_on f_inv_into_f permutes_imp_bij) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 358 | ultimately show ?case by auto | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 359 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 360 | case 1 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 361 | thus ?case using assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 362 | by (force simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_left | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 363 | dest: bij_betwE) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 364 | next | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 365 | case 2 | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 366 | moreover have "bij_betw (inv_into A f) B A" | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 367 | by (intro bij_betw_inv_into assms) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 368 | ultimately show ?case using assms | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 369 | by (force simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_right | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 370 | dest: bij_betwE) | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 371 | qed | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 372 | qed | 
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 373 | |
| 
ffaaa83543b2
Lemmas about analysis and permutations
 Manuel Eberl <eberlm@in.tum.de> parents: 
65552diff
changeset | 374 | |
| 60500 | 375 | 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 | 376 | |
| 30488 | 377 | lemma permutes_insert_lemma: | 
| 65342 | 378 | assumes "p permutes (insert a S)" | 
| 73648 | 379 | shows "transpose a (p a) \<circ> p permutes S" | 
| 80777 | 380 | proof (rule permutes_superset[where S = "insert a S"]) | 
| 381 | show "Transposition.transpose a (p a) \<circ> p permutes insert a S" | |
| 382 | by (meson assms insertI1 permutes_compose permutes_in_image permutes_swap_id) | |
| 383 | qed auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 384 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 385 | lemma permutes_insert: "{p. p permutes (insert a S)} =
 | 
| 73648 | 386 |   (\<lambda>(b, p). transpose a b \<circ> p) ` {(b, p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
 | 
| 54681 | 387 | proof - | 
| 65342 | 388 | have "p permutes insert a S \<longleftrightarrow> | 
| 73648 | 389 | (\<exists>b q. p = transpose a b \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p | 
| 65342 | 390 | proof - | 
| 73648 | 391 | have "\<exists>b q. p = transpose a b \<circ> q \<and> b \<in> insert a S \<and> q permutes S" | 
| 65342 | 392 | if p: "p permutes insert a S" | 
| 393 | proof - | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 394 | let ?b = "p a" | 
| 73648 | 395 | let ?q = "transpose a (p a) \<circ> p" | 
| 396 | have *: "p = transpose a ?b \<circ> ?q" | |
| 65342 | 397 | by (simp add: fun_eq_iff o_assoc) | 
| 398 | have **: "?b \<in> insert a S" | |
| 399 | unfolding permutes_in_image[OF p] by simp | |
| 400 | from permutes_insert_lemma[OF p] * ** show ?thesis | |
| 73648 | 401 | by blast | 
| 65342 | 402 | qed | 
| 403 | moreover have "p permutes insert a S" | |
| 73648 | 404 | if bq: "p = transpose a b \<circ> q" "b \<in> insert a S" "q permutes S" for b q | 
| 65342 | 405 | proof - | 
| 406 | from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S" | |
| 54681 | 407 | by auto | 
| 65342 | 408 | have a: "a \<in> insert a S" | 
| 54681 | 409 | by simp | 
| 65342 | 410 | from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis | 
| 54681 | 411 | by simp | 
| 65342 | 412 | qed | 
| 413 | ultimately show ?thesis by blast | |
| 414 | qed | |
| 415 | then show ?thesis by auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 416 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 417 | |
| 54681 | 418 | lemma card_permutations: | 
| 65342 | 419 | assumes "card S = n" | 
| 420 | and "finite S" | |
| 33715 | 421 |   shows "card {p. p permutes S} = fact n"
 | 
| 65342 | 422 | using assms(2,1) | 
| 54681 | 423 | proof (induct arbitrary: n) | 
| 424 | case empty | |
| 425 | then show ?case by simp | |
| 33715 | 426 | next | 
| 427 | case (insert x F) | |
| 54681 | 428 |   {
 | 
| 429 | fix n | |
| 72304 | 430 | assume card_insert: "card (insert x F) = n" | 
| 33715 | 431 |     let ?xF = "{p. p permutes insert x F}"
 | 
| 432 |     let ?pF = "{p. p permutes F}"
 | |
| 433 |     let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
 | |
| 73648 | 434 | let ?g = "(\<lambda>(b, p). transpose x b \<circ> p)" | 
| 65342 | 435 | have xfgpF': "?xF = ?g ` ?pF'" | 
| 436 | by (rule permutes_insert[of x F]) | |
| 72304 | 437 | from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1" | 
| 65342 | 438 | by auto | 
| 439 | from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)" | |
| 440 | by auto | |
| 54681 | 441 | then have "finite ?pF" | 
| 59730 
b7c394c7a619
The factorial function, "fact", now has type "nat => 'a"
 paulson <lp15@cam.ac.uk> parents: 
59669diff
changeset | 442 | by (auto intro: card_ge_0_finite) | 
| 72302 
d7d90ed4c74e
fixed some remarkably ugly proofs
 paulson <lp15@cam.ac.uk> parents: 
69895diff
changeset | 443 | with \<open>finite F\<close> card.insert_remove have pF'f: "finite ?pF'" | 
| 80777 | 444 | by simp | 
| 33715 | 445 | have ginj: "inj_on ?g ?pF'" | 
| 54681 | 446 | proof - | 
| 33715 | 447 |       {
 | 
| 54681 | 448 | fix b p c q | 
| 65342 | 449 | assume bp: "(b, p) \<in> ?pF'" | 
| 450 | assume cq: "(c, q) \<in> ?pF'" | |
| 451 | assume eq: "?g (b, p) = ?g (c, q)" | |
| 452 | from bp cq have pF: "p permutes F" and qF: "q permutes F" | |
| 54681 | 453 | by auto | 
| 65342 | 454 | from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x" | 
| 73663 | 455 | by (auto simp: permutes_def fun_upd_def fun_eq_iff) | 
| 65342 | 456 | also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x" | 
| 73466 | 457 | by (auto simp: fun_upd_def fun_eq_iff) | 
| 65342 | 458 | also from qF \<open>x \<notin> F\<close> have "\<dots> = c" | 
| 73663 | 459 | by (auto simp: permutes_def fun_upd_def fun_eq_iff) | 
| 65342 | 460 | finally have "b = c" . | 
| 73663 | 461 | then have "transpose x b = transpose x c" | 
| 54681 | 462 | by simp | 
| 73663 | 463 | with eq have "transpose x b \<circ> p = transpose x b \<circ> q" | 
| 54681 | 464 | by simp | 
| 73663 | 465 | then have "transpose x b \<circ> (transpose x b \<circ> p) = transpose x b \<circ> (transpose x b \<circ> q)" | 
| 54681 | 466 | by simp | 
| 467 | then have "p = q" | |
| 468 | by (simp add: o_assoc) | |
| 65342 | 469 | with \<open>b = c\<close> have "(b, p) = (c, q)" | 
| 54681 | 470 | by simp | 
| 33715 | 471 | } | 
| 54681 | 472 | then show ?thesis | 
| 473 | unfolding inj_on_def by blast | |
| 33715 | 474 | qed | 
| 72304 | 475 | from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0" | 
| 65342 | 476 | by auto | 
| 54681 | 477 | then have "\<exists>m. n = Suc m" | 
| 478 | by presburger | |
| 65342 | 479 | then obtain m where n: "n = Suc m" | 
| 54681 | 480 | by blast | 
| 72304 | 481 | from pFs card_insert have *: "card ?xF = fact n" | 
| 54681 | 482 | unfolding xfgpF' card_image[OF ginj] | 
| 60500 | 483 | using \<open>finite F\<close> \<open>finite ?pF\<close> | 
| 65342 | 484 | by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n) | 
| 54681 | 485 | from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF" | 
| 65342 | 486 | by (simp add: xfgpF' n) | 
| 487 | from * have "card ?xF = fact n" | |
| 488 | unfolding xFf by blast | |
| 33715 | 489 | } | 
| 65342 | 490 | with insert show ?case by simp | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 491 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 492 | |
| 54681 | 493 | lemma finite_permutations: | 
| 65342 | 494 | assumes "finite S" | 
| 54681 | 495 |   shows "finite {p. p permutes S}"
 | 
| 65342 | 496 | 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 | 497 | |
| 54681 | 498 | |
| 73466 | 499 | subsection \<open>Hence a sort of induction principle composing by swaps\<close> | 
| 500 | ||
| 501 | lemma permutes_induct [consumes 2, case_names id swap]: | |
| 502 | \<open>P p\<close> if \<open>p permutes S\<close> \<open>finite S\<close> | |
| 503 | and id: \<open>P id\<close> | |
| 73648 | 504 | and swap: \<open>\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> p permutes S \<Longrightarrow> P p \<Longrightarrow> P (transpose a b \<circ> p)\<close> | 
| 73466 | 505 | using \<open>finite S\<close> \<open>p permutes S\<close> swap proof (induction S arbitrary: p) | 
| 506 | case empty | |
| 507 | with id show ?case | |
| 508 | by (simp only: permutes_empty) | |
| 509 | next | |
| 510 | case (insert x S p) | |
| 73648 | 511 | define q where \<open>q = transpose x (p x) \<circ> p\<close> | 
| 512 | then have swap_q: \<open>transpose x (p x) \<circ> q = p\<close> | |
| 73466 | 513 | by (simp add: o_assoc) | 
| 514 | from \<open>p permutes insert x S\<close> have \<open>q permutes S\<close> | |
| 515 | by (simp add: q_def permutes_insert_lemma) | |
| 516 | then have \<open>q permutes insert x S\<close> | |
| 517 | by (simp add: permutes_imp_permutes_insert) | |
| 518 | from \<open>q permutes S\<close> have \<open>P q\<close> | |
| 519 | by (auto intro: insert.IH insert.prems(2) permutes_imp_permutes_insert) | |
| 520 | have \<open>x \<in> insert x S\<close> | |
| 521 | by simp | |
| 522 | moreover from \<open>p permutes insert x S\<close> have \<open>p x \<in> insert x S\<close> | |
| 523 | using permutes_in_image [of p \<open>insert x S\<close> x] by simp | |
| 73648 | 524 | ultimately have \<open>P (transpose x (p x) \<circ> q)\<close> | 
| 73466 | 525 | using \<open>q permutes insert x S\<close> \<open>P q\<close> | 
| 526 | by (rule insert.prems(2)) | |
| 527 | then show ?case | |
| 528 | by (simp add: swap_q) | |
| 529 | qed | |
| 530 | ||
| 531 | lemma permutes_rev_induct [consumes 2, case_names id swap]: | |
| 532 | \<open>P p\<close> if \<open>p permutes S\<close> \<open>finite S\<close> | |
| 533 | and id': \<open>P id\<close> | |
| 73648 | 534 | and swap': \<open>\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> p permutes S \<Longrightarrow> P p \<Longrightarrow> P (p \<circ> transpose a b)\<close> | 
| 73466 | 535 | using \<open>p permutes S\<close> \<open>finite S\<close> proof (induction rule: permutes_induct) | 
| 536 | case id | |
| 537 | from id' show ?case . | |
| 538 | next | |
| 539 | case (swap a b p) | |
| 73648 | 540 | then have \<open>bij p\<close> | 
| 73663 | 541 | using permutes_bij by blast | 
| 542 | have \<open>P (p \<circ> transpose (inv p a) (inv p b))\<close> | |
| 73466 | 543 | by (rule swap') (auto simp add: swap permutes_in_image permutes_inv) | 
| 73663 | 544 | also have \<open>p \<circ> transpose (inv p a) (inv p b) = transpose a b \<circ> p\<close> | 
| 73648 | 545 | using \<open>bij p\<close> by (rule transpose_comp_eq [symmetric]) | 
| 73466 | 546 | finally show ?case . | 
| 547 | qed | |
| 548 | ||
| 549 | ||
| 60500 | 550 | subsection \<open>Permutations of index set for iterated operations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 551 | |
| 51489 | 552 | lemma (in comm_monoid_set) permute: | 
| 553 | assumes "p permutes S" | |
| 54681 | 554 | shows "F g S = F (g \<circ> p) S" | 
| 51489 | 555 | proof - | 
| 60500 | 556 | from \<open>p permutes S\<close> have "inj p" | 
| 54681 | 557 | by (rule permutes_inj) | 
| 558 | then have "inj_on p S" | |
| 559 | by (auto intro: subset_inj_on) | |
| 560 | then have "F g (p ` S) = F (g \<circ> p) S" | |
| 561 | by (rule reindex) | |
| 60500 | 562 | moreover from \<open>p permutes S\<close> have "p ` S = S" | 
| 54681 | 563 | by (rule permutes_image) | 
| 564 | ultimately show ?thesis | |
| 565 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 566 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 567 | |
| 54681 | 568 | |
| 60500 | 569 | subsection \<open>Permutations as transposition sequences\<close> | 
| 54681 | 570 | |
| 571 | inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
 | |
| 65342 | 572 | where | 
| 573 | id[simp]: "swapidseq 0 id" | |
| 73648 | 574 | | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (transpose a b \<circ> p)" | 
| 54681 | 575 | |
| 576 | declare id[unfolded id_def, simp] | |
| 577 | ||
| 578 | definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 579 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 580 | |
| 60500 | 581 | 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 | 582 | |
| 54681 | 583 | lemma permutation_id[simp]: "permutation id" | 
| 584 | unfolding permutation_def by (rule exI[where x=0]) simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 585 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 586 | declare permutation_id[unfolded id_def, simp] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 587 | |
| 73648 | 588 | lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (transpose a b)" | 
| 80777 | 589 | using swapidseq.simps by fastforce | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 590 | |
| 73648 | 591 | lemma permutation_swap_id: "permutation (transpose a b)" | 
| 80777 | 592 | by (meson permutation_def swapidseq_swap) | 
| 65342 | 593 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 594 | |
| 54681 | 595 | lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)" | 
| 596 | proof (induct n p arbitrary: m q rule: swapidseq.induct) | |
| 597 | case (id m q) | |
| 598 | then show ?case by simp | |
| 599 | next | |
| 600 | case (comp_Suc n p a b m q) | |
| 80777 | 601 | then show ?case | 
| 602 | by (metis add_Suc comp_assoc swapidseq.comp_Suc) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 603 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 604 | |
| 54681 | 605 | 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 | 606 | 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 | 607 | |
| 73648 | 608 | lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> transpose a b)" | 
| 65342 | 609 | by (induct n p rule: swapidseq.induct) | 
| 610 | (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 | 611 | |
| 54681 | 612 | lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id" | 
| 613 | proof (induct n p rule: swapidseq.induct) | |
| 614 | case id | |
| 615 | then show ?case | |
| 616 | by (rule exI[where x=id]) simp | |
| 30488 | 617 | next | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 618 | case (comp_Suc n p a b) | 
| 54681 | 619 | from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 620 | by blast | |
| 73648 | 621 | let ?q = "q \<circ> transpose a b" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 622 | note H = comp_Suc.hyps | 
| 73648 | 623 | from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (transpose a b)" | 
| 54681 | 624 | by simp | 
| 65342 | 625 | from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q" | 
| 54681 | 626 | by simp | 
| 73648 | 627 | have "transpose a b \<circ> p \<circ> ?q = transpose a b \<circ> (p \<circ> q) \<circ> transpose a b" | 
| 54681 | 628 | by (simp add: o_assoc) | 
| 629 | also have "\<dots> = id" | |
| 630 | by (simp add: q(2)) | |
| 73648 | 631 | finally have ***: "transpose a b \<circ> p \<circ> ?q = id" . | 
| 632 | have "?q \<circ> (transpose a b \<circ> p) = q \<circ> (transpose a b \<circ> transpose a b) \<circ> p" | |
| 54681 | 633 | by (simp only: o_assoc) | 
| 73648 | 634 | then have "?q \<circ> (transpose a b \<circ> p) = id" | 
| 54681 | 635 | by (simp add: q(3)) | 
| 65342 | 636 | with ** *** show ?case | 
| 54681 | 637 | by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 638 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 639 | |
| 54681 | 640 | lemma swapidseq_inverse: | 
| 65342 | 641 | assumes "swapidseq n p" | 
| 54681 | 642 | shows "swapidseq n (inv p)" | 
| 65342 | 643 | using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto | 
| 54681 | 644 | |
| 645 | lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)" | |
| 646 | using permutation_def swapidseq_inverse by blast | |
| 647 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 648 | |
| 73328 | 649 | |
| 650 | subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close> | |
| 651 | ||
| 652 | lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow> | |
| 73663 | 653 | transpose a b \<circ> transpose a c = transpose b c \<circ> transpose a b" | 
| 654 | by (simp add: fun_eq_iff transpose_def) | |
| 73328 | 655 | |
| 656 | lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow> | |
| 73663 | 657 | transpose a c \<circ> transpose b c = transpose b c \<circ> transpose a b" | 
| 658 | by (simp add: fun_eq_iff transpose_def) | |
| 73328 | 659 | |
| 660 | lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow> | |
| 73648 | 661 | transpose a b \<circ> transpose c d = transpose c d \<circ> transpose a b" | 
| 73663 | 662 | by (simp add: fun_eq_iff transpose_def) | 
| 73328 | 663 | |
| 664 | ||
| 60500 | 665 | 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 | 666 | |
| 54681 | 667 | lemma symmetry_lemma: | 
| 668 | assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c" | |
| 669 | and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> | |
| 670 | 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> | |
| 671 | P a b c d" | |
| 672 | shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow> P a b c d" | |
| 673 | using assms by metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 674 | |
| 80777 | 675 | lemma swap_general: | 
| 676 | assumes "a \<noteq> b" "c \<noteq> d" | |
| 677 | shows "transpose a b \<circ> transpose c d = id \<or> | |
| 54681 | 678 | (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> | 
| 73648 | 679 | transpose a b \<circ> transpose c d = transpose x y \<circ> transpose a z)" | 
| 80777 | 680 | by (metis assms swap_id_common' swap_id_independent transpose_commute transpose_comp_involutory) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 681 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 682 | lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id" | 
| 65342 | 683 | using swapidseq.cases[of 0 p "p = id"] by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 684 | |
| 54681 | 685 | lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow> | 
| 73648 | 686 | n = 0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = transpose a b \<circ> q \<and> swapidseq m q \<and> a \<noteq> b)" | 
| 80777 | 687 | by (meson comp_Suc id swapidseq.cases) | 
| 54681 | 688 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 689 | lemma fixing_swapidseq_decrease: | 
| 65342 | 690 | assumes "swapidseq n p" | 
| 691 | and "a \<noteq> b" | |
| 73648 | 692 | and "(transpose a b \<circ> p) a = a" | 
| 693 | shows "n \<noteq> 0 \<and> swapidseq (n - 1) (transpose a b \<circ> p)" | |
| 65342 | 694 | using assms | 
| 54681 | 695 | proof (induct n arbitrary: p a b) | 
| 696 | case 0 | |
| 697 | then show ?case | |
| 73663 | 698 | by (auto simp add: fun_upd_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 699 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 700 | case (Suc n p a b) | 
| 54681 | 701 | from Suc.prems(1) swapidseq_cases[of "Suc n" p] | 
| 702 | obtain c d q m where | |
| 73648 | 703 | cdqm: "Suc n = Suc m" "p = transpose c d \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 704 | by auto | 
| 73648 | 705 | consider "transpose a b \<circ> transpose c d = id" | 
| 65342 | 706 | | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y" | 
| 73648 | 707 | "transpose a b \<circ> transpose c d = transpose x y \<circ> transpose a z" | 
| 65342 | 708 | using swap_general[OF Suc.prems(2) cdqm(4)] by metis | 
| 709 | then show ?case | |
| 710 | proof cases | |
| 711 | case 1 | |
| 712 | then show ?thesis | |
| 713 | by (simp only: cdqm o_assoc) (simp add: cdqm) | |
| 714 | next | |
| 80777 | 715 | case 2 | 
| 65342 | 716 | then have az: "a \<noteq> z" | 
| 54681 | 717 | by simp | 
| 80777 | 718 | from 2 have *: "(transpose x y \<circ> h) a = a \<longleftrightarrow> h a = a" for h | 
| 73648 | 719 | by (simp add: transpose_def) | 
| 720 | from cdqm(2) have "transpose a b \<circ> p = transpose a b \<circ> (transpose c d \<circ> q)" | |
| 54681 | 721 | by simp | 
| 80777 | 722 | then have \<section>: "transpose a b \<circ> p = transpose x y \<circ> (transpose a z \<circ> q)" | 
| 723 | by (simp add: o_assoc 2) | |
| 724 | obtain **: "swapidseq (n - 1) (transpose a z \<circ> q)" and "n\<noteq>0" | |
| 725 | by (metis "*" "\<section>" Suc.hyps Suc.prems(3) az cdqm(3,5)) | |
| 726 | then have "Suc n - 1 = Suc (n - 1)" | |
| 65342 | 727 | by auto | 
| 80777 | 728 | with 2 show ?thesis | 
| 729 | using "**" \<section> swapidseq.simps by blast | |
| 65342 | 730 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 731 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 732 | |
| 30488 | 733 | lemma swapidseq_identity_even: | 
| 54681 | 734 | assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)" | 
| 735 | shows "even n" | |
| 60500 | 736 | using \<open>swapidseq n id\<close> | 
| 54681 | 737 | proof (induct n rule: nat_less_induct) | 
| 65342 | 738 | case H: (1 n) | 
| 739 | consider "n = 0" | |
| 73648 | 740 | | a b :: 'a and q m where "n = Suc m" "id = transpose a b \<circ> q" "swapidseq m q" "a \<noteq> b" | 
| 65342 | 741 | using H(2)[unfolded swapidseq_cases[of n id]] by auto | 
| 742 | then show ?case | |
| 743 | proof cases | |
| 744 | case 1 | |
| 745 | then show ?thesis by presburger | |
| 746 | next | |
| 747 | case h: 2 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 748 | from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]] | 
| 54681 | 749 | have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)" | 
| 750 | by auto | |
| 751 | from h m have mn: "m - 1 < n" | |
| 752 | by arith | |
| 65342 | 753 | from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis | 
| 54681 | 754 | by presburger | 
| 65342 | 755 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 756 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 757 | |
| 54681 | 758 | |
| 60500 | 759 | 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 | 760 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 761 | definition "evenperm p = even (SOME n. swapidseq n p)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 762 | |
| 54681 | 763 | lemma swapidseq_even_even: | 
| 764 | assumes m: "swapidseq m p" | |
| 765 | and n: "swapidseq n p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 766 | shows "even m \<longleftrightarrow> even n" | 
| 54681 | 767 | proof - | 
| 65342 | 768 | from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 54681 | 769 | by blast | 
| 65342 | 770 | from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis | 
| 54681 | 771 | by arith | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 772 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 773 | |
| 54681 | 774 | lemma evenperm_unique: | 
| 80777 | 775 | assumes "swapidseq n p" and"even n = b" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 776 | shows "evenperm p = b" | 
| 80777 | 777 | by (metis evenperm_def assms someI swapidseq_even_even) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 778 | |
| 54681 | 779 | |
| 60500 | 780 | subsection \<open>And it has the expected composition properties\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 781 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 782 | lemma evenperm_id[simp]: "evenperm id = True" | 
| 54681 | 783 | by (rule evenperm_unique[where n = 0]) simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 784 | |
| 73621 | 785 | lemma evenperm_identity [simp]: | 
| 786 | \<open>evenperm (\<lambda>x. x)\<close> | |
| 787 | using evenperm_id by (simp add: id_def [abs_def]) | |
| 788 | ||
| 73648 | 789 | lemma evenperm_swap: "evenperm (transpose a b) = (a = b)" | 
| 54681 | 790 | 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 | 791 | |
| 30488 | 792 | lemma evenperm_comp: | 
| 65342 | 793 | assumes "permutation p" "permutation q" | 
| 794 | shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q" | |
| 54681 | 795 | proof - | 
| 65342 | 796 | 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 | 797 | unfolding permutation_def by blast | 
| 65342 | 798 | have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)" | 
| 54681 | 799 | by arith | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 800 | from evenperm_unique[OF n refl] evenperm_unique[OF m refl] | 
| 65342 | 801 | and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis | 
| 54681 | 802 | by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 803 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 804 | |
| 54681 | 805 | lemma evenperm_inv: | 
| 65342 | 806 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 807 | shows "evenperm (inv p) = evenperm p" | 
| 54681 | 808 | proof - | 
| 65342 | 809 | from assms obtain n where n: "swapidseq n p" | 
| 54681 | 810 | unfolding permutation_def by blast | 
| 65342 | 811 | show ?thesis | 
| 812 | 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 | 813 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 814 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 815 | |
| 60500 | 816 | subsection \<open>A more abstract characterization of permutations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 817 | |
| 30488 | 818 | lemma permutation_bijective: | 
| 65342 | 819 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 820 | shows "bij p" | 
| 80777 | 821 | by (meson assms o_bij permutation_def swapidseq_inverse_exists) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 822 | |
| 54681 | 823 | lemma permutation_finite_support: | 
| 65342 | 824 | assumes "permutation p" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 825 |   shows "finite {x. p x \<noteq> x}"
 | 
| 54681 | 826 | proof - | 
| 65342 | 827 | from assms obtain n where "swapidseq n p" | 
| 54681 | 828 | unfolding permutation_def by blast | 
| 65342 | 829 | then show ?thesis | 
| 54681 | 830 | proof (induct n p rule: swapidseq.induct) | 
| 831 | case id | |
| 832 | then show ?case by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 833 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 834 | case (comp_Suc n p a b) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 835 |     let ?S = "insert a (insert b {x. p x \<noteq> x})"
 | 
| 65342 | 836 | from comp_Suc.hyps(2) have *: "finite ?S" | 
| 54681 | 837 | by simp | 
| 73648 | 838 |     from \<open>a \<noteq> b\<close> have **: "{x. (transpose a b \<circ> p) x \<noteq> x} \<subseteq> ?S"
 | 
| 73663 | 839 | by auto | 
| 65342 | 840 | show ?case | 
| 841 | by (rule finite_subset[OF ** *]) | |
| 54681 | 842 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 843 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 844 | |
| 30488 | 845 | lemma permutation_lemma: | 
| 65342 | 846 | assumes "finite S" | 
| 847 | and "bij p" | |
| 73328 | 848 | and "\<forall>x. x \<notin> S \<longrightarrow> p x = x" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 849 | shows "permutation p" | 
| 65342 | 850 | using assms | 
| 54681 | 851 | proof (induct S arbitrary: p rule: finite_induct) | 
| 65342 | 852 | case empty | 
| 853 | then show ?case | |
| 854 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 855 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 856 | case (insert a F p) | 
| 73663 | 857 | let ?r = "transpose a (p a) \<circ> p" | 
| 858 | let ?q = "transpose a (p a) \<circ> ?r" | |
| 65342 | 859 | have *: "?r a = a" | 
| 73663 | 860 | by simp | 
| 65342 | 861 | 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 | 862 | by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3)) | 
| 65342 | 863 | have "bij ?r" | 
| 73663 | 864 | using insert by (simp add: bij_comp) | 
| 65342 | 865 | have "permutation ?r" | 
| 866 | by (rule insert(3)[OF \<open>bij ?r\<close> **]) | |
| 867 | then have "permutation ?q" | |
| 868 | by (simp add: permutation_compose permutation_swap_id) | |
| 54681 | 869 | then show ?case | 
| 870 | by (simp add: o_assoc) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 871 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 872 | |
| 30488 | 873 | lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
 | 
| 80777 | 874 | using permutation_bijective permutation_finite_support permutation_lemma by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 875 | |
| 54681 | 876 | lemma permutation_inverse_works: | 
| 65342 | 877 | assumes "permutation p" | 
| 54681 | 878 | shows "inv p \<circ> p = id" | 
| 879 | and "p \<circ> inv p = id" | |
| 65342 | 880 | 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 | 881 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 882 | lemma permutation_inverse_compose: | 
| 54681 | 883 | assumes p: "permutation p" | 
| 884 | and q: "permutation q" | |
| 885 | shows "inv (p \<circ> q) = inv q \<circ> inv p" | |
| 80777 | 886 | by (simp add: o_inv_distrib p permutation_bijective q) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 887 | |
| 54681 | 888 | |
| 65342 | 889 | subsection \<open>Relation to \<open>permutes\<close>\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 890 | |
| 73466 | 891 | lemma permutes_imp_permutation: | 
| 892 | \<open>permutation p\<close> if \<open>finite S\<close> \<open>p permutes S\<close> | |
| 893 | proof - | |
| 894 |   from \<open>p permutes S\<close> have \<open>{x. p x \<noteq> x} \<subseteq> S\<close>
 | |
| 895 | by (auto dest: permutes_not_in) | |
| 896 |   then have \<open>finite {x. p x \<noteq> x}\<close>
 | |
| 897 | using \<open>finite S\<close> by (rule finite_subset) | |
| 898 | moreover from \<open>p permutes S\<close> have \<open>bij p\<close> | |
| 899 | by (auto dest: permutes_bij) | |
| 900 | ultimately show ?thesis | |
| 901 | by (simp add: permutation) | |
| 902 | qed | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 903 | |
| 73466 | 904 | lemma permutation_permutesE: | 
| 905 | assumes \<open>permutation p\<close> | |
| 906 | obtains S where \<open>finite S\<close> \<open>p permutes S\<close> | |
| 907 | proof - | |
| 908 |   from assms have fin: \<open>finite {x. p x \<noteq> x}\<close>
 | |
| 909 | by (simp add: permutation) | |
| 910 | from assms have \<open>bij p\<close> | |
| 911 | by (simp add: permutation) | |
| 912 |   also have \<open>UNIV = {x. p x \<noteq> x} \<union> {x. p x = x}\<close>
 | |
| 913 | by auto | |
| 914 |   finally have \<open>bij_betw p {x. p x \<noteq> x} {x. p x \<noteq> x}\<close>
 | |
| 915 | by (rule bij_betw_partition) (auto simp add: bij_betw_fixpoints) | |
| 916 |   then have \<open>p permutes {x. p x \<noteq> x}\<close>
 | |
| 917 | by (auto intro: bij_imp_permutes) | |
| 918 | with fin show thesis .. | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 919 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 920 | |
| 73466 | 921 | lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)" | 
| 922 | by (auto elim: permutation_permutesE intro: permutes_imp_permutation) | |
| 923 | ||
| 54681 | 924 | |
| 60500 | 925 | 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 | 926 | |
| 73328 | 927 | definition sign :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> int\<close> \<comment> \<open>TODO: prefer less generic name\<close>
 | 
| 73621 | 928 | where \<open>sign p = (if evenperm p then 1 else - 1)\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 929 | |
| 73621 | 930 | lemma sign_cases [case_names even odd]: | 
| 931 | obtains \<open>sign p = 1\<close> | \<open>sign p = - 1\<close> | |
| 932 | by (cases \<open>evenperm p\<close>) (simp_all add: sign_def) | |
| 933 | ||
| 934 | lemma sign_nz [simp]: "sign p \<noteq> 0" | |
| 935 | by (cases p rule: sign_cases) simp_all | |
| 936 | ||
| 937 | lemma sign_id [simp]: "sign id = 1" | |
| 54681 | 938 | by (simp add: sign_def) | 
| 939 | ||
| 73621 | 940 | lemma sign_identity [simp]: | 
| 941 | \<open>sign (\<lambda>x. x) = 1\<close> | |
| 54681 | 942 | by (simp add: sign_def) | 
| 943 | ||
| 944 | 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 | 945 | by (simp add: sign_def evenperm_inv) | 
| 54681 | 946 | |
| 947 | lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q" | |
| 948 | by (simp add: sign_def evenperm_comp) | |
| 949 | ||
| 73648 | 950 | lemma sign_swap_id: "sign (transpose a b) = (if a = b then 1 else - 1)" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 951 | by (simp add: sign_def evenperm_swap) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 952 | |
| 73621 | 953 | lemma sign_idempotent [simp]: "sign p * sign p = 1" | 
| 54681 | 954 | by (simp add: sign_def) | 
| 955 | ||
| 73621 | 956 | lemma sign_left_idempotent [simp]: | 
| 957 | \<open>sign p * (sign p * sign q) = sign q\<close> | |
| 958 | by (simp add: sign_def) | |
| 959 | ||
| 960 | term "(bij, bij_betw, permutation)" | |
| 961 | ||
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 962 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 963 | subsection \<open>Permuting a list\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 964 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 965 | 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 | 966 | |
| 65342 | 967 | definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list" | 
| 968 | 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 | 969 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 970 | lemma permute_list_map: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 971 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 972 | 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 | 973 | 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 | 974 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 975 | lemma permute_list_nth: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 976 |   assumes "f permutes {..<length xs}" "i < length xs"
 | 
| 65342 | 977 | 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 | 978 | using permutes_in_image[OF assms(1)] assms(2) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 979 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 980 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 981 | lemma permute_list_Nil [simp]: "permute_list f [] = []" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 982 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 983 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 984 | 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 | 985 | by (simp add: permute_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 986 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 987 | lemma permute_list_compose: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 988 |   assumes "g permutes {..<length xs}"
 | 
| 65342 | 989 | 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 | 990 | 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 | 991 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 992 | 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 | 993 | by (simp add: permute_list_def map_nth) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 994 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 995 | lemma permute_list_id [simp]: "permute_list id xs = xs" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 996 | by (simp add: id_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 997 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 998 | lemma mset_permute_list [simp]: | 
| 65342 | 999 | fixes xs :: "'a list" | 
| 1000 |   assumes "f permutes {..<length xs}"
 | |
| 1001 | shows "mset (permute_list f xs) = mset xs" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1002 | proof (rule multiset_eqI) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1003 | fix y :: 'a | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1004 | 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 | 1005 | using permutes_in_image[OF assms] by auto | 
| 65342 | 1006 |   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 | 1007 | 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 | 1008 |   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 | 1009 | by auto | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1010 |   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 | 1011 | by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj) | 
| 65342 | 1012 | also have "\<dots> = count (mset xs) y" | 
| 1013 | by (simp add: count_mset length_filter_conv_card) | |
| 1014 | finally show "count (mset (permute_list f xs)) y = count (mset xs) y" | |
| 1015 | by simp | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1016 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1017 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 1018 | lemma set_permute_list [simp]: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1019 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 1020 | shows "set (permute_list f xs) = set xs" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1021 | by (rule mset_eq_setD[OF mset_permute_list]) fact | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1022 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1023 | lemma distinct_permute_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1024 |   assumes "f permutes {..<length xs}"
 | 
| 65342 | 1025 | shows "distinct (permute_list f xs) = distinct xs" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1026 | by (simp add: distinct_count_atmost_1 assms) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1027 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 1028 | lemma permute_list_zip: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1029 |   assumes "f permutes A" "A = {..<length xs}"
 | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1030 | assumes [simp]: "length xs = length ys" | 
| 65342 | 1031 | 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 | 1032 | proof - | 
| 65342 | 1033 | from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i | 
| 1034 | by simp | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1035 | 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 | 1036 | by (simp_all add: permute_list_def zip_map_map) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1037 | also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])" | 
| 65342 | 1038 | by (intro nth_equalityI) (simp_all add: *) | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1039 | 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 | 1040 | by (simp_all add: permute_list_def zip_map_map) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1041 | finally show ?thesis . | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1042 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1043 | |
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 1044 | lemma map_of_permute: | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1045 | assumes "\<sigma> permutes fst ` set xs" | 
| 65342 | 1046 | shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)" | 
| 1047 | (is "_ = map_of (map ?f _)") | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1048 | proof | 
| 65342 | 1049 | from assms have "inj \<sigma>" "surj \<sigma>" | 
| 1050 | by (simp_all add: permutes_inj permutes_surj) | |
| 1051 | then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x | |
| 1052 | 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 | 1053 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1054 | |
| 73706 | 1055 | lemma list_all2_permute_list_iff: | 
| 1056 | \<open>list_all2 P (permute_list p xs) (permute_list p ys) \<longleftrightarrow> list_all2 P xs ys\<close> | |
| 1057 |   if \<open>p permutes {..<length xs}\<close>
 | |
| 1058 | using that by (auto simp add: list_all2_iff simp flip: permute_list_zip) | |
| 1059 | ||
| 54681 | 1060 | |
| 60500 | 1061 | subsection \<open>More lemmas about permutations\<close> | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1062 | |
| 73555 | 1063 | lemma permutes_in_funpow_image: \<^marker>\<open>contributor \<open>Lars Noschinski\<close>\<close> | 
| 1064 | assumes "f permutes S" "x \<in> S" | |
| 1065 | shows "(f ^^ n) x \<in> S" | |
| 1066 | using assms by (induction n) (auto simp: permutes_in_image) | |
| 1067 | ||
| 1068 | lemma permutation_self: \<^marker>\<open>contributor \<open>Lars Noschinski\<close>\<close> | |
| 1069 | assumes \<open>permutation p\<close> | |
| 1070 | obtains n where \<open>n > 0\<close> \<open>(p ^^ n) x = x\<close> | |
| 1071 | proof (cases \<open>p x = x\<close>) | |
| 1072 | case True | |
| 1073 | with that [of 1] show thesis by simp | |
| 1074 | next | |
| 1075 | case False | |
| 1076 | from \<open>permutation p\<close> have \<open>inj p\<close> | |
| 1077 | by (intro permutation_bijective bij_is_inj) | |
| 1078 | moreover from \<open>p x \<noteq> x\<close> have \<open>(p ^^ Suc n) x \<noteq> (p ^^ n) x\<close> for n | |
| 1079 | proof (induction n arbitrary: x) | |
| 1080 | case 0 then show ?case by simp | |
| 1081 | next | |
| 1082 | case (Suc n) | |
| 1083 | have "p (p x) \<noteq> p x" | |
| 1084 | proof (rule notI) | |
| 1085 | assume "p (p x) = p x" | |
| 1086 | then show False using \<open>p x \<noteq> x\<close> \<open>inj p\<close> by (simp add: inj_eq) | |
| 1087 | qed | |
| 1088 | have "(p ^^ Suc (Suc n)) x = (p ^^ Suc n) (p x)" | |
| 1089 | by (simp add: funpow_swap1) | |
| 1090 | also have "\<dots> \<noteq> (p ^^ n) (p x)" | |
| 1091 | by (rule Suc) fact | |
| 1092 | also have "(p ^^ n) (p x) = (p ^^ Suc n) x" | |
| 1093 | by (simp add: funpow_swap1) | |
| 1094 | finally show ?case by simp | |
| 1095 | qed | |
| 1096 |   then have "{y. \<exists>n. y = (p ^^ n) x} \<subseteq> {x. p x \<noteq> x}"
 | |
| 1097 | by auto | |
| 1098 |   then have "finite {y. \<exists>n. y = (p ^^ n) x}"
 | |
| 1099 | using permutation_finite_support[OF assms] by (rule finite_subset) | |
| 1100 | ultimately obtain n where \<open>n > 0\<close> \<open>(p ^^ n) x = x\<close> | |
| 1101 | by (rule funpow_inj_finite) | |
| 1102 | with that [of n] show thesis by blast | |
| 1103 | qed | |
| 1104 | ||
| 65342 | 1105 | 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 | 1106 | |
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1107 | lemma count_image_mset_eq_card_vimage: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1108 | assumes "finite A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1109 |   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 | 1110 | using assms | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1111 | proof (induct A) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1112 | case empty | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1113 | show ?case by simp | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1114 | next | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1115 | case (insert x F) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1116 | show ?case | 
| 65342 | 1117 | proof (cases "f x = b") | 
| 1118 | case True | |
| 1119 | with insert.hyps | |
| 1120 |     have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
 | |
| 1121 | by auto | |
| 1122 |     also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
 | |
| 1123 | by simp | |
| 1124 |     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}"
 | |
| 1125 | by (auto intro: arg_cong[where f="card"]) | |
| 1126 | finally show ?thesis | |
| 1127 | using insert by auto | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1128 | next | 
| 65342 | 1129 | case False | 
| 1130 |     then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
 | |
| 1131 | by auto | |
| 1132 | with insert False show ?thesis | |
| 1133 | by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1134 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1135 | qed | 
| 64284 
f3b905b2eee2
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
 hoelzl parents: 
64267diff
changeset | 1136 | |
| 67408 | 1137 | \<comment> \<open>Prove \<open>image_mset_eq_implies_permutes\<close> ...\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1138 | lemma image_mset_eq_implies_permutes: | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1139 | fixes f :: "'a \<Rightarrow> 'b" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1140 | assumes "finite A" | 
| 65342 | 1141 | 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 | 1142 | 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 | 1143 | proof - | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1144 |   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 | 1145 | have "f ` A = f' ` A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1146 | proof - | 
| 65342 | 1147 | from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))" | 
| 1148 | by simp | |
| 1149 | 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 | 1150 | by (metis mset_eq multiset.set_map) | 
| 65342 | 1151 | also from \<open>finite A\<close> have "\<dots> = f' ` A" | 
| 1152 | by simp | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1153 | finally show ?thesis . | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1154 | qed | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1155 |   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 | 1156 | proof | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1157 | fix b | 
| 65342 | 1158 | from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b" | 
| 1159 | by simp | |
| 1160 |     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 | 1161 | by (simp add: count_image_mset_eq_card_vimage) | 
| 65342 | 1162 |     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 | 1163 | by (intro finite_same_card_bij) simp_all | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1164 | qed | 
| 65342 | 1165 |   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 | 1166 | by (rule bchoice) | 
| 65342 | 1167 |   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 | 1168 | 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 | 1169 | have "p' permutes A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1170 | proof (rule bij_imp_permutes) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1171 |     have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
 | 
| 65342 | 1172 | by (auto simp: disjoint_family_on_def) | 
| 1173 | moreover | |
| 1174 |     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
 | |
| 1175 | using p that by (subst bij_betw_cong[where g="p b"]) auto | |
| 1176 | ultimately | |
| 1177 |     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 | 1178 | by (rule bij_betw_UNION_disjoint) | 
| 65342 | 1179 |     moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
 | 
| 1180 | by auto | |
| 1181 |     moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
 | |
| 1182 | by auto | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1183 | ultimately show "bij_betw p' A A" | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1184 | 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 | 1185 | next | 
| 65342 | 1186 | show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x" | 
| 1187 | by (simp add: p'_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1188 | qed | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1189 | 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 | 1190 | unfolding p'_def using bij_betwE by fastforce | 
| 65342 | 1191 | ultimately show ?thesis .. | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1192 | qed | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1193 | |
| 67408 | 1194 | \<comment> \<open>... and derive the existing property:\<close> | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1195 | lemma mset_eq_permutation: | 
| 65342 | 1196 | fixes xs ys :: "'a list" | 
| 1197 | assumes mset_eq: "mset xs = mset ys" | |
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1198 |   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 | 1199 | proof - | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1200 | from mset_eq have length_eq: "length xs = length ys" | 
| 65342 | 1201 | by (rule mset_eq_length) | 
| 63921 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1202 |   have "mset_set {..<length ys} = mset [0..<length ys]"
 | 
| 65342 | 1203 | by (rule mset_set_upto_eq_mset_upto) | 
| 1204 |   with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
 | |
| 1205 |     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 | 1206 | by (metis map_nth mset_map) | 
| 
0a5184877cb7
Additions to permutations (contributed by Lukas Bulwahn)
 eberlm <eberlm@in.tum.de> parents: 
63539diff
changeset | 1207 | from image_mset_eq_implies_permutes[OF _ this] | 
| 65342 | 1208 |   obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
 | 
| 1209 | by auto | |
| 1210 | with length_eq have "permute_list p ys = xs" | |
| 1211 | by (auto intro!: nth_equalityI simp: permute_list_nth) | |
| 1212 | with p show thesis .. | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1213 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1214 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1215 | lemma permutes_natset_le: | 
| 54681 | 1216 | fixes S :: "'a::wellorder set" | 
| 65342 | 1217 | assumes "p permutes S" | 
| 1218 | and "\<forall>i \<in> S. p i \<le> i" | |
| 54681 | 1219 | shows "p = id" | 
| 1220 | proof - | |
| 65342 | 1221 | have "p n = n" for n | 
| 1222 | using assms | |
| 1223 | proof (induct n arbitrary: S rule: less_induct) | |
| 1224 | case (less n) | |
| 1225 | show ?case | |
| 1226 | proof (cases "n \<in> S") | |
| 1227 | case False | |
| 1228 | with less(2) show ?thesis | |
| 1229 | unfolding permutes_def by metis | |
| 1230 | next | |
| 1231 | case True | |
| 1232 | with less(3) have "p n < n \<or> p n = n" | |
| 1233 | by auto | |
| 1234 | then show ?thesis | |
| 1235 | proof | |
| 1236 | assume "p n < n" | |
| 1237 | with less have "p (p n) = p n" | |
| 1238 | by metis | |
| 1239 | with permutes_inj[OF less(2)] have "p n = n" | |
| 1240 | unfolding inj_def by blast | |
| 1241 | with \<open>p n < n\<close> have False | |
| 1242 | by simp | |
| 1243 | then show ?thesis .. | |
| 1244 | qed | |
| 54681 | 1245 | qed | 
| 65342 | 1246 | qed | 
| 1247 | then show ?thesis by (auto simp: fun_eq_iff) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1248 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1249 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1250 | lemma permutes_natset_ge: | 
| 54681 | 1251 | fixes S :: "'a::wellorder set" | 
| 1252 | assumes p: "p permutes S" | |
| 1253 | and le: "\<forall>i \<in> S. p i \<ge> i" | |
| 1254 | shows "p = id" | |
| 1255 | proof - | |
| 65342 | 1256 | have "i \<ge> inv p i" if "i \<in> S" for i | 
| 1257 | proof - | |
| 1258 | from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S" | |
| 54681 | 1259 | by simp | 
| 1260 | with le have "p (inv p i) \<ge> inv p i" | |
| 1261 | by blast | |
| 65342 | 1262 | with permutes_inverses[OF p] show ?thesis | 
| 54681 | 1263 | by simp | 
| 65342 | 1264 | qed | 
| 1265 | then have "\<forall>i\<in>S. inv p i \<le> i" | |
| 54681 | 1266 | by blast | 
| 65342 | 1267 | from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id" | 
| 54681 | 1268 | by simp | 
| 30488 | 1269 | then show ?thesis | 
| 80777 | 1270 | using p permutes_inv_inv by fastforce | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1271 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1272 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1273 | lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
 | 
| 80777 | 1274 | using permutes_inv permutes_inv_inv by force | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1275 | |
| 30488 | 1276 | lemma image_compose_permutations_left: | 
| 65342 | 1277 | assumes "q permutes S" | 
| 1278 |   shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
 | |
| 80777 | 1279 | proof - | 
| 1280 | have "\<And>p. p permutes S \<Longrightarrow> q \<circ> p permutes S" | |
| 1281 | by (simp add: assms permutes_compose) | |
| 1282 | moreover have "\<And>x. x permutes S \<Longrightarrow> \<exists>p. x = q \<circ> p \<and> p permutes S" | |
| 1283 | by (metis assms id_comp o_assoc permutes_compose permutes_inv permutes_inv_o(1)) | |
| 1284 | ultimately show ?thesis | |
| 1285 | by auto | |
| 1286 | qed | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1287 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1288 | lemma image_compose_permutations_right: | 
| 65342 | 1289 | assumes "q permutes S" | 
| 54681 | 1290 |   shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
 | 
| 80777 | 1291 | by (metis (no_types, opaque_lifting) assms comp_id fun.map_comp permutes_compose permutes_inv permutes_inv_o(2)) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1292 | |
| 54681 | 1293 | lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
 | 
| 1294 | by (simp add: permutes_def) metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1295 | |
| 65342 | 1296 | lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
 | 
| 54681 | 1297 | (is "?lhs = ?rhs") | 
| 1298 | proof - | |
| 30036 | 1299 |   let ?S = "{p . p permutes S}"
 | 
| 65342 | 1300 | have *: "inj_on inv ?S" | 
| 54681 | 1301 | proof (auto simp add: inj_on_def) | 
| 1302 | fix q r | |
| 1303 | assume q: "q permutes S" | |
| 1304 | and r: "r permutes S" | |
| 1305 | and qr: "inv q = inv r" | |
| 1306 | then have "inv (inv q) = inv (inv r)" | |
| 1307 | by simp | |
| 1308 | with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r" | |
| 1309 | by metis | |
| 1310 | qed | |
| 65342 | 1311 | have **: "inv ` ?S = ?S" | 
| 54681 | 1312 | using image_inverse_permutations by blast | 
| 65342 | 1313 | have ***: "?rhs = sum (f \<circ> inv) ?S" | 
| 54681 | 1314 | by (simp add: o_def) | 
| 65342 | 1315 | from sum.reindex[OF *, of f] show ?thesis | 
| 1316 | by (simp only: ** ***) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1317 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1318 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1319 | lemma setum_permutations_compose_left: | 
| 30036 | 1320 | assumes q: "q permutes S" | 
| 64267 | 1321 |   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
 | 
| 54681 | 1322 | (is "?lhs = ?rhs") | 
| 1323 | proof - | |
| 30036 | 1324 |   let ?S = "{p. p permutes S}"
 | 
| 67399 | 1325 | have *: "?rhs = sum (f \<circ> ((\<circ>) q)) ?S" | 
| 54681 | 1326 | by (simp add: o_def) | 
| 67399 | 1327 | have **: "inj_on ((\<circ>) q) ?S" | 
| 54681 | 1328 | proof (auto simp add: inj_on_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1329 | fix p r | 
| 54681 | 1330 | assume "p permutes S" | 
| 1331 | and r: "r permutes S" | |
| 1332 | and rp: "q \<circ> p = q \<circ> r" | |
| 1333 | then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r" | |
| 1334 | by (simp add: comp_assoc) | |
| 1335 | with permutes_inj[OF q, unfolded inj_iff] show "p = r" | |
| 1336 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1337 | qed | 
| 67399 | 1338 | have "((\<circ>) q) ` ?S = ?S" | 
| 54681 | 1339 | using image_compose_permutations_left[OF q] by auto | 
| 65342 | 1340 | with * sum.reindex[OF **, of f] show ?thesis | 
| 1341 | by (simp only:) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1342 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1343 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1344 | lemma sum_permutations_compose_right: | 
| 30036 | 1345 | assumes q: "q permutes S" | 
| 64267 | 1346 |   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
 | 
| 54681 | 1347 | (is "?lhs = ?rhs") | 
| 1348 | proof - | |
| 30036 | 1349 |   let ?S = "{p. p permutes S}"
 | 
| 65342 | 1350 | have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S" | 
| 54681 | 1351 | by (simp add: o_def) | 
| 65342 | 1352 | have **: "inj_on (\<lambda>p. p \<circ> q) ?S" | 
| 54681 | 1353 | proof (auto simp add: inj_on_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1354 | fix p r | 
| 54681 | 1355 | assume "p permutes S" | 
| 1356 | and r: "r permutes S" | |
| 1357 | and rp: "p \<circ> q = r \<circ> q" | |
| 1358 | then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)" | |
| 1359 | by (simp add: o_assoc) | |
| 1360 | with permutes_surj[OF q, unfolded surj_iff] show "p = r" | |
| 1361 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1362 | qed | 
| 65342 | 1363 | from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S" | 
| 1364 | by auto | |
| 1365 | with * sum.reindex[OF **, of f] show ?thesis | |
| 1366 | by (simp only:) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1367 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1368 | |
| 73621 | 1369 | lemma inv_inj_on_permutes: | 
| 1370 |   \<open>inj_on inv {p. p permutes S}\<close>
 | |
| 1371 | proof (intro inj_onI, unfold mem_Collect_eq) | |
| 1372 | fix p q | |
| 1373 | assume p: "p permutes S" and q: "q permutes S" and eq: "inv p = inv q" | |
| 1374 | have "inv (inv p) = inv (inv q)" using eq by simp | |
| 1375 | thus "p = q" | |
| 1376 | using inv_inv_eq[OF permutes_bij] p q by metis | |
| 1377 | qed | |
| 1378 | ||
| 1379 | lemma permutes_pair_eq: | |
| 1380 |   \<open>{(p s, s) |s. s \<in> S} = {(s, inv p s) |s. s \<in> S}\<close> (is \<open>?L = ?R\<close>) if \<open>p permutes S\<close>
 | |
| 1381 | proof | |
| 1382 | show "?L \<subseteq> ?R" | |
| 1383 | proof | |
| 1384 | fix x assume "x \<in> ?L" | |
| 1385 | then obtain s where x: "x = (p s, s)" and s: "s \<in> S" by auto | |
| 1386 | note x | |
| 1387 | also have "(p s, s) = (p s, Hilbert_Choice.inv p (p s))" | |
| 1388 | using permutes_inj [OF that] inv_f_f by auto | |
| 1389 | also have "... \<in> ?R" using s permutes_in_image[OF that] by auto | |
| 1390 | finally show "x \<in> ?R". | |
| 1391 | qed | |
| 1392 | show "?R \<subseteq> ?L" | |
| 1393 | proof | |
| 1394 | fix x assume "x \<in> ?R" | |
| 1395 | then obtain s | |
| 1396 | where x: "x = (s, Hilbert_Choice.inv p s)" (is "_ = (s, ?ips)") | |
| 1397 | and s: "s \<in> S" by auto | |
| 1398 | note x | |
| 1399 | also have "(s, ?ips) = (p ?ips, ?ips)" | |
| 1400 | using inv_f_f[OF permutes_inj[OF permutes_inv[OF that]]] | |
| 1401 | using inv_inv_eq[OF permutes_bij[OF that]] by auto | |
| 1402 | also have "... \<in> ?L" | |
| 1403 | using s permutes_in_image[OF permutes_inv[OF that]] by auto | |
| 1404 | finally show "x \<in> ?L". | |
| 1405 | qed | |
| 1406 | qed | |
| 1407 | ||
| 1408 | context | |
| 1409 | fixes p and n i :: nat | |
| 1410 |   assumes p: \<open>p permutes {0..<n}\<close> and i: \<open>i < n\<close>
 | |
| 1411 | begin | |
| 1412 | ||
| 1413 | lemma permutes_nat_less: | |
| 1414 | \<open>p i < n\<close> | |
| 1415 | proof - | |
| 1416 |   have \<open>?thesis \<longleftrightarrow> p i \<in> {0..<n}\<close>
 | |
| 1417 | by simp | |
| 1418 |   also from p have \<open>p i \<in> {0..<n} \<longleftrightarrow> i \<in> {0..<n}\<close>
 | |
| 1419 | by (rule permutes_in_image) | |
| 1420 | finally show ?thesis | |
| 1421 | using i by simp | |
| 1422 | qed | |
| 1423 | ||
| 1424 | lemma permutes_nat_inv_less: | |
| 1425 | \<open>inv p i < n\<close> | |
| 1426 | proof - | |
| 1427 |   from p have \<open>inv p permutes {0..<n}\<close>
 | |
| 1428 | by (rule permutes_inv) | |
| 1429 | then show ?thesis | |
| 1430 | using i by (rule Permutations.permutes_nat_less) | |
| 1431 | qed | |
| 1432 | ||
| 1433 | end | |
| 1434 | ||
| 1435 | context comm_monoid_set | |
| 1436 | begin | |
| 1437 | ||
| 1438 | lemma permutes_inv: | |
| 1439 | \<open>F (\<lambda>s. g (p s) s) S = F (\<lambda>s. g s (inv p s)) S\<close> (is \<open>?l = ?r\<close>) | |
| 1440 | if \<open>p permutes S\<close> | |
| 1441 | proof - | |
| 1442 | let ?g = "\<lambda>(x, y). g x y" | |
| 1443 | let ?ps = "\<lambda>s. (p s, s)" | |
| 1444 | let ?ips = "\<lambda>s. (s, inv p s)" | |
| 1445 | have inj1: "inj_on ?ps S" by (rule inj_onI) auto | |
| 1446 | have inj2: "inj_on ?ips S" by (rule inj_onI) auto | |
| 1447 | have "?l = F ?g (?ps ` S)" | |
| 1448 | using reindex [OF inj1, of ?g] by simp | |
| 1449 |   also have "?ps ` S = {(p s, s) |s. s \<in> S}" by auto
 | |
| 1450 |   also have "... = {(s, inv p s) |s. s \<in> S}"
 | |
| 1451 | unfolding permutes_pair_eq [OF that] by simp | |
| 1452 | also have "... = ?ips ` S" by auto | |
| 1453 | also have "F ?g ... = ?r" | |
| 1454 | using reindex [OF inj2, of ?g] by simp | |
| 1455 | finally show ?thesis. | |
| 1456 | qed | |
| 1457 | ||
| 1458 | end | |
| 1459 | ||
| 54681 | 1460 | |
| 60500 | 1461 | 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 | 1462 | |
| 64267 | 1463 | lemma sum_over_permutations_insert: | 
| 54681 | 1464 | assumes fS: "finite S" | 
| 1465 | and aS: "a \<notin> S" | |
| 64267 | 1466 |   shows "sum f {p. p permutes (insert a S)} =
 | 
| 73648 | 1467 |     sum (\<lambda>b. sum (\<lambda>q. f (transpose a b \<circ> q)) {p. p permutes S}) (insert a S)"
 | 
| 54681 | 1468 | proof - | 
| 73648 | 1469 | have *: "\<And>f a b. (\<lambda>(b, p). f (transpose a b \<circ> p)) = f \<circ> (\<lambda>(b,p). transpose a b \<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 | 1470 | by (simp add: fun_eq_iff) | 
| 65342 | 1471 |   have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
 | 
| 54681 | 1472 | by blast | 
| 30488 | 1473 | show ?thesis | 
| 65342 | 1474 | unfolding * ** sum.cartesian_product permutes_insert | 
| 64267 | 1475 | proof (rule sum.reindex) | 
| 73648 | 1476 | let ?f = "(\<lambda>(b, y). transpose a b \<circ> y)" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1477 |     let ?P = "{p. p permutes S}"
 | 
| 54681 | 1478 |     {
 | 
| 1479 | fix b c p q | |
| 1480 | assume b: "b \<in> insert a S" | |
| 1481 | assume c: "c \<in> insert a S" | |
| 1482 | assume p: "p permutes S" | |
| 1483 | assume q: "q permutes S" | |
| 73648 | 1484 | assume eq: "transpose a b \<circ> p = transpose a c \<circ> q" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1485 | 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 | 1486 | unfolding permutes_def by metis+ | 
| 73648 | 1487 | from eq have "(transpose a b \<circ> p) a = (transpose a c \<circ> q) a" | 
| 54681 | 1488 | by simp | 
| 1489 | then have bc: "b = c" | |
| 73663 | 1490 | by (simp add: permutes_def pa qa o_def fun_upd_def id_def | 
| 62390 | 1491 | cong del: if_weak_cong split: if_split_asm) | 
| 73648 | 1492 | from eq[unfolded bc] have "(\<lambda>p. transpose a c \<circ> p) (transpose a c \<circ> p) = | 
| 1493 | (\<lambda>p. transpose a c \<circ> p) (transpose a c \<circ> q)" by simp | |
| 54681 | 1494 | then have "p = q" | 
| 65342 | 1495 | unfolding o_assoc swap_id_idempotent by simp | 
| 54681 | 1496 | with bc have "b = c \<and> p = q" | 
| 1497 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1498 | } | 
| 30488 | 1499 | then show "inj_on ?f (insert a S \<times> ?P)" | 
| 54681 | 1500 | unfolding inj_on_def by clarify metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1501 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1502 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1503 | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1504 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1505 | subsection \<open>Constructing permutations from association lists\<close> | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1506 | |
| 65342 | 1507 | definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
 | 
| 1508 | where "list_permutes xs A \<longleftrightarrow> | |
| 1509 | set (map fst xs) \<subseteq> A \<and> | |
| 1510 | set (map snd xs) = set (map fst xs) \<and> | |
| 1511 | distinct (map fst xs) \<and> | |
| 1512 | distinct (map snd xs)" | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1513 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1514 | lemma list_permutesI [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1515 | assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)" | 
| 65342 | 1516 | shows "list_permutes xs A" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1517 | proof - | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1518 | from assms(2,3) have "distinct (map snd xs)" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1519 | by (intro card_distinct) (simp_all add: distinct_card del: set_map) | 
| 65342 | 1520 | with assms show ?thesis | 
| 1521 | by (simp add: list_permutes_def) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1522 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1523 | |
| 65342 | 1524 | definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
 | 
| 1525 | 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 | 1526 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1527 | lemma permutation_of_list_Cons: | 
| 65342 | 1528 | "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 | 1529 | by (simp add: permutation_of_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1530 | |
| 65342 | 1531 | fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
 | 
| 1532 | where | |
| 1533 | "inverse_permutation_of_list [] x = x" | |
| 1534 | | "inverse_permutation_of_list ((y, x') # xs) x = | |
| 1535 | (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 | 1536 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1537 | declare inverse_permutation_of_list.simps [simp del] | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1538 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1539 | lemma inj_on_map_of: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1540 | assumes "distinct (map snd xs)" | 
| 65342 | 1541 | 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 | 1542 | proof (rule inj_onI) | 
| 65342 | 1543 | fix x y | 
| 1544 | 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 | 1545 | assume eq: "map_of xs x = map_of xs y" | 
| 65342 | 1546 | from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'" | 
| 1547 | by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff) | |
| 1548 | 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 | 1549 | by (force dest: map_of_SomeD)+ | 
| 65342 | 1550 | moreover from * eq x'y' have "x' = y'" | 
| 1551 | by simp | |
| 1552 | ultimately show "x = y" | |
| 1553 | 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 | 1554 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1555 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1556 | 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 | 1557 | 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 | 1558 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1559 | lemma inj_on_map_of': | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1560 | assumes "distinct (map snd xs)" | 
| 65342 | 1561 | 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 | 1562 | by (intro comp_inj_on inj_on_map_of assms inj_on_the) | 
| 65342 | 1563 | (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 | 1564 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1565 | lemma image_map_of: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1566 | assumes "distinct (map fst xs)" | 
| 65342 | 1567 | 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 | 1568 | using assms by (auto simp: rev_image_eqI) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1569 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1570 | lemma the_Some_image [simp]: "the ` Some ` A = A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1571 | by (subst image_image) simp | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1572 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1573 | lemma image_map_of': | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1574 | assumes "distinct (map fst xs)" | 
| 65342 | 1575 | 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 | 1576 | 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 | 1577 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1578 | lemma permutation_of_list_permutes [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1579 | assumes "list_permutes xs A" | 
| 65342 | 1580 | shows "permutation_of_list xs permutes A" | 
| 1581 | (is "?f permutes _") | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1582 | proof (rule permutes_subset[OF bij_imp_permutes]) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1583 | from assms show "set (map fst xs) \<subseteq> A" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1584 | by (simp add: list_permutes_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1585 | 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 | 1586 | 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 | 1587 | 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 | 1588 | by (intro inj_on_cong) | 
| 65342 | 1589 | (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 | 1590 | 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 | 1591 | by (rule inj_on_imp_bij_betw) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1592 | 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 | 1593 | by (intro image_cong refl) | 
| 65342 | 1594 | (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 | 1595 | also from assms have "\<dots> = set (map fst xs)" | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1596 | 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 | 1597 | 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 | 1598 | 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 | 1599 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1600 | lemma eval_permutation_of_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1601 | "permutation_of_list [] x = x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1602 | "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 | 1603 | "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 | 1604 | by (simp_all add: permutation_of_list_def) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1605 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1606 | lemma eval_inverse_permutation_of_list [simp]: | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1607 | "inverse_permutation_of_list [] x = x" | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1608 | "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 | 1609 | "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 | 1610 | by (simp_all add: inverse_permutation_of_list.simps) | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1611 | |
| 65342 | 1612 | lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x" | 
| 1613 | by (induct xs) (auto simp: permutation_of_list_Cons) | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1614 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1615 | lemma permutation_of_list_unique': | 
| 65342 | 1616 | "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y" | 
| 1617 | by (induct xs) (force simp: permutation_of_list_Cons)+ | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1618 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1619 | lemma permutation_of_list_unique: | 
| 65342 | 1620 | "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y" | 
| 1621 | 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 | 1622 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1623 | lemma inverse_permutation_of_list_id: | 
| 65342 | 1624 | "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x" | 
| 1625 | by (induct xs) auto | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1626 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1627 | lemma inverse_permutation_of_list_unique': | 
| 65342 | 1628 | "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x" | 
| 73328 | 1629 | by (induct xs) (force simp: inverse_permutation_of_list.simps(2))+ | 
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1630 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1631 | lemma inverse_permutation_of_list_unique: | 
| 65342 | 1632 | "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x" | 
| 1633 | 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 | 1634 | |
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1635 | lemma inverse_permutation_of_list_correct: | 
| 65342 | 1636 | fixes A :: "'a set" | 
| 1637 | assumes "list_permutes xs A" | |
| 1638 | 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 | 1639 | proof (rule ext, rule sym, subst permutes_inv_eq) | 
| 65342 | 1640 | from assms show "permutation_of_list xs permutes A" | 
| 1641 | by simp | |
| 1642 | 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 | 1643 | proof (cases "x \<in> set (map snd xs)") | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1644 | case True | 
| 65342 | 1645 | 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 | 1646 | with assms show ?thesis | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1647 | by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique) | 
| 65342 | 1648 | next | 
| 1649 | case False | |
| 1650 | with assms show ?thesis | |
| 1651 | by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id) | |
| 1652 | qed | |
| 63099 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1653 | qed | 
| 
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
 eberlm parents: 
62390diff
changeset | 1654 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1655 | end |