22 swap [intro!]: "y # x # l <~~> x # y # l" |
22 swap [intro!]: "y # x # l <~~> x # y # l" |
23 Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys" |
23 Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys" |
24 trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs" |
24 trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs" |
25 |
25 |
26 lemma perm_refl [iff]: "l <~~> l" |
26 lemma perm_refl [iff]: "l <~~> l" |
27 by (induct l, auto) |
27 by (induct l) auto |
28 |
28 |
29 |
29 |
30 subsection {* Some examples of rule induction on permutations *} |
30 subsection {* Some examples of rule induction on permutations *} |
31 |
31 |
32 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []" |
32 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []" |
33 -- {*the form of the premise lets the induction bind @{term xs} |
33 -- {*the form of the premise lets the induction bind @{term xs} |
34 and @{term ys} *} |
34 and @{term ys} *} |
35 apply (erule perm.induct) |
35 apply (erule perm.induct) |
36 apply (simp_all (no_asm_simp)) |
36 apply (simp_all (no_asm_simp)) |
37 done |
37 done |
38 |
38 |
39 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []" |
39 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []" |
40 by (insert xperm_empty_imp_aux, blast) |
40 using xperm_empty_imp_aux by blast |
41 |
41 |
42 |
42 |
43 text {* |
43 text {* |
44 \medskip This more general theorem is easier to understand! |
44 \medskip This more general theorem is easier to understand! |
45 *} |
45 *} |
46 |
46 |
47 lemma perm_length: "xs <~~> ys ==> length xs = length ys" |
47 lemma perm_length: "xs <~~> ys ==> length xs = length ys" |
48 by (erule perm.induct, simp_all) |
48 by (erule perm.induct) simp_all |
49 |
49 |
50 lemma perm_empty_imp: "[] <~~> xs ==> xs = []" |
50 lemma perm_empty_imp: "[] <~~> xs ==> xs = []" |
51 by (drule perm_length, auto) |
51 by (drule perm_length) auto |
52 |
52 |
53 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs" |
53 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs" |
54 by (erule perm.induct, auto) |
54 by (erule perm.induct) auto |
55 |
55 |
56 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys" |
56 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys" |
57 by (erule perm.induct, auto) |
57 by (erule perm.induct) auto |
58 |
58 |
59 |
59 |
60 subsection {* Ways of making new permutations *} |
60 subsection {* Ways of making new permutations *} |
61 |
61 |
62 text {* |
62 text {* |
63 We can insert the head anywhere in the list. |
63 We can insert the head anywhere in the list. |
64 *} |
64 *} |
65 |
65 |
66 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys" |
66 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys" |
67 by (induct xs, auto) |
67 by (induct xs) auto |
68 |
68 |
69 lemma perm_append_swap: "xs @ ys <~~> ys @ xs" |
69 lemma perm_append_swap: "xs @ ys <~~> ys @ xs" |
70 apply (induct xs, simp_all) |
70 apply (induct xs) |
|
71 apply simp_all |
71 apply (blast intro: perm_append_Cons) |
72 apply (blast intro: perm_append_Cons) |
72 done |
73 done |
73 |
74 |
74 lemma perm_append_single: "a # xs <~~> xs @ [a]" |
75 lemma perm_append_single: "a # xs <~~> xs @ [a]" |
75 by (rule perm.trans [OF _ perm_append_swap], simp) |
76 by (rule perm.trans [OF _ perm_append_swap]) simp |
76 |
77 |
77 lemma perm_rev: "rev xs <~~> xs" |
78 lemma perm_rev: "rev xs <~~> xs" |
78 apply (induct xs, simp_all) |
79 apply (induct xs) |
|
80 apply simp_all |
79 apply (blast intro!: perm_append_single intro: perm_sym) |
81 apply (blast intro!: perm_append_single intro: perm_sym) |
80 done |
82 done |
81 |
83 |
82 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys" |
84 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys" |
83 by (induct l, auto) |
85 by (induct l) auto |
84 |
86 |
85 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l" |
87 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l" |
86 by (blast intro!: perm_append_swap perm_append1) |
88 by (blast intro!: perm_append_swap perm_append1) |
87 |
89 |
88 |
90 |
89 subsection {* Further results *} |
91 subsection {* Further results *} |
90 |
92 |
91 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])" |
93 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])" |
92 by (blast intro: perm_empty_imp) |
94 by (blast intro: perm_empty_imp) |
93 |
95 |
94 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])" |
96 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])" |
95 apply auto |
97 apply auto |
96 apply (erule perm_sym [THEN perm_empty_imp]) |
98 apply (erule perm_sym [THEN perm_empty_imp]) |
97 done |
99 done |
98 |
100 |
99 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]" |
101 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]" |
100 by (erule perm.induct, auto) |
102 by (erule perm.induct) auto |
101 |
103 |
102 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])" |
104 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])" |
103 by (blast intro: perm_sing_imp) |
105 by (blast intro: perm_sing_imp) |
104 |
106 |
105 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])" |
107 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])" |
106 by (blast dest: perm_sym) |
108 by (blast dest: perm_sym) |
107 |
109 |
108 |
110 |
109 subsection {* Removing elements *} |
111 subsection {* Removing elements *} |
110 |
112 |
111 consts |
113 consts |
113 primrec |
115 primrec |
114 "remove x [] = []" |
116 "remove x [] = []" |
115 "remove x (y # ys) = (if x = y then ys else y # remove x ys)" |
117 "remove x (y # ys) = (if x = y then ys else y # remove x ys)" |
116 |
118 |
117 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys" |
119 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys" |
118 by (induct ys, auto) |
120 by (induct ys) auto |
119 |
121 |
120 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)" |
122 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)" |
121 by (induct l, auto) |
123 by (induct l) auto |
122 |
124 |
123 lemma multiset_of_remove[simp]: |
125 lemma multiset_of_remove[simp]: |
124 "multiset_of (remove a x) = multiset_of x - {#a#}" |
126 "multiset_of (remove a x) = multiset_of x - {#a#}" |
125 by (induct_tac x, auto simp: multiset_eq_conv_count_eq) |
127 apply (induct x) |
|
128 apply (auto simp: multiset_eq_conv_count_eq) |
|
129 done |
126 |
130 |
127 |
131 |
128 text {* \medskip Congruence rule *} |
132 text {* \medskip Congruence rule *} |
129 |
133 |
130 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys" |
134 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys" |
131 by (erule perm.induct, auto) |
135 by (erule perm.induct) auto |
132 |
136 |
133 lemma remove_hd [simp]: "remove z (z # xs) = xs" |
137 lemma remove_hd [simp]: "remove z (z # xs) = xs" |
134 by auto |
138 by auto |
135 |
139 |
136 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys" |
140 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys" |
137 by (drule_tac z = z in perm_remove_perm, auto) |
141 by (drule_tac z = z in perm_remove_perm) auto |
138 |
142 |
139 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)" |
143 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)" |
140 by (blast intro: cons_perm_imp_perm) |
144 by (blast intro: cons_perm_imp_perm) |
141 |
145 |
142 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys" |
146 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys" |
143 apply (induct zs rule: rev_induct) |
147 apply (induct zs rule: rev_induct) |
144 apply (simp_all (no_asm_use)) |
148 apply (simp_all (no_asm_use)) |
145 apply blast |
149 apply blast |
146 done |
150 done |
147 |
151 |
148 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)" |
152 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)" |
149 by (blast intro: append_perm_imp_perm perm_append1) |
153 by (blast intro: append_perm_imp_perm perm_append1) |
150 |
154 |
151 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)" |
155 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)" |
152 apply (safe intro!: perm_append2) |
156 apply (safe intro!: perm_append2) |
153 apply (rule append_perm_imp_perm) |
157 apply (rule append_perm_imp_perm) |
154 apply (rule perm_append_swap [THEN perm.trans]) |
158 apply (rule perm_append_swap [THEN perm.trans]) |
155 -- {* the previous step helps this @{text blast} call succeed quickly *} |
159 -- {* the previous step helps this @{text blast} call succeed quickly *} |
156 apply (blast intro: perm_append_swap) |
160 apply (blast intro: perm_append_swap) |
157 done |
161 done |
158 |
162 |
159 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) " |
163 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) " |
160 apply (rule iffI) |
164 apply (rule iffI) |
161 apply (erule_tac [2] perm.induct, simp_all add: union_ac) |
165 apply (erule_tac [2] perm.induct, simp_all add: union_ac) |
162 apply (erule rev_mp, rule_tac x=ys in spec) |
166 apply (erule rev_mp, rule_tac x=ys in spec) |
163 apply (induct_tac xs, auto) |
167 apply (induct_tac xs, auto) |
164 apply (erule_tac x = "remove a x" in allE, drule sym, simp) |
168 apply (erule_tac x = "remove a x" in allE, drule sym, simp) |
165 apply (subgoal_tac "a \<in> set x") |
169 apply (subgoal_tac "a \<in> set x") |
166 apply (drule_tac z=a in perm.Cons) |
170 apply (drule_tac z=a in perm.Cons) |
167 apply (erule perm.trans, rule perm_sym, erule perm_remove) |
171 apply (erule perm.trans, rule perm_sym, erule perm_remove) |
168 apply (drule_tac f=set_of in arg_cong, simp) |
172 apply (drule_tac f=set_of in arg_cong, simp) |
169 done |
173 done |
170 |
174 |
171 lemma multiset_of_le_perm_append: |
175 lemma multiset_of_le_perm_append: |
172 "(multiset_of xs \<le># multiset_of ys) = (\<exists> zs. xs @ zs <~~> ys)"; |
176 "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)"; |
173 apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) |
177 apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) |
174 apply (insert surj_multiset_of, drule surjD) |
178 apply (insert surj_multiset_of, drule surjD) |
175 apply (blast intro: sym)+ |
179 apply (blast intro: sym)+ |
176 done |
180 done |
177 |
181 |
178 end |
182 end |