author | Christian Sternagel |
Thu, 30 Aug 2012 13:03:03 +0900 | |
changeset 49086 | 835fd053d17d |
parent 45236 | src/HOL/Library/List_Prefix.thy@ac4a2a66707d |
permissions | -rw-r--r-- |
49086 | 1 |
(* Title: HOL/Library/Sublist.thy |
10330
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 |
|
49086 | 5 |
header {* List prefixes, suffixes, and embedding*} |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
6 |
|
49086 | 7 |
theory Sublist |
8 |
imports 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 |
|
49086 | 13 |
definition prefixeq :: "'a list => 'a list => bool" where |
14 |
"prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)" |
|
15 |
||
16 |
definition prefix :: "'a list => 'a list => bool" where |
|
17 |
"prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys" |
|
25764 | 18 |
|
49086 | 19 |
interpretation prefix_order: order prefixeq prefix |
20 |
by default (auto simp: prefixeq_def prefix_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
21 |
|
49086 | 22 |
interpretation prefix_bot: bot prefixeq prefix Nil |
23 |
by default (simp add: prefixeq_def) |
|
24 |
||
25 |
lemma prefixeqI [intro?]: "ys = xs @ zs ==> prefixeq xs ys" |
|
26 |
unfolding prefixeq_def by blast |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
27 |
|
49086 | 28 |
lemma prefixeqE [elim?]: |
29 |
assumes "prefixeq xs ys" |
|
30 |
obtains zs where "ys = xs @ zs" |
|
31 |
using assms unfolding prefixeq_def by blast |
|
32 |
||
33 |
lemma prefixI' [intro?]: "ys = xs @ z # zs ==> prefix xs ys" |
|
34 |
unfolding prefix_def prefixeq_def by blast |
|
37474 | 35 |
|
49086 | 36 |
lemma prefixE' [elim?]: |
37 |
assumes "prefix xs ys" |
|
38 |
obtains z zs where "ys = xs @ z # zs" |
|
39 |
proof - |
|
40 |
from `prefix xs ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys" |
|
41 |
unfolding prefix_def prefixeq_def by blast |
|
42 |
with that show ?thesis by (auto simp add: neq_Nil_conv) |
|
43 |
qed |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
44 |
|
49086 | 45 |
lemma prefixI [intro?]: "prefixeq xs ys ==> xs \<noteq> ys ==> prefix xs ys" |
18730 | 46 |
unfolding prefix_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
47 |
|
21305 | 48 |
lemma prefixE [elim?]: |
49086 | 49 |
fixes xs ys :: "'a list" |
50 |
assumes "prefix xs ys" |
|
51 |
obtains "prefixeq xs ys" and "xs \<noteq> ys" |
|
23394 | 52 |
using assms unfolding prefix_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
53 |
|
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
54 |
|
10389 | 55 |
subsection {* Basic properties of prefixes *} |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
56 |
|
49086 | 57 |
theorem Nil_prefixeq [iff]: "prefixeq [] xs" |
58 |
by (simp add: prefixeq_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
59 |
|
49086 | 60 |
theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])" |
61 |
by (induct xs) (simp_all add: prefixeq_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
62 |
|
49086 | 63 |
lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys" |
10389 | 64 |
proof |
49086 | 65 |
assume "prefixeq xs (ys @ [y])" |
10389 | 66 |
then obtain zs where zs: "ys @ [y] = xs @ zs" .. |
49086 | 67 |
show "xs = ys @ [y] \<or> prefixeq xs ys" |
68 |
by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs) |
|
10389 | 69 |
next |
49086 | 70 |
assume "xs = ys @ [y] \<or> prefixeq xs ys" |
71 |
then show "prefixeq xs (ys @ [y])" |
|
72 |
by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI) |
|
10389 | 73 |
qed |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
74 |
|
49086 | 75 |
lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)" |
76 |
by (auto simp add: prefixeq_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
77 |
|
49086 | 78 |
lemma prefixeq_code [code]: |
79 |
"prefixeq [] xs \<longleftrightarrow> True" |
|
80 |
"prefixeq (x # xs) [] \<longleftrightarrow> False" |
|
81 |
"prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys" |
|
37474 | 82 |
by simp_all |
83 |
||
49086 | 84 |
lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs" |
10389 | 85 |
by (induct xs) simp_all |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
86 |
|
49086 | 87 |
lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])" |
88 |
by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI) |
|
25665 | 89 |
|
49086 | 90 |
lemma prefixeq_prefixeq [simp]: "prefixeq xs ys ==> prefixeq xs (ys @ zs)" |
91 |
by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI) |
|
25665 | 92 |
|
49086 | 93 |
lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs" |
94 |
by (auto simp add: prefixeq_def) |
|
14300 | 95 |
|
49086 | 96 |
theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))" |
97 |
by (cases xs) (auto simp add: prefixeq_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
98 |
|
49086 | 99 |
theorem prefixeq_append: |
100 |
"prefixeq xs (ys @ zs) = (prefixeq xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefixeq us zs))" |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
101 |
apply (induct zs rule: rev_induct) |
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
102 |
apply force |
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
103 |
apply (simp del: append_assoc add: append_assoc [symmetric]) |
25564 | 104 |
apply (metis append_eq_appendI) |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
105 |
done |
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
106 |
|
49086 | 107 |
lemma append_one_prefixeq: |
108 |
"prefixeq xs ys ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys" |
|
109 |
unfolding prefixeq_def |
|
25692 | 110 |
by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj |
111 |
eq_Nil_appendI nth_drop') |
|
25665 | 112 |
|
49086 | 113 |
theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys" |
114 |
by (auto simp add: prefixeq_def) |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
115 |
|
49086 | 116 |
lemma prefixeq_same_cases: |
117 |
"prefixeq (xs\<^isub>1::'a list) ys \<Longrightarrow> prefixeq xs\<^isub>2 ys \<Longrightarrow> prefixeq xs\<^isub>1 xs\<^isub>2 \<or> prefixeq xs\<^isub>2 xs\<^isub>1" |
|
118 |
unfolding prefixeq_def by (metis append_eq_append_conv2) |
|
25665 | 119 |
|
49086 | 120 |
lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" |
121 |
by (auto simp add: prefixeq_def) |
|
14300 | 122 |
|
49086 | 123 |
lemma take_is_prefixeq: "prefixeq (take n xs) xs" |
124 |
unfolding prefixeq_def by (metis append_take_drop_id) |
|
25665 | 125 |
|
49086 | 126 |
lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)" |
127 |
by (auto simp: prefixeq_def) |
|
25322 | 128 |
|
49086 | 129 |
lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys" |
130 |
by (auto simp: prefix_def prefixeq_def) |
|
25665 | 131 |
|
49086 | 132 |
lemma prefix_simps [simp, code]: |
133 |
"prefix xs [] \<longleftrightarrow> False" |
|
134 |
"prefix [] (x # xs) \<longleftrightarrow> True" |
|
135 |
"prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys" |
|
136 |
by (simp_all add: prefix_def cong: conj_cong) |
|
25299 | 137 |
|
49086 | 138 |
lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys" |
25692 | 139 |
apply (induct n arbitrary: xs ys) |
140 |
apply (case_tac ys, simp_all)[1] |
|
49086 | 141 |
apply (metis prefix_order.less_trans prefixI take_is_prefixeq) |
25692 | 142 |
done |
25299 | 143 |
|
49086 | 144 |
lemma not_prefixeq_cases: |
145 |
assumes pfx: "\<not> prefixeq ps ls" |
|
25356 | 146 |
obtains |
147 |
(c1) "ps \<noteq> []" and "ls = []" |
|
49086 | 148 |
| (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs" |
25356 | 149 |
| (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a" |
25299 | 150 |
proof (cases ps) |
25692 | 151 |
case Nil then show ?thesis using pfx by simp |
25299 | 152 |
next |
153 |
case (Cons a as) |
|
25692 | 154 |
note c = `ps = a#as` |
25299 | 155 |
show ?thesis |
156 |
proof (cases ls) |
|
49086 | 157 |
case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil) |
25299 | 158 |
next |
159 |
case (Cons x xs) |
|
160 |
show ?thesis |
|
161 |
proof (cases "x = a") |
|
25355 | 162 |
case True |
49086 | 163 |
have "\<not> prefixeq as xs" using pfx c Cons True by simp |
25355 | 164 |
with c Cons True show ?thesis by (rule c2) |
165 |
next |
|
166 |
case False |
|
167 |
with c Cons show ?thesis by (rule c3) |
|
25299 | 168 |
qed |
169 |
qed |
|
170 |
qed |
|
171 |
||
49086 | 172 |
lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]: |
173 |
assumes np: "\<not> prefixeq ps ls" |
|
25356 | 174 |
and base: "\<And>x xs. P (x#xs) []" |
175 |
and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)" |
|
49086 | 176 |
and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)" |
25356 | 177 |
shows "P ps ls" using np |
25299 | 178 |
proof (induct ls arbitrary: ps) |
25355 | 179 |
case Nil then show ?case |
49086 | 180 |
by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base) |
25299 | 181 |
next |
25355 | 182 |
case (Cons y ys) |
49086 | 183 |
then have npfx: "\<not> prefixeq ps (y # ys)" by simp |
25355 | 184 |
then obtain x xs where pv: "ps = x # xs" |
49086 | 185 |
by (rule not_prefixeq_cases) auto |
186 |
show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2) |
|
25299 | 187 |
qed |
14300 | 188 |
|
25356 | 189 |
|
10389 | 190 |
subsection {* Parallel lists *} |
191 |
||
19086 | 192 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21305
diff
changeset
|
193 |
parallel :: "'a list => 'a list => bool" (infixl "\<parallel>" 50) where |
49086 | 194 |
"(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)" |
10389 | 195 |
|
49086 | 196 |
lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> xs \<parallel> ys" |
25692 | 197 |
unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
198 |
|
10389 | 199 |
lemma parallelE [elim]: |
25692 | 200 |
assumes "xs \<parallel> ys" |
49086 | 201 |
obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs" |
25692 | 202 |
using assms unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
203 |
|
49086 | 204 |
theorem prefixeq_cases: |
205 |
obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys" |
|
206 |
unfolding parallel_def prefix_def by blast |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
207 |
|
10389 | 208 |
theorem parallel_decomp: |
209 |
"xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs" |
|
10408 | 210 |
proof (induct xs rule: rev_induct) |
11987 | 211 |
case Nil |
23254 | 212 |
then have False by auto |
213 |
then show ?case .. |
|
10408 | 214 |
next |
11987 | 215 |
case (snoc x xs) |
216 |
show ?case |
|
49086 | 217 |
proof (rule prefixeq_cases) |
218 |
assume le: "prefixeq xs ys" |
|
10408 | 219 |
then obtain ys' where ys: "ys = xs @ ys'" .. |
220 |
show ?thesis |
|
221 |
proof (cases ys') |
|
25564 | 222 |
assume "ys' = []" |
49086 | 223 |
then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys) |
10389 | 224 |
next |
10408 | 225 |
fix c cs assume ys': "ys' = c # cs" |
25692 | 226 |
then show ?thesis |
49086 | 227 |
by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI |
228 |
same_prefixeq_prefixeq snoc.prems ys) |
|
10389 | 229 |
qed |
10408 | 230 |
next |
49086 | 231 |
assume "prefix ys xs" then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def) |
11987 | 232 |
with snoc have False by blast |
23254 | 233 |
then show ?thesis .. |
10408 | 234 |
next |
235 |
assume "xs \<parallel> ys" |
|
11987 | 236 |
with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c" |
10408 | 237 |
and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" |
238 |
by blast |
|
239 |
from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp |
|
240 |
with neq ys show ?thesis by blast |
|
10389 | 241 |
qed |
242 |
qed |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
243 |
|
25564 | 244 |
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" |
25692 | 245 |
apply (rule parallelI) |
246 |
apply (erule parallelE, erule conjE, |
|
49086 | 247 |
induct rule: not_prefixeq_induct, simp+)+ |
25692 | 248 |
done |
25299 | 249 |
|
25692 | 250 |
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" |
251 |
by (simp add: parallel_append) |
|
25299 | 252 |
|
25692 | 253 |
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" |
254 |
unfolding parallel_def by auto |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
255 |
|
25356 | 256 |
|
49086 | 257 |
subsection {* Suffix order on lists *} |
17201 | 258 |
|
19086 | 259 |
definition |
49086 | 260 |
suffixeq :: "'a list => 'a list => bool" where |
261 |
"suffixeq xs ys = (\<exists>zs. ys = zs @ xs)" |
|
262 |
||
263 |
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
264 |
"suffix xs ys \<equiv> \<exists>us. ys = us @ xs \<and> us \<noteq> []" |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
265 |
|
49086 | 266 |
lemma suffix_imp_suffixeq: |
267 |
"suffix xs ys \<Longrightarrow> suffixeq xs ys" |
|
268 |
by (auto simp: suffixeq_def suffix_def) |
|
269 |
||
270 |
lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys" |
|
271 |
unfolding suffixeq_def by blast |
|
21305 | 272 |
|
49086 | 273 |
lemma suffixeqE [elim?]: |
274 |
assumes "suffixeq xs ys" |
|
275 |
obtains zs where "ys = zs @ xs" |
|
276 |
using assms unfolding suffixeq_def by blast |
|
21305 | 277 |
|
49086 | 278 |
lemma suffixeq_refl [iff]: "suffixeq xs xs" |
279 |
by (auto simp add: suffixeq_def) |
|
280 |
lemma suffix_trans: |
|
281 |
"suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs" |
|
282 |
by (auto simp: suffix_def) |
|
283 |
lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs" |
|
284 |
by (auto simp add: suffixeq_def) |
|
285 |
lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys" |
|
286 |
by (auto simp add: suffixeq_def) |
|
287 |
||
288 |
lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs" |
|
289 |
by (induct xs) (auto simp: suffixeq_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
290 |
|
49086 | 291 |
lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs" |
292 |
by (induct xs) (auto simp: suffix_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
293 |
|
49086 | 294 |
lemma Nil_suffixeq [iff]: "suffixeq [] xs" |
295 |
by (simp add: suffixeq_def) |
|
296 |
lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])" |
|
297 |
by (auto simp add: suffixeq_def) |
|
298 |
||
299 |
lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y#ys)" |
|
300 |
by (auto simp add: suffixeq_def) |
|
301 |
lemma suffixeq_ConsD: "suffixeq (x#xs) ys \<Longrightarrow> suffixeq xs ys" |
|
302 |
by (auto simp add: suffixeq_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
303 |
|
49086 | 304 |
lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)" |
305 |
by (auto simp add: suffixeq_def) |
|
306 |
lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys" |
|
307 |
by (auto simp add: suffixeq_def) |
|
308 |
||
309 |
lemma suffix_set_subset: |
|
310 |
"suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
311 |
|
49086 | 312 |
lemma suffixeq_set_subset: |
313 |
"suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def) |
|
314 |
||
315 |
lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys" |
|
21305 | 316 |
proof - |
49086 | 317 |
assume "suffixeq (x#xs) (y#ys)" |
318 |
then obtain zs where "y#ys = zs @ x#xs" .. |
|
319 |
then show ?thesis |
|
320 |
by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI) |
|
21305 | 321 |
qed |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
322 |
|
49086 | 323 |
lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)" |
324 |
proof |
|
325 |
assume "suffixeq xs ys" |
|
326 |
then obtain zs where "ys = zs @ xs" .. |
|
327 |
then have "rev ys = rev xs @ rev zs" by simp |
|
328 |
then show "prefixeq (rev xs) (rev ys)" .. |
|
329 |
next |
|
330 |
assume "prefixeq (rev xs) (rev ys)" |
|
331 |
then obtain zs where "rev ys = rev xs @ zs" .. |
|
332 |
then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp |
|
333 |
then have "ys = rev zs @ xs" by simp |
|
334 |
then show "suffixeq xs ys" .. |
|
21305 | 335 |
qed |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
336 |
|
49086 | 337 |
lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs" |
338 |
by (clarsimp elim!: suffixeqE) |
|
17201 | 339 |
|
49086 | 340 |
lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)" |
341 |
by (auto elim!: suffixeqE intro: suffixeqI) |
|
25299 | 342 |
|
49086 | 343 |
lemma suffixeq_drop: "suffixeq (drop n as) as" |
344 |
unfolding suffixeq_def |
|
25692 | 345 |
apply (rule exI [where x = "take n as"]) |
346 |
apply simp |
|
347 |
done |
|
25299 | 348 |
|
49086 | 349 |
lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs" |
350 |
by (clarsimp elim!: suffixeqE) |
|
25299 | 351 |
|
49086 | 352 |
lemma suffixeq_suffix_reflclp_conv: |
353 |
"suffixeq = suffix\<^sup>=\<^sup>=" |
|
354 |
proof (intro ext iffI) |
|
355 |
fix xs ys :: "'a list" |
|
356 |
assume "suffixeq xs ys" |
|
357 |
show "suffix\<^sup>=\<^sup>= xs ys" |
|
358 |
proof |
|
359 |
assume "xs \<noteq> ys" |
|
360 |
with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def) |
|
361 |
qed |
|
362 |
next |
|
363 |
fix xs ys :: "'a list" |
|
364 |
assume "suffix\<^sup>=\<^sup>= xs ys" |
|
365 |
thus "suffixeq xs ys" |
|
366 |
proof |
|
367 |
assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq) |
|
368 |
next |
|
369 |
assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def) |
|
370 |
qed |
|
371 |
qed |
|
372 |
||
373 |
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y" |
|
25692 | 374 |
by blast |
25299 | 375 |
|
49086 | 376 |
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x" |
25692 | 377 |
by blast |
25355 | 378 |
|
379 |
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" |
|
25692 | 380 |
unfolding parallel_def by simp |
25355 | 381 |
|
25299 | 382 |
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" |
25692 | 383 |
unfolding parallel_def by simp |
25299 | 384 |
|
25564 | 385 |
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" |
25692 | 386 |
by auto |
25299 | 387 |
|
25564 | 388 |
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" |
49086 | 389 |
by (metis Cons_prefixeq_Cons parallelE parallelI) |
25665 | 390 |
|
25299 | 391 |
lemma not_equal_is_parallel: |
392 |
assumes neq: "xs \<noteq> ys" |
|
25356 | 393 |
and len: "length xs = length ys" |
394 |
shows "xs \<parallel> ys" |
|
25299 | 395 |
using len neq |
25355 | 396 |
proof (induct rule: list_induct2) |
26445 | 397 |
case Nil |
25356 | 398 |
then show ?case by simp |
25299 | 399 |
next |
26445 | 400 |
case (Cons a as b bs) |
25355 | 401 |
have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact |
25299 | 402 |
show ?case |
403 |
proof (cases "a = b") |
|
25355 | 404 |
case True |
26445 | 405 |
then have "as \<noteq> bs" using Cons by simp |
25355 | 406 |
then show ?thesis by (rule Cons_parallelI2 [OF True ih]) |
25299 | 407 |
next |
408 |
case False |
|
25355 | 409 |
then show ?thesis by (rule Cons_parallelI1) |
25299 | 410 |
qed |
411 |
qed |
|
22178 | 412 |
|
49086 | 413 |
lemma suffix_reflclp_conv: |
414 |
"suffix\<^sup>=\<^sup>= = suffixeq" |
|
415 |
by (intro ext) (auto simp: suffixeq_def suffix_def) |
|
416 |
||
417 |
lemma suffix_lists: |
|
418 |
"suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A" |
|
419 |
unfolding suffix_def by auto |
|
420 |
||
421 |
||
422 |
subsection {* Embedding on lists *} |
|
423 |
||
424 |
inductive |
|
425 |
emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
|
426 |
for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)" |
|
427 |
where |
|
428 |
emb_Nil [intro, simp]: "emb P [] ys" |
|
429 |
| emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)" |
|
430 |
| emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)" |
|
431 |
||
432 |
lemma emb_Nil2 [simp]: |
|
433 |
assumes "emb P xs []" shows "xs = []" |
|
434 |
using assms by (cases rule: emb.cases) auto |
|
435 |
||
436 |
lemma emb_Cons_Nil [simp]: |
|
437 |
"emb P (x#xs) [] = False" |
|
438 |
proof - |
|
439 |
{ assume "emb P (x#xs) []" |
|
440 |
from emb_Nil2 [OF this] have False by simp |
|
441 |
} moreover { |
|
442 |
assume False |
|
443 |
hence "emb P (x#xs) []" by simp |
|
444 |
} ultimately show ?thesis by blast |
|
445 |
qed |
|
446 |
||
447 |
lemma emb_append2 [intro]: |
|
448 |
"emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)" |
|
449 |
by (induct zs) auto |
|
450 |
||
451 |
lemma emb_prefix [intro]: |
|
452 |
assumes "emb P xs ys" shows "emb P xs (ys @ zs)" |
|
453 |
using assms |
|
454 |
by (induct arbitrary: zs) auto |
|
455 |
||
456 |
lemma emb_ConsD: |
|
457 |
assumes "emb P (x#xs) ys" |
|
458 |
shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> emb P xs vs" |
|
459 |
using assms |
|
460 |
proof (induct x\<equiv>"x#xs" y\<equiv>"ys" arbitrary: x xs ys) |
|
461 |
case emb_Cons thus ?case by (metis append_Cons) |
|
462 |
next |
|
463 |
case (emb_Cons2 x y xs ys) |
|
464 |
thus ?case by (cases xs) (auto, blast+) |
|
465 |
qed |
|
466 |
||
467 |
lemma emb_appendD: |
|
468 |
assumes "emb P (xs @ ys) zs" |
|
469 |
shows "\<exists>us vs. zs = us @ vs \<and> emb P xs us \<and> emb P ys vs" |
|
470 |
using assms |
|
471 |
proof (induction xs arbitrary: ys zs) |
|
472 |
case Nil thus ?case by auto |
|
473 |
next |
|
474 |
case (Cons x xs) |
|
475 |
then obtain us v vs where "zs = us @ v # vs" |
|
476 |
and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD) |
|
477 |
with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2) |
|
478 |
qed |
|
479 |
||
480 |
lemma emb_suffix: |
|
481 |
assumes "emb P xs ys" and "suffix ys zs" |
|
482 |
shows "emb P xs zs" |
|
483 |
using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def) |
|
484 |
||
485 |
lemma emb_suffixeq: |
|
486 |
assumes "emb P xs ys" and "suffixeq ys zs" |
|
487 |
shows "emb P xs zs" |
|
488 |
using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto |
|
489 |
||
490 |
lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys" |
|
491 |
by (induct rule: emb.induct) auto |
|
492 |
||
493 |
(*FIXME: move*) |
|
494 |
definition transp_on :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where |
|
495 |
"transp_on P A \<equiv> \<forall>a\<in>A. \<forall>b\<in>A. \<forall>c\<in>A. P a b \<and> P b c \<longrightarrow> P a c" |
|
496 |
lemma transp_onI [Pure.intro]: |
|
497 |
"(\<And>a b c. \<lbrakk>a \<in> A; b \<in> A; c \<in> A; P a b; P b c\<rbrakk> \<Longrightarrow> P a c) \<Longrightarrow> transp_on P A" |
|
498 |
unfolding transp_on_def by blast |
|
499 |
||
500 |
lemma transp_on_emb: |
|
501 |
assumes "transp_on P A" |
|
502 |
shows "transp_on (emb P) (lists A)" |
|
503 |
proof |
|
504 |
fix xs ys zs |
|
505 |
assume "emb P xs ys" and "emb P ys zs" |
|
506 |
and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A" |
|
507 |
thus "emb P xs zs" |
|
508 |
proof (induction arbitrary: zs) |
|
509 |
case emb_Nil show ?case by blast |
|
510 |
next |
|
511 |
case (emb_Cons xs ys y) |
|
512 |
from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs |
|
513 |
where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast |
|
514 |
hence "emb P ys (v#vs)" by blast |
|
515 |
hence "emb P ys zs" unfolding zs by (rule emb_append2) |
|
516 |
from emb_Cons.IH [OF this] and emb_Cons.prems show ?case by simp |
|
517 |
next |
|
518 |
case (emb_Cons2 x y xs ys) |
|
519 |
from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs |
|
520 |
where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast |
|
521 |
with emb_Cons2 have "emb P xs vs" by simp |
|
522 |
moreover have "P x v" |
|
523 |
proof - |
|
524 |
from zs and `zs \<in> lists A` have "v \<in> A" by auto |
|
525 |
moreover have "x \<in> A" and "y \<in> A" using emb_Cons2 by simp_all |
|
526 |
ultimately show ?thesis using `P x y` and `P y v` and assms |
|
527 |
unfolding transp_on_def by blast |
|
528 |
qed |
|
529 |
ultimately have "emb P (x#xs) (v#vs)" by blast |
|
530 |
thus ?case unfolding zs by (rule emb_append2) |
|
531 |
qed |
|
532 |
qed |
|
533 |
||
534 |
||
535 |
subsection {* Sublists (special case of embedding) *} |
|
536 |
||
537 |
abbreviation sub :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
538 |
"sub xs ys \<equiv> emb (op =) xs ys" |
|
539 |
||
540 |
lemma sub_Cons2: "sub xs ys \<Longrightarrow> sub (x#xs) (x#ys)" by auto |
|
541 |
||
542 |
lemma sub_same_length: |
|
543 |
assumes "sub xs ys" and "length xs = length ys" shows "xs = ys" |
|
544 |
using assms by (induct) (auto dest: emb_length) |
|
545 |
||
546 |
lemma not_sub_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sub xs ys" |
|
547 |
by (metis emb_length linorder_not_less) |
|
548 |
||
549 |
lemma [code]: |
|
550 |
"emb P [] ys \<longleftrightarrow> True" |
|
551 |
"emb P (x#xs) [] \<longleftrightarrow> False" |
|
552 |
by (simp_all) |
|
553 |
||
554 |
lemma sub_Cons': "sub (x#xs) ys \<Longrightarrow> sub xs ys" |
|
555 |
by (induct xs) (auto dest: emb_ConsD) |
|
556 |
||
557 |
lemma sub_Cons2': |
|
558 |
assumes "sub (x#xs) (x#ys)" shows "sub xs ys" |
|
559 |
using assms by (cases) (rule sub_Cons') |
|
560 |
||
561 |
lemma sub_Cons2_neq: |
|
562 |
assumes "sub (x#xs) (y#ys)" |
|
563 |
shows "x \<noteq> y \<Longrightarrow> sub (x#xs) ys" |
|
564 |
using assms by (cases) auto |
|
565 |
||
566 |
lemma sub_Cons2_iff [simp, code]: |
|
567 |
"sub (x#xs) (y#ys) = (if x = y then sub xs ys else sub (x#xs) ys)" |
|
568 |
by (metis emb_Cons emb_Cons2 [of "op =", OF refl] sub_Cons2' sub_Cons2_neq) |
|
569 |
||
570 |
lemma sub_append': "sub (zs @ xs) (zs @ ys) \<longleftrightarrow> sub xs ys" |
|
571 |
by (induct zs) simp_all |
|
572 |
||
573 |
lemma sub_refl [simp, intro!]: "sub xs xs" by (induct xs) simp_all |
|
574 |
||
575 |
lemma sub_antisym: |
|
576 |
assumes "sub xs ys" and "sub ys xs" |
|
577 |
shows "xs = ys" |
|
578 |
using assms |
|
579 |
proof (induct) |
|
580 |
case emb_Nil |
|
581 |
from emb_Nil2 [OF this] show ?case by simp |
|
582 |
next |
|
583 |
case emb_Cons2 thus ?case by simp |
|
584 |
next |
|
585 |
case emb_Cons thus ?case |
|
586 |
by (metis sub_Cons' emb_length Suc_length_conv Suc_n_not_le_n) |
|
587 |
qed |
|
588 |
||
589 |
lemma transp_on_sub: "transp_on sub UNIV" |
|
590 |
proof - |
|
591 |
have "transp_on (op =) UNIV" by (simp add: transp_on_def) |
|
592 |
from transp_on_emb [OF this] show ?thesis by simp |
|
593 |
qed |
|
594 |
||
595 |
lemma sub_trans: "sub xs ys \<Longrightarrow> sub ys zs \<Longrightarrow> sub xs zs" |
|
596 |
using transp_on_sub [unfolded transp_on_def] by blast |
|
597 |
||
598 |
lemma sub_append_le_same_iff: "sub (xs @ ys) ys \<longleftrightarrow> xs = []" |
|
599 |
by (auto dest: emb_length) |
|
600 |
||
601 |
lemma emb_append_mono: |
|
602 |
"\<lbrakk> emb P xs xs'; emb P ys ys' \<rbrakk> \<Longrightarrow> emb P (xs@ys) (xs'@ys')" |
|
603 |
apply (induct rule: emb.induct) |
|
604 |
apply (metis eq_Nil_appendI emb_append2) |
|
605 |
apply (metis append_Cons emb_Cons) |
|
606 |
by (metis append_Cons emb_Cons2) |
|
607 |
||
608 |
||
609 |
subsection {* Appending elements *} |
|
610 |
||
611 |
lemma sub_append [simp]: |
|
612 |
"sub (xs @ zs) (ys @ zs) \<longleftrightarrow> sub xs ys" (is "?l = ?r") |
|
613 |
proof |
|
614 |
{ fix xs' ys' xs ys zs :: "'a list" assume "sub xs' ys'" |
|
615 |
hence "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sub xs ys" |
|
616 |
proof (induct arbitrary: xs ys zs) |
|
617 |
case emb_Nil show ?case by simp |
|
618 |
next |
|
619 |
case (emb_Cons xs' ys' x) |
|
620 |
{ assume "ys=[]" hence ?case using emb_Cons(1) by auto } |
|
621 |
moreover |
|
622 |
{ fix us assume "ys = x#us" |
|
623 |
hence ?case using emb_Cons(2) by(simp add: emb.emb_Cons) } |
|
624 |
ultimately show ?case by (auto simp:Cons_eq_append_conv) |
|
625 |
next |
|
626 |
case (emb_Cons2 x y xs' ys') |
|
627 |
{ assume "xs=[]" hence ?case using emb_Cons2(1) by auto } |
|
628 |
moreover |
|
629 |
{ fix us vs assume "xs=x#us" "ys=x#vs" hence ?case using emb_Cons2 by auto} |
|
630 |
moreover |
|
631 |
{ fix us assume "xs=x#us" "ys=[]" hence ?case using emb_Cons2(2) by bestsimp } |
|
632 |
ultimately show ?case using `x = y` by (auto simp: Cons_eq_append_conv) |
|
633 |
qed } |
|
634 |
moreover assume ?l |
|
635 |
ultimately show ?r by blast |
|
636 |
next |
|
637 |
assume ?r thus ?l by (metis emb_append_mono sub_refl) |
|
638 |
qed |
|
639 |
||
640 |
lemma sub_drop_many: "sub xs ys \<Longrightarrow> sub xs (zs @ ys)" |
|
641 |
by (induct zs) auto |
|
642 |
||
643 |
lemma sub_rev_drop_many: "sub xs ys \<Longrightarrow> sub xs (ys @ zs)" |
|
644 |
by (metis append_Nil2 emb_Nil emb_append_mono) |
|
645 |
||
646 |
||
647 |
subsection {* Relation to standard list operations *} |
|
648 |
||
649 |
lemma sub_map: |
|
650 |
assumes "sub xs ys" shows "sub (map f xs) (map f ys)" |
|
651 |
using assms by (induct) auto |
|
652 |
||
653 |
lemma sub_filter_left [simp]: "sub (filter P xs) xs" |
|
654 |
by (induct xs) auto |
|
655 |
||
656 |
lemma sub_filter [simp]: |
|
657 |
assumes "sub xs ys" shows "sub (filter P xs) (filter P ys)" |
|
658 |
using assms by (induct) auto |
|
659 |
||
660 |
lemma "sub xs ys \<longleftrightarrow> (\<exists> N. xs = sublist ys N)" (is "?L = ?R") |
|
661 |
proof |
|
662 |
assume ?L |
|
663 |
thus ?R |
|
664 |
proof (induct) |
|
665 |
case emb_Nil show ?case by (metis sublist_empty) |
|
666 |
next |
|
667 |
case (emb_Cons xs ys x) |
|
668 |
then obtain N where "xs = sublist ys N" by blast |
|
669 |
hence "xs = sublist (x#ys) (Suc ` N)" |
|
670 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
|
671 |
thus ?case by blast |
|
672 |
next |
|
673 |
case (emb_Cons2 x y xs ys) |
|
674 |
then obtain N where "xs = sublist ys N" by blast |
|
675 |
hence "x#xs = sublist (x#ys) (insert 0 (Suc ` N))" |
|
676 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
|
677 |
thus ?case unfolding `x = y` by blast |
|
678 |
qed |
|
679 |
next |
|
680 |
assume ?R |
|
681 |
then obtain N where "xs = sublist ys N" .. |
|
682 |
moreover have "sub (sublist ys N) ys" |
|
683 |
proof (induct ys arbitrary:N) |
|
684 |
case Nil show ?case by simp |
|
685 |
next |
|
686 |
case Cons thus ?case by (auto simp: sublist_Cons) |
|
687 |
qed |
|
688 |
ultimately show ?L by simp |
|
689 |
qed |
|
690 |
||
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
691 |
end |