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