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