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