Christian@49087: (* Title: HOL/Library/Sublist.thy wenzelm@10330: Author: Tobias Nipkow and Markus Wenzel, TU Muenchen Christian@49087: Author: Christian Sternagel, JAIST wenzelm@10330: *) wenzelm@10330: traytel@55579: header {* List prefixes, suffixes, and homeomorphic embedding *} wenzelm@10330: Christian@49087: theory Sublist Christian@49087: imports Main nipkow@15131: begin wenzelm@10330: traytel@55579: subsection {* Prefix order on lists *} traytel@55579: traytel@55579: definition prefixeq :: "'a list \ 'a list \ bool" traytel@55579: where "prefixeq xs ys \ (\zs. ys = xs @ zs)" traytel@55579: traytel@55579: definition prefix :: "'a list \ 'a list \ bool" traytel@55579: where "prefix xs ys \ prefixeq xs ys \ xs \ ys" traytel@55579: traytel@55579: interpretation prefix_order: order prefixeq prefix traytel@55579: by default (auto simp: prefixeq_def prefix_def) traytel@55579: traytel@55579: interpretation prefix_bot: order_bot Nil prefixeq prefix traytel@55579: by default (simp add: prefixeq_def) traytel@55579: traytel@55579: lemma prefixeqI [intro?]: "ys = xs @ zs \ prefixeq xs ys" traytel@55579: unfolding prefixeq_def by blast traytel@55579: traytel@55579: lemma prefixeqE [elim?]: traytel@55579: assumes "prefixeq xs ys" traytel@55579: obtains zs where "ys = xs @ zs" traytel@55579: using assms unfolding prefixeq_def by blast traytel@55579: traytel@55579: lemma prefixI' [intro?]: "ys = xs @ z # zs \ prefix xs ys" traytel@55579: unfolding prefix_def prefixeq_def by blast traytel@55579: traytel@55579: lemma prefixE' [elim?]: traytel@55579: assumes "prefix xs ys" traytel@55579: obtains z zs where "ys = xs @ z # zs" traytel@55579: proof - traytel@55579: from `prefix xs ys` obtain us where "ys = xs @ us" and "xs \ ys" traytel@55579: unfolding prefix_def prefixeq_def by blast traytel@55579: with that show ?thesis by (auto simp add: neq_Nil_conv) traytel@55579: qed traytel@55579: traytel@55579: lemma prefixI [intro?]: "prefixeq xs ys \ xs \ ys \ prefix xs ys" traytel@55579: unfolding prefix_def by blast traytel@55579: traytel@55579: lemma prefixE [elim?]: traytel@55579: fixes xs ys :: "'a list" traytel@55579: assumes "prefix xs ys" traytel@55579: obtains "prefixeq xs ys" and "xs \ ys" traytel@55579: using assms unfolding prefix_def by blast traytel@55579: traytel@55579: traytel@55579: subsection {* Basic properties of prefixes *} traytel@55579: traytel@55579: theorem Nil_prefixeq [iff]: "prefixeq [] xs" traytel@55579: by (simp add: prefixeq_def) traytel@55579: traytel@55579: theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])" traytel@55579: by (induct xs) (simp_all add: prefixeq_def) traytel@55579: traytel@55579: lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \ xs = ys @ [y] \ prefixeq xs ys" traytel@55579: proof traytel@55579: assume "prefixeq xs (ys @ [y])" traytel@55579: then obtain zs where zs: "ys @ [y] = xs @ zs" .. traytel@55579: show "xs = ys @ [y] \ prefixeq xs ys" traytel@55579: by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs) traytel@55579: next traytel@55579: assume "xs = ys @ [y] \ prefixeq xs ys" traytel@55579: then show "prefixeq xs (ys @ [y])" traytel@55579: by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI) traytel@55579: qed traytel@55579: traytel@55579: lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \ prefixeq xs ys)" traytel@55579: by (auto simp add: prefixeq_def) traytel@55579: traytel@55579: lemma prefixeq_code [code]: traytel@55579: "prefixeq [] xs \ True" traytel@55579: "prefixeq (x # xs) [] \ False" traytel@55579: "prefixeq (x # xs) (y # ys) \ x = y \ prefixeq xs ys" traytel@55579: by simp_all traytel@55579: traytel@55579: lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs" traytel@55579: by (induct xs) simp_all traytel@55579: traytel@55579: lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])" traytel@55579: by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI) traytel@55579: traytel@55579: lemma prefixeq_prefixeq [simp]: "prefixeq xs ys \ prefixeq xs (ys @ zs)" traytel@55579: by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI) traytel@55579: traytel@55579: lemma append_prefixeqD: "prefixeq (xs @ ys) zs \ prefixeq xs zs" traytel@55579: by (auto simp add: prefixeq_def) traytel@55579: traytel@55579: theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \ (\zs. xs = y # zs \ prefixeq zs ys))" traytel@55579: by (cases xs) (auto simp add: prefixeq_def) traytel@55579: traytel@55579: theorem prefixeq_append: traytel@55579: "prefixeq xs (ys @ zs) = (prefixeq xs ys \ (\us. xs = ys @ us \ prefixeq us zs))" traytel@55579: apply (induct zs rule: rev_induct) traytel@55579: apply force traytel@55579: apply (simp del: append_assoc add: append_assoc [symmetric]) traytel@55579: apply (metis append_eq_appendI) traytel@55579: done traytel@55579: traytel@55579: lemma append_one_prefixeq: traytel@55579: "prefixeq xs ys \ length xs < length ys \ prefixeq (xs @ [ys ! length xs]) ys" traytel@55579: proof (unfold prefixeq_def) traytel@55579: assume a1: "\zs. ys = xs @ zs" traytel@55579: then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce traytel@55579: assume a2: "length xs < length ys" traytel@55579: have f1: "\v. ([]\'a list) @ v = v" using append_Nil2 by simp traytel@55579: have "[] \ sk" using a1 a2 sk less_not_refl by force traytel@55579: hence "\v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl) traytel@55579: thus "\zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce traytel@55579: qed traytel@55579: traytel@55579: theorem prefixeq_length_le: "prefixeq xs ys \ length xs \ length ys" traytel@55579: by (auto simp add: prefixeq_def) traytel@55579: traytel@55579: lemma prefixeq_same_cases: traytel@55579: "prefixeq (xs\<^sub>1::'a list) ys \ prefixeq xs\<^sub>2 ys \ prefixeq xs\<^sub>1 xs\<^sub>2 \ prefixeq xs\<^sub>2 xs\<^sub>1" traytel@55579: unfolding prefixeq_def by (force simp: append_eq_append_conv2) traytel@55579: traytel@55579: lemma set_mono_prefixeq: "prefixeq xs ys \ set xs \ set ys" traytel@55579: by (auto simp add: prefixeq_def) traytel@55579: traytel@55579: lemma take_is_prefixeq: "prefixeq (take n xs) xs" traytel@55579: unfolding prefixeq_def by (metis append_take_drop_id) traytel@55579: traytel@55579: lemma map_prefixeqI: "prefixeq xs ys \ prefixeq (map f xs) (map f ys)" traytel@55579: by (auto simp: prefixeq_def) traytel@55579: traytel@55579: lemma prefixeq_length_less: "prefix xs ys \ length xs < length ys" traytel@55579: by (auto simp: prefix_def prefixeq_def) traytel@55579: traytel@55579: lemma prefix_simps [simp, code]: traytel@55579: "prefix xs [] \ False" traytel@55579: "prefix [] (x # xs) \ True" traytel@55579: "prefix (x # xs) (y # ys) \ x = y \ prefix xs ys" traytel@55579: by (simp_all add: prefix_def cong: conj_cong) traytel@55579: traytel@55579: lemma take_prefix: "prefix xs ys \ prefix (take n xs) ys" traytel@55579: apply (induct n arbitrary: xs ys) traytel@55579: apply (case_tac ys, simp_all)[1] traytel@55579: apply (metis prefix_order.less_trans prefixI take_is_prefixeq) traytel@55579: done traytel@55579: traytel@55579: lemma not_prefixeq_cases: traytel@55579: assumes pfx: "\ prefixeq ps ls" traytel@55579: obtains traytel@55579: (c1) "ps \ []" and "ls = []" traytel@55579: | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\ prefixeq as xs" traytel@55579: | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \ a" traytel@55579: proof (cases ps) traytel@55579: case Nil traytel@55579: then show ?thesis using pfx by simp traytel@55579: next traytel@55579: case (Cons a as) traytel@55579: note c = `ps = a#as` traytel@55579: show ?thesis traytel@55579: proof (cases ls) traytel@55579: case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil) traytel@55579: next traytel@55579: case (Cons x xs) traytel@55579: show ?thesis traytel@55579: proof (cases "x = a") traytel@55579: case True traytel@55579: have "\ prefixeq as xs" using pfx c Cons True by simp traytel@55579: with c Cons True show ?thesis by (rule c2) traytel@55579: next traytel@55579: case False traytel@55579: with c Cons show ?thesis by (rule c3) traytel@55579: qed traytel@55579: qed traytel@55579: qed traytel@55579: traytel@55579: lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]: traytel@55579: assumes np: "\ prefixeq ps ls" traytel@55579: and base: "\x xs. P (x#xs) []" traytel@55579: and r1: "\x xs y ys. x \ y \ P (x#xs) (y#ys)" traytel@55579: and r2: "\x xs y ys. \ x = y; \ prefixeq xs ys; P xs ys \ \ P (x#xs) (y#ys)" traytel@55579: shows "P ps ls" using np traytel@55579: proof (induct ls arbitrary: ps) traytel@55579: case Nil then show ?case traytel@55579: by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base) traytel@55579: next traytel@55579: case (Cons y ys) traytel@55579: then have npfx: "\ prefixeq ps (y # ys)" by simp traytel@55579: then obtain x xs where pv: "ps = x # xs" traytel@55579: by (rule not_prefixeq_cases) auto traytel@55579: show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2) traytel@55579: qed traytel@55579: traytel@55579: wenzelm@10389: subsection {* Parallel lists *} wenzelm@10389: Christian@50516: definition parallel :: "'a list \ 'a list \ bool" (infixl "\" 50) wenzelm@49107: where "(xs \ ys) = (\ prefixeq xs ys \ \ prefixeq ys xs)" wenzelm@10389: Christian@50516: lemma parallelI [intro]: "\ prefixeq xs ys \ \ prefixeq ys xs \ xs \ ys" wenzelm@25692: unfolding parallel_def by blast wenzelm@10330: wenzelm@10389: lemma parallelE [elim]: wenzelm@25692: assumes "xs \ ys" Christian@49087: obtains "\ prefixeq xs ys \ \ prefixeq ys xs" wenzelm@25692: using assms unfolding parallel_def by blast wenzelm@10330: Christian@49087: theorem prefixeq_cases: Christian@49087: obtains "prefixeq xs ys" | "prefix ys xs" | "xs \ ys" Christian@49087: unfolding parallel_def prefix_def by blast wenzelm@10330: wenzelm@10389: theorem parallel_decomp: Christian@50516: "xs \ ys \ \as b bs c cs. b \ c \ xs = as @ b # bs \ ys = as @ c # cs" wenzelm@10408: proof (induct xs rule: rev_induct) wenzelm@11987: case Nil wenzelm@23254: then have False by auto wenzelm@23254: then show ?case .. wenzelm@10408: next wenzelm@11987: case (snoc x xs) wenzelm@11987: show ?case Christian@49087: proof (rule prefixeq_cases) Christian@49087: assume le: "prefixeq xs ys" wenzelm@10408: then obtain ys' where ys: "ys = xs @ ys'" .. wenzelm@10408: show ?thesis wenzelm@10408: proof (cases ys') nipkow@25564: assume "ys' = []" Christian@49087: then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys) wenzelm@10389: next wenzelm@10408: fix c cs assume ys': "ys' = c # cs" blanchet@54483: have "x \ c" using snoc.prems ys ys' by fastforce blanchet@54483: thus "\as b bs c cs. b \ c \ xs @ [x] = as @ b # bs \ ys = as @ c # cs" blanchet@54483: using ys ys' by blast wenzelm@10389: qed wenzelm@10408: next wenzelm@49107: assume "prefix ys xs" wenzelm@49107: then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def) wenzelm@11987: with snoc have False by blast wenzelm@23254: then show ?thesis .. wenzelm@10408: next wenzelm@10408: assume "xs \ ys" wenzelm@11987: with snoc obtain as b bs c cs where neq: "(b::'a) \ c" wenzelm@10408: and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" wenzelm@10408: by blast wenzelm@10408: from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp wenzelm@10408: with neq ys show ?thesis by blast wenzelm@10389: qed wenzelm@10389: qed wenzelm@10330: nipkow@25564: lemma parallel_append: "a \ b \ a @ c \ b @ d" wenzelm@25692: apply (rule parallelI) wenzelm@25692: apply (erule parallelE, erule conjE, Christian@49087: induct rule: not_prefixeq_induct, simp+)+ wenzelm@25692: done kleing@25299: wenzelm@25692: lemma parallel_appendI: "xs \ ys \ x = xs @ xs' \ y = ys @ ys' \ x \ y" wenzelm@25692: by (simp add: parallel_append) kleing@25299: wenzelm@25692: lemma parallel_commute: "a \ b \ b \ a" wenzelm@25692: unfolding parallel_def by auto oheimb@14538: wenzelm@25356: Christian@49087: subsection {* Suffix order on lists *} wenzelm@17201: wenzelm@49107: definition suffixeq :: "'a list \ 'a list \ bool" wenzelm@49107: where "suffixeq xs ys = (\zs. ys = zs @ xs)" Christian@49087: wenzelm@49107: definition suffix :: "'a list \ 'a list \ bool" wenzelm@49107: where "suffix xs ys \ (\us. ys = us @ xs \ us \ [])" oheimb@14538: Christian@49087: lemma suffix_imp_suffixeq: Christian@49087: "suffix xs ys \ suffixeq xs ys" Christian@49087: by (auto simp: suffixeq_def suffix_def) Christian@49087: Christian@50516: lemma suffixeqI [intro?]: "ys = zs @ xs \ suffixeq xs ys" Christian@49087: unfolding suffixeq_def by blast wenzelm@21305: Christian@49087: lemma suffixeqE [elim?]: Christian@49087: assumes "suffixeq xs ys" Christian@49087: obtains zs where "ys = zs @ xs" Christian@49087: using assms unfolding suffixeq_def by blast wenzelm@21305: Christian@49087: lemma suffixeq_refl [iff]: "suffixeq xs xs" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: lemma suffix_trans: Christian@49087: "suffix xs ys \ suffix ys zs \ suffix xs zs" Christian@49087: by (auto simp: suffix_def) Christian@49087: lemma suffixeq_trans: "\suffixeq xs ys; suffixeq ys zs\ \ suffixeq xs zs" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: lemma suffixeq_antisym: "\suffixeq xs ys; suffixeq ys xs\ \ xs = ys" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: Christian@49087: lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs" Christian@49087: by (induct xs) (auto simp: suffixeq_def) oheimb@14538: Christian@49087: lemma suffix_tl [simp]: "xs \ [] \ suffix (tl xs) xs" Christian@49087: by (induct xs) (auto simp: suffix_def) oheimb@14538: Christian@49087: lemma Nil_suffixeq [iff]: "suffixeq [] xs" Christian@49087: by (simp add: suffixeq_def) Christian@49087: lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: wenzelm@49107: lemma suffixeq_ConsI: "suffixeq xs ys \ suffixeq xs (y # ys)" Christian@49087: by (auto simp add: suffixeq_def) wenzelm@49107: lemma suffixeq_ConsD: "suffixeq (x # xs) ys \ suffixeq xs ys" Christian@49087: by (auto simp add: suffixeq_def) oheimb@14538: Christian@49087: lemma suffixeq_appendI: "suffixeq xs ys \ suffixeq xs (zs @ ys)" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \ suffixeq xs ys" Christian@49087: by (auto simp add: suffixeq_def) Christian@49087: Christian@49087: lemma suffix_set_subset: Christian@49087: "suffix xs ys \ set xs \ set ys" by (auto simp: suffix_def) oheimb@14538: Christian@49087: lemma suffixeq_set_subset: Christian@49087: "suffixeq xs ys \ set xs \ set ys" by (auto simp: suffixeq_def) Christian@49087: wenzelm@49107: lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \ suffixeq xs ys" wenzelm@21305: proof - wenzelm@49107: assume "suffixeq (x # xs) (y # ys)" wenzelm@49107: then obtain zs where "y # ys = zs @ x # xs" .. Christian@49087: then show ?thesis Christian@49087: by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI) wenzelm@21305: qed oheimb@14538: Christian@49087: lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \ prefixeq (rev xs) (rev ys)" Christian@49087: proof Christian@49087: assume "suffixeq xs ys" Christian@49087: then obtain zs where "ys = zs @ xs" .. Christian@49087: then have "rev ys = rev xs @ rev zs" by simp Christian@49087: then show "prefixeq (rev xs) (rev ys)" .. Christian@49087: next Christian@49087: assume "prefixeq (rev xs) (rev ys)" Christian@49087: then obtain zs where "rev ys = rev xs @ zs" .. Christian@49087: then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp Christian@49087: then have "ys = rev zs @ xs" by simp Christian@49087: then show "suffixeq xs ys" .. wenzelm@21305: qed oheimb@14538: Christian@49087: lemma distinct_suffixeq: "distinct ys \ suffixeq xs ys \ distinct xs" Christian@49087: by (clarsimp elim!: suffixeqE) wenzelm@17201: Christian@49087: lemma suffixeq_map: "suffixeq xs ys \ suffixeq (map f xs) (map f ys)" Christian@49087: by (auto elim!: suffixeqE intro: suffixeqI) kleing@25299: Christian@49087: lemma suffixeq_drop: "suffixeq (drop n as) as" Christian@49087: unfolding suffixeq_def wenzelm@25692: apply (rule exI [where x = "take n as"]) wenzelm@25692: apply simp wenzelm@25692: done kleing@25299: Christian@49087: lemma suffixeq_take: "suffixeq xs ys \ ys = take (length ys - length xs) ys @ xs" wenzelm@49107: by (auto elim!: suffixeqE) kleing@25299: wenzelm@49107: lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>=" Christian@49087: proof (intro ext iffI) Christian@49087: fix xs ys :: "'a list" Christian@49087: assume "suffixeq xs ys" Christian@49087: show "suffix\<^sup>=\<^sup>= xs ys" Christian@49087: proof Christian@49087: assume "xs \ ys" wenzelm@49107: with `suffixeq xs ys` show "suffix xs ys" wenzelm@49107: by (auto simp: suffixeq_def suffix_def) Christian@49087: qed Christian@49087: next Christian@49087: fix xs ys :: "'a list" Christian@49087: assume "suffix\<^sup>=\<^sup>= xs ys" wenzelm@49107: then show "suffixeq xs ys" Christian@49087: proof wenzelm@49107: assume "suffix xs ys" then show "suffixeq xs ys" wenzelm@49107: by (rule suffix_imp_suffixeq) Christian@49087: next wenzelm@49107: assume "xs = ys" then show "suffixeq xs ys" wenzelm@49107: by (auto simp: suffixeq_def) Christian@49087: qed Christian@49087: qed Christian@49087: Christian@49087: lemma parallelD1: "x \ y \ \ prefixeq x y" wenzelm@25692: by blast kleing@25299: Christian@49087: lemma parallelD2: "x \ y \ \ prefixeq y x" wenzelm@25692: by blast wenzelm@25355: wenzelm@25355: lemma parallel_Nil1 [simp]: "\ x \ []" wenzelm@25692: unfolding parallel_def by simp wenzelm@25355: kleing@25299: lemma parallel_Nil2 [simp]: "\ [] \ x" wenzelm@25692: unfolding parallel_def by simp kleing@25299: nipkow@25564: lemma Cons_parallelI1: "a \ b \ a # as \ b # bs" wenzelm@25692: by auto kleing@25299: nipkow@25564: lemma Cons_parallelI2: "\ a = b; as \ bs \ \ a # as \ b # bs" Christian@49087: by (metis Cons_prefixeq_Cons parallelE parallelI) nipkow@25665: kleing@25299: lemma not_equal_is_parallel: kleing@25299: assumes neq: "xs \ ys" wenzelm@25356: and len: "length xs = length ys" wenzelm@25356: shows "xs \ ys" kleing@25299: using len neq wenzelm@25355: proof (induct rule: list_induct2) haftmann@26445: case Nil wenzelm@25356: then show ?case by simp kleing@25299: next haftmann@26445: case (Cons a as b bs) wenzelm@25355: have ih: "as \ bs \ as \ bs" by fact kleing@25299: show ?case kleing@25299: proof (cases "a = b") wenzelm@25355: case True haftmann@26445: then have "as \ bs" using Cons by simp wenzelm@25355: then show ?thesis by (rule Cons_parallelI2 [OF True ih]) kleing@25299: next kleing@25299: case False wenzelm@25355: then show ?thesis by (rule Cons_parallelI1) kleing@25299: qed kleing@25299: qed haftmann@22178: wenzelm@49107: lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq" Christian@49087: by (intro ext) (auto simp: suffixeq_def suffix_def) Christian@49087: wenzelm@49107: lemma suffix_lists: "suffix xs ys \ ys \ lists A \ xs \ lists A" Christian@49087: unfolding suffix_def by auto Christian@49087: Christian@49087: Christian@50516: subsection {* Homeomorphic embedding on lists *} Christian@49087: Christian@57497: inductive list_emb :: "('a \ 'a \ bool) \ 'a list \ 'a list \ bool" Christian@49087: for P :: "('a \ 'a \ bool)" Christian@49087: where Christian@57497: list_emb_Nil [intro, simp]: "list_emb P [] ys" Christian@57497: | list_emb_Cons [intro] : "list_emb P xs ys \ list_emb P xs (y#ys)" Christian@57498: | list_emb_Cons2 [intro]: "P x y \ list_emb P xs ys \ list_emb P (x#xs) (y#ys)" Christian@50516: Christian@57499: lemma list_emb_mono: Christian@57499: assumes "\x y. P x y \ Q x y" Christian@57499: shows "list_emb P xs ys \ list_emb Q xs ys" Christian@57499: proof Christian@57499: assume "list_emb P xs ys" Christian@57499: then show "list_emb Q xs ys" by (induct) (auto simp: assms) Christian@57499: qed Christian@57499: Christian@57497: lemma list_emb_Nil2 [simp]: Christian@57497: assumes "list_emb P xs []" shows "xs = []" Christian@57497: using assms by (cases rule: list_emb.cases) auto Christian@49087: Christian@57498: lemma list_emb_refl: Christian@57498: assumes "\x. x \ set xs \ P x x" Christian@57498: shows "list_emb P xs xs" Christian@57498: using assms by (induct xs) auto Christian@49087: Christian@57497: lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False" Christian@49087: proof - Christian@57497: { assume "list_emb P (x#xs) []" Christian@57497: from list_emb_Nil2 [OF this] have False by simp Christian@49087: } moreover { Christian@49087: assume False Christian@57497: then have "list_emb P (x#xs) []" by simp Christian@49087: } ultimately show ?thesis by blast Christian@49087: qed Christian@49087: Christian@57497: lemma list_emb_append2 [intro]: "list_emb P xs ys \ list_emb P xs (zs @ ys)" Christian@49087: by (induct zs) auto Christian@49087: Christian@57497: lemma list_emb_prefix [intro]: Christian@57497: assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)" Christian@49087: using assms Christian@49087: by (induct arbitrary: zs) auto Christian@49087: Christian@57497: lemma list_emb_ConsD: Christian@57497: assumes "list_emb P (x#xs) ys" Christian@57498: shows "\us v vs. ys = us @ v # vs \ P x v \ list_emb P xs vs" Christian@49087: using assms wenzelm@49107: proof (induct x \ "x # xs" ys arbitrary: x xs) Christian@57497: case list_emb_Cons wenzelm@49107: then show ?case by (metis append_Cons) Christian@49087: next Christian@57497: case (list_emb_Cons2 x y xs ys) blanchet@54483: then show ?case by blast Christian@49087: qed Christian@49087: Christian@57497: lemma list_emb_appendD: Christian@57497: assumes "list_emb P (xs @ ys) zs" Christian@57497: shows "\us vs. zs = us @ vs \ list_emb P xs us \ list_emb P ys vs" Christian@49087: using assms Christian@49087: proof (induction xs arbitrary: ys zs) wenzelm@49107: case Nil then show ?case by auto Christian@49087: next Christian@49087: case (Cons x xs) blanchet@54483: then obtain us v vs where Christian@57498: zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs" Christian@57497: by (auto dest: list_emb_ConsD) blanchet@54483: obtain sk\<^sub>0 :: "'a list \ 'a list \ 'a list" and sk\<^sub>1 :: "'a list \ 'a list \ 'a list" where Christian@57497: sk: "\x\<^sub>0 x\<^sub>1. \ list_emb P (xs @ x\<^sub>0) x\<^sub>1 \ sk\<^sub>0 x\<^sub>0 x\<^sub>1 @ sk\<^sub>1 x\<^sub>0 x\<^sub>1 = x\<^sub>1 \ list_emb P xs (sk\<^sub>0 x\<^sub>0 x\<^sub>1) \ list_emb P x\<^sub>0 (sk\<^sub>1 x\<^sub>0 x\<^sub>1)" blanchet@54483: using Cons(1) by (metis (no_types)) Christian@57497: hence "\x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto blanchet@54483: thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc) Christian@49087: qed Christian@49087: Christian@57497: lemma list_emb_suffix: Christian@57497: assumes "list_emb P xs ys" and "suffix ys zs" Christian@57497: shows "list_emb P xs zs" Christian@57497: using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: suffix_def) Christian@49087: Christian@57497: lemma list_emb_suffixeq: Christian@57497: assumes "list_emb P xs ys" and "suffixeq ys zs" Christian@57497: shows "list_emb P xs zs" Christian@57497: using assms and list_emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto Christian@49087: Christian@57497: lemma list_emb_length: "list_emb P xs ys \ length xs \ length ys" Christian@57497: by (induct rule: list_emb.induct) auto Christian@49087: Christian@57497: lemma list_emb_trans: Christian@50516: assumes "\x y z. \x \ A; y \ A; z \ A; P x y; P y z\ \ P x z" Christian@50516: shows "\xs ys zs. \xs \ lists A; ys \ lists A; zs \ lists A; Christian@57497: list_emb P xs ys; list_emb P ys zs\ \ list_emb P xs zs" Christian@50516: proof - Christian@49087: fix xs ys zs Christian@57497: assume "list_emb P xs ys" and "list_emb P ys zs" Christian@49087: and "xs \ lists A" and "ys \ lists A" and "zs \ lists A" Christian@57497: then show "list_emb P xs zs" Christian@49087: proof (induction arbitrary: zs) Christian@57497: case list_emb_Nil show ?case by blast Christian@49087: next Christian@57497: case (list_emb_Cons xs ys y) Christian@57497: from list_emb_ConsD [OF `list_emb P (y#ys) zs`] obtain us v vs Christian@57498: where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast Christian@57497: then have "list_emb P ys (v#vs)" by blast Christian@57497: then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2) Christian@57497: from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by simp Christian@49087: next Christian@57497: case (list_emb_Cons2 x y xs ys) Christian@57497: from list_emb_ConsD [OF `list_emb P (y#ys) zs`] obtain us v vs Christian@57498: where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast Christian@57497: with list_emb_Cons2 have "list_emb P xs vs" by simp Christian@57498: moreover have "P x v" Christian@49087: proof - Christian@49087: from zs and `zs \ lists A` have "v \ A" by auto Christian@57497: moreover have "x \ A" and "y \ A" using list_emb_Cons2 by simp_all Christian@50516: ultimately show ?thesis Christian@57498: using `P x y` and `P y v` and assms Christian@50516: by blast Christian@49087: qed Christian@57497: ultimately have "list_emb P (x#xs) (v#vs)" by blast Christian@57497: then show ?case unfolding zs by (rule list_emb_append2) Christian@49087: qed Christian@49087: qed Christian@49087: Christian@49087: Christian@50516: subsection {* Sublists (special case of homeomorphic embedding) *} Christian@49087: Christian@50516: abbreviation sublisteq :: "'a list \ 'a list \ bool" Christian@57497: where "sublisteq xs ys \ list_emb (op =) xs ys" Christian@49087: Christian@50516: lemma sublisteq_Cons2: "sublisteq xs ys \ sublisteq (x#xs) (x#ys)" by auto Christian@49087: Christian@50516: lemma sublisteq_same_length: Christian@50516: assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys" Christian@57497: using assms by (induct) (auto dest: list_emb_length) Christian@49087: Christian@50516: lemma not_sublisteq_length [simp]: "length ys < length xs \ \ sublisteq xs ys" Christian@57497: by (metis list_emb_length linorder_not_less) Christian@49087: Christian@49087: lemma [code]: Christian@57497: "list_emb P [] ys \ True" Christian@57497: "list_emb P (x#xs) [] \ False" Christian@49087: by (simp_all) Christian@49087: Christian@50516: lemma sublisteq_Cons': "sublisteq (x#xs) ys \ sublisteq xs ys" Christian@57497: by (induct xs, simp, blast dest: list_emb_ConsD) Christian@49087: Christian@50516: lemma sublisteq_Cons2': Christian@50516: assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys" Christian@50516: using assms by (cases) (rule sublisteq_Cons') Christian@49087: Christian@50516: lemma sublisteq_Cons2_neq: Christian@50516: assumes "sublisteq (x#xs) (y#ys)" Christian@50516: shows "x \ y \ sublisteq (x#xs) ys" Christian@49087: using assms by (cases) auto Christian@49087: Christian@50516: lemma sublisteq_Cons2_iff [simp, code]: Christian@50516: "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)" Christian@57497: by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq) Christian@49087: Christian@50516: lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \ sublisteq xs ys" Christian@49087: by (induct zs) simp_all Christian@49087: Christian@50516: lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all Christian@49087: Christian@50516: lemma sublisteq_antisym: Christian@50516: assumes "sublisteq xs ys" and "sublisteq ys xs" Christian@49087: shows "xs = ys" Christian@49087: using assms Christian@49087: proof (induct) Christian@57497: case list_emb_Nil Christian@57497: from list_emb_Nil2 [OF this] show ?case by simp Christian@49087: next Christian@57497: case list_emb_Cons2 blanchet@54483: thus ?case by simp Christian@49087: next Christian@57497: case list_emb_Cons blanchet@54483: hence False using sublisteq_Cons' by fastforce blanchet@54483: thus ?case .. Christian@49087: qed Christian@49087: Christian@50516: lemma sublisteq_trans: "sublisteq xs ys \ sublisteq ys zs \ sublisteq xs zs" Christian@57497: by (rule list_emb_trans [of UNIV "op ="]) auto Christian@49087: Christian@50516: lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \ xs = []" Christian@57497: by (auto dest: list_emb_length) Christian@49087: Christian@57497: lemma list_emb_append_mono: Christian@57497: "\ list_emb P xs xs'; list_emb P ys ys' \ \ list_emb P (xs@ys) (xs'@ys')" Christian@57497: apply (induct rule: list_emb.induct) Christian@57497: apply (metis eq_Nil_appendI list_emb_append2) Christian@57497: apply (metis append_Cons list_emb_Cons) Christian@57497: apply (metis append_Cons list_emb_Cons2) wenzelm@49107: done Christian@49087: Christian@49087: Christian@49087: subsection {* Appending elements *} Christian@49087: Christian@50516: lemma sublisteq_append [simp]: Christian@50516: "sublisteq (xs @ zs) (ys @ zs) \ sublisteq xs ys" (is "?l = ?r") Christian@49087: proof Christian@50516: { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'" Christian@50516: then have "xs' = xs @ zs & ys' = ys @ zs \ sublisteq xs ys" Christian@49087: proof (induct arbitrary: xs ys zs) Christian@57497: case list_emb_Nil show ?case by simp Christian@49087: next Christian@57497: case (list_emb_Cons xs' ys' x) Christian@57497: { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto } Christian@49087: moreover Christian@49087: { fix us assume "ys = x#us" Christian@57497: then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) } Christian@49087: ultimately show ?case by (auto simp:Cons_eq_append_conv) Christian@49087: next Christian@57497: case (list_emb_Cons2 x y xs' ys') Christian@57497: { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto } Christian@49087: moreover Christian@57497: { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto} Christian@49087: moreover Christian@57497: { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp } Christian@57498: ultimately show ?case using `op = x y` by (auto simp: Cons_eq_append_conv) Christian@49087: qed } Christian@49087: moreover assume ?l Christian@49087: ultimately show ?r by blast Christian@49087: next Christian@57497: assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl) Christian@49087: qed Christian@49087: Christian@50516: lemma sublisteq_drop_many: "sublisteq xs ys \ sublisteq xs (zs @ ys)" Christian@49087: by (induct zs) auto Christian@49087: Christian@50516: lemma sublisteq_rev_drop_many: "sublisteq xs ys \ sublisteq xs (ys @ zs)" Christian@57497: by (metis append_Nil2 list_emb_Nil list_emb_append_mono) Christian@49087: Christian@49087: Christian@49087: subsection {* Relation to standard list operations *} Christian@49087: Christian@50516: lemma sublisteq_map: Christian@50516: assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)" Christian@49087: using assms by (induct) auto Christian@49087: Christian@50516: lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs" Christian@49087: by (induct xs) auto Christian@49087: Christian@50516: lemma sublisteq_filter [simp]: Christian@50516: assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)" blanchet@54483: using assms by induct auto Christian@49087: Christian@50516: lemma "sublisteq xs ys \ (\N. xs = sublist ys N)" (is "?L = ?R") Christian@49087: proof Christian@49087: assume ?L wenzelm@49107: then show ?R Christian@49087: proof (induct) Christian@57497: case list_emb_Nil show ?case by (metis sublist_empty) Christian@49087: next Christian@57497: case (list_emb_Cons xs ys x) Christian@49087: then obtain N where "xs = sublist ys N" by blast wenzelm@49107: then have "xs = sublist (x#ys) (Suc ` N)" Christian@49087: by (clarsimp simp add:sublist_Cons inj_image_mem_iff) wenzelm@49107: then show ?case by blast Christian@49087: next Christian@57497: case (list_emb_Cons2 x y xs ys) Christian@49087: then obtain N where "xs = sublist ys N" by blast wenzelm@49107: then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))" Christian@49087: by (clarsimp simp add:sublist_Cons inj_image_mem_iff) Christian@57497: moreover from list_emb_Cons2 have "x = y" by simp Christian@50516: ultimately show ?case by blast Christian@49087: qed Christian@49087: next Christian@49087: assume ?R Christian@49087: then obtain N where "xs = sublist ys N" .. Christian@50516: moreover have "sublisteq (sublist ys N) ys" wenzelm@49107: proof (induct ys arbitrary: N) Christian@49087: case Nil show ?case by simp Christian@49087: next wenzelm@49107: case Cons then show ?case by (auto simp: sublist_Cons) Christian@49087: qed Christian@49087: ultimately show ?L by simp Christian@49087: qed Christian@49087: wenzelm@10330: end