src/HOL/Library/Sublist.thy
author nipkow
Tue Sep 22 14:31:22 2015 +0200 (2015-09-22)
changeset 61225 1a690dce8cfc
parent 61076 bdc1e2f0a86a
child 63117 acb6d72fc42e
permissions -rw-r--r--
tuned references
     1 (*  Title:      HOL/Library/Sublist.thy
     2     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     3     Author:     Christian Sternagel, JAIST
     4 *)
     5 
     6 section \<open>List prefixes, suffixes, and homeomorphic embedding\<close>
     7 
     8 theory Sublist
     9 imports Main
    10 begin
    11 
    12 subsection \<open>Prefix order on lists\<close>
    13 
    14 definition prefixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    15   where "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
    16 
    17 definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    18   where "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys"
    19 
    20 interpretation prefix_order: order prefixeq prefix
    21   by standard (auto simp: prefixeq_def prefix_def)
    22 
    23 interpretation prefix_bot: order_bot Nil prefixeq prefix
    24   by standard (simp add: prefixeq_def)
    25 
    26 lemma prefixeqI [intro?]: "ys = xs @ zs \<Longrightarrow> prefixeq xs ys"
    27   unfolding prefixeq_def by blast
    28 
    29 lemma prefixeqE [elim?]:
    30   assumes "prefixeq xs ys"
    31   obtains zs where "ys = xs @ zs"
    32   using assms unfolding prefixeq_def by blast
    33 
    34 lemma prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> prefix xs ys"
    35   unfolding prefix_def prefixeq_def by blast
    36 
    37 lemma prefixE' [elim?]:
    38   assumes "prefix xs ys"
    39   obtains z zs where "ys = xs @ z # zs"
    40 proof -
    41   from \<open>prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys"
    42     unfolding prefix_def prefixeq_def by blast
    43   with that show ?thesis by (auto simp add: neq_Nil_conv)
    44 qed
    45 
    46 lemma prefixI [intro?]: "prefixeq xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> prefix xs ys"
    47   unfolding prefix_def by blast
    48 
    49 lemma prefixE [elim?]:
    50   fixes xs ys :: "'a list"
    51   assumes "prefix xs ys"
    52   obtains "prefixeq xs ys" and "xs \<noteq> ys"
    53   using assms unfolding prefix_def by blast
    54 
    55 
    56 subsection \<open>Basic properties of prefixes\<close>
    57 
    58 theorem Nil_prefixeq [iff]: "prefixeq [] xs"
    59   by (simp add: prefixeq_def)
    60 
    61 theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])"
    62   by (induct xs) (simp_all add: prefixeq_def)
    63 
    64 lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys"
    65 proof
    66   assume "prefixeq xs (ys @ [y])"
    67   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
    68   show "xs = ys @ [y] \<or> prefixeq xs ys"
    69     by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)
    70 next
    71   assume "xs = ys @ [y] \<or> prefixeq xs ys"
    72   then show "prefixeq xs (ys @ [y])"
    73     by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI)
    74 qed
    75 
    76 lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)"
    77   by (auto simp add: prefixeq_def)
    78 
    79 lemma prefixeq_code [code]:
    80   "prefixeq [] xs \<longleftrightarrow> True"
    81   "prefixeq (x # xs) [] \<longleftrightarrow> False"
    82   "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys"
    83   by simp_all
    84 
    85 lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs"
    86   by (induct xs) simp_all
    87 
    88 lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])"
    89   by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI)
    90 
    91 lemma prefixeq_prefixeq [simp]: "prefixeq xs ys \<Longrightarrow> prefixeq xs (ys @ zs)"
    92   by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI)
    93 
    94 lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs"
    95   by (auto simp add: prefixeq_def)
    96 
    97 theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))"
    98   by (cases xs) (auto simp add: prefixeq_def)
    99 
   100 theorem prefixeq_append:
   101   "prefixeq xs (ys @ zs) = (prefixeq xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefixeq us zs))"
   102   apply (induct zs rule: rev_induct)
   103    apply force
   104   apply (simp del: append_assoc add: append_assoc [symmetric])
   105   apply (metis append_eq_appendI)
   106   done
   107 
   108 lemma append_one_prefixeq:
   109   "prefixeq xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefixeq (xs @ [ys ! length xs]) ys"
   110   proof (unfold prefixeq_def)
   111     assume a1: "\<exists>zs. ys = xs @ zs"
   112     then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce
   113     assume a2: "length xs < length ys"
   114     have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp
   115     have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force
   116     hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)
   117     thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce
   118   qed
   119 
   120 theorem prefixeq_length_le: "prefixeq xs ys \<Longrightarrow> length xs \<le> length ys"
   121   by (auto simp add: prefixeq_def)
   122 
   123 lemma prefixeq_same_cases:
   124   "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"
   125   unfolding prefixeq_def by (force simp: append_eq_append_conv2)
   126 
   127 lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   128   by (auto simp add: prefixeq_def)
   129 
   130 lemma take_is_prefixeq: "prefixeq (take n xs) xs"
   131   unfolding prefixeq_def by (metis append_take_drop_id)
   132 
   133 lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)"
   134   by (auto simp: prefixeq_def)
   135 
   136 lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys"
   137   by (auto simp: prefix_def prefixeq_def)
   138 
   139 lemma prefix_simps [simp, code]:
   140   "prefix xs [] \<longleftrightarrow> False"
   141   "prefix [] (x # xs) \<longleftrightarrow> True"
   142   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
   143   by (simp_all add: prefix_def cong: conj_cong)
   144 
   145 lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys"
   146   apply (induct n arbitrary: xs ys)
   147    apply (case_tac ys; simp)
   148   apply (metis prefix_order.less_trans prefixI take_is_prefixeq)
   149   done
   150 
   151 lemma not_prefixeq_cases:
   152   assumes pfx: "\<not> prefixeq ps ls"
   153   obtains
   154     (c1) "ps \<noteq> []" and "ls = []"
   155   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs"
   156   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   157 proof (cases ps)
   158   case Nil
   159   then show ?thesis using pfx by simp
   160 next
   161   case (Cons a as)
   162   note c = \<open>ps = a#as\<close>
   163   show ?thesis
   164   proof (cases ls)
   165     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil)
   166   next
   167     case (Cons x xs)
   168     show ?thesis
   169     proof (cases "x = a")
   170       case True
   171       have "\<not> prefixeq as xs" using pfx c Cons True by simp
   172       with c Cons True show ?thesis by (rule c2)
   173     next
   174       case False
   175       with c Cons show ?thesis by (rule c3)
   176     qed
   177   qed
   178 qed
   179 
   180 lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]:
   181   assumes np: "\<not> prefixeq ps ls"
   182     and base: "\<And>x xs. P (x#xs) []"
   183     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   184     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   185   shows "P ps ls" using np
   186 proof (induct ls arbitrary: ps)
   187   case Nil then show ?case
   188     by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base)
   189 next
   190   case (Cons y ys)
   191   then have npfx: "\<not> prefixeq ps (y # ys)" by simp
   192   then obtain x xs where pv: "ps = x # xs"
   193     by (rule not_prefixeq_cases) auto
   194   show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2)
   195 qed
   196 
   197 
   198 subsection \<open>Parallel lists\<close>
   199 
   200 definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "\<parallel>" 50)
   201   where "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"
   202 
   203 lemma parallelI [intro]: "\<not> prefixeq xs ys \<Longrightarrow> \<not> prefixeq ys xs \<Longrightarrow> xs \<parallel> ys"
   204   unfolding parallel_def by blast
   205 
   206 lemma parallelE [elim]:
   207   assumes "xs \<parallel> ys"
   208   obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs"
   209   using assms unfolding parallel_def by blast
   210 
   211 theorem prefixeq_cases:
   212   obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys"
   213   unfolding parallel_def prefix_def by blast
   214 
   215 theorem parallel_decomp:
   216   "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   217 proof (induct xs rule: rev_induct)
   218   case Nil
   219   then have False by auto
   220   then show ?case ..
   221 next
   222   case (snoc x xs)
   223   show ?case
   224   proof (rule prefixeq_cases)
   225     assume le: "prefixeq xs ys"
   226     then obtain ys' where ys: "ys = xs @ ys'" ..
   227     show ?thesis
   228     proof (cases ys')
   229       assume "ys' = []"
   230       then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys)
   231     next
   232       fix c cs assume ys': "ys' = c # cs"
   233       have "x \<noteq> c" using snoc.prems ys ys' by fastforce
   234       thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs"
   235         using ys ys' by blast
   236     qed
   237   next
   238     assume "prefix ys xs"
   239     then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def)
   240     with snoc have False by blast
   241     then show ?thesis ..
   242   next
   243     assume "xs \<parallel> ys"
   244     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   245       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   246       by blast
   247     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   248     with neq ys show ?thesis by blast
   249   qed
   250 qed
   251 
   252 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   253   apply (rule parallelI)
   254     apply (erule parallelE, erule conjE,
   255       induct rule: not_prefixeq_induct, simp+)+
   256   done
   257 
   258 lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
   259   by (simp add: parallel_append)
   260 
   261 lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
   262   unfolding parallel_def by auto
   263 
   264 
   265 subsection \<open>Suffix order on lists\<close>
   266 
   267 definition suffixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   268   where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
   269 
   270 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   271   where "suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
   272 
   273 lemma suffix_imp_suffixeq:
   274   "suffix xs ys \<Longrightarrow> suffixeq xs ys"
   275   by (auto simp: suffixeq_def suffix_def)
   276 
   277 lemma suffixeqI [intro?]: "ys = zs @ xs \<Longrightarrow> suffixeq xs ys"
   278   unfolding suffixeq_def by blast
   279 
   280 lemma suffixeqE [elim?]:
   281   assumes "suffixeq xs ys"
   282   obtains zs where "ys = zs @ xs"
   283   using assms unfolding suffixeq_def by blast
   284 
   285 lemma suffixeq_refl [iff]: "suffixeq xs xs"
   286   by (auto simp add: suffixeq_def)
   287 lemma suffix_trans:
   288   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
   289   by (auto simp: suffix_def)
   290 lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
   291   by (auto simp add: suffixeq_def)
   292 lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   293   by (auto simp add: suffixeq_def)
   294 
   295 lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"
   296   by (induct xs) (auto simp: suffixeq_def)
   297 
   298 lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"
   299   by (induct xs) (auto simp: suffix_def)
   300 
   301 lemma Nil_suffixeq [iff]: "suffixeq [] xs"
   302   by (simp add: suffixeq_def)
   303 lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
   304   by (auto simp add: suffixeq_def)
   305 
   306 lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y # ys)"
   307   by (auto simp add: suffixeq_def)
   308 lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys"
   309   by (auto simp add: suffixeq_def)
   310 
   311 lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
   312   by (auto simp add: suffixeq_def)
   313 lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
   314   by (auto simp add: suffixeq_def)
   315 
   316 lemma suffix_set_subset:
   317   "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
   318 
   319 lemma suffixeq_set_subset:
   320   "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
   321 
   322 lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> suffixeq xs ys"
   323 proof -
   324   assume "suffixeq (x # xs) (y # ys)"
   325   then obtain zs where "y # ys = zs @ x # xs" ..
   326   then show ?thesis
   327     by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI)
   328 qed
   329 
   330 lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)"
   331 proof
   332   assume "suffixeq xs ys"
   333   then obtain zs where "ys = zs @ xs" ..
   334   then have "rev ys = rev xs @ rev zs" by simp
   335   then show "prefixeq (rev xs) (rev ys)" ..
   336 next
   337   assume "prefixeq (rev xs) (rev ys)"
   338   then obtain zs where "rev ys = rev xs @ zs" ..
   339   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
   340   then have "ys = rev zs @ xs" by simp
   341   then show "suffixeq xs ys" ..
   342 qed
   343 
   344 lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"
   345   by (clarsimp elim!: suffixeqE)
   346 
   347 lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)"
   348   by (auto elim!: suffixeqE intro: suffixeqI)
   349 
   350 lemma suffixeq_drop: "suffixeq (drop n as) as"
   351   unfolding suffixeq_def
   352   apply (rule exI [where x = "take n as"])
   353   apply simp
   354   done
   355 
   356 lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   357   by (auto elim!: suffixeqE)
   358 
   359 lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>="
   360 proof (intro ext iffI)
   361   fix xs ys :: "'a list"
   362   assume "suffixeq xs ys"
   363   show "suffix\<^sup>=\<^sup>= xs ys"
   364   proof
   365     assume "xs \<noteq> ys"
   366     with \<open>suffixeq xs ys\<close> show "suffix xs ys"
   367       by (auto simp: suffixeq_def suffix_def)
   368   qed
   369 next
   370   fix xs ys :: "'a list"
   371   assume "suffix\<^sup>=\<^sup>= xs ys"
   372   then show "suffixeq xs ys"
   373   proof
   374     assume "suffix xs ys" then show "suffixeq xs ys"
   375       by (rule suffix_imp_suffixeq)
   376   next
   377     assume "xs = ys" then show "suffixeq xs ys"
   378       by (auto simp: suffixeq_def)
   379   qed
   380 qed
   381 
   382 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y"
   383   by blast
   384 
   385 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x"
   386   by blast
   387 
   388 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   389   unfolding parallel_def by simp
   390 
   391 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   392   unfolding parallel_def by simp
   393 
   394 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   395   by auto
   396 
   397 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   398   by (metis Cons_prefixeq_Cons parallelE parallelI)
   399 
   400 lemma not_equal_is_parallel:
   401   assumes neq: "xs \<noteq> ys"
   402     and len: "length xs = length ys"
   403   shows "xs \<parallel> ys"
   404   using len neq
   405 proof (induct rule: list_induct2)
   406   case Nil
   407   then show ?case by simp
   408 next
   409   case (Cons a as b bs)
   410   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   411   show ?case
   412   proof (cases "a = b")
   413     case True
   414     then have "as \<noteq> bs" using Cons by simp
   415     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   416   next
   417     case False
   418     then show ?thesis by (rule Cons_parallelI1)
   419   qed
   420 qed
   421 
   422 lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq"
   423   by (intro ext) (auto simp: suffixeq_def suffix_def)
   424 
   425 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
   426   unfolding suffix_def by auto
   427 
   428 
   429 subsection \<open>Homeomorphic embedding on lists\<close>
   430 
   431 inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   432   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
   433 where
   434   list_emb_Nil [intro, simp]: "list_emb P [] ys"
   435 | list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"
   436 | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
   437 
   438 lemma list_emb_mono:                         
   439   assumes "\<And>x y. P x y \<longrightarrow> Q x y"
   440   shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"
   441 proof                                        
   442   assume "list_emb P xs ys"                    
   443   then show "list_emb Q xs ys" by (induct) (auto simp: assms)
   444 qed 
   445 
   446 lemma list_emb_Nil2 [simp]:
   447   assumes "list_emb P xs []" shows "xs = []"
   448   using assms by (cases rule: list_emb.cases) auto
   449 
   450 lemma list_emb_refl:
   451   assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"
   452   shows "list_emb P xs xs"
   453   using assms by (induct xs) auto
   454 
   455 lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"
   456 proof -
   457   { assume "list_emb P (x#xs) []"
   458     from list_emb_Nil2 [OF this] have False by simp
   459   } moreover {
   460     assume False
   461     then have "list_emb P (x#xs) []" by simp
   462   } ultimately show ?thesis by blast
   463 qed
   464 
   465 lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"
   466   by (induct zs) auto
   467 
   468 lemma list_emb_prefix [intro]:
   469   assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"
   470   using assms
   471   by (induct arbitrary: zs) auto
   472 
   473 lemma list_emb_ConsD:
   474   assumes "list_emb P (x#xs) ys"
   475   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
   476 using assms
   477 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   478   case list_emb_Cons
   479   then show ?case by (metis append_Cons)
   480 next
   481   case (list_emb_Cons2 x y xs ys)
   482   then show ?case by blast
   483 qed
   484 
   485 lemma list_emb_appendD:
   486   assumes "list_emb P (xs @ ys) zs"
   487   shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"
   488 using assms
   489 proof (induction xs arbitrary: ys zs)
   490   case Nil then show ?case by auto
   491 next
   492   case (Cons x xs)
   493   then obtain us v vs where
   494     zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"
   495     by (auto dest: list_emb_ConsD)
   496   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
   497     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)"
   498     using Cons(1) by (metis (no_types))
   499   hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
   500   thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
   501 qed
   502 
   503 lemma list_emb_suffix:
   504   assumes "list_emb P xs ys" and "suffix ys zs"
   505   shows "list_emb P xs zs"
   506   using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: suffix_def)
   507 
   508 lemma list_emb_suffixeq:
   509   assumes "list_emb P xs ys" and "suffixeq ys zs"
   510   shows "list_emb P xs zs"
   511   using assms and list_emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto
   512 
   513 lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"
   514   by (induct rule: list_emb.induct) auto
   515 
   516 lemma list_emb_trans:
   517   assumes "\<And>x y z. \<lbrakk>x \<in> set xs; y \<in> set ys; z \<in> set zs; P x y; P y z\<rbrakk> \<Longrightarrow> P x z"
   518   shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"
   519 proof -
   520   assume "list_emb P xs ys" and "list_emb P ys zs"
   521   then show "list_emb P xs zs" using assms
   522   proof (induction arbitrary: zs)
   523     case list_emb_Nil show ?case by blast
   524   next
   525     case (list_emb_Cons xs ys y)
   526     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   527       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast
   528     then have "list_emb P ys (v#vs)" by blast
   529     then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)
   530     from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto
   531   next
   532     case (list_emb_Cons2 x y xs ys)
   533     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   534       where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast
   535     with list_emb_Cons2 have "list_emb P xs vs" by auto
   536     moreover have "P x v"
   537     proof -
   538       from zs have "v \<in> set zs" by auto
   539       moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all
   540       ultimately show ?thesis
   541         using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2
   542         by blast
   543     qed
   544     ultimately have "list_emb P (x#xs) (v#vs)" by blast
   545     then show ?case unfolding zs by (rule list_emb_append2)
   546   qed
   547 qed
   548 
   549 lemma list_emb_set:
   550   assumes "list_emb P xs ys" and "x \<in> set xs"
   551   obtains y where "y \<in> set ys" and "P x y"
   552   using assms by (induct) auto
   553 
   554 
   555 subsection \<open>Sublists (special case of homeomorphic embedding)\<close>
   556 
   557 abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   558   where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"
   559 
   560 lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto
   561 
   562 lemma sublisteq_same_length:
   563   assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"
   564   using assms by (induct) (auto dest: list_emb_length)
   565 
   566 lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"
   567   by (metis list_emb_length linorder_not_less)
   568 
   569 lemma [code]:
   570   "list_emb P [] ys \<longleftrightarrow> True"
   571   "list_emb P (x#xs) [] \<longleftrightarrow> False"
   572   by (simp_all)
   573 
   574 lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"
   575   by (induct xs, simp, blast dest: list_emb_ConsD)
   576 
   577 lemma sublisteq_Cons2':
   578   assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"
   579   using assms by (cases) (rule sublisteq_Cons')
   580 
   581 lemma sublisteq_Cons2_neq:
   582   assumes "sublisteq (x#xs) (y#ys)"
   583   shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"
   584   using assms by (cases) auto
   585 
   586 lemma sublisteq_Cons2_iff [simp, code]:
   587   "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"
   588   by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)
   589 
   590 lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"
   591   by (induct zs) simp_all
   592 
   593 lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all
   594 
   595 lemma sublisteq_antisym:
   596   assumes "sublisteq xs ys" and "sublisteq ys xs"
   597   shows "xs = ys"
   598 using assms
   599 proof (induct)
   600   case list_emb_Nil
   601   from list_emb_Nil2 [OF this] show ?case by simp
   602 next
   603   case list_emb_Cons2
   604   thus ?case by simp
   605 next
   606   case list_emb_Cons
   607   hence False using sublisteq_Cons' by fastforce
   608   thus ?case ..
   609 qed
   610 
   611 lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"
   612   by (rule list_emb_trans [of _ _ _ "op ="]) auto
   613 
   614 lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"
   615   by (auto dest: list_emb_length)
   616 
   617 lemma list_emb_append_mono:
   618   "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"
   619   apply (induct rule: list_emb.induct)
   620     apply (metis eq_Nil_appendI list_emb_append2)
   621    apply (metis append_Cons list_emb_Cons)
   622   apply (metis append_Cons list_emb_Cons2)
   623   done
   624 
   625 
   626 subsection \<open>Appending elements\<close>
   627 
   628 lemma sublisteq_append [simp]:
   629   "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")
   630 proof
   631   { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
   632     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"
   633     proof (induct arbitrary: xs ys zs)
   634       case list_emb_Nil show ?case by simp
   635     next
   636       case (list_emb_Cons xs' ys' x)
   637       { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
   638       moreover
   639       { fix us assume "ys = x#us"
   640         then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }
   641       ultimately show ?case by (auto simp:Cons_eq_append_conv)
   642     next
   643       case (list_emb_Cons2 x y xs' ys')
   644       { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
   645       moreover
   646       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
   647       moreover
   648       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
   649       ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)
   650     qed }
   651   moreover assume ?l
   652   ultimately show ?r by blast
   653 next
   654   assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl)
   655 qed
   656 
   657 lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"
   658   by (induct zs) auto
   659 
   660 lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"
   661   by (metis append_Nil2 list_emb_Nil list_emb_append_mono)
   662 
   663 
   664 subsection \<open>Relation to standard list operations\<close>
   665 
   666 lemma sublisteq_map:
   667   assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"
   668   using assms by (induct) auto
   669 
   670 lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"
   671   by (induct xs) auto
   672 
   673 lemma sublisteq_filter [simp]:
   674   assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"
   675   using assms by induct auto
   676 
   677 lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")
   678 proof
   679   assume ?L
   680   then show ?R
   681   proof (induct)
   682     case list_emb_Nil show ?case by (metis sublist_empty)
   683   next
   684     case (list_emb_Cons xs ys x)
   685     then obtain N where "xs = sublist ys N" by blast
   686     then have "xs = sublist (x#ys) (Suc ` N)"
   687       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   688     then show ?case by blast
   689   next
   690     case (list_emb_Cons2 x y xs ys)
   691     then obtain N where "xs = sublist ys N" by blast
   692     then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
   693       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   694     moreover from list_emb_Cons2 have "x = y" by simp
   695     ultimately show ?case by blast
   696   qed
   697 next
   698   assume ?R
   699   then obtain N where "xs = sublist ys N" ..
   700   moreover have "sublisteq (sublist ys N) ys"
   701   proof (induct ys arbitrary: N)
   702     case Nil show ?case by simp
   703   next
   704     case Cons then show ?case by (auto simp: sublist_Cons)
   705   qed
   706   ultimately show ?L by simp
   707 qed
   708 
   709 end