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