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