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