src/HOL/Library/Multiset_Permutations.thy
author eberlm <eberlm@in.tum.de>
Thu Sep 29 16:49:42 2016 +0200 (2016-09-29)
changeset 63965 d510b816ea41
child 64267 b9a1486e79be
permissions -rw-r--r--
Set_Permutations replaced by more general Multiset_Permutations
eberlm@63965
     1
(*
eberlm@63965
     2
  File:      Multiset_Permutations.thy
eberlm@63965
     3
  Author:    Manuel Eberl (TU M√ľnchen)
eberlm@63965
     4
eberlm@63965
     5
  Defines the set of permutations of a given multiset (or set), i.e. the set of all lists whose 
eberlm@63965
     6
  entries correspond to the multiset (resp. set).
eberlm@63965
     7
*)
eberlm@63965
     8
section \<open>Permutations of a Multiset\<close>
eberlm@63965
     9
theory Multiset_Permutations
eberlm@63965
    10
imports 
eberlm@63965
    11
  Complex_Main 
eberlm@63965
    12
  "~~/src/HOL/Library/Multiset" 
eberlm@63965
    13
  "~~/src/HOL/Library/Permutations"
eberlm@63965
    14
begin
eberlm@63965
    15
eberlm@63965
    16
(* TODO Move *)
eberlm@63965
    17
lemma mset_tl: "xs \<noteq> [] \<Longrightarrow> mset (tl xs) = mset xs - {#hd xs#}"
eberlm@63965
    18
  by (cases xs) simp_all
eberlm@63965
    19
eberlm@63965
    20
lemma mset_set_image_inj:
eberlm@63965
    21
  assumes "inj_on f A"
eberlm@63965
    22
  shows   "mset_set (f ` A) = image_mset f (mset_set A)"
eberlm@63965
    23
proof (cases "finite A")
eberlm@63965
    24
  case True
eberlm@63965
    25
  from this and assms show ?thesis by (induction A) auto
eberlm@63965
    26
qed (insert assms, simp add: finite_image_iff)
eberlm@63965
    27
eberlm@63965
    28
lemma multiset_remove_induct [case_names empty remove]:
eberlm@63965
    29
  assumes "P {#}" "\<And>A. A \<noteq> {#} \<Longrightarrow> (\<And>x. x \<in># A \<Longrightarrow> P (A - {#x#})) \<Longrightarrow> P A"
eberlm@63965
    30
  shows   "P A"
eberlm@63965
    31
proof (induction A rule: full_multiset_induct)
eberlm@63965
    32
  case (less A)
eberlm@63965
    33
  hence IH: "P B" if "B \<subset># A" for B using that by blast
eberlm@63965
    34
  show ?case
eberlm@63965
    35
  proof (cases "A = {#}")
eberlm@63965
    36
    case True
eberlm@63965
    37
    thus ?thesis by (simp add: assms)
eberlm@63965
    38
  next
eberlm@63965
    39
    case False
eberlm@63965
    40
    hence "P (A - {#x#})" if "x \<in># A" for x
eberlm@63965
    41
      using that by (intro IH) (simp add: mset_subset_diff_self)
eberlm@63965
    42
    from False and this show "P A" by (rule assms)
eberlm@63965
    43
  qed
eberlm@63965
    44
qed
eberlm@63965
    45
eberlm@63965
    46
lemma map_list_bind: "map g (List.bind xs f) = List.bind xs (map g \<circ> f)"
eberlm@63965
    47
  by (simp add: List.bind_def map_concat)
eberlm@63965
    48
eberlm@63965
    49
lemma mset_eq_mset_set_imp_distinct:
eberlm@63965
    50
  "finite A \<Longrightarrow> mset_set A = mset xs \<Longrightarrow> distinct xs"
eberlm@63965
    51
proof (induction xs arbitrary: A)
eberlm@63965
    52
  case (Cons x xs A)
eberlm@63965
    53
  from Cons.prems(2) have "x \<in># mset_set A" by simp
eberlm@63965
    54
  with Cons.prems(1) have [simp]: "x \<in> A" by simp
eberlm@63965
    55
  from Cons.prems have "x \<notin># mset_set (A - {x})" by simp
eberlm@63965
    56
  also from Cons.prems have "mset_set (A - {x}) = mset_set A - {#x#}"
eberlm@63965
    57
    by (subst mset_set_Diff) simp_all
eberlm@63965
    58
  also have "mset_set A = mset (x#xs)" by (simp add: Cons.prems)
eberlm@63965
    59
  also have "\<dots> - {#x#} = mset xs" by simp
eberlm@63965
    60
  finally have [simp]: "x \<notin> set xs" by (simp add: in_multiset_in_set)
eberlm@63965
    61
  from Cons.prems show ?case by (auto intro!: Cons.IH[of "A - {x}"] simp: mset_set_Diff)
eberlm@63965
    62
qed simp_all
eberlm@63965
    63
(* END TODO *)
eberlm@63965
    64
eberlm@63965
    65
eberlm@63965
    66
subsection \<open>Permutations of a multiset\<close>
eberlm@63965
    67
eberlm@63965
    68
definition permutations_of_multiset :: "'a multiset \<Rightarrow> 'a list set" where
eberlm@63965
    69
  "permutations_of_multiset A = {xs. mset xs = A}"
eberlm@63965
    70
eberlm@63965
    71
lemma permutations_of_multisetI: "mset xs = A \<Longrightarrow> xs \<in> permutations_of_multiset A"
eberlm@63965
    72
  by (simp add: permutations_of_multiset_def)
eberlm@63965
    73
eberlm@63965
    74
lemma permutations_of_multisetD: "xs \<in> permutations_of_multiset A \<Longrightarrow> mset xs = A"
eberlm@63965
    75
  by (simp add: permutations_of_multiset_def)
eberlm@63965
    76
eberlm@63965
    77
lemma permutations_of_multiset_Cons_iff:
eberlm@63965
    78
  "x # xs \<in> permutations_of_multiset A \<longleftrightarrow> x \<in># A \<and> xs \<in> permutations_of_multiset (A - {#x#})"
eberlm@63965
    79
  by (auto simp: permutations_of_multiset_def)
eberlm@63965
    80
eberlm@63965
    81
lemma permutations_of_multiset_empty [simp]: "permutations_of_multiset {#} = {[]}"
eberlm@63965
    82
  unfolding permutations_of_multiset_def by simp
eberlm@63965
    83
eberlm@63965
    84
lemma permutations_of_multiset_nonempty: 
eberlm@63965
    85
  assumes nonempty: "A \<noteq> {#}"
eberlm@63965
    86
  shows   "permutations_of_multiset A = 
eberlm@63965
    87
             (\<Union>x\<in>set_mset A. (op # x) ` permutations_of_multiset (A - {#x#}))" (is "_ = ?rhs")
eberlm@63965
    88
proof safe
eberlm@63965
    89
  fix xs assume "xs \<in> permutations_of_multiset A"
eberlm@63965
    90
  hence mset_xs: "mset xs = A" by (simp add: permutations_of_multiset_def)
eberlm@63965
    91
  hence "xs \<noteq> []" by (auto simp: nonempty)
eberlm@63965
    92
  then obtain x xs' where xs: "xs = x # xs'" by (cases xs) simp_all
eberlm@63965
    93
  with mset_xs have "x \<in> set_mset A" "xs' \<in> permutations_of_multiset (A - {#x#})"
eberlm@63965
    94
    by (auto simp: permutations_of_multiset_def)
eberlm@63965
    95
  with xs show "xs \<in> ?rhs" by auto
eberlm@63965
    96
qed (auto simp: permutations_of_multiset_def)
eberlm@63965
    97
eberlm@63965
    98
lemma permutations_of_multiset_singleton [simp]: "permutations_of_multiset {#x#} = {[x]}"
eberlm@63965
    99
  by (simp add: permutations_of_multiset_nonempty)
eberlm@63965
   100
eberlm@63965
   101
lemma permutations_of_multiset_doubleton: 
eberlm@63965
   102
  "permutations_of_multiset {#x,y#} = {[x,y], [y,x]}"
eberlm@63965
   103
  by (simp add: permutations_of_multiset_nonempty insert_commute)
eberlm@63965
   104
eberlm@63965
   105
lemma rev_permutations_of_multiset [simp]:
eberlm@63965
   106
  "rev ` permutations_of_multiset A = permutations_of_multiset A"
eberlm@63965
   107
proof
eberlm@63965
   108
  have "rev ` rev ` permutations_of_multiset A \<subseteq> rev ` permutations_of_multiset A"
eberlm@63965
   109
    unfolding permutations_of_multiset_def by auto
eberlm@63965
   110
  also have "rev ` rev ` permutations_of_multiset A = permutations_of_multiset A"
eberlm@63965
   111
    by (simp add: image_image)
eberlm@63965
   112
  finally show "permutations_of_multiset A \<subseteq> rev ` permutations_of_multiset A" .
eberlm@63965
   113
next
eberlm@63965
   114
  show "rev ` permutations_of_multiset A \<subseteq> permutations_of_multiset A"
eberlm@63965
   115
    unfolding permutations_of_multiset_def by auto
eberlm@63965
   116
qed
eberlm@63965
   117
eberlm@63965
   118
lemma length_finite_permutations_of_multiset:
eberlm@63965
   119
  "xs \<in> permutations_of_multiset A \<Longrightarrow> length xs = size A"
eberlm@63965
   120
  by (auto simp: permutations_of_multiset_def)
eberlm@63965
   121
eberlm@63965
   122
lemma permutations_of_multiset_lists: "permutations_of_multiset A \<subseteq> lists (set_mset A)"
eberlm@63965
   123
  by (auto simp: permutations_of_multiset_def)
eberlm@63965
   124
eberlm@63965
   125
lemma finite_permutations_of_multiset [simp]: "finite (permutations_of_multiset A)"
eberlm@63965
   126
proof (rule finite_subset)
eberlm@63965
   127
  show "permutations_of_multiset A \<subseteq> {xs. set xs \<subseteq> set_mset A \<and> length xs = size A}" 
eberlm@63965
   128
    by (auto simp: permutations_of_multiset_def)
eberlm@63965
   129
  show "finite {xs. set xs \<subseteq> set_mset A \<and> length xs = size A}" 
eberlm@63965
   130
    by (rule finite_lists_length_eq) simp_all
eberlm@63965
   131
qed
eberlm@63965
   132
eberlm@63965
   133
lemma permutations_of_multiset_not_empty [simp]: "permutations_of_multiset A \<noteq> {}"
eberlm@63965
   134
proof -
eberlm@63965
   135
  from ex_mset[of A] guess xs ..
eberlm@63965
   136
  thus ?thesis by (auto simp: permutations_of_multiset_def)
eberlm@63965
   137
qed
eberlm@63965
   138
eberlm@63965
   139
lemma permutations_of_multiset_image:
eberlm@63965
   140
  "permutations_of_multiset (image_mset f A) = map f ` permutations_of_multiset A"
eberlm@63965
   141
proof safe
eberlm@63965
   142
  fix xs assume A: "xs \<in> permutations_of_multiset (image_mset f A)"
eberlm@63965
   143
  from ex_mset[of A] obtain ys where ys: "mset ys = A" ..
eberlm@63965
   144
  with A have "mset xs = mset (map f ys)" 
eberlm@63965
   145
    by (simp add: permutations_of_multiset_def)
eberlm@63965
   146
  from mset_eq_permutation[OF this] guess \<sigma> . note \<sigma> = this
eberlm@63965
   147
  with ys have "xs = map f (permute_list \<sigma> ys)"
eberlm@63965
   148
    by (simp add: permute_list_map)
eberlm@63965
   149
  moreover from \<sigma> ys have "permute_list \<sigma> ys \<in> permutations_of_multiset A"
eberlm@63965
   150
    by (simp add: permutations_of_multiset_def)
eberlm@63965
   151
  ultimately show "xs \<in> map f ` permutations_of_multiset A" by blast
eberlm@63965
   152
qed (auto simp: permutations_of_multiset_def)
eberlm@63965
   153
eberlm@63965
   154
eberlm@63965
   155
subsection \<open>Cardinality of permutations\<close>
eberlm@63965
   156
eberlm@63965
   157
text \<open>
eberlm@63965
   158
  In this section, we prove some basic facts about the number of permutations of a multiset.
eberlm@63965
   159
\<close>
eberlm@63965
   160
eberlm@63965
   161
context
eberlm@63965
   162
begin
eberlm@63965
   163
eberlm@63965
   164
private lemma multiset_setprod_fact_insert:
eberlm@63965
   165
  "(\<Prod>y\<in>set_mset (A+{#x#}). fact (count (A+{#x#}) y)) =
eberlm@63965
   166
     (count A x + 1) * (\<Prod>y\<in>set_mset A. fact (count A y))"
eberlm@63965
   167
proof -
eberlm@63965
   168
  have "(\<Prod>y\<in>set_mset (A+{#x#}). fact (count (A+{#x#}) y)) =
eberlm@63965
   169
          (\<Prod>y\<in>set_mset (A+{#x#}). (if y = x then count A x + 1 else 1) * fact (count A y))"
eberlm@63965
   170
    by (intro setprod.cong) simp_all
eberlm@63965
   171
  also have "\<dots> = (count A x + 1) * (\<Prod>y\<in>set_mset (A+{#x#}). fact (count A y))"
eberlm@63965
   172
    by (simp add: setprod.distrib setprod.delta)
eberlm@63965
   173
  also have "(\<Prod>y\<in>set_mset (A+{#x#}). fact (count A y)) = (\<Prod>y\<in>set_mset A. fact (count A y))"
eberlm@63965
   174
    by (intro setprod.mono_neutral_right) (auto simp: not_in_iff)
eberlm@63965
   175
  finally show ?thesis .
eberlm@63965
   176
qed
eberlm@63965
   177
eberlm@63965
   178
private lemma multiset_setprod_fact_remove:
eberlm@63965
   179
  "x \<in># A \<Longrightarrow> (\<Prod>y\<in>set_mset A. fact (count A y)) =
eberlm@63965
   180
                   count A x * (\<Prod>y\<in>set_mset (A-{#x#}). fact (count (A-{#x#}) y))"
eberlm@63965
   181
  using multiset_setprod_fact_insert[of "A - {#x#}" x] by simp
eberlm@63965
   182
eberlm@63965
   183
lemma card_permutations_of_multiset_aux:
eberlm@63965
   184
  "card (permutations_of_multiset A) * (\<Prod>x\<in>set_mset A. fact (count A x)) = fact (size A)"
eberlm@63965
   185
proof (induction A rule: multiset_remove_induct)
eberlm@63965
   186
  case (remove A)
eberlm@63965
   187
  have "card (permutations_of_multiset A) = 
eberlm@63965
   188
          card (\<Union>x\<in>set_mset A. op # x ` permutations_of_multiset (A - {#x#}))"
eberlm@63965
   189
    by (simp add: permutations_of_multiset_nonempty remove.hyps)
eberlm@63965
   190
  also have "\<dots> = (\<Sum>x\<in>set_mset A. card (permutations_of_multiset (A - {#x#})))"
eberlm@63965
   191
    by (subst card_UN_disjoint) (auto simp: card_image)
eberlm@63965
   192
  also have "\<dots> * (\<Prod>x\<in>set_mset A. fact (count A x)) = 
eberlm@63965
   193
               (\<Sum>x\<in>set_mset A. card (permutations_of_multiset (A - {#x#})) * 
eberlm@63965
   194
                 (\<Prod>y\<in>set_mset A. fact (count A y)))"
eberlm@63965
   195
    by (subst setsum_distrib_right) simp_all
eberlm@63965
   196
  also have "\<dots> = (\<Sum>x\<in>set_mset A. count A x * fact (size A - 1))"
eberlm@63965
   197
  proof (intro setsum.cong refl)
eberlm@63965
   198
    fix x assume x: "x \<in># A"
eberlm@63965
   199
    have "card (permutations_of_multiset (A - {#x#})) * (\<Prod>y\<in>set_mset A. fact (count A y)) = 
eberlm@63965
   200
            count A x * (card (permutations_of_multiset (A - {#x#})) * 
eberlm@63965
   201
              (\<Prod>y\<in>set_mset (A - {#x#}). fact (count (A - {#x#}) y)))" (is "?lhs = _")
eberlm@63965
   202
      by (subst multiset_setprod_fact_remove[OF x]) simp_all
eberlm@63965
   203
    also note remove.IH[OF x]
eberlm@63965
   204
    also from x have "size (A - {#x#}) = size A - 1" by (simp add: size_Diff_submset)
eberlm@63965
   205
    finally show "?lhs = count A x * fact (size A - 1)" .
eberlm@63965
   206
  qed
eberlm@63965
   207
  also have "(\<Sum>x\<in>set_mset A. count A x * fact (size A - 1)) =
eberlm@63965
   208
                size A * fact (size A - 1)"
eberlm@63965
   209
    by (simp add: setsum_distrib_right size_multiset_overloaded_eq)
eberlm@63965
   210
  also from remove.hyps have "\<dots> = fact (size A)"
eberlm@63965
   211
    by (cases "size A") auto
eberlm@63965
   212
  finally show ?case .
eberlm@63965
   213
qed simp_all
eberlm@63965
   214
eberlm@63965
   215
theorem card_permutations_of_multiset:
eberlm@63965
   216
  "card (permutations_of_multiset A) = fact (size A) div (\<Prod>x\<in>set_mset A. fact (count A x))"
eberlm@63965
   217
  "(\<Prod>x\<in>set_mset A. fact (count A x) :: nat) dvd fact (size A)"
eberlm@63965
   218
  by (simp_all add: card_permutations_of_multiset_aux[of A, symmetric])
eberlm@63965
   219
eberlm@63965
   220
lemma card_permutations_of_multiset_insert_aux:
eberlm@63965
   221
  "card (permutations_of_multiset (A + {#x#})) * (count A x + 1) = 
eberlm@63965
   222
      (size A + 1) * card (permutations_of_multiset A)"
eberlm@63965
   223
proof -
eberlm@63965
   224
  note card_permutations_of_multiset_aux[of "A + {#x#}"]
eberlm@63965
   225
  also have "fact (size (A + {#x#})) = (size A + 1) * fact (size A)" by simp
eberlm@63965
   226
  also note multiset_setprod_fact_insert[of A x]
eberlm@63965
   227
  also note card_permutations_of_multiset_aux[of A, symmetric]
eberlm@63965
   228
  finally have "card (permutations_of_multiset (A + {#x#})) * (count A x + 1) *
eberlm@63965
   229
                    (\<Prod>y\<in>set_mset A. fact (count A y)) =
eberlm@63965
   230
                (size A + 1) * card (permutations_of_multiset A) *
eberlm@63965
   231
                    (\<Prod>x\<in>set_mset A. fact (count A x))" by (simp only: mult_ac)
eberlm@63965
   232
  thus ?thesis by (subst (asm) mult_right_cancel) simp_all
eberlm@63965
   233
qed
eberlm@63965
   234
eberlm@63965
   235
lemma card_permutations_of_multiset_remove_aux:
eberlm@63965
   236
  assumes "x \<in># A"
eberlm@63965
   237
  shows   "card (permutations_of_multiset A) * count A x = 
eberlm@63965
   238
             size A * card (permutations_of_multiset (A - {#x#}))"
eberlm@63965
   239
proof -
eberlm@63965
   240
  from assms have A: "A - {#x#} + {#x#} = A" by simp
eberlm@63965
   241
  from assms have B: "size A = size (A - {#x#}) + 1" 
eberlm@63965
   242
    by (subst A [symmetric], subst size_union) simp
eberlm@63965
   243
  show ?thesis
eberlm@63965
   244
    using card_permutations_of_multiset_insert_aux[of "A - {#x#}" x, unfolded A] assms
eberlm@63965
   245
    by (simp add: B)
eberlm@63965
   246
qed
eberlm@63965
   247
eberlm@63965
   248
lemma real_card_permutations_of_multiset_remove:
eberlm@63965
   249
  assumes "x \<in># A"
eberlm@63965
   250
  shows   "real (card (permutations_of_multiset (A - {#x#}))) = 
eberlm@63965
   251
             real (card (permutations_of_multiset A) * count A x) / real (size A)"
eberlm@63965
   252
  using assms by (subst card_permutations_of_multiset_remove_aux[OF assms]) auto
eberlm@63965
   253
eberlm@63965
   254
lemma real_card_permutations_of_multiset_remove':
eberlm@63965
   255
  assumes "x \<in># A"
eberlm@63965
   256
  shows   "real (card (permutations_of_multiset A)) = 
eberlm@63965
   257
             real (size A * card (permutations_of_multiset (A - {#x#}))) / real (count A x)"
eberlm@63965
   258
  using assms by (subst card_permutations_of_multiset_remove_aux[OF assms, symmetric]) simp
eberlm@63965
   259
eberlm@63965
   260
end
eberlm@63965
   261
eberlm@63965
   262
eberlm@63965
   263
eberlm@63965
   264
subsection \<open>Permutations of a set\<close>
eberlm@63965
   265
eberlm@63965
   266
definition permutations_of_set :: "'a set \<Rightarrow> 'a list set" where
eberlm@63965
   267
  "permutations_of_set A = {xs. set xs = A \<and> distinct xs}"
eberlm@63965
   268
eberlm@63965
   269
lemma permutations_of_set_altdef:
eberlm@63965
   270
  "finite A \<Longrightarrow> permutations_of_set A = permutations_of_multiset (mset_set A)"
eberlm@63965
   271
  by (auto simp add: permutations_of_set_def permutations_of_multiset_def mset_set_set 
eberlm@63965
   272
        in_multiset_in_set [symmetric] mset_eq_mset_set_imp_distinct)
eberlm@63965
   273
eberlm@63965
   274
lemma permutations_of_setI [intro]:
eberlm@63965
   275
  assumes "set xs = A" "distinct xs"
eberlm@63965
   276
  shows   "xs \<in> permutations_of_set A"
eberlm@63965
   277
  using assms unfolding permutations_of_set_def by simp
eberlm@63965
   278
  
eberlm@63965
   279
lemma permutations_of_setD:
eberlm@63965
   280
  assumes "xs \<in> permutations_of_set A"
eberlm@63965
   281
  shows   "set xs = A" "distinct xs"
eberlm@63965
   282
  using assms unfolding permutations_of_set_def by simp_all
eberlm@63965
   283
  
eberlm@63965
   284
lemma permutations_of_set_lists: "permutations_of_set A \<subseteq> lists A"
eberlm@63965
   285
  unfolding permutations_of_set_def by auto
eberlm@63965
   286
eberlm@63965
   287
lemma permutations_of_set_empty [simp]: "permutations_of_set {} = {[]}"
eberlm@63965
   288
  by (auto simp: permutations_of_set_def)
eberlm@63965
   289
  
eberlm@63965
   290
lemma UN_set_permutations_of_set [simp]:
eberlm@63965
   291
  "finite A \<Longrightarrow> (\<Union>xs\<in>permutations_of_set A. set xs) = A"
eberlm@63965
   292
  using finite_distinct_list by (auto simp: permutations_of_set_def)
eberlm@63965
   293
eberlm@63965
   294
lemma permutations_of_set_infinite:
eberlm@63965
   295
  "\<not>finite A \<Longrightarrow> permutations_of_set A = {}"
eberlm@63965
   296
  by (auto simp: permutations_of_set_def)
eberlm@63965
   297
eberlm@63965
   298
lemma permutations_of_set_nonempty:
eberlm@63965
   299
  "A \<noteq> {} \<Longrightarrow> permutations_of_set A = 
eberlm@63965
   300
                  (\<Union>x\<in>A. (\<lambda>xs. x # xs) ` permutations_of_set (A - {x}))"
eberlm@63965
   301
  by (cases "finite A")
eberlm@63965
   302
     (simp_all add: permutations_of_multiset_nonempty mset_set_empty_iff mset_set_Diff 
eberlm@63965
   303
                    permutations_of_set_altdef permutations_of_set_infinite)
eberlm@63965
   304
    
eberlm@63965
   305
lemma permutations_of_set_singleton [simp]: "permutations_of_set {x} = {[x]}"
eberlm@63965
   306
  by (subst permutations_of_set_nonempty) auto
eberlm@63965
   307
eberlm@63965
   308
lemma permutations_of_set_doubleton: 
eberlm@63965
   309
  "x \<noteq> y \<Longrightarrow> permutations_of_set {x,y} = {[x,y], [y,x]}"
eberlm@63965
   310
  by (subst permutations_of_set_nonempty) 
eberlm@63965
   311
     (simp_all add: insert_Diff_if insert_commute)
eberlm@63965
   312
eberlm@63965
   313
lemma rev_permutations_of_set [simp]:
eberlm@63965
   314
  "rev ` permutations_of_set A = permutations_of_set A"
eberlm@63965
   315
  by (cases "finite A") (simp_all add: permutations_of_set_altdef permutations_of_set_infinite)
eberlm@63965
   316
eberlm@63965
   317
lemma length_finite_permutations_of_set:
eberlm@63965
   318
  "xs \<in> permutations_of_set A \<Longrightarrow> length xs = card A"
eberlm@63965
   319
  by (auto simp: permutations_of_set_def distinct_card)
eberlm@63965
   320
eberlm@63965
   321
lemma finite_permutations_of_set [simp]: "finite (permutations_of_set A)"
eberlm@63965
   322
  by (cases "finite A") (simp_all add: permutations_of_set_infinite permutations_of_set_altdef)
eberlm@63965
   323
eberlm@63965
   324
lemma permutations_of_set_empty_iff [simp]:
eberlm@63965
   325
  "permutations_of_set A = {} \<longleftrightarrow> \<not>finite A"
eberlm@63965
   326
  unfolding permutations_of_set_def using finite_distinct_list[of A] by auto
eberlm@63965
   327
eberlm@63965
   328
lemma card_permutations_of_set [simp]:
eberlm@63965
   329
  "finite A \<Longrightarrow> card (permutations_of_set A) = fact (card A)"
eberlm@63965
   330
  by (simp add: permutations_of_set_altdef card_permutations_of_multiset del: One_nat_def)
eberlm@63965
   331
eberlm@63965
   332
lemma permutations_of_set_image_inj:
eberlm@63965
   333
  assumes inj: "inj_on f A"
eberlm@63965
   334
  shows   "permutations_of_set (f ` A) = map f ` permutations_of_set A"
eberlm@63965
   335
  by (cases "finite A")
eberlm@63965
   336
     (simp_all add: permutations_of_set_infinite permutations_of_set_altdef
eberlm@63965
   337
                    permutations_of_multiset_image mset_set_image_inj inj finite_image_iff)
eberlm@63965
   338
eberlm@63965
   339
lemma permutations_of_set_image_permutes:
eberlm@63965
   340
  "\<sigma> permutes A \<Longrightarrow> map \<sigma> ` permutations_of_set A = permutations_of_set A"
eberlm@63965
   341
  by (subst permutations_of_set_image_inj [symmetric])
eberlm@63965
   342
     (simp_all add: permutes_inj_on permutes_image)
eberlm@63965
   343
eberlm@63965
   344
eberlm@63965
   345
subsection \<open>Code generation\<close>
eberlm@63965
   346
eberlm@63965
   347
text \<open>
eberlm@63965
   348
  First, we give code an implementation for permutations of lists.
eberlm@63965
   349
\<close>
eberlm@63965
   350
eberlm@63965
   351
declare length_remove1 [termination_simp] 
eberlm@63965
   352
eberlm@63965
   353
fun permutations_of_list_impl where
eberlm@63965
   354
  "permutations_of_list_impl xs = (if xs = [] then [[]] else
eberlm@63965
   355
     List.bind (remdups xs) (\<lambda>x. map (op # x) (permutations_of_list_impl (remove1 x xs))))"
eberlm@63965
   356
eberlm@63965
   357
fun permutations_of_list_impl_aux where
eberlm@63965
   358
  "permutations_of_list_impl_aux acc xs = (if xs = [] then [acc] else
eberlm@63965
   359
     List.bind (remdups xs) (\<lambda>x. permutations_of_list_impl_aux (x#acc) (remove1 x xs)))"
eberlm@63965
   360
eberlm@63965
   361
declare permutations_of_list_impl_aux.simps [simp del]    
eberlm@63965
   362
declare permutations_of_list_impl.simps [simp del]
eberlm@63965
   363
    
eberlm@63965
   364
lemma permutations_of_list_impl_Nil [simp]:
eberlm@63965
   365
  "permutations_of_list_impl [] = [[]]"
eberlm@63965
   366
  by (simp add: permutations_of_list_impl.simps)
eberlm@63965
   367
eberlm@63965
   368
lemma permutations_of_list_impl_nonempty:
eberlm@63965
   369
  "xs \<noteq> [] \<Longrightarrow> permutations_of_list_impl xs = 
eberlm@63965
   370
     List.bind (remdups xs) (\<lambda>x. map (op # x) (permutations_of_list_impl (remove1 x xs)))"
eberlm@63965
   371
  by (subst permutations_of_list_impl.simps) simp_all
eberlm@63965
   372
eberlm@63965
   373
lemma set_permutations_of_list_impl:
eberlm@63965
   374
  "set (permutations_of_list_impl xs) = permutations_of_multiset (mset xs)"
eberlm@63965
   375
  by (induction xs rule: permutations_of_list_impl.induct)
eberlm@63965
   376
     (subst permutations_of_list_impl.simps, 
eberlm@63965
   377
      simp_all add: permutations_of_multiset_nonempty set_list_bind)
eberlm@63965
   378
eberlm@63965
   379
lemma distinct_permutations_of_list_impl:
eberlm@63965
   380
  "distinct (permutations_of_list_impl xs)"
eberlm@63965
   381
  by (induction xs rule: permutations_of_list_impl.induct, 
eberlm@63965
   382
      subst permutations_of_list_impl.simps)
eberlm@63965
   383
     (auto intro!: distinct_list_bind simp: distinct_map o_def disjoint_family_on_def)
eberlm@63965
   384
eberlm@63965
   385
lemma permutations_of_list_impl_aux_correct':
eberlm@63965
   386
  "permutations_of_list_impl_aux acc xs = 
eberlm@63965
   387
     map (\<lambda>xs. rev xs @ acc) (permutations_of_list_impl xs)"
eberlm@63965
   388
  by (induction acc xs rule: permutations_of_list_impl_aux.induct,
eberlm@63965
   389
      subst permutations_of_list_impl_aux.simps, subst permutations_of_list_impl.simps)
eberlm@63965
   390
     (auto simp: map_list_bind intro!: list_bind_cong)
eberlm@63965
   391
    
eberlm@63965
   392
lemma permutations_of_list_impl_aux_correct:
eberlm@63965
   393
  "permutations_of_list_impl_aux [] xs = map rev (permutations_of_list_impl xs)"
eberlm@63965
   394
  by (simp add: permutations_of_list_impl_aux_correct')
eberlm@63965
   395
eberlm@63965
   396
lemma distinct_permutations_of_list_impl_aux:
eberlm@63965
   397
  "distinct (permutations_of_list_impl_aux acc xs)"
eberlm@63965
   398
  by (simp add: permutations_of_list_impl_aux_correct' distinct_map 
eberlm@63965
   399
        distinct_permutations_of_list_impl inj_on_def)
eberlm@63965
   400
eberlm@63965
   401
lemma set_permutations_of_list_impl_aux:
eberlm@63965
   402
  "set (permutations_of_list_impl_aux [] xs) = permutations_of_multiset (mset xs)"
eberlm@63965
   403
  by (simp add: permutations_of_list_impl_aux_correct set_permutations_of_list_impl)
eberlm@63965
   404
  
eberlm@63965
   405
declare set_permutations_of_list_impl_aux [symmetric, code]
eberlm@63965
   406
eberlm@63965
   407
value [code] "permutations_of_multiset {#1,2,3,4::int#}"
eberlm@63965
   408
eberlm@63965
   409
eberlm@63965
   410
eberlm@63965
   411
text \<open>
eberlm@63965
   412
  Now we turn to permutations of sets. We define an auxiliary version with an 
eberlm@63965
   413
  accumulator to avoid having to map over the results.
eberlm@63965
   414
\<close>
eberlm@63965
   415
function permutations_of_set_aux where
eberlm@63965
   416
  "permutations_of_set_aux acc A = 
eberlm@63965
   417
     (if \<not>finite A then {} else if A = {} then {acc} else 
eberlm@63965
   418
        (\<Union>x\<in>A. permutations_of_set_aux (x#acc) (A - {x})))"
eberlm@63965
   419
by auto
eberlm@63965
   420
termination by (relation "Wellfounded.measure (card \<circ> snd)") (simp_all add: card_gt_0_iff)
eberlm@63965
   421
eberlm@63965
   422
lemma permutations_of_set_aux_altdef:
eberlm@63965
   423
  "permutations_of_set_aux acc A = (\<lambda>xs. rev xs @ acc) ` permutations_of_set A"
eberlm@63965
   424
proof (cases "finite A")
eberlm@63965
   425
  assume "finite A"
eberlm@63965
   426
  thus ?thesis
eberlm@63965
   427
  proof (induction A arbitrary: acc rule: finite_psubset_induct)
eberlm@63965
   428
    case (psubset A acc)
eberlm@63965
   429
    show ?case
eberlm@63965
   430
    proof (cases "A = {}")
eberlm@63965
   431
      case False
eberlm@63965
   432
      note [simp del] = permutations_of_set_aux.simps
eberlm@63965
   433
      from psubset.hyps False 
eberlm@63965
   434
        have "permutations_of_set_aux acc A = 
eberlm@63965
   435
                (\<Union>y\<in>A. permutations_of_set_aux (y#acc) (A - {y}))"
eberlm@63965
   436
        by (subst permutations_of_set_aux.simps) simp_all
eberlm@63965
   437
      also have "\<dots> = (\<Union>y\<in>A. (\<lambda>xs. rev xs @ acc) ` (\<lambda>xs. y # xs) ` permutations_of_set (A - {y}))"
eberlm@63965
   438
        by (intro SUP_cong refl, subst psubset) (auto simp: image_image)
eberlm@63965
   439
      also from False have "\<dots> = (\<lambda>xs. rev xs @ acc) ` permutations_of_set A"
eberlm@63965
   440
        by (subst (2) permutations_of_set_nonempty) (simp_all add: image_UN)
eberlm@63965
   441
      finally show ?thesis .
eberlm@63965
   442
    qed simp_all
eberlm@63965
   443
  qed
eberlm@63965
   444
qed (simp_all add: permutations_of_set_infinite)
eberlm@63965
   445
eberlm@63965
   446
declare permutations_of_set_aux.simps [simp del]
eberlm@63965
   447
eberlm@63965
   448
lemma permutations_of_set_aux_correct:
eberlm@63965
   449
  "permutations_of_set_aux [] A = permutations_of_set A"
eberlm@63965
   450
  by (simp add: permutations_of_set_aux_altdef)
eberlm@63965
   451
eberlm@63965
   452
eberlm@63965
   453
text \<open>
eberlm@63965
   454
  In another refinement step, we define a version on lists.
eberlm@63965
   455
\<close>
eberlm@63965
   456
declare length_remove1 [termination_simp]
eberlm@63965
   457
eberlm@63965
   458
fun permutations_of_set_aux_list where
eberlm@63965
   459
  "permutations_of_set_aux_list acc xs = 
eberlm@63965
   460
     (if xs = [] then [acc] else 
eberlm@63965
   461
        List.bind xs (\<lambda>x. permutations_of_set_aux_list (x#acc) (List.remove1 x xs)))"
eberlm@63965
   462
eberlm@63965
   463
definition permutations_of_set_list where
eberlm@63965
   464
  "permutations_of_set_list xs = permutations_of_set_aux_list [] xs"
eberlm@63965
   465
eberlm@63965
   466
declare permutations_of_set_aux_list.simps [simp del]
eberlm@63965
   467
eberlm@63965
   468
lemma permutations_of_set_aux_list_refine:
eberlm@63965
   469
  assumes "distinct xs"
eberlm@63965
   470
  shows   "set (permutations_of_set_aux_list acc xs) = permutations_of_set_aux acc (set xs)"
eberlm@63965
   471
  using assms
eberlm@63965
   472
  by (induction acc xs rule: permutations_of_set_aux_list.induct)
eberlm@63965
   473
     (subst permutations_of_set_aux_list.simps,
eberlm@63965
   474
      subst permutations_of_set_aux.simps,
eberlm@63965
   475
      simp_all add: set_list_bind cong: SUP_cong)
eberlm@63965
   476
eberlm@63965
   477
eberlm@63965
   478
text \<open>
eberlm@63965
   479
  The permutation lists contain no duplicates if the inputs contain no duplicates.
eberlm@63965
   480
  Therefore, these functions can easily be used when working with a representation of
eberlm@63965
   481
  sets by distinct lists.
eberlm@63965
   482
  The same approach should generalise to any kind of set implementation that supports
eberlm@63965
   483
  a monadic bind operation, and since the results are disjoint, merging should be cheap.
eberlm@63965
   484
\<close>
eberlm@63965
   485
lemma distinct_permutations_of_set_aux_list:
eberlm@63965
   486
  "distinct xs \<Longrightarrow> distinct (permutations_of_set_aux_list acc xs)"
eberlm@63965
   487
  by (induction acc xs rule: permutations_of_set_aux_list.induct)
eberlm@63965
   488
     (subst permutations_of_set_aux_list.simps,
eberlm@63965
   489
      auto intro!: distinct_list_bind simp: disjoint_family_on_def 
eberlm@63965
   490
         permutations_of_set_aux_list_refine permutations_of_set_aux_altdef)
eberlm@63965
   491
eberlm@63965
   492
lemma distinct_permutations_of_set_list:
eberlm@63965
   493
    "distinct xs \<Longrightarrow> distinct (permutations_of_set_list xs)"
eberlm@63965
   494
  by (simp add: permutations_of_set_list_def distinct_permutations_of_set_aux_list)
eberlm@63965
   495
eberlm@63965
   496
lemma permutations_of_list:
eberlm@63965
   497
    "permutations_of_set (set xs) = set (permutations_of_set_list (remdups xs))"
eberlm@63965
   498
  by (simp add: permutations_of_set_aux_correct [symmetric] 
eberlm@63965
   499
        permutations_of_set_aux_list_refine permutations_of_set_list_def)
eberlm@63965
   500
eberlm@63965
   501
lemma permutations_of_list_code [code]:
eberlm@63965
   502
  "permutations_of_set (set xs) = set (permutations_of_set_list (remdups xs))"
eberlm@63965
   503
  "permutations_of_set (List.coset xs) = 
eberlm@63965
   504
     Code.abort (STR ''Permutation of set complement not supported'') 
eberlm@63965
   505
       (\<lambda>_. permutations_of_set (List.coset xs))"
eberlm@63965
   506
  by (simp_all add: permutations_of_list)
eberlm@63965
   507
eberlm@63965
   508
value [code] "permutations_of_set (set ''abcd'')"
eberlm@63965
   509
eberlm@63965
   510
end