src/HOL/Bali/State.thy
author wenzelm
Mon Mar 22 20:58:52 2010 +0100 (2010-03-22)
changeset 35898 c890a3835d15
parent 35431 8758fe1fc9f8
child 37956 ee939247b2fb
permissions -rw-r--r--
recovered header;
     1 (*  Title:      HOL/Bali/State.thy
     2     Author:     David von Oheimb
     3 *)
     4 header {* State for evaluation of Java expressions and statements *}
     5 
     6 theory State
     7 imports DeclConcepts
     8 begin
     9 
    10 text {*
    11 design issues:
    12 \begin{itemize}
    13 \item all kinds of objects (class instances, arrays, and class objects)
    14   are handeled via a general object abstraction
    15 \item the heap and the map for class objects are combined into a single table
    16   @{text "(recall (loc, obj) table \<times> (qtname, obj) table  ~=  (loc + qtname, obj) table)"}
    17 \end{itemize}
    18 *}
    19 
    20 section "objects"
    21 
    22 datatype  obj_tag =     --{* tag for generic object   *}
    23           CInst qtname  --{* class instance           *}
    24         | Arr  ty int   --{* array with component type and length *}
    25     --{* | CStat qtname   the tag is irrelevant for a class object,
    26                            i.e. the static fields of a class,
    27                            since its type is given already by the reference to 
    28                            it (see below) *}
    29 
    30 types   vn   = "fspec + int"                    --{* variable name      *}
    31 record  obj  = 
    32           tag :: "obj_tag"                      --{* generalized object *}
    33           "values" :: "(vn, val) table"      
    34 
    35 translations 
    36   (type) "fspec" <= (type) "vname \<times> qtname" 
    37   (type) "vn"    <= (type) "fspec + int"
    38   (type) "obj"   <= (type) "\<lparr>tag::obj_tag, values::vn \<Rightarrow> val option\<rparr>"
    39   (type) "obj"   <= (type) "\<lparr>tag::obj_tag, values::vn \<Rightarrow> val option,\<dots>::'a\<rparr>"
    40 
    41 definition the_Arr :: "obj option \<Rightarrow> ty \<times> int \<times> (vn, val) table" where
    42  "the_Arr obj \<equiv> SOME (T,k,t). obj = Some \<lparr>tag=Arr T k,values=t\<rparr>"
    43 
    44 lemma the_Arr_Arr [simp]: "the_Arr (Some \<lparr>tag=Arr T k,values=cs\<rparr>) = (T,k,cs)"
    45 apply (auto simp: the_Arr_def)
    46 done
    47 
    48 lemma the_Arr_Arr1 [simp,intro,dest]:
    49  "\<lbrakk>tag obj = Arr T k\<rbrakk> \<Longrightarrow> the_Arr (Some obj) = (T,k,values obj)"
    50 apply (auto simp add: the_Arr_def)
    51 done
    52 
    53 definition upd_obj :: "vn \<Rightarrow> val \<Rightarrow> obj \<Rightarrow> obj" where 
    54  "upd_obj n v \<equiv> \<lambda> obj . obj \<lparr>values:=(values obj)(n\<mapsto>v)\<rparr>"
    55 
    56 lemma upd_obj_def2 [simp]: 
    57   "upd_obj n v obj = obj \<lparr>values:=(values obj)(n\<mapsto>v)\<rparr>" 
    58 apply (auto simp: upd_obj_def)
    59 done
    60 
    61 definition obj_ty :: "obj \<Rightarrow> ty" where
    62  "obj_ty obj    \<equiv> case tag obj of 
    63                     CInst C \<Rightarrow> Class C 
    64                   | Arr T k \<Rightarrow> T.[]"
    65 
    66 lemma obj_ty_eq [intro!]: "obj_ty \<lparr>tag=oi,values=x\<rparr> = obj_ty \<lparr>tag=oi,values=y\<rparr>" 
    67 by (simp add: obj_ty_def)
    68 
    69 
    70 lemma obj_ty_eq1 [intro!,dest]: 
    71   "tag obj = tag obj' \<Longrightarrow> obj_ty obj = obj_ty obj'" 
    72 by (simp add: obj_ty_def)
    73 
    74 lemma obj_ty_cong [simp]: 
    75   "obj_ty (obj \<lparr>values:=vs\<rparr>) = obj_ty obj" 
    76 by auto
    77 
    78 lemma obj_ty_CInst [simp]: 
    79  "obj_ty \<lparr>tag=CInst C,values=vs\<rparr> = Class C" 
    80 by (simp add: obj_ty_def)
    81 
    82 lemma obj_ty_CInst1 [simp,intro!,dest]: 
    83  "\<lbrakk>tag obj = CInst C\<rbrakk> \<Longrightarrow> obj_ty obj = Class C" 
    84 by (simp add: obj_ty_def)
    85 
    86 lemma obj_ty_Arr [simp]: 
    87  "obj_ty \<lparr>tag=Arr T i,values=vs\<rparr> = T.[]"
    88 by (simp add: obj_ty_def)
    89 
    90 lemma obj_ty_Arr1 [simp,intro!,dest]: 
    91  "\<lbrakk>tag obj = Arr T i\<rbrakk> \<Longrightarrow> obj_ty obj = T.[]"
    92 by (simp add: obj_ty_def)
    93 
    94 lemma obj_ty_widenD: 
    95  "G\<turnstile>obj_ty obj\<preceq>RefT t \<Longrightarrow> (\<exists>C. tag obj = CInst C) \<or> (\<exists>T k. tag obj = Arr T k)"
    96 apply (unfold obj_ty_def)
    97 apply (auto split add: obj_tag.split_asm)
    98 done
    99 
   100 definition obj_class :: "obj \<Rightarrow> qtname" where
   101  "obj_class obj \<equiv> case tag obj of 
   102                     CInst C \<Rightarrow> C 
   103                   | Arr T k \<Rightarrow> Object"
   104 
   105 
   106 lemma obj_class_CInst [simp]: "obj_class \<lparr>tag=CInst C,values=vs\<rparr> = C" 
   107 by (auto simp: obj_class_def)
   108 
   109 lemma obj_class_CInst1 [simp,intro!,dest]: 
   110   "tag obj = CInst C \<Longrightarrow> obj_class obj = C" 
   111 by (auto simp: obj_class_def)
   112 
   113 lemma obj_class_Arr [simp]: "obj_class \<lparr>tag=Arr T k,values=vs\<rparr> = Object" 
   114 by (auto simp: obj_class_def)
   115 
   116 lemma obj_class_Arr1 [simp,intro!,dest]: 
   117  "tag obj = Arr T k \<Longrightarrow> obj_class obj = Object" 
   118 by (auto simp: obj_class_def)
   119 
   120 lemma obj_ty_obj_class: "G\<turnstile>obj_ty obj\<preceq> Class statC = G\<turnstile>obj_class obj \<preceq>\<^sub>C statC"
   121 apply (case_tac "tag obj")
   122 apply (auto simp add: obj_ty_def obj_class_def)
   123 apply (case_tac "statC = Object")
   124 apply (auto dest: widen_Array_Class)
   125 done
   126 
   127 section "object references"
   128 
   129 types oref = "loc + qtname"         --{* generalized object reference *}
   130 syntax
   131   Heap  :: "loc   \<Rightarrow> oref"
   132   Stat  :: "qtname \<Rightarrow> oref"
   133 
   134 translations
   135   "Heap" => "CONST Inl"
   136   "Stat" => "CONST Inr"
   137   (type) "oref" <= (type) "loc + qtname"
   138 
   139 definition fields_table :: "prog \<Rightarrow> qtname \<Rightarrow> (fspec \<Rightarrow> field \<Rightarrow> bool)  \<Rightarrow> (fspec, ty) table" where
   140  "fields_table G C P 
   141     \<equiv> Option.map type \<circ> table_of (filter (split P) (DeclConcepts.fields G C))"
   142 
   143 lemma fields_table_SomeI: 
   144 "\<lbrakk>table_of (DeclConcepts.fields G C) n = Some f; P n f\<rbrakk> 
   145  \<Longrightarrow> fields_table G C P n = Some (type f)"
   146 apply (unfold fields_table_def)
   147 apply clarsimp
   148 apply (rule exI)
   149 apply (rule conjI)
   150 apply (erule map_of_filter_in)
   151 apply assumption
   152 apply simp
   153 done
   154 
   155 (* unused *)
   156 lemma fields_table_SomeD': "fields_table G C P fn = Some T \<Longrightarrow>  
   157   \<exists>f. (fn,f)\<in>set(DeclConcepts.fields G C) \<and> type f = T"
   158 apply (unfold fields_table_def)
   159 apply clarsimp
   160 apply (drule map_of_SomeD)
   161 apply auto
   162 done
   163 
   164 lemma fields_table_SomeD: 
   165 "\<lbrakk>fields_table G C P fn = Some T; unique (DeclConcepts.fields G C)\<rbrakk> \<Longrightarrow>  
   166   \<exists>f. table_of (DeclConcepts.fields G C) fn = Some f \<and> type f = T"
   167 apply (unfold fields_table_def)
   168 apply clarsimp
   169 apply (rule exI)
   170 apply (rule conjI)
   171 apply (erule table_of_filter_unique_SomeD)
   172 apply assumption
   173 apply simp
   174 done
   175 
   176 definition in_bounds :: "int \<Rightarrow> int \<Rightarrow> bool" ("(_/ in'_bounds _)" [50, 51] 50) where
   177  "i in_bounds k \<equiv> 0 \<le> i \<and> i < k"
   178 
   179 definition arr_comps :: "'a \<Rightarrow> int \<Rightarrow> int \<Rightarrow> 'a option" where
   180  "arr_comps T k \<equiv> \<lambda>i. if i in_bounds k then Some T else None"
   181   
   182 definition var_tys       :: "prog \<Rightarrow> obj_tag \<Rightarrow> oref \<Rightarrow> (vn, ty) table" where
   183 "var_tys G oi r 
   184   \<equiv> case r of 
   185       Heap a \<Rightarrow> (case oi of 
   186                    CInst C \<Rightarrow> fields_table G C (\<lambda>n f. \<not>static f) (+) empty
   187                  | Arr T k \<Rightarrow> empty (+) arr_comps T k)
   188     | Stat C \<Rightarrow> fields_table G C (\<lambda>fn f. declclassf fn = C \<and> static f) 
   189                 (+) empty"
   190 
   191 lemma var_tys_Some_eq: 
   192  "var_tys G oi r n = Some T 
   193   = (case r of 
   194        Inl a \<Rightarrow> (case oi of  
   195                    CInst C \<Rightarrow> (\<exists>nt. n = Inl nt \<and> fields_table G C (\<lambda>n f. 
   196                                \<not>static f) nt = Some T)  
   197                  | Arr t k \<Rightarrow> (\<exists> i. n = Inr i  \<and> i in_bounds k \<and> t = T))  
   198      | Inr C \<Rightarrow> (\<exists>nt. n = Inl nt \<and> 
   199                  fields_table G C (\<lambda>fn f. declclassf fn = C \<and> static f) nt 
   200                   = Some T))"
   201 apply (unfold var_tys_def arr_comps_def)
   202 apply (force split add: sum.split_asm sum.split obj_tag.split)
   203 done
   204 
   205 
   206 section "stores"
   207 
   208 types   globs               --{* global variables: heap and static variables *}
   209         = "(oref , obj) table"
   210         heap
   211         = "(loc  , obj) table"
   212 (*      locals                   
   213         = "(lname, val) table" *) (* defined in Value.thy local variables *)
   214 
   215 translations
   216  (type) "globs"  <= (type) "(oref , obj) table"
   217  (type) "heap"   <= (type) "(loc  , obj) table"
   218 (*  (type) "locals" <= (type) "(lname, val) table" *)
   219 
   220 datatype st = (* pure state, i.e. contents of all variables *)
   221          st globs locals
   222 
   223 subsection "access"
   224 
   225 definition globs :: "st \<Rightarrow> globs" where
   226  "globs  \<equiv> st_case (\<lambda>g l. g)"
   227   
   228 definition locals :: "st \<Rightarrow> locals" where
   229  "locals \<equiv> st_case (\<lambda>g l. l)"
   230 
   231 definition heap   :: "st \<Rightarrow> heap" where
   232  "heap s \<equiv> globs s \<circ> Heap"
   233 
   234 
   235 lemma globs_def2 [simp]: " globs (st g l) = g"
   236 by (simp add: globs_def)
   237 
   238 lemma locals_def2 [simp]: "locals (st g l) = l"
   239 by (simp add: locals_def)
   240 
   241 lemma heap_def2 [simp]:  "heap s a=globs s (Heap a)"
   242 by (simp add: heap_def)
   243 
   244 
   245 abbreviation val_this :: "st \<Rightarrow> val"
   246   where "val_this s == the (locals s This)"
   247 
   248 abbreviation lookup_obj :: "st \<Rightarrow> val \<Rightarrow> obj"
   249   where "lookup_obj s a' == the (heap s (the_Addr a'))"
   250 
   251 subsection "memory allocation"
   252 
   253 definition new_Addr :: "heap \<Rightarrow> loc option" where
   254  "new_Addr h   \<equiv> if (\<forall>a. h a \<noteq> None) then None else Some (SOME a. h a = None)"
   255 
   256 lemma new_AddrD: "new_Addr h = Some a \<Longrightarrow> h a = None"
   257 apply (auto simp add: new_Addr_def)
   258 apply (erule someI) 
   259 done
   260 
   261 lemma new_AddrD2: "new_Addr h = Some a \<Longrightarrow> \<forall>b. h b \<noteq> None \<longrightarrow> b \<noteq> a"
   262 apply (drule new_AddrD)
   263 apply auto
   264 done
   265 
   266 lemma new_Addr_SomeI: "h a = None \<Longrightarrow> \<exists>b. new_Addr h = Some b \<and> h b = None"
   267 apply (simp add: new_Addr_def)
   268 apply (fast intro: someI2)
   269 done
   270 
   271 
   272 subsection "initialization"
   273 
   274 abbreviation init_vals :: "('a, ty) table \<Rightarrow> ('a, val) table"
   275   where "init_vals vs == Option.map default_val \<circ> vs"
   276 
   277 lemma init_arr_comps_base [simp]: "init_vals (arr_comps T 0) = empty"
   278 apply (unfold arr_comps_def in_bounds_def)
   279 apply (rule ext)
   280 apply auto
   281 done
   282 
   283 lemma init_arr_comps_step [simp]: 
   284 "0 < j \<Longrightarrow> init_vals (arr_comps T  j    ) =  
   285            init_vals (arr_comps T (j - 1))(j - 1\<mapsto>default_val T)"
   286 apply (unfold arr_comps_def in_bounds_def)
   287 apply (rule ext)
   288 apply auto
   289 done
   290 
   291 subsection "update"
   292 
   293 definition gupd :: "oref  \<Rightarrow> obj \<Rightarrow> st \<Rightarrow> st" ("gupd'(_\<mapsto>_')"[10,10]1000) where
   294  "gupd r obj  \<equiv> st_case (\<lambda>g l. st (g(r\<mapsto>obj)) l)"
   295 
   296 definition lupd       :: "lname \<Rightarrow> val \<Rightarrow> st \<Rightarrow> st" ("lupd'(_\<mapsto>_')"[10,10]1000) where
   297  "lupd vn v   \<equiv> st_case (\<lambda>g l. st g (l(vn\<mapsto>v)))"
   298 
   299 definition upd_gobj   :: "oref \<Rightarrow> vn \<Rightarrow> val \<Rightarrow> st \<Rightarrow> st" where
   300  "upd_gobj r n v \<equiv> st_case (\<lambda>g l. st (chg_map (upd_obj n v) r g) l)"
   301 
   302 definition set_locals  :: "locals \<Rightarrow> st \<Rightarrow> st" where
   303  "set_locals l \<equiv> st_case (\<lambda>g l'. st g l)"
   304 
   305 definition init_obj    :: "prog \<Rightarrow> obj_tag \<Rightarrow> oref \<Rightarrow> st \<Rightarrow> st" where
   306  "init_obj G oi r \<equiv> gupd(r\<mapsto>\<lparr>tag=oi, values=init_vals (var_tys G oi r)\<rparr>)"
   307 
   308 abbreviation
   309   init_class_obj :: "prog \<Rightarrow> qtname \<Rightarrow> st \<Rightarrow> st"
   310   where "init_class_obj G C == init_obj G undefined (Inr C)"
   311 
   312 lemma gupd_def2 [simp]: "gupd(r\<mapsto>obj) (st g l) = st (g(r\<mapsto>obj)) l"
   313 apply (unfold gupd_def)
   314 apply (simp (no_asm))
   315 done
   316 
   317 lemma lupd_def2 [simp]: "lupd(vn\<mapsto>v) (st g l) = st g (l(vn\<mapsto>v))"
   318 apply (unfold lupd_def)
   319 apply (simp (no_asm))
   320 done
   321 
   322 lemma globs_gupd [simp]: "globs  (gupd(r\<mapsto>obj) s) = globs s(r\<mapsto>obj)"
   323 apply (induct "s")
   324 by (simp add: gupd_def)
   325 
   326 lemma globs_lupd [simp]: "globs  (lupd(vn\<mapsto>v ) s) = globs  s"
   327 apply (induct "s")
   328 by (simp add: lupd_def)
   329 
   330 lemma locals_gupd [simp]: "locals (gupd(r\<mapsto>obj) s) = locals s"
   331 apply (induct "s")
   332 by (simp add: gupd_def)
   333 
   334 lemma locals_lupd [simp]: "locals (lupd(vn\<mapsto>v ) s) = locals s(vn\<mapsto>v )"
   335 apply (induct "s")
   336 by (simp add: lupd_def)
   337 
   338 lemma globs_upd_gobj_new [rule_format (no_asm), simp]: 
   339   "globs s r = None \<longrightarrow> globs (upd_gobj r n v s) = globs s"
   340 apply (unfold upd_gobj_def)
   341 apply (induct "s")
   342 apply auto
   343 done
   344 
   345 lemma globs_upd_gobj_upd [rule_format (no_asm), simp]: 
   346 "globs s r=Some obj\<longrightarrow> globs (upd_gobj r n v s) = globs s(r\<mapsto>upd_obj n v obj)"
   347 apply (unfold upd_gobj_def)
   348 apply (induct "s")
   349 apply auto
   350 done
   351 
   352 lemma locals_upd_gobj [simp]: "locals (upd_gobj r n v s) = locals s"
   353 apply (induct "s")
   354 by (simp add: upd_gobj_def) 
   355 
   356 
   357 lemma globs_init_obj [simp]: "globs (init_obj G oi r s) t =  
   358   (if t=r then Some \<lparr>tag=oi,values=init_vals (var_tys G oi r)\<rparr> else globs s t)"
   359 apply (unfold init_obj_def)
   360 apply (simp (no_asm))
   361 done
   362 
   363 lemma locals_init_obj [simp]: "locals (init_obj G oi r s) = locals s"
   364 by (simp add: init_obj_def)
   365   
   366 lemma surjective_st [simp]: "st (globs s) (locals s) = s"
   367 apply (induct "s")
   368 by auto
   369 
   370 lemma surjective_st_init_obj: 
   371  "st (globs (init_obj G oi r s)) (locals s) = init_obj G oi r s"
   372 apply (subst locals_init_obj [THEN sym])
   373 apply (rule surjective_st)
   374 done
   375 
   376 lemma heap_heap_upd [simp]: 
   377   "heap (st (g(Inl a\<mapsto>obj)) l) = heap (st g l)(a\<mapsto>obj)"
   378 apply (rule ext)
   379 apply (simp (no_asm))
   380 done
   381 lemma heap_stat_upd [simp]: "heap (st (g(Inr C\<mapsto>obj)) l) = heap (st g l)"
   382 apply (rule ext)
   383 apply (simp (no_asm))
   384 done
   385 lemma heap_local_upd [simp]: "heap (st g (l(vn\<mapsto>v))) = heap (st g l)"
   386 apply (rule ext)
   387 apply (simp (no_asm))
   388 done
   389 
   390 lemma heap_gupd_Heap [simp]: "heap (gupd(Heap a\<mapsto>obj) s) = heap s(a\<mapsto>obj)"
   391 apply (rule ext)
   392 apply (simp (no_asm))
   393 done
   394 lemma heap_gupd_Stat [simp]: "heap (gupd(Stat C\<mapsto>obj) s) = heap s"
   395 apply (rule ext)
   396 apply (simp (no_asm))
   397 done
   398 lemma heap_lupd [simp]: "heap (lupd(vn\<mapsto>v) s) = heap s"
   399 apply (rule ext)
   400 apply (simp (no_asm))
   401 done
   402 
   403 lemma heap_upd_gobj_Stat [simp]: "heap (upd_gobj (Stat C) n v s) = heap s"
   404 apply (rule ext)
   405 apply (simp (no_asm))
   406 apply (case_tac "globs s (Stat C)")
   407 apply  auto
   408 done
   409 
   410 lemma set_locals_def2 [simp]: "set_locals l (st g l') = st g l"
   411 apply (unfold set_locals_def)
   412 apply (simp (no_asm))
   413 done
   414 
   415 lemma set_locals_id [simp]: "set_locals (locals s) s = s"
   416 apply (unfold set_locals_def)
   417 apply (induct_tac "s")
   418 apply (simp (no_asm))
   419 done
   420 
   421 lemma set_set_locals [simp]: "set_locals l (set_locals l' s) = set_locals l s"
   422 apply (unfold set_locals_def)
   423 apply (induct_tac "s")
   424 apply (simp (no_asm))
   425 done
   426 
   427 lemma locals_set_locals [simp]: "locals (set_locals l s) = l"
   428 apply (unfold set_locals_def)
   429 apply (induct_tac "s")
   430 apply (simp (no_asm))
   431 done
   432 
   433 lemma globs_set_locals [simp]: "globs (set_locals l s) = globs s"
   434 apply (unfold set_locals_def)
   435 apply (induct_tac "s")
   436 apply (simp (no_asm))
   437 done
   438 
   439 lemma heap_set_locals [simp]: "heap (set_locals l s) = heap s"
   440 apply (unfold heap_def)
   441 apply (induct_tac "s")
   442 apply (simp (no_asm))
   443 done
   444 
   445 
   446 section "abrupt completion"
   447 
   448 
   449 
   450 consts
   451 
   452   the_Xcpt :: "abrupt \<Rightarrow> xcpt"
   453   the_Jump :: "abrupt => jump"
   454   the_Loc  :: "xcpt \<Rightarrow> loc"
   455   the_Std  :: "xcpt \<Rightarrow> xname"
   456 
   457 primrec "the_Xcpt (Xcpt x) = x"
   458 primrec "the_Jump (Jump j) = j"
   459 primrec "the_Loc (Loc a) = a"
   460 primrec "the_Std (Std x) = x"
   461 
   462 
   463         
   464 
   465 definition abrupt_if :: "bool \<Rightarrow> abopt \<Rightarrow> abopt \<Rightarrow> abopt" where
   466  "abrupt_if c x' x \<equiv> if c \<and> (x = None) then x' else x"
   467 
   468 lemma abrupt_if_True_None [simp]: "abrupt_if True x None = x"
   469 by (simp add: abrupt_if_def)
   470 
   471 lemma abrupt_if_True_not_None [simp]: "x \<noteq> None \<Longrightarrow> abrupt_if True x y \<noteq> None"
   472 by (simp add: abrupt_if_def)
   473 
   474 lemma abrupt_if_False [simp]: "abrupt_if False x y = y"
   475 by (simp add: abrupt_if_def)
   476 
   477 lemma abrupt_if_Some [simp]: "abrupt_if c x (Some y) = Some y"
   478 by (simp add: abrupt_if_def)
   479 
   480 lemma abrupt_if_not_None [simp]: "y \<noteq> None \<Longrightarrow> abrupt_if c x y = y"
   481 apply (simp add: abrupt_if_def)
   482 by auto
   483 
   484 
   485 lemma split_abrupt_if: 
   486 "P (abrupt_if c x' x) = 
   487       ((c \<and> x = None \<longrightarrow> P x') \<and> (\<not> (c \<and> x = None) \<longrightarrow> P x))"
   488 apply (unfold abrupt_if_def)
   489 apply (split split_if)
   490 apply auto
   491 done
   492 
   493 abbreviation raise_if :: "bool \<Rightarrow> xname \<Rightarrow> abopt \<Rightarrow> abopt"
   494   where "raise_if c xn == abrupt_if c (Some (Xcpt (Std xn)))"
   495 
   496 abbreviation np :: "val \<Rightarrow> abopt \<Rightarrow> abopt"
   497   where "np v == raise_if (v = Null) NullPointer"
   498 
   499 abbreviation check_neg :: "val \<Rightarrow> abopt \<Rightarrow> abopt"
   500   where "check_neg i' == raise_if (the_Intg i'<0) NegArrSize"
   501 
   502 abbreviation error_if :: "bool \<Rightarrow> error \<Rightarrow> abopt \<Rightarrow> abopt"
   503   where "error_if c e == abrupt_if c (Some (Error e))"
   504 
   505 lemma raise_if_None [simp]: "(raise_if c x y = None) = (\<not>c \<and> y = None)"
   506 apply (simp add: abrupt_if_def)
   507 by auto
   508 declare raise_if_None [THEN iffD1, dest!]
   509 
   510 lemma if_raise_if_None [simp]: 
   511   "((if b then y else raise_if c x y) = None) = ((c \<longrightarrow> b) \<and> y = None)"
   512 apply (simp add: abrupt_if_def)
   513 apply auto
   514 done
   515 
   516 lemma raise_if_SomeD [dest!]:
   517   "raise_if c x y = Some z \<Longrightarrow> c \<and> z=(Xcpt (Std x)) \<and> y=None \<or> (y=Some z)"
   518 apply (case_tac y)
   519 apply (case_tac c)
   520 apply (simp add: abrupt_if_def)
   521 apply (simp add: abrupt_if_def)
   522 apply auto
   523 done
   524 
   525 lemma error_if_None [simp]: "(error_if c e y = None) = (\<not>c \<and> y = None)"
   526 apply (simp add: abrupt_if_def)
   527 by auto
   528 declare error_if_None [THEN iffD1, dest!]
   529 
   530 lemma if_error_if_None [simp]: 
   531   "((if b then y else error_if c e y) = None) = ((c \<longrightarrow> b) \<and> y = None)"
   532 apply (simp add: abrupt_if_def)
   533 apply auto
   534 done
   535 
   536 lemma error_if_SomeD [dest!]:
   537   "error_if c e y = Some z \<Longrightarrow> c \<and> z=(Error e) \<and> y=None \<or> (y=Some z)"
   538 apply (case_tac y)
   539 apply (case_tac c)
   540 apply (simp add: abrupt_if_def)
   541 apply (simp add: abrupt_if_def)
   542 apply auto
   543 done
   544 
   545 definition absorb :: "jump \<Rightarrow> abopt \<Rightarrow> abopt" where
   546   "absorb j a \<equiv> if a=Some (Jump j) then None else a"
   547 
   548 lemma absorb_SomeD [dest!]: "absorb j a = Some x \<Longrightarrow> a = Some x"
   549 by (auto simp add: absorb_def)
   550 
   551 lemma absorb_same [simp]: "absorb j (Some (Jump j)) = None"
   552 by (auto simp add: absorb_def)
   553 
   554 lemma absorb_other [simp]: "a \<noteq> Some (Jump j) \<Longrightarrow> absorb j a = a"
   555 by (auto simp add: absorb_def)
   556 
   557 lemma absorb_Some_NoneD: "absorb j (Some abr) = None \<Longrightarrow> abr = Jump j"
   558   by (simp add: absorb_def)
   559 
   560 lemma absorb_Some_JumpD: "absorb j s = Some (Jump j') \<Longrightarrow> j'\<noteq>j"
   561   by (simp add: absorb_def)
   562 
   563 
   564 section "full program state"
   565 
   566 types
   567   state = "abopt \<times> st"          --{* state including abruption information *}
   568 
   569 translations
   570   (type) "abopt" <= (type) "abrupt option"
   571   (type) "state" <= (type) "abopt \<times> st"
   572 
   573 abbreviation
   574   Norm :: "st \<Rightarrow> state"
   575   where "Norm s == (None, s)"
   576 
   577 abbreviation (input)
   578   abrupt :: "state \<Rightarrow> abopt"
   579   where "abrupt == fst"
   580 
   581 abbreviation (input)
   582   store :: "state \<Rightarrow> st"
   583   where "store == snd"
   584 
   585 lemma single_stateE: "\<forall>Z. Z = (s::state) \<Longrightarrow> False"
   586 apply (erule_tac x = "(Some k,y)" in all_dupE)
   587 apply (erule_tac x = "(None,y)" in allE)
   588 apply clarify
   589 done
   590 
   591 lemma state_not_single: "All (op = (x::state)) \<Longrightarrow> R"
   592 apply (drule_tac x = "(if abrupt x = None then Some ?x else None,?y)" in spec)
   593 apply clarsimp
   594 done
   595 
   596 definition normal :: "state \<Rightarrow> bool" where
   597  "normal \<equiv> \<lambda>s. abrupt s = None"
   598 
   599 lemma normal_def2 [simp]: "normal s = (abrupt s = None)"
   600 apply (unfold normal_def)
   601 apply (simp (no_asm))
   602 done
   603 
   604 definition heap_free :: "nat \<Rightarrow> state \<Rightarrow> bool" where
   605  "heap_free n \<equiv> \<lambda>s. atleast_free (heap (store s)) n"
   606 
   607 lemma heap_free_def2 [simp]: "heap_free n s = atleast_free (heap (store s)) n"
   608 apply (unfold heap_free_def)
   609 apply simp
   610 done
   611 
   612 subsection "update"
   613 
   614 definition abupd :: "(abopt \<Rightarrow> abopt) \<Rightarrow> state \<Rightarrow> state" where
   615  "abupd f \<equiv> prod_fun f id"
   616 
   617 definition supd     :: "(st \<Rightarrow> st) \<Rightarrow> state \<Rightarrow> state" where
   618  "supd \<equiv> prod_fun id"
   619   
   620 lemma abupd_def2 [simp]: "abupd f (x,s) = (f x,s)"
   621 by (simp add: abupd_def)
   622 
   623 lemma abupd_abrupt_if_False [simp]: "\<And> s. abupd (abrupt_if False xo) s = s"
   624 by simp
   625 
   626 lemma supd_def2 [simp]: "supd f (x,s) = (x,f s)"
   627 by (simp add: supd_def)
   628 
   629 lemma supd_lupd [simp]: 
   630  "\<And> s. supd (lupd vn v ) s = (abrupt s,lupd vn v (store s))"
   631 apply (simp (no_asm_simp) only: split_tupled_all)
   632 apply (simp (no_asm))
   633 done
   634 
   635 
   636 lemma supd_gupd [simp]: 
   637  "\<And> s. supd (gupd r obj) s = (abrupt s,gupd r obj (store s))"
   638 apply (simp (no_asm_simp) only: split_tupled_all)
   639 apply (simp (no_asm))
   640 done
   641 
   642 lemma supd_init_obj [simp]: 
   643  "supd (init_obj G oi r) s = (abrupt s,init_obj G oi r (store s))"
   644 apply (unfold init_obj_def)
   645 apply (simp (no_asm))
   646 done
   647 
   648 lemma abupd_store_invariant [simp]: "store (abupd f s) = store s"
   649   by (cases s) simp
   650 
   651 lemma supd_abrupt_invariant [simp]: "abrupt (supd f s) = abrupt s"
   652   by (cases s) simp
   653 
   654 abbreviation set_lvars :: "locals \<Rightarrow> state \<Rightarrow> state"
   655   where "set_lvars l == supd (set_locals l)"
   656 
   657 abbreviation restore_lvars :: "state  \<Rightarrow> state \<Rightarrow> state"
   658   where "restore_lvars s' s == set_lvars (locals (store s')) s"
   659 
   660 lemma set_set_lvars [simp]: "\<And> s. set_lvars l (set_lvars l' s) = set_lvars l s"
   661 apply (simp (no_asm_simp) only: split_tupled_all)
   662 apply (simp (no_asm))
   663 done
   664 
   665 lemma set_lvars_id [simp]: "\<And> s. set_lvars (locals (store s)) s = s"
   666 apply (simp (no_asm_simp) only: split_tupled_all)
   667 apply (simp (no_asm))
   668 done
   669 
   670 section "initialisation test"
   671 
   672 definition inited :: "qtname \<Rightarrow> globs \<Rightarrow> bool" where
   673  "inited C g \<equiv> g (Stat C) \<noteq> None"
   674 
   675 definition initd    :: "qtname \<Rightarrow> state \<Rightarrow> bool" where
   676  "initd C \<equiv> inited C \<circ> globs \<circ> store"
   677 
   678 lemma not_inited_empty [simp]: "\<not>inited C empty"
   679 apply (unfold inited_def)
   680 apply (simp (no_asm))
   681 done
   682 
   683 lemma inited_gupdate [simp]: "inited C (g(r\<mapsto>obj)) = (inited C g \<or> r = Stat C)"
   684 apply (unfold inited_def)
   685 apply (auto split add: st.split)
   686 done
   687 
   688 lemma inited_init_class_obj [intro!]: "inited C (globs (init_class_obj G C s))"
   689 apply (unfold inited_def)
   690 apply (simp (no_asm))
   691 done
   692 
   693 lemma not_initedD: "\<not> inited C g \<Longrightarrow> g (Stat C) = None"
   694 apply (unfold inited_def)
   695 apply (erule notnotD)
   696 done
   697 
   698 lemma initedD: "inited C g \<Longrightarrow> \<exists> obj. g (Stat C) = Some obj"
   699 apply (unfold inited_def)
   700 apply auto
   701 done
   702 
   703 lemma initd_def2 [simp]: "initd C s = inited C (globs (store s))"
   704 apply (unfold initd_def)
   705 apply (simp (no_asm))
   706 done
   707 
   708 section {* @{text error_free} *}
   709 definition error_free :: "state \<Rightarrow> bool" where
   710 "error_free s \<equiv> \<not> (\<exists> err. abrupt s = Some (Error err))"
   711 
   712 lemma error_free_Norm [simp,intro]: "error_free (Norm s)"
   713 by (simp add: error_free_def)
   714 
   715 lemma error_free_normal [simp,intro]: "normal s \<Longrightarrow> error_free s"
   716 by (simp add: error_free_def)
   717 
   718 lemma error_free_Xcpt [simp]: "error_free (Some (Xcpt x),s)"
   719 by (simp add: error_free_def)
   720 
   721 lemma error_free_Jump [simp,intro]: "error_free (Some (Jump j),s)"
   722 by (simp add: error_free_def)
   723 
   724 lemma error_free_Error [simp]: "error_free (Some (Error e),s) = False"
   725 by (simp add: error_free_def)  
   726 
   727 lemma error_free_Some [simp,intro]: 
   728  "\<not> (\<exists> err. x=Error err) \<Longrightarrow> error_free ((Some x),s)"
   729 by (auto simp add: error_free_def)
   730 
   731 lemma error_free_abupd_absorb [simp,intro]: 
   732  "error_free s \<Longrightarrow> error_free (abupd (absorb j) s)"
   733 by (cases s) 
   734    (auto simp add: error_free_def absorb_def
   735          split: split_if_asm)
   736 
   737 lemma error_free_absorb [simp,intro]: 
   738  "error_free (a,s) \<Longrightarrow> error_free (absorb j a, s)"
   739 by (auto simp add: error_free_def absorb_def
   740             split: split_if_asm)
   741 
   742 lemma error_free_abrupt_if [simp,intro]:
   743 "\<lbrakk>error_free s; \<not> (\<exists> err. x=Error err)\<rbrakk>
   744  \<Longrightarrow> error_free (abupd (abrupt_if p (Some x)) s)"
   745 by (cases s)
   746    (auto simp add: abrupt_if_def
   747             split: split_if)
   748 
   749 lemma error_free_abrupt_if1 [simp,intro]:
   750 "\<lbrakk>error_free (a,s); \<not> (\<exists> err. x=Error err)\<rbrakk>
   751  \<Longrightarrow> error_free (abrupt_if p (Some x) a, s)"
   752 by  (auto simp add: abrupt_if_def
   753             split: split_if)
   754 
   755 lemma error_free_abrupt_if_Xcpt [simp,intro]:
   756  "error_free s 
   757   \<Longrightarrow> error_free (abupd (abrupt_if p (Some (Xcpt x))) s)"
   758 by simp 
   759 
   760 lemma error_free_abrupt_if_Xcpt1 [simp,intro]:
   761  "error_free (a,s) 
   762   \<Longrightarrow> error_free (abrupt_if p (Some (Xcpt x)) a, s)" 
   763 by simp 
   764 
   765 lemma error_free_abrupt_if_Jump [simp,intro]:
   766  "error_free s 
   767   \<Longrightarrow> error_free (abupd (abrupt_if p (Some (Jump j))) s)" 
   768 by simp
   769 
   770 lemma error_free_abrupt_if_Jump1 [simp,intro]:
   771  "error_free (a,s) 
   772   \<Longrightarrow> error_free (abrupt_if p (Some (Jump j)) a, s)" 
   773 by simp
   774 
   775 lemma error_free_raise_if [simp,intro]:
   776  "error_free s \<Longrightarrow> error_free (abupd (raise_if p x) s)"
   777 by simp 
   778 
   779 lemma error_free_raise_if1 [simp,intro]:
   780  "error_free (a,s) \<Longrightarrow> error_free ((raise_if p x a), s)"
   781 by simp 
   782 
   783 lemma error_free_supd [simp,intro]:
   784  "error_free s \<Longrightarrow> error_free (supd f s)"
   785 by (cases s) (simp add: error_free_def)
   786 
   787 lemma error_free_supd1 [simp,intro]:
   788  "error_free (a,s) \<Longrightarrow> error_free (a,f s)"
   789 by (simp add: error_free_def)
   790 
   791 lemma error_free_set_lvars [simp,intro]:
   792 "error_free s \<Longrightarrow> error_free ((set_lvars l) s)"
   793 by (cases s) simp
   794 
   795 lemma error_free_set_locals [simp,intro]: 
   796 "error_free (x, s)
   797        \<Longrightarrow> error_free (x, set_locals l s')"
   798 by (simp add: error_free_def)
   799 
   800 
   801 end
   802