src/HOL/Library/Sublist.thy
author wenzelm
Wed Aug 10 14:50:59 2016 +0200 (2016-08-10)
changeset 63649 e690d6f2185b
parent 63173 3413b1cf30cd
child 64886 cea327ecb8e3
permissions -rw-r--r--
tuned proofs;
     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 (* FIXME rm *)
    47 lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys"
    48 by(fact prefix_order.le_neq_trans)
    49 
    50 lemma strict_prefixE [elim?]:
    51   fixes xs ys :: "'a list"
    52   assumes "strict_prefix xs ys"
    53   obtains "prefix xs ys" and "xs \<noteq> ys"
    54   using assms unfolding strict_prefix_def by blast
    55 
    56 
    57 subsection \<open>Basic properties of prefixes\<close>
    58 
    59 (* FIXME rm *)
    60 theorem Nil_prefix [iff]: "prefix [] xs"
    61 by(fact prefix_bot.bot_least)
    62 
    63 (* FIXME rm *)
    64 theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])"
    65 by(fact prefix_bot.bot_unique)
    66 
    67 lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys"
    68 proof
    69   assume "prefix xs (ys @ [y])"
    70   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
    71   show "xs = ys @ [y] \<or> prefix xs ys"
    72     by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
    73 next
    74   assume "xs = ys @ [y] \<or> prefix xs ys"
    75   then show "prefix xs (ys @ [y])"
    76     by (metis prefix_order.eq_iff prefix_order.order_trans prefixI)
    77 qed
    78 
    79 lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)"
    80   by (auto simp add: prefix_def)
    81 
    82 lemma prefix_code [code]:
    83   "prefix [] xs \<longleftrightarrow> True"
    84   "prefix (x # xs) [] \<longleftrightarrow> False"
    85   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
    86   by simp_all
    87 
    88 lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs"
    89   by (induct xs) simp_all
    90 
    91 lemma same_prefix_nil [iff]: "prefix (xs @ ys) xs = (ys = [])"
    92   by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)
    93 
    94 lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)"
    95   by (metis prefix_order.le_less_trans prefixI strict_prefixE strict_prefixI)
    96 
    97 lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs"
    98   by (auto simp add: prefix_def)
    99 
   100 theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))"
   101   by (cases xs) (auto simp add: prefix_def)
   102 
   103 theorem prefix_append:
   104   "prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix us zs))"
   105   apply (induct zs rule: rev_induct)
   106    apply force
   107   apply (simp del: append_assoc add: append_assoc [symmetric])
   108   apply (metis append_eq_appendI)
   109   done
   110 
   111 lemma append_one_prefix:
   112   "prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys"
   113   proof (unfold prefix_def)
   114     assume a1: "\<exists>zs. ys = xs @ zs"
   115     then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce
   116     assume a2: "length xs < length ys"
   117     have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp
   118     have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force
   119     hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)
   120     thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce
   121   qed
   122 
   123 theorem prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys"
   124   by (auto simp add: prefix_def)
   125 
   126 lemma prefix_same_cases:
   127   "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"
   128   unfolding prefix_def by (force simp: append_eq_append_conv2)
   129 
   130 lemma prefix_length_prefix:
   131   "prefix ps xs \<Longrightarrow> prefix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> prefix ps qs"
   132 by (auto simp: prefix_def) (metis append_Nil2 append_eq_append_conv_if)
   133 
   134 lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   135   by (auto simp add: prefix_def)
   136 
   137 lemma take_is_prefix: "prefix (take n xs) xs"
   138   unfolding prefix_def by (metis append_take_drop_id)
   139 
   140 lemma prefixeq_butlast: "prefix (butlast xs) xs"
   141 by (simp add: butlast_conv_take take_is_prefix)
   142 
   143 lemma map_prefixI: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"
   144   by (auto simp: prefix_def)
   145 
   146 lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys"
   147   by (auto simp: strict_prefix_def prefix_def)
   148 
   149 lemma prefix_snocD: "prefix (xs@[x]) ys \<Longrightarrow> strict_prefix xs ys"
   150   by (simp add: strict_prefixI' prefix_order.dual_order.strict_trans1)
   151 
   152 lemma strict_prefix_simps [simp, code]:
   153   "strict_prefix xs [] \<longleftrightarrow> False"
   154   "strict_prefix [] (x # xs) \<longleftrightarrow> True"
   155   "strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys"
   156   by (simp_all add: strict_prefix_def cong: conj_cong)
   157 
   158 lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_prefix (take n xs) ys"
   159 proof (induct n arbitrary: xs ys)
   160   case 0
   161   then show ?case by (cases ys) simp_all
   162 next
   163   case (Suc n)
   164   then show ?case by (metis prefix_order.less_trans strict_prefixI take_is_prefix)
   165 qed
   166 
   167 lemma not_prefix_cases:
   168   assumes pfx: "\<not> prefix ps ls"
   169   obtains
   170     (c1) "ps \<noteq> []" and "ls = []"
   171   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefix as xs"
   172   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   173 proof (cases ps)
   174   case Nil
   175   then show ?thesis using pfx by simp
   176 next
   177   case (Cons a as)
   178   note c = \<open>ps = a#as\<close>
   179   show ?thesis
   180   proof (cases ls)
   181     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
   182   next
   183     case (Cons x xs)
   184     show ?thesis
   185     proof (cases "x = a")
   186       case True
   187       have "\<not> prefix as xs" using pfx c Cons True by simp
   188       with c Cons True show ?thesis by (rule c2)
   189     next
   190       case False
   191       with c Cons show ?thesis by (rule c3)
   192     qed
   193   qed
   194 qed
   195 
   196 lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
   197   assumes np: "\<not> prefix ps ls"
   198     and base: "\<And>x xs. P (x#xs) []"
   199     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   200     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   201   shows "P ps ls" using np
   202 proof (induct ls arbitrary: ps)
   203   case Nil
   204   then show ?case
   205     by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
   206 next
   207   case (Cons y ys)
   208   then have npfx: "\<not> prefix ps (y # ys)" by simp
   209   then obtain x xs where pv: "ps = x # xs"
   210     by (rule not_prefix_cases) auto
   211   show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
   212 qed
   213 
   214 
   215 subsection \<open>Prefixes\<close>
   216 
   217 fun prefixes where
   218 "prefixes [] = [[]]" |
   219 "prefixes (x#xs) = [] # map (op # x) (prefixes xs)"
   220 
   221 lemma in_set_prefixes[simp]: "xs \<in> set (prefixes ys) \<longleftrightarrow> prefix xs ys"
   222 proof (induct xs arbitrary: ys)
   223   case Nil
   224   then show ?case by (cases ys) auto
   225 next
   226   case (Cons a xs)
   227   then show ?case by (cases ys) auto
   228 qed
   229 
   230 lemma length_prefixes[simp]: "length (prefixes xs) = length xs+1"
   231 by (induction xs) auto
   232 
   233 lemma prefixes_snoc[simp]:
   234   "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]"
   235 by (induction xs) auto
   236 
   237 lemma prefixes_eq_Snoc:
   238   "prefixes ys = xs @ [x] \<longleftrightarrow>
   239   (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = zs@[z] \<and> xs = prefixes zs)) \<and> x = ys"
   240 by (cases ys rule: rev_cases) auto
   241 
   242 
   243 subsection \<open>Longest Common Prefix\<close>
   244 
   245 definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where
   246 "Longest_common_prefix L = (GREATEST ps WRT length. \<forall>xs \<in> L. prefix ps xs)"
   247 
   248 lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow>
   249   \<exists>ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)"
   250   (is "_ \<Longrightarrow> \<exists>ps. ?P L ps")
   251 proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L)
   252   case 0
   253   have "[] : L" using "0.hyps" LeastI[of "\<lambda>n. \<exists>xs\<in>L. n = length xs"] \<open>L \<noteq> {}\<close>
   254     by auto
   255   hence "?P L []" by(auto)
   256   thus ?case ..
   257 next
   258   case (Suc n)
   259   let ?EX = "\<lambda>n. \<exists>xs\<in>L. n = length xs"
   260   obtain x xs where xxs: "x#xs \<in> L" "size xs = n" using Suc.prems Suc.hyps(2)
   261     by(metis LeastI_ex[of ?EX] Suc_length_conv ex_in_conv)
   262   hence "[] \<notin> L" using Suc.hyps(2) by auto
   263   show ?case
   264   proof (cases "\<forall>xs \<in> L. \<exists>ys. xs = x#ys")
   265     case True
   266     let ?L = "{ys. x#ys \<in> L}"
   267     have 1: "(LEAST n. \<exists>xs \<in> ?L. n = length xs) = n"
   268       using xxs Suc.prems Suc.hyps(2) Least_le[of "?EX"]
   269       by - (rule Least_equality, fastforce+)
   270     have 2: "?L \<noteq> {}" using \<open>x # xs \<in> L\<close> by auto
   271     from Suc.hyps(1)[OF 1[symmetric] 2] obtain ps where IH: "?P ?L ps" ..
   272     { fix qs
   273       assume "\<forall>qs. (\<forall>xa. x # xa \<in> L \<longrightarrow> prefix qs xa) \<longrightarrow> length qs \<le> length ps"
   274       and "\<forall>xs\<in>L. prefix qs xs"
   275       hence "length (tl qs) \<le> length ps"
   276         by (metis Cons_prefix_Cons hd_Cons_tl list.sel(2) Nil_prefix) 
   277       hence "length qs \<le> Suc (length ps)" by auto
   278     }
   279     hence "?P L (x#ps)" using True IH by auto
   280     thus ?thesis ..
   281   next
   282     case False
   283     then obtain y ys where yys: "x\<noteq>y" "y#ys \<in> L" using \<open>[] \<notin> L\<close>
   284       by (auto) (metis list.exhaust)
   285     have "\<forall>qs. (\<forall>xs\<in>L. prefix qs xs) \<longrightarrow> qs = []" using yys \<open>x#xs \<in> L\<close>
   286       by auto (metis Cons_prefix_Cons prefix_Cons)
   287     hence "?P L []" by auto
   288     thus ?thesis ..
   289   qed
   290 qed
   291 
   292 lemma Longest_common_prefix_unique: "L \<noteq> {} \<Longrightarrow>
   293   \<exists>! ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)"
   294 by(rule ex_ex1I[OF Longest_common_prefix_ex];
   295    meson equals0I prefix_length_prefix prefix_order.antisym)
   296 
   297 lemma Longest_common_prefix_eq:
   298  "\<lbrakk> L \<noteq> {};  \<forall>xs \<in> L. prefix ps xs;
   299     \<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk>
   300   \<Longrightarrow> Longest_common_prefix L = ps"
   301 unfolding Longest_common_prefix_def GreatestM_def
   302 by(rule some1_equality[OF Longest_common_prefix_unique]) auto
   303 
   304 lemma Longest_common_prefix_prefix:
   305   "xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs"
   306 unfolding Longest_common_prefix_def GreatestM_def
   307 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto
   308 
   309 lemma Longest_common_prefix_longest:
   310   "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)"
   311 unfolding Longest_common_prefix_def GreatestM_def
   312 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto
   313 
   314 lemma Longest_common_prefix_max_prefix:
   315   "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)"
   316 by(metis Longest_common_prefix_prefix Longest_common_prefix_longest
   317      prefix_length_prefix ex_in_conv)
   318 
   319 lemma Longest_common_prefix_Nil: "[] \<in> L \<Longrightarrow> Longest_common_prefix L = []"
   320 using Longest_common_prefix_prefix prefix_Nil by blast
   321 
   322 lemma Longest_common_prefix_image_Cons: "L \<noteq> {} \<Longrightarrow>
   323   Longest_common_prefix (op # x ` L) = x # Longest_common_prefix L"
   324 apply(rule Longest_common_prefix_eq)
   325   apply(simp)
   326  apply (simp add: Longest_common_prefix_prefix)
   327 apply simp
   328 by(metis Longest_common_prefix_longest[of L] Cons_prefix_Cons Nitpick.size_list_simp(2)
   329      Suc_le_mono hd_Cons_tl order.strict_implies_order zero_less_Suc)
   330 
   331 lemma Longest_common_prefix_eq_Cons: assumes "L \<noteq> {}" "[] \<notin> L"  "\<forall>xs\<in>L. hd xs = x"
   332 shows "Longest_common_prefix L = x # Longest_common_prefix {ys. x#ys \<in> L}"
   333 proof -
   334   have "L = op # x ` {ys. x#ys \<in> L}" using assms(2,3)
   335     by (auto simp: image_def)(metis hd_Cons_tl)
   336   thus ?thesis
   337     by (metis Longest_common_prefix_image_Cons image_is_empty assms(1))
   338 qed
   339 
   340 lemma Longest_common_prefix_eq_Nil:
   341   "\<lbrakk>x#ys \<in> L; y#zs \<in> L; x \<noteq> y \<rbrakk> \<Longrightarrow> Longest_common_prefix L = []"
   342 by (metis Longest_common_prefix_prefix list.inject prefix_Cons)
   343 
   344 
   345 fun longest_common_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   346 "longest_common_prefix (x#xs) (y#ys) =
   347   (if x=y then x # longest_common_prefix xs ys else [])" |
   348 "longest_common_prefix _ _ = []"
   349 
   350 lemma longest_common_prefix_prefix1:
   351   "prefix (longest_common_prefix xs ys) xs"
   352 by(induction xs ys rule: longest_common_prefix.induct) auto
   353 
   354 lemma longest_common_prefix_prefix2:
   355   "prefix (longest_common_prefix xs ys) ys"
   356 by(induction xs ys rule: longest_common_prefix.induct) auto
   357 
   358 lemma longest_common_prefix_max_prefix:
   359   "\<lbrakk> prefix ps xs; prefix ps ys \<rbrakk>
   360    \<Longrightarrow> prefix ps (longest_common_prefix xs ys)"
   361 by(induction xs ys arbitrary: ps rule: longest_common_prefix.induct)
   362   (auto simp: prefix_Cons)
   363 
   364 
   365 subsection \<open>Parallel lists\<close>
   366 
   367 definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "\<parallel>" 50)
   368   where "(xs \<parallel> ys) = (\<not> prefix xs ys \<and> \<not> prefix ys xs)"
   369 
   370 lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix ys xs \<Longrightarrow> xs \<parallel> ys"
   371   unfolding parallel_def by blast
   372 
   373 lemma parallelE [elim]:
   374   assumes "xs \<parallel> ys"
   375   obtains "\<not> prefix xs ys \<and> \<not> prefix ys xs"
   376   using assms unfolding parallel_def by blast
   377 
   378 theorem prefix_cases:
   379   obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys"
   380   unfolding parallel_def strict_prefix_def by blast
   381 
   382 theorem parallel_decomp:
   383   "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   384 proof (induct xs rule: rev_induct)
   385   case Nil
   386   then have False by auto
   387   then show ?case ..
   388 next
   389   case (snoc x xs)
   390   show ?case
   391   proof (rule prefix_cases)
   392     assume le: "prefix xs ys"
   393     then obtain ys' where ys: "ys = xs @ ys'" ..
   394     show ?thesis
   395     proof (cases ys')
   396       assume "ys' = []"
   397       then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
   398     next
   399       fix c cs assume ys': "ys' = c # cs"
   400       have "x \<noteq> c" using snoc.prems ys ys' by fastforce
   401       thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs"
   402         using ys ys' by blast
   403     qed
   404   next
   405     assume "strict_prefix ys xs"
   406     then have "prefix ys (xs @ [x])" by (simp add: strict_prefix_def)
   407     with snoc have False by blast
   408     then show ?thesis ..
   409   next
   410     assume "xs \<parallel> ys"
   411     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   412       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   413       by blast
   414     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   415     with neq ys show ?thesis by blast
   416   qed
   417 qed
   418 
   419 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   420   apply (rule parallelI)
   421     apply (erule parallelE, erule conjE,
   422       induct rule: not_prefix_induct, simp+)+
   423   done
   424 
   425 lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
   426   by (simp add: parallel_append)
   427 
   428 lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
   429   unfolding parallel_def by auto
   430 
   431 
   432 subsection \<open>Suffix order on lists\<close>
   433 
   434 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   435   where "suffix xs ys = (\<exists>zs. ys = zs @ xs)"
   436 
   437 definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   438   where "strict_suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
   439 
   440 lemma strict_suffix_imp_suffix:
   441   "strict_suffix xs ys \<Longrightarrow> suffix xs ys"
   442   by (auto simp: suffix_def strict_suffix_def)
   443 
   444 lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys"
   445   unfolding suffix_def by blast
   446 
   447 lemma suffixE [elim?]:
   448   assumes "suffix xs ys"
   449   obtains zs where "ys = zs @ xs"
   450   using assms unfolding suffix_def by blast
   451 
   452 lemma suffix_refl [iff]: "suffix xs xs"
   453   by (auto simp add: suffix_def)
   454 
   455 lemma suffix_trans:
   456   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
   457   by (auto simp: suffix_def)
   458 
   459 lemma strict_suffix_trans:
   460   "\<lbrakk>strict_suffix xs ys; strict_suffix ys zs\<rbrakk> \<Longrightarrow> strict_suffix xs zs"
   461 by (auto simp add: strict_suffix_def)
   462 
   463 lemma suffix_antisym: "\<lbrakk>suffix xs ys; suffix ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   464   by (auto simp add: suffix_def)
   465 
   466 lemma suffix_tl [simp]: "suffix (tl xs) xs"
   467   by (induct xs) (auto simp: suffix_def)
   468 
   469 lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs"
   470   by (induct xs) (auto simp: strict_suffix_def)
   471 
   472 lemma Nil_suffix [iff]: "suffix [] xs"
   473   by (simp add: suffix_def)
   474 
   475 lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])"
   476   by (auto simp add: suffix_def)
   477 
   478 lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)"
   479   by (auto simp add: suffix_def)
   480 
   481 lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys"
   482   by (auto simp add: suffix_def)
   483 
   484 lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)"
   485   by (auto simp add: suffix_def)
   486 
   487 lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys"
   488   by (auto simp add: suffix_def)
   489 
   490 lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   491 by (auto simp: strict_suffix_def)
   492 
   493 lemma suffix_set_subset: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   494 by (auto simp: suffix_def)
   495 
   496 lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys"
   497 proof -
   498   assume "suffix (x # xs) (y # ys)"
   499   then obtain zs where "y # ys = zs @ x # xs" ..
   500   then show ?thesis
   501     by (induct zs) (auto intro!: suffix_appendI suffix_ConsI)
   502 qed
   503 
   504 lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"
   505 proof
   506   assume "suffix xs ys"
   507   then obtain zs where "ys = zs @ xs" ..
   508   then have "rev ys = rev xs @ rev zs" by simp
   509   then show "prefix (rev xs) (rev ys)" ..
   510 next
   511   assume "prefix (rev xs) (rev ys)"
   512   then obtain zs where "rev ys = rev xs @ zs" ..
   513   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
   514   then have "ys = rev zs @ xs" by simp
   515   then show "suffix xs ys" ..
   516 qed
   517 
   518 lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs"
   519   by (clarsimp elim!: suffixE)
   520 
   521 lemma suffix_map: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"
   522   by (auto elim!: suffixE intro: suffixI)
   523 
   524 lemma suffix_drop: "suffix (drop n as) as"
   525   unfolding suffix_def
   526   apply (rule exI [where x = "take n as"])
   527   apply simp
   528   done
   529 
   530 lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   531   by (auto elim!: suffixE)
   532 
   533 lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix"
   534 by (intro ext) (auto simp: suffix_def strict_suffix_def)
   535 
   536 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
   537   unfolding suffix_def by auto
   538 
   539 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y"
   540   by blast
   541 
   542 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x"
   543   by blast
   544 
   545 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   546   unfolding parallel_def by simp
   547 
   548 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   549   unfolding parallel_def by simp
   550 
   551 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   552   by auto
   553 
   554 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   555   by (metis Cons_prefix_Cons parallelE parallelI)
   556 
   557 lemma not_equal_is_parallel:
   558   assumes neq: "xs \<noteq> ys"
   559     and len: "length xs = length ys"
   560   shows "xs \<parallel> ys"
   561   using len neq
   562 proof (induct rule: list_induct2)
   563   case Nil
   564   then show ?case by simp
   565 next
   566   case (Cons a as b bs)
   567   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   568   show ?case
   569   proof (cases "a = b")
   570     case True
   571     then have "as \<noteq> bs" using Cons by simp
   572     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   573   next
   574     case False
   575     then show ?thesis by (rule Cons_parallelI1)
   576   qed
   577 qed
   578 
   579 
   580 subsection \<open>Homeomorphic embedding on lists\<close>
   581 
   582 inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   583   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
   584 where
   585   list_emb_Nil [intro, simp]: "list_emb P [] ys"
   586 | list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"
   587 | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
   588 
   589 lemma list_emb_mono:                         
   590   assumes "\<And>x y. P x y \<longrightarrow> Q x y"
   591   shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"
   592 proof                                        
   593   assume "list_emb P xs ys"                    
   594   then show "list_emb Q xs ys" by (induct) (auto simp: assms)
   595 qed 
   596 
   597 lemma list_emb_Nil2 [simp]:
   598   assumes "list_emb P xs []" shows "xs = []"
   599   using assms by (cases rule: list_emb.cases) auto
   600 
   601 lemma list_emb_refl:
   602   assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"
   603   shows "list_emb P xs xs"
   604   using assms by (induct xs) auto
   605 
   606 lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"
   607 proof -
   608   { assume "list_emb P (x#xs) []"
   609     from list_emb_Nil2 [OF this] have False by simp
   610   } moreover {
   611     assume False
   612     then have "list_emb P (x#xs) []" by simp
   613   } ultimately show ?thesis by blast
   614 qed
   615 
   616 lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"
   617   by (induct zs) auto
   618 
   619 lemma list_emb_prefix [intro]:
   620   assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"
   621   using assms
   622   by (induct arbitrary: zs) auto
   623 
   624 lemma list_emb_ConsD:
   625   assumes "list_emb P (x#xs) ys"
   626   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
   627 using assms
   628 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   629   case list_emb_Cons
   630   then show ?case by (metis append_Cons)
   631 next
   632   case (list_emb_Cons2 x y xs ys)
   633   then show ?case by blast
   634 qed
   635 
   636 lemma list_emb_appendD:
   637   assumes "list_emb P (xs @ ys) zs"
   638   shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"
   639 using assms
   640 proof (induction xs arbitrary: ys zs)
   641   case Nil then show ?case by auto
   642 next
   643   case (Cons x xs)
   644   then obtain us v vs where
   645     zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"
   646     by (auto dest: list_emb_ConsD)
   647   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
   648     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)"
   649     using Cons(1) by (metis (no_types))
   650   hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
   651   thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
   652 qed
   653 
   654 lemma list_emb_strict_suffix:
   655   assumes "list_emb P xs ys" and "strict_suffix ys zs"
   656   shows "list_emb P xs zs"
   657   using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def)
   658 
   659 lemma list_emb_suffix:
   660   assumes "list_emb P xs ys" and "suffix ys zs"
   661   shows "list_emb P xs zs"
   662 using assms and list_emb_strict_suffix
   663 unfolding strict_suffix_reflclp_conv[symmetric] by auto
   664 
   665 lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"
   666   by (induct rule: list_emb.induct) auto
   667 
   668 lemma list_emb_trans:
   669   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"
   670   shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"
   671 proof -
   672   assume "list_emb P xs ys" and "list_emb P ys zs"
   673   then show "list_emb P xs zs" using assms
   674   proof (induction arbitrary: zs)
   675     case list_emb_Nil show ?case by blast
   676   next
   677     case (list_emb_Cons xs ys y)
   678     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   679       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast
   680     then have "list_emb P ys (v#vs)" by blast
   681     then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)
   682     from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto
   683   next
   684     case (list_emb_Cons2 x y xs ys)
   685     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   686       where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast
   687     with list_emb_Cons2 have "list_emb P xs vs" by auto
   688     moreover have "P x v"
   689     proof -
   690       from zs have "v \<in> set zs" by auto
   691       moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all
   692       ultimately show ?thesis
   693         using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2
   694         by blast
   695     qed
   696     ultimately have "list_emb P (x#xs) (v#vs)" by blast
   697     then show ?case unfolding zs by (rule list_emb_append2)
   698   qed
   699 qed
   700 
   701 lemma list_emb_set:
   702   assumes "list_emb P xs ys" and "x \<in> set xs"
   703   obtains y where "y \<in> set ys" and "P x y"
   704   using assms by (induct) auto
   705 
   706 
   707 subsection \<open>Sublists (special case of homeomorphic embedding)\<close>
   708 
   709 abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   710   where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"
   711 
   712 lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto
   713 
   714 lemma sublisteq_same_length:
   715   assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"
   716   using assms by (induct) (auto dest: list_emb_length)
   717 
   718 lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"
   719   by (metis list_emb_length linorder_not_less)
   720 
   721 lemma [code]:
   722   "list_emb P [] ys \<longleftrightarrow> True"
   723   "list_emb P (x#xs) [] \<longleftrightarrow> False"
   724   by (simp_all)
   725 
   726 lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"
   727   by (induct xs, simp, blast dest: list_emb_ConsD)
   728 
   729 lemma sublisteq_Cons2':
   730   assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"
   731   using assms by (cases) (rule sublisteq_Cons')
   732 
   733 lemma sublisteq_Cons2_neq:
   734   assumes "sublisteq (x#xs) (y#ys)"
   735   shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"
   736   using assms by (cases) auto
   737 
   738 lemma sublisteq_Cons2_iff [simp, code]:
   739   "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"
   740   by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)
   741 
   742 lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"
   743   by (induct zs) simp_all
   744 
   745 lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all
   746 
   747 lemma sublisteq_antisym:
   748   assumes "sublisteq xs ys" and "sublisteq ys xs"
   749   shows "xs = ys"
   750 using assms
   751 proof (induct)
   752   case list_emb_Nil
   753   from list_emb_Nil2 [OF this] show ?case by simp
   754 next
   755   case list_emb_Cons2
   756   thus ?case by simp
   757 next
   758   case list_emb_Cons
   759   hence False using sublisteq_Cons' by fastforce
   760   thus ?case ..
   761 qed
   762 
   763 lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"
   764   by (rule list_emb_trans [of _ _ _ "op ="]) auto
   765 
   766 lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"
   767   by (auto dest: list_emb_length)
   768 
   769 lemma list_emb_append_mono:
   770   "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"
   771   apply (induct rule: list_emb.induct)
   772     apply (metis eq_Nil_appendI list_emb_append2)
   773    apply (metis append_Cons list_emb_Cons)
   774   apply (metis append_Cons list_emb_Cons2)
   775   done
   776 
   777 
   778 subsection \<open>Appending elements\<close>
   779 
   780 lemma sublisteq_append [simp]:
   781   "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")
   782 proof
   783   { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
   784     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"
   785     proof (induct arbitrary: xs ys zs)
   786       case list_emb_Nil show ?case by simp
   787     next
   788       case (list_emb_Cons xs' ys' x)
   789       { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
   790       moreover
   791       { fix us assume "ys = x#us"
   792         then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }
   793       ultimately show ?case by (auto simp:Cons_eq_append_conv)
   794     next
   795       case (list_emb_Cons2 x y xs' ys')
   796       { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
   797       moreover
   798       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
   799       moreover
   800       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
   801       ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)
   802     qed }
   803   moreover assume ?l
   804   ultimately show ?r by blast
   805 next
   806   assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl)
   807 qed
   808 
   809 lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"
   810   by (induct zs) auto
   811 
   812 lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"
   813   by (metis append_Nil2 list_emb_Nil list_emb_append_mono)
   814 
   815 
   816 subsection \<open>Relation to standard list operations\<close>
   817 
   818 lemma sublisteq_map:
   819   assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"
   820   using assms by (induct) auto
   821 
   822 lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"
   823   by (induct xs) auto
   824 
   825 lemma sublisteq_filter [simp]:
   826   assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"
   827   using assms by induct auto
   828 
   829 lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")
   830 proof
   831   assume ?L
   832   then show ?R
   833   proof (induct)
   834     case list_emb_Nil show ?case by (metis sublist_empty)
   835   next
   836     case (list_emb_Cons xs ys x)
   837     then obtain N where "xs = sublist ys N" by blast
   838     then have "xs = sublist (x#ys) (Suc ` N)"
   839       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   840     then show ?case by blast
   841   next
   842     case (list_emb_Cons2 x y xs ys)
   843     then obtain N where "xs = sublist ys N" by blast
   844     then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
   845       by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
   846     moreover from list_emb_Cons2 have "x = y" by simp
   847     ultimately show ?case by blast
   848   qed
   849 next
   850   assume ?R
   851   then obtain N where "xs = sublist ys N" ..
   852   moreover have "sublisteq (sublist ys N) ys"
   853   proof (induct ys arbitrary: N)
   854     case Nil show ?case by simp
   855   next
   856     case Cons then show ?case by (auto simp: sublist_Cons)
   857   qed
   858   ultimately show ?L by simp
   859 qed
   860 
   861 end