src/HOL/Library/Permutations.thy
 author huffman Thu Jun 11 09:03:24 2009 -0700 (2009-06-11) changeset 31563 ded2364d14d4 parent 30663 0b6aff7451b2 child 32456 341c83339aeb permissions -rw-r--r--
cleaned up some proofs
```     1 (* Title:      Library/Permutations
```
```     2    Author:     Amine Chaieb, University of Cambridge
```
```     3 *)
```
```     4
```
```     5 header {* Permutations, both general and specifically on finite sets.*}
```
```     6
```
```     7 theory Permutations
```
```     8 imports Finite_Cartesian_Product Parity Fact Main
```
```     9 begin
```
```    10
```
```    11   (* Why should I import Main just to solve the Typerep problem! *)
```
```    12
```
```    13 definition permutes (infixr "permutes" 41) where
```
```    14   "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)"
```
```    15
```
```    16 (* ------------------------------------------------------------------------- *)
```
```    17 (* Transpositions.                                                           *)
```
```    18 (* ------------------------------------------------------------------------- *)
```
```    19
```
```    20 declare swap_self[simp]
```
```    21 lemma swapid_sym: "Fun.swap a b id = Fun.swap b a id"
```
```    22   by (auto simp add: expand_fun_eq swap_def fun_upd_def)
```
```    23 lemma swap_id_refl: "Fun.swap a a id = id" by simp
```
```    24 lemma swap_id_sym: "Fun.swap a b id = Fun.swap b a id"
```
```    25   by (rule ext, simp add: swap_def)
```
```    26 lemma swap_id_idempotent[simp]: "Fun.swap a b id o Fun.swap a b id = id"
```
```    27   by (rule ext, auto simp add: swap_def)
```
```    28
```
```    29 lemma inv_unique_comp: assumes fg: "f o g = id" and gf: "g o f = id"
```
```    30   shows "inv f = g"
```
```    31   using fg gf inv_equality[of g f] by (auto simp add: expand_fun_eq)
```
```    32
```
```    33 lemma inverse_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id"
```
```    34   by (rule inv_unique_comp, simp_all)
```
```    35
```
```    36 lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
```
```    37   by (simp add: swap_def)
```
```    38
```
```    39 (* ------------------------------------------------------------------------- *)
```
```    40 (* Basic consequences of the definition.                                     *)
```
```    41 (* ------------------------------------------------------------------------- *)
```
```    42
```
```    43 lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
```
```    44   unfolding permutes_def by metis
```
```    45
```
```    46 lemma permutes_image: assumes pS: "p permutes S" shows "p ` S = S"
```
```    47   using pS
```
```    48   unfolding permutes_def
```
```    49   apply -
```
```    50   apply (rule set_ext)
```
```    51   apply (simp add: image_iff)
```
```    52   apply metis
```
```    53   done
```
```    54
```
```    55 lemma permutes_inj: "p permutes S ==> inj p "
```
```    56   unfolding permutes_def inj_on_def by blast
```
```    57
```
```    58 lemma permutes_surj: "p permutes s ==> surj p"
```
```    59   unfolding permutes_def surj_def by metis
```
```    60
```
```    61 lemma permutes_inv_o: assumes pS: "p permutes S"
```
```    62   shows " p o inv p = id"
```
```    63   and "inv p o p = id"
```
```    64   using permutes_inj[OF pS] permutes_surj[OF pS]
```
```    65   unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+
```
```    66
```
```    67
```
```    68 lemma permutes_inverses:
```
```    69   fixes p :: "'a \<Rightarrow> 'a"
```
```    70   assumes pS: "p permutes S"
```
```    71   shows "p (inv p x) = x"
```
```    72   and "inv p (p x) = x"
```
```    73   using permutes_inv_o[OF pS, unfolded expand_fun_eq o_def] by auto
```
```    74
```
```    75 lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T ==> p permutes T"
```
```    76   unfolding permutes_def by blast
```
```    77
```
```    78 lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
```
```    79   unfolding expand_fun_eq permutes_def apply simp by metis
```
```    80
```
```    81 lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
```
```    82   unfolding expand_fun_eq permutes_def apply simp by metis
```
```    83
```
```    84 lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
```
```    85   unfolding permutes_def by simp
```
```    86
```
```    87 lemma permutes_inv_eq: "p permutes S ==> inv p y = x \<longleftrightarrow> p x = y"
```
```    88   unfolding permutes_def inv_def apply auto
```
```    89   apply (erule allE[where x=y])
```
```    90   apply (erule allE[where x=y])
```
```    91   apply (rule someI_ex) apply blast
```
```    92   apply (rule some1_equality)
```
```    93   apply blast
```
```    94   apply blast
```
```    95   done
```
```    96
```
```    97 lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S ==> Fun.swap a b id permutes S"
```
```    98   unfolding permutes_def swap_def fun_upd_def  apply auto apply metis done
```
```    99
```
```   100 lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T"
```
```   101 apply (simp add: Ball_def permutes_def Diff_iff) by metis
```
```   102
```
```   103 (* ------------------------------------------------------------------------- *)
```
```   104 (* Group properties.                                                         *)
```
```   105 (* ------------------------------------------------------------------------- *)
```
```   106
```
```   107 lemma permutes_id: "id permutes S" unfolding permutes_def by simp
```
```   108
```
```   109 lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S ==> q o p permutes S"
```
```   110   unfolding permutes_def o_def by metis
```
```   111
```
```   112 lemma permutes_inv: assumes pS: "p permutes S" shows "inv p permutes S"
```
```   113   using pS unfolding permutes_def permutes_inv_eq[OF pS] by metis
```
```   114
```
```   115 lemma permutes_inv_inv: assumes pS: "p permutes S" shows "inv (inv p) = p"
```
```   116   unfolding expand_fun_eq permutes_inv_eq[OF pS] permutes_inv_eq[OF permutes_inv[OF pS]]
```
```   117   by blast
```
```   118
```
```   119 (* ------------------------------------------------------------------------- *)
```
```   120 (* The number of permutations on a finite set.                               *)
```
```   121 (* ------------------------------------------------------------------------- *)
```
```   122
```
```   123 lemma permutes_insert_lemma:
```
```   124   assumes pS: "p permutes (insert a S)"
```
```   125   shows "Fun.swap a (p a) id o p permutes S"
```
```   126   apply (rule permutes_superset[where S = "insert a S"])
```
```   127   apply (rule permutes_compose[OF pS])
```
```   128   apply (rule permutes_swap_id, simp)
```
```   129   using permutes_in_image[OF pS, of a] apply simp
```
```   130   apply (auto simp add: Ball_def Diff_iff swap_def)
```
```   131   done
```
```   132
```
```   133 lemma permutes_insert: "{p. p permutes (insert a S)} =
```
```   134         (\<lambda>(b,p). Fun.swap a b id o p) ` {(b,p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
```
```   135 proof-
```
```   136
```
```   137   {fix p
```
```   138     {assume pS: "p permutes insert a S"
```
```   139       let ?b = "p a"
```
```   140       let ?q = "Fun.swap a (p a) id o p"
```
```   141       have th0: "p = Fun.swap a ?b id o ?q" unfolding expand_fun_eq o_assoc by simp
```
```   142       have th1: "?b \<in> insert a S " unfolding permutes_in_image[OF pS] by simp
```
```   143       from permutes_insert_lemma[OF pS] th0 th1
```
```   144       have "\<exists> b q. p = Fun.swap a b id o q \<and> b \<in> insert a S \<and> q permutes S" by blast}
```
```   145     moreover
```
```   146     {fix b q assume bq: "p = Fun.swap a b id o q" "b \<in> insert a S" "q permutes S"
```
```   147       from permutes_subset[OF bq(3), of "insert a S"]
```
```   148       have qS: "q permutes insert a S" by auto
```
```   149       have aS: "a \<in> insert a S" by simp
```
```   150       from bq(1) permutes_compose[OF qS permutes_swap_id[OF aS bq(2)]]
```
```   151       have "p permutes insert a S"  by simp }
```
```   152     ultimately have "p permutes insert a S \<longleftrightarrow> (\<exists> b q. p = Fun.swap a b id o q \<and> b \<in> insert a S \<and> q permutes S)" by blast}
```
```   153   thus ?thesis by auto
```
```   154 qed
```
```   155
```
```   156 lemma hassize_insert: "a \<notin> F \<Longrightarrow> insert a F hassize n \<Longrightarrow> F hassize (n - 1)"
```
```   157   by (auto simp add: hassize_def)
```
```   158
```
```   159 lemma hassize_permutations: assumes Sn: "S hassize n"
```
```   160   shows "{p. p permutes S} hassize (fact n)"
```
```   161 proof-
```
```   162   from Sn have fS:"finite S" by (simp add: hassize_def)
```
```   163
```
```   164   have "\<forall>n. (S hassize n) \<longrightarrow> ({p. p permutes S} hassize (fact n))"
```
```   165   proof(rule finite_induct[where F = S])
```
```   166     from fS show "finite S" .
```
```   167   next
```
```   168     show "\<forall>n. ({} hassize n) \<longrightarrow> ({p. p permutes {}} hassize fact n)"
```
```   169       by (simp add: hassize_def permutes_empty)
```
```   170   next
```
```   171     fix x F
```
```   172     assume fF: "finite F" and xF: "x \<notin> F"
```
```   173       and H: "\<forall>n. (F hassize n) \<longrightarrow> ({p. p permutes F} hassize fact n)"
```
```   174     {fix n assume H0: "insert x F hassize n"
```
```   175       let ?xF = "{p. p permutes insert x F}"
```
```   176       let ?pF = "{p. p permutes F}"
```
```   177       let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
```
```   178       let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
```
```   179       from permutes_insert[of x F]
```
```   180       have xfgpF': "?xF = ?g ` ?pF'" .
```
```   181       from hassize_insert[OF xF H0] have Fs: "F hassize (n - 1)" .
```
```   182       from H Fs have pFs: "?pF hassize fact (n - 1)" by blast
```
```   183       hence pF'f: "finite ?pF'" using H0 unfolding hassize_def
```
```   184 	apply (simp only: Collect_split Collect_mem_eq)
```
```   185 	apply (rule finite_cartesian_product)
```
```   186 	apply simp_all
```
```   187 	done
```
```   188
```
```   189       have ginj: "inj_on ?g ?pF'"
```
```   190       proof-
```
```   191 	{
```
```   192 	  fix b p c q assume bp: "(b,p) \<in> ?pF'" and cq: "(c,q) \<in> ?pF'"
```
```   193 	    and eq: "?g (b,p) = ?g (c,q)"
```
```   194 	  from bp cq have ths: "b \<in> insert x F" "c \<in> insert x F" "x \<in> insert x F" "p permutes F" "q permutes F" by auto
```
```   195 	  from ths(4) xF eq have "b = ?g (b,p) x" unfolding permutes_def
```
```   196 	    by (auto simp add: swap_def fun_upd_def expand_fun_eq)
```
```   197 	  also have "\<dots> = ?g (c,q) x" using ths(5) xF eq
```
```   198 	    by (auto simp add: swap_def fun_upd_def expand_fun_eq)
```
```   199 	  also have "\<dots> = c"using ths(5) xF unfolding permutes_def
```
```   200 	    by (auto simp add: swap_def fun_upd_def expand_fun_eq)
```
```   201 	  finally have bc: "b = c" .
```
```   202 	  hence "Fun.swap x b id = Fun.swap x c id" by simp
```
```   203 	  with eq have "Fun.swap x b id o p = Fun.swap x b id o q" by simp
```
```   204 	  hence "Fun.swap x b id o (Fun.swap x b id o p) = Fun.swap x b id o (Fun.swap x b id o q)" by simp
```
```   205 	  hence "p = q" by (simp add: o_assoc)
```
```   206 	  with bc have "(b,p) = (c,q)" by simp }
```
```   207 	thus ?thesis  unfolding inj_on_def by blast
```
```   208       qed
```
```   209       from xF H0 have n0: "n \<noteq> 0 " by (auto simp add: hassize_def)
```
```   210       hence "\<exists>m. n = Suc m" by arith
```
```   211       then obtain m where n[simp]: "n = Suc m" by blast
```
```   212       from pFs H0 have xFc: "card ?xF = fact n"
```
```   213 	unfolding xfgpF' card_image[OF ginj] hassize_def
```
```   214 	apply (simp only: Collect_split Collect_mem_eq card_cartesian_product)
```
```   215 	by simp
```
```   216       from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF" unfolding xfgpF' by simp
```
```   217       have "?xF hassize fact n"
```
```   218 	using xFf xFc
```
```   219 	unfolding hassize_def  xFf by blast }
```
```   220     thus "\<forall>n. (insert x F hassize n) \<longrightarrow> ({p. p permutes insert x F} hassize fact n)"
```
```   221       by blast
```
```   222   qed
```
```   223   with Sn show ?thesis by blast
```
```   224 qed
```
```   225
```
```   226 lemma finite_permutations: "finite S ==> finite {p. p permutes S}"
```
```   227   using hassize_permutations[of S] unfolding hassize_def by blast
```
```   228
```
```   229 (* ------------------------------------------------------------------------- *)
```
```   230 (* Permutations of index set for iterated operations.                        *)
```
```   231 (* ------------------------------------------------------------------------- *)
```
```   232
```
```   233 lemma (in ab_semigroup_mult) fold_image_permute: assumes fS: "finite S" and pS: "p permutes S"
```
```   234   shows "fold_image times f z S = fold_image times (f o p) z S"
```
```   235   using fold_image_reindex[OF fS subset_inj_on[OF permutes_inj[OF pS], of S, simplified], of f z]
```
```   236   unfolding permutes_image[OF pS] .
```
```   237 lemma (in ab_semigroup_add) fold_image_permute: assumes fS: "finite S" and pS: "p permutes S"
```
```   238   shows "fold_image plus f z S = fold_image plus (f o p) z S"
```
```   239 proof-
```
```   240   interpret ab_semigroup_mult plus apply unfold_locales apply (simp add: add_assoc)
```
```   241     apply (simp add: add_commute) done
```
```   242   from fold_image_reindex[OF fS subset_inj_on[OF permutes_inj[OF pS], of S, simplified], of f z]
```
```   243   show ?thesis
```
```   244   unfolding permutes_image[OF pS] .
```
```   245 qed
```
```   246
```
```   247 lemma setsum_permute: assumes pS: "p permutes S"
```
```   248   shows "setsum f S = setsum (f o p) S"
```
```   249   unfolding setsum_def using fold_image_permute[of S p f 0] pS by clarsimp
```
```   250
```
```   251 lemma setsum_permute_natseg:assumes pS: "p permutes {m .. n}"
```
```   252   shows "setsum f {m .. n} = setsum (f o p) {m .. n}"
```
```   253   using setsum_permute[OF pS, of f ] pS by blast
```
```   254
```
```   255 lemma setprod_permute: assumes pS: "p permutes S"
```
```   256   shows "setprod f S = setprod (f o p) S"
```
```   257   unfolding setprod_def
```
```   258   using ab_semigroup_mult_class.fold_image_permute[of S p f 1] pS by clarsimp
```
```   259
```
```   260 lemma setprod_permute_natseg:assumes pS: "p permutes {m .. n}"
```
```   261   shows "setprod f {m .. n} = setprod (f o p) {m .. n}"
```
```   262   using setprod_permute[OF pS, of f ] pS by blast
```
```   263
```
```   264 (* ------------------------------------------------------------------------- *)
```
```   265 (* Various combinations of transpositions with 2, 1 and 0 common elements.   *)
```
```   266 (* ------------------------------------------------------------------------- *)
```
```   267
```
```   268 lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>  Fun.swap a b id o Fun.swap a c id = Fun.swap b c id o Fun.swap a b id" by (simp add: expand_fun_eq swap_def)
```
```   269
```
```   270 lemma swap_id_common': "~(a = b) \<Longrightarrow> ~(a = c) \<Longrightarrow> Fun.swap a c id o Fun.swap b c id = Fun.swap b c id o Fun.swap a b id" by (simp add: expand_fun_eq swap_def)
```
```   271
```
```   272 lemma swap_id_independent: "~(a = c) \<Longrightarrow> ~(a = d) \<Longrightarrow> ~(b = c) \<Longrightarrow> ~(b = d) ==> Fun.swap a b id o Fun.swap c d id = Fun.swap c d id o Fun.swap a b id"
```
```   273   by (simp add: swap_def expand_fun_eq)
```
```   274
```
```   275 (* ------------------------------------------------------------------------- *)
```
```   276 (* Permutations as transposition sequences.                                  *)
```
```   277 (* ------------------------------------------------------------------------- *)
```
```   278
```
```   279
```
```   280 inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool" where
```
```   281   id[simp]: "swapidseq 0 id"
```
```   282 | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id o p)"
```
```   283
```
```   284 declare id[unfolded id_def, simp]
```
```   285 definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
```
```   286
```
```   287 (* ------------------------------------------------------------------------- *)
```
```   288 (* Some closure properties of the set of permutations, with lengths.         *)
```
```   289 (* ------------------------------------------------------------------------- *)
```
```   290
```
```   291 lemma permutation_id[simp]: "permutation id"unfolding permutation_def
```
```   292   by (rule exI[where x=0], simp)
```
```   293 declare permutation_id[unfolded id_def, simp]
```
```   294
```
```   295 lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
```
```   296   apply clarsimp
```
```   297   using comp_Suc[of 0 id a b] by simp
```
```   298
```
```   299 lemma permutation_swap_id: "permutation (Fun.swap a b id)"
```
```   300   apply (cases "a=b", simp_all)
```
```   301   unfolding permutation_def using swapidseq_swap[of a b] by blast
```
```   302
```
```   303 lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q ==> swapidseq (n + m) (p o q)"
```
```   304   proof (induct n p arbitrary: m q rule: swapidseq.induct)
```
```   305     case (id m q) thus ?case by simp
```
```   306   next
```
```   307     case (comp_Suc n p a b m q)
```
```   308     have th: "Suc n + m = Suc (n + m)" by arith
```
```   309     show ?case unfolding th o_assoc[symmetric]
```
```   310       apply (rule swapidseq.comp_Suc) using comp_Suc.hyps(2)[OF comp_Suc.prems]  comp_Suc.hyps(3) by blast+
```
```   311 qed
```
```   312
```
```   313 lemma permutation_compose: "permutation p \<Longrightarrow> permutation q ==> permutation(p o q)"
```
```   314   unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
```
```   315
```
```   316 lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b ==> swapidseq (Suc n) (p o Fun.swap a b id)"
```
```   317   apply (induct n p rule: swapidseq.induct)
```
```   318   using swapidseq_swap[of a b]
```
```   319   by (auto simp add: o_assoc[symmetric] intro: swapidseq.comp_Suc)
```
```   320
```
```   321 lemma swapidseq_inverse_exists: "swapidseq n p ==> \<exists>q. swapidseq n q \<and> p o q = id \<and> q o p = id"
```
```   322 proof(induct n p rule: swapidseq.induct)
```
```   323   case id  thus ?case by (rule exI[where x=id], simp)
```
```   324 next
```
```   325   case (comp_Suc n p a b)
```
```   326   from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" by blast
```
```   327   let ?q = "q o Fun.swap a b id"
```
```   328   note H = comp_Suc.hyps
```
```   329   from swapidseq_swap[of a b] H(3)  have th0: "swapidseq 1 (Fun.swap a b id)" by simp
```
```   330   from swapidseq_comp_add[OF q(1) th0] have th1:"swapidseq (Suc n) ?q" by simp
```
```   331   have "Fun.swap a b id o p o ?q = Fun.swap a b id o (p o q) o Fun.swap a b id" by (simp add: o_assoc)
```
```   332   also have "\<dots> = id" by (simp add: q(2))
```
```   333   finally have th2: "Fun.swap a b id o p o ?q = id" .
```
```   334   have "?q \<circ> (Fun.swap a b id \<circ> p) = q \<circ> (Fun.swap a b id o Fun.swap a b id) \<circ> p" by (simp only: o_assoc)
```
```   335   hence "?q \<circ> (Fun.swap a b id \<circ> p) = id" by (simp add: q(3))
```
```   336   with th1 th2 show ?case by blast
```
```   337 qed
```
```   338
```
```   339
```
```   340 lemma swapidseq_inverse: assumes H: "swapidseq n p" shows "swapidseq n (inv p)"
```
```   341   using swapidseq_inverse_exists[OF H] inv_unique_comp[of p] by auto
```
```   342
```
```   343 lemma permutation_inverse: "permutation p ==> permutation (inv p)"
```
```   344   using permutation_def swapidseq_inverse by blast
```
```   345
```
```   346 (* ------------------------------------------------------------------------- *)
```
```   347 (* The identity map only has even transposition sequences.                   *)
```
```   348 (* ------------------------------------------------------------------------- *)
```
```   349
```
```   350 lemma symmetry_lemma:"(\<And>a b c d. P a b c d ==> P a b d c) \<Longrightarrow>
```
```   351    (\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> (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) ==> P a b c d)
```
```   352    ==> (\<And>a b c d. a \<noteq> b --> c \<noteq> d \<longrightarrow>  P a b c d)" by metis
```
```   353
```
```   354 lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow> Fun.swap a b id o Fun.swap c d id = id \<or>
```
```   355   (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> Fun.swap a b id o Fun.swap c d id = Fun.swap x y id o Fun.swap a z id)"
```
```   356 proof-
```
```   357   assume H: "a\<noteq>b" "c\<noteq>d"
```
```   358 have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
```
```   359 (  Fun.swap a b id o Fun.swap c d id = id \<or>
```
```   360   (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and> Fun.swap a b id o Fun.swap c d id = Fun.swap x y id o Fun.swap a z id))"
```
```   361   apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
```
```   362   apply (simp_all only: swapid_sym)
```
```   363   apply (case_tac "a = c \<and> b = d", clarsimp simp only: swapid_sym swap_id_idempotent)
```
```   364   apply (case_tac "a = c \<and> b \<noteq> d")
```
```   365   apply (rule disjI2)
```
```   366   apply (rule_tac x="b" in exI)
```
```   367   apply (rule_tac x="d" in exI)
```
```   368   apply (rule_tac x="b" in exI)
```
```   369   apply (clarsimp simp add: expand_fun_eq swap_def)
```
```   370   apply (case_tac "a \<noteq> c \<and> b = d")
```
```   371   apply (rule disjI2)
```
```   372   apply (rule_tac x="c" in exI)
```
```   373   apply (rule_tac x="d" in exI)
```
```   374   apply (rule_tac x="c" in exI)
```
```   375   apply (clarsimp simp add: expand_fun_eq swap_def)
```
```   376   apply (rule disjI2)
```
```   377   apply (rule_tac x="c" in exI)
```
```   378   apply (rule_tac x="d" in exI)
```
```   379   apply (rule_tac x="b" in exI)
```
```   380   apply (clarsimp simp add: expand_fun_eq swap_def)
```
```   381   done
```
```   382 with H show ?thesis by metis
```
```   383 qed
```
```   384
```
```   385 lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
```
```   386   using swapidseq.cases[of 0 p "p = id"]
```
```   387   by auto
```
```   388
```
```   389 lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow> (n=0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = Fun.swap a b id o q \<and> swapidseq m q \<and> a\<noteq> b))"
```
```   390   apply (rule iffI)
```
```   391   apply (erule swapidseq.cases[of n p])
```
```   392   apply simp
```
```   393   apply (rule disjI2)
```
```   394   apply (rule_tac x= "a" in exI)
```
```   395   apply (rule_tac x= "b" in exI)
```
```   396   apply (rule_tac x= "pa" in exI)
```
```   397   apply (rule_tac x= "na" in exI)
```
```   398   apply simp
```
```   399   apply auto
```
```   400   apply (rule comp_Suc, simp_all)
```
```   401   done
```
```   402 lemma fixing_swapidseq_decrease:
```
```   403   assumes spn: "swapidseq n p" and ab: "a\<noteq>b" and pa: "(Fun.swap a b id o p) a = a"
```
```   404   shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id o p)"
```
```   405   using spn ab pa
```
```   406 proof(induct n arbitrary: p a b)
```
```   407   case 0 thus ?case by (auto simp add: swap_def fun_upd_def)
```
```   408 next
```
```   409   case (Suc n p a b)
```
```   410   from Suc.prems(1) swapidseq_cases[of "Suc n" p] obtain
```
```   411     c d q m where cdqm: "Suc n = Suc m" "p = Fun.swap c d id o q" "swapidseq m q" "c \<noteq> d" "n = m"
```
```   412     by auto
```
```   413   {assume H: "Fun.swap a b id o Fun.swap c d id = id"
```
```   414
```
```   415     have ?case apply (simp only: cdqm o_assoc H)
```
```   416       by (simp add: cdqm)}
```
```   417   moreover
```
```   418   { fix x y z
```
```   419     assume H: "x\<noteq>a" "y\<noteq>a" "z \<noteq>a" "x \<noteq>y"
```
```   420       "Fun.swap a b id o Fun.swap c d id = Fun.swap x y id o Fun.swap a z id"
```
```   421     from H have az: "a \<noteq> z" by simp
```
```   422
```
```   423     {fix h have "(Fun.swap x y id o h) a = a \<longleftrightarrow> h a = a"
```
```   424       using H by (simp add: swap_def)}
```
```   425     note th3 = this
```
```   426     from cdqm(2) have "Fun.swap a b id o p = Fun.swap a b id o (Fun.swap c d id o q)" by simp
```
```   427     hence "Fun.swap a b id o p = Fun.swap x y id o (Fun.swap a z id o q)" by (simp add: o_assoc H)
```
```   428     hence "(Fun.swap a b id o p) a = (Fun.swap x y id o (Fun.swap a z id o q)) a" by simp
```
```   429     hence "(Fun.swap x y id o (Fun.swap a z id o q)) a  = a" unfolding Suc by metis
```
```   430     hence th1: "(Fun.swap a z id o q) a = a" unfolding th3 .
```
```   431     from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az th1]
```
```   432     have th2: "swapidseq (n - 1) (Fun.swap a z id o q)" "n \<noteq> 0" by blast+
```
```   433     have th: "Suc n - 1 = Suc (n - 1)" using th2(2) by auto
```
```   434     have ?case unfolding cdqm(2) H o_assoc th
```
```   435       apply (simp only: Suc_not_Zero simp_thms o_assoc[symmetric])
```
```   436       apply (rule comp_Suc)
```
```   437       using th2 H apply blast+
```
```   438       done}
```
```   439   ultimately show ?case using swap_general[OF Suc.prems(2) cdqm(4)] by metis
```
```   440 qed
```
```   441
```
```   442 lemma swapidseq_identity_even:
```
```   443   assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)" shows "even n"
```
```   444   using `swapidseq n id`
```
```   445 proof(induct n rule: nat_less_induct)
```
```   446   fix n
```
```   447   assume H: "\<forall>m<n. swapidseq m (id::'a \<Rightarrow> 'a) \<longrightarrow> even m" "swapidseq n (id :: 'a \<Rightarrow> 'a)"
```
```   448   {assume "n = 0" hence "even n" by arith}
```
```   449   moreover
```
```   450   {fix a b :: 'a and q m
```
```   451     assume h: "n = Suc m" "(id :: 'a \<Rightarrow> 'a) = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
```
```   452     from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
```
```   453     have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)" by auto
```
```   454     from h m have mn: "m - 1 < n" by arith
```
```   455     from H(1)[rule_format, OF mn m(2)] h(1) m(1) have "even n" apply arith done}
```
```   456   ultimately show "even n" using H(2)[unfolded swapidseq_cases[of n id]] by auto
```
```   457 qed
```
```   458
```
```   459 (* ------------------------------------------------------------------------- *)
```
```   460 (* Therefore we have a welldefined notion of parity.                         *)
```
```   461 (* ------------------------------------------------------------------------- *)
```
```   462
```
```   463 definition "evenperm p = even (SOME n. swapidseq n p)"
```
```   464
```
```   465 lemma swapidseq_even_even: assumes
```
```   466   m: "swapidseq m p" and n: "swapidseq n p"
```
```   467   shows "even m \<longleftrightarrow> even n"
```
```   468 proof-
```
```   469   from swapidseq_inverse_exists[OF n]
```
```   470   obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" by blast
```
```   471
```
```   472   from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]]
```
```   473   show ?thesis by arith
```
```   474 qed
```
```   475
```
```   476 lemma evenperm_unique: assumes p: "swapidseq n p" and n:"even n = b"
```
```   477   shows "evenperm p = b"
```
```   478   unfolding n[symmetric] evenperm_def
```
```   479   apply (rule swapidseq_even_even[where p = p])
```
```   480   apply (rule someI[where x = n])
```
```   481   using p by blast+
```
```   482
```
```   483 (* ------------------------------------------------------------------------- *)
```
```   484 (* And it has the expected composition properties.                           *)
```
```   485 (* ------------------------------------------------------------------------- *)
```
```   486
```
```   487 lemma evenperm_id[simp]: "evenperm id = True"
```
```   488   apply (rule evenperm_unique[where n = 0]) by simp_all
```
```   489
```
```   490 lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
```
```   491 apply (rule evenperm_unique[where n="if a = b then 0 else 1"])
```
```   492 by (simp_all add: swapidseq_swap)
```
```   493
```
```   494 lemma evenperm_comp:
```
```   495   assumes p: "permutation p" and q:"permutation q"
```
```   496   shows "evenperm (p o q) = (evenperm p = evenperm q)"
```
```   497 proof-
```
```   498   from p q obtain
```
```   499     n m where n: "swapidseq n p" and m: "swapidseq m q"
```
```   500     unfolding permutation_def by blast
```
```   501   note nm =  swapidseq_comp_add[OF n m]
```
```   502   have th: "even (n + m) = (even n \<longleftrightarrow> even m)" by arith
```
```   503   from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
```
```   504     evenperm_unique[OF nm th]
```
```   505   show ?thesis by blast
```
```   506 qed
```
```   507
```
```   508 lemma evenperm_inv: assumes p: "permutation p"
```
```   509   shows "evenperm (inv p) = evenperm p"
```
```   510 proof-
```
```   511   from p obtain n where n: "swapidseq n p" unfolding permutation_def by blast
```
```   512   from evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]]
```
```   513   show ?thesis .
```
```   514 qed
```
```   515
```
```   516 (* ------------------------------------------------------------------------- *)
```
```   517 (* A more abstract characterization of permutations.                         *)
```
```   518 (* ------------------------------------------------------------------------- *)
```
```   519
```
```   520
```
```   521 lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
```
```   522   unfolding bij_def inj_on_def surj_def
```
```   523   apply auto
```
```   524   apply metis
```
```   525   apply metis
```
```   526   done
```
```   527
```
```   528 lemma permutation_bijective:
```
```   529   assumes p: "permutation p"
```
```   530   shows "bij p"
```
```   531 proof-
```
```   532   from p obtain n where n: "swapidseq n p" unfolding permutation_def by blast
```
```   533   from swapidseq_inverse_exists[OF n] obtain q where
```
```   534     q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id" by blast
```
```   535   thus ?thesis unfolding bij_iff  apply (auto simp add: expand_fun_eq) apply metis done
```
```   536 qed
```
```   537
```
```   538 lemma permutation_finite_support: assumes p: "permutation p"
```
```   539   shows "finite {x. p x \<noteq> x}"
```
```   540 proof-
```
```   541   from p obtain n where n: "swapidseq n p" unfolding permutation_def by blast
```
```   542   from n show ?thesis
```
```   543   proof(induct n p rule: swapidseq.induct)
```
```   544     case id thus ?case by simp
```
```   545   next
```
```   546     case (comp_Suc n p a b)
```
```   547     let ?S = "insert a (insert b {x. p x \<noteq> x})"
```
```   548     from comp_Suc.hyps(2) have fS: "finite ?S" by simp
```
```   549     from `a \<noteq> b` have th: "{x. (Fun.swap a b id o p) x \<noteq> x} \<subseteq> ?S"
```
```   550       by (auto simp add: swap_def)
```
```   551     from finite_subset[OF th fS] show ?case  .
```
```   552 qed
```
```   553 qed
```
```   554
```
```   555 lemma bij_inv_eq_iff: "bij p ==> x = inv p y \<longleftrightarrow> p x = y"
```
```   556   using surj_f_inv_f[of p] inv_f_f[of f] by (auto simp add: bij_def)
```
```   557
```
```   558 lemma bij_swap_comp:
```
```   559   assumes bp: "bij p" shows "Fun.swap a b id o p = Fun.swap (inv p a) (inv p b) p"
```
```   560   using surj_f_inv_f[OF bij_is_surj[OF bp]]
```
```   561   by (simp add: expand_fun_eq swap_def bij_inv_eq_iff[OF bp])
```
```   562
```
```   563 lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id o p)"
```
```   564 proof-
```
```   565   assume H: "bij p"
```
```   566   show ?thesis
```
```   567     unfolding bij_swap_comp[OF H] bij_swap_iff
```
```   568     using H .
```
```   569 qed
```
```   570
```
```   571 lemma permutation_lemma:
```
```   572   assumes fS: "finite S" and p: "bij p" and pS: "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
```
```   573   shows "permutation p"
```
```   574 using fS p pS
```
```   575 proof(induct S arbitrary: p rule: finite_induct)
```
```   576   case (empty p) thus ?case by simp
```
```   577 next
```
```   578   case (insert a F p)
```
```   579   let ?r = "Fun.swap a (p a) id o p"
```
```   580   let ?q = "Fun.swap a (p a) id o ?r "
```
```   581   have raa: "?r a = a" by (simp add: swap_def)
```
```   582   from bij_swap_ompose_bij[OF insert(4)]
```
```   583   have br: "bij ?r"  .
```
```   584
```
```   585   from insert raa have th: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
```
```   586     apply (clarsimp simp add: swap_def)
```
```   587     apply (erule_tac x="x" in allE)
```
```   588     apply auto
```
```   589     unfolding bij_iff apply metis
```
```   590     done
```
```   591   from insert(3)[OF br th]
```
```   592   have rp: "permutation ?r" .
```
```   593   have "permutation ?q" by (simp add: permutation_compose permutation_swap_id rp)
```
```   594   thus ?case by (simp add: o_assoc)
```
```   595 qed
```
```   596
```
```   597 lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
```
```   598   (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
```
```   599 proof
```
```   600   assume p: ?lhs
```
```   601   from p permutation_bijective permutation_finite_support show "?b \<and> ?f" by auto
```
```   602 next
```
```   603   assume bf: "?b \<and> ?f"
```
```   604   hence bf: "?f" "?b" by blast+
```
```   605   from permutation_lemma[OF bf] show ?lhs by blast
```
```   606 qed
```
```   607
```
```   608 lemma permutation_inverse_works: assumes p: "permutation p"
```
```   609   shows "inv p o p = id" "p o inv p = id"
```
```   610 using permutation_bijective[OF p] surj_iff bij_def inj_iff by auto
```
```   611
```
```   612 lemma permutation_inverse_compose:
```
```   613   assumes p: "permutation p" and q: "permutation q"
```
```   614   shows "inv (p o q) = inv q o inv p"
```
```   615 proof-
```
```   616   note ps = permutation_inverse_works[OF p]
```
```   617   note qs = permutation_inverse_works[OF q]
```
```   618   have "p o q o (inv q o inv p) = p o (q o inv q) o inv p" by (simp add: o_assoc)
```
```   619   also have "\<dots> = id" by (simp add: ps qs)
```
```   620   finally have th0: "p o q o (inv q o inv p) = id" .
```
```   621   have "inv q o inv p o (p o q) = inv q o (inv p o p) o q" by (simp add: o_assoc)
```
```   622   also have "\<dots> = id" by (simp add: ps qs)
```
```   623   finally have th1: "inv q o inv p o (p o q) = id" .
```
```   624   from inv_unique_comp[OF th0 th1] show ?thesis .
```
```   625 qed
```
```   626
```
```   627 (* ------------------------------------------------------------------------- *)
```
```   628 (* Relation to "permutes".                                                   *)
```
```   629 (* ------------------------------------------------------------------------- *)
```
```   630
```
```   631 lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
```
```   632 unfolding permutation permutes_def bij_iff[symmetric]
```
```   633 apply (rule iffI, clarify)
```
```   634 apply (rule exI[where x="{x. p x \<noteq> x}"])
```
```   635 apply simp
```
```   636 apply clarsimp
```
```   637 apply (rule_tac B="S" in finite_subset)
```
```   638 apply auto
```
```   639 done
```
```   640
```
```   641 (* ------------------------------------------------------------------------- *)
```
```   642 (* Hence a sort of induction principle composing by swaps.                   *)
```
```   643 (* ------------------------------------------------------------------------- *)
```
```   644
```
```   645 lemma permutes_induct: "finite S \<Longrightarrow>  P id  \<Longrightarrow> (\<And> a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> P p \<Longrightarrow> P p \<Longrightarrow> permutation p ==> P (Fun.swap a b id o p))
```
```   646          ==> (\<And>p. p permutes S ==> P p)"
```
```   647 proof(induct S rule: finite_induct)
```
```   648   case empty thus ?case by auto
```
```   649 next
```
```   650   case (insert x F p)
```
```   651   let ?r = "Fun.swap x (p x) id o p"
```
```   652   let ?q = "Fun.swap x (p x) id o ?r"
```
```   653   have qp: "?q = p" by (simp add: o_assoc)
```
```   654   from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r" by blast
```
```   655   from permutes_in_image[OF insert.prems(3), of x]
```
```   656   have pxF: "p x \<in> insert x F" by simp
```
```   657   have xF: "x \<in> insert x F" by simp
```
```   658   have rp: "permutation ?r"
```
```   659     unfolding permutation_permutes using insert.hyps(1)
```
```   660       permutes_insert_lemma[OF insert.prems(3)] by blast
```
```   661   from insert.prems(2)[OF xF pxF Pr Pr rp]
```
```   662   show ?case  unfolding qp .
```
```   663 qed
```
```   664
```
```   665 (* ------------------------------------------------------------------------- *)
```
```   666 (* Sign of a permutation as a real number.                                   *)
```
```   667 (* ------------------------------------------------------------------------- *)
```
```   668
```
```   669 definition "sign p = (if evenperm p then (1::int) else -1)"
```
```   670
```
```   671 lemma sign_nz: "sign p \<noteq> 0" by (simp add: sign_def)
```
```   672 lemma sign_id: "sign id = 1" by (simp add: sign_def)
```
```   673 lemma sign_inverse: "permutation p ==> sign (inv p) = sign p"
```
```   674   by (simp add: sign_def evenperm_inv)
```
```   675 lemma sign_compose: "permutation p \<Longrightarrow> permutation q ==> sign (p o q) = sign(p) * sign(q)" by (simp add: sign_def evenperm_comp)
```
```   676 lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
```
```   677   by (simp add: sign_def evenperm_swap)
```
```   678 lemma sign_idempotent: "sign p * sign p = 1" by (simp add: sign_def)
```
```   679
```
```   680 (* ------------------------------------------------------------------------- *)
```
```   681 (* More lemmas about permutations.                                           *)
```
```   682 (* ------------------------------------------------------------------------- *)
```
```   683
```
```   684 lemma permutes_natset_le:
```
```   685   assumes p: "p permutes (S::'a::wellorder set)" and le: "\<forall>i \<in> S.  p i <= i" shows "p = id"
```
```   686 proof-
```
```   687   {fix n
```
```   688     have "p n = n"
```
```   689       using p le
```
```   690     proof(induct n arbitrary: S rule: less_induct)
```
```   691       fix n S assume H: "\<And>m S. \<lbrakk>m < n; p permutes S; \<forall>i\<in>S. p i \<le> i\<rbrakk> \<Longrightarrow> p m = m"
```
```   692 	"p permutes S" "\<forall>i \<in>S. p i \<le> i"
```
```   693       {assume "n \<notin> S"
```
```   694 	with H(2) have "p n = n" unfolding permutes_def by metis}
```
```   695       moreover
```
```   696       {assume ns: "n \<in> S"
```
```   697 	from H(3)  ns have "p n < n \<or> p n = n" by auto
```
```   698 	moreover{assume h: "p n < n"
```
```   699 	  from H h have "p (p n) = p n" by metis
```
```   700 	  with permutes_inj[OF H(2)] have "p n = n" unfolding inj_on_def by blast
```
```   701 	  with h have False by simp}
```
```   702 	ultimately have "p n = n" by blast }
```
```   703       ultimately show "p n = n"  by blast
```
```   704     qed}
```
```   705   thus ?thesis by (auto simp add: expand_fun_eq)
```
```   706 qed
```
```   707
```
```   708 lemma permutes_natset_ge:
```
```   709   assumes p: "p permutes (S::'a::wellorder set)" and le: "\<forall>i \<in> S.  p i \<ge> i" shows "p = id"
```
```   710 proof-
```
```   711   {fix i assume i: "i \<in> S"
```
```   712     from i permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S" by simp
```
```   713     with le have "p (inv p i) \<ge> inv p i" by blast
```
```   714     with permutes_inverses[OF p] have "i \<ge> inv p i" by simp}
```
```   715   then have th: "\<forall>i\<in>S. inv p i \<le> i"  by blast
```
```   716   from permutes_natset_le[OF permutes_inv[OF p] th]
```
```   717   have "inv p = inv id" by simp
```
```   718   then show ?thesis
```
```   719     apply (subst permutes_inv_inv[OF p, symmetric])
```
```   720     apply (rule inv_unique_comp)
```
```   721     apply simp_all
```
```   722     done
```
```   723 qed
```
```   724
```
```   725 lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
```
```   726 apply (rule set_ext)
```
```   727 apply auto
```
```   728   using permutes_inv_inv permutes_inv apply auto
```
```   729   apply (rule_tac x="inv x" in exI)
```
```   730   apply auto
```
```   731   done
```
```   732
```
```   733 lemma image_compose_permutations_left:
```
```   734   assumes q: "q permutes S" shows "{q o p | p. p permutes S} = {p . p permutes S}"
```
```   735 apply (rule set_ext)
```
```   736 apply auto
```
```   737 apply (rule permutes_compose)
```
```   738 using q apply auto
```
```   739 apply (rule_tac x = "inv q o x" in exI)
```
```   740 by (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
```
```   741
```
```   742 lemma image_compose_permutations_right:
```
```   743   assumes q: "q permutes S"
```
```   744   shows "{p o q | p. p permutes S} = {p . p permutes S}"
```
```   745 apply (rule set_ext)
```
```   746 apply auto
```
```   747 apply (rule permutes_compose)
```
```   748 using q apply auto
```
```   749 apply (rule_tac x = "x o inv q" in exI)
```
```   750 by (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o o_assoc[symmetric])
```
```   751
```
```   752 lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} ==> 1 <= p i \<and> p i <= n"
```
```   753
```
```   754 apply (simp add: permutes_def)
```
```   755 apply metis
```
```   756 done
```
```   757
```
```   758 term setsum
```
```   759 lemma setsum_permutations_inverse: "setsum f {p. p permutes S} = setsum (\<lambda>p. f(inv p)) {p. p permutes S}" (is "?lhs = ?rhs")
```
```   760 proof-
```
```   761   let ?S = "{p . p permutes S}"
```
```   762 have th0: "inj_on inv ?S"
```
```   763 proof(auto simp add: inj_on_def)
```
```   764   fix q r
```
```   765   assume q: "q permutes S" and r: "r permutes S" and qr: "inv q = inv r"
```
```   766   hence "inv (inv q) = inv (inv r)" by simp
```
```   767   with permutes_inv_inv[OF q] permutes_inv_inv[OF r]
```
```   768   show "q = r" by metis
```
```   769 qed
```
```   770   have th1: "inv ` ?S = ?S" using image_inverse_permutations by blast
```
```   771   have th2: "?rhs = setsum (f o inv) ?S" by (simp add: o_def)
```
```   772   from setsum_reindex[OF th0, of f]  show ?thesis unfolding th1 th2 .
```
```   773 qed
```
```   774
```
```   775 lemma setum_permutations_compose_left:
```
```   776   assumes q: "q permutes S"
```
```   777   shows "setsum f {p. p permutes S} =
```
```   778             setsum (\<lambda>p. f(q o p)) {p. p permutes S}" (is "?lhs = ?rhs")
```
```   779 proof-
```
```   780   let ?S = "{p. p permutes S}"
```
```   781   have th0: "?rhs = setsum (f o (op o q)) ?S" by (simp add: o_def)
```
```   782   have th1: "inj_on (op o q) ?S"
```
```   783     apply (auto simp add: inj_on_def)
```
```   784   proof-
```
```   785     fix p r
```
```   786     assume "p permutes S" and r:"r permutes S" and rp: "q \<circ> p = q \<circ> r"
```
```   787     hence "inv q o q o p = inv q o q o r" by (simp add: o_assoc[symmetric])
```
```   788     with permutes_inj[OF q, unfolded inj_iff]
```
```   789
```
```   790     show "p = r" by simp
```
```   791   qed
```
```   792   have th3: "(op o q) ` ?S = ?S" using image_compose_permutations_left[OF q] by auto
```
```   793   from setsum_reindex[OF th1, of f]
```
```   794   show ?thesis unfolding th0 th1 th3 .
```
```   795 qed
```
```   796
```
```   797 lemma sum_permutations_compose_right:
```
```   798   assumes q: "q permutes S"
```
```   799   shows "setsum f {p. p permutes S} =
```
```   800             setsum (\<lambda>p. f(p o q)) {p. p permutes S}" (is "?lhs = ?rhs")
```
```   801 proof-
```
```   802   let ?S = "{p. p permutes S}"
```
```   803   have th0: "?rhs = setsum (f o (\<lambda>p. p o q)) ?S" by (simp add: o_def)
```
```   804   have th1: "inj_on (\<lambda>p. p o q) ?S"
```
```   805     apply (auto simp add: inj_on_def)
```
```   806   proof-
```
```   807     fix p r
```
```   808     assume "p permutes S" and r:"r permutes S" and rp: "p o q = r o q"
```
```   809     hence "p o (q o inv q)  = r o (q o inv q)" by (simp add: o_assoc)
```
```   810     with permutes_surj[OF q, unfolded surj_iff]
```
```   811
```
```   812     show "p = r" by simp
```
```   813   qed
```
```   814   have th3: "(\<lambda>p. p o q) ` ?S = ?S" using image_compose_permutations_right[OF q] by auto
```
```   815   from setsum_reindex[OF th1, of f]
```
```   816   show ?thesis unfolding th0 th1 th3 .
```
```   817 qed
```
```   818
```
```   819 (* ------------------------------------------------------------------------- *)
```
```   820 (* Sum over a set of permutations (could generalize to iteration).           *)
```
```   821 (* ------------------------------------------------------------------------- *)
```
```   822
```
```   823 lemma setsum_over_permutations_insert:
```
```   824   assumes fS: "finite S" and aS: "a \<notin> S"
```
```   825   shows "setsum f {p. p permutes (insert a S)} = setsum (\<lambda>b. setsum (\<lambda>q. f (Fun.swap a b id o q)) {p. p permutes S}) (insert a S)"
```
```   826 proof-
```
```   827   have th0: "\<And>f a b. (\<lambda>(b,p). f (Fun.swap a b id o p)) = f o (\<lambda>(b,p). Fun.swap a b id o p)"
```
```   828     by (simp add: expand_fun_eq)
```
```   829   have th1: "\<And>P Q. P \<times> Q = {(a,b). a \<in> P \<and> b \<in> Q}" by blast
```
```   830   have th2: "\<And>P Q. P \<Longrightarrow> (P \<Longrightarrow> Q) \<Longrightarrow> P \<and> Q" by blast
```
```   831   show ?thesis
```
```   832     unfolding permutes_insert
```
```   833     unfolding setsum_cartesian_product
```
```   834     unfolding  th1[symmetric]
```
```   835     unfolding th0
```
```   836   proof(rule setsum_reindex)
```
```   837     let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
```
```   838     let ?P = "{p. p permutes S}"
```
```   839     {fix b c p q assume b: "b \<in> insert a S" and c: "c \<in> insert a S"
```
```   840       and p: "p permutes S" and q: "q permutes S"
```
```   841       and eq: "Fun.swap a b id o p = Fun.swap a c id o q"
```
```   842       from p q aS have pa: "p a = a" and qa: "q a = a"
```
```   843 	unfolding permutes_def by metis+
```
```   844       from eq have "(Fun.swap a b id o p) a  = (Fun.swap a c id o q) a" by simp
```
```   845       hence bc: "b = c"
```
```   846 	apply (simp add: permutes_def pa qa o_def fun_upd_def swap_def id_def cong del: if_weak_cong)
```
```   847 	apply (cases "a = b", auto)
```
```   848 	by (cases "b = c", auto)
```
```   849       from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id o p) (Fun.swap a c id o p) = (\<lambda>p. Fun.swap a c id o p) (Fun.swap a c id o q)" by simp
```
```   850       hence "p = q" unfolding o_assoc swap_id_idempotent
```
```   851 	by (simp add: o_def)
```
```   852       with bc have "b = c \<and> p = q" by blast
```
```   853     }
```
```   854
```
```   855     then show "inj_on ?f (insert a S \<times> ?P)"
```
```   856       unfolding inj_on_def
```
```   857       apply clarify by metis
```
```   858   qed
```
```   859 qed
```
```   860
```
```   861 end
```