src/HOL/MicroJava/J/State.thy
author wenzelm
Sun, 07 Aug 2011 18:38:36 +0200
changeset 44913 2ff2a54d0fb5
parent 44906 322d1657c40c
child 48269 a360406f1fcb
permissions -rw-r--r--
fixed document;
nipkow@8011
     1
(*  Title:      HOL/MicroJava/J/State.thy
nipkow@8011
     2
    Author:     David von Oheimb
nipkow@8011
     3
    Copyright   1999 Technische Universitaet Muenchen
oheimb@11070
     4
*)
nipkow@8011
     5
kleing@12911
     6
header {* \isaheader{Program State} *}
nipkow@8011
     7
haftmann@32356
     8
theory State
haftmann@32356
     9
imports TypeRel Value
haftmann@32356
    10
begin
nipkow@8011
    11
wenzelm@43334
    12
type_synonym 
wenzelm@24783
    13
  fields' = "(vname \<times> cname \<rightharpoonup> val)"  -- "field name, defining class, value"
nipkow@8011
    14
wenzelm@43334
    15
type_synonym
wenzelm@24783
    16
  obj = "cname \<times> fields'"    -- "class instance with class name and fields"
nipkow@8011
    17
haftmann@35413
    18
definition obj_ty :: "obj => ty" where
kleing@10042
    19
 "obj_ty obj  == Class (fst obj)"
nipkow@8011
    20
haftmann@35413
    21
definition init_vars :: "('a \<times> ty) list => ('a \<rightharpoonup> val)" where
kleing@12517
    22
 "init_vars == map_of o map (\<lambda>(n,T). (n,default_val T))"
haftmann@32356
    23
wenzelm@43334
    24
type_synonym aheap = "loc \<rightharpoonup> obj"    -- {* "@{text heap}" used in a translation below *}
wenzelm@43334
    25
type_synonym locals = "vname \<rightharpoonup> val"  -- "simple state, i.e. variable contents"
nipkow@8011
    26
wenzelm@43334
    27
type_synonym state = "aheap \<times> locals"      -- "heap, local parameter including This"
wenzelm@43334
    28
type_synonym xstate = "val option \<times> state" -- "state including exception information"
nipkow@8011
    29
wenzelm@35102
    30
abbreviation (input)
wenzelm@35102
    31
  heap :: "state => aheap"
wenzelm@35102
    32
  where "heap == fst"
nipkow@8011
    33
wenzelm@35102
    34
abbreviation (input)
wenzelm@35102
    35
  locals :: "state => locals"
wenzelm@35102
    36
  where "locals == snd"
wenzelm@35102
    37
wenzelm@35102
    38
abbreviation "Norm s == (None, s)"
wenzelm@35102
    39
wenzelm@35102
    40
abbreviation (input)
wenzelm@35102
    41
  abrupt :: "xstate \<Rightarrow> val option"
wenzelm@35102
    42
  where "abrupt == fst"
wenzelm@35102
    43
wenzelm@35102
    44
abbreviation (input)
wenzelm@35102
    45
  store :: "xstate \<Rightarrow> state"
wenzelm@35102
    46
  where "store == snd"
wenzelm@35102
    47
wenzelm@35102
    48
abbreviation
wenzelm@35102
    49
  lookup_obj :: "state \<Rightarrow> val \<Rightarrow> obj"
wenzelm@35102
    50
  where "lookup_obj s a' == the (heap s (the_Addr a'))"
streckem@13672
    51
haftmann@35413
    52
definition raise_if :: "bool \<Rightarrow> xcpt \<Rightarrow> val option \<Rightarrow> val option" where
streckem@13672
    53
  "raise_if b x xo \<equiv> if b \<and>  (xo = None) then Some (Addr (XcptRef x)) else xo"
nipkow@8011
    54
wenzelm@44913
    55
text {* Make @{text new_Addr} completely specified (at least for the code generator) *}
Andreas@44906
    56
(*
haftmann@35413
    57
definition new_Addr  :: "aheap => loc \<times> val option" where
streckem@13672
    58
  "new_Addr h \<equiv> SOME (a,x). (h a = None \<and>  x = None) |  x = Some (Addr (XcptRef OutOfMemory))"
Andreas@44906
    59
*)
Andreas@44906
    60
consts nat_to_loc' :: "nat => loc'"
Andreas@44906
    61
code_datatype nat_to_loc'
Andreas@44906
    62
definition new_Addr  :: "aheap => loc \<times> val option" where
Andreas@44906
    63
  "new_Addr h \<equiv> 
Andreas@44906
    64
   if \<exists>n. h (Loc (nat_to_loc' n)) = None 
Andreas@44906
    65
   then (Loc (nat_to_loc' (LEAST n. h (Loc (nat_to_loc' n)) = None)), None)
Andreas@44906
    66
   else (Loc (nat_to_loc' 0), Some (Addr (XcptRef OutOfMemory)))"
nipkow@8011
    67
haftmann@35413
    68
definition np    :: "val => val option => val option" where
kleing@10042
    69
 "np v == raise_if (v = Null) NullPointer"
nipkow@8011
    70
haftmann@35413
    71
definition c_hupd  :: "aheap => xstate => xstate" where
oheimb@11026
    72
 "c_hupd h'== \<lambda>(xo,(h,l)). if xo = None then (None,(h',l)) else (xo,(h,l))"
nipkow@8011
    73
haftmann@35413
    74
definition cast_ok :: "'c prog => cname => aheap => val => bool" where
oheimb@11026
    75
 "cast_ok G C h v == v = Null \<or> G\<turnstile>obj_ty (the (h (the_Addr v)))\<preceq> Class C"
oheimb@11026
    76
oheimb@11026
    77
lemma obj_ty_def2 [simp]: "obj_ty (C,fs) = Class C"
oheimb@11026
    78
apply (unfold obj_ty_def)
oheimb@11026
    79
apply (simp (no_asm))
oheimb@11026
    80
done
oheimb@11026
    81
streckem@13672
    82
streckem@13672
    83
lemma new_AddrD: "new_Addr hp = (ref, xcp) \<Longrightarrow>
streckem@13672
    84
  hp ref = None \<and> xcp = None \<or> xcp = Some (Addr (XcptRef OutOfMemory))"
streckem@13672
    85
apply (drule sym)
oheimb@11026
    86
apply (unfold new_Addr_def)
Andreas@44906
    87
apply (simp split: split_if_asm)
Andreas@44906
    88
apply (erule LeastI)
oheimb@11026
    89
done
oheimb@11026
    90
oheimb@11026
    91
lemma raise_if_True [simp]: "raise_if True x y \<noteq> None"
oheimb@11026
    92
apply (unfold raise_if_def)
oheimb@11026
    93
apply auto
oheimb@11026
    94
done
oheimb@11026
    95
oheimb@11026
    96
lemma raise_if_False [simp]: "raise_if False x y = y"
oheimb@11026
    97
apply (unfold raise_if_def)
oheimb@11026
    98
apply auto
oheimb@11026
    99
done
oheimb@11026
   100
oheimb@11026
   101
lemma raise_if_Some [simp]: "raise_if c x (Some y) \<noteq> None"
oheimb@11026
   102
apply (unfold raise_if_def)
oheimb@11026
   103
apply auto
oheimb@11026
   104
done
oheimb@11026
   105
kleing@12517
   106
lemma raise_if_Some2 [simp]: 
kleing@12517
   107
  "raise_if c z (if x = None then Some y else x) \<noteq> None"
oheimb@11026
   108
apply (unfold raise_if_def)
oheimb@11026
   109
apply(induct_tac "x")
oheimb@11026
   110
apply auto
oheimb@11026
   111
done
oheimb@11026
   112
oheimb@11026
   113
lemma raise_if_SomeD [rule_format (no_asm)]: 
streckem@13672
   114
  "raise_if c x y = Some z \<longrightarrow> c \<and>  Some z = Some (Addr (XcptRef x)) |  y = Some z"
oheimb@11026
   115
apply (unfold raise_if_def)
oheimb@11026
   116
apply auto
oheimb@11026
   117
done
oheimb@11026
   118
kleing@12517
   119
lemma raise_if_NoneD [rule_format (no_asm)]: 
kleing@12517
   120
  "raise_if c x y = None --> \<not> c \<and>  y = None"
oheimb@11026
   121
apply (unfold raise_if_def)
oheimb@11026
   122
apply auto
oheimb@11026
   123
done
oheimb@11026
   124
kleing@12517
   125
lemma np_NoneD [rule_format (no_asm)]: 
kleing@12517
   126
  "np a' x' = None --> x' = None \<and>  a' \<noteq> Null"
oheimb@11026
   127
apply (unfold np_def raise_if_def)
oheimb@11026
   128
apply auto
oheimb@11026
   129
done
oheimb@11026
   130
oheimb@11026
   131
lemma np_None [rule_format (no_asm), simp]: "a' \<noteq> Null --> np a' x' = x'"
oheimb@11026
   132
apply (unfold np_def raise_if_def)
oheimb@11026
   133
apply auto
oheimb@11026
   134
done
oheimb@11026
   135
oheimb@11026
   136
lemma np_Some [simp]: "np a' (Some xc) = Some xc"
oheimb@11026
   137
apply (unfold np_def raise_if_def)
oheimb@11026
   138
apply auto
oheimb@11026
   139
done
oheimb@11026
   140
streckem@13672
   141
lemma np_Null [simp]: "np Null None = Some (Addr (XcptRef NullPointer))"
oheimb@11026
   142
apply (unfold np_def raise_if_def)
oheimb@11026
   143
apply auto
oheimb@11026
   144
done
oheimb@11026
   145
oheimb@11026
   146
lemma np_Addr [simp]: "np (Addr a) None = None"
oheimb@11026
   147
apply (unfold np_def raise_if_def)
oheimb@11026
   148
apply auto
oheimb@11026
   149
done
oheimb@11026
   150
oheimb@11026
   151
lemma np_raise_if [simp]: "(np Null (raise_if c xc None)) =  
streckem@13672
   152
  Some (Addr (XcptRef (if c then  xc else NullPointer)))"
oheimb@11026
   153
apply (unfold raise_if_def)
oheimb@11026
   154
apply (simp (no_asm))
oheimb@11026
   155
done
nipkow@8011
   156
streckem@14144
   157
lemma c_hupd_fst [simp]: "fst (c_hupd h (x, s)) = x"
streckem@14144
   158
by (simp add: c_hupd_def split_beta)
streckem@14144
   159
Andreas@44906
   160
text {* Naive implementation for @{term "new_Addr"} by exhaustive search *}
Andreas@44906
   161
Andreas@44906
   162
definition gen_new_Addr :: "aheap => nat \<Rightarrow> loc \<times> val option" where
Andreas@44906
   163
  "gen_new_Addr h n \<equiv> 
Andreas@44906
   164
   if \<exists>a. a \<ge> n \<and> h (Loc (nat_to_loc' a)) = None 
Andreas@44906
   165
   then (Loc (nat_to_loc' (LEAST a. a \<ge> n \<and> h (Loc (nat_to_loc' a)) = None)), None)
Andreas@44906
   166
   else (Loc (nat_to_loc' 0), Some (Addr (XcptRef OutOfMemory)))"
Andreas@44906
   167
Andreas@44906
   168
lemma new_Addr_code_code [code]:
Andreas@44906
   169
  "new_Addr h = gen_new_Addr h 0"
Andreas@44906
   170
by(simp only: new_Addr_def gen_new_Addr_def split: split_if) simp
Andreas@44906
   171
Andreas@44906
   172
lemma gen_new_Addr_code [code]:
Andreas@44906
   173
  "gen_new_Addr h n = (if h (Loc (nat_to_loc' n)) = None then (Loc (nat_to_loc' n), None) else gen_new_Addr h (Suc n))"
Andreas@44906
   174
apply(simp add: gen_new_Addr_def)
Andreas@44906
   175
apply(rule impI)
Andreas@44906
   176
apply(rule conjI)
Andreas@44906
   177
 apply safe[1]
Andreas@44906
   178
  apply(auto intro: arg_cong[where f=nat_to_loc'] Least_equality)[1]
Andreas@44906
   179
 apply(rule arg_cong[where f=nat_to_loc'])
Andreas@44906
   180
 apply(rule arg_cong[where f=Least])
Andreas@44906
   181
 apply(rule ext)
Andreas@44906
   182
 apply(safe, simp_all)[1]
Andreas@44906
   183
 apply(rename_tac "n'")
Andreas@44906
   184
 apply(case_tac "n = n'", simp_all)[1]
Andreas@44906
   185
apply clarify
Andreas@44906
   186
apply(subgoal_tac "a = n")
Andreas@44906
   187
 apply(auto intro: Least_equality arg_cong[where f=nat_to_loc'])[1]
Andreas@44906
   188
apply(rule ccontr)
Andreas@44906
   189
apply(erule_tac x="a" in allE)
Andreas@44906
   190
apply simp
Andreas@44906
   191
done
Andreas@44906
   192
Andreas@44906
   193
instantiation loc' :: equal begin
Andreas@44906
   194
definition "HOL.equal (l :: loc') l' \<longleftrightarrow> l = l'"
Andreas@44906
   195
instance proof qed(simp add: equal_loc'_def)
nipkow@8011
   196
end
Andreas@44906
   197
Andreas@44906
   198
end