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