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

src/HOL/Library/Sublist.thy

author | Christian Sternagel |

Wed Aug 29 10:57:24 2012 +0900 (2012-08-29) | |

changeset 49081 | 092668a120cc |

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

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

changed arguement order of suffixeq (to facilitate reading "suffixeq xs ys" as "xs is a (possibly empty) suffix of ys)

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 instantiation list :: (type) "{order, bot}"

14 begin

16 definition

17 prefixeq_def: "xs \<le> ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"

19 definition

20 prefix_def: "xs < ys \<longleftrightarrow> xs \<le> ys \<and> xs \<noteq> (ys::'a list)"

22 definition

23 "bot = []"

25 instance proof

26 qed (auto simp add: prefixeq_def prefix_def bot_list_def)

28 end

30 lemma prefixeqI [intro?]: "ys = xs @ zs ==> xs \<le> ys"

31 unfolding prefixeq_def by blast

33 lemma prefixeqE [elim?]:

34 assumes "xs \<le> ys"

35 obtains zs where "ys = xs @ zs"

36 using assms unfolding prefixeq_def by blast

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

39 unfolding prefix_def prefixeq_def by blast

41 lemma prefixE' [elim?]:

42 assumes "xs < ys"

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

44 proof -

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

46 unfolding prefix_def prefixeq_def by blast

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

48 qed

50 lemma prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"

51 unfolding prefix_def by blast

53 lemma prefixE [elim?]:

54 fixes xs ys :: "'a list"

55 assumes "xs < ys"

56 obtains "xs \<le> ys" and "xs \<noteq> ys"

57 using assms unfolding prefix_def by blast

60 subsection {* Basic properties of prefixes *}

62 theorem Nil_prefixeq [iff]: "[] \<le> xs"

63 by (simp add: prefixeq_def)

65 theorem prefixeq_Nil [simp]: "(xs \<le> []) = (xs = [])"

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

68 lemma prefixeq_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"

69 proof

70 assume "xs \<le> ys @ [y]"

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

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

73 by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)

74 next

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

76 then show "xs \<le> ys @ [y]"

77 by (metis order_eq_iff order_trans prefixeqI)

78 qed

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

81 by (auto simp add: prefixeq_def)

83 lemma less_eq_list_code [code]:

84 "([]\<Colon>'a\<Colon>{equal, ord} list) \<le> xs \<longleftrightarrow> True"

85 "(x\<Colon>'a\<Colon>{equal, ord}) # xs \<le> [] \<longleftrightarrow> False"

86 "(x\<Colon>'a\<Colon>{equal, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"

87 by simp_all

89 lemma same_prefixeq_prefixeq [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"

90 by (induct xs) simp_all

92 lemma same_prefixeq_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"

93 by (metis append_Nil2 append_self_conv order_eq_iff prefixeqI)

95 lemma prefixeq_prefixeq [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"

96 by (metis order_le_less_trans prefixeqI prefixE prefixI)

98 lemma append_prefixeqD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"

99 by (auto simp add: prefixeq_def)

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

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

104 theorem prefixeq_append:

105 "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> 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_prefixeq:

113 "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"

114 unfolding prefixeq_def

115 by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj

116 eq_Nil_appendI nth_drop')

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

119 by (auto simp add: prefixeq_def)

121 lemma prefixeq_same_cases:

122 "(xs\<^isub>1::'a list) \<le> ys \<Longrightarrow> xs\<^isub>2 \<le> ys \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1"

123 unfolding prefixeq_def by (metis append_eq_append_conv2)

125 lemma set_mono_prefixeq: "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"

126 by (auto simp add: prefixeq_def)

128 lemma take_is_prefixeq: "take n xs \<le> xs"

129 unfolding prefixeq_def by (metis append_take_drop_id)

131 lemma map_prefixeqI: "xs \<le> ys \<Longrightarrow> map f xs \<le> map f ys"

132 by (auto simp: prefixeq_def)

134 lemma prefixeq_length_less: "xs < ys \<Longrightarrow> length xs < length ys"

135 by (auto simp: prefix_def prefixeq_def)

137 lemma prefix_simps [simp, code]:

138 "xs < [] \<longleftrightarrow> False"

139 "[] < x # xs \<longleftrightarrow> True"

140 "x # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"

141 by (simp_all add: prefix_def cong: conj_cong)

143 lemma take_prefix: "xs < ys \<Longrightarrow> take n xs < ys"

144 apply (induct n arbitrary: xs ys)

145 apply (case_tac ys, simp_all)[1]

146 apply (metis order_less_trans prefixI take_is_prefixeq)

147 done

149 lemma not_prefixeq_cases:

150 assumes pfx: "\<not> ps \<le> ls"

151 obtains

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

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

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

155 proof (cases ps)

156 case Nil then show ?thesis using pfx by simp

157 next

158 case (Cons a as)

159 note c = `ps = a#as`

160 show ?thesis

161 proof (cases ls)

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

163 next

164 case (Cons x xs)

165 show ?thesis

166 proof (cases "x = a")

167 case True

168 have "\<not> as \<le> xs" using pfx c Cons True by simp

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

170 next

171 case False

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

173 qed

174 qed

175 qed

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

178 assumes np: "\<not> ps \<le> ls"

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

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

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

182 shows "P ps ls" using np

183 proof (induct ls arbitrary: ps)

184 case Nil then show ?case

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

186 next

187 case (Cons y ys)

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

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

190 by (rule not_prefixeq_cases) auto

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

192 qed

195 subsection {* Parallel lists *}

197 definition

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

199 "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)"

201 lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"

202 unfolding parallel_def by blast

204 lemma parallelE [elim]:

205 assumes "xs \<parallel> ys"

206 obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs"

207 using assms unfolding parallel_def by blast

209 theorem prefixeq_cases:

210 obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys"

211 unfolding parallel_def prefix_def by blast

213 theorem parallel_decomp:

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

215 proof (induct xs rule: rev_induct)

216 case Nil

217 then have False by auto

218 then show ?case ..

219 next

220 case (snoc x xs)

221 show ?case

222 proof (rule prefixeq_cases)

223 assume le: "xs \<le> ys"

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

225 show ?thesis

226 proof (cases ys')

227 assume "ys' = []"

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

229 next

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

231 then show ?thesis

232 by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI

233 same_prefixeq_prefixeq snoc.prems ys)

234 qed

235 next

236 assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: prefix_def)

237 with snoc have False by blast

238 then show ?thesis ..

239 next

240 assume "xs \<parallel> ys"

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

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

243 by blast

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

245 with neq ys show ?thesis by blast

246 qed

247 qed

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

250 apply (rule parallelI)

251 apply (erule parallelE, erule conjE,

252 induct rule: not_prefixeq_induct, simp+)+

253 done

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

256 by (simp add: parallel_append)

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

259 unfolding parallel_def by auto

262 subsection {* Suffix order on lists *}

264 definition

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

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

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

269 unfolding suffixeq_def by blast

271 lemma suffixeqE [elim?]:

272 assumes "suffixeq xs ys"

273 obtains zs where "ys = zs @ xs"

274 using assms unfolding suffixeq_def by blast

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

277 by (auto simp add: suffixeq_def)

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

279 by (auto simp add: suffixeq_def)

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

281 by (auto simp add: suffixeq_def)

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

284 by (simp add: suffixeq_def)

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

286 by (auto simp add: suffixeq_def)

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

289 by (auto simp add: suffixeq_def)

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

291 by (auto simp add: suffixeq_def)

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

294 by (auto simp add: suffixeq_def)

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

296 by (auto simp add: suffixeq_def)

298 lemma suffixeq_is_subset: "suffixeq xs ys ==> set xs \<subseteq> set ys"

299 proof -

300 assume "suffixeq xs ys"

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

302 then show ?thesis by (induct zs) auto

303 qed

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

306 proof -

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

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

309 then show ?thesis

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

311 qed

313 lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> rev xs \<le> rev ys"

314 proof

315 assume "suffixeq xs ys"

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

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

318 then show "rev xs <= rev ys" ..

319 next

320 assume "rev xs <= rev ys"

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

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

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

324 then show "suffixeq xs ys" ..

325 qed

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

328 by (clarsimp elim!: suffixeqE)

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

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

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

334 unfolding suffixeq_def

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

336 apply simp

337 done

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

340 by (clarsimp elim!: suffixeqE)

342 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"

343 by blast

345 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"

346 by blast

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

349 unfolding parallel_def by simp

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

352 unfolding parallel_def by simp

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

355 by auto

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

358 by (metis Cons_prefixeq_Cons parallelE parallelI)

360 lemma not_equal_is_parallel:

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

362 and len: "length xs = length ys"

363 shows "xs \<parallel> ys"

364 using len neq

365 proof (induct rule: list_induct2)

366 case Nil

367 then show ?case by simp

368 next

369 case (Cons a as b bs)

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

371 show ?case

372 proof (cases "a = b")

373 case True

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

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

376 next

377 case False

378 then show ?thesis by (rule Cons_parallelI1)

379 qed

380 qed

383 subsection {* Embedding on lists *}

385 inductive

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

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

388 where

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

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

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

393 lemma emb_Nil2 [simp]:

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

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

397 lemma emb_append2 [intro]:

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

399 by (induct zs) auto

401 lemma emb_prefix [intro]:

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

403 using assms

404 by (induct arbitrary: zs) auto

406 lemma emb_ConsD:

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

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

409 using assms

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

411 case emb_Cons thus ?case by (metis append_Cons)

412 next

413 case (emb_Cons2 x y xs ys)

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

415 qed

417 lemma emb_appendD:

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

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

420 using assms

421 proof (induction xs arbitrary: ys zs)

422 case Nil thus ?case by auto

423 next

424 case (Cons x xs)

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

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

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

428 qed

430 end