src/HOL/Library/List_Prefix.thy
author wenzelm
Mon Nov 06 22:56:07 2000 +0100 (2000-11-06)
changeset 10408 d8b3613158b1
parent 10389 c7d8901ab269
child 10512 d34192966cd8
permissions -rw-r--r--
improved: 'induct' handle non-atomic goals;
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@10389
    30
lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
wenzelm@10389
    31
  by (unfold strict_prefix_def) blast
wenzelm@10330
    32
wenzelm@10389
    33
lemma strict_prefixE [elim?]:
wenzelm@10389
    34
    "xs < ys ==> (xs \<le> ys ==> xs \<noteq> (ys::'a list) ==> C) ==> C"
wenzelm@10389
    35
  by (unfold strict_prefix_def) blast
wenzelm@10330
    36
wenzelm@10330
    37
wenzelm@10389
    38
subsection {* Basic properties of prefixes *}
wenzelm@10330
    39
wenzelm@10330
    40
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    41
  by (simp add: prefix_def)
wenzelm@10330
    42
wenzelm@10330
    43
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    44
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    45
wenzelm@10330
    46
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    47
proof
wenzelm@10389
    48
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    49
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    50
  show "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    51
  proof (cases zs rule: rev_cases)
wenzelm@10389
    52
    assume "zs = []"
wenzelm@10389
    53
    with zs have "xs = ys @ [y]" by simp
wenzelm@10389
    54
    thus ?thesis ..
wenzelm@10389
    55
  next
wenzelm@10389
    56
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    57
    with zs have "ys = xs @ zs'" by simp
wenzelm@10389
    58
    hence "xs \<le> ys" ..
wenzelm@10389
    59
    thus ?thesis ..
wenzelm@10389
    60
  qed
wenzelm@10389
    61
next
wenzelm@10389
    62
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    63
  thus "xs \<le> ys @ [y]"
wenzelm@10389
    64
  proof
wenzelm@10389
    65
    assume "xs = ys @ [y]"
wenzelm@10389
    66
    thus ?thesis by simp
wenzelm@10389
    67
  next
wenzelm@10389
    68
    assume "xs \<le> ys"
wenzelm@10389
    69
    then obtain zs where "ys = xs @ zs" ..
wenzelm@10389
    70
    hence "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@10389
    71
    thus ?thesis ..
wenzelm@10389
    72
  qed
wenzelm@10389
    73
qed
wenzelm@10330
    74
wenzelm@10330
    75
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    76
  by (auto simp add: prefix_def)
wenzelm@10330
    77
wenzelm@10330
    78
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
    79
  by (induct xs) simp_all
wenzelm@10330
    80
wenzelm@10389
    81
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
wenzelm@10389
    82
proof -
wenzelm@10389
    83
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@10389
    84
  thus ?thesis by simp
wenzelm@10389
    85
qed
wenzelm@10330
    86
wenzelm@10330
    87
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
wenzelm@10389
    88
proof -
wenzelm@10389
    89
  assume "xs \<le> ys"
wenzelm@10389
    90
  then obtain us where "ys = xs @ us" ..
wenzelm@10389
    91
  hence "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@10389
    92
  thus ?thesis ..
wenzelm@10389
    93
qed
wenzelm@10330
    94
wenzelm@10330
    95
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
    96
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
    97
wenzelm@10330
    98
theorem prefix_append:
wenzelm@10330
    99
    "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   100
  apply (induct zs rule: rev_induct)
wenzelm@10330
   101
   apply force
wenzelm@10330
   102
  apply (simp del: append_assoc add: append_assoc [symmetric])
wenzelm@10330
   103
  apply simp
wenzelm@10330
   104
  apply blast
wenzelm@10330
   105
  done
wenzelm@10330
   106
wenzelm@10330
   107
lemma append_one_prefix:
wenzelm@10330
   108
    "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
wenzelm@10330
   109
  apply (unfold prefix_def)
wenzelm@10330
   110
  apply (auto simp add: nth_append)
wenzelm@10389
   111
  apply (case_tac zs)
wenzelm@10330
   112
   apply auto
wenzelm@10330
   113
  done
wenzelm@10330
   114
wenzelm@10330
   115
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   116
  by (auto simp add: prefix_def)
wenzelm@10330
   117
wenzelm@10330
   118
wenzelm@10389
   119
subsection {* Parallel lists *}
wenzelm@10389
   120
wenzelm@10389
   121
constdefs
wenzelm@10389
   122
  parallel :: "'a list => 'a list => bool"    (infixl "\<parallel>" 50)
wenzelm@10389
   123
  "xs \<parallel> ys == \<not> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@10389
   124
wenzelm@10389
   125
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
wenzelm@10389
   126
  by (unfold parallel_def) blast
wenzelm@10330
   127
wenzelm@10389
   128
lemma parallelE [elim]:
wenzelm@10389
   129
    "xs \<parallel> ys ==> (\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> C) ==> C"
wenzelm@10389
   130
  by (unfold parallel_def) blast
wenzelm@10330
   131
wenzelm@10389
   132
theorem prefix_cases:
wenzelm@10389
   133
  "(xs \<le> ys ==> C) ==>
wenzelm@10389
   134
    (ys \<le> xs ==> C) ==>
wenzelm@10389
   135
    (xs \<parallel> ys ==> C) ==> C"
wenzelm@10389
   136
  by (unfold parallel_def) blast
wenzelm@10330
   137
wenzelm@10389
   138
theorem parallel_decomp:
wenzelm@10389
   139
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   140
  (is "PROP ?P xs" concl is "?E xs")
wenzelm@10408
   141
proof (induct xs rule: rev_induct)
wenzelm@10408
   142
  assume "[] \<parallel> ys" hence False by auto
wenzelm@10408
   143
  thus "?E []" ..
wenzelm@10408
   144
next
wenzelm@10408
   145
  fix x xs
wenzelm@10408
   146
  assume hyp: "PROP ?P xs"
wenzelm@10408
   147
  assume asm: "xs @ [x] \<parallel> ys"
wenzelm@10408
   148
  show "?E (xs @ [x])"
wenzelm@10408
   149
  proof (rule prefix_cases)
wenzelm@10408
   150
    assume le: "xs \<le> ys"
wenzelm@10408
   151
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   152
    show ?thesis
wenzelm@10408
   153
    proof (cases ys')
wenzelm@10408
   154
      assume "ys' = []" with ys have "xs = ys" by simp
wenzelm@10408
   155
      with asm have "[x] \<parallel> []" by auto
wenzelm@10408
   156
      hence False by blast
wenzelm@10389
   157
      thus ?thesis ..
wenzelm@10389
   158
    next
wenzelm@10408
   159
      fix c cs assume ys': "ys' = c # cs"
wenzelm@10408
   160
      with asm ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@10408
   161
      hence "x \<noteq> c" by auto
wenzelm@10408
   162
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   163
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   164
      ultimately show ?thesis by blast
wenzelm@10389
   165
    qed
wenzelm@10408
   166
  next
wenzelm@10408
   167
    assume "ys \<le> xs" hence "ys \<le> xs @ [x]" by simp
wenzelm@10408
   168
    with asm have False by blast
wenzelm@10408
   169
    thus ?thesis ..
wenzelm@10408
   170
  next
wenzelm@10408
   171
    assume "xs \<parallel> ys"
wenzelm@10408
   172
    with hyp obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   173
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   174
      by blast
wenzelm@10408
   175
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   176
    with neq ys show ?thesis by blast
wenzelm@10389
   177
  qed
wenzelm@10389
   178
qed
wenzelm@10330
   179
wenzelm@10330
   180
end