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