src/HOL/Library/Sublist.thy
author traytel
Wed Feb 19 10:30:21 2014 +0100 (2014-02-19)
changeset 55579 207538943038
parent 54538 ba7392b52a7c
child 57497 4106a2bc066a
permissions -rw-r--r--
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
     1 (*  Title:      HOL/Library/Sublist.thy
     2     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     3     Author:     Christian Sternagel, JAIST
     4 *)
     5 
     6 header {* List prefixes, suffixes, and homeomorphic embedding *}
     7 
     8 theory Sublist
     9 imports Main
    10 begin
    11 
    12 subsection {* Prefix order on lists *}
    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 default (auto simp: prefixeq_def prefix_def)
    22 
    23 interpretation prefix_bot: order_bot Nil prefixeq prefix
    24   by default (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 `prefix xs ys` 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 {* Basic properties of prefixes *}
    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. ([]\<Colon>'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_all)[1]
   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 = `ps = a#as`
   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 {* Parallel lists *}
   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 {* Suffix order on lists *}
   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 `suffixeq xs ys` 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 {* Homeomorphic embedding on lists *}
   430 
   431 inductive list_hembeq :: "('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_hembeq_Nil [intro, simp]: "list_hembeq P [] ys"
   435 | list_hembeq_Cons [intro] : "list_hembeq P xs ys \<Longrightarrow> list_hembeq P xs (y#ys)"
   436 | list_hembeq_Cons2 [intro]: "P\<^sup>=\<^sup>= x y \<Longrightarrow> list_hembeq P xs ys \<Longrightarrow> list_hembeq P (x#xs) (y#ys)"
   437 
   438 lemma list_hembeq_Nil2 [simp]:
   439   assumes "list_hembeq P xs []" shows "xs = []"
   440   using assms by (cases rule: list_hembeq.cases) auto
   441 
   442 lemma list_hembeq_refl [simp, intro!]:
   443   "list_hembeq P xs xs"
   444   by (induct xs) auto
   445 
   446 lemma list_hembeq_Cons_Nil [simp]: "list_hembeq P (x#xs) [] = False"
   447 proof -
   448   { assume "list_hembeq P (x#xs) []"
   449     from list_hembeq_Nil2 [OF this] have False by simp
   450   } moreover {
   451     assume False
   452     then have "list_hembeq P (x#xs) []" by simp
   453   } ultimately show ?thesis by blast
   454 qed
   455 
   456 lemma list_hembeq_append2 [intro]: "list_hembeq P xs ys \<Longrightarrow> list_hembeq P xs (zs @ ys)"
   457   by (induct zs) auto
   458 
   459 lemma list_hembeq_prefix [intro]:
   460   assumes "list_hembeq P xs ys" shows "list_hembeq P xs (ys @ zs)"
   461   using assms
   462   by (induct arbitrary: zs) auto
   463 
   464 lemma list_hembeq_ConsD:
   465   assumes "list_hembeq P (x#xs) ys"
   466   shows "\<exists>us v vs. ys = us @ v # vs \<and> P\<^sup>=\<^sup>= x v \<and> list_hembeq P xs vs"
   467 using assms
   468 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   469   case list_hembeq_Cons
   470   then show ?case by (metis append_Cons)
   471 next
   472   case (list_hembeq_Cons2 x y xs ys)
   473   then show ?case by blast
   474 qed
   475 
   476 lemma list_hembeq_appendD:
   477   assumes "list_hembeq P (xs @ ys) zs"
   478   shows "\<exists>us vs. zs = us @ vs \<and> list_hembeq P xs us \<and> list_hembeq P ys vs"
   479 using assms
   480 proof (induction xs arbitrary: ys zs)
   481   case Nil then show ?case by auto
   482 next
   483   case (Cons x xs)
   484   then obtain us v vs where
   485     zs: "zs = us @ v # vs" and p: "P\<^sup>=\<^sup>= x v" and lh: "list_hembeq P (xs @ ys) vs"
   486     by (auto dest: list_hembeq_ConsD)
   487   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
   488     sk: "\<forall>x\<^sub>0 x\<^sub>1. \<not> list_hembeq 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_hembeq P xs (sk\<^sub>0 x\<^sub>0 x\<^sub>1) \<and> list_hembeq P x\<^sub>0 (sk\<^sub>1 x\<^sub>0 x\<^sub>1)"
   489     using Cons(1) by (metis (no_types))
   490   hence "\<forall>x\<^sub>2. list_hembeq P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
   491   thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
   492 qed
   493 
   494 lemma list_hembeq_suffix:
   495   assumes "list_hembeq P xs ys" and "suffix ys zs"
   496   shows "list_hembeq P xs zs"
   497   using assms(2) and list_hembeq_append2 [OF assms(1)] by (auto simp: suffix_def)
   498 
   499 lemma list_hembeq_suffixeq:
   500   assumes "list_hembeq P xs ys" and "suffixeq ys zs"
   501   shows "list_hembeq P xs zs"
   502   using assms and list_hembeq_suffix unfolding suffixeq_suffix_reflclp_conv by auto
   503 
   504 lemma list_hembeq_length: "list_hembeq P xs ys \<Longrightarrow> length xs \<le> length ys"
   505   by (induct rule: list_hembeq.induct) auto
   506 
   507 lemma list_hembeq_trans:
   508   assumes "\<And>x y z. \<lbrakk>x \<in> A; y \<in> A; z \<in> A; P x y; P y z\<rbrakk> \<Longrightarrow> P x z"
   509   shows "\<And>xs ys zs. \<lbrakk>xs \<in> lists A; ys \<in> lists A; zs \<in> lists A;
   510     list_hembeq P xs ys; list_hembeq P ys zs\<rbrakk> \<Longrightarrow> list_hembeq P xs zs"
   511 proof -
   512   fix xs ys zs
   513   assume "list_hembeq P xs ys" and "list_hembeq P ys zs"
   514     and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A"
   515   then show "list_hembeq P xs zs"
   516   proof (induction arbitrary: zs)
   517     case list_hembeq_Nil show ?case by blast
   518   next
   519     case (list_hembeq_Cons xs ys y)
   520     from list_hembeq_ConsD [OF `list_hembeq P (y#ys) zs`] obtain us v vs
   521       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_hembeq P ys vs" by blast
   522     then have "list_hembeq P ys (v#vs)" by blast
   523     then have "list_hembeq P ys zs" unfolding zs by (rule list_hembeq_append2)
   524     from list_hembeq_Cons.IH [OF this] and list_hembeq_Cons.prems show ?case by simp
   525   next
   526     case (list_hembeq_Cons2 x y xs ys)
   527     from list_hembeq_ConsD [OF `list_hembeq P (y#ys) zs`] obtain us v vs
   528       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_hembeq P ys vs" by blast
   529     with list_hembeq_Cons2 have "list_hembeq P xs vs" by simp
   530     moreover have "P\<^sup>=\<^sup>= x v"
   531     proof -
   532       from zs and `zs \<in> lists A` have "v \<in> A" by auto
   533       moreover have "x \<in> A" and "y \<in> A" using list_hembeq_Cons2 by simp_all
   534       ultimately show ?thesis
   535         using `P\<^sup>=\<^sup>= x y` and `P\<^sup>=\<^sup>= y v` and assms
   536         by blast
   537     qed
   538     ultimately have "list_hembeq P (x#xs) (v#vs)" by blast
   539     then show ?case unfolding zs by (rule list_hembeq_append2)
   540   qed
   541 qed
   542 
   543 
   544 subsection {* Sublists (special case of homeomorphic embedding) *}
   545 
   546 abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   547   where "sublisteq xs ys \<equiv> list_hembeq (op =) xs ys"
   548 
   549 lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto
   550 
   551 lemma sublisteq_same_length:
   552   assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"
   553   using assms by (induct) (auto dest: list_hembeq_length)
   554 
   555 lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"
   556   by (metis list_hembeq_length linorder_not_less)
   557 
   558 lemma [code]:
   559   "list_hembeq P [] ys \<longleftrightarrow> True"
   560   "list_hembeq P (x#xs) [] \<longleftrightarrow> False"
   561   by (simp_all)
   562 
   563 lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"
   564   by (induct xs, simp, blast dest: list_hembeq_ConsD)
   565 
   566 lemma sublisteq_Cons2':
   567   assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"
   568   using assms by (cases) (rule sublisteq_Cons')
   569 
   570 lemma sublisteq_Cons2_neq:
   571   assumes "sublisteq (x#xs) (y#ys)"
   572   shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"
   573   using assms by (cases) auto
   574 
   575 lemma sublisteq_Cons2_iff [simp, code]:
   576   "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"
   577   by (metis list_hembeq_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)
   578 
   579 lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"
   580   by (induct zs) simp_all
   581 
   582 lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all
   583 
   584 lemma sublisteq_antisym:
   585   assumes "sublisteq xs ys" and "sublisteq ys xs"
   586   shows "xs = ys"
   587 using assms
   588 proof (induct)
   589   case list_hembeq_Nil
   590   from list_hembeq_Nil2 [OF this] show ?case by simp
   591 next
   592   case list_hembeq_Cons2
   593   thus ?case by simp
   594 next
   595   case list_hembeq_Cons
   596   hence False using sublisteq_Cons' by fastforce
   597   thus ?case ..
   598 qed
   599 
   600 lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"
   601   by (rule list_hembeq_trans [of UNIV "op ="]) auto
   602 
   603 lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"
   604   by (auto dest: list_hembeq_length)
   605 
   606 lemma list_hembeq_append_mono:
   607   "\<lbrakk> list_hembeq P xs xs'; list_hembeq P ys ys' \<rbrakk> \<Longrightarrow> list_hembeq P (xs@ys) (xs'@ys')"
   608   apply (induct rule: list_hembeq.induct)
   609     apply (metis eq_Nil_appendI list_hembeq_append2)
   610    apply (metis append_Cons list_hembeq_Cons)
   611   apply (metis append_Cons list_hembeq_Cons2)
   612   done
   613 
   614 
   615 subsection {* Appending elements *}
   616 
   617 lemma sublisteq_append [simp]:
   618   "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")
   619 proof
   620   { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
   621     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"
   622     proof (induct arbitrary: xs ys zs)
   623       case list_hembeq_Nil show ?case by simp
   624     next
   625       case (list_hembeq_Cons xs' ys' x)
   626       { assume "ys=[]" then have ?case using list_hembeq_Cons(1) by auto }
   627       moreover
   628       { fix us assume "ys = x#us"
   629         then have ?case using list_hembeq_Cons(2) by(simp add: list_hembeq.list_hembeq_Cons) }
   630       ultimately show ?case by (auto simp:Cons_eq_append_conv)
   631     next
   632       case (list_hembeq_Cons2 x y xs' ys')
   633       { assume "xs=[]" then have ?case using list_hembeq_Cons2(1) by auto }
   634       moreover
   635       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_hembeq_Cons2 by auto}
   636       moreover
   637       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_hembeq_Cons2(2) by bestsimp }
   638       ultimately show ?case using `op =\<^sup>=\<^sup>= x y` by (auto simp: Cons_eq_append_conv)
   639     qed }
   640   moreover assume ?l
   641   ultimately show ?r by blast
   642 next
   643   assume ?r then show ?l by (metis list_hembeq_append_mono sublisteq_refl)
   644 qed
   645 
   646 lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"
   647   by (induct zs) auto
   648 
   649 lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"
   650   by (metis append_Nil2 list_hembeq_Nil list_hembeq_append_mono)
   651 
   652 
   653 subsection {* Relation to standard list operations *}
   654 
   655 lemma sublisteq_map:
   656   assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"
   657   using assms by (induct) auto
   658 
   659 lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"
   660   by (induct xs) auto
   661 
   662 lemma sublisteq_filter [simp]:
   663   assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"
   664   using assms by induct auto
   665 
   666 lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")
   667 proof
   668   assume ?L
   669   then show ?R
   670   proof (induct)
   671     case list_hembeq_Nil show ?case by (metis sublist_empty)
   672   next
   673     case (list_hembeq_Cons xs ys x)
   674     then obtain N where "xs = sublist ys N" by blast
   675     then have "xs = sublist (x#ys) (Suc ` N)"
   676       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   677     then show ?case by blast
   678   next
   679     case (list_hembeq_Cons2 x y xs ys)
   680     then obtain N where "xs = sublist ys N" by blast
   681     then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
   682       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   683     moreover from list_hembeq_Cons2 have "x = y" by simp
   684     ultimately show ?case by blast
   685   qed
   686 next
   687   assume ?R
   688   then obtain N where "xs = sublist ys N" ..
   689   moreover have "sublisteq (sublist ys N) ys"
   690   proof (induct ys arbitrary: N)
   691     case Nil show ?case by simp
   692   next
   693     case Cons then show ?case by (auto simp: sublist_Cons)
   694   qed
   695   ultimately show ?L by simp
   696 qed
   697 
   698 end