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

src/HOL/Library/Sublist.thy

author | nipkow |

Thu May 26 09:05:00 2016 +0200 (2016-05-26) | |

changeset 63155 | ea8540c71581 |

parent 63149 | f5dbab18c404 |

child 63173 | 3413b1cf30cd |

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

added function "prefixes" and some lemmas

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 (* FIXME rm *)

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

48 by(fact prefix_order.le_neq_trans)

50 lemma strict_prefixE [elim?]:

51 fixes xs ys :: "'a list"

52 assumes "strict_prefix xs ys"

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

54 using assms unfolding strict_prefix_def by blast

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

59 (* FIXME rm *)

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

61 by(fact prefix_bot.bot_least)

63 (* FIXME rm *)

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

65 by(fact prefix_bot.bot_unique)

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

68 proof

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

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

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

72 by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)

73 next

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

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

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

77 qed

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

80 by (auto simp add: prefix_def)

82 lemma prefix_code [code]:

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

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

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

86 by simp_all

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

89 by (induct xs) simp_all

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

92 by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)

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

95 by (metis prefix_order.le_less_trans prefixI strict_prefixE strict_prefixI)

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

98 by (auto simp add: prefix_def)

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

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

103 theorem prefix_append:

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

105 apply (induct zs rule: rev_induct)

106 apply force

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

108 apply (metis append_eq_appendI)

109 done

111 lemma append_one_prefix:

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

113 proof (unfold prefix_def)

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

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

116 assume a2: "length xs < length ys"

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

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

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

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

121 qed

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

124 by (auto simp add: prefix_def)

126 lemma prefix_same_cases:

127 "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"

128 unfolding prefix_def by (force simp: append_eq_append_conv2)

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

131 by (auto simp add: prefix_def)

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

134 unfolding prefix_def by (metis append_take_drop_id)

136 lemma prefixeq_butlast: "prefix (butlast xs) xs"

137 by (simp add: butlast_conv_take take_is_prefix)

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

140 by (auto simp: prefix_def)

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

143 by (auto simp: strict_prefix_def prefix_def)

145 lemma prefix_snocD: "prefix (xs@[x]) ys \<Longrightarrow> strict_prefix xs ys"

146 by (simp add: strict_prefixI' prefix_order.dual_order.strict_trans1)

148 lemma strict_prefix_simps [simp, code]:

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

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

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

152 by (simp_all add: strict_prefix_def cong: conj_cong)

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

155 apply (induct n arbitrary: xs ys)

156 apply (case_tac ys; simp)

157 apply (metis prefix_order.less_trans strict_prefixI take_is_prefix)

158 done

160 lemma not_prefix_cases:

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

162 obtains

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

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

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

166 proof (cases ps)

167 case Nil

168 then show ?thesis using pfx by simp

169 next

170 case (Cons a as)

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

172 show ?thesis

173 proof (cases ls)

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

175 next

176 case (Cons x xs)

177 show ?thesis

178 proof (cases "x = a")

179 case True

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

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

182 next

183 case False

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

185 qed

186 qed

187 qed

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

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

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

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

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

194 shows "P ps ls" using np

195 proof (induct ls arbitrary: ps)

196 case Nil then show ?case

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

198 next

199 case (Cons y ys)

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

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

202 by (rule not_prefix_cases) auto

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

204 qed

207 subsection \<open>Prefixes\<close>

209 fun prefixes where

210 "prefixes [] = [[]]" |

211 "prefixes (x#xs) = [] # map (op # x) (prefixes xs)"

213 lemma in_set_prefixes[simp]: "xs \<in> set (prefixes ys) \<longleftrightarrow> prefix xs ys"

214 by (induction "xs" arbitrary: "ys"; rename_tac "ys", case_tac "ys"; auto)

216 lemma length_prefixes[simp]: "length (prefixes xs) = length xs+1"

217 by (induction xs) auto

219 lemma prefixes_snoc[simp]:

220 "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]"

221 by (induction xs) auto

223 lemma prefixes_eq_Snoc:

224 "prefixes ys = xs @ [x] \<longleftrightarrow>

225 (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = zs@[z] \<and> xs = prefixes zs)) \<and> x = ys"

226 by (cases ys rule: rev_cases) auto

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

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

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

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

235 unfolding parallel_def by blast

237 lemma parallelE [elim]:

238 assumes "xs \<parallel> ys"

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

240 using assms unfolding parallel_def by blast

242 theorem prefix_cases:

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

244 unfolding parallel_def strict_prefix_def by blast

246 theorem parallel_decomp:

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

248 proof (induct xs rule: rev_induct)

249 case Nil

250 then have False by auto

251 then show ?case ..

252 next

253 case (snoc x xs)

254 show ?case

255 proof (rule prefix_cases)

256 assume le: "prefix xs ys"

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

258 show ?thesis

259 proof (cases ys')

260 assume "ys' = []"

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

262 next

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

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

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

266 using ys ys' by blast

267 qed

268 next

269 assume "strict_prefix ys xs"

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

271 with snoc have False by blast

272 then show ?thesis ..

273 next

274 assume "xs \<parallel> ys"

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

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

277 by blast

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

279 with neq ys show ?thesis by blast

280 qed

281 qed

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

284 apply (rule parallelI)

285 apply (erule parallelE, erule conjE,

286 induct rule: not_prefix_induct, simp+)+

287 done

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

290 by (simp add: parallel_append)

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

293 unfolding parallel_def by auto

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

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

299 where "suffix xs ys = (\<exists>zs. ys = zs @ xs)"

301 definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

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

304 lemma strict_suffix_imp_suffix:

305 "strict_suffix xs ys \<Longrightarrow> suffix xs ys"

306 by (auto simp: suffix_def strict_suffix_def)

308 lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys"

309 unfolding suffix_def by blast

311 lemma suffixE [elim?]:

312 assumes "suffix xs ys"

313 obtains zs where "ys = zs @ xs"

314 using assms unfolding suffix_def by blast

316 lemma suffix_refl [iff]: "suffix xs xs"

317 by (auto simp add: suffix_def)

319 lemma suffix_trans:

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

321 by (auto simp: suffix_def)

323 lemma strict_suffix_trans:

324 "\<lbrakk>strict_suffix xs ys; strict_suffix ys zs\<rbrakk> \<Longrightarrow> strict_suffix xs zs"

325 by (auto simp add: strict_suffix_def)

327 lemma suffix_antisym: "\<lbrakk>suffix xs ys; suffix ys xs\<rbrakk> \<Longrightarrow> xs = ys"

328 by (auto simp add: suffix_def)

330 lemma suffix_tl [simp]: "suffix (tl xs) xs"

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

333 lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs"

334 by (induct xs) (auto simp: strict_suffix_def)

336 lemma Nil_suffix [iff]: "suffix [] xs"

337 by (simp add: suffix_def)

339 lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])"

340 by (auto simp add: suffix_def)

342 lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)"

343 by (auto simp add: suffix_def)

345 lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys"

346 by (auto simp add: suffix_def)

348 lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)"

349 by (auto simp add: suffix_def)

351 lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys"

352 by (auto simp add: suffix_def)

354 lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"

355 by (auto simp: strict_suffix_def)

357 lemma suffix_set_subset: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"

358 by (auto simp: suffix_def)

360 lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys"

361 proof -

362 assume "suffix (x # xs) (y # ys)"

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

364 then show ?thesis

365 by (induct zs) (auto intro!: suffix_appendI suffix_ConsI)

366 qed

368 lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"

369 proof

370 assume "suffix xs ys"

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

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

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

374 next

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

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

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

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

379 then show "suffix xs ys" ..

380 qed

382 lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs"

383 by (clarsimp elim!: suffixE)

385 lemma suffix_map: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"

386 by (auto elim!: suffixE intro: suffixI)

388 lemma suffix_drop: "suffix (drop n as) as"

389 unfolding suffix_def

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

391 apply simp

392 done

394 lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"

395 by (auto elim!: suffixE)

397 lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix"

398 by (intro ext) (auto simp: suffix_def strict_suffix_def)

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

401 unfolding suffix_def by auto

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

404 by blast

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

407 by blast

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

410 unfolding parallel_def by simp

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

413 unfolding parallel_def by simp

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

416 by auto

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

419 by (metis Cons_prefix_Cons parallelE parallelI)

421 lemma not_equal_is_parallel:

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

423 and len: "length xs = length ys"

424 shows "xs \<parallel> ys"

425 using len neq

426 proof (induct rule: list_induct2)

427 case Nil

428 then show ?case by simp

429 next

430 case (Cons a as b bs)

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

432 show ?case

433 proof (cases "a = b")

434 case True

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

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

437 next

438 case False

439 then show ?thesis by (rule Cons_parallelI1)

440 qed

441 qed

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

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

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

448 where

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

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

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

453 lemma list_emb_mono:

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

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

456 proof

457 assume "list_emb P xs ys"

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

459 qed

461 lemma list_emb_Nil2 [simp]:

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

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

465 lemma list_emb_refl:

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

467 shows "list_emb P xs xs"

468 using assms by (induct xs) auto

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

471 proof -

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

473 from list_emb_Nil2 [OF this] have False by simp

474 } moreover {

475 assume False

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

477 } ultimately show ?thesis by blast

478 qed

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

481 by (induct zs) auto

483 lemma list_emb_prefix [intro]:

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

485 using assms

486 by (induct arbitrary: zs) auto

488 lemma list_emb_ConsD:

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

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

491 using assms

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

493 case list_emb_Cons

494 then show ?case by (metis append_Cons)

495 next

496 case (list_emb_Cons2 x y xs ys)

497 then show ?case by blast

498 qed

500 lemma list_emb_appendD:

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

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

503 using assms

504 proof (induction xs arbitrary: ys zs)

505 case Nil then show ?case by auto

506 next

507 case (Cons x xs)

508 then obtain us v vs where

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

510 by (auto dest: list_emb_ConsD)

511 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

512 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)"

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

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

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

516 qed

518 lemma list_emb_strict_suffix:

519 assumes "list_emb P xs ys" and "strict_suffix ys zs"

520 shows "list_emb P xs zs"

521 using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def)

523 lemma list_emb_suffix:

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

525 shows "list_emb P xs zs"

526 using assms and list_emb_strict_suffix

527 unfolding strict_suffix_reflclp_conv[symmetric] by auto

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

530 by (induct rule: list_emb.induct) auto

532 lemma list_emb_trans:

533 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"

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

535 proof -

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

537 then show "list_emb P xs zs" using assms

538 proof (induction arbitrary: zs)

539 case list_emb_Nil show ?case by blast

540 next

541 case (list_emb_Cons xs ys y)

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

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

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

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

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

547 next

548 case (list_emb_Cons2 x y xs ys)

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

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

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

552 moreover have "P x v"

553 proof -

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

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

556 ultimately show ?thesis

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

558 by blast

559 qed

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

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

562 qed

563 qed

565 lemma list_emb_set:

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

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

568 using assms by (induct) auto

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

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

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

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

578 lemma sublisteq_same_length:

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

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

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

583 by (metis list_emb_length linorder_not_less)

585 lemma [code]:

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

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

588 by (simp_all)

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

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

593 lemma sublisteq_Cons2':

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

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

597 lemma sublisteq_Cons2_neq:

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

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

600 using assms by (cases) auto

602 lemma sublisteq_Cons2_iff [simp, code]:

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

604 by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)

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

607 by (induct zs) simp_all

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

611 lemma sublisteq_antisym:

612 assumes "sublisteq xs ys" and "sublisteq ys xs"

613 shows "xs = ys"

614 using assms

615 proof (induct)

616 case list_emb_Nil

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

618 next

619 case list_emb_Cons2

620 thus ?case by simp

621 next

622 case list_emb_Cons

623 hence False using sublisteq_Cons' by fastforce

624 thus ?case ..

625 qed

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

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

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

631 by (auto dest: list_emb_length)

633 lemma list_emb_append_mono:

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

635 apply (induct rule: list_emb.induct)

636 apply (metis eq_Nil_appendI list_emb_append2)

637 apply (metis append_Cons list_emb_Cons)

638 apply (metis append_Cons list_emb_Cons2)

639 done

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

644 lemma sublisteq_append [simp]:

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

646 proof

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

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

649 proof (induct arbitrary: xs ys zs)

650 case list_emb_Nil show ?case by simp

651 next

652 case (list_emb_Cons xs' ys' x)

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

654 moreover

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

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

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

658 next

659 case (list_emb_Cons2 x y xs' ys')

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

661 moreover

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

663 moreover

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

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

666 qed }

667 moreover assume ?l

668 ultimately show ?r by blast

669 next

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

671 qed

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

674 by (induct zs) auto

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

677 by (metis append_Nil2 list_emb_Nil list_emb_append_mono)

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

682 lemma sublisteq_map:

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

684 using assms by (induct) auto

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

687 by (induct xs) auto

689 lemma sublisteq_filter [simp]:

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

691 using assms by induct auto

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

694 proof

695 assume ?L

696 then show ?R

697 proof (induct)

698 case list_emb_Nil show ?case by (metis sublist_empty)

699 next

700 case (list_emb_Cons xs ys x)

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

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

703 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

704 then show ?case by blast

705 next

706 case (list_emb_Cons2 x y xs ys)

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

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

709 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

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

711 ultimately show ?case by blast

712 qed

713 next

714 assume ?R

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

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

717 proof (induct ys arbitrary: N)

718 case Nil show ?case by simp

719 next

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

721 qed

722 ultimately show ?L by simp

723 qed

725 end