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