| author | paulson <lp15@cam.ac.uk> | 
| Fri, 20 Mar 2015 16:11:28 +0000 | |
| changeset 59765 | 26d1c71784f1 | 
| parent 59730 | b7c394c7a619 | 
| child 60500 | 903bb1495239 | 
| permissions | -rw-r--r-- | 
| 41959 | 1 | (* Title: HOL/Library/Permutations.thy | 
| 2 | Author: Amine Chaieb, University of Cambridge | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 3 | *) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 4 | |
| 58881 | 5 | section {* Permutations, both general and specifically on finite sets.*}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 6 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 7 | theory Permutations | 
| 59669 
de7792ea4090
renaming HOL/Fact.thy -> Binomial.thy
 paulson <lp15@cam.ac.uk> parents: 
59474diff
changeset | 8 | imports Binomial | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 9 | begin | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 10 | |
| 54681 | 11 | subsection {* Transpositions *}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 12 | |
| 56608 | 13 | lemma swap_id_idempotent [simp]: | 
| 14 | "Fun.swap a b id \<circ> Fun.swap a b id = id" | |
| 56545 | 15 | by (rule ext, auto simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 16 | |
| 56608 | 17 | lemma inv_swap_id: | 
| 18 | "inv (Fun.swap a b id) = Fun.swap a b id" | |
| 54681 | 19 | by (rule inv_unique_comp) simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 20 | |
| 56608 | 21 | lemma swap_id_eq: | 
| 22 | "Fun.swap a b id x = (if x = a then b else if x = b then a else x)" | |
| 56545 | 23 | by (simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 24 | |
| 54681 | 25 | |
| 26 | subsection {* Basic consequences of the definition *}
 | |
| 27 | ||
| 28 | definition permutes (infixr "permutes" 41) | |
| 29 | where "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 30 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 31 | lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 32 | unfolding permutes_def by metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 33 | |
| 54681 | 34 | lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S" | 
| 30488 | 35 | unfolding permutes_def | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 36 | apply (rule set_eqI) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 37 | apply (simp add: image_iff) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 38 | apply metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 39 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 40 | |
| 54681 | 41 | lemma permutes_inj: "p permutes S \<Longrightarrow> inj p" | 
| 30488 | 42 | unfolding permutes_def inj_on_def by blast | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 43 | |
| 54681 | 44 | lemma permutes_surj: "p permutes s \<Longrightarrow> surj p" | 
| 30488 | 45 | unfolding permutes_def surj_def by metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 46 | |
| 59474 | 47 | lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S" | 
| 48 | by (metis UNIV_I bij_betw_def permutes_image permutes_inj subsetI subset_inj_on) | |
| 59669 
de7792ea4090
renaming HOL/Fact.thy -> Binomial.thy
 paulson <lp15@cam.ac.uk> parents: 
59474diff
changeset | 49 | |
| 59474 | 50 | lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S" | 
| 51 | unfolding permutes_def bij_betw_def inj_on_def | |
| 52 | by auto (metis image_iff)+ | |
| 53 | ||
| 54681 | 54 | lemma permutes_inv_o: | 
| 55 | assumes pS: "p permutes S" | |
| 56 | shows "p \<circ> inv p = id" | |
| 57 | and "inv p \<circ> p = id" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 58 | using permutes_inj[OF pS] permutes_surj[OF pS] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 59 | unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+ | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 60 | |
| 30488 | 61 | lemma permutes_inverses: | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 62 | fixes p :: "'a \<Rightarrow> 'a" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 63 | assumes pS: "p permutes S" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 64 | shows "p (inv p x) = x" | 
| 54681 | 65 | and "inv p (p x) = x" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 66 | using permutes_inv_o[OF pS, unfolded fun_eq_iff o_def] by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 67 | |
| 54681 | 68 | lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 69 | unfolding permutes_def by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 70 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 71 | lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
 | 
| 54681 | 72 | unfolding fun_eq_iff permutes_def by simp metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 73 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 74 | lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
 | 
| 54681 | 75 | unfolding fun_eq_iff permutes_def by simp metis | 
| 30488 | 76 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 77 | lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 78 | unfolding permutes_def by simp | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 79 | |
| 54681 | 80 | lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y" | 
| 81 | unfolding permutes_def inv_def | |
| 82 | apply auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 83 | apply (erule allE[where x=y]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 84 | apply (erule allE[where x=y]) | 
| 54681 | 85 | apply (rule someI_ex) | 
| 86 | apply blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 87 | apply (rule some1_equality) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 88 | apply blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 89 | apply blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 90 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 91 | |
| 54681 | 92 | lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S" | 
| 56545 | 93 | unfolding permutes_def Fun.swap_def fun_upd_def by auto metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 94 | |
| 54681 | 95 | lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T" | 
| 96 | by (simp add: Ball_def permutes_def) metis | |
| 97 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 98 | |
| 54681 | 99 | subsection {* Group properties *}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 100 | |
| 54681 | 101 | lemma permutes_id: "id permutes S" | 
| 102 | unfolding permutes_def by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 103 | |
| 54681 | 104 | 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 | 105 | unfolding permutes_def o_def by metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 106 | |
| 54681 | 107 | lemma permutes_inv: | 
| 108 | assumes pS: "p permutes S" | |
| 109 | shows "inv p permutes S" | |
| 30488 | 110 | using pS unfolding permutes_def permutes_inv_eq[OF pS] by metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 111 | |
| 54681 | 112 | lemma permutes_inv_inv: | 
| 113 | assumes pS: "p permutes S" | |
| 114 | shows "inv (inv p) = p" | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 115 | unfolding fun_eq_iff permutes_inv_eq[OF pS] permutes_inv_eq[OF permutes_inv[OF pS]] | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 116 | by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 117 | |
| 54681 | 118 | |
| 119 | subsection {* The number of permutations on a finite set *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 120 | |
| 30488 | 121 | lemma permutes_insert_lemma: | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 122 | assumes pS: "p permutes (insert a S)" | 
| 54681 | 123 | shows "Fun.swap a (p a) id \<circ> p permutes S" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 124 | apply (rule permutes_superset[where S = "insert a S"]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 125 | apply (rule permutes_compose[OF pS]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 126 | apply (rule permutes_swap_id, simp) | 
| 54681 | 127 | using permutes_in_image[OF pS, of a] | 
| 128 | apply simp | |
| 56545 | 129 | apply (auto simp add: Ball_def Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 130 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 131 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 132 | lemma permutes_insert: "{p. p permutes (insert a S)} =
 | 
| 54681 | 133 |   (\<lambda>(b,p). Fun.swap a b id \<circ> p) ` {(b,p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
 | 
| 134 | proof - | |
| 135 |   {
 | |
| 136 | fix p | |
| 137 |     {
 | |
| 138 | assume pS: "p permutes insert a S" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 139 | let ?b = "p a" | 
| 54681 | 140 | let ?q = "Fun.swap a (p a) id \<circ> p" | 
| 141 | have th0: "p = Fun.swap a ?b id \<circ> ?q" | |
| 142 | unfolding fun_eq_iff o_assoc by simp | |
| 143 | have th1: "?b \<in> insert a S" | |
| 144 | unfolding permutes_in_image[OF pS] by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 145 | from permutes_insert_lemma[OF pS] th0 th1 | 
| 54681 | 146 | have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S" by blast | 
| 147 | } | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 148 | moreover | 
| 54681 | 149 |     {
 | 
| 150 | fix b q | |
| 151 | assume bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" | |
| 30488 | 152 | from permutes_subset[OF bq(3), of "insert a S"] | 
| 54681 | 153 | have qS: "q permutes insert a S" | 
| 154 | by auto | |
| 155 | have aS: "a \<in> insert a S" | |
| 156 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 157 | from bq(1) permutes_compose[OF qS permutes_swap_id[OF aS bq(2)]] | 
| 54681 | 158 | have "p permutes insert a S" | 
| 159 | by simp | |
| 160 | } | |
| 161 | ultimately have "p permutes insert a S \<longleftrightarrow> | |
| 162 | (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" | |
| 163 | by blast | |
| 164 | } | |
| 165 | then show ?thesis | |
| 166 | by auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 167 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 168 | |
| 54681 | 169 | lemma card_permutations: | 
| 170 | assumes Sn: "card S = n" | |
| 171 | and fS: "finite S" | |
| 33715 | 172 |   shows "card {p. p permutes S} = fact n"
 | 
| 54681 | 173 | using fS Sn | 
| 174 | proof (induct arbitrary: n) | |
| 175 | case empty | |
| 176 | then show ?case by simp | |
| 33715 | 177 | next | 
| 178 | case (insert x F) | |
| 54681 | 179 |   {
 | 
| 180 | fix n | |
| 181 | assume H0: "card (insert x F) = n" | |
| 33715 | 182 |     let ?xF = "{p. p permutes insert x F}"
 | 
| 183 |     let ?pF = "{p. p permutes F}"
 | |
| 184 |     let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
 | |
| 185 | let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)" | |
| 186 | from permutes_insert[of x F] | |
| 187 | have xfgpF': "?xF = ?g ` ?pF'" . | |
| 54681 | 188 | have Fs: "card F = n - 1" | 
| 189 | using `x \<notin> F` H0 `finite F` by auto | |
| 190 | from insert.hyps Fs have pFs: "card ?pF = fact (n - 1)" | |
| 191 | using `finite F` by auto | |
| 192 | then have "finite ?pF" | |
| 59730 
b7c394c7a619
The factorial function, "fact", now has type "nat => 'a"
 paulson <lp15@cam.ac.uk> parents: 
59669diff
changeset | 193 | by (auto intro: card_ge_0_finite) | 
| 54681 | 194 | then have pF'f: "finite ?pF'" | 
| 195 | using H0 `finite F` | |
| 33715 | 196 | apply (simp only: Collect_split Collect_mem_eq) | 
| 197 | apply (rule finite_cartesian_product) | |
| 198 | apply simp_all | |
| 199 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 200 | |
| 33715 | 201 | have ginj: "inj_on ?g ?pF'" | 
| 54681 | 202 | proof - | 
| 33715 | 203 |       {
 | 
| 54681 | 204 | fix b p c q | 
| 205 | assume bp: "(b,p) \<in> ?pF'" | |
| 206 | assume cq: "(c,q) \<in> ?pF'" | |
| 207 | assume eq: "?g (b,p) = ?g (c,q)" | |
| 208 | from bp cq have ths: "b \<in> insert x F" "c \<in> insert x F" "x \<in> insert x F" | |
| 209 | "p permutes F" "q permutes F" | |
| 210 | by auto | |
| 211 | from ths(4) `x \<notin> F` eq have "b = ?g (b,p) x" | |
| 212 | unfolding permutes_def | |
| 56545 | 213 | by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff) | 
| 54681 | 214 | also have "\<dots> = ?g (c,q) x" | 
| 215 | using ths(5) `x \<notin> F` eq | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 216 | by (auto simp add: swap_def fun_upd_def fun_eq_iff) | 
| 54681 | 217 | also have "\<dots> = c" | 
| 218 | using ths(5) `x \<notin> F` | |
| 219 | unfolding permutes_def | |
| 56545 | 220 | by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff) | 
| 33715 | 221 | finally have bc: "b = c" . | 
| 54681 | 222 | then have "Fun.swap x b id = Fun.swap x c id" | 
| 223 | by simp | |
| 224 | with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q" | |
| 225 | by simp | |
| 226 | then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = | |
| 227 | Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)" | |
| 228 | by simp | |
| 229 | then have "p = q" | |
| 230 | by (simp add: o_assoc) | |
| 231 | with bc have "(b, p) = (c, q)" | |
| 232 | by simp | |
| 33715 | 233 | } | 
| 54681 | 234 | then show ?thesis | 
| 235 | unfolding inj_on_def by blast | |
| 33715 | 236 | qed | 
| 54681 | 237 | from `x \<notin> F` H0 have n0: "n \<noteq> 0" | 
| 238 | using `finite F` by auto | |
| 239 | then have "\<exists>m. n = Suc m" | |
| 240 | by presburger | |
| 241 | then obtain m where n[simp]: "n = Suc m" | |
| 242 | by blast | |
| 33715 | 243 | from pFs H0 have xFc: "card ?xF = fact n" | 
| 54681 | 244 | unfolding xfgpF' card_image[OF ginj] | 
| 245 | using `finite F` `finite ?pF` | |
| 33715 | 246 | apply (simp only: Collect_split Collect_mem_eq card_cartesian_product) | 
| 54681 | 247 | apply simp | 
| 248 | done | |
| 249 | from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF" | |
| 250 | unfolding xfgpF' by simp | |
| 33715 | 251 | have "card ?xF = fact n" | 
| 252 | using xFf xFc unfolding xFf by blast | |
| 253 | } | |
| 54681 | 254 | then show ?case | 
| 255 | using insert by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 256 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 257 | |
| 54681 | 258 | lemma finite_permutations: | 
| 259 | assumes fS: "finite S" | |
| 260 |   shows "finite {p. p permutes S}"
 | |
| 59730 
b7c394c7a619
The factorial function, "fact", now has type "nat => 'a"
 paulson <lp15@cam.ac.uk> parents: 
59669diff
changeset | 261 | using card_permutations[OF refl fS] | 
| 33715 | 262 | by (auto intro: card_ge_0_finite) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 263 | |
| 54681 | 264 | |
| 265 | subsection {* Permutations of index set for iterated operations *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 266 | |
| 51489 | 267 | lemma (in comm_monoid_set) permute: | 
| 268 | assumes "p permutes S" | |
| 54681 | 269 | shows "F g S = F (g \<circ> p) S" | 
| 51489 | 270 | proof - | 
| 54681 | 271 | from `p permutes S` have "inj p" | 
| 272 | by (rule permutes_inj) | |
| 273 | then have "inj_on p S" | |
| 274 | by (auto intro: subset_inj_on) | |
| 275 | then have "F g (p ` S) = F (g \<circ> p) S" | |
| 276 | by (rule reindex) | |
| 277 | moreover from `p permutes S` have "p ` S = S" | |
| 278 | by (rule permutes_image) | |
| 279 | ultimately show ?thesis | |
| 280 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 281 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 282 | |
| 54681 | 283 | |
| 284 | subsection {* Various combinations of transpositions with 2, 1 and 0 common elements *}
 | |
| 285 | ||
| 286 | lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow> | |
| 287 | Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id" | |
| 56545 | 288 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 289 | |
| 54681 | 290 | lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow> | 
| 291 | Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id" | |
| 56545 | 292 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 293 | |
| 54681 | 294 | lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow> | 
| 295 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id" | |
| 56545 | 296 | by (simp add: fun_eq_iff Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 297 | |
| 54681 | 298 | |
| 299 | subsection {* Permutations as transposition sequences *}
 | |
| 300 | ||
| 301 | inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
 | |
| 302 | where | |
| 303 | id[simp]: "swapidseq 0 id" | |
| 304 | | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)" | |
| 305 | ||
| 306 | declare id[unfolded id_def, simp] | |
| 307 | ||
| 308 | definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 309 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 310 | |
| 54681 | 311 | subsection {* Some closure properties of the set of permutations, with lengths *}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 312 | |
| 54681 | 313 | lemma permutation_id[simp]: "permutation id" | 
| 314 | unfolding permutation_def by (rule exI[where x=0]) simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 315 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 316 | declare permutation_id[unfolded id_def, simp] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 317 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 318 | lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 319 | apply clarsimp | 
| 54681 | 320 | using comp_Suc[of 0 id a b] | 
| 321 | apply simp | |
| 322 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 323 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 324 | lemma permutation_swap_id: "permutation (Fun.swap a b id)" | 
| 54681 | 325 | apply (cases "a = b") | 
| 326 | apply simp_all | |
| 327 | unfolding permutation_def | |
| 328 | using swapidseq_swap[of a b] | |
| 329 | apply blast | |
| 330 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 331 | |
| 54681 | 332 | lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)" | 
| 333 | proof (induct n p arbitrary: m q rule: swapidseq.induct) | |
| 334 | case (id m q) | |
| 335 | then show ?case by simp | |
| 336 | next | |
| 337 | case (comp_Suc n p a b m q) | |
| 338 | have th: "Suc n + m = Suc (n + m)" | |
| 339 | by arith | |
| 340 | show ?case | |
| 341 | unfolding th comp_assoc | |
| 342 | apply (rule swapidseq.comp_Suc) | |
| 343 | using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3) | |
| 344 | apply blast+ | |
| 345 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 346 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 347 | |
| 54681 | 348 | 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 | 349 | 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 | 350 | |
| 54681 | 351 | lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 352 | apply (induct n p rule: swapidseq.induct) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 353 | using swapidseq_swap[of a b] | 
| 54681 | 354 | apply (auto simp add: comp_assoc intro: swapidseq.comp_Suc) | 
| 355 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 356 | |
| 54681 | 357 | lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id" | 
| 358 | proof (induct n p rule: swapidseq.induct) | |
| 359 | case id | |
| 360 | then show ?case | |
| 361 | by (rule exI[where x=id]) simp | |
| 30488 | 362 | next | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 363 | case (comp_Suc n p a b) | 
| 54681 | 364 | from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 365 | by blast | |
| 366 | let ?q = "q \<circ> Fun.swap a b id" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 367 | note H = comp_Suc.hyps | 
| 54681 | 368 | from swapidseq_swap[of a b] H(3) have th0: "swapidseq 1 (Fun.swap a b id)" | 
| 369 | by simp | |
| 370 | from swapidseq_comp_add[OF q(1) th0] have th1: "swapidseq (Suc n) ?q" | |
| 371 | by simp | |
| 372 | have "Fun.swap a b id \<circ> p \<circ> ?q = Fun.swap a b id \<circ> (p \<circ> q) \<circ> Fun.swap a b id" | |
| 373 | by (simp add: o_assoc) | |
| 374 | also have "\<dots> = id" | |
| 375 | by (simp add: q(2)) | |
| 376 | finally have th2: "Fun.swap a b id \<circ> p \<circ> ?q = id" . | |
| 377 | have "?q \<circ> (Fun.swap a b id \<circ> p) = q \<circ> (Fun.swap a b id \<circ> Fun.swap a b id) \<circ> p" | |
| 378 | by (simp only: o_assoc) | |
| 379 | then have "?q \<circ> (Fun.swap a b id \<circ> p) = id" | |
| 380 | by (simp add: q(3)) | |
| 381 | with th1 th2 show ?case | |
| 382 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 383 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 384 | |
| 54681 | 385 | lemma swapidseq_inverse: | 
| 386 | assumes H: "swapidseq n p" | |
| 387 | shows "swapidseq n (inv p)" | |
| 388 | using swapidseq_inverse_exists[OF H] inv_unique_comp[of p] by auto | |
| 389 | ||
| 390 | lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)" | |
| 391 | using permutation_def swapidseq_inverse by blast | |
| 392 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 393 | |
| 54681 | 394 | subsection {* The identity map only has even transposition sequences *}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 395 | |
| 54681 | 396 | lemma symmetry_lemma: | 
| 397 | assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c" | |
| 398 | and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> | |
| 399 | 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> | |
| 400 | P a b c d" | |
| 401 | shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow> P a b c d" | |
| 402 | using assms by metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 403 | |
| 54681 | 404 | lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> | 
| 405 | Fun.swap a b id \<circ> Fun.swap c d id = id \<or> | |
| 406 | (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> | |
| 407 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)" | |
| 408 | proof - | |
| 409 | assume H: "a \<noteq> b" "c \<noteq> d" | |
| 410 | have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow> | |
| 411 | (Fun.swap a b id \<circ> Fun.swap c d id = id \<or> | |
| 412 | (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> | |
| 413 | Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))" | |
| 414 | apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d]) | |
| 56545 | 415 | apply (simp_all only: swap_commute) | 
| 54681 | 416 | apply (case_tac "a = c \<and> b = d") | 
| 56608 | 417 | apply (clarsimp simp only: swap_commute swap_id_idempotent) | 
| 54681 | 418 | apply (case_tac "a = c \<and> b \<noteq> d") | 
| 419 | apply (rule disjI2) | |
| 420 | apply (rule_tac x="b" in exI) | |
| 421 | apply (rule_tac x="d" in exI) | |
| 422 | apply (rule_tac x="b" in exI) | |
| 56545 | 423 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | 
| 54681 | 424 | apply (case_tac "a \<noteq> c \<and> b = d") | 
| 425 | apply (rule disjI2) | |
| 426 | apply (rule_tac x="c" in exI) | |
| 427 | apply (rule_tac x="d" in exI) | |
| 428 | apply (rule_tac x="c" in exI) | |
| 56545 | 429 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | 
| 54681 | 430 | apply (rule disjI2) | 
| 431 | apply (rule_tac x="c" in exI) | |
| 432 | apply (rule_tac x="d" in exI) | |
| 433 | apply (rule_tac x="b" in exI) | |
| 56545 | 434 | apply (clarsimp simp add: fun_eq_iff Fun.swap_def) | 
| 54681 | 435 | done | 
| 436 | with H show ?thesis by metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 437 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 438 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 439 | lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 440 | using swapidseq.cases[of 0 p "p = id"] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 441 | by auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 442 | |
| 54681 | 443 | lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow> | 
| 444 | n = 0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = Fun.swap a b id \<circ> q \<and> swapidseq m q \<and> a \<noteq> b)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 445 | apply (rule iffI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 446 | apply (erule swapidseq.cases[of n p]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 447 | apply simp | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 448 | apply (rule disjI2) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 449 | apply (rule_tac x= "a" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 450 | apply (rule_tac x= "b" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 451 | apply (rule_tac x= "pa" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 452 | apply (rule_tac x= "na" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 453 | apply simp | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 454 | apply auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 455 | apply (rule comp_Suc, simp_all) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 456 | done | 
| 54681 | 457 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 458 | lemma fixing_swapidseq_decrease: | 
| 54681 | 459 | assumes spn: "swapidseq n p" | 
| 460 | and ab: "a \<noteq> b" | |
| 461 | and pa: "(Fun.swap a b id \<circ> p) a = a" | |
| 462 | shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 463 | using spn ab pa | 
| 54681 | 464 | proof (induct n arbitrary: p a b) | 
| 465 | case 0 | |
| 466 | then show ?case | |
| 56545 | 467 | by (auto simp add: Fun.swap_def fun_upd_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 468 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 469 | case (Suc n p a b) | 
| 54681 | 470 | from Suc.prems(1) swapidseq_cases[of "Suc n" p] | 
| 471 | obtain c d q m where | |
| 472 | cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 473 | by auto | 
| 54681 | 474 |   {
 | 
| 475 | assume H: "Fun.swap a b id \<circ> Fun.swap c d id = id" | |
| 476 | have ?case by (simp only: cdqm o_assoc H) (simp add: cdqm) | |
| 477 | } | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 478 | moreover | 
| 54681 | 479 |   {
 | 
| 480 | fix x y z | |
| 481 | assume H: "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y" | |
| 482 | "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id" | |
| 483 | from H have az: "a \<noteq> z" | |
| 484 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 485 | |
| 54681 | 486 |     {
 | 
| 487 | fix h | |
| 488 | have "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" | |
| 56545 | 489 | using H by (simp add: Fun.swap_def) | 
| 54681 | 490 | } | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 491 | note th3 = this | 
| 54681 | 492 | from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)" | 
| 493 | by simp | |
| 494 | then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)" | |
| 495 | by (simp add: o_assoc H) | |
| 496 | then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a" | |
| 497 | by simp | |
| 498 | then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a" | |
| 499 | unfolding Suc by metis | |
| 500 | then have th1: "(Fun.swap a z id \<circ> q) a = a" | |
| 501 | unfolding th3 . | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 502 | from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az th1] | 
| 54681 | 503 | have th2: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0" | 
| 504 | by blast+ | |
| 505 | have th: "Suc n - 1 = Suc (n - 1)" | |
| 506 | using th2(2) by auto | |
| 507 | have ?case | |
| 508 | unfolding cdqm(2) H o_assoc th | |
| 49739 | 509 | apply (simp only: Suc_not_Zero simp_thms comp_assoc) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 510 | apply (rule comp_Suc) | 
| 54681 | 511 | using th2 H | 
| 512 | apply blast+ | |
| 513 | done | |
| 514 | } | |
| 515 | ultimately show ?case | |
| 516 | using swap_general[OF Suc.prems(2) cdqm(4)] by metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 517 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 518 | |
| 30488 | 519 | lemma swapidseq_identity_even: | 
| 54681 | 520 | assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)" | 
| 521 | shows "even n" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 522 | using `swapidseq n id` | 
| 54681 | 523 | proof (induct n rule: nat_less_induct) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 524 | fix n | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 525 | assume H: "\<forall>m<n. swapidseq m (id::'a \<Rightarrow> 'a) \<longrightarrow> even m" "swapidseq n (id :: 'a \<Rightarrow> 'a)" | 
| 54681 | 526 |   {
 | 
| 527 | assume "n = 0" | |
| 528 | then have "even n" by presburger | |
| 529 | } | |
| 30488 | 530 | moreover | 
| 54681 | 531 |   {
 | 
| 532 | fix a b :: 'a and q m | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 533 | assume h: "n = Suc m" "(id :: 'a \<Rightarrow> 'a) = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 534 | from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]] | 
| 54681 | 535 | have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)" | 
| 536 | by auto | |
| 537 | from h m have mn: "m - 1 < n" | |
| 538 | by arith | |
| 539 | from H(1)[rule_format, OF mn m(2)] h(1) m(1) have "even n" | |
| 540 | by presburger | |
| 541 | } | |
| 542 | ultimately show "even n" | |
| 543 | using H(2)[unfolded swapidseq_cases[of n id]] by auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 544 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 545 | |
| 54681 | 546 | |
| 547 | subsection {* Therefore we have a welldefined notion of parity *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 548 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 549 | definition "evenperm p = even (SOME n. swapidseq n p)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 550 | |
| 54681 | 551 | lemma swapidseq_even_even: | 
| 552 | assumes m: "swapidseq m p" | |
| 553 | and n: "swapidseq n p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 554 | shows "even m \<longleftrightarrow> even n" | 
| 54681 | 555 | proof - | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 556 | from swapidseq_inverse_exists[OF n] | 
| 54681 | 557 | obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | 
| 558 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 559 | from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] | 
| 54681 | 560 | show ?thesis | 
| 561 | by arith | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 562 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 563 | |
| 54681 | 564 | lemma evenperm_unique: | 
| 565 | assumes p: "swapidseq n p" | |
| 566 | and n:"even n = b" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 567 | shows "evenperm p = b" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 568 | unfolding n[symmetric] evenperm_def | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 569 | apply (rule swapidseq_even_even[where p = p]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 570 | apply (rule someI[where x = n]) | 
| 54681 | 571 | using p | 
| 572 | apply blast+ | |
| 573 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 574 | |
| 54681 | 575 | |
| 576 | subsection {* And it has the expected composition properties *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 577 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 578 | lemma evenperm_id[simp]: "evenperm id = True" | 
| 54681 | 579 | by (rule evenperm_unique[where n = 0]) simp_all | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 580 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 581 | lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)" | 
| 54681 | 582 | 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 | 583 | |
| 30488 | 584 | lemma evenperm_comp: | 
| 54681 | 585 | assumes p: "permutation p" | 
| 586 | and q:"permutation q" | |
| 587 | shows "evenperm (p \<circ> q) = (evenperm p = evenperm q)" | |
| 588 | proof - | |
| 589 | from p q 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 | 590 | unfolding permutation_def by blast | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 591 | note nm = swapidseq_comp_add[OF n m] | 
| 54681 | 592 | have th: "even (n + m) = (even n \<longleftrightarrow> even m)" | 
| 593 | by arith | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 594 | from evenperm_unique[OF n refl] evenperm_unique[OF m refl] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 595 | evenperm_unique[OF nm th] | 
| 54681 | 596 | show ?thesis | 
| 597 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 598 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 599 | |
| 54681 | 600 | lemma evenperm_inv: | 
| 601 | assumes p: "permutation p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 602 | shows "evenperm (inv p) = evenperm p" | 
| 54681 | 603 | proof - | 
| 604 | from p obtain n where n: "swapidseq n p" | |
| 605 | unfolding permutation_def by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 606 | from evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 607 | show ?thesis . | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 608 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 609 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 610 | |
| 54681 | 611 | subsection {* A more abstract characterization of permutations *}
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 612 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 613 | lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 614 | unfolding bij_def inj_on_def surj_def | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 615 | apply auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 616 | apply metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 617 | apply metis | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 618 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 619 | |
| 30488 | 620 | lemma permutation_bijective: | 
| 621 | assumes p: "permutation p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 622 | shows "bij p" | 
| 54681 | 623 | proof - | 
| 624 | from p obtain n where n: "swapidseq n p" | |
| 625 | unfolding permutation_def by blast | |
| 626 | from swapidseq_inverse_exists[OF n] | |
| 627 | obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" | |
| 628 | by blast | |
| 629 | then show ?thesis unfolding bij_iff | |
| 630 | apply (auto simp add: fun_eq_iff) | |
| 631 | apply metis | |
| 632 | done | |
| 30488 | 633 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 634 | |
| 54681 | 635 | lemma permutation_finite_support: | 
| 636 | assumes p: "permutation p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 637 |   shows "finite {x. p x \<noteq> x}"
 | 
| 54681 | 638 | proof - | 
| 639 | from p obtain n where n: "swapidseq n p" | |
| 640 | unfolding permutation_def by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 641 | from n show ?thesis | 
| 54681 | 642 | proof (induct n p rule: swapidseq.induct) | 
| 643 | case id | |
| 644 | then show ?case by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 645 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 646 | case (comp_Suc n p a b) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 647 |     let ?S = "insert a (insert b {x. p x \<noteq> x})"
 | 
| 54681 | 648 | from comp_Suc.hyps(2) have fS: "finite ?S" | 
| 649 | by simp | |
| 650 |     from `a \<noteq> b` have th: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
 | |
| 56545 | 651 | by (auto simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 652 | from finite_subset[OF th fS] show ?case . | 
| 54681 | 653 | qed | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 654 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 655 | |
| 54681 | 656 | lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y" | 
| 657 | using surj_f_inv_f[of p] by (auto simp add: bij_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 658 | |
| 30488 | 659 | lemma bij_swap_comp: | 
| 54681 | 660 | assumes bp: "bij p" | 
| 661 | shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 662 | using surj_f_inv_f[OF bij_is_surj[OF bp]] | 
| 56545 | 663 | by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp]) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 664 | |
| 54681 | 665 | lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)" | 
| 666 | proof - | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 667 | assume H: "bij p" | 
| 30488 | 668 | show ?thesis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 669 | unfolding bij_swap_comp[OF H] bij_swap_iff | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 670 | using H . | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 671 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 672 | |
| 30488 | 673 | lemma permutation_lemma: | 
| 54681 | 674 | assumes fS: "finite S" | 
| 675 | and p: "bij p" | |
| 676 | and pS: "\<forall>x. x\<notin> S \<longrightarrow> p x = x" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 677 | shows "permutation p" | 
| 54681 | 678 | using fS p pS | 
| 679 | proof (induct S arbitrary: p rule: finite_induct) | |
| 680 | case (empty p) | |
| 681 | then show ?case by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 682 | next | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 683 | case (insert a F p) | 
| 54681 | 684 | let ?r = "Fun.swap a (p a) id \<circ> p" | 
| 685 | let ?q = "Fun.swap a (p a) id \<circ> ?r" | |
| 686 | have raa: "?r a = a" | |
| 56545 | 687 | by (simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 688 | from bij_swap_ompose_bij[OF insert(4)] | 
| 30488 | 689 | have br: "bij ?r" . | 
| 690 | ||
| 691 | from insert raa have th: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x" | |
| 56545 | 692 | apply (clarsimp simp add: Fun.swap_def) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 693 | apply (erule_tac x="x" in allE) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 694 | apply auto | 
| 54681 | 695 | unfolding bij_iff | 
| 696 | apply metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 697 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 698 | from insert(3)[OF br th] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 699 | have rp: "permutation ?r" . | 
| 54681 | 700 | have "permutation ?q" | 
| 701 | by (simp add: permutation_compose permutation_swap_id rp) | |
| 702 | then show ?case | |
| 703 | by (simp add: o_assoc) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 704 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 705 | |
| 30488 | 706 | lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
 | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 707 | (is "?lhs \<longleftrightarrow> ?b \<and> ?f") | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 708 | proof | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 709 | assume p: ?lhs | 
| 54681 | 710 | from p permutation_bijective permutation_finite_support show "?b \<and> ?f" | 
| 711 | by auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 712 | next | 
| 54681 | 713 | assume "?b \<and> ?f" | 
| 714 | then have "?f" "?b" by blast+ | |
| 715 | from permutation_lemma[OF this] show ?lhs | |
| 716 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 717 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 718 | |
| 54681 | 719 | lemma permutation_inverse_works: | 
| 720 | assumes p: "permutation p" | |
| 721 | shows "inv p \<circ> p = id" | |
| 722 | and "p \<circ> inv p = id" | |
| 44227 
78e033e8ba05
get Library/Permutations.thy compiled and working again
 huffman parents: 
41959diff
changeset | 723 | using permutation_bijective [OF p] | 
| 
78e033e8ba05
get Library/Permutations.thy compiled and working again
 huffman parents: 
41959diff
changeset | 724 | unfolding bij_def inj_iff surj_iff by auto | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 725 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 726 | lemma permutation_inverse_compose: | 
| 54681 | 727 | assumes p: "permutation p" | 
| 728 | and q: "permutation q" | |
| 729 | shows "inv (p \<circ> q) = inv q \<circ> inv p" | |
| 730 | proof - | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 731 | note ps = permutation_inverse_works[OF p] | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 732 | note qs = permutation_inverse_works[OF q] | 
| 54681 | 733 | have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p" | 
| 734 | by (simp add: o_assoc) | |
| 735 | also have "\<dots> = id" | |
| 736 | by (simp add: ps qs) | |
| 737 | finally have th0: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" . | |
| 738 | have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q" | |
| 739 | by (simp add: o_assoc) | |
| 740 | also have "\<dots> = id" | |
| 741 | by (simp add: ps qs) | |
| 742 | finally have th1: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" . | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 743 | from inv_unique_comp[OF th0 th1] show ?thesis . | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 744 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 745 | |
| 54681 | 746 | |
| 747 | subsection {* Relation to "permutes" *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 748 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 749 | lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)" | 
| 54681 | 750 | unfolding permutation permutes_def bij_iff[symmetric] | 
| 751 | apply (rule iffI, clarify) | |
| 752 |   apply (rule exI[where x="{x. p x \<noteq> x}"])
 | |
| 753 | apply simp | |
| 754 | apply clarsimp | |
| 755 | apply (rule_tac B="S" in finite_subset) | |
| 756 | apply auto | |
| 757 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 758 | |
| 54681 | 759 | |
| 760 | subsection {* Hence a sort of induction principle composing by swaps *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 761 | |
| 54681 | 762 | lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow> | 
| 763 | (\<And> a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> P p \<Longrightarrow> P p \<Longrightarrow> permutation p \<Longrightarrow> P (Fun.swap a b id \<circ> p)) \<Longrightarrow> | |
| 764 | (\<And>p. p permutes S \<Longrightarrow> P p)" | |
| 765 | proof (induct S rule: finite_induct) | |
| 766 | case empty | |
| 767 | then show ?case by auto | |
| 30488 | 768 | next | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 769 | case (insert x F p) | 
| 54681 | 770 | let ?r = "Fun.swap x (p x) id \<circ> p" | 
| 771 | let ?q = "Fun.swap x (p x) id \<circ> ?r" | |
| 772 | have qp: "?q = p" | |
| 773 | by (simp add: o_assoc) | |
| 774 | from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r" | |
| 775 | by blast | |
| 30488 | 776 | from permutes_in_image[OF insert.prems(3), of x] | 
| 54681 | 777 | have pxF: "p x \<in> insert x F" | 
| 778 | by simp | |
| 779 | have xF: "x \<in> insert x F" | |
| 780 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 781 | have rp: "permutation ?r" | 
| 30488 | 782 | unfolding permutation_permutes using insert.hyps(1) | 
| 54681 | 783 | permutes_insert_lemma[OF insert.prems(3)] | 
| 784 | by blast | |
| 30488 | 785 | from insert.prems(2)[OF xF pxF Pr Pr rp] | 
| 54681 | 786 | show ?case | 
| 787 | unfolding qp . | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 788 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 789 | |
| 54681 | 790 | |
| 791 | subsection {* Sign of a permutation as a real number *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 792 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 793 | definition "sign p = (if evenperm p then (1::int) else -1)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 794 | |
| 54681 | 795 | lemma sign_nz: "sign p \<noteq> 0" | 
| 796 | by (simp add: sign_def) | |
| 797 | ||
| 798 | lemma sign_id: "sign id = 1" | |
| 799 | by (simp add: sign_def) | |
| 800 | ||
| 801 | 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 | 802 | by (simp add: sign_def evenperm_inv) | 
| 54681 | 803 | |
| 804 | lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q" | |
| 805 | by (simp add: sign_def evenperm_comp) | |
| 806 | ||
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 807 | lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 808 | by (simp add: sign_def evenperm_swap) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 809 | |
| 54681 | 810 | lemma sign_idempotent: "sign p * sign p = 1" | 
| 811 | by (simp add: sign_def) | |
| 812 | ||
| 813 | ||
| 814 | subsection {* More lemmas about permutations *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 815 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 816 | lemma permutes_natset_le: | 
| 54681 | 817 | fixes S :: "'a::wellorder set" | 
| 818 | assumes p: "p permutes S" | |
| 819 | and le: "\<forall>i \<in> S. p i \<le> i" | |
| 820 | shows "p = id" | |
| 821 | proof - | |
| 822 |   {
 | |
| 823 | fix n | |
| 30488 | 824 | have "p n = n" | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 825 | using p le | 
| 54681 | 826 | proof (induct n arbitrary: S rule: less_induct) | 
| 827 | fix n S | |
| 828 | assume H: | |
| 829 | "\<And>m S. m < n \<Longrightarrow> p permutes S \<Longrightarrow> \<forall>i\<in>S. p i \<le> i \<Longrightarrow> p m = m" | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32456diff
changeset | 830 | "p permutes S" "\<forall>i \<in>S. p i \<le> i" | 
| 54681 | 831 |       {
 | 
| 832 | assume "n \<notin> S" | |
| 833 | with H(2) have "p n = n" | |
| 834 | unfolding permutes_def by metis | |
| 835 | } | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 836 | moreover | 
| 54681 | 837 |       {
 | 
| 838 | assume ns: "n \<in> S" | |
| 839 | from H(3) ns have "p n < n \<or> p n = n" | |
| 840 | by auto | |
| 841 |         moreover {
 | |
| 842 | assume h: "p n < n" | |
| 843 | from H h have "p (p n) = p n" | |
| 844 | by metis | |
| 845 | with permutes_inj[OF H(2)] have "p n = n" | |
| 846 | unfolding inj_on_def by blast | |
| 847 | with h have False | |
| 848 | by simp | |
| 849 | } | |
| 850 | ultimately have "p n = n" | |
| 851 | by blast | |
| 852 | } | |
| 853 | ultimately show "p n = n" | |
| 854 | by blast | |
| 855 | qed | |
| 856 | } | |
| 857 | then show ?thesis | |
| 858 | by (auto simp add: fun_eq_iff) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 859 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 860 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 861 | lemma permutes_natset_ge: | 
| 54681 | 862 | fixes S :: "'a::wellorder set" | 
| 863 | assumes p: "p permutes S" | |
| 864 | and le: "\<forall>i \<in> S. p i \<ge> i" | |
| 865 | shows "p = id" | |
| 866 | proof - | |
| 867 |   {
 | |
| 868 | fix i | |
| 869 | assume i: "i \<in> S" | |
| 870 | from i permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S" | |
| 871 | by simp | |
| 872 | with le have "p (inv p i) \<ge> inv p i" | |
| 873 | by blast | |
| 874 | with permutes_inverses[OF p] have "i \<ge> inv p i" | |
| 875 | by simp | |
| 876 | } | |
| 877 | then have th: "\<forall>i\<in>S. inv p i \<le> i" | |
| 878 | by blast | |
| 30488 | 879 | from permutes_natset_le[OF permutes_inv[OF p] th] | 
| 54681 | 880 | have "inv p = inv id" | 
| 881 | by simp | |
| 30488 | 882 | then show ?thesis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 883 | apply (subst permutes_inv_inv[OF p, symmetric]) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 884 | apply (rule inv_unique_comp) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 885 | apply simp_all | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 886 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 887 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 888 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 889 | lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
 | 
| 54681 | 890 | apply (rule set_eqI) | 
| 891 | apply auto | |
| 892 | using permutes_inv_inv permutes_inv | |
| 893 | apply auto | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 894 | apply (rule_tac x="inv x" in exI) | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 895 | apply auto | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 896 | done | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 897 | |
| 30488 | 898 | lemma image_compose_permutations_left: | 
| 54681 | 899 | assumes q: "q permutes S" | 
| 900 |   shows "{q \<circ> p | p. p permutes S} = {p . p permutes S}"
 | |
| 901 | apply (rule set_eqI) | |
| 902 | apply auto | |
| 903 | apply (rule permutes_compose) | |
| 904 | using q | |
| 905 | apply auto | |
| 906 | apply (rule_tac x = "inv q \<circ> x" in exI) | |
| 907 | apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o) | |
| 908 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 909 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 910 | lemma image_compose_permutations_right: | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 911 | assumes q: "q permutes S" | 
| 54681 | 912 |   shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
 | 
| 913 | apply (rule set_eqI) | |
| 914 | apply auto | |
| 915 | apply (rule permutes_compose) | |
| 916 | using q | |
| 917 | apply auto | |
| 918 | apply (rule_tac x = "x \<circ> inv q" in exI) | |
| 919 | apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc) | |
| 920 | done | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 921 | |
| 54681 | 922 | lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
 | 
| 923 | by (simp add: permutes_def) metis | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 924 | |
| 54681 | 925 | lemma setsum_permutations_inverse: | 
| 926 |   "setsum f {p. p permutes S} = setsum (\<lambda>p. f(inv p)) {p. p permutes S}"
 | |
| 927 | (is "?lhs = ?rhs") | |
| 928 | proof - | |
| 30036 | 929 |   let ?S = "{p . p permutes S}"
 | 
| 54681 | 930 | have th0: "inj_on inv ?S" | 
| 931 | proof (auto simp add: inj_on_def) | |
| 932 | fix q r | |
| 933 | assume q: "q permutes S" | |
| 934 | and r: "r permutes S" | |
| 935 | and qr: "inv q = inv r" | |
| 936 | then have "inv (inv q) = inv (inv r)" | |
| 937 | by simp | |
| 938 | with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r" | |
| 939 | by metis | |
| 940 | qed | |
| 941 | have th1: "inv ` ?S = ?S" | |
| 942 | using image_inverse_permutations by blast | |
| 943 | have th2: "?rhs = setsum (f \<circ> inv) ?S" | |
| 944 | by (simp add: o_def) | |
| 57418 | 945 | from setsum.reindex[OF th0, of f] show ?thesis unfolding th1 th2 . | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 946 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 947 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 948 | lemma setum_permutations_compose_left: | 
| 30036 | 949 | assumes q: "q permutes S" | 
| 54681 | 950 |   shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
 | 
| 951 | (is "?lhs = ?rhs") | |
| 952 | proof - | |
| 30036 | 953 |   let ?S = "{p. p permutes S}"
 | 
| 54681 | 954 | have th0: "?rhs = setsum (f \<circ> (op \<circ> q)) ?S" | 
| 955 | by (simp add: o_def) | |
| 956 | have th1: "inj_on (op \<circ> q) ?S" | |
| 957 | proof (auto simp add: inj_on_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 958 | fix p r | 
| 54681 | 959 | assume "p permutes S" | 
| 960 | and r: "r permutes S" | |
| 961 | and rp: "q \<circ> p = q \<circ> r" | |
| 962 | then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r" | |
| 963 | by (simp add: comp_assoc) | |
| 964 | with permutes_inj[OF q, unfolded inj_iff] show "p = r" | |
| 965 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 966 | qed | 
| 54681 | 967 | have th3: "(op \<circ> q) ` ?S = ?S" | 
| 968 | using image_compose_permutations_left[OF q] by auto | |
| 57418 | 969 | from setsum.reindex[OF th1, of f] show ?thesis unfolding th0 th1 th3 . | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 970 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 971 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 972 | lemma sum_permutations_compose_right: | 
| 30036 | 973 | assumes q: "q permutes S" | 
| 54681 | 974 |   shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
 | 
| 975 | (is "?lhs = ?rhs") | |
| 976 | proof - | |
| 30036 | 977 |   let ?S = "{p. p permutes S}"
 | 
| 54681 | 978 | have th0: "?rhs = setsum (f \<circ> (\<lambda>p. p \<circ> q)) ?S" | 
| 979 | by (simp add: o_def) | |
| 980 | have th1: "inj_on (\<lambda>p. p \<circ> q) ?S" | |
| 981 | proof (auto simp add: inj_on_def) | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 982 | fix p r | 
| 54681 | 983 | assume "p permutes S" | 
| 984 | and r: "r permutes S" | |
| 985 | and rp: "p \<circ> q = r \<circ> q" | |
| 986 | then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)" | |
| 987 | by (simp add: o_assoc) | |
| 988 | with permutes_surj[OF q, unfolded surj_iff] show "p = r" | |
| 989 | by simp | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 990 | qed | 
| 54681 | 991 | have th3: "(\<lambda>p. p \<circ> q) ` ?S = ?S" | 
| 992 | using image_compose_permutations_right[OF q] by auto | |
| 57418 | 993 | from setsum.reindex[OF th1, of f] | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 994 | show ?thesis unfolding th0 th1 th3 . | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 995 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 996 | |
| 54681 | 997 | |
| 998 | subsection {* Sum over a set of permutations (could generalize to iteration) *}
 | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 999 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1000 | lemma setsum_over_permutations_insert: | 
| 54681 | 1001 | assumes fS: "finite S" | 
| 1002 | and aS: "a \<notin> S" | |
| 1003 |   shows "setsum f {p. p permutes (insert a S)} =
 | |
| 1004 |     setsum (\<lambda>b. setsum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
 | |
| 1005 | proof - | |
| 1006 | have th0: "\<And>f a b. (\<lambda>(b,p). f (Fun.swap a b id \<circ> p)) = f \<circ> (\<lambda>(b,p). Fun.swap a b id \<circ> p)" | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 1007 | by (simp add: fun_eq_iff) | 
| 54681 | 1008 |   have th1: "\<And>P Q. P \<times> Q = {(a,b). a \<in> P \<and> b \<in> Q}"
 | 
| 1009 | by blast | |
| 1010 | have th2: "\<And>P Q. P \<Longrightarrow> (P \<Longrightarrow> Q) \<Longrightarrow> P \<and> Q" | |
| 1011 | by blast | |
| 30488 | 1012 | show ?thesis | 
| 1013 | unfolding permutes_insert | |
| 57418 | 1014 | unfolding setsum.cartesian_product | 
| 57129 
7edb7550663e
introduce more powerful reindexing rules for big operators
 hoelzl parents: 
56608diff
changeset | 1015 | unfolding th1[symmetric] | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1016 | unfolding th0 | 
| 57418 | 1017 | proof (rule setsum.reindex) | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1018 | let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)" | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1019 |     let ?P = "{p. p permutes S}"
 | 
| 54681 | 1020 |     {
 | 
| 1021 | fix b c p q | |
| 1022 | assume b: "b \<in> insert a S" | |
| 1023 | assume c: "c \<in> insert a S" | |
| 1024 | assume p: "p permutes S" | |
| 1025 | assume q: "q permutes S" | |
| 1026 | assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q" | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1027 | 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 | 1028 | unfolding permutes_def by metis+ | 
| 54681 | 1029 | from eq have "(Fun.swap a b id \<circ> p) a = (Fun.swap a c id \<circ> q) a" | 
| 1030 | by simp | |
| 1031 | then have bc: "b = c" | |
| 56545 | 1032 | by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def | 
| 54681 | 1033 | cong del: if_weak_cong split: split_if_asm) | 
| 1034 | from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) = | |
| 1035 | (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp | |
| 1036 | then have "p = q" | |
| 1037 | unfolding o_assoc swap_id_idempotent | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32456diff
changeset | 1038 | by (simp add: o_def) | 
| 54681 | 1039 | with bc have "b = c \<and> p = q" | 
| 1040 | by blast | |
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1041 | } | 
| 30488 | 1042 | then show "inj_on ?f (insert a S \<times> ?P)" | 
| 54681 | 1043 | unfolding inj_on_def by clarify metis | 
| 29840 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1044 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1045 | qed | 
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1046 | |
| 
cfab6a76aa13
Permutations, both general and specifically on finite sets.
 chaieb parents: diff
changeset | 1047 | end | 
| 51489 | 1048 |