src/HOL/Bali/WellForm.thy
author berghofe
Sun Jan 10 18:43:45 2010 +0100 (2010-01-10)
changeset 34915 7894c7dab132
parent 32960 69916a850301
child 35067 af4c18c30593
permissions -rw-r--r--
Adapted to changes in induct method.
wenzelm@12857
     1
(*  Title:      HOL/Bali/WellForm.thy
schirmer@12925
     2
    Author:     David von Oheimb and Norbert Schirmer
schirmer@12854
     3
*)
schirmer@12854
     4
schirmer@12854
     5
header {* Well-formedness of Java programs *}
haftmann@16417
     6
theory WellForm imports DefiniteAssignment begin
schirmer@12854
     7
schirmer@12854
     8
text {*
schirmer@12854
     9
For static checks on expressions and statements, see WellType.thy
schirmer@12854
    10
schirmer@12854
    11
improvements over Java Specification 1.0 (cf. 8.4.6.3, 8.4.6.4, 9.4.1):
schirmer@12854
    12
\begin{itemize}
schirmer@12854
    13
\item a method implementing or overwriting another method may have a result 
schirmer@12854
    14
      type that widens to the result type of the other method 
schirmer@12854
    15
      (instead of identical type)
schirmer@12854
    16
\item if a method hides another method (both methods have to be static!)
schirmer@12854
    17
  there are no restrictions to the result type 
schirmer@12854
    18
  since the methods have to be static and there is no dynamic binding of 
schirmer@12854
    19
  static methods
schirmer@12854
    20
\item if an interface inherits more than one method with the same signature, the
schirmer@12854
    21
  methods need not have identical return types
schirmer@12854
    22
\end{itemize}
schirmer@12854
    23
simplifications:
schirmer@12854
    24
\begin{itemize}
schirmer@12854
    25
\item Object and standard exceptions are assumed to be declared like normal 
schirmer@12854
    26
      classes
schirmer@12854
    27
\end{itemize}
schirmer@12854
    28
*}
schirmer@12854
    29
schirmer@12854
    30
section "well-formed field declarations"
schirmer@12925
    31
text  {* well-formed field declaration (common part for classes and interfaces),
schirmer@12925
    32
        cf. 8.3 and (9.3) *}
schirmer@12854
    33
schirmer@12854
    34
constdefs
schirmer@12854
    35
  wf_fdecl :: "prog \<Rightarrow> pname \<Rightarrow> fdecl \<Rightarrow> bool"
schirmer@12854
    36
 "wf_fdecl G P \<equiv> \<lambda>(fn,f). is_acc_type G P (type f)"
schirmer@12854
    37
schirmer@12854
    38
lemma wf_fdecl_def2: "\<And>fd. wf_fdecl G P fd = is_acc_type G P (type (snd fd))"
schirmer@12854
    39
apply (unfold wf_fdecl_def)
schirmer@12854
    40
apply simp
schirmer@12854
    41
done
schirmer@12854
    42
schirmer@12854
    43
schirmer@12854
    44
schirmer@12854
    45
section "well-formed method declarations"
schirmer@12854
    46
  (*well-formed method declaration,cf. 8.4, 8.4.1, 8.4.3, 8.4.5, 14.3.2, (9.4)*)
schirmer@12854
    47
  (* cf. 14.15, 15.7.2, for scope issues cf. 8.4.1 and 14.3.2 *)
schirmer@12854
    48
schirmer@12854
    49
text {*
schirmer@12854
    50
A method head is wellformed if:
schirmer@12854
    51
\begin{itemize}
schirmer@12854
    52
\item the signature and the method head agree in the number of parameters
schirmer@12854
    53
\item all types of the parameters are visible
schirmer@12854
    54
\item the result type is visible
schirmer@12854
    55
\item the parameter names are unique
schirmer@12854
    56
\end{itemize} 
schirmer@12854
    57
*}
schirmer@12854
    58
constdefs
schirmer@12854
    59
  wf_mhead :: "prog \<Rightarrow> pname \<Rightarrow> sig \<Rightarrow> mhead \<Rightarrow> bool"
schirmer@12854
    60
 "wf_mhead G P \<equiv> \<lambda> sig mh. length (parTs sig) = length (pars mh) \<and>
wenzelm@32960
    61
                            \<spacespace> ( \<forall>T\<in>set (parTs sig). is_acc_type G P T) \<and> 
schirmer@12854
    62
                            is_acc_type G P (resTy mh) \<and>
wenzelm@32960
    63
                            \<spacespace> distinct (pars mh)"
schirmer@12854
    64
schirmer@12854
    65
schirmer@12854
    66
text {*
schirmer@12854
    67
A method declaration is wellformed if:
schirmer@12854
    68
\begin{itemize}
schirmer@12854
    69
\item the method head is wellformed
schirmer@12854
    70
\item the names of the local variables are unique
schirmer@12854
    71
\item the types of the local variables must be accessible
schirmer@12854
    72
\item the local variables don't shadow the parameters
schirmer@12854
    73
\item the class of the method is defined
schirmer@12854
    74
\item the body statement is welltyped with respect to the
schirmer@12854
    75
      modified environment of local names, were the local variables, 
schirmer@12854
    76
      the parameters the special result variable (Res) and This are assoziated
schirmer@12854
    77
      with there types. 
schirmer@12854
    78
\end{itemize}
schirmer@12854
    79
*}
schirmer@12854
    80
schirmer@13688
    81
constdefs callee_lcl:: "qtname \<Rightarrow> sig \<Rightarrow> methd \<Rightarrow> lenv"
schirmer@13688
    82
"callee_lcl C sig m 
schirmer@13688
    83
 \<equiv> \<lambda> k. (case k of
schirmer@13688
    84
            EName e 
schirmer@13688
    85
            \<Rightarrow> (case e of 
schirmer@13688
    86
                  VNam v 
schirmer@13688
    87
                  \<Rightarrow>(table_of (lcls (mbody m))((pars m)[\<mapsto>](parTs sig))) v
schirmer@13688
    88
                | Res \<Rightarrow> Some (resTy m))
wenzelm@32960
    89
          | This \<Rightarrow> if is_static m then None else Some (Class C))"
schirmer@13688
    90
schirmer@13688
    91
constdefs parameters :: "methd \<Rightarrow> lname set"
schirmer@13688
    92
"parameters m \<equiv>  set (map (EName \<circ> VNam) (pars m)) 
schirmer@13688
    93
                  \<union> (if (static m) then {} else {This})"
schirmer@13688
    94
schirmer@12854
    95
constdefs
schirmer@12854
    96
  wf_mdecl :: "prog \<Rightarrow> qtname \<Rightarrow> mdecl \<Rightarrow> bool"
schirmer@12854
    97
 "wf_mdecl G C \<equiv> 
schirmer@12854
    98
      \<lambda>(sig,m).
wenzelm@32960
    99
          wf_mhead G (pid C) sig (mhead m) \<and> 
schirmer@12854
   100
          unique (lcls (mbody m)) \<and> 
schirmer@12854
   101
          (\<forall>(vn,T)\<in>set (lcls (mbody m)). is_acc_type G (pid C) T) \<and> 
wenzelm@32960
   102
          (\<forall>pn\<in>set (pars m). table_of (lcls (mbody m)) pn = None) \<and>
schirmer@13688
   103
          jumpNestingOkS {Ret} (stmt (mbody m)) \<and> 
schirmer@12854
   104
          is_class G C \<and>
schirmer@13688
   105
          \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr>\<turnstile>(stmt (mbody m))\<Colon>\<surd> \<and>
schirmer@13688
   106
          (\<exists> A. \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr> 
schirmer@13688
   107
                \<turnstile> parameters m \<guillemotright>\<langle>stmt (mbody m)\<rangle>\<guillemotright> A 
schirmer@13688
   108
               \<and> Result \<in> nrm A)"
schirmer@13688
   109
schirmer@13688
   110
lemma callee_lcl_VNam_simp [simp]:
schirmer@13688
   111
"callee_lcl C sig m (EName (VNam v)) 
schirmer@13688
   112
  = (table_of (lcls (mbody m))((pars m)[\<mapsto>](parTs sig))) v"
schirmer@13688
   113
by (simp add: callee_lcl_def)
schirmer@13688
   114
 
schirmer@13688
   115
lemma callee_lcl_Res_simp [simp]:
schirmer@13688
   116
"callee_lcl C sig m (EName Res) = Some (resTy m)" 
schirmer@13688
   117
by (simp add: callee_lcl_def)
schirmer@13688
   118
schirmer@13688
   119
lemma callee_lcl_This_simp [simp]:
schirmer@13688
   120
"callee_lcl C sig m (This) = (if is_static m then None else Some (Class C))" 
schirmer@13688
   121
by (simp add: callee_lcl_def)
schirmer@13688
   122
schirmer@13688
   123
lemma callee_lcl_This_static_simp:
schirmer@13688
   124
"is_static m \<Longrightarrow> callee_lcl C sig m (This) = None"
schirmer@13688
   125
by simp
schirmer@13688
   126
schirmer@13688
   127
lemma callee_lcl_This_not_static_simp:
schirmer@13688
   128
"\<not> is_static m \<Longrightarrow> callee_lcl C sig m (This) = Some (Class C)"
schirmer@13688
   129
by simp
schirmer@12854
   130
schirmer@12854
   131
lemma wf_mheadI: 
schirmer@12854
   132
"\<lbrakk>length (parTs sig) = length (pars m); \<forall>T\<in>set (parTs sig). is_acc_type G P T;
wenzelm@12893
   133
  is_acc_type G P (resTy m); distinct (pars m)\<rbrakk> \<Longrightarrow>  
schirmer@12854
   134
  wf_mhead G P sig m"
schirmer@12854
   135
apply (unfold wf_mhead_def)
schirmer@12854
   136
apply (simp (no_asm_simp))
schirmer@12854
   137
done
schirmer@12854
   138
schirmer@12854
   139
lemma wf_mdeclI: "\<lbrakk>  
schirmer@12854
   140
  wf_mhead G (pid C) sig (mhead m); unique (lcls (mbody m));  
schirmer@12854
   141
  (\<forall>pn\<in>set (pars m). table_of (lcls (mbody m)) pn = None); 
schirmer@12854
   142
  \<forall>(vn,T)\<in>set (lcls (mbody m)). is_acc_type G (pid C) T;
schirmer@13688
   143
  jumpNestingOkS {Ret} (stmt (mbody m));
schirmer@12854
   144
  is_class G C;
schirmer@13688
   145
  \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr>\<turnstile>(stmt (mbody m))\<Colon>\<surd>;
schirmer@13688
   146
  (\<exists> A. \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr> \<turnstile> parameters m \<guillemotright>\<langle>stmt (mbody m)\<rangle>\<guillemotright> A
schirmer@13688
   147
        \<and> Result \<in> nrm A)
schirmer@12854
   148
  \<rbrakk> \<Longrightarrow>  
schirmer@12854
   149
  wf_mdecl G C (sig,m)"
schirmer@12854
   150
apply (unfold wf_mdecl_def)
schirmer@12854
   151
apply simp
schirmer@12854
   152
done
schirmer@12854
   153
schirmer@13688
   154
lemma wf_mdeclE [consumes 1]:  
schirmer@13688
   155
  "\<lbrakk>wf_mdecl G C (sig,m); 
schirmer@13688
   156
    \<lbrakk>wf_mhead G (pid C) sig (mhead m); unique (lcls (mbody m));  
schirmer@13688
   157
     \<forall>pn\<in>set (pars m). table_of (lcls (mbody m)) pn = None; 
schirmer@13688
   158
     \<forall>(vn,T)\<in>set (lcls (mbody m)). is_acc_type G (pid C) T;
schirmer@13688
   159
     jumpNestingOkS {Ret} (stmt (mbody m));
schirmer@13688
   160
     is_class G C;
schirmer@13688
   161
     \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr>\<turnstile>(stmt (mbody m))\<Colon>\<surd>;
schirmer@13688
   162
   (\<exists> A. \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr>\<turnstile> parameters m \<guillemotright>\<langle>stmt (mbody m)\<rangle>\<guillemotright> A
schirmer@13688
   163
        \<and> Result \<in> nrm A)
schirmer@13688
   164
    \<rbrakk> \<Longrightarrow> P
schirmer@13688
   165
  \<rbrakk> \<Longrightarrow> P"
schirmer@13688
   166
by (unfold wf_mdecl_def) simp
schirmer@13688
   167
schirmer@12854
   168
schirmer@12854
   169
lemma wf_mdeclD1: 
schirmer@12854
   170
"wf_mdecl G C (sig,m) \<Longrightarrow>  
schirmer@12854
   171
   wf_mhead G (pid C) sig (mhead m) \<and> unique (lcls (mbody m)) \<and>  
schirmer@12854
   172
  (\<forall>pn\<in>set (pars m). table_of (lcls (mbody m)) pn = None) \<and> 
schirmer@12854
   173
  (\<forall>(vn,T)\<in>set (lcls (mbody m)). is_acc_type G (pid C) T)"
schirmer@12854
   174
apply (unfold wf_mdecl_def)
schirmer@13688
   175
apply simp
schirmer@12854
   176
done
schirmer@12854
   177
schirmer@12854
   178
lemma wf_mdecl_bodyD: 
schirmer@12854
   179
"wf_mdecl G C (sig,m) \<Longrightarrow>  
schirmer@13688
   180
 (\<exists>T. \<lparr>prg=G,cls=C,lcl=callee_lcl C sig m\<rparr>\<turnstile>Body C (stmt (mbody m))\<Colon>-T \<and> 
schirmer@13688
   181
      G\<turnstile>T\<preceq>(resTy m))"
schirmer@12854
   182
apply (unfold wf_mdecl_def)
schirmer@12854
   183
apply clarify
schirmer@12854
   184
apply (rule_tac x="(resTy m)" in exI)
schirmer@12854
   185
apply (unfold wf_mhead_def)
schirmer@12854
   186
apply (auto simp add: wf_mhead_def is_acc_type_def intro: wt.Body )
schirmer@12854
   187
done
schirmer@12854
   188
schirmer@12854
   189
schirmer@12854
   190
(*
schirmer@12854
   191
lemma static_Object_methodsE [elim!]: 
schirmer@12854
   192
 "\<lbrakk>wf_mdecl G Object (sig, m);static m\<rbrakk> \<Longrightarrow> R"
schirmer@12854
   193
apply (unfold wf_mdecl_def)
schirmer@12854
   194
apply auto
schirmer@12854
   195
done
schirmer@12854
   196
*)
schirmer@12854
   197
schirmer@12854
   198
lemma rT_is_acc_type: 
schirmer@12854
   199
  "wf_mhead G P sig m \<Longrightarrow> is_acc_type G P (resTy m)"
schirmer@12854
   200
apply (unfold wf_mhead_def)
schirmer@12854
   201
apply auto
schirmer@12854
   202
done
schirmer@12854
   203
schirmer@12854
   204
section "well-formed interface declarations"
schirmer@12854
   205
  (* well-formed interface declaration, cf. 9.1, 9.1.2.1, 9.1.3, 9.4 *)
schirmer@12854
   206
schirmer@12854
   207
text {*
schirmer@12854
   208
A interface declaration is wellformed if:
schirmer@12854
   209
\begin{itemize}
schirmer@12854
   210
\item the interface hierarchy is wellstructured
schirmer@12854
   211
\item there is no class with the same name
schirmer@12854
   212
\item the method heads are wellformed and not static and have Public access
schirmer@12854
   213
\item the methods are uniquely named
schirmer@12854
   214
\item all superinterfaces are accessible
schirmer@12854
   215
\item the result type of a method overriding a method of Object widens to the
schirmer@12854
   216
      result type of the overridden method.
schirmer@12854
   217
      Shadowing static methods is forbidden.
schirmer@12854
   218
\item the result type of a method overriding a set of methods defined in the
schirmer@12854
   219
      superinterfaces widens to each of the corresponding result types
schirmer@12854
   220
\end{itemize}
schirmer@12854
   221
*}
schirmer@12854
   222
constdefs
schirmer@12854
   223
  wf_idecl :: "prog  \<Rightarrow> idecl \<Rightarrow> bool"
schirmer@12854
   224
 "wf_idecl G \<equiv> 
schirmer@12854
   225
    \<lambda>(I,i). 
schirmer@12854
   226
        ws_idecl G I (isuperIfs i) \<and> 
wenzelm@32960
   227
        \<not>is_class G I \<and>
wenzelm@32960
   228
        (\<forall>(sig,mh)\<in>set (imethods i). wf_mhead G (pid I) sig mh \<and> 
schirmer@12854
   229
                                     \<not>is_static mh \<and>
schirmer@12854
   230
                                      accmodi mh = Public) \<and>
wenzelm@32960
   231
        unique (imethods i) \<and>
schirmer@12854
   232
        (\<forall> J\<in>set (isuperIfs i). is_acc_iface G (pid I) J) \<and>
schirmer@12854
   233
        (table_of (imethods i)
schirmer@12854
   234
          hiding (methd G Object)
schirmer@12854
   235
          under  (\<lambda> new old. accmodi old \<noteq> Private)
schirmer@12854
   236
          entails (\<lambda>new old. G\<turnstile>resTy new\<preceq>resTy old \<and> 
schirmer@12854
   237
                             is_static new = is_static old)) \<and> 
nipkow@30235
   238
        (Option.set \<circ> table_of (imethods i) 
schirmer@12854
   239
               hidings Un_tables((\<lambda>J.(imethds G J))`set (isuperIfs i))
wenzelm@32960
   240
               entails (\<lambda>new old. G\<turnstile>resTy new\<preceq>resTy old))"
schirmer@12854
   241
schirmer@12854
   242
lemma wf_idecl_mhead: "\<lbrakk>wf_idecl G (I,i); (sig,mh)\<in>set (imethods i)\<rbrakk> \<Longrightarrow>  
schirmer@12854
   243
  wf_mhead G (pid I) sig mh \<and> \<not>is_static mh \<and> accmodi mh = Public"
schirmer@12854
   244
apply (unfold wf_idecl_def)
schirmer@12854
   245
apply auto
schirmer@12854
   246
done
schirmer@12854
   247
schirmer@12854
   248
lemma wf_idecl_hidings: 
schirmer@12854
   249
"wf_idecl G (I, i) \<Longrightarrow> 
nipkow@30235
   250
  (\<lambda>s. Option.set (table_of (imethods i) s)) 
schirmer@12854
   251
  hidings Un_tables ((\<lambda>J. imethds G J) ` set (isuperIfs i))  
schirmer@12854
   252
  entails \<lambda>new old. G\<turnstile>resTy new\<preceq>resTy old"
schirmer@12854
   253
apply (unfold wf_idecl_def o_def)
schirmer@12854
   254
apply simp
schirmer@12854
   255
done
schirmer@12854
   256
schirmer@12854
   257
lemma wf_idecl_hiding:
schirmer@12854
   258
"wf_idecl G (I, i) \<Longrightarrow> 
schirmer@12854
   259
 (table_of (imethods i)
schirmer@12854
   260
           hiding (methd G Object)
schirmer@12854
   261
           under  (\<lambda> new old. accmodi old \<noteq> Private)
schirmer@12854
   262
           entails (\<lambda>new old. G\<turnstile>resTy new\<preceq>resTy old \<and> 
schirmer@12854
   263
                              is_static new = is_static old))"
schirmer@12854
   264
apply (unfold wf_idecl_def)
schirmer@12854
   265
apply simp
schirmer@12854
   266
done
schirmer@12854
   267
schirmer@12854
   268
lemma wf_idecl_supD: 
schirmer@12854
   269
"\<lbrakk>wf_idecl G (I,i); J \<in> set (isuperIfs i)\<rbrakk> 
schirmer@12854
   270
 \<Longrightarrow> is_acc_iface G (pid I) J \<and> (J, I) \<notin> (subint1 G)^+"
schirmer@12854
   271
apply (unfold wf_idecl_def ws_idecl_def)
schirmer@12854
   272
apply auto
schirmer@12854
   273
done
schirmer@12854
   274
schirmer@12854
   275
section "well-formed class declarations"
schirmer@12854
   276
  (* well-formed class declaration, cf. 8.1, 8.1.2.1, 8.1.2.2, 8.1.3, 8.1.4 and
schirmer@12854
   277
   class method declaration, cf. 8.4.3.3, 8.4.6.1, 8.4.6.2, 8.4.6.3, 8.4.6.4 *)
schirmer@12854
   278
schirmer@12854
   279
text {*
schirmer@12854
   280
A class declaration is wellformed if:
schirmer@12854
   281
\begin{itemize}
schirmer@12854
   282
\item there is no interface with the same name
schirmer@12854
   283
\item all superinterfaces are accessible and for all methods implementing 
schirmer@12854
   284
      an interface method the result type widens to the result type of 
schirmer@12854
   285
      the interface method, the method is not static and offers at least 
schirmer@12854
   286
      as much access 
schirmer@12854
   287
      (this actually means that the method has Public access, since all 
schirmer@12854
   288
      interface methods have public access)
schirmer@12854
   289
\item all field declarations are wellformed and the field names are unique
schirmer@12854
   290
\item all method declarations are wellformed and the method names are unique
schirmer@12854
   291
\item the initialization statement is welltyped
schirmer@12854
   292
\item the classhierarchy is wellstructured
schirmer@12854
   293
\item Unless the class is Object:
schirmer@12854
   294
      \begin{itemize}
schirmer@12854
   295
      \item the superclass is accessible
schirmer@12854
   296
      \item for all methods overriding another method (of a superclass )the
schirmer@12854
   297
            result type widens to the result type of the overridden method,
schirmer@12854
   298
            the access modifier of the new method provides at least as much
schirmer@12854
   299
            access as the overwritten one.
schirmer@12854
   300
      \item for all methods hiding a method (of a superclass) the hidden 
schirmer@12854
   301
            method must be static and offer at least as much access rights.
schirmer@12854
   302
            Remark: In contrast to the Java Language Specification we don't
schirmer@12854
   303
            restrict the result types of the method
schirmer@12854
   304
            (as in case of overriding), because there seems to be no reason,
schirmer@12854
   305
            since there is no dynamic binding of static methods.
schirmer@12854
   306
            (cf. 8.4.6.3 vs. 15.12.1).
schirmer@12854
   307
            Stricly speaking the restrictions on the access rights aren't 
schirmer@12854
   308
            necessary to, since the static type and the access rights 
schirmer@12854
   309
            together determine which method is to be called statically. 
schirmer@12854
   310
            But if a class gains more then one static method with the 
schirmer@12854
   311
            same signature due to inheritance, it is confusing when the 
schirmer@12854
   312
            method selection depends on the access rights only: 
schirmer@12854
   313
            e.g.
schirmer@12854
   314
              Class C declares static public method foo().
schirmer@12854
   315
              Class D is subclass of C and declares static method foo()
schirmer@12854
   316
              with default package access.
schirmer@12854
   317
              D.foo() ? if this call is in the same package as D then
schirmer@12854
   318
                        foo of class D is called, otherwise foo of class C.
schirmer@12854
   319
      \end{itemize}
schirmer@12854
   320
schirmer@12854
   321
\end{itemize}
schirmer@12854
   322
*}
schirmer@12854
   323
(* to Table *)
schirmer@12854
   324
constdefs entails:: "('a,'b) table \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> bool"
schirmer@12854
   325
                                 ("_ entails _" 20)
schirmer@12854
   326
"t entails P \<equiv> \<forall>k. \<forall> x \<in> t k: P x"
schirmer@12854
   327
schirmer@12854
   328
lemma entailsD:
schirmer@12854
   329
 "\<lbrakk>t entails P; t k = Some x\<rbrakk> \<Longrightarrow> P x"
schirmer@12854
   330
by (simp add: entails_def)
schirmer@12854
   331
schirmer@12854
   332
lemma empty_entails[simp]: "empty entails P"
schirmer@12854
   333
by (simp add: entails_def)
schirmer@12854
   334
schirmer@12854
   335
constdefs
schirmer@12854
   336
 wf_cdecl :: "prog \<Rightarrow> cdecl \<Rightarrow> bool"
schirmer@12854
   337
"wf_cdecl G \<equiv> 
schirmer@12854
   338
   \<lambda>(C,c).
schirmer@12854
   339
      \<not>is_iface G C \<and>
schirmer@12854
   340
      (\<forall>I\<in>set (superIfs c). is_acc_iface G (pid C) I \<and>
schirmer@12854
   341
        (\<forall>s. \<forall> im \<in> imethds G I s.
wenzelm@32960
   342
            (\<exists> cm \<in> methd  G C s: G\<turnstile>resTy cm\<preceq>resTy im \<and>
wenzelm@32960
   343
                                     \<not> is_static cm \<and>
schirmer@12854
   344
                                     accmodi im \<le> accmodi cm))) \<and>
schirmer@12854
   345
      (\<forall>f\<in>set (cfields c). wf_fdecl G (pid C) f) \<and> unique (cfields c) \<and> 
schirmer@13688
   346
      (\<forall>m\<in>set (methods c). wf_mdecl G C m) \<and> unique (methods c) \<and>
schirmer@13688
   347
      jumpNestingOkS {} (init c) \<and>
schirmer@13688
   348
      (\<exists> A. \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile> {} \<guillemotright>\<langle>init c\<rangle>\<guillemotright> A) \<and>
schirmer@12854
   349
      \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile>(init c)\<Colon>\<surd> \<and> ws_cdecl G C (super c) \<and>
schirmer@12854
   350
      (C \<noteq> Object \<longrightarrow> 
schirmer@12854
   351
            (is_acc_class G (pid C) (super c) \<and>
schirmer@12854
   352
            (table_of (map (\<lambda> (s,m). (s,C,m)) (methods c)) 
schirmer@12854
   353
             entails (\<lambda> new. \<forall> old sig. 
schirmer@12854
   354
                       (G,sig\<turnstile>new overrides\<^sub>S old 
schirmer@12854
   355
                        \<longrightarrow> (G\<turnstile>resTy new\<preceq>resTy old \<and>
schirmer@12854
   356
                             accmodi old \<le> accmodi new \<and>
wenzelm@32960
   357
                             \<not>is_static old)) \<and>
schirmer@12854
   358
                       (G,sig\<turnstile>new hides old 
schirmer@12854
   359
                         \<longrightarrow> (accmodi old \<le> accmodi new \<and>
wenzelm@32960
   360
                              is_static old)))) 
schirmer@12854
   361
            ))"
schirmer@12854
   362
schirmer@12854
   363
(*
schirmer@12854
   364
constdefs
schirmer@12854
   365
 wf_cdecl :: "prog \<Rightarrow> cdecl \<Rightarrow> bool"
schirmer@12854
   366
"wf_cdecl G \<equiv> 
schirmer@12854
   367
   \<lambda>(C,c).
schirmer@12854
   368
      \<not>is_iface G C \<and>
schirmer@12854
   369
      (\<forall>I\<in>set (superIfs c). is_acc_iface G (pid C) I \<and>
schirmer@12854
   370
        (\<forall>s. \<forall> im \<in> imethds G I s.
wenzelm@32960
   371
            (\<exists> cm \<in> methd  G C s: G\<turnstile>resTy (mthd cm)\<preceq>resTy (mthd im) \<and>
wenzelm@32960
   372
                                     \<not> is_static cm \<and>
schirmer@12854
   373
                                     accmodi im \<le> accmodi cm))) \<and>
schirmer@12854
   374
      (\<forall>f\<in>set (cfields c). wf_fdecl G (pid C) f) \<and> unique (cfields c) \<and> 
schirmer@12854
   375
      (\<forall>m\<in>set (methods c). wf_mdecl G C m) \<and> unique (methods c) \<and> 
schirmer@12854
   376
      \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile>(init c)\<Colon>\<surd> \<and> ws_cdecl G C (super c) \<and>
schirmer@12854
   377
      (C \<noteq> Object \<longrightarrow> 
schirmer@12854
   378
            (is_acc_class G (pid C) (super c) \<and>
schirmer@12854
   379
            (table_of (map (\<lambda> (s,m). (s,C,m)) (methods c)) 
schirmer@12854
   380
              hiding methd G (super c)
schirmer@12854
   381
              under (\<lambda> new old. G\<turnstile>new overrides old)
schirmer@12854
   382
              entails (\<lambda> new old. 
schirmer@12854
   383
                           (G\<turnstile>resTy (mthd new)\<preceq>resTy (mthd old) \<and>
schirmer@12854
   384
                            accmodi old \<le> accmodi new \<and>
wenzelm@32960
   385
                           \<not> is_static old)))  \<and>
schirmer@12854
   386
            (table_of (map (\<lambda> (s,m). (s,C,m)) (methods c)) 
schirmer@12854
   387
              hiding methd G (super c)
schirmer@12854
   388
              under (\<lambda> new old. G\<turnstile>new hides old)
schirmer@12854
   389
              entails (\<lambda> new old. is_static old \<and> 
schirmer@12854
   390
                                  accmodi old \<le> accmodi new))  \<and>
schirmer@12854
   391
            (table_of (cfields c) hiding accfield G C (super c)
schirmer@12854
   392
              entails (\<lambda> newF oldF. accmodi oldF \<le> access newF))))"
schirmer@12854
   393
*)
schirmer@12854
   394
schirmer@13688
   395
lemma wf_cdeclE [consumes 1]: 
schirmer@13688
   396
 "\<lbrakk>wf_cdecl G (C,c);
schirmer@13688
   397
   \<lbrakk>\<not>is_iface G C;
schirmer@13688
   398
    (\<forall>I\<in>set (superIfs c). is_acc_iface G (pid C) I \<and>
schirmer@13688
   399
        (\<forall>s. \<forall> im \<in> imethds G I s.
wenzelm@32960
   400
            (\<exists> cm \<in> methd  G C s: G\<turnstile>resTy cm\<preceq>resTy im \<and>
wenzelm@32960
   401
                                     \<not> is_static cm \<and>
schirmer@13688
   402
                                     accmodi im \<le> accmodi cm))); 
schirmer@13688
   403
      \<forall>f\<in>set (cfields c). wf_fdecl G (pid C) f; unique (cfields c); 
schirmer@13688
   404
      \<forall>m\<in>set (methods c). wf_mdecl G C m; unique (methods c);
schirmer@13688
   405
      jumpNestingOkS {} (init c);
schirmer@13688
   406
      \<exists> A. \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile> {} \<guillemotright>\<langle>init c\<rangle>\<guillemotright> A;
schirmer@13688
   407
      \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile>(init c)\<Colon>\<surd>; 
schirmer@13688
   408
      ws_cdecl G C (super c); 
schirmer@13688
   409
      (C \<noteq> Object \<longrightarrow> 
schirmer@13688
   410
            (is_acc_class G (pid C) (super c) \<and>
schirmer@13688
   411
            (table_of (map (\<lambda> (s,m). (s,C,m)) (methods c)) 
schirmer@13688
   412
             entails (\<lambda> new. \<forall> old sig. 
schirmer@13688
   413
                       (G,sig\<turnstile>new overrides\<^sub>S old 
schirmer@13688
   414
                        \<longrightarrow> (G\<turnstile>resTy new\<preceq>resTy old \<and>
schirmer@13688
   415
                             accmodi old \<le> accmodi new \<and>
wenzelm@32960
   416
                             \<not>is_static old)) \<and>
schirmer@13688
   417
                       (G,sig\<turnstile>new hides old 
schirmer@13688
   418
                         \<longrightarrow> (accmodi old \<le> accmodi new \<and>
wenzelm@32960
   419
                              is_static old)))) 
schirmer@13688
   420
            ))\<rbrakk> \<Longrightarrow> P
schirmer@13688
   421
  \<rbrakk> \<Longrightarrow> P"
schirmer@13688
   422
by (unfold wf_cdecl_def) simp
schirmer@13688
   423
schirmer@12854
   424
lemma wf_cdecl_unique: 
schirmer@12854
   425
"wf_cdecl G (C,c) \<Longrightarrow> unique (cfields c) \<and> unique (methods c)"
schirmer@12854
   426
apply (unfold wf_cdecl_def)
schirmer@12854
   427
apply auto
schirmer@12854
   428
done
schirmer@12854
   429
schirmer@12854
   430
lemma wf_cdecl_fdecl: 
schirmer@12854
   431
"\<lbrakk>wf_cdecl G (C,c); f\<in>set (cfields c)\<rbrakk> \<Longrightarrow> wf_fdecl G (pid C) f"
schirmer@12854
   432
apply (unfold wf_cdecl_def)
schirmer@12854
   433
apply auto
schirmer@12854
   434
done
schirmer@12854
   435
schirmer@12854
   436
lemma wf_cdecl_mdecl: 
schirmer@12854
   437
"\<lbrakk>wf_cdecl G (C,c); m\<in>set (methods c)\<rbrakk> \<Longrightarrow> wf_mdecl G C m"
schirmer@12854
   438
apply (unfold wf_cdecl_def)
schirmer@12854
   439
apply auto
schirmer@12854
   440
done
schirmer@12854
   441
schirmer@12854
   442
lemma wf_cdecl_impD: 
schirmer@12854
   443
"\<lbrakk>wf_cdecl G (C,c); I\<in>set (superIfs c)\<rbrakk> 
schirmer@12854
   444
\<Longrightarrow> is_acc_iface G (pid C) I \<and>  
schirmer@12854
   445
    (\<forall>s. \<forall>im \<in> imethds G I s.  
schirmer@12854
   446
        (\<exists>cm \<in> methd G C s: G\<turnstile>resTy cm\<preceq>resTy im \<and> \<not>is_static cm \<and>
schirmer@12854
   447
                                   accmodi im \<le> accmodi cm))"
schirmer@12854
   448
apply (unfold wf_cdecl_def)
schirmer@12854
   449
apply auto
schirmer@12854
   450
done
schirmer@12854
   451
schirmer@12854
   452
lemma wf_cdecl_supD: 
schirmer@12854
   453
"\<lbrakk>wf_cdecl G (C,c); C \<noteq> Object\<rbrakk> \<Longrightarrow>  
schirmer@12854
   454
  is_acc_class G (pid C) (super c) \<and> (super c,C) \<notin> (subcls1 G)^+ \<and> 
schirmer@12854
   455
   (table_of (map (\<lambda> (s,m). (s,C,m)) (methods c)) 
schirmer@12854
   456
    entails (\<lambda> new. \<forall> old sig. 
schirmer@12854
   457
                 (G,sig\<turnstile>new overrides\<^sub>S old 
schirmer@12854
   458
                  \<longrightarrow> (G\<turnstile>resTy new\<preceq>resTy old \<and>
schirmer@12854
   459
                       accmodi old \<le> accmodi new \<and>
schirmer@12854
   460
                       \<not>is_static old)) \<and>
schirmer@12854
   461
                 (G,sig\<turnstile>new hides old 
schirmer@12854
   462
                   \<longrightarrow> (accmodi old \<le> accmodi new \<and>
schirmer@12854
   463
                        is_static old))))"
schirmer@12854
   464
apply (unfold wf_cdecl_def ws_cdecl_def)
schirmer@12854
   465
apply auto
schirmer@12854
   466
done
schirmer@12854
   467
schirmer@12854
   468
schirmer@12854
   469
lemma wf_cdecl_overrides_SomeD:
schirmer@12854
   470
"\<lbrakk>wf_cdecl G (C,c); C \<noteq> Object; table_of (methods c) sig = Some newM;
schirmer@12854
   471
  G,sig\<turnstile>(C,newM) overrides\<^sub>S old
schirmer@12854
   472
\<rbrakk> \<Longrightarrow>  G\<turnstile>resTy newM\<preceq>resTy old \<and>
schirmer@12854
   473
       accmodi old \<le> accmodi newM \<and>
schirmer@12854
   474
       \<not> is_static old" 
schirmer@12854
   475
apply (drule (1) wf_cdecl_supD)
schirmer@12854
   476
apply (clarify)
schirmer@12854
   477
apply (drule entailsD)
schirmer@12854
   478
apply   (blast intro: table_of_map_SomeI)
schirmer@12854
   479
apply (drule_tac x="old" in spec)
schirmer@12854
   480
apply (auto dest: overrides_eq_sigD simp add: msig_def)
schirmer@12854
   481
done
schirmer@12854
   482
schirmer@12854
   483
lemma wf_cdecl_hides_SomeD:
schirmer@12854
   484
"\<lbrakk>wf_cdecl G (C,c); C \<noteq> Object; table_of (methods c) sig = Some newM;
schirmer@12854
   485
  G,sig\<turnstile>(C,newM) hides old
schirmer@12854
   486
\<rbrakk> \<Longrightarrow>  accmodi old \<le> access newM \<and>
schirmer@12854
   487
       is_static old" 
schirmer@12854
   488
apply (drule (1) wf_cdecl_supD)
schirmer@12854
   489
apply (clarify)
schirmer@12854
   490
apply (drule entailsD)
schirmer@12854
   491
apply   (blast intro: table_of_map_SomeI)
schirmer@12854
   492
apply (drule_tac x="old" in spec)
schirmer@12854
   493
apply (auto dest: hides_eq_sigD simp add: msig_def)
schirmer@12854
   494
done
schirmer@12854
   495
schirmer@12854
   496
lemma wf_cdecl_wt_init: 
schirmer@12854
   497
 "wf_cdecl G (C, c) \<Longrightarrow> \<lparr>prg=G,cls=C,lcl=empty\<rparr>\<turnstile>init c\<Colon>\<surd>"
schirmer@12854
   498
apply (unfold wf_cdecl_def)
schirmer@12854
   499
apply auto
schirmer@12854
   500
done
schirmer@12854
   501
schirmer@12854
   502
schirmer@12854
   503
section "well-formed programs"
schirmer@12854
   504
  (* well-formed program, cf. 8.1, 9.1 *)
schirmer@12854
   505
schirmer@12854
   506
text {*
schirmer@12854
   507
A program declaration is wellformed if:
schirmer@12854
   508
\begin{itemize}
schirmer@12854
   509
\item the class ObjectC of Object is defined
schirmer@14030
   510
\item every method of Object has an access modifier distinct from Package. 
schirmer@14030
   511
      This is
schirmer@12854
   512
      necessary since every interface automatically inherits from Object.  
schirmer@12854
   513
      We must know, that every time a Object method is "overriden" by an 
schirmer@12854
   514
      interface method this is also overriden by the class implementing the
schirmer@12854
   515
      the interface (see @{text "implement_dynmethd and class_mheadsD"})
schirmer@12854
   516
\item all standard Exceptions are defined
schirmer@12854
   517
\item all defined interfaces are wellformed
schirmer@12854
   518
\item all defined classes are wellformed
schirmer@12854
   519
\end{itemize}
schirmer@12854
   520
*}
schirmer@12854
   521
constdefs
schirmer@12854
   522
  wf_prog  :: "prog \<Rightarrow> bool"
schirmer@12854
   523
 "wf_prog G \<equiv> let is = ifaces G; cs = classes G in
wenzelm@32960
   524
                 ObjectC \<in> set cs \<and> 
schirmer@12854
   525
                (\<forall> m\<in>set Object_mdecls. accmodi m \<noteq> Package) \<and>
schirmer@12854
   526
                (\<forall>xn. SXcptC xn \<in> set cs) \<and>
wenzelm@32960
   527
                (\<forall>i\<in>set is. wf_idecl G i) \<and> unique is \<and>
wenzelm@32960
   528
                (\<forall>c\<in>set cs. wf_cdecl G c) \<and> unique cs"
schirmer@12854
   529
schirmer@12854
   530
lemma wf_prog_idecl: "\<lbrakk>iface G I = Some i; wf_prog G\<rbrakk> \<Longrightarrow> wf_idecl G (I,i)"
schirmer@12854
   531
apply (unfold wf_prog_def Let_def)
schirmer@12854
   532
apply simp
schirmer@12854
   533
apply (fast dest: map_of_SomeD)
schirmer@12854
   534
done
schirmer@12854
   535
schirmer@12854
   536
lemma wf_prog_cdecl: "\<lbrakk>class G C = Some c; wf_prog G\<rbrakk> \<Longrightarrow> wf_cdecl G (C,c)"
schirmer@12854
   537
apply (unfold wf_prog_def Let_def)
schirmer@12854
   538
apply simp
schirmer@12854
   539
apply (fast dest: map_of_SomeD)
schirmer@12854
   540
done
schirmer@12854
   541
schirmer@12854
   542
lemma wf_prog_Object_mdecls:
schirmer@12854
   543
"wf_prog G \<Longrightarrow> (\<forall> m\<in>set Object_mdecls. accmodi m \<noteq> Package)"
schirmer@12854
   544
apply (unfold wf_prog_def Let_def)
schirmer@12854
   545
apply simp
schirmer@12854
   546
done
schirmer@12854
   547
schirmer@12854
   548
lemma wf_prog_acc_superD:
schirmer@12854
   549
 "\<lbrakk>wf_prog G; class G C = Some c; C \<noteq> Object \<rbrakk> 
schirmer@12854
   550
  \<Longrightarrow> is_acc_class G (pid C) (super c)"
schirmer@12854
   551
by (auto dest: wf_prog_cdecl wf_cdecl_supD)
schirmer@12854
   552
schirmer@12854
   553
lemma wf_ws_prog [elim!,simp]: "wf_prog G \<Longrightarrow> ws_prog G"
schirmer@12854
   554
apply (unfold wf_prog_def Let_def)
schirmer@12854
   555
apply (rule ws_progI)
schirmer@12854
   556
apply  (simp_all (no_asm))
schirmer@12854
   557
apply  (auto simp add: is_acc_class_def is_acc_iface_def 
schirmer@12854
   558
             dest!: wf_idecl_supD wf_cdecl_supD )+
schirmer@12854
   559
done
schirmer@12854
   560
schirmer@12854
   561
lemma class_Object [simp]: 
schirmer@12854
   562
"wf_prog G \<Longrightarrow> 
schirmer@12854
   563
  class G Object = Some \<lparr>access=Public,cfields=[],methods=Object_mdecls,
haftmann@28524
   564
                                  init=Skip,super=undefined,superIfs=[]\<rparr>"
schirmer@12854
   565
apply (unfold wf_prog_def Let_def ObjectC_def)
schirmer@12854
   566
apply (fast dest!: map_of_SomeI)
schirmer@12854
   567
done
schirmer@12854
   568
schirmer@12854
   569
lemma methd_Object[simp]: "wf_prog G \<Longrightarrow> methd G Object =  
schirmer@12854
   570
  table_of (map (\<lambda>(s,m). (s, Object, m)) Object_mdecls)"
schirmer@12854
   571
apply (subst methd_rec)
schirmer@12854
   572
apply (auto simp add: Let_def)
schirmer@12854
   573
done
schirmer@12854
   574
schirmer@12854
   575
lemma wf_prog_Object_methd:
schirmer@12854
   576
"\<lbrakk>wf_prog G; methd G Object sig = Some m\<rbrakk> \<Longrightarrow> accmodi m \<noteq> Package"
schirmer@12854
   577
by (auto dest!: wf_prog_Object_mdecls) (auto dest!: map_of_SomeD) 
schirmer@12854
   578
schirmer@12854
   579
lemma wf_prog_Object_is_public[intro]:
schirmer@12854
   580
 "wf_prog G \<Longrightarrow> is_public G Object"
schirmer@12854
   581
by (auto simp add: is_public_def dest: class_Object)
schirmer@12854
   582
schirmer@12854
   583
lemma class_SXcpt [simp]: 
schirmer@12854
   584
"wf_prog G \<Longrightarrow> 
schirmer@12854
   585
  class G (SXcpt xn) = Some \<lparr>access=Public,cfields=[],methods=SXcpt_mdecls,
schirmer@12854
   586
                                   init=Skip,
schirmer@12854
   587
                                   super=if xn = Throwable then Object 
schirmer@12854
   588
                                                           else SXcpt Throwable,
schirmer@12854
   589
                                   superIfs=[]\<rparr>"
schirmer@12854
   590
apply (unfold wf_prog_def Let_def SXcptC_def)
schirmer@12854
   591
apply (fast dest!: map_of_SomeI)
schirmer@12854
   592
done
schirmer@12854
   593
schirmer@12854
   594
lemma wf_ObjectC [simp]: 
wenzelm@32960
   595
        "wf_cdecl G ObjectC = (\<not>is_iface G Object \<and> Ball (set Object_mdecls)
schirmer@12854
   596
  (wf_mdecl G Object) \<and> unique Object_mdecls)"
schirmer@12854
   597
apply (unfold wf_cdecl_def ws_cdecl_def ObjectC_def)
schirmer@13688
   598
apply (auto intro: da.Skip)
schirmer@12854
   599
done
schirmer@12854
   600
schirmer@12854
   601
lemma Object_is_class [simp,elim!]: "wf_prog G \<Longrightarrow> is_class G Object"
schirmer@12854
   602
apply (simp (no_asm_simp))
schirmer@12854
   603
done
schirmer@12854
   604
 
schirmer@12854
   605
lemma Object_is_acc_class [simp,elim!]: "wf_prog G \<Longrightarrow> is_acc_class G S Object"
schirmer@12854
   606
apply (simp (no_asm_simp) add: is_acc_class_def is_public_def
schirmer@12854
   607
                               accessible_in_RefT_simp)
schirmer@12854
   608
done
schirmer@12854
   609
schirmer@12854
   610
lemma SXcpt_is_class [simp,elim!]: "wf_prog G \<Longrightarrow> is_class G (SXcpt xn)"
schirmer@12854
   611
apply (simp (no_asm_simp))
schirmer@12854
   612
done
schirmer@12854
   613
schirmer@12854
   614
lemma SXcpt_is_acc_class [simp,elim!]: 
schirmer@12854
   615
"wf_prog G \<Longrightarrow> is_acc_class G S (SXcpt xn)"
schirmer@12854
   616
apply (simp (no_asm_simp) add: is_acc_class_def is_public_def
schirmer@12854
   617
                               accessible_in_RefT_simp)
schirmer@12854
   618
done
schirmer@12854
   619
schirmer@12854
   620
lemma fields_Object [simp]: "wf_prog G \<Longrightarrow> DeclConcepts.fields G Object = []"
schirmer@12854
   621
by (force intro: fields_emptyI)
schirmer@12854
   622
schirmer@12854
   623
lemma accfield_Object [simp]: 
schirmer@12854
   624
 "wf_prog G \<Longrightarrow> accfield G S Object = empty"
schirmer@12854
   625
apply (unfold accfield_def)
schirmer@12854
   626
apply (simp (no_asm_simp) add: Let_def)
schirmer@12854
   627
done
schirmer@12854
   628
schirmer@12854
   629
lemma fields_Throwable [simp]: 
schirmer@12854
   630
 "wf_prog G \<Longrightarrow> DeclConcepts.fields G (SXcpt Throwable) = []"
schirmer@12854
   631
by (force intro: fields_emptyI)
schirmer@12854
   632
schirmer@12854
   633
lemma fields_SXcpt [simp]: "wf_prog G \<Longrightarrow> DeclConcepts.fields G (SXcpt xn) = []"
schirmer@12854
   634
apply (case_tac "xn = Throwable")
schirmer@12854
   635
apply  (simp (no_asm_simp))
schirmer@12854
   636
by (force intro: fields_emptyI)
schirmer@12854
   637
schirmer@12854
   638
lemmas widen_trans = ws_widen_trans [OF _ _ wf_ws_prog, elim]
schirmer@12854
   639
lemma widen_trans2 [elim]: "\<lbrakk>G\<turnstile>U\<preceq>T; G\<turnstile>S\<preceq>U; wf_prog G\<rbrakk> \<Longrightarrow> G\<turnstile>S\<preceq>T"
schirmer@12854
   640
apply (erule (2) widen_trans)
schirmer@12854
   641
done
schirmer@12854
   642
schirmer@12854
   643
lemma Xcpt_subcls_Throwable [simp]: 
schirmer@12854
   644
"wf_prog G \<Longrightarrow> G\<turnstile>SXcpt xn\<preceq>\<^sub>C SXcpt Throwable"
schirmer@12854
   645
apply (rule SXcpt_subcls_Throwable_lemma)
schirmer@12854
   646
apply auto
schirmer@12854
   647
done
schirmer@12854
   648
schirmer@12854
   649
lemma unique_fields: 
schirmer@12854
   650
 "\<lbrakk>is_class G C; wf_prog G\<rbrakk> \<Longrightarrow> unique (DeclConcepts.fields G C)"
schirmer@12854
   651
apply (erule ws_unique_fields)
schirmer@12854
   652
apply  (erule wf_ws_prog)
schirmer@12854
   653
apply (erule (1) wf_prog_cdecl [THEN wf_cdecl_unique [THEN conjunct1]])
schirmer@12854
   654
done
schirmer@12854
   655
schirmer@12854
   656
lemma fields_mono: 
schirmer@12854
   657
"\<lbrakk>table_of (DeclConcepts.fields G C) fn = Some f; G\<turnstile>D\<preceq>\<^sub>C C; 
schirmer@12854
   658
  is_class G D; wf_prog G\<rbrakk> 
schirmer@12854
   659
   \<Longrightarrow> table_of (DeclConcepts.fields G D) fn = Some f"
schirmer@12854
   660
apply (rule map_of_SomeI)
schirmer@12854
   661
apply  (erule (1) unique_fields)
schirmer@12854
   662
apply (erule (1) map_of_SomeD [THEN fields_mono_lemma])
schirmer@12854
   663
apply (erule wf_ws_prog)
schirmer@12854
   664
done
schirmer@12854
   665
schirmer@12854
   666
schirmer@12854
   667
lemma fields_is_type [elim]: 
schirmer@12854
   668
"\<lbrakk>table_of (DeclConcepts.fields G C) m = Some f; wf_prog G; is_class G C\<rbrakk> \<Longrightarrow> 
schirmer@12854
   669
      is_type G (type f)"
schirmer@12854
   670
apply (frule wf_ws_prog)
schirmer@12854
   671
apply (force dest: fields_declC [THEN conjunct1] 
schirmer@12854
   672
                   wf_prog_cdecl [THEN wf_cdecl_fdecl]
schirmer@12854
   673
             simp add: wf_fdecl_def2 is_acc_type_def)
schirmer@12854
   674
done
schirmer@12854
   675
schirmer@12854
   676
lemma imethds_wf_mhead [rule_format (no_asm)]: 
schirmer@12854
   677
"\<lbrakk>m \<in> imethds G I sig; wf_prog G; is_iface G I\<rbrakk> \<Longrightarrow>  
schirmer@12854
   678
  wf_mhead G (pid (decliface m)) sig (mthd m) \<and> 
schirmer@12854
   679
  \<not> is_static m \<and> accmodi m = Public"
schirmer@12854
   680
apply (frule wf_ws_prog)
schirmer@12854
   681
apply (drule (2) imethds_declI [THEN conjunct1])
schirmer@12854
   682
apply clarify
schirmer@12854
   683
apply (frule_tac I="(decliface m)" in wf_prog_idecl,assumption)
schirmer@12854
   684
apply (drule wf_idecl_mhead)
schirmer@12854
   685
apply (erule map_of_SomeD)
schirmer@12854
   686
apply (cases m, simp)
schirmer@12854
   687
done
schirmer@12854
   688
schirmer@12854
   689
lemma methd_wf_mdecl: 
schirmer@12854
   690
 "\<lbrakk>methd G C sig = Some m; wf_prog G; class G C = Some y\<rbrakk> \<Longrightarrow>  
schirmer@12854
   691
  G\<turnstile>C\<preceq>\<^sub>C (declclass m) \<and> is_class G (declclass m) \<and> 
schirmer@12854
   692
  wf_mdecl G (declclass m) (sig,(mthd m))"
schirmer@12854
   693
apply (frule wf_ws_prog)
schirmer@12854
   694
apply (drule (1) methd_declC)
schirmer@12854
   695
apply  fast
schirmer@12854
   696
apply clarsimp
schirmer@12854
   697
apply (frule (1) wf_prog_cdecl, erule wf_cdecl_mdecl, erule map_of_SomeD)
schirmer@12854
   698
done
schirmer@12854
   699
schirmer@12854
   700
(*
schirmer@12854
   701
This lemma doesn't hold!
schirmer@12854
   702
lemma methd_rT_is_acc_type: 
schirmer@12854
   703
"\<lbrakk>wf_prog G;methd G C C sig = Some (D,m);
schirmer@12854
   704
    class G C = Some y\<rbrakk>
schirmer@12854
   705
\<Longrightarrow> is_acc_type G (pid C) (resTy m)"
schirmer@12854
   706
The result Type is only visible in the scope of defining class D 
schirmer@12854
   707
"is_vis_type G (pid D) (resTy m)" but not necessarily in scope of class C!
schirmer@12854
   708
(The same is true for the type of pramaters of a method)
schirmer@12854
   709
*)
schirmer@12854
   710
schirmer@12854
   711
schirmer@12854
   712
lemma methd_rT_is_type: 
schirmer@12854
   713
"\<lbrakk>wf_prog G;methd G C sig = Some m;
schirmer@12854
   714
    class G C = Some y\<rbrakk>
schirmer@12854
   715
\<Longrightarrow> is_type G (resTy m)"
schirmer@12854
   716
apply (drule (2) methd_wf_mdecl)
schirmer@12854
   717
apply clarify
schirmer@12854
   718
apply (drule wf_mdeclD1)
schirmer@12854
   719
apply clarify
schirmer@12854
   720
apply (drule rT_is_acc_type)
schirmer@12854
   721
apply (cases m, simp add: is_acc_type_def)
schirmer@12854
   722
done
schirmer@12854
   723
schirmer@12854
   724
lemma accmethd_rT_is_type:
schirmer@12854
   725
"\<lbrakk>wf_prog G;accmethd G S C sig = Some m;
schirmer@12854
   726
    class G C = Some y\<rbrakk>
schirmer@12854
   727
\<Longrightarrow> is_type G (resTy m)"
schirmer@12854
   728
by (auto simp add: accmethd_def  
schirmer@12854
   729
         intro: methd_rT_is_type)
schirmer@12854
   730
schirmer@12854
   731
lemma methd_Object_SomeD:
schirmer@12854
   732
"\<lbrakk>wf_prog G;methd G Object sig = Some m\<rbrakk> 
schirmer@12854
   733
 \<Longrightarrow> declclass m = Object"
schirmer@12854
   734
by (auto dest: class_Object simp add: methd_rec )
schirmer@12854
   735
schirmer@12854
   736
lemma wf_imethdsD: 
schirmer@12854
   737
 "\<lbrakk>im \<in> imethds G I sig;wf_prog G; is_iface G I\<rbrakk> 
schirmer@12854
   738
 \<Longrightarrow> \<not>is_static im \<and> accmodi im = Public"
schirmer@12854
   739
proof -
schirmer@12854
   740
  assume asm: "wf_prog G" "is_iface G I" "im \<in> imethds G I sig"
schirmer@12854
   741
  have "wf_prog G \<longrightarrow> 
schirmer@12854
   742
         (\<forall> i im. iface G I = Some i \<longrightarrow> im \<in> imethds G I sig
schirmer@12854
   743
                  \<longrightarrow> \<not>is_static im \<and> accmodi im = Public)" (is "?P G I")
schirmer@12854
   744
  proof (rule iface_rec.induct,intro allI impI)
schirmer@12854
   745
    fix G I i im
schirmer@12854
   746
    assume hyp: "\<forall> J i. J \<in> set (isuperIfs i) \<and> ws_prog G \<and> iface G I = Some i
schirmer@12854
   747
                 \<longrightarrow> ?P G J"
schirmer@12854
   748
    assume wf: "wf_prog G" and if_I: "iface G I = Some i" and 
schirmer@12854
   749
           im: "im \<in> imethds G I sig" 
schirmer@12854
   750
    show "\<not>is_static im \<and> accmodi im = Public" 
schirmer@12854
   751
    proof -
schirmer@12854
   752
      let ?inherited = "Un_tables (imethds G ` set (isuperIfs i))"
nipkow@30235
   753
      let ?new = "(Option.set \<circ> table_of (map (\<lambda>(s, mh). (s, I, mh)) (imethods i)))"
schirmer@12854
   754
      from if_I wf im have imethds:"im \<in> (?inherited \<oplus>\<oplus> ?new) sig"
wenzelm@32960
   755
        by (simp add: imethds_rec)
schirmer@12854
   756
      from wf if_I have 
wenzelm@32960
   757
        wf_supI: "\<forall> J. J \<in> set (isuperIfs i) \<longrightarrow> (\<exists> j. iface G J = Some j)"
wenzelm@32960
   758
        by (blast dest: wf_prog_idecl wf_idecl_supD is_acc_ifaceD)
schirmer@12854
   759
      from wf if_I have
wenzelm@32960
   760
        "\<forall> im \<in> set (imethods i). \<not> is_static im \<and> accmodi im = Public"
wenzelm@32960
   761
        by (auto dest!: wf_prog_idecl wf_idecl_mhead)
schirmer@12854
   762
      then have new_ok: "\<forall> im. table_of (imethods i) sig = Some im 
schirmer@12854
   763
                         \<longrightarrow>  \<not> is_static im \<and> accmodi im = Public"
wenzelm@32960
   764
        by (auto dest!: table_of_Some_in_set)
schirmer@12854
   765
      show ?thesis
wenzelm@32960
   766
        proof (cases "?new sig = {}")
wenzelm@32960
   767
          case True
wenzelm@32960
   768
          from True wf wf_supI if_I imethds hyp 
wenzelm@32960
   769
          show ?thesis by (auto simp del:  split_paired_All)  
wenzelm@32960
   770
        next
wenzelm@32960
   771
          case False
wenzelm@32960
   772
          from False wf wf_supI if_I imethds new_ok hyp 
wenzelm@32960
   773
          show ?thesis by (auto dest: wf_idecl_hidings hidings_entailsD)
wenzelm@32960
   774
        qed
schirmer@12854
   775
      qed
schirmer@12854
   776
    qed
schirmer@12854
   777
  with asm show ?thesis by (auto simp del: split_paired_All)
schirmer@12854
   778
qed
schirmer@12854
   779
schirmer@12854
   780
lemma wf_prog_hidesD:
wenzelm@12937
   781
  assumes hides: "G \<turnstile>new hides old" and wf: "wf_prog G"
wenzelm@12937
   782
  shows
schirmer@12854
   783
   "accmodi old \<le> accmodi new \<and>
schirmer@12854
   784
    is_static old"
schirmer@12854
   785
proof -
schirmer@12854
   786
  from hides 
schirmer@12854
   787
  obtain c where 
schirmer@12854
   788
    clsNew: "class G (declclass new) = Some c" and
schirmer@12854
   789
    neqObj: "declclass new \<noteq> Object"
schirmer@12854
   790
    by (auto dest: hidesD declared_in_classD)
schirmer@12854
   791
  with hides obtain newM oldM where
schirmer@12854
   792
    newM: "table_of (methods c) (msig new) = Some newM" and 
schirmer@12854
   793
     new: "new = (declclass new,(msig new),newM)" and
schirmer@12854
   794
     old: "old = (declclass old,(msig old),oldM)" and
schirmer@12854
   795
          "msig new = msig old"
schirmer@12854
   796
    by (cases new,cases old) 
schirmer@12854
   797
       (auto dest: hidesD 
schirmer@12854
   798
         simp add: cdeclaredmethd_def declared_in_def)
schirmer@12854
   799
  with hides 
schirmer@12854
   800
  have hides':
schirmer@12854
   801
        "G,(msig new)\<turnstile>(declclass new,newM) hides (declclass old,oldM)"
schirmer@12854
   802
    by auto
schirmer@12854
   803
  from clsNew wf 
schirmer@12854
   804
  have "wf_cdecl G (declclass new,c)" by (blast intro: wf_prog_cdecl)
schirmer@12854
   805
  note wf_cdecl_hides_SomeD [OF this neqObj newM hides']
schirmer@12854
   806
  with new old 
schirmer@12854
   807
  show ?thesis
schirmer@12854
   808
    by (cases new, cases old) auto
schirmer@12854
   809
qed
schirmer@12854
   810
schirmer@12854
   811
text {* Compare this lemma about static  
schirmer@12854
   812
overriding @{term "G \<turnstile>new overrides\<^sub>S old"} with the definition of 
schirmer@12854
   813
dynamic overriding @{term "G \<turnstile>new overrides old"}. 
schirmer@12854
   814
Conforming result types and restrictions on the access modifiers of the old 
schirmer@12854
   815
and the new method are not part of the predicate for static overriding. But
schirmer@12854
   816
they are enshured in a wellfromed program.  Dynamic overriding has 
schirmer@12854
   817
no restrictions on the access modifiers but enforces confrom result types 
schirmer@12854
   818
as precondition. But with some efford we can guarantee the access modifier
schirmer@12854
   819
restriction for dynamic overriding, too. See lemma 
schirmer@12854
   820
@{text wf_prog_dyn_override_prop}.
schirmer@12854
   821
*}
schirmer@12854
   822
lemma wf_prog_stat_overridesD:
wenzelm@12937
   823
  assumes stat_override: "G \<turnstile>new overrides\<^sub>S old" and wf: "wf_prog G"
wenzelm@12937
   824
  shows
schirmer@12854
   825
   "G\<turnstile>resTy new\<preceq>resTy old \<and>
schirmer@12854
   826
    accmodi old \<le> accmodi new \<and>
schirmer@12854
   827
    \<not> is_static old"
schirmer@12854
   828
proof -
schirmer@12854
   829
  from stat_override 
schirmer@12854
   830
  obtain c where 
schirmer@12854
   831
    clsNew: "class G (declclass new) = Some c" and
schirmer@12854
   832
    neqObj: "declclass new \<noteq> Object"
schirmer@12854
   833
    by (auto dest: stat_overrides_commonD declared_in_classD)
schirmer@12854
   834
  with stat_override obtain newM oldM where
schirmer@12854
   835
    newM: "table_of (methods c) (msig new) = Some newM" and 
schirmer@12854
   836
     new: "new = (declclass new,(msig new),newM)" and
schirmer@12854
   837
     old: "old = (declclass old,(msig old),oldM)" and
schirmer@12854
   838
          "msig new = msig old"
schirmer@12854
   839
    by (cases new,cases old) 
schirmer@12854
   840
       (auto dest: stat_overrides_commonD 
schirmer@12854
   841
         simp add: cdeclaredmethd_def declared_in_def)
schirmer@12854
   842
  with stat_override 
schirmer@12854
   843
  have stat_override':
schirmer@12854
   844
        "G,(msig new)\<turnstile>(declclass new,newM) overrides\<^sub>S (declclass old,oldM)"
schirmer@12854
   845
    by auto
schirmer@12854
   846
  from clsNew wf 
schirmer@12854
   847
  have "wf_cdecl G (declclass new,c)" by (blast intro: wf_prog_cdecl)
schirmer@12854
   848
  note wf_cdecl_overrides_SomeD [OF this neqObj newM stat_override']
schirmer@12854
   849
  with new old 
schirmer@12854
   850
  show ?thesis
schirmer@12854
   851
    by (cases new, cases old) auto
schirmer@12854
   852
qed
schirmer@12854
   853
    
schirmer@12854
   854
lemma static_to_dynamic_overriding: 
wenzelm@12937
   855
  assumes stat_override: "G\<turnstile>new overrides\<^sub>S old" and wf : "wf_prog G"
wenzelm@12937
   856
  shows "G\<turnstile>new overrides old"
schirmer@12854
   857
proof -
schirmer@12854
   858
  from stat_override 
schirmer@12854
   859
  show ?thesis (is "?Overrides new old")
schirmer@12854
   860
  proof (induct)
schirmer@12854
   861
    case (Direct new old superNew)
schirmer@12854
   862
    then have stat_override:"G\<turnstile>new overrides\<^sub>S old" 
schirmer@12854
   863
      by (rule stat_overridesR.Direct)
schirmer@12854
   864
    from stat_override wf
schirmer@12854
   865
    have resTy_widen: "G\<turnstile>resTy new\<preceq>resTy old" and
schirmer@12854
   866
      not_static_old: "\<not> is_static old" 
schirmer@12854
   867
      by (auto dest: wf_prog_stat_overridesD)  
schirmer@12854
   868
    have not_private_new: "accmodi new \<noteq> Private"
schirmer@12854
   869
    proof -
schirmer@12854
   870
      from stat_override 
schirmer@12854
   871
      have "accmodi old \<noteq> Private"
wenzelm@32960
   872
        by (rule no_Private_stat_override)
schirmer@12854
   873
      moreover
schirmer@12854
   874
      from stat_override wf
schirmer@12854
   875
      have "accmodi old \<le> accmodi new"
wenzelm@32960
   876
        by (auto dest: wf_prog_stat_overridesD)
schirmer@12854
   877
      ultimately
schirmer@12854
   878
      show ?thesis
wenzelm@32960
   879
        by (auto dest: acc_modi_bottom)
schirmer@12854
   880
    qed
schirmer@12854
   881
    with Direct resTy_widen not_static_old 
schirmer@12854
   882
    show "?Overrides new old" 
schirmer@12962
   883
      by (auto intro: overridesR.Direct stat_override_declclasses_relation) 
schirmer@12854
   884
  next
berghofe@21765
   885
    case (Indirect new inter old)
schirmer@12854
   886
    then show "?Overrides new old" 
schirmer@12854
   887
      by (blast intro: overridesR.Indirect) 
schirmer@12854
   888
  qed
schirmer@12854
   889
qed
schirmer@12854
   890
schirmer@12854
   891
lemma non_Package_instance_method_inheritance:
wenzelm@12937
   892
  assumes old_inheritable: "G\<turnstile>Method old inheritable_in (pid C)" and
wenzelm@12937
   893
              accmodi_old: "accmodi old \<noteq> Package" and 
wenzelm@12937
   894
          instance_method: "\<not> is_static old" and
wenzelm@12937
   895
                   subcls: "G\<turnstile>C \<prec>\<^sub>C declclass old" and
wenzelm@12937
   896
             old_declared: "G\<turnstile>Method old declared_in (declclass old)" and
wenzelm@12937
   897
                       wf: "wf_prog G"
wenzelm@12937
   898
  shows "G\<turnstile>Method old member_of C \<or>
wenzelm@12937
   899
   (\<exists> new. G\<turnstile> new overrides\<^sub>S old \<and> G\<turnstile>Method new member_of C)"
schirmer@12854
   900
proof -
schirmer@12854
   901
  from wf have ws: "ws_prog G" by auto
schirmer@12854
   902
  from old_declared have iscls_declC_old: "is_class G (declclass old)"
schirmer@12854
   903
    by (auto simp add: declared_in_def cdeclaredmethd_def)
schirmer@12854
   904
  from subcls have  iscls_C: "is_class G C"
schirmer@12854
   905
    by (blast dest:  subcls_is_class)
schirmer@12854
   906
  from iscls_C ws old_inheritable subcls 
schirmer@12854
   907
  show ?thesis (is "?P C old")
schirmer@12854
   908
  proof (induct rule: ws_class_induct')
schirmer@12854
   909
    case Object
schirmer@12854
   910
    assume "G\<turnstile>Object\<prec>\<^sub>C declclass old"
schirmer@12854
   911
    then show "?P Object old"
schirmer@12854
   912
      by blast
schirmer@12854
   913
  next
schirmer@12854
   914
    case (Subcls C c)
schirmer@12854
   915
    assume cls_C: "class G C = Some c" and 
schirmer@12854
   916
       neq_C_Obj: "C \<noteq> Object" and
schirmer@12854
   917
             hyp: "\<lbrakk>G \<turnstile>Method old inheritable_in pid (super c); 
schirmer@12854
   918
                   G\<turnstile>super c\<prec>\<^sub>C declclass old\<rbrakk> \<Longrightarrow> ?P (super c) old" and
schirmer@12854
   919
     inheritable: "G \<turnstile>Method old inheritable_in pid C" and
schirmer@12854
   920
         subclsC: "G\<turnstile>C\<prec>\<^sub>C declclass old"
schirmer@12854
   921
    from cls_C neq_C_Obj  
schirmer@12854
   922
    have super: "G\<turnstile>C \<prec>\<^sub>C\<^sub>1 super c" 
schirmer@12854
   923
      by (rule subcls1I)
schirmer@12854
   924
    from wf cls_C neq_C_Obj
schirmer@12854
   925
    have accessible_super: "G\<turnstile>(Class (super c)) accessible_in (pid C)" 
schirmer@12854
   926
      by (auto dest: wf_prog_cdecl wf_cdecl_supD is_acc_classD)
schirmer@12854
   927
    {
schirmer@12854
   928
      fix old
schirmer@12854
   929
      assume    member_super: "G\<turnstile>Method old member_of (super c)"
schirmer@12854
   930
      assume     inheritable: "G \<turnstile>Method old inheritable_in pid C"
schirmer@12854
   931
      assume instance_method: "\<not> is_static old"
schirmer@12854
   932
      from member_super
schirmer@12854
   933
      have old_declared: "G\<turnstile>Method old declared_in (declclass old)"
schirmer@12854
   934
       by (cases old) (auto dest: member_of_declC)
schirmer@12854
   935
      have "?P C old"
schirmer@12854
   936
      proof (cases "G\<turnstile>mid (msig old) undeclared_in C")
wenzelm@32960
   937
        case True
wenzelm@32960
   938
        with inheritable super accessible_super member_super
wenzelm@32960
   939
        have "G\<turnstile>Method old member_of C"
wenzelm@32960
   940
          by (cases old) (auto intro: members.Inherited)
wenzelm@32960
   941
        then show ?thesis
wenzelm@32960
   942
          by auto
schirmer@12854
   943
      next
wenzelm@32960
   944
        case False
wenzelm@32960
   945
        then obtain new_member where
wenzelm@32960
   946
             "G\<turnstile>new_member declared_in C" and
schirmer@12854
   947
             "mid (msig old) = memberid new_member"
schirmer@12854
   948
          by (auto dest: not_undeclared_declared)
wenzelm@32960
   949
        then obtain new where
wenzelm@32960
   950
                  new: "G\<turnstile>Method new declared_in C" and
schirmer@12854
   951
               eq_sig: "msig old = msig new" and
wenzelm@32960
   952
            declC_new: "declclass new = C" 
wenzelm@32960
   953
          by (cases new_member) auto
wenzelm@32960
   954
        then have member_new: "G\<turnstile>Method new member_of C"
wenzelm@32960
   955
          by (cases new) (auto intro: members.Immediate)
wenzelm@32960
   956
        from declC_new super member_super
wenzelm@32960
   957
        have subcls_new_old: "G\<turnstile>declclass new \<prec>\<^sub>C declclass old"
wenzelm@32960
   958
          by (auto dest!: member_of_subclseq_declC
wenzelm@32960
   959
                    dest: r_into_trancl intro: trancl_rtrancl_trancl)
wenzelm@32960
   960
        show ?thesis
wenzelm@32960
   961
        proof (cases "is_static new")
wenzelm@32960
   962
          case False
wenzelm@32960
   963
          with eq_sig declC_new new old_declared inheritable
wenzelm@32960
   964
               super member_super subcls_new_old
wenzelm@32960
   965
          have "G\<turnstile>new overrides\<^sub>S old"
wenzelm@32960
   966
            by (auto intro!: stat_overridesR.Direct)
wenzelm@32960
   967
          with member_new show ?thesis
wenzelm@32960
   968
            by blast
wenzelm@32960
   969
        next
wenzelm@32960
   970
          case True
wenzelm@32960
   971
          with eq_sig declC_new subcls_new_old new old_declared inheritable
wenzelm@32960
   972
          have "G\<turnstile>new hides old"
wenzelm@32960
   973
            by (auto intro: hidesI)    
wenzelm@32960
   974
          with wf 
wenzelm@32960
   975
          have "is_static old"
wenzelm@32960
   976
            by (blast dest: wf_prog_hidesD)
wenzelm@32960
   977
          with instance_method
wenzelm@32960
   978
          show ?thesis
wenzelm@32960
   979
            by (contradiction)
wenzelm@32960
   980
        qed
schirmer@12854
   981
      qed
schirmer@12854
   982
    } note hyp_member_super = this
schirmer@12854
   983
    from subclsC cls_C 
schirmer@12854
   984
    have "G\<turnstile>(super c)\<preceq>\<^sub>C declclass old"
schirmer@12854
   985
      by (rule subcls_superD)
schirmer@12854
   986
    then
schirmer@12854
   987
    show "?P C old"
schirmer@12854
   988
    proof (cases rule: subclseq_cases) 
schirmer@12854
   989
      case Eq
schirmer@12854
   990
      assume "super c = declclass old"
schirmer@12854
   991
      with old_declared 
schirmer@12854
   992
      have "G\<turnstile>Method old member_of (super c)" 
wenzelm@32960
   993
        by (cases old) (auto intro: members.Immediate)
schirmer@12854
   994
      with inheritable instance_method 
schirmer@12854
   995
      show ?thesis
wenzelm@32960
   996
        by (blast dest: hyp_member_super)
schirmer@12854
   997
    next
schirmer@12854
   998
      case Subcls
schirmer@12854
   999
      assume "G\<turnstile>super c\<prec>\<^sub>C declclass old"
schirmer@12854
  1000
      moreover
schirmer@12854
  1001
      from inheritable accmodi_old
schirmer@12854
  1002
      have "G \<turnstile>Method old inheritable_in pid (super c)"
wenzelm@32960
  1003
        by (cases "accmodi old") (auto simp add: inheritable_in_def)
schirmer@12854
  1004
      ultimately
schirmer@12854
  1005
      have "?P (super c) old"
wenzelm@32960
  1006
        by (blast dest: hyp)
schirmer@12854
  1007
      then show ?thesis
schirmer@12854
  1008
      proof
wenzelm@32960
  1009
        assume "G \<turnstile>Method old member_of super c"
wenzelm@32960
  1010
        with inheritable instance_method
wenzelm@32960
  1011
        show ?thesis
wenzelm@32960
  1012
          by (blast dest: hyp_member_super)
schirmer@12854
  1013
      next
wenzelm@32960
  1014
        assume "\<exists>new. G \<turnstile> new overrides\<^sub>S old \<and> G \<turnstile>Method new member_of super c"
wenzelm@32960
  1015
        then obtain super_new where
wenzelm@32960
  1016
          super_new_override:  "G \<turnstile> super_new overrides\<^sub>S old" and
schirmer@12854
  1017
            super_new_member:  "G \<turnstile>Method super_new member_of super c"
wenzelm@32960
  1018
          by blast
wenzelm@32960
  1019
        from super_new_override wf
wenzelm@32960
  1020
        have "accmodi old \<le> accmodi super_new"
wenzelm@32960
  1021
          by (auto dest: wf_prog_stat_overridesD)
wenzelm@32960
  1022
        with inheritable accmodi_old
wenzelm@32960
  1023
        have "G \<turnstile>Method super_new inheritable_in pid C"
wenzelm@32960
  1024
          by (auto simp add: inheritable_in_def 
wenzelm@32960
  1025
                      split: acc_modi.splits
schirmer@12854
  1026
                       dest: acc_modi_le_Dests)
wenzelm@32960
  1027
        moreover
wenzelm@32960
  1028
        from super_new_override 
wenzelm@32960
  1029
        have "\<not> is_static super_new"
wenzelm@32960
  1030
          by (auto dest: stat_overrides_commonD)
wenzelm@32960
  1031
        moreover
wenzelm@32960
  1032
        note super_new_member
wenzelm@32960
  1033
        ultimately have "?P C super_new"
wenzelm@32960
  1034
          by (auto dest: hyp_member_super)
wenzelm@32960
  1035
        then show ?thesis
wenzelm@32960
  1036
        proof 
wenzelm@32960
  1037
          assume "G \<turnstile>Method super_new member_of C"
wenzelm@32960
  1038
          with super_new_override
wenzelm@32960
  1039
          show ?thesis
wenzelm@32960
  1040
            by blast
wenzelm@32960
  1041
        next
wenzelm@32960
  1042
          assume "\<exists>new. G \<turnstile> new overrides\<^sub>S super_new \<and> 
schirmer@12854
  1043
                  G \<turnstile>Method new member_of C"
wenzelm@32960
  1044
          with super_new_override show ?thesis
wenzelm@32960
  1045
            by (blast intro: stat_overridesR.Indirect) 
wenzelm@32960
  1046
        qed
schirmer@12854
  1047
      qed
schirmer@12854
  1048
    qed
schirmer@12854
  1049
  qed
schirmer@12854
  1050
qed
schirmer@12854
  1051
schirmer@12854
  1052
lemma non_Package_instance_method_inheritance_cases [consumes 6,
schirmer@12854
  1053
         case_names Inheritance Overriding]:
wenzelm@12937
  1054
  assumes old_inheritable: "G\<turnstile>Method old inheritable_in (pid C)" and
wenzelm@12937
  1055
              accmodi_old: "accmodi old \<noteq> Package" and 
wenzelm@12937
  1056
          instance_method: "\<not> is_static old" and
wenzelm@12937
  1057
                   subcls: "G\<turnstile>C \<prec>\<^sub>C declclass old" and
wenzelm@12937
  1058
             old_declared: "G\<turnstile>Method old declared_in (declclass old)" and
wenzelm@12937
  1059
                       wf: "wf_prog G" and
wenzelm@12937
  1060
              inheritance: "G\<turnstile>Method old member_of C \<Longrightarrow> P" and
wenzelm@12937
  1061
               overriding: "\<And> new.
schirmer@12854
  1062
                           \<lbrakk>G\<turnstile> new overrides\<^sub>S old;G\<turnstile>Method new member_of C\<rbrakk>
schirmer@12854
  1063
                           \<Longrightarrow> P"
wenzelm@12937
  1064
  shows P
schirmer@12854
  1065
proof -
schirmer@12854
  1066
  from old_inheritable accmodi_old instance_method subcls old_declared wf 
schirmer@12854
  1067
       inheritance overriding
schirmer@12854
  1068
  show ?thesis
schirmer@12854
  1069
    by (auto dest: non_Package_instance_method_inheritance)
schirmer@12854
  1070
qed
schirmer@12854
  1071
schirmer@12854
  1072
lemma dynamic_to_static_overriding:
wenzelm@12937
  1073
  assumes dyn_override: "G\<turnstile> new overrides old" and
wenzelm@12937
  1074
           accmodi_old: "accmodi old \<noteq> Package" and
wenzelm@12937
  1075
                    wf: "wf_prog G"
wenzelm@12937
  1076
  shows "G\<turnstile> new overrides\<^sub>S old"  
schirmer@12854
  1077
proof - 
schirmer@12854
  1078
  from dyn_override accmodi_old
schirmer@12854
  1079
  show ?thesis (is "?Overrides new old")
schirmer@12854
  1080
  proof (induct rule: overridesR.induct)
schirmer@12854
  1081
    case (Direct new old)
schirmer@12854
  1082
    assume   new_declared: "G\<turnstile>Method new declared_in declclass new"
schirmer@12854
  1083
    assume eq_sig_new_old: "msig new = msig old"
schirmer@12854
  1084
    assume subcls_new_old: "G\<turnstile>declclass new \<prec>\<^sub>C declclass old"
schirmer@12854
  1085
    assume "G \<turnstile>Method old inheritable_in pid (declclass new)" and
schirmer@12854
  1086
           "accmodi old \<noteq> Package" and
schirmer@12854
  1087
           "\<not> is_static old" and
schirmer@12854
  1088
           "G\<turnstile>declclass new\<prec>\<^sub>C declclass old" and
schirmer@12854
  1089
           "G\<turnstile>Method old declared_in declclass old" 
schirmer@12854
  1090
    from this wf
schirmer@12854
  1091
    show "?Overrides new old"
schirmer@12854
  1092
    proof (cases rule: non_Package_instance_method_inheritance_cases)
schirmer@12854
  1093
      case Inheritance
schirmer@12854
  1094
      assume "G \<turnstile>Method old member_of declclass new"
schirmer@12854
  1095
      then have "G\<turnstile>mid (msig old) undeclared_in declclass new"
schirmer@12854
  1096
      proof cases
wenzelm@32960
  1097
        case Immediate 
wenzelm@32960
  1098
        with subcls_new_old wf show ?thesis     
wenzelm@32960
  1099
          by (auto dest: subcls_irrefl)
schirmer@12854
  1100
      next
wenzelm@32960
  1101
        case Inherited
wenzelm@32960
  1102
        then show ?thesis
wenzelm@32960
  1103
          by (cases old) auto
schirmer@12854
  1104
      qed
schirmer@12854
  1105
      with eq_sig_new_old new_declared
schirmer@12854
  1106
      show ?thesis
wenzelm@32960
  1107
        by (cases old,cases new) (auto dest!: declared_not_undeclared)
schirmer@12854
  1108
    next
schirmer@12854
  1109
      case (Overriding new') 
schirmer@12854
  1110
      assume stat_override_new': "G \<turnstile> new' overrides\<^sub>S old"
schirmer@12854
  1111
      then have "msig new' = msig old"
wenzelm@32960
  1112
        by (auto dest: stat_overrides_commonD)
schirmer@12854
  1113
      with eq_sig_new_old have eq_sig_new_new': "msig new=msig new'"
wenzelm@32960
  1114
        by simp
schirmer@12854
  1115
      assume "G \<turnstile>Method new' member_of declclass new"
schirmer@12854
  1116
      then show ?thesis
schirmer@12854
  1117
      proof (cases)
wenzelm@32960
  1118
        case Immediate
wenzelm@32960
  1119
        then have declC_new: "declclass new' = declclass new" 
wenzelm@32960
  1120
          by auto
wenzelm@32960
  1121
        from Immediate 
wenzelm@32960
  1122
        have "G\<turnstile>Method new' declared_in declclass new"
wenzelm@32960
  1123
          by (cases new') auto
wenzelm@32960
  1124
        with new_declared eq_sig_new_new' declC_new 
wenzelm@32960
  1125
        have "new=new'"
wenzelm@32960
  1126
          by (cases new, cases new') (auto dest: unique_declared_in) 
wenzelm@32960
  1127
        with stat_override_new'
wenzelm@32960
  1128
        show ?thesis
wenzelm@32960
  1129
          by simp
schirmer@12854
  1130
      next
wenzelm@32960
  1131
        case Inherited
wenzelm@32960
  1132
        then have "G\<turnstile>mid (msig new') undeclared_in declclass new"
wenzelm@32960
  1133
          by (cases new') (auto)
wenzelm@32960
  1134
        with eq_sig_new_new' new_declared
wenzelm@32960
  1135
        show ?thesis
wenzelm@32960
  1136
          by (cases new,cases new') (auto dest!: declared_not_undeclared)
schirmer@12854
  1137
      qed
schirmer@12854
  1138
    qed
schirmer@12854
  1139
  next
berghofe@21765
  1140
    case (Indirect new inter old)
schirmer@12854
  1141
    assume accmodi_old: "accmodi old \<noteq> Package"
schirmer@12854
  1142
    assume "accmodi old \<noteq> Package \<Longrightarrow> G \<turnstile> inter overrides\<^sub>S old"
schirmer@12854
  1143
    with accmodi_old 
schirmer@12854
  1144
    have stat_override_inter_old: "G \<turnstile> inter overrides\<^sub>S old"
schirmer@12854
  1145
      by blast
schirmer@12854
  1146
    moreover 
schirmer@12854
  1147
    assume hyp_inter: "accmodi inter \<noteq> Package \<Longrightarrow> G \<turnstile> new overrides\<^sub>S inter"
schirmer@12854
  1148
    moreover
schirmer@12854
  1149
    have "accmodi inter \<noteq> Package"
schirmer@12854
  1150
    proof -
schirmer@12854
  1151
      from stat_override_inter_old wf 
schirmer@12854
  1152
      have "accmodi old \<le> accmodi inter"
wenzelm@32960
  1153
        by (auto dest: wf_prog_stat_overridesD)
schirmer@12854
  1154
      with stat_override_inter_old accmodi_old
schirmer@12854
  1155
      show ?thesis
wenzelm@32960
  1156
        by (auto dest!: no_Private_stat_override
schirmer@12854
  1157
                 split: acc_modi.splits 
wenzelm@32960
  1158
                 dest: acc_modi_le_Dests)
schirmer@12854
  1159
    qed
schirmer@12854
  1160
    ultimately show "?Overrides new old"
schirmer@12854
  1161
      by (blast intro: stat_overridesR.Indirect)
schirmer@12854
  1162
  qed
schirmer@12854
  1163
qed
schirmer@12854
  1164
schirmer@12854
  1165
lemma wf_prog_dyn_override_prop:
wenzelm@12937
  1166
  assumes dyn_override: "G \<turnstile> new overrides old" and
schirmer@12854
  1167
                    wf: "wf_prog G"
wenzelm@12937
  1168
  shows "accmodi old \<le> accmodi new"
schirmer@12854
  1169
proof (cases "accmodi old = Package")
schirmer@12854
  1170
  case True
schirmer@12854
  1171
  note old_Package = this
schirmer@12854
  1172
  show ?thesis
schirmer@12854
  1173
  proof (cases "accmodi old \<le> accmodi new")
schirmer@12854
  1174
    case True then show ?thesis .
schirmer@12854
  1175
  next
schirmer@12854
  1176
    case False
schirmer@12854
  1177
    with old_Package 
schirmer@12854
  1178
    have "accmodi new = Private"
schirmer@12854
  1179
      by (cases "accmodi new") (auto simp add: le_acc_def less_acc_def)
schirmer@12854
  1180
    with dyn_override 
schirmer@12854
  1181
    show ?thesis
schirmer@12854
  1182
      by (auto dest: overrides_commonD)
schirmer@12854
  1183
  qed    
schirmer@12854
  1184
next
schirmer@12854
  1185
  case False
schirmer@12854
  1186
  with dyn_override wf
schirmer@12854
  1187
  have "G \<turnstile> new overrides\<^sub>S old"
schirmer@12854
  1188
    by (blast intro: dynamic_to_static_overriding)
schirmer@12854
  1189
  with wf 
schirmer@12854
  1190
  show ?thesis
schirmer@12854
  1191
   by (blast dest: wf_prog_stat_overridesD)
schirmer@12854
  1192
qed 
schirmer@12854
  1193
schirmer@12854
  1194
lemma overrides_Package_old: 
wenzelm@12937
  1195
  assumes dyn_override: "G \<turnstile> new overrides old" and 
wenzelm@12937
  1196
           accmodi_new: "accmodi new = Package" and
wenzelm@12937
  1197
                    wf: "wf_prog G "
wenzelm@12937
  1198
  shows "accmodi old = Package"
schirmer@12854
  1199
proof (cases "accmodi old")
schirmer@12854
  1200
  case Private
schirmer@12854
  1201
  with dyn_override show ?thesis
schirmer@12854
  1202
    by (simp add: no_Private_override)
schirmer@12854
  1203
next
schirmer@12854
  1204
  case Package
schirmer@12854
  1205
  then show ?thesis .
schirmer@12854
  1206
next
schirmer@12854
  1207
  case Protected
schirmer@12854
  1208
  with dyn_override wf
schirmer@12854
  1209
  have "G \<turnstile> new overrides\<^sub>S old"
schirmer@12854
  1210
    by (auto intro: dynamic_to_static_overriding)
schirmer@12854
  1211
  with wf 
schirmer@12854
  1212
  have "accmodi old \<le> accmodi new"
schirmer@12854
  1213
    by (auto dest: wf_prog_stat_overridesD)
schirmer@12854
  1214
  with Protected accmodi_new
schirmer@12854
  1215
  show ?thesis
schirmer@12854
  1216
    by (simp add: less_acc_def le_acc_def)
schirmer@12854
  1217
next
schirmer@12854
  1218
  case Public
schirmer@12854
  1219
  with dyn_override wf
schirmer@12854
  1220
  have "G \<turnstile> new overrides\<^sub>S old"
schirmer@12854
  1221
    by (auto intro: dynamic_to_static_overriding)
schirmer@12854
  1222
  with wf 
schirmer@12854
  1223
  have "accmodi old \<le> accmodi new"
schirmer@12854
  1224
    by (auto dest: wf_prog_stat_overridesD)
schirmer@12854
  1225
  with Public accmodi_new
schirmer@12854
  1226
  show ?thesis
schirmer@12854
  1227
    by (simp add: less_acc_def le_acc_def)
schirmer@12854
  1228
qed
schirmer@12854
  1229
schirmer@12854
  1230
lemma dyn_override_Package:
wenzelm@12937
  1231
  assumes dyn_override: "G \<turnstile> new overrides old" and
wenzelm@12937
  1232
           accmodi_old: "accmodi old = Package" and 
wenzelm@12937
  1233
           accmodi_new: "accmodi new = Package" and
schirmer@12854
  1234
                    wf: "wf_prog G"
wenzelm@12937
  1235
  shows "pid (declclass old) = pid (declclass new)"
schirmer@12854
  1236
proof - 
schirmer@12854
  1237
  from dyn_override accmodi_old accmodi_new
schirmer@12854
  1238
  show ?thesis (is "?EqPid old new")
schirmer@12854
  1239
  proof (induct rule: overridesR.induct)
schirmer@12854
  1240
    case (Direct new old)
schirmer@12854
  1241
    assume "accmodi old = Package"
schirmer@12854
  1242
           "G \<turnstile>Method old inheritable_in pid (declclass new)"
schirmer@12854
  1243
    then show "pid (declclass old) =  pid (declclass new)"
schirmer@12854
  1244
      by (auto simp add: inheritable_in_def)
schirmer@12854
  1245
  next
berghofe@21765
  1246
    case (Indirect new inter old)
schirmer@12854
  1247
    assume accmodi_old: "accmodi old = Package" and
schirmer@12854
  1248
           accmodi_new: "accmodi new = Package" 
schirmer@12854
  1249
    assume "G \<turnstile> new overrides inter"
schirmer@12854
  1250
    with accmodi_new wf
schirmer@12854
  1251
    have "accmodi inter = Package"
schirmer@12854
  1252
      by  (auto intro: overrides_Package_old)
schirmer@12854
  1253
    with Indirect
schirmer@12854
  1254
    show "pid (declclass old) =  pid (declclass new)"
schirmer@12854
  1255
      by auto
schirmer@12854
  1256
  qed
schirmer@12854
  1257
qed
schirmer@12854
  1258
schirmer@12854
  1259
lemma dyn_override_Package_escape:
wenzelm@12937
  1260
  assumes dyn_override: "G \<turnstile> new overrides old" and
wenzelm@12937
  1261
           accmodi_old: "accmodi old = Package" and 
wenzelm@12937
  1262
          outside_pack: "pid (declclass old) \<noteq> pid (declclass new)" and
schirmer@12854
  1263
                    wf: "wf_prog G"
wenzelm@12937
  1264
  shows "\<exists> inter. G \<turnstile> new overrides inter \<and> G \<turnstile> inter overrides old \<and>
schirmer@12854
  1265
             pid (declclass old) = pid (declclass inter) \<and>
schirmer@12854
  1266
             Protected \<le> accmodi inter"
schirmer@12854
  1267
proof -
schirmer@12854
  1268
  from dyn_override accmodi_old outside_pack
schirmer@12854
  1269
  show ?thesis (is "?P new old")
schirmer@12854
  1270
  proof (induct rule: overridesR.induct)
schirmer@12854
  1271
    case (Direct new old)
schirmer@12854
  1272
    assume accmodi_old: "accmodi old = Package"
schirmer@12854
  1273
    assume outside_pack: "pid (declclass old) \<noteq> pid (declclass new)"
schirmer@12854
  1274
    assume "G \<turnstile>Method old inheritable_in pid (declclass new)"
schirmer@12854
  1275
    with accmodi_old 
schirmer@12854
  1276
    have "pid (declclass old) = pid (declclass new)"
schirmer@12854
  1277
      by (simp add: inheritable_in_def)
schirmer@12854
  1278
    with outside_pack 
schirmer@12854
  1279
    show "?P new old"
schirmer@12854
  1280
      by (contradiction)
schirmer@12854
  1281
  next
berghofe@21765
  1282
    case (Indirect new inter old)
schirmer@12854
  1283
    assume accmodi_old: "accmodi old = Package"
schirmer@12854
  1284
    assume outside_pack: "pid (declclass old) \<noteq> pid (declclass new)"
schirmer@12854
  1285
    assume override_new_inter: "G \<turnstile> new overrides inter"
schirmer@12854
  1286
    assume override_inter_old: "G \<turnstile> inter overrides old"
schirmer@12854
  1287
    assume hyp_new_inter: "\<lbrakk>accmodi inter = Package; 
schirmer@12854
  1288
                           pid (declclass inter) \<noteq> pid (declclass new)\<rbrakk>
schirmer@12854
  1289
                           \<Longrightarrow> ?P new inter"
schirmer@12854
  1290
    assume hyp_inter_old: "\<lbrakk>accmodi old = Package; 
schirmer@12854
  1291
                           pid (declclass old) \<noteq> pid (declclass inter)\<rbrakk>
schirmer@12854
  1292
                           \<Longrightarrow> ?P inter old"
schirmer@12854
  1293
    show "?P new old"
schirmer@12854
  1294
    proof (cases "pid (declclass old) = pid (declclass inter)")
schirmer@12854
  1295
      case True
schirmer@12854
  1296
      note same_pack_old_inter = this
schirmer@12854
  1297
      show ?thesis
schirmer@12854
  1298
      proof (cases "pid (declclass inter) = pid (declclass new)")
wenzelm@32960
  1299
        case True
wenzelm@32960
  1300
        with same_pack_old_inter outside_pack
wenzelm@32960
  1301
        show ?thesis
wenzelm@32960
  1302
          by auto
schirmer@12854
  1303
      next
wenzelm@32960
  1304
        case False
wenzelm@32960
  1305
        note diff_pack_inter_new = this
wenzelm@32960
  1306
        show ?thesis
wenzelm@32960
  1307
        proof (cases "accmodi inter = Package")
wenzelm@32960
  1308
          case True
wenzelm@32960
  1309
          with diff_pack_inter_new hyp_new_inter  
wenzelm@32960
  1310
          obtain newinter where
wenzelm@32960
  1311
            over_new_newinter: "G \<turnstile> new overrides newinter" and
schirmer@12854
  1312
            over_newinter_inter: "G \<turnstile> newinter overrides inter" and 
schirmer@12854
  1313
            eq_pid: "pid (declclass inter) = pid (declclass newinter)" and
schirmer@12854
  1314
            accmodi_newinter: "Protected \<le> accmodi newinter"
wenzelm@32960
  1315
            by auto
wenzelm@32960
  1316
          from over_newinter_inter override_inter_old
wenzelm@32960
  1317
          have "G\<turnstile>newinter overrides old"
wenzelm@32960
  1318
            by (rule overridesR.Indirect)
wenzelm@32960
  1319
          moreover
wenzelm@32960
  1320
          from eq_pid same_pack_old_inter 
wenzelm@32960
  1321
          have "pid (declclass old) = pid (declclass newinter)"
wenzelm@32960
  1322
            by simp
wenzelm@32960
  1323
          moreover
wenzelm@32960
  1324
          note over_new_newinter accmodi_newinter
wenzelm@32960
  1325
          ultimately show ?thesis
wenzelm@32960
  1326
            by blast
wenzelm@32960
  1327
        next
wenzelm@32960
  1328
          case False
wenzelm@32960
  1329
          with override_new_inter
wenzelm@32960
  1330
          have "Protected \<le> accmodi inter"
wenzelm@32960
  1331
            by (cases "accmodi inter") (auto dest: no_Private_override)
wenzelm@32960
  1332
          with override_new_inter override_inter_old same_pack_old_inter
wenzelm@32960
  1333
          show ?thesis
wenzelm@32960
  1334
            by blast
wenzelm@32960
  1335
        qed
schirmer@12854
  1336
      qed
schirmer@12854
  1337
    next
schirmer@12854
  1338
      case False
schirmer@12854
  1339
      with accmodi_old hyp_inter_old
schirmer@12854
  1340
      obtain newinter where
wenzelm@32960
  1341
        over_inter_newinter: "G \<turnstile> inter overrides newinter" and
schirmer@12854
  1342
          over_newinter_old: "G \<turnstile> newinter overrides old" and 
schirmer@12854
  1343
                eq_pid: "pid (declclass old) = pid (declclass newinter)" and
wenzelm@32960
  1344
        accmodi_newinter: "Protected \<le> accmodi newinter"
wenzelm@32960
  1345
        by auto
schirmer@12854
  1346
      from override_new_inter over_inter_newinter 
schirmer@12854
  1347
      have "G \<turnstile> new overrides newinter"
wenzelm@32960
  1348
        by (rule overridesR.Indirect)
schirmer@12854
  1349
      with eq_pid over_newinter_old accmodi_newinter
schirmer@12854
  1350
      show ?thesis
wenzelm@32960
  1351
        by blast
schirmer@12854
  1352
    qed
schirmer@12854
  1353
  qed
schirmer@12854
  1354
qed
schirmer@12854
  1355
schirmer@12854
  1356
lemma declclass_widen[rule_format]: 
schirmer@12854
  1357
 "wf_prog G 
schirmer@12854
  1358
 \<longrightarrow> (\<forall>c m. class G C = Some c \<longrightarrow> methd G C sig = Some m 
schirmer@12854
  1359
 \<longrightarrow> G\<turnstile>C \<preceq>\<^sub>C declclass m)" (is "?P G C")
schirmer@12854
  1360
proof (rule class_rec.induct,intro allI impI)
schirmer@12854
  1361
  fix G C c m
schirmer@12854
  1362
  assume Hyp: "\<forall>c. C \<noteq> Object \<and> ws_prog G \<and> class G C = Some c 
schirmer@12854
  1363
               \<longrightarrow> ?P G (super c)"
schirmer@12854
  1364
  assume wf: "wf_prog G" and cls_C: "class G C = Some c" and
schirmer@12854
  1365
         m:  "methd G C sig = Some m"
schirmer@12854
  1366
  show "G\<turnstile>C\<preceq>\<^sub>C declclass m" 
schirmer@12854
  1367
  proof (cases "C=Object")
schirmer@12854
  1368
    case True 
schirmer@12854
  1369
    with wf m show ?thesis by (simp add: methd_Object_SomeD)
schirmer@12854
  1370
  next
schirmer@12854
  1371
    let ?filter="filter_tab (\<lambda>sig m. G\<turnstile>C inherits method sig m)"
schirmer@12854
  1372
    let ?table = "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c))"
schirmer@12854
  1373
    case False 
schirmer@12854
  1374
    with cls_C wf m
schirmer@12854
  1375
    have methd_C: "(?filter (methd G (super c)) ++ ?table) sig = Some m "
schirmer@12854
  1376
      by (simp add: methd_rec)
schirmer@12854
  1377
    show ?thesis
schirmer@12854
  1378
    proof (cases "?table sig")
schirmer@12854
  1379
      case None
schirmer@12854
  1380
      from this methd_C have "?filter (methd G (super c)) sig = Some m"
wenzelm@32960
  1381
        by simp
schirmer@12854
  1382
      moreover
schirmer@12854
  1383
      from wf cls_C False obtain sup where "class G (super c) = Some sup"
wenzelm@32960
  1384
        by (blast dest: wf_prog_cdecl wf_cdecl_supD is_acc_class_is_class)
schirmer@12925
  1385
      moreover note wf False cls_C  
schirmer@12925
  1386
      ultimately have "G\<turnstile>super c \<preceq>\<^sub>C declclass m"  
wenzelm@32960
  1387
        by (auto intro: Hyp [rule_format])
schirmer@12854
  1388
      moreover from cls_C False have  "G\<turnstile>C \<prec>\<^sub>C\<^sub>1 super c" by (rule subcls1I)
schirmer@12854
  1389
      ultimately show ?thesis by - (rule rtrancl_into_rtrancl2)
schirmer@12854
  1390
    next
schirmer@12854
  1391
      case Some
schirmer@12854
  1392
      from this wf False cls_C methd_C show ?thesis by auto
schirmer@12854
  1393
    qed
schirmer@12854
  1394
  qed
schirmer@12854
  1395
qed
schirmer@12854
  1396
schirmer@12854
  1397
lemma declclass_methd_Object: 
schirmer@12854
  1398
 "\<lbrakk>wf_prog G; methd G Object sig = Some m\<rbrakk> \<Longrightarrow> declclass m = Object"
schirmer@12854
  1399
by auto
schirmer@12854
  1400
schirmer@12854
  1401
lemma methd_declaredD: 
schirmer@12854
  1402
 "\<lbrakk>wf_prog G; is_class G C;methd G C sig = Some m\<rbrakk> 
schirmer@12854
  1403
  \<Longrightarrow> G\<turnstile>(mdecl (sig,mthd m)) declared_in (declclass m)"
schirmer@12854
  1404
proof -
schirmer@12854
  1405
  assume    wf: "wf_prog G"
schirmer@12854
  1406
  then have ws: "ws_prog G" ..
schirmer@12854
  1407
  assume  clsC: "is_class G C"
schirmer@12854
  1408
  from clsC ws 
schirmer@12854
  1409
  show "methd G C sig = Some m 
schirmer@12854
  1410
        \<Longrightarrow> G\<turnstile>(mdecl (sig,mthd m)) declared_in (declclass m)"
berghofe@34915
  1411
  proof (induct C rule: ws_class_induct')
schirmer@12854
  1412
    case Object
schirmer@12854
  1413
    assume "methd G Object sig = Some m" 
schirmer@12854
  1414
    with wf show ?thesis
schirmer@12854
  1415
      by - (rule method_declared_inI, auto) 
schirmer@12854
  1416
  next
schirmer@12854
  1417
    case Subcls
schirmer@12854
  1418
    fix C c
schirmer@12854
  1419
    assume clsC: "class G C = Some c"
schirmer@12854
  1420
    and       m: "methd G C sig = Some m"
schirmer@12854
  1421
    and     hyp: "methd G (super c) sig = Some m \<Longrightarrow> ?thesis" 
schirmer@12854
  1422
    let ?newMethods = "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c))"
schirmer@12854
  1423
    show ?thesis
schirmer@12854
  1424
    proof (cases "?newMethods sig")
schirmer@12854
  1425
      case None
schirmer@12854
  1426
      from None ws clsC m hyp 
schirmer@12854
  1427
      show ?thesis by (auto intro: method_declared_inI simp add: methd_rec)
schirmer@12854
  1428
    next
schirmer@12854
  1429
      case Some
schirmer@12854
  1430
      from Some ws clsC m 
schirmer@12854
  1431
      show ?thesis by (auto intro: method_declared_inI simp add: methd_rec) 
schirmer@12854
  1432
    qed
schirmer@12854
  1433
  qed
schirmer@12854
  1434
qed
schirmer@12854
  1435
schirmer@12854
  1436
lemma methd_rec_Some_cases [consumes 4, case_names NewMethod InheritedMethod]:
wenzelm@12937
  1437
  assumes methd_C: "methd G C sig = Some m" and
wenzelm@12937
  1438
               ws: "ws_prog G" and
wenzelm@12937
  1439
             clsC: "class G C = Some c" and
wenzelm@12937
  1440
        neq_C_Obj: "C\<noteq>Object"
wenzelm@12937
  1441
  shows
schirmer@12854
  1442
"\<lbrakk>table_of (map (\<lambda>(s, m). (s, C, m)) (methods c)) sig = Some m \<Longrightarrow> P;
schirmer@12854
  1443
  \<lbrakk>G\<turnstile>C inherits (method sig m); methd G (super c) sig = Some m\<rbrakk> \<Longrightarrow> P 
schirmer@12854
  1444
 \<rbrakk> \<Longrightarrow> P"
schirmer@12854
  1445
proof -
schirmer@12854
  1446
  let ?inherited   = "filter_tab (\<lambda>sig m. G\<turnstile>C inherits method sig m) 
schirmer@12854
  1447
                              (methd G (super c))"
schirmer@12854
  1448
  let ?new = "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c))"
schirmer@12854
  1449
  from ws clsC neq_C_Obj methd_C 
schirmer@12854
  1450
  have methd_unfold: "(?inherited ++ ?new) sig = Some m"
schirmer@12854
  1451
    by (simp add: methd_rec)
schirmer@12854
  1452
  assume NewMethod: "?new sig = Some m \<Longrightarrow> P"
schirmer@12854
  1453
  assume InheritedMethod: "\<lbrakk>G\<turnstile>C inherits (method sig m); 
schirmer@12854
  1454
                            methd G (super c) sig = Some m\<rbrakk> \<Longrightarrow> P"
schirmer@12854
  1455
  show P
schirmer@12854
  1456
  proof (cases "?new sig")
schirmer@12854
  1457
    case None
schirmer@12854
  1458
    with methd_unfold have "?inherited sig = Some m"
schirmer@12854
  1459
      by (auto)
schirmer@12854
  1460
    with InheritedMethod show P by blast
schirmer@12854
  1461
  next
schirmer@12854
  1462
    case Some
schirmer@12854
  1463
    with methd_unfold have "?new sig = Some m"
schirmer@12854
  1464
      by auto
schirmer@12854
  1465
    with NewMethod show P by blast
schirmer@12854
  1466
  qed
schirmer@12854
  1467
qed
schirmer@12854
  1468
schirmer@12854
  1469
  
schirmer@12854
  1470
lemma methd_member_of:
wenzelm@12937
  1471
  assumes wf: "wf_prog G"
wenzelm@12937
  1472
  shows
wenzelm@12937
  1473
    "\<lbrakk>is_class G C; methd G C sig = Some m\<rbrakk> \<Longrightarrow> G\<turnstile>Methd sig m member_of C" 
schirmer@12854
  1474
  (is "?Class C \<Longrightarrow> ?Method C \<Longrightarrow> ?MemberOf C") 
schirmer@12854
  1475
proof -
schirmer@12854
  1476
  from wf   have   ws: "ws_prog G" ..
schirmer@12854
  1477
  assume defC: "is_class G C"
schirmer@12854
  1478
  from defC ws 
schirmer@12854
  1479
  show "?Class C \<Longrightarrow> ?Method C \<Longrightarrow> ?MemberOf C"
schirmer@12854
  1480
  proof (induct rule: ws_class_induct')  
schirmer@12854
  1481
    case Object
berghofe@13601
  1482
    with wf have declC: "Object = declclass m"
berghofe@13601
  1483
      by (simp add: declclass_methd_Object)
berghofe@13601
  1484
    from Object wf have "G\<turnstile>Methd sig m declared_in Object"
berghofe@13601
  1485
      by (auto intro: methd_declaredD simp add: declC)
schirmer@12854
  1486
    with declC 
schirmer@12854
  1487
    show "?MemberOf Object"
schirmer@12854
  1488
      by (auto intro!: members.Immediate
schirmer@12854
  1489
                  simp del: methd_Object)
schirmer@12854
  1490
  next
schirmer@12854
  1491
    case (Subcls C c)
schirmer@12854
  1492
    assume  clsC: "class G C = Some c" and
schirmer@12854
  1493
       neq_C_Obj: "C \<noteq> Object"  
schirmer@12854
  1494
    assume methd: "?Method C"
schirmer@12854
  1495
    from methd ws clsC neq_C_Obj
schirmer@12854
  1496
    show "?MemberOf C"
schirmer@12854
  1497
    proof (cases rule: methd_rec_Some_cases)
schirmer@12854
  1498
      case NewMethod
schirmer@12854
  1499
      with clsC show ?thesis
wenzelm@32960
  1500
        by (auto dest: method_declared_inI intro!: members.Immediate)
schirmer@12854
  1501
    next
schirmer@12854
  1502
      case InheritedMethod
schirmer@12854
  1503
      then show "?thesis"
wenzelm@32960
  1504
        by (blast dest: inherits_member)
schirmer@12854
  1505
    qed
schirmer@12854
  1506
  qed
schirmer@12854
  1507
qed
schirmer@12854
  1508
schirmer@12854
  1509
lemma current_methd: 
schirmer@12854
  1510
      "\<lbrakk>table_of (methods c) sig = Some new;
schirmer@12854
  1511
        ws_prog G; class G C = Some c; C \<noteq> Object; 
schirmer@12854
  1512
        methd G (super c) sig = Some old\<rbrakk> 
schirmer@12854
  1513
    \<Longrightarrow> methd G C sig = Some (C,new)"
schirmer@12854
  1514
by (auto simp add: methd_rec
nipkow@14025
  1515
            intro: filter_tab_SomeI map_add_find_right table_of_map_SomeI)
schirmer@12854
  1516
schirmer@12854
  1517
lemma wf_prog_staticD:
wenzelm@12937
  1518
  assumes     wf: "wf_prog G" and
schirmer@12854
  1519
            clsC: "class G C = Some c" and
schirmer@12854
  1520
       neq_C_Obj: "C \<noteq> Object" and 
schirmer@12854
  1521
             old: "methd G (super c) sig = Some old" and 
schirmer@12854
  1522
     accmodi_old: "Protected \<le> accmodi old" and
schirmer@12854
  1523
             new: "table_of (methods c) sig = Some new"
wenzelm@12937
  1524
  shows "is_static new = is_static old"
schirmer@12854
  1525
proof -
schirmer@12854
  1526
  from clsC wf 
schirmer@12854
  1527
  have wf_cdecl: "wf_cdecl G (C,c)" by (rule wf_prog_cdecl)
schirmer@12854
  1528
  from wf clsC neq_C_Obj
schirmer@12854
  1529
  have is_cls_super: "is_class G (super c)" 
schirmer@12854
  1530
    by (blast dest: wf_prog_acc_superD is_acc_classD)
schirmer@12854
  1531
  from wf is_cls_super old 
schirmer@12854
  1532
  have old_member_of: "G\<turnstile>Methd sig old member_of (super c)"  
schirmer@12854
  1533
    by (rule methd_member_of)
schirmer@12854
  1534
  from old wf is_cls_super 
schirmer@12854
  1535
  have old_declared: "G\<turnstile>Methd sig old declared_in (declclass old)"
schirmer@12854
  1536
    by (auto dest: methd_declared_in_declclass)
schirmer@12854
  1537
  from new clsC 
schirmer@12854
  1538
  have new_declared: "G\<turnstile>Methd sig (C,new) declared_in C"
schirmer@12854
  1539
    by (auto intro: method_declared_inI)
schirmer@12854
  1540
  note trancl_rtrancl_tranc = trancl_rtrancl_trancl [trans] (* ### in Basis *)
schirmer@12854
  1541
  from clsC neq_C_Obj
schirmer@12854
  1542
  have subcls1_C_super: "G\<turnstile>C \<prec>\<^sub>C\<^sub>1 super c"
schirmer@12854
  1543
    by (rule subcls1I)
schirmer@12854
  1544
  then have "G\<turnstile>C \<prec>\<^sub>C super c" ..
schirmer@12854
  1545
  also from old wf is_cls_super
schirmer@12854
  1546
  have "G\<turnstile>super c \<preceq>\<^sub>C (declclass old)" by (auto dest: methd_declC)
schirmer@12854
  1547
  finally have subcls_C_old:  "G\<turnstile>C \<prec>\<^sub>C (declclass old)" .
schirmer@12854
  1548
  from accmodi_old 
schirmer@12854
  1549
  have inheritable: "G\<turnstile>Methd sig old inheritable_in pid C"
schirmer@12854
  1550
    by (auto simp add: inheritable_in_def
schirmer@12854
  1551
                 dest: acc_modi_le_Dests)
schirmer@12854
  1552
  show ?thesis
schirmer@12854
  1553
  proof (cases "is_static new")
schirmer@12854
  1554
    case True
schirmer@12854
  1555
    with subcls_C_old new_declared old_declared inheritable
schirmer@12854
  1556
    have "G,sig\<turnstile>(C,new) hides old"
schirmer@12854
  1557
      by (auto intro: hidesI)
schirmer@12854
  1558
    with True wf_cdecl neq_C_Obj new 
schirmer@12854
  1559
    show ?thesis
schirmer@12854
  1560
      by (auto dest: wf_cdecl_hides_SomeD)
schirmer@12854
  1561
  next
schirmer@12854
  1562
    case False
schirmer@12854
  1563
    with subcls_C_old new_declared old_declared inheritable subcls1_C_super
schirmer@12854
  1564
         old_member_of
schirmer@12854
  1565
    have "G,sig\<turnstile>(C,new) overrides\<^sub>S old"
schirmer@12854
  1566
      by (auto intro: stat_overridesR.Direct)
schirmer@12854
  1567
    with False wf_cdecl neq_C_Obj new 
schirmer@12854
  1568
    show ?thesis
schirmer@12854
  1569
      by (auto dest: wf_cdecl_overrides_SomeD)
schirmer@12854
  1570
  qed
schirmer@12854
  1571
qed
schirmer@12854
  1572
schirmer@12854
  1573
lemma inheritable_instance_methd: 
wenzelm@12937
  1574
  assumes subclseq_C_D: "G\<turnstile>C \<preceq>\<^sub>C D" and
schirmer@12854
  1575
              is_cls_D: "is_class G D" and
schirmer@12854
  1576
                    wf: "wf_prog G" and 
schirmer@12854
  1577
                   old: "methd G D sig = Some old" and
schirmer@12854
  1578
           accmodi_old: "Protected \<le> accmodi old" and  
schirmer@12854
  1579
        not_static_old: "\<not> is_static old"
wenzelm@12937
  1580
  shows
schirmer@12854
  1581
  "\<exists>new. methd G C sig = Some new \<and>
schirmer@12854
  1582
         (new = old \<or> G,sig\<turnstile>new overrides\<^sub>S old)"
schirmer@12854
  1583
 (is "(\<exists>new. (?Constraint C new old))")
schirmer@12854
  1584
proof -
schirmer@12854
  1585
  from subclseq_C_D is_cls_D 
schirmer@12854
  1586
  have is_cls_C: "is_class G C" by (rule subcls_is_class2) 
schirmer@12854
  1587
  from wf 
schirmer@12854
  1588
  have ws: "ws_prog G" ..
schirmer@12854
  1589
  from is_cls_C ws subclseq_C_D 
schirmer@12854
  1590
  show "\<exists>new. ?Constraint C new old"
schirmer@12854
  1591
  proof (induct rule: ws_class_induct')
schirmer@12854
  1592
    case (Object co)
schirmer@12854
  1593
    then have eq_D_Obj: "D=Object" by auto
schirmer@12854
  1594
    with old 
schirmer@12854
  1595
    have "?Constraint Object old old"
schirmer@12854
  1596
      by auto
schirmer@12854
  1597
    with eq_D_Obj 
schirmer@12854
  1598
    show "\<exists> new. ?Constraint Object new old" by auto
schirmer@12854
  1599
  next
schirmer@12854
  1600
    case (Subcls C c)
schirmer@12854
  1601
    assume hyp: "G\<turnstile>super c\<preceq>\<^sub>C D \<Longrightarrow> \<exists>new. ?Constraint (super c) new old"
schirmer@12854
  1602
    assume clsC: "class G C = Some c"
schirmer@12854
  1603
    assume neq_C_Obj: "C\<noteq>Object"
schirmer@12854
  1604
    from clsC wf 
schirmer@12854
  1605
    have wf_cdecl: "wf_cdecl G (C,c)" 
schirmer@12854
  1606
      by (rule wf_prog_cdecl)
schirmer@12854
  1607
    from ws clsC neq_C_Obj
schirmer@12854
  1608
    have is_cls_super: "is_class G (super c)"
schirmer@12854
  1609
      by (auto dest: ws_prog_cdeclD)
schirmer@12854
  1610
    from clsC wf neq_C_Obj 
schirmer@12854
  1611
    have superAccessible: "G\<turnstile>(Class (super c)) accessible_in (pid C)" and
wenzelm@32960
  1612
         subcls1_C_super: "G\<turnstile>C \<prec>\<^sub>C\<^sub>1 super c"
schirmer@12854
  1613
      by (auto dest: wf_prog_cdecl wf_cdecl_supD is_acc_classD
schirmer@12854
  1614
              intro: subcls1I)
schirmer@12854
  1615
    show "\<exists>new. ?Constraint C new old"
schirmer@12854
  1616
    proof (cases "G\<turnstile>super c\<preceq>\<^sub>C D")
schirmer@12854
  1617
      case False
schirmer@12854
  1618
      from False Subcls 
schirmer@12854
  1619
      have eq_C_D: "C=D"
wenzelm@32960
  1620
        by (auto dest: subclseq_superD)
schirmer@12854
  1621
      with old 
schirmer@12854
  1622
      have "?Constraint C old old"
wenzelm@32960
  1623
        by auto
schirmer@12854
  1624
      with eq_C_D 
schirmer@12854
  1625
      show "\<exists> new. ?Constraint C new old" by auto
schirmer@12854
  1626
    next
schirmer@12854
  1627
      case True
schirmer@12854
  1628
      with hyp obtain super_method
wenzelm@32960
  1629
        where super: "?Constraint (super c) super_method old" by blast
schirmer@12854
  1630
      from super not_static_old
schirmer@12854
  1631
      have not_static_super: "\<not> is_static super_method"
wenzelm@32960
  1632
        by (auto dest!: stat_overrides_commonD)
schirmer@12854
  1633
      from super old wf accmodi_old
schirmer@12854
  1634
      have accmodi_super_method: "Protected \<le> accmodi super_method"
wenzelm@32960
  1635
        by (auto dest!: wf_prog_stat_overridesD)
schirmer@12854
  1636
      from super accmodi_old wf
schirmer@12854
  1637
      have inheritable: "G\<turnstile>Methd sig super_method inheritable_in (pid C)"
wenzelm@32960
  1638
        by (auto dest!: wf_prog_stat_overridesD
schirmer@12854
  1639
                        acc_modi_le_Dests
wenzelm@32960
  1640
              simp add: inheritable_in_def)                
schirmer@12854
  1641
      from super wf is_cls_super
schirmer@12854
  1642
      have member: "G\<turnstile>Methd sig super_method member_of (super c)"
wenzelm@32960
  1643
        by (auto intro: methd_member_of) 
schirmer@12854
  1644
      from member
schirmer@12854
  1645
      have decl_super_method:
schirmer@12854
  1646
        "G\<turnstile>Methd sig super_method declared_in (declclass super_method)"
wenzelm@32960
  1647
        by (auto dest: member_of_declC)
schirmer@12854
  1648
      from super subcls1_C_super ws is_cls_super 
schirmer@12854
  1649
      have subcls_C_super: "G\<turnstile>C \<prec>\<^sub>C (declclass super_method)"
wenzelm@32960
  1650
        by (auto intro: rtrancl_into_trancl2 dest: methd_declC) 
schirmer@12854
  1651
      show "\<exists> new. ?Constraint C new old"
schirmer@12854
  1652
      proof (cases "methd G C sig")
wenzelm@32960
  1653
        case None
wenzelm@32960
  1654
        have "methd G (super c) sig = None"
wenzelm@32960
  1655
        proof -
wenzelm@32960
  1656
          from clsC ws None 
wenzelm@32960
  1657
          have no_new: "table_of (methods c) sig = None" 
wenzelm@32960
  1658
            by (auto simp add: methd_rec)
wenzelm@32960
  1659
          with clsC 
wenzelm@32960
  1660
          have undeclared: "G\<turnstile>mid sig undeclared_in C"
wenzelm@32960
  1661
            by (auto simp add: undeclared_in_def cdeclaredmethd_def)
wenzelm@32960
  1662
          with inheritable member superAccessible subcls1_C_super
wenzelm@32960
  1663
          have inherits: "G\<turnstile>C inherits (method sig super_method)"
wenzelm@32960
  1664
            by (auto simp add: inherits_def)
wenzelm@32960
  1665
          with clsC ws no_new super neq_C_Obj
wenzelm@32960
  1666
          have "methd G C sig = Some super_method"
wenzelm@32960
  1667
            by (auto simp add: methd_rec map_add_def intro: filter_tab_SomeI)
schirmer@12854
  1668
          with None show ?thesis
wenzelm@32960
  1669
            by simp
wenzelm@32960
  1670
        qed
wenzelm@32960
  1671
        with super show ?thesis by auto
schirmer@12854
  1672
      next
wenzelm@32960
  1673
        case (Some new)
wenzelm@32960
  1674
        from this ws clsC neq_C_Obj
wenzelm@32960
  1675
        show ?thesis
wenzelm@32960
  1676
        proof (cases rule: methd_rec_Some_cases)
wenzelm@32960
  1677
          case InheritedMethod
wenzelm@32960
  1678
          with super Some show ?thesis 
wenzelm@32960
  1679
            by auto
wenzelm@32960
  1680
        next
wenzelm@32960
  1681
          case NewMethod
wenzelm@32960
  1682
          assume new: "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c)) sig 
schirmer@12854
  1683
                       = Some new"
wenzelm@32960
  1684
          from new 
wenzelm@32960
  1685
          have declcls_new: "declclass new = C" 
wenzelm@32960
  1686
            by auto
wenzelm@32960
  1687
          from wf clsC neq_C_Obj super new not_static_super accmodi_super_method
wenzelm@32960
  1688
          have not_static_new: "\<not> is_static new" 
wenzelm@32960
  1689
            by (auto dest: wf_prog_staticD) 
wenzelm@32960
  1690
          from clsC new
wenzelm@32960
  1691
          have decl_new: "G\<turnstile>Methd sig new declared_in C"
wenzelm@32960
  1692
            by (auto simp add: declared_in_def cdeclaredmethd_def)
wenzelm@32960
  1693
          from not_static_new decl_new decl_super_method
wenzelm@32960
  1694
               member subcls1_C_super inheritable declcls_new subcls_C_super 
wenzelm@32960
  1695
          have "G,sig\<turnstile> new overrides\<^sub>S super_method"
wenzelm@32960
  1696
            by (auto intro: stat_overridesR.Direct) 
wenzelm@32960
  1697
          with super Some
wenzelm@32960
  1698
          show ?thesis
wenzelm@32960
  1699
            by (auto intro: stat_overridesR.Indirect)
wenzelm@32960
  1700
        qed
schirmer@12854
  1701
      qed
schirmer@12854
  1702
    qed
schirmer@12854
  1703
  qed
schirmer@12854
  1704
qed
schirmer@12854
  1705
schirmer@12854
  1706
lemma inheritable_instance_methd_cases [consumes 6
schirmer@12854
  1707
                                       , case_names Inheritance Overriding]: 
wenzelm@12937
  1708
  assumes subclseq_C_D: "G\<turnstile>C \<preceq>\<^sub>C D" and
schirmer@12854
  1709
              is_cls_D: "is_class G D" and
schirmer@12854
  1710
                    wf: "wf_prog G" and 
schirmer@12854
  1711
                   old: "methd G D sig = Some old" and
schirmer@12854
  1712
           accmodi_old: "Protected \<le> accmodi old" and  
schirmer@12854
  1713
        not_static_old: "\<not> is_static old" and
schirmer@12854
  1714
           inheritance:  "methd G C sig = Some old \<Longrightarrow> P" and
schirmer@12854
  1715
            overriding:  "\<And> new. \<lbrakk>methd G C sig = Some new;
schirmer@12854
  1716
                                   G,sig\<turnstile>new overrides\<^sub>S old\<rbrakk> \<Longrightarrow> P"
schirmer@12854
  1717
        
wenzelm@12937
  1718
  shows P
schirmer@12854
  1719
proof -
schirmer@12854
  1720
from subclseq_C_D is_cls_D wf old accmodi_old not_static_old 
schirmer@12854
  1721
show ?thesis
schirmer@12925
  1722
  by (auto dest: inheritable_instance_methd intro: inheritance overriding)
schirmer@12854
  1723
qed
schirmer@12854
  1724
schirmer@12854
  1725
lemma inheritable_instance_methd_props: 
wenzelm@12937
  1726
  assumes subclseq_C_D: "G\<turnstile>C \<preceq>\<^sub>C D" and
schirmer@12854
  1727
              is_cls_D: "is_class G D" and
schirmer@12854
  1728
                    wf: "wf_prog G" and 
schirmer@12854
  1729
                   old: "methd G D sig = Some old" and
schirmer@12854
  1730
           accmodi_old: "Protected \<le> accmodi old" and  
schirmer@12854
  1731
        not_static_old: "\<not> is_static old"
wenzelm@12937
  1732
  shows
schirmer@12854
  1733
  "\<exists>new. methd G C sig = Some new \<and>
schirmer@12854
  1734
          \<not> is_static new \<and> G\<turnstile>resTy new\<preceq>resTy old \<and> accmodi old \<le>accmodi new"
schirmer@12854
  1735
 (is "(\<exists>new. (?Constraint C new old))")
schirmer@12854
  1736
proof -
schirmer@12854
  1737
  from subclseq_C_D is_cls_D wf old accmodi_old not_static_old 
schirmer@12854
  1738
  show ?thesis
schirmer@12854
  1739
  proof (cases rule: inheritable_instance_methd_cases)
schirmer@12854
  1740
    case Inheritance
schirmer@12854
  1741
    with not_static_old accmodi_old show ?thesis by auto
schirmer@12854
  1742
  next
schirmer@12854
  1743
    case (Overriding new)
schirmer@12854
  1744
    then have "\<not> is_static new" by (auto dest: stat_overrides_commonD)
schirmer@12854
  1745
    with Overriding not_static_old accmodi_old wf 
schirmer@12854
  1746
    show ?thesis 
haftmann@22424
  1747
      by (auto dest!: wf_prog_stat_overridesD)
schirmer@12854
  1748
  qed
schirmer@12854
  1749
qed
wenzelm@32960
  1750
              
schirmer@12854
  1751
(* local lemma *)
wenzelm@24019
  1752
lemma bexI': "x \<in> A \<Longrightarrow> P x \<Longrightarrow> \<exists>x\<in>A. P x" by blast
wenzelm@24019
  1753
lemma ballE': "\<forall>x\<in>A. P x \<Longrightarrow> (x \<notin> A \<Longrightarrow> Q) \<Longrightarrow> (P x \<Longrightarrow> Q) \<Longrightarrow> Q" by blast
wenzelm@24019
  1754
schirmer@12854
  1755
lemma subint_widen_imethds: 
berghofe@34915
  1756
  assumes irel: "G\<turnstile>I\<preceq>I J"
berghofe@34915
  1757
  and wf: "wf_prog G"
berghofe@34915
  1758
  and is_iface: "is_iface G J"
berghofe@34915
  1759
  and jm: "jm \<in> imethds G J sig"
berghofe@34915
  1760
  shows "\<exists>im \<in> imethds G I sig. is_static im = is_static jm \<and> 
schirmer@12854
  1761
                          accmodi im = accmodi jm \<and>
schirmer@12854
  1762
                          G\<turnstile>resTy im\<preceq>resTy jm"
berghofe@34915
  1763
  using irel jm
berghofe@34915
  1764
proof (induct rule: converse_rtrancl_induct)
berghofe@34915
  1765
    case base
berghofe@34915
  1766
    then show ?case by  (blast elim: bexI')
schirmer@12854
  1767
  next
berghofe@34915
  1768
    case (step I SI)
berghofe@34915
  1769
    from `G\<turnstile>I \<prec>I1 SI`
schirmer@12854
  1770
    obtain i where
schirmer@12854
  1771
      ifI: "iface G I = Some i" and
schirmer@12854
  1772
       SI: "SI \<in> set (isuperIfs i)"
schirmer@12854
  1773
      by (blast dest: subint1D)
schirmer@12854
  1774
schirmer@12854
  1775
    let ?newMethods 
nipkow@30235
  1776
          = "(Option.set \<circ> table_of (map (\<lambda>(sig, mh). (sig, I, mh)) (imethods i)))"
berghofe@34915
  1777
    show ?case
schirmer@12854
  1778
    proof (cases "?newMethods sig = {}")
schirmer@12854
  1779
      case True
berghofe@34915
  1780
      with ifI SI step wf
schirmer@12854
  1781
      show "?thesis" 
wenzelm@32960
  1782
        by (auto simp add: imethds_rec) 
schirmer@12854
  1783
    next
schirmer@12854
  1784
      case False
schirmer@12854
  1785
      from ifI wf False
schirmer@12854
  1786
      have imethds: "imethds G I sig = ?newMethods sig"
wenzelm@32960
  1787
        by (simp add: imethds_rec)
schirmer@12854
  1788
      from False
schirmer@12854
  1789
      obtain im where
schirmer@12854
  1790
        imdef: "im \<in> ?newMethods sig" 
wenzelm@32960
  1791
        by (blast)
schirmer@12854
  1792
      with imethds 
schirmer@12854
  1793
      have im: "im \<in> imethds G I sig"
wenzelm@32960
  1794
        by (blast)
schirmer@12854
  1795
      with im wf ifI 
schirmer@12854
  1796
      obtain
wenzelm@32960
  1797
         imStatic: "\<not> is_static im" and
schirmer@12854
  1798
         imPublic: "accmodi im = Public"
wenzelm@32960
  1799
        by (auto dest!: imethds_wf_mhead)       
schirmer@12854
  1800
      from ifI wf 
schirmer@12854
  1801
      have wf_I: "wf_idecl G (I,i)" 
wenzelm@32960
  1802
        by (rule wf_prog_idecl)
schirmer@12854
  1803
      with SI wf  
schirmer@12854
  1804
      obtain si where
wenzelm@32960
  1805
         ifSI: "iface G SI = Some si" and
wenzelm@32960
  1806
        wf_SI: "wf_idecl G (SI,si)" 
wenzelm@32960
  1807
        by (auto dest!: wf_idecl_supD is_acc_ifaceD
schirmer@12854
  1808
                  dest: wf_prog_idecl)
berghofe@34915
  1809
      from step
schirmer@12854
  1810
      obtain sim::"qtname \<times> mhead"  where
schirmer@12854
  1811
                      sim: "sim \<in> imethds G SI sig" and
schirmer@12854
  1812
         eq_static_sim_jm: "is_static sim = is_static jm" and 
schirmer@12854
  1813
         eq_access_sim_jm: "accmodi sim = accmodi jm" and 
schirmer@12854
  1814
        resTy_widen_sim_jm: "G\<turnstile>resTy sim\<preceq>resTy jm"
wenzelm@32960
  1815
        by blast
schirmer@12854
  1816
      with wf_I SI imdef sim 
schirmer@12854
  1817
      have "G\<turnstile>resTy im\<preceq>resTy sim"   
wenzelm@32960
  1818
        by (auto dest!: wf_idecl_hidings hidings_entailsD)
schirmer@12854
  1819
      with wf resTy_widen_sim_jm 
schirmer@12854
  1820
      have resTy_widen_im_jm: "G\<turnstile>resTy im\<preceq>resTy jm"
wenzelm@32960
  1821
        by (blast intro: widen_trans)
schirmer@12854
  1822
      from sim wf ifSI  
schirmer@12854
  1823
      obtain
wenzelm@32960
  1824
        simStatic: "\<not> is_static sim" and
schirmer@12854
  1825
        simPublic: "accmodi sim = Public"
wenzelm@32960
  1826
        by (auto dest!: imethds_wf_mhead)
schirmer@12854
  1827
      from im 
schirmer@12854
  1828
           imStatic simStatic eq_static_sim_jm
schirmer@12854
  1829
           imPublic simPublic eq_access_sim_jm
schirmer@12854
  1830
           resTy_widen_im_jm
schirmer@12854
  1831
      show ?thesis 
wenzelm@32960
  1832
        by auto 
schirmer@12854
  1833
    qed
schirmer@12854
  1834
qed
schirmer@12854
  1835
     
schirmer@12854
  1836
(* Tactical version *)
schirmer@12854
  1837
(* 
schirmer@12854
  1838
lemma subint_widen_imethds: "\<lbrakk>G\<turnstile>I\<preceq>I J; wf_prog G; is_iface G J\<rbrakk> \<Longrightarrow>  
schirmer@12854
  1839
  \<forall> jm \<in> imethds G J sig.  
schirmer@12854
  1840
  \<exists> im \<in> imethds G I sig. static (mthd im)=static (mthd jm) \<and> 
schirmer@12854
  1841
                          access (mthd im)= access (mthd jm) \<and>
schirmer@12854
  1842
                          G\<turnstile>resTy (mthd im)\<preceq>resTy (mthd jm)"
schirmer@12854
  1843
apply (erule converse_rtrancl_induct)
schirmer@12854
  1844
apply  (clarsimp elim!: bexI')
schirmer@12854
  1845
apply (frule subint1D)
schirmer@12854
  1846
apply clarify
schirmer@12854
  1847
apply (erule ballE')
schirmer@12854
  1848
apply  fast
schirmer@12854
  1849
apply (erule_tac V = "?x \<in> imethds G J sig" in thin_rl)
schirmer@12854
  1850
apply clarsimp
schirmer@12854
  1851
apply (subst imethds_rec, assumption, erule wf_ws_prog)
schirmer@12854
  1852
apply (unfold overrides_t_def)
schirmer@12854
  1853
apply (drule (1) wf_prog_idecl)
schirmer@12854
  1854
apply (frule (3) imethds_wf_mhead [OF _ _ wf_idecl_supD [THEN conjunct1 
schirmer@12854
  1855
                                       [THEN is_acc_ifaceD [THEN conjunct1]]]])
nipkow@30235
  1856
apply (case_tac "(Option.set \<circ> table_of (map (\<lambda>(s, mh). (s, y, mh)) (imethods i)))
schirmer@12854
  1857
                  sig ={}")
schirmer@12854
  1858
apply   force
schirmer@12854
  1859
schirmer@12854
  1860
apply   (simp only:)
schirmer@12854
  1861
apply   (simp)
schirmer@12854
  1862
apply   clarify
schirmer@12854
  1863
apply   (frule wf_idecl_hidings [THEN hidings_entailsD])
schirmer@12854
  1864
apply     blast
schirmer@12854
  1865
apply     blast
schirmer@12854
  1866
apply   (rule bexI')
schirmer@12854
  1867
apply     simp
schirmer@12854
  1868
apply     (drule table_of_map_SomeI [of _ "sig"])
schirmer@12854
  1869
apply     simp
schirmer@12854
  1870
schirmer@12854
  1871
apply     (frule wf_idecl_mhead [of _ _ _ "sig"])
schirmer@12854
  1872
apply       (rule table_of_Some_in_set)
schirmer@12854
  1873
apply       assumption
schirmer@12854
  1874
apply     auto
schirmer@12854
  1875
done
schirmer@12854
  1876
*)
schirmer@12854
  1877
    
schirmer@12854
  1878
schirmer@12854
  1879
(* local lemma *)
schirmer@12854
  1880
lemma implmt1_methd: 
schirmer@12854
  1881
 "\<And>sig. \<lbrakk>G\<turnstile>C\<leadsto>1I; wf_prog G; im \<in> imethds G I sig\<rbrakk> \<Longrightarrow>  
schirmer@12854
  1882
  \<exists>cm \<in>methd G C sig: \<not> is_static cm \<and> \<not> is_static im \<and> 
schirmer@12854
  1883
                       G\<turnstile>resTy cm\<preceq>resTy im \<and>
schirmer@12854
  1884
                       accmodi im = Public \<and> accmodi cm = Public"
schirmer@12854
  1885
apply (drule implmt1D)
schirmer@12854
  1886
apply clarify
schirmer@12854
  1887
apply (drule (2) wf_prog_cdecl [THEN wf_cdecl_impD])
schirmer@12854
  1888
apply (frule (1) imethds_wf_mhead)
schirmer@12854
  1889
apply  (simp add: is_acc_iface_def)
schirmer@12854
  1890
apply (force)
schirmer@12854
  1891
done
schirmer@12854
  1892
schirmer@12854
  1893
schirmer@12854
  1894
(* local lemma *)
schirmer@12854
  1895
lemma implmt_methd [rule_format (no_asm)]: 
schirmer@12854
  1896
"\<lbrakk>wf_prog G; G\<turnstile>C\<leadsto>I\<rbrakk> \<Longrightarrow> is_iface G I \<longrightarrow>  
schirmer@12854
  1897
 (\<forall> im    \<in>imethds G I   sig.  
schirmer@12854
  1898
  \<exists> cm\<in>methd G C sig: \<not>is_static cm \<and> \<not> is_static im \<and> 
schirmer@12854
  1899
                      G\<turnstile>resTy cm\<preceq>resTy im \<and>
schirmer@12854
  1900
                      accmodi im = Public \<and> accmodi cm = Public)"
schirmer@12854
  1901
apply (frule implmt_is_class)
schirmer@12854
  1902
apply (erule implmt.induct)
schirmer@12854
  1903
apply   safe
schirmer@12854
  1904
apply   (drule (2) implmt1_methd)
schirmer@12854
  1905
apply   fast
schirmer@12854
  1906
apply  (drule (1) subint_widen_imethds)
schirmer@12854
  1907
apply   simp
schirmer@12854
  1908
apply   assumption
schirmer@12854
  1909
apply  clarify
schirmer@12854
  1910
apply  (drule (2) implmt1_methd)
schirmer@12854
  1911
apply  (force)
schirmer@12854
  1912
apply (frule subcls1D)
schirmer@12854
  1913
apply (drule (1) bspec)
schirmer@12854
  1914
apply clarify
schirmer@12854
  1915
apply (drule (3) r_into_rtrancl [THEN inheritable_instance_methd_props, 
schirmer@12854
  1916
                                 OF _ implmt_is_class])
schirmer@12854
  1917
apply auto 
schirmer@12854
  1918
done
schirmer@12854
  1919
schirmer@12854
  1920
lemma mheadsD [rule_format (no_asm)]: 
schirmer@12854
  1921
"emh \<in> mheads G S t sig \<longrightarrow> wf_prog G \<longrightarrow>
schirmer@12854
  1922
 (\<exists>C D m. t = ClassT C \<and> declrefT emh = ClassT D \<and> 
schirmer@12854
  1923
          accmethd G S C sig = Some m \<and>
schirmer@12854
  1924
          (declclass m = D) \<and> mhead (mthd m) = (mhd emh)) \<or>
schirmer@12854
  1925
 (\<exists>I. t = IfaceT I \<and> ((\<exists>im. im  \<in> accimethds G (pid S) I sig \<and> 
schirmer@12854
  1926
          mthd im = mhd emh) \<or> 
schirmer@12854
  1927
  (\<exists>m. G\<turnstile>Iface I accessible_in (pid S) \<and> accmethd G S Object sig = Some m \<and> 
schirmer@12854
  1928
       accmodi m \<noteq> Private \<and> 
schirmer@12854
  1929
       declrefT emh = ClassT Object \<and> mhead (mthd m) = mhd emh))) \<or>
schirmer@12854
  1930
 (\<exists>T m. t = ArrayT T \<and> G\<turnstile>Array T accessible_in (pid S) \<and>
schirmer@12854
  1931
        accmethd G S Object sig = Some m \<and> accmodi m \<noteq> Private \<and> 
schirmer@12854
  1932
        declrefT emh = ClassT Object \<and> mhead (mthd m) = mhd emh)"
ballarin@14174
  1933
apply (rule_tac ref_ty1="t" in ref_ty_ty.induct [THEN conjunct1])
schirmer@12854
  1934
apply auto
schirmer@12854
  1935
apply (auto simp add: cmheads_def accObjectmheads_def Objectmheads_def)
schirmer@12854
  1936
apply (auto  dest!: accmethd_SomeD)
schirmer@12854
  1937
done
schirmer@12854
  1938
schirmer@12854
  1939
lemma mheads_cases [consumes 2, case_names Class_methd 
schirmer@12854
  1940
                    Iface_methd Iface_Object_methd Array_Object_methd]: 
schirmer@12854
  1941
"\<lbrakk>emh \<in> mheads G S t sig; wf_prog G;
schirmer@12854
  1942
 \<And> C D m. \<lbrakk>t = ClassT C;declrefT emh = ClassT D; accmethd G S C sig = Some m;
schirmer@12854
  1943
           (declclass m = D); mhead (mthd m) = (mhd emh)\<rbrakk> \<Longrightarrow> P emh; 
schirmer@12854
  1944
 \<And> I im. \<lbrakk>t = IfaceT I; im  \<in> accimethds G (pid S) I sig; mthd im = mhd emh\<rbrakk>
schirmer@12854
  1945
          \<Longrightarrow> P emh;
schirmer@12854
  1946
 \<And> I m. \<lbrakk>t = IfaceT I; G\<turnstile>Iface I accessible_in (pid S);
schirmer@12854
  1947
          accmethd G S Object sig = Some m; accmodi m \<noteq> Private;
schirmer@12854
  1948
         declrefT emh = ClassT Object; mhead (mthd m) = mhd emh\<rbrakk> \<Longrightarrow> P emh;
schirmer@12854
  1949
 \<And> T m. \<lbrakk>t = ArrayT T;G\<turnstile>Array T accessible_in (pid S);
schirmer@12854
  1950
          accmethd G S Object sig = Some m; accmodi m \<noteq> Private; 
schirmer@12854
  1951
          declrefT emh = ClassT Object; mhead (mthd m) = mhd emh\<rbrakk> \<Longrightarrow>  P emh 
schirmer@12854
  1952
\<rbrakk> \<Longrightarrow> P emh"
schirmer@12854
  1953
by (blast dest!: mheadsD)
schirmer@12854
  1954
schirmer@12854
  1955
lemma declclassD[rule_format]:
schirmer@12854
  1956
 "\<lbrakk>wf_prog G;class G C = Some c; methd G C sig = Some m; 
schirmer@12854
  1957
   class G (declclass m) = Some d\<rbrakk>
schirmer@12854
  1958
  \<Longrightarrow> table_of (methods d) sig  = Some (mthd m)"
schirmer@12854
  1959
proof -
schirmer@12854
  1960
  assume    wf: "wf_prog G"
schirmer@12854
  1961
  then have ws: "ws_prog G" ..
schirmer@12854
  1962
  assume  clsC: "class G C = Some c"
schirmer@12854
  1963
  from clsC ws 
schirmer@12854
  1964
  show "\<And> m d. \<lbrakk>methd G C sig = Some m; class G (declclass m) = Some d\<rbrakk>
schirmer@12854
  1965
        \<Longrightarrow> table_of (methods d) sig  = Some (mthd m)" 
berghofe@34915
  1966
  proof (induct rule: ws_class_induct)
schirmer@12854
  1967
    case Object
schirmer@12854
  1968
    with wf show "?thesis m d" by auto
schirmer@12854
  1969
  next
berghofe@34915
  1970
    case (Subcls C c)
schirmer@12854
  1971
    let ?newMethods = "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c)) sig"
schirmer@12854
  1972
    show "?thesis m d" 
schirmer@12854
  1973
    proof (cases "?newMethods")
schirmer@12854
  1974
      case None
berghofe@34915
  1975
      from None ws Subcls
berghofe@34915
  1976
      show "?thesis" by (auto simp add: methd_rec) (rule Subcls)
schirmer@12854
  1977
    next
schirmer@12854
  1978
      case Some
berghofe@34915
  1979
      from Some ws Subcls
schirmer@12854
  1980
      show "?thesis" 
wenzelm@32960
  1981
        by (auto simp add: methd_rec
schirmer@12854
  1982
                     dest: wf_prog_cdecl wf_cdecl_supD is_acc_class_is_class)
schirmer@12854
  1983
    qed
schirmer@12854
  1984
  qed
schirmer@12854
  1985
qed
schirmer@12854
  1986
schirmer@12854
  1987
(* Tactical version *)
schirmer@12854
  1988
(*
schirmer@12854
  1989
lemma declclassD[rule_format]:
schirmer@12854
  1990
 "wf_prog G \<longrightarrow>  
schirmer@12854
  1991
 (\<forall> c d m. class G C = Some c \<longrightarrow> methd G C sig = Some m \<longrightarrow> 
schirmer@12854
  1992
  class G (declclass m) = Some d
schirmer@12854
  1993
 \<longrightarrow> table_of (methods d) sig  = Some (mthd m))"
schirmer@12854
  1994
apply (rule class_rec.induct)
schirmer@12854
  1995
apply (rule impI)
schirmer@12854
  1996
apply (rule allI)+
schirmer@12854
  1997
apply (rule impI)
schirmer@12854
  1998
apply (case_tac "C=Object")
schirmer@12854
  1999
apply   (force simp add: methd_rec)
schirmer@12854
  2000
schirmer@12854
  2001
apply   (subst methd_rec)
schirmer@12854
  2002
apply     (blast dest: wf_ws_prog)+
schirmer@12854
  2003
apply   (case_tac "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c)) sig")
schirmer@12854
  2004
apply     (auto dest: wf_prog_cdecl wf_cdecl_supD is_acc_class_is_class)
schirmer@12854
  2005
done
schirmer@12854
  2006
*)
schirmer@12854
  2007
schirmer@12854
  2008
lemma dynmethd_Object:
wenzelm@12937
  2009
  assumes statM: "methd G Object sig = Some statM" and
schirmer@12854
  2010
        private: "accmodi statM = Private" and 
schirmer@12854
  2011
       is_cls_C: "is_class G C" and
schirmer@12854
  2012
             wf: "wf_prog G"
wenzelm@12937
  2013
  shows "dynmethd G Object C sig = Some statM"
schirmer@12854
  2014
proof -
schirmer@12854
  2015
  from is_cls_C wf 
schirmer@12854
  2016
  have subclseq: "G\<turnstile>C \<preceq>\<^sub>C Object" 
schirmer@12854
  2017
    by (auto intro: subcls_ObjectI)
schirmer@12854
  2018
  from wf have ws: "ws_prog G" 
schirmer@12854
  2019
    by simp
schirmer@12854
  2020
  from wf 
schirmer@12854
  2021
  have is_cls_Obj: "is_class G Object" 
schirmer@12854
  2022
    by simp
schirmer@12854
  2023
  from statM subclseq is_cls_Obj ws private
schirmer@12854
  2024
  show ?thesis
schirmer@12854
  2025
  proof (cases rule: dynmethd_cases)
schirmer@12854
  2026
    case Static then show ?thesis .
schirmer@12854
  2027
  next
schirmer@12854
  2028
    case Overrides 
schirmer@12854
  2029
    with private show ?thesis 
schirmer@12854
  2030
      by (auto dest: no_Private_override)
schirmer@12854
  2031
  qed
schirmer@12854
  2032
qed
schirmer@12854
  2033
schirmer@12854
  2034
lemma wf_imethds_hiding_objmethdsD: 
wenzelm@12937
  2035
  assumes     old: "methd G Object sig = Some old" and
wenzelm@12937
  2036
          is_if_I: "is_iface G I" and
wenzelm@12937
  2037
               wf: "wf_prog G" and    
wenzelm@12937
  2038
      not_private: "accmodi old \<noteq> Private" and
wenzelm@12937
  2039
              new: "new \<in> imethds G I sig" 
wenzelm@12937
  2040
  shows "G\<turnstile>resTy new\<preceq>resTy old \<and> is_static new = is_static old" (is "?P new")
schirmer@12854
  2041
proof -
schirmer@12854
  2042
  from wf have ws: "ws_prog G" by simp
schirmer@12854
  2043
  {
schirmer@12854
  2044
    fix I i new
schirmer@12854
  2045
    assume ifI: "iface G I = Some i"
schirmer@12854
  2046
    assume new: "table_of (imethods i) sig = Some new" 
schirmer@12854
  2047
    from ifI new not_private wf old  
schirmer@12854
  2048
    have "?P (I,new)"
schirmer@12854
  2049
      by (auto dest!: wf_prog_idecl wf_idecl_hiding cond_hiding_entailsD
schirmer@12854
  2050
            simp del: methd_Object)
schirmer@12854
  2051
  } note hyp_newmethod = this  
schirmer@12854
  2052
  from is_if_I ws new 
schirmer@12854
  2053
  show ?thesis
schirmer@12854
  2054
  proof (induct rule: ws_interface_induct)
schirmer@12854
  2055
    case (Step I i)
schirmer@12854
  2056
    assume ifI: "iface G I = Some i" 
schirmer@12854
  2057
    assume new: "new \<in> imethds G I sig" 
schirmer@12854
  2058
    from Step
schirmer@12854
  2059
    have hyp: "\<forall> J \<in> set (isuperIfs i). (new \<in> imethds G J sig \<longrightarrow> ?P new)"
schirmer@12854
  2060
      by auto 
schirmer@12854
  2061
    from new ifI ws
schirmer@12854
  2062
    show "?P new"
schirmer@12854
  2063
    proof (cases rule: imethds_cases)
schirmer@12854
  2064
      case NewMethod
schirmer@12854
  2065
      with ifI hyp_newmethod
schirmer@12854
  2066
      show ?thesis
wenzelm@32960
  2067
        by auto
schirmer@12854
  2068
    next
schirmer@12854
  2069
      case (InheritedMethod J)
schirmer@12854
  2070
      assume "J \<in> set (isuperIfs i)" 
schirmer@12854
  2071
             "new \<in> imethds G J sig"
schirmer@12854
  2072
      with hyp 
schirmer@12854
  2073
      show "?thesis"
wenzelm@32960
  2074
        by auto
schirmer@12854
  2075
    qed
schirmer@12854
  2076
  qed
schirmer@12854
  2077
qed
schirmer@12854
  2078
schirmer@12854
  2079
text {*
schirmer@12854
  2080
Which dynamic classes are valid to look up a member of a distinct static type?
schirmer@12854
  2081
We have to distinct class members (named static members in Java) 
schirmer@12854
  2082
from instance members. Class members are global to all Objects of a class,
schirmer@12854
  2083
instance members are local to a single Object instance. If a member is
schirmer@12854
  2084
equipped with the static modifier it is a class member, else it is an 
schirmer@12854
  2085
instance member.
schirmer@12854
  2086
The following table gives an overview of the current framework. We assume
schirmer@12854
  2087
to have a reference with static type statT and a dynamic class dynC. Between
schirmer@12854
  2088
both of these types the widening relation holds 
schirmer@12854
  2089
@{term "G\<turnstile>Class dynC\<preceq> statT"}. Unfortunately this ordinary widening relation 
schirmer@12854
  2090
isn't enough to describe the valid lookup classes, since we must cope the
schirmer@12854
  2091
special cases of arrays and interfaces,too. If we statically expect an array or
schirmer@12854
  2092
inteface we may lookup a field or a method in Object which isn't covered in 
schirmer@12854
  2093
the widening relation.
schirmer@12925
  2094
schirmer@12854
  2095
statT      field         instance method       static (class) method
schirmer@12854
  2096
------------------------------------------------------------------------
schirmer@12854
  2097
 NullT      /                  /                   /
schirmer@12854
  2098
 Iface      /                dynC                Object
schirmer@12854
  2099
 Class    dynC               dynC                 dynC
schirmer@12854
  2100
 Array      /                Object              Object
schirmer@12925
  2101
schirmer@12854
  2102
In most cases we con lookup the member in the dynamic class. But as an
schirmer@12854
  2103
interface can't declare new static methods, nor an array can define new
schirmer@12854
  2104
methods at all, we have to lookup methods in the base class Object.
schirmer@12854
  2105
schirmer@12854
  2106
The limitation to classes in the field column is artificial  and comes out
schirmer@12854
  2107
of the typing rule for the field access (see rule @{text "FVar"} in the 
schirmer@12854
  2108
welltyping relation @{term "wt"} in theory WellType). 
schirmer@12854
  2109
I stems out of the fact, that Object
schirmer@12854
  2110
indeed has no non private fields. So interfaces and arrays can actually
schirmer@12854
  2111
have no fields at all and a field access would be senseless. (In Java
schirmer@12854
  2112
interfaces are allowed to declare new fields but in current Bali not!).
schirmer@12854
  2113
So there is no principal reason why we should not allow Objects to declare
schirmer@12854
  2114
non private fields. Then we would get the following column:
schirmer@12925
  2115
       
schirmer@12854
  2116
 statT    field
schirmer@12854
  2117
----------------- 
schirmer@12854
  2118
 NullT      /  
schirmer@12854
  2119
 Iface    Object 
schirmer@12854
  2120
 Class    dynC 
schirmer@12854
  2121
 Array    Object
schirmer@12854
  2122
*}
schirmer@12854
  2123
consts valid_lookup_cls:: "prog \<Rightarrow> ref_ty \<Rightarrow> qtname \<Rightarrow> bool \<Rightarrow> bool"
schirmer@12854
  2124
                        ("_,_ \<turnstile> _ valid'_lookup'_cls'_for _" [61,61,61,61] 60)
schirmer@12854
  2125
primrec
schirmer@12854
  2126
"G,NullT    \<turnstile> dynC valid_lookup_cls_for static_membr = False"
schirmer@12854
  2127
"G,IfaceT I \<turnstile> dynC valid_lookup_cls_for static_membr 
schirmer@12854
  2128
              = (if static_membr 
schirmer@12854
  2129
                    then dynC=Object 
schirmer@12854
  2130
                    else G\<turnstile>Class dynC\<preceq> Iface I)"
schirmer@12854
  2131
"G,ClassT C \<turnstile> dynC valid_lookup_cls_for static_membr = G\<turnstile>Class dynC\<preceq> Class C"
schirmer@12854
  2132
"G,ArrayT T \<turnstile> dynC valid_lookup_cls_for static_membr = (dynC=Object)"
schirmer@12854
  2133
schirmer@12854
  2134
lemma valid_lookup_cls_is_class:
wenzelm@12937
  2135
  assumes dynC: "G,statT \<turnstile> dynC valid_lookup_cls_for static_membr" and
schirmer@12854
  2136
      ty_statT: "isrtype G statT" and
schirmer@12854
  2137
            wf: "wf_prog G"
wenzelm@12937
  2138
  shows "is_class G dynC"
schirmer@12854
  2139
proof (cases statT)
schirmer@12854
  2140
  case NullT
schirmer@12854
  2141
  with dynC ty_statT show ?thesis
schirmer@12854
  2142
    by (auto dest: widen_NT2)
schirmer@12854
  2143
next
schirmer@12854
  2144
  case (IfaceT I)
schirmer@12854
  2145
  with dynC wf show ?thesis
schirmer@12854
  2146
    by (auto dest: implmt_is_class)
schirmer@12854
  2147
next
schirmer@12854
  2148
  case (ClassT C)
schirmer@12854
  2149
  with dynC ty_statT show ?thesis
schirmer@12854
  2150
    by (auto dest: subcls_is_class2)
schirmer@12854
  2151
next
schirmer@12854
  2152
  case (ArrayT T)
schirmer@12854
  2153
  with dynC wf show ?thesis
schirmer@12854
  2154
    by (auto)
schirmer@12854
  2155
qed
schirmer@12854
  2156
schirmer@12854
  2157
declare split_paired_All [simp del] split_paired_Ex [simp del]
wenzelm@24019
  2158
declaration {* K (Simplifier.map_ss (fn ss => ss delloop "split_all_tac")) *}
wenzelm@24019
  2159
declaration {* K (Classical.map_cs (fn cs => cs delSWrapper "split_all_tac")) *}
wenzelm@24019
  2160
schirmer@12854
  2161
lemma dynamic_mheadsD:   
schirmer@12854
  2162
"\<lbrakk>emh \<in> mheads G S statT sig;    
schirmer@12854
  2163
  G,statT \<turnstile> dynC valid_lookup_cls_for (is_static emh);
schirmer@12854
  2164
  isrtype G statT; wf_prog G
schirmer@12854
  2165
 \<rbrakk> \<Longrightarrow> \<exists>m \<in> dynlookup G statT dynC sig: 
schirmer@12854
  2166
          is_static m=is_static emh \<and> G\<turnstile>resTy m\<preceq>resTy emh"
schirmer@12854
  2167
proof - 
schirmer@12854
  2168
  assume      emh: "emh \<in> mheads G S statT sig"
schirmer@12854
  2169
  and          wf: "wf_prog G"
schirmer@12854
  2170
  and   dynC_Prop: "G,statT \<turnstile> dynC valid_lookup_cls_for (is_static emh)"
schirmer@12854
  2171
  and      istype: "isrtype G statT"
schirmer@12854
  2172
  from dynC_Prop istype wf 
schirmer@12854
  2173
  obtain y where
schirmer@12854
  2174
    dynC: "class G dynC = Some y" 
schirmer@12854
  2175
    by (auto dest: valid_lookup_cls_is_class)
schirmer@12854
  2176
  from emh wf show ?thesis
schirmer@12854
  2177
  proof (cases rule: mheads_cases)
schirmer@12854
  2178
    case Class_methd
schirmer@12854
  2179
    fix statC statDeclC sm
schirmer@12854
  2180
    assume     statC: "statT = ClassT statC"
schirmer@12854
  2181
    assume            "accmethd G S statC sig = Some sm"
schirmer@12854
  2182
    then have     sm: "methd G statC sig = Some sm" 
schirmer@12854
  2183
      by (blast dest: accmethd_SomeD)  
schirmer@12854
  2184
    assume eq_mheads: "mhead (mthd sm) = mhd emh"
schirmer@12854
  2185
    from statC 
schirmer@12854
  2186
    have dynlookup: "dynlookup G statT dynC sig = dynmethd G statC dynC sig"
schirmer@12854
  2187
      by (simp add: dynlookup_def)
schirmer@12854
  2188
    from wf statC istype dynC_Prop sm 
schirmer@12854
  2189
    obtain dm where
schirmer@12854
  2190
      "dynmethd G statC dynC sig = Some dm"
schirmer@12854
  2191
      "is_static dm = is_static sm" 
schirmer@12854
  2192
      "G\<turnstile>resTy dm\<preceq>resTy sm"  
schirmer@12925
  2193
      by (force dest!: ws_dynmethd accmethd_SomeD)
schirmer@12854
  2194
    with dynlookup eq_mheads 
schirmer@12854
  2195
    show ?thesis 
schirmer@12854
  2196
      by (cases emh type: *) (auto)
schirmer@12854
  2197
  next
schirmer@12854
  2198
    case Iface_methd
schirmer@12854
  2199
    fix I im
schirmer@12854
  2200
    assume    statI: "statT = IfaceT I" and
schirmer@12854
  2201
          eq_mheads: "mthd im = mhd emh" and
schirmer@12854
  2202
                     "im \<in> accimethds G (pid S) I sig" 
schirmer@12854
  2203
    then have im: "im \<in> imethds G I sig" 
schirmer@12854
  2204
      by (blast dest: accimethdsD)
schirmer@12854
  2205
    with istype statI eq_mheads wf 
schirmer@12854
  2206
    have not_static_emh: "\<not> is_static emh"
schirmer@12854
  2207
      by (cases emh) (auto dest: wf_prog_idecl imethds_wf_mhead)
schirmer@12854
  2208
    from statI im
schirmer@12854
  2209
    have dynlookup: "dynlookup G statT dynC sig = methd G dynC sig"
schirmer@12854
  2210
      by (auto simp add: dynlookup_def dynimethd_def) 
schirmer@12854
  2211
    from wf dynC_Prop statI istype im not_static_emh 
schirmer@12854
  2212
    obtain dm where
schirmer@12854
  2213
      "methd G dynC sig = Some dm"
schirmer@12854
  2214
      "is_static dm = is_static im" 
schirmer@12854
  2215
      "G\<turnstile>resTy (mthd dm)\<preceq>resTy (mthd im)" 
schirmer@12925
  2216
      by (force dest: implmt_methd)
schirmer@12854
  2217
    with dynlookup eq_mheads
schirmer@12854
  2218
    show ?thesis 
schirmer@12854
  2219
      by (cases emh type: *) (auto)
schirmer@12854
  2220
  next
schirmer@12854
  2221
    case Iface_Object_methd
schirmer@12854
  2222
    fix I sm
schirmer@12854
  2223
    assume   statI: "statT = IfaceT I" and
schirmer@12854
  2224
                sm: "accmethd G S Object sig = Some sm" and 
schirmer@12854
  2225
         eq_mheads: "mhead (mthd sm) = mhd emh" and
schirmer@12854
  2226
             nPriv: "accmodi sm \<noteq> Private"
schirmer@12854
  2227
     show ?thesis 
schirmer@12854
  2228
     proof (cases "imethds G I sig = {}")
schirmer@12854
  2229
       case True
schirmer@12854
  2230
       with statI 
schirmer@12854
  2231
       have dynlookup: "dynlookup G statT dynC sig = dynmethd G Object dynC sig"
wenzelm@32960
  2232
         by (simp add: dynlookup_def dynimethd_def)
schirmer@12854
  2233
       from wf dynC 
schirmer@12854
  2234
       have subclsObj: "G\<turnstile>dynC \<preceq>\<^sub>C Object"
wenzelm@32960
  2235
         by (auto intro: subcls_ObjectI)
schirmer@12854
  2236
       from wf dynC dynC_Prop istype sm subclsObj 
schirmer@12854
  2237
       obtain dm where
wenzelm@32960
  2238
         "dynmethd G Object dynC sig = Some dm"
wenzelm@32960
  2239
         "is_static dm = is_static sm" 
wenzelm@32960
  2240
         "G\<turnstile>resTy (mthd dm)\<preceq>resTy (mthd sm)"  
wenzelm@32960
  2241
         by (auto dest!: ws_dynmethd accmethd_SomeD 
schirmer@12925
  2242
                  intro: class_Object [OF wf] intro: that)
schirmer@12854
  2243
       with dynlookup eq_mheads
schirmer@12854
  2244
       show ?thesis 
wenzelm@32960
  2245
         by (cases emh type: *) (auto)
schirmer@12854
  2246
     next
schirmer@12854
  2247
       case False
schirmer@12854
  2248
       with statI
schirmer@12854
  2249
       have dynlookup: "dynlookup G statT dynC sig = methd G dynC sig"
wenzelm@32960
  2250
         by (simp add: dynlookup_def dynimethd_def)
schirmer@12854
  2251
       from istype statI
schirmer@12854
  2252
       have "is_iface G I"
wenzelm@32960
  2253
         by auto
schirmer@12854
  2254
       with wf sm nPriv False 
schirmer@12854
  2255
       obtain im where
wenzelm@32960
  2256
              im: "im \<in> imethds G I sig" and
wenzelm@32960
  2257
         eq_stat: "is_static im = is_static sm" and
schirmer@12854
  2258
         resProp: "G\<turnstile>resTy (mthd im)\<preceq>resTy (mthd sm)"
wenzelm@32960
  2259
         by (auto dest: wf_imethds_hiding_objmethdsD accmethd_SomeD)
schirmer@12854
  2260
       from im wf statI istype eq_stat eq_mheads
schirmer@12854
  2261
       have not_static_sm: "\<not> is_static emh"
wenzelm@32960
  2262
         by (cases emh) (auto dest: wf_prog_idecl imethds_wf_mhead)
schirmer@12854
  2263
       from im wf dynC_Prop dynC istype statI not_static_sm
schirmer@12854
  2264
       obtain dm where
wenzelm@32960
  2265
         "methd G dynC sig = Some dm"
wenzelm@32960
  2266
         "is_static dm = is_static im" 
wenzelm@32960
  2267
         "G\<turnstile>resTy (mthd dm)\<preceq>resTy (mthd im)" 
wenzelm@32960
  2268
         by (auto dest: implmt_methd)
schirmer@12854
  2269
       with wf eq_stat resProp dynlookup eq_mheads
schirmer@12854
  2270
       show ?thesis 
wenzelm@32960
  2271
         by (cases emh type: *) (auto intro: widen_trans)
schirmer@12854
  2272
     qed
schirmer@12854
  2273
  next
schirmer@12854
  2274
    case Array_Object_methd
schirmer@12854
  2275
    fix T sm
schirmer@12854
  2276
    assume statArr: "statT = ArrayT T" and
schirmer@12854
  2277
                sm: "accmethd G S Object sig = Some sm" and 
schirmer@12854
  2278
         eq_mheads: "mhead (mthd sm) = mhd emh" 
schirmer@12854
  2279
    from statArr dynC_Prop wf
schirmer@12854
  2280
    have dynlookup: "dynlookup G statT dynC sig = methd G Object sig"
schirmer@12854
  2281
      by (auto simp add: dynlookup_def dynmethd_C_C)
schirmer@12854
  2282
    with sm eq_mheads sm 
schirmer@12854
  2283
    show ?thesis 
schirmer@12854
  2284
      by (cases emh type: *) (auto dest: accmethd_SomeD)
schirmer@12854
  2285
  qed
schirmer@12854
  2286
qed
schirmer@12925
  2287
declare split_paired_All [simp] split_paired_Ex [simp]
wenzelm@24019
  2288
declaration {* K (Classical.map_cs (fn cs => cs addSbefore ("split_all_tac", split_all_tac))) *}
wenzelm@24019
  2289
declaration {* K (Simplifier.map_ss (fn ss => ss addloop ("split_all_tac", split_all_tac))) *}
schirmer@12854
  2290
schirmer@12854
  2291
(* Tactical version *)
schirmer@12854
  2292
(*
schirmer@12854
  2293
lemma dynamic_mheadsD: "  
schirmer@12854
  2294
 \<lbrakk>emh \<in> mheads G S statT sig; wf_prog G; class G dynC = Some y;  
schirmer@12854
  2295
   if (\<exists>T. statT=ArrayT T) then dynC=Object else G\<turnstile>Class dynC\<preceq>RefT statT; 
schirmer@12854
  2296
   isrtype G statT\<rbrakk> \<Longrightarrow>  
schirmer@12854
  2297
  \<exists>m \<in> dynlookup G statT dynC sig: 
schirmer@12854
  2298
     static (mthd m)=static (mhd emh) \<and> G\<turnstile>resTy (mthd m)\<preceq>resTy (mhd emh)"
schirmer@12854
  2299
apply (drule mheadsD)
schirmer@12854
  2300
apply safe
schirmer@12854
  2301
       -- reftype statT is a class  
schirmer@12854
  2302
apply  (case_tac "\<exists>T. ClassT C = ArrayT T")
schirmer@12854
  2303
apply    (simp)
schirmer@12854
  2304
schirmer@12854
  2305
apply    (clarsimp simp add: dynlookup_def )
schirmer@12854
  2306
apply    (frule_tac statC="C" and dynC="dynC"  and sig="sig"  
schirmer@12854
  2307
         in ws_dynmethd)
schirmer@12854
  2308
apply      assumption+
schirmer@12854
  2309
apply    (case_tac "emh")  
schirmer@12854
  2310
apply    (force dest: accmethd_SomeD)
schirmer@12854
  2311
schirmer@12854
  2312
       -- reftype statT is a interface, method defined in interface 
schirmer@12854
  2313
apply    (clarsimp simp add: dynlookup_def)
schirmer@12854
  2314
apply    (drule (1) implmt_methd)
schirmer@12854
  2315
apply      blast
schirmer@12854
  2316
apply      blast
schirmer@12854
  2317
apply    (clarify)  
schirmer@12854
  2318
apply    (unfold dynimethd_def)
schirmer@12854
  2319
apply    (rule_tac x="cm" in bexI)
schirmer@12854
  2320
apply      (case_tac "emh")
schirmer@12854
  2321
apply      force
schirmer@12854
  2322
schirmer@12854
  2323
apply      force
schirmer@12854
  2324
schirmer@12854
  2325
        -- reftype statT is a interface, method defined in Object 
schirmer@12854
  2326
apply    (simp add: dynlookup_def)
schirmer@12854
  2327
apply    (simp only: dynimethd_def)
schirmer@12854
  2328
apply    (case_tac "imethds G I sig = {}")
schirmer@12854
  2329
apply       simp
schirmer@12854
  2330
apply       (frule_tac statC="Object" and dynC="dynC"  and sig="sig"  
schirmer@12854
  2331
             in ws_dynmethd)
schirmer@12854
  2332
apply          (blast intro: subcls_ObjectI wf_ws_prog) 
schirmer@12854
  2333
apply          (blast dest: class_Object)
schirmer@12854
  2334
apply       (case_tac "emh") 
schirmer@12854
  2335
apply       (force dest: accmethd_SomeD)
schirmer@12854
  2336
schirmer@12854
  2337
apply       simp
schirmer@12854
  2338
apply       (subgoal_tac "\<exists> im. im \<in> imethds G I sig") 
schirmer@12854
  2339
prefer 2      apply blast
schirmer@12854
  2340
apply       clarify
schirmer@12854
  2341
apply       (frule (1) implmt_methd)
schirmer@12854
  2342
apply         simp
schirmer@12854
  2343
apply         blast  
schirmer@12854
  2344
apply       (clarify dest!: accmethd_SomeD)
schirmer@12854
  2345
apply       (frule (4) iface_overrides_Object)
schirmer@12854
  2346
apply       clarify
schirmer@12854
  2347
apply       (case_tac emh)
schirmer@12854
  2348
apply       force
schirmer@12854
  2349
schirmer@12854
  2350
        -- reftype statT is a array
schirmer@12854
  2351
apply    (simp add: dynlookup_def)
schirmer@12854
  2352
apply    (case_tac emh)
schirmer@12854
  2353
apply    (force dest: accmethd_SomeD simp add: dynmethd_def)
schirmer@12854
  2354
done
schirmer@12854
  2355
*)
schirmer@12854
  2356
schirmer@12925
  2357
(* FIXME occasionally convert to ws_class_induct*) 
schirmer@12854
  2358
lemma methd_declclass:
schirmer@12854
  2359
"\<lbrakk>class G C = Some c; wf_prog G; methd G C sig = Some m\<rbrakk> 
schirmer@12854
  2360
 \<Longrightarrow> methd G (declclass m) sig = Some m"
schirmer@12854
  2361
proof -
schirmer@12854
  2362
  assume asm: "class G C = Some c" "wf_prog G" "methd G C sig = Some m"
schirmer@12854
  2363
  have "wf_prog G  \<longrightarrow> 
wenzelm@32960
  2364
           (\<forall> c m. class G C = Some c \<longrightarrow>  methd G C sig = Some m 
schirmer@12854
  2365
                   \<longrightarrow>  methd G (declclass m) sig = Some m)"      (is "?P G C") 
schirmer@12854
  2366
  proof (rule class_rec.induct,intro allI impI)
schirmer@12854
  2367
    fix G C c m
schirmer@12854
  2368
    assume hyp: "\<forall>c. C \<noteq> Object \<and> ws_prog G \<and> class G C = Some c \<longrightarrow>
schirmer@12854
  2369
                     ?P G (super c)"
schirmer@12854
  2370
    assume wf: "wf_prog G" and cls_C: "class G C = Some c" and
schirmer@12854
  2371
            m: "methd G C sig = Some m"
schirmer@12854
  2372
    show "methd G (declclass m) sig = Some m"
schirmer@12854
  2373
    proof (cases "C=Object")
schirmer@12854
  2374
      case True
schirmer@12854
  2375
      with wf m show ?thesis by (auto intro: table_of_map_SomeI)
schirmer@12854
  2376
    next
schirmer@12854
  2377
      let ?filter="filter_tab (\<lambda>sig m. G\<turnstile>C inherits method sig m)"
schirmer@12854
  2378
      let ?table = "table_of (map (\<lambda>(s, m). (s, C, m)) (methods c))"
schirmer@12854
  2379
      case False
schirmer@12854
  2380
      with cls_C wf m
schirmer@12854
  2381
      have methd_C: "(?filter (methd G (super c)) ++ ?table) sig = Some m "
wenzelm@32960
  2382
        by (simp add: methd_rec)
schirmer@12854
  2383
      show ?thesis
schirmer@12854
  2384
      proof (cases "?table sig")
wenzelm@32960
  2385
        case None
wenzelm@32960
  2386
        from this methd_C have "?filter (methd G (super c)) sig = Some m"
wenzelm@32960
  2387
          by simp
wenzelm@32960
  2388
        moreover
wenzelm@32960
  2389
        from wf cls_C False obtain sup where "class G (super c) = Some sup"
wenzelm@32960
  2390
          by (blast dest: wf_prog_cdecl wf_cdecl_supD is_acc_class_is_class)
wenzelm@32960
  2391
        moreover note wf False cls_C 
wenzelm@32960
  2392
        ultimately show ?thesis by (auto intro: hyp [rule_format])
schirmer@12854
  2393
      next
wenzelm@32960
  2394
        case Some
wenzelm@32960
  2395
        from this methd_C m show ?thesis by auto 
schirmer@12854
  2396
      qed
schirmer@12854
  2397
    qed
wenzelm@32960
  2398
  qed   
schirmer@12854
  2399
  with asm show ?thesis by auto
schirmer@12854
  2400
qed
schirmer@12854
  2401
schirmer@12854
  2402
lemma dynmethd_declclass:
schirmer@12854
  2403
 "\<lbrakk>dynmethd G statC dynC sig = Some m;
schirmer@12854
  2404
   wf_prog G; is_class G statC
schirmer@12854
  2405
  \<rbrakk> \<Longrightarrow> methd G (declclass m) sig = Some m"
schirmer@12854
  2406
by (auto dest: dynmethd_declC)
schirmer@12854
  2407
schirmer@12854
  2408
lemma dynlookup_declC:
schirmer@12854
  2409
 "\<lbrakk>dynlookup G statT dynC sig = Some m; wf_prog G;
schirmer@12854
  2410
   is_class G dynC;isrtype G statT
schirmer@12854
  2411
  \<rbrakk> \<Longrightarrow> G\<turnstile>dynC \<preceq>\<^sub>C (declclass m) \<and> is_class G (declclass m)"
schirmer@12854
  2412
by (cases "statT")
schirmer@12854
  2413
   (auto simp add: dynlookup_def dynimethd_def 
schirmer@12854
  2414
             dest: methd_declC dynmethd_declC)
schirmer@12854
  2415
schirmer@12854
  2416
lemma dynlookup_Array_declclassD [simp]:
schirmer@12854
  2417
"\<lbrakk>dynlookup G (ArrayT T) Object sig = Some dm;wf_prog G\<rbrakk> 
schirmer@12854
  2418
 \<Longrightarrow> declclass dm = Object"
schirmer@12854
  2419
proof -
schirmer@12854
  2420
  assume dynL: "dynlookup G (ArrayT T) Object sig = Some dm"
schirmer@12854
  2421
  assume wf: "wf_prog G"
schirmer@12854
  2422
  from wf have ws: "ws_prog G" by auto
schirmer@12854
  2423
  from wf have is_cls_Obj: "is_class G Object" by auto
schirmer@12854
  2424
  from dynL wf
schirmer@12854
  2425
  show ?thesis
schirmer@12854
  2426
    by (auto simp add: dynlookup_def dynmethd_C_C [OF is_cls_Obj ws]
schirmer@12854
  2427
                 dest: methd_Object_SomeD)
schirmer@12854
  2428
qed   
schirmer@12854
  2429
  
schirmer@12925
  2430
schirmer@12854
  2431
declare split_paired_All [simp del] split_paired_Ex [simp del]
wenzelm@24019
  2432
declaration {* K (Simplifier.map_ss (fn ss => ss delloop "split_all_tac")) *}
wenzelm@24019
  2433
declaration {* K (Classical.map_cs (fn cs => cs delSWrapper "split_all_tac")) *}
wenzelm@24019
  2434
schirmer@12854
  2435
lemma wt_is_type: "E,dt\<Turnstile>v\<Colon>T \<Longrightarrow>  wf_prog (prg E) \<longrightarrow> 
schirmer@12854
  2436
  dt=empty_dt \<longrightarrow> (case T of 
schirmer@12854
  2437
                     Inl T \<Rightarrow> is_type (prg E) T 
schirmer@12854
  2438
                   | Inr Ts \<Rightarrow> Ball (set Ts) (is_type (prg E)))"
schirmer@12854
  2439
apply (unfold empty_dt_def)
schirmer@12854
  2440
apply (erule wt.induct)
schirmer@12854
  2441
apply (auto split del: split_if_asm simp del: snd_conv 
schirmer@12854
  2442
            simp add: is_acc_class_def is_acc_type_def)
schirmer@12854
  2443
apply    (erule typeof_empty_is_type)
schirmer@13688
  2444
apply   (frule (1) wf_prog_cdecl [THEN wf_cdecl_supD], 
schirmer@12854
  2445
        force simp del: snd_conv, clarsimp simp add: is_acc_class_def)
schirmer@12854
  2446
apply  (drule (1) max_spec2mheads [THEN conjunct1, THEN mheadsD])
schirmer@12854
  2447
apply  (drule_tac [2] accfield_fields) 
schirmer@12854
  2448
apply  (frule class_Object)
schirmer@12854
  2449
apply  (auto dest: accmethd_rT_is_type 
schirmer@12854
  2450
                   imethds_wf_mhead [THEN conjunct1, THEN rT_is_acc_type]
schirmer@12854
  2451
             dest!:accimethdsD
schirmer@12854
  2452
             simp del: class_Object
schirmer@12854
  2453
             simp add: is_acc_type_def
schirmer@12854
  2454
    )
schirmer@12854
  2455
done
schirmer@12854
  2456
declare split_paired_All [simp] split_paired_Ex [simp]
wenzelm@24019
  2457
declaration {* K (Classical.map_cs (fn cs => cs addSbefore ("split_all_tac", split_all_tac))) *}
wenzelm@24019
  2458
declaration {* K (Simplifier.map_ss (fn ss => ss addloop ("split_all_tac", split_all_tac))) *}
schirmer@12854
  2459
schirmer@12854
  2460
lemma ty_expr_is_type: 
schirmer@12854
  2461
"\<lbrakk>E\<turnstile>e\<Colon>-T; wf_prog (prg E)\<rbrakk> \<Longrightarrow> is_type (prg E) T"
schirmer@12854
  2462
by (auto dest!: wt_is_type)
schirmer@12854
  2463
lemma ty_var_is_type: 
schirmer@12854
  2464
"\<lbrakk>E\<turnstile>v\<Colon>=T; wf_prog (prg E)\<rbrakk> \<Longrightarrow> is_type (prg E) T"
schirmer@12854
  2465
by (auto dest!: wt_is_type)
schirmer@12854
  2466
lemma ty_exprs_is_type: 
schirmer@12854
  2467
"\<lbrakk>E\<turnstile>es\<Colon>\<doteq>Ts; wf_prog (prg E)\<rbrakk> \<Longrightarrow> Ball (set Ts) (is_type (prg E))"
schirmer@12854
  2468
by (auto dest!: wt_is_type)
schirmer@12854
  2469
schirmer@12854
  2470
schirmer@12854
  2471
lemma static_mheadsD: 
schirmer@12854
  2472
 "\<lbrakk> emh \<in> mheads G S t sig; wf_prog G; E\<turnstile>e\<Colon>-RefT t; prg E=G ; 
schirmer@12854
  2473
   invmode (mhd emh) e \<noteq> IntVir 
schirmer@12854
  2474
  \<rbrakk> \<Longrightarrow> \<exists>m. (   (\<exists> C. t = ClassT C \<and> accmethd G S C sig = Some m)
schirmer@12854
  2475
               \<or> (\<forall> C. t \<noteq> ClassT C \<and> accmethd G S Object sig = Some m )) \<and> 
schirmer@12854
  2476
          declrefT emh = ClassT (declclass m) \<and>  mhead (mthd m) = (mhd emh)"
schirmer@12854
  2477
apply (subgoal_tac "is_static emh \<or> e = Super")
schirmer@12854
  2478
defer apply (force simp add: invmode_def)
schirmer@12854
  2479
apply (frule  ty_expr_is_type)
schirmer@12854
  2480
apply   simp
schirmer@12854
  2481
apply (case_tac "is_static emh")
schirmer@12854
  2482
apply  (frule (1) mheadsD)
schirmer@12854
  2483
apply  clarsimp
schirmer@12854
  2484
apply  safe
schirmer@12854
  2485
apply    blast
schirmer@12854
  2486
apply   (auto dest!: imethds_wf_mhead
schirmer@12854
  2487
                     accmethd_SomeD 
schirmer@12854
  2488
                     accimethdsD
schirmer@12854
  2489
              simp add: accObjectmheads_def Objectmheads_def)
schirmer@12854
  2490
schirmer@12854
  2491
apply  (erule wt_elim_cases)
schirmer@12854
  2492
apply  (force simp add: cmheads_def)
schirmer@12854
  2493
done
schirmer@12854
  2494
schirmer@12854
  2495
lemma wt_MethdI:  
schirmer@12854
  2496
"\<lbrakk>methd G C sig = Some m; wf_prog G;  
schirmer@12854
  2497
  class G C = Some c\<rbrakk> \<Longrightarrow>  
schirmer@12854
  2498
 \<exists>T. \<lparr>prg=G,cls=(declclass m),
schirmer@13688
  2499
      lcl=callee_lcl (declclass m) sig (mthd m)\<rparr>\<turnstile> Methd C sig\<Colon>-T \<and> G\<turnstile>T\<preceq>resTy m"
schirmer@12854
  2500
apply (frule (2) methd_wf_mdecl, clarify)
schirmer@12854
  2501
apply (force dest!: wf_mdecl_bodyD intro!: wt.Methd)
schirmer@12854
  2502
done
schirmer@12854
  2503
schirmer@12854
  2504
subsection "accessibility concerns"
schirmer@12854
  2505
schirmer@12854
  2506
lemma mheads_type_accessible:
schirmer@12854
  2507
 "\<lbrakk>emh \<in> mheads G S T sig; wf_prog G\<rbrakk>
schirmer@12854
  2508
 \<Longrightarrow> G\<turnstile>RefT T accessible_in (pid S)"
schirmer@12854
  2509
by (erule mheads_cases)
schirmer@12854
  2510
   (auto dest: accmethd_SomeD accessible_from_commonD accimethdsD)
schirmer@12854
  2511
schirmer@12925
  2512
lemma static_to_dynamic_accessible_from_aux:
schirmer@12854
  2513
"\<lbrakk>G\<turnstile>m of C accessible_from accC;wf_prog G\<rbrakk> 
schirmer@12854
  2514
 \<Longrightarrow> G\<turnstile>m in C dyn_accessible_from accC"
schirmer@12854
  2515
proof (induct rule: accessible_fromR.induct)
schirmer@12854
  2516
qed (auto intro: dyn_accessible_fromR.intros 
schirmer@12854
  2517
                 member_of_to_member_in
schirmer@12854
  2518
                 static_to_dynamic_overriding)
schirmer@12854
  2519
schirmer@12854
  2520
lemma static_to_dynamic_accessible_from:
wenzelm@12937
  2521
  assumes stat_acc: "G\<turnstile>m of statC accessible_from accC" and
schirmer@12854
  2522
          subclseq: "G\<turnstile>dynC \<preceq>\<^sub>C statC" and
schirmer@12854
  2523
                wf: "wf_prog G"
wenzelm@12937
  2524
  shows "G\<turnstile>m in dynC dyn_accessible_from accC"
schirmer@12854
  2525
proof - 
schirmer@12854
  2526
  from stat_acc subclseq 
schirmer@12854
  2527
  show ?thesis (is "?Dyn_accessible m")
schirmer@12854
  2528
  proof (induct rule: accessible_fromR.induct)