| author | wenzelm |
| Thu, 13 May 2021 15:52:10 +0200 | |
| changeset 73690 | 9267a04aabe6 |
| parent 73477 | 1d8a79aa2a99 |
| child 73706 | 4b1386b2c23e |
| permissions | -rw-r--r-- |
| 73477 | 1 |
(* Author: Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker |
| 11054 | 2 |
*) |
3 |
||
| 73297 | 4 |
section \<open>Permuted Lists\<close> |
| 11054 | 5 |
|
| 73297 | 6 |
theory List_Permutation |
|
73327
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
7 |
imports Permutations |
| 15131 | 8 |
begin |
| 11054 | 9 |
|
| 73329 | 10 |
text \<open> |
11 |
Note that multisets already provide the notion of permutated list and hence |
|
12 |
this theory mostly echoes material already logically present in theory |
|
13 |
\<^text>\<open>Permutations\<close>; it should be seldom needed. |
|
14 |
\<close> |
|
15 |
||
| 73305 | 16 |
subsection \<open>An inductive definition \ldots\<close> |
| 73300 | 17 |
|
| 70680 | 18 |
inductive perm :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixr \<open><~~>\<close> 50) |
| 53238 | 19 |
where |
20 |
Nil [intro!]: "[] <~~> []" |
|
21 |
| swap [intro!]: "y # x # l <~~> x # y # l" |
|
22 |
| Cons [intro!]: "xs <~~> ys \<Longrightarrow> z # xs <~~> z # ys" |
|
23 |
| trans [intro]: "xs <~~> ys \<Longrightarrow> ys <~~> zs \<Longrightarrow> xs <~~> zs" |
|
| 11054 | 24 |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
25 |
proposition perm_refl [iff]: "l <~~> l" |
| 17200 | 26 |
by (induct l) auto |
| 11054 | 27 |
|
| 73305 | 28 |
text \<open>\ldots that is equivalent to an already existing notion:\<close> |
| 11054 | 29 |
|
| 73300 | 30 |
lemma perm_iff_eq_mset: |
31 |
\<open>xs <~~> ys \<longleftrightarrow> mset xs = mset ys\<close> |
|
32 |
proof |
|
33 |
assume \<open>mset xs = mset ys\<close> |
|
34 |
then show \<open>xs <~~> ys\<close> |
|
35 |
proof (induction xs arbitrary: ys) |
|
36 |
case Nil |
|
37 |
then show ?case |
|
38 |
by simp |
|
39 |
next |
|
40 |
case (Cons x xs) |
|
41 |
from Cons.prems [symmetric] have \<open>mset xs = mset (remove1 x ys)\<close> |
|
42 |
by simp |
|
43 |
then have \<open>xs <~~> remove1 x ys\<close> |
|
44 |
by (rule Cons.IH) |
|
45 |
then have \<open>x # xs <~~> x # remove1 x ys\<close> |
|
46 |
by (rule perm.Cons) |
|
47 |
moreover from Cons.prems have \<open>x \<in> set ys\<close> |
|
48 |
by (auto dest: union_single_eq_member) |
|
49 |
then have \<open>x # remove1 x ys <~~> ys\<close> |
|
50 |
by (induction ys) auto |
|
51 |
ultimately show \<open>x # xs <~~> ys\<close> |
|
52 |
by (rule perm.trans) |
|
53 |
qed |
|
54 |
next |
|
55 |
assume \<open>xs <~~> ys\<close> |
|
56 |
then show \<open>mset xs = mset ys\<close> |
|
57 |
by induction simp_all |
|
58 |
qed |
|
| 11054 | 59 |
|
| 73300 | 60 |
theorem mset_eq_perm: \<open>mset xs = mset ys \<longleftrightarrow> xs <~~> ys\<close> |
61 |
by (simp add: perm_iff_eq_mset) |
|
| 11054 | 62 |
|
63 |
||
| 73300 | 64 |
subsection \<open>Nontrivial conclusions\<close> |
| 11054 | 65 |
|
| 73300 | 66 |
proposition perm_swap: |
67 |
\<open>xs[i := xs ! j, j := xs ! i] <~~> xs\<close> |
|
68 |
if \<open>i < length xs\<close> \<open>j < length xs\<close> |
|
| 73301 | 69 |
using that by (simp add: perm_iff_eq_mset mset_swap) |
| 15005 | 70 |
|
| 64587 | 71 |
proposition mset_le_perm_append: "mset xs \<subseteq># mset ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)" |
| 73300 | 72 |
by (auto simp add: perm_iff_eq_mset mset_subset_eq_exists_conv ex_mset dest: sym) |
| 15005 | 73 |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
74 |
proposition perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys" |
| 73300 | 75 |
by (rule mset_eq_setD) (simp add: perm_iff_eq_mset) |
| 25277 | 76 |
|
| 73301 | 77 |
proposition perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs \<longleftrightarrow> distinct ys" |
78 |
by (rule mset_eq_imp_distinct_iff) (simp add: perm_iff_eq_mset) |
|
| 25277 | 79 |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
80 |
theorem eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys" |
| 73300 | 81 |
by (simp add: perm_iff_eq_mset set_eq_iff_mset_remdups_eq) |
| 25287 | 82 |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
83 |
proposition perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y" |
| 73300 | 84 |
by (simp add: perm_iff_eq_mset set_eq_iff_mset_remdups_eq) |
| 25287 | 85 |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
86 |
theorem permutation_Ex_bij: |
| 39075 | 87 |
assumes "xs <~~> ys" |
88 |
shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
|
|
| 73301 | 89 |
proof - |
|
73327
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
90 |
from assms have \<open>mset xs = mset ys\<close> \<open>length xs = length ys\<close> |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
91 |
by (auto simp add: perm_iff_eq_mset dest: mset_eq_length) |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
92 |
from \<open>mset xs = mset ys\<close> obtain p where \<open>p permutes {..<length ys}\<close> \<open>permute_list p ys = xs\<close>
|
| 73301 | 93 |
by (rule mset_eq_permutation) |
|
73327
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
94 |
then have \<open>bij_betw p {..<length xs} {..<length ys}\<close>
|
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
95 |
by (simp add: \<open>length xs = length ys\<close> permutes_imp_bij) |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
96 |
moreover have \<open>\<forall>i<length xs. xs ! i = ys ! (p i)\<close> |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
97 |
using \<open>permute_list p ys = xs\<close> \<open>length xs = length ys\<close> \<open>p permutes {..<length ys}\<close> permute_list_nth
|
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
98 |
by auto |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
99 |
ultimately show ?thesis |
|
fd32f08f4fb5
more connections between mset _ = mset _ and permutations
haftmann
parents:
73305
diff
changeset
|
100 |
by blast |
| 39075 | 101 |
qed |
102 |
||
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
103 |
proposition perm_finite: "finite {B. B <~~> A}"
|
| 73301 | 104 |
using mset_eq_finite by (auto simp add: perm_iff_eq_mset) |
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
105 |
|
| 73300 | 106 |
|
107 |
subsection \<open>Trivial conclusions:\<close> |
|
108 |
||
109 |
proposition perm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []" |
|
110 |
by (simp add: perm_iff_eq_mset) |
|
111 |
||
112 |
||
113 |
text \<open>\medskip This more general theorem is easier to understand!\<close> |
|
114 |
||
115 |
proposition perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys" |
|
116 |
by (rule mset_eq_length) (simp add: perm_iff_eq_mset) |
|
117 |
||
118 |
proposition perm_sym: "xs <~~> ys \<Longrightarrow> ys <~~> xs" |
|
119 |
by (simp add: perm_iff_eq_mset) |
|
120 |
||
121 |
||
122 |
text \<open>We can insert the head anywhere in the list.\<close> |
|
123 |
||
124 |
proposition perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys" |
|
125 |
by (simp add: perm_iff_eq_mset) |
|
126 |
||
127 |
proposition perm_append_swap: "xs @ ys <~~> ys @ xs" |
|
128 |
by (simp add: perm_iff_eq_mset) |
|
129 |
||
130 |
proposition perm_append_single: "a # xs <~~> xs @ [a]" |
|
131 |
by (simp add: perm_iff_eq_mset) |
|
132 |
||
133 |
proposition perm_rev: "rev xs <~~> xs" |
|
134 |
by (simp add: perm_iff_eq_mset) |
|
135 |
||
136 |
proposition perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys" |
|
137 |
by (simp add: perm_iff_eq_mset) |
|
138 |
||
139 |
proposition perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l" |
|
140 |
by (simp add: perm_iff_eq_mset) |
|
141 |
||
142 |
proposition perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []" |
|
143 |
by (simp add: perm_iff_eq_mset) |
|
144 |
||
145 |
proposition perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []" |
|
146 |
by (simp add: perm_iff_eq_mset) |
|
147 |
||
148 |
proposition perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]" |
|
149 |
by (simp add: perm_iff_eq_mset) |
|
150 |
||
151 |
proposition perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]" |
|
152 |
by (simp add: perm_iff_eq_mset) |
|
153 |
||
154 |
proposition perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]" |
|
155 |
by (simp add: perm_iff_eq_mset) |
|
156 |
||
157 |
proposition perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys" |
|
158 |
by (simp add: perm_iff_eq_mset) |
|
159 |
||
160 |
||
161 |
text \<open>\medskip Congruence rule\<close> |
|
162 |
||
163 |
proposition perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys" |
|
164 |
by (simp add: perm_iff_eq_mset) |
|
165 |
||
166 |
proposition remove_hd [simp]: "remove1 z (z # xs) = xs" |
|
167 |
by (simp add: perm_iff_eq_mset) |
|
168 |
||
169 |
proposition cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys" |
|
170 |
by (simp add: perm_iff_eq_mset) |
|
171 |
||
172 |
proposition cons_perm_eq [simp]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys" |
|
173 |
by (simp add: perm_iff_eq_mset) |
|
174 |
||
175 |
proposition append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys" |
|
176 |
by (simp add: perm_iff_eq_mset) |
|
177 |
||
178 |
proposition perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys" |
|
179 |
by (simp add: perm_iff_eq_mset) |
|
180 |
||
181 |
proposition perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys" |
|
182 |
by (simp add: perm_iff_eq_mset) |
|
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61585
diff
changeset
|
183 |
|
| 11054 | 184 |
end |