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