author | blanchet |
Wed, 20 Nov 2013 18:58:00 +0100 | |
changeset 54538 | ba7392b52a7c |
parent 54483 | 9f24325c2550 |
child 55579 | 207538943038 |
permissions | -rw-r--r-- |
49087 | 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 |
49087 | 3 |
Author: Christian Sternagel, JAIST |
10330
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 |
|
54538
ba7392b52a7c
factor 'List_Prefix' out of 'Sublist' and move to 'Main' (needed for codatatypes)
blanchet
parents:
54483
diff
changeset
|
6 |
header {* Parallel lists, list suffixes, and homeomorphic embedding *} |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
7 |
|
49087 | 8 |
theory Sublist |
9 |
imports Main |
|
15131 | 10 |
begin |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
11 |
|
10389 | 12 |
subsection {* Parallel lists *} |
13 |
||
50516 | 14 |
definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "\<parallel>" 50) |
49107 | 15 |
where "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)" |
10389 | 16 |
|
50516 | 17 |
lemma parallelI [intro]: "\<not> prefixeq xs ys \<Longrightarrow> \<not> prefixeq ys xs \<Longrightarrow> xs \<parallel> ys" |
25692 | 18 |
unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
19 |
|
10389 | 20 |
lemma parallelE [elim]: |
25692 | 21 |
assumes "xs \<parallel> ys" |
49087 | 22 |
obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs" |
25692 | 23 |
using assms unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
24 |
|
49087 | 25 |
theorem prefixeq_cases: |
26 |
obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys" |
|
27 |
unfolding parallel_def prefix_def by blast |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
28 |
|
10389 | 29 |
theorem parallel_decomp: |
50516 | 30 |
"xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs" |
10408 | 31 |
proof (induct xs rule: rev_induct) |
11987 | 32 |
case Nil |
23254 | 33 |
then have False by auto |
34 |
then show ?case .. |
|
10408 | 35 |
next |
11987 | 36 |
case (snoc x xs) |
37 |
show ?case |
|
49087 | 38 |
proof (rule prefixeq_cases) |
39 |
assume le: "prefixeq xs ys" |
|
10408 | 40 |
then obtain ys' where ys: "ys = xs @ ys'" .. |
41 |
show ?thesis |
|
42 |
proof (cases ys') |
|
25564 | 43 |
assume "ys' = []" |
49087 | 44 |
then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys) |
10389 | 45 |
next |
10408 | 46 |
fix c cs assume ys': "ys' = c # cs" |
54483 | 47 |
have "x \<noteq> c" using snoc.prems ys ys' by fastforce |
48 |
thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs" |
|
49 |
using ys ys' by blast |
|
10389 | 50 |
qed |
10408 | 51 |
next |
49107 | 52 |
assume "prefix ys xs" |
53 |
then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def) |
|
11987 | 54 |
with snoc have False by blast |
23254 | 55 |
then show ?thesis .. |
10408 | 56 |
next |
57 |
assume "xs \<parallel> ys" |
|
11987 | 58 |
with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c" |
10408 | 59 |
and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" |
60 |
by blast |
|
61 |
from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp |
|
62 |
with neq ys show ?thesis by blast |
|
10389 | 63 |
qed |
64 |
qed |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
65 |
|
25564 | 66 |
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" |
25692 | 67 |
apply (rule parallelI) |
68 |
apply (erule parallelE, erule conjE, |
|
49087 | 69 |
induct rule: not_prefixeq_induct, simp+)+ |
25692 | 70 |
done |
25299 | 71 |
|
25692 | 72 |
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" |
73 |
by (simp add: parallel_append) |
|
25299 | 74 |
|
25692 | 75 |
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" |
76 |
unfolding parallel_def by auto |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
77 |
|
25356 | 78 |
|
49087 | 79 |
subsection {* Suffix order on lists *} |
17201 | 80 |
|
49107 | 81 |
definition suffixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
82 |
where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)" |
|
49087 | 83 |
|
49107 | 84 |
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
85 |
where "suffix xs ys \<longleftrightarrow> (\<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
|
86 |
|
49087 | 87 |
lemma suffix_imp_suffixeq: |
88 |
"suffix xs ys \<Longrightarrow> suffixeq xs ys" |
|
89 |
by (auto simp: suffixeq_def suffix_def) |
|
90 |
||
50516 | 91 |
lemma suffixeqI [intro?]: "ys = zs @ xs \<Longrightarrow> suffixeq xs ys" |
49087 | 92 |
unfolding suffixeq_def by blast |
21305 | 93 |
|
49087 | 94 |
lemma suffixeqE [elim?]: |
95 |
assumes "suffixeq xs ys" |
|
96 |
obtains zs where "ys = zs @ xs" |
|
97 |
using assms unfolding suffixeq_def by blast |
|
21305 | 98 |
|
49087 | 99 |
lemma suffixeq_refl [iff]: "suffixeq xs xs" |
100 |
by (auto simp add: suffixeq_def) |
|
101 |
lemma suffix_trans: |
|
102 |
"suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs" |
|
103 |
by (auto simp: suffix_def) |
|
104 |
lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs" |
|
105 |
by (auto simp add: suffixeq_def) |
|
106 |
lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys" |
|
107 |
by (auto simp add: suffixeq_def) |
|
108 |
||
109 |
lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs" |
|
110 |
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
|
111 |
|
49087 | 112 |
lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs" |
113 |
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
|
114 |
|
49087 | 115 |
lemma Nil_suffixeq [iff]: "suffixeq [] xs" |
116 |
by (simp add: suffixeq_def) |
|
117 |
lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])" |
|
118 |
by (auto simp add: suffixeq_def) |
|
119 |
||
49107 | 120 |
lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y # ys)" |
49087 | 121 |
by (auto simp add: suffixeq_def) |
49107 | 122 |
lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys" |
49087 | 123 |
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
|
124 |
|
49087 | 125 |
lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)" |
126 |
by (auto simp add: suffixeq_def) |
|
127 |
lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys" |
|
128 |
by (auto simp add: suffixeq_def) |
|
129 |
||
130 |
lemma suffix_set_subset: |
|
131 |
"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
|
132 |
|
49087 | 133 |
lemma suffixeq_set_subset: |
134 |
"suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def) |
|
135 |
||
49107 | 136 |
lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> suffixeq xs ys" |
21305 | 137 |
proof - |
49107 | 138 |
assume "suffixeq (x # xs) (y # ys)" |
139 |
then obtain zs where "y # ys = zs @ x # xs" .. |
|
49087 | 140 |
then show ?thesis |
141 |
by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI) |
|
21305 | 142 |
qed |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
143 |
|
49087 | 144 |
lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)" |
145 |
proof |
|
146 |
assume "suffixeq xs ys" |
|
147 |
then obtain zs where "ys = zs @ xs" .. |
|
148 |
then have "rev ys = rev xs @ rev zs" by simp |
|
149 |
then show "prefixeq (rev xs) (rev ys)" .. |
|
150 |
next |
|
151 |
assume "prefixeq (rev xs) (rev ys)" |
|
152 |
then obtain zs where "rev ys = rev xs @ zs" .. |
|
153 |
then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp |
|
154 |
then have "ys = rev zs @ xs" by simp |
|
155 |
then show "suffixeq xs ys" .. |
|
21305 | 156 |
qed |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
157 |
|
49087 | 158 |
lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs" |
159 |
by (clarsimp elim!: suffixeqE) |
|
17201 | 160 |
|
49087 | 161 |
lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)" |
162 |
by (auto elim!: suffixeqE intro: suffixeqI) |
|
25299 | 163 |
|
49087 | 164 |
lemma suffixeq_drop: "suffixeq (drop n as) as" |
165 |
unfolding suffixeq_def |
|
25692 | 166 |
apply (rule exI [where x = "take n as"]) |
167 |
apply simp |
|
168 |
done |
|
25299 | 169 |
|
49087 | 170 |
lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs" |
49107 | 171 |
by (auto elim!: suffixeqE) |
25299 | 172 |
|
49107 | 173 |
lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>=" |
49087 | 174 |
proof (intro ext iffI) |
175 |
fix xs ys :: "'a list" |
|
176 |
assume "suffixeq xs ys" |
|
177 |
show "suffix\<^sup>=\<^sup>= xs ys" |
|
178 |
proof |
|
179 |
assume "xs \<noteq> ys" |
|
49107 | 180 |
with `suffixeq xs ys` show "suffix xs ys" |
181 |
by (auto simp: suffixeq_def suffix_def) |
|
49087 | 182 |
qed |
183 |
next |
|
184 |
fix xs ys :: "'a list" |
|
185 |
assume "suffix\<^sup>=\<^sup>= xs ys" |
|
49107 | 186 |
then show "suffixeq xs ys" |
49087 | 187 |
proof |
49107 | 188 |
assume "suffix xs ys" then show "suffixeq xs ys" |
189 |
by (rule suffix_imp_suffixeq) |
|
49087 | 190 |
next |
49107 | 191 |
assume "xs = ys" then show "suffixeq xs ys" |
192 |
by (auto simp: suffixeq_def) |
|
49087 | 193 |
qed |
194 |
qed |
|
195 |
||
196 |
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y" |
|
25692 | 197 |
by blast |
25299 | 198 |
|
49087 | 199 |
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x" |
25692 | 200 |
by blast |
25355 | 201 |
|
202 |
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" |
|
25692 | 203 |
unfolding parallel_def by simp |
25355 | 204 |
|
25299 | 205 |
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" |
25692 | 206 |
unfolding parallel_def by simp |
25299 | 207 |
|
25564 | 208 |
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" |
25692 | 209 |
by auto |
25299 | 210 |
|
25564 | 211 |
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" |
49087 | 212 |
by (metis Cons_prefixeq_Cons parallelE parallelI) |
25665 | 213 |
|
25299 | 214 |
lemma not_equal_is_parallel: |
215 |
assumes neq: "xs \<noteq> ys" |
|
25356 | 216 |
and len: "length xs = length ys" |
217 |
shows "xs \<parallel> ys" |
|
25299 | 218 |
using len neq |
25355 | 219 |
proof (induct rule: list_induct2) |
26445 | 220 |
case Nil |
25356 | 221 |
then show ?case by simp |
25299 | 222 |
next |
26445 | 223 |
case (Cons a as b bs) |
25355 | 224 |
have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact |
25299 | 225 |
show ?case |
226 |
proof (cases "a = b") |
|
25355 | 227 |
case True |
26445 | 228 |
then have "as \<noteq> bs" using Cons by simp |
25355 | 229 |
then show ?thesis by (rule Cons_parallelI2 [OF True ih]) |
25299 | 230 |
next |
231 |
case False |
|
25355 | 232 |
then show ?thesis by (rule Cons_parallelI1) |
25299 | 233 |
qed |
234 |
qed |
|
22178 | 235 |
|
49107 | 236 |
lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq" |
49087 | 237 |
by (intro ext) (auto simp: suffixeq_def suffix_def) |
238 |
||
49107 | 239 |
lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A" |
49087 | 240 |
unfolding suffix_def by auto |
241 |
||
242 |
||
50516 | 243 |
subsection {* Homeomorphic embedding on lists *} |
49087 | 244 |
|
50516 | 245 |
inductive list_hembeq :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
49087 | 246 |
for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)" |
247 |
where |
|
50516 | 248 |
list_hembeq_Nil [intro, simp]: "list_hembeq P [] ys" |
249 |
| list_hembeq_Cons [intro] : "list_hembeq P xs ys \<Longrightarrow> list_hembeq P xs (y#ys)" |
|
250 |
| list_hembeq_Cons2 [intro]: "P\<^sup>=\<^sup>= x y \<Longrightarrow> list_hembeq P xs ys \<Longrightarrow> list_hembeq P (x#xs) (y#ys)" |
|
251 |
||
252 |
lemma list_hembeq_Nil2 [simp]: |
|
253 |
assumes "list_hembeq P xs []" shows "xs = []" |
|
254 |
using assms by (cases rule: list_hembeq.cases) auto |
|
49087 | 255 |
|
50516 | 256 |
lemma list_hembeq_refl [simp, intro!]: |
257 |
"list_hembeq P xs xs" |
|
258 |
by (induct xs) auto |
|
49087 | 259 |
|
50516 | 260 |
lemma list_hembeq_Cons_Nil [simp]: "list_hembeq P (x#xs) [] = False" |
49087 | 261 |
proof - |
50516 | 262 |
{ assume "list_hembeq P (x#xs) []" |
263 |
from list_hembeq_Nil2 [OF this] have False by simp |
|
49087 | 264 |
} moreover { |
265 |
assume False |
|
50516 | 266 |
then have "list_hembeq P (x#xs) []" by simp |
49087 | 267 |
} ultimately show ?thesis by blast |
268 |
qed |
|
269 |
||
50516 | 270 |
lemma list_hembeq_append2 [intro]: "list_hembeq P xs ys \<Longrightarrow> list_hembeq P xs (zs @ ys)" |
49087 | 271 |
by (induct zs) auto |
272 |
||
50516 | 273 |
lemma list_hembeq_prefix [intro]: |
274 |
assumes "list_hembeq P xs ys" shows "list_hembeq P xs (ys @ zs)" |
|
49087 | 275 |
using assms |
276 |
by (induct arbitrary: zs) auto |
|
277 |
||
50516 | 278 |
lemma list_hembeq_ConsD: |
279 |
assumes "list_hembeq P (x#xs) ys" |
|
280 |
shows "\<exists>us v vs. ys = us @ v # vs \<and> P\<^sup>=\<^sup>= x v \<and> list_hembeq P xs vs" |
|
49087 | 281 |
using assms |
49107 | 282 |
proof (induct x \<equiv> "x # xs" ys arbitrary: x xs) |
50516 | 283 |
case list_hembeq_Cons |
49107 | 284 |
then show ?case by (metis append_Cons) |
49087 | 285 |
next |
50516 | 286 |
case (list_hembeq_Cons2 x y xs ys) |
54483 | 287 |
then show ?case by blast |
49087 | 288 |
qed |
289 |
||
50516 | 290 |
lemma list_hembeq_appendD: |
291 |
assumes "list_hembeq P (xs @ ys) zs" |
|
292 |
shows "\<exists>us vs. zs = us @ vs \<and> list_hembeq P xs us \<and> list_hembeq P ys vs" |
|
49087 | 293 |
using assms |
294 |
proof (induction xs arbitrary: ys zs) |
|
49107 | 295 |
case Nil then show ?case by auto |
49087 | 296 |
next |
297 |
case (Cons x xs) |
|
54483 | 298 |
then obtain us v vs where |
299 |
zs: "zs = us @ v # vs" and p: "P\<^sup>=\<^sup>= x v" and lh: "list_hembeq P (xs @ ys) vs" |
|
300 |
by (auto dest: list_hembeq_ConsD) |
|
301 |
obtain sk\<^sub>0 :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" and sk\<^sub>1 :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
302 |
sk: "\<forall>x\<^sub>0 x\<^sub>1. \<not> list_hembeq P (xs @ x\<^sub>0) x\<^sub>1 \<or> sk\<^sub>0 x\<^sub>0 x\<^sub>1 @ sk\<^sub>1 x\<^sub>0 x\<^sub>1 = x\<^sub>1 \<and> list_hembeq P xs (sk\<^sub>0 x\<^sub>0 x\<^sub>1) \<and> list_hembeq P x\<^sub>0 (sk\<^sub>1 x\<^sub>0 x\<^sub>1)" |
|
303 |
using Cons(1) by (metis (no_types)) |
|
304 |
hence "\<forall>x\<^sub>2. list_hembeq P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto |
|
305 |
thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc) |
|
49087 | 306 |
qed |
307 |
||
50516 | 308 |
lemma list_hembeq_suffix: |
309 |
assumes "list_hembeq P xs ys" and "suffix ys zs" |
|
310 |
shows "list_hembeq P xs zs" |
|
311 |
using assms(2) and list_hembeq_append2 [OF assms(1)] by (auto simp: suffix_def) |
|
49087 | 312 |
|
50516 | 313 |
lemma list_hembeq_suffixeq: |
314 |
assumes "list_hembeq P xs ys" and "suffixeq ys zs" |
|
315 |
shows "list_hembeq P xs zs" |
|
316 |
using assms and list_hembeq_suffix unfolding suffixeq_suffix_reflclp_conv by auto |
|
49087 | 317 |
|
50516 | 318 |
lemma list_hembeq_length: "list_hembeq P xs ys \<Longrightarrow> length xs \<le> length ys" |
319 |
by (induct rule: list_hembeq.induct) auto |
|
49087 | 320 |
|
50516 | 321 |
lemma list_hembeq_trans: |
322 |
assumes "\<And>x y z. \<lbrakk>x \<in> A; y \<in> A; z \<in> A; P x y; P y z\<rbrakk> \<Longrightarrow> P x z" |
|
323 |
shows "\<And>xs ys zs. \<lbrakk>xs \<in> lists A; ys \<in> lists A; zs \<in> lists A; |
|
324 |
list_hembeq P xs ys; list_hembeq P ys zs\<rbrakk> \<Longrightarrow> list_hembeq P xs zs" |
|
325 |
proof - |
|
49087 | 326 |
fix xs ys zs |
50516 | 327 |
assume "list_hembeq P xs ys" and "list_hembeq P ys zs" |
49087 | 328 |
and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A" |
50516 | 329 |
then show "list_hembeq P xs zs" |
49087 | 330 |
proof (induction arbitrary: zs) |
50516 | 331 |
case list_hembeq_Nil show ?case by blast |
49087 | 332 |
next |
50516 | 333 |
case (list_hembeq_Cons xs ys y) |
334 |
from list_hembeq_ConsD [OF `list_hembeq P (y#ys) zs`] obtain us v vs |
|
335 |
where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_hembeq P ys vs" by blast |
|
336 |
then have "list_hembeq P ys (v#vs)" by blast |
|
337 |
then have "list_hembeq P ys zs" unfolding zs by (rule list_hembeq_append2) |
|
338 |
from list_hembeq_Cons.IH [OF this] and list_hembeq_Cons.prems show ?case by simp |
|
49087 | 339 |
next |
50516 | 340 |
case (list_hembeq_Cons2 x y xs ys) |
341 |
from list_hembeq_ConsD [OF `list_hembeq P (y#ys) zs`] obtain us v vs |
|
342 |
where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_hembeq P ys vs" by blast |
|
343 |
with list_hembeq_Cons2 have "list_hembeq P xs vs" by simp |
|
344 |
moreover have "P\<^sup>=\<^sup>= x v" |
|
49087 | 345 |
proof - |
346 |
from zs and `zs \<in> lists A` have "v \<in> A" by auto |
|
50516 | 347 |
moreover have "x \<in> A" and "y \<in> A" using list_hembeq_Cons2 by simp_all |
348 |
ultimately show ?thesis |
|
349 |
using `P\<^sup>=\<^sup>= x y` and `P\<^sup>=\<^sup>= y v` and assms |
|
350 |
by blast |
|
49087 | 351 |
qed |
50516 | 352 |
ultimately have "list_hembeq P (x#xs) (v#vs)" by blast |
353 |
then show ?case unfolding zs by (rule list_hembeq_append2) |
|
49087 | 354 |
qed |
355 |
qed |
|
356 |
||
357 |
||
50516 | 358 |
subsection {* Sublists (special case of homeomorphic embedding) *} |
49087 | 359 |
|
50516 | 360 |
abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
361 |
where "sublisteq xs ys \<equiv> list_hembeq (op =) xs ys" |
|
49087 | 362 |
|
50516 | 363 |
lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto |
49087 | 364 |
|
50516 | 365 |
lemma sublisteq_same_length: |
366 |
assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys" |
|
367 |
using assms by (induct) (auto dest: list_hembeq_length) |
|
49087 | 368 |
|
50516 | 369 |
lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys" |
370 |
by (metis list_hembeq_length linorder_not_less) |
|
49087 | 371 |
|
372 |
lemma [code]: |
|
50516 | 373 |
"list_hembeq P [] ys \<longleftrightarrow> True" |
374 |
"list_hembeq P (x#xs) [] \<longleftrightarrow> False" |
|
49087 | 375 |
by (simp_all) |
376 |
||
50516 | 377 |
lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys" |
54483 | 378 |
by (induct xs, simp, blast dest: list_hembeq_ConsD) |
49087 | 379 |
|
50516 | 380 |
lemma sublisteq_Cons2': |
381 |
assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys" |
|
382 |
using assms by (cases) (rule sublisteq_Cons') |
|
49087 | 383 |
|
50516 | 384 |
lemma sublisteq_Cons2_neq: |
385 |
assumes "sublisteq (x#xs) (y#ys)" |
|
386 |
shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys" |
|
49087 | 387 |
using assms by (cases) auto |
388 |
||
50516 | 389 |
lemma sublisteq_Cons2_iff [simp, code]: |
390 |
"sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)" |
|
391 |
by (metis list_hembeq_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq) |
|
49087 | 392 |
|
50516 | 393 |
lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys" |
49087 | 394 |
by (induct zs) simp_all |
395 |
||
50516 | 396 |
lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all |
49087 | 397 |
|
50516 | 398 |
lemma sublisteq_antisym: |
399 |
assumes "sublisteq xs ys" and "sublisteq ys xs" |
|
49087 | 400 |
shows "xs = ys" |
401 |
using assms |
|
402 |
proof (induct) |
|
50516 | 403 |
case list_hembeq_Nil |
404 |
from list_hembeq_Nil2 [OF this] show ?case by simp |
|
49087 | 405 |
next |
50516 | 406 |
case list_hembeq_Cons2 |
54483 | 407 |
thus ?case by simp |
49087 | 408 |
next |
50516 | 409 |
case list_hembeq_Cons |
54483 | 410 |
hence False using sublisteq_Cons' by fastforce |
411 |
thus ?case .. |
|
49087 | 412 |
qed |
413 |
||
50516 | 414 |
lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs" |
415 |
by (rule list_hembeq_trans [of UNIV "op ="]) auto |
|
49087 | 416 |
|
50516 | 417 |
lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []" |
418 |
by (auto dest: list_hembeq_length) |
|
49087 | 419 |
|
50516 | 420 |
lemma list_hembeq_append_mono: |
421 |
"\<lbrakk> list_hembeq P xs xs'; list_hembeq P ys ys' \<rbrakk> \<Longrightarrow> list_hembeq P (xs@ys) (xs'@ys')" |
|
422 |
apply (induct rule: list_hembeq.induct) |
|
423 |
apply (metis eq_Nil_appendI list_hembeq_append2) |
|
424 |
apply (metis append_Cons list_hembeq_Cons) |
|
425 |
apply (metis append_Cons list_hembeq_Cons2) |
|
49107 | 426 |
done |
49087 | 427 |
|
428 |
||
429 |
subsection {* Appending elements *} |
|
430 |
||
50516 | 431 |
lemma sublisteq_append [simp]: |
432 |
"sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r") |
|
49087 | 433 |
proof |
50516 | 434 |
{ fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'" |
435 |
then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys" |
|
49087 | 436 |
proof (induct arbitrary: xs ys zs) |
50516 | 437 |
case list_hembeq_Nil show ?case by simp |
49087 | 438 |
next |
50516 | 439 |
case (list_hembeq_Cons xs' ys' x) |
440 |
{ assume "ys=[]" then have ?case using list_hembeq_Cons(1) by auto } |
|
49087 | 441 |
moreover |
442 |
{ fix us assume "ys = x#us" |
|
50516 | 443 |
then have ?case using list_hembeq_Cons(2) by(simp add: list_hembeq.list_hembeq_Cons) } |
49087 | 444 |
ultimately show ?case by (auto simp:Cons_eq_append_conv) |
445 |
next |
|
50516 | 446 |
case (list_hembeq_Cons2 x y xs' ys') |
447 |
{ assume "xs=[]" then have ?case using list_hembeq_Cons2(1) by auto } |
|
49087 | 448 |
moreover |
50516 | 449 |
{ fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_hembeq_Cons2 by auto} |
49087 | 450 |
moreover |
50516 | 451 |
{ fix us assume "xs=x#us" "ys=[]" then have ?case using list_hembeq_Cons2(2) by bestsimp } |
452 |
ultimately show ?case using `op =\<^sup>=\<^sup>= x y` by (auto simp: Cons_eq_append_conv) |
|
49087 | 453 |
qed } |
454 |
moreover assume ?l |
|
455 |
ultimately show ?r by blast |
|
456 |
next |
|
50516 | 457 |
assume ?r then show ?l by (metis list_hembeq_append_mono sublisteq_refl) |
49087 | 458 |
qed |
459 |
||
50516 | 460 |
lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)" |
49087 | 461 |
by (induct zs) auto |
462 |
||
50516 | 463 |
lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)" |
464 |
by (metis append_Nil2 list_hembeq_Nil list_hembeq_append_mono) |
|
49087 | 465 |
|
466 |
||
467 |
subsection {* Relation to standard list operations *} |
|
468 |
||
50516 | 469 |
lemma sublisteq_map: |
470 |
assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)" |
|
49087 | 471 |
using assms by (induct) auto |
472 |
||
50516 | 473 |
lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs" |
49087 | 474 |
by (induct xs) auto |
475 |
||
50516 | 476 |
lemma sublisteq_filter [simp]: |
477 |
assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)" |
|
54483 | 478 |
using assms by induct auto |
49087 | 479 |
|
50516 | 480 |
lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R") |
49087 | 481 |
proof |
482 |
assume ?L |
|
49107 | 483 |
then show ?R |
49087 | 484 |
proof (induct) |
50516 | 485 |
case list_hembeq_Nil show ?case by (metis sublist_empty) |
49087 | 486 |
next |
50516 | 487 |
case (list_hembeq_Cons xs ys x) |
49087 | 488 |
then obtain N where "xs = sublist ys N" by blast |
49107 | 489 |
then have "xs = sublist (x#ys) (Suc ` N)" |
49087 | 490 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
49107 | 491 |
then show ?case by blast |
49087 | 492 |
next |
50516 | 493 |
case (list_hembeq_Cons2 x y xs ys) |
49087 | 494 |
then obtain N where "xs = sublist ys N" by blast |
49107 | 495 |
then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))" |
49087 | 496 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
50516 | 497 |
moreover from list_hembeq_Cons2 have "x = y" by simp |
498 |
ultimately show ?case by blast |
|
49087 | 499 |
qed |
500 |
next |
|
501 |
assume ?R |
|
502 |
then obtain N where "xs = sublist ys N" .. |
|
50516 | 503 |
moreover have "sublisteq (sublist ys N) ys" |
49107 | 504 |
proof (induct ys arbitrary: N) |
49087 | 505 |
case Nil show ?case by simp |
506 |
next |
|
49107 | 507 |
case Cons then show ?case by (auto simp: sublist_Cons) |
49087 | 508 |
qed |
509 |
ultimately show ?L by simp |
|
510 |
qed |
|
511 |
||
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
512 |
end |