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