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