author | Christian Sternagel |
Thu, 30 Aug 2012 15:44:03 +0900 | |
changeset 49093 | fdc301f592c4 |
parent 49087 | 7a17ba4bc997 |
child 49107 | ec34e9df0514 |
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 |
|
49087 | 6 |
header {* List prefixes, suffixes, and 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 |
|
49087 | 14 |
definition prefixeq :: "'a list => 'a list => bool" where |
15 |
"prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)" |
|
16 |
||
17 |
definition prefix :: "'a list => 'a list => bool" where |
|
18 |
"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 |
||
26 |
lemma prefixeqI [intro?]: "ys = xs @ zs ==> prefixeq xs ys" |
|
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 |
||
34 |
lemma prefixI' [intro?]: "ys = xs @ z # zs ==> prefix xs ys" |
|
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 |
|
49087 | 46 |
lemma prefixI [intro?]: "prefixeq xs ys ==> xs \<noteq> ys ==> 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 |
|
49087 | 91 |
lemma prefixeq_prefixeq [simp]: "prefixeq xs ys ==> prefixeq xs (ys @ zs)" |
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: |
109 |
"prefixeq xs ys ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys" |
|
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 |
|
49087 | 114 |
theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys" |
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) |
25692 | 152 |
case Nil then show ?thesis using pfx by simp |
25299 | 153 |
next |
154 |
case (Cons a as) |
|
25692 | 155 |
note c = `ps = a#as` |
25299 | 156 |
show ?thesis |
157 |
proof (cases ls) |
|
49087 | 158 |
case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil) |
25299 | 159 |
next |
160 |
case (Cons x xs) |
|
161 |
show ?thesis |
|
162 |
proof (cases "x = a") |
|
25355 | 163 |
case True |
49087 | 164 |
have "\<not> prefixeq as xs" using pfx c Cons True by simp |
25355 | 165 |
with c Cons True show ?thesis by (rule c2) |
166 |
next |
|
167 |
case False |
|
168 |
with c Cons show ?thesis by (rule c3) |
|
25299 | 169 |
qed |
170 |
qed |
|
171 |
qed |
|
172 |
||
49087 | 173 |
lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]: |
174 |
assumes np: "\<not> prefixeq ps ls" |
|
25356 | 175 |
and base: "\<And>x xs. P (x#xs) []" |
176 |
and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)" |
|
49087 | 177 |
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 | 178 |
shows "P ps ls" using np |
25299 | 179 |
proof (induct ls arbitrary: ps) |
25355 | 180 |
case Nil then show ?case |
49087 | 181 |
by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base) |
25299 | 182 |
next |
25355 | 183 |
case (Cons y ys) |
49087 | 184 |
then have npfx: "\<not> prefixeq ps (y # ys)" by simp |
25355 | 185 |
then obtain x xs where pv: "ps = x # xs" |
49087 | 186 |
by (rule not_prefixeq_cases) auto |
187 |
show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2) |
|
25299 | 188 |
qed |
14300 | 189 |
|
25356 | 190 |
|
10389 | 191 |
subsection {* Parallel lists *} |
192 |
||
19086 | 193 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21305
diff
changeset
|
194 |
parallel :: "'a list => 'a list => bool" (infixl "\<parallel>" 50) where |
49087 | 195 |
"(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)" |
10389 | 196 |
|
49087 | 197 |
lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> 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: |
210 |
"xs \<parallel> ys ==> \<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 |
49087 | 232 |
assume "prefix ys xs" then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def) |
11987 | 233 |
with snoc have False by blast |
23254 | 234 |
then show ?thesis .. |
10408 | 235 |
next |
236 |
assume "xs \<parallel> ys" |
|
11987 | 237 |
with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c" |
10408 | 238 |
and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" |
239 |
by blast |
|
240 |
from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp |
|
241 |
with neq ys show ?thesis by blast |
|
10389 | 242 |
qed |
243 |
qed |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
244 |
|
25564 | 245 |
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" |
25692 | 246 |
apply (rule parallelI) |
247 |
apply (erule parallelE, erule conjE, |
|
49087 | 248 |
induct rule: not_prefixeq_induct, simp+)+ |
25692 | 249 |
done |
25299 | 250 |
|
25692 | 251 |
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" |
252 |
by (simp add: parallel_append) |
|
25299 | 253 |
|
25692 | 254 |
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" |
255 |
unfolding parallel_def by auto |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
256 |
|
25356 | 257 |
|
49087 | 258 |
subsection {* Suffix order on lists *} |
17201 | 259 |
|
19086 | 260 |
definition |
49087 | 261 |
suffixeq :: "'a list => 'a list => bool" where |
262 |
"suffixeq xs ys = (\<exists>zs. ys = zs @ xs)" |
|
263 |
||
264 |
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
265 |
"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
|
266 |
|
49087 | 267 |
lemma suffix_imp_suffixeq: |
268 |
"suffix xs ys \<Longrightarrow> suffixeq xs ys" |
|
269 |
by (auto simp: suffixeq_def suffix_def) |
|
270 |
||
271 |
lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys" |
|
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 |
||
300 |
lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y#ys)" |
|
301 |
by (auto simp add: suffixeq_def) |
|
302 |
lemma suffixeq_ConsD: "suffixeq (x#xs) ys \<Longrightarrow> suffixeq xs ys" |
|
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 |
||
316 |
lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys" |
|
21305 | 317 |
proof - |
49087 | 318 |
assume "suffixeq (x#xs) (y#ys)" |
319 |
then obtain zs where "y#ys = zs @ x#xs" .. |
|
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" |
351 |
by (clarsimp elim!: suffixeqE) |
|
25299 | 352 |
|
49087 | 353 |
lemma suffixeq_suffix_reflclp_conv: |
354 |
"suffixeq = suffix\<^sup>=\<^sup>=" |
|
355 |
proof (intro ext iffI) |
|
356 |
fix xs ys :: "'a list" |
|
357 |
assume "suffixeq xs ys" |
|
358 |
show "suffix\<^sup>=\<^sup>= xs ys" |
|
359 |
proof |
|
360 |
assume "xs \<noteq> ys" |
|
361 |
with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def) |
|
362 |
qed |
|
363 |
next |
|
364 |
fix xs ys :: "'a list" |
|
365 |
assume "suffix\<^sup>=\<^sup>= xs ys" |
|
366 |
thus "suffixeq xs ys" |
|
367 |
proof |
|
368 |
assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq) |
|
369 |
next |
|
370 |
assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def) |
|
371 |
qed |
|
372 |
qed |
|
373 |
||
374 |
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y" |
|
25692 | 375 |
by blast |
25299 | 376 |
|
49087 | 377 |
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x" |
25692 | 378 |
by blast |
25355 | 379 |
|
380 |
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" |
|
25692 | 381 |
unfolding parallel_def by simp |
25355 | 382 |
|
25299 | 383 |
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" |
25692 | 384 |
unfolding parallel_def by simp |
25299 | 385 |
|
25564 | 386 |
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" |
25692 | 387 |
by auto |
25299 | 388 |
|
25564 | 389 |
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" |
49087 | 390 |
by (metis Cons_prefixeq_Cons parallelE parallelI) |
25665 | 391 |
|
25299 | 392 |
lemma not_equal_is_parallel: |
393 |
assumes neq: "xs \<noteq> ys" |
|
25356 | 394 |
and len: "length xs = length ys" |
395 |
shows "xs \<parallel> ys" |
|
25299 | 396 |
using len neq |
25355 | 397 |
proof (induct rule: list_induct2) |
26445 | 398 |
case Nil |
25356 | 399 |
then show ?case by simp |
25299 | 400 |
next |
26445 | 401 |
case (Cons a as b bs) |
25355 | 402 |
have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact |
25299 | 403 |
show ?case |
404 |
proof (cases "a = b") |
|
25355 | 405 |
case True |
26445 | 406 |
then have "as \<noteq> bs" using Cons by simp |
25355 | 407 |
then show ?thesis by (rule Cons_parallelI2 [OF True ih]) |
25299 | 408 |
next |
409 |
case False |
|
25355 | 410 |
then show ?thesis by (rule Cons_parallelI1) |
25299 | 411 |
qed |
412 |
qed |
|
22178 | 413 |
|
49087 | 414 |
lemma suffix_reflclp_conv: |
415 |
"suffix\<^sup>=\<^sup>= = suffixeq" |
|
416 |
by (intro ext) (auto simp: suffixeq_def suffix_def) |
|
417 |
||
418 |
lemma suffix_lists: |
|
419 |
"suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A" |
|
420 |
unfolding suffix_def by auto |
|
421 |
||
422 |
||
423 |
subsection {* Embedding on lists *} |
|
424 |
||
425 |
inductive |
|
426 |
emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
|
427 |
for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)" |
|
428 |
where |
|
429 |
emb_Nil [intro, simp]: "emb P [] ys" |
|
430 |
| emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)" |
|
431 |
| emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)" |
|
432 |
||
433 |
lemma emb_Nil2 [simp]: |
|
434 |
assumes "emb P xs []" shows "xs = []" |
|
435 |
using assms by (cases rule: emb.cases) auto |
|
436 |
||
437 |
lemma emb_Cons_Nil [simp]: |
|
438 |
"emb P (x#xs) [] = False" |
|
439 |
proof - |
|
440 |
{ assume "emb P (x#xs) []" |
|
441 |
from emb_Nil2 [OF this] have False by simp |
|
442 |
} moreover { |
|
443 |
assume False |
|
444 |
hence "emb P (x#xs) []" by simp |
|
445 |
} ultimately show ?thesis by blast |
|
446 |
qed |
|
447 |
||
448 |
lemma emb_append2 [intro]: |
|
449 |
"emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)" |
|
450 |
by (induct zs) auto |
|
451 |
||
452 |
lemma emb_prefix [intro]: |
|
453 |
assumes "emb P xs ys" shows "emb P xs (ys @ zs)" |
|
454 |
using assms |
|
455 |
by (induct arbitrary: zs) auto |
|
456 |
||
457 |
lemma emb_ConsD: |
|
458 |
assumes "emb P (x#xs) ys" |
|
459 |
shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> emb P xs vs" |
|
460 |
using assms |
|
461 |
proof (induct x\<equiv>"x#xs" y\<equiv>"ys" arbitrary: x xs ys) |
|
462 |
case emb_Cons thus ?case by (metis append_Cons) |
|
463 |
next |
|
464 |
case (emb_Cons2 x y xs ys) |
|
465 |
thus ?case by (cases xs) (auto, blast+) |
|
466 |
qed |
|
467 |
||
468 |
lemma emb_appendD: |
|
469 |
assumes "emb P (xs @ ys) zs" |
|
470 |
shows "\<exists>us vs. zs = us @ vs \<and> emb P xs us \<and> emb P ys vs" |
|
471 |
using assms |
|
472 |
proof (induction xs arbitrary: ys zs) |
|
473 |
case Nil thus ?case by auto |
|
474 |
next |
|
475 |
case (Cons x xs) |
|
476 |
then obtain us v vs where "zs = us @ v # vs" |
|
477 |
and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD) |
|
478 |
with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2) |
|
479 |
qed |
|
480 |
||
481 |
lemma emb_suffix: |
|
482 |
assumes "emb P xs ys" and "suffix ys zs" |
|
483 |
shows "emb P xs zs" |
|
484 |
using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def) |
|
485 |
||
486 |
lemma emb_suffixeq: |
|
487 |
assumes "emb P xs ys" and "suffixeq ys zs" |
|
488 |
shows "emb P xs zs" |
|
489 |
using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto |
|
490 |
||
491 |
lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys" |
|
492 |
by (induct rule: emb.induct) auto |
|
493 |
||
494 |
(*FIXME: move*) |
|
495 |
definition transp_on :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where |
|
496 |
"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" |
|
497 |
lemma transp_onI [Pure.intro]: |
|
498 |
"(\<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" |
|
499 |
unfolding transp_on_def by blast |
|
500 |
||
501 |
lemma transp_on_emb: |
|
502 |
assumes "transp_on P A" |
|
503 |
shows "transp_on (emb P) (lists A)" |
|
504 |
proof |
|
505 |
fix xs ys zs |
|
506 |
assume "emb P xs ys" and "emb P ys zs" |
|
507 |
and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A" |
|
508 |
thus "emb P xs zs" |
|
509 |
proof (induction arbitrary: zs) |
|
510 |
case emb_Nil show ?case by blast |
|
511 |
next |
|
512 |
case (emb_Cons xs ys y) |
|
513 |
from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs |
|
514 |
where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast |
|
515 |
hence "emb P ys (v#vs)" by blast |
|
516 |
hence "emb P ys zs" unfolding zs by (rule emb_append2) |
|
517 |
from emb_Cons.IH [OF this] and emb_Cons.prems show ?case by simp |
|
518 |
next |
|
519 |
case (emb_Cons2 x y xs ys) |
|
520 |
from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs |
|
521 |
where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast |
|
522 |
with emb_Cons2 have "emb P xs vs" by simp |
|
523 |
moreover have "P x v" |
|
524 |
proof - |
|
525 |
from zs and `zs \<in> lists A` have "v \<in> A" by auto |
|
526 |
moreover have "x \<in> A" and "y \<in> A" using emb_Cons2 by simp_all |
|
527 |
ultimately show ?thesis using `P x y` and `P y v` and assms |
|
528 |
unfolding transp_on_def by blast |
|
529 |
qed |
|
530 |
ultimately have "emb P (x#xs) (v#vs)" by blast |
|
531 |
thus ?case unfolding zs by (rule emb_append2) |
|
532 |
qed |
|
533 |
qed |
|
534 |
||
535 |
||
536 |
subsection {* Sublists (special case of embedding) *} |
|
537 |
||
538 |
abbreviation sub :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
539 |
"sub xs ys \<equiv> emb (op =) xs ys" |
|
540 |
||
541 |
lemma sub_Cons2: "sub xs ys \<Longrightarrow> sub (x#xs) (x#ys)" by auto |
|
542 |
||
543 |
lemma sub_same_length: |
|
544 |
assumes "sub xs ys" and "length xs = length ys" shows "xs = ys" |
|
545 |
using assms by (induct) (auto dest: emb_length) |
|
546 |
||
547 |
lemma not_sub_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sub xs ys" |
|
548 |
by (metis emb_length linorder_not_less) |
|
549 |
||
550 |
lemma [code]: |
|
551 |
"emb P [] ys \<longleftrightarrow> True" |
|
552 |
"emb P (x#xs) [] \<longleftrightarrow> False" |
|
553 |
by (simp_all) |
|
554 |
||
555 |
lemma sub_Cons': "sub (x#xs) ys \<Longrightarrow> sub xs ys" |
|
556 |
by (induct xs) (auto dest: emb_ConsD) |
|
557 |
||
558 |
lemma sub_Cons2': |
|
559 |
assumes "sub (x#xs) (x#ys)" shows "sub xs ys" |
|
560 |
using assms by (cases) (rule sub_Cons') |
|
561 |
||
562 |
lemma sub_Cons2_neq: |
|
563 |
assumes "sub (x#xs) (y#ys)" |
|
564 |
shows "x \<noteq> y \<Longrightarrow> sub (x#xs) ys" |
|
565 |
using assms by (cases) auto |
|
566 |
||
567 |
lemma sub_Cons2_iff [simp, code]: |
|
568 |
"sub (x#xs) (y#ys) = (if x = y then sub xs ys else sub (x#xs) ys)" |
|
569 |
by (metis emb_Cons emb_Cons2 [of "op =", OF refl] sub_Cons2' sub_Cons2_neq) |
|
570 |
||
571 |
lemma sub_append': "sub (zs @ xs) (zs @ ys) \<longleftrightarrow> sub xs ys" |
|
572 |
by (induct zs) simp_all |
|
573 |
||
574 |
lemma sub_refl [simp, intro!]: "sub xs xs" by (induct xs) simp_all |
|
575 |
||
576 |
lemma sub_antisym: |
|
577 |
assumes "sub xs ys" and "sub ys xs" |
|
578 |
shows "xs = ys" |
|
579 |
using assms |
|
580 |
proof (induct) |
|
581 |
case emb_Nil |
|
582 |
from emb_Nil2 [OF this] show ?case by simp |
|
583 |
next |
|
584 |
case emb_Cons2 thus ?case by simp |
|
585 |
next |
|
586 |
case emb_Cons thus ?case |
|
587 |
by (metis sub_Cons' emb_length Suc_length_conv Suc_n_not_le_n) |
|
588 |
qed |
|
589 |
||
590 |
lemma transp_on_sub: "transp_on sub UNIV" |
|
591 |
proof - |
|
592 |
have "transp_on (op =) UNIV" by (simp add: transp_on_def) |
|
593 |
from transp_on_emb [OF this] show ?thesis by simp |
|
594 |
qed |
|
595 |
||
596 |
lemma sub_trans: "sub xs ys \<Longrightarrow> sub ys zs \<Longrightarrow> sub xs zs" |
|
597 |
using transp_on_sub [unfolded transp_on_def] by blast |
|
598 |
||
599 |
lemma sub_append_le_same_iff: "sub (xs @ ys) ys \<longleftrightarrow> xs = []" |
|
600 |
by (auto dest: emb_length) |
|
601 |
||
602 |
lemma emb_append_mono: |
|
603 |
"\<lbrakk> emb P xs xs'; emb P ys ys' \<rbrakk> \<Longrightarrow> emb P (xs@ys) (xs'@ys')" |
|
604 |
apply (induct rule: emb.induct) |
|
605 |
apply (metis eq_Nil_appendI emb_append2) |
|
606 |
apply (metis append_Cons emb_Cons) |
|
607 |
by (metis append_Cons emb_Cons2) |
|
608 |
||
609 |
||
610 |
subsection {* Appending elements *} |
|
611 |
||
612 |
lemma sub_append [simp]: |
|
613 |
"sub (xs @ zs) (ys @ zs) \<longleftrightarrow> sub xs ys" (is "?l = ?r") |
|
614 |
proof |
|
615 |
{ fix xs' ys' xs ys zs :: "'a list" assume "sub xs' ys'" |
|
616 |
hence "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sub xs ys" |
|
617 |
proof (induct arbitrary: xs ys zs) |
|
618 |
case emb_Nil show ?case by simp |
|
619 |
next |
|
620 |
case (emb_Cons xs' ys' x) |
|
621 |
{ assume "ys=[]" hence ?case using emb_Cons(1) by auto } |
|
622 |
moreover |
|
623 |
{ fix us assume "ys = x#us" |
|
624 |
hence ?case using emb_Cons(2) by(simp add: emb.emb_Cons) } |
|
625 |
ultimately show ?case by (auto simp:Cons_eq_append_conv) |
|
626 |
next |
|
627 |
case (emb_Cons2 x y xs' ys') |
|
628 |
{ assume "xs=[]" hence ?case using emb_Cons2(1) by auto } |
|
629 |
moreover |
|
630 |
{ fix us vs assume "xs=x#us" "ys=x#vs" hence ?case using emb_Cons2 by auto} |
|
631 |
moreover |
|
632 |
{ fix us assume "xs=x#us" "ys=[]" hence ?case using emb_Cons2(2) by bestsimp } |
|
633 |
ultimately show ?case using `x = y` by (auto simp: Cons_eq_append_conv) |
|
634 |
qed } |
|
635 |
moreover assume ?l |
|
636 |
ultimately show ?r by blast |
|
637 |
next |
|
638 |
assume ?r thus ?l by (metis emb_append_mono sub_refl) |
|
639 |
qed |
|
640 |
||
641 |
lemma sub_drop_many: "sub xs ys \<Longrightarrow> sub xs (zs @ ys)" |
|
642 |
by (induct zs) auto |
|
643 |
||
644 |
lemma sub_rev_drop_many: "sub xs ys \<Longrightarrow> sub xs (ys @ zs)" |
|
645 |
by (metis append_Nil2 emb_Nil emb_append_mono) |
|
646 |
||
647 |
||
648 |
subsection {* Relation to standard list operations *} |
|
649 |
||
650 |
lemma sub_map: |
|
651 |
assumes "sub xs ys" shows "sub (map f xs) (map f ys)" |
|
652 |
using assms by (induct) auto |
|
653 |
||
654 |
lemma sub_filter_left [simp]: "sub (filter P xs) xs" |
|
655 |
by (induct xs) auto |
|
656 |
||
657 |
lemma sub_filter [simp]: |
|
658 |
assumes "sub xs ys" shows "sub (filter P xs) (filter P ys)" |
|
659 |
using assms by (induct) auto |
|
660 |
||
661 |
lemma "sub xs ys \<longleftrightarrow> (\<exists> N. xs = sublist ys N)" (is "?L = ?R") |
|
662 |
proof |
|
663 |
assume ?L |
|
664 |
thus ?R |
|
665 |
proof (induct) |
|
666 |
case emb_Nil show ?case by (metis sublist_empty) |
|
667 |
next |
|
668 |
case (emb_Cons xs ys x) |
|
669 |
then obtain N where "xs = sublist ys N" by blast |
|
670 |
hence "xs = sublist (x#ys) (Suc ` N)" |
|
671 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
|
672 |
thus ?case by blast |
|
673 |
next |
|
674 |
case (emb_Cons2 x y xs ys) |
|
675 |
then obtain N where "xs = sublist ys N" by blast |
|
676 |
hence "x#xs = sublist (x#ys) (insert 0 (Suc ` N))" |
|
677 |
by (clarsimp simp add:sublist_Cons inj_image_mem_iff) |
|
678 |
thus ?case unfolding `x = y` by blast |
|
679 |
qed |
|
680 |
next |
|
681 |
assume ?R |
|
682 |
then obtain N where "xs = sublist ys N" .. |
|
683 |
moreover have "sub (sublist ys N) ys" |
|
684 |
proof (induct ys arbitrary:N) |
|
685 |
case Nil show ?case by simp |
|
686 |
next |
|
687 |
case Cons thus ?case by (auto simp: sublist_Cons) |
|
688 |
qed |
|
689 |
ultimately show ?L by simp |
|
690 |
qed |
|
691 |
||
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
692 |
end |