src/HOL/NanoJava/State.thy
changeset 35417 47ee18b6ae32
parent 35355 613e133966ea
parent 35416 d8d7d1b785af
child 35431 8758fe1fc9f8
equal deleted inserted replaced
35415:1810b1ade437 35417:47ee18b6ae32
     5 
     5 
     6 header "Program State"
     6 header "Program State"
     7 
     7 
     8 theory State imports TypeRel begin
     8 theory State imports TypeRel begin
     9 
     9 
    10 constdefs
    10 definition body :: "cname \<times> mname => stmt" where
    11 
       
    12   body :: "cname \<times> mname => stmt"
       
    13  "body \<equiv> \<lambda>(C,m). bdy (the (method C m))"
    11  "body \<equiv> \<lambda>(C,m). bdy (the (method C m))"
    14 
    12 
    15 text {* Locations, i.e.\ abstract references to objects *}
    13 text {* Locations, i.e.\ abstract references to objects *}
    16 typedecl loc 
    14 typedecl loc 
    17 
    15 
    27 translations
    25 translations
    28 
    26 
    29   "fields" \<leftharpoondown> (type)"fname => val option"
    27   "fields" \<leftharpoondown> (type)"fname => val option"
    30   "obj"    \<leftharpoondown> (type)"cname \<times> fields"
    28   "obj"    \<leftharpoondown> (type)"cname \<times> fields"
    31 
    29 
    32 constdefs
    30 definition init_vars :: "('a \<rightharpoonup> 'b) => ('a \<rightharpoonup> val)" where
    33 
       
    34   init_vars:: "('a \<rightharpoonup> 'b) => ('a \<rightharpoonup> val)"
       
    35  "init_vars m == Option.map (\<lambda>T. Null) o m"
    31  "init_vars m == Option.map (\<lambda>T. Null) o m"
    36   
    32   
    37 text {* private: *}
    33 text {* private: *}
    38 types   heap   = "loc   \<rightharpoonup> obj"
    34 types   heap   = "loc   \<rightharpoonup> obj"
    39         locals = "vname \<rightharpoonup> val"  
    35         locals = "vname \<rightharpoonup> val"  
    47 
    43 
    48   "heap"   \<leftharpoondown> (type)"loc   => obj option"
    44   "heap"   \<leftharpoondown> (type)"loc   => obj option"
    49   "locals" \<leftharpoondown> (type)"vname => val option"
    45   "locals" \<leftharpoondown> (type)"vname => val option"
    50   "state" \<leftharpoondown> (type)"(|heap :: heap, locals :: locals|)"
    46   "state" \<leftharpoondown> (type)"(|heap :: heap, locals :: locals|)"
    51 
    47 
    52 constdefs
    48 definition del_locs :: "state => state" where
    53 
       
    54   del_locs     :: "state => state"
       
    55  "del_locs s \<equiv> s (| locals := empty |)"
    49  "del_locs s \<equiv> s (| locals := empty |)"
    56 
    50 
    57   init_locs     :: "cname => mname => state => state"
    51 definition init_locs     :: "cname => mname => state => state" where
    58  "init_locs C m s \<equiv> s (| locals := locals s ++ 
    52  "init_locs C m s \<equiv> s (| locals := locals s ++ 
    59                          init_vars (map_of (lcl (the (method C m)))) |)"
    53                          init_vars (map_of (lcl (the (method C m)))) |)"
    60 
    54 
    61 text {* The first parameter of @{term set_locs} is of type @{typ state} 
    55 text {* The first parameter of @{term set_locs} is of type @{typ state} 
    62         rather than @{typ locals} in order to keep @{typ locals} private.*}
    56         rather than @{typ locals} in order to keep @{typ locals} private.*}
    63 constdefs
    57 definition set_locs :: "state => state => state" where
    64   set_locs  :: "state => state => state"
       
    65  "set_locs s s' \<equiv> s' (| locals := locals s |)"
    58  "set_locs s s' \<equiv> s' (| locals := locals s |)"
    66 
    59 
    67   get_local     :: "state => vname => val" ("_<_>" [99,0] 99)
    60 definition get_local     :: "state => vname => val" ("_<_>" [99,0] 99) where
    68  "get_local s x  \<equiv> the (locals s x)"
    61  "get_local s x  \<equiv> the (locals s x)"
    69 
    62 
    70 --{* local function: *}
    63 --{* local function: *}
    71   get_obj       :: "state => loc => obj"
    64 definition get_obj       :: "state => loc => obj" where
    72  "get_obj s a \<equiv> the (heap s a)"
    65  "get_obj s a \<equiv> the (heap s a)"
    73 
    66 
    74   obj_class     :: "state => loc => cname"
    67 definition obj_class     :: "state => loc => cname" where
    75  "obj_class s a \<equiv> fst (get_obj s a)"
    68  "obj_class s a \<equiv> fst (get_obj s a)"
    76 
    69 
    77   get_field     :: "state => loc => fname => val"
    70 definition get_field     :: "state => loc => fname => val" where
    78  "get_field s a f \<equiv> the (snd (get_obj s a) f)"
    71  "get_field s a f \<equiv> the (snd (get_obj s a) f)"
    79 
    72 
    80 --{* local function: *}
    73 --{* local function: *}
    81   hupd       :: "loc => obj => state => state"   ("hupd'(_|->_')" [10,10] 1000)
    74 definition hupd       :: "loc => obj => state => state"   ("hupd'(_|->_')" [10,10] 1000) where
    82  "hupd a obj s \<equiv> s (| heap   := ((heap   s)(a\<mapsto>obj))|)"
    75  "hupd a obj s \<equiv> s (| heap   := ((heap   s)(a\<mapsto>obj))|)"
    83 
    76 
    84   lupd       :: "vname => val => state => state" ("lupd'(_|->_')" [10,10] 1000)
    77 definition lupd       :: "vname => val => state => state" ("lupd'(_|->_')" [10,10] 1000) where
    85  "lupd x v s   \<equiv> s (| locals := ((locals s)(x\<mapsto>v  ))|)"
    78  "lupd x v s   \<equiv> s (| locals := ((locals s)(x\<mapsto>v  ))|)"
    86 
    79 
    87 notation (xsymbols)
    80 notation (xsymbols)
    88   hupd  ("hupd'(_\<mapsto>_')" [10,10] 1000) and
    81   hupd  ("hupd'(_\<mapsto>_')" [10,10] 1000) and
    89   lupd  ("lupd'(_\<mapsto>_')" [10,10] 1000)
    82   lupd  ("lupd'(_\<mapsto>_')" [10,10] 1000)
    90 
    83 
    91 constdefs
    84 definition new_obj :: "loc => cname => state => state" where
    92 
       
    93   new_obj    :: "loc => cname => state => state"
       
    94  "new_obj a C   \<equiv> hupd(a\<mapsto>(C,init_vars (field C)))"
    85  "new_obj a C   \<equiv> hupd(a\<mapsto>(C,init_vars (field C)))"
    95 
    86 
    96   upd_obj    :: "loc => fname => val => state => state"
    87 definition upd_obj    :: "loc => fname => val => state => state" where
    97  "upd_obj a f v s \<equiv> let (C,fs) = the (heap s a) in hupd(a\<mapsto>(C,fs(f\<mapsto>v))) s"
    88  "upd_obj a f v s \<equiv> let (C,fs) = the (heap s a) in hupd(a\<mapsto>(C,fs(f\<mapsto>v))) s"
    98 
    89 
    99   new_Addr      :: "state => val"
    90 definition new_Addr      :: "state => val" where
   100  "new_Addr s == SOME v. (\<exists>a. v = Addr a \<and> (heap s) a = None) | v = Null"
    91  "new_Addr s == SOME v. (\<exists>a. v = Addr a \<and> (heap s) a = None) | v = Null"
   101 
    92 
   102 
    93 
   103 subsection "Properties not used in the meta theory"
    94 subsection "Properties not used in the meta theory"
   104 
    95