src/HOL/MicroJava/BV/JType.thy
author kleing
Sun Jan 07 18:43:13 2001 +0100 (2001-01-07)
changeset 10812 ead84e90bfeb
parent 10797 028d22926a41
child 10896 23386a5b63eb
permissions -rw-r--r--
merged semilattice orders with <=' from Convert.thy (now defined in JVMType.thy)
kleing@10497
     1
(*  Title:      HOL/BCV/JType.thy
kleing@10497
     2
    ID:         $Id$
kleing@10812
     3
    Author:     Tobias Nipkow, Gerwin Klein
kleing@10497
     4
    Copyright   2000 TUM
kleing@10497
     5
*)
kleing@10497
     6
kleing@10812
     7
header "Java Type System as Semilattice"
kleing@10497
     8
kleing@10497
     9
theory JType = WellForm + Err:
kleing@10497
    10
kleing@10497
    11
constdefs
kleing@10497
    12
  is_ref :: "ty => bool"
kleing@10497
    13
  "is_ref T == case T of PrimT t => False | RefT r => True"
kleing@10497
    14
kleing@10497
    15
  sup :: "'c prog => ty => ty => ty err"
kleing@10497
    16
  "sup G T1 T2 ==
kleing@10497
    17
  case T1 of PrimT P1 => (case T2 of PrimT P2 => (if P1 = P2 then OK (PrimT P1) else Err) | RefT R => Err)
kleing@10497
    18
           | RefT R1 => (case T2 of PrimT P => Err | RefT R2 => 
kleing@10497
    19
  (case R1 of NullT => (case R2 of NullT => OK NT | ClassT C => OK (Class C))
kleing@10497
    20
            | ClassT C => (case R2 of NullT => OK (Class C) 
kleing@10497
    21
                                    | ClassT D => OK (Class (some_lub ((subcls1 G)^* ) C D)))))"
kleing@10497
    22
kleing@10497
    23
  subtype :: "'c prog => ty => ty => bool"
kleing@10497
    24
  "subtype G T1 T2 == G \<turnstile> T1 \<preceq> T2"
kleing@10497
    25
kleing@10497
    26
  is_ty :: "'c prog => ty => bool"
kleing@10497
    27
  "is_ty G T == case T of PrimT P => True | RefT R =>
kleing@10497
    28
               (case R of NullT => True | ClassT C => (C,Object):(subcls1 G)^*)"
kleing@10497
    29
kleing@10497
    30
translations
kleing@10497
    31
  "types G" == "Collect (is_type G)"
kleing@10497
    32
kleing@10497
    33
constdefs
kleing@10497
    34
  esl :: "'c prog => ty esl"
kleing@10497
    35
  "esl G == (types G, subtype G, sup G)"
kleing@10497
    36
kleing@10497
    37
lemma PrimT_PrimT: "(G \<turnstile> xb \<preceq> PrimT p) = (xb = PrimT p)"
kleing@10497
    38
  by (auto elim: widen.elims)
kleing@10497
    39
kleing@10497
    40
lemma PrimT_PrimT2: "(G \<turnstile> PrimT p \<preceq> xb) = (xb = PrimT p)"
kleing@10497
    41
  by (auto elim: widen.elims)
kleing@10497
    42
kleing@10497
    43
lemma is_tyI:
kleing@10497
    44
  "[| is_type G T; wf_prog wf_mb G |] ==> is_ty G T"
kleing@10630
    45
  by (auto simp add: is_ty_def intro: subcls_C_Object 
kleing@10497
    46
           split: ty.splits ref_ty.splits)
kleing@10497
    47
kleing@10497
    48
lemma is_type_conv: 
kleing@10497
    49
  "wf_prog wf_mb G ==> is_type G T = is_ty G T"
kleing@10497
    50
proof
kleing@10497
    51
  assume "is_type G T" "wf_prog wf_mb G" 
kleing@10497
    52
  thus "is_ty G T"
kleing@10497
    53
    by (rule is_tyI)
kleing@10497
    54
next
kleing@10497
    55
  assume wf: "wf_prog wf_mb G" and
kleing@10497
    56
         ty: "is_ty G T"
kleing@10497
    57
kleing@10497
    58
  show "is_type G T"
kleing@10497
    59
  proof (cases T)
kleing@10497
    60
    case PrimT
kleing@10497
    61
    thus ?thesis by simp
kleing@10497
    62
  next
kleing@10497
    63
    fix R assume R: "T = RefT R"
kleing@10497
    64
    with wf
kleing@10497
    65
    have "R = ClassT Object \<Longrightarrow> ?thesis" by simp
kleing@10497
    66
    moreover    
kleing@10497
    67
    from R wf ty
kleing@10497
    68
    have "R \<noteq> ClassT Object \<Longrightarrow> ?thesis"
oheimb@10612
    69
      by (auto simp add: is_ty_def subcls1_def 
kleing@10497
    70
               elim: converse_rtranclE
kleing@10497
    71
               split: ref_ty.splits)    
kleing@10497
    72
    ultimately    
kleing@10497
    73
    show ?thesis by blast
kleing@10497
    74
  qed
kleing@10497
    75
qed
kleing@10497
    76
kleing@10497
    77
kleing@10497
    78
lemma order_widen:
kleing@10497
    79
  "acyclic (subcls1 G) ==> order (subtype G)"
kleing@10497
    80
  apply (unfold order_def lesub_def subtype_def)
kleing@10497
    81
  apply (auto intro: widen_trans)
kleing@10497
    82
  apply (case_tac x)
kleing@10497
    83
   apply (case_tac y)
kleing@10497
    84
    apply (auto simp add: PrimT_PrimT)
kleing@10497
    85
   apply (case_tac y)
kleing@10497
    86
    apply simp
kleing@10497
    87
  apply simp
kleing@10497
    88
  apply (case_tac ref_ty)
kleing@10497
    89
   apply (case_tac ref_tya)
kleing@10497
    90
    apply simp
kleing@10497
    91
   apply simp
kleing@10497
    92
  apply (case_tac ref_tya)
kleing@10497
    93
   apply simp
kleing@10497
    94
  apply simp
kleing@10497
    95
  apply (auto dest: acyclic_impl_antisym_rtrancl antisymD)  
kleing@10497
    96
  done
kleing@10497
    97
kleing@10592
    98
lemma wf_converse_subcls1_impl_acc_subtype:
kleing@10592
    99
  "wf ((subcls1 G)^-1) ==> acc (subtype G)"
kleing@10592
   100
apply (unfold acc_def lesssub_def)
kleing@10592
   101
apply (drule_tac p = "(subcls1 G)^-1 - Id" in wf_subset)
kleing@10592
   102
 apply blast
kleing@10592
   103
apply (drule wf_trancl)
kleing@10592
   104
apply (simp add: wf_eq_minimal)
kleing@10592
   105
apply clarify
kleing@10592
   106
apply (unfold lesub_def subtype_def)
kleing@10592
   107
apply (rename_tac M T) 
kleing@10592
   108
apply (case_tac "EX C. Class C : M")
kleing@10592
   109
 prefer 2
kleing@10592
   110
 apply (case_tac T)
kleing@10592
   111
  apply (fastsimp simp add: PrimT_PrimT2)
kleing@10592
   112
 apply simp
kleing@10592
   113
 apply (subgoal_tac "ref_ty = NullT")
kleing@10592
   114
  apply simp
kleing@10592
   115
  apply (rule_tac x = NT in bexI)
kleing@10592
   116
   apply (rule allI)
kleing@10592
   117
   apply (rule impI, erule conjE)
kleing@10592
   118
   apply (drule widen_RefT)
kleing@10592
   119
   apply clarsimp
kleing@10592
   120
   apply (case_tac t)
kleing@10592
   121
    apply simp
kleing@10592
   122
   apply simp
kleing@10592
   123
  apply simp
kleing@10592
   124
 apply (case_tac ref_ty)
kleing@10592
   125
  apply simp
kleing@10592
   126
 apply simp
kleing@10592
   127
apply (erule_tac x = "{C. Class C : M}" in allE)
kleing@10592
   128
apply auto
kleing@10592
   129
apply (rename_tac D)
kleing@10592
   130
apply (rule_tac x = "Class D" in bexI)
kleing@10592
   131
 prefer 2
kleing@10592
   132
 apply assumption
kleing@10592
   133
apply clarify 
kleing@10592
   134
apply (frule widen_RefT)
kleing@10592
   135
apply (erule exE)
kleing@10592
   136
apply (case_tac t)
kleing@10592
   137
 apply simp
kleing@10592
   138
apply simp
kleing@10592
   139
apply (insert rtrancl_r_diff_Id [symmetric, standard, of "(subcls1 G)"])
kleing@10592
   140
apply simp
kleing@10592
   141
apply (erule rtranclE)
kleing@10592
   142
 apply blast
kleing@10592
   143
apply (drule rtrancl_converseI)
kleing@10592
   144
apply (subgoal_tac "((subcls1 G)-Id)^-1 = ((subcls1 G)^-1 - Id)")
kleing@10592
   145
 prefer 2
kleing@10592
   146
 apply blast
kleing@10592
   147
apply simp 
kleing@10592
   148
apply (blast intro: rtrancl_into_trancl2)
kleing@10592
   149
done 
kleing@10592
   150
kleing@10497
   151
lemma closed_err_types:
nipkow@10797
   152
  "[| wf_prog wf_mb G; single_valued (subcls1 G); acyclic (subcls1 G) |] 
kleing@10497
   153
  ==> closed (err (types G)) (lift2 (sup G))"
kleing@10497
   154
  apply (unfold closed_def plussub_def lift2_def sup_def)
kleing@10497
   155
  apply (auto split: err.split)
kleing@10497
   156
  apply (drule is_tyI, assumption)
kleing@10497
   157
  apply (auto simp add: is_ty_def is_type_conv simp del: is_type.simps 
kleing@10497
   158
              split: ty.split ref_ty.split)
kleing@10497
   159
  apply (blast dest!: is_lub_some_lub is_lubD is_ubD intro!: is_ubI)
kleing@10497
   160
  done
kleing@10497
   161
kleing@10497
   162
kleing@10497
   163
lemma err_semilat_JType_esl_lemma:
nipkow@10797
   164
  "[| wf_prog wf_mb G; single_valued (subcls1 G); acyclic (subcls1 G) |] 
kleing@10497
   165
  ==> err_semilat (esl G)"
kleing@10497
   166
proof -
kleing@10497
   167
  assume wf_prog:   "wf_prog wf_mb G"
nipkow@10797
   168
  assume single_valued: "single_valued (subcls1 G)" 
kleing@10497
   169
  assume acyclic:   "acyclic (subcls1 G)"
kleing@10497
   170
  
kleing@10497
   171
  hence "order (subtype G)"
kleing@10497
   172
    by (rule order_widen)
kleing@10497
   173
  moreover
nipkow@10797
   174
  from wf_prog single_valued acyclic
kleing@10497
   175
  have "closed (err (types G)) (lift2 (sup G))"
kleing@10497
   176
    by (rule closed_err_types)
kleing@10497
   177
  moreover
kleing@10497
   178
  
kleing@10497
   179
  { fix c1 c2
kleing@10497
   180
    assume is_class: "is_class G c1" "is_class G c2"
kleing@10497
   181
    with wf_prog 
kleing@10497
   182
    obtain 
kleing@10497
   183
      "G \<turnstile> c1 \<preceq>C Object"
kleing@10497
   184
      "G \<turnstile> c2 \<preceq>C Object"
kleing@10497
   185
      by (blast intro: subcls_C_Object)
nipkow@10797
   186
    with wf_prog single_valued
kleing@10497
   187
    obtain u where
kleing@10497
   188
      "is_lub ((subcls1 G)^* ) c1 c2 u"
nipkow@10797
   189
      by (blast dest: single_valued_has_lubs)
kleing@10497
   190
    with acyclic
kleing@10497
   191
    have "G \<turnstile> c1 \<preceq>C some_lub ((subcls1 G)^* ) c1 c2"
kleing@10497
   192
      by (simp add: some_lub_conv) (blast dest: is_lubD is_ubD)
kleing@10497
   193
  } note this [intro]
kleing@10497
   194
      
kleing@10497
   195
  { fix t1 t2 s
kleing@10497
   196
    assume "is_type G t1" "is_type G t2" "sup G t1 t2 = OK s"    
kleing@10497
   197
    hence "subtype G t1 s"
kleing@10497
   198
      by (unfold sup_def subtype_def) 
kleing@10497
   199
         (cases s, auto intro: widen.null 
kleing@10497
   200
                        split: ty.splits ref_ty.splits if_splits)
kleing@10497
   201
  } note this [intro]
kleing@10812
   202
  
kleing@10497
   203
  have
kleing@10497
   204
    "\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). x <=_(le (subtype G)) x +_(lift2 (sup G)) y"
kleing@10497
   205
    by (auto simp add: lesub_def plussub_def le_def lift2_def split: err.split)
kleing@10812
   206
kleing@10497
   207
  moreover
kleing@10497
   208
kleing@10497
   209
  { fix c1 c2
kleing@10497
   210
    assume "is_class G c1" "is_class G c2"
kleing@10497
   211
    with wf_prog
kleing@10497
   212
    obtain 
kleing@10497
   213
      "G \<turnstile> c1 \<preceq>C Object"
kleing@10497
   214
      "G \<turnstile> c2 \<preceq>C Object"
kleing@10497
   215
      by (blast intro: subcls_C_Object)
nipkow@10797
   216
    with wf_prog single_valued
kleing@10497
   217
    obtain u where
kleing@10497
   218
      "is_lub ((subcls1 G)^* ) c2 c1 u"
nipkow@10797
   219
      by (blast dest: single_valued_has_lubs)
kleing@10497
   220
    with acyclic
kleing@10497
   221
    have "G \<turnstile> c1 \<preceq>C some_lub ((subcls1 G)^* ) c2 c1"
kleing@10497
   222
      by (simp add: some_lub_conv) (blast dest: is_lubD is_ubD)
kleing@10497
   223
  } note this [intro]
kleing@10497
   224
      
kleing@10497
   225
  have "\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). 
kleing@10497
   226
    y <=_(le (subtype G)) x +_(lift2 (sup G)) y"
kleing@10497
   227
    by (auto simp add: lesub_def plussub_def le_def sup_def subtype_def lift2_def 
kleing@10497
   228
             split: err.split ty.splits ref_ty.splits if_splits intro: widen.null)
kleing@10497
   229
  moreover
kleing@10497
   230
    
kleing@10497
   231
  have [intro]: 
kleing@10497
   232
    "!!a b c. [| G \<turnstile> a \<preceq> c; G \<turnstile> b \<preceq> c; sup G a b = Err |] ==> False"
kleing@10497
   233
    by (auto simp add: PrimT_PrimT PrimT_PrimT2 sup_def 
kleing@10497
   234
             split: ty.splits ref_ty.splits)
kleing@10497
   235
kleing@10497
   236
  { fix c1 c2 D
kleing@10497
   237
    assume is_class: "is_class G c1" "is_class G c2"
kleing@10497
   238
    assume le: "G \<turnstile> c1 \<preceq>C D" "G \<turnstile> c2 \<preceq>C D"
kleing@10497
   239
    from wf_prog is_class
kleing@10497
   240
    obtain 
kleing@10497
   241
      "G \<turnstile> c1 \<preceq>C Object"
kleing@10497
   242
      "G \<turnstile> c2 \<preceq>C Object"
kleing@10497
   243
      by (blast intro: subcls_C_Object)
nipkow@10797
   244
    with wf_prog single_valued
kleing@10497
   245
    obtain u where
kleing@10497
   246
      lub: "is_lub ((subcls1 G)^* ) c1 c2 u"
nipkow@10797
   247
      by (blast dest: single_valued_has_lubs)   
kleing@10497
   248
    with acyclic
kleing@10497
   249
    have "some_lub ((subcls1 G)^* ) c1 c2 = u"
kleing@10497
   250
      by (rule some_lub_conv)
kleing@10497
   251
    moreover
kleing@10497
   252
    from lub le
kleing@10497
   253
    have "G \<turnstile> u \<preceq>C D" 
kleing@10497
   254
      by (simp add: is_lub_def is_ub_def)
kleing@10497
   255
    ultimately     
kleing@10497
   256
    have "G \<turnstile> some_lub ((subcls1 G)\<^sup>*) c1 c2 \<preceq>C D"
kleing@10497
   257
      by blast
kleing@10497
   258
  } note this [intro]
kleing@10497
   259
kleing@10497
   260
  have [dest!]:
kleing@10497
   261
    "!!C T. G \<turnstile> Class C \<preceq> T ==> \<exists>D. T=Class D \<and> G \<turnstile> C \<preceq>C D"
kleing@10497
   262
    by (frule widen_Class, auto)
kleing@10497
   263
kleing@10497
   264
  { fix a b c d
kleing@10497
   265
    assume "is_type G a" "is_type G b" "is_type G c" and
kleing@10497
   266
           "G \<turnstile> a \<preceq> c" "G \<turnstile> b \<preceq> c" and
kleing@10497
   267
           "sup G a b = OK d" 
kleing@10497
   268
    hence "G \<turnstile> d \<preceq> c"
kleing@10497
   269
      by (auto simp add: sup_def split: ty.splits ref_ty.splits if_splits)
kleing@10497
   270
  } note this [intro]
kleing@10497
   271
kleing@10497
   272
  have
kleing@10497
   273
    "\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). \<forall>z\<in>err (types G). 
kleing@10497
   274
    x <=_(le (subtype G)) z \<and> y <=_(le (subtype G)) z \<longrightarrow> x +_(lift2 (sup G)) y <=_(le (subtype G)) z"
kleing@10497
   275
    by (simp add: lift2_def plussub_def lesub_def subtype_def le_def split: err.splits) blast
kleing@10497
   276
kleing@10497
   277
  ultimately
kleing@10497
   278
  
kleing@10497
   279
  show ?thesis
kleing@10497
   280
    by (unfold esl_def semilat_def sl_def) auto
kleing@10497
   281
qed
kleing@10497
   282
nipkow@10797
   283
lemma single_valued_subcls1:
nipkow@10797
   284
  "wf_prog wf_mb G ==> single_valued (subcls1 G)"
nipkow@10797
   285
  by (unfold wf_prog_def unique_def single_valued_def subcls1_def) auto
kleing@10497
   286
kleing@10592
   287
theorem err_semilat_JType_esl:
kleing@10592
   288
  "wf_prog wf_mb G ==> err_semilat (esl G)"
nipkow@10797
   289
  by (frule acyclic_subcls1, frule single_valued_subcls1, rule err_semilat_JType_esl_lemma)
kleing@10497
   290
kleing@10497
   291
end