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