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