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