src/HOL/UNITY/ListOrder.thy
author haftmann
Fri Jul 25 12:03:34 2008 +0200 (2008-07-25)
changeset 27682 25aceefd4786
parent 23767 7272a839ccd9
child 30198 922f944f03b2
permissions -rw-r--r--
added class preorder
     1 (*  Title:      HOL/UNITY/ListOrder
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1998  University of Cambridge
     5 
     6 Lists are partially ordered by Charpentier's Generalized Prefix Relation
     7    (xs,ys) : genPrefix(r)
     8      if ys = xs' @ zs where length xs = length xs'
     9      and corresponding elements of xs, xs' are pairwise related by r
    10 
    11 Also overloads <= and < for lists!
    12 
    13 Based on Lex/Prefix
    14 *)
    15 
    16 header {*The Prefix Ordering on Lists*}
    17 
    18 theory ListOrder
    19 imports Main
    20 begin
    21 
    22 inductive_set
    23   genPrefix :: "('a * 'a)set => ('a list * 'a list)set"
    24   for r :: "('a * 'a)set"
    25  where
    26    Nil:     "([],[]) : genPrefix(r)"
    27 
    28  | prepend: "[| (xs,ys) : genPrefix(r);  (x,y) : r |] ==>
    29 	     (x#xs, y#ys) : genPrefix(r)"
    30 
    31  | append:  "(xs,ys) : genPrefix(r) ==> (xs, ys@zs) : genPrefix(r)"
    32 
    33 instantiation list :: (type) ord 
    34 begin
    35 
    36 definition
    37   prefix_def:        "xs <= zs \<longleftrightarrow>  (xs, zs) : genPrefix Id"
    38 
    39 definition
    40   strict_prefix_def: "xs < zs  \<longleftrightarrow>  xs \<le> zs \<and> \<not> zs \<le> (xs :: 'a list)"
    41 
    42 instance ..  
    43 
    44 (*Constants for the <= and >= relations, used below in translations*)
    45 
    46 end
    47 
    48 constdefs
    49   Le :: "(nat*nat) set"
    50     "Le == {(x,y). x <= y}"
    51 
    52   Ge :: "(nat*nat) set"
    53     "Ge == {(x,y). y <= x}"
    54 
    55 abbreviation
    56   pfixLe :: "[nat list, nat list] => bool"  (infixl "pfixLe" 50)  where
    57   "xs pfixLe ys == (xs,ys) : genPrefix Le"
    58 
    59 abbreviation
    60   pfixGe :: "[nat list, nat list] => bool"  (infixl "pfixGe" 50)  where
    61   "xs pfixGe ys == (xs,ys) : genPrefix Ge"
    62 
    63 
    64 subsection{*preliminary lemmas*}
    65 
    66 lemma Nil_genPrefix [iff]: "([], xs) : genPrefix r"
    67 by (cut_tac genPrefix.Nil [THEN genPrefix.append], auto)
    68 
    69 lemma genPrefix_length_le: "(xs,ys) : genPrefix r ==> length xs <= length ys"
    70 by (erule genPrefix.induct, auto)
    71 
    72 lemma cdlemma:
    73      "[| (xs', ys'): genPrefix r |]  
    74       ==> (ALL x xs. xs' = x#xs --> (EX y ys. ys' = y#ys & (x,y) : r & (xs, ys) : genPrefix r))"
    75 apply (erule genPrefix.induct, blast, blast)
    76 apply (force intro: genPrefix.append)
    77 done
    78 
    79 (*As usual converting it to an elimination rule is tiresome*)
    80 lemma cons_genPrefixE [elim!]: 
    81      "[| (x#xs, zs): genPrefix r;   
    82          !!y ys. [| zs = y#ys;  (x,y) : r;  (xs, ys) : genPrefix r |] ==> P  
    83       |] ==> P"
    84 by (drule cdlemma, simp, blast)
    85 
    86 lemma Cons_genPrefix_Cons [iff]:
    87      "((x#xs,y#ys) : genPrefix r) = ((x,y) : r & (xs,ys) : genPrefix r)"
    88 by (blast intro: genPrefix.prepend)
    89 
    90 
    91 subsection{*genPrefix is a partial order*}
    92 
    93 lemma refl_genPrefix: "reflexive r ==> reflexive (genPrefix r)"
    94 
    95 apply (unfold refl_def, auto)
    96 apply (induct_tac "x")
    97 prefer 2 apply (blast intro: genPrefix.prepend)
    98 apply (blast intro: genPrefix.Nil)
    99 done
   100 
   101 lemma genPrefix_refl [simp]: "reflexive r ==> (l,l) : genPrefix r"
   102 by (erule reflD [OF refl_genPrefix UNIV_I])
   103 
   104 lemma genPrefix_mono: "r<=s ==> genPrefix r <= genPrefix s"
   105 apply clarify
   106 apply (erule genPrefix.induct)
   107 apply (auto intro: genPrefix.append)
   108 done
   109 
   110 
   111 (** Transitivity **)
   112 
   113 (*A lemma for proving genPrefix_trans_O*)
   114 lemma append_genPrefix [rule_format]:
   115      "ALL zs. (xs @ ys, zs) : genPrefix r --> (xs, zs) : genPrefix r"
   116 by (induct_tac "xs", auto)
   117 
   118 (*Lemma proving transitivity and more*)
   119 lemma genPrefix_trans_O [rule_format]: 
   120      "(x, y) : genPrefix r  
   121       ==> ALL z. (y,z) : genPrefix s --> (x, z) : genPrefix (s O r)"
   122 apply (erule genPrefix.induct)
   123   prefer 3 apply (blast dest: append_genPrefix)
   124  prefer 2 apply (blast intro: genPrefix.prepend, blast)
   125 done
   126 
   127 lemma genPrefix_trans [rule_format]:
   128      "[| (x,y) : genPrefix r;  (y,z) : genPrefix r;  trans r |]  
   129       ==> (x,z) : genPrefix r"
   130 apply (rule trans_O_subset [THEN genPrefix_mono, THEN subsetD])
   131  apply assumption
   132 apply (blast intro: genPrefix_trans_O)
   133 done
   134 
   135 lemma prefix_genPrefix_trans [rule_format]: 
   136      "[| x<=y;  (y,z) : genPrefix r |] ==> (x, z) : genPrefix r"
   137 apply (unfold prefix_def)
   138 apply (subst R_O_Id [symmetric], erule genPrefix_trans_O, assumption)
   139 done
   140 
   141 lemma genPrefix_prefix_trans [rule_format]: 
   142      "[| (x,y) : genPrefix r;  y<=z |] ==> (x,z) : genPrefix r"
   143 apply (unfold prefix_def)
   144 apply (subst Id_O_R [symmetric], erule genPrefix_trans_O, assumption)
   145 done
   146 
   147 lemma trans_genPrefix: "trans r ==> trans (genPrefix r)"
   148 by (blast intro: transI genPrefix_trans)
   149 
   150 
   151 (** Antisymmetry **)
   152 
   153 lemma genPrefix_antisym [rule_format]:
   154      "[| (xs,ys) : genPrefix r;  antisym r |]  
   155       ==> (ys,xs) : genPrefix r --> xs = ys"
   156 apply (erule genPrefix.induct)
   157   txt{*Base case*}
   158   apply blast
   159  txt{*prepend case*}
   160  apply (simp add: antisym_def)
   161 txt{*append case is the hardest*}
   162 apply clarify
   163 apply (subgoal_tac "length zs = 0", force)
   164 apply (drule genPrefix_length_le)+
   165 apply (simp del: length_0_conv)
   166 done
   167 
   168 lemma antisym_genPrefix: "antisym r ==> antisym (genPrefix r)"
   169 by (blast intro: antisymI genPrefix_antisym)
   170 
   171 
   172 subsection{*recursion equations*}
   173 
   174 lemma genPrefix_Nil [simp]: "((xs, []) : genPrefix r) = (xs = [])"
   175 apply (induct_tac "xs")
   176 prefer 2 apply blast
   177 apply simp
   178 done
   179 
   180 lemma same_genPrefix_genPrefix [simp]: 
   181     "reflexive r ==> ((xs@ys, xs@zs) : genPrefix r) = ((ys,zs) : genPrefix r)"
   182 apply (unfold refl_def)
   183 apply (induct_tac "xs")
   184 apply (simp_all (no_asm_simp))
   185 done
   186 
   187 lemma genPrefix_Cons:
   188      "((xs, y#ys) : genPrefix r) =  
   189       (xs=[] | (EX z zs. xs=z#zs & (z,y) : r & (zs,ys) : genPrefix r))"
   190 by (case_tac "xs", auto)
   191 
   192 lemma genPrefix_take_append:
   193      "[| reflexive r;  (xs,ys) : genPrefix r |]  
   194       ==>  (xs@zs, take (length xs) ys @ zs) : genPrefix r"
   195 apply (erule genPrefix.induct)
   196 apply (frule_tac [3] genPrefix_length_le)
   197 apply (simp_all (no_asm_simp) add: diff_is_0_eq [THEN iffD2])
   198 done
   199 
   200 lemma genPrefix_append_both:
   201      "[| reflexive r;  (xs,ys) : genPrefix r;  length xs = length ys |]  
   202       ==>  (xs@zs, ys @ zs) : genPrefix r"
   203 apply (drule genPrefix_take_append, assumption)
   204 apply (simp add: take_all)
   205 done
   206 
   207 
   208 (*NOT suitable for rewriting since [y] has the form y#ys*)
   209 lemma append_cons_eq: "xs @ y # ys = (xs @ [y]) @ ys"
   210 by auto
   211 
   212 lemma aolemma:
   213      "[| (xs,ys) : genPrefix r;  reflexive r |]  
   214       ==> length xs < length ys --> (xs @ [ys ! length xs], ys) : genPrefix r"
   215 apply (erule genPrefix.induct)
   216   apply blast
   217  apply simp
   218 txt{*Append case is hardest*}
   219 apply simp
   220 apply (frule genPrefix_length_le [THEN le_imp_less_or_eq])
   221 apply (erule disjE)
   222 apply (simp_all (no_asm_simp) add: neq_Nil_conv nth_append)
   223 apply (blast intro: genPrefix.append, auto)
   224 apply (subst append_cons_eq, fast intro: genPrefix_append_both genPrefix.append)
   225 done
   226 
   227 lemma append_one_genPrefix:
   228      "[| (xs,ys) : genPrefix r;  length xs < length ys;  reflexive r |]  
   229       ==> (xs @ [ys ! length xs], ys) : genPrefix r"
   230 by (blast intro: aolemma [THEN mp])
   231 
   232 
   233 (** Proving the equivalence with Charpentier's definition **)
   234 
   235 lemma genPrefix_imp_nth [rule_format]:
   236      "ALL i ys. i < length xs  
   237                 --> (xs, ys) : genPrefix r --> (xs ! i, ys ! i) : r"
   238 apply (induct_tac "xs", auto)
   239 apply (case_tac "i", auto)
   240 done
   241 
   242 lemma nth_imp_genPrefix [rule_format]:
   243      "ALL ys. length xs <= length ys   
   244       --> (ALL i. i < length xs --> (xs ! i, ys ! i) : r)   
   245       --> (xs, ys) : genPrefix r"
   246 apply (induct_tac "xs")
   247 apply (simp_all (no_asm_simp) add: less_Suc_eq_0_disj all_conj_distrib)
   248 apply clarify
   249 apply (case_tac "ys")
   250 apply (force+)
   251 done
   252 
   253 lemma genPrefix_iff_nth:
   254      "((xs,ys) : genPrefix r) =  
   255       (length xs <= length ys & (ALL i. i < length xs --> (xs!i, ys!i) : r))"
   256 apply (blast intro: genPrefix_length_le genPrefix_imp_nth nth_imp_genPrefix)
   257 done
   258 
   259 
   260 subsection{*The type of lists is partially ordered*}
   261 
   262 declare reflexive_Id [iff] 
   263         antisym_Id [iff] 
   264         trans_Id [iff]
   265 
   266 lemma prefix_refl [iff]: "xs <= (xs::'a list)"
   267 by (simp add: prefix_def)
   268 
   269 lemma prefix_trans: "!!xs::'a list. [| xs <= ys; ys <= zs |] ==> xs <= zs"
   270 apply (unfold prefix_def)
   271 apply (blast intro: genPrefix_trans)
   272 done
   273 
   274 lemma prefix_antisym: "!!xs::'a list. [| xs <= ys; ys <= xs |] ==> xs = ys"
   275 apply (unfold prefix_def)
   276 apply (blast intro: genPrefix_antisym)
   277 done
   278 
   279 lemma prefix_less_le_not_le: "!!xs::'a list. (xs < zs) = (xs <= zs & \<not> zs \<le> xs)"
   280 by (unfold strict_prefix_def, auto)
   281 
   282 instance list :: (type) order
   283   by (intro_classes,
   284       (assumption | rule prefix_refl prefix_trans prefix_antisym
   285                      prefix_less_le_not_le)+)
   286 
   287 (*Monotonicity of "set" operator WRT prefix*)
   288 lemma set_mono: "xs <= ys ==> set xs <= set ys"
   289 apply (unfold prefix_def)
   290 apply (erule genPrefix.induct, auto)
   291 done
   292 
   293 
   294 (** recursion equations **)
   295 
   296 lemma Nil_prefix [iff]: "[] <= xs"
   297 apply (unfold prefix_def)
   298 apply (simp add: Nil_genPrefix)
   299 done
   300 
   301 lemma prefix_Nil [simp]: "(xs <= []) = (xs = [])"
   302 apply (unfold prefix_def)
   303 apply (simp add: genPrefix_Nil)
   304 done
   305 
   306 lemma Cons_prefix_Cons [simp]: "(x#xs <= y#ys) = (x=y & xs<=ys)"
   307 by (simp add: prefix_def)
   308 
   309 lemma same_prefix_prefix [simp]: "(xs@ys <= xs@zs) = (ys <= zs)"
   310 by (simp add: prefix_def)
   311 
   312 lemma append_prefix [iff]: "(xs@ys <= xs) = (ys <= [])"
   313 by (insert same_prefix_prefix [of xs ys "[]"], simp)
   314 
   315 lemma prefix_appendI [simp]: "xs <= ys ==> xs <= ys@zs"
   316 apply (unfold prefix_def)
   317 apply (erule genPrefix.append)
   318 done
   319 
   320 lemma prefix_Cons: 
   321    "(xs <= y#ys) = (xs=[] | (? zs. xs=y#zs & zs <= ys))"
   322 by (simp add: prefix_def genPrefix_Cons)
   323 
   324 lemma append_one_prefix: 
   325   "[| xs <= ys; length xs < length ys |] ==> xs @ [ys ! length xs] <= ys"
   326 apply (unfold prefix_def)
   327 apply (simp add: append_one_genPrefix)
   328 done
   329 
   330 lemma prefix_length_le: "xs <= ys ==> length xs <= length ys"
   331 apply (unfold prefix_def)
   332 apply (erule genPrefix_length_le)
   333 done
   334 
   335 lemma splemma: "xs<=ys ==> xs~=ys --> length xs < length ys"
   336 apply (unfold prefix_def)
   337 apply (erule genPrefix.induct, auto)
   338 done
   339 
   340 lemma strict_prefix_length_less: "xs < ys ==> length xs < length ys"
   341 apply (unfold strict_prefix_def)
   342 apply (blast intro: splemma [THEN mp])
   343 done
   344 
   345 lemma mono_length: "mono length"
   346 by (blast intro: monoI prefix_length_le)
   347 
   348 (*Equivalence to the definition used in Lex/Prefix.thy*)
   349 lemma prefix_iff: "(xs <= zs) = (EX ys. zs = xs@ys)"
   350 apply (unfold prefix_def)
   351 apply (auto simp add: genPrefix_iff_nth nth_append)
   352 apply (rule_tac x = "drop (length xs) zs" in exI)
   353 apply (rule nth_equalityI)
   354 apply (simp_all (no_asm_simp) add: nth_append)
   355 done
   356 
   357 lemma prefix_snoc [simp]: "(xs <= ys@[y]) = (xs = ys@[y] | xs <= ys)"
   358 apply (simp add: prefix_iff)
   359 apply (rule iffI)
   360  apply (erule exE)
   361  apply (rename_tac "zs")
   362  apply (rule_tac xs = zs in rev_exhaust)
   363   apply simp
   364  apply clarify
   365  apply (simp del: append_assoc add: append_assoc [symmetric], force)
   366 done
   367 
   368 lemma prefix_append_iff:
   369      "(xs <= ys@zs) = (xs <= ys | (? us. xs = ys@us & us <= zs))"
   370 apply (rule_tac xs = zs in rev_induct)
   371  apply force
   372 apply (simp del: append_assoc add: append_assoc [symmetric], force)
   373 done
   374 
   375 (*Although the prefix ordering is not linear, the prefixes of a list
   376   are linearly ordered.*)
   377 lemma common_prefix_linear [rule_format]:
   378      "!!zs::'a list. xs <= zs --> ys <= zs --> xs <= ys | ys <= xs"
   379 by (rule_tac xs = zs in rev_induct, auto)
   380 
   381 
   382 subsection{*pfixLe, pfixGe: properties inherited from the translations*}
   383 
   384 (** pfixLe **)
   385 
   386 lemma reflexive_Le [iff]: "reflexive Le"
   387 by (unfold refl_def Le_def, auto)
   388 
   389 lemma antisym_Le [iff]: "antisym Le"
   390 by (unfold antisym_def Le_def, auto)
   391 
   392 lemma trans_Le [iff]: "trans Le"
   393 by (unfold trans_def Le_def, auto)
   394 
   395 lemma pfixLe_refl [iff]: "x pfixLe x"
   396 by simp
   397 
   398 lemma pfixLe_trans: "[| x pfixLe y; y pfixLe z |] ==> x pfixLe z"
   399 by (blast intro: genPrefix_trans)
   400 
   401 lemma pfixLe_antisym: "[| x pfixLe y; y pfixLe x |] ==> x = y"
   402 by (blast intro: genPrefix_antisym)
   403 
   404 lemma prefix_imp_pfixLe: "xs<=ys ==> xs pfixLe ys"
   405 apply (unfold prefix_def Le_def)
   406 apply (blast intro: genPrefix_mono [THEN [2] rev_subsetD])
   407 done
   408 
   409 lemma reflexive_Ge [iff]: "reflexive Ge"
   410 by (unfold refl_def Ge_def, auto)
   411 
   412 lemma antisym_Ge [iff]: "antisym Ge"
   413 by (unfold antisym_def Ge_def, auto)
   414 
   415 lemma trans_Ge [iff]: "trans Ge"
   416 by (unfold trans_def Ge_def, auto)
   417 
   418 lemma pfixGe_refl [iff]: "x pfixGe x"
   419 by simp
   420 
   421 lemma pfixGe_trans: "[| x pfixGe y; y pfixGe z |] ==> x pfixGe z"
   422 by (blast intro: genPrefix_trans)
   423 
   424 lemma pfixGe_antisym: "[| x pfixGe y; y pfixGe x |] ==> x = y"
   425 by (blast intro: genPrefix_antisym)
   426 
   427 lemma prefix_imp_pfixGe: "xs<=ys ==> xs pfixGe ys"
   428 apply (unfold prefix_def Ge_def)
   429 apply (blast intro: genPrefix_mono [THEN [2] rev_subsetD])
   430 done
   431 
   432 end