src/HOL/Library/Sublist.thy
author wenzelm
Wed Sep 12 13:42:28 2012 +0200 (2012-09-12)
changeset 49322 fbb320d02420
parent 49107 ec34e9df0514
child 50516 ed6b40d15d1c
permissions -rw-r--r--
tuned headers;
     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 embedding*}
     7 
     8 theory Sublist
     9 imports Main
    10 begin
    11 
    12 subsection {* Prefix order on lists *}
    13 
    14 definition prefixeq :: "'a list => 'a list => bool"
    15   where "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
    16 
    17 definition prefix :: "'a list => 'a list => 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: bot prefixeq prefix Nil
    24   by default (simp add: prefixeq_def)
    25 
    26 lemma prefixeqI [intro?]: "ys = xs @ zs ==> 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 ==> 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 ==> xs \<noteq> ys ==> 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 ==> 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 ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys"
   110   unfolding prefixeq_def
   111   by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj
   112     eq_Nil_appendI nth_drop')
   113 
   114 theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys"
   115   by (auto simp add: prefixeq_def)
   116 
   117 lemma prefixeq_same_cases:
   118   "prefixeq (xs\<^isub>1::'a list) ys \<Longrightarrow> prefixeq xs\<^isub>2 ys \<Longrightarrow> prefixeq xs\<^isub>1 xs\<^isub>2 \<or> prefixeq xs\<^isub>2 xs\<^isub>1"
   119   unfolding prefixeq_def by (metis append_eq_append_conv2)
   120 
   121 lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   122   by (auto simp add: prefixeq_def)
   123 
   124 lemma take_is_prefixeq: "prefixeq (take n xs) xs"
   125   unfolding prefixeq_def by (metis append_take_drop_id)
   126 
   127 lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)"
   128   by (auto simp: prefixeq_def)
   129 
   130 lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys"
   131   by (auto simp: prefix_def prefixeq_def)
   132 
   133 lemma prefix_simps [simp, code]:
   134   "prefix xs [] \<longleftrightarrow> False"
   135   "prefix [] (x # xs) \<longleftrightarrow> True"
   136   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
   137   by (simp_all add: prefix_def cong: conj_cong)
   138 
   139 lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys"
   140   apply (induct n arbitrary: xs ys)
   141    apply (case_tac ys, simp_all)[1]
   142   apply (metis prefix_order.less_trans prefixI take_is_prefixeq)
   143   done
   144 
   145 lemma not_prefixeq_cases:
   146   assumes pfx: "\<not> prefixeq ps ls"
   147   obtains
   148     (c1) "ps \<noteq> []" and "ls = []"
   149   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs"
   150   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   151 proof (cases ps)
   152   case Nil
   153   then show ?thesis using pfx by simp
   154 next
   155   case (Cons a as)
   156   note c = `ps = a#as`
   157   show ?thesis
   158   proof (cases ls)
   159     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil)
   160   next
   161     case (Cons x xs)
   162     show ?thesis
   163     proof (cases "x = a")
   164       case True
   165       have "\<not> prefixeq as xs" using pfx c Cons True by simp
   166       with c Cons True show ?thesis by (rule c2)
   167     next
   168       case False
   169       with c Cons show ?thesis by (rule c3)
   170     qed
   171   qed
   172 qed
   173 
   174 lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]:
   175   assumes np: "\<not> prefixeq ps ls"
   176     and base: "\<And>x xs. P (x#xs) []"
   177     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   178     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   179   shows "P ps ls" using np
   180 proof (induct ls arbitrary: ps)
   181   case Nil then show ?case
   182     by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base)
   183 next
   184   case (Cons y ys)
   185   then have npfx: "\<not> prefixeq ps (y # ys)" by simp
   186   then obtain x xs where pv: "ps = x # xs"
   187     by (rule not_prefixeq_cases) auto
   188   show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2)
   189 qed
   190 
   191 
   192 subsection {* Parallel lists *}
   193 
   194 definition parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50)
   195   where "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"
   196 
   197 lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> xs \<parallel> ys"
   198   unfolding parallel_def by blast
   199 
   200 lemma parallelE [elim]:
   201   assumes "xs \<parallel> ys"
   202   obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs"
   203   using assms unfolding parallel_def by blast
   204 
   205 theorem prefixeq_cases:
   206   obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys"
   207   unfolding parallel_def prefix_def by blast
   208 
   209 theorem parallel_decomp:
   210   "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   211 proof (induct xs rule: rev_induct)
   212   case Nil
   213   then have False by auto
   214   then show ?case ..
   215 next
   216   case (snoc x xs)
   217   show ?case
   218   proof (rule prefixeq_cases)
   219     assume le: "prefixeq xs ys"
   220     then obtain ys' where ys: "ys = xs @ ys'" ..
   221     show ?thesis
   222     proof (cases ys')
   223       assume "ys' = []"
   224       then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys)
   225     next
   226       fix c cs assume ys': "ys' = c # cs"
   227       then show ?thesis
   228         by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI
   229           same_prefixeq_prefixeq snoc.prems ys)
   230     qed
   231   next
   232     assume "prefix ys xs"
   233     then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def)
   234     with snoc have False by blast
   235     then show ?thesis ..
   236   next
   237     assume "xs \<parallel> ys"
   238     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   239       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   240       by blast
   241     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   242     with neq ys show ?thesis by blast
   243   qed
   244 qed
   245 
   246 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   247   apply (rule parallelI)
   248     apply (erule parallelE, erule conjE,
   249       induct rule: not_prefixeq_induct, simp+)+
   250   done
   251 
   252 lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
   253   by (simp add: parallel_append)
   254 
   255 lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
   256   unfolding parallel_def by auto
   257 
   258 
   259 subsection {* Suffix order on lists *}
   260 
   261 definition suffixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   262   where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
   263 
   264 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   265   where "suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
   266 
   267 lemma suffix_imp_suffixeq:
   268   "suffix xs ys \<Longrightarrow> suffixeq xs ys"
   269   by (auto simp: suffixeq_def suffix_def)
   270 
   271 lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"
   272   unfolding suffixeq_def by blast
   273 
   274 lemma suffixeqE [elim?]:
   275   assumes "suffixeq xs ys"
   276   obtains zs where "ys = zs @ xs"
   277   using assms unfolding suffixeq_def by blast
   278 
   279 lemma suffixeq_refl [iff]: "suffixeq xs xs"
   280   by (auto simp add: suffixeq_def)
   281 lemma suffix_trans:
   282   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
   283   by (auto simp: suffix_def)
   284 lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
   285   by (auto simp add: suffixeq_def)
   286 lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   287   by (auto simp add: suffixeq_def)
   288 
   289 lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"
   290   by (induct xs) (auto simp: suffixeq_def)
   291 
   292 lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"
   293   by (induct xs) (auto simp: suffix_def)
   294 
   295 lemma Nil_suffixeq [iff]: "suffixeq [] xs"
   296   by (simp add: suffixeq_def)
   297 lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
   298   by (auto simp add: suffixeq_def)
   299 
   300 lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y # ys)"
   301   by (auto simp add: suffixeq_def)
   302 lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys"
   303   by (auto simp add: suffixeq_def)
   304 
   305 lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
   306   by (auto simp add: suffixeq_def)
   307 lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
   308   by (auto simp add: suffixeq_def)
   309 
   310 lemma suffix_set_subset:
   311   "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
   312 
   313 lemma suffixeq_set_subset:
   314   "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
   315 
   316 lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> suffixeq xs ys"
   317 proof -
   318   assume "suffixeq (x # xs) (y # ys)"
   319   then obtain zs where "y # ys = zs @ x # xs" ..
   320   then show ?thesis
   321     by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI)
   322 qed
   323 
   324 lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)"
   325 proof
   326   assume "suffixeq xs ys"
   327   then obtain zs where "ys = zs @ xs" ..
   328   then have "rev ys = rev xs @ rev zs" by simp
   329   then show "prefixeq (rev xs) (rev ys)" ..
   330 next
   331   assume "prefixeq (rev xs) (rev ys)"
   332   then obtain zs where "rev ys = rev xs @ zs" ..
   333   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
   334   then have "ys = rev zs @ xs" by simp
   335   then show "suffixeq xs ys" ..
   336 qed
   337 
   338 lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"
   339   by (clarsimp elim!: suffixeqE)
   340 
   341 lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)"
   342   by (auto elim!: suffixeqE intro: suffixeqI)
   343 
   344 lemma suffixeq_drop: "suffixeq (drop n as) as"
   345   unfolding suffixeq_def
   346   apply (rule exI [where x = "take n as"])
   347   apply simp
   348   done
   349 
   350 lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   351   by (auto elim!: suffixeqE)
   352 
   353 lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>="
   354 proof (intro ext iffI)
   355   fix xs ys :: "'a list"
   356   assume "suffixeq xs ys"
   357   show "suffix\<^sup>=\<^sup>= xs ys"
   358   proof
   359     assume "xs \<noteq> ys"
   360     with `suffixeq xs ys` show "suffix xs ys"
   361       by (auto simp: suffixeq_def suffix_def)
   362   qed
   363 next
   364   fix xs ys :: "'a list"
   365   assume "suffix\<^sup>=\<^sup>= xs ys"
   366   then show "suffixeq xs ys"
   367   proof
   368     assume "suffix xs ys" then show "suffixeq xs ys"
   369       by (rule suffix_imp_suffixeq)
   370   next
   371     assume "xs = ys" then show "suffixeq xs ys"
   372       by (auto simp: suffixeq_def)
   373   qed
   374 qed
   375 
   376 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y"
   377   by blast
   378 
   379 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x"
   380   by blast
   381 
   382 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   383   unfolding parallel_def by simp
   384 
   385 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   386   unfolding parallel_def by simp
   387 
   388 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   389   by auto
   390 
   391 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   392   by (metis Cons_prefixeq_Cons parallelE parallelI)
   393 
   394 lemma not_equal_is_parallel:
   395   assumes neq: "xs \<noteq> ys"
   396     and len: "length xs = length ys"
   397   shows "xs \<parallel> ys"
   398   using len neq
   399 proof (induct rule: list_induct2)
   400   case Nil
   401   then show ?case by simp
   402 next
   403   case (Cons a as b bs)
   404   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   405   show ?case
   406   proof (cases "a = b")
   407     case True
   408     then have "as \<noteq> bs" using Cons by simp
   409     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   410   next
   411     case False
   412     then show ?thesis by (rule Cons_parallelI1)
   413   qed
   414 qed
   415 
   416 lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq"
   417   by (intro ext) (auto simp: suffixeq_def suffix_def)
   418 
   419 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
   420   unfolding suffix_def by auto
   421 
   422 
   423 subsection {* Embedding on lists *}
   424 
   425 inductive emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   426   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
   427 where
   428   emb_Nil [intro, simp]: "emb P [] ys"
   429 | emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)"
   430 | emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)"
   431 
   432 lemma emb_Nil2 [simp]:
   433   assumes "emb P xs []" shows "xs = []"
   434   using assms by (cases rule: emb.cases) auto
   435 
   436 lemma emb_Cons_Nil [simp]: "emb P (x#xs) [] = False"
   437 proof -
   438   { assume "emb P (x#xs) []"
   439     from emb_Nil2 [OF this] have False by simp
   440   } moreover {
   441     assume False
   442     then have "emb P (x#xs) []" by simp
   443   } ultimately show ?thesis by blast
   444 qed
   445 
   446 lemma emb_append2 [intro]: "emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)"
   447   by (induct zs) auto
   448 
   449 lemma emb_prefix [intro]:
   450   assumes "emb P xs ys" shows "emb P xs (ys @ zs)"
   451   using assms
   452   by (induct arbitrary: zs) auto
   453 
   454 lemma emb_ConsD:
   455   assumes "emb P (x#xs) ys"
   456   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> emb P xs vs"
   457 using assms
   458 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   459   case emb_Cons
   460   then show ?case by (metis append_Cons)
   461 next
   462   case (emb_Cons2 x y xs ys)
   463   then show ?case by (cases xs) (auto, blast+)
   464 qed
   465 
   466 lemma emb_appendD:
   467   assumes "emb P (xs @ ys) zs"
   468   shows "\<exists>us vs. zs = us @ vs \<and> emb P xs us \<and> emb P ys vs"
   469 using assms
   470 proof (induction xs arbitrary: ys zs)
   471   case Nil then show ?case by auto
   472 next
   473   case (Cons x xs)
   474   then obtain us v vs where "zs = us @ v # vs"
   475     and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)
   476   with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)
   477 qed
   478 
   479 lemma emb_suffix:
   480   assumes "emb P xs ys" and "suffix ys zs"
   481   shows "emb P xs zs"
   482   using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def)
   483 
   484 lemma emb_suffixeq:
   485   assumes "emb P xs ys" and "suffixeq ys zs"
   486   shows "emb P xs zs"
   487   using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto
   488 
   489 lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys"
   490   by (induct rule: emb.induct) auto
   491 
   492 (*FIXME: move*)
   493 definition transp_on :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
   494   where "transp_on P A \<longleftrightarrow> (\<forall>a\<in>A. \<forall>b\<in>A. \<forall>c\<in>A. P a b \<and> P b c \<longrightarrow> P a c)"
   495 lemma transp_onI [Pure.intro]:
   496   "(\<And>a b c. \<lbrakk>a \<in> A; b \<in> A; c \<in> A; P a b; P b c\<rbrakk> \<Longrightarrow> P a c) \<Longrightarrow> transp_on P A"
   497   unfolding transp_on_def by blast
   498 
   499 lemma transp_on_emb:
   500   assumes "transp_on P A"
   501   shows "transp_on (emb P) (lists A)"
   502 proof
   503   fix xs ys zs
   504   assume "emb P xs ys" and "emb P ys zs"
   505     and "xs \<in> lists A" and "ys \<in> lists A" and "zs \<in> lists A"
   506   then show "emb P xs zs"
   507   proof (induction arbitrary: zs)
   508     case emb_Nil show ?case by blast
   509   next
   510     case (emb_Cons xs ys y)
   511     from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs
   512       where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast
   513     then have "emb P ys (v#vs)" by blast
   514     then have "emb P ys zs" unfolding zs by (rule emb_append2)
   515     from emb_Cons.IH [OF this] and emb_Cons.prems show ?case by simp
   516   next
   517     case (emb_Cons2 x y xs ys)
   518     from emb_ConsD [OF `emb P (y#ys) zs`] obtain us v vs
   519       where zs: "zs = us @ v # vs" and "P y v" and "emb P ys vs" by blast
   520     with emb_Cons2 have "emb P xs vs" by simp
   521     moreover have "P x v"
   522     proof -
   523       from zs and `zs \<in> lists A` have "v \<in> A" by auto
   524       moreover have "x \<in> A" and "y \<in> A" using emb_Cons2 by simp_all
   525       ultimately show ?thesis using `P x y` and `P y v` and assms
   526         unfolding transp_on_def by blast
   527     qed
   528     ultimately have "emb P (x#xs) (v#vs)" by blast
   529     then show ?case unfolding zs by (rule emb_append2)
   530   qed
   531 qed
   532 
   533 
   534 subsection {* Sublists (special case of embedding) *}
   535 
   536 abbreviation sub :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   537   where "sub xs ys \<equiv> emb (op =) xs ys"
   538 
   539 lemma sub_Cons2: "sub xs ys \<Longrightarrow> sub (x#xs) (x#ys)" by auto
   540 
   541 lemma sub_same_length:
   542   assumes "sub xs ys" and "length xs = length ys" shows "xs = ys"
   543   using assms by (induct) (auto dest: emb_length)
   544 
   545 lemma not_sub_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sub xs ys"
   546   by (metis emb_length linorder_not_less)
   547 
   548 lemma [code]:
   549   "emb P [] ys \<longleftrightarrow> True"
   550   "emb P (x#xs) [] \<longleftrightarrow> False"
   551   by (simp_all)
   552 
   553 lemma sub_Cons': "sub (x#xs) ys \<Longrightarrow> sub xs ys"
   554   by (induct xs) (auto dest: emb_ConsD)
   555 
   556 lemma sub_Cons2':
   557   assumes "sub (x#xs) (x#ys)" shows "sub xs ys"
   558   using assms by (cases) (rule sub_Cons')
   559 
   560 lemma sub_Cons2_neq:
   561   assumes "sub (x#xs) (y#ys)"
   562   shows "x \<noteq> y \<Longrightarrow> sub (x#xs) ys"
   563   using assms by (cases) auto
   564 
   565 lemma sub_Cons2_iff [simp, code]:
   566   "sub (x#xs) (y#ys) = (if x = y then sub xs ys else sub (x#xs) ys)"
   567   by (metis emb_Cons emb_Cons2 [of "op =", OF refl] sub_Cons2' sub_Cons2_neq)
   568 
   569 lemma sub_append': "sub (zs @ xs) (zs @ ys) \<longleftrightarrow> sub xs ys"
   570   by (induct zs) simp_all
   571 
   572 lemma sub_refl [simp, intro!]: "sub xs xs" by (induct xs) simp_all
   573 
   574 lemma sub_antisym:
   575   assumes "sub xs ys" and "sub ys xs"
   576   shows "xs = ys"
   577 using assms
   578 proof (induct)
   579   case emb_Nil
   580   from emb_Nil2 [OF this] show ?case by simp
   581 next
   582   case emb_Cons2
   583   then show ?case by simp
   584 next
   585   case emb_Cons
   586   then show ?case
   587     by (metis sub_Cons' emb_length Suc_length_conv Suc_n_not_le_n)
   588 qed
   589 
   590 lemma transp_on_sub: "transp_on sub UNIV"
   591 proof -
   592   have "transp_on (op =) UNIV" by (simp add: transp_on_def)
   593   from transp_on_emb [OF this] show ?thesis by simp
   594 qed
   595 
   596 lemma sub_trans: "sub xs ys \<Longrightarrow> sub ys zs \<Longrightarrow> sub xs zs"
   597   using transp_on_sub [unfolded transp_on_def] by blast
   598 
   599 lemma sub_append_le_same_iff: "sub (xs @ ys) ys \<longleftrightarrow> xs = []"
   600   by (auto dest: emb_length)
   601 
   602 lemma emb_append_mono:
   603   "\<lbrakk> emb P xs xs'; emb P ys ys' \<rbrakk> \<Longrightarrow> emb P (xs@ys) (xs'@ys')"
   604   apply (induct rule: emb.induct)
   605     apply (metis eq_Nil_appendI emb_append2)
   606    apply (metis append_Cons emb_Cons)
   607   apply (metis append_Cons emb_Cons2)
   608   done
   609 
   610 
   611 subsection {* Appending elements *}
   612 
   613 lemma sub_append [simp]:
   614   "sub (xs @ zs) (ys @ zs) \<longleftrightarrow> sub xs ys" (is "?l = ?r")
   615 proof
   616   { fix xs' ys' xs ys zs :: "'a list" assume "sub xs' ys'"
   617     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sub xs ys"
   618     proof (induct arbitrary: xs ys zs)
   619       case emb_Nil show ?case by simp
   620     next
   621       case (emb_Cons xs' ys' x)
   622       { assume "ys=[]" then have ?case using emb_Cons(1) by auto }
   623       moreover
   624       { fix us assume "ys = x#us"
   625         then have ?case using emb_Cons(2) by(simp add: emb.emb_Cons) }
   626       ultimately show ?case by (auto simp:Cons_eq_append_conv)
   627     next
   628       case (emb_Cons2 x y xs' ys')
   629       { assume "xs=[]" then have ?case using emb_Cons2(1) by auto }
   630       moreover
   631       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using emb_Cons2 by auto}
   632       moreover
   633       { fix us assume "xs=x#us" "ys=[]" then have ?case using emb_Cons2(2) by bestsimp }
   634       ultimately show ?case using `x = y` 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 emb_append_mono sub_refl)
   640 qed
   641 
   642 lemma sub_drop_many: "sub xs ys \<Longrightarrow> sub xs (zs @ ys)"
   643   by (induct zs) auto
   644 
   645 lemma sub_rev_drop_many: "sub xs ys \<Longrightarrow> sub xs (ys @ zs)"
   646   by (metis append_Nil2 emb_Nil emb_append_mono)
   647 
   648 
   649 subsection {* Relation to standard list operations *}
   650 
   651 lemma sub_map:
   652   assumes "sub xs ys" shows "sub (map f xs) (map f ys)"
   653   using assms by (induct) auto
   654 
   655 lemma sub_filter_left [simp]: "sub (filter P xs) xs"
   656   by (induct xs) auto
   657 
   658 lemma sub_filter [simp]:
   659   assumes "sub xs ys" shows "sub (filter P xs) (filter P ys)"
   660   using assms by (induct) auto
   661 
   662 lemma "sub 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 emb_Nil show ?case by (metis sublist_empty)
   668   next
   669     case (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 (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     then show ?case unfolding `x = y` by blast
   680   qed
   681 next
   682   assume ?R
   683   then obtain N where "xs = sublist ys N" ..
   684   moreover have "sub (sublist ys N) ys"
   685   proof (induct ys arbitrary: N)
   686     case Nil show ?case by simp
   687   next
   688     case Cons then show ?case by (auto simp: sublist_Cons)
   689   qed
   690   ultimately show ?L by simp
   691 qed
   692 
   693 end