
Christian@49085: (*  Title:      HOL/Library/Sublist.thy
wenzelm@10330:     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@10330: *)
wenzelm@10330: 
Christian@49085: header {* List prefixes, suffixes, and embedding*}
wenzelm@10330: 
Christian@49085: theory Sublist
haftmann@30663: imports List Main
nipkow@15131: begin
wenzelm@10330: 
wenzelm@10330: subsection {* Prefix order on lists *}
wenzelm@10330: 
Christian@49085: definition prefixeq :: "'a list => 'a list => bool" where
Christian@49085:   "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
Christian@49085: 
Christian@49085: definition prefix :: "'a list => 'a list => bool" where
Christian@49085:   "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys"
haftmann@25764: 
Christian@49085: interpretation prefix_order: order prefixeq prefix
Christian@49085:   by default (auto simp: prefixeq_def prefix_def)
wenzelm@10330: 
Christian@49085: interpretation prefix_bot: bot prefixeq prefix Nil
Christian@49085:   by default (simp add: prefixeq_def)
Christian@49085: 
Christian@49085: lemma prefixeqI [intro?]: "ys = xs @ zs ==> prefixeq xs ys"
Christian@49085:   unfolding prefixeq_def by blast
wenzelm@10330: 
Christian@49085: lemma prefixeqE [elim?]:
Christian@49085:   assumes "prefixeq xs ys"
Christian@49085:   obtains zs where "ys = xs @ zs"
Christian@49085:   using assms unfolding prefixeq_def by blast
Christian@49085: 
Christian@49085: lemma prefixI' [intro?]: "ys = xs @ z # zs ==> prefix xs ys"
Christian@49085:   unfolding prefix_def prefixeq_def by blast
haftmann@37474: 
Christian@49085: lemma prefixE' [elim?]:
Christian@49085:   assumes "prefix xs ys"
Christian@49085:   obtains z zs where "ys = xs @ z # zs"
Christian@49085: proof -
Christian@49085:   from `prefix xs ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"
Christian@49085:     unfolding prefix_def prefixeq_def by blast
Christian@49085:   with that show ?thesis by (auto simp add: neq_Nil_conv)
Christian@49085: qed
wenzelm@10330: 
Christian@49085: lemma prefixI [intro?]: "prefixeq xs ys ==> xs \<noteq> ys ==> prefix xs ys"
wenzelm@18730:   unfolding prefix_def by blast
wenzelm@10330: 
wenzelm@21305: lemma prefixE [elim?]:
Christian@49085:   fixes xs ys :: "'a list"
Christian@49085:   assumes "prefix xs ys"
Christian@49085:   obtains "prefixeq xs ys" and "xs \<noteq> ys"
wenzelm@23394:   using assms unfolding prefix_def by blast
wenzelm@10330: 
wenzelm@10330: 
wenzelm@10389: subsection {* Basic properties of prefixes *}
wenzelm@10330: 
Christian@49085: theorem Nil_prefixeq [iff]: "prefixeq [] xs"
Christian@49085:   by (simp add: prefixeq_def)
wenzelm@10330: 
Christian@49085: theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])"
Christian@49085:   by (induct xs) (simp_all add: prefixeq_def)
wenzelm@10330: 
Christian@49085: lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys"
wenzelm@10389: proof
Christian@49085:   assume "prefixeq xs (ys @ [y])"
wenzelm@10389:   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
Christian@49085:   show "xs = ys @ [y] \<or> prefixeq xs ys"
Christian@49085:     by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)
wenzelm@10389: next
Christian@49085:   assume "xs = ys @ [y] \<or> prefixeq xs ys"
Christian@49085:   then show "prefixeq xs (ys @ [y])"
Christian@49085:     by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI)
wenzelm@10389: qed
wenzelm@10330: 
Christian@49085: lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)"
Christian@49085:   by (auto simp add: prefixeq_def)
wenzelm@10330: 
Christian@49085: lemma prefixeq_code [code]:
Christian@49085:   "prefixeq [] xs \<longleftrightarrow> True"
Christian@49085:   "prefixeq (x # xs) [] \<longleftrightarrow> False"
Christian@49085:   "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys"
haftmann@37474:   by simp_all
haftmann@37474: 
Christian@49085: lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs"
wenzelm@10389:   by (induct xs) simp_all
wenzelm@10330: 
Christian@49085: lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])"
Christian@49085:   by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI)
nipkow@25665: 
Christian@49085: lemma prefixeq_prefixeq [simp]: "prefixeq xs ys ==> prefixeq xs (ys @ zs)"
Christian@49085:   by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI)
nipkow@25665: 
Christian@49085: lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs"
Christian@49085:   by (auto simp add: prefixeq_def)
nipkow@14300: 
Christian@49085: theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))"
Christian@49085:   by (cases xs) (auto simp add: prefixeq_def)
wenzelm@10330: 
Christian@49085: theorem prefixeq_append:
Christian@49085:   "prefixeq xs (ys @ zs) = (prefixeq xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefixeq us zs))"
wenzelm@10330:   apply (induct zs rule: rev_induct)
wenzelm@10330:    apply force
wenzelm@10330:   apply (simp del: append_assoc add: append_assoc [symmetric])
nipkow@25564:   apply (metis append_eq_appendI)
wenzelm@10330:   done
wenzelm@10330: 
Christian@49085: lemma append_one_prefixeq:
Christian@49085:   "prefixeq xs ys ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys"
Christian@49085:   unfolding prefixeq_def
wenzelm@25692:   by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj
wenzelm@25692:     eq_Nil_appendI nth_drop')
nipkow@25665: 
Christian@49085: theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys"
Christian@49085:   by (auto simp add: prefixeq_def)
wenzelm@10330: 
Christian@49085: lemma prefixeq_same_cases:
Christian@49085:   "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"
Christian@49085:   unfolding prefixeq_def by (metis append_eq_append_conv2)
nipkow@25665: 
Christian@49085: lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys"
Christian@49085:   by (auto simp add: prefixeq_def)
nipkow@14300: 
Christian@49085: lemma take_is_prefixeq: "prefixeq (take n xs) xs"
Christian@49085:   unfolding prefixeq_def by (metis append_take_drop_id)
nipkow@25665: 
Christian@49085: lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)"
Christian@49085:   by (auto simp: prefixeq_def)
kleing@25322: 
Christian@49085: lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys"
Christian@49085:   by (auto simp: prefix_def prefixeq_def)
nipkow@25665: 
Christian@49085: lemma prefix_simps [simp, code]:
Christian@49085:   "prefix xs [] \<longleftrightarrow> False"
Christian@49085:   "prefix [] (x # xs) \<longleftrightarrow> True"
Christian@49085:   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
Christian@49085:   by (simp_all add: prefix_def cong: conj_cong)
kleing@25299: 
Christian@49085: lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys"
wenzelm@25692:   apply (induct n arbitrary: xs ys)
wenzelm@25692:    apply (case_tac ys, simp_all)[1]
Christian@49085:   apply (metis prefix_order.less_trans prefixI take_is_prefixeq)
wenzelm@25692:   done
kleing@25299: 
Christian@49085: lemma not_prefixeq_cases:
Christian@49085:   assumes pfx: "\<not> prefixeq ps ls"
wenzelm@25356:   obtains
wenzelm@25356:     (c1) "ps \<noteq> []" and "ls = []"
Christian@49085:   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs"
wenzelm@25356:   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
kleing@25299: proof (cases ps)
wenzelm@25692:   case Nil then show ?thesis using pfx by simp
kleing@25299: next
kleing@25299:   case (Cons a as)
wenzelm@25692:   note c = `ps = a#as`
kleing@25299:   show ?thesis
kleing@25299:   proof (cases ls)
Christian@49085:     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil)
kleing@25299:   next
kleing@25299:     case (Cons x xs)
kleing@25299:     show ?thesis
kleing@25299:     proof (cases "x = a")
wenzelm@25355:       case True
Christian@49085:       have "\<not> prefixeq as xs" using pfx c Cons True by simp
wenzelm@25355:       with c Cons True show ?thesis by (rule c2)
wenzelm@25355:     next
wenzelm@25355:       case False
wenzelm@25355:       with c Cons show ?thesis by (rule c3)
kleing@25299:     qed
kleing@25299:   qed
kleing@25299: qed
kleing@25299: 
Christian@49085: lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]:
Christian@49085:   assumes np: "\<not> prefixeq ps ls"
wenzelm@25356:     and base: "\<And>x xs. P (x#xs) []"
wenzelm@25356:     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
Christian@49085:     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
wenzelm@25356:   shows "P ps ls" using np
kleing@25299: proof (induct ls arbitrary: ps)
wenzelm@25355:   case Nil then show ?case
Christian@49085:     by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base)
kleing@25299: next
wenzelm@25355:   case (Cons y ys)
Christian@49085:   then have npfx: "\<not> prefixeq ps (y # ys)" by simp
wenzelm@25355:   then obtain x xs where pv: "ps = x # xs"
Christian@49085:     by (rule not_prefixeq_cases) auto
Christian@49085:   show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2)
kleing@25299: qed
nipkow@14300: 
wenzelm@25356: 
wenzelm@10389: subsection {* Parallel lists *}
wenzelm@10389: 
wenzelm@19086: definition
wenzelm@21404:   parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
Christian@49085:   "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"
wenzelm@10389: 
Christian@49085: lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> xs \<parallel> ys"
wenzelm@25692:   unfolding parallel_def by blast
wenzelm@10330: 
wenzelm@10389: lemma parallelE [elim]:
wenzelm@25692:   assumes "xs \<parallel> ys"
Christian@49085:   obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs"
wenzelm@25692:   using assms unfolding parallel_def by blast
wenzelm@10330: 
Christian@49085: theorem prefixeq_cases:
Christian@49085:   obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys"
Christian@49085:   unfolding parallel_def prefix_def by blast
wenzelm@10330: 
wenzelm@10389: theorem parallel_decomp:
wenzelm@10389:   "xs \<parallel> ys ==> \<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@49085:   proof (rule prefixeq_cases)
Christian@49085:     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@49085:       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"
wenzelm@25692:       then show ?thesis
Christian@49085:         by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI
Christian@49085:           same_prefixeq_prefixeq snoc.prems ys)
wenzelm@10389:     qed
wenzelm@10408:   next
Christian@49085:     assume "prefix ys xs" 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@49085:       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@49085: subsection {* Suffix order on lists *}
wenzelm@17201: 
wenzelm@19086: definition
Christian@49085:   suffixeq :: "'a list => 'a list => bool" where
Christian@49085:   "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
Christian@49085: 
Christian@49085: definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
Christian@49085:   "suffix xs ys \<equiv> \<exists>us. ys = us @ xs \<and> us \<noteq> []"
oheimb@14538: 
Christian@49085: lemma suffix_imp_suffixeq:
Christian@49085:   "suffix xs ys \<Longrightarrow> suffixeq xs ys"
Christian@49085:   by (auto simp: suffixeq_def suffix_def)
Christian@49085: 
Christian@49085: lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"
Christian@49085:   unfolding suffixeq_def by blast
wenzelm@21305: 
Christian@49085: lemma suffixeqE [elim?]:
Christian@49085:   assumes "suffixeq xs ys"
Christian@49085:   obtains zs where "ys = zs @ xs"
Christian@49085:   using assms unfolding suffixeq_def by blast
wenzelm@21305: 
Christian@49085: lemma suffixeq_refl [iff]: "suffixeq xs xs"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: lemma suffix_trans:
Christian@49085:   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
Christian@49085:   by (auto simp: suffix_def)
Christian@49085: lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: 
Christian@49085: lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"
Christian@49085:   by (induct xs) (auto simp: suffixeq_def)
oheimb@14538: 
Christian@49085: lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"
Christian@49085:   by (induct xs) (auto simp: suffix_def)
oheimb@14538: 
Christian@49085: lemma Nil_suffixeq [iff]: "suffixeq [] xs"
Christian@49085:   by (simp add: suffixeq_def)
Christian@49085: lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: 
Christian@49085: lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y#ys)"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: lemma suffixeq_ConsD: "suffixeq (x#xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49085:   by (auto simp add: suffixeq_def)
oheimb@14538: 
Christian@49085: lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49085:   by (auto simp add: suffixeq_def)
Christian@49085: 
Christian@49085: lemma suffix_set_subset:
Christian@49085:   "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
oheimb@14538: 
Christian@49085: lemma suffixeq_set_subset:
Christian@49085:   "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
Christian@49085: 
Christian@49085: lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys"
wenzelm@21305: proof -
Christian@49085:   assume "suffixeq (x#xs) (y#ys)"
Christian@49085:   then obtain zs where "y#ys = zs @ x#xs" ..
Christian@49085:   then show ?thesis
Christian@49085:     by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI)
wenzelm@21305: qed
oheimb@14538: 
Christian@49085: lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)"
Christian@49085: proof
Christian@49085:   assume "suffixeq xs ys"
Christian@49085:   then obtain zs where "ys = zs @ xs" ..
Christian@49085:   then have "rev ys = rev xs @ rev zs" by simp
Christian@49085:   then show "prefixeq (rev xs) (rev ys)" ..
Christian@49085: next
Christian@49085:   assume "prefixeq (rev xs) (rev ys)"
Christian@49085:   then obtain zs where "rev ys = rev xs @ zs" ..
Christian@49085:   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
Christian@49085:   then have "ys = rev zs @ xs" by simp
Christian@49085:   then show "suffixeq xs ys" ..
wenzelm@21305: qed
oheimb@14538: 
Christian@49085: lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"
Christian@49085:   by (clarsimp elim!: suffixeqE)
wenzelm@17201: 
Christian@49085: lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)"
Christian@49085:   by (auto elim!: suffixeqE intro: suffixeqI)
kleing@25299: 
Christian@49085: lemma suffixeq_drop: "suffixeq (drop n as) as"
Christian@49085:   unfolding suffixeq_def
wenzelm@25692:   apply (rule exI [where x = "take n as"])
wenzelm@25692:   apply simp
wenzelm@25692:   done
kleing@25299: 
Christian@49085: lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
Christian@49085:   by (clarsimp elim!: suffixeqE)
kleing@25299: 
Christian@49085: lemma suffixeq_suffix_reflclp_conv:
Christian@49085:   "suffixeq = suffix\<^sup>=\<^sup>="
Christian@49085: proof (intro ext iffI)
Christian@49085:   fix xs ys :: "'a list"
Christian@49085:   assume "suffixeq xs ys"
Christian@49085:   show "suffix\<^sup>=\<^sup>= xs ys"
Christian@49085:   proof
Christian@49085:     assume "xs \<noteq> ys"
Christian@49085:     with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def)
Christian@49085:   qed
Christian@49085: next
Christian@49085:   fix xs ys :: "'a list"
Christian@49085:   assume "suffix\<^sup>=\<^sup>= xs ys"
Christian@49085:   thus "suffixeq xs ys"
Christian@49085:   proof
Christian@49085:     assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq)
Christian@49085:   next
Christian@49085:     assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def)
Christian@49085:   qed
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y"
wenzelm@25692:   by blast
kleing@25299: 
Christian@49085: 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@49085:   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: 
Christian@49085: lemma suffix_reflclp_conv:
Christian@49085:   "suffix\<^sup>=\<^sup>= = suffixeq"
Christian@49085:   by (intro ext) (auto simp: suffixeq_def suffix_def)
Christian@49085: 
Christian@49085: lemma suffix_lists:
Christian@49085:   "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
Christian@49085:   unfolding suffix_def by auto
Christian@49085: 
Christian@49085: 
Christian@49085: subsection {* Embedding on lists *}
Christian@49085: 
Christian@49085: inductive
Christian@49085:   emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@49085:   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
Christian@49085: where
Christian@49085:   emb_Nil [intro, simp]: "emb P [] ys"
Christian@49085: | emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)"
Christian@49085: | emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)"
Christian@49085: 
Christian@49085: lemma emb_Nil2 [simp]:
Christian@49085:   assumes "emb P xs []" shows "xs = []"
Christian@49085:   using assms by (cases rule: emb.cases) auto
Christian@49085: 
Christian@49085: lemma emb_Cons_Nil [simp]:
Christian@49085:   "emb P (x#xs) [] = False"
Christian@49085: proof -
Christian@49085:   { assume "emb P (x#xs) []"
Christian@49085:     from emb_Nil2 [OF this] have False by simp
Christian@49085:   } moreover {
Christian@49085:     assume False
Christian@49085:     hence "emb P (x#xs) []" by simp
Christian@49085:   } ultimately show ?thesis by blast
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma emb_append2 [intro]:
Christian@49085:   "emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)"
Christian@49085:   by (induct zs) auto
Christian@49085: 
Christian@49085: lemma emb_prefix [intro]:
Christian@49085:   assumes "emb P xs ys" shows "emb P xs (ys @ zs)"
Christian@49085:   using assms
Christian@49085:   by (induct arbitrary: zs) auto
Christian@49085: 
Christian@49085: lemma emb_ConsD:
Christian@49085:   assumes "emb P (x#xs) ys"
Christian@49085:   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> emb P xs vs"
Christian@49085: using assms
Christian@49085: proof (induct x\<equiv>"x#xs" y\<equiv>"ys" arbitrary: x xs ys)
Christian@49085:   case emb_Cons thus ?case by (metis append_Cons)
Christian@49085: next
Christian@49085:   case (emb_Cons2 x y xs ys)
Christian@49085:   thus ?case by (cases xs) (auto, blast+)
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma emb_appendD:
Christian@49085:   assumes "emb P (xs @ ys) zs"
Christian@49085:   shows "\<exists>us vs. zs = us @ vs \<and> emb P xs us \<and> emb P ys vs"
Christian@49085: using assms
Christian@49085: proof (induction xs arbitrary: ys zs)
Christian@49085:   case Nil thus ?case by auto
Christian@49085: next
Christian@49085:   case (Cons x xs)
Christian@49085:   then obtain us v vs where "zs = us @ v # vs"
Christian@49085:     and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)
Christian@49085:   with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma emb_suffix:
Christian@49085:   assumes "emb P xs ys" and "suffix ys zs"
Christian@49085:   shows "emb P xs zs"
Christian@49085:   using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def)
Christian@49085: 
Christian@49085: lemma emb_suffixeq:
Christian@49085:   assumes "emb P xs ys" and "suffixeq ys zs"
Christian@49085:   shows "emb P xs zs"
Christian@49085:   using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto
Christian@49085: 
Christian@49085: lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys"
Christian@49085:   by (induct rule: emb.induct) auto
Christian@49085: 
Christian@49085: (*FIXME: move*)
Christian@49085: definition transp_on :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
Christian@49085:   "transp_on P A \<equiv> \<forall>a\<in>A. \<forall>b\<in>A. \<forall>c\<in>A. P a b \<and> P b c \<longrightarrow> P a c"
Christian@49085: lemma transp_onI [Pure.intro]:
Christian@49085:   "(\<And>a b c. \<lbrakk>a \<in> A; b \<in> A; c \<in> A; P a b; P b c\<rbrakk> \<Longrightarrow> P a c) \<Longrightarrow> transp_on P A"
Christian@49085:   unfolding transp_on_def by blast
Christian@49085: 
Christian@49085: lemma transp_on_emb:
Christian@49085:   assumes "transp_on P A"
Christian@49085:   shows "transp_on (emb P) (lists A)"
Christian@49085: proof
Christian@49085:   fix xs ys zs
Christian@49085:   assume "emb P xs ys" and "emb P ys zs"
Christian@49085:     and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A"
Christian@49085:   thus "emb P xs zs"
Christian@49085:   proof (induction arbitrary: zs)
Christian@49085:     case emb_Nil show ?case by blast
Christian@49085:   next
Christian@49085:     case (emb_Cons xs ys y)
Christian@49085:     from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs
Christian@49085:       where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast
Christian@49085:     hence "emb P ys (v#vs)" by blast
Christian@49085:     hence "emb P ys zs" unfolding zs by (rule emb_append2)
Christian@49085:     from emb_Cons.IH [OF this] and emb_Cons.prems show ?case by simp
Christian@49085:   next
Christian@49085:     case (emb_Cons2 x y xs ys)
Christian@49085:     from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs
Christian@49085:       where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast
Christian@49085:     with emb_Cons2 have "emb P xs vs" by simp
Christian@49085:     moreover have "P x v"
Christian@49085:     proof -
Christian@49085:       from zs and `zs \<in> lists A` have "v \<in> A" by auto
Christian@49085:       moreover have "x \<in> A" and "y \<in> A" using emb_Cons2 by simp_all
Christian@49085:       ultimately show ?thesis using `P x y` and `P y v` and assms
Christian@49085:         unfolding transp_on_def by blast
Christian@49085:     qed
Christian@49085:     ultimately have "emb P (x#xs) (v#vs)" by blast
Christian@49085:     thus ?case unfolding zs by (rule emb_append2)
Christian@49085:   qed
Christian@49085: qed
Christian@49085: 
Christian@49085: 
Christian@49085: subsection {* Sublists (special case of embedding) *}
Christian@49085: 
Christian@49085: abbreviation sub :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
Christian@49085:   "sub xs ys \<equiv> emb (op =) xs ys"
Christian@49085: 
Christian@49085: lemma sub_Cons2: "sub xs ys \<Longrightarrow> sub (x#xs) (x#ys)" by auto
Christian@49085: 
Christian@49085: lemma sub_same_length:
Christian@49085:   assumes "sub xs ys" and "length xs = length ys" shows "xs = ys"
Christian@49085:   using assms by (induct) (auto dest: emb_length)
Christian@49085: 
Christian@49085: lemma not_sub_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sub xs ys"
Christian@49085:   by (metis emb_length linorder_not_less)
Christian@49085: 
Christian@49085: lemma [code]:
Christian@49085:   "emb P [] ys \<longleftrightarrow> True"
Christian@49085:   "emb P (x#xs) [] \<longleftrightarrow> False"
Christian@49085:   by (simp_all)
Christian@49085: 
Christian@49085: lemma sub_Cons': "sub (x#xs) ys \<Longrightarrow> sub xs ys"
Christian@49085:   by (induct xs) (auto dest: emb_ConsD)
Christian@49085: 
Christian@49085: lemma sub_Cons2':
Christian@49085:   assumes "sub (x#xs) (x#ys)" shows "sub xs ys"
Christian@49085:   using assms by (cases) (rule sub_Cons')
Christian@49085: 
Christian@49085: lemma sub_Cons2_neq:
Christian@49085:   assumes "sub (x#xs) (y#ys)"
Christian@49085:   shows "x \<noteq> y \<Longrightarrow> sub (x#xs) ys"
Christian@49085:   using assms by (cases) auto
Christian@49085: 
Christian@49085: lemma sub_Cons2_iff [simp, code]:
Christian@49085:   "sub (x#xs) (y#ys) = (if x = y then sub xs ys else sub (x#xs) ys)"
Christian@49085:   by (metis emb_Cons emb_Cons2 [of "op =", OF refl] sub_Cons2' sub_Cons2_neq)
Christian@49085: 
Christian@49085: lemma sub_append': "sub (zs @ xs) (zs @ ys) \<longleftrightarrow> sub xs ys"
Christian@49085:   by (induct zs) simp_all
Christian@49085: 
Christian@49085: lemma sub_refl [simp, intro!]: "sub xs xs" by (induct xs) simp_all
Christian@49085: 
Christian@49085: lemma sub_antisym:
Christian@49085:   assumes "sub xs ys" and "sub ys xs"
Christian@49085:   shows "xs = ys"
Christian@49085: using assms
Christian@49085: proof (induct)
Christian@49085:   case emb_Nil
Christian@49085:   from emb_Nil2 [OF this] show ?case by simp
Christian@49085: next
Christian@49085:   case emb_Cons2 thus ?case by simp
Christian@49085: next
Christian@49085:   case emb_Cons thus ?case
Christian@49085:     by (metis sub_Cons' emb_length Suc_length_conv Suc_n_not_le_n)
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma transp_on_sub: "transp_on sub UNIV"
Christian@49085: proof -
Christian@49085:   have "transp_on (op =) UNIV" by (simp add: transp_on_def)
Christian@49085:   from transp_on_emb [OF this] show ?thesis by simp
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma sub_trans: "sub xs ys \<Longrightarrow> sub ys zs \<Longrightarrow> sub xs zs"
Christian@49085:   using transp_on_sub [unfolded transp_on_def] by blast
Christian@49085: 
Christian@49085: lemma sub_append_le_same_iff: "sub (xs @ ys) ys \<longleftrightarrow> xs = []"
Christian@49085:   by (auto dest: emb_length)
Christian@49085: 
Christian@49085: lemma emb_append_mono:
Christian@49085:   "\<lbrakk> emb P xs xs'; emb P ys ys' \<rbrakk> \<Longrightarrow> emb P (xs@ys) (xs'@ys')"
Christian@49085: apply (induct rule: emb.induct)
Christian@49085:   apply (metis eq_Nil_appendI emb_append2)
Christian@49085:  apply (metis append_Cons emb_Cons)
Christian@49085: by (metis append_Cons emb_Cons2)
Christian@49085: 
Christian@49085: 
Christian@49085: subsection {* Appending elements *}
Christian@49085: 
Christian@49085: lemma sub_append [simp]:
Christian@49085:   "sub (xs @ zs) (ys @ zs) \<longleftrightarrow> sub xs ys" (is "?l = ?r")
Christian@49085: proof
Christian@49085:   { fix xs' ys' xs ys zs :: "'a list" assume "sub xs' ys'"
Christian@49085:     hence "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sub xs ys"
Christian@49085:     proof (induct arbitrary: xs ys zs)
Christian@49085:       case emb_Nil show ?case by simp
Christian@49085:     next
Christian@49085:       case (emb_Cons xs' ys' x)
Christian@49085:       { assume "ys=[]" hence ?case using emb_Cons(1) by auto }
Christian@49085:       moreover
Christian@49085:       { fix us assume "ys = x#us"
Christian@49085:         hence ?case using emb_Cons(2) by(simp add: emb.emb_Cons) }
Christian@49085:       ultimately show ?case by (auto simp:Cons_eq_append_conv)
Christian@49085:     next
Christian@49085:       case (emb_Cons2 x y xs' ys')
Christian@49085:       { assume "xs=[]" hence ?case using emb_Cons2(1) by auto }
Christian@49085:       moreover
Christian@49085:       { fix us vs assume "xs=x#us" "ys=x#vs" hence ?case using emb_Cons2 by auto}
Christian@49085:       moreover
Christian@49085:       { fix us assume "xs=x#us" "ys=[]" hence ?case using emb_Cons2(2) by bestsimp }
Christian@49085:       ultimately show ?case using `x = y` by (auto simp: Cons_eq_append_conv)
Christian@49085:     qed }
Christian@49085:   moreover assume ?l
Christian@49085:   ultimately show ?r by blast
Christian@49085: next
Christian@49085:   assume ?r thus ?l by (metis emb_append_mono sub_refl)
Christian@49085: qed
Christian@49085: 
Christian@49085: lemma sub_drop_many: "sub xs ys \<Longrightarrow> sub xs (zs @ ys)"
Christian@49085:   by (induct zs) auto
Christian@49085: 
Christian@49085: lemma sub_rev_drop_many: "sub xs ys \<Longrightarrow> sub xs (ys @ zs)"
Christian@49085:   by (metis append_Nil2 emb_Nil emb_append_mono)
Christian@49085: 
Christian@49085: 
Christian@49085: subsection {* Relation to standard list operations *}
Christian@49085: 
Christian@49085: lemma sub_map:
Christian@49085:   assumes "sub xs ys" shows "sub (map f xs) (map f ys)"
Christian@49085:   using assms by (induct) auto
Christian@49085: 
Christian@49085: lemma sub_filter_left [simp]: "sub (filter P xs) xs"
Christian@49085:   by (induct xs) auto
Christian@49085: 
Christian@49085: lemma sub_filter [simp]:
Christian@49085:   assumes "sub xs ys" shows "sub (filter P xs) (filter P ys)"
Christian@49085:   using assms by (induct) auto
Christian@49085: 
Christian@49085: lemma "sub xs ys \<longleftrightarrow> (\<exists> N. xs = sublist ys N)" (is "?L = ?R")
Christian@49085: proof
Christian@49085:   assume ?L
Christian@49085:   thus ?R
Christian@49085:   proof (induct)
Christian@49085:     case emb_Nil show ?case by (metis sublist_empty)
Christian@49085:   next
Christian@49085:     case (emb_Cons xs ys x)
Christian@49085:     then obtain N where "xs = sublist ys N" by blast
Christian@49085:     hence "xs = sublist (x#ys) (Suc ` N)"
Christian@49085:       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
Christian@49085:     thus ?case by blast
Christian@49085:   next
Christian@49085:     case (emb_Cons2 x y xs ys)
Christian@49085:     then obtain N where "xs = sublist ys N" by blast
Christian@49085:     hence "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
Christian@49085:       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
Christian@49085:     thus ?case unfolding `x = y` by blast
Christian@49085:   qed
Christian@49085: next
Christian@49085:   assume ?R
Christian@49085:   then obtain N where "xs = sublist ys N" ..
Christian@49085:   moreover have "sub (sublist ys N) ys"
Christian@49085:   proof (induct ys arbitrary:N)
Christian@49085:     case Nil show ?case by simp
Christian@49085:   next
Christian@49085:     case Cons thus ?case by (auto simp: sublist_Cons)
Christian@49085:   qed
Christian@49085:   ultimately show ?L by simp
Christian@49085: qed
Christian@49085: 
wenzelm@10330: end




