summary |
shortlog |
changelog |
graph |
tags |
branches |
files |
changeset |
file |
revisions |
annotate |
diff |
raw

src/HOL/Library/Sublist.thy

author | nipkow |

Mon May 23 22:43:11 2016 +0200 (2016-05-23) | |

changeset 63117 | acb6d72fc42e |

parent 61076 | bdc1e2f0a86a |

child 63149 | f5dbab18c404 |

permissions | -rw-r--r-- |

renamed prefix* in Library/Sublist

1 (* Title: HOL/Library/Sublist.thy

2 Author: Tobias Nipkow and Markus Wenzel, TU Muenchen

3 Author: Christian Sternagel, JAIST

4 *)

6 section \<open>List prefixes, suffixes, and homeomorphic embedding\<close>

8 theory Sublist

9 imports Main

10 begin

12 subsection \<open>Prefix order on lists\<close>

14 definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

15 where "prefix xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"

17 definition strict_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

18 where "strict_prefix xs ys \<longleftrightarrow> prefix xs ys \<and> xs \<noteq> ys"

20 interpretation prefix_order: order prefix strict_prefix

21 by standard (auto simp: prefix_def strict_prefix_def)

23 interpretation prefix_bot: order_bot Nil prefix strict_prefix

24 by standard (simp add: prefix_def)

26 lemma prefixI [intro?]: "ys = xs @ zs \<Longrightarrow> prefix xs ys"

27 unfolding prefix_def by blast

29 lemma prefixE [elim?]:

30 assumes "prefix xs ys"

31 obtains zs where "ys = xs @ zs"

32 using assms unfolding prefix_def by blast

34 lemma strict_prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> strict_prefix xs ys"

35 unfolding strict_prefix_def prefix_def by blast

37 lemma strict_prefixE' [elim?]:

38 assumes "strict_prefix xs ys"

39 obtains z zs where "ys = xs @ z # zs"

40 proof -

41 from \<open>strict_prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys"

42 unfolding strict_prefix_def prefix_def by blast

43 with that show ?thesis by (auto simp add: neq_Nil_conv)

44 qed

46 lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys"

47 unfolding strict_prefix_def by blast

49 lemma strict_prefixE [elim?]:

50 fixes xs ys :: "'a list"

51 assumes "strict_prefix xs ys"

52 obtains "prefix xs ys" and "xs \<noteq> ys"

53 using assms unfolding strict_prefix_def by blast

56 subsection \<open>Basic properties of prefixes\<close>

58 theorem Nil_prefix [iff]: "prefix [] xs"

59 by (simp add: prefix_def)

61 theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])"

62 by (induct xs) (simp_all add: prefix_def)

64 lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys"

65 proof

66 assume "prefix xs (ys @ [y])"

67 then obtain zs where zs: "ys @ [y] = xs @ zs" ..

68 show "xs = ys @ [y] \<or> prefix xs ys"

69 by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)

70 next

71 assume "xs = ys @ [y] \<or> prefix xs ys"

72 then show "prefix xs (ys @ [y])"

73 by (metis prefix_order.eq_iff prefix_order.order_trans prefixI)

74 qed

76 lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)"

77 by (auto simp add: prefix_def)

79 lemma prefix_code [code]:

80 "prefix [] xs \<longleftrightarrow> True"

81 "prefix (x # xs) [] \<longleftrightarrow> False"

82 "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"

83 by simp_all

85 lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs"

86 by (induct xs) simp_all

88 lemma same_prefix_nil [iff]: "prefix (xs @ ys) xs = (ys = [])"

89 by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)

91 lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)"

92 by (metis prefix_order.le_less_trans prefixI strict_prefixE strict_prefixI)

94 lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs"

95 by (auto simp add: prefix_def)

97 theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))"

98 by (cases xs) (auto simp add: prefix_def)

100 theorem prefix_append:

101 "prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix us zs))"

102 apply (induct zs rule: rev_induct)

103 apply force

104 apply (simp del: append_assoc add: append_assoc [symmetric])

105 apply (metis append_eq_appendI)

106 done

108 lemma append_one_prefix:

109 "prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys"

110 proof (unfold prefix_def)

111 assume a1: "\<exists>zs. ys = xs @ zs"

112 then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce

113 assume a2: "length xs < length ys"

114 have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp

115 have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force

116 hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)

117 thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce

118 qed

120 theorem prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys"

121 by (auto simp add: prefix_def)

123 lemma prefix_same_cases:

124 "prefix (xs\<^sub>1::'a list) ys \<Longrightarrow> prefix xs\<^sub>2 ys \<Longrightarrow> prefix xs\<^sub>1 xs\<^sub>2 \<or> prefix xs\<^sub>2 xs\<^sub>1"

125 unfolding prefix_def by (force simp: append_eq_append_conv2)

127 lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys"

128 by (auto simp add: prefix_def)

130 lemma take_is_prefix: "prefix (take n xs) xs"

131 unfolding prefix_def by (metis append_take_drop_id)

133 lemma map_prefixI: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"

134 by (auto simp: prefix_def)

136 lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys"

137 by (auto simp: strict_prefix_def prefix_def)

139 lemma strict_prefix_simps [simp, code]:

140 "strict_prefix xs [] \<longleftrightarrow> False"

141 "strict_prefix [] (x # xs) \<longleftrightarrow> True"

142 "strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys"

143 by (simp_all add: strict_prefix_def cong: conj_cong)

145 lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_prefix (take n xs) ys"

146 apply (induct n arbitrary: xs ys)

147 apply (case_tac ys; simp)

148 apply (metis prefix_order.less_trans strict_prefixI take_is_prefix)

149 done

151 lemma not_prefix_cases:

152 assumes pfx: "\<not> prefix ps ls"

153 obtains

154 (c1) "ps \<noteq> []" and "ls = []"

155 | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefix as xs"

156 | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"

157 proof (cases ps)

158 case Nil

159 then show ?thesis using pfx by simp

160 next

161 case (Cons a as)

162 note c = \<open>ps = a#as\<close>

163 show ?thesis

164 proof (cases ls)

165 case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)

166 next

167 case (Cons x xs)

168 show ?thesis

169 proof (cases "x = a")

170 case True

171 have "\<not> prefix as xs" using pfx c Cons True by simp

172 with c Cons True show ?thesis by (rule c2)

173 next

174 case False

175 with c Cons show ?thesis by (rule c3)

176 qed

177 qed

178 qed

180 lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:

181 assumes np: "\<not> prefix ps ls"

182 and base: "\<And>x xs. P (x#xs) []"

183 and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"

184 and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"

185 shows "P ps ls" using np

186 proof (induct ls arbitrary: ps)

187 case Nil then show ?case

188 by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)

189 next

190 case (Cons y ys)

191 then have npfx: "\<not> prefix ps (y # ys)" by simp

192 then obtain x xs where pv: "ps = x # xs"

193 by (rule not_prefix_cases) auto

194 show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)

195 qed

198 subsection \<open>Parallel lists\<close>

200 definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "\<parallel>" 50)

201 where "(xs \<parallel> ys) = (\<not> prefix xs ys \<and> \<not> prefix ys xs)"

203 lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix ys xs \<Longrightarrow> xs \<parallel> ys"

204 unfolding parallel_def by blast

206 lemma parallelE [elim]:

207 assumes "xs \<parallel> ys"

208 obtains "\<not> prefix xs ys \<and> \<not> prefix ys xs"

209 using assms unfolding parallel_def by blast

211 theorem prefix_cases:

212 obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys"

213 unfolding parallel_def strict_prefix_def by blast

215 theorem parallel_decomp:

216 "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"

217 proof (induct xs rule: rev_induct)

218 case Nil

219 then have False by auto

220 then show ?case ..

221 next

222 case (snoc x xs)

223 show ?case

224 proof (rule prefix_cases)

225 assume le: "prefix xs ys"

226 then obtain ys' where ys: "ys = xs @ ys'" ..

227 show ?thesis

228 proof (cases ys')

229 assume "ys' = []"

230 then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)

231 next

232 fix c cs assume ys': "ys' = c # cs"

233 have "x \<noteq> c" using snoc.prems ys ys' by fastforce

234 thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs"

235 using ys ys' by blast

236 qed

237 next

238 assume "strict_prefix ys xs"

239 then have "prefix ys (xs @ [x])" by (simp add: strict_prefix_def)

240 with snoc have False by blast

241 then show ?thesis ..

242 next

243 assume "xs \<parallel> ys"

244 with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"

245 and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"

246 by blast

247 from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp

248 with neq ys show ?thesis by blast

249 qed

250 qed

252 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"

253 apply (rule parallelI)

254 apply (erule parallelE, erule conjE,

255 induct rule: not_prefix_induct, simp+)+

256 done

258 lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"

259 by (simp add: parallel_append)

261 lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"

262 unfolding parallel_def by auto

265 subsection \<open>Suffix order on lists\<close>

267 definition suffixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

268 where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"

270 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

271 where "suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"

273 lemma suffix_imp_suffixeq:

274 "suffix xs ys \<Longrightarrow> suffixeq xs ys"

275 by (auto simp: suffixeq_def suffix_def)

277 lemma suffixeqI [intro?]: "ys = zs @ xs \<Longrightarrow> suffixeq xs ys"

278 unfolding suffixeq_def by blast

280 lemma suffixeqE [elim?]:

281 assumes "suffixeq xs ys"

282 obtains zs where "ys = zs @ xs"

283 using assms unfolding suffixeq_def by blast

285 lemma suffixeq_refl [iff]: "suffixeq xs xs"

286 by (auto simp add: suffixeq_def)

287 lemma suffix_trans:

288 "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"

289 by (auto simp: suffix_def)

290 lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"

291 by (auto simp add: suffixeq_def)

292 lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"

293 by (auto simp add: suffixeq_def)

295 lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"

296 by (induct xs) (auto simp: suffixeq_def)

298 lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"

299 by (induct xs) (auto simp: suffix_def)

301 lemma Nil_suffixeq [iff]: "suffixeq [] xs"

302 by (simp add: suffixeq_def)

303 lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"

304 by (auto simp add: suffixeq_def)

306 lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y # ys)"

307 by (auto simp add: suffixeq_def)

308 lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys"

309 by (auto simp add: suffixeq_def)

311 lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"

312 by (auto simp add: suffixeq_def)

313 lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"

314 by (auto simp add: suffixeq_def)

316 lemma suffix_set_subset:

317 "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)

319 lemma suffixeq_set_subset:

320 "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)

322 lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> suffixeq xs ys"

323 proof -

324 assume "suffixeq (x # xs) (y # ys)"

325 then obtain zs where "y # ys = zs @ x # xs" ..

326 then show ?thesis

327 by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI)

328 qed

330 lemma suffixeq_to_prefix [code]: "suffixeq xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"

331 proof

332 assume "suffixeq xs ys"

333 then obtain zs where "ys = zs @ xs" ..

334 then have "rev ys = rev xs @ rev zs" by simp

335 then show "prefix (rev xs) (rev ys)" ..

336 next

337 assume "prefix (rev xs) (rev ys)"

338 then obtain zs where "rev ys = rev xs @ zs" ..

339 then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp

340 then have "ys = rev zs @ xs" by simp

341 then show "suffixeq xs ys" ..

342 qed

344 lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"

345 by (clarsimp elim!: suffixeqE)

347 lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)"

348 by (auto elim!: suffixeqE intro: suffixeqI)

350 lemma suffixeq_drop: "suffixeq (drop n as) as"

351 unfolding suffixeq_def

352 apply (rule exI [where x = "take n as"])

353 apply simp

354 done

356 lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"

357 by (auto elim!: suffixeqE)

359 lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>="

360 proof (intro ext iffI)

361 fix xs ys :: "'a list"

362 assume "suffixeq xs ys"

363 show "suffix\<^sup>=\<^sup>= xs ys"

364 proof

365 assume "xs \<noteq> ys"

366 with \<open>suffixeq xs ys\<close> show "suffix xs ys"

367 by (auto simp: suffixeq_def suffix_def)

368 qed

369 next

370 fix xs ys :: "'a list"

371 assume "suffix\<^sup>=\<^sup>= xs ys"

372 then show "suffixeq xs ys"

373 proof

374 assume "suffix xs ys" then show "suffixeq xs ys"

375 by (rule suffix_imp_suffixeq)

376 next

377 assume "xs = ys" then show "suffixeq xs ys"

378 by (auto simp: suffixeq_def)

379 qed

380 qed

382 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y"

383 by blast

385 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x"

386 by blast

388 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"

389 unfolding parallel_def by simp

391 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"

392 unfolding parallel_def by simp

394 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"

395 by auto

397 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"

398 by (metis Cons_prefix_Cons parallelE parallelI)

400 lemma not_equal_is_parallel:

401 assumes neq: "xs \<noteq> ys"

402 and len: "length xs = length ys"

403 shows "xs \<parallel> ys"

404 using len neq

405 proof (induct rule: list_induct2)

406 case Nil

407 then show ?case by simp

408 next

409 case (Cons a as b bs)

410 have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact

411 show ?case

412 proof (cases "a = b")

413 case True

414 then have "as \<noteq> bs" using Cons by simp

415 then show ?thesis by (rule Cons_parallelI2 [OF True ih])

416 next

417 case False

418 then show ?thesis by (rule Cons_parallelI1)

419 qed

420 qed

422 lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq"

423 by (intro ext) (auto simp: suffixeq_def suffix_def)

425 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"

426 unfolding suffix_def by auto

429 subsection \<open>Homeomorphic embedding on lists\<close>

431 inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"

432 for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"

433 where

434 list_emb_Nil [intro, simp]: "list_emb P [] ys"

435 | list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"

436 | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"

438 lemma list_emb_mono:

439 assumes "\<And>x y. P x y \<longrightarrow> Q x y"

440 shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"

441 proof

442 assume "list_emb P xs ys"

443 then show "list_emb Q xs ys" by (induct) (auto simp: assms)

444 qed

446 lemma list_emb_Nil2 [simp]:

447 assumes "list_emb P xs []" shows "xs = []"

448 using assms by (cases rule: list_emb.cases) auto

450 lemma list_emb_refl:

451 assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"

452 shows "list_emb P xs xs"

453 using assms by (induct xs) auto

455 lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"

456 proof -

457 { assume "list_emb P (x#xs) []"

458 from list_emb_Nil2 [OF this] have False by simp

459 } moreover {

460 assume False

461 then have "list_emb P (x#xs) []" by simp

462 } ultimately show ?thesis by blast

463 qed

465 lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"

466 by (induct zs) auto

468 lemma list_emb_prefix [intro]:

469 assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"

470 using assms

471 by (induct arbitrary: zs) auto

473 lemma list_emb_ConsD:

474 assumes "list_emb P (x#xs) ys"

475 shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"

476 using assms

477 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)

478 case list_emb_Cons

479 then show ?case by (metis append_Cons)

480 next

481 case (list_emb_Cons2 x y xs ys)

482 then show ?case by blast

483 qed

485 lemma list_emb_appendD:

486 assumes "list_emb P (xs @ ys) zs"

487 shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"

488 using assms

489 proof (induction xs arbitrary: ys zs)

490 case Nil then show ?case by auto

491 next

492 case (Cons x xs)

493 then obtain us v vs where

494 zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"

495 by (auto dest: list_emb_ConsD)

496 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

497 sk: "\<forall>x\<^sub>0 x\<^sub>1. \<not> list_emb 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_emb P xs (sk\<^sub>0 x\<^sub>0 x\<^sub>1) \<and> list_emb P x\<^sub>0 (sk\<^sub>1 x\<^sub>0 x\<^sub>1)"

498 using Cons(1) by (metis (no_types))

499 hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto

500 thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)

501 qed

503 lemma list_emb_suffix:

504 assumes "list_emb P xs ys" and "suffix ys zs"

505 shows "list_emb P xs zs"

506 using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: suffix_def)

508 lemma list_emb_suffixeq:

509 assumes "list_emb P xs ys" and "suffixeq ys zs"

510 shows "list_emb P xs zs"

511 using assms and list_emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto

513 lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"

514 by (induct rule: list_emb.induct) auto

516 lemma list_emb_trans:

517 assumes "\<And>x y z. \<lbrakk>x \<in> set xs; y \<in> set ys; z \<in> set zs; P x y; P y z\<rbrakk> \<Longrightarrow> P x z"

518 shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"

519 proof -

520 assume "list_emb P xs ys" and "list_emb P ys zs"

521 then show "list_emb P xs zs" using assms

522 proof (induction arbitrary: zs)

523 case list_emb_Nil show ?case by blast

524 next

525 case (list_emb_Cons xs ys y)

526 from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs

527 where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast

528 then have "list_emb P ys (v#vs)" by blast

529 then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)

530 from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto

531 next

532 case (list_emb_Cons2 x y xs ys)

533 from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs

534 where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast

535 with list_emb_Cons2 have "list_emb P xs vs" by auto

536 moreover have "P x v"

537 proof -

538 from zs have "v \<in> set zs" by auto

539 moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all

540 ultimately show ?thesis

541 using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2

542 by blast

543 qed

544 ultimately have "list_emb P (x#xs) (v#vs)" by blast

545 then show ?case unfolding zs by (rule list_emb_append2)

546 qed

547 qed

549 lemma list_emb_set:

550 assumes "list_emb P xs ys" and "x \<in> set xs"

551 obtains y where "y \<in> set ys" and "P x y"

552 using assms by (induct) auto

555 subsection \<open>Sublists (special case of homeomorphic embedding)\<close>

557 abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

558 where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"

560 lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto

562 lemma sublisteq_same_length:

563 assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"

564 using assms by (induct) (auto dest: list_emb_length)

566 lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"

567 by (metis list_emb_length linorder_not_less)

569 lemma [code]:

570 "list_emb P [] ys \<longleftrightarrow> True"

571 "list_emb P (x#xs) [] \<longleftrightarrow> False"

572 by (simp_all)

574 lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"

575 by (induct xs, simp, blast dest: list_emb_ConsD)

577 lemma sublisteq_Cons2':

578 assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"

579 using assms by (cases) (rule sublisteq_Cons')

581 lemma sublisteq_Cons2_neq:

582 assumes "sublisteq (x#xs) (y#ys)"

583 shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"

584 using assms by (cases) auto

586 lemma sublisteq_Cons2_iff [simp, code]:

587 "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"

588 by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)

590 lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"

591 by (induct zs) simp_all

593 lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all

595 lemma sublisteq_antisym:

596 assumes "sublisteq xs ys" and "sublisteq ys xs"

597 shows "xs = ys"

598 using assms

599 proof (induct)

600 case list_emb_Nil

601 from list_emb_Nil2 [OF this] show ?case by simp

602 next

603 case list_emb_Cons2

604 thus ?case by simp

605 next

606 case list_emb_Cons

607 hence False using sublisteq_Cons' by fastforce

608 thus ?case ..

609 qed

611 lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"

612 by (rule list_emb_trans [of _ _ _ "op ="]) auto

614 lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"

615 by (auto dest: list_emb_length)

617 lemma list_emb_append_mono:

618 "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"

619 apply (induct rule: list_emb.induct)

620 apply (metis eq_Nil_appendI list_emb_append2)

621 apply (metis append_Cons list_emb_Cons)

622 apply (metis append_Cons list_emb_Cons2)

623 done

626 subsection \<open>Appending elements\<close>

628 lemma sublisteq_append [simp]:

629 "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")

630 proof

631 { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"

632 then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"

633 proof (induct arbitrary: xs ys zs)

634 case list_emb_Nil show ?case by simp

635 next

636 case (list_emb_Cons xs' ys' x)

637 { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }

638 moreover

639 { fix us assume "ys = x#us"

640 then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }

641 ultimately show ?case by (auto simp:Cons_eq_append_conv)

642 next

643 case (list_emb_Cons2 x y xs' ys')

644 { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }

645 moreover

646 { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}

647 moreover

648 { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }

649 ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)

650 qed }

651 moreover assume ?l

652 ultimately show ?r by blast

653 next

654 assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl)

655 qed

657 lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"

658 by (induct zs) auto

660 lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"

661 by (metis append_Nil2 list_emb_Nil list_emb_append_mono)

664 subsection \<open>Relation to standard list operations\<close>

666 lemma sublisteq_map:

667 assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"

668 using assms by (induct) auto

670 lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"

671 by (induct xs) auto

673 lemma sublisteq_filter [simp]:

674 assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"

675 using assms by induct auto

677 lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")

678 proof

679 assume ?L

680 then show ?R

681 proof (induct)

682 case list_emb_Nil show ?case by (metis sublist_empty)

683 next

684 case (list_emb_Cons xs ys x)

685 then obtain N where "xs = sublist ys N" by blast

686 then have "xs = sublist (x#ys) (Suc ` N)"

687 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

688 then show ?case by blast

689 next

690 case (list_emb_Cons2 x y xs ys)

691 then obtain N where "xs = sublist ys N" by blast

692 then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"

693 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

694 moreover from list_emb_Cons2 have "x = y" by simp

695 ultimately show ?case by blast

696 qed

697 next

698 assume ?R

699 then obtain N where "xs = sublist ys N" ..

700 moreover have "sublisteq (sublist ys N) ys"

701 proof (induct ys arbitrary: N)

702 case Nil show ?case by simp

703 next

704 case Cons then show ?case by (auto simp: sublist_Cons)

705 qed

706 ultimately show ?L by simp

707 qed

709 end