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