src/HOL/Library/Permutations.thy
author wenzelm
Sat Apr 01 18:50:26 2017 +0200 (2017-04-01)
changeset 65342 e32eb488c3a3
parent 64966 d53d7ca3303e
child 65552 f533820e7248
permissions -rw-r--r--
misc tuning and modernization;
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
wenzelm@65342
     8
  imports Binomial Multiset Disjoint_Sets
chaieb@29840
     9
begin
chaieb@29840
    10
wenzelm@60500
    11
subsection \<open>Transpositions\<close>
chaieb@29840
    12
wenzelm@65342
    13
lemma swap_id_idempotent [simp]: "Fun.swap a b id \<circ> Fun.swap a b id = id"
wenzelm@65342
    14
  by (rule ext) (auto simp add: Fun.swap_def)
chaieb@29840
    15
wenzelm@65342
    16
lemma inv_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id"
wenzelm@54681
    17
  by (rule inv_unique_comp) simp_all
chaieb@29840
    18
wenzelm@65342
    19
lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
haftmann@56545
    20
  by (simp add: Fun.swap_def)
chaieb@29840
    21
hoelzl@64284
    22
lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
hoelzl@64284
    23
  using surj_f_inv_f[of p] by (auto simp add: bij_def)
hoelzl@64284
    24
hoelzl@64284
    25
lemma bij_swap_comp:
wenzelm@65342
    26
  assumes "bij p"
hoelzl@64284
    27
  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
wenzelm@65342
    28
  using surj_f_inv_f[OF bij_is_surj[OF \<open>bij p\<close>]]
wenzelm@65342
    29
  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF \<open>bij p\<close>])
hoelzl@64284
    30
wenzelm@65342
    31
lemma bij_swap_compose_bij:
wenzelm@65342
    32
  assumes "bij p"
wenzelm@65342
    33
  shows "bij (Fun.swap a b id \<circ> p)"
wenzelm@65342
    34
  by (simp only: bij_swap_comp[OF \<open>bij p\<close>] bij_swap_iff \<open>bij p\<close>)
hoelzl@64284
    35
wenzelm@54681
    36
wenzelm@60500
    37
subsection \<open>Basic consequences of the definition\<close>
wenzelm@54681
    38
wenzelm@54681
    39
definition permutes  (infixr "permutes" 41)
wenzelm@54681
    40
  where "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)"
chaieb@29840
    41
chaieb@29840
    42
lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
chaieb@29840
    43
  unfolding permutes_def by metis
hoelzl@64284
    44
wenzelm@65342
    45
lemma permutes_not_in: "f permutes S \<Longrightarrow> x \<notin> S \<Longrightarrow> f x = x"
wenzelm@65342
    46
  by (auto simp: permutes_def)
chaieb@29840
    47
wenzelm@54681
    48
lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S"
huffman@30488
    49
  unfolding permutes_def
nipkow@39302
    50
  apply (rule set_eqI)
chaieb@29840
    51
  apply (simp add: image_iff)
chaieb@29840
    52
  apply metis
chaieb@29840
    53
  done
chaieb@29840
    54
wenzelm@54681
    55
lemma permutes_inj: "p permutes S \<Longrightarrow> inj p"
wenzelm@64966
    56
  unfolding permutes_def inj_def by blast
chaieb@29840
    57
eberlm@63099
    58
lemma permutes_inj_on: "f permutes S \<Longrightarrow> inj_on f A"
wenzelm@65342
    59
  by (auto simp: permutes_def inj_on_def)
eberlm@63099
    60
wenzelm@54681
    61
lemma permutes_surj: "p permutes s \<Longrightarrow> surj p"
huffman@30488
    62
  unfolding permutes_def surj_def by metis
chaieb@29840
    63
nipkow@60601
    64
lemma permutes_bij: "p permutes s \<Longrightarrow> bij p"
wenzelm@65342
    65
  unfolding bij_def by (metis permutes_inj permutes_surj)
nipkow@60601
    66
hoelzl@59474
    67
lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S"
wenzelm@65342
    68
  by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI)
lp15@59669
    69
hoelzl@59474
    70
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
    71
  unfolding permutes_def bij_betw_def inj_on_def
hoelzl@59474
    72
  by auto (metis image_iff)+
hoelzl@59474
    73
wenzelm@54681
    74
lemma permutes_inv_o:
wenzelm@65342
    75
  assumes permutes: "p permutes S"
wenzelm@54681
    76
  shows "p \<circ> inv p = id"
wenzelm@54681
    77
    and "inv p \<circ> p = id"
wenzelm@65342
    78
  using permutes_inj[OF permutes] permutes_surj[OF permutes]
chaieb@29840
    79
  unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+
chaieb@29840
    80
huffman@30488
    81
lemma permutes_inverses:
chaieb@29840
    82
  fixes p :: "'a \<Rightarrow> 'a"
wenzelm@65342
    83
  assumes permutes: "p permutes S"
chaieb@29840
    84
  shows "p (inv p x) = x"
wenzelm@54681
    85
    and "inv p (p x) = x"
wenzelm@65342
    86
  using permutes_inv_o[OF permutes, unfolded fun_eq_iff o_def] by auto
chaieb@29840
    87
wenzelm@54681
    88
lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T"
chaieb@29840
    89
  unfolding permutes_def by blast
chaieb@29840
    90
chaieb@29840
    91
lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
wenzelm@65342
    92
  by (auto simp add: fun_eq_iff permutes_def)
chaieb@29840
    93
chaieb@29840
    94
lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
wenzelm@65342
    95
  by (simp add: fun_eq_iff permutes_def) metis  (*somewhat slow*)
huffman@30488
    96
chaieb@29840
    97
lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
wenzelm@65342
    98
  by (simp add: permutes_def)
chaieb@29840
    99
wenzelm@54681
   100
lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y"
wenzelm@54681
   101
  unfolding permutes_def inv_def
wenzelm@54681
   102
  apply auto
chaieb@29840
   103
  apply (erule allE[where x=y])
chaieb@29840
   104
  apply (erule allE[where x=y])
wenzelm@54681
   105
  apply (rule someI_ex)
wenzelm@54681
   106
  apply blast
chaieb@29840
   107
  apply (rule some1_equality)
chaieb@29840
   108
  apply blast
chaieb@29840
   109
  apply blast
chaieb@29840
   110
  done
chaieb@29840
   111
wenzelm@54681
   112
lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S"
haftmann@56545
   113
  unfolding permutes_def Fun.swap_def fun_upd_def by auto metis
chaieb@29840
   114
wenzelm@54681
   115
lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T"
wenzelm@54681
   116
  by (simp add: Ball_def permutes_def) metis
wenzelm@54681
   117
eberlm@63921
   118
(* Next three lemmas contributed by Lukas Bulwahn *)
eberlm@63921
   119
lemma permutes_bij_inv_into:
wenzelm@65342
   120
  fixes A :: "'a set"
wenzelm@65342
   121
    and B :: "'b set"
eberlm@63921
   122
  assumes "p permutes A"
wenzelm@65342
   123
    and "bij_betw f A B"
eberlm@63921
   124
  shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B"
eberlm@63921
   125
proof (rule bij_imp_permutes)
wenzelm@65342
   126
  from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A"
wenzelm@65342
   127
    by (auto simp add: permutes_imp_bij bij_betw_inv_into)
wenzelm@65342
   128
  then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B"
wenzelm@65342
   129
    by (simp add: bij_betw_trans)
wenzelm@65342
   130
  then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B"
wenzelm@65342
   131
    by (subst bij_betw_cong[where g="f \<circ> p \<circ> inv_into A f"]) auto
eberlm@63921
   132
next
eberlm@63921
   133
  fix x
eberlm@63921
   134
  assume "x \<notin> B"
wenzelm@65342
   135
  then show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto
eberlm@63921
   136
qed
eberlm@63921
   137
eberlm@63921
   138
lemma permutes_image_mset:
eberlm@63921
   139
  assumes "p permutes A"
eberlm@63921
   140
  shows "image_mset p (mset_set A) = mset_set A"
wenzelm@65342
   141
  using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image)
eberlm@63921
   142
eberlm@63921
   143
lemma permutes_implies_image_mset_eq:
eberlm@63921
   144
  assumes "p permutes A" "\<And>x. x \<in> A \<Longrightarrow> f x = f' (p x)"
eberlm@63921
   145
  shows "image_mset f' (mset_set A) = image_mset f (mset_set A)"
eberlm@63921
   146
proof -
wenzelm@65342
   147
  have "f x = f' (p x)" if "x \<in># mset_set A" for x
wenzelm@65342
   148
    using assms(2)[of x] that by (cases "finite A") auto
wenzelm@65342
   149
  with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)"
wenzelm@65342
   150
    by (auto intro!: image_mset_cong)
eberlm@63921
   151
  also have "\<dots> = image_mset f' (image_mset p (mset_set A))"
eberlm@63921
   152
    by (simp add: image_mset.compositionality)
eberlm@63921
   153
  also have "\<dots> = image_mset f' (mset_set A)"
eberlm@63921
   154
  proof -
wenzelm@65342
   155
    from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A"
wenzelm@65342
   156
      by blast
wenzelm@65342
   157
    then show ?thesis by simp
eberlm@63921
   158
  qed
eberlm@63921
   159
  finally show ?thesis ..
eberlm@63921
   160
qed
eberlm@63921
   161
chaieb@29840
   162
wenzelm@60500
   163
subsection \<open>Group properties\<close>
chaieb@29840
   164
wenzelm@54681
   165
lemma permutes_id: "id permutes S"
wenzelm@65342
   166
  by (simp add: permutes_def)
chaieb@29840
   167
wenzelm@54681
   168
lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S"
chaieb@29840
   169
  unfolding permutes_def o_def by metis
chaieb@29840
   170
wenzelm@54681
   171
lemma permutes_inv:
wenzelm@65342
   172
  assumes "p permutes S"
wenzelm@54681
   173
  shows "inv p permutes S"
wenzelm@65342
   174
  using assms unfolding permutes_def permutes_inv_eq[OF assms] by metis
chaieb@29840
   175
wenzelm@54681
   176
lemma permutes_inv_inv:
wenzelm@65342
   177
  assumes "p permutes S"
wenzelm@54681
   178
  shows "inv (inv p) = p"
wenzelm@65342
   179
  unfolding fun_eq_iff permutes_inv_eq[OF assms] permutes_inv_eq[OF permutes_inv[OF assms]]
chaieb@29840
   180
  by blast
chaieb@29840
   181
hoelzl@64284
   182
lemma permutes_invI:
eberlm@63099
   183
  assumes perm: "p permutes S"
wenzelm@65342
   184
    and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
wenzelm@65342
   185
    and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
wenzelm@65342
   186
  shows "inv p = p'"
eberlm@63099
   187
proof
wenzelm@65342
   188
  show "inv p x = p' x" for x
eberlm@63099
   189
  proof (cases "x \<in> S")
wenzelm@65342
   190
    case True
wenzelm@65342
   191
    from assms have "p' x = p' (p (inv p x))"
wenzelm@65342
   192
      by (simp add: permutes_inverses)
wenzelm@65342
   193
    also from permutes_inv[OF perm] True have "\<dots> = inv p x"
wenzelm@65342
   194
      by (subst inv) (simp_all add: permutes_in_image)
wenzelm@65342
   195
    finally show ?thesis ..
wenzelm@65342
   196
  next
wenzelm@65342
   197
    case False
wenzelm@65342
   198
    with permutes_inv[OF perm] show ?thesis
wenzelm@65342
   199
      by (simp_all add: outside permutes_not_in)
wenzelm@65342
   200
  qed
eberlm@63099
   201
qed
eberlm@63099
   202
eberlm@63099
   203
lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A"
hoelzl@64284
   204
  by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv])
eberlm@63099
   205
wenzelm@54681
   206
wenzelm@60500
   207
subsection \<open>The number of permutations on a finite set\<close>
chaieb@29840
   208
huffman@30488
   209
lemma permutes_insert_lemma:
wenzelm@65342
   210
  assumes "p permutes (insert a S)"
wenzelm@54681
   211
  shows "Fun.swap a (p a) id \<circ> p permutes S"
chaieb@29840
   212
  apply (rule permutes_superset[where S = "insert a S"])
wenzelm@65342
   213
  apply (rule permutes_compose[OF assms])
chaieb@29840
   214
  apply (rule permutes_swap_id, simp)
wenzelm@65342
   215
  using permutes_in_image[OF assms, of a]
wenzelm@54681
   216
  apply simp
haftmann@56545
   217
  apply (auto simp add: Ball_def Fun.swap_def)
chaieb@29840
   218
  done
chaieb@29840
   219
chaieb@29840
   220
lemma permutes_insert: "{p. p permutes (insert a S)} =
wenzelm@65342
   221
  (\<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
   222
proof -
wenzelm@65342
   223
  have "p permutes insert a S \<longleftrightarrow>
wenzelm@65342
   224
    (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p
wenzelm@65342
   225
  proof -
wenzelm@65342
   226
    have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S"
wenzelm@65342
   227
      if p: "p permutes insert a S"
wenzelm@65342
   228
    proof -
chaieb@29840
   229
      let ?b = "p a"
wenzelm@54681
   230
      let ?q = "Fun.swap a (p a) id \<circ> p"
wenzelm@65342
   231
      have *: "p = Fun.swap a ?b id \<circ> ?q"
wenzelm@65342
   232
        by (simp add: fun_eq_iff o_assoc)
wenzelm@65342
   233
      have **: "?b \<in> insert a S"
wenzelm@65342
   234
        unfolding permutes_in_image[OF p] by simp
wenzelm@65342
   235
      from permutes_insert_lemma[OF p] * ** show ?thesis
wenzelm@65342
   236
       by blast
wenzelm@65342
   237
    qed
wenzelm@65342
   238
    moreover have "p permutes insert a S"
wenzelm@65342
   239
      if bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" for b q
wenzelm@65342
   240
    proof -
wenzelm@65342
   241
      from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S"
wenzelm@54681
   242
        by auto
wenzelm@65342
   243
      have a: "a \<in> insert a S"
wenzelm@54681
   244
        by simp
wenzelm@65342
   245
      from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis
wenzelm@54681
   246
        by simp
wenzelm@65342
   247
    qed
wenzelm@65342
   248
    ultimately show ?thesis by blast
wenzelm@65342
   249
  qed
wenzelm@65342
   250
  then show ?thesis by auto
chaieb@29840
   251
qed
chaieb@29840
   252
wenzelm@54681
   253
lemma card_permutations:
wenzelm@65342
   254
  assumes "card S = n"
wenzelm@65342
   255
    and "finite S"
hoelzl@33715
   256
  shows "card {p. p permutes S} = fact n"
wenzelm@65342
   257
  using assms(2,1)
wenzelm@54681
   258
proof (induct arbitrary: n)
wenzelm@54681
   259
  case empty
wenzelm@54681
   260
  then show ?case by simp
hoelzl@33715
   261
next
hoelzl@33715
   262
  case (insert x F)
wenzelm@54681
   263
  {
wenzelm@54681
   264
    fix n
wenzelm@65342
   265
    assume card_insert: "card (insert x F) = n"
hoelzl@33715
   266
    let ?xF = "{p. p permutes insert x F}"
hoelzl@33715
   267
    let ?pF = "{p. p permutes F}"
hoelzl@33715
   268
    let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
hoelzl@33715
   269
    let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
wenzelm@65342
   270
    have xfgpF': "?xF = ?g ` ?pF'"
wenzelm@65342
   271
      by (rule permutes_insert[of x F])
wenzelm@65342
   272
    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1"
wenzelm@65342
   273
      by auto
wenzelm@65342
   274
    from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
wenzelm@65342
   275
      by auto
wenzelm@54681
   276
    then have "finite ?pF"
lp15@59730
   277
      by (auto intro: card_ge_0_finite)
wenzelm@65342
   278
    with \<open>finite F\<close> card_insert have pF'f: "finite ?pF'"
haftmann@61424
   279
      apply (simp only: Collect_case_prod Collect_mem_eq)
hoelzl@33715
   280
      apply (rule finite_cartesian_product)
hoelzl@33715
   281
      apply simp_all
hoelzl@33715
   282
      done
chaieb@29840
   283
hoelzl@33715
   284
    have ginj: "inj_on ?g ?pF'"
wenzelm@54681
   285
    proof -
hoelzl@33715
   286
      {
wenzelm@54681
   287
        fix b p c q
wenzelm@65342
   288
        assume bp: "(b, p) \<in> ?pF'"
wenzelm@65342
   289
        assume cq: "(c, q) \<in> ?pF'"
wenzelm@65342
   290
        assume eq: "?g (b, p) = ?g (c, q)"
wenzelm@65342
   291
        from bp cq have pF: "p permutes F" and qF: "q permutes F"
wenzelm@54681
   292
          by auto
wenzelm@65342
   293
        from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x"
wenzelm@65342
   294
          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
wenzelm@65342
   295
        also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x"
wenzelm@65342
   296
          by (auto simp: swap_def fun_upd_def fun_eq_iff)
wenzelm@65342
   297
        also from qF \<open>x \<notin> F\<close> have "\<dots> = c"
wenzelm@65342
   298
          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
wenzelm@65342
   299
        finally have "b = c" .
wenzelm@54681
   300
        then have "Fun.swap x b id = Fun.swap x c id"
wenzelm@54681
   301
          by simp
wenzelm@54681
   302
        with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q"
wenzelm@54681
   303
          by simp
wenzelm@65342
   304
        then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
wenzelm@54681
   305
          by simp
wenzelm@54681
   306
        then have "p = q"
wenzelm@54681
   307
          by (simp add: o_assoc)
wenzelm@65342
   308
        with \<open>b = c\<close> have "(b, p) = (c, q)"
wenzelm@54681
   309
          by simp
hoelzl@33715
   310
      }
wenzelm@54681
   311
      then show ?thesis
wenzelm@54681
   312
        unfolding inj_on_def by blast
hoelzl@33715
   313
    qed
wenzelm@65342
   314
    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0"
wenzelm@65342
   315
      by auto
wenzelm@54681
   316
    then have "\<exists>m. n = Suc m"
wenzelm@54681
   317
      by presburger
wenzelm@65342
   318
    then obtain m where n: "n = Suc m"
wenzelm@54681
   319
      by blast
wenzelm@65342
   320
    from pFs card_insert have *: "card ?xF = fact n"
wenzelm@54681
   321
      unfolding xfgpF' card_image[OF ginj]
wenzelm@60500
   322
      using \<open>finite F\<close> \<open>finite ?pF\<close>
wenzelm@65342
   323
      by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n)
wenzelm@54681
   324
    from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF"
wenzelm@65342
   325
      by (simp add: xfgpF' n)
wenzelm@65342
   326
    from * have "card ?xF = fact n"
wenzelm@65342
   327
      unfolding xFf by blast
hoelzl@33715
   328
  }
wenzelm@65342
   329
  with insert show ?case by simp
chaieb@29840
   330
qed
chaieb@29840
   331
wenzelm@54681
   332
lemma finite_permutations:
wenzelm@65342
   333
  assumes "finite S"
wenzelm@54681
   334
  shows "finite {p. p permutes S}"
wenzelm@65342
   335
  using card_permutations[OF refl assms] by (auto intro: card_ge_0_finite)
chaieb@29840
   336
wenzelm@54681
   337
wenzelm@60500
   338
subsection \<open>Permutations of index set for iterated operations\<close>
chaieb@29840
   339
haftmann@51489
   340
lemma (in comm_monoid_set) permute:
haftmann@51489
   341
  assumes "p permutes S"
wenzelm@54681
   342
  shows "F g S = F (g \<circ> p) S"
haftmann@51489
   343
proof -
wenzelm@60500
   344
  from \<open>p permutes S\<close> have "inj p"
wenzelm@54681
   345
    by (rule permutes_inj)
wenzelm@54681
   346
  then have "inj_on p S"
wenzelm@54681
   347
    by (auto intro: subset_inj_on)
wenzelm@54681
   348
  then have "F g (p ` S) = F (g \<circ> p) S"
wenzelm@54681
   349
    by (rule reindex)
wenzelm@60500
   350
  moreover from \<open>p permutes S\<close> have "p ` S = S"
wenzelm@54681
   351
    by (rule permutes_image)
wenzelm@54681
   352
  ultimately show ?thesis
wenzelm@54681
   353
    by simp
chaieb@29840
   354
qed
chaieb@29840
   355
wenzelm@54681
   356
wenzelm@60500
   357
subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close>
wenzelm@54681
   358
wenzelm@54681
   359
lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>
wenzelm@54681
   360
  Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id"
haftmann@56545
   361
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   362
wenzelm@54681
   363
lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow>
wenzelm@54681
   364
  Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id"
haftmann@56545
   365
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   366
wenzelm@54681
   367
lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow>
wenzelm@54681
   368
  Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id"
haftmann@56545
   369
  by (simp add: fun_eq_iff Fun.swap_def)
chaieb@29840
   370
wenzelm@54681
   371
wenzelm@60500
   372
subsection \<open>Permutations as transposition sequences\<close>
wenzelm@54681
   373
wenzelm@54681
   374
inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
wenzelm@65342
   375
  where
wenzelm@65342
   376
    id[simp]: "swapidseq 0 id"
wenzelm@65342
   377
  | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
wenzelm@54681
   378
wenzelm@54681
   379
declare id[unfolded id_def, simp]
wenzelm@54681
   380
wenzelm@54681
   381
definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
chaieb@29840
   382
chaieb@29840
   383
wenzelm@60500
   384
subsection \<open>Some closure properties of the set of permutations, with lengths\<close>
chaieb@29840
   385
wenzelm@54681
   386
lemma permutation_id[simp]: "permutation id"
wenzelm@54681
   387
  unfolding permutation_def by (rule exI[where x=0]) simp
chaieb@29840
   388
chaieb@29840
   389
declare permutation_id[unfolded id_def, simp]
chaieb@29840
   390
chaieb@29840
   391
lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
chaieb@29840
   392
  apply clarsimp
wenzelm@54681
   393
  using comp_Suc[of 0 id a b]
wenzelm@54681
   394
  apply simp
wenzelm@54681
   395
  done
chaieb@29840
   396
chaieb@29840
   397
lemma permutation_swap_id: "permutation (Fun.swap a b id)"
wenzelm@65342
   398
proof (cases "a = b")
wenzelm@65342
   399
  case True
wenzelm@65342
   400
  then show ?thesis by simp
wenzelm@65342
   401
next
wenzelm@65342
   402
  case False
wenzelm@65342
   403
  then show ?thesis
wenzelm@65342
   404
    unfolding permutation_def
wenzelm@65342
   405
    using swapidseq_swap[of a b] by blast
wenzelm@65342
   406
qed
wenzelm@65342
   407
chaieb@29840
   408
wenzelm@54681
   409
lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
wenzelm@54681
   410
proof (induct n p arbitrary: m q rule: swapidseq.induct)
wenzelm@54681
   411
  case (id m q)
wenzelm@54681
   412
  then show ?case by simp
wenzelm@54681
   413
next
wenzelm@54681
   414
  case (comp_Suc n p a b m q)
wenzelm@65342
   415
  have eq: "Suc n + m = Suc (n + m)"
wenzelm@54681
   416
    by arith
wenzelm@54681
   417
  show ?case
wenzelm@65342
   418
    apply (simp only: eq comp_assoc)
wenzelm@54681
   419
    apply (rule swapidseq.comp_Suc)
wenzelm@54681
   420
    using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
wenzelm@65342
   421
     apply blast+
wenzelm@54681
   422
    done
chaieb@29840
   423
qed
chaieb@29840
   424
wenzelm@54681
   425
lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)"
chaieb@29840
   426
  unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
chaieb@29840
   427
wenzelm@54681
   428
lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
wenzelm@65342
   429
  by (induct n p rule: swapidseq.induct)
wenzelm@65342
   430
    (use swapidseq_swap[of a b] in \<open>auto simp add: comp_assoc intro: swapidseq.comp_Suc\<close>)
chaieb@29840
   431
wenzelm@54681
   432
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
   433
proof (induct n p rule: swapidseq.induct)
wenzelm@54681
   434
  case id
wenzelm@54681
   435
  then show ?case
wenzelm@54681
   436
    by (rule exI[where x=id]) simp
huffman@30488
   437
next
chaieb@29840
   438
  case (comp_Suc n p a b)
wenzelm@54681
   439
  from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   440
    by blast
wenzelm@54681
   441
  let ?q = "q \<circ> Fun.swap a b id"
chaieb@29840
   442
  note H = comp_Suc.hyps
wenzelm@65342
   443
  from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (Fun.swap a b id)"
wenzelm@54681
   444
    by simp
wenzelm@65342
   445
  from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q"
wenzelm@54681
   446
    by simp
wenzelm@54681
   447
  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
   448
    by (simp add: o_assoc)
wenzelm@54681
   449
  also have "\<dots> = id"
wenzelm@54681
   450
    by (simp add: q(2))
wenzelm@65342
   451
  finally have ***: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
wenzelm@54681
   452
  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
   453
    by (simp only: o_assoc)
wenzelm@54681
   454
  then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
wenzelm@54681
   455
    by (simp add: q(3))
wenzelm@65342
   456
  with ** *** show ?case
wenzelm@54681
   457
    by blast
chaieb@29840
   458
qed
chaieb@29840
   459
wenzelm@54681
   460
lemma swapidseq_inverse:
wenzelm@65342
   461
  assumes "swapidseq n p"
wenzelm@54681
   462
  shows "swapidseq n (inv p)"
wenzelm@65342
   463
  using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto
wenzelm@54681
   464
wenzelm@54681
   465
lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
wenzelm@54681
   466
  using permutation_def swapidseq_inverse by blast
wenzelm@54681
   467
chaieb@29840
   468
wenzelm@60500
   469
subsection \<open>The identity map only has even transposition sequences\<close>
chaieb@29840
   470
wenzelm@54681
   471
lemma symmetry_lemma:
wenzelm@54681
   472
  assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c"
wenzelm@54681
   473
    and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
wenzelm@54681
   474
      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
   475
      P a b c d"
wenzelm@54681
   476
  shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>  P a b c d"
wenzelm@54681
   477
  using assms by metis
chaieb@29840
   478
wenzelm@54681
   479
lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
wenzelm@54681
   480
  Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
wenzelm@54681
   481
  (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
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
proof -
wenzelm@65342
   484
  assume neq: "a \<noteq> b" "c \<noteq> d"
wenzelm@54681
   485
  have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
wenzelm@54681
   486
    (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
wenzelm@54681
   487
      (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
wenzelm@54681
   488
        Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
wenzelm@54681
   489
    apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
wenzelm@65342
   490
     apply (simp_all only: swap_commute)
wenzelm@54681
   491
    apply (case_tac "a = c \<and> b = d")
wenzelm@65342
   492
     apply (clarsimp simp only: swap_commute swap_id_idempotent)
wenzelm@54681
   493
    apply (case_tac "a = c \<and> b \<noteq> d")
wenzelm@65342
   494
     apply (rule disjI2)
wenzelm@65342
   495
     apply (rule_tac x="b" in exI)
wenzelm@65342
   496
     apply (rule_tac x="d" in exI)
wenzelm@65342
   497
     apply (rule_tac x="b" in exI)
wenzelm@65342
   498
     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   499
    apply (case_tac "a \<noteq> c \<and> b = d")
wenzelm@65342
   500
     apply (rule disjI2)
wenzelm@65342
   501
     apply (rule_tac x="c" in exI)
wenzelm@65342
   502
     apply (rule_tac x="d" in exI)
wenzelm@65342
   503
     apply (rule_tac x="c" in exI)
wenzelm@65342
   504
     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   505
    apply (rule disjI2)
wenzelm@54681
   506
    apply (rule_tac x="c" in exI)
wenzelm@54681
   507
    apply (rule_tac x="d" in exI)
wenzelm@54681
   508
    apply (rule_tac x="b" in exI)
haftmann@56545
   509
    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
wenzelm@54681
   510
    done
wenzelm@65342
   511
  with neq show ?thesis by metis
chaieb@29840
   512
qed
chaieb@29840
   513
chaieb@29840
   514
lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
wenzelm@65342
   515
  using swapidseq.cases[of 0 p "p = id"] by auto
chaieb@29840
   516
wenzelm@54681
   517
lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
wenzelm@65342
   518
    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
   519
  apply (rule iffI)
wenzelm@65342
   520
   apply (erule swapidseq.cases[of n p])
wenzelm@65342
   521
    apply simp
wenzelm@65342
   522
   apply (rule disjI2)
wenzelm@65342
   523
   apply (rule_tac x= "a" in exI)
wenzelm@65342
   524
   apply (rule_tac x= "b" in exI)
wenzelm@65342
   525
   apply (rule_tac x= "pa" in exI)
wenzelm@65342
   526
   apply (rule_tac x= "na" in exI)
wenzelm@65342
   527
   apply simp
chaieb@29840
   528
  apply auto
chaieb@29840
   529
  apply (rule comp_Suc, simp_all)
chaieb@29840
   530
  done
wenzelm@54681
   531
chaieb@29840
   532
lemma fixing_swapidseq_decrease:
wenzelm@65342
   533
  assumes "swapidseq n p"
wenzelm@65342
   534
    and "a \<noteq> b"
wenzelm@65342
   535
    and "(Fun.swap a b id \<circ> p) a = a"
wenzelm@54681
   536
  shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
wenzelm@65342
   537
  using assms
wenzelm@54681
   538
proof (induct n arbitrary: p a b)
wenzelm@54681
   539
  case 0
wenzelm@54681
   540
  then show ?case
haftmann@56545
   541
    by (auto simp add: Fun.swap_def fun_upd_def)
chaieb@29840
   542
next
chaieb@29840
   543
  case (Suc n p a b)
wenzelm@54681
   544
  from Suc.prems(1) swapidseq_cases[of "Suc n" p]
wenzelm@54681
   545
  obtain c d q m where
wenzelm@54681
   546
    cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
chaieb@29840
   547
    by auto
wenzelm@65342
   548
  consider "Fun.swap a b id \<circ> Fun.swap c d id = id"
wenzelm@65342
   549
    | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
wenzelm@54681
   550
      "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
wenzelm@65342
   551
    using swap_general[OF Suc.prems(2) cdqm(4)] by metis
wenzelm@65342
   552
  then show ?case
wenzelm@65342
   553
  proof cases
wenzelm@65342
   554
    case 1
wenzelm@65342
   555
    then show ?thesis
wenzelm@65342
   556
      by (simp only: cdqm o_assoc) (simp add: cdqm)
wenzelm@65342
   557
  next
wenzelm@65342
   558
    case prems: 2
wenzelm@65342
   559
    then have az: "a \<noteq> z"
wenzelm@54681
   560
      by simp
wenzelm@65342
   561
    from prems have *: "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" for h
wenzelm@65342
   562
      by (simp add: Fun.swap_def)
wenzelm@54681
   563
    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
   564
      by simp
wenzelm@54681
   565
    then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
wenzelm@65342
   566
      by (simp add: o_assoc prems)
wenzelm@54681
   567
    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
   568
      by simp
wenzelm@54681
   569
    then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
wenzelm@54681
   570
      unfolding Suc by metis
wenzelm@65342
   571
    then have "(Fun.swap a z id \<circ> q) a = a"
wenzelm@65342
   572
      by (simp only: *)
wenzelm@65342
   573
    from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az this]
wenzelm@65342
   574
    have **: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
wenzelm@54681
   575
      by blast+
wenzelm@65342
   576
    from \<open>n \<noteq> 0\<close> have ***: "Suc n - 1 = Suc (n - 1)"
wenzelm@65342
   577
      by auto
wenzelm@65342
   578
    show ?thesis
wenzelm@65342
   579
      apply (simp only: cdqm(2) prems o_assoc ***)
haftmann@49739
   580
      apply (simp only: Suc_not_Zero simp_thms comp_assoc)
chaieb@29840
   581
      apply (rule comp_Suc)
wenzelm@65342
   582
      using ** prems
wenzelm@65342
   583
       apply blast+
wenzelm@54681
   584
      done
wenzelm@65342
   585
  qed
chaieb@29840
   586
qed
chaieb@29840
   587
huffman@30488
   588
lemma swapidseq_identity_even:
wenzelm@54681
   589
  assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)"
wenzelm@54681
   590
  shows "even n"
wenzelm@60500
   591
  using \<open>swapidseq n id\<close>
wenzelm@54681
   592
proof (induct n rule: nat_less_induct)
wenzelm@65342
   593
  case H: (1 n)
wenzelm@65342
   594
  consider "n = 0"
wenzelm@65342
   595
    | a b :: 'a and q m where "n = Suc m" "id = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
wenzelm@65342
   596
    using H(2)[unfolded swapidseq_cases[of n id]] by auto
wenzelm@65342
   597
  then show ?case
wenzelm@65342
   598
  proof cases
wenzelm@65342
   599
    case 1
wenzelm@65342
   600
    then show ?thesis by presburger
wenzelm@65342
   601
  next
wenzelm@65342
   602
    case h: 2
chaieb@29840
   603
    from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
wenzelm@54681
   604
    have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
wenzelm@54681
   605
      by auto
wenzelm@54681
   606
    from h m have mn: "m - 1 < n"
wenzelm@54681
   607
      by arith
wenzelm@65342
   608
    from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis
wenzelm@54681
   609
      by presburger
wenzelm@65342
   610
  qed
chaieb@29840
   611
qed
chaieb@29840
   612
wenzelm@54681
   613
wenzelm@60500
   614
subsection \<open>Therefore we have a welldefined notion of parity\<close>
chaieb@29840
   615
chaieb@29840
   616
definition "evenperm p = even (SOME n. swapidseq n p)"
chaieb@29840
   617
wenzelm@54681
   618
lemma swapidseq_even_even:
wenzelm@54681
   619
  assumes m: "swapidseq m p"
wenzelm@54681
   620
    and n: "swapidseq n p"
chaieb@29840
   621
  shows "even m \<longleftrightarrow> even n"
wenzelm@54681
   622
proof -
wenzelm@65342
   623
  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   624
    by blast
wenzelm@65342
   625
  from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis
wenzelm@54681
   626
    by arith
chaieb@29840
   627
qed
chaieb@29840
   628
wenzelm@54681
   629
lemma evenperm_unique:
wenzelm@54681
   630
  assumes p: "swapidseq n p"
wenzelm@54681
   631
    and n:"even n = b"
chaieb@29840
   632
  shows "evenperm p = b"
chaieb@29840
   633
  unfolding n[symmetric] evenperm_def
chaieb@29840
   634
  apply (rule swapidseq_even_even[where p = p])
wenzelm@65342
   635
   apply (rule someI[where x = n])
wenzelm@54681
   636
  using p
wenzelm@65342
   637
   apply blast+
wenzelm@54681
   638
  done
chaieb@29840
   639
wenzelm@54681
   640
wenzelm@60500
   641
subsection \<open>And it has the expected composition properties\<close>
chaieb@29840
   642
chaieb@29840
   643
lemma evenperm_id[simp]: "evenperm id = True"
wenzelm@54681
   644
  by (rule evenperm_unique[where n = 0]) simp_all
chaieb@29840
   645
chaieb@29840
   646
lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
wenzelm@54681
   647
  by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
chaieb@29840
   648
huffman@30488
   649
lemma evenperm_comp:
wenzelm@65342
   650
  assumes "permutation p" "permutation q"
wenzelm@65342
   651
  shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q"
wenzelm@54681
   652
proof -
wenzelm@65342
   653
  from assms obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
chaieb@29840
   654
    unfolding permutation_def by blast
wenzelm@65342
   655
  have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)"
wenzelm@54681
   656
    by arith
chaieb@29840
   657
  from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
wenzelm@65342
   658
    and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis
wenzelm@54681
   659
    by blast
chaieb@29840
   660
qed
chaieb@29840
   661
wenzelm@54681
   662
lemma evenperm_inv:
wenzelm@65342
   663
  assumes "permutation p"
chaieb@29840
   664
  shows "evenperm (inv p) = evenperm p"
wenzelm@54681
   665
proof -
wenzelm@65342
   666
  from assms obtain n where n: "swapidseq n p"
wenzelm@54681
   667
    unfolding permutation_def by blast
wenzelm@65342
   668
  show ?thesis
wenzelm@65342
   669
    by (rule evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]])
chaieb@29840
   670
qed
chaieb@29840
   671
chaieb@29840
   672
wenzelm@60500
   673
subsection \<open>A more abstract characterization of permutations\<close>
chaieb@29840
   674
chaieb@29840
   675
lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
wenzelm@64966
   676
  unfolding bij_def inj_def surj_def
chaieb@29840
   677
  apply auto
wenzelm@65342
   678
   apply metis
chaieb@29840
   679
  apply metis
chaieb@29840
   680
  done
chaieb@29840
   681
huffman@30488
   682
lemma permutation_bijective:
wenzelm@65342
   683
  assumes "permutation p"
chaieb@29840
   684
  shows "bij p"
wenzelm@54681
   685
proof -
wenzelm@65342
   686
  from assms obtain n where n: "swapidseq n p"
wenzelm@54681
   687
    unfolding permutation_def by blast
wenzelm@65342
   688
  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
wenzelm@54681
   689
    by blast
wenzelm@65342
   690
  then show ?thesis
wenzelm@65342
   691
    unfolding bij_iff
wenzelm@54681
   692
    apply (auto simp add: fun_eq_iff)
wenzelm@54681
   693
    apply metis
wenzelm@54681
   694
    done
huffman@30488
   695
qed
chaieb@29840
   696
wenzelm@54681
   697
lemma permutation_finite_support:
wenzelm@65342
   698
  assumes "permutation p"
chaieb@29840
   699
  shows "finite {x. p x \<noteq> x}"
wenzelm@54681
   700
proof -
wenzelm@65342
   701
  from assms obtain n where "swapidseq n p"
wenzelm@54681
   702
    unfolding permutation_def by blast
wenzelm@65342
   703
  then show ?thesis
wenzelm@54681
   704
  proof (induct n p rule: swapidseq.induct)
wenzelm@54681
   705
    case id
wenzelm@54681
   706
    then show ?case by simp
chaieb@29840
   707
  next
chaieb@29840
   708
    case (comp_Suc n p a b)
chaieb@29840
   709
    let ?S = "insert a (insert b {x. p x \<noteq> x})"
wenzelm@65342
   710
    from comp_Suc.hyps(2) have *: "finite ?S"
wenzelm@54681
   711
      by simp
wenzelm@65342
   712
    from \<open>a \<noteq> b\<close> have **: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
wenzelm@65342
   713
      by (auto simp: Fun.swap_def)
wenzelm@65342
   714
    show ?case
wenzelm@65342
   715
      by (rule finite_subset[OF ** *])
wenzelm@54681
   716
  qed
chaieb@29840
   717
qed
chaieb@29840
   718
huffman@30488
   719
lemma permutation_lemma:
wenzelm@65342
   720
  assumes "finite S"
wenzelm@65342
   721
    and "bij p"
wenzelm@65342
   722
    and "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
chaieb@29840
   723
  shows "permutation p"
wenzelm@65342
   724
  using assms
wenzelm@54681
   725
proof (induct S arbitrary: p rule: finite_induct)
wenzelm@65342
   726
  case empty
wenzelm@65342
   727
  then show ?case
wenzelm@65342
   728
    by simp
chaieb@29840
   729
next
chaieb@29840
   730
  case (insert a F p)
wenzelm@54681
   731
  let ?r = "Fun.swap a (p a) id \<circ> p"
wenzelm@54681
   732
  let ?q = "Fun.swap a (p a) id \<circ> ?r"
wenzelm@65342
   733
  have *: "?r a = a"
haftmann@56545
   734
    by (simp add: Fun.swap_def)
wenzelm@65342
   735
  from insert * have **: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
wenzelm@64966
   736
    by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3))
wenzelm@65342
   737
  have "bij ?r"
wenzelm@65342
   738
    by (rule bij_swap_compose_bij[OF insert(4)])
wenzelm@65342
   739
  have "permutation ?r"
wenzelm@65342
   740
    by (rule insert(3)[OF \<open>bij ?r\<close> **])
wenzelm@65342
   741
  then have "permutation ?q"
wenzelm@65342
   742
    by (simp add: permutation_compose permutation_swap_id)
wenzelm@54681
   743
  then show ?case
wenzelm@54681
   744
    by (simp add: o_assoc)
chaieb@29840
   745
qed
chaieb@29840
   746
huffman@30488
   747
lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
chaieb@29840
   748
  (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
chaieb@29840
   749
proof
wenzelm@65342
   750
  assume ?lhs
wenzelm@65342
   751
  with permutation_bijective permutation_finite_support show "?b \<and> ?f"
wenzelm@54681
   752
    by auto
chaieb@29840
   753
next
wenzelm@54681
   754
  assume "?b \<and> ?f"
wenzelm@54681
   755
  then have "?f" "?b" by blast+
wenzelm@54681
   756
  from permutation_lemma[OF this] show ?lhs
wenzelm@54681
   757
    by blast
chaieb@29840
   758
qed
chaieb@29840
   759
wenzelm@54681
   760
lemma permutation_inverse_works:
wenzelm@65342
   761
  assumes "permutation p"
wenzelm@54681
   762
  shows "inv p \<circ> p = id"
wenzelm@54681
   763
    and "p \<circ> inv p = id"
wenzelm@65342
   764
  using permutation_bijective [OF assms] by (auto simp: bij_def inj_iff surj_iff)
chaieb@29840
   765
chaieb@29840
   766
lemma permutation_inverse_compose:
wenzelm@54681
   767
  assumes p: "permutation p"
wenzelm@54681
   768
    and q: "permutation q"
wenzelm@54681
   769
  shows "inv (p \<circ> q) = inv q \<circ> inv p"
wenzelm@54681
   770
proof -
chaieb@29840
   771
  note ps = permutation_inverse_works[OF p]
chaieb@29840
   772
  note qs = permutation_inverse_works[OF q]
wenzelm@54681
   773
  have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p"
wenzelm@54681
   774
    by (simp add: o_assoc)
wenzelm@54681
   775
  also have "\<dots> = id"
wenzelm@54681
   776
    by (simp add: ps qs)
wenzelm@65342
   777
  finally have *: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
wenzelm@54681
   778
  have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
wenzelm@54681
   779
    by (simp add: o_assoc)
wenzelm@54681
   780
  also have "\<dots> = id"
wenzelm@54681
   781
    by (simp add: ps qs)
wenzelm@65342
   782
  finally have **: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
wenzelm@65342
   783
  show ?thesis
wenzelm@65342
   784
    by (rule inv_unique_comp[OF * **])
chaieb@29840
   785
qed
chaieb@29840
   786
wenzelm@54681
   787
wenzelm@65342
   788
subsection \<open>Relation to \<open>permutes\<close>\<close>
chaieb@29840
   789
chaieb@29840
   790
lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
wenzelm@54681
   791
  unfolding permutation permutes_def bij_iff[symmetric]
wenzelm@54681
   792
  apply (rule iffI, clarify)
wenzelm@65342
   793
   apply (rule exI[where x="{x. p x \<noteq> x}"])
wenzelm@65342
   794
   apply simp
wenzelm@54681
   795
  apply clarsimp
wenzelm@54681
   796
  apply (rule_tac B="S" in finite_subset)
wenzelm@65342
   797
   apply auto
wenzelm@54681
   798
  done
chaieb@29840
   799
wenzelm@54681
   800
wenzelm@60500
   801
subsection \<open>Hence a sort of induction principle composing by swaps\<close>
chaieb@29840
   802
wenzelm@54681
   803
lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow>
wenzelm@65342
   804
  (\<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
   805
  (\<And>p. p permutes S \<Longrightarrow> P p)"
wenzelm@54681
   806
proof (induct S rule: finite_induct)
wenzelm@54681
   807
  case empty
wenzelm@54681
   808
  then show ?case by auto
huffman@30488
   809
next
chaieb@29840
   810
  case (insert x F p)
wenzelm@54681
   811
  let ?r = "Fun.swap x (p x) id \<circ> p"
wenzelm@54681
   812
  let ?q = "Fun.swap x (p x) id \<circ> ?r"
wenzelm@54681
   813
  have qp: "?q = p"
wenzelm@54681
   814
    by (simp add: o_assoc)
wenzelm@54681
   815
  from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r"
wenzelm@54681
   816
    by blast
huffman@30488
   817
  from permutes_in_image[OF insert.prems(3), of x]
wenzelm@54681
   818
  have pxF: "p x \<in> insert x F"
wenzelm@54681
   819
    by simp
wenzelm@54681
   820
  have xF: "x \<in> insert x F"
wenzelm@54681
   821
    by simp
chaieb@29840
   822
  have rp: "permutation ?r"
wenzelm@65342
   823
    unfolding permutation_permutes
wenzelm@65342
   824
    using insert.hyps(1) permutes_insert_lemma[OF insert.prems(3)]
wenzelm@54681
   825
    by blast
wenzelm@65342
   826
  from insert.prems(2)[OF xF pxF Pr Pr rp] qp show ?case
wenzelm@65342
   827
    by (simp only:)
chaieb@29840
   828
qed
chaieb@29840
   829
wenzelm@54681
   830
wenzelm@60500
   831
subsection \<open>Sign of a permutation as a real number\<close>
chaieb@29840
   832
chaieb@29840
   833
definition "sign p = (if evenperm p then (1::int) else -1)"
chaieb@29840
   834
wenzelm@54681
   835
lemma sign_nz: "sign p \<noteq> 0"
wenzelm@54681
   836
  by (simp add: sign_def)
wenzelm@54681
   837
wenzelm@54681
   838
lemma sign_id: "sign id = 1"
wenzelm@54681
   839
  by (simp add: sign_def)
wenzelm@54681
   840
wenzelm@54681
   841
lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p"
chaieb@29840
   842
  by (simp add: sign_def evenperm_inv)
wenzelm@54681
   843
wenzelm@54681
   844
lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q"
wenzelm@54681
   845
  by (simp add: sign_def evenperm_comp)
wenzelm@54681
   846
chaieb@29840
   847
lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
chaieb@29840
   848
  by (simp add: sign_def evenperm_swap)
chaieb@29840
   849
wenzelm@54681
   850
lemma sign_idempotent: "sign p * sign p = 1"
wenzelm@54681
   851
  by (simp add: sign_def)
wenzelm@54681
   852
hoelzl@64284
   853
eberlm@63099
   854
subsection \<open>Permuting a list\<close>
eberlm@63099
   855
eberlm@63099
   856
text \<open>This function permutes a list by applying a permutation to the indices.\<close>
eberlm@63099
   857
wenzelm@65342
   858
definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@65342
   859
  where "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
eberlm@63099
   860
hoelzl@64284
   861
lemma permute_list_map:
eberlm@63099
   862
  assumes "f permutes {..<length xs}"
wenzelm@65342
   863
  shows "permute_list f (map g xs) = map g (permute_list f xs)"
eberlm@63099
   864
  using permutes_in_image[OF assms] by (auto simp: permute_list_def)
eberlm@63099
   865
eberlm@63099
   866
lemma permute_list_nth:
eberlm@63099
   867
  assumes "f permutes {..<length xs}" "i < length xs"
wenzelm@65342
   868
  shows "permute_list f xs ! i = xs ! f i"
hoelzl@64284
   869
  using permutes_in_image[OF assms(1)] assms(2)
eberlm@63099
   870
  by (simp add: permute_list_def)
eberlm@63099
   871
eberlm@63099
   872
lemma permute_list_Nil [simp]: "permute_list f [] = []"
eberlm@63099
   873
  by (simp add: permute_list_def)
eberlm@63099
   874
eberlm@63099
   875
lemma length_permute_list [simp]: "length (permute_list f xs) = length xs"
eberlm@63099
   876
  by (simp add: permute_list_def)
eberlm@63099
   877
hoelzl@64284
   878
lemma permute_list_compose:
eberlm@63099
   879
  assumes "g permutes {..<length xs}"
wenzelm@65342
   880
  shows "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
eberlm@63099
   881
  using assms[THEN permutes_in_image] by (auto simp add: permute_list_def)
eberlm@63099
   882
eberlm@63099
   883
lemma permute_list_ident [simp]: "permute_list (\<lambda>x. x) xs = xs"
eberlm@63099
   884
  by (simp add: permute_list_def map_nth)
eberlm@63099
   885
eberlm@63099
   886
lemma permute_list_id [simp]: "permute_list id xs = xs"
eberlm@63099
   887
  by (simp add: id_def)
eberlm@63099
   888
eberlm@63099
   889
lemma mset_permute_list [simp]:
wenzelm@65342
   890
  fixes xs :: "'a list"
wenzelm@65342
   891
  assumes "f permutes {..<length xs}"
wenzelm@65342
   892
  shows "mset (permute_list f xs) = mset xs"
eberlm@63099
   893
proof (rule multiset_eqI)
eberlm@63099
   894
  fix y :: 'a
eberlm@63099
   895
  from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x
eberlm@63099
   896
    using permutes_in_image[OF assms] by auto
wenzelm@65342
   897
  have "count (mset (permute_list f xs)) y = card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
bulwahn@64543
   898
    by (simp add: permute_list_def count_image_mset atLeast0LessThan)
eberlm@63099
   899
  also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
eberlm@63099
   900
    by auto
eberlm@63099
   901
  also from assms have "card \<dots> = card {i. i < length xs \<and> y = xs ! i}"
eberlm@63099
   902
    by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj)
wenzelm@65342
   903
  also have "\<dots> = count (mset xs) y"
wenzelm@65342
   904
    by (simp add: count_mset length_filter_conv_card)
wenzelm@65342
   905
  finally show "count (mset (permute_list f xs)) y = count (mset xs) y"
wenzelm@65342
   906
    by simp
eberlm@63099
   907
qed
eberlm@63099
   908
hoelzl@64284
   909
lemma set_permute_list [simp]:
eberlm@63099
   910
  assumes "f permutes {..<length xs}"
wenzelm@65342
   911
  shows "set (permute_list f xs) = set xs"
eberlm@63099
   912
  by (rule mset_eq_setD[OF mset_permute_list]) fact
eberlm@63099
   913
eberlm@63099
   914
lemma distinct_permute_list [simp]:
eberlm@63099
   915
  assumes "f permutes {..<length xs}"
wenzelm@65342
   916
  shows "distinct (permute_list f xs) = distinct xs"
eberlm@63099
   917
  by (simp add: distinct_count_atmost_1 assms)
eberlm@63099
   918
hoelzl@64284
   919
lemma permute_list_zip:
eberlm@63099
   920
  assumes "f permutes A" "A = {..<length xs}"
eberlm@63099
   921
  assumes [simp]: "length xs = length ys"
wenzelm@65342
   922
  shows "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
eberlm@63099
   923
proof -
wenzelm@65342
   924
  from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i
wenzelm@65342
   925
    by simp
eberlm@63099
   926
  have "permute_list f (zip xs ys) = map (\<lambda>i. zip xs ys ! f i) [0..<length ys]"
eberlm@63099
   927
    by (simp_all add: permute_list_def zip_map_map)
eberlm@63099
   928
  also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])"
wenzelm@65342
   929
    by (intro nth_equalityI) (simp_all add: *)
eberlm@63099
   930
  also have "\<dots> = zip (permute_list f xs) (permute_list f ys)"
eberlm@63099
   931
    by (simp_all add: permute_list_def zip_map_map)
eberlm@63099
   932
  finally show ?thesis .
eberlm@63099
   933
qed
eberlm@63099
   934
hoelzl@64284
   935
lemma map_of_permute:
eberlm@63099
   936
  assumes "\<sigma> permutes fst ` set xs"
wenzelm@65342
   937
  shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)"
wenzelm@65342
   938
    (is "_ = map_of (map ?f _)")
eberlm@63099
   939
proof
wenzelm@65342
   940
  from assms have "inj \<sigma>" "surj \<sigma>"
wenzelm@65342
   941
    by (simp_all add: permutes_inj permutes_surj)
wenzelm@65342
   942
  then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x
wenzelm@65342
   943
    by (induct xs) (auto simp: inv_f_f surj_f_inv_f)
eberlm@63099
   944
qed
eberlm@63099
   945
wenzelm@54681
   946
wenzelm@60500
   947
subsection \<open>More lemmas about permutations\<close>
chaieb@29840
   948
wenzelm@65342
   949
text \<open>The following few lemmas were contributed by Lukas Bulwahn.\<close>
eberlm@63921
   950
eberlm@63921
   951
lemma count_image_mset_eq_card_vimage:
eberlm@63921
   952
  assumes "finite A"
eberlm@63921
   953
  shows "count (image_mset f (mset_set A)) b = card {a \<in> A. f a = b}"
eberlm@63921
   954
  using assms
eberlm@63921
   955
proof (induct A)
eberlm@63921
   956
  case empty
eberlm@63921
   957
  show ?case by simp
eberlm@63921
   958
next
eberlm@63921
   959
  case (insert x F)
eberlm@63921
   960
  show ?case
wenzelm@65342
   961
  proof (cases "f x = b")
wenzelm@65342
   962
    case True
wenzelm@65342
   963
    with insert.hyps
wenzelm@65342
   964
    have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
wenzelm@65342
   965
      by auto
wenzelm@65342
   966
    also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
wenzelm@65342
   967
      by simp
wenzelm@65342
   968
    also from \<open>f x = b\<close> have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
wenzelm@65342
   969
      by (auto intro: arg_cong[where f="card"])
wenzelm@65342
   970
    finally show ?thesis
wenzelm@65342
   971
      using insert by auto
eberlm@63921
   972
  next
wenzelm@65342
   973
    case False
wenzelm@65342
   974
    then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
wenzelm@65342
   975
      by auto
wenzelm@65342
   976
    with insert False show ?thesis
wenzelm@65342
   977
      by simp
eberlm@63921
   978
  qed
eberlm@63921
   979
qed
hoelzl@64284
   980
eberlm@63921
   981
(* Prove image_mset_eq_implies_permutes *)
eberlm@63921
   982
lemma image_mset_eq_implies_permutes:
eberlm@63921
   983
  fixes f :: "'a \<Rightarrow> 'b"
eberlm@63921
   984
  assumes "finite A"
wenzelm@65342
   985
    and mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)"
eberlm@63921
   986
  obtains p where "p permutes A" and "\<forall>x\<in>A. f x = f' (p x)"
eberlm@63099
   987
proof -
eberlm@63921
   988
  from \<open>finite A\<close> have [simp]: "finite {a \<in> A. f a = (b::'b)}" for f b by auto
eberlm@63921
   989
  have "f ` A = f' ` A"
eberlm@63921
   990
  proof -
wenzelm@65342
   991
    from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))"
wenzelm@65342
   992
      by simp
wenzelm@65342
   993
    also have "\<dots> = f' ` set_mset (mset_set A)"
eberlm@63921
   994
      by (metis mset_eq multiset.set_map)
wenzelm@65342
   995
    also from \<open>finite A\<close> have "\<dots> = f' ` A"
wenzelm@65342
   996
      by simp
eberlm@63921
   997
    finally show ?thesis .
eberlm@63921
   998
  qed
eberlm@63921
   999
  have "\<forall>b\<in>(f ` A). \<exists>p. bij_betw p {a \<in> A. f a = b} {a \<in> A. f' a = b}"
eberlm@63099
  1000
  proof
eberlm@63921
  1001
    fix b
wenzelm@65342
  1002
    from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b"
wenzelm@65342
  1003
      by simp
wenzelm@65342
  1004
    with \<open>finite A\<close> have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
eberlm@63921
  1005
      by (simp add: count_image_mset_eq_card_vimage)
wenzelm@65342
  1006
    then show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
eberlm@63099
  1007
      by (intro finite_same_card_bij) simp_all
eberlm@63099
  1008
  qed
wenzelm@65342
  1009
  then have "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
eberlm@63099
  1010
    by (rule bchoice)
wenzelm@65342
  1011
  then obtain p where p: "\<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}" ..
eberlm@63921
  1012
  define p' where "p' = (\<lambda>a. if a \<in> A then p (f a) a else a)"
eberlm@63921
  1013
  have "p' permutes A"
eberlm@63921
  1014
  proof (rule bij_imp_permutes)
eberlm@63921
  1015
    have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
wenzelm@65342
  1016
      by (auto simp: disjoint_family_on_def)
wenzelm@65342
  1017
    moreover
wenzelm@65342
  1018
    have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if "b \<in> f ` A" for b
wenzelm@65342
  1019
      using p that by (subst bij_betw_cong[where g="p b"]) auto
wenzelm@65342
  1020
    ultimately
wenzelm@65342
  1021
    have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
eberlm@63921
  1022
      by (rule bij_betw_UNION_disjoint)
wenzelm@65342
  1023
    moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
wenzelm@65342
  1024
      by auto
wenzelm@65342
  1025
    moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
wenzelm@65342
  1026
      by auto
eberlm@63921
  1027
    ultimately show "bij_betw p' A A"
eberlm@63921
  1028
      unfolding p'_def by (subst bij_betw_cong[where g="(\<lambda>a. p (f a) a)"]) auto
eberlm@63921
  1029
  next
wenzelm@65342
  1030
    show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x"
wenzelm@65342
  1031
      by (simp add: p'_def)
eberlm@63099
  1032
  qed
eberlm@63921
  1033
  moreover from p have "\<forall>x\<in>A. f x = f' (p' x)"
eberlm@63921
  1034
    unfolding p'_def using bij_betwE by fastforce
wenzelm@65342
  1035
  ultimately show ?thesis ..
eberlm@63921
  1036
qed
eberlm@63099
  1037
wenzelm@65342
  1038
lemma mset_set_upto_eq_mset_upto: "mset_set {..<n} = mset [0..<n]"
wenzelm@65342
  1039
  by (induct n) (auto simp: add.commute lessThan_Suc)
eberlm@63099
  1040
eberlm@63921
  1041
(* and derive the existing property: *)
eberlm@63921
  1042
lemma mset_eq_permutation:
wenzelm@65342
  1043
  fixes xs ys :: "'a list"
wenzelm@65342
  1044
  assumes mset_eq: "mset xs = mset ys"
eberlm@63921
  1045
  obtains p where "p permutes {..<length ys}" "permute_list p ys = xs"
eberlm@63921
  1046
proof -
eberlm@63921
  1047
  from mset_eq have length_eq: "length xs = length ys"
wenzelm@65342
  1048
    by (rule mset_eq_length)
eberlm@63921
  1049
  have "mset_set {..<length ys} = mset [0..<length ys]"
wenzelm@65342
  1050
    by (rule mset_set_upto_eq_mset_upto)
wenzelm@65342
  1051
  with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
wenzelm@65342
  1052
    image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
eberlm@63921
  1053
    by (metis map_nth mset_map)
eberlm@63921
  1054
  from image_mset_eq_implies_permutes[OF _ this]
wenzelm@65342
  1055
  obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
wenzelm@65342
  1056
    by auto
wenzelm@65342
  1057
  with length_eq have "permute_list p ys = xs"
wenzelm@65342
  1058
    by (auto intro!: nth_equalityI simp: permute_list_nth)
wenzelm@65342
  1059
  with p show thesis ..
eberlm@63099
  1060
qed
eberlm@63099
  1061
chaieb@29840
  1062
lemma permutes_natset_le:
wenzelm@54681
  1063
  fixes S :: "'a::wellorder set"
wenzelm@65342
  1064
  assumes "p permutes S"
wenzelm@65342
  1065
    and "\<forall>i \<in> S. p i \<le> i"
wenzelm@54681
  1066
  shows "p = id"
wenzelm@54681
  1067
proof -
wenzelm@65342
  1068
  have "p n = n" for n
wenzelm@65342
  1069
    using assms
wenzelm@65342
  1070
  proof (induct n arbitrary: S rule: less_induct)
wenzelm@65342
  1071
    case (less n)
wenzelm@65342
  1072
    show ?case
wenzelm@65342
  1073
    proof (cases "n \<in> S")
wenzelm@65342
  1074
      case False
wenzelm@65342
  1075
      with less(2) show ?thesis
wenzelm@65342
  1076
        unfolding permutes_def by metis
wenzelm@65342
  1077
    next
wenzelm@65342
  1078
      case True
wenzelm@65342
  1079
      with less(3) have "p n < n \<or> p n = n"
wenzelm@65342
  1080
        by auto
wenzelm@65342
  1081
      then show ?thesis
wenzelm@65342
  1082
      proof
wenzelm@65342
  1083
        assume "p n < n"
wenzelm@65342
  1084
        with less have "p (p n) = p n"
wenzelm@65342
  1085
          by metis
wenzelm@65342
  1086
        with permutes_inj[OF less(2)] have "p n = n"
wenzelm@65342
  1087
          unfolding inj_def by blast
wenzelm@65342
  1088
        with \<open>p n < n\<close> have False
wenzelm@65342
  1089
          by simp
wenzelm@65342
  1090
        then show ?thesis ..
wenzelm@65342
  1091
      qed
wenzelm@54681
  1092
    qed
wenzelm@65342
  1093
  qed
wenzelm@65342
  1094
  then show ?thesis by (auto simp: fun_eq_iff)
chaieb@29840
  1095
qed
chaieb@29840
  1096
chaieb@29840
  1097
lemma permutes_natset_ge:
wenzelm@54681
  1098
  fixes S :: "'a::wellorder set"
wenzelm@54681
  1099
  assumes p: "p permutes S"
wenzelm@54681
  1100
    and le: "\<forall>i \<in> S. p i \<ge> i"
wenzelm@54681
  1101
  shows "p = id"
wenzelm@54681
  1102
proof -
wenzelm@65342
  1103
  have "i \<ge> inv p i" if "i \<in> S" for i
wenzelm@65342
  1104
  proof -
wenzelm@65342
  1105
    from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
wenzelm@54681
  1106
      by simp
wenzelm@54681
  1107
    with le have "p (inv p i) \<ge> inv p i"
wenzelm@54681
  1108
      by blast
wenzelm@65342
  1109
    with permutes_inverses[OF p] show ?thesis
wenzelm@54681
  1110
      by simp
wenzelm@65342
  1111
  qed
wenzelm@65342
  1112
  then have "\<forall>i\<in>S. inv p i \<le> i"
wenzelm@54681
  1113
    by blast
wenzelm@65342
  1114
  from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id"
wenzelm@54681
  1115
    by simp
huffman@30488
  1116
  then show ?thesis
chaieb@29840
  1117
    apply (subst permutes_inv_inv[OF p, symmetric])
chaieb@29840
  1118
    apply (rule inv_unique_comp)
wenzelm@65342
  1119
     apply simp_all
chaieb@29840
  1120
    done
chaieb@29840
  1121
qed
chaieb@29840
  1122
chaieb@29840
  1123
lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
wenzelm@54681
  1124
  apply (rule set_eqI)
wenzelm@54681
  1125
  apply auto
wenzelm@54681
  1126
  using permutes_inv_inv permutes_inv
wenzelm@65342
  1127
   apply auto
chaieb@29840
  1128
  apply (rule_tac x="inv x" in exI)
chaieb@29840
  1129
  apply auto
chaieb@29840
  1130
  done
chaieb@29840
  1131
huffman@30488
  1132
lemma image_compose_permutations_left:
wenzelm@65342
  1133
  assumes "q permutes S"
wenzelm@65342
  1134
  shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
wenzelm@54681
  1135
  apply (rule set_eqI)
wenzelm@54681
  1136
  apply auto
wenzelm@65342
  1137
   apply (rule permutes_compose)
wenzelm@65342
  1138
  using assms
wenzelm@65342
  1139
    apply auto
wenzelm@54681
  1140
  apply (rule_tac x = "inv q \<circ> x" in exI)
wenzelm@54681
  1141
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
wenzelm@54681
  1142
  done
chaieb@29840
  1143
chaieb@29840
  1144
lemma image_compose_permutations_right:
wenzelm@65342
  1145
  assumes "q permutes S"
wenzelm@54681
  1146
  shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
wenzelm@54681
  1147
  apply (rule set_eqI)
wenzelm@54681
  1148
  apply auto
wenzelm@65342
  1149
   apply (rule permutes_compose)
wenzelm@65342
  1150
  using assms
wenzelm@65342
  1151
    apply auto
wenzelm@54681
  1152
  apply (rule_tac x = "x \<circ> inv q" in exI)
wenzelm@54681
  1153
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
wenzelm@54681
  1154
  done
chaieb@29840
  1155
wenzelm@54681
  1156
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
  1157
  by (simp add: permutes_def) metis
chaieb@29840
  1158
wenzelm@65342
  1159
lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
wenzelm@54681
  1160
  (is "?lhs = ?rhs")
wenzelm@54681
  1161
proof -
huffman@30036
  1162
  let ?S = "{p . p permutes S}"
wenzelm@65342
  1163
  have *: "inj_on inv ?S"
wenzelm@54681
  1164
  proof (auto simp add: inj_on_def)
wenzelm@54681
  1165
    fix q r
wenzelm@54681
  1166
    assume q: "q permutes S"
wenzelm@54681
  1167
      and r: "r permutes S"
wenzelm@54681
  1168
      and qr: "inv q = inv r"
wenzelm@54681
  1169
    then have "inv (inv q) = inv (inv r)"
wenzelm@54681
  1170
      by simp
wenzelm@54681
  1171
    with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
wenzelm@54681
  1172
      by metis
wenzelm@54681
  1173
  qed
wenzelm@65342
  1174
  have **: "inv ` ?S = ?S"
wenzelm@54681
  1175
    using image_inverse_permutations by blast
wenzelm@65342
  1176
  have ***: "?rhs = sum (f \<circ> inv) ?S"
wenzelm@54681
  1177
    by (simp add: o_def)
wenzelm@65342
  1178
  from sum.reindex[OF *, of f] show ?thesis
wenzelm@65342
  1179
    by (simp only: ** ***)
chaieb@29840
  1180
qed
chaieb@29840
  1181
chaieb@29840
  1182
lemma setum_permutations_compose_left:
huffman@30036
  1183
  assumes q: "q permutes S"
nipkow@64267
  1184
  shows "sum f {p. p permutes S} = sum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
wenzelm@54681
  1185
  (is "?lhs = ?rhs")
wenzelm@54681
  1186
proof -
huffman@30036
  1187
  let ?S = "{p. p permutes S}"
wenzelm@65342
  1188
  have *: "?rhs = sum (f \<circ> (op \<circ> q)) ?S"
wenzelm@54681
  1189
    by (simp add: o_def)
wenzelm@65342
  1190
  have **: "inj_on (op \<circ> q) ?S"
wenzelm@54681
  1191
  proof (auto simp add: inj_on_def)
chaieb@29840
  1192
    fix p r
wenzelm@54681
  1193
    assume "p permutes S"
wenzelm@54681
  1194
      and r: "r permutes S"
wenzelm@54681
  1195
      and rp: "q \<circ> p = q \<circ> r"
wenzelm@54681
  1196
    then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r"
wenzelm@54681
  1197
      by (simp add: comp_assoc)
wenzelm@54681
  1198
    with permutes_inj[OF q, unfolded inj_iff] show "p = r"
wenzelm@54681
  1199
      by simp
chaieb@29840
  1200
  qed
wenzelm@65342
  1201
  have "(op \<circ> q) ` ?S = ?S"
wenzelm@54681
  1202
    using image_compose_permutations_left[OF q] by auto
wenzelm@65342
  1203
  with * sum.reindex[OF **, of f] show ?thesis
wenzelm@65342
  1204
    by (simp only:)
chaieb@29840
  1205
qed
chaieb@29840
  1206
chaieb@29840
  1207
lemma sum_permutations_compose_right:
huffman@30036
  1208
  assumes q: "q permutes S"
nipkow@64267
  1209
  shows "sum f {p. p permutes S} = sum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
wenzelm@54681
  1210
  (is "?lhs = ?rhs")
wenzelm@54681
  1211
proof -
huffman@30036
  1212
  let ?S = "{p. p permutes S}"
wenzelm@65342
  1213
  have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
wenzelm@54681
  1214
    by (simp add: o_def)
wenzelm@65342
  1215
  have **: "inj_on (\<lambda>p. p \<circ> q) ?S"
wenzelm@54681
  1216
  proof (auto simp add: inj_on_def)
chaieb@29840
  1217
    fix p r
wenzelm@54681
  1218
    assume "p permutes S"
wenzelm@54681
  1219
      and r: "r permutes S"
wenzelm@54681
  1220
      and rp: "p \<circ> q = r \<circ> q"
wenzelm@54681
  1221
    then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)"
wenzelm@54681
  1222
      by (simp add: o_assoc)
wenzelm@54681
  1223
    with permutes_surj[OF q, unfolded surj_iff] show "p = r"
wenzelm@54681
  1224
      by simp
chaieb@29840
  1225
  qed
wenzelm@65342
  1226
  from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S"
wenzelm@65342
  1227
    by auto
wenzelm@65342
  1228
  with * sum.reindex[OF **, of f] show ?thesis
wenzelm@65342
  1229
    by (simp only:)
chaieb@29840
  1230
qed
chaieb@29840
  1231
wenzelm@54681
  1232
wenzelm@60500
  1233
subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close>
chaieb@29840
  1234
nipkow@64267
  1235
lemma sum_over_permutations_insert:
wenzelm@54681
  1236
  assumes fS: "finite S"
wenzelm@54681
  1237
    and aS: "a \<notin> S"
nipkow@64267
  1238
  shows "sum f {p. p permutes (insert a S)} =
nipkow@64267
  1239
    sum (\<lambda>b. sum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
wenzelm@54681
  1240
proof -
wenzelm@65342
  1241
  have *: "\<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
  1242
    by (simp add: fun_eq_iff)
wenzelm@65342
  1243
  have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
wenzelm@54681
  1244
    by blast
huffman@30488
  1245
  show ?thesis
wenzelm@65342
  1246
    unfolding * ** sum.cartesian_product permutes_insert
nipkow@64267
  1247
  proof (rule sum.reindex)
chaieb@29840
  1248
    let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
chaieb@29840
  1249
    let ?P = "{p. p permutes S}"
wenzelm@54681
  1250
    {
wenzelm@54681
  1251
      fix b c p q
wenzelm@54681
  1252
      assume b: "b \<in> insert a S"
wenzelm@54681
  1253
      assume c: "c \<in> insert a S"
wenzelm@54681
  1254
      assume p: "p permutes S"
wenzelm@54681
  1255
      assume q: "q permutes S"
wenzelm@54681
  1256
      assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q"
chaieb@29840
  1257
      from p q aS have pa: "p a = a" and qa: "q a = a"
wenzelm@32960
  1258
        unfolding permutes_def by metis+
wenzelm@54681
  1259
      from eq have "(Fun.swap a b id \<circ> p) a  = (Fun.swap a c id \<circ> q) a"
wenzelm@54681
  1260
        by simp
wenzelm@54681
  1261
      then have bc: "b = c"
haftmann@56545
  1262
        by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def
nipkow@62390
  1263
            cong del: if_weak_cong split: if_split_asm)
wenzelm@54681
  1264
      from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
wenzelm@54681
  1265
        (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
wenzelm@54681
  1266
      then have "p = q"
wenzelm@65342
  1267
        unfolding o_assoc swap_id_idempotent by simp
wenzelm@54681
  1268
      with bc have "b = c \<and> p = q"
wenzelm@54681
  1269
        by blast
chaieb@29840
  1270
    }
huffman@30488
  1271
    then show "inj_on ?f (insert a S \<times> ?P)"
wenzelm@54681
  1272
      unfolding inj_on_def by clarify metis
chaieb@29840
  1273
  qed
chaieb@29840
  1274
qed
chaieb@29840
  1275
eberlm@63099
  1276
eberlm@63099
  1277
subsection \<open>Constructing permutations from association lists\<close>
eberlm@63099
  1278
wenzelm@65342
  1279
definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
wenzelm@65342
  1280
  where "list_permutes xs A \<longleftrightarrow>
wenzelm@65342
  1281
    set (map fst xs) \<subseteq> A \<and>
wenzelm@65342
  1282
    set (map snd xs) = set (map fst xs) \<and>
wenzelm@65342
  1283
    distinct (map fst xs) \<and>
wenzelm@65342
  1284
    distinct (map snd xs)"
eberlm@63099
  1285
eberlm@63099
  1286
lemma list_permutesI [simp]:
eberlm@63099
  1287
  assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)"
wenzelm@65342
  1288
  shows "list_permutes xs A"
eberlm@63099
  1289
proof -
eberlm@63099
  1290
  from assms(2,3) have "distinct (map snd xs)"
eberlm@63099
  1291
    by (intro card_distinct) (simp_all add: distinct_card del: set_map)
wenzelm@65342
  1292
  with assms show ?thesis
wenzelm@65342
  1293
    by (simp add: list_permutes_def)
eberlm@63099
  1294
qed
eberlm@63099
  1295
wenzelm@65342
  1296
definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@65342
  1297
  where "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)"
eberlm@63099
  1298
eberlm@63099
  1299
lemma permutation_of_list_Cons:
wenzelm@65342
  1300
  "permutation_of_list ((x, y) # xs) x' = (if x = x' then y else permutation_of_list xs x')"
eberlm@63099
  1301
  by (simp add: permutation_of_list_def)
eberlm@63099
  1302
wenzelm@65342
  1303
fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
wenzelm@65342
  1304
  where
wenzelm@65342
  1305
    "inverse_permutation_of_list [] x = x"
wenzelm@65342
  1306
  | "inverse_permutation_of_list ((y, x') # xs) x =
wenzelm@65342
  1307
      (if x = x' then y else inverse_permutation_of_list xs x)"
eberlm@63099
  1308
eberlm@63099
  1309
declare inverse_permutation_of_list.simps [simp del]
eberlm@63099
  1310
eberlm@63099
  1311
lemma inj_on_map_of:
eberlm@63099
  1312
  assumes "distinct (map snd xs)"
wenzelm@65342
  1313
  shows "inj_on (map_of xs) (set (map fst xs))"
eberlm@63099
  1314
proof (rule inj_onI)
wenzelm@65342
  1315
  fix x y
wenzelm@65342
  1316
  assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
eberlm@63099
  1317
  assume eq: "map_of xs x = map_of xs y"
wenzelm@65342
  1318
  from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
wenzelm@65342
  1319
    by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff)
wenzelm@65342
  1320
  moreover from x'y' have *: "(x, x') \<in> set xs" "(y, y') \<in> set xs"
eberlm@63099
  1321
    by (force dest: map_of_SomeD)+
wenzelm@65342
  1322
  moreover from * eq x'y' have "x' = y'"
wenzelm@65342
  1323
    by simp
wenzelm@65342
  1324
  ultimately show "x = y"
wenzelm@65342
  1325
    using assms by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"])
eberlm@63099
  1326
qed
eberlm@63099
  1327
eberlm@63099
  1328
lemma inj_on_the: "None \<notin> A \<Longrightarrow> inj_on the A"
eberlm@63099
  1329
  by (auto simp: inj_on_def option.the_def split: option.splits)
eberlm@63099
  1330
eberlm@63099
  1331
lemma inj_on_map_of':
eberlm@63099
  1332
  assumes "distinct (map snd xs)"
wenzelm@65342
  1333
  shows "inj_on (the \<circ> map_of xs) (set (map fst xs))"
eberlm@63099
  1334
  by (intro comp_inj_on inj_on_map_of assms inj_on_the)
wenzelm@65342
  1335
    (force simp: eq_commute[of None] map_of_eq_None_iff)
eberlm@63099
  1336
eberlm@63099
  1337
lemma image_map_of:
eberlm@63099
  1338
  assumes "distinct (map fst xs)"
wenzelm@65342
  1339
  shows "map_of xs ` set (map fst xs) = Some ` set (map snd xs)"
eberlm@63099
  1340
  using assms by (auto simp: rev_image_eqI)
eberlm@63099
  1341
eberlm@63099
  1342
lemma the_Some_image [simp]: "the ` Some ` A = A"
eberlm@63099
  1343
  by (subst image_image) simp
eberlm@63099
  1344
eberlm@63099
  1345
lemma image_map_of':
eberlm@63099
  1346
  assumes "distinct (map fst xs)"
wenzelm@65342
  1347
  shows "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)"
eberlm@63099
  1348
  by (simp only: image_comp [symmetric] image_map_of assms the_Some_image)
eberlm@63099
  1349
eberlm@63099
  1350
lemma permutation_of_list_permutes [simp]:
eberlm@63099
  1351
  assumes "list_permutes xs A"
wenzelm@65342
  1352
  shows "permutation_of_list xs permutes A"
wenzelm@65342
  1353
    (is "?f permutes _")
eberlm@63099
  1354
proof (rule permutes_subset[OF bij_imp_permutes])
eberlm@63099
  1355
  from assms show "set (map fst xs) \<subseteq> A"
eberlm@63099
  1356
    by (simp add: list_permutes_def)
eberlm@63099
  1357
  from assms have "inj_on (the \<circ> map_of xs) (set (map fst xs))" (is ?P)
eberlm@63099
  1358
    by (intro inj_on_map_of') (simp_all add: list_permutes_def)
eberlm@63099
  1359
  also have "?P \<longleftrightarrow> inj_on ?f (set (map fst xs))"
eberlm@63099
  1360
    by (intro inj_on_cong)
wenzelm@65342
  1361
      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
eberlm@63099
  1362
  finally have "bij_betw ?f (set (map fst xs)) (?f ` set (map fst xs))"
eberlm@63099
  1363
    by (rule inj_on_imp_bij_betw)
eberlm@63099
  1364
  also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)"
eberlm@63099
  1365
    by (intro image_cong refl)
wenzelm@65342
  1366
      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
hoelzl@64284
  1367
  also from assms have "\<dots> = set (map fst xs)"
eberlm@63099
  1368
    by (subst image_map_of') (simp_all add: list_permutes_def)
eberlm@63099
  1369
  finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" .
eberlm@63099
  1370
qed (force simp: permutation_of_list_def dest!: map_of_SomeD split: option.splits)+
eberlm@63099
  1371
eberlm@63099
  1372
lemma eval_permutation_of_list [simp]:
eberlm@63099
  1373
  "permutation_of_list [] x = x"
eberlm@63099
  1374
  "x = x' \<Longrightarrow> permutation_of_list ((x',y)#xs) x = y"
eberlm@63099
  1375
  "x \<noteq> x' \<Longrightarrow> permutation_of_list ((x',y')#xs) x = permutation_of_list xs x"
eberlm@63099
  1376
  by (simp_all add: permutation_of_list_def)
eberlm@63099
  1377
eberlm@63099
  1378
lemma eval_inverse_permutation_of_list [simp]:
eberlm@63099
  1379
  "inverse_permutation_of_list [] x = x"
eberlm@63099
  1380
  "x = x' \<Longrightarrow> inverse_permutation_of_list ((y,x')#xs) x = y"
eberlm@63099
  1381
  "x \<noteq> x' \<Longrightarrow> inverse_permutation_of_list ((y',x')#xs) x = inverse_permutation_of_list xs x"
eberlm@63099
  1382
  by (simp_all add: inverse_permutation_of_list.simps)
eberlm@63099
  1383
wenzelm@65342
  1384
lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x"
wenzelm@65342
  1385
  by (induct xs) (auto simp: permutation_of_list_Cons)
eberlm@63099
  1386
eberlm@63099
  1387
lemma permutation_of_list_unique':
wenzelm@65342
  1388
  "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
wenzelm@65342
  1389
  by (induct xs) (force simp: permutation_of_list_Cons)+
eberlm@63099
  1390
eberlm@63099
  1391
lemma permutation_of_list_unique:
wenzelm@65342
  1392
  "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
wenzelm@65342
  1393
  by (intro permutation_of_list_unique') (simp_all add: list_permutes_def)
eberlm@63099
  1394
eberlm@63099
  1395
lemma inverse_permutation_of_list_id:
wenzelm@65342
  1396
  "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x"
wenzelm@65342
  1397
  by (induct xs) auto
eberlm@63099
  1398
eberlm@63099
  1399
lemma inverse_permutation_of_list_unique':
wenzelm@65342
  1400
  "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
wenzelm@65342
  1401
  by (induct xs) (force simp: inverse_permutation_of_list.simps)+
eberlm@63099
  1402
eberlm@63099
  1403
lemma inverse_permutation_of_list_unique:
wenzelm@65342
  1404
  "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
wenzelm@65342
  1405
  by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def)
eberlm@63099
  1406
eberlm@63099
  1407
lemma inverse_permutation_of_list_correct:
wenzelm@65342
  1408
  fixes A :: "'a set"
wenzelm@65342
  1409
  assumes "list_permutes xs A"
wenzelm@65342
  1410
  shows "inverse_permutation_of_list xs = inv (permutation_of_list xs)"
eberlm@63099
  1411
proof (rule ext, rule sym, subst permutes_inv_eq)
wenzelm@65342
  1412
  from assms show "permutation_of_list xs permutes A"
wenzelm@65342
  1413
    by simp
wenzelm@65342
  1414
  show "permutation_of_list xs (inverse_permutation_of_list xs x) = x" for x
eberlm@63099
  1415
  proof (cases "x \<in> set (map snd xs)")
eberlm@63099
  1416
    case True
wenzelm@65342
  1417
    then obtain y where "(y, x) \<in> set xs" by auto
eberlm@63099
  1418
    with assms show ?thesis
eberlm@63099
  1419
      by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique)
wenzelm@65342
  1420
  next
wenzelm@65342
  1421
    case False
wenzelm@65342
  1422
    with assms show ?thesis
wenzelm@65342
  1423
      by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id)
wenzelm@65342
  1424
  qed
eberlm@63099
  1425
qed
eberlm@63099
  1426
chaieb@29840
  1427
end