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