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

src/HOL/Library/Sublist.thy

author | Christian Sternagel |

Wed Aug 29 12:23:14 2012 +0900 (2012-08-29) | |

changeset 49083 | 01081bca31b6 |

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

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

dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)

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_append2 [intro]:

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

438 by (induct zs) auto

440 lemma emb_prefix [intro]:

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

442 using assms

443 by (induct arbitrary: zs) auto

445 lemma emb_ConsD:

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

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

448 using assms

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

450 case emb_Cons thus ?case by (metis append_Cons)

451 next

452 case (emb_Cons2 x y xs ys)

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

454 qed

456 lemma emb_appendD:

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

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

459 using assms

460 proof (induction xs arbitrary: ys zs)

461 case Nil thus ?case by auto

462 next

463 case (Cons x xs)

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

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

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

467 qed

469 lemma emb_suffix:

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

471 shows "emb P xs zs"

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

474 lemma emb_suffixeq:

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

476 shows "emb P xs zs"

477 using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto

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

480 by (induct rule: emb.induct) auto

482 end