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

src/HOL/Library/Sublist.thy

author | Christian Sternagel |

Wed Aug 29 16:25:35 2012 +0900 (2012-08-29) | |

changeset 49085 | 4eef5c2ff5ad |

parent 45236 | src/HOL/Library/List_Prefix.thy@ac4a2a66707d |

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

introduced "sub" as abbreviation for "emb (op =)";

Sublist_Order is now based on Sublist.sub;

simplified and moved most lemmas on sub from Sublist_Order to Sublist;

Sublist_Order merely contains ord and order instances for sub plus some lemmas on the strict part of the order

Sublist_Order is now based on Sublist.sub;

simplified and moved most lemmas on sub from Sublist_Order to Sublist;

Sublist_Order merely contains ord and order instances for sub plus some lemmas on the strict part of the order

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

2 Author: Tobias Nipkow and Markus Wenzel, TU Muenchen

3 *)

5 header {* List prefixes, suffixes, and embedding*}

7 theory Sublist

8 imports List Main

9 begin

11 subsection {* Prefix order on lists *}

13 definition prefixeq :: "'a list => 'a list => bool" where

14 "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"

16 definition prefix :: "'a list => 'a list => bool" where

17 "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys"

19 interpretation prefix_order: order prefixeq prefix

20 by default (auto simp: prefixeq_def prefix_def)

22 interpretation prefix_bot: bot prefixeq prefix Nil

23 by default (simp add: prefixeq_def)

25 lemma prefixeqI [intro?]: "ys = xs @ zs ==> prefixeq xs ys"

26 unfolding prefixeq_def by blast

28 lemma prefixeqE [elim?]:

29 assumes "prefixeq xs ys"

30 obtains zs where "ys = xs @ zs"

31 using assms unfolding prefixeq_def by blast

33 lemma prefixI' [intro?]: "ys = xs @ z # zs ==> prefix xs ys"

34 unfolding prefix_def prefixeq_def by blast

36 lemma prefixE' [elim?]:

37 assumes "prefix xs ys"

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

39 proof -

40 from `prefix xs ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"

41 unfolding prefix_def prefixeq_def by blast

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

43 qed

45 lemma prefixI [intro?]: "prefixeq xs ys ==> xs \<noteq> ys ==> prefix xs ys"

46 unfolding prefix_def by blast

48 lemma prefixE [elim?]:

49 fixes xs ys :: "'a list"

50 assumes "prefix xs ys"

51 obtains "prefixeq xs ys" and "xs \<noteq> ys"

52 using assms unfolding prefix_def by blast

55 subsection {* Basic properties of prefixes *}

57 theorem Nil_prefixeq [iff]: "prefixeq [] xs"

58 by (simp add: prefixeq_def)

60 theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])"

61 by (induct xs) (simp_all add: prefixeq_def)

63 lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys"

64 proof

65 assume "prefixeq xs (ys @ [y])"

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

67 show "xs = ys @ [y] \<or> prefixeq xs ys"

68 by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)

69 next

70 assume "xs = ys @ [y] \<or> prefixeq xs ys"

71 then show "prefixeq xs (ys @ [y])"

72 by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI)

73 qed

75 lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)"

76 by (auto simp add: prefixeq_def)

78 lemma prefixeq_code [code]:

79 "prefixeq [] xs \<longleftrightarrow> True"

80 "prefixeq (x # xs) [] \<longleftrightarrow> False"

81 "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys"

82 by simp_all

84 lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs"

85 by (induct xs) simp_all

87 lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])"

88 by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI)

90 lemma prefixeq_prefixeq [simp]: "prefixeq xs ys ==> prefixeq xs (ys @ zs)"

91 by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI)

93 lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs"

94 by (auto simp add: prefixeq_def)

96 theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))"

97 by (cases xs) (auto simp add: prefixeq_def)

99 theorem prefixeq_append:

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

101 apply (induct zs rule: rev_induct)

102 apply force

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

104 apply (metis append_eq_appendI)

105 done

107 lemma append_one_prefixeq:

108 "prefixeq xs ys ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys"

109 unfolding prefixeq_def

110 by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj

111 eq_Nil_appendI nth_drop')

113 theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys"

114 by (auto simp add: prefixeq_def)

116 lemma prefixeq_same_cases:

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

118 unfolding prefixeq_def by (metis append_eq_append_conv2)

120 lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys"

121 by (auto simp add: prefixeq_def)

123 lemma take_is_prefixeq: "prefixeq (take n xs) xs"

124 unfolding prefixeq_def by (metis append_take_drop_id)

126 lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)"

127 by (auto simp: prefixeq_def)

129 lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys"

130 by (auto simp: prefix_def prefixeq_def)

132 lemma prefix_simps [simp, code]:

133 "prefix xs [] \<longleftrightarrow> False"

134 "prefix [] (x # xs) \<longleftrightarrow> True"

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

136 by (simp_all add: prefix_def cong: conj_cong)

138 lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys"

139 apply (induct n arbitrary: xs ys)

140 apply (case_tac ys, simp_all)[1]

141 apply (metis prefix_order.less_trans prefixI take_is_prefixeq)

142 done

144 lemma not_prefixeq_cases:

145 assumes pfx: "\<not> prefixeq ps ls"

146 obtains

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

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

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

150 proof (cases ps)

151 case Nil then show ?thesis using pfx by simp

152 next

153 case (Cons a as)

154 note c = `ps = a#as`

155 show ?thesis

156 proof (cases ls)

157 case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil)

158 next

159 case (Cons x xs)

160 show ?thesis

161 proof (cases "x = a")

162 case True

163 have "\<not> prefixeq as xs" using pfx c Cons True by simp

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

165 next

166 case False

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

168 qed

169 qed

170 qed

172 lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]:

173 assumes np: "\<not> prefixeq ps ls"

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

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

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

177 shows "P ps ls" using np

178 proof (induct ls arbitrary: ps)

179 case Nil then show ?case

180 by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base)

181 next

182 case (Cons y ys)

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

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

185 by (rule not_prefixeq_cases) auto

186 show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2)

187 qed

190 subsection {* Parallel lists *}

192 definition

193 parallel :: "'a list => 'a list => bool" (infixl "\<parallel>" 50) where

194 "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"

196 lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> xs \<parallel> ys"

197 unfolding parallel_def by blast

199 lemma parallelE [elim]:

200 assumes "xs \<parallel> ys"

201 obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs"

202 using assms unfolding parallel_def by blast

204 theorem prefixeq_cases:

205 obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys"

206 unfolding parallel_def prefix_def by blast

208 theorem parallel_decomp:

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

210 proof (induct xs rule: rev_induct)

211 case Nil

212 then have False by auto

213 then show ?case ..

214 next

215 case (snoc x xs)

216 show ?case

217 proof (rule prefixeq_cases)

218 assume le: "prefixeq xs ys"

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

220 show ?thesis

221 proof (cases ys')

222 assume "ys' = []"

223 then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys)

224 next

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

226 then show ?thesis

227 by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI

228 same_prefixeq_prefixeq snoc.prems ys)

229 qed

230 next

231 assume "prefix ys xs" then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def)

232 with snoc have False by blast

233 then show ?thesis ..

234 next

235 assume "xs \<parallel> ys"

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

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

238 by blast

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

240 with neq ys show ?thesis by blast

241 qed

242 qed

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

245 apply (rule parallelI)

246 apply (erule parallelE, erule conjE,

247 induct rule: not_prefixeq_induct, simp+)+

248 done

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

251 by (simp add: parallel_append)

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

254 unfolding parallel_def by auto

257 subsection {* Suffix order on lists *}

259 definition

260 suffixeq :: "'a list => 'a list => bool" where

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

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

264 "suffix xs ys \<equiv> \<exists>us. ys = us @ xs \<and> us \<noteq> []"

266 lemma suffix_imp_suffixeq:

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

268 by (auto simp: suffixeq_def suffix_def)

270 lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"

271 unfolding suffixeq_def by blast

273 lemma suffixeqE [elim?]:

274 assumes "suffixeq xs ys"

275 obtains zs where "ys = zs @ xs"

276 using assms unfolding suffixeq_def by blast

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

279 by (auto simp add: suffixeq_def)

280 lemma suffix_trans:

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

282 by (auto simp: suffix_def)

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

284 by (auto simp add: suffixeq_def)

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

286 by (auto simp add: suffixeq_def)

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

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

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

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

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

295 by (simp add: suffixeq_def)

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

297 by (auto simp add: suffixeq_def)

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

300 by (auto simp add: suffixeq_def)

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

302 by (auto simp add: suffixeq_def)

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

305 by (auto simp add: suffixeq_def)

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

307 by (auto simp add: suffixeq_def)

309 lemma suffix_set_subset:

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

312 lemma suffixeq_set_subset:

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

315 lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys"

316 proof -

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

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

319 then show ?thesis

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

321 qed

323 lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)"

324 proof

325 assume "suffixeq xs ys"

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

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

328 then show "prefixeq (rev xs) (rev ys)" ..

329 next

330 assume "prefixeq (rev xs) (rev ys)"

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

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

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

334 then show "suffixeq xs ys" ..

335 qed

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

338 by (clarsimp elim!: suffixeqE)

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

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

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

344 unfolding suffixeq_def

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

346 apply simp

347 done

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

350 by (clarsimp elim!: suffixeqE)

352 lemma suffixeq_suffix_reflclp_conv:

353 "suffixeq = suffix\<^sup>=\<^sup>="

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"

360 with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def)

361 qed

362 next

363 fix xs ys :: "'a list"

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

365 thus "suffixeq xs ys"

366 proof

367 assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq)

368 next

369 assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def)

370 qed

371 qed

373 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y"

374 by blast

376 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x"

377 by blast

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

380 unfolding parallel_def by simp

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

383 unfolding parallel_def by simp

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

386 by auto

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

389 by (metis Cons_prefixeq_Cons parallelE parallelI)

391 lemma not_equal_is_parallel:

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

393 and len: "length xs = length ys"

394 shows "xs \<parallel> ys"

395 using len neq

396 proof (induct rule: list_induct2)

397 case Nil

398 then show ?case by simp

399 next

400 case (Cons a as b bs)

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

402 show ?case

403 proof (cases "a = b")

404 case True

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

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

407 next

408 case False

409 then show ?thesis by (rule Cons_parallelI1)

410 qed

411 qed

413 lemma suffix_reflclp_conv:

414 "suffix\<^sup>=\<^sup>= = suffixeq"

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

417 lemma suffix_lists:

418 "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"

419 unfolding suffix_def by auto

422 subsection {* Embedding on lists *}

424 inductive

425 emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"

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

427 where

428 emb_Nil [intro, simp]: "emb P [] ys"

429 | emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)"

430 | emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)"

432 lemma emb_Nil2 [simp]:

433 assumes "emb P xs []" shows "xs = []"

434 using assms by (cases rule: emb.cases) auto

436 lemma emb_Cons_Nil [simp]:

437 "emb P (x#xs) [] = False"

438 proof -

439 { assume "emb P (x#xs) []"

440 from emb_Nil2 [OF this] have False by simp

441 } moreover {

442 assume False

443 hence "emb P (x#xs) []" by simp

444 } ultimately show ?thesis by blast

445 qed

447 lemma emb_append2 [intro]:

448 "emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)"

449 by (induct zs) auto

451 lemma emb_prefix [intro]:

452 assumes "emb P xs ys" shows "emb P xs (ys @ zs)"

453 using assms

454 by (induct arbitrary: zs) auto

456 lemma emb_ConsD:

457 assumes "emb P (x#xs) ys"

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

459 using assms

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

461 case emb_Cons thus ?case by (metis append_Cons)

462 next

463 case (emb_Cons2 x y xs ys)

464 thus ?case by (cases xs) (auto, blast+)

465 qed

467 lemma emb_appendD:

468 assumes "emb P (xs @ ys) zs"

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

470 using assms

471 proof (induction xs arbitrary: ys zs)

472 case Nil thus ?case by auto

473 next

474 case (Cons x xs)

475 then obtain us v vs where "zs = us @ v # vs"

476 and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)

477 with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)

478 qed

480 lemma emb_suffix:

481 assumes "emb P xs ys" and "suffix ys zs"

482 shows "emb P xs zs"

483 using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def)

485 lemma emb_suffixeq:

486 assumes "emb P xs ys" and "suffixeq ys zs"

487 shows "emb P xs zs"

488 using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto

490 lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys"

491 by (induct rule: emb.induct) auto

493 (*FIXME: move*)

494 definition transp_on :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where

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

496 lemma transp_onI [Pure.intro]:

497 "(\<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"

498 unfolding transp_on_def by blast

500 lemma transp_on_emb:

501 assumes "transp_on P A"

502 shows "transp_on (emb P) (lists A)"

503 proof

504 fix xs ys zs

505 assume "emb P xs ys" and "emb P ys zs"

506 and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A"

507 thus "emb P xs zs"

508 proof (induction arbitrary: zs)

509 case emb_Nil show ?case by blast

510 next

511 case (emb_Cons xs ys y)

512 from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs

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

514 hence "emb P ys (v#vs)" by blast

515 hence "emb P ys zs" unfolding zs by (rule emb_append2)

516 from emb_Cons.IH [OF this] and emb_Cons.prems show ?case by simp

517 next

518 case (emb_Cons2 x y xs ys)

519 from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs

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

521 with emb_Cons2 have "emb P xs vs" by simp

522 moreover have "P x v"

523 proof -

524 from zs and `zs \<in> lists A` have "v \<in> A" by auto

525 moreover have "x \<in> A" and "y \<in> A" using emb_Cons2 by simp_all

526 ultimately show ?thesis using `P x y` and `P y v` and assms

527 unfolding transp_on_def by blast

528 qed

529 ultimately have "emb P (x#xs) (v#vs)" by blast

530 thus ?case unfolding zs by (rule emb_append2)

531 qed

532 qed

535 subsection {* Sublists (special case of embedding) *}

537 abbreviation sub :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where

538 "sub xs ys \<equiv> emb (op =) xs ys"

540 lemma sub_Cons2: "sub xs ys \<Longrightarrow> sub (x#xs) (x#ys)" by auto

542 lemma sub_same_length:

543 assumes "sub xs ys" and "length xs = length ys" shows "xs = ys"

544 using assms by (induct) (auto dest: emb_length)

546 lemma not_sub_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sub xs ys"

547 by (metis emb_length linorder_not_less)

549 lemma [code]:

550 "emb P [] ys \<longleftrightarrow> True"

551 "emb P (x#xs) [] \<longleftrightarrow> False"

552 by (simp_all)

554 lemma sub_Cons': "sub (x#xs) ys \<Longrightarrow> sub xs ys"

555 by (induct xs) (auto dest: emb_ConsD)

557 lemma sub_Cons2':

558 assumes "sub (x#xs) (x#ys)" shows "sub xs ys"

559 using assms by (cases) (rule sub_Cons')

561 lemma sub_Cons2_neq:

562 assumes "sub (x#xs) (y#ys)"

563 shows "x \<noteq> y \<Longrightarrow> sub (x#xs) ys"

564 using assms by (cases) auto

566 lemma sub_Cons2_iff [simp, code]:

567 "sub (x#xs) (y#ys) = (if x = y then sub xs ys else sub (x#xs) ys)"

568 by (metis emb_Cons emb_Cons2 [of "op =", OF refl] sub_Cons2' sub_Cons2_neq)

570 lemma sub_append': "sub (zs @ xs) (zs @ ys) \<longleftrightarrow> sub xs ys"

571 by (induct zs) simp_all

573 lemma sub_refl [simp, intro!]: "sub xs xs" by (induct xs) simp_all

575 lemma sub_antisym:

576 assumes "sub xs ys" and "sub ys xs"

577 shows "xs = ys"

578 using assms

579 proof (induct)

580 case emb_Nil

581 from emb_Nil2 [OF this] show ?case by simp

582 next

583 case emb_Cons2 thus ?case by simp

584 next

585 case emb_Cons thus ?case

586 by (metis sub_Cons' emb_length Suc_length_conv Suc_n_not_le_n)

587 qed

589 lemma transp_on_sub: "transp_on sub UNIV"

590 proof -

591 have "transp_on (op =) UNIV" by (simp add: transp_on_def)

592 from transp_on_emb [OF this] show ?thesis by simp

593 qed

595 lemma sub_trans: "sub xs ys \<Longrightarrow> sub ys zs \<Longrightarrow> sub xs zs"

596 using transp_on_sub [unfolded transp_on_def] by blast

598 lemma sub_append_le_same_iff: "sub (xs @ ys) ys \<longleftrightarrow> xs = []"

599 by (auto dest: emb_length)

601 lemma emb_append_mono:

602 "\<lbrakk> emb P xs xs'; emb P ys ys' \<rbrakk> \<Longrightarrow> emb P (xs@ys) (xs'@ys')"

603 apply (induct rule: emb.induct)

604 apply (metis eq_Nil_appendI emb_append2)

605 apply (metis append_Cons emb_Cons)

606 by (metis append_Cons emb_Cons2)

609 subsection {* Appending elements *}

611 lemma sub_append [simp]:

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

613 proof

614 { fix xs' ys' xs ys zs :: "'a list" assume "sub xs' ys'"

615 hence "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sub xs ys"

616 proof (induct arbitrary: xs ys zs)

617 case emb_Nil show ?case by simp

618 next

619 case (emb_Cons xs' ys' x)

620 { assume "ys=[]" hence ?case using emb_Cons(1) by auto }

621 moreover

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

623 hence ?case using emb_Cons(2) by(simp add: emb.emb_Cons) }

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

625 next

626 case (emb_Cons2 x y xs' ys')

627 { assume "xs=[]" hence ?case using emb_Cons2(1) by auto }

628 moreover

629 { fix us vs assume "xs=x#us" "ys=x#vs" hence ?case using emb_Cons2 by auto}

630 moreover

631 { fix us assume "xs=x#us" "ys=[]" hence ?case using emb_Cons2(2) by bestsimp }

632 ultimately show ?case using `x = y` by (auto simp: Cons_eq_append_conv)

633 qed }

634 moreover assume ?l

635 ultimately show ?r by blast

636 next

637 assume ?r thus ?l by (metis emb_append_mono sub_refl)

638 qed

640 lemma sub_drop_many: "sub xs ys \<Longrightarrow> sub xs (zs @ ys)"

641 by (induct zs) auto

643 lemma sub_rev_drop_many: "sub xs ys \<Longrightarrow> sub xs (ys @ zs)"

644 by (metis append_Nil2 emb_Nil emb_append_mono)

647 subsection {* Relation to standard list operations *}

649 lemma sub_map:

650 assumes "sub xs ys" shows "sub (map f xs) (map f ys)"

651 using assms by (induct) auto

653 lemma sub_filter_left [simp]: "sub (filter P xs) xs"

654 by (induct xs) auto

656 lemma sub_filter [simp]:

657 assumes "sub xs ys" shows "sub (filter P xs) (filter P ys)"

658 using assms by (induct) auto

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

661 proof

662 assume ?L

663 thus ?R

664 proof (induct)

665 case emb_Nil show ?case by (metis sublist_empty)

666 next

667 case (emb_Cons xs ys x)

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

669 hence "xs = sublist (x#ys) (Suc ` N)"

670 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

671 thus ?case by blast

672 next

673 case (emb_Cons2 x y xs ys)

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

675 hence "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"

676 by (clarsimp simp add:sublist_Cons inj_image_mem_iff)

677 thus ?case unfolding `x = y` by blast

678 qed

679 next

680 assume ?R

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

682 moreover have "sub (sublist ys N) ys"

683 proof (induct ys arbitrary:N)

684 case Nil show ?case by simp

685 next

686 case Cons thus ?case by (auto simp: sublist_Cons)

687 qed

688 ultimately show ?L by simp

689 qed

691 end