misc tuning and modernization;
authorwenzelm
Sat Apr 01 18:50:26 2017 +0200 (2017-04-01)
changeset 65342e32eb488c3a3
parent 65341 c82a1620b274
child 65343 0a8e30a7b10e
misc tuning and modernization;
src/HOL/Library/Permutations.thy
     1.1 --- a/src/HOL/Library/Permutations.thy	Sat Apr 01 15:35:32 2017 +0200
     1.2 +++ b/src/HOL/Library/Permutations.thy	Sat Apr 01 18:50:26 2017 +0200
     1.3 @@ -5,39 +5,33 @@
     1.4  section \<open>Permutations, both general and specifically on finite sets.\<close>
     1.5  
     1.6  theory Permutations
     1.7 -imports Binomial Multiset Disjoint_Sets
     1.8 +  imports Binomial Multiset Disjoint_Sets
     1.9  begin
    1.10  
    1.11  subsection \<open>Transpositions\<close>
    1.12  
    1.13 -lemma swap_id_idempotent [simp]:
    1.14 -  "Fun.swap a b id \<circ> Fun.swap a b id = id"
    1.15 -  by (rule ext, auto simp add: Fun.swap_def)
    1.16 +lemma swap_id_idempotent [simp]: "Fun.swap a b id \<circ> Fun.swap a b id = id"
    1.17 +  by (rule ext) (auto simp add: Fun.swap_def)
    1.18  
    1.19 -lemma inv_swap_id:
    1.20 -  "inv (Fun.swap a b id) = Fun.swap a b id"
    1.21 +lemma inv_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id"
    1.22    by (rule inv_unique_comp) simp_all
    1.23  
    1.24 -lemma swap_id_eq:
    1.25 -  "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
    1.26 +lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
    1.27    by (simp add: Fun.swap_def)
    1.28  
    1.29  lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
    1.30    using surj_f_inv_f[of p] by (auto simp add: bij_def)
    1.31  
    1.32  lemma bij_swap_comp:
    1.33 -  assumes bp: "bij p"
    1.34 +  assumes "bij p"
    1.35    shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
    1.36 -  using surj_f_inv_f[OF bij_is_surj[OF bp]]
    1.37 -  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp])
    1.38 +  using surj_f_inv_f[OF bij_is_surj[OF \<open>bij p\<close>]]
    1.39 +  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF \<open>bij p\<close>])
    1.40  
    1.41 -lemma bij_swap_compose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)"
    1.42 -proof -
    1.43 -  assume H: "bij p"
    1.44 -  show ?thesis
    1.45 -    unfolding bij_swap_comp[OF H] bij_swap_iff
    1.46 -    using H .
    1.47 -qed
    1.48 +lemma bij_swap_compose_bij:
    1.49 +  assumes "bij p"
    1.50 +  shows "bij (Fun.swap a b id \<circ> p)"
    1.51 +  by (simp only: bij_swap_comp[OF \<open>bij p\<close>] bij_swap_iff \<open>bij p\<close>)
    1.52  
    1.53  
    1.54  subsection \<open>Basic consequences of the definition\<close>
    1.55 @@ -48,9 +42,8 @@
    1.56  lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
    1.57    unfolding permutes_def by metis
    1.58  
    1.59 -lemma permutes_not_in:
    1.60 -  assumes "f permutes S" "x \<notin> S" shows "f x = x"
    1.61 -  using assms by (auto simp: permutes_def)
    1.62 +lemma permutes_not_in: "f permutes S \<Longrightarrow> x \<notin> S \<Longrightarrow> f x = x"
    1.63 +  by (auto simp: permutes_def)
    1.64  
    1.65  lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S"
    1.66    unfolding permutes_def
    1.67 @@ -63,46 +56,46 @@
    1.68    unfolding permutes_def inj_def by blast
    1.69  
    1.70  lemma permutes_inj_on: "f permutes S \<Longrightarrow> inj_on f A"
    1.71 -  unfolding permutes_def inj_on_def by auto
    1.72 +  by (auto simp: permutes_def inj_on_def)
    1.73  
    1.74  lemma permutes_surj: "p permutes s \<Longrightarrow> surj p"
    1.75    unfolding permutes_def surj_def by metis
    1.76  
    1.77  lemma permutes_bij: "p permutes s \<Longrightarrow> bij p"
    1.78 -unfolding bij_def by (metis permutes_inj permutes_surj)
    1.79 +  unfolding bij_def by (metis permutes_inj permutes_surj)
    1.80  
    1.81  lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S"
    1.82 -by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI)
    1.83 +  by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI)
    1.84  
    1.85  lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S"
    1.86    unfolding permutes_def bij_betw_def inj_on_def
    1.87    by auto (metis image_iff)+
    1.88  
    1.89  lemma permutes_inv_o:
    1.90 -  assumes pS: "p permutes S"
    1.91 +  assumes permutes: "p permutes S"
    1.92    shows "p \<circ> inv p = id"
    1.93      and "inv p \<circ> p = id"
    1.94 -  using permutes_inj[OF pS] permutes_surj[OF pS]
    1.95 +  using permutes_inj[OF permutes] permutes_surj[OF permutes]
    1.96    unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+
    1.97  
    1.98  lemma permutes_inverses:
    1.99    fixes p :: "'a \<Rightarrow> 'a"
   1.100 -  assumes pS: "p permutes S"
   1.101 +  assumes permutes: "p permutes S"
   1.102    shows "p (inv p x) = x"
   1.103      and "inv p (p x) = x"
   1.104 -  using permutes_inv_o[OF pS, unfolded fun_eq_iff o_def] by auto
   1.105 +  using permutes_inv_o[OF permutes, unfolded fun_eq_iff o_def] by auto
   1.106  
   1.107  lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T"
   1.108    unfolding permutes_def by blast
   1.109  
   1.110  lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
   1.111 -  unfolding fun_eq_iff permutes_def by simp metis
   1.112 +  by (auto simp add: fun_eq_iff permutes_def)
   1.113  
   1.114  lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
   1.115 -  unfolding fun_eq_iff permutes_def by simp metis
   1.116 +  by (simp add: fun_eq_iff permutes_def) metis  (*somewhat slow*)
   1.117  
   1.118  lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
   1.119 -  unfolding permutes_def by simp
   1.120 +  by (simp add: permutes_def)
   1.121  
   1.122  lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y"
   1.123    unfolding permutes_def inv_def
   1.124 @@ -124,42 +117,44 @@
   1.125  
   1.126  (* Next three lemmas contributed by Lukas Bulwahn *)
   1.127  lemma permutes_bij_inv_into:
   1.128 -  fixes A :: "'a set" and B :: "'b set"
   1.129 +  fixes A :: "'a set"
   1.130 +    and B :: "'b set"
   1.131    assumes "p permutes A"
   1.132 -  assumes "bij_betw f A B"
   1.133 +    and "bij_betw f A B"
   1.134    shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B"
   1.135  proof (rule bij_imp_permutes)
   1.136 -  have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A"
   1.137 -    using assms by (auto simp add: permutes_imp_bij bij_betw_inv_into)
   1.138 -  from this have "bij_betw (f o p o inv_into A f) B B" by (simp add: bij_betw_trans)
   1.139 -  from this show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B"
   1.140 -    by (subst bij_betw_cong[where g="f o p o inv_into A f"]) auto
   1.141 +  from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A"
   1.142 +    by (auto simp add: permutes_imp_bij bij_betw_inv_into)
   1.143 +  then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B"
   1.144 +    by (simp add: bij_betw_trans)
   1.145 +  then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B"
   1.146 +    by (subst bij_betw_cong[where g="f \<circ> p \<circ> inv_into A f"]) auto
   1.147  next
   1.148    fix x
   1.149    assume "x \<notin> B"
   1.150 -  from this show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto
   1.151 +  then show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto
   1.152  qed
   1.153  
   1.154  lemma permutes_image_mset:
   1.155    assumes "p permutes A"
   1.156    shows "image_mset p (mset_set A) = mset_set A"
   1.157 -using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image)
   1.158 +  using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image)
   1.159  
   1.160  lemma permutes_implies_image_mset_eq:
   1.161    assumes "p permutes A" "\<And>x. x \<in> A \<Longrightarrow> f x = f' (p x)"
   1.162    shows "image_mset f' (mset_set A) = image_mset f (mset_set A)"
   1.163  proof -
   1.164 -  have "f x = f' (p x)" if x: "x \<in># mset_set A" for x
   1.165 -    using assms(2)[of x] x by (cases "finite A") auto
   1.166 -  from this have "image_mset f (mset_set A) = image_mset (f' o p) (mset_set A)"
   1.167 -    using assms by (auto intro!: image_mset_cong)
   1.168 +  have "f x = f' (p x)" if "x \<in># mset_set A" for x
   1.169 +    using assms(2)[of x] that by (cases "finite A") auto
   1.170 +  with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)"
   1.171 +    by (auto intro!: image_mset_cong)
   1.172    also have "\<dots> = image_mset f' (image_mset p (mset_set A))"
   1.173      by (simp add: image_mset.compositionality)
   1.174    also have "\<dots> = image_mset f' (mset_set A)"
   1.175    proof -
   1.176 -    from assms have "image_mset p (mset_set A) = mset_set A"
   1.177 -      using permutes_image_mset by blast
   1.178 -    from this show ?thesis by simp
   1.179 +    from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A"
   1.180 +      by blast
   1.181 +    then show ?thesis by simp
   1.182    qed
   1.183    finally show ?thesis ..
   1.184  qed
   1.185 @@ -168,36 +163,41 @@
   1.186  subsection \<open>Group properties\<close>
   1.187  
   1.188  lemma permutes_id: "id permutes S"
   1.189 -  unfolding permutes_def by simp
   1.190 +  by (simp add: permutes_def)
   1.191  
   1.192  lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S"
   1.193    unfolding permutes_def o_def by metis
   1.194  
   1.195  lemma permutes_inv:
   1.196 -  assumes pS: "p permutes S"
   1.197 +  assumes "p permutes S"
   1.198    shows "inv p permutes S"
   1.199 -  using pS unfolding permutes_def permutes_inv_eq[OF pS] by metis
   1.200 +  using assms unfolding permutes_def permutes_inv_eq[OF assms] by metis
   1.201  
   1.202  lemma permutes_inv_inv:
   1.203 -  assumes pS: "p permutes S"
   1.204 +  assumes "p permutes S"
   1.205    shows "inv (inv p) = p"
   1.206 -  unfolding fun_eq_iff permutes_inv_eq[OF pS] permutes_inv_eq[OF permutes_inv[OF pS]]
   1.207 +  unfolding fun_eq_iff permutes_inv_eq[OF assms] permutes_inv_eq[OF permutes_inv[OF assms]]
   1.208    by blast
   1.209  
   1.210  lemma permutes_invI:
   1.211    assumes perm: "p permutes S"
   1.212 -      and inv:  "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
   1.213 -      and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
   1.214 -  shows   "inv p = p'"
   1.215 +    and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
   1.216 +    and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
   1.217 +  shows "inv p = p'"
   1.218  proof
   1.219 -  fix x show "inv p x = p' x"
   1.220 +  show "inv p x = p' x" for x
   1.221    proof (cases "x \<in> S")
   1.222 -    assume [simp]: "x \<in> S"
   1.223 -    from assms have "p' x = p' (p (inv p x))" by (simp add: permutes_inverses)
   1.224 -    also from permutes_inv[OF perm]
   1.225 -      have "\<dots> = inv p x" by (subst inv) (simp_all add: permutes_in_image)
   1.226 -    finally show "inv p x = p' x" ..
   1.227 -  qed (insert permutes_inv[OF perm], simp_all add: outside permutes_not_in)
   1.228 +    case True
   1.229 +    from assms have "p' x = p' (p (inv p x))"
   1.230 +      by (simp add: permutes_inverses)
   1.231 +    also from permutes_inv[OF perm] True have "\<dots> = inv p x"
   1.232 +      by (subst inv) (simp_all add: permutes_in_image)
   1.233 +    finally show ?thesis ..
   1.234 +  next
   1.235 +    case False
   1.236 +    with permutes_inv[OF perm] show ?thesis
   1.237 +      by (simp_all add: outside permutes_not_in)
   1.238 +  qed
   1.239  qed
   1.240  
   1.241  lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A"
   1.242 @@ -207,58 +207,54 @@
   1.243  subsection \<open>The number of permutations on a finite set\<close>
   1.244  
   1.245  lemma permutes_insert_lemma:
   1.246 -  assumes pS: "p permutes (insert a S)"
   1.247 +  assumes "p permutes (insert a S)"
   1.248    shows "Fun.swap a (p a) id \<circ> p permutes S"
   1.249    apply (rule permutes_superset[where S = "insert a S"])
   1.250 -  apply (rule permutes_compose[OF pS])
   1.251 +  apply (rule permutes_compose[OF assms])
   1.252    apply (rule permutes_swap_id, simp)
   1.253 -  using permutes_in_image[OF pS, of a]
   1.254 +  using permutes_in_image[OF assms, of a]
   1.255    apply simp
   1.256    apply (auto simp add: Ball_def Fun.swap_def)
   1.257    done
   1.258  
   1.259  lemma permutes_insert: "{p. p permutes (insert a S)} =
   1.260 -  (\<lambda>(b,p). Fun.swap a b id \<circ> p) ` {(b,p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
   1.261 +  (\<lambda>(b, p). Fun.swap a b id \<circ> p) ` {(b, p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
   1.262  proof -
   1.263 -  {
   1.264 -    fix p
   1.265 -    {
   1.266 -      assume pS: "p permutes insert a S"
   1.267 +  have "p permutes insert a S \<longleftrightarrow>
   1.268 +    (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p
   1.269 +  proof -
   1.270 +    have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S"
   1.271 +      if p: "p permutes insert a S"
   1.272 +    proof -
   1.273        let ?b = "p a"
   1.274        let ?q = "Fun.swap a (p a) id \<circ> p"
   1.275 -      have th0: "p = Fun.swap a ?b id \<circ> ?q"
   1.276 -        unfolding fun_eq_iff o_assoc by simp
   1.277 -      have th1: "?b \<in> insert a S"
   1.278 -        unfolding permutes_in_image[OF pS] by simp
   1.279 -      from permutes_insert_lemma[OF pS] th0 th1
   1.280 -      have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S" by blast
   1.281 -    }
   1.282 -    moreover
   1.283 -    {
   1.284 -      fix b q
   1.285 -      assume bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S"
   1.286 -      from permutes_subset[OF bq(3), of "insert a S"]
   1.287 -      have qS: "q permutes insert a S"
   1.288 +      have *: "p = Fun.swap a ?b id \<circ> ?q"
   1.289 +        by (simp add: fun_eq_iff o_assoc)
   1.290 +      have **: "?b \<in> insert a S"
   1.291 +        unfolding permutes_in_image[OF p] by simp
   1.292 +      from permutes_insert_lemma[OF p] * ** show ?thesis
   1.293 +       by blast
   1.294 +    qed
   1.295 +    moreover have "p permutes insert a S"
   1.296 +      if bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" for b q
   1.297 +    proof -
   1.298 +      from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S"
   1.299          by auto
   1.300 -      have aS: "a \<in> insert a S"
   1.301 +      have a: "a \<in> insert a S"
   1.302          by simp
   1.303 -      from bq(1) permutes_compose[OF qS permutes_swap_id[OF aS bq(2)]]
   1.304 -      have "p permutes insert a S"
   1.305 +      from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis
   1.306          by simp
   1.307 -    }
   1.308 -    ultimately have "p permutes insert a S \<longleftrightarrow>
   1.309 -        (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)"
   1.310 -      by blast
   1.311 -  }
   1.312 -  then show ?thesis
   1.313 -    by auto
   1.314 +    qed
   1.315 +    ultimately show ?thesis by blast
   1.316 +  qed
   1.317 +  then show ?thesis by auto
   1.318  qed
   1.319  
   1.320  lemma card_permutations:
   1.321 -  assumes Sn: "card S = n"
   1.322 -    and fS: "finite S"
   1.323 +  assumes "card S = n"
   1.324 +    and "finite S"
   1.325    shows "card {p. p permutes S} = fact n"
   1.326 -  using fS Sn
   1.327 +  using assms(2,1)
   1.328  proof (induct arbitrary: n)
   1.329    case empty
   1.330    then show ?case by simp
   1.331 @@ -266,21 +262,20 @@
   1.332    case (insert x F)
   1.333    {
   1.334      fix n
   1.335 -    assume H0: "card (insert x F) = n"
   1.336 +    assume card_insert: "card (insert x F) = n"
   1.337      let ?xF = "{p. p permutes insert x F}"
   1.338      let ?pF = "{p. p permutes F}"
   1.339      let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
   1.340      let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
   1.341 -    from permutes_insert[of x F]
   1.342 -    have xfgpF': "?xF = ?g ` ?pF'" .
   1.343 -    have Fs: "card F = n - 1"
   1.344 -      using \<open>x \<notin> F\<close> H0 \<open>finite F\<close> by auto
   1.345 -    from insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
   1.346 -      using \<open>finite F\<close> by auto
   1.347 +    have xfgpF': "?xF = ?g ` ?pF'"
   1.348 +      by (rule permutes_insert[of x F])
   1.349 +    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1"
   1.350 +      by auto
   1.351 +    from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
   1.352 +      by auto
   1.353      then have "finite ?pF"
   1.354        by (auto intro: card_ge_0_finite)
   1.355 -    then have pF'f: "finite ?pF'"
   1.356 -      using H0 \<open>finite F\<close>
   1.357 +    with \<open>finite F\<close> card_insert have pF'f: "finite ?pF'"
   1.358        apply (simp only: Collect_case_prod Collect_mem_eq)
   1.359        apply (rule finite_cartesian_product)
   1.360        apply simp_all
   1.361 @@ -290,64 +285,54 @@
   1.362      proof -
   1.363        {
   1.364          fix b p c q
   1.365 -        assume bp: "(b,p) \<in> ?pF'"
   1.366 -        assume cq: "(c,q) \<in> ?pF'"
   1.367 -        assume eq: "?g (b,p) = ?g (c,q)"
   1.368 -        from bp cq have ths: "b \<in> insert x F" "c \<in> insert x F" "x \<in> insert x F"
   1.369 -          "p permutes F" "q permutes F"
   1.370 +        assume bp: "(b, p) \<in> ?pF'"
   1.371 +        assume cq: "(c, q) \<in> ?pF'"
   1.372 +        assume eq: "?g (b, p) = ?g (c, q)"
   1.373 +        from bp cq have pF: "p permutes F" and qF: "q permutes F"
   1.374            by auto
   1.375 -        from ths(4) \<open>x \<notin> F\<close> eq have "b = ?g (b,p) x"
   1.376 -          unfolding permutes_def
   1.377 -          by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff)
   1.378 -        also have "\<dots> = ?g (c,q) x"
   1.379 -          using ths(5) \<open>x \<notin> F\<close> eq
   1.380 -          by (auto simp add: swap_def fun_upd_def fun_eq_iff)
   1.381 -        also have "\<dots> = c"
   1.382 -          using ths(5) \<open>x \<notin> F\<close>
   1.383 -          unfolding permutes_def
   1.384 -          by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff)
   1.385 -        finally have bc: "b = c" .
   1.386 +        from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x"
   1.387 +          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
   1.388 +        also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x"
   1.389 +          by (auto simp: swap_def fun_upd_def fun_eq_iff)
   1.390 +        also from qF \<open>x \<notin> F\<close> have "\<dots> = c"
   1.391 +          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
   1.392 +        finally have "b = c" .
   1.393          then have "Fun.swap x b id = Fun.swap x c id"
   1.394            by simp
   1.395          with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q"
   1.396            by simp
   1.397 -        then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) =
   1.398 -          Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
   1.399 +        then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
   1.400            by simp
   1.401          then have "p = q"
   1.402            by (simp add: o_assoc)
   1.403 -        with bc have "(b, p) = (c, q)"
   1.404 +        with \<open>b = c\<close> have "(b, p) = (c, q)"
   1.405            by simp
   1.406        }
   1.407        then show ?thesis
   1.408          unfolding inj_on_def by blast
   1.409      qed
   1.410 -    from \<open>x \<notin> F\<close> H0 have n0: "n \<noteq> 0"
   1.411 -      using \<open>finite F\<close> by auto
   1.412 +    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0"
   1.413 +      by auto
   1.414      then have "\<exists>m. n = Suc m"
   1.415        by presburger
   1.416 -    then obtain m where n[simp]: "n = Suc m"
   1.417 +    then obtain m where n: "n = Suc m"
   1.418        by blast
   1.419 -    from pFs H0 have xFc: "card ?xF = fact n"
   1.420 +    from pFs card_insert have *: "card ?xF = fact n"
   1.421        unfolding xfgpF' card_image[OF ginj]
   1.422        using \<open>finite F\<close> \<open>finite ?pF\<close>
   1.423 -      apply (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product)
   1.424 -      apply simp
   1.425 -      done
   1.426 +      by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n)
   1.427      from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF"
   1.428 -      unfolding xfgpF' by simp
   1.429 -    have "card ?xF = fact n"
   1.430 -      using xFf xFc unfolding xFf by blast
   1.431 +      by (simp add: xfgpF' n)
   1.432 +    from * have "card ?xF = fact n"
   1.433 +      unfolding xFf by blast
   1.434    }
   1.435 -  then show ?case
   1.436 -    using insert by simp
   1.437 +  with insert show ?case by simp
   1.438  qed
   1.439  
   1.440  lemma finite_permutations:
   1.441 -  assumes fS: "finite S"
   1.442 +  assumes "finite S"
   1.443    shows "finite {p. p permutes S}"
   1.444 -  using card_permutations[OF refl fS]
   1.445 -  by (auto intro: card_ge_0_finite)
   1.446 +  using card_permutations[OF refl assms] by (auto intro: card_ge_0_finite)
   1.447  
   1.448  
   1.449  subsection \<open>Permutations of index set for iterated operations\<close>
   1.450 @@ -387,9 +372,9 @@
   1.451  subsection \<open>Permutations as transposition sequences\<close>
   1.452  
   1.453  inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
   1.454 -where
   1.455 -  id[simp]: "swapidseq 0 id"
   1.456 -| comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
   1.457 +  where
   1.458 +    id[simp]: "swapidseq 0 id"
   1.459 +  | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
   1.460  
   1.461  declare id[unfolded id_def, simp]
   1.462  
   1.463 @@ -410,12 +395,16 @@
   1.464    done
   1.465  
   1.466  lemma permutation_swap_id: "permutation (Fun.swap a b id)"
   1.467 -  apply (cases "a = b")
   1.468 -  apply simp_all
   1.469 -  unfolding permutation_def
   1.470 -  using swapidseq_swap[of a b]
   1.471 -  apply blast
   1.472 -  done
   1.473 +proof (cases "a = b")
   1.474 +  case True
   1.475 +  then show ?thesis by simp
   1.476 +next
   1.477 +  case False
   1.478 +  then show ?thesis
   1.479 +    unfolding permutation_def
   1.480 +    using swapidseq_swap[of a b] by blast
   1.481 +qed
   1.482 +
   1.483  
   1.484  lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
   1.485  proof (induct n p arbitrary: m q rule: swapidseq.induct)
   1.486 @@ -423,13 +412,13 @@
   1.487    then show ?case by simp
   1.488  next
   1.489    case (comp_Suc n p a b m q)
   1.490 -  have th: "Suc n + m = Suc (n + m)"
   1.491 +  have eq: "Suc n + m = Suc (n + m)"
   1.492      by arith
   1.493    show ?case
   1.494 -    unfolding th comp_assoc
   1.495 +    apply (simp only: eq comp_assoc)
   1.496      apply (rule swapidseq.comp_Suc)
   1.497      using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
   1.498 -    apply blast+
   1.499 +     apply blast+
   1.500      done
   1.501  qed
   1.502  
   1.503 @@ -437,10 +426,8 @@
   1.504    unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
   1.505  
   1.506  lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
   1.507 -  apply (induct n p rule: swapidseq.induct)
   1.508 -  using swapidseq_swap[of a b]
   1.509 -  apply (auto simp add: comp_assoc intro: swapidseq.comp_Suc)
   1.510 -  done
   1.511 +  by (induct n p rule: swapidseq.induct)
   1.512 +    (use swapidseq_swap[of a b] in \<open>auto simp add: comp_assoc intro: swapidseq.comp_Suc\<close>)
   1.513  
   1.514  lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id"
   1.515  proof (induct n p rule: swapidseq.induct)
   1.516 @@ -453,27 +440,27 @@
   1.517      by blast
   1.518    let ?q = "q \<circ> Fun.swap a b id"
   1.519    note H = comp_Suc.hyps
   1.520 -  from swapidseq_swap[of a b] H(3) have th0: "swapidseq 1 (Fun.swap a b id)"
   1.521 +  from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (Fun.swap a b id)"
   1.522      by simp
   1.523 -  from swapidseq_comp_add[OF q(1) th0] have th1: "swapidseq (Suc n) ?q"
   1.524 +  from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q"
   1.525      by simp
   1.526    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"
   1.527      by (simp add: o_assoc)
   1.528    also have "\<dots> = id"
   1.529      by (simp add: q(2))
   1.530 -  finally have th2: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
   1.531 +  finally have ***: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
   1.532    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"
   1.533      by (simp only: o_assoc)
   1.534    then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
   1.535      by (simp add: q(3))
   1.536 -  with th1 th2 show ?case
   1.537 +  with ** *** show ?case
   1.538      by blast
   1.539  qed
   1.540  
   1.541  lemma swapidseq_inverse:
   1.542 -  assumes H: "swapidseq n p"
   1.543 +  assumes "swapidseq n p"
   1.544    shows "swapidseq n (inv p)"
   1.545 -  using swapidseq_inverse_exists[OF H] inv_unique_comp[of p] by auto
   1.546 +  using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto
   1.547  
   1.548  lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
   1.549    using permutation_def swapidseq_inverse by blast
   1.550 @@ -494,61 +481,60 @@
   1.551    (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
   1.552      Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)"
   1.553  proof -
   1.554 -  assume H: "a \<noteq> b" "c \<noteq> d"
   1.555 +  assume neq: "a \<noteq> b" "c \<noteq> d"
   1.556    have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
   1.557      (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
   1.558        (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
   1.559          Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
   1.560      apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
   1.561 -    apply (simp_all only: swap_commute)
   1.562 +     apply (simp_all only: swap_commute)
   1.563      apply (case_tac "a = c \<and> b = d")
   1.564 -    apply (clarsimp simp only: swap_commute swap_id_idempotent)
   1.565 +     apply (clarsimp simp only: swap_commute swap_id_idempotent)
   1.566      apply (case_tac "a = c \<and> b \<noteq> d")
   1.567 -    apply (rule disjI2)
   1.568 -    apply (rule_tac x="b" in exI)
   1.569 -    apply (rule_tac x="d" in exI)
   1.570 -    apply (rule_tac x="b" in exI)
   1.571 -    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   1.572 +     apply (rule disjI2)
   1.573 +     apply (rule_tac x="b" in exI)
   1.574 +     apply (rule_tac x="d" in exI)
   1.575 +     apply (rule_tac x="b" in exI)
   1.576 +     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   1.577      apply (case_tac "a \<noteq> c \<and> b = d")
   1.578 -    apply (rule disjI2)
   1.579 -    apply (rule_tac x="c" in exI)
   1.580 -    apply (rule_tac x="d" in exI)
   1.581 -    apply (rule_tac x="c" in exI)
   1.582 -    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   1.583 +     apply (rule disjI2)
   1.584 +     apply (rule_tac x="c" in exI)
   1.585 +     apply (rule_tac x="d" in exI)
   1.586 +     apply (rule_tac x="c" in exI)
   1.587 +     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   1.588      apply (rule disjI2)
   1.589      apply (rule_tac x="c" in exI)
   1.590      apply (rule_tac x="d" in exI)
   1.591      apply (rule_tac x="b" in exI)
   1.592      apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   1.593      done
   1.594 -  with H show ?thesis by metis
   1.595 +  with neq show ?thesis by metis
   1.596  qed
   1.597  
   1.598  lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
   1.599 -  using swapidseq.cases[of 0 p "p = id"]
   1.600 -  by auto
   1.601 +  using swapidseq.cases[of 0 p "p = id"] by auto
   1.602  
   1.603  lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
   1.604 -  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)"
   1.605 +    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)"
   1.606    apply (rule iffI)
   1.607 -  apply (erule swapidseq.cases[of n p])
   1.608 -  apply simp
   1.609 -  apply (rule disjI2)
   1.610 -  apply (rule_tac x= "a" in exI)
   1.611 -  apply (rule_tac x= "b" in exI)
   1.612 -  apply (rule_tac x= "pa" in exI)
   1.613 -  apply (rule_tac x= "na" in exI)
   1.614 -  apply simp
   1.615 +   apply (erule swapidseq.cases[of n p])
   1.616 +    apply simp
   1.617 +   apply (rule disjI2)
   1.618 +   apply (rule_tac x= "a" in exI)
   1.619 +   apply (rule_tac x= "b" in exI)
   1.620 +   apply (rule_tac x= "pa" in exI)
   1.621 +   apply (rule_tac x= "na" in exI)
   1.622 +   apply simp
   1.623    apply auto
   1.624    apply (rule comp_Suc, simp_all)
   1.625    done
   1.626  
   1.627  lemma fixing_swapidseq_decrease:
   1.628 -  assumes spn: "swapidseq n p"
   1.629 -    and ab: "a \<noteq> b"
   1.630 -    and pa: "(Fun.swap a b id \<circ> p) a = a"
   1.631 +  assumes "swapidseq n p"
   1.632 +    and "a \<noteq> b"
   1.633 +    and "(Fun.swap a b id \<circ> p) a = a"
   1.634    shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
   1.635 -  using spn ab pa
   1.636 +  using assms
   1.637  proof (induct n arbitrary: p a b)
   1.638    case 0
   1.639    then show ?case
   1.640 @@ -559,49 +545,44 @@
   1.641    obtain c d q m where
   1.642      cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
   1.643      by auto
   1.644 -  {
   1.645 -    assume H: "Fun.swap a b id \<circ> Fun.swap c d id = id"
   1.646 -    have ?case by (simp only: cdqm o_assoc H) (simp add: cdqm)
   1.647 -  }
   1.648 -  moreover
   1.649 -  {
   1.650 -    fix x y z
   1.651 -    assume H: "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
   1.652 +  consider "Fun.swap a b id \<circ> Fun.swap c d id = id"
   1.653 +    | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
   1.654        "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
   1.655 -    from H have az: "a \<noteq> z"
   1.656 +    using swap_general[OF Suc.prems(2) cdqm(4)] by metis
   1.657 +  then show ?case
   1.658 +  proof cases
   1.659 +    case 1
   1.660 +    then show ?thesis
   1.661 +      by (simp only: cdqm o_assoc) (simp add: cdqm)
   1.662 +  next
   1.663 +    case prems: 2
   1.664 +    then have az: "a \<noteq> z"
   1.665        by simp
   1.666 -
   1.667 -    {
   1.668 -      fix h
   1.669 -      have "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a"
   1.670 -        using H by (simp add: Fun.swap_def)
   1.671 -    }
   1.672 -    note th3 = this
   1.673 +    from prems have *: "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" for h
   1.674 +      by (simp add: Fun.swap_def)
   1.675      from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)"
   1.676        by simp
   1.677      then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
   1.678 -      by (simp add: o_assoc H)
   1.679 +      by (simp add: o_assoc prems)
   1.680      then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a"
   1.681        by simp
   1.682      then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
   1.683        unfolding Suc by metis
   1.684 -    then have th1: "(Fun.swap a z id \<circ> q) a = a"
   1.685 -      unfolding th3 .
   1.686 -    from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az th1]
   1.687 -    have th2: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
   1.688 +    then have "(Fun.swap a z id \<circ> q) a = a"
   1.689 +      by (simp only: *)
   1.690 +    from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az this]
   1.691 +    have **: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
   1.692        by blast+
   1.693 -    have th: "Suc n - 1 = Suc (n - 1)"
   1.694 -      using th2(2) by auto
   1.695 -    have ?case
   1.696 -      unfolding cdqm(2) H o_assoc th
   1.697 +    from \<open>n \<noteq> 0\<close> have ***: "Suc n - 1 = Suc (n - 1)"
   1.698 +      by auto
   1.699 +    show ?thesis
   1.700 +      apply (simp only: cdqm(2) prems o_assoc ***)
   1.701        apply (simp only: Suc_not_Zero simp_thms comp_assoc)
   1.702        apply (rule comp_Suc)
   1.703 -      using th2 H
   1.704 -      apply blast+
   1.705 +      using ** prems
   1.706 +       apply blast+
   1.707        done
   1.708 -  }
   1.709 -  ultimately show ?case
   1.710 -    using swap_general[OF Suc.prems(2) cdqm(4)] by metis
   1.711 +  qed
   1.712  qed
   1.713  
   1.714  lemma swapidseq_identity_even:
   1.715 @@ -609,26 +590,24 @@
   1.716    shows "even n"
   1.717    using \<open>swapidseq n id\<close>
   1.718  proof (induct n rule: nat_less_induct)
   1.719 -  fix n
   1.720 -  assume H: "\<forall>m<n. swapidseq m (id::'a \<Rightarrow> 'a) \<longrightarrow> even m" "swapidseq n (id :: 'a \<Rightarrow> 'a)"
   1.721 -  {
   1.722 -    assume "n = 0"
   1.723 -    then have "even n" by presburger
   1.724 -  }
   1.725 -  moreover
   1.726 -  {
   1.727 -    fix a b :: 'a and q m
   1.728 -    assume h: "n = Suc m" "(id :: 'a \<Rightarrow> 'a) = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
   1.729 +  case H: (1 n)
   1.730 +  consider "n = 0"
   1.731 +    | a b :: 'a and q m where "n = Suc m" "id = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
   1.732 +    using H(2)[unfolded swapidseq_cases[of n id]] by auto
   1.733 +  then show ?case
   1.734 +  proof cases
   1.735 +    case 1
   1.736 +    then show ?thesis by presburger
   1.737 +  next
   1.738 +    case h: 2
   1.739      from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
   1.740      have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
   1.741        by auto
   1.742      from h m have mn: "m - 1 < n"
   1.743        by arith
   1.744 -    from H(1)[rule_format, OF mn m(2)] h(1) m(1) have "even n"
   1.745 +    from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis
   1.746        by presburger
   1.747 -  }
   1.748 -  ultimately show "even n"
   1.749 -    using H(2)[unfolded swapidseq_cases[of n id]] by auto
   1.750 +  qed
   1.751  qed
   1.752  
   1.753  
   1.754 @@ -641,11 +620,9 @@
   1.755      and n: "swapidseq n p"
   1.756    shows "even m \<longleftrightarrow> even n"
   1.757  proof -
   1.758 -  from swapidseq_inverse_exists[OF n]
   1.759 -  obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   1.760 +  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   1.761      by blast
   1.762 -  from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]]
   1.763 -  show ?thesis
   1.764 +  from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis
   1.765      by arith
   1.766  qed
   1.767  
   1.768 @@ -655,9 +632,9 @@
   1.769    shows "evenperm p = b"
   1.770    unfolding n[symmetric] evenperm_def
   1.771    apply (rule swapidseq_even_even[where p = p])
   1.772 -  apply (rule someI[where x = n])
   1.773 +   apply (rule someI[where x = n])
   1.774    using p
   1.775 -  apply blast+
   1.776 +   apply blast+
   1.777    done
   1.778  
   1.779  
   1.780 @@ -670,29 +647,26 @@
   1.781    by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
   1.782  
   1.783  lemma evenperm_comp:
   1.784 -  assumes p: "permutation p"
   1.785 -    and q:"permutation q"
   1.786 -  shows "evenperm (p \<circ> q) = (evenperm p = evenperm q)"
   1.787 +  assumes "permutation p" "permutation q"
   1.788 +  shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q"
   1.789  proof -
   1.790 -  from p q obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
   1.791 +  from assms obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
   1.792      unfolding permutation_def by blast
   1.793 -  note nm =  swapidseq_comp_add[OF n m]
   1.794 -  have th: "even (n + m) = (even n \<longleftrightarrow> even m)"
   1.795 +  have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)"
   1.796      by arith
   1.797    from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
   1.798 -    evenperm_unique[OF nm th]
   1.799 -  show ?thesis
   1.800 +    and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis
   1.801      by blast
   1.802  qed
   1.803  
   1.804  lemma evenperm_inv:
   1.805 -  assumes p: "permutation p"
   1.806 +  assumes "permutation p"
   1.807    shows "evenperm (inv p) = evenperm p"
   1.808  proof -
   1.809 -  from p obtain n where n: "swapidseq n p"
   1.810 +  from assms obtain n where n: "swapidseq n p"
   1.811      unfolding permutation_def by blast
   1.812 -  from evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]]
   1.813 -  show ?thesis .
   1.814 +  show ?thesis
   1.815 +    by (rule evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]])
   1.816  qed
   1.817  
   1.818  
   1.819 @@ -701,67 +675,71 @@
   1.820  lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
   1.821    unfolding bij_def inj_def surj_def
   1.822    apply auto
   1.823 -  apply metis
   1.824 +   apply metis
   1.825    apply metis
   1.826    done
   1.827  
   1.828  lemma permutation_bijective:
   1.829 -  assumes p: "permutation p"
   1.830 +  assumes "permutation p"
   1.831    shows "bij p"
   1.832  proof -
   1.833 -  from p obtain n where n: "swapidseq n p"
   1.834 +  from assms obtain n where n: "swapidseq n p"
   1.835      unfolding permutation_def by blast
   1.836 -  from swapidseq_inverse_exists[OF n]
   1.837 -  obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   1.838 +  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   1.839      by blast
   1.840 -  then show ?thesis unfolding bij_iff
   1.841 +  then show ?thesis
   1.842 +    unfolding bij_iff
   1.843      apply (auto simp add: fun_eq_iff)
   1.844      apply metis
   1.845      done
   1.846  qed
   1.847  
   1.848  lemma permutation_finite_support:
   1.849 -  assumes p: "permutation p"
   1.850 +  assumes "permutation p"
   1.851    shows "finite {x. p x \<noteq> x}"
   1.852  proof -
   1.853 -  from p obtain n where n: "swapidseq n p"
   1.854 +  from assms obtain n where "swapidseq n p"
   1.855      unfolding permutation_def by blast
   1.856 -  from n show ?thesis
   1.857 +  then show ?thesis
   1.858    proof (induct n p rule: swapidseq.induct)
   1.859      case id
   1.860      then show ?case by simp
   1.861    next
   1.862      case (comp_Suc n p a b)
   1.863      let ?S = "insert a (insert b {x. p x \<noteq> x})"
   1.864 -    from comp_Suc.hyps(2) have fS: "finite ?S"
   1.865 +    from comp_Suc.hyps(2) have *: "finite ?S"
   1.866        by simp
   1.867 -    from \<open>a \<noteq> b\<close> have th: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
   1.868 -      by (auto simp add: Fun.swap_def)
   1.869 -    from finite_subset[OF th fS] show ?case  .
   1.870 +    from \<open>a \<noteq> b\<close> have **: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
   1.871 +      by (auto simp: Fun.swap_def)
   1.872 +    show ?case
   1.873 +      by (rule finite_subset[OF ** *])
   1.874    qed
   1.875  qed
   1.876  
   1.877  lemma permutation_lemma:
   1.878 -  assumes fS: "finite S"
   1.879 -    and p: "bij p"
   1.880 -    and pS: "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
   1.881 +  assumes "finite S"
   1.882 +    and "bij p"
   1.883 +    and "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
   1.884    shows "permutation p"
   1.885 -  using fS p pS
   1.886 +  using assms
   1.887  proof (induct S arbitrary: p rule: finite_induct)
   1.888 -  case (empty p)
   1.889 -  then show ?case by simp
   1.890 +  case empty
   1.891 +  then show ?case
   1.892 +    by simp
   1.893  next
   1.894    case (insert a F p)
   1.895    let ?r = "Fun.swap a (p a) id \<circ> p"
   1.896    let ?q = "Fun.swap a (p a) id \<circ> ?r"
   1.897 -  have raa: "?r a = a"
   1.898 +  have *: "?r a = a"
   1.899      by (simp add: Fun.swap_def)
   1.900 -  from bij_swap_compose_bij[OF insert(4)] have br: "bij ?r"  .
   1.901 -  from insert raa have th: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
   1.902 +  from insert * have **: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
   1.903      by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3))
   1.904 -  from insert(3)[OF br th] have rp: "permutation ?r" .
   1.905 -  have "permutation ?q"
   1.906 -    by (simp add: permutation_compose permutation_swap_id rp)
   1.907 +  have "bij ?r"
   1.908 +    by (rule bij_swap_compose_bij[OF insert(4)])
   1.909 +  have "permutation ?r"
   1.910 +    by (rule insert(3)[OF \<open>bij ?r\<close> **])
   1.911 +  then have "permutation ?q"
   1.912 +    by (simp add: permutation_compose permutation_swap_id)
   1.913    then show ?case
   1.914      by (simp add: o_assoc)
   1.915  qed
   1.916 @@ -769,8 +747,8 @@
   1.917  lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
   1.918    (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
   1.919  proof
   1.920 -  assume p: ?lhs
   1.921 -  from p permutation_bijective permutation_finite_support show "?b \<and> ?f"
   1.922 +  assume ?lhs
   1.923 +  with permutation_bijective permutation_finite_support show "?b \<and> ?f"
   1.924      by auto
   1.925  next
   1.926    assume "?b \<and> ?f"
   1.927 @@ -780,11 +758,10 @@
   1.928  qed
   1.929  
   1.930  lemma permutation_inverse_works:
   1.931 -  assumes p: "permutation p"
   1.932 +  assumes "permutation p"
   1.933    shows "inv p \<circ> p = id"
   1.934      and "p \<circ> inv p = id"
   1.935 -  using permutation_bijective [OF p]
   1.936 -  unfolding bij_def inj_iff surj_iff by auto
   1.937 +  using permutation_bijective [OF assms] by (auto simp: bij_def inj_iff surj_iff)
   1.938  
   1.939  lemma permutation_inverse_compose:
   1.940    assumes p: "permutation p"
   1.941 @@ -797,33 +774,34 @@
   1.942      by (simp add: o_assoc)
   1.943    also have "\<dots> = id"
   1.944      by (simp add: ps qs)
   1.945 -  finally have th0: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
   1.946 +  finally have *: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
   1.947    have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
   1.948      by (simp add: o_assoc)
   1.949    also have "\<dots> = id"
   1.950      by (simp add: ps qs)
   1.951 -  finally have th1: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
   1.952 -  from inv_unique_comp[OF th0 th1] show ?thesis .
   1.953 +  finally have **: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
   1.954 +  show ?thesis
   1.955 +    by (rule inv_unique_comp[OF * **])
   1.956  qed
   1.957  
   1.958  
   1.959 -subsection \<open>Relation to "permutes"\<close>
   1.960 +subsection \<open>Relation to \<open>permutes\<close>\<close>
   1.961  
   1.962  lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
   1.963    unfolding permutation permutes_def bij_iff[symmetric]
   1.964    apply (rule iffI, clarify)
   1.965 -  apply (rule exI[where x="{x. p x \<noteq> x}"])
   1.966 -  apply simp
   1.967 +   apply (rule exI[where x="{x. p x \<noteq> x}"])
   1.968 +   apply simp
   1.969    apply clarsimp
   1.970    apply (rule_tac B="S" in finite_subset)
   1.971 -  apply auto
   1.972 +   apply auto
   1.973    done
   1.974  
   1.975  
   1.976  subsection \<open>Hence a sort of induction principle composing by swaps\<close>
   1.977  
   1.978  lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow>
   1.979 -  (\<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>
   1.980 +  (\<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>
   1.981    (\<And>p. p permutes S \<Longrightarrow> P p)"
   1.982  proof (induct S rule: finite_induct)
   1.983    case empty
   1.984 @@ -842,12 +820,11 @@
   1.985    have xF: "x \<in> insert x F"
   1.986      by simp
   1.987    have rp: "permutation ?r"
   1.988 -    unfolding permutation_permutes using insert.hyps(1)
   1.989 -      permutes_insert_lemma[OF insert.prems(3)]
   1.990 +    unfolding permutation_permutes
   1.991 +    using insert.hyps(1) permutes_insert_lemma[OF insert.prems(3)]
   1.992      by blast
   1.993 -  from insert.prems(2)[OF xF pxF Pr Pr rp]
   1.994 -  show ?case
   1.995 -    unfolding qp .
   1.996 +  from insert.prems(2)[OF xF pxF Pr Pr rp] qp show ?case
   1.997 +    by (simp only:)
   1.998  qed
   1.999  
  1.1000  
  1.1001 @@ -878,17 +855,17 @@
  1.1002  
  1.1003  text \<open>This function permutes a list by applying a permutation to the indices.\<close>
  1.1004  
  1.1005 -definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
  1.1006 -  "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
  1.1007 +definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list"
  1.1008 +  where "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
  1.1009  
  1.1010  lemma permute_list_map:
  1.1011    assumes "f permutes {..<length xs}"
  1.1012 -  shows   "permute_list f (map g xs) = map g (permute_list f xs)"
  1.1013 +  shows "permute_list f (map g xs) = map g (permute_list f xs)"
  1.1014    using permutes_in_image[OF assms] by (auto simp: permute_list_def)
  1.1015  
  1.1016  lemma permute_list_nth:
  1.1017    assumes "f permutes {..<length xs}" "i < length xs"
  1.1018 -  shows   "permute_list f xs ! i = xs ! f i"
  1.1019 +  shows "permute_list f xs ! i = xs ! f i"
  1.1020    using permutes_in_image[OF assms(1)] assms(2)
  1.1021    by (simp add: permute_list_def)
  1.1022  
  1.1023 @@ -900,7 +877,7 @@
  1.1024  
  1.1025  lemma permute_list_compose:
  1.1026    assumes "g permutes {..<length xs}"
  1.1027 -  shows   "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
  1.1028 +  shows "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
  1.1029    using assms[THEN permutes_in_image] by (auto simp add: permute_list_def)
  1.1030  
  1.1031  lemma permute_list_ident [simp]: "permute_list (\<lambda>x. x) xs = xs"
  1.1032 @@ -910,44 +887,46 @@
  1.1033    by (simp add: id_def)
  1.1034  
  1.1035  lemma mset_permute_list [simp]:
  1.1036 -  assumes "f permutes {..<length (xs :: 'a list)}"
  1.1037 -  shows   "mset (permute_list f xs) = mset xs"
  1.1038 +  fixes xs :: "'a list"
  1.1039 +  assumes "f permutes {..<length xs}"
  1.1040 +  shows "mset (permute_list f xs) = mset xs"
  1.1041  proof (rule multiset_eqI)
  1.1042    fix y :: 'a
  1.1043    from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x
  1.1044      using permutes_in_image[OF assms] by auto
  1.1045 -  have "count (mset (permute_list f xs)) y =
  1.1046 -          card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
  1.1047 +  have "count (mset (permute_list f xs)) y = card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
  1.1048      by (simp add: permute_list_def count_image_mset atLeast0LessThan)
  1.1049    also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
  1.1050      by auto
  1.1051    also from assms have "card \<dots> = card {i. i < length xs \<and> y = xs ! i}"
  1.1052      by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj)
  1.1053 -  also have "\<dots> = count (mset xs) y" by (simp add: count_mset length_filter_conv_card)
  1.1054 -  finally show "count (mset (permute_list f xs)) y = count (mset xs) y" by simp
  1.1055 +  also have "\<dots> = count (mset xs) y"
  1.1056 +    by (simp add: count_mset length_filter_conv_card)
  1.1057 +  finally show "count (mset (permute_list f xs)) y = count (mset xs) y"
  1.1058 +    by simp
  1.1059  qed
  1.1060  
  1.1061  lemma set_permute_list [simp]:
  1.1062    assumes "f permutes {..<length xs}"
  1.1063 -  shows   "set (permute_list f xs) = set xs"
  1.1064 +  shows "set (permute_list f xs) = set xs"
  1.1065    by (rule mset_eq_setD[OF mset_permute_list]) fact
  1.1066  
  1.1067  lemma distinct_permute_list [simp]:
  1.1068    assumes "f permutes {..<length xs}"
  1.1069 -  shows   "distinct (permute_list f xs) = distinct xs"
  1.1070 +  shows "distinct (permute_list f xs) = distinct xs"
  1.1071    by (simp add: distinct_count_atmost_1 assms)
  1.1072  
  1.1073  lemma permute_list_zip:
  1.1074    assumes "f permutes A" "A = {..<length xs}"
  1.1075    assumes [simp]: "length xs = length ys"
  1.1076 -  shows   "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
  1.1077 +  shows "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
  1.1078  proof -
  1.1079 -  from permutes_in_image[OF assms(1)] assms(2)
  1.1080 -    have [simp]: "f i < length ys \<longleftrightarrow> i < length ys" for i by simp
  1.1081 +  from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i
  1.1082 +    by simp
  1.1083    have "permute_list f (zip xs ys) = map (\<lambda>i. zip xs ys ! f i) [0..<length ys]"
  1.1084      by (simp_all add: permute_list_def zip_map_map)
  1.1085    also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])"
  1.1086 -    by (intro nth_equalityI) simp_all
  1.1087 +    by (intro nth_equalityI) (simp_all add: *)
  1.1088    also have "\<dots> = zip (permute_list f xs) (permute_list f ys)"
  1.1089      by (simp_all add: permute_list_def zip_map_map)
  1.1090    finally show ?thesis .
  1.1091 @@ -955,20 +934,19 @@
  1.1092  
  1.1093  lemma map_of_permute:
  1.1094    assumes "\<sigma> permutes fst ` set xs"
  1.1095 -  shows   "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)" (is "_ = map_of (map ?f _)")
  1.1096 +  shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)"
  1.1097 +    (is "_ = map_of (map ?f _)")
  1.1098  proof
  1.1099 -  fix x
  1.1100 -  from assms have "inj \<sigma>" "surj \<sigma>" by (simp_all add: permutes_inj permutes_surj)
  1.1101 -  thus "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x"
  1.1102 -    by (induction xs) (auto simp: inv_f_f surj_f_inv_f)
  1.1103 +  from assms have "inj \<sigma>" "surj \<sigma>"
  1.1104 +    by (simp_all add: permutes_inj permutes_surj)
  1.1105 +  then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x
  1.1106 +    by (induct xs) (auto simp: inv_f_f surj_f_inv_f)
  1.1107  qed
  1.1108  
  1.1109  
  1.1110  subsection \<open>More lemmas about permutations\<close>
  1.1111  
  1.1112 -text \<open>
  1.1113 -  The following few lemmas were contributed by Lukas Bulwahn.
  1.1114 -\<close>
  1.1115 +text \<open>The following few lemmas were contributed by Lukas Bulwahn.\<close>
  1.1116  
  1.1117  lemma count_image_mset_eq_card_vimage:
  1.1118    assumes "finite A"
  1.1119 @@ -980,19 +958,23 @@
  1.1120  next
  1.1121    case (insert x F)
  1.1122    show ?case
  1.1123 -  proof cases
  1.1124 -    assume "f x = b"
  1.1125 -    from this have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
  1.1126 -      using insert.hyps by auto
  1.1127 -    also have "\<dots> = card (insert x {a \<in> F. f a = f x})"
  1.1128 -      using insert.hyps(1,2) by simp
  1.1129 -    also have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
  1.1130 -      using \<open>f x = b\<close> by (auto intro: arg_cong[where f="card"])
  1.1131 -    finally show ?thesis using insert by auto
  1.1132 +  proof (cases "f x = b")
  1.1133 +    case True
  1.1134 +    with insert.hyps
  1.1135 +    have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
  1.1136 +      by auto
  1.1137 +    also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
  1.1138 +      by simp
  1.1139 +    also from \<open>f x = b\<close> have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
  1.1140 +      by (auto intro: arg_cong[where f="card"])
  1.1141 +    finally show ?thesis
  1.1142 +      using insert by auto
  1.1143    next
  1.1144 -    assume A: "f x \<noteq> b"
  1.1145 -    hence "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}" by auto
  1.1146 -    with insert A show ?thesis by simp
  1.1147 +    case False
  1.1148 +    then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
  1.1149 +      by auto
  1.1150 +    with insert False show ?thesis
  1.1151 +      by simp
  1.1152    qed
  1.1153  qed
  1.1154  
  1.1155 @@ -1000,123 +982,116 @@
  1.1156  lemma image_mset_eq_implies_permutes:
  1.1157    fixes f :: "'a \<Rightarrow> 'b"
  1.1158    assumes "finite A"
  1.1159 -  assumes mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)"
  1.1160 +    and mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)"
  1.1161    obtains p where "p permutes A" and "\<forall>x\<in>A. f x = f' (p x)"
  1.1162  proof -
  1.1163    from \<open>finite A\<close> have [simp]: "finite {a \<in> A. f a = (b::'b)}" for f b by auto
  1.1164    have "f ` A = f' ` A"
  1.1165    proof -
  1.1166 -    have "f ` A = f ` (set_mset (mset_set A))" using \<open>finite A\<close> by simp
  1.1167 -    also have "\<dots> = f' ` (set_mset (mset_set A))"
  1.1168 +    from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))"
  1.1169 +      by simp
  1.1170 +    also have "\<dots> = f' ` set_mset (mset_set A)"
  1.1171        by (metis mset_eq multiset.set_map)
  1.1172 -    also have "\<dots> = f' ` A" using \<open>finite A\<close> by simp
  1.1173 +    also from \<open>finite A\<close> have "\<dots> = f' ` A"
  1.1174 +      by simp
  1.1175      finally show ?thesis .
  1.1176    qed
  1.1177    have "\<forall>b\<in>(f ` A). \<exists>p. bij_betw p {a \<in> A. f a = b} {a \<in> A. f' a = b}"
  1.1178    proof
  1.1179      fix b
  1.1180 -    from mset_eq have
  1.1181 -      "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b" by simp
  1.1182 -    from this  have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
  1.1183 -      using \<open>finite A\<close>
  1.1184 +    from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b"
  1.1185 +      by simp
  1.1186 +    with \<open>finite A\<close> have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
  1.1187        by (simp add: count_image_mset_eq_card_vimage)
  1.1188 -    from this show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
  1.1189 +    then show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
  1.1190        by (intro finite_same_card_bij) simp_all
  1.1191    qed
  1.1192 -  hence "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
  1.1193 +  then have "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
  1.1194      by (rule bchoice)
  1.1195 -  then guess p .. note p = this
  1.1196 +  then obtain p where p: "\<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}" ..
  1.1197    define p' where "p' = (\<lambda>a. if a \<in> A then p (f a) a else a)"
  1.1198    have "p' permutes A"
  1.1199    proof (rule bij_imp_permutes)
  1.1200      have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
  1.1201 -      unfolding disjoint_family_on_def by auto
  1.1202 -    moreover have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if b: "b \<in> f ` A" for b
  1.1203 -      using p b by (subst bij_betw_cong[where g="p b"]) auto
  1.1204 -    ultimately have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
  1.1205 +      by (auto simp: disjoint_family_on_def)
  1.1206 +    moreover
  1.1207 +    have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if "b \<in> f ` A" for b
  1.1208 +      using p that by (subst bij_betw_cong[where g="p b"]) auto
  1.1209 +    ultimately
  1.1210 +    have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
  1.1211        by (rule bij_betw_UNION_disjoint)
  1.1212 -    moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A" by auto
  1.1213 -    moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A" using \<open>f ` A = f' ` A\<close> by auto
  1.1214 +    moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
  1.1215 +      by auto
  1.1216 +    moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
  1.1217 +      by auto
  1.1218      ultimately show "bij_betw p' A A"
  1.1219        unfolding p'_def by (subst bij_betw_cong[where g="(\<lambda>a. p (f a) a)"]) auto
  1.1220    next
  1.1221 -    fix x
  1.1222 -    assume "x \<notin> A"
  1.1223 -    from this show "p' x = x"
  1.1224 -      unfolding p'_def by simp
  1.1225 +    show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x"
  1.1226 +      by (simp add: p'_def)
  1.1227    qed
  1.1228    moreover from p have "\<forall>x\<in>A. f x = f' (p' x)"
  1.1229      unfolding p'_def using bij_betwE by fastforce
  1.1230 -  ultimately show ?thesis by (rule that)
  1.1231 +  ultimately show ?thesis ..
  1.1232  qed
  1.1233  
  1.1234 -lemma mset_set_upto_eq_mset_upto:
  1.1235 -  "mset_set {..<n} = mset [0..<n]"
  1.1236 -  by (induct n) (auto simp add: add.commute lessThan_Suc)
  1.1237 +lemma mset_set_upto_eq_mset_upto: "mset_set {..<n} = mset [0..<n]"
  1.1238 +  by (induct n) (auto simp: add.commute lessThan_Suc)
  1.1239  
  1.1240  (* and derive the existing property: *)
  1.1241  lemma mset_eq_permutation:
  1.1242 -  assumes mset_eq: "mset (xs::'a list) = mset ys"
  1.1243 +  fixes xs ys :: "'a list"
  1.1244 +  assumes mset_eq: "mset xs = mset ys"
  1.1245    obtains p where "p permutes {..<length ys}" "permute_list p ys = xs"
  1.1246  proof -
  1.1247    from mset_eq have length_eq: "length xs = length ys"
  1.1248 -    using mset_eq_length by blast
  1.1249 +    by (rule mset_eq_length)
  1.1250    have "mset_set {..<length ys} = mset [0..<length ys]"
  1.1251 -    using mset_set_upto_eq_mset_upto by blast
  1.1252 -  from mset_eq length_eq this have
  1.1253 -    "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) = image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
  1.1254 +    by (rule mset_set_upto_eq_mset_upto)
  1.1255 +  with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
  1.1256 +    image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
  1.1257      by (metis map_nth mset_map)
  1.1258    from image_mset_eq_implies_permutes[OF _ this]
  1.1259 -    obtain p where "p permutes {..<length ys}"
  1.1260 -    and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)" by auto
  1.1261 -  moreover from this length_eq have "permute_list p ys = xs"
  1.1262 -    by (auto intro!: nth_equalityI simp add: permute_list_nth)
  1.1263 -  ultimately show thesis using that by blast
  1.1264 +  obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
  1.1265 +    by auto
  1.1266 +  with length_eq have "permute_list p ys = xs"
  1.1267 +    by (auto intro!: nth_equalityI simp: permute_list_nth)
  1.1268 +  with p show thesis ..
  1.1269  qed
  1.1270  
  1.1271  lemma permutes_natset_le:
  1.1272    fixes S :: "'a::wellorder set"
  1.1273 -  assumes p: "p permutes S"
  1.1274 -    and le: "\<forall>i \<in> S. p i \<le> i"
  1.1275 +  assumes "p permutes S"
  1.1276 +    and "\<forall>i \<in> S. p i \<le> i"
  1.1277    shows "p = id"
  1.1278  proof -
  1.1279 -  {
  1.1280 -    fix n
  1.1281 -    have "p n = n"
  1.1282 -      using p le
  1.1283 -    proof (induct n arbitrary: S rule: less_induct)
  1.1284 -      fix n S
  1.1285 -      assume H:
  1.1286 -        "\<And>m S. m < n \<Longrightarrow> p permutes S \<Longrightarrow> \<forall>i\<in>S. p i \<le> i \<Longrightarrow> p m = m"
  1.1287 -        "p permutes S" "\<forall>i \<in>S. p i \<le> i"
  1.1288 -      {
  1.1289 -        assume "n \<notin> S"
  1.1290 -        with H(2) have "p n = n"
  1.1291 -          unfolding permutes_def by metis
  1.1292 -      }
  1.1293 -      moreover
  1.1294 -      {
  1.1295 -        assume ns: "n \<in> S"
  1.1296 -        from H(3)  ns have "p n < n \<or> p n = n"
  1.1297 -          by auto
  1.1298 -        moreover {
  1.1299 -          assume h: "p n < n"
  1.1300 -          from H h have "p (p n) = p n"
  1.1301 -            by metis
  1.1302 -          with permutes_inj[OF H(2)] have "p n = n"
  1.1303 -            unfolding inj_def by blast
  1.1304 -          with h have False
  1.1305 -            by simp
  1.1306 -        }
  1.1307 -        ultimately have "p n = n"
  1.1308 -          by blast
  1.1309 -      }
  1.1310 -      ultimately show "p n = n"
  1.1311 -        by blast
  1.1312 +  have "p n = n" for n
  1.1313 +    using assms
  1.1314 +  proof (induct n arbitrary: S rule: less_induct)
  1.1315 +    case (less n)
  1.1316 +    show ?case
  1.1317 +    proof (cases "n \<in> S")
  1.1318 +      case False
  1.1319 +      with less(2) show ?thesis
  1.1320 +        unfolding permutes_def by metis
  1.1321 +    next
  1.1322 +      case True
  1.1323 +      with less(3) have "p n < n \<or> p n = n"
  1.1324 +        by auto
  1.1325 +      then show ?thesis
  1.1326 +      proof
  1.1327 +        assume "p n < n"
  1.1328 +        with less have "p (p n) = p n"
  1.1329 +          by metis
  1.1330 +        with permutes_inj[OF less(2)] have "p n = n"
  1.1331 +          unfolding inj_def by blast
  1.1332 +        with \<open>p n < n\<close> have False
  1.1333 +          by simp
  1.1334 +        then show ?thesis ..
  1.1335 +      qed
  1.1336      qed
  1.1337 -  }
  1.1338 -  then show ?thesis
  1.1339 -    by (auto simp add: fun_eq_iff)
  1.1340 +  qed
  1.1341 +  then show ?thesis by (auto simp: fun_eq_iff)
  1.1342  qed
  1.1343  
  1.1344  lemma permutes_natset_ge:
  1.1345 @@ -1125,25 +1100,23 @@
  1.1346      and le: "\<forall>i \<in> S. p i \<ge> i"
  1.1347    shows "p = id"
  1.1348  proof -
  1.1349 -  {
  1.1350 -    fix i
  1.1351 -    assume i: "i \<in> S"
  1.1352 -    from i permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
  1.1353 +  have "i \<ge> inv p i" if "i \<in> S" for i
  1.1354 +  proof -
  1.1355 +    from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
  1.1356        by simp
  1.1357      with le have "p (inv p i) \<ge> inv p i"
  1.1358        by blast
  1.1359 -    with permutes_inverses[OF p] have "i \<ge> inv p i"
  1.1360 +    with permutes_inverses[OF p] show ?thesis
  1.1361        by simp
  1.1362 -  }
  1.1363 -  then have th: "\<forall>i\<in>S. inv p i \<le> i"
  1.1364 +  qed
  1.1365 +  then have "\<forall>i\<in>S. inv p i \<le> i"
  1.1366      by blast
  1.1367 -  from permutes_natset_le[OF permutes_inv[OF p] th]
  1.1368 -  have "inv p = inv id"
  1.1369 +  from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id"
  1.1370      by simp
  1.1371    then show ?thesis
  1.1372      apply (subst permutes_inv_inv[OF p, symmetric])
  1.1373      apply (rule inv_unique_comp)
  1.1374 -    apply simp_all
  1.1375 +     apply simp_all
  1.1376      done
  1.1377  qed
  1.1378  
  1.1379 @@ -1151,31 +1124,31 @@
  1.1380    apply (rule set_eqI)
  1.1381    apply auto
  1.1382    using permutes_inv_inv permutes_inv
  1.1383 -  apply auto
  1.1384 +   apply auto
  1.1385    apply (rule_tac x="inv x" in exI)
  1.1386    apply auto
  1.1387    done
  1.1388  
  1.1389  lemma image_compose_permutations_left:
  1.1390 -  assumes q: "q permutes S"
  1.1391 -  shows "{q \<circ> p | p. p permutes S} = {p . p permutes S}"
  1.1392 +  assumes "q permutes S"
  1.1393 +  shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
  1.1394    apply (rule set_eqI)
  1.1395    apply auto
  1.1396 -  apply (rule permutes_compose)
  1.1397 -  using q
  1.1398 -  apply auto
  1.1399 +   apply (rule permutes_compose)
  1.1400 +  using assms
  1.1401 +    apply auto
  1.1402    apply (rule_tac x = "inv q \<circ> x" in exI)
  1.1403    apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
  1.1404    done
  1.1405  
  1.1406  lemma image_compose_permutations_right:
  1.1407 -  assumes q: "q permutes S"
  1.1408 +  assumes "q permutes S"
  1.1409    shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
  1.1410    apply (rule set_eqI)
  1.1411    apply auto
  1.1412 -  apply (rule permutes_compose)
  1.1413 -  using q
  1.1414 -  apply auto
  1.1415 +   apply (rule permutes_compose)
  1.1416 +  using assms
  1.1417 +    apply auto
  1.1418    apply (rule_tac x = "x \<circ> inv q" in exI)
  1.1419    apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
  1.1420    done
  1.1421 @@ -1183,12 +1156,11 @@
  1.1422  lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
  1.1423    by (simp add: permutes_def) metis
  1.1424  
  1.1425 -lemma sum_permutations_inverse:
  1.1426 -  "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
  1.1427 +lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
  1.1428    (is "?lhs = ?rhs")
  1.1429  proof -
  1.1430    let ?S = "{p . p permutes S}"
  1.1431 -  have th0: "inj_on inv ?S"
  1.1432 +  have *: "inj_on inv ?S"
  1.1433    proof (auto simp add: inj_on_def)
  1.1434      fix q r
  1.1435      assume q: "q permutes S"
  1.1436 @@ -1199,11 +1171,12 @@
  1.1437      with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
  1.1438        by metis
  1.1439    qed
  1.1440 -  have th1: "inv ` ?S = ?S"
  1.1441 +  have **: "inv ` ?S = ?S"
  1.1442      using image_inverse_permutations by blast
  1.1443 -  have th2: "?rhs = sum (f \<circ> inv) ?S"
  1.1444 +  have ***: "?rhs = sum (f \<circ> inv) ?S"
  1.1445      by (simp add: o_def)
  1.1446 -  from sum.reindex[OF th0, of f] show ?thesis unfolding th1 th2 .
  1.1447 +  from sum.reindex[OF *, of f] show ?thesis
  1.1448 +    by (simp only: ** ***)
  1.1449  qed
  1.1450  
  1.1451  lemma setum_permutations_compose_left:
  1.1452 @@ -1212,9 +1185,9 @@
  1.1453    (is "?lhs = ?rhs")
  1.1454  proof -
  1.1455    let ?S = "{p. p permutes S}"
  1.1456 -  have th0: "?rhs = sum (f \<circ> (op \<circ> q)) ?S"
  1.1457 +  have *: "?rhs = sum (f \<circ> (op \<circ> q)) ?S"
  1.1458      by (simp add: o_def)
  1.1459 -  have th1: "inj_on (op \<circ> q) ?S"
  1.1460 +  have **: "inj_on (op \<circ> q) ?S"
  1.1461    proof (auto simp add: inj_on_def)
  1.1462      fix p r
  1.1463      assume "p permutes S"
  1.1464 @@ -1225,9 +1198,10 @@
  1.1465      with permutes_inj[OF q, unfolded inj_iff] show "p = r"
  1.1466        by simp
  1.1467    qed
  1.1468 -  have th3: "(op \<circ> q) ` ?S = ?S"
  1.1469 +  have "(op \<circ> q) ` ?S = ?S"
  1.1470      using image_compose_permutations_left[OF q] by auto
  1.1471 -  from sum.reindex[OF th1, of f] show ?thesis unfolding th0 th1 th3 .
  1.1472 +  with * sum.reindex[OF **, of f] show ?thesis
  1.1473 +    by (simp only:)
  1.1474  qed
  1.1475  
  1.1476  lemma sum_permutations_compose_right:
  1.1477 @@ -1236,9 +1210,9 @@
  1.1478    (is "?lhs = ?rhs")
  1.1479  proof -
  1.1480    let ?S = "{p. p permutes S}"
  1.1481 -  have th0: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
  1.1482 +  have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
  1.1483      by (simp add: o_def)
  1.1484 -  have th1: "inj_on (\<lambda>p. p \<circ> q) ?S"
  1.1485 +  have **: "inj_on (\<lambda>p. p \<circ> q) ?S"
  1.1486    proof (auto simp add: inj_on_def)
  1.1487      fix p r
  1.1488      assume "p permutes S"
  1.1489 @@ -1249,10 +1223,10 @@
  1.1490      with permutes_surj[OF q, unfolded surj_iff] show "p = r"
  1.1491        by simp
  1.1492    qed
  1.1493 -  have th3: "(\<lambda>p. p \<circ> q) ` ?S = ?S"
  1.1494 -    using image_compose_permutations_right[OF q] by auto
  1.1495 -  from sum.reindex[OF th1, of f]
  1.1496 -  show ?thesis unfolding th0 th1 th3 .
  1.1497 +  from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S"
  1.1498 +    by auto
  1.1499 +  with * sum.reindex[OF **, of f] show ?thesis
  1.1500 +    by (simp only:)
  1.1501  qed
  1.1502  
  1.1503  
  1.1504 @@ -1264,17 +1238,12 @@
  1.1505    shows "sum f {p. p permutes (insert a S)} =
  1.1506      sum (\<lambda>b. sum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
  1.1507  proof -
  1.1508 -  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)"
  1.1509 +  have *: "\<And>f a b. (\<lambda>(b, p). f (Fun.swap a b id \<circ> p)) = f \<circ> (\<lambda>(b,p). Fun.swap a b id \<circ> p)"
  1.1510      by (simp add: fun_eq_iff)
  1.1511 -  have th1: "\<And>P Q. P \<times> Q = {(a,b). a \<in> P \<and> b \<in> Q}"
  1.1512 -    by blast
  1.1513 -  have th2: "\<And>P Q. P \<Longrightarrow> (P \<Longrightarrow> Q) \<Longrightarrow> P \<and> Q"
  1.1514 +  have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
  1.1515      by blast
  1.1516    show ?thesis
  1.1517 -    unfolding permutes_insert
  1.1518 -    unfolding sum.cartesian_product
  1.1519 -    unfolding th1[symmetric]
  1.1520 -    unfolding th0
  1.1521 +    unfolding * ** sum.cartesian_product permutes_insert
  1.1522    proof (rule sum.reindex)
  1.1523      let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
  1.1524      let ?P = "{p. p permutes S}"
  1.1525 @@ -1295,8 +1264,7 @@
  1.1526        from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
  1.1527          (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
  1.1528        then have "p = q"
  1.1529 -        unfolding o_assoc swap_id_idempotent
  1.1530 -        by (simp add: o_def)
  1.1531 +        unfolding o_assoc swap_id_idempotent by simp
  1.1532        with bc have "b = c \<and> p = q"
  1.1533          by blast
  1.1534      }
  1.1535 @@ -1308,48 +1276,53 @@
  1.1536  
  1.1537  subsection \<open>Constructing permutations from association lists\<close>
  1.1538  
  1.1539 -definition list_permutes where
  1.1540 -  "list_permutes xs A \<longleftrightarrow> set (map fst xs) \<subseteq> A \<and> set (map snd xs) = set (map fst xs) \<and>
  1.1541 -     distinct (map fst xs) \<and> distinct (map snd xs)"
  1.1542 +definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
  1.1543 +  where "list_permutes xs A \<longleftrightarrow>
  1.1544 +    set (map fst xs) \<subseteq> A \<and>
  1.1545 +    set (map snd xs) = set (map fst xs) \<and>
  1.1546 +    distinct (map fst xs) \<and>
  1.1547 +    distinct (map snd xs)"
  1.1548  
  1.1549  lemma list_permutesI [simp]:
  1.1550    assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)"
  1.1551 -  shows   "list_permutes xs A"
  1.1552 +  shows "list_permutes xs A"
  1.1553  proof -
  1.1554    from assms(2,3) have "distinct (map snd xs)"
  1.1555      by (intro card_distinct) (simp_all add: distinct_card del: set_map)
  1.1556 -  with assms show ?thesis by (simp add: list_permutes_def)
  1.1557 +  with assms show ?thesis
  1.1558 +    by (simp add: list_permutes_def)
  1.1559  qed
  1.1560  
  1.1561 -definition permutation_of_list where
  1.1562 -  "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)"
  1.1563 +definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
  1.1564 +  where "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)"
  1.1565  
  1.1566  lemma permutation_of_list_Cons:
  1.1567 -  "permutation_of_list ((x,y) # xs) x' = (if x = x' then y else permutation_of_list xs x')"
  1.1568 +  "permutation_of_list ((x, y) # xs) x' = (if x = x' then y else permutation_of_list xs x')"
  1.1569    by (simp add: permutation_of_list_def)
  1.1570  
  1.1571 -fun inverse_permutation_of_list where
  1.1572 -  "inverse_permutation_of_list [] x = x"
  1.1573 -| "inverse_permutation_of_list ((y,x')#xs) x =
  1.1574 -     (if x = x' then y else inverse_permutation_of_list xs x)"
  1.1575 +fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
  1.1576 +  where
  1.1577 +    "inverse_permutation_of_list [] x = x"
  1.1578 +  | "inverse_permutation_of_list ((y, x') # xs) x =
  1.1579 +      (if x = x' then y else inverse_permutation_of_list xs x)"
  1.1580  
  1.1581  declare inverse_permutation_of_list.simps [simp del]
  1.1582  
  1.1583  lemma inj_on_map_of:
  1.1584    assumes "distinct (map snd xs)"
  1.1585 -  shows   "inj_on (map_of xs) (set (map fst xs))"
  1.1586 +  shows "inj_on (map_of xs) (set (map fst xs))"
  1.1587  proof (rule inj_onI)
  1.1588 -  fix x y assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
  1.1589 +  fix x y
  1.1590 +  assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
  1.1591    assume eq: "map_of xs x = map_of xs y"
  1.1592 -  from xy obtain x' y'
  1.1593 -    where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
  1.1594 -    by (cases "map_of xs x"; cases "map_of xs y")
  1.1595 -       (simp_all add: map_of_eq_None_iff)
  1.1596 -  moreover from x'y' have *: "(x,x') \<in> set xs" "(y,y') \<in> set xs"
  1.1597 +  from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
  1.1598 +    by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff)
  1.1599 +  moreover from x'y' have *: "(x, x') \<in> set xs" "(y, y') \<in> set xs"
  1.1600      by (force dest: map_of_SomeD)+
  1.1601 -  moreover from * eq x'y' have "x' = y'" by simp
  1.1602 -  ultimately show "x = y" using assms
  1.1603 -    by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"])
  1.1604 +  moreover from * eq x'y' have "x' = y'"
  1.1605 +    by simp
  1.1606 +  ultimately show "x = y"
  1.1607 +    using assms by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"])
  1.1608  qed
  1.1609  
  1.1610  lemma inj_on_the: "None \<notin> A \<Longrightarrow> inj_on the A"
  1.1611 @@ -1357,13 +1330,13 @@
  1.1612  
  1.1613  lemma inj_on_map_of':
  1.1614    assumes "distinct (map snd xs)"
  1.1615 -  shows   "inj_on (the \<circ> map_of xs) (set (map fst xs))"
  1.1616 +  shows "inj_on (the \<circ> map_of xs) (set (map fst xs))"
  1.1617    by (intro comp_inj_on inj_on_map_of assms inj_on_the)
  1.1618 -     (force simp: eq_commute[of None] map_of_eq_None_iff)
  1.1619 +    (force simp: eq_commute[of None] map_of_eq_None_iff)
  1.1620  
  1.1621  lemma image_map_of:
  1.1622    assumes "distinct (map fst xs)"
  1.1623 -  shows   "map_of xs ` set (map fst xs) = Some ` set (map snd xs)"
  1.1624 +  shows "map_of xs ` set (map fst xs) = Some ` set (map snd xs)"
  1.1625    using assms by (auto simp: rev_image_eqI)
  1.1626  
  1.1627  lemma the_Some_image [simp]: "the ` Some ` A = A"
  1.1628 @@ -1371,12 +1344,13 @@
  1.1629  
  1.1630  lemma image_map_of':
  1.1631    assumes "distinct (map fst xs)"
  1.1632 -  shows   "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)"
  1.1633 +  shows "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)"
  1.1634    by (simp only: image_comp [symmetric] image_map_of assms the_Some_image)
  1.1635  
  1.1636  lemma permutation_of_list_permutes [simp]:
  1.1637    assumes "list_permutes xs A"
  1.1638 -  shows   "permutation_of_list xs permutes A" (is "?f permutes _")
  1.1639 +  shows "permutation_of_list xs permutes A"
  1.1640 +    (is "?f permutes _")
  1.1641  proof (rule permutes_subset[OF bij_imp_permutes])
  1.1642    from assms show "set (map fst xs) \<subseteq> A"
  1.1643      by (simp add: list_permutes_def)
  1.1644 @@ -1384,12 +1358,12 @@
  1.1645      by (intro inj_on_map_of') (simp_all add: list_permutes_def)
  1.1646    also have "?P \<longleftrightarrow> inj_on ?f (set (map fst xs))"
  1.1647      by (intro inj_on_cong)
  1.1648 -       (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
  1.1649 +      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
  1.1650    finally have "bij_betw ?f (set (map fst xs)) (?f ` set (map fst xs))"
  1.1651      by (rule inj_on_imp_bij_betw)
  1.1652    also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)"
  1.1653      by (intro image_cong refl)
  1.1654 -       (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
  1.1655 +      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
  1.1656    also from assms have "\<dots> = set (map fst xs)"
  1.1657      by (subst image_map_of') (simp_all add: list_permutes_def)
  1.1658    finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" .
  1.1659 @@ -1407,52 +1381,47 @@
  1.1660    "x \<noteq> x' \<Longrightarrow> inverse_permutation_of_list ((y',x')#xs) x = inverse_permutation_of_list xs x"
  1.1661    by (simp_all add: inverse_permutation_of_list.simps)
  1.1662  
  1.1663 -lemma permutation_of_list_id:
  1.1664 -  assumes "x \<notin> set (map fst xs)"
  1.1665 -  shows   "permutation_of_list xs x = x"
  1.1666 -  using assms by (induction xs) (auto simp: permutation_of_list_Cons)
  1.1667 +lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x"
  1.1668 +  by (induct xs) (auto simp: permutation_of_list_Cons)
  1.1669  
  1.1670  lemma permutation_of_list_unique':
  1.1671 -  assumes "distinct (map fst xs)" "(x, y) \<in> set xs"
  1.1672 -  shows   "permutation_of_list xs x = y"
  1.1673 -  using assms by (induction xs) (force simp: permutation_of_list_Cons)+
  1.1674 +  "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
  1.1675 +  by (induct xs) (force simp: permutation_of_list_Cons)+
  1.1676  
  1.1677  lemma permutation_of_list_unique:
  1.1678 -  assumes "list_permutes xs A" "(x,y) \<in> set xs"
  1.1679 -  shows   "permutation_of_list xs x = y"
  1.1680 -  using assms by (intro permutation_of_list_unique') (simp_all add: list_permutes_def)
  1.1681 +  "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
  1.1682 +  by (intro permutation_of_list_unique') (simp_all add: list_permutes_def)
  1.1683  
  1.1684  lemma inverse_permutation_of_list_id:
  1.1685 -  assumes "x \<notin> set (map snd xs)"
  1.1686 -  shows   "inverse_permutation_of_list xs x = x"
  1.1687 -  using assms by (induction xs) auto
  1.1688 +  "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x"
  1.1689 +  by (induct xs) auto
  1.1690  
  1.1691  lemma inverse_permutation_of_list_unique':
  1.1692 -  assumes "distinct (map snd xs)" "(x, y) \<in> set xs"
  1.1693 -  shows   "inverse_permutation_of_list xs y = x"
  1.1694 -  using assms by (induction xs) (force simp: inverse_permutation_of_list.simps)+
  1.1695 +  "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
  1.1696 +  by (induct xs) (force simp: inverse_permutation_of_list.simps)+
  1.1697  
  1.1698  lemma inverse_permutation_of_list_unique:
  1.1699 -  assumes "list_permutes xs A" "(x,y) \<in> set xs"
  1.1700 -  shows   "inverse_permutation_of_list xs y = x"
  1.1701 -  using assms by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def)
  1.1702 +  "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
  1.1703 +  by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def)
  1.1704  
  1.1705  lemma inverse_permutation_of_list_correct:
  1.1706 -  assumes "list_permutes xs (A :: 'a set)"
  1.1707 -  shows   "inverse_permutation_of_list xs = inv (permutation_of_list xs)"
  1.1708 +  fixes A :: "'a set"
  1.1709 +  assumes "list_permutes xs A"
  1.1710 +  shows "inverse_permutation_of_list xs = inv (permutation_of_list xs)"
  1.1711  proof (rule ext, rule sym, subst permutes_inv_eq)
  1.1712 -  from assms show "permutation_of_list xs permutes A" by simp
  1.1713 -next
  1.1714 -  fix x
  1.1715 -  show "permutation_of_list xs (inverse_permutation_of_list xs x) = x"
  1.1716 +  from assms show "permutation_of_list xs permutes A"
  1.1717 +    by simp
  1.1718 +  show "permutation_of_list xs (inverse_permutation_of_list xs x) = x" for x
  1.1719    proof (cases "x \<in> set (map snd xs)")
  1.1720      case True
  1.1721 -    then obtain y where "(y, x) \<in> set xs" by force
  1.1722 +    then obtain y where "(y, x) \<in> set xs" by auto
  1.1723      with assms show ?thesis
  1.1724        by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique)
  1.1725 -  qed (insert assms, auto simp: list_permutes_def
  1.1726 -         inverse_permutation_of_list_id permutation_of_list_id)
  1.1727 +  next
  1.1728 +    case False
  1.1729 +    with assms show ?thesis
  1.1730 +      by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id)
  1.1731 +  qed
  1.1732  qed
  1.1733  
  1.1734  end
  1.1735 -