src/HOL/MicroJava/BV/LBVJVM.thy
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 14045 a34d89ce6097
child 16417 9bc16273c2d4
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
kleing@13215
     1
(*  Title:      HOL/MicroJava/BV/JVM.thy
kleing@13215
     2
    ID:         $Id$
kleing@13215
     3
    Author:     Tobias Nipkow, Gerwin Klein
kleing@13215
     4
    Copyright   2000 TUM
kleing@13215
     5
*)
kleing@13215
     6
kleing@13215
     7
header {* \isaheader{LBV for the JVM}\label{sec:JVM} *}
kleing@13215
     8
kleing@13224
     9
theory LBVJVM = LBVCorrect + LBVComplete + Typing_Framework_JVM:
kleing@13215
    10
kleing@13215
    11
types prog_cert = "cname \<Rightarrow> sig \<Rightarrow> state list"
kleing@13215
    12
kleing@13215
    13
constdefs
kleing@13215
    14
  check_cert :: "jvm_prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state list \<Rightarrow> bool"
kleing@13215
    15
  "check_cert G mxs mxr n cert \<equiv> check_types G mxs mxr cert \<and> length cert = n+1 \<and>
kleing@13215
    16
                                 (\<forall>i<n. cert!i \<noteq> Err) \<and> cert!n = OK None"
kleing@13215
    17
kleing@13215
    18
  lbvjvm :: "jvm_prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> ty \<Rightarrow> exception_table \<Rightarrow> 
kleing@13215
    19
             state list \<Rightarrow> instr list \<Rightarrow> state \<Rightarrow> state"
kleing@13215
    20
  "lbvjvm G maxs maxr rT et cert bs \<equiv>
kleing@13215
    21
  wtl_inst_list bs cert  (JVMType.sup G maxs maxr) (JVMType.le G maxs maxr) Err (OK None) (exec G maxs rT et bs) 0"
kleing@13215
    22
kleing@13215
    23
  wt_lbv :: "jvm_prog \<Rightarrow> cname \<Rightarrow> ty list \<Rightarrow> ty \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 
kleing@13215
    24
             exception_table \<Rightarrow> state list \<Rightarrow> instr list \<Rightarrow> bool"
kleing@13215
    25
  "wt_lbv G C pTs rT mxs mxl et cert ins \<equiv>
kleing@13215
    26
   check_bounded ins et \<and> 
kleing@13215
    27
   check_cert G mxs (1+size pTs+mxl) (length ins) cert \<and>
kleing@13215
    28
   0 < size ins \<and> 
kleing@13215
    29
   (let start  = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
kleing@13215
    30
        result = lbvjvm G mxs (1+size pTs+mxl) rT et cert ins (OK start)
kleing@13215
    31
    in result \<noteq> Err)"
kleing@13215
    32
kleing@13215
    33
  wt_jvm_prog_lbv :: "jvm_prog \<Rightarrow> prog_cert \<Rightarrow> bool"
kleing@13215
    34
  "wt_jvm_prog_lbv G cert \<equiv>
kleing@13215
    35
  wf_prog (\<lambda>G C (sig,rT,(maxs,maxl,b,et)). wt_lbv G C (snd sig) rT maxs maxl et (cert C sig) b) G"
kleing@13215
    36
kleing@13215
    37
  mk_cert :: "jvm_prog \<Rightarrow> nat \<Rightarrow> ty \<Rightarrow> exception_table \<Rightarrow> instr list 
kleing@13215
    38
              \<Rightarrow> method_type \<Rightarrow> state list"
kleing@13215
    39
  "mk_cert G maxs rT et bs phi \<equiv> make_cert (exec G maxs rT et bs) (map OK phi) (OK None)"
kleing@13215
    40
kleing@13215
    41
  prg_cert :: "jvm_prog \<Rightarrow> prog_type \<Rightarrow> prog_cert"
kleing@13215
    42
  "prg_cert G phi C sig \<equiv> let (C,rT,(maxs,maxl,ins,et)) = the (method (G,C) sig) in 
kleing@13215
    43
                           mk_cert G maxs rT et ins (phi C sig)"
kleing@13215
    44
 
kleing@13215
    45
  
kleing@13215
    46
lemma wt_method_def2:
kleing@13215
    47
  fixes pTs and mxl and G and mxs and rT and et and bs and phi 
kleing@13215
    48
  defines [simp]: "mxr   \<equiv> 1 + length pTs + mxl"
kleing@13215
    49
  defines [simp]: "r     \<equiv> sup_state_opt G"
kleing@13215
    50
  defines [simp]: "app0  \<equiv> \<lambda>pc. app (bs!pc) G mxs rT pc et"
kleing@13215
    51
  defines [simp]: "step0 \<equiv> \<lambda>pc. eff (bs!pc) G pc et"
kleing@13215
    52
kleing@13215
    53
  shows
kleing@13215
    54
  "wt_method G C pTs rT mxs mxl bs et phi = 
kleing@13215
    55
  (bs \<noteq> [] \<and> 
kleing@13215
    56
   length phi = length bs \<and>
kleing@13215
    57
   check_bounded bs et \<and> 
kleing@13215
    58
   check_types G mxs mxr (map OK phi) \<and>   
kleing@13215
    59
   wt_start G C pTs mxl phi \<and> 
kleing@13215
    60
   wt_app_eff r app0 step0 phi)"
kleing@13215
    61
  by (auto simp add: wt_method_def wt_app_eff_def wt_instr_def lesub_def
kleing@13215
    62
           dest: check_bounded_is_bounded boundedD)
kleing@13215
    63
kleing@13215
    64
kleing@13224
    65
lemma check_certD:
kleing@13224
    66
  "check_cert G mxs mxr n cert \<Longrightarrow> cert_ok cert n Err (OK None) (states G mxs mxr)"
kleing@13224
    67
  apply (unfold cert_ok_def check_cert_def check_types_def)
kleing@13224
    68
  apply (auto simp add: list_all_ball)
kleing@13224
    69
  done
kleing@13224
    70
kleing@13215
    71
kleing@13215
    72
lemma wt_lbv_wt_step:
kleing@13215
    73
  assumes wf:  "wf_prog wf_mb G"
kleing@13215
    74
  assumes lbv: "wt_lbv G C pTs rT mxs mxl et cert ins"
kleing@13215
    75
  assumes C:   "is_class G C" 
kleing@13215
    76
  assumes pTs: "set pTs \<subseteq> types G"
kleing@13215
    77
  
kleing@13215
    78
  defines [simp]: "mxr \<equiv> 1+length pTs+mxl"
kleing@13215
    79
kleing@13215
    80
  shows "\<exists>ts \<in> list (size ins) (states G mxs mxr). 
kleing@13215
    81
            wt_step (JVMType.le G mxs mxr) Err (exec G mxs rT et ins) ts
kleing@13215
    82
          \<and> OK (Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err))) <=_(JVMType.le G mxs mxr) ts!0"
kleing@13215
    83
proof -
kleing@13215
    84
  let ?step = "exec G mxs rT et ins"
kleing@13215
    85
  let ?r    = "JVMType.le G mxs mxr"
kleing@13215
    86
  let ?f    = "JVMType.sup G mxs mxr"
kleing@13215
    87
  let ?A    = "states G mxs mxr"
kleing@13215
    88
streckem@14045
    89
  have "semilat (JVMType.sl G mxs mxr)" 
streckem@14045
    90
    by (rule semilat_JVM_slI, rule wf_prog_ws_prog)
kleing@13215
    91
  hence "semilat (?A, ?r, ?f)" by (unfold sl_triple_conv)
kleing@13215
    92
  moreover
kleing@13215
    93
  have "top ?r Err"  by (simp add: JVM_le_unfold)
kleing@13215
    94
  moreover
kleing@13215
    95
  have "Err \<in> ?A" by (simp add: JVM_states_unfold)
kleing@13215
    96
  moreover
kleing@13215
    97
  have "bottom ?r (OK None)" 
kleing@13215
    98
    by (simp add: JVM_le_unfold bottom_def)
kleing@13215
    99
  moreover
kleing@13215
   100
  have "OK None \<in> ?A" by (simp add: JVM_states_unfold)
kleing@13215
   101
  moreover
kleing@13215
   102
  from lbv
kleing@13215
   103
  have "bounded ?step (length ins)" 
kleing@13215
   104
    by (clarsimp simp add: wt_lbv_def exec_def) 
kleing@13215
   105
       (intro bounded_lift check_bounded_is_bounded) 
kleing@13215
   106
  moreover
kleing@13215
   107
  from lbv
kleing@13215
   108
  have "cert_ok cert (length ins) Err (OK None) ?A" 
kleing@13215
   109
    by (unfold wt_lbv_def) (auto dest: check_certD)
kleing@13215
   110
  moreover
kleing@13224
   111
  have "pres_type ?step (length ins) ?A" by (rule exec_pres_type)
kleing@13215
   112
  moreover
kleing@13215
   113
  let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"
kleing@13215
   114
  from lbv
kleing@13215
   115
  have "wtl_inst_list ins cert ?f ?r Err (OK None) ?step 0 ?start \<noteq> Err"
kleing@13215
   116
    by (simp add: wt_lbv_def lbvjvm_def)    
kleing@13215
   117
  moreover
kleing@13215
   118
  from C pTs have "?start \<in> ?A"
kleing@13215
   119
    by (unfold JVM_states_unfold) (auto intro: list_appendI, force)
kleing@13215
   120
  moreover
kleing@13215
   121
  from lbv have "0 < length ins" by (simp add: wt_lbv_def)
kleing@13215
   122
  ultimately
kleing@13215
   123
  show ?thesis by (rule lbvs.wtl_sound_strong)
kleing@13215
   124
qed
kleing@13215
   125
  
kleing@13215
   126
lemma wt_lbv_wt_method:
kleing@13215
   127
  assumes wf:  "wf_prog wf_mb G"
kleing@13215
   128
  assumes lbv: "wt_lbv G C pTs rT mxs mxl et cert ins"
kleing@13215
   129
  assumes C:   "is_class G C" 
kleing@13215
   130
  assumes pTs: "set pTs \<subseteq> types G"
kleing@13215
   131
  
kleing@13215
   132
  shows "\<exists>phi. wt_method G C pTs rT mxs mxl ins et phi"
kleing@13215
   133
proof -
kleing@13215
   134
  let ?mxr   = "1 + length pTs + mxl"
kleing@13215
   135
  let ?step  = "exec G mxs rT et ins"
kleing@13215
   136
  let ?r     = "JVMType.le G mxs ?mxr"
kleing@13215
   137
  let ?f     = "JVMType.sup G mxs ?mxr"
kleing@13215
   138
  let ?A     = "states G mxs ?mxr"
kleing@13215
   139
  let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"
kleing@13215
   140
  
kleing@13215
   141
  from lbv have l: "ins \<noteq> []" by (simp add: wt_lbv_def)
kleing@13215
   142
  moreover
kleing@13215
   143
  from wf lbv C pTs
kleing@13215
   144
  obtain phi where 
kleing@13215
   145
    list:  "phi \<in> list (length ins) ?A" and
kleing@13215
   146
    step:  "wt_step ?r Err ?step phi" and    
kleing@13215
   147
    start: "?start <=_?r phi!0" 
kleing@13215
   148
    by (blast dest: wt_lbv_wt_step)
kleing@13215
   149
  from list have [simp]: "length phi = length ins" by simp
kleing@13215
   150
  have "length (map ok_val phi) = length ins" by simp  
kleing@13215
   151
  moreover
kleing@13215
   152
  from l have 0: "0 < length phi" by simp
kleing@13215
   153
  with step obtain phi0 where "phi!0 = OK phi0"
kleing@13215
   154
    by (unfold wt_step_def) blast
kleing@13215
   155
  with start 0
kleing@13215
   156
  have "wt_start G C pTs mxl (map ok_val phi)"
kleing@13215
   157
    by (simp add: wt_start_def JVM_le_Err_conv lesub_def)
kleing@13215
   158
  moreover
kleing@13215
   159
  from lbv  have chk_bounded: "check_bounded ins et"
kleing@13215
   160
    by (simp add: wt_lbv_def)
kleing@13215
   161
  moreover {
kleing@13215
   162
    from list
kleing@13215
   163
    have "check_types G mxs ?mxr phi"
kleing@13215
   164
      by (simp add: check_types_def)
kleing@13215
   165
    also from step
kleing@13224
   166
    have [symmetric]: "map OK (map ok_val phi) = phi" 
kleing@13224
   167
      by (auto intro!: map_id simp add: wt_step_def)
kleing@13215
   168
    finally have "check_types G mxs ?mxr (map OK (map ok_val phi))" .
kleing@13215
   169
  }
kleing@13215
   170
  moreover {  
kleing@13215
   171
    let ?app = "\<lambda>pc. app (ins!pc) G mxs rT pc et"
kleing@13215
   172
    let ?eff = "\<lambda>pc. eff (ins!pc) G pc et"
kleing@13215
   173
kleing@13215
   174
    from chk_bounded
kleing@13215
   175
    have "bounded (err_step (length ins) ?app ?eff) (length ins)"
kleing@13215
   176
      by (blast dest: check_bounded_is_bounded boundedD intro: bounded_err_stepI)
kleing@13215
   177
    moreover
kleing@13215
   178
    from step
kleing@13215
   179
    have "wt_err_step (sup_state_opt G) ?step phi"
kleing@13215
   180
      by (simp add: wt_err_step_def JVM_le_Err_conv)
kleing@13215
   181
    ultimately
kleing@13215
   182
    have "wt_app_eff (sup_state_opt G) ?app ?eff (map ok_val phi)"
kleing@13215
   183
      by (auto intro: wt_err_imp_wt_app_eff simp add: exec_def)
kleing@13215
   184
  }    
kleing@13215
   185
  ultimately
kleing@13215
   186
  have "wt_method G C pTs rT mxs mxl ins et (map ok_val phi)"
kleing@13215
   187
    by - (rule wt_method_def2 [THEN iffD2], simp)
kleing@13215
   188
  thus ?thesis ..
kleing@13215
   189
qed
kleing@13215
   190
kleing@13215
   191
kleing@13215
   192
lemma wt_method_wt_lbv:
kleing@13215
   193
  assumes wf:  "wf_prog wf_mb G"
kleing@13215
   194
  assumes wt:  "wt_method G C pTs rT mxs mxl ins et phi"
kleing@13215
   195
  assumes C:   "is_class G C" 
kleing@13215
   196
  assumes pTs: "set pTs \<subseteq> types G"
kleing@13215
   197
  
kleing@13215
   198
  defines [simp]: "cert \<equiv> mk_cert G mxs rT et ins phi"
kleing@13215
   199
kleing@13215
   200
  shows "wt_lbv G C pTs rT mxs mxl et cert ins"
kleing@13215
   201
proof -
kleing@13215
   202
  let ?mxr  = "1 + length pTs + mxl"
kleing@13215
   203
  let ?step = "exec G mxs rT et ins"
kleing@13215
   204
  let ?app  = "\<lambda>pc. app (ins!pc) G mxs rT pc et"
kleing@13215
   205
  let ?eff  = "\<lambda>pc. eff (ins!pc) G pc et"
kleing@13215
   206
  let ?r    = "JVMType.le G mxs ?mxr"
kleing@13215
   207
  let ?f    = "JVMType.sup G mxs ?mxr"
kleing@13215
   208
  let ?A    = "states G mxs ?mxr"
kleing@13215
   209
  let ?phi  = "map OK phi"
kleing@13215
   210
  let ?cert = "make_cert ?step ?phi (OK None)"
kleing@13215
   211
kleing@13215
   212
  from wt obtain 
kleing@13215
   213
    0:          "0 < length ins" and
kleing@13215
   214
    length:     "length ins = length ?phi" and
kleing@13215
   215
    ck_bounded: "check_bounded ins et" and
kleing@13215
   216
    ck_types:   "check_types G mxs ?mxr ?phi" and
kleing@13215
   217
    wt_start:   "wt_start G C pTs mxl phi" and
kleing@13215
   218
    app_eff:    "wt_app_eff (sup_state_opt G) ?app ?eff phi"
berghofe@13601
   219
    by (simp (asm_lr) add: wt_method_def2)
kleing@13215
   220
  
streckem@14045
   221
  have "semilat (JVMType.sl G mxs ?mxr)" 
streckem@14045
   222
    by (rule semilat_JVM_slI, rule wf_prog_ws_prog)
kleing@13215
   223
  hence "semilat (?A, ?r, ?f)" by (unfold sl_triple_conv)
kleing@13215
   224
  moreover
kleing@13215
   225
  have "top ?r Err"  by (simp add: JVM_le_unfold)
kleing@13215
   226
  moreover
kleing@13215
   227
  have "Err \<in> ?A" by (simp add: JVM_states_unfold)
kleing@13215
   228
  moreover
kleing@13215
   229
  have "bottom ?r (OK None)" 
kleing@13215
   230
    by (simp add: JVM_le_unfold bottom_def)
kleing@13215
   231
  moreover
kleing@13215
   232
  have "OK None \<in> ?A" by (simp add: JVM_states_unfold)
kleing@13215
   233
  moreover
kleing@13215
   234
  from ck_bounded
kleing@13215
   235
  have bounded: "bounded ?step (length ins)" 
kleing@13215
   236
    by (clarsimp simp add: exec_def) 
kleing@13215
   237
       (intro bounded_lift check_bounded_is_bounded)
kleing@13215
   238
  with wf
streckem@14045
   239
  have "mono ?r ?step (length ins) ?A"
streckem@14045
   240
    by (rule wf_prog_ws_prog [THEN exec_mono])
kleing@13215
   241
  hence "mono ?r ?step (length ?phi) ?A" by (simp add: length)
kleing@13215
   242
  moreover
kleing@13224
   243
  have "pres_type ?step (length ins) ?A" by (rule exec_pres_type)
kleing@13215
   244
  hence "pres_type ?step (length ?phi) ?A" by (simp add: length)
kleing@13215
   245
  moreover
kleing@13215
   246
  from ck_types
kleing@13215
   247
  have "set ?phi \<subseteq> ?A" by (simp add: check_types_def) 
kleing@13215
   248
  hence "\<forall>pc. pc < length ?phi \<longrightarrow> ?phi!pc \<in> ?A \<and> ?phi!pc \<noteq> Err" by auto
kleing@13215
   249
  moreover 
kleing@13215
   250
  from bounded 
kleing@13215
   251
  have "bounded (exec G mxs rT et ins) (length ?phi)" by (simp add: length)
kleing@13215
   252
  moreover
kleing@13215
   253
  have "OK None \<noteq> Err" by simp
kleing@13215
   254
  moreover
kleing@13215
   255
  from bounded length app_eff
kleing@13215
   256
  have "wt_err_step (sup_state_opt G) ?step ?phi"
kleing@13215
   257
    by (auto intro: wt_app_eff_imp_wt_err simp add: exec_def)
kleing@13215
   258
  hence "wt_step ?r Err ?step ?phi"
kleing@13215
   259
    by (simp add: wt_err_step_def JVM_le_Err_conv)
kleing@13215
   260
  moreover 
kleing@13215
   261
  let ?start = "OK (Some ([],(OK (Class C))#(map OK pTs)@(replicate mxl Err)))"  
kleing@13215
   262
  from 0 length have "0 < length phi" by auto
kleing@13215
   263
  hence "?phi!0 = OK (phi!0)" by simp
kleing@13215
   264
  with wt_start have "?start <=_?r ?phi!0"
kleing@13215
   265
    by (clarsimp simp add: wt_start_def lesub_def JVM_le_Err_conv)
kleing@13215
   266
  moreover
kleing@13215
   267
  from C pTs have "?start \<in> ?A"
kleing@13215
   268
    by (unfold JVM_states_unfold) (auto intro: list_appendI, force)
kleing@13215
   269
  moreover
kleing@13215
   270
  have "?start \<noteq> Err" by simp
kleing@13215
   271
  moreover
kleing@13215
   272
  note length 
kleing@13215
   273
  ultimately
kleing@13215
   274
  have "wtl_inst_list ins ?cert ?f ?r Err (OK None) ?step 0 ?start \<noteq> Err"
kleing@13215
   275
    by (rule lbvc.wtl_complete)
kleing@13215
   276
  moreover
kleing@13215
   277
  from 0 length have "phi \<noteq> []" by auto
kleing@13215
   278
  moreover
kleing@13215
   279
  from ck_types
kleing@13215
   280
  have "check_types G mxs ?mxr ?cert"
kleing@13215
   281
    by (auto simp add: make_cert_def check_types_def JVM_states_unfold)
kleing@13215
   282
  moreover
kleing@13215
   283
  note ck_bounded 0 length
kleing@13215
   284
  ultimately 
kleing@13215
   285
  show ?thesis 
kleing@13215
   286
    by (simp add: wt_lbv_def lbvjvm_def mk_cert_def 
kleing@13215
   287
      check_cert_def make_cert_def nth_append)
kleing@13215
   288
qed  
kleing@13215
   289
kleing@13215
   290
kleing@13224
   291
kleing@13224
   292
theorem jvm_lbv_correct:
kleing@13224
   293
  "wt_jvm_prog_lbv G Cert \<Longrightarrow> \<exists>Phi. wt_jvm_prog G Phi"
kleing@13224
   294
proof -  
kleing@13224
   295
  let ?Phi = "\<lambda>C sig. let (C,rT,(maxs,maxl,ins,et)) = the (method (G,C) sig) in 
kleing@13224
   296
              SOME phi. wt_method G C (snd sig) rT maxs maxl ins et phi"
kleing@13224
   297
    
kleing@13224
   298
  assume "wt_jvm_prog_lbv G Cert"
kleing@13224
   299
  hence "wt_jvm_prog G ?Phi"
kleing@13224
   300
    apply (unfold wt_jvm_prog_def wt_jvm_prog_lbv_def)
kleing@13224
   301
    apply (erule jvm_prog_lift)
kleing@13224
   302
    apply (auto dest: wt_lbv_wt_method intro: someI)
kleing@13224
   303
    done
kleing@13224
   304
  thus ?thesis by blast
kleing@13224
   305
qed
kleing@13224
   306
kleing@13215
   307
theorem jvm_lbv_complete:
kleing@13215
   308
  "wt_jvm_prog G Phi \<Longrightarrow> wt_jvm_prog_lbv G (prg_cert G Phi)"
kleing@13224
   309
  apply (unfold wt_jvm_prog_def wt_jvm_prog_lbv_def)
kleing@13224
   310
  apply (erule jvm_prog_lift)
kleing@13224
   311
  apply (auto simp add: prg_cert_def intro wt_method_wt_lbv)
kleing@13224
   312
  done  
kleing@13215
   313
kleing@13215
   314
end