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