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