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