src/HOL/Library/Permutations.thy
author wenzelm
Wed Jun 17 11:03:05 2015 +0200 (2015-06-17)
changeset 60500 903bb1495239
parent 59730 b7c394c7a619
child 60601 6e83d94760c4
permissions -rw-r--r--
isabelle update_cartouches;
wenzelm@41959
     1
(*  Title:      HOL/Library/Permutations.thy
wenzelm@41959
     2
    Author:     Amine Chaieb, University of Cambridge
chaieb@29840
     3
*)
chaieb@29840
     4
wenzelm@60500
     5
section \<open>Permutations, both general and specifically on finite sets.\<close>
chaieb@29840
     6
chaieb@29840
     7
theory Permutations
lp15@59669
     8
imports Binomial
chaieb@29840
     9
begin
chaieb@29840
    10
wenzelm@60500
    11
subsection \<open>Transpositions\<close>
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@60500
    26
subsection \<open>Basic consequences of the definition\<close>
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@60500
    99
subsection \<open>Group properties\<close>
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@60500
   119
subsection \<open>The number of permutations on a finite set\<close>
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@60500
   189
      using \<open>x \<notin> F\<close> H0 \<open>finite F\<close> by auto
wenzelm@54681
   190
    from insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
wenzelm@60500
   191
      using \<open>finite F\<close> by auto
wenzelm@54681
   192
    then have "finite ?pF"
lp15@59730
   193
      by (auto intro: card_ge_0_finite)
wenzelm@54681
   194
    then have pF'f: "finite ?pF'"
wenzelm@60500
   195
      using H0 \<open>finite F\<close>
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@60500
   211
        from ths(4) \<open>x \<notin> F\<close> 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@60500
   215
          using ths(5) \<open>x \<notin> F\<close> eq
nipkow@39302
   216
          by (auto simp add: swap_def fun_upd_def fun_eq_iff)
wenzelm@54681
   217
        also have "\<dots> = c"
wenzelm@60500
   218
          using ths(5) \<open>x \<notin> F\<close>
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@60500
   237
    from \<open>x \<notin> F\<close> H0 have n0: "n \<noteq> 0"
wenzelm@60500
   238
      using \<open>finite F\<close> 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@60500
   245
      using \<open>finite F\<close> \<open>finite ?pF\<close>
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}"
lp15@59730
   261
  using card_permutations[OF refl fS] 
hoelzl@33715
   262
  by (auto intro: card_ge_0_finite)
chaieb@29840
   263
wenzelm@54681
   264
wenzelm@60500
   265
subsection \<open>Permutations of index set for iterated operations\<close>
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@60500
   271
  from \<open>p permutes S\<close> 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@60500
   277
  moreover from \<open>p permutes S\<close> 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
wenzelm@54681
   283
wenzelm@60500
   284
subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close>
wenzelm@54681
   285
wenzelm@54681
   286
lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>
wenzelm@54681
   287
  Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id"
haftmann@56545
   288
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   289
wenzelm@54681
   290
lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow>
wenzelm@54681
   291
  Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id"
haftmann@56545
   292
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   293
wenzelm@54681
   294
lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow>
wenzelm@54681
   295
  Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id"
haftmann@56545
   296
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   297
wenzelm@54681
   298
wenzelm@60500
   299
subsection \<open>Permutations as transposition sequences\<close>
wenzelm@54681
   300
wenzelm@54681
   301
inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
wenzelm@54681
   302
where
wenzelm@54681
   303
  id[simp]: "swapidseq 0 id"
wenzelm@54681
   304
| comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
wenzelm@54681
   305
wenzelm@54681
   306
declare id[unfolded id_def, simp]
wenzelm@54681
   307
wenzelm@54681
   308
definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
chaieb@29840
   309
chaieb@29840
   310
wenzelm@60500
   311
subsection \<open>Some closure properties of the set of permutations, with lengths\<close>
chaieb@29840
   312
wenzelm@54681
   313
lemma permutation_id[simp]: "permutation id"
wenzelm@54681
   314
  unfolding permutation_def by (rule exI[where x=0]) simp
chaieb@29840
   315
chaieb@29840
   316
declare permutation_id[unfolded id_def, simp]
chaieb@29840
   317
chaieb@29840
   318
lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
chaieb@29840
   319
  apply clarsimp
wenzelm@54681
   320
  using comp_Suc[of 0 id a b]
wenzelm@54681
   321
  apply simp
wenzelm@54681
   322
  done
chaieb@29840
   323
chaieb@29840
   324
lemma permutation_swap_id: "permutation (Fun.swap a b id)"
wenzelm@54681
   325
  apply (cases "a = b")
wenzelm@54681
   326
  apply simp_all
wenzelm@54681
   327
  unfolding permutation_def
wenzelm@54681
   328
  using swapidseq_swap[of a b]
wenzelm@54681
   329
  apply blast
wenzelm@54681
   330
  done
chaieb@29840
   331
wenzelm@54681
   332
lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
wenzelm@54681
   333
proof (induct n p arbitrary: m q rule: swapidseq.induct)
wenzelm@54681
   334
  case (id m q)
wenzelm@54681
   335
  then show ?case by simp
wenzelm@54681
   336
next
wenzelm@54681
   337
  case (comp_Suc n p a b m q)
wenzelm@54681
   338
  have th: "Suc n + m = Suc (n + m)"
wenzelm@54681
   339
    by arith
wenzelm@54681
   340
  show ?case
wenzelm@54681
   341
    unfolding th comp_assoc
wenzelm@54681
   342
    apply (rule swapidseq.comp_Suc)
wenzelm@54681
   343
    using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
wenzelm@54681
   344
    apply blast+
wenzelm@54681
   345
    done
chaieb@29840
   346
qed
chaieb@29840
   347
wenzelm@54681
   348
lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)"
chaieb@29840
   349
  unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
chaieb@29840
   350
wenzelm@54681
   351
lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
chaieb@29840
   352
  apply (induct n p rule: swapidseq.induct)
chaieb@29840
   353
  using swapidseq_swap[of a b]
wenzelm@54681
   354
  apply (auto simp add: comp_assoc intro: swapidseq.comp_Suc)
wenzelm@54681
   355
  done
chaieb@29840
   356
wenzelm@54681
   357
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
   358
proof (induct n p rule: swapidseq.induct)
wenzelm@54681
   359
  case id
wenzelm@54681
   360
  then show ?case
wenzelm@54681
   361
    by (rule exI[where x=id]) simp
huffman@30488
   362
next
chaieb@29840
   363
  case (comp_Suc n p a b)
wenzelm@54681
   364
  from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   365
    by blast
wenzelm@54681
   366
  let ?q = "q \<circ> Fun.swap a b id"
chaieb@29840
   367
  note H = comp_Suc.hyps
wenzelm@54681
   368
  from swapidseq_swap[of a b] H(3) have th0: "swapidseq 1 (Fun.swap a b id)"
wenzelm@54681
   369
    by simp
wenzelm@54681
   370
  from swapidseq_comp_add[OF q(1) th0] have th1: "swapidseq (Suc n) ?q"
wenzelm@54681
   371
    by simp
wenzelm@54681
   372
  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
   373
    by (simp add: o_assoc)
wenzelm@54681
   374
  also have "\<dots> = id"
wenzelm@54681
   375
    by (simp add: q(2))
wenzelm@54681
   376
  finally have th2: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
wenzelm@54681
   377
  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
   378
    by (simp only: o_assoc)
wenzelm@54681
   379
  then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
wenzelm@54681
   380
    by (simp add: q(3))
wenzelm@54681
   381
  with th1 th2 show ?case
wenzelm@54681
   382
    by blast
chaieb@29840
   383
qed
chaieb@29840
   384
wenzelm@54681
   385
lemma swapidseq_inverse:
wenzelm@54681
   386
  assumes H: "swapidseq n p"
wenzelm@54681
   387
  shows "swapidseq n (inv p)"
wenzelm@54681
   388
  using swapidseq_inverse_exists[OF H] inv_unique_comp[of p] by auto
wenzelm@54681
   389
wenzelm@54681
   390
lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
wenzelm@54681
   391
  using permutation_def swapidseq_inverse by blast
wenzelm@54681
   392
chaieb@29840
   393
wenzelm@60500
   394
subsection \<open>The identity map only has even transposition sequences\<close>
chaieb@29840
   395
wenzelm@54681
   396
lemma symmetry_lemma:
wenzelm@54681
   397
  assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c"
wenzelm@54681
   398
    and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
wenzelm@54681
   399
      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
   400
      P a b c d"
wenzelm@54681
   401
  shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>  P a b c d"
wenzelm@54681
   402
  using assms by metis
chaieb@29840
   403
wenzelm@54681
   404
lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
wenzelm@54681
   405
  Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
wenzelm@54681
   406
  (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
wenzelm@54681
   407
    Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)"
wenzelm@54681
   408
proof -
wenzelm@54681
   409
  assume H: "a \<noteq> b" "c \<noteq> d"
wenzelm@54681
   410
  have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
wenzelm@54681
   411
    (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
wenzelm@54681
   412
      (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
wenzelm@54681
   413
        Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
wenzelm@54681
   414
    apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
haftmann@56545
   415
    apply (simp_all only: swap_commute)
wenzelm@54681
   416
    apply (case_tac "a = c \<and> b = d")
haftmann@56608
   417
    apply (clarsimp simp only: swap_commute swap_id_idempotent)
wenzelm@54681
   418
    apply (case_tac "a = c \<and> b \<noteq> d")
wenzelm@54681
   419
    apply (rule disjI2)
wenzelm@54681
   420
    apply (rule_tac x="b" in exI)
wenzelm@54681
   421
    apply (rule_tac x="d" in exI)
wenzelm@54681
   422
    apply (rule_tac x="b" in exI)
haftmann@56545
   423
    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   424
    apply (case_tac "a \<noteq> c \<and> b = d")
wenzelm@54681
   425
    apply (rule disjI2)
wenzelm@54681
   426
    apply (rule_tac x="c" in exI)
wenzelm@54681
   427
    apply (rule_tac x="d" in exI)
wenzelm@54681
   428
    apply (rule_tac x="c" in exI)
haftmann@56545
   429
    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   430
    apply (rule disjI2)
wenzelm@54681
   431
    apply (rule_tac x="c" in exI)
wenzelm@54681
   432
    apply (rule_tac x="d" in exI)
wenzelm@54681
   433
    apply (rule_tac x="b" in exI)
haftmann@56545
   434
    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   435
    done
wenzelm@54681
   436
  with H show ?thesis by metis
chaieb@29840
   437
qed
chaieb@29840
   438
chaieb@29840
   439
lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
chaieb@29840
   440
  using swapidseq.cases[of 0 p "p = id"]
chaieb@29840
   441
  by auto
chaieb@29840
   442
wenzelm@54681
   443
lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
wenzelm@54681
   444
  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
   445
  apply (rule iffI)
chaieb@29840
   446
  apply (erule swapidseq.cases[of n p])
chaieb@29840
   447
  apply simp
chaieb@29840
   448
  apply (rule disjI2)
chaieb@29840
   449
  apply (rule_tac x= "a" in exI)
chaieb@29840
   450
  apply (rule_tac x= "b" in exI)
chaieb@29840
   451
  apply (rule_tac x= "pa" in exI)
chaieb@29840
   452
  apply (rule_tac x= "na" in exI)
chaieb@29840
   453
  apply simp
chaieb@29840
   454
  apply auto
chaieb@29840
   455
  apply (rule comp_Suc, simp_all)
chaieb@29840
   456
  done
wenzelm@54681
   457
chaieb@29840
   458
lemma fixing_swapidseq_decrease:
wenzelm@54681
   459
  assumes spn: "swapidseq n p"
wenzelm@54681
   460
    and ab: "a \<noteq> b"
wenzelm@54681
   461
    and pa: "(Fun.swap a b id \<circ> p) a = a"
wenzelm@54681
   462
  shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
chaieb@29840
   463
  using spn ab pa
wenzelm@54681
   464
proof (induct n arbitrary: p a b)
wenzelm@54681
   465
  case 0
wenzelm@54681
   466
  then show ?case
haftmann@56545
   467
    by (auto simp add: Fun.swap_def fun_upd_def)
chaieb@29840
   468
next
chaieb@29840
   469
  case (Suc n p a b)
wenzelm@54681
   470
  from Suc.prems(1) swapidseq_cases[of "Suc n" p]
wenzelm@54681
   471
  obtain c d q m where
wenzelm@54681
   472
    cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
chaieb@29840
   473
    by auto
wenzelm@54681
   474
  {
wenzelm@54681
   475
    assume H: "Fun.swap a b id \<circ> Fun.swap c d id = id"
wenzelm@54681
   476
    have ?case by (simp only: cdqm o_assoc H) (simp add: cdqm)
wenzelm@54681
   477
  }
chaieb@29840
   478
  moreover
wenzelm@54681
   479
  {
wenzelm@54681
   480
    fix x y z
wenzelm@54681
   481
    assume H: "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
wenzelm@54681
   482
      "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
wenzelm@54681
   483
    from H have az: "a \<noteq> z"
wenzelm@54681
   484
      by simp
chaieb@29840
   485
wenzelm@54681
   486
    {
wenzelm@54681
   487
      fix h
wenzelm@54681
   488
      have "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a"
haftmann@56545
   489
        using H by (simp add: Fun.swap_def)
wenzelm@54681
   490
    }
chaieb@29840
   491
    note th3 = this
wenzelm@54681
   492
    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
   493
      by simp
wenzelm@54681
   494
    then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
wenzelm@54681
   495
      by (simp add: o_assoc H)
wenzelm@54681
   496
    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
   497
      by simp
wenzelm@54681
   498
    then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
wenzelm@54681
   499
      unfolding Suc by metis
wenzelm@54681
   500
    then have th1: "(Fun.swap a z id \<circ> q) a = a"
wenzelm@54681
   501
      unfolding th3 .
chaieb@29840
   502
    from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az th1]
wenzelm@54681
   503
    have th2: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
wenzelm@54681
   504
      by blast+
wenzelm@54681
   505
    have th: "Suc n - 1 = Suc (n - 1)"
wenzelm@54681
   506
      using th2(2) by auto
wenzelm@54681
   507
    have ?case
wenzelm@54681
   508
      unfolding cdqm(2) H o_assoc th
haftmann@49739
   509
      apply (simp only: Suc_not_Zero simp_thms comp_assoc)
chaieb@29840
   510
      apply (rule comp_Suc)
wenzelm@54681
   511
      using th2 H
wenzelm@54681
   512
      apply blast+
wenzelm@54681
   513
      done
wenzelm@54681
   514
  }
wenzelm@54681
   515
  ultimately show ?case
wenzelm@54681
   516
    using swap_general[OF Suc.prems(2) cdqm(4)] by metis
chaieb@29840
   517
qed
chaieb@29840
   518
huffman@30488
   519
lemma swapidseq_identity_even:
wenzelm@54681
   520
  assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)"
wenzelm@54681
   521
  shows "even n"
wenzelm@60500
   522
  using \<open>swapidseq n id\<close>
wenzelm@54681
   523
proof (induct n rule: nat_less_induct)
chaieb@29840
   524
  fix n
chaieb@29840
   525
  assume H: "\<forall>m<n. swapidseq m (id::'a \<Rightarrow> 'a) \<longrightarrow> even m" "swapidseq n (id :: 'a \<Rightarrow> 'a)"
wenzelm@54681
   526
  {
wenzelm@54681
   527
    assume "n = 0"
wenzelm@54681
   528
    then have "even n" by presburger
wenzelm@54681
   529
  }
huffman@30488
   530
  moreover
wenzelm@54681
   531
  {
wenzelm@54681
   532
    fix a b :: 'a and q m
chaieb@29840
   533
    assume h: "n = Suc m" "(id :: 'a \<Rightarrow> 'a) = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
chaieb@29840
   534
    from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
wenzelm@54681
   535
    have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
wenzelm@54681
   536
      by auto
wenzelm@54681
   537
    from h m have mn: "m - 1 < n"
wenzelm@54681
   538
      by arith
wenzelm@54681
   539
    from H(1)[rule_format, OF mn m(2)] h(1) m(1) have "even n"
wenzelm@54681
   540
      by presburger
wenzelm@54681
   541
  }
wenzelm@54681
   542
  ultimately show "even n"
wenzelm@54681
   543
    using H(2)[unfolded swapidseq_cases[of n id]] by auto
chaieb@29840
   544
qed
chaieb@29840
   545
wenzelm@54681
   546
wenzelm@60500
   547
subsection \<open>Therefore we have a welldefined notion of parity\<close>
chaieb@29840
   548
chaieb@29840
   549
definition "evenperm p = even (SOME n. swapidseq n p)"
chaieb@29840
   550
wenzelm@54681
   551
lemma swapidseq_even_even:
wenzelm@54681
   552
  assumes m: "swapidseq m p"
wenzelm@54681
   553
    and n: "swapidseq n p"
chaieb@29840
   554
  shows "even m \<longleftrightarrow> even n"
wenzelm@54681
   555
proof -
chaieb@29840
   556
  from swapidseq_inverse_exists[OF n]
wenzelm@54681
   557
  obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   558
    by blast
chaieb@29840
   559
  from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]]
wenzelm@54681
   560
  show ?thesis
wenzelm@54681
   561
    by arith
chaieb@29840
   562
qed
chaieb@29840
   563
wenzelm@54681
   564
lemma evenperm_unique:
wenzelm@54681
   565
  assumes p: "swapidseq n p"
wenzelm@54681
   566
    and n:"even n = b"
chaieb@29840
   567
  shows "evenperm p = b"
chaieb@29840
   568
  unfolding n[symmetric] evenperm_def
chaieb@29840
   569
  apply (rule swapidseq_even_even[where p = p])
chaieb@29840
   570
  apply (rule someI[where x = n])
wenzelm@54681
   571
  using p
wenzelm@54681
   572
  apply blast+
wenzelm@54681
   573
  done
chaieb@29840
   574
wenzelm@54681
   575
wenzelm@60500
   576
subsection \<open>And it has the expected composition properties\<close>
chaieb@29840
   577
chaieb@29840
   578
lemma evenperm_id[simp]: "evenperm id = True"
wenzelm@54681
   579
  by (rule evenperm_unique[where n = 0]) simp_all
chaieb@29840
   580
chaieb@29840
   581
lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
wenzelm@54681
   582
  by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
chaieb@29840
   583
huffman@30488
   584
lemma evenperm_comp:
wenzelm@54681
   585
  assumes p: "permutation p"
wenzelm@54681
   586
    and q:"permutation q"
wenzelm@54681
   587
  shows "evenperm (p \<circ> q) = (evenperm p = evenperm q)"
wenzelm@54681
   588
proof -
wenzelm@54681
   589
  from p q obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
chaieb@29840
   590
    unfolding permutation_def by blast
chaieb@29840
   591
  note nm =  swapidseq_comp_add[OF n m]
wenzelm@54681
   592
  have th: "even (n + m) = (even n \<longleftrightarrow> even m)"
wenzelm@54681
   593
    by arith
chaieb@29840
   594
  from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
chaieb@29840
   595
    evenperm_unique[OF nm th]
wenzelm@54681
   596
  show ?thesis
wenzelm@54681
   597
    by blast
chaieb@29840
   598
qed
chaieb@29840
   599
wenzelm@54681
   600
lemma evenperm_inv:
wenzelm@54681
   601
  assumes p: "permutation p"
chaieb@29840
   602
  shows "evenperm (inv p) = evenperm p"
wenzelm@54681
   603
proof -
wenzelm@54681
   604
  from p obtain n where n: "swapidseq n p"
wenzelm@54681
   605
    unfolding permutation_def by blast
chaieb@29840
   606
  from evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]]
chaieb@29840
   607
  show ?thesis .
chaieb@29840
   608
qed
chaieb@29840
   609
chaieb@29840
   610
wenzelm@60500
   611
subsection \<open>A more abstract characterization of permutations\<close>
chaieb@29840
   612
chaieb@29840
   613
lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
chaieb@29840
   614
  unfolding bij_def inj_on_def surj_def
chaieb@29840
   615
  apply auto
chaieb@29840
   616
  apply metis
chaieb@29840
   617
  apply metis
chaieb@29840
   618
  done
chaieb@29840
   619
huffman@30488
   620
lemma permutation_bijective:
huffman@30488
   621
  assumes p: "permutation p"
chaieb@29840
   622
  shows "bij 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
wenzelm@54681
   626
  from swapidseq_inverse_exists[OF n]
wenzelm@54681
   627
  obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   628
    by blast
wenzelm@54681
   629
  then show ?thesis unfolding bij_iff
wenzelm@54681
   630
    apply (auto simp add: fun_eq_iff)
wenzelm@54681
   631
    apply metis
wenzelm@54681
   632
    done
huffman@30488
   633
qed
chaieb@29840
   634
wenzelm@54681
   635
lemma permutation_finite_support:
wenzelm@54681
   636
  assumes p: "permutation p"
chaieb@29840
   637
  shows "finite {x. p x \<noteq> x}"
wenzelm@54681
   638
proof -
wenzelm@54681
   639
  from p obtain n where n: "swapidseq n p"
wenzelm@54681
   640
    unfolding permutation_def by blast
chaieb@29840
   641
  from n show ?thesis
wenzelm@54681
   642
  proof (induct n p rule: swapidseq.induct)
wenzelm@54681
   643
    case id
wenzelm@54681
   644
    then show ?case by simp
chaieb@29840
   645
  next
chaieb@29840
   646
    case (comp_Suc n p a b)
chaieb@29840
   647
    let ?S = "insert a (insert b {x. p x \<noteq> x})"
wenzelm@54681
   648
    from comp_Suc.hyps(2) have fS: "finite ?S"
wenzelm@54681
   649
      by simp
wenzelm@60500
   650
    from \<open>a \<noteq> b\<close> have th: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
haftmann@56545
   651
      by (auto simp add: Fun.swap_def)
chaieb@29840
   652
    from finite_subset[OF th fS] show ?case  .
wenzelm@54681
   653
  qed
chaieb@29840
   654
qed
chaieb@29840
   655
wenzelm@54681
   656
lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
wenzelm@54681
   657
  using surj_f_inv_f[of p] by (auto simp add: bij_def)
chaieb@29840
   658
huffman@30488
   659
lemma bij_swap_comp:
wenzelm@54681
   660
  assumes bp: "bij p"
wenzelm@54681
   661
  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
chaieb@29840
   662
  using surj_f_inv_f[OF bij_is_surj[OF bp]]
haftmann@56545
   663
  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp])
chaieb@29840
   664
wenzelm@54681
   665
lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)"
wenzelm@54681
   666
proof -
chaieb@29840
   667
  assume H: "bij p"
huffman@30488
   668
  show ?thesis
chaieb@29840
   669
    unfolding bij_swap_comp[OF H] bij_swap_iff
chaieb@29840
   670
    using H .
chaieb@29840
   671
qed
chaieb@29840
   672
huffman@30488
   673
lemma permutation_lemma:
wenzelm@54681
   674
  assumes fS: "finite S"
wenzelm@54681
   675
    and p: "bij p"
wenzelm@54681
   676
    and pS: "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
chaieb@29840
   677
  shows "permutation p"
wenzelm@54681
   678
  using fS p pS
wenzelm@54681
   679
proof (induct S arbitrary: p rule: finite_induct)
wenzelm@54681
   680
  case (empty p)
wenzelm@54681
   681
  then show ?case by simp
chaieb@29840
   682
next
chaieb@29840
   683
  case (insert a F p)
wenzelm@54681
   684
  let ?r = "Fun.swap a (p a) id \<circ> p"
wenzelm@54681
   685
  let ?q = "Fun.swap a (p a) id \<circ> ?r"
wenzelm@54681
   686
  have raa: "?r a = a"
haftmann@56545
   687
    by (simp add: Fun.swap_def)
chaieb@29840
   688
  from bij_swap_ompose_bij[OF insert(4)]
huffman@30488
   689
  have br: "bij ?r"  .
huffman@30488
   690
huffman@30488
   691
  from insert raa have th: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
haftmann@56545
   692
    apply (clarsimp simp add: Fun.swap_def)
chaieb@29840
   693
    apply (erule_tac x="x" in allE)
chaieb@29840
   694
    apply auto
wenzelm@54681
   695
    unfolding bij_iff
wenzelm@54681
   696
    apply metis
chaieb@29840
   697
    done
chaieb@29840
   698
  from insert(3)[OF br th]
chaieb@29840
   699
  have rp: "permutation ?r" .
wenzelm@54681
   700
  have "permutation ?q"
wenzelm@54681
   701
    by (simp add: permutation_compose permutation_swap_id rp)
wenzelm@54681
   702
  then show ?case
wenzelm@54681
   703
    by (simp add: o_assoc)
chaieb@29840
   704
qed
chaieb@29840
   705
huffman@30488
   706
lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
chaieb@29840
   707
  (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
chaieb@29840
   708
proof
chaieb@29840
   709
  assume p: ?lhs
wenzelm@54681
   710
  from p permutation_bijective permutation_finite_support show "?b \<and> ?f"
wenzelm@54681
   711
    by auto
chaieb@29840
   712
next
wenzelm@54681
   713
  assume "?b \<and> ?f"
wenzelm@54681
   714
  then have "?f" "?b" by blast+
wenzelm@54681
   715
  from permutation_lemma[OF this] show ?lhs
wenzelm@54681
   716
    by blast
chaieb@29840
   717
qed
chaieb@29840
   718
wenzelm@54681
   719
lemma permutation_inverse_works:
wenzelm@54681
   720
  assumes p: "permutation p"
wenzelm@54681
   721
  shows "inv p \<circ> p = id"
wenzelm@54681
   722
    and "p \<circ> inv p = id"
huffman@44227
   723
  using permutation_bijective [OF p]
huffman@44227
   724
  unfolding bij_def inj_iff surj_iff by auto
chaieb@29840
   725
chaieb@29840
   726
lemma permutation_inverse_compose:
wenzelm@54681
   727
  assumes p: "permutation p"
wenzelm@54681
   728
    and q: "permutation q"
wenzelm@54681
   729
  shows "inv (p \<circ> q) = inv q \<circ> inv p"
wenzelm@54681
   730
proof -
chaieb@29840
   731
  note ps = permutation_inverse_works[OF p]
chaieb@29840
   732
  note qs = permutation_inverse_works[OF q]
wenzelm@54681
   733
  have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p"
wenzelm@54681
   734
    by (simp add: o_assoc)
wenzelm@54681
   735
  also have "\<dots> = id"
wenzelm@54681
   736
    by (simp add: ps qs)
wenzelm@54681
   737
  finally have th0: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
wenzelm@54681
   738
  have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
wenzelm@54681
   739
    by (simp add: o_assoc)
wenzelm@54681
   740
  also have "\<dots> = id"
wenzelm@54681
   741
    by (simp add: ps qs)
wenzelm@54681
   742
  finally have th1: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
chaieb@29840
   743
  from inv_unique_comp[OF th0 th1] show ?thesis .
chaieb@29840
   744
qed
chaieb@29840
   745
wenzelm@54681
   746
wenzelm@60500
   747
subsection \<open>Relation to "permutes"\<close>
chaieb@29840
   748
chaieb@29840
   749
lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
wenzelm@54681
   750
  unfolding permutation permutes_def bij_iff[symmetric]
wenzelm@54681
   751
  apply (rule iffI, clarify)
wenzelm@54681
   752
  apply (rule exI[where x="{x. p x \<noteq> x}"])
wenzelm@54681
   753
  apply simp
wenzelm@54681
   754
  apply clarsimp
wenzelm@54681
   755
  apply (rule_tac B="S" in finite_subset)
wenzelm@54681
   756
  apply auto
wenzelm@54681
   757
  done
chaieb@29840
   758
wenzelm@54681
   759
wenzelm@60500
   760
subsection \<open>Hence a sort of induction principle composing by swaps\<close>
chaieb@29840
   761
wenzelm@54681
   762
lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow>
wenzelm@54681
   763
  (\<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
   764
  (\<And>p. p permutes S \<Longrightarrow> P p)"
wenzelm@54681
   765
proof (induct S rule: finite_induct)
wenzelm@54681
   766
  case empty
wenzelm@54681
   767
  then show ?case by auto
huffman@30488
   768
next
chaieb@29840
   769
  case (insert x F p)
wenzelm@54681
   770
  let ?r = "Fun.swap x (p x) id \<circ> p"
wenzelm@54681
   771
  let ?q = "Fun.swap x (p x) id \<circ> ?r"
wenzelm@54681
   772
  have qp: "?q = p"
wenzelm@54681
   773
    by (simp add: o_assoc)
wenzelm@54681
   774
  from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r"
wenzelm@54681
   775
    by blast
huffman@30488
   776
  from permutes_in_image[OF insert.prems(3), of x]
wenzelm@54681
   777
  have pxF: "p x \<in> insert x F"
wenzelm@54681
   778
    by simp
wenzelm@54681
   779
  have xF: "x \<in> insert x F"
wenzelm@54681
   780
    by simp
chaieb@29840
   781
  have rp: "permutation ?r"
huffman@30488
   782
    unfolding permutation_permutes using insert.hyps(1)
wenzelm@54681
   783
      permutes_insert_lemma[OF insert.prems(3)]
wenzelm@54681
   784
    by blast
huffman@30488
   785
  from insert.prems(2)[OF xF pxF Pr Pr rp]
wenzelm@54681
   786
  show ?case
wenzelm@54681
   787
    unfolding qp .
chaieb@29840
   788
qed
chaieb@29840
   789
wenzelm@54681
   790
wenzelm@60500
   791
subsection \<open>Sign of a permutation as a real number\<close>
chaieb@29840
   792
chaieb@29840
   793
definition "sign p = (if evenperm p then (1::int) else -1)"
chaieb@29840
   794
wenzelm@54681
   795
lemma sign_nz: "sign p \<noteq> 0"
wenzelm@54681
   796
  by (simp add: sign_def)
wenzelm@54681
   797
wenzelm@54681
   798
lemma sign_id: "sign id = 1"
wenzelm@54681
   799
  by (simp add: sign_def)
wenzelm@54681
   800
wenzelm@54681
   801
lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p"
chaieb@29840
   802
  by (simp add: sign_def evenperm_inv)
wenzelm@54681
   803
wenzelm@54681
   804
lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q"
wenzelm@54681
   805
  by (simp add: sign_def evenperm_comp)
wenzelm@54681
   806
chaieb@29840
   807
lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
chaieb@29840
   808
  by (simp add: sign_def evenperm_swap)
chaieb@29840
   809
wenzelm@54681
   810
lemma sign_idempotent: "sign p * sign p = 1"
wenzelm@54681
   811
  by (simp add: sign_def)
wenzelm@54681
   812
wenzelm@54681
   813
wenzelm@60500
   814
subsection \<open>More lemmas about permutations\<close>
chaieb@29840
   815
chaieb@29840
   816
lemma permutes_natset_le:
wenzelm@54681
   817
  fixes S :: "'a::wellorder set"
wenzelm@54681
   818
  assumes p: "p permutes S"
wenzelm@54681
   819
    and le: "\<forall>i \<in> S. p i \<le> i"
wenzelm@54681
   820
  shows "p = id"
wenzelm@54681
   821
proof -
wenzelm@54681
   822
  {
wenzelm@54681
   823
    fix n
huffman@30488
   824
    have "p n = n"
chaieb@29840
   825
      using p le
wenzelm@54681
   826
    proof (induct n arbitrary: S rule: less_induct)
wenzelm@54681
   827
      fix n S
wenzelm@54681
   828
      assume H:
wenzelm@54681
   829
        "\<And>m S. m < n \<Longrightarrow> p permutes S \<Longrightarrow> \<forall>i\<in>S. p i \<le> i \<Longrightarrow> p m = m"
wenzelm@32960
   830
        "p permutes S" "\<forall>i \<in>S. p i \<le> i"
wenzelm@54681
   831
      {
wenzelm@54681
   832
        assume "n \<notin> S"
wenzelm@54681
   833
        with H(2) have "p n = n"
wenzelm@54681
   834
          unfolding permutes_def by metis
wenzelm@54681
   835
      }
chaieb@29840
   836
      moreover
wenzelm@54681
   837
      {
wenzelm@54681
   838
        assume ns: "n \<in> S"
wenzelm@54681
   839
        from H(3)  ns have "p n < n \<or> p n = n"
wenzelm@54681
   840
          by auto
wenzelm@54681
   841
        moreover {
wenzelm@54681
   842
          assume h: "p n < n"
wenzelm@54681
   843
          from H h have "p (p n) = p n"
wenzelm@54681
   844
            by metis
wenzelm@54681
   845
          with permutes_inj[OF H(2)] have "p n = n"
wenzelm@54681
   846
            unfolding inj_on_def by blast
wenzelm@54681
   847
          with h have False
wenzelm@54681
   848
            by simp
wenzelm@54681
   849
        }
wenzelm@54681
   850
        ultimately have "p n = n"
wenzelm@54681
   851
          by blast
wenzelm@54681
   852
      }
wenzelm@54681
   853
      ultimately show "p n = n"
wenzelm@54681
   854
        by blast
wenzelm@54681
   855
    qed
wenzelm@54681
   856
  }
wenzelm@54681
   857
  then show ?thesis
wenzelm@54681
   858
    by (auto simp add: fun_eq_iff)
chaieb@29840
   859
qed
chaieb@29840
   860
chaieb@29840
   861
lemma permutes_natset_ge:
wenzelm@54681
   862
  fixes S :: "'a::wellorder set"
wenzelm@54681
   863
  assumes p: "p permutes S"
wenzelm@54681
   864
    and le: "\<forall>i \<in> S. p i \<ge> i"
wenzelm@54681
   865
  shows "p = id"
wenzelm@54681
   866
proof -
wenzelm@54681
   867
  {
wenzelm@54681
   868
    fix i
wenzelm@54681
   869
    assume i: "i \<in> S"
wenzelm@54681
   870
    from i permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
wenzelm@54681
   871
      by simp
wenzelm@54681
   872
    with le have "p (inv p i) \<ge> inv p i"
wenzelm@54681
   873
      by blast
wenzelm@54681
   874
    with permutes_inverses[OF p] have "i \<ge> inv p i"
wenzelm@54681
   875
      by simp
wenzelm@54681
   876
  }
wenzelm@54681
   877
  then have th: "\<forall>i\<in>S. inv p i \<le> i"
wenzelm@54681
   878
    by blast
huffman@30488
   879
  from permutes_natset_le[OF permutes_inv[OF p] th]
wenzelm@54681
   880
  have "inv p = inv id"
wenzelm@54681
   881
    by simp
huffman@30488
   882
  then show ?thesis
chaieb@29840
   883
    apply (subst permutes_inv_inv[OF p, symmetric])
chaieb@29840
   884
    apply (rule inv_unique_comp)
chaieb@29840
   885
    apply simp_all
chaieb@29840
   886
    done
chaieb@29840
   887
qed
chaieb@29840
   888
chaieb@29840
   889
lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
wenzelm@54681
   890
  apply (rule set_eqI)
wenzelm@54681
   891
  apply auto
wenzelm@54681
   892
  using permutes_inv_inv permutes_inv
wenzelm@54681
   893
  apply auto
chaieb@29840
   894
  apply (rule_tac x="inv x" in exI)
chaieb@29840
   895
  apply auto
chaieb@29840
   896
  done
chaieb@29840
   897
huffman@30488
   898
lemma image_compose_permutations_left:
wenzelm@54681
   899
  assumes q: "q permutes S"
wenzelm@54681
   900
  shows "{q \<circ> p | p. p permutes S} = {p . p permutes S}"
wenzelm@54681
   901
  apply (rule set_eqI)
wenzelm@54681
   902
  apply auto
wenzelm@54681
   903
  apply (rule permutes_compose)
wenzelm@54681
   904
  using q
wenzelm@54681
   905
  apply auto
wenzelm@54681
   906
  apply (rule_tac x = "inv q \<circ> x" in exI)
wenzelm@54681
   907
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
wenzelm@54681
   908
  done
chaieb@29840
   909
chaieb@29840
   910
lemma image_compose_permutations_right:
chaieb@29840
   911
  assumes q: "q permutes S"
wenzelm@54681
   912
  shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
wenzelm@54681
   913
  apply (rule set_eqI)
wenzelm@54681
   914
  apply auto
wenzelm@54681
   915
  apply (rule permutes_compose)
wenzelm@54681
   916
  using q
wenzelm@54681
   917
  apply auto
wenzelm@54681
   918
  apply (rule_tac x = "x \<circ> inv q" in exI)
wenzelm@54681
   919
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
wenzelm@54681
   920
  done
chaieb@29840
   921
wenzelm@54681
   922
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
   923
  by (simp add: permutes_def) metis
chaieb@29840
   924
wenzelm@54681
   925
lemma setsum_permutations_inverse:
wenzelm@54681
   926
  "setsum f {p. p permutes S} = setsum (\<lambda>p. f(inv p)) {p. p permutes S}"
wenzelm@54681
   927
  (is "?lhs = ?rhs")
wenzelm@54681
   928
proof -
huffman@30036
   929
  let ?S = "{p . p permutes S}"
wenzelm@54681
   930
  have th0: "inj_on inv ?S"
wenzelm@54681
   931
  proof (auto simp add: inj_on_def)
wenzelm@54681
   932
    fix q r
wenzelm@54681
   933
    assume q: "q permutes S"
wenzelm@54681
   934
      and r: "r permutes S"
wenzelm@54681
   935
      and qr: "inv q = inv r"
wenzelm@54681
   936
    then have "inv (inv q) = inv (inv r)"
wenzelm@54681
   937
      by simp
wenzelm@54681
   938
    with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
wenzelm@54681
   939
      by metis
wenzelm@54681
   940
  qed
wenzelm@54681
   941
  have th1: "inv ` ?S = ?S"
wenzelm@54681
   942
    using image_inverse_permutations by blast
wenzelm@54681
   943
  have th2: "?rhs = setsum (f \<circ> inv) ?S"
wenzelm@54681
   944
    by (simp add: o_def)
haftmann@57418
   945
  from setsum.reindex[OF th0, of f] show ?thesis unfolding th1 th2 .
chaieb@29840
   946
qed
chaieb@29840
   947
chaieb@29840
   948
lemma setum_permutations_compose_left:
huffman@30036
   949
  assumes q: "q permutes S"
wenzelm@54681
   950
  shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
wenzelm@54681
   951
  (is "?lhs = ?rhs")
wenzelm@54681
   952
proof -
huffman@30036
   953
  let ?S = "{p. p permutes S}"
wenzelm@54681
   954
  have th0: "?rhs = setsum (f \<circ> (op \<circ> q)) ?S"
wenzelm@54681
   955
    by (simp add: o_def)
wenzelm@54681
   956
  have th1: "inj_on (op \<circ> q) ?S"
wenzelm@54681
   957
  proof (auto simp add: inj_on_def)
chaieb@29840
   958
    fix p r
wenzelm@54681
   959
    assume "p permutes S"
wenzelm@54681
   960
      and r: "r permutes S"
wenzelm@54681
   961
      and rp: "q \<circ> p = q \<circ> r"
wenzelm@54681
   962
    then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r"
wenzelm@54681
   963
      by (simp add: comp_assoc)
wenzelm@54681
   964
    with permutes_inj[OF q, unfolded inj_iff] show "p = r"
wenzelm@54681
   965
      by simp
chaieb@29840
   966
  qed
wenzelm@54681
   967
  have th3: "(op \<circ> q) ` ?S = ?S"
wenzelm@54681
   968
    using image_compose_permutations_left[OF q] by auto
haftmann@57418
   969
  from setsum.reindex[OF th1, of f] show ?thesis unfolding th0 th1 th3 .
chaieb@29840
   970
qed
chaieb@29840
   971
chaieb@29840
   972
lemma sum_permutations_compose_right:
huffman@30036
   973
  assumes q: "q permutes S"
wenzelm@54681
   974
  shows "setsum f {p. p permutes S} = setsum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
wenzelm@54681
   975
  (is "?lhs = ?rhs")
wenzelm@54681
   976
proof -
huffman@30036
   977
  let ?S = "{p. p permutes S}"
wenzelm@54681
   978
  have th0: "?rhs = setsum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
wenzelm@54681
   979
    by (simp add: o_def)
wenzelm@54681
   980
  have th1: "inj_on (\<lambda>p. p \<circ> q) ?S"
wenzelm@54681
   981
  proof (auto simp add: inj_on_def)
chaieb@29840
   982
    fix p r
wenzelm@54681
   983
    assume "p permutes S"
wenzelm@54681
   984
      and r: "r permutes S"
wenzelm@54681
   985
      and rp: "p \<circ> q = r \<circ> q"
wenzelm@54681
   986
    then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)"
wenzelm@54681
   987
      by (simp add: o_assoc)
wenzelm@54681
   988
    with permutes_surj[OF q, unfolded surj_iff] show "p = r"
wenzelm@54681
   989
      by simp
chaieb@29840
   990
  qed
wenzelm@54681
   991
  have th3: "(\<lambda>p. p \<circ> q) ` ?S = ?S"
wenzelm@54681
   992
    using image_compose_permutations_right[OF q] by auto
haftmann@57418
   993
  from setsum.reindex[OF th1, of f]
chaieb@29840
   994
  show ?thesis unfolding th0 th1 th3 .
chaieb@29840
   995
qed
chaieb@29840
   996
wenzelm@54681
   997
wenzelm@60500
   998
subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close>
chaieb@29840
   999
chaieb@29840
  1000
lemma setsum_over_permutations_insert:
wenzelm@54681
  1001
  assumes fS: "finite S"
wenzelm@54681
  1002
    and aS: "a \<notin> S"
wenzelm@54681
  1003
  shows "setsum f {p. p permutes (insert a S)} =
wenzelm@54681
  1004
    setsum (\<lambda>b. setsum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
wenzelm@54681
  1005
proof -
wenzelm@54681
  1006
  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
  1007
    by (simp add: fun_eq_iff)
wenzelm@54681
  1008
  have th1: "\<And>P Q. P \<times> Q = {(a,b). a \<in> P \<and> b \<in> Q}"
wenzelm@54681
  1009
    by blast
wenzelm@54681
  1010
  have th2: "\<And>P Q. P \<Longrightarrow> (P \<Longrightarrow> Q) \<Longrightarrow> P \<and> Q"
wenzelm@54681
  1011
    by blast
huffman@30488
  1012
  show ?thesis
huffman@30488
  1013
    unfolding permutes_insert
haftmann@57418
  1014
    unfolding setsum.cartesian_product
hoelzl@57129
  1015
    unfolding th1[symmetric]
chaieb@29840
  1016
    unfolding th0
haftmann@57418
  1017
  proof (rule setsum.reindex)
chaieb@29840
  1018
    let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
chaieb@29840
  1019
    let ?P = "{p. p permutes S}"
wenzelm@54681
  1020
    {
wenzelm@54681
  1021
      fix b c p q
wenzelm@54681
  1022
      assume b: "b \<in> insert a S"
wenzelm@54681
  1023
      assume c: "c \<in> insert a S"
wenzelm@54681
  1024
      assume p: "p permutes S"
wenzelm@54681
  1025
      assume q: "q permutes S"
wenzelm@54681
  1026
      assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q"
chaieb@29840
  1027
      from p q aS have pa: "p a = a" and qa: "q a = a"
wenzelm@32960
  1028
        unfolding permutes_def by metis+
wenzelm@54681
  1029
      from eq have "(Fun.swap a b id \<circ> p) a  = (Fun.swap a c id \<circ> q) a"
wenzelm@54681
  1030
        by simp
wenzelm@54681
  1031
      then have bc: "b = c"
haftmann@56545
  1032
        by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def
wenzelm@54681
  1033
            cong del: if_weak_cong split: split_if_asm)
wenzelm@54681
  1034
      from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
wenzelm@54681
  1035
        (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
wenzelm@54681
  1036
      then have "p = q"
wenzelm@54681
  1037
        unfolding o_assoc swap_id_idempotent
wenzelm@32960
  1038
        by (simp add: o_def)
wenzelm@54681
  1039
      with bc have "b = c \<and> p = q"
wenzelm@54681
  1040
        by blast
chaieb@29840
  1041
    }
huffman@30488
  1042
    then show "inj_on ?f (insert a S \<times> ?P)"
wenzelm@54681
  1043
      unfolding inj_on_def by clarify metis
chaieb@29840
  1044
  qed
chaieb@29840
  1045
qed
chaieb@29840
  1046
chaieb@29840
  1047
end
haftmann@51489
  1048