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

src/HOL/Library/Sublist.thy

author | Manuel Eberl <eberlm@in.tum.de> |

Mon Mar 26 16:14:16 2018 +0200 (18 months ago) | |

changeset 67951 | 655aa11359dc |

parent 67614 | 560fbd6bc047 |

child 68406 | 6beb45f6cf67 |

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

Removed some uses of deprecated _tac methods. (Patch from Viorel Preoteasa)

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

2 Author: Tobias Nipkow and Markus Wenzel, TU München

3 Author: Christian Sternagel, JAIST

4 Author: Manuel Eberl, TU München

5 *)

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

9 theory Sublist

10 imports Main

11 begin

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

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

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

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

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

21 interpretation prefix_order: order prefix strict_prefix

22 by standard (auto simp: prefix_def strict_prefix_def)

24 interpretation prefix_bot: order_bot Nil prefix strict_prefix

25 by standard (simp add: prefix_def)

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

28 unfolding prefix_def by blast

30 lemma prefixE [elim?]:

31 assumes "prefix xs ys"

32 obtains zs where "ys = xs @ zs"

33 using assms unfolding prefix_def by blast

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

36 unfolding strict_prefix_def prefix_def by blast

38 lemma strict_prefixE' [elim?]:

39 assumes "strict_prefix xs ys"

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

41 proof -

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

43 unfolding strict_prefix_def prefix_def by blast

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

45 qed

47 (* FIXME rm *)

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

49 by(fact prefix_order.le_neq_trans)

51 lemma strict_prefixE [elim?]:

52 fixes xs ys :: "'a list"

53 assumes "strict_prefix xs ys"

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

55 using assms unfolding strict_prefix_def by blast

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

60 (* FIXME rm *)

61 theorem Nil_prefix [simp]: "prefix [] xs"

62 by (fact prefix_bot.bot_least)

64 (* FIXME rm *)

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

66 by (fact prefix_bot.bot_unique)

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

69 proof

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

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

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

73 by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)

74 next

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

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

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

78 qed

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

81 by (auto simp add: prefix_def)

83 lemma prefix_code [code]:

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

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

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

87 by simp_all

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

90 by (induct xs) simp_all

92 lemma same_prefix_nil [simp]: "prefix (xs @ ys) xs = (ys = [])"

93 by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)

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

96 unfolding prefix_def by fastforce

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

99 by (auto simp add: prefix_def)

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

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

104 theorem prefix_append:

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

106 apply (induct zs rule: rev_induct)

107 apply force

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

109 apply (metis append_eq_appendI)

110 done

112 lemma append_one_prefix:

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

114 proof (unfold prefix_def)

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

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

117 assume a2: "length xs < length ys"

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

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

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

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

122 qed

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

125 by (auto simp add: prefix_def)

127 lemma prefix_same_cases:

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

129 unfolding prefix_def by (force simp: append_eq_append_conv2)

131 lemma prefix_length_prefix:

132 "prefix ps xs \<Longrightarrow> prefix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> prefix ps qs"

133 by (auto simp: prefix_def) (metis append_Nil2 append_eq_append_conv_if)

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

136 by (auto simp add: prefix_def)

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

139 unfolding prefix_def by (metis append_take_drop_id)

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

142 by (simp add: butlast_conv_take take_is_prefix)

144 lemma map_mono_prefix: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"

145 by (auto simp: prefix_def)

147 lemma filter_mono_prefix: "prefix xs ys \<Longrightarrow> prefix (filter P xs) (filter P ys)"

148 by (auto simp: prefix_def)

150 lemma sorted_antimono_prefix: "prefix xs ys \<Longrightarrow> sorted ys \<Longrightarrow> sorted xs"

151 by (metis sorted_append prefix_def)

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

154 by (auto simp: strict_prefix_def prefix_def)

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

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

159 lemma strict_prefix_simps [simp, code]:

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

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

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

163 by (simp_all add: strict_prefix_def cong: conj_cong)

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

166 proof (induct n arbitrary: xs ys)

167 case 0

168 then show ?case by (cases ys) simp_all

169 next

170 case (Suc n)

171 then show ?case by (metis prefix_order.less_trans strict_prefixI take_is_prefix)

172 qed

174 lemma not_prefix_cases:

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

176 obtains

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

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

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

180 proof (cases ps)

181 case Nil

182 then show ?thesis using pfx by simp

183 next

184 case (Cons a as)

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

186 show ?thesis

187 proof (cases ls)

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

189 next

190 case (Cons x xs)

191 show ?thesis

192 proof (cases "x = a")

193 case True

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

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

196 next

197 case False

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

199 qed

200 qed

201 qed

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

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

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

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

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

208 shows "P ps ls" using np

209 proof (induct ls arbitrary: ps)

210 case Nil

211 then show ?case

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

213 next

214 case (Cons y ys)

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

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

217 by (rule not_prefix_cases) auto

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

219 qed

222 subsection \<open>Prefixes\<close>

224 primrec prefixes where

225 "prefixes [] = [[]]" |

226 "prefixes (x#xs) = [] # map ((#) x) (prefixes xs)"

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

229 proof (induct xs arbitrary: ys)

230 case Nil

231 then show ?case by (cases ys) auto

232 next

233 case (Cons a xs)

234 then show ?case by (cases ys) auto

235 qed

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

238 by (induction xs) auto

240 lemma distinct_prefixes [intro]: "distinct (prefixes xs)"

241 by (induction xs) (auto simp: distinct_map)

243 lemma prefixes_snoc [simp]: "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]"

244 by (induction xs) auto

246 lemma prefixes_not_Nil [simp]: "prefixes xs \<noteq> []"

247 by (cases xs) auto

249 lemma hd_prefixes [simp]: "hd (prefixes xs) = []"

250 by (cases xs) simp_all

252 lemma last_prefixes [simp]: "last (prefixes xs) = xs"

253 by (induction xs) (simp_all add: last_map)

255 lemma prefixes_append:

256 "prefixes (xs @ ys) = prefixes xs @ map (\<lambda>ys'. xs @ ys') (tl (prefixes ys))"

257 proof (induction xs)

258 case Nil

259 thus ?case by (cases ys) auto

260 qed simp_all

262 lemma prefixes_eq_snoc:

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

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

265 by (cases ys rule: rev_cases) auto

267 lemma prefixes_tailrec [code]:

268 "prefixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) ([],[[]]) xs))"

269 proof -

270 have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) (ys, rev ys # zs) xs =

271 (rev xs @ ys, rev (map (\<lambda>as. rev ys @ as) (prefixes xs)) @ zs)" for ys zs

272 proof (induction xs arbitrary: ys zs)

273 case (Cons x xs ys zs)

274 from Cons.IH[of "x # ys" "rev ys # zs"]

275 show ?case by (simp add: o_def)

276 qed simp_all

277 from this [of "[]" "[]"] show ?thesis by simp

278 qed

280 lemma set_prefixes_eq: "set (prefixes xs) = {ys. prefix ys xs}"

281 by auto

283 lemma card_set_prefixes [simp]: "card (set (prefixes xs)) = Suc (length xs)"

284 by (subst distinct_card) auto

286 lemma set_prefixes_append:

287 "set (prefixes (xs @ ys)) = set (prefixes xs) \<union> {xs @ ys' |ys'. ys' \<in> set (prefixes ys)}"

288 by (subst prefixes_append, cases ys) auto

291 subsection \<open>Longest Common Prefix\<close>

293 definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where

294 "Longest_common_prefix L = (ARG_MAX length ps. \<forall>xs \<in> L. prefix ps xs)"

296 lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow>

297 \<exists>ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)"

298 (is "_ \<Longrightarrow> \<exists>ps. ?P L ps")

299 proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L)

300 case 0

301 have "[] \<in> L" using "0.hyps" LeastI[of "\<lambda>n. \<exists>xs\<in>L. n = length xs"] \<open>L \<noteq> {}\<close>

302 by auto

303 hence "?P L []" by(auto)

304 thus ?case ..

305 next

306 case (Suc n)

307 let ?EX = "\<lambda>n. \<exists>xs\<in>L. n = length xs"

308 obtain x xs where xxs: "x#xs \<in> L" "size xs = n" using Suc.prems Suc.hyps(2)

309 by(metis LeastI_ex[of ?EX] Suc_length_conv ex_in_conv)

310 hence "[] \<notin> L" using Suc.hyps(2) by auto

311 show ?case

312 proof (cases "\<forall>xs \<in> L. \<exists>ys. xs = x#ys")

313 case True

314 let ?L = "{ys. x#ys \<in> L}"

315 have 1: "(LEAST n. \<exists>xs \<in> ?L. n = length xs) = n"

316 using xxs Suc.prems Suc.hyps(2) Least_le[of "?EX"]

317 by - (rule Least_equality, fastforce+)

318 have 2: "?L \<noteq> {}" using \<open>x # xs \<in> L\<close> by auto

319 from Suc.hyps(1)[OF 1[symmetric] 2] obtain ps where IH: "?P ?L ps" ..

320 { fix qs

321 assume "\<forall>qs. (\<forall>xa. x # xa \<in> L \<longrightarrow> prefix qs xa) \<longrightarrow> length qs \<le> length ps"

322 and "\<forall>xs\<in>L. prefix qs xs"

323 hence "length (tl qs) \<le> length ps"

324 by (metis Cons_prefix_Cons hd_Cons_tl list.sel(2) Nil_prefix)

325 hence "length qs \<le> Suc (length ps)" by auto

326 }

327 hence "?P L (x#ps)" using True IH by auto

328 thus ?thesis ..

329 next

330 case False

331 then obtain y ys where yys: "x\<noteq>y" "y#ys \<in> L" using \<open>[] \<notin> L\<close>

332 by (auto) (metis list.exhaust)

333 have "\<forall>qs. (\<forall>xs\<in>L. prefix qs xs) \<longrightarrow> qs = []" using yys \<open>x#xs \<in> L\<close>

334 by auto (metis Cons_prefix_Cons prefix_Cons)

335 hence "?P L []" by auto

336 thus ?thesis ..

337 qed

338 qed

340 lemma Longest_common_prefix_unique: "L \<noteq> {} \<Longrightarrow>

341 \<exists>! ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)"

342 by(rule ex_ex1I[OF Longest_common_prefix_ex];

343 meson equals0I prefix_length_prefix prefix_order.antisym)

345 lemma Longest_common_prefix_eq:

346 "\<lbrakk> L \<noteq> {}; \<forall>xs \<in> L. prefix ps xs;

347 \<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk>

348 \<Longrightarrow> Longest_common_prefix L = ps"

349 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder

350 by(rule some1_equality[OF Longest_common_prefix_unique]) auto

352 lemma Longest_common_prefix_prefix:

353 "xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs"

354 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder

355 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto

357 lemma Longest_common_prefix_longest:

358 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)"

359 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder

360 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto

362 lemma Longest_common_prefix_max_prefix:

363 "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)"

364 by(metis Longest_common_prefix_prefix Longest_common_prefix_longest

365 prefix_length_prefix ex_in_conv)

367 lemma Longest_common_prefix_Nil: "[] \<in> L \<Longrightarrow> Longest_common_prefix L = []"

368 using Longest_common_prefix_prefix prefix_Nil by blast

370 lemma Longest_common_prefix_image_Cons: "L \<noteq> {} \<Longrightarrow>

371 Longest_common_prefix ((#) x ` L) = x # Longest_common_prefix L"

372 apply(rule Longest_common_prefix_eq)

373 apply(simp)

374 apply (simp add: Longest_common_prefix_prefix)

375 apply simp

376 by(metis Longest_common_prefix_longest[of L] Cons_prefix_Cons Nitpick.size_list_simp(2)

377 Suc_le_mono hd_Cons_tl order.strict_implies_order zero_less_Suc)

379 lemma Longest_common_prefix_eq_Cons: assumes "L \<noteq> {}" "[] \<notin> L" "\<forall>xs\<in>L. hd xs = x"

380 shows "Longest_common_prefix L = x # Longest_common_prefix {ys. x#ys \<in> L}"

381 proof -

382 have "L = (#) x ` {ys. x#ys \<in> L}" using assms(2,3)

383 by (auto simp: image_def)(metis hd_Cons_tl)

384 thus ?thesis

385 by (metis Longest_common_prefix_image_Cons image_is_empty assms(1))

386 qed

388 lemma Longest_common_prefix_eq_Nil:

389 "\<lbrakk>x#ys \<in> L; y#zs \<in> L; x \<noteq> y \<rbrakk> \<Longrightarrow> Longest_common_prefix L = []"

390 by (metis Longest_common_prefix_prefix list.inject prefix_Cons)

393 fun longest_common_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where

394 "longest_common_prefix (x#xs) (y#ys) =

395 (if x=y then x # longest_common_prefix xs ys else [])" |

396 "longest_common_prefix _ _ = []"

398 lemma longest_common_prefix_prefix1:

399 "prefix (longest_common_prefix xs ys) xs"

400 by(induction xs ys rule: longest_common_prefix.induct) auto

402 lemma longest_common_prefix_prefix2:

403 "prefix (longest_common_prefix xs ys) ys"

404 by(induction xs ys rule: longest_common_prefix.induct) auto

406 lemma longest_common_prefix_max_prefix:

407 "\<lbrakk> prefix ps xs; prefix ps ys \<rbrakk>

408 \<Longrightarrow> prefix ps (longest_common_prefix xs ys)"

409 by(induction xs ys arbitrary: ps rule: longest_common_prefix.induct)

410 (auto simp: prefix_Cons)

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

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

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

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

419 unfolding parallel_def by blast

421 lemma parallelE [elim]:

422 assumes "xs \<parallel> ys"

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

424 using assms unfolding parallel_def by blast

426 theorem prefix_cases:

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

428 unfolding parallel_def strict_prefix_def by blast

430 theorem parallel_decomp:

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

432 proof (induct xs rule: rev_induct)

433 case Nil

434 then have False by auto

435 then show ?case ..

436 next

437 case (snoc x xs)

438 show ?case

439 proof (rule prefix_cases)

440 assume le: "prefix xs ys"

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

442 show ?thesis

443 proof (cases ys')

444 assume "ys' = []"

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

446 next

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

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

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

450 using ys ys' by blast

451 qed

452 next

453 assume "strict_prefix ys xs"

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

455 with snoc have False by blast

456 then show ?thesis ..

457 next

458 assume "xs \<parallel> ys"

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

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

461 by blast

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

463 with neq ys show ?thesis by blast

464 qed

465 qed

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

468 apply (rule parallelI)

469 apply (erule parallelE, erule conjE,

470 induct rule: not_prefix_induct, simp+)+

471 done

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

474 by (simp add: parallel_append)

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

477 unfolding parallel_def by auto

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

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

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

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

486 where "strict_suffix xs ys \<longleftrightarrow> suffix xs ys \<and> xs \<noteq> ys"

488 interpretation suffix_order: order suffix strict_suffix

489 by standard (auto simp: suffix_def strict_suffix_def)

491 interpretation suffix_bot: order_bot Nil suffix strict_suffix

492 by standard (simp add: suffix_def)

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

495 unfolding suffix_def by blast

497 lemma suffixE [elim?]:

498 assumes "suffix xs ys"

499 obtains zs where "ys = zs @ xs"

500 using assms unfolding suffix_def by blast

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

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

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

506 by (induct xs) (auto simp: strict_suffix_def suffix_def)

508 lemma Nil_suffix [simp]: "suffix [] xs"

509 by (simp add: suffix_def)

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

512 by (auto simp add: suffix_def)

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

515 by (auto simp add: suffix_def)

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

518 by (auto simp add: suffix_def)

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

521 by (auto simp add: suffix_def)

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

524 by (auto simp add: suffix_def)

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

527 by (auto simp: strict_suffix_def suffix_def)

529 lemma set_mono_suffix: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"

530 by (auto simp: suffix_def)

532 lemma sorted_antimono_suffix: "suffix xs ys \<Longrightarrow> sorted ys \<Longrightarrow> sorted xs"

533 by (metis sorted_append suffix_def)

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

536 proof -

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

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

539 then show ?thesis

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

541 qed

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

544 proof

545 assume "suffix xs ys"

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

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

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

549 next

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

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

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

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

554 then show "suffix xs ys" ..

555 qed

557 lemma strict_suffix_to_prefix [code]: "strict_suffix xs ys \<longleftrightarrow> strict_prefix (rev xs) (rev ys)"

558 by (auto simp: suffix_to_prefix strict_suffix_def strict_prefix_def)

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

561 by (clarsimp elim!: suffixE)

563 lemma map_mono_suffix: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"

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

566 lemma filter_mono_suffix: "suffix xs ys \<Longrightarrow> suffix (filter P xs) (filter P ys)"

567 by (auto simp: suffix_def)

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

570 unfolding suffix_def by (rule exI [where x = "take n as"]) simp

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

573 by (auto elim!: suffixE)

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

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

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

579 unfolding suffix_def by auto

581 lemma suffix_snoc [simp]: "suffix xs (ys @ [y]) \<longleftrightarrow> xs = [] \<or> (\<exists>zs. xs = zs @ [y] \<and> suffix zs ys)"

582 by (cases xs rule: rev_cases) (auto simp: suffix_def)

584 lemma snoc_suffix_snoc [simp]: "suffix (xs @ [x]) (ys @ [y]) = (x = y \<and> suffix xs ys)"

585 by (auto simp add: suffix_def)

587 lemma same_suffix_suffix [simp]: "suffix (ys @ xs) (zs @ xs) = suffix ys zs"

588 by (simp add: suffix_to_prefix)

590 lemma same_suffix_nil [simp]: "suffix (ys @ xs) xs = (ys = [])"

591 by (simp add: suffix_to_prefix)

593 theorem suffix_Cons: "suffix xs (y # ys) \<longleftrightarrow> xs = y # ys \<or> suffix xs ys"

594 unfolding suffix_def by (auto simp: Cons_eq_append_conv)

596 theorem suffix_append:

597 "suffix xs (ys @ zs) \<longleftrightarrow> suffix xs zs \<or> (\<exists>xs'. xs = xs' @ zs \<and> suffix xs' ys)"

598 by (auto simp: suffix_def append_eq_append_conv2)

600 theorem suffix_length_le: "suffix xs ys \<Longrightarrow> length xs \<le> length ys"

601 by (auto simp add: suffix_def)

603 lemma suffix_same_cases:

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

605 unfolding suffix_def by (force simp: append_eq_append_conv2)

607 lemma suffix_length_suffix:

608 "suffix ps xs \<Longrightarrow> suffix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> suffix ps qs"

609 by (auto simp: suffix_to_prefix intro: prefix_length_prefix)

611 lemma suffix_length_less: "strict_suffix xs ys \<Longrightarrow> length xs < length ys"

612 by (auto simp: strict_suffix_def suffix_def)

614 lemma suffix_ConsD': "suffix (x#xs) ys \<Longrightarrow> strict_suffix xs ys"

615 by (auto simp: strict_suffix_def suffix_def)

617 lemma drop_strict_suffix: "strict_suffix xs ys \<Longrightarrow> strict_suffix (drop n xs) ys"

618 proof (induct n arbitrary: xs ys)

619 case 0

620 then show ?case by (cases ys) simp_all

621 next

622 case (Suc n)

623 then show ?case

624 by (cases xs) (auto intro: Suc dest: suffix_ConsD' suffix_order.less_imp_le)

625 qed

627 lemma not_suffix_cases:

628 assumes pfx: "\<not> suffix ps ls"

629 obtains

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

631 | (c2) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x = a" and "\<not> suffix as xs"

632 | (c3) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x \<noteq> a"

633 proof (cases ps rule: rev_cases)

634 case Nil

635 then show ?thesis using pfx by simp

636 next

637 case (snoc as a)

638 note c = \<open>ps = as@[a]\<close>

639 show ?thesis

640 proof (cases ls rule: rev_cases)

641 case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_suffix_nil)

642 next

643 case (snoc xs x)

644 show ?thesis

645 proof (cases "x = a")

646 case True

647 have "\<not> suffix as xs" using pfx c snoc True by simp

648 with c snoc True show ?thesis by (rule c2)

649 next

650 case False

651 with c snoc show ?thesis by (rule c3)

652 qed

653 qed

654 qed

656 lemma not_suffix_induct [consumes 1, case_names Nil Neq Eq]:

657 assumes np: "\<not> suffix ps ls"

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

659 and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (xs@[x]) (ys@[y])"

660 and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> suffix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (xs@[x]) (ys@[y])"

661 shows "P ps ls" using np

662 proof (induct ls arbitrary: ps rule: rev_induct)

663 case Nil

664 then show ?case by (cases ps rule: rev_cases) (auto intro: base)

665 next

666 case (snoc y ys ps)

667 then have npfx: "\<not> suffix ps (ys @ [y])" by simp

668 then obtain x xs where pv: "ps = xs @ [x]"

669 by (rule not_suffix_cases) auto

670 show ?case by (metis snoc.hyps snoc_suffix_snoc npfx pv r1 r2)

671 qed

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

675 by blast

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

678 by blast

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

681 unfolding parallel_def by simp

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

684 unfolding parallel_def by simp

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

687 by auto

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

690 by (metis Cons_prefix_Cons parallelE parallelI)

692 lemma not_equal_is_parallel:

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

694 and len: "length xs = length ys"

695 shows "xs \<parallel> ys"

696 using len neq

697 proof (induct rule: list_induct2)

698 case Nil

699 then show ?case by simp

700 next

701 case (Cons a as b bs)

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

703 show ?case

704 proof (cases "a = b")

705 case True

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

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

708 next

709 case False

710 then show ?thesis by (rule Cons_parallelI1)

711 qed

712 qed

714 subsection \<open>Suffixes\<close>

716 primrec suffixes where

717 "suffixes [] = [[]]"

718 | "suffixes (x#xs) = suffixes xs @ [x # xs]"

720 lemma in_set_suffixes [simp]: "xs \<in> set (suffixes ys) \<longleftrightarrow> suffix xs ys"

721 by (induction ys) (auto simp: suffix_def Cons_eq_append_conv)

723 lemma distinct_suffixes [intro]: "distinct (suffixes xs)"

724 by (induction xs) (auto simp: suffix_def)

726 lemma length_suffixes [simp]: "length (suffixes xs) = Suc (length xs)"

727 by (induction xs) auto

729 lemma suffixes_snoc [simp]: "suffixes (xs @ [x]) = [] # map (\<lambda>ys. ys @ [x]) (suffixes xs)"

730 by (induction xs) auto

732 lemma suffixes_not_Nil [simp]: "suffixes xs \<noteq> []"

733 by (cases xs) auto

735 lemma hd_suffixes [simp]: "hd (suffixes xs) = []"

736 by (induction xs) simp_all

738 lemma last_suffixes [simp]: "last (suffixes xs) = xs"

739 by (cases xs) simp_all

741 lemma suffixes_append:

742 "suffixes (xs @ ys) = suffixes ys @ map (\<lambda>xs'. xs' @ ys) (tl (suffixes xs))"

743 proof (induction ys rule: rev_induct)

744 case Nil

745 thus ?case by (cases xs rule: rev_cases) auto

746 next

747 case (snoc y ys)

748 show ?case

749 by (simp only: append.assoc [symmetric] suffixes_snoc snoc.IH) simp

750 qed

752 lemma suffixes_eq_snoc:

753 "suffixes ys = xs @ [x] \<longleftrightarrow>

754 (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = z#zs \<and> xs = suffixes zs)) \<and> x = ys"

755 by (cases ys) auto

757 lemma suffixes_tailrec [code]:

758 "suffixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) ([],[[]]) (rev xs)))"

759 proof -

760 have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) (ys, ys # zs) (rev xs) =

761 (xs @ ys, rev (map (\<lambda>as. as @ ys) (suffixes xs)) @ zs)" for ys zs

762 proof (induction xs arbitrary: ys zs)

763 case (Cons x xs ys zs)

764 from Cons.IH[of ys zs]

765 show ?case by (simp add: o_def case_prod_unfold)

766 qed simp_all

767 from this [of "[]" "[]"] show ?thesis by simp

768 qed

770 lemma set_suffixes_eq: "set (suffixes xs) = {ys. suffix ys xs}"

771 by auto

773 lemma card_set_suffixes [simp]: "card (set (suffixes xs)) = Suc (length xs)"

774 by (subst distinct_card) auto

776 lemma set_suffixes_append:

777 "set (suffixes (xs @ ys)) = set (suffixes ys) \<union> {xs' @ ys |xs'. xs' \<in> set (suffixes xs)}"

778 by (subst suffixes_append, cases xs rule: rev_cases) auto

781 lemma suffixes_conv_prefixes: "suffixes xs = map rev (prefixes (rev xs))"

782 by (induction xs) auto

784 lemma prefixes_conv_suffixes: "prefixes xs = map rev (suffixes (rev xs))"

785 by (induction xs) auto

787 lemma prefixes_rev: "prefixes (rev xs) = map rev (suffixes xs)"

788 by (induction xs) auto

790 lemma suffixes_rev: "suffixes (rev xs) = map rev (prefixes xs)"

791 by (induction xs) auto

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

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

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

798 where

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

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

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

803 lemma list_emb_mono:

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

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

806 proof

807 assume "list_emb P xs ys"

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

809 qed

811 lemma list_emb_Nil2 [simp]:

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

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

815 lemma list_emb_refl:

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

817 shows "list_emb P xs xs"

818 using assms by (induct xs) auto

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

821 proof -

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

823 from list_emb_Nil2 [OF this] have False by simp

824 } moreover {

825 assume False

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

827 } ultimately show ?thesis by blast

828 qed

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

831 by (induct zs) auto

833 lemma list_emb_prefix [intro]:

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

835 using assms

836 by (induct arbitrary: zs) auto

838 lemma list_emb_ConsD:

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

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

841 using assms

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

843 case list_emb_Cons

844 then show ?case by (metis append_Cons)

845 next

846 case (list_emb_Cons2 x y xs ys)

847 then show ?case by blast

848 qed

850 lemma list_emb_appendD:

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

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

853 using assms

854 proof (induction xs arbitrary: ys zs)

855 case Nil then show ?case by auto

856 next

857 case (Cons x xs)

858 then obtain us v vs where

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

860 by (auto dest: list_emb_ConsD)

861 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

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

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

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

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

866 qed

868 lemma list_emb_strict_suffix:

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

870 shows "list_emb P xs zs"

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

873 lemma list_emb_suffix:

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

875 shows "list_emb P xs zs"

876 using assms and list_emb_strict_suffix

877 unfolding strict_suffix_reflclp_conv[symmetric] by auto

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

880 by (induct rule: list_emb.induct) auto

882 lemma list_emb_trans:

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

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

885 proof -

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

887 then show "list_emb P xs zs" using assms

888 proof (induction arbitrary: zs)

889 case list_emb_Nil show ?case by blast

890 next

891 case (list_emb_Cons xs ys y)

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

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

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

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

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

897 next

898 case (list_emb_Cons2 x y xs ys)

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

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

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

902 moreover have "P x v"

903 proof -

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

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

906 ultimately show ?thesis

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

908 by blast

909 qed

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

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

912 qed

913 qed

915 lemma list_emb_set:

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

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

918 using assms by (induct) auto

920 lemma list_emb_Cons_iff1 [simp]:

921 assumes "P x y"

922 shows "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P xs ys"

923 using assms by (subst list_emb.simps) (auto dest: list_emb_ConsD)

925 lemma list_emb_Cons_iff2 [simp]:

926 assumes "\<not>P x y"

927 shows "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P (x#xs) ys"

928 using assms by (subst list_emb.simps) auto

930 lemma list_emb_code [code]:

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

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

933 "list_emb P (x#xs) (y#ys) \<longleftrightarrow> (if P x y then list_emb P xs ys else list_emb P (x#xs) ys)"

934 by simp_all

937 subsection \<open>Subsequences (special case of homeomorphic embedding)\<close>

939 abbreviation subseq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"

940 where "subseq xs ys \<equiv> list_emb (=) xs ys"

942 definition strict_subseq where "strict_subseq xs ys \<longleftrightarrow> xs \<noteq> ys \<and> subseq xs ys"

944 lemma subseq_Cons2: "subseq xs ys \<Longrightarrow> subseq (x#xs) (x#ys)" by auto

946 lemma subseq_same_length:

947 assumes "subseq xs ys" and "length xs = length ys" shows "xs = ys"

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

950 lemma not_subseq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> subseq xs ys"

951 by (metis list_emb_length linorder_not_less)

953 lemma subseq_Cons': "subseq (x#xs) ys \<Longrightarrow> subseq xs ys"

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

956 lemma subseq_Cons2':

957 assumes "subseq (x#xs) (x#ys)" shows "subseq xs ys"

958 using assms by (cases) (rule subseq_Cons')

960 lemma subseq_Cons2_neq:

961 assumes "subseq (x#xs) (y#ys)"

962 shows "x \<noteq> y \<Longrightarrow> subseq (x#xs) ys"

963 using assms by (cases) auto

965 lemma subseq_Cons2_iff [simp]:

966 "subseq (x#xs) (y#ys) = (if x = y then subseq xs ys else subseq (x#xs) ys)"

967 by simp

969 lemma subseq_append': "subseq (zs @ xs) (zs @ ys) \<longleftrightarrow> subseq xs ys"

970 by (induct zs) simp_all

972 interpretation subseq_order: order subseq strict_subseq

973 proof

974 fix xs ys :: "'a list"

975 {

976 assume "subseq xs ys" and "subseq ys xs"

977 thus "xs = ys"

978 proof (induct)

979 case list_emb_Nil

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

981 next

982 case list_emb_Cons2

983 thus ?case by simp

984 next

985 case list_emb_Cons

986 hence False using subseq_Cons' by fastforce

987 thus ?case ..

988 qed

989 }

990 thus "strict_subseq xs ys \<longleftrightarrow> (subseq xs ys \<and> \<not>subseq ys xs)"

991 by (auto simp: strict_subseq_def)

992 qed (auto simp: list_emb_refl intro: list_emb_trans)

994 lemma in_set_subseqs [simp]: "xs \<in> set (subseqs ys) \<longleftrightarrow> subseq xs ys"

995 proof

996 assume "xs \<in> set (subseqs ys)"

997 thus "subseq xs ys"

998 by (induction ys arbitrary: xs) (auto simp: Let_def)

999 next

1000 have [simp]: "[] \<in> set (subseqs ys)" for ys :: "'a list"

1001 by (induction ys) (auto simp: Let_def)

1002 assume "subseq xs ys"

1003 thus "xs \<in> set (subseqs ys)"

1004 by (induction xs ys rule: list_emb.induct) (auto simp: Let_def)

1005 qed

1007 lemma set_subseqs_eq: "set (subseqs ys) = {xs. subseq xs ys}"

1008 by auto

1010 lemma subseq_append_le_same_iff: "subseq (xs @ ys) ys \<longleftrightarrow> xs = []"

1011 by (auto dest: list_emb_length)

1013 lemma subseq_singleton_left: "subseq [x] ys \<longleftrightarrow> x \<in> set ys"

1014 by (fastforce dest: list_emb_ConsD split_list_last)

1016 lemma list_emb_append_mono:

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

1018 by (induct rule: list_emb.induct) auto

1020 lemma prefix_imp_subseq [intro]: "prefix xs ys \<Longrightarrow> subseq xs ys"

1021 by (auto simp: prefix_def)

1023 lemma suffix_imp_subseq [intro]: "suffix xs ys \<Longrightarrow> subseq xs ys"

1024 by (auto simp: suffix_def)

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

1029 lemma subseq_append [simp]:

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

1031 proof

1032 { fix xs' ys' xs ys zs :: "'a list" assume "subseq xs' ys'"

1033 then have "xs' = xs @ zs \<and> ys' = ys @ zs \<longrightarrow> subseq xs ys"

1034 proof (induct arbitrary: xs ys zs)

1035 case list_emb_Nil show ?case by simp

1036 next

1037 case (list_emb_Cons xs' ys' x)

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

1039 moreover

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

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

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

1043 next

1044 case (list_emb_Cons2 x y xs' ys')

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

1046 moreover

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

1048 moreover

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

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

1051 qed }

1052 moreover assume ?l

1053 ultimately show ?r by blast

1054 next

1055 assume ?r then show ?l by (metis list_emb_append_mono subseq_order.order_refl)

1056 qed

1058 lemma subseq_append_iff:

1059 "subseq xs (ys @ zs) \<longleftrightarrow> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> subseq xs1 ys \<and> subseq xs2 zs)"

1060 (is "?lhs = ?rhs")

1061 proof

1062 assume ?lhs thus ?rhs

1063 proof (induction xs "ys @ zs" arbitrary: ys zs rule: list_emb.induct)

1064 case (list_emb_Cons xs ws y ys zs)

1065 from list_emb_Cons(2)[of "tl ys" zs] and list_emb_Cons(2)[of "[]" "tl zs"] and list_emb_Cons(1,3)

1066 show ?case by (cases ys) auto

1067 next

1068 case (list_emb_Cons2 x y xs ws ys zs)

1069 from list_emb_Cons2(3)[of "tl ys" zs] and list_emb_Cons2(3)[of "[]" "tl zs"]

1070 and list_emb_Cons2(1,2,4)

1071 show ?case by (cases ys) (auto simp: Cons_eq_append_conv)

1072 qed auto

1073 qed (auto intro: list_emb_append_mono)

1075 lemma subseq_appendE [case_names append]:

1076 assumes "subseq xs (ys @ zs)"

1077 obtains xs1 xs2 where "xs = xs1 @ xs2" "subseq xs1 ys" "subseq xs2 zs"

1078 using assms by (subst (asm) subseq_append_iff) auto

1080 lemma subseq_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (zs @ ys)"

1081 by (induct zs) auto

1083 lemma subseq_rev_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (ys @ zs)"

1084 by (metis append_Nil2 list_emb_Nil list_emb_append_mono)

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

1089 lemma subseq_map:

1090 assumes "subseq xs ys" shows "subseq (map f xs) (map f ys)"

1091 using assms by (induct) auto

1093 lemma subseq_filter_left [simp]: "subseq (filter P xs) xs"

1094 by (induct xs) auto

1096 lemma subseq_filter [simp]:

1097 assumes "subseq xs ys" shows "subseq (filter P xs) (filter P ys)"

1098 using assms by induct auto

1100 lemma subseq_conv_nths:

1101 "subseq xs ys \<longleftrightarrow> (\<exists>N. xs = nths ys N)" (is "?L = ?R")

1102 proof

1103 assume ?L

1104 then show ?R

1105 proof (induct)

1106 case list_emb_Nil show ?case by (metis nths_empty)

1107 next

1108 case (list_emb_Cons xs ys x)

1109 then obtain N where "xs = nths ys N" by blast

1110 then have "xs = nths (x#ys) (Suc ` N)"

1111 by (clarsimp simp add: nths_Cons inj_image_mem_iff)

1112 then show ?case by blast

1113 next

1114 case (list_emb_Cons2 x y xs ys)

1115 then obtain N where "xs = nths ys N" by blast

1116 then have "x#xs = nths (x#ys) (insert 0 (Suc ` N))"

1117 by (clarsimp simp add: nths_Cons inj_image_mem_iff)

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

1119 ultimately show ?case by blast

1120 qed

1121 next

1122 assume ?R

1123 then obtain N where "xs = nths ys N" ..

1124 moreover have "subseq (nths ys N) ys"

1125 proof (induct ys arbitrary: N)

1126 case Nil show ?case by simp

1127 next

1128 case Cons then show ?case by (auto simp: nths_Cons)

1129 qed

1130 ultimately show ?L by simp

1131 qed

1134 subsection \<open>Contiguous sublists\<close>

1136 definition sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where

1137 "sublist xs ys = (\<exists>ps ss. ys = ps @ xs @ ss)"

1139 definition strict_sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where

1140 "strict_sublist xs ys \<longleftrightarrow> sublist xs ys \<and> xs \<noteq> ys"

1142 interpretation sublist_order: order sublist strict_sublist

1143 proof

1144 fix xs ys zs :: "'a list"

1145 assume "sublist xs ys" "sublist ys zs"

1146 then obtain xs1 xs2 ys1 ys2 where "ys = xs1 @ xs @ xs2" "zs = ys1 @ ys @ ys2"

1147 by (auto simp: sublist_def)

1148 hence "zs = (ys1 @ xs1) @ xs @ (xs2 @ ys2)" by simp

1149 thus "sublist xs zs" unfolding sublist_def by blast

1150 next

1151 fix xs ys :: "'a list"

1152 {

1153 assume "sublist xs ys" "sublist ys xs"

1154 then obtain as bs cs ds

1155 where xs: "xs = as @ ys @ bs" and ys: "ys = cs @ xs @ ds"

1156 by (auto simp: sublist_def)

1157 have "xs = as @ cs @ xs @ ds @ bs" by (subst xs, subst ys) auto

1158 also have "length \<dots> = length as + length cs + length xs + length bs + length ds"

1159 by simp

1160 finally have "as = []" "bs = []" by simp_all

1161 with xs show "xs = ys" by simp

1162 }

1163 thus "strict_sublist xs ys \<longleftrightarrow> (sublist xs ys \<and> \<not>sublist ys xs)"

1164 by (auto simp: strict_sublist_def)

1165 qed (auto simp: strict_sublist_def sublist_def intro: exI[of _ "[]"])

1167 lemma sublist_Nil_left [simp, intro]: "sublist [] ys"

1168 by (auto simp: sublist_def)

1170 lemma sublist_Cons_Nil [simp]: "\<not>sublist (x#xs) []"

1171 by (auto simp: sublist_def)

1173 lemma sublist_Nil_right [simp]: "sublist xs [] \<longleftrightarrow> xs = []"

1174 by (cases xs) auto

1176 lemma sublist_appendI [simp, intro]: "sublist xs (ps @ xs @ ss)"

1177 by (auto simp: sublist_def)

1179 lemma sublist_append_leftI [simp, intro]: "sublist xs (ps @ xs)"

1180 by (auto simp: sublist_def intro: exI[of _ "[]"])

1182 lemma sublist_append_rightI [simp, intro]: "sublist xs (xs @ ss)"

1183 by (auto simp: sublist_def intro: exI[of _ "[]"])

1185 lemma sublist_altdef: "sublist xs ys \<longleftrightarrow> (\<exists>ys'. prefix ys' ys \<and> suffix xs ys')"

1186 proof safe

1187 assume "sublist xs ys"

1188 then obtain ps ss where "ys = ps @ xs @ ss" by (auto simp: sublist_def)

1189 thus "\<exists>ys'. prefix ys' ys \<and> suffix xs ys'"

1190 by (intro exI[of _ "ps @ xs"] conjI suffix_appendI) auto

1191 next

1192 fix ys'

1193 assume "prefix ys' ys" "suffix xs ys'"

1194 thus "sublist xs ys" by (auto simp: prefix_def suffix_def)

1195 qed

1197 lemma sublist_altdef': "sublist xs ys \<longleftrightarrow> (\<exists>ys'. suffix ys' ys \<and> prefix xs ys')"

1198 proof safe

1199 assume "sublist xs ys"

1200 then obtain ps ss where "ys = ps @ xs @ ss" by (auto simp: sublist_def)

1201 thus "\<exists>ys'. suffix ys' ys \<and> prefix xs ys'"

1202 by (intro exI[of _ "xs @ ss"] conjI suffixI) auto

1203 next

1204 fix ys'

1205 assume "suffix ys' ys" "prefix xs ys'"

1206 thus "sublist xs ys" by (auto simp: prefix_def suffix_def)

1207 qed

1209 lemma sublist_Cons_right: "sublist xs (y # ys) \<longleftrightarrow> prefix xs (y # ys) \<or> sublist xs ys"

1210 by (auto simp: sublist_def prefix_def Cons_eq_append_conv)

1212 lemma sublist_code [code]:

1213 "sublist [] ys \<longleftrightarrow> True"

1214 "sublist (x # xs) [] \<longleftrightarrow> False"

1215 "sublist (x # xs) (y # ys) \<longleftrightarrow> prefix (x # xs) (y # ys) \<or> sublist (x # xs) ys"

1216 by (simp_all add: sublist_Cons_right)

1219 lemma sublist_append:

1220 "sublist xs (ys @ zs) \<longleftrightarrow>

1221 sublist xs ys \<or> sublist xs zs \<or> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> suffix xs1 ys \<and> prefix xs2 zs)"

1222 by (auto simp: sublist_altdef prefix_append suffix_append)

1224 primrec sublists :: "'a list \<Rightarrow> 'a list list" where

1225 "sublists [] = [[]]"

1226 | "sublists (x # xs) = sublists xs @ map ((#) x) (prefixes xs)"

1228 lemma in_set_sublists [simp]: "xs \<in> set (sublists ys) \<longleftrightarrow> sublist xs ys"

1229 by (induction ys arbitrary: xs) (auto simp: sublist_Cons_right prefix_Cons)

1231 lemma set_sublists_eq: "set (sublists xs) = {ys. sublist ys xs}"

1232 by auto

1234 lemma length_sublists [simp]: "length (sublists xs) = Suc (length xs * Suc (length xs) div 2)"

1235 by (induction xs) simp_all

1237 lemma sublist_length_le: "sublist xs ys \<Longrightarrow> length xs \<le> length ys"

1238 by (auto simp add: sublist_def)

1240 lemma set_mono_sublist: "sublist xs ys \<Longrightarrow> set xs \<subseteq> set ys"

1241 by (auto simp add: sublist_def)

1243 lemma prefix_imp_sublist [simp, intro]: "prefix xs ys \<Longrightarrow> sublist xs ys"

1244 by (auto simp: sublist_def prefix_def intro: exI[of _ "[]"])

1246 lemma suffix_imp_sublist [simp, intro]: "suffix xs ys \<Longrightarrow> sublist xs ys"

1247 by (auto simp: sublist_def suffix_def intro: exI[of _ "[]"])

1249 lemma sublist_take [simp, intro]: "sublist (take n xs) xs"

1250 by (rule prefix_imp_sublist) (simp_all add: take_is_prefix)

1252 lemma sublist_drop [simp, intro]: "sublist (drop n xs) xs"

1253 by (rule suffix_imp_sublist) (simp_all add: suffix_drop)

1255 lemma sublist_tl [simp, intro]: "sublist (tl xs) xs"

1256 by (rule suffix_imp_sublist) (simp_all add: suffix_drop)

1258 lemma sublist_butlast [simp, intro]: "sublist (butlast xs) xs"

1259 by (rule prefix_imp_sublist) (simp_all add: prefixeq_butlast)

1261 lemma sublist_rev [simp]: "sublist (rev xs) (rev ys) = sublist xs ys"

1262 proof

1263 assume "sublist (rev xs) (rev ys)"

1264 then obtain as bs where "rev ys = as @ rev xs @ bs"

1265 by (auto simp: sublist_def)

1266 also have "rev \<dots> = rev bs @ xs @ rev as" by simp

1267 finally show "sublist xs ys" by simp

1268 next

1269 assume "sublist xs ys"

1270 then obtain as bs where "ys = as @ xs @ bs"

1271 by (auto simp: sublist_def)

1272 also have "rev \<dots> = rev bs @ rev xs @ rev as" by simp

1273 finally show "sublist (rev xs) (rev ys)" by simp

1274 qed

1276 lemma sublist_rev_left: "sublist (rev xs) ys = sublist xs (rev ys)"

1277 by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident)

1279 lemma sublist_rev_right: "sublist xs (rev ys) = sublist (rev xs) ys"

1280 by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident)

1282 lemma snoc_sublist_snoc:

1283 "sublist (xs @ [x]) (ys @ [y]) \<longleftrightarrow>

1284 (x = y \<and> suffix xs ys \<or> sublist (xs @ [x]) ys) "

1285 by (subst (1 2) sublist_rev [symmetric])

1286 (simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix)

1288 lemma sublist_snoc:

1289 "sublist xs (ys @ [y]) \<longleftrightarrow> suffix xs (ys @ [y]) \<or> sublist xs ys"

1290 by (subst (1 2) sublist_rev [symmetric])

1291 (simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix)

1293 lemma sublist_imp_subseq [intro]: "sublist xs ys \<Longrightarrow> subseq xs ys"

1294 by (auto simp: sublist_def)

1296 subsection \<open>Parametricity\<close>

1298 context includes lifting_syntax

1299 begin

1301 private lemma prefix_primrec:

1302 "prefix = rec_list (\<lambda>xs. True) (\<lambda>x xs xsa ys.

1303 case ys of [] \<Rightarrow> False | y # ys \<Rightarrow> x = y \<and> xsa ys)"

1304 proof (intro ext, goal_cases)

1305 case (1 xs ys)

1306 show ?case by (induction xs arbitrary: ys) (auto simp: prefix_Cons split: list.splits)

1307 qed

1309 private lemma sublist_primrec:

1310 "sublist = (\<lambda>xs ys. rec_list (\<lambda>xs. xs = []) (\<lambda>y ys ysa xs. prefix xs (y # ys) \<or> ysa xs) ys xs)"

1311 proof (intro ext, goal_cases)

1312 case (1 xs ys)

1313 show ?case by (induction ys) (auto simp: sublist_Cons_right)

1314 qed

1316 private lemma list_emb_primrec:

1317 "list_emb = (\<lambda>uu uua uuaa. rec_list (\<lambda>P xs. List.null xs) (\<lambda>y ys ysa P xs. case xs of [] \<Rightarrow> True

1318 | x # xs \<Rightarrow> if P x y then ysa P xs else ysa P (x # xs)) uuaa uu uua)"

1319 proof (intro ext, goal_cases)

1320 case (1 P xs ys)

1321 show ?case

1322 by (induction ys arbitrary: xs)

1323 (auto simp: list_emb_code List.null_def split: list.splits)

1324 qed

1326 lemma prefix_transfer [transfer_rule]:

1327 assumes [transfer_rule]: "bi_unique A"

1328 shows "(list_all2 A ===> list_all2 A ===> (=)) prefix prefix"

1329 unfolding prefix_primrec by transfer_prover

1331 lemma suffix_transfer [transfer_rule]:

1332 assumes [transfer_rule]: "bi_unique A"

1333 shows "(list_all2 A ===> list_all2 A ===> (=)) suffix suffix"

1334 unfolding suffix_to_prefix [abs_def] by transfer_prover

1336 lemma sublist_transfer [transfer_rule]:

1337 assumes [transfer_rule]: "bi_unique A"

1338 shows "(list_all2 A ===> list_all2 A ===> (=)) sublist sublist"

1339 unfolding sublist_primrec by transfer_prover

1341 lemma parallel_transfer [transfer_rule]:

1342 assumes [transfer_rule]: "bi_unique A"

1343 shows "(list_all2 A ===> list_all2 A ===> (=)) parallel parallel"

1344 unfolding parallel_def by transfer_prover

1348 lemma list_emb_transfer [transfer_rule]:

1349 "((A ===> A ===> (=)) ===> list_all2 A ===> list_all2 A ===> (=)) list_emb list_emb"

1350 unfolding list_emb_primrec by transfer_prover

1352 lemma strict_prefix_transfer [transfer_rule]:

1353 assumes [transfer_rule]: "bi_unique A"

1354 shows "(list_all2 A ===> list_all2 A ===> (=)) strict_prefix strict_prefix"

1355 unfolding strict_prefix_def by transfer_prover

1357 lemma strict_suffix_transfer [transfer_rule]:

1358 assumes [transfer_rule]: "bi_unique A"

1359 shows "(list_all2 A ===> list_all2 A ===> (=)) strict_suffix strict_suffix"

1360 unfolding strict_suffix_def by transfer_prover

1362 lemma strict_subseq_transfer [transfer_rule]:

1363 assumes [transfer_rule]: "bi_unique A"

1364 shows "(list_all2 A ===> list_all2 A ===> (=)) strict_subseq strict_subseq"

1365 unfolding strict_subseq_def by transfer_prover

1367 lemma strict_sublist_transfer [transfer_rule]:

1368 assumes [transfer_rule]: "bi_unique A"

1369 shows "(list_all2 A ===> list_all2 A ===> (=)) strict_sublist strict_sublist"

1370 unfolding strict_sublist_def by transfer_prover

1372 lemma prefixes_transfer [transfer_rule]:

1373 assumes [transfer_rule]: "bi_unique A"

1374 shows "(list_all2 A ===> list_all2 (list_all2 A)) prefixes prefixes"

1375 unfolding prefixes_def by transfer_prover

1377 lemma suffixes_transfer [transfer_rule]:

1378 assumes [transfer_rule]: "bi_unique A"

1379 shows "(list_all2 A ===> list_all2 (list_all2 A)) suffixes suffixes"

1380 unfolding suffixes_def by transfer_prover

1382 lemma sublists_transfer [transfer_rule]:

1383 assumes [transfer_rule]: "bi_unique A"

1384 shows "(list_all2 A ===> list_all2 (list_all2 A)) sublists sublists"

1385 unfolding sublists_def by transfer_prover

1387 end

1389 end