src/HOL/MicroJava/BV/Listn.thy
author kleing
Sun Dec 16 00:17:44 2001 +0100 (2001-12-16)
changeset 12516 d09d0f160888
parent 11225 01181fdbc9f0
child 12911 704713ca07ea
permissions -rw-r--r--
exceptions
     1 (*  Title:      HOL/MicroJava/BV/Listn.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   2000 TUM
     5 
     6 Lists of a fixed length
     7 *)
     8 
     9 header "Fixed Length Lists"
    10 
    11 theory Listn = Err:
    12 
    13 constdefs
    14 
    15  list :: "nat => 'a set => 'a list set"
    16 "list n A == {xs. length xs = n & set xs <= A}"
    17 
    18  le :: "'a ord => ('a list)ord"
    19 "le r == list_all2 (%x y. x <=_r y)"
    20 
    21 syntax "@lesublist" :: "'a list => 'a ord => 'a list => bool"
    22        ("(_ /<=[_] _)" [50, 0, 51] 50)
    23 syntax "@lesssublist" :: "'a list => 'a ord => 'a list => bool"
    24        ("(_ /<[_] _)" [50, 0, 51] 50)
    25 translations
    26  "x <=[r] y" == "x <=_(Listn.le r) y"
    27  "x <[r] y"  == "x <_(Listn.le r) y"
    28 
    29 constdefs
    30  map2 :: "('a => 'b => 'c) => 'a list => 'b list => 'c list"
    31 "map2 f == (%xs ys. map (split f) (zip xs ys))"
    32 
    33 syntax "@plussublist" :: "'a list => ('a => 'b => 'c) => 'b list => 'c list"
    34        ("(_ /+[_] _)" [65, 0, 66] 65)
    35 translations  "x +[f] y" == "x +_(map2 f) y"
    36 
    37 consts coalesce :: "'a err list => 'a list err"
    38 primrec
    39 "coalesce [] = OK[]"
    40 "coalesce (ex#exs) = Err.sup (op #) ex (coalesce exs)"
    41 
    42 constdefs
    43  sl :: "nat => 'a sl => 'a list sl"
    44 "sl n == %(A,r,f). (list n A, le r, map2 f)"
    45 
    46  sup :: "('a => 'b => 'c err) => 'a list => 'b list => 'c list err"
    47 "sup f == %xs ys. if size xs = size ys then coalesce(xs +[f] ys) else Err"
    48 
    49  upto_esl :: "nat => 'a esl => 'a list esl"
    50 "upto_esl m == %(A,r,f). (Union{list n A |n. n <= m}, le r, sup f)"
    51 
    52 lemmas [simp] = set_update_subsetI
    53 
    54 lemma unfold_lesub_list:
    55   "xs <=[r] ys == Listn.le r xs ys"
    56   by (simp add: lesub_def)
    57 
    58 lemma Nil_le_conv [iff]:
    59   "([] <=[r] ys) = (ys = [])"
    60 apply (unfold lesub_def Listn.le_def)
    61 apply simp
    62 done
    63 
    64 lemma Cons_notle_Nil [iff]: 
    65   "~ x#xs <=[r] []"
    66 apply (unfold lesub_def Listn.le_def)
    67 apply simp
    68 done
    69 
    70 
    71 lemma Cons_le_Cons [iff]:
    72   "x#xs <=[r] y#ys = (x <=_r y & xs <=[r] ys)"
    73 apply (unfold lesub_def Listn.le_def)
    74 apply simp
    75 done
    76 
    77 lemma Cons_less_Conss [simp]:
    78   "order r ==> 
    79   x#xs <_(Listn.le r) y#ys = 
    80   (x <_r y & xs <=[r] ys  |  x = y & xs <_(Listn.le r) ys)"
    81 apply (unfold lesssub_def)
    82 apply blast
    83 done  
    84 
    85 lemma list_update_le_cong:
    86   "[| i<size xs; xs <=[r] ys; x <=_r y |] ==> xs[i:=x] <=[r] ys[i:=y]";
    87 apply (unfold unfold_lesub_list)
    88 apply (unfold Listn.le_def)
    89 apply (simp add: list_all2_conv_all_nth nth_list_update)
    90 done
    91 
    92 
    93 lemma le_listD:
    94   "[| xs <=[r] ys; p < size xs |] ==> xs!p <=_r ys!p"
    95 apply (unfold Listn.le_def lesub_def)
    96 apply (simp add: list_all2_conv_all_nth)
    97 done
    98 
    99 lemma le_list_refl:
   100   "!x. x <=_r x ==> xs <=[r] xs"
   101 apply (unfold unfold_lesub_list)
   102 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   103 done
   104 
   105 lemma le_list_trans:
   106   "[| order r; xs <=[r] ys; ys <=[r] zs |] ==> xs <=[r] zs"
   107 apply (unfold unfold_lesub_list)
   108 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   109 apply clarify
   110 apply simp
   111 apply (blast intro: order_trans)
   112 done
   113 
   114 lemma le_list_antisym:
   115   "[| order r; xs <=[r] ys; ys <=[r] xs |] ==> xs = ys"
   116 apply (unfold unfold_lesub_list)
   117 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   118 apply (rule nth_equalityI)
   119  apply blast
   120 apply clarify
   121 apply simp
   122 apply (blast intro: order_antisym)
   123 done
   124 
   125 lemma order_listI [simp, intro!]:
   126   "order r ==> order(Listn.le r)"
   127 apply (subst order_def)
   128 apply (blast intro: le_list_refl le_list_trans le_list_antisym
   129              dest: order_refl)
   130 done
   131 
   132 
   133 lemma lesub_list_impl_same_size [simp]:
   134   "xs <=[r] ys ==> size ys = size xs"  
   135 apply (unfold Listn.le_def lesub_def)
   136 apply (simp add: list_all2_conv_all_nth)
   137 done 
   138 
   139 lemma lesssub_list_impl_same_size:
   140   "xs <_(Listn.le r) ys ==> size ys = size xs"
   141 apply (unfold lesssub_def)
   142 apply auto
   143 done  
   144 
   145 lemma listI:
   146   "[| length xs = n; set xs <= A |] ==> xs : list n A"
   147 apply (unfold list_def)
   148 apply blast
   149 done
   150 
   151 lemma listE_length [simp]:
   152    "xs : list n A ==> length xs = n"
   153 apply (unfold list_def)
   154 apply blast
   155 done 
   156 
   157 lemma less_lengthI:
   158   "[| xs : list n A; p < n |] ==> p < length xs"
   159   by simp
   160 
   161 lemma listE_set [simp]:
   162   "xs : list n A ==> set xs <= A"
   163 apply (unfold list_def)
   164 apply blast
   165 done 
   166 
   167 lemma list_0 [simp]:
   168   "list 0 A = {[]}"
   169 apply (unfold list_def)
   170 apply auto
   171 done 
   172 
   173 lemma in_list_Suc_iff: 
   174   "(xs : list (Suc n) A) = (? y:A. ? ys:list n A. xs = y#ys)"
   175 apply (unfold list_def)
   176 apply (case_tac "xs")
   177 apply auto
   178 done 
   179 
   180 lemma Cons_in_list_Suc [iff]:
   181   "(x#xs : list (Suc n) A) = (x:A & xs : list n A)";
   182 apply (simp add: in_list_Suc_iff)
   183 done 
   184 
   185 lemma list_not_empty:
   186   "? a. a:A ==> ? xs. xs : list n A";
   187 apply (induct "n")
   188  apply simp
   189 apply (simp add: in_list_Suc_iff)
   190 apply blast
   191 done
   192 
   193 
   194 lemma nth_in [rule_format, simp]:
   195   "!i n. length xs = n --> set xs <= A --> i < n --> (xs!i) : A"
   196 apply (induct "xs")
   197  apply simp
   198 apply (simp add: nth_Cons split: nat.split)
   199 done
   200 
   201 lemma listE_nth_in:
   202   "[| xs : list n A; i < n |] ==> (xs!i) : A"
   203   by auto
   204 
   205 lemma listt_update_in_list [simp, intro!]:
   206   "[| xs : list n A; x:A |] ==> xs[i := x] : list n A"
   207 apply (unfold list_def)
   208 apply simp
   209 done 
   210 
   211 lemma plus_list_Nil [simp]:
   212   "[] +[f] xs = []"
   213 apply (unfold plussub_def map2_def)
   214 apply simp
   215 done 
   216 
   217 lemma plus_list_Cons [simp]:
   218   "(x#xs) +[f] ys = (case ys of [] => [] | y#ys => (x +_f y)#(xs +[f] ys))"
   219   by (simp add: plussub_def map2_def split: list.split)
   220 
   221 lemma length_plus_list [rule_format, simp]:
   222   "!ys. length(xs +[f] ys) = min(length xs) (length ys)"
   223 apply (induct xs)
   224  apply simp
   225 apply clarify
   226 apply (simp (no_asm_simp) split: list.split)
   227 done
   228 
   229 lemma nth_plus_list [rule_format, simp]:
   230   "!xs ys i. length xs = n --> length ys = n --> i<n --> 
   231   (xs +[f] ys)!i = (xs!i) +_f (ys!i)"
   232 apply (induct n)
   233  apply simp
   234 apply clarify
   235 apply (case_tac xs)
   236  apply simp
   237 apply (force simp add: nth_Cons split: list.split nat.split)
   238 done
   239 
   240 
   241 lemma plus_list_ub1 [rule_format]:
   242   "[| semilat(A,r,f); set xs <= A; set ys <= A; size xs = size ys |] 
   243   ==> xs <=[r] xs +[f] ys"
   244 apply (unfold unfold_lesub_list)
   245 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   246 done
   247 
   248 lemma plus_list_ub2:
   249   "[| semilat(A,r,f); set xs <= A; set ys <= A; size xs = size ys |]
   250   ==> ys <=[r] xs +[f] ys"
   251 apply (unfold unfold_lesub_list)
   252 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   253 done 
   254 
   255 lemma plus_list_lub [rule_format]:
   256   "semilat(A,r,f) ==> !xs ys zs. set xs <= A --> set ys <= A --> set zs <= A 
   257   --> size xs = n & size ys = n --> 
   258   xs <=[r] zs & ys <=[r] zs --> xs +[f] ys <=[r] zs"
   259 apply (unfold unfold_lesub_list)
   260 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   261 done 
   262 
   263 lemma list_update_incr [rule_format]:
   264   "[| semilat(A,r,f); x:A |] ==> set xs <= A --> 
   265   (!i. i<size xs --> xs <=[r] xs[i := x +_f xs!i])"
   266 apply (unfold unfold_lesub_list)
   267 apply (simp add: Listn.le_def list_all2_conv_all_nth)
   268 apply (induct xs)
   269  apply simp
   270 apply (simp add: in_list_Suc_iff)
   271 apply clarify
   272 apply (simp add: nth_Cons split: nat.split)
   273 done 
   274 
   275 lemma acc_le_listI [intro!]:
   276   "[| order r; acc r |] ==> acc(Listn.le r)"
   277 apply (unfold acc_def)
   278 apply (subgoal_tac
   279  "wf(UN n. {(ys,xs). size xs = n & size ys = n & xs <_(Listn.le r) ys})")
   280  apply (erule wf_subset)
   281  apply (blast intro: lesssub_list_impl_same_size)
   282 apply (rule wf_UN)
   283  prefer 2
   284  apply clarify
   285  apply (rename_tac m n)
   286  apply (case_tac "m=n")
   287   apply simp
   288  apply (rule conjI)
   289   apply (fast intro!: equals0I dest: not_sym)
   290  apply (fast intro!: equals0I dest: not_sym)
   291 apply clarify
   292 apply (rename_tac n)
   293 apply (induct_tac n)
   294  apply (simp add: lesssub_def cong: conj_cong)
   295 apply (rename_tac k)
   296 apply (simp add: wf_eq_minimal)
   297 apply (simp (no_asm) add: length_Suc_conv cong: conj_cong)
   298 apply clarify
   299 apply (rename_tac M m)
   300 apply (case_tac "? x xs. size xs = k & x#xs : M")
   301  prefer 2
   302  apply (erule thin_rl)
   303  apply (erule thin_rl)
   304  apply blast
   305 apply (erule_tac x = "{a. ? xs. size xs = k & a#xs:M}" in allE)
   306 apply (erule impE)
   307  apply blast
   308 apply (thin_tac "? x xs. ?P x xs")
   309 apply clarify
   310 apply (rename_tac maxA xs)
   311 apply (erule_tac x = "{ys. size ys = size xs & maxA#ys : M}" in allE)
   312 apply (erule impE)
   313  apply blast
   314 apply clarify
   315 apply (thin_tac "m : M")
   316 apply (thin_tac "maxA#xs : M")
   317 apply (rule bexI)
   318  prefer 2
   319  apply assumption
   320 apply clarify
   321 apply simp
   322 apply blast
   323 done 
   324 
   325 lemma closed_listI:
   326   "closed S f ==> closed (list n S) (map2 f)"
   327 apply (unfold closed_def)
   328 apply (induct n)
   329  apply simp
   330 apply clarify
   331 apply (simp add: in_list_Suc_iff)
   332 apply clarify
   333 apply simp
   334 done 
   335 
   336 
   337 lemma semilat_Listn_sl:
   338   "!!L. semilat L ==> semilat (Listn.sl n L)"
   339 apply (unfold Listn.sl_def)
   340 apply (simp (no_asm_simp) only: split_tupled_all)
   341 apply (simp (no_asm) only: semilat_Def split_conv)
   342 apply (rule conjI)
   343  apply simp
   344 apply (rule conjI)
   345  apply (simp only: semilatDclosedI closed_listI)
   346 apply (simp (no_asm) only: list_def)
   347 apply (simp (no_asm_simp) add: plus_list_ub1 plus_list_ub2 plus_list_lub)
   348 done  
   349 
   350 
   351 lemma coalesce_in_err_list [rule_format]:
   352   "!xes. xes : list n (err A) --> coalesce xes : err(list n A)"
   353 apply (induct n)
   354  apply simp
   355 apply clarify
   356 apply (simp add: in_list_Suc_iff)
   357 apply clarify
   358 apply (simp (no_asm) add: plussub_def Err.sup_def lift2_def split: err.split)
   359 apply force
   360 done 
   361 
   362 lemma lem: "!!x xs. x +_(op #) xs = x#xs"
   363   by (simp add: plussub_def)
   364 
   365 lemma coalesce_eq_OK1_D [rule_format]:
   366   "semilat(err A, Err.le r, lift2 f) ==> 
   367   !xs. xs : list n A --> (!ys. ys : list n A --> 
   368   (!zs. coalesce (xs +[f] ys) = OK zs --> xs <=[r] zs))"
   369 apply (induct n)
   370   apply simp
   371 apply clarify
   372 apply (simp add: in_list_Suc_iff)
   373 apply clarify
   374 apply (simp split: err.split_asm add: lem Err.sup_def lift2_def)
   375 apply (force simp add: semilat_le_err_OK1)
   376 done
   377 
   378 lemma coalesce_eq_OK2_D [rule_format]:
   379   "semilat(err A, Err.le r, lift2 f) ==> 
   380   !xs. xs : list n A --> (!ys. ys : list n A --> 
   381   (!zs. coalesce (xs +[f] ys) = OK zs --> ys <=[r] zs))"
   382 apply (induct n)
   383  apply simp
   384 apply clarify
   385 apply (simp add: in_list_Suc_iff)
   386 apply clarify
   387 apply (simp split: err.split_asm add: lem Err.sup_def lift2_def)
   388 apply (force simp add: semilat_le_err_OK2)
   389 done 
   390 
   391 lemma lift2_le_ub:
   392   "[| semilat(err A, Err.le r, lift2 f); x:A; y:A; x +_f y = OK z; 
   393       u:A; x <=_r u; y <=_r u |] ==> z <=_r u"
   394 apply (unfold semilat_Def plussub_def err_def)
   395 apply (simp add: lift2_def)
   396 apply clarify
   397 apply (rotate_tac -3)
   398 apply (erule thin_rl)
   399 apply (erule thin_rl)
   400 apply force
   401 done 
   402 
   403 lemma coalesce_eq_OK_ub_D [rule_format]:
   404   "semilat(err A, Err.le r, lift2 f) ==> 
   405   !xs. xs : list n A --> (!ys. ys : list n A --> 
   406   (!zs us. coalesce (xs +[f] ys) = OK zs & xs <=[r] us & ys <=[r] us 
   407            & us : list n A --> zs <=[r] us))"
   408 apply (induct n)
   409  apply simp
   410 apply clarify
   411 apply (simp add: in_list_Suc_iff)
   412 apply clarify
   413 apply (simp (no_asm_use) split: err.split_asm add: lem Err.sup_def lift2_def)
   414 apply clarify
   415 apply (rule conjI)
   416  apply (blast intro: lift2_le_ub)
   417 apply blast
   418 done 
   419 
   420 lemma lift2_eq_ErrD:
   421   "[| x +_f y = Err; semilat(err A, Err.le r, lift2 f); x:A; y:A |] 
   422   ==> ~(? u:A. x <=_r u & y <=_r u)"
   423   by (simp add: OK_plus_OK_eq_Err_conv [THEN iffD1])
   424 
   425 
   426 lemma coalesce_eq_Err_D [rule_format]:
   427   "[| semilat(err A, Err.le r, lift2 f) |] 
   428   ==> !xs. xs:list n A --> (!ys. ys:list n A --> 
   429       coalesce (xs +[f] ys) = Err --> 
   430       ~(? zs:list n A. xs <=[r] zs & ys <=[r] zs))"
   431 apply (induct n)
   432  apply simp
   433 apply clarify
   434 apply (simp add: in_list_Suc_iff)
   435 apply clarify
   436 apply (simp split: err.split_asm add: lem Err.sup_def lift2_def)
   437  apply (blast dest: lift2_eq_ErrD)
   438 apply blast
   439 done 
   440 
   441 lemma closed_err_lift2_conv:
   442   "closed (err A) (lift2 f) = (!x:A. !y:A. x +_f y : err A)"
   443 apply (unfold closed_def)
   444 apply (simp add: err_def)
   445 done 
   446 
   447 lemma closed_map2_list [rule_format]:
   448   "closed (err A) (lift2 f) ==> 
   449   !xs. xs : list n A --> (!ys. ys : list n A --> 
   450   map2 f xs ys : list n (err A))"
   451 apply (unfold map2_def)
   452 apply (induct n)
   453  apply simp
   454 apply clarify
   455 apply (simp add: in_list_Suc_iff)
   456 apply clarify
   457 apply (simp add: plussub_def closed_err_lift2_conv)
   458 done 
   459 
   460 lemma closed_lift2_sup:
   461   "closed (err A) (lift2 f) ==> 
   462   closed (err (list n A)) (lift2 (sup f))"
   463   by (fastsimp  simp add: closed_def plussub_def sup_def lift2_def
   464                           coalesce_in_err_list closed_map2_list
   465                 split: err.split)
   466 
   467 lemma err_semilat_sup:
   468   "err_semilat (A,r,f) ==> 
   469   err_semilat (list n A, Listn.le r, sup f)"
   470 apply (unfold Err.sl_def)
   471 apply (simp only: split_conv)
   472 apply (simp (no_asm) only: semilat_Def plussub_def)
   473 apply (simp (no_asm_simp) only: semilatDclosedI closed_lift2_sup)
   474 apply (rule conjI)
   475  apply (drule semilatDorderI)
   476  apply simp
   477 apply (simp (no_asm) only: unfold_lesub_err Err.le_def err_def sup_def lift2_def)
   478 apply (simp (no_asm_simp) add: coalesce_eq_OK1_D coalesce_eq_OK2_D split: err.split)
   479 apply (blast intro: coalesce_eq_OK_ub_D dest: coalesce_eq_Err_D)
   480 done 
   481 
   482 lemma err_semilat_upto_esl:
   483   "!!L. err_semilat L ==> err_semilat(upto_esl m L)"
   484 apply (unfold Listn.upto_esl_def)
   485 apply (simp (no_asm_simp) only: split_tupled_all)
   486 apply simp
   487 apply (fastsimp intro!: err_semilat_UnionI err_semilat_sup
   488                 dest: lesub_list_impl_same_size 
   489                 simp add: plussub_def Listn.sup_def)
   490 done
   491 
   492 end