src/HOL/Imperative_HOL/ex/Sublist.thy
author wenzelm
Thu May 24 17:25:53 2012 +0200 (2012-05-24)
changeset 47988 e4b69e10b990
parent 45129 1fce03e3e8ad
child 53374 a14d2a854c02
permissions -rw-r--r--
tuned proofs;
bulwahn@36098
     1
(*  Title:      HOL/Imperative_HOL/ex/Sublist.thy
bulwahn@36098
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@36098
     3
*)
bulwahn@27656
     4
bulwahn@27656
     5
header {* Slices of lists *}
bulwahn@27656
     6
bulwahn@27656
     7
theory Sublist
wenzelm@41413
     8
imports "~~/src/HOL/Library/Multiset"
bulwahn@27656
     9
begin
bulwahn@27656
    10
bulwahn@27656
    11
lemma sublist_split: "i \<le> j \<and> j \<le> k \<Longrightarrow> sublist xs {i..<j} @ sublist xs {j..<k} = sublist xs {i..<k}" 
bulwahn@27656
    12
apply (induct xs arbitrary: i j k)
bulwahn@27656
    13
apply simp
bulwahn@27656
    14
apply (simp only: sublist_Cons)
bulwahn@27656
    15
apply simp
bulwahn@27656
    16
apply safe
bulwahn@27656
    17
apply simp
bulwahn@27656
    18
apply (erule_tac x="0" in meta_allE)
bulwahn@27656
    19
apply (erule_tac x="j - 1" in meta_allE)
bulwahn@27656
    20
apply (erule_tac x="k - 1" in meta_allE)
bulwahn@27656
    21
apply (subgoal_tac "0 \<le> j - 1 \<and> j - 1 \<le> k - 1")
bulwahn@27656
    22
apply simp
bulwahn@27656
    23
apply (subgoal_tac "{ja. Suc ja < j} = {0..<j - Suc 0}")
bulwahn@27656
    24
apply (subgoal_tac "{ja. j \<le> Suc ja \<and> Suc ja < k} = {j - Suc 0..<k - Suc 0}")
bulwahn@27656
    25
apply (subgoal_tac "{j. Suc j < k} = {0..<k - Suc 0}")
bulwahn@27656
    26
apply simp
nipkow@44890
    27
apply fastforce
nipkow@44890
    28
apply fastforce
nipkow@44890
    29
apply fastforce
nipkow@44890
    30
apply fastforce
bulwahn@27656
    31
apply (erule_tac x="i - 1" in meta_allE)
bulwahn@27656
    32
apply (erule_tac x="j - 1" in meta_allE)
bulwahn@27656
    33
apply (erule_tac x="k - 1" in meta_allE)
bulwahn@27656
    34
apply (subgoal_tac " {ja. i \<le> Suc ja \<and> Suc ja < j} = {i - 1 ..<j - 1}")
bulwahn@27656
    35
apply (subgoal_tac " {ja. j \<le> Suc ja \<and> Suc ja < k} = {j - 1..<k - 1}")
bulwahn@27656
    36
apply (subgoal_tac "{j. i \<le> Suc j \<and> Suc j < k} = {i - 1..<k - 1}")
bulwahn@27656
    37
apply (subgoal_tac " i - 1 \<le> j - 1 \<and> j - 1 \<le> k - 1")
bulwahn@27656
    38
apply simp
nipkow@44890
    39
apply fastforce
nipkow@44890
    40
apply fastforce
nipkow@44890
    41
apply fastforce
nipkow@44890
    42
apply fastforce
bulwahn@27656
    43
done
bulwahn@27656
    44
bulwahn@27656
    45
lemma sublist_update1: "i \<notin> inds \<Longrightarrow> sublist (xs[i := v]) inds = sublist xs inds"
bulwahn@27656
    46
apply (induct xs arbitrary: i inds)
bulwahn@27656
    47
apply simp
bulwahn@27656
    48
apply (case_tac i)
bulwahn@27656
    49
apply (simp add: sublist_Cons)
bulwahn@27656
    50
apply (simp add: sublist_Cons)
bulwahn@27656
    51
done
bulwahn@27656
    52
bulwahn@27656
    53
lemma sublist_update2: "i \<in> inds \<Longrightarrow> sublist (xs[i := v]) inds = (sublist xs inds)[(card {k \<in> inds. k < i}):= v]"
bulwahn@27656
    54
proof (induct xs arbitrary: i inds)
bulwahn@27656
    55
  case Nil thus ?case by simp
bulwahn@27656
    56
next
bulwahn@27656
    57
  case (Cons x xs)
bulwahn@27656
    58
  thus ?case
bulwahn@27656
    59
  proof (cases i)
bulwahn@27656
    60
    case 0 with Cons show ?thesis by (simp add: sublist_Cons)
bulwahn@27656
    61
  next
bulwahn@27656
    62
    case (Suc i')
bulwahn@27656
    63
    with Cons show ?thesis
bulwahn@27656
    64
      apply simp
bulwahn@27656
    65
      apply (simp add: sublist_Cons)
bulwahn@27656
    66
      apply auto
bulwahn@27656
    67
      apply (auto simp add: nat.split)
bulwahn@27656
    68
      apply (simp add: card_less_Suc[symmetric])
bulwahn@27656
    69
      apply (simp add: card_less_Suc2)
bulwahn@27656
    70
      done
bulwahn@27656
    71
  qed
bulwahn@27656
    72
qed
bulwahn@27656
    73
bulwahn@27656
    74
lemma sublist_update: "sublist (xs[i := v]) inds = (if i \<in> inds then (sublist xs inds)[(card {k \<in> inds. k < i}) := v] else sublist xs inds)"
bulwahn@27656
    75
by (simp add: sublist_update1 sublist_update2)
bulwahn@27656
    76
bulwahn@27656
    77
lemma sublist_take: "sublist xs {j. j < m} = take m xs"
bulwahn@27656
    78
apply (induct xs arbitrary: m)
bulwahn@27656
    79
apply simp
bulwahn@27656
    80
apply (case_tac m)
bulwahn@27656
    81
apply simp
bulwahn@27656
    82
apply (simp add: sublist_Cons)
bulwahn@27656
    83
done
bulwahn@27656
    84
bulwahn@27656
    85
lemma sublist_take': "sublist xs {0..<m} = take m xs"
bulwahn@27656
    86
apply (induct xs arbitrary: m)
bulwahn@27656
    87
apply simp
bulwahn@27656
    88
apply (case_tac m)
bulwahn@27656
    89
apply simp
bulwahn@27656
    90
apply (simp add: sublist_Cons sublist_take)
bulwahn@27656
    91
done
bulwahn@27656
    92
bulwahn@27656
    93
lemma sublist_all[simp]: "sublist xs {j. j < length xs} = xs"
bulwahn@27656
    94
apply (induct xs)
bulwahn@27656
    95
apply simp
bulwahn@27656
    96
apply (simp add: sublist_Cons)
bulwahn@27656
    97
done
bulwahn@27656
    98
bulwahn@27656
    99
lemma sublist_all'[simp]: "sublist xs {0..<length xs} = xs"
bulwahn@27656
   100
apply (induct xs)
bulwahn@27656
   101
apply simp
bulwahn@27656
   102
apply (simp add: sublist_Cons)
bulwahn@27656
   103
done
bulwahn@27656
   104
bulwahn@27656
   105
lemma sublist_single: "a < length xs \<Longrightarrow> sublist xs {a} = [xs ! a]"
bulwahn@27656
   106
apply (induct xs arbitrary: a)
bulwahn@27656
   107
apply simp
bulwahn@27656
   108
apply(case_tac aa)
bulwahn@27656
   109
apply simp
bulwahn@27656
   110
apply (simp add: sublist_Cons)
bulwahn@27656
   111
apply simp
bulwahn@27656
   112
apply (simp add: sublist_Cons)
bulwahn@27656
   113
done
bulwahn@27656
   114
bulwahn@27656
   115
lemma sublist_is_Nil: "\<forall>i \<in> inds. i \<ge> length xs \<Longrightarrow> sublist xs inds = []" 
bulwahn@27656
   116
apply (induct xs arbitrary: inds)
bulwahn@27656
   117
apply simp
bulwahn@27656
   118
apply (simp add: sublist_Cons)
bulwahn@27656
   119
apply auto
bulwahn@27656
   120
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
bulwahn@27656
   121
apply auto
bulwahn@27656
   122
done
bulwahn@27656
   123
bulwahn@27656
   124
lemma sublist_Nil': "sublist xs inds = [] \<Longrightarrow> \<forall>i \<in> inds. i \<ge> length xs"
bulwahn@27656
   125
apply (induct xs arbitrary: inds)
bulwahn@27656
   126
apply simp
bulwahn@27656
   127
apply (simp add: sublist_Cons)
bulwahn@27656
   128
apply (auto split: if_splits)
bulwahn@27656
   129
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
bulwahn@27656
   130
apply (case_tac x, auto)
bulwahn@27656
   131
done
bulwahn@27656
   132
bulwahn@27656
   133
lemma sublist_Nil[simp]: "(sublist xs inds = []) = (\<forall>i \<in> inds. i \<ge> length xs)"
bulwahn@27656
   134
apply (induct xs arbitrary: inds)
bulwahn@27656
   135
apply simp
bulwahn@27656
   136
apply (simp add: sublist_Cons)
bulwahn@27656
   137
apply auto
bulwahn@27656
   138
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
bulwahn@27656
   139
apply (case_tac x, auto)
bulwahn@27656
   140
done
bulwahn@27656
   141
bulwahn@27656
   142
lemma sublist_eq_subseteq: " \<lbrakk> inds' \<subseteq> inds; sublist xs inds = sublist ys inds \<rbrakk> \<Longrightarrow> sublist xs inds' = sublist ys inds'"
bulwahn@27656
   143
apply (induct xs arbitrary: ys inds inds')
bulwahn@27656
   144
apply simp
bulwahn@27656
   145
apply (drule sym, rule sym)
nipkow@44890
   146
apply (simp add: sublist_Nil, fastforce)
bulwahn@27656
   147
apply (case_tac ys)
nipkow@44890
   148
apply (simp add: sublist_Nil, fastforce)
bulwahn@27656
   149
apply (auto simp add: sublist_Cons)
bulwahn@27656
   150
apply (erule_tac x="list" in meta_allE)
bulwahn@27656
   151
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
bulwahn@27656
   152
apply (erule_tac x="{j. Suc j \<in> inds'}" in meta_allE)
nipkow@44890
   153
apply fastforce
bulwahn@27656
   154
apply (erule_tac x="list" in meta_allE)
bulwahn@27656
   155
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
bulwahn@27656
   156
apply (erule_tac x="{j. Suc j \<in> inds'}" in meta_allE)
nipkow@44890
   157
apply fastforce
bulwahn@27656
   158
done
bulwahn@27656
   159
bulwahn@27656
   160
lemma sublist_eq: "\<lbrakk> \<forall>i \<in> inds. ((i < length xs) \<and> (i < length ys)) \<or> ((i \<ge> length xs ) \<and> (i \<ge> length ys)); \<forall>i \<in> inds. xs ! i = ys ! i \<rbrakk> \<Longrightarrow> sublist xs inds = sublist ys inds"
bulwahn@27656
   161
apply (induct xs arbitrary: ys inds)
bulwahn@27656
   162
apply simp
bulwahn@27656
   163
apply (rule sym, simp add: sublist_Nil)
bulwahn@27656
   164
apply (case_tac ys)
bulwahn@27656
   165
apply (simp add: sublist_Nil)
bulwahn@27656
   166
apply (auto simp add: sublist_Cons)
bulwahn@27656
   167
apply (erule_tac x="list" in meta_allE)
bulwahn@27656
   168
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
nipkow@44890
   169
apply fastforce
bulwahn@27656
   170
apply (erule_tac x="list" in meta_allE)
bulwahn@27656
   171
apply (erule_tac x="{j. Suc j \<in> inds}" in meta_allE)
nipkow@44890
   172
apply fastforce
bulwahn@27656
   173
done
bulwahn@27656
   174
bulwahn@27656
   175
lemma sublist_eq_samelength: "\<lbrakk> length xs = length ys; \<forall>i \<in> inds. xs ! i = ys ! i \<rbrakk> \<Longrightarrow> sublist xs inds = sublist ys inds"
bulwahn@27656
   176
by (rule sublist_eq, auto)
bulwahn@27656
   177
bulwahn@27656
   178
lemma sublist_eq_samelength_iff: "length xs = length ys \<Longrightarrow> (sublist xs inds = sublist ys inds) = (\<forall>i \<in> inds. xs ! i = ys ! i)"
bulwahn@27656
   179
apply (induct xs arbitrary: ys inds)
bulwahn@27656
   180
apply simp
bulwahn@27656
   181
apply (rule sym, simp add: sublist_Nil)
bulwahn@27656
   182
apply (case_tac ys)
bulwahn@27656
   183
apply (simp add: sublist_Nil)
bulwahn@27656
   184
apply (auto simp add: sublist_Cons)
bulwahn@27656
   185
apply (case_tac i)
bulwahn@27656
   186
apply auto
bulwahn@27656
   187
apply (case_tac i)
bulwahn@27656
   188
apply auto
bulwahn@27656
   189
done
bulwahn@27656
   190
bulwahn@27656
   191
section {* Another sublist function *}
bulwahn@27656
   192
bulwahn@27656
   193
function sublist' :: "nat \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
bulwahn@27656
   194
where
bulwahn@27656
   195
  "sublist' n m [] = []"
bulwahn@27656
   196
| "sublist' n 0 xs = []"
bulwahn@27656
   197
| "sublist' 0 (Suc m) (x#xs) = (x#sublist' 0 m xs)"
bulwahn@27656
   198
| "sublist' (Suc n) (Suc m) (x#xs) = sublist' n m xs"
bulwahn@27656
   199
by pat_completeness auto
bulwahn@27656
   200
termination by lexicographic_order
bulwahn@27656
   201
bulwahn@27656
   202
subsection {* Proving equivalence to the other sublist command *}
bulwahn@27656
   203
bulwahn@27656
   204
lemma sublist'_sublist: "sublist' n m xs = sublist xs {j. n \<le> j \<and> j < m}"
bulwahn@27656
   205
apply (induct xs arbitrary: n m)
bulwahn@27656
   206
apply simp
bulwahn@27656
   207
apply (case_tac n)
bulwahn@27656
   208
apply (case_tac m)
bulwahn@27656
   209
apply simp
bulwahn@27656
   210
apply (simp add: sublist_Cons)
bulwahn@27656
   211
apply (case_tac m)
bulwahn@27656
   212
apply simp
bulwahn@27656
   213
apply (simp add: sublist_Cons)
bulwahn@27656
   214
done
bulwahn@27656
   215
bulwahn@27656
   216
bulwahn@27656
   217
lemma "sublist' n m xs = sublist xs {n..<m}"
bulwahn@27656
   218
apply (induct xs arbitrary: n m)
bulwahn@27656
   219
apply simp
bulwahn@27656
   220
apply (case_tac n, case_tac m)
bulwahn@27656
   221
apply simp
bulwahn@27656
   222
apply simp
bulwahn@27656
   223
apply (simp add: sublist_take')
bulwahn@27656
   224
apply (case_tac m)
bulwahn@27656
   225
apply simp
bulwahn@27656
   226
apply (simp add: sublist_Cons sublist'_sublist)
bulwahn@27656
   227
done
bulwahn@27656
   228
bulwahn@27656
   229
bulwahn@27656
   230
subsection {* Showing equivalence to use of drop and take for definition *}
bulwahn@27656
   231
bulwahn@27656
   232
lemma "sublist' n m xs = take (m - n) (drop n xs)"
bulwahn@27656
   233
apply (induct xs arbitrary: n m)
bulwahn@27656
   234
apply simp
bulwahn@27656
   235
apply (case_tac m)
bulwahn@27656
   236
apply simp
bulwahn@27656
   237
apply (case_tac n)
bulwahn@27656
   238
apply simp
bulwahn@27656
   239
apply simp
bulwahn@27656
   240
done
bulwahn@27656
   241
bulwahn@27656
   242
subsection {* General lemma about sublist *}
bulwahn@27656
   243
bulwahn@27656
   244
lemma sublist'_Nil[simp]: "sublist' i j [] = []"
bulwahn@27656
   245
by simp
bulwahn@27656
   246
bulwahn@27656
   247
lemma sublist'_Cons[simp]: "sublist' i (Suc j) (x#xs) = (case i of 0 \<Rightarrow> (x # sublist' 0 j xs) | Suc i' \<Rightarrow>  sublist' i' j xs)"
bulwahn@27656
   248
by (cases i) auto
bulwahn@27656
   249
bulwahn@27656
   250
lemma sublist'_Cons2[simp]: "sublist' i j (x#xs) = (if (j = 0) then [] else ((if (i = 0) then [x] else []) @ sublist' (i - 1) (j - 1) xs))"
bulwahn@27656
   251
apply (cases j)
bulwahn@27656
   252
apply auto
bulwahn@27656
   253
apply (cases i)
bulwahn@27656
   254
apply auto
bulwahn@27656
   255
done
bulwahn@27656
   256
bulwahn@27656
   257
lemma sublist_n_0: "sublist' n 0 xs = []"
bulwahn@27656
   258
by (induct xs, auto)
bulwahn@27656
   259
bulwahn@27656
   260
lemma sublist'_Nil': "n \<ge> m \<Longrightarrow> sublist' n m xs = []"
bulwahn@27656
   261
apply (induct xs arbitrary: n m)
bulwahn@27656
   262
apply simp
bulwahn@27656
   263
apply (case_tac m)
bulwahn@27656
   264
apply simp
bulwahn@27656
   265
apply (case_tac n)
bulwahn@27656
   266
apply simp
bulwahn@27656
   267
apply simp
bulwahn@27656
   268
done
bulwahn@27656
   269
bulwahn@27656
   270
lemma sublist'_Nil2: "n \<ge> length xs \<Longrightarrow> sublist' n m xs = []"
bulwahn@27656
   271
apply (induct xs arbitrary: n m)
bulwahn@27656
   272
apply simp
bulwahn@27656
   273
apply (case_tac m)
bulwahn@27656
   274
apply simp
bulwahn@27656
   275
apply (case_tac n)
bulwahn@27656
   276
apply simp
bulwahn@27656
   277
apply simp
bulwahn@27656
   278
done
bulwahn@27656
   279
bulwahn@27656
   280
lemma sublist'_Nil3: "(sublist' n m xs = []) = ((n \<ge> m) \<or> (n \<ge> length xs))"
bulwahn@27656
   281
apply (induct xs arbitrary: n m)
bulwahn@27656
   282
apply simp
bulwahn@27656
   283
apply (case_tac m)
bulwahn@27656
   284
apply simp
bulwahn@27656
   285
apply (case_tac n)
bulwahn@27656
   286
apply simp
bulwahn@27656
   287
apply simp
bulwahn@27656
   288
done
bulwahn@27656
   289
bulwahn@27656
   290
lemma sublist'_notNil: "\<lbrakk> n < length xs; n < m \<rbrakk> \<Longrightarrow> sublist' n m xs \<noteq> []"
bulwahn@27656
   291
apply (induct xs arbitrary: n m)
bulwahn@27656
   292
apply simp
bulwahn@27656
   293
apply (case_tac m)
bulwahn@27656
   294
apply simp
bulwahn@27656
   295
apply (case_tac n)
bulwahn@27656
   296
apply simp
bulwahn@27656
   297
apply simp
bulwahn@27656
   298
done
bulwahn@27656
   299
bulwahn@27656
   300
lemma sublist'_single: "n < length xs \<Longrightarrow> sublist' n (Suc n) xs = [xs ! n]"
bulwahn@27656
   301
apply (induct xs arbitrary: n)
bulwahn@27656
   302
apply simp
bulwahn@27656
   303
apply simp
bulwahn@27656
   304
apply (case_tac n)
bulwahn@27656
   305
apply (simp add: sublist_n_0)
bulwahn@27656
   306
apply simp
bulwahn@27656
   307
done
bulwahn@27656
   308
bulwahn@27656
   309
lemma sublist'_update1: "i \<ge> m \<Longrightarrow> sublist' n m (xs[i:=v]) = sublist' n m xs"
bulwahn@27656
   310
apply (induct xs arbitrary: n m i)
bulwahn@27656
   311
apply simp
bulwahn@27656
   312
apply simp
bulwahn@27656
   313
apply (case_tac i)
bulwahn@27656
   314
apply simp
bulwahn@27656
   315
apply simp
bulwahn@27656
   316
done
bulwahn@27656
   317
bulwahn@27656
   318
lemma sublist'_update2: "i < n \<Longrightarrow> sublist' n m (xs[i:=v]) = sublist' n m xs"
bulwahn@27656
   319
apply (induct xs arbitrary: n m i)
bulwahn@27656
   320
apply simp
bulwahn@27656
   321
apply simp
bulwahn@27656
   322
apply (case_tac i)
bulwahn@27656
   323
apply simp
bulwahn@27656
   324
apply simp
bulwahn@27656
   325
done
bulwahn@27656
   326
bulwahn@27656
   327
lemma sublist'_update3: "\<lbrakk>n \<le> i; i < m\<rbrakk> \<Longrightarrow> sublist' n m (xs[i := v]) = (sublist' n m xs)[i - n := v]"
bulwahn@27656
   328
proof (induct xs arbitrary: n m i)
bulwahn@27656
   329
  case Nil thus ?case by auto
bulwahn@27656
   330
next
bulwahn@27656
   331
  case (Cons x xs)
bulwahn@27656
   332
  thus ?case
bulwahn@27656
   333
    apply -
bulwahn@27656
   334
    apply auto
bulwahn@27656
   335
    apply (cases i)
bulwahn@27656
   336
    apply auto
bulwahn@27656
   337
    apply (cases i)
bulwahn@27656
   338
    apply auto
bulwahn@27656
   339
    done
bulwahn@27656
   340
qed
bulwahn@27656
   341
bulwahn@27656
   342
lemma "\<lbrakk> sublist' i j xs = sublist' i j ys; n \<ge> i; m \<le> j \<rbrakk> \<Longrightarrow> sublist' n m xs = sublist' n m ys"
bulwahn@27656
   343
proof (induct xs arbitrary: i j ys n m)
bulwahn@27656
   344
  case Nil
bulwahn@27656
   345
  thus ?case
bulwahn@27656
   346
    apply -
bulwahn@27656
   347
    apply (rule sym, drule sym)
bulwahn@27656
   348
    apply (simp add: sublist'_Nil)
bulwahn@27656
   349
    apply (simp add: sublist'_Nil3)
bulwahn@27656
   350
    apply arith
bulwahn@27656
   351
    done
bulwahn@27656
   352
next
bulwahn@27656
   353
  case (Cons x xs i j ys n m)
bulwahn@27656
   354
  note c = this
bulwahn@27656
   355
  thus ?case
bulwahn@27656
   356
  proof (cases m)
bulwahn@27656
   357
    case 0 thus ?thesis by (simp add: sublist_n_0)
bulwahn@27656
   358
  next
bulwahn@27656
   359
    case (Suc m')
bulwahn@27656
   360
    note a = this
bulwahn@27656
   361
    thus ?thesis
bulwahn@27656
   362
    proof (cases n)
bulwahn@27656
   363
      case 0 note b = this
bulwahn@27656
   364
      show ?thesis
bulwahn@27656
   365
      proof (cases ys)
wenzelm@32960
   366
        case Nil  with a b Cons.prems show ?thesis by (simp add: sublist'_Nil3)
bulwahn@27656
   367
      next
wenzelm@32960
   368
        case (Cons y ys)
wenzelm@32960
   369
        show ?thesis
wenzelm@32960
   370
        proof (cases j)
wenzelm@32960
   371
          case 0 with a b Cons.prems show ?thesis by simp
wenzelm@32960
   372
        next
wenzelm@32960
   373
          case (Suc j') with a b Cons.prems Cons show ?thesis 
wenzelm@32960
   374
            apply -
wenzelm@32960
   375
            apply (simp, rule Cons.hyps [of "0" "j'" "ys" "0" "m'"], auto)
wenzelm@32960
   376
            done
wenzelm@32960
   377
        qed
bulwahn@27656
   378
      qed
bulwahn@27656
   379
    next
bulwahn@27656
   380
      case (Suc n')
bulwahn@27656
   381
      show ?thesis
bulwahn@27656
   382
      proof (cases ys)
wenzelm@32960
   383
        case Nil with Suc a Cons.prems show ?thesis by (auto simp add: sublist'_Nil3)
bulwahn@27656
   384
      next
wenzelm@32960
   385
        case (Cons y ys) with Suc a Cons.prems show ?thesis
wenzelm@32960
   386
          apply -
wenzelm@32960
   387
          apply simp
wenzelm@32960
   388
          apply (cases j)
wenzelm@32960
   389
          apply simp
wenzelm@32960
   390
          apply (cases i)
wenzelm@32960
   391
          apply simp
wenzelm@32960
   392
          apply (rule_tac j="nat" in Cons.hyps [of "0" _ "ys" "n'" "m'"])
wenzelm@32960
   393
          apply simp
wenzelm@32960
   394
          apply simp
wenzelm@32960
   395
          apply simp
wenzelm@32960
   396
          apply simp
wenzelm@32960
   397
          apply (rule_tac i="nata" and j="nat" in Cons.hyps [of _ _ "ys" "n'" "m'"])
wenzelm@32960
   398
          apply simp
wenzelm@32960
   399
          apply simp
wenzelm@32960
   400
          apply simp
wenzelm@32960
   401
          done
bulwahn@27656
   402
      qed
bulwahn@27656
   403
    qed
bulwahn@27656
   404
  qed
bulwahn@27656
   405
qed
bulwahn@27656
   406
bulwahn@27656
   407
lemma length_sublist': "j \<le> length xs \<Longrightarrow> length (sublist' i j xs) = j - i"
bulwahn@27656
   408
by (induct xs arbitrary: i j, auto)
bulwahn@27656
   409
bulwahn@27656
   410
lemma sublist'_front: "\<lbrakk> i < j; i < length xs \<rbrakk> \<Longrightarrow> sublist' i j xs = xs ! i # sublist' (Suc i) j xs"
wenzelm@45129
   411
apply (induct xs arbitrary: i j)
bulwahn@27656
   412
apply simp
bulwahn@27656
   413
apply (case_tac j)
bulwahn@27656
   414
apply simp
bulwahn@27656
   415
apply (case_tac i)
bulwahn@27656
   416
apply simp
bulwahn@27656
   417
apply simp
bulwahn@27656
   418
done
bulwahn@27656
   419
bulwahn@27656
   420
lemma sublist'_back: "\<lbrakk> i < j; j \<le> length xs \<rbrakk> \<Longrightarrow> sublist' i j xs = sublist' i (j - 1) xs @ [xs ! (j - 1)]"
wenzelm@45129
   421
apply (induct xs arbitrary: i j)
bulwahn@27656
   422
apply simp
bulwahn@27656
   423
apply simp
bulwahn@27656
   424
done
bulwahn@27656
   425
bulwahn@27656
   426
(* suffices that j \<le> length xs and length ys *) 
bulwahn@27656
   427
lemma sublist'_eq_samelength_iff: "length xs = length ys \<Longrightarrow> (sublist' i j xs  = sublist' i j ys) = (\<forall>i'. i \<le> i' \<and> i' < j \<longrightarrow> xs ! i' = ys ! i')"
bulwahn@27656
   428
proof (induct xs arbitrary: ys i j)
bulwahn@27656
   429
  case Nil thus ?case by simp
bulwahn@27656
   430
next
bulwahn@27656
   431
  case (Cons x xs)
bulwahn@27656
   432
  thus ?case
bulwahn@27656
   433
    apply -
bulwahn@27656
   434
    apply (cases ys)
bulwahn@27656
   435
    apply simp
bulwahn@27656
   436
    apply simp
bulwahn@27656
   437
    apply auto
bulwahn@27656
   438
    apply (case_tac i', auto)
bulwahn@27656
   439
    apply (erule_tac x="Suc i'" in allE, auto)
bulwahn@27656
   440
    apply (erule_tac x="i' - 1" in allE, auto)
bulwahn@27656
   441
    apply (erule_tac x="Suc i'" in allE, auto)
bulwahn@27656
   442
    done
bulwahn@27656
   443
qed
bulwahn@27656
   444
bulwahn@27656
   445
lemma sublist'_all[simp]: "sublist' 0 (length xs) xs = xs"
bulwahn@27656
   446
by (induct xs, auto)
bulwahn@27656
   447
bulwahn@27656
   448
lemma sublist'_sublist': "sublist' n m (sublist' i j xs) = sublist' (i + n) (min (i + m) j) xs" 
bulwahn@27656
   449
by (induct xs arbitrary: i j n m) (auto simp add: min_diff)
bulwahn@27656
   450
bulwahn@27656
   451
lemma sublist'_append: "\<lbrakk> i \<le> j; j \<le> k \<rbrakk> \<Longrightarrow>(sublist' i j xs) @ (sublist' j k xs) = sublist' i k xs"
bulwahn@27656
   452
by (induct xs arbitrary: i j k) auto
bulwahn@27656
   453
bulwahn@27656
   454
lemma nth_sublist': "\<lbrakk> k < j - i; j \<le> length xs \<rbrakk> \<Longrightarrow> (sublist' i j xs) ! k = xs ! (i + k)"
bulwahn@27656
   455
apply (induct xs arbitrary: i j k)
nipkow@41842
   456
apply simp
bulwahn@27656
   457
apply (case_tac k)
bulwahn@27656
   458
apply auto
bulwahn@27656
   459
done
bulwahn@27656
   460
bulwahn@27656
   461
lemma set_sublist': "set (sublist' i j xs) = {x. \<exists>k. i \<le> k \<and> k < j \<and> k < List.length xs \<and> x = xs ! k}"
bulwahn@27656
   462
apply (simp add: sublist'_sublist)
bulwahn@27656
   463
apply (simp add: set_sublist)
bulwahn@27656
   464
apply auto
bulwahn@27656
   465
done
bulwahn@27656
   466
bulwahn@27656
   467
lemma all_in_set_sublist'_conv: "(\<forall>j. j \<in> set (sublist' l r xs) \<longrightarrow> P j) = (\<forall>k. l \<le> k \<and> k < r \<and> k < List.length xs \<longrightarrow> P (xs ! k))"
bulwahn@27656
   468
unfolding set_sublist' by blast
bulwahn@27656
   469
bulwahn@27656
   470
lemma ball_in_set_sublist'_conv: "(\<forall>j \<in> set (sublist' l r xs). P j) = (\<forall>k. l \<le> k \<and> k < r \<and> k < List.length xs \<longrightarrow> P (xs ! k))"
bulwahn@27656
   471
unfolding set_sublist' by blast
bulwahn@27656
   472
bulwahn@27656
   473
bulwahn@27656
   474
lemma multiset_of_sublist:
bulwahn@27656
   475
assumes l_r: "l \<le> r \<and> r \<le> List.length xs"
bulwahn@27656
   476
assumes left: "\<forall> i. i < l \<longrightarrow> (xs::'a list) ! i = ys ! i"
bulwahn@27656
   477
assumes right: "\<forall> i. i \<ge> r \<longrightarrow> (xs::'a list) ! i = ys ! i"
bulwahn@27656
   478
assumes multiset: "multiset_of xs = multiset_of ys"
bulwahn@27656
   479
  shows "multiset_of (sublist' l r xs) = multiset_of (sublist' l r ys)"
bulwahn@27656
   480
proof -
bulwahn@27656
   481
  from l_r have xs_def: "xs = (sublist' 0 l xs) @ (sublist' l r xs) @ (sublist' r (List.length xs) xs)" (is "_ = ?xs_long") 
bulwahn@27656
   482
    by (simp add: sublist'_append)
bulwahn@27656
   483
  from multiset have length_eq: "List.length xs = List.length ys" by (rule multiset_of_eq_length)
bulwahn@27656
   484
  with l_r have ys_def: "ys = (sublist' 0 l ys) @ (sublist' l r ys) @ (sublist' r (List.length ys) ys)" (is "_ = ?ys_long") 
bulwahn@27656
   485
    by (simp add: sublist'_append)
bulwahn@27656
   486
  from xs_def ys_def multiset have "multiset_of ?xs_long = multiset_of ?ys_long" by simp
bulwahn@27656
   487
  moreover
bulwahn@27656
   488
  from left l_r length_eq have "sublist' 0 l xs = sublist' 0 l ys"
bulwahn@27656
   489
    by (auto simp add: length_sublist' nth_sublist' intro!: nth_equalityI)
bulwahn@27656
   490
  moreover
bulwahn@27656
   491
  from right l_r length_eq have "sublist' r (List.length xs) xs = sublist' r (List.length ys) ys"
bulwahn@27656
   492
    by (auto simp add: length_sublist' nth_sublist' intro!: nth_equalityI)
bulwahn@27656
   493
  moreover
bulwahn@27656
   494
  ultimately show ?thesis by (simp add: multiset_of_append)
bulwahn@27656
   495
qed
bulwahn@27656
   496
bulwahn@27656
   497
bulwahn@27656
   498
end