src/HOL/MicroJava/BV/JType.thy
author berghofe
Mon Dec 10 15:26:42 2001 +0100 (2001-12-10)
changeset 12443 e56ab6134b41
parent 11085 b830bf10bf71
child 12516 d09d0f160888
permissions -rw-r--r--
Turned subcls1 into an inductive relation to make it executable.
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@11085
    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@11085
    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@11085
    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@11085
    65
    have "R = ClassT Object \<Longrightarrow> ?thesis" by simp
kleing@10497
    66
    moreover    
kleing@10497
    67
    from R wf ty
kleing@11085
    68
    have "R \<noteq> ClassT Object \<Longrightarrow> ?thesis"
berghofe@12443
    69
     by (auto simp add: is_ty_def is_class_def split_tupled_all
berghofe@12443
    70
               elim!: subcls1.elims
kleing@10497
    71
               elim: converse_rtranclE
berghofe@12443
    72
               split: ref_ty.splits)
kleing@10497
    73
    ultimately    
kleing@10497
    74
    show ?thesis by blast
kleing@10497
    75
  qed
kleing@10497
    76
qed
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@10896
   163
lemma sup_subtype_greater:
kleing@10896
   164
  "[| wf_prog wf_mb G; single_valued (subcls1 G); acyclic (subcls1 G);
kleing@10896
   165
      is_type G t1; is_type G t2; sup G t1 t2 = OK s |] 
kleing@11085
   166
  ==> subtype G t1 s \<and> subtype G t2 s"
kleing@10497
   167
proof -
kleing@10896
   168
  assume wf_prog:       "wf_prog wf_mb G"
nipkow@10797
   169
  assume single_valued: "single_valued (subcls1 G)" 
kleing@10896
   170
  assume acyclic:       "acyclic (subcls1 G)"
kleing@10896
   171
 
kleing@10497
   172
  { fix c1 c2
kleing@10497
   173
    assume is_class: "is_class G c1" "is_class G c2"
kleing@10497
   174
    with wf_prog 
kleing@10497
   175
    obtain 
kleing@11085
   176
      "G \<turnstile> c1 \<preceq>C Object"
kleing@11085
   177
      "G \<turnstile> c2 \<preceq>C Object"
kleing@10497
   178
      by (blast intro: subcls_C_Object)
nipkow@10797
   179
    with wf_prog single_valued
kleing@10497
   180
    obtain u where
kleing@10896
   181
      "is_lub ((subcls1 G)^* ) c1 c2 u"      
nipkow@10797
   182
      by (blast dest: single_valued_has_lubs)
kleing@10497
   183
    with acyclic
kleing@11085
   184
    have "G \<turnstile> c1 \<preceq>C some_lub ((subcls1 G)^* ) c1 c2 \<and>
kleing@11085
   185
          G \<turnstile> c2 \<preceq>C some_lub ((subcls1 G)^* ) c1 c2"
kleing@10497
   186
      by (simp add: some_lub_conv) (blast dest: is_lubD is_ubD)
kleing@10896
   187
  } note this [simp]
kleing@10497
   188
      
kleing@10896
   189
  assume "is_type G t1" "is_type G t2" "sup G t1 t2 = OK s"
kleing@10896
   190
  thus ?thesis
kleing@10896
   191
    apply (unfold sup_def subtype_def) 
kleing@10896
   192
    apply (cases s)
kleing@10896
   193
    apply (auto split: ty.split_asm ref_ty.split_asm split_if_asm)
kleing@10896
   194
    done
kleing@10896
   195
qed
kleing@10497
   196
kleing@10896
   197
lemma sup_subtype_smallest:
kleing@10896
   198
  "[| wf_prog wf_mb G; single_valued (subcls1 G); acyclic (subcls1 G);
kleing@10896
   199
      is_type G a; is_type G b; is_type G c; 
kleing@10896
   200
      subtype G a c; subtype G b c; sup G a b = OK d |]
kleing@10896
   201
  ==> subtype G d c"
kleing@10896
   202
proof -
kleing@10896
   203
  assume wf_prog:       "wf_prog wf_mb G"
kleing@10896
   204
  assume single_valued: "single_valued (subcls1 G)" 
kleing@10896
   205
  assume acyclic:       "acyclic (subcls1 G)"
kleing@10497
   206
kleing@10497
   207
  { fix c1 c2 D
kleing@10497
   208
    assume is_class: "is_class G c1" "is_class G c2"
kleing@11085
   209
    assume le: "G \<turnstile> c1 \<preceq>C D" "G \<turnstile> c2 \<preceq>C D"
kleing@10497
   210
    from wf_prog is_class
kleing@10497
   211
    obtain 
kleing@11085
   212
      "G \<turnstile> c1 \<preceq>C Object"
kleing@11085
   213
      "G \<turnstile> c2 \<preceq>C Object"
kleing@10497
   214
      by (blast intro: subcls_C_Object)
nipkow@10797
   215
    with wf_prog single_valued
kleing@10497
   216
    obtain u where
kleing@10497
   217
      lub: "is_lub ((subcls1 G)^* ) c1 c2 u"
nipkow@10797
   218
      by (blast dest: single_valued_has_lubs)   
kleing@10497
   219
    with acyclic
kleing@10497
   220
    have "some_lub ((subcls1 G)^* ) c1 c2 = u"
kleing@10497
   221
      by (rule some_lub_conv)
kleing@10497
   222
    moreover
kleing@10497
   223
    from lub le
kleing@11085
   224
    have "G \<turnstile> u \<preceq>C D" 
kleing@10497
   225
      by (simp add: is_lub_def is_ub_def)
kleing@10497
   226
    ultimately     
kleing@11085
   227
    have "G \<turnstile> some_lub ((subcls1 G)\<^sup>*) c1 c2 \<preceq>C D"
kleing@10497
   228
      by blast
kleing@10497
   229
  } note this [intro]
kleing@10497
   230
kleing@10497
   231
  have [dest!]:
kleing@11085
   232
    "!!C T. G \<turnstile> Class C \<preceq> T ==> \<exists>D. T=Class D \<and> G \<turnstile> C \<preceq>C D"
kleing@10497
   233
    by (frule widen_Class, auto)
kleing@10497
   234
kleing@10896
   235
  assume "is_type G a" "is_type G b" "is_type G c"
kleing@10896
   236
         "subtype G a c" "subtype G b c" "sup G a b = OK d"
kleing@10896
   237
  thus ?thesis
kleing@10896
   238
    by (auto simp add: subtype_def sup_def 
kleing@10896
   239
             split: ty.split_asm ref_ty.split_asm split_if_asm)
kleing@10896
   240
qed
kleing@10896
   241
kleing@10896
   242
lemma sup_exists:
kleing@10896
   243
  "[| subtype G a c; subtype G b c; sup G a b = Err |] ==> False"
kleing@10896
   244
  by (auto simp add: PrimT_PrimT PrimT_PrimT2 sup_def subtype_def
kleing@10896
   245
           split: ty.splits ref_ty.splits)
kleing@10497
   246
kleing@10896
   247
lemma err_semilat_JType_esl_lemma:
kleing@10896
   248
  "[| wf_prog wf_mb G; single_valued (subcls1 G); acyclic (subcls1 G) |] 
kleing@10896
   249
  ==> err_semilat (esl G)"
kleing@10896
   250
proof -
kleing@10896
   251
  assume wf_prog:   "wf_prog wf_mb G"
kleing@10896
   252
  assume single_valued: "single_valued (subcls1 G)" 
kleing@10896
   253
  assume acyclic:   "acyclic (subcls1 G)"
kleing@10896
   254
  
kleing@10896
   255
  hence "order (subtype G)"
kleing@10896
   256
    by (rule order_widen)
kleing@10896
   257
  moreover
kleing@10896
   258
  from wf_prog single_valued acyclic
kleing@10896
   259
  have "closed (err (types G)) (lift2 (sup G))"
kleing@10896
   260
    by (rule closed_err_types)
kleing@10896
   261
  moreover
kleing@10896
   262
kleing@10896
   263
  from wf_prog single_valued acyclic 
kleing@10896
   264
  have
kleing@11085
   265
    "(\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). x <=_(le (subtype G)) x +_(lift2 (sup G)) y) \<and> 
kleing@11085
   266
     (\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). y <=_(le (subtype G)) x +_(lift2 (sup G)) y)"
kleing@10896
   267
    by (auto simp add: lesub_def plussub_def le_def lift2_def sup_subtype_greater split: err.split)
kleing@10896
   268
kleing@10896
   269
  moreover    
kleing@10896
   270
kleing@10896
   271
  from wf_prog single_valued acyclic 
kleing@10497
   272
  have
kleing@11085
   273
    "\<forall>x\<in>err (types G). \<forall>y\<in>err (types G). \<forall>z\<in>err (types G). 
kleing@11085
   274
    x <=_(le (subtype G)) z \<and> y <=_(le (subtype G)) z \<longrightarrow> x +_(lift2 (sup G)) y <=_(le (subtype G)) z"
kleing@10896
   275
    by (unfold lift2_def plussub_def lesub_def le_def)
kleing@10896
   276
       (auto intro: sup_subtype_smallest sup_exists split: err.split)
kleing@10497
   277
kleing@10497
   278
  ultimately
kleing@10497
   279
  
kleing@10497
   280
  show ?thesis
kleing@10497
   281
    by (unfold esl_def semilat_def sl_def) auto
kleing@10497
   282
qed
kleing@10497
   283
nipkow@10797
   284
lemma single_valued_subcls1:
nipkow@10797
   285
  "wf_prog wf_mb G ==> single_valued (subcls1 G)"
berghofe@12443
   286
  by (auto simp add: wf_prog_def unique_def single_valued_def
berghofe@12443
   287
    intro: subcls1I elim!: subcls1.elims)
kleing@10497
   288
kleing@10592
   289
theorem err_semilat_JType_esl:
kleing@10592
   290
  "wf_prog wf_mb G ==> err_semilat (esl G)"
nipkow@10797
   291
  by (frule acyclic_subcls1, frule single_valued_subcls1, rule err_semilat_JType_esl_lemma)
kleing@10497
   292
kleing@10497
   293
end