| author | haftmann | 
| Mon, 07 Jun 2010 13:42:38 +0200 | |
| changeset 37384 | 5aba26803073 | 
| parent 30663 | 0b6aff7451b2 | 
| child 37474 | ce943f9edf5e | 
| permissions | -rw-r--r-- | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 1 | (* Title: HOL/Library/List_Prefix.thy | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 2 | Author: Tobias Nipkow and Markus Wenzel, TU Muenchen | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 3 | *) | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 4 | |
| 14706 | 5 | header {* List prefixes and postfixes *}
 | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 6 | |
| 15131 | 7 | theory List_Prefix | 
| 30663 
0b6aff7451b2
Main is (Complex_Main) base entry point in library theories
 haftmann parents: 
28562diff
changeset | 8 | imports List Main | 
| 15131 | 9 | begin | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 10 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 11 | subsection {* Prefix order on lists *}
 | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 12 | |
| 25764 | 13 | instantiation list :: (type) order | 
| 14 | begin | |
| 15 | ||
| 16 | definition | |
| 28562 | 17 | prefix_def [code del]: "xs \<le> ys = (\<exists>zs. ys = xs @ zs)" | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 18 | |
| 25764 | 19 | definition | 
| 28562 | 20 | strict_prefix_def [code del]: "xs < ys = (xs \<le> ys \<and> xs \<noteq> (ys::'a list))" | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 21 | |
| 25764 | 22 | instance | 
| 10389 | 23 | by intro_classes (auto simp add: prefix_def strict_prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 24 | |
| 25764 | 25 | end | 
| 26 | ||
| 10389 | 27 | lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys" | 
| 18730 | 28 | unfolding prefix_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 29 | |
| 21305 | 30 | lemma prefixE [elim?]: | 
| 31 | assumes "xs \<le> ys" | |
| 32 | obtains zs where "ys = xs @ zs" | |
| 23394 | 33 | using assms unfolding prefix_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 34 | |
| 10870 | 35 | lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys" | 
| 18730 | 36 | unfolding strict_prefix_def prefix_def by blast | 
| 10870 | 37 | |
| 38 | lemma strict_prefixE' [elim?]: | |
| 21305 | 39 | assumes "xs < ys" | 
| 40 | obtains z zs where "ys = xs @ z # zs" | |
| 10870 | 41 | proof - | 
| 21305 | 42 | from `xs < ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys" | 
| 18730 | 43 | unfolding strict_prefix_def prefix_def by blast | 
| 21305 | 44 | with that show ?thesis by (auto simp add: neq_Nil_conv) | 
| 10870 | 45 | qed | 
| 46 | ||
| 10389 | 47 | lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)" | 
| 18730 | 48 | unfolding strict_prefix_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 49 | |
| 10389 | 50 | lemma strict_prefixE [elim?]: | 
| 21305 | 51 | fixes xs ys :: "'a list" | 
| 52 | assumes "xs < ys" | |
| 53 | obtains "xs \<le> ys" and "xs \<noteq> ys" | |
| 23394 | 54 | using assms unfolding strict_prefix_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 55 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 56 | |
| 10389 | 57 | subsection {* Basic properties of prefixes *}
 | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 58 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 59 | theorem Nil_prefix [iff]: "[] \<le> xs" | 
| 10389 | 60 | by (simp add: prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 61 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 62 | theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])" | 
| 10389 | 63 | by (induct xs) (simp_all add: prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 64 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 65 | lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)" | 
| 10389 | 66 | proof | 
| 67 | assume "xs \<le> ys @ [y]" | |
| 68 | then obtain zs where zs: "ys @ [y] = xs @ zs" .. | |
| 69 | show "xs = ys @ [y] \<or> xs \<le> ys" | |
| 25564 | 70 | by (metis append_Nil2 butlast_append butlast_snoc prefixI zs) | 
| 10389 | 71 | next | 
| 72 | assume "xs = ys @ [y] \<or> xs \<le> ys" | |
| 23254 | 73 | then show "xs \<le> ys @ [y]" | 
| 25564 | 74 | by (metis order_eq_iff strict_prefixE strict_prefixI' xt1(7)) | 
| 10389 | 75 | qed | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 76 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 77 | lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)" | 
| 10389 | 78 | by (auto simp add: prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 79 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 80 | lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)" | 
| 10389 | 81 | by (induct xs) simp_all | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 82 | |
| 10389 | 83 | lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])" | 
| 25692 | 84 | by (metis append_Nil2 append_self_conv order_eq_iff prefixI) | 
| 25665 | 85 | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 86 | lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs" | 
| 25692 | 87 | by (metis order_le_less_trans prefixI strict_prefixE strict_prefixI) | 
| 25665 | 88 | |
| 14300 | 89 | lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs" | 
| 17201 | 90 | by (auto simp add: prefix_def) | 
| 14300 | 91 | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 92 | theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))" | 
| 10389 | 93 | by (cases xs) (auto simp add: prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 94 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 95 | theorem prefix_append: | 
| 25564 | 96 | "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))" | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 97 | apply (induct zs rule: rev_induct) | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 98 | apply force | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 99 | apply (simp del: append_assoc add: append_assoc [symmetric]) | 
| 25564 | 100 | apply (metis append_eq_appendI) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 101 | done | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 102 | |
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 103 | lemma append_one_prefix: | 
| 25564 | 104 | "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys" | 
| 25692 | 105 | unfolding prefix_def | 
| 106 | by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj | |
| 107 | eq_Nil_appendI nth_drop') | |
| 25665 | 108 | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 109 | theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys" | 
| 10389 | 110 | by (auto simp add: prefix_def) | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 111 | |
| 14300 | 112 | lemma prefix_same_cases: | 
| 25564 | 113 | "(xs\<^isub>1::'a list) \<le> ys \<Longrightarrow> xs\<^isub>2 \<le> ys \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1" | 
| 25692 | 114 | unfolding prefix_def by (metis append_eq_append_conv2) | 
| 25665 | 115 | |
| 25564 | 116 | lemma set_mono_prefix: "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys" | 
| 25692 | 117 | by (auto simp add: prefix_def) | 
| 14300 | 118 | |
| 25564 | 119 | lemma take_is_prefix: "take n xs \<le> xs" | 
| 25692 | 120 | unfolding prefix_def by (metis append_take_drop_id) | 
| 25665 | 121 | |
| 25692 | 122 | lemma map_prefixI: "xs \<le> ys \<Longrightarrow> map f xs \<le> map f ys" | 
| 123 | by (auto simp: prefix_def) | |
| 25322 | 124 | |
| 25692 | 125 | lemma prefix_length_less: "xs < ys \<Longrightarrow> length xs < length ys" | 
| 126 | by (auto simp: strict_prefix_def prefix_def) | |
| 25665 | 127 | |
| 25299 | 128 | lemma strict_prefix_simps [simp]: | 
| 25692 | 129 | "xs < [] = False" | 
| 130 | "[] < (x # xs) = True" | |
| 131 | "(x # xs) < (y # ys) = (x = y \<and> xs < ys)" | |
| 132 | by (simp_all add: strict_prefix_def cong: conj_cong) | |
| 25299 | 133 | |
| 25564 | 134 | lemma take_strict_prefix: "xs < ys \<Longrightarrow> take n xs < ys" | 
| 25692 | 135 | apply (induct n arbitrary: xs ys) | 
| 136 | apply (case_tac ys, simp_all)[1] | |
| 137 | apply (metis order_less_trans strict_prefixI take_is_prefix) | |
| 138 | done | |
| 25299 | 139 | |
| 25355 | 140 | lemma not_prefix_cases: | 
| 25299 | 141 | assumes pfx: "\<not> ps \<le> ls" | 
| 25356 | 142 | obtains | 
| 143 | (c1) "ps \<noteq> []" and "ls = []" | |
| 144 | | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> as \<le> xs" | |
| 145 | | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a" | |
| 25299 | 146 | proof (cases ps) | 
| 25692 | 147 | case Nil then show ?thesis using pfx by simp | 
| 25299 | 148 | next | 
| 149 | case (Cons a as) | |
| 25692 | 150 | note c = `ps = a#as` | 
| 25299 | 151 | show ?thesis | 
| 152 | proof (cases ls) | |
| 25692 | 153 | case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil) | 
| 25299 | 154 | next | 
| 155 | case (Cons x xs) | |
| 156 | show ?thesis | |
| 157 | proof (cases "x = a") | |
| 25355 | 158 | case True | 
| 159 | have "\<not> as \<le> xs" using pfx c Cons True by simp | |
| 160 | with c Cons True show ?thesis by (rule c2) | |
| 161 | next | |
| 162 | case False | |
| 163 | with c Cons show ?thesis by (rule c3) | |
| 25299 | 164 | qed | 
| 165 | qed | |
| 166 | qed | |
| 167 | ||
| 168 | lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]: | |
| 169 | assumes np: "\<not> ps \<le> ls" | |
| 25356 | 170 | and base: "\<And>x xs. P (x#xs) []" | 
| 171 | and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)" | |
| 172 | and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> xs \<le> ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)" | |
| 173 | shows "P ps ls" using np | |
| 25299 | 174 | proof (induct ls arbitrary: ps) | 
| 25355 | 175 | case Nil then show ?case | 
| 25299 | 176 | by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base) | 
| 177 | next | |
| 25355 | 178 | case (Cons y ys) | 
| 179 | then have npfx: "\<not> ps \<le> (y # ys)" by simp | |
| 180 | then obtain x xs where pv: "ps = x # xs" | |
| 25299 | 181 | by (rule not_prefix_cases) auto | 
| 25564 | 182 | show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2) | 
| 25299 | 183 | qed | 
| 14300 | 184 | |
| 25356 | 185 | |
| 10389 | 186 | subsection {* Parallel lists *}
 | 
| 187 | ||
| 19086 | 188 | definition | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21305diff
changeset | 189 | parallel :: "'a list => 'a list => bool" (infixl "\<parallel>" 50) where | 
| 19086 | 190 | "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)" | 
| 10389 | 191 | |
| 192 | lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys" | |
| 25692 | 193 | unfolding parallel_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 194 | |
| 10389 | 195 | lemma parallelE [elim]: | 
| 25692 | 196 | assumes "xs \<parallel> ys" | 
| 197 | obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs" | |
| 198 | using assms unfolding parallel_def by blast | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 199 | |
| 10389 | 200 | theorem prefix_cases: | 
| 25692 | 201 | obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys" | 
| 202 | unfolding parallel_def strict_prefix_def by blast | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 203 | |
| 10389 | 204 | theorem parallel_decomp: | 
| 205 | "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs" | |
| 10408 | 206 | proof (induct xs rule: rev_induct) | 
| 11987 | 207 | case Nil | 
| 23254 | 208 | then have False by auto | 
| 209 | then show ?case .. | |
| 10408 | 210 | next | 
| 11987 | 211 | case (snoc x xs) | 
| 212 | show ?case | |
| 10408 | 213 | proof (rule prefix_cases) | 
| 214 | assume le: "xs \<le> ys" | |
| 215 | then obtain ys' where ys: "ys = xs @ ys'" .. | |
| 216 | show ?thesis | |
| 217 | proof (cases ys') | |
| 25564 | 218 | assume "ys' = []" | 
| 25692 | 219 | then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys) | 
| 10389 | 220 | next | 
| 10408 | 221 | fix c cs assume ys': "ys' = c # cs" | 
| 25692 | 222 | then show ?thesis | 
| 223 | by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixI | |
| 224 | same_prefix_prefix snoc.prems ys) | |
| 10389 | 225 | qed | 
| 10408 | 226 | next | 
| 23254 | 227 | assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: strict_prefix_def) | 
| 11987 | 228 | with snoc have False by blast | 
| 23254 | 229 | then show ?thesis .. | 
| 10408 | 230 | next | 
| 231 | assume "xs \<parallel> ys" | |
| 11987 | 232 | with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c" | 
| 10408 | 233 | and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" | 
| 234 | by blast | |
| 235 | from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp | |
| 236 | with neq ys show ?thesis by blast | |
| 10389 | 237 | qed | 
| 238 | qed | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 239 | |
| 25564 | 240 | lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" | 
| 25692 | 241 | apply (rule parallelI) | 
| 242 | apply (erule parallelE, erule conjE, | |
| 243 | induct rule: not_prefix_induct, simp+)+ | |
| 244 | done | |
| 25299 | 245 | |
| 25692 | 246 | lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" | 
| 247 | by (simp add: parallel_append) | |
| 25299 | 248 | |
| 25692 | 249 | lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" | 
| 250 | unfolding parallel_def by auto | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 251 | |
| 25356 | 252 | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 253 | subsection {* Postfix order on lists *}
 | 
| 17201 | 254 | |
| 19086 | 255 | definition | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21305diff
changeset | 256 |   postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
 | 
| 19086 | 257 | "(xs >>= ys) = (\<exists>zs. xs = zs @ ys)" | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 258 | |
| 21305 | 259 | lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys" | 
| 25692 | 260 | unfolding postfix_def by blast | 
| 21305 | 261 | |
| 262 | lemma postfixE [elim?]: | |
| 25692 | 263 | assumes "xs >>= ys" | 
| 264 | obtains zs where "xs = zs @ ys" | |
| 265 | using assms unfolding postfix_def by blast | |
| 21305 | 266 | |
| 267 | lemma postfix_refl [iff]: "xs >>= xs" | |
| 14706 | 268 | by (auto simp add: postfix_def) | 
| 17201 | 269 | lemma postfix_trans: "\<lbrakk>xs >>= ys; ys >>= zs\<rbrakk> \<Longrightarrow> xs >>= zs" | 
| 14706 | 270 | by (auto simp add: postfix_def) | 
| 17201 | 271 | lemma postfix_antisym: "\<lbrakk>xs >>= ys; ys >>= xs\<rbrakk> \<Longrightarrow> xs = ys" | 
| 14706 | 272 | by (auto simp add: postfix_def) | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 273 | |
| 17201 | 274 | lemma Nil_postfix [iff]: "xs >>= []" | 
| 14706 | 275 | by (simp add: postfix_def) | 
| 17201 | 276 | lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])" | 
| 21305 | 277 | by (auto simp add: postfix_def) | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 278 | |
| 17201 | 279 | lemma postfix_ConsI: "xs >>= ys \<Longrightarrow> x#xs >>= ys" | 
| 14706 | 280 | by (auto simp add: postfix_def) | 
| 17201 | 281 | lemma postfix_ConsD: "xs >>= y#ys \<Longrightarrow> xs >>= ys" | 
| 14706 | 282 | by (auto simp add: postfix_def) | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 283 | |
| 17201 | 284 | lemma postfix_appendI: "xs >>= ys \<Longrightarrow> zs @ xs >>= ys" | 
| 14706 | 285 | by (auto simp add: postfix_def) | 
| 17201 | 286 | lemma postfix_appendD: "xs >>= zs @ ys \<Longrightarrow> xs >>= ys" | 
| 21305 | 287 | by (auto simp add: postfix_def) | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 288 | |
| 21305 | 289 | lemma postfix_is_subset: "xs >>= ys ==> set ys \<subseteq> set xs" | 
| 290 | proof - | |
| 291 | assume "xs >>= ys" | |
| 292 | then obtain zs where "xs = zs @ ys" .. | |
| 293 | then show ?thesis by (induct zs) auto | |
| 294 | qed | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 295 | |
| 21305 | 296 | lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys" | 
| 297 | proof - | |
| 298 | assume "x#xs >>= y#ys" | |
| 299 | then obtain zs where "x#xs = zs @ y#ys" .. | |
| 300 | then show ?thesis | |
| 301 | by (induct zs) (auto intro!: postfix_appendI postfix_ConsI) | |
| 302 | qed | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 303 | |
| 21305 | 304 | lemma postfix_to_prefix: "xs >>= ys \<longleftrightarrow> rev ys \<le> rev xs" | 
| 305 | proof | |
| 306 | assume "xs >>= ys" | |
| 307 | then obtain zs where "xs = zs @ ys" .. | |
| 308 | then have "rev xs = rev ys @ rev zs" by simp | |
| 309 | then show "rev ys <= rev xs" .. | |
| 310 | next | |
| 311 | assume "rev ys <= rev xs" | |
| 312 | then obtain zs where "rev xs = rev ys @ zs" .. | |
| 313 | then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp | |
| 314 | then have "xs = rev zs @ ys" by simp | |
| 315 | then show "xs >>= ys" .. | |
| 316 | qed | |
| 17201 | 317 | |
| 25564 | 318 | lemma distinct_postfix: "distinct xs \<Longrightarrow> xs >>= ys \<Longrightarrow> distinct ys" | 
| 25692 | 319 | by (clarsimp elim!: postfixE) | 
| 25299 | 320 | |
| 25564 | 321 | lemma postfix_map: "xs >>= ys \<Longrightarrow> map f xs >>= map f ys" | 
| 25692 | 322 | by (auto elim!: postfixE intro: postfixI) | 
| 25299 | 323 | |
| 25356 | 324 | lemma postfix_drop: "as >>= drop n as" | 
| 25692 | 325 | unfolding postfix_def | 
| 326 | apply (rule exI [where x = "take n as"]) | |
| 327 | apply simp | |
| 328 | done | |
| 25299 | 329 | |
| 25564 | 330 | lemma postfix_take: "xs >>= ys \<Longrightarrow> xs = take (length xs - length ys) xs @ ys" | 
| 25692 | 331 | by (clarsimp elim!: postfixE) | 
| 25299 | 332 | |
| 25356 | 333 | lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y" | 
| 25692 | 334 | by blast | 
| 25299 | 335 | |
| 25356 | 336 | lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x" | 
| 25692 | 337 | by blast | 
| 25355 | 338 | |
| 339 | lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" | |
| 25692 | 340 | unfolding parallel_def by simp | 
| 25355 | 341 | |
| 25299 | 342 | lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" | 
| 25692 | 343 | unfolding parallel_def by simp | 
| 25299 | 344 | |
| 25564 | 345 | lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" | 
| 25692 | 346 | by auto | 
| 25299 | 347 | |
| 25564 | 348 | lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" | 
| 25692 | 349 | by (metis Cons_prefix_Cons parallelE parallelI) | 
| 25665 | 350 | |
| 25299 | 351 | lemma not_equal_is_parallel: | 
| 352 | assumes neq: "xs \<noteq> ys" | |
| 25356 | 353 | and len: "length xs = length ys" | 
| 354 | shows "xs \<parallel> ys" | |
| 25299 | 355 | using len neq | 
| 25355 | 356 | proof (induct rule: list_induct2) | 
| 26445 | 357 | case Nil | 
| 25356 | 358 | then show ?case by simp | 
| 25299 | 359 | next | 
| 26445 | 360 | case (Cons a as b bs) | 
| 25355 | 361 | have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact | 
| 25299 | 362 | show ?case | 
| 363 | proof (cases "a = b") | |
| 25355 | 364 | case True | 
| 26445 | 365 | then have "as \<noteq> bs" using Cons by simp | 
| 25355 | 366 | then show ?thesis by (rule Cons_parallelI2 [OF True ih]) | 
| 25299 | 367 | next | 
| 368 | case False | |
| 25355 | 369 | then show ?thesis by (rule Cons_parallelI1) | 
| 25299 | 370 | qed | 
| 371 | qed | |
| 22178 | 372 | |
| 25355 | 373 | |
| 25356 | 374 | subsection {* Executable code *}
 | 
| 22178 | 375 | |
| 28562 | 376 | lemma less_eq_code [code]: | 
| 25356 | 377 |     "([]\<Colon>'a\<Colon>{eq, ord} list) \<le> xs \<longleftrightarrow> True"
 | 
| 378 |     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> [] \<longleftrightarrow> False"
 | |
| 379 |     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"
 | |
| 22178 | 380 | by simp_all | 
| 381 | ||
| 28562 | 382 | lemma less_code [code]: | 
| 25356 | 383 |     "xs < ([]\<Colon>'a\<Colon>{eq, ord} list) \<longleftrightarrow> False"
 | 
| 384 |     "[] < (x\<Colon>'a\<Colon>{eq, ord})# xs \<longleftrightarrow> True"
 | |
| 385 |     "(x\<Colon>'a\<Colon>{eq, ord}) # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"
 | |
| 22178 | 386 | unfolding strict_prefix_def by auto | 
| 387 | ||
| 28562 | 388 | lemmas [code] = postfix_to_prefix | 
| 22178 | 389 | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 390 | end |