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