| author | wenzelm | 
| Tue, 01 Sep 2015 22:32:58 +0200 | |
| changeset 61076 | bdc1e2f0a86a | 
| parent 60679 | ade12ef2773c | 
| child 63117 | acb6d72fc42e | 
| permissions | -rw-r--r-- | 
| 49087 | 1 | (* Title: HOL/Library/Sublist.thy | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 2 | Author: Tobias Nipkow and Markus Wenzel, TU Muenchen | 
| 49087 | 3 | Author: Christian Sternagel, JAIST | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 4 | *) | 
| 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 5 | |
| 60500 | 6 | section \<open>List prefixes, suffixes, and homeomorphic embedding\<close> | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 7 | |
| 49087 | 8 | theory Sublist | 
| 9 | imports Main | |
| 15131 | 10 | begin | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 11 | |
| 60500 | 12 | subsection \<open>Prefix order on lists\<close> | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 13 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 14 | definition prefixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 15 | where "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 16 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 17 | definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 18 | where "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 19 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 20 | interpretation prefix_order: order prefixeq prefix | 
| 60679 | 21 | by standard (auto simp: prefixeq_def prefix_def) | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 22 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 23 | interpretation prefix_bot: order_bot Nil prefixeq prefix | 
| 60679 | 24 | by standard (simp add: prefixeq_def) | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 25 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 26 | lemma prefixeqI [intro?]: "ys = xs @ zs \<Longrightarrow> prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 27 | unfolding prefixeq_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 28 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 29 | lemma prefixeqE [elim?]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 30 | assumes "prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 31 | obtains zs where "ys = xs @ zs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 32 | using assms unfolding prefixeq_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 33 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 34 | lemma prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> prefix xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 35 | unfolding prefix_def prefixeq_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 36 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 37 | lemma prefixE' [elim?]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 38 | assumes "prefix xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 39 | obtains z zs where "ys = xs @ z # zs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 40 | proof - | 
| 60500 | 41 | from \<open>prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys" | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 42 | unfolding prefix_def prefixeq_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 43 | 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 | 44 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 45 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 46 | lemma prefixI [intro?]: "prefixeq xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> prefix xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 47 | unfolding prefix_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 48 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 49 | lemma prefixE [elim?]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 50 | fixes xs ys :: "'a list" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 51 | assumes "prefix xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 52 | obtains "prefixeq xs ys" and "xs \<noteq> ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 53 | using assms unfolding prefix_def by blast | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 54 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 55 | |
| 60500 | 56 | subsection \<open>Basic properties of prefixes\<close> | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 57 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 58 | theorem Nil_prefixeq [iff]: "prefixeq [] xs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 59 | by (simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 60 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 61 | theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 62 | by (induct xs) (simp_all add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 63 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 64 | lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 65 | proof | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 66 | assume "prefixeq xs (ys @ [y])" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 67 | then obtain zs where zs: "ys @ [y] = xs @ zs" .. | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 68 | show "xs = ys @ [y] \<or> prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 69 | by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 70 | next | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 71 | assume "xs = ys @ [y] \<or> prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 72 | then show "prefixeq xs (ys @ [y])" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 73 | by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 74 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 75 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 76 | lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 77 | by (auto simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 78 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 79 | lemma prefixeq_code [code]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 80 | "prefixeq [] xs \<longleftrightarrow> True" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 81 | "prefixeq (x # xs) [] \<longleftrightarrow> False" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 82 | "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 83 | by simp_all | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 84 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 85 | lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 86 | by (induct xs) simp_all | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 87 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 88 | lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 89 | by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 90 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 91 | lemma prefixeq_prefixeq [simp]: "prefixeq xs ys \<Longrightarrow> prefixeq xs (ys @ zs)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 92 | by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 93 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 94 | lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 95 | by (auto simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 96 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 97 | theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 98 | by (cases xs) (auto simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 99 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 100 | theorem prefixeq_append: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 101 | "prefixeq xs (ys @ zs) = (prefixeq xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefixeq us zs))" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 102 | apply (induct zs rule: rev_induct) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 103 | apply force | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 104 | apply (simp del: append_assoc add: append_assoc [symmetric]) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 105 | apply (metis append_eq_appendI) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 106 | done | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 107 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 108 | lemma append_one_prefixeq: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 109 | "prefixeq xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefixeq (xs @ [ys ! length xs]) ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 110 | proof (unfold prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 111 | assume a1: "\<exists>zs. ys = xs @ zs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 112 | 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 | 113 | assume a2: "length xs < length ys" | 
| 61076 | 114 | 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 | 115 | 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 | 116 | 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 | 117 | 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 | 118 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 119 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 120 | theorem prefixeq_length_le: "prefixeq xs ys \<Longrightarrow> length xs \<le> length ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 121 | by (auto simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 122 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 123 | lemma prefixeq_same_cases: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 124 | "prefixeq (xs\<^sub>1::'a list) ys \<Longrightarrow> prefixeq xs\<^sub>2 ys \<Longrightarrow> prefixeq xs\<^sub>1 xs\<^sub>2 \<or> prefixeq xs\<^sub>2 xs\<^sub>1" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 125 | unfolding prefixeq_def by (force simp: append_eq_append_conv2) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 126 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 127 | lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 128 | by (auto simp add: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 129 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 130 | lemma take_is_prefixeq: "prefixeq (take n xs) xs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 131 | unfolding prefixeq_def by (metis append_take_drop_id) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 132 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 133 | lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 134 | by (auto simp: prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 135 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 136 | lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 137 | by (auto simp: prefix_def prefixeq_def) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 138 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 139 | lemma prefix_simps [simp, code]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 140 | "prefix xs [] \<longleftrightarrow> False" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 141 | "prefix [] (x # xs) \<longleftrightarrow> True" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 142 | "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 143 | by (simp_all add: prefix_def cong: conj_cong) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 144 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 145 | lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 146 | apply (induct n arbitrary: xs ys) | 
| 59997 | 147 | apply (case_tac ys; simp) | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 148 | apply (metis prefix_order.less_trans prefixI take_is_prefixeq) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 149 | done | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 150 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 151 | lemma not_prefixeq_cases: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 152 | assumes pfx: "\<not> prefixeq ps ls" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 153 | obtains | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 154 | (c1) "ps \<noteq> []" and "ls = []" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 155 | | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 156 | | (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 | 157 | proof (cases ps) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 158 | case Nil | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 159 | then show ?thesis using pfx by simp | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 160 | next | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 161 | case (Cons a as) | 
| 60500 | 162 | note c = \<open>ps = a#as\<close> | 
| 55579 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 163 | show ?thesis | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 164 | proof (cases ls) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 165 | case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 166 | next | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 167 | case (Cons x xs) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 168 | show ?thesis | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 169 | proof (cases "x = a") | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 170 | case True | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 171 | have "\<not> prefixeq as xs" using pfx c Cons True by simp | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 172 | with c Cons True show ?thesis by (rule c2) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 173 | next | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 174 | case False | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 175 | with c Cons show ?thesis by (rule c3) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 176 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 177 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 178 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 179 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 180 | lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]: | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 181 | assumes np: "\<not> prefixeq ps ls" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 182 | and base: "\<And>x xs. P (x#xs) []" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 183 | and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 184 | and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 185 | shows "P ps ls" using np | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 186 | proof (induct ls arbitrary: ps) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 187 | case Nil then show ?case | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 188 | by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base) | 
| 
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 y ys) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 191 | then have npfx: "\<not> prefixeq ps (y # ys)" by simp | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 192 | then obtain x xs where pv: "ps = x # xs" | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 193 | by (rule not_prefixeq_cases) auto | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 194 | show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2) | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 195 | qed | 
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 196 | |
| 
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
 traytel parents: 
54538diff
changeset | 197 | |
| 60500 | 198 | subsection \<open>Parallel lists\<close> | 
| 10389 | 199 | |
| 50516 | 200 | definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "\<parallel>" 50) | 
| 49107 | 201 | where "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)" | 
| 10389 | 202 | |
| 50516 | 203 | lemma parallelI [intro]: "\<not> prefixeq xs ys \<Longrightarrow> \<not> prefixeq ys xs \<Longrightarrow> xs \<parallel> ys" | 
| 25692 | 204 | unfolding parallel_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 205 | |
| 10389 | 206 | lemma parallelE [elim]: | 
| 25692 | 207 | assumes "xs \<parallel> ys" | 
| 49087 | 208 | obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs" | 
| 25692 | 209 | using assms unfolding parallel_def by blast | 
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 210 | |
| 49087 | 211 | theorem prefixeq_cases: | 
| 212 | obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys" | |
| 213 | unfolding parallel_def prefix_def by blast | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 214 | |
| 10389 | 215 | theorem parallel_decomp: | 
| 50516 | 216 | "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs" | 
| 10408 | 217 | proof (induct xs rule: rev_induct) | 
| 11987 | 218 | case Nil | 
| 23254 | 219 | then have False by auto | 
| 220 | then show ?case .. | |
| 10408 | 221 | next | 
| 11987 | 222 | case (snoc x xs) | 
| 223 | show ?case | |
| 49087 | 224 | proof (rule prefixeq_cases) | 
| 225 | assume le: "prefixeq xs ys" | |
| 10408 | 226 | then obtain ys' where ys: "ys = xs @ ys'" .. | 
| 227 | show ?thesis | |
| 228 | proof (cases ys') | |
| 25564 | 229 | assume "ys' = []" | 
| 49087 | 230 | then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys) | 
| 10389 | 231 | next | 
| 10408 | 232 | fix c cs assume ys': "ys' = c # cs" | 
| 54483 | 233 | have "x \<noteq> c" using snoc.prems ys ys' by fastforce | 
| 234 | thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs" | |
| 235 | using ys ys' by blast | |
| 10389 | 236 | qed | 
| 10408 | 237 | next | 
| 49107 | 238 | assume "prefix ys xs" | 
| 239 | then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def) | |
| 11987 | 240 | with snoc have False by blast | 
| 23254 | 241 | then show ?thesis .. | 
| 10408 | 242 | next | 
| 243 | assume "xs \<parallel> ys" | |
| 11987 | 244 | with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c" | 
| 10408 | 245 | and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs" | 
| 246 | by blast | |
| 247 | from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp | |
| 248 | with neq ys show ?thesis by blast | |
| 10389 | 249 | qed | 
| 250 | qed | |
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 251 | |
| 25564 | 252 | lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" | 
| 25692 | 253 | apply (rule parallelI) | 
| 254 | apply (erule parallelE, erule conjE, | |
| 49087 | 255 | induct rule: not_prefixeq_induct, simp+)+ | 
| 25692 | 256 | done | 
| 25299 | 257 | |
| 25692 | 258 | lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" | 
| 259 | by (simp add: parallel_append) | |
| 25299 | 260 | |
| 25692 | 261 | lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" | 
| 262 | unfolding parallel_def by auto | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 263 | |
| 25356 | 264 | |
| 60500 | 265 | subsection \<open>Suffix order on lists\<close> | 
| 17201 | 266 | |
| 49107 | 267 | definition suffixeq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 268 | where "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)" | |
| 49087 | 269 | |
| 49107 | 270 | definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 271 | where "suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])" | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 272 | |
| 49087 | 273 | lemma suffix_imp_suffixeq: | 
| 274 | "suffix xs ys \<Longrightarrow> suffixeq xs ys" | |
| 275 | by (auto simp: suffixeq_def suffix_def) | |
| 276 | ||
| 50516 | 277 | lemma suffixeqI [intro?]: "ys = zs @ xs \<Longrightarrow> suffixeq xs ys" | 
| 49087 | 278 | unfolding suffixeq_def by blast | 
| 21305 | 279 | |
| 49087 | 280 | lemma suffixeqE [elim?]: | 
| 281 | assumes "suffixeq xs ys" | |
| 282 | obtains zs where "ys = zs @ xs" | |
| 283 | using assms unfolding suffixeq_def by blast | |
| 21305 | 284 | |
| 49087 | 285 | lemma suffixeq_refl [iff]: "suffixeq xs xs" | 
| 286 | by (auto simp add: suffixeq_def) | |
| 287 | lemma suffix_trans: | |
| 288 | "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs" | |
| 289 | by (auto simp: suffix_def) | |
| 290 | lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs" | |
| 291 | by (auto simp add: suffixeq_def) | |
| 292 | lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys" | |
| 293 | by (auto simp add: suffixeq_def) | |
| 294 | ||
| 295 | lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs" | |
| 296 | by (induct xs) (auto simp: suffixeq_def) | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 297 | |
| 49087 | 298 | lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs" | 
| 299 | 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 | 300 | |
| 49087 | 301 | lemma Nil_suffixeq [iff]: "suffixeq [] xs" | 
| 302 | by (simp add: suffixeq_def) | |
| 303 | lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])" | |
| 304 | by (auto simp add: suffixeq_def) | |
| 305 | ||
| 49107 | 306 | lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y # ys)" | 
| 49087 | 307 | by (auto simp add: suffixeq_def) | 
| 49107 | 308 | lemma suffixeq_ConsD: "suffixeq (x # xs) ys \<Longrightarrow> suffixeq xs ys" | 
| 49087 | 309 | by (auto simp add: suffixeq_def) | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 310 | |
| 49087 | 311 | lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)" | 
| 312 | by (auto simp add: suffixeq_def) | |
| 313 | lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys" | |
| 314 | by (auto simp add: suffixeq_def) | |
| 315 | ||
| 316 | lemma suffix_set_subset: | |
| 317 | "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def) | |
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 318 | |
| 49087 | 319 | lemma suffixeq_set_subset: | 
| 320 | "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def) | |
| 321 | ||
| 49107 | 322 | lemma suffixeq_ConsD2: "suffixeq (x # xs) (y # ys) \<Longrightarrow> suffixeq xs ys" | 
| 21305 | 323 | proof - | 
| 49107 | 324 | assume "suffixeq (x # xs) (y # ys)" | 
| 325 | then obtain zs where "y # ys = zs @ x # xs" .. | |
| 49087 | 326 | then show ?thesis | 
| 327 | by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI) | |
| 21305 | 328 | qed | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 329 | |
| 49087 | 330 | lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)" | 
| 331 | proof | |
| 332 | assume "suffixeq xs ys" | |
| 333 | then obtain zs where "ys = zs @ xs" .. | |
| 334 | then have "rev ys = rev xs @ rev zs" by simp | |
| 335 | then show "prefixeq (rev xs) (rev ys)" .. | |
| 336 | next | |
| 337 | assume "prefixeq (rev xs) (rev ys)" | |
| 338 | then obtain zs where "rev ys = rev xs @ zs" .. | |
| 339 | then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp | |
| 340 | then have "ys = rev zs @ xs" by simp | |
| 341 | then show "suffixeq xs ys" .. | |
| 21305 | 342 | qed | 
| 14538 
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
 oheimb parents: 
14300diff
changeset | 343 | |
| 49087 | 344 | lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs" | 
| 345 | by (clarsimp elim!: suffixeqE) | |
| 17201 | 346 | |
| 49087 | 347 | lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)" | 
| 348 | by (auto elim!: suffixeqE intro: suffixeqI) | |
| 25299 | 349 | |
| 49087 | 350 | lemma suffixeq_drop: "suffixeq (drop n as) as" | 
| 351 | unfolding suffixeq_def | |
| 25692 | 352 | apply (rule exI [where x = "take n as"]) | 
| 353 | apply simp | |
| 354 | done | |
| 25299 | 355 | |
| 49087 | 356 | lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs" | 
| 49107 | 357 | by (auto elim!: suffixeqE) | 
| 25299 | 358 | |
| 49107 | 359 | lemma suffixeq_suffix_reflclp_conv: "suffixeq = suffix\<^sup>=\<^sup>=" | 
| 49087 | 360 | proof (intro ext iffI) | 
| 361 | fix xs ys :: "'a list" | |
| 362 | assume "suffixeq xs ys" | |
| 363 | show "suffix\<^sup>=\<^sup>= xs ys" | |
| 364 | proof | |
| 365 | assume "xs \<noteq> ys" | |
| 60500 | 366 | with \<open>suffixeq xs ys\<close> show "suffix xs ys" | 
| 49107 | 367 | by (auto simp: suffixeq_def suffix_def) | 
| 49087 | 368 | qed | 
| 369 | next | |
| 370 | fix xs ys :: "'a list" | |
| 371 | assume "suffix\<^sup>=\<^sup>= xs ys" | |
| 49107 | 372 | then show "suffixeq xs ys" | 
| 49087 | 373 | proof | 
| 49107 | 374 | assume "suffix xs ys" then show "suffixeq xs ys" | 
| 375 | by (rule suffix_imp_suffixeq) | |
| 49087 | 376 | next | 
| 49107 | 377 | assume "xs = ys" then show "suffixeq xs ys" | 
| 378 | by (auto simp: suffixeq_def) | |
| 49087 | 379 | qed | 
| 380 | qed | |
| 381 | ||
| 382 | lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y" | |
| 25692 | 383 | by blast | 
| 25299 | 384 | |
| 49087 | 385 | lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x" | 
| 25692 | 386 | by blast | 
| 25355 | 387 | |
| 388 | lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" | |
| 25692 | 389 | unfolding parallel_def by simp | 
| 25355 | 390 | |
| 25299 | 391 | lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" | 
| 25692 | 392 | unfolding parallel_def by simp | 
| 25299 | 393 | |
| 25564 | 394 | lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" | 
| 25692 | 395 | by auto | 
| 25299 | 396 | |
| 25564 | 397 | lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" | 
| 49087 | 398 | by (metis Cons_prefixeq_Cons parallelE parallelI) | 
| 25665 | 399 | |
| 25299 | 400 | lemma not_equal_is_parallel: | 
| 401 | assumes neq: "xs \<noteq> ys" | |
| 25356 | 402 | and len: "length xs = length ys" | 
| 403 | shows "xs \<parallel> ys" | |
| 25299 | 404 | using len neq | 
| 25355 | 405 | proof (induct rule: list_induct2) | 
| 26445 | 406 | case Nil | 
| 25356 | 407 | then show ?case by simp | 
| 25299 | 408 | next | 
| 26445 | 409 | case (Cons a as b bs) | 
| 25355 | 410 | have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact | 
| 25299 | 411 | show ?case | 
| 412 | proof (cases "a = b") | |
| 25355 | 413 | case True | 
| 26445 | 414 | then have "as \<noteq> bs" using Cons by simp | 
| 25355 | 415 | then show ?thesis by (rule Cons_parallelI2 [OF True ih]) | 
| 25299 | 416 | next | 
| 417 | case False | |
| 25355 | 418 | then show ?thesis by (rule Cons_parallelI1) | 
| 25299 | 419 | qed | 
| 420 | qed | |
| 22178 | 421 | |
| 49107 | 422 | lemma suffix_reflclp_conv: "suffix\<^sup>=\<^sup>= = suffixeq" | 
| 49087 | 423 | by (intro ext) (auto simp: suffixeq_def suffix_def) | 
| 424 | ||
| 49107 | 425 | lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A" | 
| 49087 | 426 | unfolding suffix_def by auto | 
| 427 | ||
| 428 | ||
| 60500 | 429 | subsection \<open>Homeomorphic embedding on lists\<close> | 
| 49087 | 430 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 431 | inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
 | 
| 49087 | 432 |   for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
 | 
| 433 | where | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 434 | list_emb_Nil [intro, simp]: "list_emb P [] ys" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 435 | | 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 | 436 | | list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)" | 
| 50516 | 437 | |
| 57499 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 438 | lemma list_emb_mono: | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 439 | assumes "\<And>x y. P x y \<longrightarrow> Q x y" | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 440 | shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys" | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 441 | proof | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 442 | assume "list_emb P xs ys" | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 443 | then show "list_emb Q xs ys" by (induct) (auto simp: assms) | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 444 | qed | 
| 
7e22776f2d32
added monotonicity lemma for list embedding
 Christian Sternagel parents: 
57498diff
changeset | 445 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 446 | lemma list_emb_Nil2 [simp]: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 447 | assumes "list_emb P xs []" shows "xs = []" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 448 | using assms by (cases rule: list_emb.cases) auto | 
| 49087 | 449 | |
| 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 | 450 | 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 | 451 | 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 | 452 | 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 | 453 | using assms by (induct xs) auto | 
| 49087 | 454 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 455 | lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False" | 
| 49087 | 456 | proof - | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 457 |   { assume "list_emb P (x#xs) []"
 | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 458 | from list_emb_Nil2 [OF this] have False by simp | 
| 49087 | 459 |   } moreover {
 | 
| 460 | assume False | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 461 | then have "list_emb P (x#xs) []" by simp | 
| 49087 | 462 | } ultimately show ?thesis by blast | 
| 463 | qed | |
| 464 | ||
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 465 | lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)" | 
| 49087 | 466 | by (induct zs) auto | 
| 467 | ||
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 468 | lemma list_emb_prefix [intro]: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 469 | assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)" | 
| 49087 | 470 | using assms | 
| 471 | by (induct arbitrary: zs) auto | |
| 472 | ||
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 473 | lemma list_emb_ConsD: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 474 | 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 | 475 | shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs" | 
| 49087 | 476 | using assms | 
| 49107 | 477 | 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 | 478 | case list_emb_Cons | 
| 49107 | 479 | then show ?case by (metis append_Cons) | 
| 49087 | 480 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 481 | case (list_emb_Cons2 x y xs ys) | 
| 54483 | 482 | then show ?case by blast | 
| 49087 | 483 | qed | 
| 484 | ||
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 485 | lemma list_emb_appendD: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 486 | assumes "list_emb P (xs @ ys) zs" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 487 | shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs" | 
| 49087 | 488 | using assms | 
| 489 | proof (induction xs arbitrary: ys zs) | |
| 49107 | 490 | case Nil then show ?case by auto | 
| 49087 | 491 | next | 
| 492 | case (Cons x xs) | |
| 54483 | 493 | 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 | 494 | 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 | 495 | by (auto dest: list_emb_ConsD) | 
| 54483 | 496 | 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 | 497 | 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 | 498 | using Cons(1) by (metis (no_types)) | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 499 | 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 | 500 | thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc) | 
| 49087 | 501 | qed | 
| 502 | ||
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 503 | lemma list_emb_suffix: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 504 | assumes "list_emb P xs ys" and "suffix ys zs" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 505 | shows "list_emb P xs zs" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 506 | using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: suffix_def) | 
| 49087 | 507 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 508 | lemma list_emb_suffixeq: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 509 | assumes "list_emb P xs ys" and "suffixeq ys zs" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 510 | shows "list_emb P xs zs" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 511 | using assms and list_emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto | 
| 49087 | 512 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 513 | 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 | 514 | by (induct rule: list_emb.induct) auto | 
| 49087 | 515 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 516 | lemma list_emb_trans: | 
| 57500 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 517 | 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 | 518 | shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs" | 
| 50516 | 519 | proof - | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 520 | 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 | 521 | then show "list_emb P xs zs" using assms | 
| 49087 | 522 | proof (induction arbitrary: zs) | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 523 | case list_emb_Nil show ?case by blast | 
| 49087 | 524 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 525 | case (list_emb_Cons xs ys y) | 
| 60500 | 526 | 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 | 527 | 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 | 528 | then have "list_emb P ys (v#vs)" by blast | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 529 | 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 | 530 | from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto | 
| 49087 | 531 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 532 | case (list_emb_Cons2 x y xs ys) | 
| 60500 | 533 | 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 | 534 | 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 | 535 | 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 | 536 | moreover have "P x v" | 
| 49087 | 537 | proof - | 
| 57500 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 538 | from zs have "v \<in> set zs" by auto | 
| 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 539 | moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all | 
| 50516 | 540 | ultimately show ?thesis | 
| 60500 | 541 | using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2 | 
| 50516 | 542 | by blast | 
| 49087 | 543 | qed | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 544 | 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 | 545 | then show ?case unfolding zs by (rule list_emb_append2) | 
| 49087 | 546 | qed | 
| 547 | qed | |
| 548 | ||
| 57500 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 549 | lemma list_emb_set: | 
| 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 550 | 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 | 551 | 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 | 552 | using assms by (induct) auto | 
| 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 553 | |
| 49087 | 554 | |
| 60500 | 555 | subsection \<open>Sublists (special case of homeomorphic embedding)\<close> | 
| 49087 | 556 | |
| 50516 | 557 | abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 558 | where "sublisteq xs ys \<equiv> list_emb (op =) xs ys" | 
| 49087 | 559 | |
| 50516 | 560 | lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto | 
| 49087 | 561 | |
| 50516 | 562 | lemma sublisteq_same_length: | 
| 563 | assumes "sublisteq 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 | 564 | using assms by (induct) (auto dest: list_emb_length) | 
| 49087 | 565 | |
| 50516 | 566 | lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys" | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 567 | by (metis list_emb_length linorder_not_less) | 
| 49087 | 568 | |
| 569 | lemma [code]: | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 570 | "list_emb P [] ys \<longleftrightarrow> True" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 571 | "list_emb P (x#xs) [] \<longleftrightarrow> False" | 
| 49087 | 572 | by (simp_all) | 
| 573 | ||
| 50516 | 574 | lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys" | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 575 | by (induct xs, simp, blast dest: list_emb_ConsD) | 
| 49087 | 576 | |
| 50516 | 577 | lemma sublisteq_Cons2': | 
| 578 | assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys" | |
| 579 | using assms by (cases) (rule sublisteq_Cons') | |
| 49087 | 580 | |
| 50516 | 581 | lemma sublisteq_Cons2_neq: | 
| 582 | assumes "sublisteq (x#xs) (y#ys)" | |
| 583 | shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys" | |
| 49087 | 584 | using assms by (cases) auto | 
| 585 | ||
| 50516 | 586 | lemma sublisteq_Cons2_iff [simp, code]: | 
| 587 | "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)" | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 588 | by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq) | 
| 49087 | 589 | |
| 50516 | 590 | lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys" | 
| 49087 | 591 | by (induct zs) simp_all | 
| 592 | ||
| 50516 | 593 | lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all | 
| 49087 | 594 | |
| 50516 | 595 | lemma sublisteq_antisym: | 
| 596 | assumes "sublisteq xs ys" and "sublisteq ys xs" | |
| 49087 | 597 | shows "xs = ys" | 
| 598 | using assms | |
| 599 | proof (induct) | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 600 | case list_emb_Nil | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 601 | from list_emb_Nil2 [OF this] show ?case by simp | 
| 49087 | 602 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 603 | case list_emb_Cons2 | 
| 54483 | 604 | thus ?case by simp | 
| 49087 | 605 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 606 | case list_emb_Cons | 
| 54483 | 607 | hence False using sublisteq_Cons' by fastforce | 
| 608 | thus ?case .. | |
| 49087 | 609 | qed | 
| 610 | ||
| 50516 | 611 | lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs" | 
| 57500 
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
 Christian Sternagel parents: 
57499diff
changeset | 612 | by (rule list_emb_trans [of _ _ _ "op ="]) auto | 
| 49087 | 613 | |
| 50516 | 614 | lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []" | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 615 | by (auto dest: list_emb_length) | 
| 49087 | 616 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 617 | lemma list_emb_append_mono: | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 618 | "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')" | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 619 | apply (induct rule: list_emb.induct) | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 620 | apply (metis eq_Nil_appendI list_emb_append2) | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 621 | apply (metis append_Cons list_emb_Cons) | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 622 | apply (metis append_Cons list_emb_Cons2) | 
| 49107 | 623 | done | 
| 49087 | 624 | |
| 625 | ||
| 60500 | 626 | subsection \<open>Appending elements\<close> | 
| 49087 | 627 | |
| 50516 | 628 | lemma sublisteq_append [simp]: | 
| 629 | "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r") | |
| 49087 | 630 | proof | 
| 50516 | 631 |   { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
 | 
| 632 | then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys" | |
| 49087 | 633 | proof (induct arbitrary: xs ys zs) | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 634 | case list_emb_Nil show ?case by simp | 
| 49087 | 635 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 636 | case (list_emb_Cons xs' ys' x) | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 637 |       { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
 | 
| 49087 | 638 | moreover | 
| 639 |       { fix us assume "ys = x#us"
 | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 640 | then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) } | 
| 49087 | 641 | ultimately show ?case by (auto simp:Cons_eq_append_conv) | 
| 642 | next | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 643 | case (list_emb_Cons2 x y xs' ys') | 
| 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 644 |       { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
 | 
| 49087 | 645 | moreover | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 646 |       { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
 | 
| 49087 | 647 | moreover | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 648 |       { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
 | 
| 60500 | 649 | ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv) | 
| 49087 | 650 | qed } | 
| 651 | moreover assume ?l | |
| 652 | ultimately show ?r by blast | |
| 653 | next | |
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 654 | assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl) | 
| 49087 | 655 | qed | 
| 656 | ||
| 50516 | 657 | lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)" | 
| 49087 | 658 | by (induct zs) auto | 
| 659 | ||
| 50516 | 660 | lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)" | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 661 | by (metis append_Nil2 list_emb_Nil list_emb_append_mono) | 
| 49087 | 662 | |
| 663 | ||
| 60500 | 664 | subsection \<open>Relation to standard list operations\<close> | 
| 49087 | 665 | |
| 50516 | 666 | lemma sublisteq_map: | 
| 667 | assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)" | |
| 49087 | 668 | using assms by (induct) auto | 
| 669 | ||
| 50516 | 670 | lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs" | 
| 49087 | 671 | by (induct xs) auto | 
| 672 | ||
| 50516 | 673 | lemma sublisteq_filter [simp]: | 
| 674 | assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)" | |
| 54483 | 675 | using assms by induct auto | 
| 49087 | 676 | |
| 50516 | 677 | lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R") | 
| 49087 | 678 | proof | 
| 679 | assume ?L | |
| 49107 | 680 | then show ?R | 
| 49087 | 681 | proof (induct) | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 682 | case list_emb_Nil show ?case by (metis sublist_empty) | 
| 49087 | 683 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 684 | case (list_emb_Cons xs ys x) | 
| 49087 | 685 | then obtain N where "xs = sublist ys N" by blast | 
| 49107 | 686 | then have "xs = sublist (x#ys) (Suc ` N)" | 
| 49087 | 687 | by (clarsimp simp add:sublist_Cons inj_image_mem_iff) | 
| 49107 | 688 | then show ?case by blast | 
| 49087 | 689 | next | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 690 | case (list_emb_Cons2 x y xs ys) | 
| 49087 | 691 | then obtain N where "xs = sublist ys N" by blast | 
| 49107 | 692 | then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))" | 
| 49087 | 693 | by (clarsimp simp add:sublist_Cons inj_image_mem_iff) | 
| 57497 
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
 Christian Sternagel parents: 
55579diff
changeset | 694 | moreover from list_emb_Cons2 have "x = y" by simp | 
| 50516 | 695 | ultimately show ?case by blast | 
| 49087 | 696 | qed | 
| 697 | next | |
| 698 | assume ?R | |
| 699 | then obtain N where "xs = sublist ys N" .. | |
| 50516 | 700 | moreover have "sublisteq (sublist ys N) ys" | 
| 49107 | 701 | proof (induct ys arbitrary: N) | 
| 49087 | 702 | case Nil show ?case by simp | 
| 703 | next | |
| 49107 | 704 | case Cons then show ?case by (auto simp: sublist_Cons) | 
| 49087 | 705 | qed | 
| 706 | ultimately show ?L by simp | |
| 707 | qed | |
| 708 | ||
| 10330 
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
 wenzelm parents: diff
changeset | 709 | end |