src/HOL/Library/List_Prefix.thy
author wenzelm
Thu Jan 11 19:37:46 2001 +0100 (2001-01-11)
changeset 10870 9444e3cf37e1
parent 10512 d34192966cd8
child 11780 d17ee2241257
permissions -rw-r--r--
added strict_prefixI', strict_prefixE';
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@10330
     6
header {*
wenzelm@10330
     7
  \title{List prefixes}
wenzelm@10330
     8
  \author{Tobias Nipkow and Markus Wenzel}
wenzelm@10330
     9
*}
wenzelm@10330
    10
wenzelm@10330
    11
theory List_Prefix = Main:
wenzelm@10330
    12
wenzelm@10330
    13
subsection {* Prefix order on lists *}
wenzelm@10330
    14
wenzelm@10330
    15
instance list :: ("term") ord ..
wenzelm@10330
    16
wenzelm@10330
    17
defs (overloaded)
wenzelm@10389
    18
  prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
wenzelm@10389
    19
  strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
wenzelm@10330
    20
wenzelm@10330
    21
instance list :: ("term") order
wenzelm@10389
    22
  by intro_classes (auto simp add: prefix_def strict_prefix_def)
wenzelm@10330
    23
wenzelm@10389
    24
lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
wenzelm@10389
    25
  by (unfold prefix_def) blast
wenzelm@10330
    26
wenzelm@10389
    27
lemma prefixE [elim?]: "xs \<le> ys ==> (!!zs. ys = xs @ zs ==> C) ==> C"
wenzelm@10389
    28
  by (unfold prefix_def) blast
wenzelm@10330
    29
wenzelm@10870
    30
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
wenzelm@10870
    31
  by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    32
wenzelm@10870
    33
lemma strict_prefixE' [elim?]:
wenzelm@10870
    34
    "xs < ys ==> (!!z zs. ys = xs @ z # zs ==> C) ==> C"
wenzelm@10870
    35
proof -
wenzelm@10870
    36
  assume r: "!!z zs. ys = xs @ z # zs ==> C"
wenzelm@10870
    37
  assume "xs < ys"
wenzelm@10870
    38
  then obtain us where "ys = xs @ us" and "xs \<noteq> ys"
wenzelm@10870
    39
    by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    40
  with r 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@10389
    44
  by (unfold strict_prefix_def) blast
wenzelm@10330
    45
wenzelm@10389
    46
lemma strict_prefixE [elim?]:
wenzelm@10389
    47
    "xs < ys ==> (xs \<le> ys ==> xs \<noteq> (ys::'a list) ==> C) ==> C"
wenzelm@10389
    48
  by (unfold strict_prefix_def) blast
wenzelm@10330
    49
wenzelm@10330
    50
wenzelm@10389
    51
subsection {* Basic properties of prefixes *}
wenzelm@10330
    52
wenzelm@10330
    53
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    54
  by (simp add: prefix_def)
wenzelm@10330
    55
wenzelm@10330
    56
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    57
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    58
wenzelm@10330
    59
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    60
proof
wenzelm@10389
    61
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    62
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    63
  show "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    64
  proof (cases zs rule: rev_cases)
wenzelm@10389
    65
    assume "zs = []"
wenzelm@10389
    66
    with zs have "xs = ys @ [y]" by simp
wenzelm@10389
    67
    thus ?thesis ..
wenzelm@10389
    68
  next
wenzelm@10389
    69
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    70
    with zs have "ys = xs @ zs'" by simp
wenzelm@10389
    71
    hence "xs \<le> ys" ..
wenzelm@10389
    72
    thus ?thesis ..
wenzelm@10389
    73
  qed
wenzelm@10389
    74
next
wenzelm@10389
    75
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    76
  thus "xs \<le> ys @ [y]"
wenzelm@10389
    77
  proof
wenzelm@10389
    78
    assume "xs = ys @ [y]"
wenzelm@10389
    79
    thus ?thesis by simp
wenzelm@10389
    80
  next
wenzelm@10389
    81
    assume "xs \<le> ys"
wenzelm@10389
    82
    then obtain zs where "ys = xs @ zs" ..
wenzelm@10389
    83
    hence "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@10389
    84
    thus ?thesis ..
wenzelm@10389
    85
  qed
wenzelm@10389
    86
qed
wenzelm@10330
    87
wenzelm@10330
    88
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    89
  by (auto simp add: prefix_def)
wenzelm@10330
    90
wenzelm@10330
    91
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
    92
  by (induct xs) simp_all
wenzelm@10330
    93
wenzelm@10389
    94
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
wenzelm@10389
    95
proof -
wenzelm@10389
    96
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@10389
    97
  thus ?thesis by simp
wenzelm@10389
    98
qed
wenzelm@10330
    99
wenzelm@10330
   100
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
wenzelm@10389
   101
proof -
wenzelm@10389
   102
  assume "xs \<le> ys"
wenzelm@10389
   103
  then obtain us where "ys = xs @ us" ..
wenzelm@10389
   104
  hence "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@10389
   105
  thus ?thesis ..
wenzelm@10389
   106
qed
wenzelm@10330
   107
wenzelm@10330
   108
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
   109
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
   110
wenzelm@10330
   111
theorem prefix_append:
wenzelm@10330
   112
    "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   113
  apply (induct zs rule: rev_induct)
wenzelm@10330
   114
   apply force
wenzelm@10330
   115
  apply (simp del: append_assoc add: append_assoc [symmetric])
wenzelm@10330
   116
  apply simp
wenzelm@10330
   117
  apply blast
wenzelm@10330
   118
  done
wenzelm@10330
   119
wenzelm@10330
   120
lemma append_one_prefix:
wenzelm@10330
   121
    "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
wenzelm@10330
   122
  apply (unfold prefix_def)
wenzelm@10330
   123
  apply (auto simp add: nth_append)
wenzelm@10389
   124
  apply (case_tac zs)
wenzelm@10330
   125
   apply auto
wenzelm@10330
   126
  done
wenzelm@10330
   127
wenzelm@10330
   128
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   129
  by (auto simp add: prefix_def)
wenzelm@10330
   130
wenzelm@10330
   131
wenzelm@10389
   132
subsection {* Parallel lists *}
wenzelm@10389
   133
wenzelm@10389
   134
constdefs
wenzelm@10389
   135
  parallel :: "'a list => 'a list => bool"    (infixl "\<parallel>" 50)
wenzelm@10389
   136
  "xs \<parallel> ys == \<not> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@10389
   137
wenzelm@10389
   138
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
wenzelm@10389
   139
  by (unfold parallel_def) blast
wenzelm@10330
   140
wenzelm@10389
   141
lemma parallelE [elim]:
wenzelm@10389
   142
    "xs \<parallel> ys ==> (\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> C) ==> C"
wenzelm@10389
   143
  by (unfold parallel_def) blast
wenzelm@10330
   144
wenzelm@10389
   145
theorem prefix_cases:
wenzelm@10389
   146
  "(xs \<le> ys ==> C) ==>
wenzelm@10512
   147
    (ys < xs ==> C) ==>
wenzelm@10389
   148
    (xs \<parallel> ys ==> C) ==> C"
wenzelm@10512
   149
  by (unfold parallel_def strict_prefix_def) blast
wenzelm@10330
   150
wenzelm@10389
   151
theorem parallel_decomp:
wenzelm@10389
   152
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   153
  (is "PROP ?P xs" concl is "?E xs")
wenzelm@10408
   154
proof (induct xs rule: rev_induct)
wenzelm@10408
   155
  assume "[] \<parallel> ys" hence False by auto
wenzelm@10408
   156
  thus "?E []" ..
wenzelm@10408
   157
next
wenzelm@10408
   158
  fix x xs
wenzelm@10408
   159
  assume hyp: "PROP ?P xs"
wenzelm@10408
   160
  assume asm: "xs @ [x] \<parallel> ys"
wenzelm@10408
   161
  show "?E (xs @ [x])"
wenzelm@10408
   162
  proof (rule prefix_cases)
wenzelm@10408
   163
    assume le: "xs \<le> ys"
wenzelm@10408
   164
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   165
    show ?thesis
wenzelm@10408
   166
    proof (cases ys')
wenzelm@10408
   167
      assume "ys' = []" with ys have "xs = ys" by simp
wenzelm@10408
   168
      with asm have "[x] \<parallel> []" by auto
wenzelm@10408
   169
      hence False by blast
wenzelm@10389
   170
      thus ?thesis ..
wenzelm@10389
   171
    next
wenzelm@10408
   172
      fix c cs assume ys': "ys' = c # cs"
wenzelm@10408
   173
      with asm ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@10408
   174
      hence "x \<noteq> c" by auto
wenzelm@10408
   175
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   176
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   177
      ultimately show ?thesis by blast
wenzelm@10389
   178
    qed
wenzelm@10408
   179
  next
wenzelm@10512
   180
    assume "ys < xs" hence "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
wenzelm@10408
   181
    with asm have False by blast
wenzelm@10408
   182
    thus ?thesis ..
wenzelm@10408
   183
  next
wenzelm@10408
   184
    assume "xs \<parallel> ys"
wenzelm@10408
   185
    with hyp obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   186
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   187
      by blast
wenzelm@10408
   188
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   189
    with neq ys show ?thesis by blast
wenzelm@10389
   190
  qed
wenzelm@10389
   191
qed
wenzelm@10330
   192
wenzelm@10330
   193
end