src/HOL/Library/List_Prefix.thy
author chaieb
Mon Jun 11 11:06:04 2007 +0200 (2007-06-11)
changeset 23315 df3a7e9ebadb
parent 23254 99644a53f16d
child 23394 474ff28210c0
permissions -rw-r--r--
tuned Proof
wenzelm@10330
     1
(*  Title:      HOL/Library/List_Prefix.thy
wenzelm@10330
     2
    ID:         $Id$
wenzelm@10330
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@10330
     4
*)
wenzelm@10330
     5
wenzelm@14706
     6
header {* List prefixes and postfixes *}
wenzelm@10330
     7
nipkow@15131
     8
theory List_Prefix
nipkow@15140
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10330
    11
wenzelm@10330
    12
subsection {* Prefix order on lists *}
wenzelm@10330
    13
wenzelm@12338
    14
instance list :: (type) ord ..
wenzelm@10330
    15
wenzelm@10330
    16
defs (overloaded)
wenzelm@10389
    17
  prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
wenzelm@10389
    18
  strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
wenzelm@10330
    19
wenzelm@12338
    20
instance list :: (type) order
wenzelm@10389
    21
  by intro_classes (auto simp add: prefix_def strict_prefix_def)
wenzelm@10330
    22
wenzelm@10389
    23
lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
wenzelm@18730
    24
  unfolding prefix_def by blast
wenzelm@10330
    25
wenzelm@21305
    26
lemma prefixE [elim?]:
wenzelm@21305
    27
  assumes "xs \<le> ys"
wenzelm@21305
    28
  obtains zs where "ys = xs @ zs"
wenzelm@21305
    29
  using prems unfolding prefix_def by blast
wenzelm@10330
    30
wenzelm@10870
    31
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
wenzelm@18730
    32
  unfolding strict_prefix_def prefix_def by blast
wenzelm@10870
    33
wenzelm@10870
    34
lemma strict_prefixE' [elim?]:
wenzelm@21305
    35
  assumes "xs < ys"
wenzelm@21305
    36
  obtains z zs where "ys = xs @ z # zs"
wenzelm@10870
    37
proof -
wenzelm@21305
    38
  from `xs < ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"
wenzelm@18730
    39
    unfolding strict_prefix_def prefix_def by blast
wenzelm@21305
    40
  with that show ?thesis by (auto simp add: neq_Nil_conv)
wenzelm@10870
    41
qed
wenzelm@10870
    42
wenzelm@10389
    43
lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
wenzelm@18730
    44
  unfolding strict_prefix_def by blast
wenzelm@10330
    45
wenzelm@10389
    46
lemma strict_prefixE [elim?]:
wenzelm@21305
    47
  fixes xs ys :: "'a list"
wenzelm@21305
    48
  assumes "xs < ys"
wenzelm@21305
    49
  obtains "xs \<le> ys" and "xs \<noteq> ys"
wenzelm@21305
    50
  using prems unfolding strict_prefix_def by blast
wenzelm@10330
    51
wenzelm@10330
    52
wenzelm@10389
    53
subsection {* Basic properties of prefixes *}
wenzelm@10330
    54
wenzelm@10330
    55
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    56
  by (simp add: prefix_def)
wenzelm@10330
    57
wenzelm@10330
    58
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    59
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    60
wenzelm@10330
    61
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    62
proof
wenzelm@10389
    63
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    64
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    65
  show "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    66
  proof (cases zs rule: rev_cases)
wenzelm@10389
    67
    assume "zs = []"
wenzelm@10389
    68
    with zs have "xs = ys @ [y]" by simp
wenzelm@23254
    69
    then show ?thesis ..
wenzelm@10389
    70
  next
wenzelm@10389
    71
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    72
    with zs have "ys = xs @ zs'" by simp
wenzelm@23254
    73
    then have "xs \<le> ys" ..
wenzelm@23254
    74
    then show ?thesis ..
wenzelm@10389
    75
  qed
wenzelm@10389
    76
next
wenzelm@10389
    77
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@23254
    78
  then show "xs \<le> ys @ [y]"
wenzelm@10389
    79
  proof
wenzelm@10389
    80
    assume "xs = ys @ [y]"
wenzelm@23254
    81
    then show ?thesis by simp
wenzelm@10389
    82
  next
wenzelm@10389
    83
    assume "xs \<le> ys"
wenzelm@10389
    84
    then obtain zs where "ys = xs @ zs" ..
wenzelm@23254
    85
    then have "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@23254
    86
    then show ?thesis ..
wenzelm@10389
    87
  qed
wenzelm@10389
    88
qed
wenzelm@10330
    89
wenzelm@10330
    90
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    91
  by (auto simp add: prefix_def)
wenzelm@10330
    92
wenzelm@10330
    93
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
    94
  by (induct xs) simp_all
wenzelm@10330
    95
wenzelm@10389
    96
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
wenzelm@10389
    97
proof -
wenzelm@10389
    98
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@23254
    99
  then show ?thesis by simp
wenzelm@10389
   100
qed
wenzelm@10330
   101
wenzelm@10330
   102
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
wenzelm@10389
   103
proof -
wenzelm@10389
   104
  assume "xs \<le> ys"
wenzelm@10389
   105
  then obtain us where "ys = xs @ us" ..
wenzelm@23254
   106
  then have "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@23254
   107
  then show ?thesis ..
wenzelm@10389
   108
qed
wenzelm@10330
   109
nipkow@14300
   110
lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
wenzelm@17201
   111
  by (auto simp add: prefix_def)
nipkow@14300
   112
wenzelm@10330
   113
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
   114
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
   115
wenzelm@10330
   116
theorem prefix_append:
wenzelm@10330
   117
    "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   118
  apply (induct zs rule: rev_induct)
wenzelm@10330
   119
   apply force
wenzelm@10330
   120
  apply (simp del: append_assoc add: append_assoc [symmetric])
wenzelm@10330
   121
  apply simp
wenzelm@10330
   122
  apply blast
wenzelm@10330
   123
  done
wenzelm@10330
   124
wenzelm@10330
   125
lemma append_one_prefix:
wenzelm@10330
   126
    "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
wenzelm@10330
   127
  apply (unfold prefix_def)
wenzelm@10330
   128
  apply (auto simp add: nth_append)
wenzelm@10389
   129
  apply (case_tac zs)
wenzelm@10330
   130
   apply auto
wenzelm@10330
   131
  done
wenzelm@10330
   132
wenzelm@10330
   133
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   134
  by (auto simp add: prefix_def)
wenzelm@10330
   135
nipkow@14300
   136
lemma prefix_same_cases:
wenzelm@17201
   137
    "(xs\<^isub>1::'a list) \<le> ys \<Longrightarrow> xs\<^isub>2 \<le> ys \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1"
wenzelm@17201
   138
  apply (simp add: prefix_def)
wenzelm@17201
   139
  apply (erule exE)+
wenzelm@17201
   140
  apply (simp add: append_eq_append_conv_if split: if_splits)
wenzelm@17201
   141
   apply (rule disjI2)
wenzelm@17201
   142
   apply (rule_tac x = "drop (size xs\<^isub>2) xs\<^isub>1" in exI)
wenzelm@17201
   143
   apply clarify
wenzelm@17201
   144
   apply (drule sym)
wenzelm@17201
   145
   apply (insert append_take_drop_id [of "length xs\<^isub>2" xs\<^isub>1])
wenzelm@17201
   146
   apply simp
wenzelm@17201
   147
  apply (rule disjI1)
wenzelm@17201
   148
  apply (rule_tac x = "drop (size xs\<^isub>1) xs\<^isub>2" in exI)
wenzelm@17201
   149
  apply clarify
wenzelm@17201
   150
  apply (insert append_take_drop_id [of "length xs\<^isub>1" xs\<^isub>2])
wenzelm@17201
   151
  apply simp
wenzelm@17201
   152
  done
nipkow@14300
   153
nipkow@14300
   154
lemma set_mono_prefix:
wenzelm@17201
   155
    "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
wenzelm@17201
   156
  by (auto simp add: prefix_def)
nipkow@14300
   157
nipkow@14300
   158
wenzelm@10389
   159
subsection {* Parallel lists *}
wenzelm@10389
   160
wenzelm@19086
   161
definition
wenzelm@21404
   162
  parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
wenzelm@19086
   163
  "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)"
wenzelm@10389
   164
wenzelm@10389
   165
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
wenzelm@18730
   166
  unfolding parallel_def by blast
wenzelm@10330
   167
wenzelm@10389
   168
lemma parallelE [elim]:
wenzelm@21305
   169
  assumes "xs \<parallel> ys"
wenzelm@21305
   170
  obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@21305
   171
  using prems unfolding parallel_def by blast
wenzelm@10330
   172
wenzelm@10389
   173
theorem prefix_cases:
wenzelm@21305
   174
  obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys"
wenzelm@18730
   175
  unfolding parallel_def strict_prefix_def by blast
wenzelm@10330
   176
wenzelm@10389
   177
theorem parallel_decomp:
wenzelm@10389
   178
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   179
proof (induct xs rule: rev_induct)
wenzelm@11987
   180
  case Nil
wenzelm@23254
   181
  then have False by auto
wenzelm@23254
   182
  then show ?case ..
wenzelm@10408
   183
next
wenzelm@11987
   184
  case (snoc x xs)
wenzelm@11987
   185
  show ?case
wenzelm@10408
   186
  proof (rule prefix_cases)
wenzelm@10408
   187
    assume le: "xs \<le> ys"
wenzelm@10408
   188
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   189
    show ?thesis
wenzelm@10408
   190
    proof (cases ys')
wenzelm@10408
   191
      assume "ys' = []" with ys have "xs = ys" by simp
wenzelm@11987
   192
      with snoc have "[x] \<parallel> []" by auto
wenzelm@23254
   193
      then have False by blast
wenzelm@23254
   194
      then show ?thesis ..
wenzelm@10389
   195
    next
wenzelm@10408
   196
      fix c cs assume ys': "ys' = c # cs"
wenzelm@11987
   197
      with snoc ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@23254
   198
      then have "x \<noteq> c" by auto
wenzelm@10408
   199
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   200
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   201
      ultimately show ?thesis by blast
wenzelm@10389
   202
    qed
wenzelm@10408
   203
  next
wenzelm@23254
   204
    assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
wenzelm@11987
   205
    with snoc have False by blast
wenzelm@23254
   206
    then show ?thesis ..
wenzelm@10408
   207
  next
wenzelm@10408
   208
    assume "xs \<parallel> ys"
wenzelm@11987
   209
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   210
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   211
      by blast
wenzelm@10408
   212
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   213
    with neq ys show ?thesis by blast
wenzelm@10389
   214
  qed
wenzelm@10389
   215
qed
wenzelm@10330
   216
oheimb@14538
   217
oheimb@14538
   218
subsection {* Postfix order on lists *}
wenzelm@17201
   219
wenzelm@19086
   220
definition
wenzelm@21404
   221
  postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
wenzelm@19086
   222
  "(xs >>= ys) = (\<exists>zs. xs = zs @ ys)"
oheimb@14538
   223
wenzelm@21305
   224
lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys"
wenzelm@21305
   225
  unfolding postfix_def by blast
wenzelm@21305
   226
wenzelm@21305
   227
lemma postfixE [elim?]:
wenzelm@21305
   228
  assumes "xs >>= ys"
wenzelm@21305
   229
  obtains zs where "xs = zs @ ys"
wenzelm@21305
   230
  using prems unfolding postfix_def by blast
wenzelm@21305
   231
wenzelm@21305
   232
lemma postfix_refl [iff]: "xs >>= xs"
wenzelm@14706
   233
  by (auto simp add: postfix_def)
wenzelm@17201
   234
lemma postfix_trans: "\<lbrakk>xs >>= ys; ys >>= zs\<rbrakk> \<Longrightarrow> xs >>= zs"
wenzelm@14706
   235
  by (auto simp add: postfix_def)
wenzelm@17201
   236
lemma postfix_antisym: "\<lbrakk>xs >>= ys; ys >>= xs\<rbrakk> \<Longrightarrow> xs = ys"
wenzelm@14706
   237
  by (auto simp add: postfix_def)
oheimb@14538
   238
wenzelm@17201
   239
lemma Nil_postfix [iff]: "xs >>= []"
wenzelm@14706
   240
  by (simp add: postfix_def)
wenzelm@17201
   241
lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])"
wenzelm@21305
   242
  by (auto simp add: postfix_def)
oheimb@14538
   243
wenzelm@17201
   244
lemma postfix_ConsI: "xs >>= ys \<Longrightarrow> x#xs >>= ys"
wenzelm@14706
   245
  by (auto simp add: postfix_def)
wenzelm@17201
   246
lemma postfix_ConsD: "xs >>= y#ys \<Longrightarrow> xs >>= ys"
wenzelm@14706
   247
  by (auto simp add: postfix_def)
oheimb@14538
   248
wenzelm@17201
   249
lemma postfix_appendI: "xs >>= ys \<Longrightarrow> zs @ xs >>= ys"
wenzelm@14706
   250
  by (auto simp add: postfix_def)
wenzelm@17201
   251
lemma postfix_appendD: "xs >>= zs @ ys \<Longrightarrow> xs >>= ys"
wenzelm@21305
   252
  by (auto simp add: postfix_def)
oheimb@14538
   253
wenzelm@21305
   254
lemma postfix_is_subset: "xs >>= ys ==> set ys \<subseteq> set xs"
wenzelm@21305
   255
proof -
wenzelm@21305
   256
  assume "xs >>= ys"
wenzelm@21305
   257
  then obtain zs where "xs = zs @ ys" ..
wenzelm@21305
   258
  then show ?thesis by (induct zs) auto
wenzelm@21305
   259
qed
oheimb@14538
   260
wenzelm@21305
   261
lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys"
wenzelm@21305
   262
proof -
wenzelm@21305
   263
  assume "x#xs >>= y#ys"
wenzelm@21305
   264
  then obtain zs where "x#xs = zs @ y#ys" ..
wenzelm@21305
   265
  then show ?thesis
wenzelm@21305
   266
    by (induct zs) (auto intro!: postfix_appendI postfix_ConsI)
wenzelm@21305
   267
qed
oheimb@14538
   268
wenzelm@21305
   269
lemma postfix_to_prefix: "xs >>= ys \<longleftrightarrow> rev ys \<le> rev xs"
wenzelm@21305
   270
proof
wenzelm@21305
   271
  assume "xs >>= ys"
wenzelm@21305
   272
  then obtain zs where "xs = zs @ ys" ..
wenzelm@21305
   273
  then have "rev xs = rev ys @ rev zs" by simp
wenzelm@21305
   274
  then show "rev ys <= rev xs" ..
wenzelm@21305
   275
next
wenzelm@21305
   276
  assume "rev ys <= rev xs"
wenzelm@21305
   277
  then obtain zs where "rev xs = rev ys @ zs" ..
wenzelm@21305
   278
  then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp
wenzelm@21305
   279
  then have "xs = rev zs @ ys" by simp
wenzelm@21305
   280
  then show "xs >>= ys" ..
wenzelm@21305
   281
qed
wenzelm@17201
   282
haftmann@22178
   283
haftmann@22178
   284
subsection {* Exeuctable code *}
haftmann@22178
   285
haftmann@22178
   286
lemma less_eq_code [code func]:
haftmann@22178
   287
  "([]\<Colon>'a\<Colon>{eq, ord} list) \<le> xs \<longleftrightarrow> True"
haftmann@22178
   288
  "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> [] \<longleftrightarrow> False"
haftmann@22178
   289
  "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"
haftmann@22178
   290
  by simp_all
haftmann@22178
   291
haftmann@22178
   292
lemma less_code [code func]:
haftmann@22178
   293
  "xs < ([]\<Colon>'a\<Colon>{eq, ord} list) \<longleftrightarrow> False"
haftmann@22178
   294
  "[] < (x\<Colon>'a\<Colon>{eq, ord})# xs \<longleftrightarrow> True"
haftmann@22178
   295
  "(x\<Colon>'a\<Colon>{eq, ord}) # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"
haftmann@22178
   296
  unfolding strict_prefix_def by auto
haftmann@22178
   297
haftmann@22178
   298
lemmas [code func] = postfix_to_prefix
haftmann@22178
   299
wenzelm@10330
   300
end