
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 \<Rightarrow> 'a list \<Rightarrow> bool"
traytel@55579:   where "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
traytel@55579: 
traytel@55579: definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
traytel@55579:   where "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> 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 \<Longrightarrow> 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 \<Longrightarrow> 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 \<noteq> 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 \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> 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 \<noteq> 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]) \<longleftrightarrow> xs = ys @ [y] \<or> 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] \<or> prefixeq xs ys"
traytel@55579:     by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)
traytel@55579: next
traytel@55579:   assume "xs = ys @ [y] \<or> 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 \<and> prefixeq xs ys)"
traytel@55579:   by (auto simp add: prefixeq_def)
traytel@55579: 
traytel@55579: lemma prefixeq_code [code]:
traytel@55579:   "prefixeq [] xs \<longleftrightarrow> True"
traytel@55579:   "prefixeq (x # xs) [] \<longleftrightarrow> False"
traytel@55579:   "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> 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 \<Longrightarrow> 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 \<Longrightarrow> prefixeq xs zs"
traytel@55579:   by (auto simp add: prefixeq_def)
traytel@55579: 
traytel@55579: theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> 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 \<or> (\<exists>us. xs = ys @ us \<and> 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 \<Longrightarrow> length xs < length ys \<Longrightarrow> prefixeq (xs @ [ys ! length xs]) ys"
traytel@55579:   proof (unfold prefixeq_def)
traytel@55579:     assume a1: "\<exists>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: "\<And>v. ([]\<Colon>'a list) @ v = v" using append_Nil2 by simp
traytel@55579:     have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force
traytel@55579:     hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)
traytel@55579:     thus "\<exists>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 \<Longrightarrow> length xs \<le> 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 \<Longrightarrow> prefixeq xs\<^sub>2 ys \<Longrightarrow> prefixeq xs\<^sub>1 xs\<^sub>2 \<or> 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 \<Longrightarrow> set xs \<subseteq> 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 \<Longrightarrow> 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 \<Longrightarrow> 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 [] \<longleftrightarrow> False"
traytel@55579:   "prefix [] (x # xs) \<longleftrightarrow> True"
traytel@55579:   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
traytel@55579:   by (simp_all add: prefix_def cong: conj_cong)
traytel@55579: 
traytel@55579: lemma take_prefix: "prefix xs ys \<Longrightarrow> 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: "\<not> prefixeq ps ls"
traytel@55579:   obtains
traytel@55579:     (c1) "ps \<noteq> []" and "ls = []"
traytel@55579:   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs"
traytel@55579:   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> 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 "\<not> 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: "\<not> prefixeq ps ls"
traytel@55579:     and base: "\<And>x xs. P (x#xs) []"
traytel@55579:     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
traytel@55579:     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> 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: "\<not> 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 \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "\<parallel>" 50)
wenzelm@49107:   where "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"
wenzelm@10389: 
Christian@50516: lemma parallelI [intro]: "\<not> prefixeq xs ys \<Longrightarrow> \<not> prefixeq ys xs \<Longrightarrow> xs \<parallel> ys"
wenzelm@25692:   unfolding parallel_def by blast
wenzelm@10330: 
wenzelm@10389: lemma parallelE [elim]:
wenzelm@25692:   assumes "xs \<parallel> ys"
Christian@49087:   obtains "\<not> prefixeq xs ys \<and> \<not> 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 \<parallel> ys"
Christian@49087:   unfolding parallel_def prefix_def by blast
wenzelm@10330: 
wenzelm@10389: theorem parallel_decomp:
Christian@50516:   "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> 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 \<noteq> c" using snoc.prems ys ys' by fastforce
blanchet@54483:       thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> 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 \<parallel> ys"
wenzelm@11987:     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> 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 \<parallel> b \<Longrightarrow> a @ c \<parallel> 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 \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
wenzelm@25692:   by (simp add: parallel_append)
kleing@25299: 
wenzelm@25692: lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> 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 \<Rightarrow> 'a list \<Rightarrow> bool"
wenzelm@49107:   where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
Christian@49087: 
wenzelm@49107: definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
wenzelm@49107:   where "suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
oheimb@14538: 
Christian@49087: lemma suffix_imp_suffixeq:
Christian@49087:   "suffix xs ys \<Longrightarrow> suffixeq xs ys"
Christian@49087:   by (auto simp: suffixeq_def suffix_def)
Christian@49087: 
Christian@50516: lemma suffixeqI [intro?]: "ys = zs @ xs \<Longrightarrow> 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 \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
Christian@49087:   by (auto simp: suffix_def)
Christian@49087: lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
Christian@49087:   by (auto simp add: suffixeq_def)
Christian@49087: lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> 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 \<noteq> [] \<Longrightarrow> 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 \<Longrightarrow> suffixeq xs (y # ys)"
Christian@49087:   by (auto simp add: suffixeq_def)
wenzelm@49107: lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49087:   by (auto simp add: suffixeq_def)
oheimb@14538: 
Christian@49087: lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
Christian@49087:   by (auto simp add: suffixeq_def)
Christian@49087: lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49087:   by (auto simp add: suffixeq_def)
Christian@49087: 
Christian@49087: lemma suffix_set_subset:
Christian@49087:   "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
oheimb@14538: 
Christian@49087: lemma suffixeq_set_subset:
Christian@49087:   "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
Christian@49087: 
wenzelm@49107: lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> 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 \<longleftrightarrow> 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 \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"
Christian@49087:   by (clarsimp elim!: suffixeqE)
wenzelm@17201: 
Christian@49087: lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> 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 \<Longrightarrow> 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 \<noteq> 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 \<parallel> y \<Longrightarrow> \<not> prefixeq x y"
wenzelm@25692:   by blast
kleing@25299: 
Christian@49087: lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x"
wenzelm@25692:   by blast
wenzelm@25355: 
wenzelm@25355: lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
wenzelm@25692:   unfolding parallel_def by simp
wenzelm@25355: 
kleing@25299: lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
wenzelm@25692:   unfolding parallel_def by simp
kleing@25299: 
nipkow@25564: lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
wenzelm@25692:   by auto
kleing@25299: 
nipkow@25564: lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> 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 \<noteq> ys"
wenzelm@25356:     and len: "length xs = length ys"
wenzelm@25356:   shows "xs \<parallel> 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 \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
kleing@25299:   show ?case
kleing@25299:   proof (cases "a = b")
wenzelm@25355:     case True
haftmann@26445:     then have "as \<noteq> 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 \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> 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 \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@49087:   for P :: "('a \<Rightarrow> 'a \<Rightarrow> 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 \<Longrightarrow> list_emb P xs (y#ys)"
Christian@57498: | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
Christian@50516: 
Christian@57499: lemma list_emb_mono:                         
Christian@57499:   assumes "\<And>x y. P x y \<longrightarrow> Q x y"
Christian@57499:   shows "list_emb P xs ys \<longrightarrow> 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 "\<And>x. x \<in> set xs \<Longrightarrow> 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 \<Longrightarrow> 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 "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
Christian@49087: using assms
wenzelm@49107: proof (induct x \<equiv> "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 "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> 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 \<Rightarrow> 'a list \<Rightarrow> 'a list" and sk\<^sub>1 :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
Christian@57497:     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)"
blanchet@54483:     using Cons(1) by (metis (no_types))
Christian@57497:   hence "\<forall>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 \<Longrightarrow> length xs \<le> length ys"
Christian@57497:   by (induct rule: list_emb.induct) auto
Christian@49087: 
Christian@57497: lemma list_emb_trans:
Christian@50516:   assumes "\<And>x y z. \<lbrakk>x \<in> A; y \<in> A; z \<in> A; P x y; P y z\<rbrakk> \<Longrightarrow> P x z"
Christian@50516:   shows "\<And>xs ys zs. \<lbrakk>xs \<in> lists A; ys \<in> lists A; zs \<in> lists A;
Christian@57497:     list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> 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 \<in> lists A" and "ys \<in> lists A" and "zs \<in> 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 \<in> lists A` have "v \<in> A" by auto
Christian@57497:       moreover have "x \<in> A" and "y \<in> 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 \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@57497:   where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"
Christian@49087: 
Christian@50516: lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> 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 \<Longrightarrow> \<not> 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 \<longleftrightarrow> True"
Christian@57497:   "list_emb P (x#xs) [] \<longleftrightarrow> False"
Christian@49087:   by (simp_all)
Christian@49087: 
Christian@50516: lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> 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 \<noteq> y \<Longrightarrow> 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) \<longleftrightarrow> 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 \<Longrightarrow> sublisteq ys zs \<Longrightarrow> 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 \<longleftrightarrow> xs = []"
Christian@57497:   by (auto dest: list_emb_length)
Christian@49087: 
Christian@57497: lemma list_emb_append_mono:
Christian@57497:   "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> 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) \<longleftrightarrow> 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 \<longrightarrow> 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 \<Longrightarrow> sublisteq xs (zs @ ys)"
Christian@49087:   by (induct zs) auto
Christian@49087: 
Christian@50516: lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> 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 \<longleftrightarrow> (\<exists>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




