src/HOL/Library/Permutations.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61424 c3658c18b7bc
child 62390 842917225d56
permissions -rw-r--r--
more symbols;
     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
     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 
    26 subsection \<open>Basic consequences of the definition\<close>
    27 
    28 definition permutes  (infixr "permutes" 41)
    29   where "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)"
    30 
    31 lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
    32   unfolding permutes_def by metis
    33 
    34 lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S"
    35   unfolding permutes_def
    36   apply (rule set_eqI)
    37   apply (simp add: image_iff)
    38   apply metis
    39   done
    40 
    41 lemma permutes_inj: "p permutes S \<Longrightarrow> inj p"
    42   unfolding permutes_def inj_on_def by blast
    43 
    44 lemma permutes_surj: "p permutes s \<Longrightarrow> surj p"
    45   unfolding permutes_def surj_def by metis
    46 
    47 lemma permutes_bij: "p permutes s \<Longrightarrow> bij p"
    48 unfolding bij_def by (metis permutes_inj permutes_surj)
    49 
    50 lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S"
    51 by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI)
    52 
    53 lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S"
    54   unfolding permutes_def bij_betw_def inj_on_def
    55   by auto (metis image_iff)+
    56 
    57 lemma permutes_inv_o:
    58   assumes pS: "p permutes S"
    59   shows "p \<circ> inv p = id"
    60     and "inv p \<circ> p = id"
    61   using permutes_inj[OF pS] permutes_surj[OF pS]
    62   unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+
    63 
    64 lemma permutes_inverses:
    65   fixes p :: "'a \<Rightarrow> 'a"
    66   assumes pS: "p permutes S"
    67   shows "p (inv p x) = x"
    68     and "inv p (p x) = x"
    69   using permutes_inv_o[OF pS, unfolded fun_eq_iff o_def] by auto
    70 
    71 lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T"
    72   unfolding permutes_def by blast
    73 
    74 lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
    75   unfolding fun_eq_iff permutes_def by simp metis
    76 
    77 lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
    78   unfolding fun_eq_iff permutes_def by simp metis
    79 
    80 lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
    81   unfolding permutes_def by simp
    82 
    83 lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y"
    84   unfolding permutes_def inv_def
    85   apply auto
    86   apply (erule allE[where x=y])
    87   apply (erule allE[where x=y])
    88   apply (rule someI_ex)
    89   apply blast
    90   apply (rule some1_equality)
    91   apply blast
    92   apply blast
    93   done
    94 
    95 lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S"
    96   unfolding permutes_def Fun.swap_def fun_upd_def by auto metis
    97 
    98 lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T"
    99   by (simp add: Ball_def permutes_def) metis
   100 
   101 
   102 subsection \<open>Group properties\<close>
   103 
   104 lemma permutes_id: "id permutes S"
   105   unfolding permutes_def by simp
   106 
   107 lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S"
   108   unfolding permutes_def o_def by metis
   109 
   110 lemma permutes_inv:
   111   assumes pS: "p permutes S"
   112   shows "inv p permutes S"
   113   using pS unfolding permutes_def permutes_inv_eq[OF pS] by metis
   114 
   115 lemma permutes_inv_inv:
   116   assumes pS: "p permutes S"
   117   shows "inv (inv p) = p"
   118   unfolding fun_eq_iff permutes_inv_eq[OF pS] permutes_inv_eq[OF permutes_inv[OF pS]]
   119   by blast
   120 
   121 
   122 subsection \<open>The number of permutations on a finite set\<close>
   123 
   124 lemma permutes_insert_lemma:
   125   assumes pS: "p permutes (insert a S)"
   126   shows "Fun.swap a (p a) id \<circ> p permutes S"
   127   apply (rule permutes_superset[where S = "insert a S"])
   128   apply (rule permutes_compose[OF pS])
   129   apply (rule permutes_swap_id, simp)
   130   using permutes_in_image[OF pS, of a]
   131   apply simp
   132   apply (auto simp add: Ball_def Fun.swap_def)
   133   done
   134 
   135 lemma permutes_insert: "{p. p permutes (insert a S)} =
   136   (\<lambda>(b,p). Fun.swap a b id \<circ> p) ` {(b,p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
   137 proof -
   138   {
   139     fix p
   140     {
   141       assume pS: "p permutes insert a S"
   142       let ?b = "p a"
   143       let ?q = "Fun.swap a (p a) id \<circ> p"
   144       have th0: "p = Fun.swap a ?b id \<circ> ?q"
   145         unfolding fun_eq_iff o_assoc by simp
   146       have th1: "?b \<in> insert a S"
   147         unfolding permutes_in_image[OF pS] by simp
   148       from permutes_insert_lemma[OF pS] th0 th1
   149       have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S" by blast
   150     }
   151     moreover
   152     {
   153       fix b q
   154       assume bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S"
   155       from permutes_subset[OF bq(3), of "insert a S"]
   156       have qS: "q permutes insert a S"
   157         by auto
   158       have aS: "a \<in> insert a S"
   159         by simp
   160       from bq(1) permutes_compose[OF qS permutes_swap_id[OF aS bq(2)]]
   161       have "p permutes insert a S"
   162         by simp
   163     }
   164     ultimately have "p permutes insert a S \<longleftrightarrow>
   165         (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)"
   166       by blast
   167   }
   168   then show ?thesis
   169     by auto
   170 qed
   171 
   172 lemma card_permutations:
   173   assumes Sn: "card S = n"
   174     and fS: "finite S"
   175   shows "card {p. p permutes S} = fact n"
   176   using fS Sn
   177 proof (induct arbitrary: n)
   178   case empty
   179   then show ?case by simp
   180 next
   181   case (insert x F)
   182   {
   183     fix n
   184     assume H0: "card (insert x F) = n"
   185     let ?xF = "{p. p permutes insert x F}"
   186     let ?pF = "{p. p permutes F}"
   187     let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
   188     let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
   189     from permutes_insert[of x F]
   190     have xfgpF': "?xF = ?g ` ?pF'" .
   191     have Fs: "card F = n - 1"
   192       using \<open>x \<notin> F\<close> H0 \<open>finite F\<close> by auto
   193     from insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
   194       using \<open>finite F\<close> by auto
   195     then have "finite ?pF"
   196       by (auto intro: card_ge_0_finite)
   197     then have pF'f: "finite ?pF'"
   198       using H0 \<open>finite F\<close>
   199       apply (simp only: Collect_case_prod Collect_mem_eq)
   200       apply (rule finite_cartesian_product)
   201       apply simp_all
   202       done
   203 
   204     have ginj: "inj_on ?g ?pF'"
   205     proof -
   206       {
   207         fix b p c q
   208         assume bp: "(b,p) \<in> ?pF'"
   209         assume cq: "(c,q) \<in> ?pF'"
   210         assume eq: "?g (b,p) = ?g (c,q)"
   211         from bp cq have ths: "b \<in> insert x F" "c \<in> insert x F" "x \<in> insert x F"
   212           "p permutes F" "q permutes F"
   213           by auto
   214         from ths(4) \<open>x \<notin> F\<close> eq have "b = ?g (b,p) x"
   215           unfolding permutes_def
   216           by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff)
   217         also have "\<dots> = ?g (c,q) x"
   218           using ths(5) \<open>x \<notin> F\<close> eq
   219           by (auto simp add: swap_def fun_upd_def fun_eq_iff)
   220         also have "\<dots> = c"
   221           using ths(5) \<open>x \<notin> F\<close>
   222           unfolding permutes_def
   223           by (auto simp add: Fun.swap_def fun_upd_def fun_eq_iff)
   224         finally have bc: "b = c" .
   225         then have "Fun.swap x b id = Fun.swap x c id"
   226           by simp
   227         with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q"
   228           by simp
   229         then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) =
   230           Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
   231           by simp
   232         then have "p = q"
   233           by (simp add: o_assoc)
   234         with bc have "(b, p) = (c, q)"
   235           by simp
   236       }
   237       then show ?thesis
   238         unfolding inj_on_def by blast
   239     qed
   240     from \<open>x \<notin> F\<close> H0 have n0: "n \<noteq> 0"
   241       using \<open>finite F\<close> by auto
   242     then have "\<exists>m. n = Suc m"
   243       by presburger
   244     then obtain m where n[simp]: "n = Suc m"
   245       by blast
   246     from pFs H0 have xFc: "card ?xF = fact n"
   247       unfolding xfgpF' card_image[OF ginj]
   248       using \<open>finite F\<close> \<open>finite ?pF\<close>
   249       apply (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product)
   250       apply simp
   251       done
   252     from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF"
   253       unfolding xfgpF' by simp
   254     have "card ?xF = fact n"
   255       using xFf xFc unfolding xFf by blast
   256   }
   257   then show ?case
   258     using insert by simp
   259 qed
   260 
   261 lemma finite_permutations:
   262   assumes fS: "finite S"
   263   shows "finite {p. p permutes S}"
   264   using card_permutations[OF refl fS] 
   265   by (auto intro: card_ge_0_finite)
   266 
   267 
   268 subsection \<open>Permutations of index set for iterated operations\<close>
   269 
   270 lemma (in comm_monoid_set) permute:
   271   assumes "p permutes S"
   272   shows "F g S = F (g \<circ> p) S"
   273 proof -
   274   from \<open>p permutes S\<close> have "inj p"
   275     by (rule permutes_inj)
   276   then have "inj_on p S"
   277     by (auto intro: subset_inj_on)
   278   then have "F g (p ` S) = F (g \<circ> p) S"
   279     by (rule reindex)
   280   moreover from \<open>p permutes S\<close> have "p ` S = S"
   281     by (rule permutes_image)
   282   ultimately show ?thesis
   283     by simp
   284 qed
   285 
   286 
   287 subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close>
   288 
   289 lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>
   290   Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id"
   291   by (simp add: fun_eq_iff Fun.swap_def)
   292 
   293 lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow>
   294   Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id"
   295   by (simp add: fun_eq_iff Fun.swap_def)
   296 
   297 lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow>
   298   Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id"
   299   by (simp add: fun_eq_iff Fun.swap_def)
   300 
   301 
   302 subsection \<open>Permutations as transposition sequences\<close>
   303 
   304 inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
   305 where
   306   id[simp]: "swapidseq 0 id"
   307 | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
   308 
   309 declare id[unfolded id_def, simp]
   310 
   311 definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
   312 
   313 
   314 subsection \<open>Some closure properties of the set of permutations, with lengths\<close>
   315 
   316 lemma permutation_id[simp]: "permutation id"
   317   unfolding permutation_def by (rule exI[where x=0]) simp
   318 
   319 declare permutation_id[unfolded id_def, simp]
   320 
   321 lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
   322   apply clarsimp
   323   using comp_Suc[of 0 id a b]
   324   apply simp
   325   done
   326 
   327 lemma permutation_swap_id: "permutation (Fun.swap a b id)"
   328   apply (cases "a = b")
   329   apply simp_all
   330   unfolding permutation_def
   331   using swapidseq_swap[of a b]
   332   apply blast
   333   done
   334 
   335 lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
   336 proof (induct n p arbitrary: m q rule: swapidseq.induct)
   337   case (id m q)
   338   then show ?case by simp
   339 next
   340   case (comp_Suc n p a b m q)
   341   have th: "Suc n + m = Suc (n + m)"
   342     by arith
   343   show ?case
   344     unfolding th comp_assoc
   345     apply (rule swapidseq.comp_Suc)
   346     using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
   347     apply blast+
   348     done
   349 qed
   350 
   351 lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)"
   352   unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
   353 
   354 lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
   355   apply (induct n p rule: swapidseq.induct)
   356   using swapidseq_swap[of a b]
   357   apply (auto simp add: comp_assoc intro: swapidseq.comp_Suc)
   358   done
   359 
   360 lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id"
   361 proof (induct n p rule: swapidseq.induct)
   362   case id
   363   then show ?case
   364     by (rule exI[where x=id]) simp
   365 next
   366   case (comp_Suc n p a b)
   367   from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   368     by blast
   369   let ?q = "q \<circ> Fun.swap a b id"
   370   note H = comp_Suc.hyps
   371   from swapidseq_swap[of a b] H(3) have th0: "swapidseq 1 (Fun.swap a b id)"
   372     by simp
   373   from swapidseq_comp_add[OF q(1) th0] have th1: "swapidseq (Suc n) ?q"
   374     by simp
   375   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"
   376     by (simp add: o_assoc)
   377   also have "\<dots> = id"
   378     by (simp add: q(2))
   379   finally have th2: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
   380   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"
   381     by (simp only: o_assoc)
   382   then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
   383     by (simp add: q(3))
   384   with th1 th2 show ?case
   385     by blast
   386 qed
   387 
   388 lemma swapidseq_inverse:
   389   assumes H: "swapidseq n p"
   390   shows "swapidseq n (inv p)"
   391   using swapidseq_inverse_exists[OF H] inv_unique_comp[of p] by auto
   392 
   393 lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
   394   using permutation_def swapidseq_inverse by blast
   395 
   396 
   397 subsection \<open>The identity map only has even transposition sequences\<close>
   398 
   399 lemma symmetry_lemma:
   400   assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c"
   401     and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
   402       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>
   403       P a b c d"
   404   shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>  P a b c d"
   405   using assms by metis
   406 
   407 lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
   408   Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
   409   (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
   410     Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)"
   411 proof -
   412   assume H: "a \<noteq> b" "c \<noteq> d"
   413   have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
   414     (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
   415       (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
   416         Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
   417     apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
   418     apply (simp_all only: swap_commute)
   419     apply (case_tac "a = c \<and> b = d")
   420     apply (clarsimp simp only: swap_commute swap_id_idempotent)
   421     apply (case_tac "a = c \<and> b \<noteq> d")
   422     apply (rule disjI2)
   423     apply (rule_tac x="b" in exI)
   424     apply (rule_tac x="d" in exI)
   425     apply (rule_tac x="b" in exI)
   426     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   427     apply (case_tac "a \<noteq> c \<and> b = d")
   428     apply (rule disjI2)
   429     apply (rule_tac x="c" in exI)
   430     apply (rule_tac x="d" in exI)
   431     apply (rule_tac x="c" in exI)
   432     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   433     apply (rule disjI2)
   434     apply (rule_tac x="c" in exI)
   435     apply (rule_tac x="d" in exI)
   436     apply (rule_tac x="b" in exI)
   437     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
   438     done
   439   with H show ?thesis by metis
   440 qed
   441 
   442 lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
   443   using swapidseq.cases[of 0 p "p = id"]
   444   by auto
   445 
   446 lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
   447   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)"
   448   apply (rule iffI)
   449   apply (erule swapidseq.cases[of n p])
   450   apply simp
   451   apply (rule disjI2)
   452   apply (rule_tac x= "a" in exI)
   453   apply (rule_tac x= "b" in exI)
   454   apply (rule_tac x= "pa" in exI)
   455   apply (rule_tac x= "na" in exI)
   456   apply simp
   457   apply auto
   458   apply (rule comp_Suc, simp_all)
   459   done
   460 
   461 lemma fixing_swapidseq_decrease:
   462   assumes spn: "swapidseq n p"
   463     and ab: "a \<noteq> b"
   464     and pa: "(Fun.swap a b id \<circ> p) a = a"
   465   shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
   466   using spn ab pa
   467 proof (induct n arbitrary: p a b)
   468   case 0
   469   then show ?case
   470     by (auto simp add: Fun.swap_def fun_upd_def)
   471 next
   472   case (Suc n p a b)
   473   from Suc.prems(1) swapidseq_cases[of "Suc n" p]
   474   obtain c d q m where
   475     cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
   476     by auto
   477   {
   478     assume H: "Fun.swap a b id \<circ> Fun.swap c d id = id"
   479     have ?case by (simp only: cdqm o_assoc H) (simp add: cdqm)
   480   }
   481   moreover
   482   {
   483     fix x y z
   484     assume H: "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
   485       "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
   486     from H have az: "a \<noteq> z"
   487       by simp
   488 
   489     {
   490       fix h
   491       have "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a"
   492         using H by (simp add: Fun.swap_def)
   493     }
   494     note th3 = this
   495     from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)"
   496       by simp
   497     then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
   498       by (simp add: o_assoc H)
   499     then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a"
   500       by simp
   501     then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
   502       unfolding Suc by metis
   503     then have th1: "(Fun.swap a z id \<circ> q) a = a"
   504       unfolding th3 .
   505     from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az th1]
   506     have th2: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
   507       by blast+
   508     have th: "Suc n - 1 = Suc (n - 1)"
   509       using th2(2) by auto
   510     have ?case
   511       unfolding cdqm(2) H o_assoc th
   512       apply (simp only: Suc_not_Zero simp_thms comp_assoc)
   513       apply (rule comp_Suc)
   514       using th2 H
   515       apply blast+
   516       done
   517   }
   518   ultimately show ?case
   519     using swap_general[OF Suc.prems(2) cdqm(4)] by metis
   520 qed
   521 
   522 lemma swapidseq_identity_even:
   523   assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)"
   524   shows "even n"
   525   using \<open>swapidseq n id\<close>
   526 proof (induct n rule: nat_less_induct)
   527   fix n
   528   assume H: "\<forall>m<n. swapidseq m (id::'a \<Rightarrow> 'a) \<longrightarrow> even m" "swapidseq n (id :: 'a \<Rightarrow> 'a)"
   529   {
   530     assume "n = 0"
   531     then have "even n" by presburger
   532   }
   533   moreover
   534   {
   535     fix a b :: 'a and q m
   536     assume h: "n = Suc m" "(id :: 'a \<Rightarrow> 'a) = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
   537     from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
   538     have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
   539       by auto
   540     from h m have mn: "m - 1 < n"
   541       by arith
   542     from H(1)[rule_format, OF mn m(2)] h(1) m(1) have "even n"
   543       by presburger
   544   }
   545   ultimately show "even n"
   546     using H(2)[unfolded swapidseq_cases[of n id]] by auto
   547 qed
   548 
   549 
   550 subsection \<open>Therefore we have a welldefined notion of parity\<close>
   551 
   552 definition "evenperm p = even (SOME n. swapidseq n p)"
   553 
   554 lemma swapidseq_even_even:
   555   assumes m: "swapidseq m p"
   556     and n: "swapidseq n p"
   557   shows "even m \<longleftrightarrow> even n"
   558 proof -
   559   from swapidseq_inverse_exists[OF n]
   560   obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   561     by blast
   562   from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]]
   563   show ?thesis
   564     by arith
   565 qed
   566 
   567 lemma evenperm_unique:
   568   assumes p: "swapidseq n p"
   569     and n:"even n = b"
   570   shows "evenperm p = b"
   571   unfolding n[symmetric] evenperm_def
   572   apply (rule swapidseq_even_even[where p = p])
   573   apply (rule someI[where x = n])
   574   using p
   575   apply blast+
   576   done
   577 
   578 
   579 subsection \<open>And it has the expected composition properties\<close>
   580 
   581 lemma evenperm_id[simp]: "evenperm id = True"
   582   by (rule evenperm_unique[where n = 0]) simp_all
   583 
   584 lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
   585   by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
   586 
   587 lemma evenperm_comp:
   588   assumes p: "permutation p"
   589     and q:"permutation q"
   590   shows "evenperm (p \<circ> q) = (evenperm p = evenperm q)"
   591 proof -
   592   from p q obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
   593     unfolding permutation_def by blast
   594   note nm =  swapidseq_comp_add[OF n m]
   595   have th: "even (n + m) = (even n \<longleftrightarrow> even m)"
   596     by arith
   597   from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
   598     evenperm_unique[OF nm th]
   599   show ?thesis
   600     by blast
   601 qed
   602 
   603 lemma evenperm_inv:
   604   assumes p: "permutation p"
   605   shows "evenperm (inv p) = evenperm p"
   606 proof -
   607   from p obtain n where n: "swapidseq n p"
   608     unfolding permutation_def by blast
   609   from evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]]
   610   show ?thesis .
   611 qed
   612 
   613 
   614 subsection \<open>A more abstract characterization of permutations\<close>
   615 
   616 lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
   617   unfolding bij_def inj_on_def surj_def
   618   apply auto
   619   apply metis
   620   apply metis
   621   done
   622 
   623 lemma permutation_bijective:
   624   assumes p: "permutation p"
   625   shows "bij p"
   626 proof -
   627   from p obtain n where n: "swapidseq n p"
   628     unfolding permutation_def by blast
   629   from swapidseq_inverse_exists[OF n]
   630   obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
   631     by blast
   632   then show ?thesis unfolding bij_iff
   633     apply (auto simp add: fun_eq_iff)
   634     apply metis
   635     done
   636 qed
   637 
   638 lemma permutation_finite_support:
   639   assumes p: "permutation p"
   640   shows "finite {x. p x \<noteq> x}"
   641 proof -
   642   from p obtain n where n: "swapidseq n p"
   643     unfolding permutation_def by blast
   644   from n show ?thesis
   645   proof (induct n p rule: swapidseq.induct)
   646     case id
   647     then show ?case by simp
   648   next
   649     case (comp_Suc n p a b)
   650     let ?S = "insert a (insert b {x. p x \<noteq> x})"
   651     from comp_Suc.hyps(2) have fS: "finite ?S"
   652       by simp
   653     from \<open>a \<noteq> b\<close> have th: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
   654       by (auto simp add: Fun.swap_def)
   655     from finite_subset[OF th fS] show ?case  .
   656   qed
   657 qed
   658 
   659 lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
   660   using surj_f_inv_f[of p] by (auto simp add: bij_def)
   661 
   662 lemma bij_swap_comp:
   663   assumes bp: "bij p"
   664   shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
   665   using surj_f_inv_f[OF bij_is_surj[OF bp]]
   666   by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp])
   667 
   668 lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)"
   669 proof -
   670   assume H: "bij p"
   671   show ?thesis
   672     unfolding bij_swap_comp[OF H] bij_swap_iff
   673     using H .
   674 qed
   675 
   676 lemma permutation_lemma:
   677   assumes fS: "finite S"
   678     and p: "bij p"
   679     and pS: "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
   680   shows "permutation p"
   681   using fS p pS
   682 proof (induct S arbitrary: p rule: finite_induct)
   683   case (empty p)
   684   then show ?case by simp
   685 next
   686   case (insert a F p)
   687   let ?r = "Fun.swap a (p a) id \<circ> p"
   688   let ?q = "Fun.swap a (p a) id \<circ> ?r"
   689   have raa: "?r a = a"
   690     by (simp add: Fun.swap_def)
   691   from bij_swap_ompose_bij[OF insert(4)]
   692   have br: "bij ?r"  .
   693 
   694   from insert raa have th: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
   695     apply (clarsimp simp add: Fun.swap_def)
   696     apply (erule_tac x="x" in allE)
   697     apply auto
   698     unfolding bij_iff
   699     apply metis
   700     done
   701   from insert(3)[OF br th]
   702   have rp: "permutation ?r" .
   703   have "permutation ?q"
   704     by (simp add: permutation_compose permutation_swap_id rp)
   705   then show ?case
   706     by (simp add: o_assoc)
   707 qed
   708 
   709 lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
   710   (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
   711 proof
   712   assume p: ?lhs
   713   from p permutation_bijective permutation_finite_support show "?b \<and> ?f"
   714     by auto
   715 next
   716   assume "?b \<and> ?f"
   717   then have "?f" "?b" by blast+
   718   from permutation_lemma[OF this] show ?lhs
   719     by blast
   720 qed
   721 
   722 lemma permutation_inverse_works:
   723   assumes p: "permutation p"
   724   shows "inv p \<circ> p = id"
   725     and "p \<circ> inv p = id"
   726   using permutation_bijective [OF p]
   727   unfolding bij_def inj_iff surj_iff by auto
   728 
   729 lemma permutation_inverse_compose:
   730   assumes p: "permutation p"
   731     and q: "permutation q"
   732   shows "inv (p \<circ> q) = inv q \<circ> inv p"
   733 proof -
   734   note ps = permutation_inverse_works[OF p]
   735   note qs = permutation_inverse_works[OF q]
   736   have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p"
   737     by (simp add: o_assoc)
   738   also have "\<dots> = id"
   739     by (simp add: ps qs)
   740   finally have th0: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
   741   have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
   742     by (simp add: o_assoc)
   743   also have "\<dots> = id"
   744     by (simp add: ps qs)
   745   finally have th1: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
   746   from inv_unique_comp[OF th0 th1] show ?thesis .
   747 qed
   748 
   749 
   750 subsection \<open>Relation to "permutes"\<close>
   751 
   752 lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
   753   unfolding permutation permutes_def bij_iff[symmetric]
   754   apply (rule iffI, clarify)
   755   apply (rule exI[where x="{x. p x \<noteq> x}"])
   756   apply simp
   757   apply clarsimp
   758   apply (rule_tac B="S" in finite_subset)
   759   apply auto
   760   done
   761 
   762 
   763 subsection \<open>Hence a sort of induction principle composing by swaps\<close>
   764 
   765 lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow>
   766   (\<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>
   767   (\<And>p. p permutes S \<Longrightarrow> P p)"
   768 proof (induct S rule: finite_induct)
   769   case empty
   770   then show ?case by auto
   771 next
   772   case (insert x F p)
   773   let ?r = "Fun.swap x (p x) id \<circ> p"
   774   let ?q = "Fun.swap x (p x) id \<circ> ?r"
   775   have qp: "?q = p"
   776     by (simp add: o_assoc)
   777   from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r"
   778     by blast
   779   from permutes_in_image[OF insert.prems(3), of x]
   780   have pxF: "p x \<in> insert x F"
   781     by simp
   782   have xF: "x \<in> insert x F"
   783     by simp
   784   have rp: "permutation ?r"
   785     unfolding permutation_permutes using insert.hyps(1)
   786       permutes_insert_lemma[OF insert.prems(3)]
   787     by blast
   788   from insert.prems(2)[OF xF pxF Pr Pr rp]
   789   show ?case
   790     unfolding qp .
   791 qed
   792 
   793 
   794 subsection \<open>Sign of a permutation as a real number\<close>
   795 
   796 definition "sign p = (if evenperm p then (1::int) else -1)"
   797 
   798 lemma sign_nz: "sign p \<noteq> 0"
   799   by (simp add: sign_def)
   800 
   801 lemma sign_id: "sign id = 1"
   802   by (simp add: sign_def)
   803 
   804 lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p"
   805   by (simp add: sign_def evenperm_inv)
   806 
   807 lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q"
   808   by (simp add: sign_def evenperm_comp)
   809 
   810 lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
   811   by (simp add: sign_def evenperm_swap)
   812 
   813 lemma sign_idempotent: "sign p * sign p = 1"
   814   by (simp add: sign_def)
   815 
   816 
   817 subsection \<open>More lemmas about permutations\<close>
   818 
   819 lemma permutes_natset_le:
   820   fixes S :: "'a::wellorder set"
   821   assumes p: "p permutes S"
   822     and le: "\<forall>i \<in> S. p i \<le> i"
   823   shows "p = id"
   824 proof -
   825   {
   826     fix n
   827     have "p n = n"
   828       using p le
   829     proof (induct n arbitrary: S rule: less_induct)
   830       fix n S
   831       assume H:
   832         "\<And>m S. m < n \<Longrightarrow> p permutes S \<Longrightarrow> \<forall>i\<in>S. p i \<le> i \<Longrightarrow> p m = m"
   833         "p permutes S" "\<forall>i \<in>S. p i \<le> i"
   834       {
   835         assume "n \<notin> S"
   836         with H(2) have "p n = n"
   837           unfolding permutes_def by metis
   838       }
   839       moreover
   840       {
   841         assume ns: "n \<in> S"
   842         from H(3)  ns have "p n < n \<or> p n = n"
   843           by auto
   844         moreover {
   845           assume h: "p n < n"
   846           from H h have "p (p n) = p n"
   847             by metis
   848           with permutes_inj[OF H(2)] have "p n = n"
   849             unfolding inj_on_def by blast
   850           with h have False
   851             by simp
   852         }
   853         ultimately have "p n = n"
   854           by blast
   855       }
   856       ultimately show "p n = n"
   857         by blast
   858     qed
   859   }
   860   then show ?thesis
   861     by (auto simp add: fun_eq_iff)
   862 qed
   863 
   864 lemma permutes_natset_ge:
   865   fixes S :: "'a::wellorder set"
   866   assumes p: "p permutes S"
   867     and le: "\<forall>i \<in> S. p i \<ge> i"
   868   shows "p = id"
   869 proof -
   870   {
   871     fix i
   872     assume i: "i \<in> S"
   873     from i permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
   874       by simp
   875     with le have "p (inv p i) \<ge> inv p i"
   876       by blast
   877     with permutes_inverses[OF p] have "i \<ge> inv p i"
   878       by simp
   879   }
   880   then have th: "\<forall>i\<in>S. inv p i \<le> i"
   881     by blast
   882   from permutes_natset_le[OF permutes_inv[OF p] th]
   883   have "inv p = inv id"
   884     by simp
   885   then show ?thesis
   886     apply (subst permutes_inv_inv[OF p, symmetric])
   887     apply (rule inv_unique_comp)
   888     apply simp_all
   889     done
   890 qed
   891 
   892 lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
   893   apply (rule set_eqI)
   894   apply auto
   895   using permutes_inv_inv permutes_inv
   896   apply auto
   897   apply (rule_tac x="inv x" in exI)
   898   apply auto
   899   done
   900 
   901 lemma image_compose_permutations_left:
   902   assumes q: "q permutes S"
   903   shows "{q \<circ> p | p. p permutes S} = {p . p permutes S}"
   904   apply (rule set_eqI)
   905   apply auto
   906   apply (rule permutes_compose)
   907   using q
   908   apply auto
   909   apply (rule_tac x = "inv q \<circ> x" in exI)
   910   apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
   911   done
   912 
   913 lemma image_compose_permutations_right:
   914   assumes q: "q permutes S"
   915   shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
   916   apply (rule set_eqI)
   917   apply auto
   918   apply (rule permutes_compose)
   919   using q
   920   apply auto
   921   apply (rule_tac x = "x \<circ> inv q" in exI)
   922   apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
   923   done
   924 
   925 lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
   926   by (simp add: permutes_def) metis
   927 
   928 lemma setsum_permutations_inverse:
   929   "setsum f {p. p permutes S} = setsum (\<lambda>p. f(inv p)) {p. p permutes S}"
   930   (is "?lhs = ?rhs")
   931 proof -
   932   let ?S = "{p . p permutes S}"
   933   have th0: "inj_on inv ?S"
   934   proof (auto simp add: inj_on_def)
   935     fix q r
   936     assume q: "q permutes S"
   937       and r: "r permutes S"
   938       and qr: "inv q = inv r"
   939     then have "inv (inv q) = inv (inv r)"
   940       by simp
   941     with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
   942       by metis
   943   qed
   944   have th1: "inv ` ?S = ?S"
   945     using image_inverse_permutations by blast
   946   have th2: "?rhs = setsum (f \<circ> inv) ?S"
   947     by (simp add: o_def)
   948   from setsum.reindex[OF th0, of f] show ?thesis unfolding th1 th2 .
   949 qed
   950 
   951 lemma setum_permutations_compose_left:
   952   assumes q: "q permutes S"
   953   shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
   954   (is "?lhs = ?rhs")
   955 proof -
   956   let ?S = "{p. p permutes S}"
   957   have th0: "?rhs = setsum (f \<circ> (op \<circ> q)) ?S"
   958     by (simp add: o_def)
   959   have th1: "inj_on (op \<circ> q) ?S"
   960   proof (auto simp add: inj_on_def)
   961     fix p r
   962     assume "p permutes S"
   963       and r: "r permutes S"
   964       and rp: "q \<circ> p = q \<circ> r"
   965     then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r"
   966       by (simp add: comp_assoc)
   967     with permutes_inj[OF q, unfolded inj_iff] show "p = r"
   968       by simp
   969   qed
   970   have th3: "(op \<circ> q) ` ?S = ?S"
   971     using image_compose_permutations_left[OF q] by auto
   972   from setsum.reindex[OF th1, of f] show ?thesis unfolding th0 th1 th3 .
   973 qed
   974 
   975 lemma sum_permutations_compose_right:
   976   assumes q: "q permutes S"
   977   shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
   978   (is "?lhs = ?rhs")
   979 proof -
   980   let ?S = "{p. p permutes S}"
   981   have th0: "?rhs = setsum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
   982     by (simp add: o_def)
   983   have th1: "inj_on (\<lambda>p. p \<circ> q) ?S"
   984   proof (auto simp add: inj_on_def)
   985     fix p r
   986     assume "p permutes S"
   987       and r: "r permutes S"
   988       and rp: "p \<circ> q = r \<circ> q"
   989     then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)"
   990       by (simp add: o_assoc)
   991     with permutes_surj[OF q, unfolded surj_iff] show "p = r"
   992       by simp
   993   qed
   994   have th3: "(\<lambda>p. p \<circ> q) ` ?S = ?S"
   995     using image_compose_permutations_right[OF q] by auto
   996   from setsum.reindex[OF th1, of f]
   997   show ?thesis unfolding th0 th1 th3 .
   998 qed
   999 
  1000 
  1001 subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close>
  1002 
  1003 lemma setsum_over_permutations_insert:
  1004   assumes fS: "finite S"
  1005     and aS: "a \<notin> S"
  1006   shows "setsum f {p. p permutes (insert a S)} =
  1007     setsum (\<lambda>b. setsum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
  1008 proof -
  1009   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)"
  1010     by (simp add: fun_eq_iff)
  1011   have th1: "\<And>P Q. P \<times> Q = {(a,b). a \<in> P \<and> b \<in> Q}"
  1012     by blast
  1013   have th2: "\<And>P Q. P \<Longrightarrow> (P \<Longrightarrow> Q) \<Longrightarrow> P \<and> Q"
  1014     by blast
  1015   show ?thesis
  1016     unfolding permutes_insert
  1017     unfolding setsum.cartesian_product
  1018     unfolding th1[symmetric]
  1019     unfolding th0
  1020   proof (rule setsum.reindex)
  1021     let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
  1022     let ?P = "{p. p permutes S}"
  1023     {
  1024       fix b c p q
  1025       assume b: "b \<in> insert a S"
  1026       assume c: "c \<in> insert a S"
  1027       assume p: "p permutes S"
  1028       assume q: "q permutes S"
  1029       assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q"
  1030       from p q aS have pa: "p a = a" and qa: "q a = a"
  1031         unfolding permutes_def by metis+
  1032       from eq have "(Fun.swap a b id \<circ> p) a  = (Fun.swap a c id \<circ> q) a"
  1033         by simp
  1034       then have bc: "b = c"
  1035         by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def
  1036             cong del: if_weak_cong split: split_if_asm)
  1037       from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
  1038         (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
  1039       then have "p = q"
  1040         unfolding o_assoc swap_id_idempotent
  1041         by (simp add: o_def)
  1042       with bc have "b = c \<and> p = q"
  1043         by blast
  1044     }
  1045     then show "inj_on ?f (insert a S \<times> ?P)"
  1046       unfolding inj_on_def by clarify metis
  1047   qed
  1048 qed
  1049 
  1050 end
  1051