src/HOL/Library/Sublist.thy
author nipkow
Wed May 25 17:40:56 2016 +0200 (2016-05-25)
changeset 63149 f5dbab18c404
parent 63117 acb6d72fc42e
child 63155 ea8540c71581
permissions -rw-r--r--
renamed suffix(eq)
     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 prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    15   where "prefix xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
    16 
    17 definition strict_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    18   where "strict_prefix xs ys \<longleftrightarrow> prefix xs ys \<and> xs \<noteq> ys"
    19 
    20 interpretation prefix_order: order prefix strict_prefix
    21   by standard (auto simp: prefix_def strict_prefix_def)
    22 
    23 interpretation prefix_bot: order_bot Nil prefix strict_prefix
    24   by standard (simp add: prefix_def)
    25 
    26 lemma prefixI [intro?]: "ys = xs @ zs \<Longrightarrow> prefix xs ys"
    27   unfolding prefix_def by blast
    28 
    29 lemma prefixE [elim?]:
    30   assumes "prefix xs ys"
    31   obtains zs where "ys = xs @ zs"
    32   using assms unfolding prefix_def by blast
    33 
    34 lemma strict_prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> strict_prefix xs ys"
    35   unfolding strict_prefix_def prefix_def by blast
    36 
    37 lemma strict_prefixE' [elim?]:
    38   assumes "strict_prefix xs ys"
    39   obtains z zs where "ys = xs @ z # zs"
    40 proof -
    41   from \<open>strict_prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys"
    42     unfolding strict_prefix_def prefix_def by blast
    43   with that show ?thesis by (auto simp add: neq_Nil_conv)
    44 qed
    45 
    46 lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys"
    47   unfolding strict_prefix_def by blast
    48 
    49 lemma strict_prefixE [elim?]:
    50   fixes xs ys :: "'a list"
    51   assumes "strict_prefix xs ys"
    52   obtains "prefix xs ys" and "xs \<noteq> ys"
    53   using assms unfolding strict_prefix_def by blast
    54 
    55 
    56 subsection \<open>Basic properties of prefixes\<close>
    57 
    58 theorem Nil_prefix [iff]: "prefix [] xs"
    59   by (simp add: prefix_def)
    60 
    61 theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])"
    62   by (induct xs) (simp_all add: prefix_def)
    63 
    64 lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys"
    65 proof
    66   assume "prefix xs (ys @ [y])"
    67   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
    68   show "xs = ys @ [y] \<or> prefix xs ys"
    69     by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
    70 next
    71   assume "xs = ys @ [y] \<or> prefix xs ys"
    72   then show "prefix xs (ys @ [y])"
    73     by (metis prefix_order.eq_iff prefix_order.order_trans prefixI)
    74 qed
    75 
    76 lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)"
    77   by (auto simp add: prefix_def)
    78 
    79 lemma prefix_code [code]:
    80   "prefix [] xs \<longleftrightarrow> True"
    81   "prefix (x # xs) [] \<longleftrightarrow> False"
    82   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
    83   by simp_all
    84 
    85 lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs"
    86   by (induct xs) simp_all
    87 
    88 lemma same_prefix_nil [iff]: "prefix (xs @ ys) xs = (ys = [])"
    89   by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)
    90 
    91 lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)"
    92   by (metis prefix_order.le_less_trans prefixI strict_prefixE strict_prefixI)
    93 
    94 lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs"
    95   by (auto simp add: prefix_def)
    96 
    97 theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))"
    98   by (cases xs) (auto simp add: prefix_def)
    99 
   100 theorem prefix_append:
   101   "prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix 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_prefix:
   109   "prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys"
   110   proof (unfold prefix_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 prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys"
   121   by (auto simp add: prefix_def)
   122 
   123 lemma prefix_same_cases:
   124   "prefix (xs\<^sub>1::'a list) ys \<Longrightarrow> prefix xs\<^sub>2 ys \<Longrightarrow> prefix xs\<^sub>1 xs\<^sub>2 \<or> prefix xs\<^sub>2 xs\<^sub>1"
   125   unfolding prefix_def by (force simp: append_eq_append_conv2)
   126 
   127 lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   128   by (auto simp add: prefix_def)
   129 
   130 lemma take_is_prefix: "prefix (take n xs) xs"
   131   unfolding prefix_def by (metis append_take_drop_id)
   132 
   133 lemma map_prefixI: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"
   134   by (auto simp: prefix_def)
   135 
   136 lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys"
   137   by (auto simp: strict_prefix_def prefix_def)
   138 
   139 lemma strict_prefix_simps [simp, code]:
   140   "strict_prefix xs [] \<longleftrightarrow> False"
   141   "strict_prefix [] (x # xs) \<longleftrightarrow> True"
   142   "strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys"
   143   by (simp_all add: strict_prefix_def cong: conj_cong)
   144 
   145 lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_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 strict_prefixI take_is_prefix)
   149   done
   150 
   151 lemma not_prefix_cases:
   152   assumes pfx: "\<not> prefix 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> prefix 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_prefix_nil)
   166   next
   167     case (Cons x xs)
   168     show ?thesis
   169     proof (cases "x = a")
   170       case True
   171       have "\<not> prefix 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_prefix_induct [consumes 1, case_names Nil Neq Eq]:
   181   assumes np: "\<not> prefix 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> prefix 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_prefix_cases intro!: base)
   189 next
   190   case (Cons y ys)
   191   then have npfx: "\<not> prefix ps (y # ys)" by simp
   192   then obtain x xs where pv: "ps = x # xs"
   193     by (rule not_prefix_cases) auto
   194   show ?case by (metis Cons.hyps Cons_prefix_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> prefix xs ys \<and> \<not> prefix ys xs)"
   202 
   203 lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix 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> prefix xs ys \<and> \<not> prefix ys xs"
   209   using assms unfolding parallel_def by blast
   210 
   211 theorem prefix_cases:
   212   obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys"
   213   unfolding parallel_def strict_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 prefix_cases)
   225     assume le: "prefix 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 prefixI 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 "strict_prefix ys xs"
   239     then have "prefix ys (xs @ [x])" by (simp add: strict_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_prefix_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 suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   268   where "suffix xs ys = (\<exists>zs. ys = zs @ xs)"
   269 
   270 definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   271   where "strict_suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
   272 
   273 lemma strict_suffix_imp_suffix:
   274   "strict_suffix xs ys \<Longrightarrow> suffix xs ys"
   275   by (auto simp: suffix_def strict_suffix_def)
   276 
   277 lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys"
   278   unfolding suffix_def by blast
   279 
   280 lemma suffixE [elim?]:
   281   assumes "suffix xs ys"
   282   obtains zs where "ys = zs @ xs"
   283   using assms unfolding suffix_def by blast
   284 
   285 lemma suffix_refl [iff]: "suffix xs xs"
   286   by (auto simp add: suffix_def)
   287 
   288 lemma suffix_trans:
   289   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
   290   by (auto simp: suffix_def)
   291 
   292 lemma strict_suffix_trans:
   293   "\<lbrakk>strict_suffix xs ys; strict_suffix ys zs\<rbrakk> \<Longrightarrow> strict_suffix xs zs"
   294 by (auto simp add: strict_suffix_def)
   295 
   296 lemma suffix_antisym: "\<lbrakk>suffix xs ys; suffix ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   297   by (auto simp add: suffix_def)
   298 
   299 lemma suffix_tl [simp]: "suffix (tl xs) xs"
   300   by (induct xs) (auto simp: suffix_def)
   301 
   302 lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs"
   303   by (induct xs) (auto simp: strict_suffix_def)
   304 
   305 lemma Nil_suffix [iff]: "suffix [] xs"
   306   by (simp add: suffix_def)
   307 
   308 lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])"
   309   by (auto simp add: suffix_def)
   310 
   311 lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)"
   312   by (auto simp add: suffix_def)
   313 
   314 lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys"
   315   by (auto simp add: suffix_def)
   316 
   317 lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)"
   318   by (auto simp add: suffix_def)
   319 
   320 lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys"
   321   by (auto simp add: suffix_def)
   322 
   323 lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   324 by (auto simp: strict_suffix_def)
   325 
   326 lemma suffix_set_subset: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   327 by (auto simp: suffix_def)
   328 
   329 lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys"
   330 proof -
   331   assume "suffix (x # xs) (y # ys)"
   332   then obtain zs where "y # ys = zs @ x # xs" ..
   333   then show ?thesis
   334     by (induct zs) (auto intro!: suffix_appendI suffix_ConsI)
   335 qed
   336 
   337 lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"
   338 proof
   339   assume "suffix xs ys"
   340   then obtain zs where "ys = zs @ xs" ..
   341   then have "rev ys = rev xs @ rev zs" by simp
   342   then show "prefix (rev xs) (rev ys)" ..
   343 next
   344   assume "prefix (rev xs) (rev ys)"
   345   then obtain zs where "rev ys = rev xs @ zs" ..
   346   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
   347   then have "ys = rev zs @ xs" by simp
   348   then show "suffix xs ys" ..
   349 qed
   350 
   351 lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs"
   352   by (clarsimp elim!: suffixE)
   353 
   354 lemma suffix_map: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"
   355   by (auto elim!: suffixE intro: suffixI)
   356 
   357 lemma suffix_drop: "suffix (drop n as) as"
   358   unfolding suffix_def
   359   apply (rule exI [where x = "take n as"])
   360   apply simp
   361   done
   362 
   363 lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   364   by (auto elim!: suffixE)
   365 
   366 lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix"
   367 by (intro ext) (auto simp: suffix_def strict_suffix_def)
   368 
   369 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
   370   unfolding suffix_def by auto
   371 
   372 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y"
   373   by blast
   374 
   375 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x"
   376   by blast
   377 
   378 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   379   unfolding parallel_def by simp
   380 
   381 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   382   unfolding parallel_def by simp
   383 
   384 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   385   by auto
   386 
   387 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   388   by (metis Cons_prefix_Cons parallelE parallelI)
   389 
   390 lemma not_equal_is_parallel:
   391   assumes neq: "xs \<noteq> ys"
   392     and len: "length xs = length ys"
   393   shows "xs \<parallel> ys"
   394   using len neq
   395 proof (induct rule: list_induct2)
   396   case Nil
   397   then show ?case by simp
   398 next
   399   case (Cons a as b bs)
   400   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   401   show ?case
   402   proof (cases "a = b")
   403     case True
   404     then have "as \<noteq> bs" using Cons by simp
   405     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   406   next
   407     case False
   408     then show ?thesis by (rule Cons_parallelI1)
   409   qed
   410 qed
   411 
   412 
   413 subsection \<open>Homeomorphic embedding on lists\<close>
   414 
   415 inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   416   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
   417 where
   418   list_emb_Nil [intro, simp]: "list_emb P [] ys"
   419 | list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"
   420 | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
   421 
   422 lemma list_emb_mono:                         
   423   assumes "\<And>x y. P x y \<longrightarrow> Q x y"
   424   shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"
   425 proof                                        
   426   assume "list_emb P xs ys"                    
   427   then show "list_emb Q xs ys" by (induct) (auto simp: assms)
   428 qed 
   429 
   430 lemma list_emb_Nil2 [simp]:
   431   assumes "list_emb P xs []" shows "xs = []"
   432   using assms by (cases rule: list_emb.cases) auto
   433 
   434 lemma list_emb_refl:
   435   assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"
   436   shows "list_emb P xs xs"
   437   using assms by (induct xs) auto
   438 
   439 lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"
   440 proof -
   441   { assume "list_emb P (x#xs) []"
   442     from list_emb_Nil2 [OF this] have False by simp
   443   } moreover {
   444     assume False
   445     then have "list_emb P (x#xs) []" by simp
   446   } ultimately show ?thesis by blast
   447 qed
   448 
   449 lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"
   450   by (induct zs) auto
   451 
   452 lemma list_emb_prefix [intro]:
   453   assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"
   454   using assms
   455   by (induct arbitrary: zs) auto
   456 
   457 lemma list_emb_ConsD:
   458   assumes "list_emb P (x#xs) ys"
   459   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
   460 using assms
   461 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   462   case list_emb_Cons
   463   then show ?case by (metis append_Cons)
   464 next
   465   case (list_emb_Cons2 x y xs ys)
   466   then show ?case by blast
   467 qed
   468 
   469 lemma list_emb_appendD:
   470   assumes "list_emb P (xs @ ys) zs"
   471   shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"
   472 using assms
   473 proof (induction xs arbitrary: ys zs)
   474   case Nil then show ?case by auto
   475 next
   476   case (Cons x xs)
   477   then obtain us v vs where
   478     zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"
   479     by (auto dest: list_emb_ConsD)
   480   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
   481     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)"
   482     using Cons(1) by (metis (no_types))
   483   hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
   484   thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
   485 qed
   486 
   487 lemma list_emb_strict_suffix:
   488   assumes "list_emb P xs ys" and "strict_suffix ys zs"
   489   shows "list_emb P xs zs"
   490   using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def)
   491 
   492 lemma list_emb_suffix:
   493   assumes "list_emb P xs ys" and "suffix ys zs"
   494   shows "list_emb P xs zs"
   495 using assms and list_emb_strict_suffix
   496 unfolding strict_suffix_reflclp_conv[symmetric] by auto
   497 
   498 lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"
   499   by (induct rule: list_emb.induct) auto
   500 
   501 lemma list_emb_trans:
   502   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"
   503   shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"
   504 proof -
   505   assume "list_emb P xs ys" and "list_emb P ys zs"
   506   then show "list_emb P xs zs" using assms
   507   proof (induction arbitrary: zs)
   508     case list_emb_Nil show ?case by blast
   509   next
   510     case (list_emb_Cons xs ys y)
   511     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   512       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast
   513     then have "list_emb P ys (v#vs)" by blast
   514     then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)
   515     from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto
   516   next
   517     case (list_emb_Cons2 x y xs ys)
   518     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   519       where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast
   520     with list_emb_Cons2 have "list_emb P xs vs" by auto
   521     moreover have "P x v"
   522     proof -
   523       from zs have "v \<in> set zs" by auto
   524       moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all
   525       ultimately show ?thesis
   526         using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2
   527         by blast
   528     qed
   529     ultimately have "list_emb P (x#xs) (v#vs)" by blast
   530     then show ?case unfolding zs by (rule list_emb_append2)
   531   qed
   532 qed
   533 
   534 lemma list_emb_set:
   535   assumes "list_emb P xs ys" and "x \<in> set xs"
   536   obtains y where "y \<in> set ys" and "P x y"
   537   using assms by (induct) auto
   538 
   539 
   540 subsection \<open>Sublists (special case of homeomorphic embedding)\<close>
   541 
   542 abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   543   where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"
   544 
   545 lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto
   546 
   547 lemma sublisteq_same_length:
   548   assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"
   549   using assms by (induct) (auto dest: list_emb_length)
   550 
   551 lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"
   552   by (metis list_emb_length linorder_not_less)
   553 
   554 lemma [code]:
   555   "list_emb P [] ys \<longleftrightarrow> True"
   556   "list_emb P (x#xs) [] \<longleftrightarrow> False"
   557   by (simp_all)
   558 
   559 lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"
   560   by (induct xs, simp, blast dest: list_emb_ConsD)
   561 
   562 lemma sublisteq_Cons2':
   563   assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"
   564   using assms by (cases) (rule sublisteq_Cons')
   565 
   566 lemma sublisteq_Cons2_neq:
   567   assumes "sublisteq (x#xs) (y#ys)"
   568   shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"
   569   using assms by (cases) auto
   570 
   571 lemma sublisteq_Cons2_iff [simp, code]:
   572   "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"
   573   by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)
   574 
   575 lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"
   576   by (induct zs) simp_all
   577 
   578 lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all
   579 
   580 lemma sublisteq_antisym:
   581   assumes "sublisteq xs ys" and "sublisteq ys xs"
   582   shows "xs = ys"
   583 using assms
   584 proof (induct)
   585   case list_emb_Nil
   586   from list_emb_Nil2 [OF this] show ?case by simp
   587 next
   588   case list_emb_Cons2
   589   thus ?case by simp
   590 next
   591   case list_emb_Cons
   592   hence False using sublisteq_Cons' by fastforce
   593   thus ?case ..
   594 qed
   595 
   596 lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"
   597   by (rule list_emb_trans [of _ _ _ "op ="]) auto
   598 
   599 lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"
   600   by (auto dest: list_emb_length)
   601 
   602 lemma list_emb_append_mono:
   603   "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"
   604   apply (induct rule: list_emb.induct)
   605     apply (metis eq_Nil_appendI list_emb_append2)
   606    apply (metis append_Cons list_emb_Cons)
   607   apply (metis append_Cons list_emb_Cons2)
   608   done
   609 
   610 
   611 subsection \<open>Appending elements\<close>
   612 
   613 lemma sublisteq_append [simp]:
   614   "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")
   615 proof
   616   { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
   617     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"
   618     proof (induct arbitrary: xs ys zs)
   619       case list_emb_Nil show ?case by simp
   620     next
   621       case (list_emb_Cons xs' ys' x)
   622       { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
   623       moreover
   624       { fix us assume "ys = x#us"
   625         then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }
   626       ultimately show ?case by (auto simp:Cons_eq_append_conv)
   627     next
   628       case (list_emb_Cons2 x y xs' ys')
   629       { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
   630       moreover
   631       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
   632       moreover
   633       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
   634       ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)
   635     qed }
   636   moreover assume ?l
   637   ultimately show ?r by blast
   638 next
   639   assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl)
   640 qed
   641 
   642 lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"
   643   by (induct zs) auto
   644 
   645 lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"
   646   by (metis append_Nil2 list_emb_Nil list_emb_append_mono)
   647 
   648 
   649 subsection \<open>Relation to standard list operations\<close>
   650 
   651 lemma sublisteq_map:
   652   assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"
   653   using assms by (induct) auto
   654 
   655 lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"
   656   by (induct xs) auto
   657 
   658 lemma sublisteq_filter [simp]:
   659   assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"
   660   using assms by induct auto
   661 
   662 lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")
   663 proof
   664   assume ?L
   665   then show ?R
   666   proof (induct)
   667     case list_emb_Nil show ?case by (metis sublist_empty)
   668   next
   669     case (list_emb_Cons xs ys x)
   670     then obtain N where "xs = sublist ys N" by blast
   671     then have "xs = sublist (x#ys) (Suc ` N)"
   672       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   673     then show ?case by blast
   674   next
   675     case (list_emb_Cons2 x y xs ys)
   676     then obtain N where "xs = sublist ys N" by blast
   677     then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
   678       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   679     moreover from list_emb_Cons2 have "x = y" by simp
   680     ultimately show ?case by blast
   681   qed
   682 next
   683   assume ?R
   684   then obtain N where "xs = sublist ys N" ..
   685   moreover have "sublisteq (sublist ys N) ys"
   686   proof (induct ys arbitrary: N)
   687     case Nil show ?case by simp
   688   next
   689     case Cons then show ?case by (auto simp: sublist_Cons)
   690   qed
   691   ultimately show ?L by simp
   692 qed
   693 
   694 end