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