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