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