author | haftmann |
Fri, 02 Jul 2010 14:23:17 +0200 | |
changeset 37692 | 7b072f0c8bde |
parent 36903 | 489c1fbbb028 |
child 39075 | a18e5946d63c |
permissions | -rw-r--r-- |
11054 | 1 |
(* Title: HOL/Library/Permutation.thy |
15005 | 2 |
Author: Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker |
11054 | 3 |
*) |
4 |
||
14706 | 5 |
header {* Permutations *} |
11054 | 6 |
|
15131 | 7 |
theory Permutation |
30738 | 8 |
imports Main Multiset |
15131 | 9 |
begin |
11054 | 10 |
|
23755 | 11 |
inductive |
12 |
perm :: "'a list => 'a list => bool" ("_ <~~> _" [50, 50] 50) |
|
13 |
where |
|
11153 | 14 |
Nil [intro!]: "[] <~~> []" |
23755 | 15 |
| swap [intro!]: "y # x # l <~~> x # y # l" |
16 |
| Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys" |
|
17 |
| trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs" |
|
11054 | 18 |
|
19 |
lemma perm_refl [iff]: "l <~~> l" |
|
17200 | 20 |
by (induct l) auto |
11054 | 21 |
|
22 |
||
23 |
subsection {* Some examples of rule induction on permutations *} |
|
24 |
||
25 |
lemma xperm_empty_imp: "[] <~~> ys ==> ys = []" |
|
25379 | 26 |
by (induct xs == "[]::'a list" ys pred: perm) simp_all |
11054 | 27 |
|
28 |
||
29 |
text {* |
|
30 |
\medskip This more general theorem is easier to understand! |
|
31 |
*} |
|
32 |
||
33 |
lemma perm_length: "xs <~~> ys ==> length xs = length ys" |
|
25379 | 34 |
by (induct pred: perm) simp_all |
11054 | 35 |
|
36 |
lemma perm_empty_imp: "[] <~~> xs ==> xs = []" |
|
17200 | 37 |
by (drule perm_length) auto |
11054 | 38 |
|
39 |
lemma perm_sym: "xs <~~> ys ==> ys <~~> xs" |
|
25379 | 40 |
by (induct pred: perm) auto |
11054 | 41 |
|
42 |
||
43 |
subsection {* Ways of making new permutations *} |
|
44 |
||
45 |
text {* |
|
46 |
We can insert the head anywhere in the list. |
|
47 |
*} |
|
48 |
||
49 |
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys" |
|
17200 | 50 |
by (induct xs) auto |
11054 | 51 |
|
52 |
lemma perm_append_swap: "xs @ ys <~~> ys @ xs" |
|
17200 | 53 |
apply (induct xs) |
54 |
apply simp_all |
|
11054 | 55 |
apply (blast intro: perm_append_Cons) |
56 |
done |
|
57 |
||
58 |
lemma perm_append_single: "a # xs <~~> xs @ [a]" |
|
17200 | 59 |
by (rule perm.trans [OF _ perm_append_swap]) simp |
11054 | 60 |
|
61 |
lemma perm_rev: "rev xs <~~> xs" |
|
17200 | 62 |
apply (induct xs) |
63 |
apply simp_all |
|
11153 | 64 |
apply (blast intro!: perm_append_single intro: perm_sym) |
11054 | 65 |
done |
66 |
||
67 |
lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys" |
|
17200 | 68 |
by (induct l) auto |
11054 | 69 |
|
70 |
lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l" |
|
17200 | 71 |
by (blast intro!: perm_append_swap perm_append1) |
11054 | 72 |
|
73 |
||
74 |
subsection {* Further results *} |
|
75 |
||
76 |
lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])" |
|
17200 | 77 |
by (blast intro: perm_empty_imp) |
11054 | 78 |
|
79 |
lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])" |
|
80 |
apply auto |
|
81 |
apply (erule perm_sym [THEN perm_empty_imp]) |
|
82 |
done |
|
83 |
||
25379 | 84 |
lemma perm_sing_imp: "ys <~~> xs ==> xs = [y] ==> ys = [y]" |
85 |
by (induct pred: perm) auto |
|
11054 | 86 |
|
87 |
lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])" |
|
17200 | 88 |
by (blast intro: perm_sing_imp) |
11054 | 89 |
|
90 |
lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])" |
|
17200 | 91 |
by (blast dest: perm_sym) |
11054 | 92 |
|
93 |
||
94 |
subsection {* Removing elements *} |
|
95 |
||
36903 | 96 |
lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove1 x ys" |
17200 | 97 |
by (induct ys) auto |
11054 | 98 |
|
99 |
||
100 |
text {* \medskip Congruence rule *} |
|
101 |
||
36903 | 102 |
lemma perm_remove_perm: "xs <~~> ys ==> remove1 z xs <~~> remove1 z ys" |
25379 | 103 |
by (induct pred: perm) auto |
11054 | 104 |
|
36903 | 105 |
lemma remove_hd [simp]: "remove1 z (z # xs) = xs" |
15072 | 106 |
by auto |
11054 | 107 |
|
108 |
lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys" |
|
17200 | 109 |
by (drule_tac z = z in perm_remove_perm) auto |
11054 | 110 |
|
111 |
lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)" |
|
17200 | 112 |
by (blast intro: cons_perm_imp_perm) |
11054 | 113 |
|
25379 | 114 |
lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys" |
115 |
apply (induct zs arbitrary: xs ys rule: rev_induct) |
|
11054 | 116 |
apply (simp_all (no_asm_use)) |
117 |
apply blast |
|
118 |
done |
|
119 |
||
120 |
lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)" |
|
17200 | 121 |
by (blast intro: append_perm_imp_perm perm_append1) |
11054 | 122 |
|
123 |
lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)" |
|
124 |
apply (safe intro!: perm_append2) |
|
125 |
apply (rule append_perm_imp_perm) |
|
126 |
apply (rule perm_append_swap [THEN perm.trans]) |
|
127 |
-- {* the previous step helps this @{text blast} call succeed quickly *} |
|
128 |
apply (blast intro: perm_append_swap) |
|
129 |
done |
|
130 |
||
15072 | 131 |
lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) " |
17200 | 132 |
apply (rule iffI) |
133 |
apply (erule_tac [2] perm.induct, simp_all add: union_ac) |
|
134 |
apply (erule rev_mp, rule_tac x=ys in spec) |
|
135 |
apply (induct_tac xs, auto) |
|
36903 | 136 |
apply (erule_tac x = "remove1 a x" in allE, drule sym, simp) |
17200 | 137 |
apply (subgoal_tac "a \<in> set x") |
138 |
apply (drule_tac z=a in perm.Cons) |
|
139 |
apply (erule perm.trans, rule perm_sym, erule perm_remove) |
|
15005 | 140 |
apply (drule_tac f=set_of in arg_cong, simp) |
141 |
done |
|
142 |
||
17200 | 143 |
lemma multiset_of_le_perm_append: |
35272
c283ae736bea
switched notations for pointwise and multiset order
haftmann
parents:
33498
diff
changeset
|
144 |
"multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)" |
17200 | 145 |
apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) |
15072 | 146 |
apply (insert surj_multiset_of, drule surjD) |
147 |
apply (blast intro: sym)+ |
|
148 |
done |
|
15005 | 149 |
|
25277 | 150 |
lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys" |
25379 | 151 |
by (metis multiset_of_eq_perm multiset_of_eq_setD) |
25277 | 152 |
|
153 |
lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys" |
|
25379 | 154 |
apply (induct pred: perm) |
155 |
apply simp_all |
|
156 |
apply fastsimp |
|
157 |
apply (metis perm_set_eq) |
|
158 |
done |
|
25277 | 159 |
|
25287 | 160 |
lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys" |
25379 | 161 |
apply (induct xs arbitrary: ys rule: length_induct) |
162 |
apply (case_tac "remdups xs", simp, simp) |
|
163 |
apply (subgoal_tac "a : set (remdups ys)") |
|
164 |
prefer 2 apply (metis set.simps(2) insert_iff set_remdups) |
|
165 |
apply (drule split_list) apply(elim exE conjE) |
|
166 |
apply (drule_tac x=list in spec) apply(erule impE) prefer 2 |
|
167 |
apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2 |
|
168 |
apply simp |
|
169 |
apply (subgoal_tac "a#list <~~> a#ysa@zs") |
|
170 |
apply (metis Cons_eq_appendI perm_append_Cons trans) |
|
171 |
apply (metis Cons Cons_eq_appendI distinct.simps(2) |
|
172 |
distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff) |
|
173 |
apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)") |
|
174 |
apply (fastsimp simp add: insert_ident) |
|
175 |
apply (metis distinct_remdups set_remdups) |
|
30742 | 176 |
apply (subgoal_tac "length (remdups xs) < Suc (length xs)") |
177 |
apply simp |
|
178 |
apply (subgoal_tac "length (remdups xs) \<le> length xs") |
|
179 |
apply simp |
|
180 |
apply (rule length_remdups_leq) |
|
25379 | 181 |
done |
25287 | 182 |
|
183 |
lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)" |
|
25379 | 184 |
by (metis List.set_remdups perm_set_eq eq_set_perm_remdups) |
25287 | 185 |
|
11054 | 186 |
end |