(* Title: HOL/Bali/State.thy Author: David von Oheimb *) subsection‹State for evaluation of Java expressions and statements›
theory State imports DeclConcepts begin
text‹ design issues: \begin{itemize} \item all kinds of objects (class instances, arrays, and class objects) are handeled via a general object abstraction \item the heap and the map for class objects are combined into a single table ‹(recall (loc, obj) table × (qtname, obj) table ~= (loc + qtname, obj) table)› \end{itemize} ›
subsubsection "objects"
datatype obj_tag = 🍋‹tag for generic object›
CInst qtname 🍋‹class instance›
| Arr ty int 🍋‹array with component type and length› 🍋‹| CStat qtname the tag is irrelevant for a class object, i.e. the static fields of a class, since its type is given already by the reference to it (see below)›
type_synonym vn = "fspec + int"🍋‹variable name› record obj =
tag :: "obj_tag"🍋‹generalized object› "values" :: "(vn, val) table"
lemma obj_ty_eq1 [intro!,dest]: "tag obj = tag obj' ==> obj_ty obj = obj_ty obj'" by (simp add: obj_ty_def)
lemma obj_ty_cong [simp]: "obj_ty (obj (values:=vs)) = obj_ty obj" by auto
lemma obj_ty_CInst [simp]: "obj_ty (tag=CInst C,values=vs) = Class C" by (simp add: obj_ty_def)
lemma obj_ty_CInst1 [simp,intro!,dest]: "[tag obj = CInst C]==> obj_ty obj = Class C" by (simp add: obj_ty_def)
lemma obj_ty_Arr [simp]: "obj_ty (tag=Arr T i,values=vs) = T.[]" by (simp add: obj_ty_def)
lemma obj_ty_Arr1 [simp,intro!,dest]: "[tag obj = Arr T i]==> obj_ty obj = T.[]" by (simp add: obj_ty_def)
lemma obj_ty_widenD: "G⊨obj_ty obj⪯RefT t ==> (∃C. tag obj = CInst C) ∨ (∃T k. tag obj = Arr T k)" apply (unfold obj_ty_def) apply (auto split: obj_tag.split_asm) done
definition
obj_class :: "obj ==> qtname"where "obj_class obj = (case tag obj of CInst C ==> C | Arr T k ==> Object)"
definition
fields_table :: "prog ==> qtname ==> (fspec ==> field ==> bool) ==> (fspec, ty) table"where "fields_table G C P = map_option type ∘ table_of (filter (case_prod P) (DeclConcepts.fields G C))"
lemma fields_table_SomeI: "[table_of (DeclConcepts.fields G C) n = Some f; P n f] ==> fields_table G C P n = Some (type f)" apply (unfold fields_table_def) apply clarsimp apply (rule exI) apply (rule conjI) apply (erule map_of_filter_in) apply assumption apply simp done
(* unused *) lemma fields_table_SomeD': "fields_table G C P fn = Some T ==> ∃f. (fn,f)∈set(DeclConcepts.fields G C) ∧ type f = T" apply (unfold fields_table_def) apply clarsimp apply (drule map_of_SomeD) apply auto done
lemma fields_table_SomeD: "[fields_table G C P fn = Some T; unique (DeclConcepts.fields G C)]==> ∃f. table_of (DeclConcepts.fields G C) fn = Some f ∧ type f = T" apply (unfold fields_table_def) apply clarsimp apply (rule exI) apply (rule conjI) apply (erule table_of_filter_unique_SomeD) apply assumption apply simp done
definition
in_bounds :: "int ==> int ==> bool" (‹(_/ in'_bounds _)› [50, 51] 50) where"i in_bounds k = (0 ≤ i ∧ i < k)"
definition
arr_comps :: "'a ==> int ==> int ==> 'a option" where"arr_comps T k = (λi. if i in_bounds k then Some T else None)"
definition
var_tys :: "prog ==> obj_tag ==> oref ==> (vn, ty) table"where "var_tys G oi r = (case r of Heap a ==> (case oi of CInst C ==> fields_table G C (λn f. ¬static f) (+) Map.empty | Arr T k ==> Map.empty (+) arr_comps T k) | Stat C ==> fields_table G C (λfn f. declclassf fn = C ∧ static f) (+) Map.empty)"
lemma var_tys_Some_eq: "var_tys G oi r n = Some T = (case r of Inl a ==> (case oi of CInst C ==> (∃nt. n = Inl nt ∧ fields_table G C (λn f. ¬static f) nt = Some T) | Arr t k ==> (∃ i. n = Inr i ∧ i in_bounds k ∧ t = T)) | Inr C ==> (∃nt. n = Inl nt ∧ fields_table G C (λfn f. declclassf fn = C ∧ static f) nt = Some T))" apply (unfold var_tys_def arr_comps_def) apply (force split: sum.split_asm sum.split obj_tag.split) done
lemma globs_upd_gobj_new [rule_format (no_asm), simp]: "globs s r = None ⟶ globs (upd_gobj r n v s) = globs s" apply (unfold upd_gobj_def) apply (induct "s") apply auto done
lemma globs_upd_gobj_upd [rule_format (no_asm), simp]: "globs s r=Some obj⟶ globs (upd_gobj r n v s) = (globs s)(r↦upd_obj n v obj)" apply (unfold upd_gobj_def) apply (induct "s") apply auto done
lemma locals_upd_gobj [simp]: "locals (upd_gobj r n v s) = locals s" apply (induct "s") by (simp add: upd_gobj_def)
lemma globs_init_obj [simp]: "globs (init_obj G oi r s) t = (if t=r then Some (tag=oi,values=init_vals (var_tys G oi r)) else globs s t)" apply (unfold init_obj_def) apply (simp (no_asm)) done
lemma locals_init_obj [simp]: "locals (init_obj G oi r s) = locals s" by (simp add: init_obj_def)
lemma surjective_st [simp]: "st (globs s) (locals s) = s" apply (induct "s") by auto
lemma surjective_st_init_obj: "st (globs (init_obj G oi r s)) (locals s) = init_obj G oi r s" apply (subst locals_init_obj [THEN sym]) apply (rule surjective_st) done
abbreviation error_if :: "bool ==> error ==> abopt ==> abopt" where"error_if c e == abrupt_if c (Some (Error e))"
lemma raise_if_None [simp]: "(raise_if c x y = None) = (¬c ∧ y = None)" apply (simp add: abrupt_if_def) by auto declare raise_if_None [THEN iffD1, dest!]
lemma if_raise_if_None [simp]: "((if b then y else raise_if c x y) = None) = ((c ⟶ b) ∧ y = None)" apply (simp add: abrupt_if_def) apply auto done
lemma raise_if_SomeD [dest!]: "raise_if c x y = Some z ==> c ∧ z=(Xcpt (Std x)) ∧ y=None ∨ (y=Some z)" apply (case_tac y) apply (case_tac c) apply (simp add: abrupt_if_def) apply (simp add: abrupt_if_def) apply auto done
lemma error_if_None [simp]: "(error_if c e y = None) = (¬c ∧ y = None)" apply (simp add: abrupt_if_def) by auto declare error_if_None [THEN iffD1, dest!]
lemma if_error_if_None [simp]: "((if b then y else error_if c e y) = None) = ((c ⟶ b) ∧ y = None)" apply (simp add: abrupt_if_def) apply auto done
lemma error_if_SomeD [dest!]: "error_if c e y = Some z ==> c ∧ z=(Error e) ∧ y=None ∨ (y=Some z)" apply (case_tac y) apply (case_tac c) apply (simp add: abrupt_if_def) apply (simp add: abrupt_if_def) apply auto done
definition
absorb :: "jump ==> abopt ==> abopt" where"absorb j a = (if a=Some (Jump j) then None else a)"
lemma absorb_SomeD [dest!]: "absorb j a = Some x ==> a = Some x" by (auto simp add: absorb_def)
abbreviation (input)
abrupt :: "state ==> abopt" where"abrupt == fst"
abbreviation (input)
store :: "state ==> st" where"store == snd"
lemma single_stateE: "∀Z. Z = (s::state) ==> False" apply (erule_tac x = "(Some k,y)"for k y in all_dupE) apply (erule_tac x = "(None,y)"for y in allE) apply clarify done
lemma state_not_single: "All ((=) (x::state)) ==> R" apply (drule_tac x = "(if abrupt x = None then Some x' else None, y)"for x' y in spec) apply clarsimp done
definition
normal :: "state ==> bool" where"normal = (λs. abrupt s = None)"
lemma normal_def2 [simp]: "normal s = (abrupt s = None)" apply (unfold normal_def) apply (simp (no_asm)) done
definition
heap_free :: "nat ==> state ==> bool" where"heap_free n = (λs. atleast_free (heap (store s)) n)"
lemma heap_free_def2 [simp]: "heap_free n s = atleast_free (heap (store s)) n" apply (unfold heap_free_def) apply simp done
subsection"update"
definition
abupd :: "(abopt ==> abopt) ==> state ==> state" where"abupd f = map_prod f id"
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.