src/HOL/NanoJava/State.thy
author wenzelm
Wed Mar 03 00:33:02 2010 +0100 (2010-03-03)
changeset 35431 8758fe1fc9f8
parent 35417 47ee18b6ae32
child 42463 f270e3e18be5
permissions -rw-r--r--
cleanup type translations;
     1 (*  Title:      HOL/NanoJava/State.thy
     2     Author:     David von Oheimb
     3     Copyright   2001 Technische Universitaet Muenchen
     4 *)
     5 
     6 header "Program State"
     7 
     8 theory State imports TypeRel begin
     9 
    10 definition body :: "cname \<times> mname => stmt" where
    11  "body \<equiv> \<lambda>(C,m). bdy (the (method C m))"
    12 
    13 text {* Locations, i.e.\ abstract references to objects *}
    14 typedecl loc 
    15 
    16 datatype val
    17   = Null        --{* null reference *}
    18   | Addr loc    --{* address, i.e. location of object *}
    19 
    20 types   fields
    21         = "(fname \<rightharpoonup> val)"
    22 
    23         obj = "cname \<times> fields"
    24 
    25 translations
    26   (type) "fields" \<leftharpoondown> (type) "fname => val option"
    27   (type) "obj"    \<leftharpoondown> (type) "cname \<times> fields"
    28 
    29 definition init_vars :: "('a \<rightharpoonup> 'b) => ('a \<rightharpoonup> val)" where
    30  "init_vars m == Option.map (\<lambda>T. Null) o m"
    31   
    32 text {* private: *}
    33 types   heap   = "loc   \<rightharpoonup> obj"
    34         locals = "vname \<rightharpoonup> val"  
    35 
    36 text {* private: *}
    37 record  state
    38         = heap   :: heap
    39           locals :: locals
    40 
    41 translations
    42   (type) "heap" \<leftharpoondown> (type) "loc => obj option"
    43   (type) "locals" \<leftharpoondown> (type) "vname => val option"
    44   (type) "state" \<leftharpoondown> (type) "(|heap :: heap, locals :: locals|)"
    45 
    46 definition del_locs :: "state => state" where
    47  "del_locs s \<equiv> s (| locals := empty |)"
    48 
    49 definition init_locs     :: "cname => mname => state => state" where
    50  "init_locs C m s \<equiv> s (| locals := locals s ++ 
    51                          init_vars (map_of (lcl (the (method C m)))) |)"
    52 
    53 text {* The first parameter of @{term set_locs} is of type @{typ state} 
    54         rather than @{typ locals} in order to keep @{typ locals} private.*}
    55 definition set_locs :: "state => state => state" where
    56  "set_locs s s' \<equiv> s' (| locals := locals s |)"
    57 
    58 definition get_local     :: "state => vname => val" ("_<_>" [99,0] 99) where
    59  "get_local s x  \<equiv> the (locals s x)"
    60 
    61 --{* local function: *}
    62 definition get_obj       :: "state => loc => obj" where
    63  "get_obj s a \<equiv> the (heap s a)"
    64 
    65 definition obj_class     :: "state => loc => cname" where
    66  "obj_class s a \<equiv> fst (get_obj s a)"
    67 
    68 definition get_field     :: "state => loc => fname => val" where
    69  "get_field s a f \<equiv> the (snd (get_obj s a) f)"
    70 
    71 --{* local function: *}
    72 definition hupd       :: "loc => obj => state => state"   ("hupd'(_|->_')" [10,10] 1000) where
    73  "hupd a obj s \<equiv> s (| heap   := ((heap   s)(a\<mapsto>obj))|)"
    74 
    75 definition lupd       :: "vname => val => state => state" ("lupd'(_|->_')" [10,10] 1000) where
    76  "lupd x v s   \<equiv> s (| locals := ((locals s)(x\<mapsto>v  ))|)"
    77 
    78 notation (xsymbols)
    79   hupd  ("hupd'(_\<mapsto>_')" [10,10] 1000) and
    80   lupd  ("lupd'(_\<mapsto>_')" [10,10] 1000)
    81 
    82 definition new_obj :: "loc => cname => state => state" where
    83  "new_obj a C   \<equiv> hupd(a\<mapsto>(C,init_vars (field C)))"
    84 
    85 definition upd_obj    :: "loc => fname => val => state => state" where
    86  "upd_obj a f v s \<equiv> let (C,fs) = the (heap s a) in hupd(a\<mapsto>(C,fs(f\<mapsto>v))) s"
    87 
    88 definition new_Addr      :: "state => val" where
    89  "new_Addr s == SOME v. (\<exists>a. v = Addr a \<and> (heap s) a = None) | v = Null"
    90 
    91 
    92 subsection "Properties not used in the meta theory"
    93 
    94 lemma locals_upd_id [simp]: "s\<lparr>locals := locals s\<rparr> = s" 
    95 by simp
    96 
    97 lemma lupd_get_local_same [simp]: "lupd(x\<mapsto>v) s<x> = v"
    98 by (simp add: lupd_def get_local_def)
    99 
   100 lemma lupd_get_local_other [simp]: "x \<noteq> y \<Longrightarrow> lupd(x\<mapsto>v) s<y> = s<y>"
   101 apply (drule not_sym)
   102 by (simp add: lupd_def get_local_def)
   103 
   104 lemma get_field_lupd [simp]:
   105   "get_field (lupd(x\<mapsto>y) s) a f = get_field s a f"
   106 by (simp add: lupd_def get_field_def get_obj_def)
   107 
   108 lemma get_field_set_locs [simp]:
   109   "get_field (set_locs l s) a f = get_field s a f"
   110 by (simp add: lupd_def get_field_def set_locs_def get_obj_def)
   111 
   112 lemma get_field_del_locs [simp]:
   113   "get_field (del_locs s) a f = get_field s a f"
   114 by (simp add: lupd_def get_field_def del_locs_def get_obj_def)
   115 
   116 lemma new_obj_get_local [simp]: "new_obj a C s <x> = s<x>"
   117 by (simp add: new_obj_def hupd_def get_local_def)
   118 
   119 lemma heap_lupd [simp]: "heap (lupd(x\<mapsto>y) s) = heap s"
   120 by (simp add: lupd_def)
   121 
   122 lemma heap_hupd_same [simp]: "heap (hupd(a\<mapsto>obj) s) a = Some obj"
   123 by (simp add: hupd_def)
   124 
   125 lemma heap_hupd_other [simp]: "aa \<noteq> a  \<Longrightarrow> heap (hupd(aa\<mapsto>obj) s) a = heap s a"
   126 apply (drule not_sym)
   127 by (simp add: hupd_def)
   128 
   129 lemma hupd_hupd [simp]: "hupd(a\<mapsto>obj) (hupd(a\<mapsto>obj') s) = hupd(a\<mapsto>obj) s"
   130 by (simp add: hupd_def)
   131 
   132 lemma heap_del_locs [simp]: "heap (del_locs s) = heap s"
   133 by (simp add: del_locs_def)
   134 
   135 lemma heap_set_locs [simp]: "heap (set_locs l s) = heap s"
   136 by (simp add: set_locs_def)
   137 
   138 lemma hupd_lupd [simp]: 
   139   "hupd(a\<mapsto>obj) (lupd(x\<mapsto>y) s) = lupd(x\<mapsto>y) (hupd(a\<mapsto>obj) s)"
   140 by (simp add: hupd_def lupd_def)
   141 
   142 lemma hupd_del_locs [simp]: 
   143   "hupd(a\<mapsto>obj) (del_locs s) = del_locs (hupd(a\<mapsto>obj) s)"
   144 by (simp add: hupd_def del_locs_def)
   145 
   146 lemma new_obj_lupd [simp]: 
   147   "new_obj a C (lupd(x\<mapsto>y) s) = lupd(x\<mapsto>y) (new_obj a C s)"
   148 by (simp add: new_obj_def)
   149 
   150 lemma new_obj_del_locs [simp]: 
   151   "new_obj a C (del_locs s) = del_locs (new_obj a C s)"
   152 by (simp add: new_obj_def)
   153 
   154 lemma upd_obj_lupd [simp]: 
   155   "upd_obj a f v (lupd(x\<mapsto>y) s) = lupd(x\<mapsto>y) (upd_obj a f v s)"
   156 by (simp add: upd_obj_def Let_def split_beta)
   157 
   158 lemma upd_obj_del_locs [simp]: 
   159   "upd_obj a f v (del_locs s) = del_locs (upd_obj a f v s)"
   160 by (simp add: upd_obj_def Let_def split_beta)
   161 
   162 lemma get_field_hupd_same [simp]:
   163  "get_field (hupd(a\<mapsto>(C, fs)) s) a = the \<circ> fs"
   164 apply (rule ext)
   165 by (simp add: get_field_def get_obj_def)
   166 
   167 lemma get_field_hupd_other [simp]:
   168  "aa \<noteq> a  \<Longrightarrow> get_field (hupd(aa\<mapsto>obj) s) a = get_field s a"
   169 apply (rule ext)
   170 by (simp add: get_field_def get_obj_def)
   171 
   172 lemma new_AddrD: 
   173 "new_Addr s = v \<Longrightarrow> (\<exists>a. v = Addr a \<and> heap s a = None) | v = Null"
   174 apply (unfold new_Addr_def)
   175 apply (erule subst)
   176 apply (rule someI)
   177 apply (rule disjI2)
   178 apply (rule HOL.refl)
   179 done
   180 
   181 end