src/HOL/Library/Sublist.thy
author eberlm <eberlm@in.tum.de>
Mon May 29 16:40:56 2017 +0200 (2017-05-29)
changeset 65957 558ba6b37f5c
parent 65956 639eb3617a86
child 67091 1393c2340eec
permissions -rw-r--r--
Tuned Library/Sublist.thy
     1 (*  Title:      HOL/Library/Sublist.thy
     2     Author:     Tobias Nipkow and Markus Wenzel, TU München
     3     Author:     Christian Sternagel, JAIST
     4     Author:     Manuel Eberl, TU München
     5 *)
     6 
     7 section \<open>List prefixes, suffixes, and homeomorphic embedding\<close>
     8 
     9 theory Sublist
    10 imports Main
    11 begin
    12 
    13 subsection \<open>Prefix order on lists\<close>
    14 
    15 definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    16   where "prefix xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
    17 
    18 definition strict_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
    19   where "strict_prefix xs ys \<longleftrightarrow> prefix xs ys \<and> xs \<noteq> ys"
    20 
    21 interpretation prefix_order: order prefix strict_prefix
    22   by standard (auto simp: prefix_def strict_prefix_def)
    23 
    24 interpretation prefix_bot: order_bot Nil prefix strict_prefix
    25   by standard (simp add: prefix_def)
    26 
    27 lemma prefixI [intro?]: "ys = xs @ zs \<Longrightarrow> prefix xs ys"
    28   unfolding prefix_def by blast
    29 
    30 lemma prefixE [elim?]:
    31   assumes "prefix xs ys"
    32   obtains zs where "ys = xs @ zs"
    33   using assms unfolding prefix_def by blast
    34 
    35 lemma strict_prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> strict_prefix xs ys"
    36   unfolding strict_prefix_def prefix_def by blast
    37 
    38 lemma strict_prefixE' [elim?]:
    39   assumes "strict_prefix xs ys"
    40   obtains z zs where "ys = xs @ z # zs"
    41 proof -
    42   from \<open>strict_prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys"
    43     unfolding strict_prefix_def prefix_def by blast
    44   with that show ?thesis by (auto simp add: neq_Nil_conv)
    45 qed
    46 
    47 (* FIXME rm *)
    48 lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys"
    49 by(fact prefix_order.le_neq_trans)
    50 
    51 lemma strict_prefixE [elim?]:
    52   fixes xs ys :: "'a list"
    53   assumes "strict_prefix xs ys"
    54   obtains "prefix xs ys" and "xs \<noteq> ys"
    55   using assms unfolding strict_prefix_def by blast
    56 
    57 
    58 subsection \<open>Basic properties of prefixes\<close>
    59 
    60 (* FIXME rm *)
    61 theorem Nil_prefix [simp]: "prefix [] xs"
    62   by (fact prefix_bot.bot_least)
    63 
    64 (* FIXME rm *)
    65 theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])"
    66   by (fact prefix_bot.bot_unique)
    67 
    68 lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys"
    69 proof
    70   assume "prefix xs (ys @ [y])"
    71   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
    72   show "xs = ys @ [y] \<or> prefix xs ys"
    73     by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
    74 next
    75   assume "xs = ys @ [y] \<or> prefix xs ys"
    76   then show "prefix xs (ys @ [y])"
    77     by (metis prefix_order.eq_iff prefix_order.order_trans prefixI)
    78 qed
    79 
    80 lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)"
    81   by (auto simp add: prefix_def)
    82 
    83 lemma prefix_code [code]:
    84   "prefix [] xs \<longleftrightarrow> True"
    85   "prefix (x # xs) [] \<longleftrightarrow> False"
    86   "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
    87   by simp_all
    88 
    89 lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs"
    90   by (induct xs) simp_all
    91 
    92 lemma same_prefix_nil [simp]: "prefix (xs @ ys) xs = (ys = [])"
    93   by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)
    94 
    95 lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)"
    96   unfolding prefix_def by fastforce
    97 
    98 lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs"
    99   by (auto simp add: prefix_def)
   100 
   101 theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))"
   102   by (cases xs) (auto simp add: prefix_def)
   103 
   104 theorem prefix_append:
   105   "prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix us zs))"
   106   apply (induct zs rule: rev_induct)
   107    apply force
   108   apply (simp del: append_assoc add: append_assoc [symmetric])
   109   apply (metis append_eq_appendI)
   110   done
   111 
   112 lemma append_one_prefix:
   113   "prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys"
   114   proof (unfold prefix_def)
   115     assume a1: "\<exists>zs. ys = xs @ zs"
   116     then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce
   117     assume a2: "length xs < length ys"
   118     have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp
   119     have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force
   120     hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)
   121     thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce
   122   qed
   123 
   124 theorem prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys"
   125   by (auto simp add: prefix_def)
   126 
   127 lemma prefix_same_cases:
   128   "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"
   129   unfolding prefix_def by (force simp: append_eq_append_conv2)
   130 
   131 lemma prefix_length_prefix:
   132   "prefix ps xs \<Longrightarrow> prefix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> prefix ps qs"
   133 by (auto simp: prefix_def) (metis append_Nil2 append_eq_append_conv_if)
   134 
   135 lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   136   by (auto simp add: prefix_def)
   137 
   138 lemma take_is_prefix: "prefix (take n xs) xs"
   139   unfolding prefix_def by (metis append_take_drop_id)
   140 
   141 lemma prefixeq_butlast: "prefix (butlast xs) xs"
   142 by (simp add: butlast_conv_take take_is_prefix)
   143 
   144 lemma map_prefixI: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"
   145   by (auto simp: prefix_def)
   146 
   147 lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys"
   148   by (auto simp: strict_prefix_def prefix_def)
   149 
   150 lemma prefix_snocD: "prefix (xs@[x]) ys \<Longrightarrow> strict_prefix xs ys"
   151   by (simp add: strict_prefixI' prefix_order.dual_order.strict_trans1)
   152 
   153 lemma strict_prefix_simps [simp, code]:
   154   "strict_prefix xs [] \<longleftrightarrow> False"
   155   "strict_prefix [] (x # xs) \<longleftrightarrow> True"
   156   "strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys"
   157   by (simp_all add: strict_prefix_def cong: conj_cong)
   158 
   159 lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_prefix (take n xs) ys"
   160 proof (induct n arbitrary: xs ys)
   161   case 0
   162   then show ?case by (cases ys) simp_all
   163 next
   164   case (Suc n)
   165   then show ?case by (metis prefix_order.less_trans strict_prefixI take_is_prefix)
   166 qed
   167 
   168 lemma not_prefix_cases:
   169   assumes pfx: "\<not> prefix ps ls"
   170   obtains
   171     (c1) "ps \<noteq> []" and "ls = []"
   172   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefix as xs"
   173   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   174 proof (cases ps)
   175   case Nil
   176   then show ?thesis using pfx by simp
   177 next
   178   case (Cons a as)
   179   note c = \<open>ps = a#as\<close>
   180   show ?thesis
   181   proof (cases ls)
   182     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
   183   next
   184     case (Cons x xs)
   185     show ?thesis
   186     proof (cases "x = a")
   187       case True
   188       have "\<not> prefix as xs" using pfx c Cons True by simp
   189       with c Cons True show ?thesis by (rule c2)
   190     next
   191       case False
   192       with c Cons show ?thesis by (rule c3)
   193     qed
   194   qed
   195 qed
   196 
   197 lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
   198   assumes np: "\<not> prefix ps ls"
   199     and base: "\<And>x xs. P (x#xs) []"
   200     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   201     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   202   shows "P ps ls" using np
   203 proof (induct ls arbitrary: ps)
   204   case Nil
   205   then show ?case
   206     by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
   207 next
   208   case (Cons y ys)
   209   then have npfx: "\<not> prefix ps (y # ys)" by simp
   210   then obtain x xs where pv: "ps = x # xs"
   211     by (rule not_prefix_cases) auto
   212   show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
   213 qed
   214 
   215 
   216 subsection \<open>Prefixes\<close>
   217 
   218 primrec prefixes where
   219 "prefixes [] = [[]]" |
   220 "prefixes (x#xs) = [] # map (op # x) (prefixes xs)"
   221 
   222 lemma in_set_prefixes[simp]: "xs \<in> set (prefixes ys) \<longleftrightarrow> prefix xs ys"
   223 proof (induct xs arbitrary: ys)
   224   case Nil
   225   then show ?case by (cases ys) auto
   226 next
   227   case (Cons a xs)
   228   then show ?case by (cases ys) auto
   229 qed
   230 
   231 lemma length_prefixes[simp]: "length (prefixes xs) = length xs+1"
   232   by (induction xs) auto
   233     
   234 lemma distinct_prefixes [intro]: "distinct (prefixes xs)"
   235   by (induction xs) (auto simp: distinct_map)
   236 
   237 lemma prefixes_snoc [simp]: "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]"
   238   by (induction xs) auto
   239 
   240 lemma prefixes_not_Nil [simp]: "prefixes xs \<noteq> []"
   241   by (cases xs) auto
   242 
   243 lemma hd_prefixes [simp]: "hd (prefixes xs) = []"
   244   by (cases xs) simp_all
   245 
   246 lemma last_prefixes [simp]: "last (prefixes xs) = xs"
   247   by (induction xs) (simp_all add: last_map)
   248     
   249 lemma prefixes_append: 
   250   "prefixes (xs @ ys) = prefixes xs @ map (\<lambda>ys'. xs @ ys') (tl (prefixes ys))"
   251 proof (induction xs)
   252   case Nil
   253   thus ?case by (cases ys) auto
   254 qed simp_all
   255 
   256 lemma prefixes_eq_snoc:
   257   "prefixes ys = xs @ [x] \<longleftrightarrow>
   258   (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = zs@[z] \<and> xs = prefixes zs)) \<and> x = ys"
   259   by (cases ys rule: rev_cases) auto
   260 
   261 lemma prefixes_tailrec [code]: 
   262   "prefixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) ([],[[]]) xs))"
   263 proof -
   264   have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) (ys, rev ys # zs) xs =
   265           (rev xs @ ys, rev (map (\<lambda>as. rev ys @ as) (prefixes xs)) @ zs)" for ys zs
   266   proof (induction xs arbitrary: ys zs)
   267     case (Cons x xs ys zs)
   268     from Cons.IH[of "x # ys" "rev ys # zs"]
   269       show ?case by (simp add: o_def)
   270   qed simp_all
   271   from this [of "[]" "[]"] show ?thesis by simp
   272 qed
   273   
   274 lemma set_prefixes_eq: "set (prefixes xs) = {ys. prefix ys xs}"
   275   by auto
   276 
   277 lemma card_set_prefixes [simp]: "card (set (prefixes xs)) = Suc (length xs)"
   278   by (subst distinct_card) auto
   279 
   280 lemma set_prefixes_append: 
   281   "set (prefixes (xs @ ys)) = set (prefixes xs) \<union> {xs @ ys' |ys'. ys' \<in> set (prefixes ys)}"
   282   by (subst prefixes_append, cases ys) auto
   283 
   284 
   285 subsection \<open>Longest Common Prefix\<close>
   286 
   287 definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where
   288 "Longest_common_prefix L = (ARG_MAX length ps. \<forall>xs \<in> L. prefix ps xs)"
   289 
   290 lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow>
   291   \<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)"
   292   (is "_ \<Longrightarrow> \<exists>ps. ?P L ps")
   293 proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L)
   294   case 0
   295   have "[] : L" using "0.hyps" LeastI[of "\<lambda>n. \<exists>xs\<in>L. n = length xs"] \<open>L \<noteq> {}\<close>
   296     by auto
   297   hence "?P L []" by(auto)
   298   thus ?case ..
   299 next
   300   case (Suc n)
   301   let ?EX = "\<lambda>n. \<exists>xs\<in>L. n = length xs"
   302   obtain x xs where xxs: "x#xs \<in> L" "size xs = n" using Suc.prems Suc.hyps(2)
   303     by(metis LeastI_ex[of ?EX] Suc_length_conv ex_in_conv)
   304   hence "[] \<notin> L" using Suc.hyps(2) by auto
   305   show ?case
   306   proof (cases "\<forall>xs \<in> L. \<exists>ys. xs = x#ys")
   307     case True
   308     let ?L = "{ys. x#ys \<in> L}"
   309     have 1: "(LEAST n. \<exists>xs \<in> ?L. n = length xs) = n"
   310       using xxs Suc.prems Suc.hyps(2) Least_le[of "?EX"]
   311       by - (rule Least_equality, fastforce+)
   312     have 2: "?L \<noteq> {}" using \<open>x # xs \<in> L\<close> by auto
   313     from Suc.hyps(1)[OF 1[symmetric] 2] obtain ps where IH: "?P ?L ps" ..
   314     { fix qs
   315       assume "\<forall>qs. (\<forall>xa. x # xa \<in> L \<longrightarrow> prefix qs xa) \<longrightarrow> length qs \<le> length ps"
   316       and "\<forall>xs\<in>L. prefix qs xs"
   317       hence "length (tl qs) \<le> length ps"
   318         by (metis Cons_prefix_Cons hd_Cons_tl list.sel(2) Nil_prefix) 
   319       hence "length qs \<le> Suc (length ps)" by auto
   320     }
   321     hence "?P L (x#ps)" using True IH by auto
   322     thus ?thesis ..
   323   next
   324     case False
   325     then obtain y ys where yys: "x\<noteq>y" "y#ys \<in> L" using \<open>[] \<notin> L\<close>
   326       by (auto) (metis list.exhaust)
   327     have "\<forall>qs. (\<forall>xs\<in>L. prefix qs xs) \<longrightarrow> qs = []" using yys \<open>x#xs \<in> L\<close>
   328       by auto (metis Cons_prefix_Cons prefix_Cons)
   329     hence "?P L []" by auto
   330     thus ?thesis ..
   331   qed
   332 qed
   333 
   334 lemma Longest_common_prefix_unique: "L \<noteq> {} \<Longrightarrow>
   335   \<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)"
   336 by(rule ex_ex1I[OF Longest_common_prefix_ex];
   337    meson equals0I prefix_length_prefix prefix_order.antisym)
   338 
   339 lemma Longest_common_prefix_eq:
   340  "\<lbrakk> L \<noteq> {};  \<forall>xs \<in> L. prefix ps xs;
   341     \<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk>
   342   \<Longrightarrow> Longest_common_prefix L = ps"
   343 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder
   344 by(rule some1_equality[OF Longest_common_prefix_unique]) auto
   345 
   346 lemma Longest_common_prefix_prefix:
   347   "xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs"
   348 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder
   349 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto
   350 
   351 lemma Longest_common_prefix_longest:
   352   "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)"
   353 unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder
   354 by(rule someI2_ex[OF Longest_common_prefix_ex]) auto
   355 
   356 lemma Longest_common_prefix_max_prefix:
   357   "L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)"
   358 by(metis Longest_common_prefix_prefix Longest_common_prefix_longest
   359      prefix_length_prefix ex_in_conv)
   360 
   361 lemma Longest_common_prefix_Nil: "[] \<in> L \<Longrightarrow> Longest_common_prefix L = []"
   362 using Longest_common_prefix_prefix prefix_Nil by blast
   363 
   364 lemma Longest_common_prefix_image_Cons: "L \<noteq> {} \<Longrightarrow>
   365   Longest_common_prefix (op # x ` L) = x # Longest_common_prefix L"
   366 apply(rule Longest_common_prefix_eq)
   367   apply(simp)
   368  apply (simp add: Longest_common_prefix_prefix)
   369 apply simp
   370 by(metis Longest_common_prefix_longest[of L] Cons_prefix_Cons Nitpick.size_list_simp(2)
   371      Suc_le_mono hd_Cons_tl order.strict_implies_order zero_less_Suc)
   372 
   373 lemma Longest_common_prefix_eq_Cons: assumes "L \<noteq> {}" "[] \<notin> L"  "\<forall>xs\<in>L. hd xs = x"
   374 shows "Longest_common_prefix L = x # Longest_common_prefix {ys. x#ys \<in> L}"
   375 proof -
   376   have "L = op # x ` {ys. x#ys \<in> L}" using assms(2,3)
   377     by (auto simp: image_def)(metis hd_Cons_tl)
   378   thus ?thesis
   379     by (metis Longest_common_prefix_image_Cons image_is_empty assms(1))
   380 qed
   381 
   382 lemma Longest_common_prefix_eq_Nil:
   383   "\<lbrakk>x#ys \<in> L; y#zs \<in> L; x \<noteq> y \<rbrakk> \<Longrightarrow> Longest_common_prefix L = []"
   384 by (metis Longest_common_prefix_prefix list.inject prefix_Cons)
   385 
   386 
   387 fun longest_common_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   388 "longest_common_prefix (x#xs) (y#ys) =
   389   (if x=y then x # longest_common_prefix xs ys else [])" |
   390 "longest_common_prefix _ _ = []"
   391 
   392 lemma longest_common_prefix_prefix1:
   393   "prefix (longest_common_prefix xs ys) xs"
   394 by(induction xs ys rule: longest_common_prefix.induct) auto
   395 
   396 lemma longest_common_prefix_prefix2:
   397   "prefix (longest_common_prefix xs ys) ys"
   398 by(induction xs ys rule: longest_common_prefix.induct) auto
   399 
   400 lemma longest_common_prefix_max_prefix:
   401   "\<lbrakk> prefix ps xs; prefix ps ys \<rbrakk>
   402    \<Longrightarrow> prefix ps (longest_common_prefix xs ys)"
   403 by(induction xs ys arbitrary: ps rule: longest_common_prefix.induct)
   404   (auto simp: prefix_Cons)
   405 
   406 
   407 subsection \<open>Parallel lists\<close>
   408 
   409 definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "\<parallel>" 50)
   410   where "(xs \<parallel> ys) = (\<not> prefix xs ys \<and> \<not> prefix ys xs)"
   411 
   412 lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix ys xs \<Longrightarrow> xs \<parallel> ys"
   413   unfolding parallel_def by blast
   414 
   415 lemma parallelE [elim]:
   416   assumes "xs \<parallel> ys"
   417   obtains "\<not> prefix xs ys \<and> \<not> prefix ys xs"
   418   using assms unfolding parallel_def by blast
   419 
   420 theorem prefix_cases:
   421   obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys"
   422   unfolding parallel_def strict_prefix_def by blast
   423 
   424 theorem parallel_decomp:
   425   "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   426 proof (induct xs rule: rev_induct)
   427   case Nil
   428   then have False by auto
   429   then show ?case ..
   430 next
   431   case (snoc x xs)
   432   show ?case
   433   proof (rule prefix_cases)
   434     assume le: "prefix xs ys"
   435     then obtain ys' where ys: "ys = xs @ ys'" ..
   436     show ?thesis
   437     proof (cases ys')
   438       assume "ys' = []"
   439       then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
   440     next
   441       fix c cs assume ys': "ys' = c # cs"
   442       have "x \<noteq> c" using snoc.prems ys ys' by fastforce
   443       thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs"
   444         using ys ys' by blast
   445     qed
   446   next
   447     assume "strict_prefix ys xs"
   448     then have "prefix ys (xs @ [x])" by (simp add: strict_prefix_def)
   449     with snoc have False by blast
   450     then show ?thesis ..
   451   next
   452     assume "xs \<parallel> ys"
   453     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   454       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   455       by blast
   456     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   457     with neq ys show ?thesis by blast
   458   qed
   459 qed
   460 
   461 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   462   apply (rule parallelI)
   463     apply (erule parallelE, erule conjE,
   464       induct rule: not_prefix_induct, simp+)+
   465   done
   466 
   467 lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
   468   by (simp add: parallel_append)
   469 
   470 lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
   471   unfolding parallel_def by auto
   472 
   473 
   474 subsection \<open>Suffix order on lists\<close>
   475 
   476 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   477   where "suffix xs ys = (\<exists>zs. ys = zs @ xs)"
   478 
   479 definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   480   where "strict_suffix xs ys \<longleftrightarrow> suffix xs ys \<and> xs \<noteq> ys"
   481 
   482 interpretation suffix_order: order suffix strict_suffix
   483   by standard (auto simp: suffix_def strict_suffix_def)
   484 
   485 interpretation suffix_bot: order_bot Nil suffix strict_suffix
   486   by standard (simp add: suffix_def)
   487 
   488 lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys"
   489   unfolding suffix_def by blast
   490 
   491 lemma suffixE [elim?]:
   492   assumes "suffix xs ys"
   493   obtains zs where "ys = zs @ xs"
   494   using assms unfolding suffix_def by blast
   495     
   496 lemma suffix_tl [simp]: "suffix (tl xs) xs"
   497   by (induct xs) (auto simp: suffix_def)
   498 
   499 lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs"
   500   by (induct xs) (auto simp: strict_suffix_def suffix_def)
   501 
   502 lemma Nil_suffix [simp]: "suffix [] xs"
   503   by (simp add: suffix_def)
   504 
   505 lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])"
   506   by (auto simp add: suffix_def)
   507 
   508 lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)"
   509   by (auto simp add: suffix_def)
   510 
   511 lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys"
   512   by (auto simp add: suffix_def)
   513 
   514 lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)"
   515   by (auto simp add: suffix_def)
   516 
   517 lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys"
   518   by (auto simp add: suffix_def)
   519 
   520 lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   521   by (auto simp: strict_suffix_def suffix_def)
   522 
   523 lemma suffix_set_subset: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
   524   by (auto simp: suffix_def)
   525 
   526 lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys"
   527 proof -
   528   assume "suffix (x # xs) (y # ys)"
   529   then obtain zs where "y # ys = zs @ x # xs" ..
   530   then show ?thesis
   531     by (induct zs) (auto intro!: suffix_appendI suffix_ConsI)
   532 qed
   533 
   534 lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"
   535 proof
   536   assume "suffix xs ys"
   537   then obtain zs where "ys = zs @ xs" ..
   538   then have "rev ys = rev xs @ rev zs" by simp
   539   then show "prefix (rev xs) (rev ys)" ..
   540 next
   541   assume "prefix (rev xs) (rev ys)"
   542   then obtain zs where "rev ys = rev xs @ zs" ..
   543   then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
   544   then have "ys = rev zs @ xs" by simp
   545   then show "suffix xs ys" ..
   546 qed
   547   
   548 lemma strict_suffix_to_prefix [code]: "strict_suffix xs ys \<longleftrightarrow> strict_prefix (rev xs) (rev ys)"
   549   by (auto simp: suffix_to_prefix strict_suffix_def strict_prefix_def)
   550 
   551 lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs"
   552   by (clarsimp elim!: suffixE)
   553 
   554 lemma suffix_map: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"
   555   by (auto elim!: suffixE intro: suffixI)
   556 
   557 lemma suffix_drop: "suffix (drop n as) as"
   558   unfolding suffix_def by (rule exI [where x = "take n as"]) simp
   559 
   560 lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   561   by (auto elim!: suffixE)
   562 
   563 lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix"
   564   by (intro ext) (auto simp: suffix_def strict_suffix_def)
   565 
   566 lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
   567   unfolding suffix_def by auto
   568 
   569 lemma suffix_snoc [simp]: "suffix xs (ys @ [y]) \<longleftrightarrow> xs = [] \<or> (\<exists>zs. xs = zs @ [y] \<and> suffix zs ys)"
   570   by (cases xs rule: rev_cases) (auto simp: suffix_def)
   571 
   572 lemma snoc_suffix_snoc [simp]: "suffix (xs @ [x]) (ys @ [y]) = (x = y \<and> suffix xs ys)"
   573   by (auto simp add: suffix_def)
   574 
   575 lemma same_suffix_suffix [simp]: "suffix (ys @ xs) (zs @ xs) = suffix ys zs"
   576   by (simp add: suffix_to_prefix)
   577 
   578 lemma same_suffix_nil [simp]: "suffix (ys @ xs) xs = (ys = [])"
   579   by (simp add: suffix_to_prefix)
   580 
   581 theorem suffix_Cons: "suffix xs (y # ys) \<longleftrightarrow> xs = y # ys \<or> suffix xs ys"
   582   unfolding suffix_def by (auto simp: Cons_eq_append_conv)
   583 
   584 theorem suffix_append: 
   585   "suffix xs (ys @ zs) \<longleftrightarrow> suffix xs zs \<or> (\<exists>xs'. xs = xs' @ zs \<and> suffix xs' ys)"
   586   by (auto simp: suffix_def append_eq_append_conv2)
   587 
   588 theorem suffix_length_le: "suffix xs ys \<Longrightarrow> length xs \<le> length ys"
   589   by (auto simp add: suffix_def)
   590 
   591 lemma suffix_same_cases:
   592   "suffix (xs\<^sub>1::'a list) ys \<Longrightarrow> suffix xs\<^sub>2 ys \<Longrightarrow> suffix xs\<^sub>1 xs\<^sub>2 \<or> suffix xs\<^sub>2 xs\<^sub>1"
   593   unfolding suffix_def by (force simp: append_eq_append_conv2)
   594 
   595 lemma suffix_length_suffix:
   596   "suffix ps xs \<Longrightarrow> suffix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> suffix ps qs"
   597   by (auto simp: suffix_to_prefix intro: prefix_length_prefix)
   598 
   599 lemma suffix_length_less: "strict_suffix xs ys \<Longrightarrow> length xs < length ys"
   600   by (auto simp: strict_suffix_def suffix_def)
   601 
   602 lemma suffix_ConsD': "suffix (x#xs) ys \<Longrightarrow> strict_suffix xs ys"
   603   by (auto simp: strict_suffix_def suffix_def)
   604 
   605 lemma drop_strict_suffix: "strict_suffix xs ys \<Longrightarrow> strict_suffix (drop n xs) ys"
   606 proof (induct n arbitrary: xs ys)
   607   case 0
   608   then show ?case by (cases ys) simp_all
   609 next
   610   case (Suc n)
   611   then show ?case 
   612     by (cases xs) (auto intro: Suc dest: suffix_ConsD' suffix_order.less_imp_le)
   613 qed
   614 
   615 lemma not_suffix_cases:
   616   assumes pfx: "\<not> suffix ps ls"
   617   obtains
   618     (c1) "ps \<noteq> []" and "ls = []"
   619   | (c2) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x = a" and "\<not> suffix as xs"
   620   | (c3) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x \<noteq> a"
   621 proof (cases ps rule: rev_cases)
   622   case Nil
   623   then show ?thesis using pfx by simp
   624 next
   625   case (snoc as a)
   626   note c = \<open>ps = as@[a]\<close>
   627   show ?thesis
   628   proof (cases ls rule: rev_cases)
   629     case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_suffix_nil)
   630   next
   631     case (snoc xs x)
   632     show ?thesis
   633     proof (cases "x = a")
   634       case True
   635       have "\<not> suffix as xs" using pfx c snoc True by simp
   636       with c snoc True show ?thesis by (rule c2)
   637     next
   638       case False
   639       with c snoc show ?thesis by (rule c3)
   640     qed
   641   qed
   642 qed
   643 
   644 lemma not_suffix_induct [consumes 1, case_names Nil Neq Eq]:
   645   assumes np: "\<not> suffix ps ls"
   646     and base: "\<And>x xs. P (xs@[x]) []"
   647     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (xs@[x]) (ys@[y])"
   648     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> suffix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (xs@[x]) (ys@[y])"
   649   shows "P ps ls" using np
   650 proof (induct ls arbitrary: ps rule: rev_induct)
   651   case Nil
   652   then show ?case by (cases ps rule: rev_cases) (auto intro: base)
   653 next
   654   case (snoc y ys ps)
   655   then have npfx: "\<not> suffix ps (ys @ [y])" by simp
   656   then obtain x xs where pv: "ps = xs @ [x]"
   657     by (rule not_suffix_cases) auto
   658   show ?case by (metis snoc.hyps snoc_suffix_snoc npfx pv r1 r2)
   659 qed
   660 
   661 
   662 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y"
   663   by blast
   664 
   665 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x"
   666   by blast
   667 
   668 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   669   unfolding parallel_def by simp
   670 
   671 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   672   unfolding parallel_def by simp
   673 
   674 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   675   by auto
   676 
   677 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   678   by (metis Cons_prefix_Cons parallelE parallelI)
   679 
   680 lemma not_equal_is_parallel:
   681   assumes neq: "xs \<noteq> ys"
   682     and len: "length xs = length ys"
   683   shows "xs \<parallel> ys"
   684   using len neq
   685 proof (induct rule: list_induct2)
   686   case Nil
   687   then show ?case by simp
   688 next
   689   case (Cons a as b bs)
   690   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   691   show ?case
   692   proof (cases "a = b")
   693     case True
   694     then have "as \<noteq> bs" using Cons by simp
   695     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   696   next
   697     case False
   698     then show ?thesis by (rule Cons_parallelI1)
   699   qed
   700 qed
   701 
   702 subsection \<open>Suffixes\<close>
   703 
   704 primrec suffixes where
   705   "suffixes [] = [[]]"
   706 | "suffixes (x#xs) = suffixes xs @ [x # xs]"
   707 
   708 lemma in_set_suffixes [simp]: "xs \<in> set (suffixes ys) \<longleftrightarrow> suffix xs ys"
   709   by (induction ys) (auto simp: suffix_def Cons_eq_append_conv)
   710 
   711 lemma distinct_suffixes [intro]: "distinct (suffixes xs)"
   712   by (induction xs) (auto simp: suffix_def)
   713 
   714 lemma length_suffixes [simp]: "length (suffixes xs) = Suc (length xs)"
   715   by (induction xs) auto
   716 
   717 lemma suffixes_snoc [simp]: "suffixes (xs @ [x]) = [] # map (\<lambda>ys. ys @ [x]) (suffixes xs)"
   718   by (induction xs) auto
   719 
   720 lemma suffixes_not_Nil [simp]: "suffixes xs \<noteq> []"
   721   by (cases xs) auto
   722 
   723 lemma hd_suffixes [simp]: "hd (suffixes xs) = []"
   724   by (induction xs) simp_all
   725 
   726 lemma last_suffixes [simp]: "last (suffixes xs) = xs"
   727   by (cases xs) simp_all
   728 
   729 lemma suffixes_append: 
   730   "suffixes (xs @ ys) = suffixes ys @ map (\<lambda>xs'. xs' @ ys) (tl (suffixes xs))"
   731 proof (induction ys rule: rev_induct)
   732   case Nil
   733   thus ?case by (cases xs rule: rev_cases) auto
   734 next
   735   case (snoc y ys)
   736   show ?case
   737     by (simp only: append.assoc [symmetric] suffixes_snoc snoc.IH) simp
   738 qed
   739 
   740 lemma suffixes_eq_snoc:
   741   "suffixes ys = xs @ [x] \<longleftrightarrow>
   742      (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = z#zs \<and> xs = suffixes zs)) \<and> x = ys"
   743   by (cases ys) auto
   744 
   745 lemma suffixes_tailrec [code]: 
   746   "suffixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) ([],[[]]) (rev xs)))"
   747 proof -
   748   have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) (ys, ys # zs) (rev xs) =
   749           (xs @ ys, rev (map (\<lambda>as. as @ ys) (suffixes xs)) @ zs)" for ys zs
   750   proof (induction xs arbitrary: ys zs)
   751     case (Cons x xs ys zs)
   752     from Cons.IH[of ys zs]
   753       show ?case by (simp add: o_def case_prod_unfold)
   754   qed simp_all
   755   from this [of "[]" "[]"] show ?thesis by simp
   756 qed
   757   
   758 lemma set_suffixes_eq: "set (suffixes xs) = {ys. suffix ys xs}"
   759   by auto
   760     
   761 lemma card_set_suffixes [simp]: "card (set (suffixes xs)) = Suc (length xs)"
   762   by (subst distinct_card) auto
   763   
   764 lemma set_suffixes_append: 
   765   "set (suffixes (xs @ ys)) = set (suffixes ys) \<union> {xs' @ ys |xs'. xs' \<in> set (suffixes xs)}"
   766   by (subst suffixes_append, cases xs rule: rev_cases) auto
   767 
   768 
   769 lemma suffixes_conv_prefixes: "suffixes xs = map rev (prefixes (rev xs))"
   770   by (induction xs) auto
   771 
   772 lemma prefixes_conv_suffixes: "prefixes xs = map rev (suffixes (rev xs))"
   773   by (induction xs) auto
   774     
   775 lemma prefixes_rev: "prefixes (rev xs) = map rev (suffixes xs)"
   776   by (induction xs) auto
   777     
   778 lemma suffixes_rev: "suffixes (rev xs) = map rev (prefixes xs)"
   779   by (induction xs) auto
   780 
   781 
   782 subsection \<open>Homeomorphic embedding on lists\<close>
   783 
   784 inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   785   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
   786 where
   787   list_emb_Nil [intro, simp]: "list_emb P [] ys"
   788 | list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"
   789 | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
   790 
   791 lemma list_emb_mono:                         
   792   assumes "\<And>x y. P x y \<longrightarrow> Q x y"
   793   shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"
   794 proof                                        
   795   assume "list_emb P xs ys"                    
   796   then show "list_emb Q xs ys" by (induct) (auto simp: assms)
   797 qed 
   798 
   799 lemma list_emb_Nil2 [simp]:
   800   assumes "list_emb P xs []" shows "xs = []"
   801   using assms by (cases rule: list_emb.cases) auto
   802 
   803 lemma list_emb_refl:
   804   assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"
   805   shows "list_emb P xs xs"
   806   using assms by (induct xs) auto
   807 
   808 lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"
   809 proof -
   810   { assume "list_emb P (x#xs) []"
   811     from list_emb_Nil2 [OF this] have False by simp
   812   } moreover {
   813     assume False
   814     then have "list_emb P (x#xs) []" by simp
   815   } ultimately show ?thesis by blast
   816 qed
   817 
   818 lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"
   819   by (induct zs) auto
   820 
   821 lemma list_emb_prefix [intro]:
   822   assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"
   823   using assms
   824   by (induct arbitrary: zs) auto
   825 
   826 lemma list_emb_ConsD:
   827   assumes "list_emb P (x#xs) ys"
   828   shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
   829 using assms
   830 proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
   831   case list_emb_Cons
   832   then show ?case by (metis append_Cons)
   833 next
   834   case (list_emb_Cons2 x y xs ys)
   835   then show ?case by blast
   836 qed
   837 
   838 lemma list_emb_appendD:
   839   assumes "list_emb P (xs @ ys) zs"
   840   shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"
   841 using assms
   842 proof (induction xs arbitrary: ys zs)
   843   case Nil then show ?case by auto
   844 next
   845   case (Cons x xs)
   846   then obtain us v vs where
   847     zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"
   848     by (auto dest: list_emb_ConsD)
   849   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
   850     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)"
   851     using Cons(1) by (metis (no_types))
   852   hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
   853   thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
   854 qed
   855 
   856 lemma list_emb_strict_suffix:
   857   assumes "list_emb P xs ys" and "strict_suffix ys zs"
   858   shows "list_emb P xs zs"
   859   using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def suffix_def)
   860 
   861 lemma list_emb_suffix:
   862   assumes "list_emb P xs ys" and "suffix ys zs"
   863   shows "list_emb P xs zs"
   864 using assms and list_emb_strict_suffix
   865 unfolding strict_suffix_reflclp_conv[symmetric] by auto
   866 
   867 lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"
   868   by (induct rule: list_emb.induct) auto
   869 
   870 lemma list_emb_trans:
   871   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"
   872   shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"
   873 proof -
   874   assume "list_emb P xs ys" and "list_emb P ys zs"
   875   then show "list_emb P xs zs" using assms
   876   proof (induction arbitrary: zs)
   877     case list_emb_Nil show ?case by blast
   878   next
   879     case (list_emb_Cons xs ys y)
   880     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   881       where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast
   882     then have "list_emb P ys (v#vs)" by blast
   883     then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)
   884     from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto
   885   next
   886     case (list_emb_Cons2 x y xs ys)
   887     from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
   888       where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast
   889     with list_emb_Cons2 have "list_emb P xs vs" by auto
   890     moreover have "P x v"
   891     proof -
   892       from zs have "v \<in> set zs" by auto
   893       moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all
   894       ultimately show ?thesis
   895         using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2
   896         by blast
   897     qed
   898     ultimately have "list_emb P (x#xs) (v#vs)" by blast
   899     then show ?case unfolding zs by (rule list_emb_append2)
   900   qed
   901 qed
   902 
   903 lemma list_emb_set:
   904   assumes "list_emb P xs ys" and "x \<in> set xs"
   905   obtains y where "y \<in> set ys" and "P x y"
   906   using assms by (induct) auto
   907 
   908 lemma list_emb_Cons_iff1 [simp]:
   909   assumes "P x y"
   910   shows   "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P xs ys"
   911   using assms by (subst list_emb.simps) (auto dest: list_emb_ConsD)
   912 
   913 lemma list_emb_Cons_iff2 [simp]:
   914   assumes "\<not>P x y"
   915   shows   "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P (x#xs) ys"
   916   using assms by (subst list_emb.simps) auto
   917 
   918 lemma list_emb_code [code]:
   919   "list_emb P [] ys \<longleftrightarrow> True"
   920   "list_emb P (x#xs) [] \<longleftrightarrow> False"
   921   "list_emb P (x#xs) (y#ys) \<longleftrightarrow> (if P x y then list_emb P xs ys else list_emb P (x#xs) ys)"
   922   by simp_all
   923     
   924 
   925 subsection \<open>Subsequences (special case of homeomorphic embedding)\<close>
   926 
   927 abbreviation subseq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   928   where "subseq xs ys \<equiv> list_emb (op =) xs ys"
   929   
   930 definition strict_subseq where "strict_subseq xs ys \<longleftrightarrow> xs \<noteq> ys \<and> subseq xs ys"
   931 
   932 lemma subseq_Cons2: "subseq xs ys \<Longrightarrow> subseq (x#xs) (x#ys)" by auto
   933 
   934 lemma subseq_same_length:
   935   assumes "subseq xs ys" and "length xs = length ys" shows "xs = ys"
   936   using assms by (induct) (auto dest: list_emb_length)
   937 
   938 lemma not_subseq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> subseq xs ys"
   939   by (metis list_emb_length linorder_not_less)
   940 
   941 lemma subseq_Cons': "subseq (x#xs) ys \<Longrightarrow> subseq xs ys"
   942   by (induct xs, simp, blast dest: list_emb_ConsD)
   943 
   944 lemma subseq_Cons2':
   945   assumes "subseq (x#xs) (x#ys)" shows "subseq xs ys"
   946   using assms by (cases) (rule subseq_Cons')
   947 
   948 lemma subseq_Cons2_neq:
   949   assumes "subseq (x#xs) (y#ys)"
   950   shows "x \<noteq> y \<Longrightarrow> subseq (x#xs) ys"
   951   using assms by (cases) auto
   952 
   953 lemma subseq_Cons2_iff [simp]:
   954   "subseq (x#xs) (y#ys) = (if x = y then subseq xs ys else subseq (x#xs) ys)"
   955   by simp
   956 
   957 lemma subseq_append': "subseq (zs @ xs) (zs @ ys) \<longleftrightarrow> subseq xs ys"
   958   by (induct zs) simp_all
   959     
   960 interpretation subseq_order: order subseq strict_subseq
   961 proof
   962   fix xs ys :: "'a list"
   963   {
   964     assume "subseq xs ys" and "subseq ys xs"
   965     thus "xs = ys"
   966     proof (induct)
   967       case list_emb_Nil
   968       from list_emb_Nil2 [OF this] show ?case by simp
   969     next
   970       case list_emb_Cons2
   971       thus ?case by simp
   972     next
   973       case list_emb_Cons
   974       hence False using subseq_Cons' by fastforce
   975       thus ?case ..
   976     qed
   977   }
   978   thus "strict_subseq xs ys \<longleftrightarrow> (subseq xs ys \<and> \<not>subseq ys xs)"
   979     by (auto simp: strict_subseq_def)
   980 qed (auto simp: list_emb_refl intro: list_emb_trans)
   981 
   982 lemma in_set_subseqs [simp]: "xs \<in> set (subseqs ys) \<longleftrightarrow> subseq xs ys"
   983 proof
   984   assume "xs \<in> set (subseqs ys)"
   985   thus "subseq xs ys"
   986     by (induction ys arbitrary: xs) (auto simp: Let_def)
   987 next
   988   have [simp]: "[] \<in> set (subseqs ys)" for ys :: "'a list" 
   989     by (induction ys) (auto simp: Let_def)
   990   assume "subseq xs ys"
   991   thus "xs \<in> set (subseqs ys)"
   992     by (induction xs ys rule: list_emb.induct) (auto simp: Let_def)
   993 qed
   994 
   995 lemma set_subseqs_eq: "set (subseqs ys) = {xs. subseq xs ys}"
   996   by auto
   997 
   998 lemma subseq_append_le_same_iff: "subseq (xs @ ys) ys \<longleftrightarrow> xs = []"
   999   by (auto dest: list_emb_length)
  1000 
  1001 lemma subseq_singleton_left: "subseq [x] ys \<longleftrightarrow> x \<in> set ys"
  1002   by (fastforce dest: list_emb_ConsD split_list_last)
  1003 
  1004 lemma list_emb_append_mono:
  1005   "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"
  1006   by (induct rule: list_emb.induct) auto
  1007 
  1008 lemma prefix_imp_subseq [intro]: "prefix xs ys \<Longrightarrow> subseq xs ys"
  1009   by (auto simp: prefix_def)
  1010 
  1011 lemma suffix_imp_subseq [intro]: "suffix xs ys \<Longrightarrow> subseq xs ys"
  1012   by (auto simp: suffix_def)
  1013 
  1014 
  1015 subsection \<open>Appending elements\<close>
  1016 
  1017 lemma subseq_append [simp]:
  1018   "subseq (xs @ zs) (ys @ zs) \<longleftrightarrow> subseq xs ys" (is "?l = ?r")
  1019 proof
  1020   { fix xs' ys' xs ys zs :: "'a list" assume "subseq xs' ys'"
  1021     then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> subseq xs ys"
  1022     proof (induct arbitrary: xs ys zs)
  1023       case list_emb_Nil show ?case by simp
  1024     next
  1025       case (list_emb_Cons xs' ys' x)
  1026       { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
  1027       moreover
  1028       { fix us assume "ys = x#us"
  1029         then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }
  1030       ultimately show ?case by (auto simp:Cons_eq_append_conv)
  1031     next
  1032       case (list_emb_Cons2 x y xs' ys')
  1033       { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
  1034       moreover
  1035       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
  1036       moreover
  1037       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
  1038       ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)
  1039     qed }
  1040   moreover assume ?l
  1041   ultimately show ?r by blast
  1042 next
  1043   assume ?r then show ?l by (metis list_emb_append_mono subseq_order.order_refl)
  1044 qed
  1045 
  1046 lemma subseq_append_iff: 
  1047   "subseq xs (ys @ zs) \<longleftrightarrow> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> subseq xs1 ys \<and> subseq xs2 zs)"
  1048   (is "?lhs = ?rhs")
  1049 proof
  1050   assume ?lhs thus ?rhs
  1051   proof (induction xs "ys @ zs" arbitrary: ys zs rule: list_emb.induct)
  1052     case (list_emb_Cons xs ws y ys zs)
  1053     from list_emb_Cons(2)[of "tl ys" zs] and list_emb_Cons(2)[of "[]" "tl zs"] and list_emb_Cons(1,3)
  1054       show ?case by (cases ys) auto
  1055   next
  1056     case (list_emb_Cons2 x y xs ws ys zs)
  1057     from list_emb_Cons2(3)[of "tl ys" zs] and list_emb_Cons2(3)[of "[]" "tl zs"]
  1058        and list_emb_Cons2(1,2,4)
  1059     show ?case by (cases ys) (auto simp: Cons_eq_append_conv)
  1060   qed auto
  1061 qed (auto intro: list_emb_append_mono)
  1062 
  1063 lemma subseq_appendE [case_names append]: 
  1064   assumes "subseq xs (ys @ zs)"
  1065   obtains xs1 xs2 where "xs = xs1 @ xs2" "subseq xs1 ys" "subseq xs2 zs"
  1066   using assms by (subst (asm) subseq_append_iff) auto
  1067 
  1068 lemma subseq_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (zs @ ys)"
  1069   by (induct zs) auto
  1070 
  1071 lemma subseq_rev_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (ys @ zs)"
  1072   by (metis append_Nil2 list_emb_Nil list_emb_append_mono)
  1073 
  1074 
  1075 subsection \<open>Relation to standard list operations\<close>
  1076 
  1077 lemma subseq_map:
  1078   assumes "subseq xs ys" shows "subseq (map f xs) (map f ys)"
  1079   using assms by (induct) auto
  1080 
  1081 lemma subseq_filter_left [simp]: "subseq (filter P xs) xs"
  1082   by (induct xs) auto
  1083 
  1084 lemma subseq_filter [simp]:
  1085   assumes "subseq xs ys" shows "subseq (filter P xs) (filter P ys)"
  1086   using assms by induct auto
  1087 
  1088 lemma subseq_conv_nths: 
  1089   "subseq xs ys \<longleftrightarrow> (\<exists>N. xs = nths ys N)" (is "?L = ?R")
  1090 proof
  1091   assume ?L
  1092   then show ?R
  1093   proof (induct)
  1094     case list_emb_Nil show ?case by (metis nths_empty)
  1095   next
  1096     case (list_emb_Cons xs ys x)
  1097     then obtain N where "xs = nths ys N" by blast
  1098     then have "xs = nths (x#ys) (Suc ` N)"
  1099       by (clarsimp simp add: nths_Cons inj_image_mem_iff)
  1100     then show ?case by blast
  1101   next
  1102     case (list_emb_Cons2 x y xs ys)
  1103     then obtain N where "xs = nths ys N" by blast
  1104     then have "x#xs = nths (x#ys) (insert 0 (Suc ` N))"
  1105       by (clarsimp simp add: nths_Cons inj_image_mem_iff)
  1106     moreover from list_emb_Cons2 have "x = y" by simp
  1107     ultimately show ?case by blast
  1108   qed
  1109 next
  1110   assume ?R
  1111   then obtain N where "xs = nths ys N" ..
  1112   moreover have "subseq (nths ys N) ys"
  1113   proof (induct ys arbitrary: N)
  1114     case Nil show ?case by simp
  1115   next
  1116     case Cons then show ?case by (auto simp: nths_Cons)
  1117   qed
  1118   ultimately show ?L by simp
  1119 qed
  1120   
  1121   
  1122 subsection \<open>Contiguous sublists\<close>
  1123 
  1124 definition sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where 
  1125   "sublist xs ys = (\<exists>ps ss. ys = ps @ xs @ ss)"
  1126   
  1127 definition strict_sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where 
  1128   "strict_sublist xs ys \<longleftrightarrow> sublist xs ys \<and> xs \<noteq> ys"
  1129 
  1130 interpretation sublist_order: order sublist strict_sublist
  1131 proof
  1132   fix xs ys zs :: "'a list"
  1133   assume "sublist xs ys" "sublist ys zs"
  1134   then obtain xs1 xs2 ys1 ys2 where "ys = xs1 @ xs @ xs2" "zs = ys1 @ ys @ ys2"
  1135     by (auto simp: sublist_def)
  1136   hence "zs = (ys1 @ xs1) @ xs @ (xs2 @ ys2)" by simp
  1137   thus "sublist xs zs" unfolding sublist_def by blast
  1138 next
  1139   fix xs ys :: "'a list"
  1140   {
  1141     assume "sublist xs ys" "sublist ys xs"
  1142     then obtain as bs cs ds 
  1143       where xs: "xs = as @ ys @ bs" and ys: "ys = cs @ xs @ ds" 
  1144       by (auto simp: sublist_def)
  1145     have "xs = as @ cs @ xs @ ds @ bs" by (subst xs, subst ys) auto
  1146     also have "length \<dots> = length as + length cs + length xs + length bs + length ds" 
  1147       by simp
  1148     finally have "as = []" "bs = []" by simp_all
  1149     with xs show "xs = ys" by simp
  1150   }
  1151   thus "strict_sublist xs ys \<longleftrightarrow> (sublist xs ys \<and> \<not>sublist ys xs)"
  1152     by (auto simp: strict_sublist_def)
  1153 qed (auto simp: strict_sublist_def sublist_def intro: exI[of _ "[]"])
  1154   
  1155 lemma sublist_Nil_left [simp, intro]: "sublist [] ys"
  1156   by (auto simp: sublist_def)
  1157     
  1158 lemma sublist_Cons_Nil [simp]: "\<not>sublist (x#xs) []"
  1159   by (auto simp: sublist_def)
  1160     
  1161 lemma sublist_Nil_right [simp]: "sublist xs [] \<longleftrightarrow> xs = []"
  1162   by (cases xs) auto
  1163     
  1164 lemma sublist_appendI [simp, intro]: "sublist xs (ps @ xs @ ss)"
  1165   by (auto simp: sublist_def)
  1166     
  1167 lemma sublist_append_leftI [simp, intro]: "sublist xs (ps @ xs)"
  1168   by (auto simp: sublist_def intro: exI[of _ "[]"])
  1169     
  1170 lemma sublist_append_rightI [simp, intro]: "sublist xs (xs @ ss)"
  1171   by (auto simp: sublist_def intro: exI[of _ "[]"]) 
  1172 
  1173 lemma sublist_altdef: "sublist xs ys \<longleftrightarrow> (\<exists>ys'. prefix ys' ys \<and> suffix xs ys')"
  1174 proof safe
  1175   assume "sublist xs ys"
  1176   then obtain ps ss where "ys = ps @ xs @ ss" by (auto simp: sublist_def)
  1177   thus "\<exists>ys'. prefix ys' ys \<and> suffix xs ys'"
  1178     by (intro exI[of _ "ps @ xs"] conjI suffix_appendI) auto
  1179 next
  1180   fix ys'
  1181   assume "prefix ys' ys" "suffix xs ys'"
  1182   thus "sublist xs ys" by (auto simp: prefix_def suffix_def)
  1183 qed
  1184   
  1185 lemma sublist_altdef': "sublist xs ys \<longleftrightarrow> (\<exists>ys'. suffix ys' ys \<and> prefix xs ys')"
  1186 proof safe
  1187   assume "sublist xs ys"
  1188   then obtain ps ss where "ys = ps @ xs @ ss" by (auto simp: sublist_def)
  1189   thus "\<exists>ys'. suffix ys' ys \<and> prefix xs ys'"
  1190     by (intro exI[of _ "xs @ ss"] conjI suffixI) auto
  1191 next
  1192   fix ys'
  1193   assume "suffix ys' ys" "prefix xs ys'"
  1194   thus "sublist xs ys" by (auto simp: prefix_def suffix_def)
  1195 qed
  1196 
  1197 lemma sublist_Cons_right: "sublist xs (y # ys) \<longleftrightarrow> prefix xs (y # ys) \<or> sublist xs ys"
  1198   by (auto simp: sublist_def prefix_def Cons_eq_append_conv)
  1199     
  1200 lemma sublist_code [code]:
  1201   "sublist [] ys \<longleftrightarrow> True"
  1202   "sublist (x # xs) [] \<longleftrightarrow> False"
  1203   "sublist (x # xs) (y # ys) \<longleftrightarrow> prefix (x # xs) (y # ys) \<or> sublist (x # xs) ys"
  1204   by (simp_all add: sublist_Cons_right)
  1205 
  1206 
  1207 lemma sublist_append:
  1208   "sublist xs (ys @ zs) \<longleftrightarrow> 
  1209      sublist xs ys \<or> sublist xs zs \<or> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> suffix xs1 ys \<and> prefix xs2 zs)"
  1210   by (auto simp: sublist_altdef prefix_append suffix_append)
  1211 
  1212 primrec sublists :: "'a list \<Rightarrow> 'a list list" where
  1213   "sublists [] = [[]]"
  1214 | "sublists (x # xs) = sublists xs @ map (op # x) (prefixes xs)"
  1215 
  1216 lemma in_set_sublists [simp]: "xs \<in> set (sublists ys) \<longleftrightarrow> sublist xs ys" 
  1217   by (induction ys arbitrary: xs) (auto simp: sublist_Cons_right prefix_Cons)
  1218 
  1219 lemma set_sublists_eq: "set (sublists xs) = {ys. sublist ys xs}"
  1220   by auto
  1221 
  1222 lemma length_sublists [simp]: "length (sublists xs) = Suc (length xs * Suc (length xs) div 2)"
  1223   by (induction xs) simp_all
  1224 
  1225 lemma sublist_length_le: "sublist xs ys \<Longrightarrow> length xs \<le> length ys"
  1226   by (auto simp add: sublist_def)
  1227 
  1228 lemma set_mono_sublist: "sublist xs ys \<Longrightarrow> set xs \<subseteq> set ys"
  1229   by (auto simp add: sublist_def)
  1230     
  1231 lemma prefix_imp_sublist [simp, intro]: "prefix xs ys \<Longrightarrow> sublist xs ys"
  1232   by (auto simp: sublist_def prefix_def intro: exI[of _ "[]"])
  1233     
  1234 lemma suffix_imp_sublist [simp, intro]: "suffix xs ys \<Longrightarrow> sublist xs ys"
  1235   by (auto simp: sublist_def suffix_def intro: exI[of _ "[]"])
  1236 
  1237 lemma sublist_take [simp, intro]: "sublist (take n xs) xs"
  1238   by (rule prefix_imp_sublist) (simp_all add: take_is_prefix)
  1239 
  1240 lemma sublist_drop [simp, intro]: "sublist (drop n xs) xs"
  1241   by (rule suffix_imp_sublist) (simp_all add: suffix_drop)
  1242     
  1243 lemma sublist_tl [simp, intro]: "sublist (tl xs) xs"
  1244   by (rule suffix_imp_sublist) (simp_all add: suffix_drop)
  1245     
  1246 lemma sublist_butlast [simp, intro]: "sublist (butlast xs) xs"
  1247   by (rule prefix_imp_sublist) (simp_all add: prefixeq_butlast)
  1248     
  1249 lemma sublist_rev [simp]: "sublist (rev xs) (rev ys) = sublist xs ys"
  1250 proof
  1251   assume "sublist (rev xs) (rev ys)"
  1252   then obtain as bs where "rev ys = as @ rev xs @ bs"
  1253     by (auto simp: sublist_def)
  1254   also have "rev \<dots> = rev bs @ xs @ rev as" by simp
  1255   finally show "sublist xs ys" by simp
  1256 next
  1257   assume "sublist xs ys"
  1258   then obtain as bs where "ys = as @ xs @ bs"
  1259     by (auto simp: sublist_def)
  1260   also have "rev \<dots> = rev bs @ rev xs @ rev as" by simp
  1261   finally show "sublist (rev xs) (rev ys)" by simp
  1262 qed
  1263     
  1264 lemma sublist_rev_left: "sublist (rev xs) ys = sublist xs (rev ys)"
  1265   by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident)
  1266     
  1267 lemma sublist_rev_right: "sublist xs (rev ys) = sublist (rev xs) ys"
  1268   by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident)
  1269 
  1270 lemma snoc_sublist_snoc: 
  1271   "sublist (xs @ [x]) (ys @ [y]) \<longleftrightarrow> 
  1272      (x = y \<and> suffix xs ys \<or> sublist (xs @ [x]) ys) "
  1273   by (subst (1 2) sublist_rev [symmetric])
  1274      (simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix)
  1275 
  1276 lemma sublist_snoc:
  1277   "sublist xs (ys @ [y]) \<longleftrightarrow> suffix xs (ys @ [y]) \<or> sublist xs ys"
  1278   by (subst (1 2) sublist_rev [symmetric])
  1279      (simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix)     
  1280      
  1281 lemma sublist_imp_subseq [intro]: "sublist xs ys \<Longrightarrow> subseq xs ys"
  1282   by (auto simp: sublist_def)
  1283 
  1284 subsection \<open>Parametricity\<close>
  1285 
  1286 context includes lifting_syntax
  1287 begin    
  1288   
  1289 private lemma prefix_primrec:
  1290   "prefix = rec_list (\<lambda>xs. True) (\<lambda>x xs xsa ys.
  1291               case ys of [] \<Rightarrow> False | y # ys \<Rightarrow> x = y \<and> xsa ys)"
  1292 proof (intro ext, goal_cases)
  1293   case (1 xs ys)
  1294   show ?case by (induction xs arbitrary: ys) (auto simp: prefix_Cons split: list.splits)
  1295 qed
  1296 
  1297 private lemma sublist_primrec:
  1298   "sublist = (\<lambda>xs ys. rec_list (\<lambda>xs. xs = []) (\<lambda>y ys ysa xs. prefix xs (y # ys) \<or> ysa xs) ys xs)"
  1299 proof (intro ext, goal_cases)
  1300   case (1 xs ys)
  1301   show ?case by (induction ys) (auto simp: sublist_Cons_right)
  1302 qed
  1303 
  1304 private lemma list_emb_primrec:
  1305   "list_emb = (\<lambda>uu uua uuaa. rec_list (\<lambda>P xs. List.null xs) (\<lambda>y ys ysa P xs. case xs of [] \<Rightarrow> True 
  1306      | x # xs \<Rightarrow> if P x y then ysa P xs else ysa P (x # xs)) uuaa uu uua)"
  1307 proof (intro ext, goal_cases)
  1308   case (1 P xs ys)
  1309   show ?case
  1310     by (induction ys arbitrary: xs)
  1311        (auto simp: list_emb_code List.null_def split: list.splits)
  1312 qed
  1313 
  1314 lemma prefix_transfer [transfer_rule]:
  1315   assumes [transfer_rule]: "bi_unique A"
  1316   shows   "(list_all2 A ===> list_all2 A ===> op =) prefix prefix"  
  1317   unfolding prefix_primrec by transfer_prover
  1318     
  1319 lemma suffix_transfer [transfer_rule]:
  1320   assumes [transfer_rule]: "bi_unique A"
  1321   shows   "(list_all2 A ===> list_all2 A ===> op =) suffix suffix"  
  1322   unfolding suffix_to_prefix [abs_def] by transfer_prover
  1323 
  1324 lemma sublist_transfer [transfer_rule]:
  1325   assumes [transfer_rule]: "bi_unique A"
  1326   shows   "(list_all2 A ===> list_all2 A ===> op =) sublist sublist"
  1327   unfolding sublist_primrec by transfer_prover
  1328 
  1329 lemma parallel_transfer [transfer_rule]:
  1330   assumes [transfer_rule]: "bi_unique A"
  1331   shows   "(list_all2 A ===> list_all2 A ===> op =) parallel parallel"
  1332   unfolding parallel_def by transfer_prover
  1333     
  1334 
  1335 
  1336 lemma list_emb_transfer [transfer_rule]:
  1337   "((A ===> A ===> op =) ===> list_all2 A ===> list_all2 A ===> op =) list_emb list_emb"
  1338   unfolding list_emb_primrec by transfer_prover
  1339 
  1340 lemma strict_prefix_transfer [transfer_rule]:
  1341   assumes [transfer_rule]: "bi_unique A"
  1342   shows   "(list_all2 A ===> list_all2 A ===> op =) strict_prefix strict_prefix"  
  1343   unfolding strict_prefix_def by transfer_prover
  1344     
  1345 lemma strict_suffix_transfer [transfer_rule]:
  1346   assumes [transfer_rule]: "bi_unique A"
  1347   shows   "(list_all2 A ===> list_all2 A ===> op =) strict_suffix strict_suffix"  
  1348   unfolding strict_suffix_def by transfer_prover
  1349     
  1350 lemma strict_subseq_transfer [transfer_rule]:
  1351   assumes [transfer_rule]: "bi_unique A"
  1352   shows   "(list_all2 A ===> list_all2 A ===> op =) strict_subseq strict_subseq"  
  1353   unfolding strict_subseq_def by transfer_prover
  1354     
  1355 lemma strict_sublist_transfer [transfer_rule]:
  1356   assumes [transfer_rule]: "bi_unique A"
  1357   shows   "(list_all2 A ===> list_all2 A ===> op =) strict_sublist strict_sublist"  
  1358   unfolding strict_sublist_def by transfer_prover
  1359 
  1360 lemma prefixes_transfer [transfer_rule]:
  1361   assumes [transfer_rule]: "bi_unique A"
  1362   shows   "(list_all2 A ===> list_all2 (list_all2 A)) prefixes prefixes"
  1363   unfolding prefixes_def by transfer_prover
  1364     
  1365 lemma suffixes_transfer [transfer_rule]:
  1366   assumes [transfer_rule]: "bi_unique A"
  1367   shows   "(list_all2 A ===> list_all2 (list_all2 A)) suffixes suffixes"
  1368   unfolding suffixes_def by transfer_prover
  1369     
  1370 lemma sublists_transfer [transfer_rule]:
  1371   assumes [transfer_rule]: "bi_unique A"
  1372   shows   "(list_all2 A ===> list_all2 (list_all2 A)) sublists sublists"
  1373   unfolding sublists_def by transfer_prover
  1374 
  1375 end
  1376 
  1377 end