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