author | wenzelm |

Tue Aug 27 22:40:39 2013 +0200 (2013-08-27) | |

changeset 53238 | 01ef0a103fc9 |

parent 53237 | 6bfe54791591 |

child 53239 | 2f21813cf2f0 |

tuned proofs;

1.1 --- a/src/HOL/Library/Permutation.thy Tue Aug 27 22:23:40 2013 +0200 1.2 +++ b/src/HOL/Library/Permutation.thy Tue Aug 27 22:40:39 2013 +0200 1.3 @@ -8,13 +8,12 @@ 1.4 imports Multiset 1.5 begin 1.6 1.7 -inductive 1.8 - perm :: "'a list => 'a list => bool" ("_ <~~> _" [50, 50] 50) 1.9 - where 1.10 - Nil [intro!]: "[] <~~> []" 1.11 - | swap [intro!]: "y # x # l <~~> x # y # l" 1.12 - | Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys" 1.13 - | trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs" 1.14 +inductive perm :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" ("_ <~~> _" [50, 50] 50) (* FIXME proper infix, without ambiguity!? *) 1.15 +where 1.16 + Nil [intro!]: "[] <~~> []" 1.17 +| swap [intro!]: "y # x # l <~~> x # y # l" 1.18 +| Cons [intro!]: "xs <~~> ys \<Longrightarrow> z # xs <~~> z # ys" 1.19 +| trans [intro]: "xs <~~> ys \<Longrightarrow> ys <~~> zs \<Longrightarrow> xs <~~> zs" 1.20 1.21 lemma perm_refl [iff]: "l <~~> l" 1.22 by (induct l) auto 1.23 @@ -22,7 +21,7 @@ 1.24 1.25 subsection {* Some examples of rule induction on permutations *} 1.26 1.27 -lemma xperm_empty_imp: "[] <~~> ys ==> ys = []" 1.28 +lemma xperm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []" 1.29 by (induct xs == "[]::'a list" ys pred: perm) simp_all 1.30 1.31 1.32 @@ -30,13 +29,13 @@ 1.33 \medskip This more general theorem is easier to understand! 1.34 *} 1.35 1.36 -lemma perm_length: "xs <~~> ys ==> length xs = length ys" 1.37 +lemma perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys" 1.38 by (induct pred: perm) simp_all 1.39 1.40 -lemma perm_empty_imp: "[] <~~> xs ==> xs = []" 1.41 +lemma perm_empty_imp: "[] <~~> xs \<Longrightarrow> xs = []" 1.42 by (drule perm_length) auto 1.43 1.44 -lemma perm_sym: "xs <~~> ys ==> ys <~~> xs" 1.45 +lemma perm_sym: "xs <~~> ys \<Longrightarrow> ys <~~> xs" 1.46 by (induct pred: perm) auto 1.47 1.48 1.49 @@ -64,10 +63,10 @@ 1.50 apply (blast intro!: perm_append_single intro: perm_sym) 1.51 done 1.52 1.53 -lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys" 1.54 +lemma perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys" 1.55 by (induct l) auto 1.56 1.57 -lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l" 1.58 +lemma perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l" 1.59 by (blast intro!: perm_append_swap perm_append1) 1.60 1.61 1.62 @@ -81,7 +80,7 @@ 1.63 apply (erule perm_sym [THEN perm_empty_imp]) 1.64 done 1.65 1.66 -lemma perm_sing_imp: "ys <~~> xs ==> xs = [y] ==> ys = [y]" 1.67 +lemma perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]" 1.68 by (induct pred: perm) auto 1.69 1.70 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])" 1.71 @@ -93,29 +92,26 @@ 1.72 1.73 subsection {* Removing elements *} 1.74 1.75 -lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove1 x ys" 1.76 +lemma perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys" 1.77 by (induct ys) auto 1.78 1.79 1.80 text {* \medskip Congruence rule *} 1.81 1.82 -lemma perm_remove_perm: "xs <~~> ys ==> remove1 z xs <~~> remove1 z ys" 1.83 +lemma perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys" 1.84 by (induct pred: perm) auto 1.85 1.86 lemma remove_hd [simp]: "remove1 z (z # xs) = xs" 1.87 by auto 1.88 1.89 -lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys" 1.90 +lemma cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys" 1.91 by (drule_tac z = z in perm_remove_perm) auto 1.92 1.93 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)" 1.94 by (blast intro: cons_perm_imp_perm) 1.95 1.96 -lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys" 1.97 - apply (induct zs arbitrary: xs ys rule: rev_induct) 1.98 - apply (simp_all (no_asm_use)) 1.99 - apply blast 1.100 - done 1.101 +lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys" 1.102 + by (induct zs arbitrary: xs ys rule: rev_induct) auto 1.103 1.104 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)" 1.105 by (blast intro: append_perm_imp_perm perm_append1) 1.106 @@ -135,38 +131,38 @@ 1.107 apply (induct_tac xs, auto) 1.108 apply (erule_tac x = "remove1 a x" in allE, drule sym, simp) 1.109 apply (subgoal_tac "a \<in> set x") 1.110 - apply (drule_tac z=a in perm.Cons) 1.111 + apply (drule_tac z = a in perm.Cons) 1.112 apply (erule perm.trans, rule perm_sym, erule perm_remove) 1.113 apply (drule_tac f=set_of in arg_cong, simp) 1.114 done 1.115 1.116 -lemma multiset_of_le_perm_append: 1.117 - "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)" 1.118 +lemma multiset_of_le_perm_append: "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)" 1.119 apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) 1.120 apply (insert surj_multiset_of, drule surjD) 1.121 apply (blast intro: sym)+ 1.122 done 1.123 1.124 -lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys" 1.125 +lemma perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys" 1.126 by (metis multiset_of_eq_perm multiset_of_eq_setD) 1.127 1.128 -lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys" 1.129 +lemma perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs = distinct ys" 1.130 apply (induct pred: perm) 1.131 apply simp_all 1.132 apply fastforce 1.133 apply (metis perm_set_eq) 1.134 done 1.135 1.136 -lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys" 1.137 +lemma eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys" 1.138 apply (induct xs arbitrary: ys rule: length_induct) 1.139 - apply (case_tac "remdups xs", simp, simp) 1.140 - apply (subgoal_tac "a : set (remdups ys)") 1.141 + apply (case_tac "remdups xs") 1.142 + apply simp_all 1.143 + apply (subgoal_tac "a \<in> set (remdups ys)") 1.144 prefer 2 apply (metis set.simps(2) insert_iff set_remdups) 1.145 apply (drule split_list) apply(elim exE conjE) 1.146 apply (drule_tac x=list in spec) apply(erule impE) prefer 2 1.147 apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2 1.148 apply simp 1.149 - apply (subgoal_tac "a#list <~~> a#ysa@zs") 1.150 + apply (subgoal_tac "a # list <~~> a # ysa @ zs") 1.151 apply (metis Cons_eq_appendI perm_append_Cons trans) 1.152 apply (metis Cons Cons_eq_appendI distinct.simps(2) 1.153 distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff) 1.154 @@ -180,21 +176,23 @@ 1.155 apply (rule length_remdups_leq) 1.156 done 1.157 1.158 -lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)" 1.159 +lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> (set x = set y)" 1.160 by (metis List.set_remdups perm_set_eq eq_set_perm_remdups) 1.161 1.162 lemma permutation_Ex_bij: 1.163 assumes "xs <~~> ys" 1.164 shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))" 1.165 using assms proof induct 1.166 - case Nil then show ?case unfolding bij_betw_def by simp 1.167 + case Nil 1.168 + then show ?case unfolding bij_betw_def by simp 1.169 next 1.170 case (swap y x l) 1.171 show ?case 1.172 proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI) 1.173 show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}" 1.174 by (auto simp: bij_betw_def) 1.175 - fix i assume "i < length(y#x#l)" 1.176 + fix i 1.177 + assume "i < length(y#x#l)" 1.178 show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i" 1.179 by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc) 1.180 qed 1.181 @@ -202,19 +200,21 @@ 1.182 case (Cons xs ys z) 1.183 then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}" and 1.184 perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" by blast 1.185 - let "?f i" = "case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0" 1.186 + let ?f = "\<lambda>i. case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0" 1.187 show ?case 1.188 proof (intro exI[of _ ?f] allI conjI impI) 1.189 have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}" 1.190 "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}" 1.191 by (simp_all add: lessThan_Suc_eq_insert_0) 1.192 - show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}" unfolding * 1.193 + show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}" 1.194 + unfolding * 1.195 proof (rule bij_betw_combine) 1.196 show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})" 1.197 using bij unfolding bij_betw_def 1.198 by (auto intro!: inj_onI imageI dest: inj_onD simp: image_compose[symmetric] comp_def) 1.199 qed (auto simp: bij_betw_def) 1.200 - fix i assume "i < length (z#xs)" 1.201 + fix i 1.202 + assume "i < length (z#xs)" 1.203 then show "(z # xs) ! i = (z # ys) ! (?f i)" 1.204 using perm by (cases i) auto 1.205 qed 1.206 @@ -224,13 +224,13 @@ 1.207 bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}" and 1.208 perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)" by blast 1.209 show ?case 1.210 - proof (intro exI[of _ "g\<circ>f"] conjI allI impI) 1.211 + proof (intro exI[of _ "g \<circ> f"] conjI allI impI) 1.212 show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}" 1.213 using bij by (rule bij_betw_trans) 1.214 fix i assume "i < length xs" 1.215 with bij have "f i < length ys" unfolding bij_betw_def by force 1.216 with `i < length xs` show "xs ! i = zs ! (g \<circ> f) i" 1.217 - using trans(1,3)[THEN perm_length] perm by force 1.218 + using trans(1,3)[THEN perm_length] perm by auto 1.219 qed 1.220 qed 1.221