src/HOL/Library/Sublist.thy
author Christian Sternagel
Wed Aug 29 12:23:14 2012 +0900 (2012-08-29)
changeset 49083 01081bca31b6
parent 45236 src/HOL/Library/List_Prefix.thy@ac4a2a66707d
permissions -rw-r--r--
dropped ord and bot instance for list prefixes (use locale interpretation instead, which allows users to decide what order to use on lists)
Christian@49083
     1
(*  Title:      HOL/Library/Sublist.thy
wenzelm@10330
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@10330
     3
*)
wenzelm@10330
     4
Christian@49083
     5
header {* List prefixes, suffixes, and embedding*}
wenzelm@10330
     6
Christian@49083
     7
theory Sublist
haftmann@30663
     8
imports List Main
nipkow@15131
     9
begin
wenzelm@10330
    10
wenzelm@10330
    11
subsection {* Prefix order on lists *}
wenzelm@10330
    12
Christian@49083
    13
definition prefixeq :: "'a list => 'a list => bool" where
Christian@49083
    14
  "prefixeq xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
Christian@49083
    15
Christian@49083
    16
definition prefix :: "'a list => 'a list => bool" where
Christian@49083
    17
  "prefix xs ys \<longleftrightarrow> prefixeq xs ys \<and> xs \<noteq> ys"
haftmann@25764
    18
Christian@49083
    19
interpretation prefix_order: order prefixeq prefix
Christian@49083
    20
  by default (auto simp: prefixeq_def prefix_def)
wenzelm@10330
    21
Christian@49083
    22
interpretation prefix_bot: bot prefixeq prefix Nil
Christian@49083
    23
  by default (simp add: prefixeq_def)
Christian@49083
    24
Christian@49083
    25
lemma prefixeqI [intro?]: "ys = xs @ zs ==> prefixeq xs ys"
Christian@49083
    26
  unfolding prefixeq_def by blast
wenzelm@10330
    27
Christian@49083
    28
lemma prefixeqE [elim?]:
Christian@49083
    29
  assumes "prefixeq xs ys"
Christian@49083
    30
  obtains zs where "ys = xs @ zs"
Christian@49083
    31
  using assms unfolding prefixeq_def by blast
Christian@49083
    32
Christian@49083
    33
lemma prefixI' [intro?]: "ys = xs @ z # zs ==> prefix xs ys"
Christian@49083
    34
  unfolding prefix_def prefixeq_def by blast
haftmann@37474
    35
Christian@49083
    36
lemma prefixE' [elim?]:
Christian@49083
    37
  assumes "prefix xs ys"
Christian@49083
    38
  obtains z zs where "ys = xs @ z # zs"
Christian@49083
    39
proof -
Christian@49083
    40
  from `prefix xs ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"
Christian@49083
    41
    unfolding prefix_def prefixeq_def by blast
Christian@49083
    42
  with that show ?thesis by (auto simp add: neq_Nil_conv)
Christian@49083
    43
qed
wenzelm@10330
    44
Christian@49083
    45
lemma prefixI [intro?]: "prefixeq xs ys ==> xs \<noteq> ys ==> prefix xs ys"
wenzelm@18730
    46
  unfolding prefix_def by blast
wenzelm@10330
    47
wenzelm@21305
    48
lemma prefixE [elim?]:
Christian@49083
    49
  fixes xs ys :: "'a list"
Christian@49083
    50
  assumes "prefix xs ys"
Christian@49083
    51
  obtains "prefixeq xs ys" and "xs \<noteq> ys"
wenzelm@23394
    52
  using assms unfolding prefix_def by blast
wenzelm@10330
    53
wenzelm@10330
    54
wenzelm@10389
    55
subsection {* Basic properties of prefixes *}
wenzelm@10330
    56
Christian@49083
    57
theorem Nil_prefixeq [iff]: "prefixeq [] xs"
Christian@49083
    58
  by (simp add: prefixeq_def)
wenzelm@10330
    59
Christian@49083
    60
theorem prefixeq_Nil [simp]: "(prefixeq xs []) = (xs = [])"
Christian@49083
    61
  by (induct xs) (simp_all add: prefixeq_def)
wenzelm@10330
    62
Christian@49083
    63
lemma prefixeq_snoc [simp]: "prefixeq xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefixeq xs ys"
wenzelm@10389
    64
proof
Christian@49083
    65
  assume "prefixeq xs (ys @ [y])"
wenzelm@10389
    66
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
Christian@49083
    67
  show "xs = ys @ [y] \<or> prefixeq xs ys"
Christian@49083
    68
    by (metis append_Nil2 butlast_append butlast_snoc prefixeqI zs)
wenzelm@10389
    69
next
Christian@49083
    70
  assume "xs = ys @ [y] \<or> prefixeq xs ys"
Christian@49083
    71
  then show "prefixeq xs (ys @ [y])"
Christian@49083
    72
    by (metis prefix_order.eq_iff prefix_order.order_trans prefixeqI)
wenzelm@10389
    73
qed
wenzelm@10330
    74
Christian@49083
    75
lemma Cons_prefixeq_Cons [simp]: "prefixeq (x # xs) (y # ys) = (x = y \<and> prefixeq xs ys)"
Christian@49083
    76
  by (auto simp add: prefixeq_def)
wenzelm@10330
    77
Christian@49083
    78
lemma prefixeq_code [code]:
Christian@49083
    79
  "prefixeq [] xs \<longleftrightarrow> True"
Christian@49083
    80
  "prefixeq (x # xs) [] \<longleftrightarrow> False"
Christian@49083
    81
  "prefixeq (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefixeq xs ys"
haftmann@37474
    82
  by simp_all
haftmann@37474
    83
Christian@49083
    84
lemma same_prefixeq_prefixeq [simp]: "prefixeq (xs @ ys) (xs @ zs) = prefixeq ys zs"
wenzelm@10389
    85
  by (induct xs) simp_all
wenzelm@10330
    86
Christian@49083
    87
lemma same_prefixeq_nil [iff]: "prefixeq (xs @ ys) xs = (ys = [])"
Christian@49083
    88
  by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixeqI)
nipkow@25665
    89
Christian@49083
    90
lemma prefixeq_prefixeq [simp]: "prefixeq xs ys ==> prefixeq xs (ys @ zs)"
Christian@49083
    91
  by (metis prefix_order.le_less_trans prefixeqI prefixE prefixI)
nipkow@25665
    92
Christian@49083
    93
lemma append_prefixeqD: "prefixeq (xs @ ys) zs \<Longrightarrow> prefixeq xs zs"
Christian@49083
    94
  by (auto simp add: prefixeq_def)
nipkow@14300
    95
Christian@49083
    96
theorem prefixeq_Cons: "prefixeq xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefixeq zs ys))"
Christian@49083
    97
  by (cases xs) (auto simp add: prefixeq_def)
wenzelm@10330
    98
Christian@49083
    99
theorem prefixeq_append:
Christian@49083
   100
  "prefixeq xs (ys @ zs) = (prefixeq xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefixeq us zs))"
wenzelm@10330
   101
  apply (induct zs rule: rev_induct)
wenzelm@10330
   102
   apply force
wenzelm@10330
   103
  apply (simp del: append_assoc add: append_assoc [symmetric])
nipkow@25564
   104
  apply (metis append_eq_appendI)
wenzelm@10330
   105
  done
wenzelm@10330
   106
Christian@49083
   107
lemma append_one_prefixeq:
Christian@49083
   108
  "prefixeq xs ys ==> length xs < length ys ==> prefixeq (xs @ [ys ! length xs]) ys"
Christian@49083
   109
  unfolding prefixeq_def
wenzelm@25692
   110
  by (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj
wenzelm@25692
   111
    eq_Nil_appendI nth_drop')
nipkow@25665
   112
Christian@49083
   113
theorem prefixeq_length_le: "prefixeq xs ys ==> length xs \<le> length ys"
Christian@49083
   114
  by (auto simp add: prefixeq_def)
wenzelm@10330
   115
Christian@49083
   116
lemma prefixeq_same_cases:
Christian@49083
   117
  "prefixeq (xs\<^isub>1::'a list) ys \<Longrightarrow> prefixeq xs\<^isub>2 ys \<Longrightarrow> prefixeq xs\<^isub>1 xs\<^isub>2 \<or> prefixeq xs\<^isub>2 xs\<^isub>1"
Christian@49083
   118
  unfolding prefixeq_def by (metis append_eq_append_conv2)
nipkow@25665
   119
Christian@49083
   120
lemma set_mono_prefixeq: "prefixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys"
Christian@49083
   121
  by (auto simp add: prefixeq_def)
nipkow@14300
   122
Christian@49083
   123
lemma take_is_prefixeq: "prefixeq (take n xs) xs"
Christian@49083
   124
  unfolding prefixeq_def by (metis append_take_drop_id)
nipkow@25665
   125
Christian@49083
   126
lemma map_prefixeqI: "prefixeq xs ys \<Longrightarrow> prefixeq (map f xs) (map f ys)"
Christian@49083
   127
  by (auto simp: prefixeq_def)
kleing@25322
   128
Christian@49083
   129
lemma prefixeq_length_less: "prefix xs ys \<Longrightarrow> length xs < length ys"
Christian@49083
   130
  by (auto simp: prefix_def prefixeq_def)
nipkow@25665
   131
Christian@49083
   132
lemma prefix_simps [simp, code]:
Christian@49083
   133
  "prefix xs [] \<longleftrightarrow> False"
Christian@49083
   134
  "prefix [] (x # xs) \<longleftrightarrow> True"
Christian@49083
   135
  "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
Christian@49083
   136
  by (simp_all add: prefix_def cong: conj_cong)
kleing@25299
   137
Christian@49083
   138
lemma take_prefix: "prefix xs ys \<Longrightarrow> prefix (take n xs) ys"
wenzelm@25692
   139
  apply (induct n arbitrary: xs ys)
wenzelm@25692
   140
   apply (case_tac ys, simp_all)[1]
Christian@49083
   141
  apply (metis prefix_order.less_trans prefixI take_is_prefixeq)
wenzelm@25692
   142
  done
kleing@25299
   143
Christian@49083
   144
lemma not_prefixeq_cases:
Christian@49083
   145
  assumes pfx: "\<not> prefixeq ps ls"
wenzelm@25356
   146
  obtains
wenzelm@25356
   147
    (c1) "ps \<noteq> []" and "ls = []"
Christian@49083
   148
  | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefixeq as xs"
wenzelm@25356
   149
  | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
kleing@25299
   150
proof (cases ps)
wenzelm@25692
   151
  case Nil then show ?thesis using pfx by simp
kleing@25299
   152
next
kleing@25299
   153
  case (Cons a as)
wenzelm@25692
   154
  note c = `ps = a#as`
kleing@25299
   155
  show ?thesis
kleing@25299
   156
  proof (cases ls)
Christian@49083
   157
    case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefixeq_nil)
kleing@25299
   158
  next
kleing@25299
   159
    case (Cons x xs)
kleing@25299
   160
    show ?thesis
kleing@25299
   161
    proof (cases "x = a")
wenzelm@25355
   162
      case True
Christian@49083
   163
      have "\<not> prefixeq as xs" using pfx c Cons True by simp
wenzelm@25355
   164
      with c Cons True show ?thesis by (rule c2)
wenzelm@25355
   165
    next
wenzelm@25355
   166
      case False
wenzelm@25355
   167
      with c Cons show ?thesis by (rule c3)
kleing@25299
   168
    qed
kleing@25299
   169
  qed
kleing@25299
   170
qed
kleing@25299
   171
Christian@49083
   172
lemma not_prefixeq_induct [consumes 1, case_names Nil Neq Eq]:
Christian@49083
   173
  assumes np: "\<not> prefixeq ps ls"
wenzelm@25356
   174
    and base: "\<And>x xs. P (x#xs) []"
wenzelm@25356
   175
    and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
Christian@49083
   176
    and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefixeq xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
wenzelm@25356
   177
  shows "P ps ls" using np
kleing@25299
   178
proof (induct ls arbitrary: ps)
wenzelm@25355
   179
  case Nil then show ?case
Christian@49083
   180
    by (auto simp: neq_Nil_conv elim!: not_prefixeq_cases intro!: base)
kleing@25299
   181
next
wenzelm@25355
   182
  case (Cons y ys)
Christian@49083
   183
  then have npfx: "\<not> prefixeq ps (y # ys)" by simp
wenzelm@25355
   184
  then obtain x xs where pv: "ps = x # xs"
Christian@49083
   185
    by (rule not_prefixeq_cases) auto
Christian@49083
   186
  show ?case by (metis Cons.hyps Cons_prefixeq_Cons npfx pv r1 r2)
kleing@25299
   187
qed
nipkow@14300
   188
wenzelm@25356
   189
wenzelm@10389
   190
subsection {* Parallel lists *}
wenzelm@10389
   191
wenzelm@19086
   192
definition
wenzelm@21404
   193
  parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
Christian@49083
   194
  "(xs \<parallel> ys) = (\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs)"
wenzelm@10389
   195
Christian@49083
   196
lemma parallelI [intro]: "\<not> prefixeq xs ys ==> \<not> prefixeq ys xs ==> xs \<parallel> ys"
wenzelm@25692
   197
  unfolding parallel_def by blast
wenzelm@10330
   198
wenzelm@10389
   199
lemma parallelE [elim]:
wenzelm@25692
   200
  assumes "xs \<parallel> ys"
Christian@49083
   201
  obtains "\<not> prefixeq xs ys \<and> \<not> prefixeq ys xs"
wenzelm@25692
   202
  using assms unfolding parallel_def by blast
wenzelm@10330
   203
Christian@49083
   204
theorem prefixeq_cases:
Christian@49083
   205
  obtains "prefixeq xs ys" | "prefix ys xs" | "xs \<parallel> ys"
Christian@49083
   206
  unfolding parallel_def prefix_def by blast
wenzelm@10330
   207
wenzelm@10389
   208
theorem parallel_decomp:
wenzelm@10389
   209
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   210
proof (induct xs rule: rev_induct)
wenzelm@11987
   211
  case Nil
wenzelm@23254
   212
  then have False by auto
wenzelm@23254
   213
  then show ?case ..
wenzelm@10408
   214
next
wenzelm@11987
   215
  case (snoc x xs)
wenzelm@11987
   216
  show ?case
Christian@49083
   217
  proof (rule prefixeq_cases)
Christian@49083
   218
    assume le: "prefixeq xs ys"
wenzelm@10408
   219
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   220
    show ?thesis
wenzelm@10408
   221
    proof (cases ys')
nipkow@25564
   222
      assume "ys' = []"
Christian@49083
   223
      then show ?thesis by (metis append_Nil2 parallelE prefixeqI snoc.prems ys)
wenzelm@10389
   224
    next
wenzelm@10408
   225
      fix c cs assume ys': "ys' = c # cs"
wenzelm@25692
   226
      then show ?thesis
Christian@49083
   227
        by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixeqI
Christian@49083
   228
          same_prefixeq_prefixeq snoc.prems ys)
wenzelm@10389
   229
    qed
wenzelm@10408
   230
  next
Christian@49083
   231
    assume "prefix ys xs" then have "prefixeq ys (xs @ [x])" by (simp add: prefix_def)
wenzelm@11987
   232
    with snoc have False by blast
wenzelm@23254
   233
    then show ?thesis ..
wenzelm@10408
   234
  next
wenzelm@10408
   235
    assume "xs \<parallel> ys"
wenzelm@11987
   236
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   237
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   238
      by blast
wenzelm@10408
   239
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   240
    with neq ys show ?thesis by blast
wenzelm@10389
   241
  qed
wenzelm@10389
   242
qed
wenzelm@10330
   243
nipkow@25564
   244
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
wenzelm@25692
   245
  apply (rule parallelI)
wenzelm@25692
   246
    apply (erule parallelE, erule conjE,
Christian@49083
   247
      induct rule: not_prefixeq_induct, simp+)+
wenzelm@25692
   248
  done
kleing@25299
   249
wenzelm@25692
   250
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
wenzelm@25692
   251
  by (simp add: parallel_append)
kleing@25299
   252
wenzelm@25692
   253
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
wenzelm@25692
   254
  unfolding parallel_def by auto
oheimb@14538
   255
wenzelm@25356
   256
Christian@49083
   257
subsection {* Suffix order on lists *}
wenzelm@17201
   258
wenzelm@19086
   259
definition
Christian@49083
   260
  suffixeq :: "'a list => 'a list => bool" where
Christian@49083
   261
  "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
Christian@49083
   262
Christian@49083
   263
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
Christian@49083
   264
  "suffix xs ys \<equiv> \<exists>us. ys = us @ xs \<and> us \<noteq> []"
oheimb@14538
   265
Christian@49083
   266
lemma suffix_imp_suffixeq:
Christian@49083
   267
  "suffix xs ys \<Longrightarrow> suffixeq xs ys"
Christian@49083
   268
  by (auto simp: suffixeq_def suffix_def)
Christian@49083
   269
Christian@49083
   270
lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"
Christian@49083
   271
  unfolding suffixeq_def by blast
wenzelm@21305
   272
Christian@49083
   273
lemma suffixeqE [elim?]:
Christian@49083
   274
  assumes "suffixeq xs ys"
Christian@49083
   275
  obtains zs where "ys = zs @ xs"
Christian@49083
   276
  using assms unfolding suffixeq_def by blast
wenzelm@21305
   277
Christian@49083
   278
lemma suffixeq_refl [iff]: "suffixeq xs xs"
Christian@49083
   279
  by (auto simp add: suffixeq_def)
Christian@49083
   280
lemma suffix_trans:
Christian@49083
   281
  "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
Christian@49083
   282
  by (auto simp: suffix_def)
Christian@49083
   283
lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
Christian@49083
   284
  by (auto simp add: suffixeq_def)
Christian@49083
   285
lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
Christian@49083
   286
  by (auto simp add: suffixeq_def)
Christian@49083
   287
Christian@49083
   288
lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"
Christian@49083
   289
  by (induct xs) (auto simp: suffixeq_def)
oheimb@14538
   290
Christian@49083
   291
lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"
Christian@49083
   292
  by (induct xs) (auto simp: suffix_def)
oheimb@14538
   293
Christian@49083
   294
lemma Nil_suffixeq [iff]: "suffixeq [] xs"
Christian@49083
   295
  by (simp add: suffixeq_def)
Christian@49083
   296
lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
Christian@49083
   297
  by (auto simp add: suffixeq_def)
Christian@49083
   298
Christian@49083
   299
lemma suffixeq_ConsI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (y#ys)"
Christian@49083
   300
  by (auto simp add: suffixeq_def)
Christian@49083
   301
lemma suffixeq_ConsD: "suffixeq (x#xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49083
   302
  by (auto simp add: suffixeq_def)
oheimb@14538
   303
Christian@49083
   304
lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
Christian@49083
   305
  by (auto simp add: suffixeq_def)
Christian@49083
   306
lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
Christian@49083
   307
  by (auto simp add: suffixeq_def)
Christian@49083
   308
Christian@49083
   309
lemma suffix_set_subset:
Christian@49083
   310
  "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
oheimb@14538
   311
Christian@49083
   312
lemma suffixeq_set_subset:
Christian@49083
   313
  "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
Christian@49083
   314
Christian@49083
   315
lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys"
wenzelm@21305
   316
proof -
Christian@49083
   317
  assume "suffixeq (x#xs) (y#ys)"
Christian@49083
   318
  then obtain zs where "y#ys = zs @ x#xs" ..
Christian@49083
   319
  then show ?thesis
Christian@49083
   320
    by (induct zs) (auto intro!: suffixeq_appendI suffixeq_ConsI)
wenzelm@21305
   321
qed
oheimb@14538
   322
Christian@49083
   323
lemma suffixeq_to_prefixeq [code]: "suffixeq xs ys \<longleftrightarrow> prefixeq (rev xs) (rev ys)"
Christian@49083
   324
proof
Christian@49083
   325
  assume "suffixeq xs ys"
Christian@49083
   326
  then obtain zs where "ys = zs @ xs" ..
Christian@49083
   327
  then have "rev ys = rev xs @ rev zs" by simp
Christian@49083
   328
  then show "prefixeq (rev xs) (rev ys)" ..
Christian@49083
   329
next
Christian@49083
   330
  assume "prefixeq (rev xs) (rev ys)"
Christian@49083
   331
  then obtain zs where "rev ys = rev xs @ zs" ..
Christian@49083
   332
  then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
Christian@49083
   333
  then have "ys = rev zs @ xs" by simp
Christian@49083
   334
  then show "suffixeq xs ys" ..
wenzelm@21305
   335
qed
oheimb@14538
   336
Christian@49083
   337
lemma distinct_suffixeq: "distinct ys \<Longrightarrow> suffixeq xs ys \<Longrightarrow> distinct xs"
Christian@49083
   338
  by (clarsimp elim!: suffixeqE)
wenzelm@17201
   339
Christian@49083
   340
lemma suffixeq_map: "suffixeq xs ys \<Longrightarrow> suffixeq (map f xs) (map f ys)"
Christian@49083
   341
  by (auto elim!: suffixeqE intro: suffixeqI)
kleing@25299
   342
Christian@49083
   343
lemma suffixeq_drop: "suffixeq (drop n as) as"
Christian@49083
   344
  unfolding suffixeq_def
wenzelm@25692
   345
  apply (rule exI [where x = "take n as"])
wenzelm@25692
   346
  apply simp
wenzelm@25692
   347
  done
kleing@25299
   348
Christian@49083
   349
lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
Christian@49083
   350
  by (clarsimp elim!: suffixeqE)
kleing@25299
   351
Christian@49083
   352
lemma suffixeq_suffix_reflclp_conv:
Christian@49083
   353
  "suffixeq = suffix\<^sup>=\<^sup>="
Christian@49083
   354
proof (intro ext iffI)
Christian@49083
   355
  fix xs ys :: "'a list"
Christian@49083
   356
  assume "suffixeq xs ys"
Christian@49083
   357
  show "suffix\<^sup>=\<^sup>= xs ys"
Christian@49083
   358
  proof
Christian@49083
   359
    assume "xs \<noteq> ys"
Christian@49083
   360
    with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def)
Christian@49083
   361
  qed
Christian@49083
   362
next
Christian@49083
   363
  fix xs ys :: "'a list"
Christian@49083
   364
  assume "suffix\<^sup>=\<^sup>= xs ys"
Christian@49083
   365
  thus "suffixeq xs ys"
Christian@49083
   366
  proof
Christian@49083
   367
    assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq)
Christian@49083
   368
  next
Christian@49083
   369
    assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def)
Christian@49083
   370
  qed
Christian@49083
   371
qed
Christian@49083
   372
Christian@49083
   373
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefixeq x y"
wenzelm@25692
   374
  by blast
kleing@25299
   375
Christian@49083
   376
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefixeq y x"
wenzelm@25692
   377
  by blast
wenzelm@25355
   378
wenzelm@25355
   379
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
wenzelm@25692
   380
  unfolding parallel_def by simp
wenzelm@25355
   381
kleing@25299
   382
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
wenzelm@25692
   383
  unfolding parallel_def by simp
kleing@25299
   384
nipkow@25564
   385
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
wenzelm@25692
   386
  by auto
kleing@25299
   387
nipkow@25564
   388
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
Christian@49083
   389
  by (metis Cons_prefixeq_Cons parallelE parallelI)
nipkow@25665
   390
kleing@25299
   391
lemma not_equal_is_parallel:
kleing@25299
   392
  assumes neq: "xs \<noteq> ys"
wenzelm@25356
   393
    and len: "length xs = length ys"
wenzelm@25356
   394
  shows "xs \<parallel> ys"
kleing@25299
   395
  using len neq
wenzelm@25355
   396
proof (induct rule: list_induct2)
haftmann@26445
   397
  case Nil
wenzelm@25356
   398
  then show ?case by simp
kleing@25299
   399
next
haftmann@26445
   400
  case (Cons a as b bs)
wenzelm@25355
   401
  have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
kleing@25299
   402
  show ?case
kleing@25299
   403
  proof (cases "a = b")
wenzelm@25355
   404
    case True
haftmann@26445
   405
    then have "as \<noteq> bs" using Cons by simp
wenzelm@25355
   406
    then show ?thesis by (rule Cons_parallelI2 [OF True ih])
kleing@25299
   407
  next
kleing@25299
   408
    case False
wenzelm@25355
   409
    then show ?thesis by (rule Cons_parallelI1)
kleing@25299
   410
  qed
kleing@25299
   411
qed
haftmann@22178
   412
Christian@49083
   413
lemma suffix_reflclp_conv:
Christian@49083
   414
  "suffix\<^sup>=\<^sup>= = suffixeq"
Christian@49083
   415
  by (intro ext) (auto simp: suffixeq_def suffix_def)
Christian@49083
   416
Christian@49083
   417
lemma suffix_lists:
Christian@49083
   418
  "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
Christian@49083
   419
  unfolding suffix_def by auto
Christian@49083
   420
Christian@49083
   421
Christian@49083
   422
subsection {* Embedding on lists *}
Christian@49083
   423
Christian@49083
   424
inductive
Christian@49083
   425
  emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@49083
   426
  for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
Christian@49083
   427
where
Christian@49083
   428
  emb_Nil [intro, simp]: "emb P [] ys"
Christian@49083
   429
| emb_Cons [intro] : "emb P xs ys \<Longrightarrow> emb P xs (y#ys)"
Christian@49083
   430
| emb_Cons2 [intro]: "P x y \<Longrightarrow> emb P xs ys \<Longrightarrow> emb P (x#xs) (y#ys)"
Christian@49083
   431
Christian@49083
   432
lemma emb_Nil2 [simp]:
Christian@49083
   433
  assumes "emb P xs []" shows "xs = []"
Christian@49083
   434
  using assms by (cases rule: emb.cases) auto
Christian@49083
   435
Christian@49083
   436
lemma emb_append2 [intro]:
Christian@49083
   437
  "emb P xs ys \<Longrightarrow> emb P xs (zs @ ys)"
Christian@49083
   438
  by (induct zs) auto
Christian@49083
   439
Christian@49083
   440
lemma emb_prefix [intro]:
Christian@49083
   441
  assumes "emb P xs ys" shows "emb P xs (ys @ zs)"
Christian@49083
   442
  using assms
Christian@49083
   443
  by (induct arbitrary: zs) auto
Christian@49083
   444
Christian@49083
   445
lemma emb_ConsD:
Christian@49083
   446
  assumes "emb P (x#xs) ys"
Christian@49083
   447
  shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> emb P xs vs"
Christian@49083
   448
using assms
Christian@49083
   449
proof (induct x\<equiv>"x#xs" y\<equiv>"ys" arbitrary: x xs ys)
Christian@49083
   450
  case emb_Cons thus ?case by (metis append_Cons)
Christian@49083
   451
next
Christian@49083
   452
  case (emb_Cons2 x y xs ys)
Christian@49083
   453
  thus ?case by (cases xs) (auto, blast+)
Christian@49083
   454
qed
Christian@49083
   455
Christian@49083
   456
lemma emb_appendD:
Christian@49083
   457
  assumes "emb P (xs @ ys) zs"
Christian@49083
   458
  shows "\<exists>us vs. zs = us @ vs \<and> emb P xs us \<and> emb P ys vs"
Christian@49083
   459
using assms
Christian@49083
   460
proof (induction xs arbitrary: ys zs)
Christian@49083
   461
  case Nil thus ?case by auto
Christian@49083
   462
next
Christian@49083
   463
  case (Cons x xs)
Christian@49083
   464
  then obtain us v vs where "zs = us @ v # vs"
Christian@49083
   465
    and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)
Christian@49083
   466
  with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)
Christian@49083
   467
qed
Christian@49083
   468
Christian@49083
   469
lemma emb_suffix:
Christian@49083
   470
  assumes "emb P xs ys" and "suffix ys zs"
Christian@49083
   471
  shows "emb P xs zs"
Christian@49083
   472
  using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def)
Christian@49083
   473
Christian@49083
   474
lemma emb_suffixeq:
Christian@49083
   475
  assumes "emb P xs ys" and "suffixeq ys zs"
Christian@49083
   476
  shows "emb P xs zs"
Christian@49083
   477
  using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto
Christian@49083
   478
Christian@49083
   479
lemma emb_length: "emb P xs ys \<Longrightarrow> length xs \<le> length ys"
Christian@49083
   480
  by (induct rule: emb.induct) auto
Christian@49083
   481
wenzelm@10330
   482
end