src/HOL/Library/Permutation.thy
 changeset 15005 546c8e7e28d4 parent 14706 71590b7733b7 child 15072 4861bf6af0b4
```     1.1 --- a/src/HOL/Library/Permutation.thy	Thu Jun 24 17:52:55 2004 +0200
1.2 +++ b/src/HOL/Library/Permutation.thy	Thu Jun 24 17:54:53 2004 +0200
1.3 @@ -1,15 +1,10 @@
1.4  (*  Title:      HOL/Library/Permutation.thy
1.5 -    ID:         \$Id\$
1.6 -    Author:     Lawrence C Paulson and Thomas M Rasmussen
1.7 -    Copyright   1995  University of Cambridge
1.8 -
1.9 -TODO: it would be nice to prove (for "multiset", defined on
1.10 -HOL/ex/Sorting.thy) xs <~~> ys = (\<forall>x. multiset xs x = multiset ys x)
1.11 +    Author:     Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker
1.12  *)
1.13
1.15
1.16 -theory Permutation = Main:
1.17 +theory Permutation = Multiset:
1.18
1.19  consts
1.20    perm :: "('a list * 'a list) set"
1.21 @@ -27,9 +22,7 @@
1.22      trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
1.23
1.24  lemma perm_refl [iff]: "l <~~> l"
1.25 -  apply (induct l)
1.26 -   apply auto
1.27 -  done
1.28 +by (induct l, auto)
1.29
1.30
1.31  subsection {* Some examples of rule induction on permutations *}
1.32 @@ -41,9 +34,7 @@
1.33    done
1.34
1.35  lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
1.36 -  apply (insert xperm_empty_imp_aux)
1.37 -  apply blast
1.38 -  done
1.39 +by (insert xperm_empty_imp_aux, blast)
1.40
1.41
1.42  text {*
1.43 @@ -51,24 +42,16 @@
1.44    *}
1.45
1.46  lemma perm_length: "xs <~~> ys ==> length xs = length ys"
1.47 -  apply (erule perm.induct)
1.48 -     apply simp_all
1.49 -  done
1.50 +by (erule perm.induct, simp_all)
1.51
1.52  lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
1.53 -  apply (drule perm_length)
1.54 -  apply auto
1.55 -  done
1.56 +by (drule perm_length, auto)
1.57
1.58  lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
1.59 -  apply (erule perm.induct)
1.60 -     apply auto
1.61 -  done
1.62 +by (erule perm.induct, auto)
1.63
1.64  lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
1.65 -  apply (erule perm.induct)
1.66 -     apply auto
1.67 -  done
1.68 +by (erule perm.induct, auto)
1.69
1.70
1.71  subsection {* Ways of making new permutations *}
1.72 @@ -78,44 +61,35 @@
1.73  *}
1.74
1.75  lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
1.76 -  apply (induct xs)
1.77 -   apply auto
1.78 -  done
1.79 +by (induct xs, auto)
1.80
1.81  lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
1.82 -  apply (induct xs)
1.83 -    apply simp_all
1.84 +  apply (induct xs, simp_all)
1.85    apply (blast intro: perm_append_Cons)
1.86    done
1.87
1.88  lemma perm_append_single: "a # xs <~~> xs @ [a]"
1.89    apply (rule perm.trans)
1.90     prefer 2
1.91 -   apply (rule perm_append_swap)
1.92 -  apply simp
1.93 +   apply (rule perm_append_swap, simp)
1.94    done
1.95
1.96  lemma perm_rev: "rev xs <~~> xs"
1.97 -  apply (induct xs)
1.98 -   apply simp_all
1.99 +  apply (induct xs, simp_all)
1.100    apply (blast intro!: perm_append_single intro: perm_sym)
1.101    done
1.102
1.103  lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
1.104 -  apply (induct l)
1.105 -   apply auto
1.106 -  done
1.107 +by (induct l, auto)
1.108
1.109  lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
1.110 -  apply (blast intro!: perm_append_swap perm_append1)
1.111 -  done
1.112 +by (blast intro!: perm_append_swap perm_append1)
1.113
1.114
1.115  subsection {* Further results *}
1.116
1.117  lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
1.118 -  apply (blast intro: perm_empty_imp)
1.119 -  done
1.120 +by (blast intro: perm_empty_imp)
1.121
1.122  lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
1.123    apply auto
1.124 @@ -123,17 +97,13 @@
1.125    done
1.126
1.127  lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
1.128 -  apply (erule perm.induct)
1.129 -     apply auto
1.130 -  done
1.131 +by (erule perm.induct, auto)
1.132
1.133  lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
1.134 -  apply (blast intro: perm_sing_imp)
1.135 -  done
1.136 +by (blast intro: perm_sing_imp)
1.137
1.138  lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
1.139 -  apply (blast dest: perm_sym)
1.140 -  done
1.141 +by (blast dest: perm_sym)
1.142
1.143
1.144  subsection {* Removing elements *}
1.145 @@ -145,35 +115,26 @@
1.146    "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
1.147
1.148  lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
1.149 -  apply (induct ys)
1.150 -   apply auto
1.151 -  done
1.152 +by (induct ys, auto)
1.153
1.154  lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
1.155 -  apply (induct l)
1.156 -   apply auto
1.157 -  done
1.158 +by (induct l, auto)
1.159
1.160
1.161  text {* \medskip Congruence rule *}
1.162
1.163  lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
1.164 -  apply (erule perm.induct)
1.165 -     apply auto
1.166 -  done
1.167 +by (erule perm.induct, auto)
1.168
1.169  lemma remove_hd [simp]: "remove z (z # xs) = xs"
1.170    apply auto
1.171    done
1.172
1.173  lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
1.174 -  apply (drule_tac z = z in perm_remove_perm)
1.175 -  apply auto
1.176 -  done
1.177 +by (drule_tac z = z in perm_remove_perm, auto)
1.178
1.179  lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
1.180 -  apply (blast intro: cons_perm_imp_perm)
1.181 -  done
1.182 +by (blast intro: cons_perm_imp_perm)
1.183
1.184  lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
1.185    apply (induct zs rule: rev_induct)
1.186 @@ -182,8 +143,7 @@
1.187    done
1.188
1.189  lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
1.190 -  apply (blast intro: append_perm_imp_perm perm_append1)
1.191 -  done
1.192 +by (blast intro: append_perm_imp_perm perm_append1)
1.193
1.194  lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
1.195    apply (safe intro!: perm_append2)
1.196 @@ -193,4 +153,47 @@
1.197    apply (blast intro: perm_append_swap)
1.198    done
1.199
1.200 +(****************** Norbert Voelker 17 June 2004 **************)
1.201 +
1.202 +consts
1.203 +  multiset_of :: "'a list \<Rightarrow> 'a multiset"
1.204 +primrec
1.205 +  "multiset_of [] = {#}"
1.206 +  "multiset_of (a # x) = multiset_of x + {# a #}"
1.207 +
1.208 +lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
1.209 +  by (induct_tac x, auto)
1.210 +
1.211 +lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
1.212 +  by (induct_tac x, auto)
1.213 +
1.214 +lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x"
1.215 + by (induct_tac x, auto)
1.216 +
1.217 +lemma multiset_of_remove[simp]:
1.218 +  "multiset_of (remove a x) = multiset_of x - {#a#}"
1.219 +  by (induct_tac x, auto simp: multiset_eq_conv_count_eq)
1.220 +
1.221 +lemma multiset_of_eq_perm:  "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
1.222 +  apply (rule iffI)
1.223 +  apply (erule_tac  perm.induct, simp_all add: union_ac)
1.224 +  apply (erule rev_mp, rule_tac x=ys in spec, induct_tac xs, auto)
1.225 +  apply (erule_tac x = "remove a x" in allE, drule sym, simp)
1.226 +  apply (subgoal_tac "a \<in> set x")
1.227 +  apply (drule_tac z=a in perm.Cons)
1.228 +  apply (erule perm.trans, rule perm_sym, erule perm_remove)
1.229 +  apply (drule_tac f=set_of in arg_cong, simp)
1.230 +  done
1.231 +
1.232 +lemma set_count_multiset_of: "set x = {a. 0 < count (multiset_of x) a}"
1.233 +  by (induct_tac x, auto)
1.234 +
1.235 +lemma distinct_count_multiset_of:
1.236 +   "distinct x \<Longrightarrow> count (multiset_of x) a = (if a \<in> set x then 1 else 0)"
1.237 +  by (erule rev_mp, induct_tac x, auto)
1.238 +
1.239 +lemma distinct_set_eq_iff_multiset_of_eq:
1.240 +  "\<lbrakk>distinct x; distinct y\<rbrakk> \<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)"
1.241 +  by (auto simp: multiset_eq_conv_count_eq distinct_count_multiset_of)
1.242 +
1.243  end
```