src/HOL/Library/Permutations.thy
changeset 73477 1d8a79aa2a99
parent 73476 6b480efe1bc3
child 73478 1be70e3de751
equal deleted inserted replaced
73476:6b480efe1bc3 73477:1d8a79aa2a99
     1 (*  Title:      HOL/Library/Permutations.thy
       
     2     Author:     Amine Chaieb, University of Cambridge
       
     3 *)
       
     4 
       
     5 section \<open>Permutations, both general and specifically on finite sets.\<close>
       
     6 
       
     7 theory Permutations
       
     8   imports Multiset Disjoint_Sets
       
     9 begin
       
    10 
       
    11 subsection \<open>Auxiliary\<close>
       
    12 
       
    13 abbreviation (input) fixpoints :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> 'a set\<close>
       
    14   where \<open>fixpoints f \<equiv> {x. f x = x}\<close>
       
    15 
       
    16 lemma inj_on_fixpoints:
       
    17   \<open>inj_on f (fixpoints f)\<close>
       
    18   by (rule inj_onI) simp
       
    19 
       
    20 lemma bij_betw_fixpoints:
       
    21   \<open>bij_betw f (fixpoints f) (fixpoints f)\<close>
       
    22   using inj_on_fixpoints by (auto simp add: bij_betw_def)
       
    23 
       
    24 
       
    25 subsection \<open>Basic definition and consequences\<close>
       
    26 
       
    27 definition permutes :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> 'a set \<Rightarrow> bool\<close>  (infixr \<open>permutes\<close> 41)
       
    28   where \<open>p permutes S \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)\<close>
       
    29 
       
    30 lemma bij_imp_permutes:
       
    31   \<open>p permutes S\<close> if \<open>bij_betw p S S\<close> and stable: \<open>\<And>x. x \<notin> S \<Longrightarrow> p x = x\<close>
       
    32 proof -
       
    33   note \<open>bij_betw p S S\<close>
       
    34   moreover have \<open>bij_betw p (- S) (- S)\<close>
       
    35     by (auto simp add: stable intro!: bij_betw_imageI inj_onI)
       
    36   ultimately have \<open>bij_betw p (S \<union> - S) (S \<union> - S)\<close>
       
    37     by (rule bij_betw_combine) simp
       
    38   then have \<open>\<exists>!x. p x = y\<close> for y
       
    39     by (simp add: bij_iff)
       
    40   with stable show ?thesis
       
    41     by (simp add: permutes_def)
       
    42 qed
       
    43 
       
    44 context
       
    45   fixes p :: \<open>'a \<Rightarrow> 'a\<close> and S :: \<open>'a set\<close>
       
    46   assumes perm: \<open>p permutes S\<close>
       
    47 begin
       
    48 
       
    49 lemma permutes_inj:
       
    50   \<open>inj p\<close>
       
    51   using perm by (auto simp: permutes_def inj_on_def)
       
    52 
       
    53 lemma permutes_image:
       
    54   \<open>p ` S = S\<close>
       
    55 proof (rule set_eqI)
       
    56   fix x
       
    57   show \<open>x \<in> p ` S \<longleftrightarrow> x \<in> S\<close>
       
    58   proof
       
    59     assume \<open>x \<in> p ` S\<close>
       
    60     then obtain y where \<open>y \<in> S\<close> \<open>p y = x\<close>
       
    61       by blast
       
    62     with perm show \<open>x \<in> S\<close>
       
    63       by (cases \<open>y = x\<close>) (auto simp add: permutes_def)
       
    64   next
       
    65     assume \<open>x \<in> S\<close>
       
    66     with perm obtain y where \<open>y \<in> S\<close> \<open>p y = x\<close>
       
    67       by (metis permutes_def)
       
    68     then show \<open>x \<in> p ` S\<close>
       
    69       by blast
       
    70   qed
       
    71 qed
       
    72 
       
    73 lemma permutes_not_in:
       
    74   \<open>x \<notin> S \<Longrightarrow> p x = x\<close>
       
    75   using perm by (auto simp: permutes_def)
       
    76 
       
    77 lemma permutes_image_complement:
       
    78   \<open>p ` (- S) = - S\<close>
       
    79   by (auto simp add: permutes_not_in)
       
    80 
       
    81 lemma permutes_in_image:
       
    82   \<open>p x \<in> S \<longleftrightarrow> x \<in> S\<close>
       
    83   using permutes_image permutes_inj by (auto dest: inj_image_mem_iff)
       
    84 
       
    85 lemma permutes_surj:
       
    86   \<open>surj p\<close>
       
    87 proof -
       
    88   have \<open>p ` (S \<union> - S) = p ` S \<union> p ` (- S)\<close>
       
    89     by (rule image_Un)
       
    90   then show ?thesis
       
    91     by (simp add: permutes_image permutes_image_complement)
       
    92 qed
       
    93 
       
    94 lemma permutes_inv_o:
       
    95   shows "p \<circ> inv p = id"
       
    96     and "inv p \<circ> p = id"
       
    97   using permutes_inj permutes_surj
       
    98   unfolding inj_iff [symmetric] surj_iff [symmetric] by auto
       
    99 
       
   100 lemma permutes_inverses:
       
   101   shows "p (inv p x) = x"
       
   102     and "inv p (p x) = x"
       
   103   using permutes_inv_o [unfolded fun_eq_iff o_def] by auto
       
   104 
       
   105 lemma permutes_inv_eq:
       
   106   \<open>inv p y = x \<longleftrightarrow> p x = y\<close>
       
   107   by (auto simp add: permutes_inverses)
       
   108 
       
   109 lemma permutes_inj_on:
       
   110   \<open>inj_on p A\<close>
       
   111   by (rule inj_on_subset [of _ UNIV]) (auto intro: permutes_inj)
       
   112 
       
   113 lemma permutes_bij:
       
   114   \<open>bij p\<close>
       
   115   unfolding bij_def by (metis permutes_inj permutes_surj)
       
   116 
       
   117 lemma permutes_imp_bij:
       
   118   \<open>bij_betw p S S\<close>
       
   119   by (simp add: bij_betw_def permutes_image permutes_inj_on)
       
   120 
       
   121 lemma permutes_subset:
       
   122   \<open>p permutes T\<close> if \<open>S \<subseteq> T\<close>
       
   123 proof (rule bij_imp_permutes)
       
   124   define R where \<open>R = T - S\<close>
       
   125   with that have \<open>T = R \<union> S\<close> \<open>R \<inter> S = {}\<close>
       
   126     by auto
       
   127   then have \<open>p x = x\<close> if \<open>x \<in> R\<close> for x
       
   128     using that by (auto intro: permutes_not_in)
       
   129   then have \<open>p ` R = R\<close>
       
   130     by simp
       
   131   with \<open>T = R \<union> S\<close> show \<open>bij_betw p T T\<close>
       
   132     by (simp add: bij_betw_def permutes_inj_on image_Un permutes_image)
       
   133   fix x
       
   134   assume \<open>x \<notin> T\<close>
       
   135   with \<open>T = R \<union> S\<close> show \<open>p x = x\<close>
       
   136     by (simp add: permutes_not_in)
       
   137 qed
       
   138 
       
   139 lemma permutes_imp_permutes_insert:
       
   140   \<open>p permutes insert x S\<close>
       
   141   by (rule permutes_subset) auto
       
   142 
       
   143 end
       
   144 
       
   145 lemma permutes_id [simp]:
       
   146   \<open>id permutes S\<close>
       
   147   by (auto intro: bij_imp_permutes)
       
   148 
       
   149 lemma permutes_empty [simp]:
       
   150   \<open>p permutes {} \<longleftrightarrow> p = id\<close>
       
   151 proof
       
   152   assume \<open>p permutes {}\<close>
       
   153   then show \<open>p = id\<close>
       
   154     by (auto simp add: fun_eq_iff permutes_not_in)
       
   155 next
       
   156   assume \<open>p = id\<close>
       
   157   then show \<open>p permutes {}\<close>
       
   158     by simp
       
   159 qed
       
   160 
       
   161 lemma permutes_sing [simp]:
       
   162   \<open>p permutes {a} \<longleftrightarrow> p = id\<close>
       
   163 proof
       
   164   assume perm: \<open>p permutes {a}\<close>
       
   165   show \<open>p = id\<close>
       
   166   proof
       
   167     fix x
       
   168     from perm have \<open>p ` {a} = {a}\<close>
       
   169       by (rule permutes_image)
       
   170     with perm show \<open>p x = id x\<close>
       
   171       by (cases \<open>x = a\<close>) (auto simp add: permutes_not_in)
       
   172   qed
       
   173 next
       
   174   assume \<open>p = id\<close>
       
   175   then show \<open>p permutes {a}\<close>
       
   176     by simp
       
   177 qed
       
   178 
       
   179 lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
       
   180   by (simp add: permutes_def)
       
   181 
       
   182 lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S"
       
   183   by (rule bij_imp_permutes) (auto simp add: swap_id_eq)
       
   184 
       
   185 lemma permutes_superset:
       
   186   \<open>p permutes T\<close> if \<open>p permutes S\<close> \<open>\<And>x. x \<in> S - T \<Longrightarrow> p x = x\<close>
       
   187 proof -
       
   188   define R U where \<open>R = T \<inter> S\<close> and \<open>U = S - T\<close>
       
   189   then have \<open>T = R \<union> (T - S)\<close> \<open>S = R \<union> U\<close> \<open>R \<inter> U = {}\<close>
       
   190     by auto
       
   191   from that \<open>U = S - T\<close> have \<open>p ` U = U\<close>
       
   192     by simp
       
   193   from \<open>p permutes S\<close> have \<open>bij_betw p (R \<union> U) (R \<union> U)\<close>
       
   194     by (simp add: permutes_imp_bij \<open>S = R \<union> U\<close>)
       
   195   moreover have \<open>bij_betw p U U\<close>
       
   196     using that \<open>U = S - T\<close> by (simp add: bij_betw_def permutes_inj_on)
       
   197   ultimately have \<open>bij_betw p R R\<close>
       
   198     using \<open>R \<inter> U = {}\<close> \<open>R \<inter> U = {}\<close> by (rule bij_betw_partition)
       
   199   then have \<open>p permutes R\<close>
       
   200   proof (rule bij_imp_permutes)
       
   201     fix x
       
   202     assume \<open>x \<notin> R\<close>
       
   203     with \<open>R = T \<inter> S\<close> \<open>p permutes S\<close> show \<open>p x = x\<close>
       
   204       by (cases \<open>x \<in> S\<close>) (auto simp add: permutes_not_in that(2))
       
   205   qed
       
   206   then have \<open>p permutes R \<union> (T - S)\<close>
       
   207     by (rule permutes_subset) simp
       
   208   with \<open>T = R \<union> (T - S)\<close> show ?thesis
       
   209     by simp
       
   210 qed
       
   211 
       
   212 lemma permutes_bij_inv_into: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close>
       
   213   fixes A :: "'a set"
       
   214     and B :: "'b set"
       
   215   assumes "p permutes A"
       
   216     and "bij_betw f A B"
       
   217   shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B"
       
   218 proof (rule bij_imp_permutes)
       
   219   from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A"
       
   220     by (auto simp add: permutes_imp_bij bij_betw_inv_into)
       
   221   then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B"
       
   222     by (simp add: bij_betw_trans)
       
   223   then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B"
       
   224     by (subst bij_betw_cong[where g="f \<circ> p \<circ> inv_into A f"]) auto
       
   225 next
       
   226   fix x
       
   227   assume "x \<notin> B"
       
   228   then show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto
       
   229 qed
       
   230 
       
   231 lemma permutes_image_mset: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close>
       
   232   assumes "p permutes A"
       
   233   shows "image_mset p (mset_set A) = mset_set A"
       
   234   using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image)
       
   235 
       
   236 lemma permutes_implies_image_mset_eq: \<^marker>\<open>contributor \<open>Lukas Bulwahn\<close>\<close>
       
   237   assumes "p permutes A" "\<And>x. x \<in> A \<Longrightarrow> f x = f' (p x)"
       
   238   shows "image_mset f' (mset_set A) = image_mset f (mset_set A)"
       
   239 proof -
       
   240   have "f x = f' (p x)" if "x \<in># mset_set A" for x
       
   241     using assms(2)[of x] that by (cases "finite A") auto
       
   242   with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)"
       
   243     by (auto intro!: image_mset_cong)
       
   244   also have "\<dots> = image_mset f' (image_mset p (mset_set A))"
       
   245     by (simp add: image_mset.compositionality)
       
   246   also have "\<dots> = image_mset f' (mset_set A)"
       
   247   proof -
       
   248     from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A"
       
   249       by blast
       
   250     then show ?thesis by simp
       
   251   qed
       
   252   finally show ?thesis ..
       
   253 qed
       
   254 
       
   255 
       
   256 subsection \<open>Group properties\<close>
       
   257 
       
   258 lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S"
       
   259   unfolding permutes_def o_def by metis
       
   260 
       
   261 lemma permutes_inv:
       
   262   assumes "p permutes S"
       
   263   shows "inv p permutes S"
       
   264   using assms unfolding permutes_def permutes_inv_eq[OF assms] by metis
       
   265 
       
   266 lemma permutes_inv_inv:
       
   267   assumes "p permutes S"
       
   268   shows "inv (inv p) = p"
       
   269   unfolding fun_eq_iff permutes_inv_eq[OF assms] permutes_inv_eq[OF permutes_inv[OF assms]]
       
   270   by blast
       
   271 
       
   272 lemma permutes_invI:
       
   273   assumes perm: "p permutes S"
       
   274     and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
       
   275     and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
       
   276   shows "inv p = p'"
       
   277 proof
       
   278   show "inv p x = p' x" for x
       
   279   proof (cases "x \<in> S")
       
   280     case True
       
   281     from assms have "p' x = p' (p (inv p x))"
       
   282       by (simp add: permutes_inverses)
       
   283     also from permutes_inv[OF perm] True have "\<dots> = inv p x"
       
   284       by (subst inv) (simp_all add: permutes_in_image)
       
   285     finally show ?thesis ..
       
   286   next
       
   287     case False
       
   288     with permutes_inv[OF perm] show ?thesis
       
   289       by (simp_all add: outside permutes_not_in)
       
   290   qed
       
   291 qed
       
   292 
       
   293 lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A"
       
   294   by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv])
       
   295 
       
   296 
       
   297 subsection \<open>Mapping permutations with bijections\<close>
       
   298 
       
   299 lemma bij_betw_permutations:
       
   300   assumes "bij_betw f A B"
       
   301   shows   "bij_betw (\<lambda>\<pi> x. if x \<in> B then f (\<pi> (inv_into A f x)) else x) 
       
   302              {\<pi>. \<pi> permutes A} {\<pi>. \<pi> permutes B}" (is "bij_betw ?f _ _")
       
   303 proof -
       
   304   let ?g = "(\<lambda>\<pi> x. if x \<in> A then inv_into A f (\<pi> (f x)) else x)"
       
   305   show ?thesis
       
   306   proof (rule bij_betw_byWitness [of _ ?g], goal_cases)
       
   307     case 3
       
   308     show ?case using permutes_bij_inv_into[OF _ assms] by auto
       
   309   next
       
   310     case 4
       
   311     have bij_inv: "bij_betw (inv_into A f) B A" by (intro bij_betw_inv_into assms)
       
   312     {
       
   313       fix \<pi> assume "\<pi> permutes B"
       
   314       from permutes_bij_inv_into[OF this bij_inv] and assms
       
   315         have "(\<lambda>x. if x \<in> A then inv_into A f (\<pi> (f x)) else x) permutes A"
       
   316         by (simp add: inv_into_inv_into_eq cong: if_cong)
       
   317     }
       
   318     from this show ?case by (auto simp: permutes_inv)
       
   319   next
       
   320     case 1
       
   321     thus ?case using assms
       
   322       by (auto simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_left
       
   323                dest: bij_betwE)
       
   324   next
       
   325     case 2
       
   326     moreover have "bij_betw (inv_into A f) B A"
       
   327       by (intro bij_betw_inv_into assms)
       
   328     ultimately show ?case using assms
       
   329       by (auto simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_right 
       
   330                dest: bij_betwE)
       
   331   qed
       
   332 qed
       
   333 
       
   334 lemma bij_betw_derangements:
       
   335   assumes "bij_betw f A B"
       
   336   shows   "bij_betw (\<lambda>\<pi> x. if x \<in> B then f (\<pi> (inv_into A f x)) else x) 
       
   337              {\<pi>. \<pi> permutes A \<and> (\<forall>x\<in>A. \<pi> x \<noteq> x)} {\<pi>. \<pi> permutes B \<and> (\<forall>x\<in>B. \<pi> x \<noteq> x)}" 
       
   338            (is "bij_betw ?f _ _")
       
   339 proof -
       
   340   let ?g = "(\<lambda>\<pi> x. if x \<in> A then inv_into A f (\<pi> (f x)) else x)"
       
   341   show ?thesis
       
   342   proof (rule bij_betw_byWitness [of _ ?g], goal_cases)
       
   343     case 3
       
   344     have "?f \<pi> x \<noteq> x" if "\<pi> permutes A" "\<And>x. x \<in> A \<Longrightarrow> \<pi> x \<noteq> x" "x \<in> B" for \<pi> x
       
   345       using that and assms by (metis bij_betwE bij_betw_imp_inj_on bij_betw_imp_surj_on
       
   346                                      inv_into_f_f inv_into_into permutes_imp_bij)
       
   347     with permutes_bij_inv_into[OF _ assms] show ?case by auto
       
   348   next
       
   349     case 4
       
   350     have bij_inv: "bij_betw (inv_into A f) B A" by (intro bij_betw_inv_into assms)
       
   351     have "?g \<pi> permutes A" if "\<pi> permutes B" for \<pi>
       
   352       using permutes_bij_inv_into[OF that bij_inv] and assms
       
   353       by (simp add: inv_into_inv_into_eq cong: if_cong)
       
   354     moreover have "?g \<pi> x \<noteq> x" if "\<pi> permutes B" "\<And>x. x \<in> B \<Longrightarrow> \<pi> x \<noteq> x" "x \<in> A" for \<pi> x
       
   355       using that and assms by (metis bij_betwE bij_betw_imp_surj_on f_inv_into_f permutes_imp_bij)
       
   356     ultimately show ?case by auto
       
   357   next
       
   358     case 1
       
   359     thus ?case using assms
       
   360       by (force simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_left
       
   361                 dest: bij_betwE)
       
   362   next
       
   363     case 2
       
   364     moreover have "bij_betw (inv_into A f) B A"
       
   365       by (intro bij_betw_inv_into assms)
       
   366     ultimately show ?case using assms
       
   367       by (force simp: fun_eq_iff permutes_not_in permutes_in_image bij_betw_inv_into_right 
       
   368                 dest: bij_betwE)
       
   369   qed
       
   370 qed
       
   371 
       
   372 
       
   373 subsection \<open>The number of permutations on a finite set\<close>
       
   374 
       
   375 lemma permutes_insert_lemma:
       
   376   assumes "p permutes (insert a S)"
       
   377   shows "Fun.swap a (p a) id \<circ> p permutes S"
       
   378   apply (rule permutes_superset[where S = "insert a S"])
       
   379   apply (rule permutes_compose[OF assms])
       
   380   apply (rule permutes_swap_id, simp)
       
   381   using permutes_in_image[OF assms, of a]
       
   382   apply simp
       
   383   apply (auto simp add: Ball_def Fun.swap_def)
       
   384   done
       
   385 
       
   386 lemma permutes_insert: "{p. p permutes (insert a S)} =
       
   387   (\<lambda>(b, p). Fun.swap a b id \<circ> p) ` {(b, p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
       
   388 proof -
       
   389   have "p permutes insert a S \<longleftrightarrow>
       
   390     (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p
       
   391   proof -
       
   392     have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S"
       
   393       if p: "p permutes insert a S"
       
   394     proof -
       
   395       let ?b = "p a"
       
   396       let ?q = "Fun.swap a (p a) id \<circ> p"
       
   397       have *: "p = Fun.swap a ?b id \<circ> ?q"
       
   398         by (simp add: fun_eq_iff o_assoc)
       
   399       have **: "?b \<in> insert a S"
       
   400         unfolding permutes_in_image[OF p] by simp
       
   401       from permutes_insert_lemma[OF p] * ** show ?thesis
       
   402        by blast
       
   403     qed
       
   404     moreover have "p permutes insert a S"
       
   405       if bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" for b q
       
   406     proof -
       
   407       from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S"
       
   408         by auto
       
   409       have a: "a \<in> insert a S"
       
   410         by simp
       
   411       from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis
       
   412         by simp
       
   413     qed
       
   414     ultimately show ?thesis by blast
       
   415   qed
       
   416   then show ?thesis by auto
       
   417 qed
       
   418 
       
   419 lemma card_permutations:
       
   420   assumes "card S = n"
       
   421     and "finite S"
       
   422   shows "card {p. p permutes S} = fact n"
       
   423   using assms(2,1)
       
   424 proof (induct arbitrary: n)
       
   425   case empty
       
   426   then show ?case by simp
       
   427 next
       
   428   case (insert x F)
       
   429   {
       
   430     fix n
       
   431     assume card_insert: "card (insert x F) = n"
       
   432     let ?xF = "{p. p permutes insert x F}"
       
   433     let ?pF = "{p. p permutes F}"
       
   434     let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
       
   435     let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
       
   436     have xfgpF': "?xF = ?g ` ?pF'"
       
   437       by (rule permutes_insert[of x F])
       
   438     from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1"
       
   439       by auto
       
   440     from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
       
   441       by auto
       
   442     then have "finite ?pF"
       
   443       by (auto intro: card_ge_0_finite)
       
   444     with \<open>finite F\<close> card.insert_remove have pF'f: "finite ?pF'"
       
   445       apply (simp only: Collect_case_prod Collect_mem_eq)
       
   446       apply (rule finite_cartesian_product)
       
   447       apply simp_all
       
   448       done
       
   449 
       
   450     have ginj: "inj_on ?g ?pF'"
       
   451     proof -
       
   452       {
       
   453         fix b p c q
       
   454         assume bp: "(b, p) \<in> ?pF'"
       
   455         assume cq: "(c, q) \<in> ?pF'"
       
   456         assume eq: "?g (b, p) = ?g (c, q)"
       
   457         from bp cq have pF: "p permutes F" and qF: "q permutes F"
       
   458           by auto
       
   459         from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x"
       
   460           by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
       
   461         also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x"
       
   462           by (auto simp: fun_upd_def fun_eq_iff)
       
   463         also from qF \<open>x \<notin> F\<close> have "\<dots> = c"
       
   464           by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
       
   465         finally have "b = c" .
       
   466         then have "Fun.swap x b id = Fun.swap x c id"
       
   467           by simp
       
   468         with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q"
       
   469           by simp
       
   470         then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
       
   471           by simp
       
   472         then have "p = q"
       
   473           by (simp add: o_assoc)
       
   474         with \<open>b = c\<close> have "(b, p) = (c, q)"
       
   475           by simp
       
   476       }
       
   477       then show ?thesis
       
   478         unfolding inj_on_def by blast
       
   479     qed
       
   480     from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0"
       
   481       by auto
       
   482     then have "\<exists>m. n = Suc m"
       
   483       by presburger
       
   484     then obtain m where n: "n = Suc m"
       
   485       by blast
       
   486     from pFs card_insert have *: "card ?xF = fact n"
       
   487       unfolding xfgpF' card_image[OF ginj]
       
   488       using \<open>finite F\<close> \<open>finite ?pF\<close>
       
   489       by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n)
       
   490     from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF"
       
   491       by (simp add: xfgpF' n)
       
   492     from * have "card ?xF = fact n"
       
   493       unfolding xFf by blast
       
   494   }
       
   495   with insert show ?case by simp
       
   496 qed
       
   497 
       
   498 lemma finite_permutations:
       
   499   assumes "finite S"
       
   500   shows "finite {p. p permutes S}"
       
   501   using card_permutations[OF refl assms] by (auto intro: card_ge_0_finite)
       
   502 
       
   503 
       
   504 subsection \<open>Hence a sort of induction principle composing by swaps\<close>
       
   505 
       
   506 lemma permutes_induct [consumes 2, case_names id swap]:
       
   507   \<open>P p\<close> if \<open>p permutes S\<close> \<open>finite S\<close>
       
   508   and id: \<open>P id\<close>
       
   509   and swap: \<open>\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> p permutes S \<Longrightarrow> P p \<Longrightarrow> P (Fun.swap a b id \<circ> p)\<close>
       
   510 using \<open>finite S\<close> \<open>p permutes S\<close> swap proof (induction S arbitrary: p)
       
   511   case empty
       
   512   with id show ?case
       
   513     by (simp only: permutes_empty)
       
   514 next
       
   515   case (insert x S p)
       
   516   define q where \<open>q = Fun.swap x (p x) id \<circ> p\<close>
       
   517   then have swap_q: \<open>Fun.swap x (p x) id \<circ> q = p\<close>
       
   518     by (simp add: o_assoc)
       
   519   from \<open>p permutes insert x S\<close> have \<open>q permutes S\<close>
       
   520     by (simp add: q_def permutes_insert_lemma)
       
   521   then have \<open>q permutes insert x S\<close>
       
   522     by (simp add: permutes_imp_permutes_insert)
       
   523   from \<open>q permutes S\<close> have \<open>P q\<close>
       
   524     by (auto intro: insert.IH insert.prems(2) permutes_imp_permutes_insert)
       
   525   have \<open>x \<in> insert x S\<close>
       
   526     by simp
       
   527   moreover from \<open>p permutes insert x S\<close> have \<open>p x \<in> insert x S\<close>
       
   528     using permutes_in_image [of p \<open>insert x S\<close> x] by simp
       
   529   ultimately have \<open>P (Fun.swap x (p x) id \<circ> q)\<close>
       
   530     using \<open>q permutes insert x S\<close> \<open>P q\<close>
       
   531     by (rule insert.prems(2))
       
   532   then show ?case
       
   533     by (simp add: swap_q)
       
   534 qed
       
   535 
       
   536 lemma permutes_rev_induct [consumes 2, case_names id swap]:
       
   537   \<open>P p\<close> if \<open>p permutes S\<close> \<open>finite S\<close>
       
   538   and id': \<open>P id\<close>
       
   539   and swap': \<open>\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> p permutes S \<Longrightarrow> P p \<Longrightarrow> P (Fun.swap a b p)\<close>
       
   540 using \<open>p permutes S\<close> \<open>finite S\<close> proof (induction rule: permutes_induct)
       
   541   case id
       
   542   from id' show ?case .
       
   543 next
       
   544   case (swap a b p)
       
   545   have \<open>P (Fun.swap (inv p a) (inv p b) p)\<close>
       
   546     by (rule swap') (auto simp add: swap permutes_in_image permutes_inv)
       
   547   also have \<open>Fun.swap (inv p a) (inv p b) p = Fun.swap a b id \<circ> p\<close>
       
   548     by (rule bij_swap_comp [symmetric]) (rule permutes_bij, rule swap)
       
   549   finally show ?case .
       
   550 qed
       
   551 
       
   552 
       
   553 subsection \<open>Permutations of index set for iterated operations\<close>
       
   554 
       
   555 lemma (in comm_monoid_set) permute:
       
   556   assumes "p permutes S"
       
   557   shows "F g S = F (g \<circ> p) S"
       
   558 proof -
       
   559   from \<open>p permutes S\<close> have "inj p"
       
   560     by (rule permutes_inj)
       
   561   then have "inj_on p S"
       
   562     by (auto intro: subset_inj_on)
       
   563   then have "F g (p ` S) = F (g \<circ> p) S"
       
   564     by (rule reindex)
       
   565   moreover from \<open>p permutes S\<close> have "p ` S = S"
       
   566     by (rule permutes_image)
       
   567   ultimately show ?thesis
       
   568     by simp
       
   569 qed
       
   570 
       
   571 
       
   572 subsection \<open>Permutations as transposition sequences\<close>
       
   573 
       
   574 inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
       
   575   where
       
   576     id[simp]: "swapidseq 0 id"
       
   577   | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
       
   578 
       
   579 declare id[unfolded id_def, simp]
       
   580 
       
   581 definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
       
   582 
       
   583 
       
   584 subsection \<open>Some closure properties of the set of permutations, with lengths\<close>
       
   585 
       
   586 lemma permutation_id[simp]: "permutation id"
       
   587   unfolding permutation_def by (rule exI[where x=0]) simp
       
   588 
       
   589 declare permutation_id[unfolded id_def, simp]
       
   590 
       
   591 lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
       
   592   apply clarsimp
       
   593   using comp_Suc[of 0 id a b]
       
   594   apply simp
       
   595   done
       
   596 
       
   597 lemma permutation_swap_id: "permutation (Fun.swap a b id)"
       
   598 proof (cases "a = b")
       
   599   case True
       
   600   then show ?thesis by simp
       
   601 next
       
   602   case False
       
   603   then show ?thesis
       
   604     unfolding permutation_def
       
   605     using swapidseq_swap[of a b] by blast
       
   606 qed
       
   607 
       
   608 
       
   609 lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
       
   610 proof (induct n p arbitrary: m q rule: swapidseq.induct)
       
   611   case (id m q)
       
   612   then show ?case by simp
       
   613 next
       
   614   case (comp_Suc n p a b m q)
       
   615   have eq: "Suc n + m = Suc (n + m)"
       
   616     by arith
       
   617   show ?case
       
   618     apply (simp only: eq comp_assoc)
       
   619     apply (rule swapidseq.comp_Suc)
       
   620     using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
       
   621      apply blast+
       
   622     done
       
   623 qed
       
   624 
       
   625 lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)"
       
   626   unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
       
   627 
       
   628 lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
       
   629   by (induct n p rule: swapidseq.induct)
       
   630     (use swapidseq_swap[of a b] in \<open>auto simp add: comp_assoc intro: swapidseq.comp_Suc\<close>)
       
   631 
       
   632 lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id"
       
   633 proof (induct n p rule: swapidseq.induct)
       
   634   case id
       
   635   then show ?case
       
   636     by (rule exI[where x=id]) simp
       
   637 next
       
   638   case (comp_Suc n p a b)
       
   639   from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
       
   640     by blast
       
   641   let ?q = "q \<circ> Fun.swap a b id"
       
   642   note H = comp_Suc.hyps
       
   643   from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (Fun.swap a b id)"
       
   644     by simp
       
   645   from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q"
       
   646     by simp
       
   647   have "Fun.swap a b id \<circ> p \<circ> ?q = Fun.swap a b id \<circ> (p \<circ> q) \<circ> Fun.swap a b id"
       
   648     by (simp add: o_assoc)
       
   649   also have "\<dots> = id"
       
   650     by (simp add: q(2))
       
   651   finally have ***: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
       
   652   have "?q \<circ> (Fun.swap a b id \<circ> p) = q \<circ> (Fun.swap a b id \<circ> Fun.swap a b id) \<circ> p"
       
   653     by (simp only: o_assoc)
       
   654   then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
       
   655     by (simp add: q(3))
       
   656   with ** *** show ?case
       
   657     by blast
       
   658 qed
       
   659 
       
   660 lemma swapidseq_inverse:
       
   661   assumes "swapidseq n p"
       
   662   shows "swapidseq n (inv p)"
       
   663   using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto
       
   664 
       
   665 lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
       
   666   using permutation_def swapidseq_inverse by blast
       
   667 
       
   668 
       
   669 
       
   670 subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close>
       
   671 
       
   672 lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>
       
   673   Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id"
       
   674   by (simp add: fun_eq_iff Fun.swap_def)
       
   675 
       
   676 lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow>
       
   677   Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id"
       
   678   by (simp add: fun_eq_iff Fun.swap_def)
       
   679 
       
   680 lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow>
       
   681   Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id"
       
   682   by (simp add: fun_eq_iff Fun.swap_def)
       
   683 
       
   684 
       
   685 subsection \<open>The identity map only has even transposition sequences\<close>
       
   686 
       
   687 lemma symmetry_lemma:
       
   688   assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c"
       
   689     and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
       
   690       a = c \<and> b = d \<or> a = c \<and> b \<noteq> d \<or> a \<noteq> c \<and> b = d \<or> a \<noteq> c \<and> a \<noteq> d \<and> b \<noteq> c \<and> b \<noteq> d \<Longrightarrow>
       
   691       P a b c d"
       
   692   shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>  P a b c d"
       
   693   using assms by metis
       
   694 
       
   695 lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
       
   696   Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
       
   697   (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
       
   698     Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)"
       
   699 proof -
       
   700   assume neq: "a \<noteq> b" "c \<noteq> d"
       
   701   have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
       
   702     (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
       
   703       (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
       
   704         Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
       
   705     apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
       
   706      apply (simp_all only: swap_commute)
       
   707     apply (case_tac "a = c \<and> b = d")
       
   708      apply (clarsimp simp only: swap_commute swap_id_idempotent)
       
   709     apply (case_tac "a = c \<and> b \<noteq> d")
       
   710      apply (rule disjI2)
       
   711      apply (rule_tac x="b" in exI)
       
   712      apply (rule_tac x="d" in exI)
       
   713      apply (rule_tac x="b" in exI)
       
   714      apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
       
   715     apply (case_tac "a \<noteq> c \<and> b = d")
       
   716      apply (rule disjI2)
       
   717      apply (rule_tac x="c" in exI)
       
   718      apply (rule_tac x="d" in exI)
       
   719      apply (rule_tac x="c" in exI)
       
   720      apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
       
   721     apply (rule disjI2)
       
   722     apply (rule_tac x="c" in exI)
       
   723     apply (rule_tac x="d" in exI)
       
   724     apply (rule_tac x="b" in exI)
       
   725     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
       
   726     done
       
   727   with neq show ?thesis by metis
       
   728 qed
       
   729 
       
   730 lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
       
   731   using swapidseq.cases[of 0 p "p = id"] by auto
       
   732 
       
   733 lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
       
   734     n = 0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = Fun.swap a b id \<circ> q \<and> swapidseq m q \<and> a \<noteq> b)"
       
   735   apply (rule iffI)
       
   736    apply (erule swapidseq.cases[of n p])
       
   737     apply simp
       
   738    apply (rule disjI2)
       
   739    apply (rule_tac x= "a" in exI)
       
   740    apply (rule_tac x= "b" in exI)
       
   741    apply (rule_tac x= "pa" in exI)
       
   742    apply (rule_tac x= "na" in exI)
       
   743    apply simp
       
   744   apply auto
       
   745   apply (rule comp_Suc, simp_all)
       
   746   done
       
   747 
       
   748 lemma fixing_swapidseq_decrease:
       
   749   assumes "swapidseq n p"
       
   750     and "a \<noteq> b"
       
   751     and "(Fun.swap a b id \<circ> p) a = a"
       
   752   shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
       
   753   using assms
       
   754 proof (induct n arbitrary: p a b)
       
   755   case 0
       
   756   then show ?case
       
   757     by (auto simp add: Fun.swap_def fun_upd_def)
       
   758 next
       
   759   case (Suc n p a b)
       
   760   from Suc.prems(1) swapidseq_cases[of "Suc n" p]
       
   761   obtain c d q m where
       
   762     cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
       
   763     by auto
       
   764   consider "Fun.swap a b id \<circ> Fun.swap c d id = id"
       
   765     | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
       
   766       "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
       
   767     using swap_general[OF Suc.prems(2) cdqm(4)] by metis
       
   768   then show ?case
       
   769   proof cases
       
   770     case 1
       
   771     then show ?thesis
       
   772       by (simp only: cdqm o_assoc) (simp add: cdqm)
       
   773   next
       
   774     case prems: 2
       
   775     then have az: "a \<noteq> z"
       
   776       by simp
       
   777     from prems have *: "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" for h
       
   778       by (simp add: Fun.swap_def)
       
   779     from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)"
       
   780       by simp
       
   781     then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
       
   782       by (simp add: o_assoc prems)
       
   783     then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a"
       
   784       by simp
       
   785     then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
       
   786       unfolding Suc by metis
       
   787     then have "(Fun.swap a z id \<circ> q) a = a"
       
   788       by (simp only: *)
       
   789     from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az this]
       
   790     have **: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
       
   791       by blast+
       
   792     from \<open>n \<noteq> 0\<close> have ***: "Suc n - 1 = Suc (n - 1)"
       
   793       by auto
       
   794     show ?thesis
       
   795       apply (simp only: cdqm(2) prems o_assoc ***)
       
   796       apply (simp only: Suc_not_Zero simp_thms comp_assoc)
       
   797       apply (rule comp_Suc)
       
   798       using ** prems
       
   799        apply blast+
       
   800       done
       
   801   qed
       
   802 qed
       
   803 
       
   804 lemma swapidseq_identity_even:
       
   805   assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)"
       
   806   shows "even n"
       
   807   using \<open>swapidseq n id\<close>
       
   808 proof (induct n rule: nat_less_induct)
       
   809   case H: (1 n)
       
   810   consider "n = 0"
       
   811     | a b :: 'a and q m where "n = Suc m" "id = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
       
   812     using H(2)[unfolded swapidseq_cases[of n id]] by auto
       
   813   then show ?case
       
   814   proof cases
       
   815     case 1
       
   816     then show ?thesis by presburger
       
   817   next
       
   818     case h: 2
       
   819     from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
       
   820     have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
       
   821       by auto
       
   822     from h m have mn: "m - 1 < n"
       
   823       by arith
       
   824     from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis
       
   825       by presburger
       
   826   qed
       
   827 qed
       
   828 
       
   829 
       
   830 subsection \<open>Therefore we have a welldefined notion of parity\<close>
       
   831 
       
   832 definition "evenperm p = even (SOME n. swapidseq n p)"
       
   833 
       
   834 lemma swapidseq_even_even:
       
   835   assumes m: "swapidseq m p"
       
   836     and n: "swapidseq n p"
       
   837   shows "even m \<longleftrightarrow> even n"
       
   838 proof -
       
   839   from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
       
   840     by blast
       
   841   from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis
       
   842     by arith
       
   843 qed
       
   844 
       
   845 lemma evenperm_unique:
       
   846   assumes p: "swapidseq n p"
       
   847     and n:"even n = b"
       
   848   shows "evenperm p = b"
       
   849   unfolding n[symmetric] evenperm_def
       
   850   apply (rule swapidseq_even_even[where p = p])
       
   851    apply (rule someI[where x = n])
       
   852   using p
       
   853    apply blast+
       
   854   done
       
   855 
       
   856 
       
   857 subsection \<open>And it has the expected composition properties\<close>
       
   858 
       
   859 lemma evenperm_id[simp]: "evenperm id = True"
       
   860   by (rule evenperm_unique[where n = 0]) simp_all
       
   861 
       
   862 lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
       
   863   by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
       
   864 
       
   865 lemma evenperm_comp:
       
   866   assumes "permutation p" "permutation q"
       
   867   shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q"
       
   868 proof -
       
   869   from assms obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
       
   870     unfolding permutation_def by blast
       
   871   have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)"
       
   872     by arith
       
   873   from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
       
   874     and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis
       
   875     by blast
       
   876 qed
       
   877 
       
   878 lemma evenperm_inv:
       
   879   assumes "permutation p"
       
   880   shows "evenperm (inv p) = evenperm p"
       
   881 proof -
       
   882   from assms obtain n where n: "swapidseq n p"
       
   883     unfolding permutation_def by blast
       
   884   show ?thesis
       
   885     by (rule evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]])
       
   886 qed
       
   887 
       
   888 
       
   889 subsection \<open>A more abstract characterization of permutations\<close>
       
   890 
       
   891 lemma permutation_bijective:
       
   892   assumes "permutation p"
       
   893   shows "bij p"
       
   894 proof -
       
   895   from assms obtain n where n: "swapidseq n p"
       
   896     unfolding permutation_def by blast
       
   897   from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
       
   898     by blast
       
   899   then show ?thesis
       
   900     unfolding bij_iff
       
   901     apply (auto simp add: fun_eq_iff)
       
   902     apply metis
       
   903     done
       
   904 qed
       
   905 
       
   906 lemma permutation_finite_support:
       
   907   assumes "permutation p"
       
   908   shows "finite {x. p x \<noteq> x}"
       
   909 proof -
       
   910   from assms obtain n where "swapidseq n p"
       
   911     unfolding permutation_def by blast
       
   912   then show ?thesis
       
   913   proof (induct n p rule: swapidseq.induct)
       
   914     case id
       
   915     then show ?case by simp
       
   916   next
       
   917     case (comp_Suc n p a b)
       
   918     let ?S = "insert a (insert b {x. p x \<noteq> x})"
       
   919     from comp_Suc.hyps(2) have *: "finite ?S"
       
   920       by simp
       
   921     from \<open>a \<noteq> b\<close> have **: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
       
   922       by (auto simp: Fun.swap_def)
       
   923     show ?case
       
   924       by (rule finite_subset[OF ** *])
       
   925   qed
       
   926 qed
       
   927 
       
   928 lemma permutation_lemma:
       
   929   assumes "finite S"
       
   930     and "bij p"
       
   931     and "\<forall>x. x \<notin> S \<longrightarrow> p x = x"
       
   932   shows "permutation p"
       
   933   using assms
       
   934 proof (induct S arbitrary: p rule: finite_induct)
       
   935   case empty
       
   936   then show ?case
       
   937     by simp
       
   938 next
       
   939   case (insert a F p)
       
   940   let ?r = "Fun.swap a (p a) id \<circ> p"
       
   941   let ?q = "Fun.swap a (p a) id \<circ> ?r"
       
   942   have *: "?r a = a"
       
   943     by (simp add: Fun.swap_def)
       
   944   from insert * have **: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
       
   945     by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3))
       
   946   have "bij ?r"
       
   947     by (rule bij_swap_compose_bij[OF insert(4)])
       
   948   have "permutation ?r"
       
   949     by (rule insert(3)[OF \<open>bij ?r\<close> **])
       
   950   then have "permutation ?q"
       
   951     by (simp add: permutation_compose permutation_swap_id)
       
   952   then show ?case
       
   953     by (simp add: o_assoc)
       
   954 qed
       
   955 
       
   956 lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
       
   957   (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
       
   958 proof
       
   959   assume ?lhs
       
   960   with permutation_bijective permutation_finite_support show "?b \<and> ?f"
       
   961     by auto
       
   962 next
       
   963   assume "?b \<and> ?f"
       
   964   then have "?f" "?b" by blast+
       
   965   from permutation_lemma[OF this] show ?lhs
       
   966     by blast
       
   967 qed
       
   968 
       
   969 lemma permutation_inverse_works:
       
   970   assumes "permutation p"
       
   971   shows "inv p \<circ> p = id"
       
   972     and "p \<circ> inv p = id"
       
   973   using permutation_bijective [OF assms] by (auto simp: bij_def inj_iff surj_iff)
       
   974 
       
   975 lemma permutation_inverse_compose:
       
   976   assumes p: "permutation p"
       
   977     and q: "permutation q"
       
   978   shows "inv (p \<circ> q) = inv q \<circ> inv p"
       
   979 proof -
       
   980   note ps = permutation_inverse_works[OF p]
       
   981   note qs = permutation_inverse_works[OF q]
       
   982   have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p"
       
   983     by (simp add: o_assoc)
       
   984   also have "\<dots> = id"
       
   985     by (simp add: ps qs)
       
   986   finally have *: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
       
   987   have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
       
   988     by (simp add: o_assoc)
       
   989   also have "\<dots> = id"
       
   990     by (simp add: ps qs)
       
   991   finally have **: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
       
   992   show ?thesis
       
   993     by (rule inv_unique_comp[OF * **])
       
   994 qed
       
   995 
       
   996 
       
   997 subsection \<open>Relation to \<open>permutes\<close>\<close>
       
   998 
       
   999 lemma permutes_imp_permutation:
       
  1000   \<open>permutation p\<close> if \<open>finite S\<close> \<open>p permutes S\<close>
       
  1001 proof -
       
  1002   from \<open>p permutes S\<close> have \<open>{x. p x \<noteq> x} \<subseteq> S\<close>
       
  1003     by (auto dest: permutes_not_in)
       
  1004   then have \<open>finite {x. p x \<noteq> x}\<close>
       
  1005     using \<open>finite S\<close> by (rule finite_subset)
       
  1006   moreover from \<open>p permutes S\<close> have \<open>bij p\<close>
       
  1007     by (auto dest: permutes_bij)
       
  1008   ultimately show ?thesis
       
  1009     by (simp add: permutation)
       
  1010 qed
       
  1011 
       
  1012 lemma permutation_permutesE:
       
  1013   assumes \<open>permutation p\<close>
       
  1014   obtains S where \<open>finite S\<close> \<open>p permutes S\<close>
       
  1015 proof -
       
  1016   from assms have fin: \<open>finite {x. p x \<noteq> x}\<close>
       
  1017     by (simp add: permutation)
       
  1018   from assms have \<open>bij p\<close>
       
  1019     by (simp add: permutation)
       
  1020   also have \<open>UNIV = {x. p x \<noteq> x} \<union> {x. p x = x}\<close>
       
  1021     by auto
       
  1022   finally have \<open>bij_betw p {x. p x \<noteq> x} {x. p x \<noteq> x}\<close>
       
  1023     by (rule bij_betw_partition) (auto simp add: bij_betw_fixpoints)
       
  1024   then have \<open>p permutes {x. p x \<noteq> x}\<close>
       
  1025     by (auto intro: bij_imp_permutes)
       
  1026   with fin show thesis ..
       
  1027 qed
       
  1028 
       
  1029 lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
       
  1030   by (auto elim: permutation_permutesE intro: permutes_imp_permutation)
       
  1031 
       
  1032 
       
  1033 subsection \<open>Sign of a permutation as a real number\<close>
       
  1034 
       
  1035 definition sign :: \<open>('a \<Rightarrow> 'a) \<Rightarrow> int\<close> \<comment> \<open>TODO: prefer less generic name\<close>
       
  1036   where \<open>sign p = (if evenperm p then (1::int) else -1)\<close>
       
  1037 
       
  1038 lemma sign_nz: "sign p \<noteq> 0"
       
  1039   by (simp add: sign_def)
       
  1040 
       
  1041 lemma sign_id: "sign id = 1"
       
  1042   by (simp add: sign_def)
       
  1043 
       
  1044 lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p"
       
  1045   by (simp add: sign_def evenperm_inv)
       
  1046 
       
  1047 lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q"
       
  1048   by (simp add: sign_def evenperm_comp)
       
  1049 
       
  1050 lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
       
  1051   by (simp add: sign_def evenperm_swap)
       
  1052 
       
  1053 lemma sign_idempotent: "sign p * sign p = 1"
       
  1054   by (simp add: sign_def)
       
  1055 
       
  1056 
       
  1057 subsection \<open>Permuting a list\<close>
       
  1058 
       
  1059 text \<open>This function permutes a list by applying a permutation to the indices.\<close>
       
  1060 
       
  1061 definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list"
       
  1062   where "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
       
  1063 
       
  1064 lemma permute_list_map:
       
  1065   assumes "f permutes {..<length xs}"
       
  1066   shows "permute_list f (map g xs) = map g (permute_list f xs)"
       
  1067   using permutes_in_image[OF assms] by (auto simp: permute_list_def)
       
  1068 
       
  1069 lemma permute_list_nth:
       
  1070   assumes "f permutes {..<length xs}" "i < length xs"
       
  1071   shows "permute_list f xs ! i = xs ! f i"
       
  1072   using permutes_in_image[OF assms(1)] assms(2)
       
  1073   by (simp add: permute_list_def)
       
  1074 
       
  1075 lemma permute_list_Nil [simp]: "permute_list f [] = []"
       
  1076   by (simp add: permute_list_def)
       
  1077 
       
  1078 lemma length_permute_list [simp]: "length (permute_list f xs) = length xs"
       
  1079   by (simp add: permute_list_def)
       
  1080 
       
  1081 lemma permute_list_compose:
       
  1082   assumes "g permutes {..<length xs}"
       
  1083   shows "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
       
  1084   using assms[THEN permutes_in_image] by (auto simp add: permute_list_def)
       
  1085 
       
  1086 lemma permute_list_ident [simp]: "permute_list (\<lambda>x. x) xs = xs"
       
  1087   by (simp add: permute_list_def map_nth)
       
  1088 
       
  1089 lemma permute_list_id [simp]: "permute_list id xs = xs"
       
  1090   by (simp add: id_def)
       
  1091 
       
  1092 lemma mset_permute_list [simp]:
       
  1093   fixes xs :: "'a list"
       
  1094   assumes "f permutes {..<length xs}"
       
  1095   shows "mset (permute_list f xs) = mset xs"
       
  1096 proof (rule multiset_eqI)
       
  1097   fix y :: 'a
       
  1098   from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x
       
  1099     using permutes_in_image[OF assms] by auto
       
  1100   have "count (mset (permute_list f xs)) y = card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
       
  1101     by (simp add: permute_list_def count_image_mset atLeast0LessThan)
       
  1102   also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
       
  1103     by auto
       
  1104   also from assms have "card \<dots> = card {i. i < length xs \<and> y = xs ! i}"
       
  1105     by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj)
       
  1106   also have "\<dots> = count (mset xs) y"
       
  1107     by (simp add: count_mset length_filter_conv_card)
       
  1108   finally show "count (mset (permute_list f xs)) y = count (mset xs) y"
       
  1109     by simp
       
  1110 qed
       
  1111 
       
  1112 lemma set_permute_list [simp]:
       
  1113   assumes "f permutes {..<length xs}"
       
  1114   shows "set (permute_list f xs) = set xs"
       
  1115   by (rule mset_eq_setD[OF mset_permute_list]) fact
       
  1116 
       
  1117 lemma distinct_permute_list [simp]:
       
  1118   assumes "f permutes {..<length xs}"
       
  1119   shows "distinct (permute_list f xs) = distinct xs"
       
  1120   by (simp add: distinct_count_atmost_1 assms)
       
  1121 
       
  1122 lemma permute_list_zip:
       
  1123   assumes "f permutes A" "A = {..<length xs}"
       
  1124   assumes [simp]: "length xs = length ys"
       
  1125   shows "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
       
  1126 proof -
       
  1127   from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i
       
  1128     by simp
       
  1129   have "permute_list f (zip xs ys) = map (\<lambda>i. zip xs ys ! f i) [0..<length ys]"
       
  1130     by (simp_all add: permute_list_def zip_map_map)
       
  1131   also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])"
       
  1132     by (intro nth_equalityI) (simp_all add: *)
       
  1133   also have "\<dots> = zip (permute_list f xs) (permute_list f ys)"
       
  1134     by (simp_all add: permute_list_def zip_map_map)
       
  1135   finally show ?thesis .
       
  1136 qed
       
  1137 
       
  1138 lemma map_of_permute:
       
  1139   assumes "\<sigma> permutes fst ` set xs"
       
  1140   shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)"
       
  1141     (is "_ = map_of (map ?f _)")
       
  1142 proof
       
  1143   from assms have "inj \<sigma>" "surj \<sigma>"
       
  1144     by (simp_all add: permutes_inj permutes_surj)
       
  1145   then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x
       
  1146     by (induct xs) (auto simp: inv_f_f surj_f_inv_f)
       
  1147 qed
       
  1148 
       
  1149 
       
  1150 subsection \<open>More lemmas about permutations\<close>
       
  1151 
       
  1152 text \<open>The following few lemmas were contributed by Lukas Bulwahn.\<close>
       
  1153 
       
  1154 lemma count_image_mset_eq_card_vimage:
       
  1155   assumes "finite A"
       
  1156   shows "count (image_mset f (mset_set A)) b = card {a \<in> A. f a = b}"
       
  1157   using assms
       
  1158 proof (induct A)
       
  1159   case empty
       
  1160   show ?case by simp
       
  1161 next
       
  1162   case (insert x F)
       
  1163   show ?case
       
  1164   proof (cases "f x = b")
       
  1165     case True
       
  1166     with insert.hyps
       
  1167     have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
       
  1168       by auto
       
  1169     also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
       
  1170       by simp
       
  1171     also from \<open>f x = b\<close> have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
       
  1172       by (auto intro: arg_cong[where f="card"])
       
  1173     finally show ?thesis
       
  1174       using insert by auto
       
  1175   next
       
  1176     case False
       
  1177     then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
       
  1178       by auto
       
  1179     with insert False show ?thesis
       
  1180       by simp
       
  1181   qed
       
  1182 qed
       
  1183 
       
  1184 \<comment> \<open>Prove \<open>image_mset_eq_implies_permutes\<close> ...\<close>
       
  1185 lemma image_mset_eq_implies_permutes:
       
  1186   fixes f :: "'a \<Rightarrow> 'b"
       
  1187   assumes "finite A"
       
  1188     and mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)"
       
  1189   obtains p where "p permutes A" and "\<forall>x\<in>A. f x = f' (p x)"
       
  1190 proof -
       
  1191   from \<open>finite A\<close> have [simp]: "finite {a \<in> A. f a = (b::'b)}" for f b by auto
       
  1192   have "f ` A = f' ` A"
       
  1193   proof -
       
  1194     from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))"
       
  1195       by simp
       
  1196     also have "\<dots> = f' ` set_mset (mset_set A)"
       
  1197       by (metis mset_eq multiset.set_map)
       
  1198     also from \<open>finite A\<close> have "\<dots> = f' ` A"
       
  1199       by simp
       
  1200     finally show ?thesis .
       
  1201   qed
       
  1202   have "\<forall>b\<in>(f ` A). \<exists>p. bij_betw p {a \<in> A. f a = b} {a \<in> A. f' a = b}"
       
  1203   proof
       
  1204     fix b
       
  1205     from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b"
       
  1206       by simp
       
  1207     with \<open>finite A\<close> have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
       
  1208       by (simp add: count_image_mset_eq_card_vimage)
       
  1209     then show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
       
  1210       by (intro finite_same_card_bij) simp_all
       
  1211   qed
       
  1212   then have "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
       
  1213     by (rule bchoice)
       
  1214   then obtain p where p: "\<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}" ..
       
  1215   define p' where "p' = (\<lambda>a. if a \<in> A then p (f a) a else a)"
       
  1216   have "p' permutes A"
       
  1217   proof (rule bij_imp_permutes)
       
  1218     have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
       
  1219       by (auto simp: disjoint_family_on_def)
       
  1220     moreover
       
  1221     have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if "b \<in> f ` A" for b
       
  1222       using p that by (subst bij_betw_cong[where g="p b"]) auto
       
  1223     ultimately
       
  1224     have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
       
  1225       by (rule bij_betw_UNION_disjoint)
       
  1226     moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
       
  1227       by auto
       
  1228     moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
       
  1229       by auto
       
  1230     ultimately show "bij_betw p' A A"
       
  1231       unfolding p'_def by (subst bij_betw_cong[where g="(\<lambda>a. p (f a) a)"]) auto
       
  1232   next
       
  1233     show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x"
       
  1234       by (simp add: p'_def)
       
  1235   qed
       
  1236   moreover from p have "\<forall>x\<in>A. f x = f' (p' x)"
       
  1237     unfolding p'_def using bij_betwE by fastforce
       
  1238   ultimately show ?thesis ..
       
  1239 qed
       
  1240 
       
  1241 \<comment> \<open>... and derive the existing property:\<close>
       
  1242 lemma mset_eq_permutation:
       
  1243   fixes xs ys :: "'a list"
       
  1244   assumes mset_eq: "mset xs = mset ys"
       
  1245   obtains p where "p permutes {..<length ys}" "permute_list p ys = xs"
       
  1246 proof -
       
  1247   from mset_eq have length_eq: "length xs = length ys"
       
  1248     by (rule mset_eq_length)
       
  1249   have "mset_set {..<length ys} = mset [0..<length ys]"
       
  1250     by (rule mset_set_upto_eq_mset_upto)
       
  1251   with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
       
  1252     image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
       
  1253     by (metis map_nth mset_map)
       
  1254   from image_mset_eq_implies_permutes[OF _ this]
       
  1255   obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
       
  1256     by auto
       
  1257   with length_eq have "permute_list p ys = xs"
       
  1258     by (auto intro!: nth_equalityI simp: permute_list_nth)
       
  1259   with p show thesis ..
       
  1260 qed
       
  1261 
       
  1262 lemma permutes_natset_le:
       
  1263   fixes S :: "'a::wellorder set"
       
  1264   assumes "p permutes S"
       
  1265     and "\<forall>i \<in> S. p i \<le> i"
       
  1266   shows "p = id"
       
  1267 proof -
       
  1268   have "p n = n" for n
       
  1269     using assms
       
  1270   proof (induct n arbitrary: S rule: less_induct)
       
  1271     case (less n)
       
  1272     show ?case
       
  1273     proof (cases "n \<in> S")
       
  1274       case False
       
  1275       with less(2) show ?thesis
       
  1276         unfolding permutes_def by metis
       
  1277     next
       
  1278       case True
       
  1279       with less(3) have "p n < n \<or> p n = n"
       
  1280         by auto
       
  1281       then show ?thesis
       
  1282       proof
       
  1283         assume "p n < n"
       
  1284         with less have "p (p n) = p n"
       
  1285           by metis
       
  1286         with permutes_inj[OF less(2)] have "p n = n"
       
  1287           unfolding inj_def by blast
       
  1288         with \<open>p n < n\<close> have False
       
  1289           by simp
       
  1290         then show ?thesis ..
       
  1291       qed
       
  1292     qed
       
  1293   qed
       
  1294   then show ?thesis by (auto simp: fun_eq_iff)
       
  1295 qed
       
  1296 
       
  1297 lemma permutes_natset_ge:
       
  1298   fixes S :: "'a::wellorder set"
       
  1299   assumes p: "p permutes S"
       
  1300     and le: "\<forall>i \<in> S. p i \<ge> i"
       
  1301   shows "p = id"
       
  1302 proof -
       
  1303   have "i \<ge> inv p i" if "i \<in> S" for i
       
  1304   proof -
       
  1305     from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
       
  1306       by simp
       
  1307     with le have "p (inv p i) \<ge> inv p i"
       
  1308       by blast
       
  1309     with permutes_inverses[OF p] show ?thesis
       
  1310       by simp
       
  1311   qed
       
  1312   then have "\<forall>i\<in>S. inv p i \<le> i"
       
  1313     by blast
       
  1314   from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id"
       
  1315     by simp
       
  1316   then show ?thesis
       
  1317     apply (subst permutes_inv_inv[OF p, symmetric])
       
  1318     apply (rule inv_unique_comp)
       
  1319      apply simp_all
       
  1320     done
       
  1321 qed
       
  1322 
       
  1323 lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
       
  1324   apply (rule set_eqI)
       
  1325   apply auto
       
  1326   using permutes_inv_inv permutes_inv
       
  1327    apply auto
       
  1328   apply (rule_tac x="inv x" in exI)
       
  1329   apply auto
       
  1330   done
       
  1331 
       
  1332 lemma image_compose_permutations_left:
       
  1333   assumes "q permutes S"
       
  1334   shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
       
  1335   apply (rule set_eqI)
       
  1336   apply auto
       
  1337    apply (rule permutes_compose)
       
  1338   using assms
       
  1339     apply auto
       
  1340   apply (rule_tac x = "inv q \<circ> x" in exI)
       
  1341   apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
       
  1342   done
       
  1343 
       
  1344 lemma image_compose_permutations_right:
       
  1345   assumes "q permutes S"
       
  1346   shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
       
  1347   apply (rule set_eqI)
       
  1348   apply auto
       
  1349    apply (rule permutes_compose)
       
  1350   using assms
       
  1351     apply auto
       
  1352   apply (rule_tac x = "x \<circ> inv q" in exI)
       
  1353   apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
       
  1354   done
       
  1355 
       
  1356 lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
       
  1357   by (simp add: permutes_def) metis
       
  1358 
       
  1359 lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
       
  1360   (is "?lhs = ?rhs")
       
  1361 proof -
       
  1362   let ?S = "{p . p permutes S}"
       
  1363   have *: "inj_on inv ?S"
       
  1364   proof (auto simp add: inj_on_def)
       
  1365     fix q r
       
  1366     assume q: "q permutes S"
       
  1367       and r: "r permutes S"
       
  1368       and qr: "inv q = inv r"
       
  1369     then have "inv (inv q) = inv (inv r)"
       
  1370       by simp
       
  1371     with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
       
  1372       by metis
       
  1373   qed
       
  1374   have **: "inv ` ?S = ?S"
       
  1375     using image_inverse_permutations by blast
       
  1376   have ***: "?rhs = sum (f \<circ> inv) ?S"
       
  1377     by (simp add: o_def)
       
  1378   from sum.reindex[OF *, of f] show ?thesis
       
  1379     by (simp only: ** ***)
       
  1380 qed
       
  1381 
       
  1382 lemma setum_permutations_compose_left:
       
  1383   assumes q: "q permutes S"
       
  1384   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
       
  1385   (is "?lhs = ?rhs")
       
  1386 proof -
       
  1387   let ?S = "{p. p permutes S}"
       
  1388   have *: "?rhs = sum (f \<circ> ((\<circ>) q)) ?S"
       
  1389     by (simp add: o_def)
       
  1390   have **: "inj_on ((\<circ>) q) ?S"
       
  1391   proof (auto simp add: inj_on_def)
       
  1392     fix p r
       
  1393     assume "p permutes S"
       
  1394       and r: "r permutes S"
       
  1395       and rp: "q \<circ> p = q \<circ> r"
       
  1396     then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r"
       
  1397       by (simp add: comp_assoc)
       
  1398     with permutes_inj[OF q, unfolded inj_iff] show "p = r"
       
  1399       by simp
       
  1400   qed
       
  1401   have "((\<circ>) q) ` ?S = ?S"
       
  1402     using image_compose_permutations_left[OF q] by auto
       
  1403   with * sum.reindex[OF **, of f] show ?thesis
       
  1404     by (simp only:)
       
  1405 qed
       
  1406 
       
  1407 lemma sum_permutations_compose_right:
       
  1408   assumes q: "q permutes S"
       
  1409   shows "sum f {p. p permutes S} = sum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
       
  1410   (is "?lhs = ?rhs")
       
  1411 proof -
       
  1412   let ?S = "{p. p permutes S}"
       
  1413   have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
       
  1414     by (simp add: o_def)
       
  1415   have **: "inj_on (\<lambda>p. p \<circ> q) ?S"
       
  1416   proof (auto simp add: inj_on_def)
       
  1417     fix p r
       
  1418     assume "p permutes S"
       
  1419       and r: "r permutes S"
       
  1420       and rp: "p \<circ> q = r \<circ> q"
       
  1421     then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)"
       
  1422       by (simp add: o_assoc)
       
  1423     with permutes_surj[OF q, unfolded surj_iff] show "p = r"
       
  1424       by simp
       
  1425   qed
       
  1426   from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S"
       
  1427     by auto
       
  1428   with * sum.reindex[OF **, of f] show ?thesis
       
  1429     by (simp only:)
       
  1430 qed
       
  1431 
       
  1432 
       
  1433 subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close>
       
  1434 
       
  1435 lemma sum_over_permutations_insert:
       
  1436   assumes fS: "finite S"
       
  1437     and aS: "a \<notin> S"
       
  1438   shows "sum f {p. p permutes (insert a S)} =
       
  1439     sum (\<lambda>b. sum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
       
  1440 proof -
       
  1441   have *: "\<And>f a b. (\<lambda>(b, p). f (Fun.swap a b id \<circ> p)) = f \<circ> (\<lambda>(b,p). Fun.swap a b id \<circ> p)"
       
  1442     by (simp add: fun_eq_iff)
       
  1443   have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
       
  1444     by blast
       
  1445   show ?thesis
       
  1446     unfolding * ** sum.cartesian_product permutes_insert
       
  1447   proof (rule sum.reindex)
       
  1448     let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
       
  1449     let ?P = "{p. p permutes S}"
       
  1450     {
       
  1451       fix b c p q
       
  1452       assume b: "b \<in> insert a S"
       
  1453       assume c: "c \<in> insert a S"
       
  1454       assume p: "p permutes S"
       
  1455       assume q: "q permutes S"
       
  1456       assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q"
       
  1457       from p q aS have pa: "p a = a" and qa: "q a = a"
       
  1458         unfolding permutes_def by metis+
       
  1459       from eq have "(Fun.swap a b id \<circ> p) a  = (Fun.swap a c id \<circ> q) a"
       
  1460         by simp
       
  1461       then have bc: "b = c"
       
  1462         by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def
       
  1463             cong del: if_weak_cong split: if_split_asm)
       
  1464       from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
       
  1465         (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
       
  1466       then have "p = q"
       
  1467         unfolding o_assoc swap_id_idempotent by simp
       
  1468       with bc have "b = c \<and> p = q"
       
  1469         by blast
       
  1470     }
       
  1471     then show "inj_on ?f (insert a S \<times> ?P)"
       
  1472       unfolding inj_on_def by clarify metis
       
  1473   qed
       
  1474 qed
       
  1475 
       
  1476 
       
  1477 subsection \<open>Constructing permutations from association lists\<close>
       
  1478 
       
  1479 definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
       
  1480   where "list_permutes xs A \<longleftrightarrow>
       
  1481     set (map fst xs) \<subseteq> A \<and>
       
  1482     set (map snd xs) = set (map fst xs) \<and>
       
  1483     distinct (map fst xs) \<and>
       
  1484     distinct (map snd xs)"
       
  1485 
       
  1486 lemma list_permutesI [simp]:
       
  1487   assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)"
       
  1488   shows "list_permutes xs A"
       
  1489 proof -
       
  1490   from assms(2,3) have "distinct (map snd xs)"
       
  1491     by (intro card_distinct) (simp_all add: distinct_card del: set_map)
       
  1492   with assms show ?thesis
       
  1493     by (simp add: list_permutes_def)
       
  1494 qed
       
  1495 
       
  1496 definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
       
  1497   where "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)"
       
  1498 
       
  1499 lemma permutation_of_list_Cons:
       
  1500   "permutation_of_list ((x, y) # xs) x' = (if x = x' then y else permutation_of_list xs x')"
       
  1501   by (simp add: permutation_of_list_def)
       
  1502 
       
  1503 fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
       
  1504   where
       
  1505     "inverse_permutation_of_list [] x = x"
       
  1506   | "inverse_permutation_of_list ((y, x') # xs) x =
       
  1507       (if x = x' then y else inverse_permutation_of_list xs x)"
       
  1508 
       
  1509 declare inverse_permutation_of_list.simps [simp del]
       
  1510 
       
  1511 lemma inj_on_map_of:
       
  1512   assumes "distinct (map snd xs)"
       
  1513   shows "inj_on (map_of xs) (set (map fst xs))"
       
  1514 proof (rule inj_onI)
       
  1515   fix x y
       
  1516   assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
       
  1517   assume eq: "map_of xs x = map_of xs y"
       
  1518   from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
       
  1519     by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff)
       
  1520   moreover from x'y' have *: "(x, x') \<in> set xs" "(y, y') \<in> set xs"
       
  1521     by (force dest: map_of_SomeD)+
       
  1522   moreover from * eq x'y' have "x' = y'"
       
  1523     by simp
       
  1524   ultimately show "x = y"
       
  1525     using assms by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"])
       
  1526 qed
       
  1527 
       
  1528 lemma inj_on_the: "None \<notin> A \<Longrightarrow> inj_on the A"
       
  1529   by (auto simp: inj_on_def option.the_def split: option.splits)
       
  1530 
       
  1531 lemma inj_on_map_of':
       
  1532   assumes "distinct (map snd xs)"
       
  1533   shows "inj_on (the \<circ> map_of xs) (set (map fst xs))"
       
  1534   by (intro comp_inj_on inj_on_map_of assms inj_on_the)
       
  1535     (force simp: eq_commute[of None] map_of_eq_None_iff)
       
  1536 
       
  1537 lemma image_map_of:
       
  1538   assumes "distinct (map fst xs)"
       
  1539   shows "map_of xs ` set (map fst xs) = Some ` set (map snd xs)"
       
  1540   using assms by (auto simp: rev_image_eqI)
       
  1541 
       
  1542 lemma the_Some_image [simp]: "the ` Some ` A = A"
       
  1543   by (subst image_image) simp
       
  1544 
       
  1545 lemma image_map_of':
       
  1546   assumes "distinct (map fst xs)"
       
  1547   shows "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)"
       
  1548   by (simp only: image_comp [symmetric] image_map_of assms the_Some_image)
       
  1549 
       
  1550 lemma permutation_of_list_permutes [simp]:
       
  1551   assumes "list_permutes xs A"
       
  1552   shows "permutation_of_list xs permutes A"
       
  1553     (is "?f permutes _")
       
  1554 proof (rule permutes_subset[OF bij_imp_permutes])
       
  1555   from assms show "set (map fst xs) \<subseteq> A"
       
  1556     by (simp add: list_permutes_def)
       
  1557   from assms have "inj_on (the \<circ> map_of xs) (set (map fst xs))" (is ?P)
       
  1558     by (intro inj_on_map_of') (simp_all add: list_permutes_def)
       
  1559   also have "?P \<longleftrightarrow> inj_on ?f (set (map fst xs))"
       
  1560     by (intro inj_on_cong)
       
  1561       (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
       
  1562   finally have "bij_betw ?f (set (map fst xs)) (?f ` set (map fst xs))"
       
  1563     by (rule inj_on_imp_bij_betw)
       
  1564   also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)"
       
  1565     by (intro image_cong refl)
       
  1566       (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
       
  1567   also from assms have "\<dots> = set (map fst xs)"
       
  1568     by (subst image_map_of') (simp_all add: list_permutes_def)
       
  1569   finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" .
       
  1570 qed (force simp: permutation_of_list_def dest!: map_of_SomeD split: option.splits)+
       
  1571 
       
  1572 lemma eval_permutation_of_list [simp]:
       
  1573   "permutation_of_list [] x = x"
       
  1574   "x = x' \<Longrightarrow> permutation_of_list ((x',y)#xs) x = y"
       
  1575   "x \<noteq> x' \<Longrightarrow> permutation_of_list ((x',y')#xs) x = permutation_of_list xs x"
       
  1576   by (simp_all add: permutation_of_list_def)
       
  1577 
       
  1578 lemma eval_inverse_permutation_of_list [simp]:
       
  1579   "inverse_permutation_of_list [] x = x"
       
  1580   "x = x' \<Longrightarrow> inverse_permutation_of_list ((y,x')#xs) x = y"
       
  1581   "x \<noteq> x' \<Longrightarrow> inverse_permutation_of_list ((y',x')#xs) x = inverse_permutation_of_list xs x"
       
  1582   by (simp_all add: inverse_permutation_of_list.simps)
       
  1583 
       
  1584 lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x"
       
  1585   by (induct xs) (auto simp: permutation_of_list_Cons)
       
  1586 
       
  1587 lemma permutation_of_list_unique':
       
  1588   "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
       
  1589   by (induct xs) (force simp: permutation_of_list_Cons)+
       
  1590 
       
  1591 lemma permutation_of_list_unique:
       
  1592   "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
       
  1593   by (intro permutation_of_list_unique') (simp_all add: list_permutes_def)
       
  1594 
       
  1595 lemma inverse_permutation_of_list_id:
       
  1596   "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x"
       
  1597   by (induct xs) auto
       
  1598 
       
  1599 lemma inverse_permutation_of_list_unique':
       
  1600   "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
       
  1601   by (induct xs) (force simp: inverse_permutation_of_list.simps(2))+
       
  1602 
       
  1603 lemma inverse_permutation_of_list_unique:
       
  1604   "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
       
  1605   by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def)
       
  1606 
       
  1607 lemma inverse_permutation_of_list_correct:
       
  1608   fixes A :: "'a set"
       
  1609   assumes "list_permutes xs A"
       
  1610   shows "inverse_permutation_of_list xs = inv (permutation_of_list xs)"
       
  1611 proof (rule ext, rule sym, subst permutes_inv_eq)
       
  1612   from assms show "permutation_of_list xs permutes A"
       
  1613     by simp
       
  1614   show "permutation_of_list xs (inverse_permutation_of_list xs x) = x" for x
       
  1615   proof (cases "x \<in> set (map snd xs)")
       
  1616     case True
       
  1617     then obtain y where "(y, x) \<in> set xs" by auto
       
  1618     with assms show ?thesis
       
  1619       by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique)
       
  1620   next
       
  1621     case False
       
  1622     with assms show ?thesis
       
  1623       by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id)
       
  1624   qed
       
  1625 qed
       
  1626 
       
  1627 end