src/HOL/Bali/Decl.thy
author wenzelm
Mon Mar 22 20:58:52 2010 +0100 (2010-03-22)
changeset 35898 c890a3835d15
parent 35547 991a6af75978
child 37678 0040bafffdef
permissions -rw-r--r--
recovered header;
wenzelm@12857
     1
(*  Title:      HOL/Bali/Decl.thy
schirmer@12925
     2
    Author:     David von Oheimb and Norbert Schirmer
schirmer@12854
     3
*)
schirmer@12854
     4
header {* Field, method, interface, and class declarations, whole Java programs
schirmer@12854
     5
*}
schirmer@12854
     6
haftmann@26566
     7
theory Decl
haftmann@27682
     8
imports Term Table (** order is significant, because of clash for "var" **)
haftmann@26566
     9
begin
schirmer@12854
    10
schirmer@12854
    11
text {*
schirmer@12854
    12
improvements:
schirmer@12854
    13
\begin{itemize}
schirmer@12854
    14
\item clarification and correction of some aspects of the package/access concept
schirmer@12854
    15
  (Also submitted as bug report to the Java Bug Database:
schirmer@12854
    16
   Bug Id: 4485402 and Bug Id: 4493343 
schirmer@12854
    17
   http://developer.java.sun.com/developer/bugParade/index.jshtml
schirmer@12854
    18
  )
schirmer@12854
    19
\end{itemize}
schirmer@12854
    20
simplifications:
schirmer@12854
    21
\begin{itemize}
schirmer@12854
    22
\item the only field and method modifiers are static and the access modifiers
schirmer@12854
    23
\item no constructors, which may be simulated by new + suitable methods
schirmer@12854
    24
\item there is just one global initializer per class, which can simulate all 
schirmer@12854
    25
      others
schirmer@12854
    26
schirmer@12854
    27
\item no throws clause
schirmer@12854
    28
\item a void method is replaced by one that returns Unit (of dummy type Void)
schirmer@12854
    29
schirmer@12854
    30
\item no interface fields
schirmer@12854
    31
schirmer@12854
    32
\item every class has an explicit superclass (unused for Object)
schirmer@12854
    33
\item the (standard) methods of Object and of standard exceptions are not 
schirmer@12854
    34
      specified
schirmer@12854
    35
schirmer@12854
    36
\item no main method
schirmer@12854
    37
\end{itemize}
schirmer@12854
    38
*}
schirmer@12854
    39
schirmer@12854
    40
subsection {* Modifier*}
schirmer@12854
    41
schirmer@12854
    42
subsubsection {* Access modifier *}
schirmer@12854
    43
schirmer@12854
    44
datatype acc_modi (* access modifier *)
schirmer@12854
    45
         = Private | Package | Protected | Public 
schirmer@12854
    46
schirmer@12854
    47
text {* 
schirmer@12854
    48
We can define a linear order for the access modifiers. With Private yielding the
schirmer@12854
    49
most restrictive access and public the most liberal access policy:
schirmer@12854
    50
  Private < Package < Protected < Public
schirmer@12854
    51
*}
schirmer@12854
    52
 
haftmann@26566
    53
instantiation acc_modi :: linorder
haftmann@26566
    54
begin
schirmer@12854
    55
haftmann@26566
    56
definition
haftmann@26566
    57
  less_acc_def: "a < b
haftmann@26566
    58
      \<longleftrightarrow> (case a of
schirmer@12854
    59
             Private    \<Rightarrow> (b=Package \<or> b=Protected \<or> b=Public)
schirmer@12854
    60
           | Package    \<Rightarrow> (b=Protected \<or> b=Public)
schirmer@12854
    61
           | Protected  \<Rightarrow> (b=Public)
schirmer@12854
    62
           | Public     \<Rightarrow> False)"
schirmer@12854
    63
haftmann@26566
    64
definition
haftmann@27682
    65
  le_acc_def: "(a :: acc_modi) \<le> b \<longleftrightarrow> a < b \<or> a = b"
haftmann@26566
    66
haftmann@26566
    67
instance proof
schirmer@12854
    68
  fix x y z::acc_modi
haftmann@27682
    69
  show "(x < y) = (x \<le> y \<and> \<not> y \<le> x)"
haftmann@27682
    70
    by (auto simp add: le_acc_def less_acc_def split add: acc_modi.split) 
schirmer@12854
    71
  {
schirmer@12854
    72
  show "x \<le> x"               \<spacespace>\<spacespace>    -- reflexivity
schirmer@12854
    73
    by (auto simp add: le_acc_def)
schirmer@12854
    74
  next
schirmer@12854
    75
  assume "x \<le> y" "y \<le> z"           -- transitivity 
schirmer@12854
    76
  thus "x \<le> z"
schirmer@12854
    77
    by (auto simp add: le_acc_def less_acc_def split add: acc_modi.split)
schirmer@12854
    78
  next
schirmer@12854
    79
  assume "x \<le> y" "y \<le> x"           -- antisymmetry
schirmer@12854
    80
  thus "x = y"
schirmer@12854
    81
  proof -
schirmer@12854
    82
    have "\<forall> x y. x < (y::acc_modi) \<and> y < x \<longrightarrow> False"
schirmer@12854
    83
      by (auto simp add: less_acc_def split add: acc_modi.split)
schirmer@12854
    84
    with prems show ?thesis
nipkow@17589
    85
      by (unfold le_acc_def) iprover
schirmer@12854
    86
  qed
schirmer@12854
    87
  next
schirmer@12854
    88
  fix x y:: acc_modi
schirmer@12854
    89
  show  "x \<le> y \<or> y \<le> x"   
schirmer@12854
    90
  by (auto simp add: less_acc_def le_acc_def split add: acc_modi.split)
haftmann@27682
    91
  }
schirmer@12854
    92
qed
haftmann@26566
    93
  
haftmann@26566
    94
end
schirmer@12854
    95
schirmer@12854
    96
lemma acc_modi_top [simp]: "Public \<le> a \<Longrightarrow> a = Public"
schirmer@12854
    97
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
    98
schirmer@12854
    99
lemma acc_modi_top1 [simp, intro!]: "a \<le> Public"
schirmer@12854
   100
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   101
schirmer@12854
   102
lemma acc_modi_le_Public: 
schirmer@12854
   103
"a \<le> Public \<Longrightarrow> a=Private \<or> a = Package \<or> a=Protected \<or> a=Public"
schirmer@12854
   104
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   105
schirmer@12854
   106
lemma acc_modi_bottom: "a \<le> Private \<Longrightarrow> a = Private"
schirmer@12854
   107
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   108
schirmer@12854
   109
lemma acc_modi_Private_le: 
schirmer@12854
   110
"Private \<le> a \<Longrightarrow> a=Private \<or> a = Package \<or> a=Protected \<or> a=Public"
schirmer@12854
   111
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   112
schirmer@12854
   113
lemma acc_modi_Package_le: 
schirmer@12854
   114
  "Package \<le> a \<Longrightarrow> a = Package \<or> a=Protected \<or> a=Public"
schirmer@12854
   115
by (auto simp add: le_acc_def less_acc_def split: acc_modi.split)
schirmer@12854
   116
schirmer@12854
   117
lemma acc_modi_le_Package: 
schirmer@12854
   118
  "a \<le> Package \<Longrightarrow> a=Private \<or> a = Package"
schirmer@12854
   119
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   120
schirmer@12854
   121
lemma acc_modi_Protected_le: 
schirmer@12854
   122
  "Protected \<le> a \<Longrightarrow> a=Protected \<or> a=Public"
schirmer@12854
   123
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   124
schirmer@12854
   125
lemma acc_modi_le_Protected: 
schirmer@12854
   126
  "a \<le> Protected  \<Longrightarrow> a=Private \<or> a = Package \<or> a = Protected"
schirmer@12854
   127
by (auto simp add: le_acc_def less_acc_def split: acc_modi.splits)
schirmer@12854
   128
schirmer@12854
   129
schirmer@12854
   130
lemmas acc_modi_le_Dests = acc_modi_top           acc_modi_le_Public
schirmer@12854
   131
                           acc_modi_Private_le    acc_modi_bottom
schirmer@12854
   132
                           acc_modi_Package_le    acc_modi_le_Package
schirmer@12854
   133
                           acc_modi_Protected_le  acc_modi_le_Protected
schirmer@12854
   134
schirmer@12854
   135
lemma acc_modi_Package_le_cases 
schirmer@12854
   136
 [consumes 1,case_names Package Protected Public]:
schirmer@12854
   137
 "Package \<le> m \<Longrightarrow> ( m = Package \<Longrightarrow> P m) \<Longrightarrow> (m=Protected \<Longrightarrow> P m) \<Longrightarrow> 
schirmer@12854
   138
   (m=Public \<Longrightarrow> P m) \<Longrightarrow> P m"
schirmer@12854
   139
by (auto dest: acc_modi_Package_le)
schirmer@12854
   140
schirmer@12854
   141
schirmer@12854
   142
subsubsection {* Static Modifier *}
schirmer@12854
   143
types stat_modi = bool (* modifier: static *)
schirmer@12854
   144
schirmer@12854
   145
subsection {* Declaration (base "class" for member,interface and class
schirmer@12854
   146
 declarations *}
schirmer@12854
   147
schirmer@12854
   148
record decl =
schirmer@12854
   149
        access :: acc_modi
schirmer@12854
   150
schirmer@12854
   151
translations
wenzelm@35431
   152
  (type) "decl" <= (type) "\<lparr>access::acc_modi\<rparr>"
wenzelm@35431
   153
  (type) "decl" <= (type) "\<lparr>access::acc_modi,\<dots>::'a\<rparr>"
schirmer@12854
   154
schirmer@12854
   155
subsection {* Member (field or method)*}
schirmer@12854
   156
record  member = decl +
schirmer@12854
   157
         static :: stat_modi
schirmer@12854
   158
schirmer@12854
   159
translations
wenzelm@35431
   160
  (type) "member" <= (type) "\<lparr>access::acc_modi,static::bool\<rparr>"
wenzelm@35431
   161
  (type) "member" <= (type) "\<lparr>access::acc_modi,static::bool,\<dots>::'a\<rparr>"
schirmer@12854
   162
schirmer@12854
   163
subsection {* Field *}
schirmer@12854
   164
schirmer@12854
   165
record field = member +
schirmer@12854
   166
        type :: ty
schirmer@12854
   167
translations
wenzelm@35431
   168
  (type) "field" <= (type) "\<lparr>access::acc_modi, static::bool, type::ty\<rparr>"
wenzelm@35431
   169
  (type) "field" <= (type) "\<lparr>access::acc_modi, static::bool, type::ty,\<dots>::'a\<rparr>"
schirmer@12854
   170
schirmer@12854
   171
types     
schirmer@12854
   172
        fdecl           (* field declaration, cf. 8.3 *)
wenzelm@32960
   173
        = "vname \<times> field"
schirmer@12854
   174
schirmer@12854
   175
schirmer@12854
   176
translations
wenzelm@35431
   177
  (type) "fdecl" <= (type) "vname \<times> field"
schirmer@12854
   178
schirmer@12854
   179
subsection  {* Method *}
schirmer@12854
   180
schirmer@12854
   181
record mhead = member +     (* method head (excluding signature) *)
schirmer@12854
   182
        pars ::"vname list" (* parameter names *)
schirmer@12854
   183
        resT ::ty           (* result type *)
schirmer@12854
   184
schirmer@12854
   185
record mbody =                      (* method body *)
schirmer@12854
   186
        lcls::  "(vname \<times> ty) list" (* local variables *)
schirmer@12854
   187
        stmt:: stmt                 (* the body statement *)
schirmer@12854
   188
schirmer@12854
   189
record methd = mhead + (* method in a class *)
schirmer@12854
   190
        mbody::mbody
schirmer@12854
   191
schirmer@12854
   192
types mdecl = "sig \<times> methd"  (* method declaration in a class *)
schirmer@12854
   193
schirmer@12854
   194
schirmer@12854
   195
translations
wenzelm@35431
   196
  (type) "mhead" <= (type) "\<lparr>access::acc_modi, static::bool, 
schirmer@12854
   197
                      pars::vname list, resT::ty\<rparr>"
wenzelm@35431
   198
  (type) "mhead" <= (type) "\<lparr>access::acc_modi, static::bool, 
schirmer@12854
   199
                      pars::vname list, resT::ty,\<dots>::'a\<rparr>"
wenzelm@35431
   200
  (type) "mbody" <= (type) "\<lparr>lcls::(vname \<times> ty) list,stmt::stmt\<rparr>"
wenzelm@35431
   201
  (type) "mbody" <= (type) "\<lparr>lcls::(vname \<times> ty) list,stmt::stmt,\<dots>::'a\<rparr>"      
wenzelm@35431
   202
  (type) "methd" <= (type) "\<lparr>access::acc_modi, static::bool, 
schirmer@12854
   203
                      pars::vname list, resT::ty,mbody::mbody\<rparr>"
wenzelm@35431
   204
  (type) "methd" <= (type) "\<lparr>access::acc_modi, static::bool, 
schirmer@12854
   205
                      pars::vname list, resT::ty,mbody::mbody,\<dots>::'a\<rparr>"
wenzelm@35431
   206
  (type) "mdecl" <= (type) "sig \<times> methd"
schirmer@12854
   207
schirmer@12854
   208
haftmann@35416
   209
definition mhead :: "methd \<Rightarrow> mhead" where
schirmer@12854
   210
  "mhead m \<equiv> \<lparr>access=access m, static=static m, pars=pars m, resT=resT m\<rparr>"
schirmer@12854
   211
schirmer@12854
   212
lemma access_mhead [simp]:"access (mhead m) = access m"
schirmer@12854
   213
by (simp add: mhead_def)
schirmer@12854
   214
schirmer@12854
   215
lemma static_mhead [simp]:"static (mhead m) = static m"
schirmer@12854
   216
by (simp add: mhead_def)
schirmer@12854
   217
schirmer@12854
   218
lemma pars_mhead [simp]:"pars (mhead m) = pars m"
schirmer@12854
   219
by (simp add: mhead_def)
schirmer@12854
   220
schirmer@12854
   221
lemma resT_mhead [simp]:"resT (mhead m) = resT m"
schirmer@12854
   222
by (simp add: mhead_def)
schirmer@12854
   223
schirmer@12854
   224
text {* To be able to talk uniformaly about field and method declarations we
schirmer@12854
   225
introduce the notion of a member declaration (e.g. useful to define 
schirmer@12854
   226
accessiblity ) *}
schirmer@12854
   227
schirmer@12854
   228
datatype memberdecl = fdecl fdecl | mdecl mdecl
schirmer@12854
   229
schirmer@12854
   230
datatype memberid = fid vname | mid sig
schirmer@12854
   231
haftmann@35315
   232
class has_memberid =
haftmann@35315
   233
  fixes memberid :: "'a \<Rightarrow> memberid"
schirmer@12854
   234
haftmann@35315
   235
instantiation memberdecl :: has_memberid
haftmann@35315
   236
begin
schirmer@12854
   237
haftmann@35315
   238
definition
schirmer@12854
   239
memberdecl_memberid_def:
schirmer@12854
   240
  "memberid m \<equiv> (case m of
schirmer@12854
   241
                    fdecl (vn,f)  \<Rightarrow> fid vn
schirmer@12854
   242
                  | mdecl (sig,m) \<Rightarrow> mid sig)"
schirmer@12854
   243
haftmann@35315
   244
instance ..
haftmann@35315
   245
haftmann@35315
   246
end
haftmann@35315
   247
schirmer@12854
   248
lemma memberid_fdecl_simp[simp]: "memberid (fdecl (vn,f)) = fid vn"
schirmer@12854
   249
by (simp add: memberdecl_memberid_def)
schirmer@12854
   250
schirmer@12854
   251
lemma memberid_fdecl_simp1: "memberid (fdecl f) = fid (fst f)"
schirmer@12854
   252
by (cases f) (simp add: memberdecl_memberid_def)
schirmer@12854
   253
schirmer@12854
   254
lemma memberid_mdecl_simp[simp]: "memberid (mdecl (sig,m)) = mid sig"
schirmer@12854
   255
by (simp add: memberdecl_memberid_def)
schirmer@12854
   256
schirmer@12854
   257
lemma memberid_mdecl_simp1: "memberid (mdecl m) = mid (fst m)"
schirmer@12854
   258
by (cases m) (simp add: memberdecl_memberid_def)
schirmer@12854
   259
haftmann@35315
   260
instantiation * :: (type, has_memberid) has_memberid
haftmann@35315
   261
begin
schirmer@12854
   262
haftmann@35315
   263
definition
schirmer@12854
   264
pair_memberid_def:
schirmer@12854
   265
  "memberid p \<equiv> memberid (snd p)"
schirmer@12854
   266
haftmann@35315
   267
instance ..
haftmann@35315
   268
haftmann@35315
   269
end
haftmann@35315
   270
schirmer@12854
   271
lemma memberid_pair_simp[simp]: "memberid (c,m) = memberid m"
schirmer@12854
   272
by (simp add: pair_memberid_def)
schirmer@12854
   273
schirmer@12854
   274
lemma memberid_pair_simp1: "memberid p  = memberid (snd p)"
schirmer@12854
   275
by (simp add: pair_memberid_def)
schirmer@12854
   276
haftmann@35416
   277
definition is_field :: "qtname \<times> memberdecl \<Rightarrow> bool" where
schirmer@12854
   278
"is_field m \<equiv> \<exists> declC f. m=(declC,fdecl f)"
schirmer@12854
   279
  
schirmer@12854
   280
lemma is_fieldD: "is_field m \<Longrightarrow> \<exists> declC f. m=(declC,fdecl f)"
schirmer@12854
   281
by (simp add: is_field_def)
schirmer@12854
   282
schirmer@12854
   283
lemma is_fieldI: "is_field (C,fdecl f)"
schirmer@12854
   284
by (simp add: is_field_def)
schirmer@12854
   285
haftmann@35416
   286
definition is_method :: "qtname \<times> memberdecl \<Rightarrow> bool" where
schirmer@12854
   287
"is_method membr \<equiv> \<exists> declC m. membr=(declC,mdecl m)"
schirmer@12854
   288
  
schirmer@12854
   289
lemma is_methodD: "is_method membr \<Longrightarrow> \<exists> declC m. membr=(declC,mdecl m)"
schirmer@12854
   290
by (simp add: is_method_def)
schirmer@12854
   291
schirmer@12854
   292
lemma is_methodI: "is_method (C,mdecl m)"
schirmer@12854
   293
by (simp add: is_method_def)
schirmer@12854
   294
schirmer@12854
   295
schirmer@12854
   296
subsection {* Interface *}
schirmer@12854
   297
schirmer@12854
   298
schirmer@13688
   299
record  ibody = decl +  --{* interface body *}
schirmer@13688
   300
          imethods :: "(sig \<times> mhead) list" --{* method heads *}
schirmer@12854
   301
schirmer@13688
   302
record  iface = ibody + --{* interface *}
schirmer@13688
   303
         isuperIfs:: "qtname list" --{* superinterface list *}
wenzelm@32960
   304
types
wenzelm@32960
   305
        idecl           --{* interface declaration, cf. 9.1 *}
wenzelm@32960
   306
        = "qtname \<times> iface"
schirmer@12854
   307
schirmer@12854
   308
translations
wenzelm@35431
   309
  (type) "ibody" <= (type) "\<lparr>access::acc_modi,imethods::(sig \<times> mhead) list\<rparr>"
wenzelm@35431
   310
  (type) "ibody" <= (type) "\<lparr>access::acc_modi,imethods::(sig \<times> mhead) list,\<dots>::'a\<rparr>"
wenzelm@35431
   311
  (type) "iface" <= (type) "\<lparr>access::acc_modi,imethods::(sig \<times> mhead) list,
schirmer@12854
   312
                      isuperIfs::qtname list\<rparr>"
wenzelm@35431
   313
  (type) "iface" <= (type) "\<lparr>access::acc_modi,imethods::(sig \<times> mhead) list,
schirmer@12854
   314
                      isuperIfs::qtname list,\<dots>::'a\<rparr>"
wenzelm@35431
   315
  (type) "idecl" <= (type) "qtname \<times> iface"
schirmer@12854
   316
haftmann@35416
   317
definition ibody :: "iface \<Rightarrow> ibody" where
schirmer@12854
   318
  "ibody i \<equiv> \<lparr>access=access i,imethods=imethods i\<rparr>"
schirmer@12854
   319
schirmer@12854
   320
lemma access_ibody [simp]: "(access (ibody i)) = access i"
schirmer@12854
   321
by (simp add: ibody_def)
schirmer@12854
   322
schirmer@12854
   323
lemma imethods_ibody [simp]: "(imethods (ibody i)) = imethods i"
schirmer@12854
   324
by (simp add: ibody_def)
schirmer@12854
   325
schirmer@12854
   326
subsection  {* Class *}
schirmer@13688
   327
record cbody = decl +          --{* class body *}
schirmer@12854
   328
         cfields:: "fdecl list" 
schirmer@12854
   329
         methods:: "mdecl list"
schirmer@13688
   330
         init   :: "stmt"       --{* initializer *}
schirmer@12854
   331
haftmann@18551
   332
record "class" = cbody +           --{* class *}
schirmer@13688
   333
        super   :: "qtname"      --{* superclass *}
schirmer@13688
   334
        superIfs:: "qtname list" --{* implemented interfaces *}
wenzelm@32960
   335
types
wenzelm@32960
   336
        cdecl           --{* class declaration, cf. 8.1 *}
wenzelm@32960
   337
        = "qtname \<times> class"
schirmer@12854
   338
schirmer@12854
   339
translations
wenzelm@35431
   340
  (type) "cbody" <= (type) "\<lparr>access::acc_modi,cfields::fdecl list,
schirmer@12854
   341
                      methods::mdecl list,init::stmt\<rparr>"
wenzelm@35431
   342
  (type) "cbody" <= (type) "\<lparr>access::acc_modi,cfields::fdecl list,
schirmer@12854
   343
                      methods::mdecl list,init::stmt,\<dots>::'a\<rparr>"
wenzelm@35431
   344
  (type) "class" <= (type) "\<lparr>access::acc_modi,cfields::fdecl list,
schirmer@12854
   345
                      methods::mdecl list,init::stmt,
schirmer@12854
   346
                      super::qtname,superIfs::qtname list\<rparr>"
wenzelm@35431
   347
  (type) "class" <= (type) "\<lparr>access::acc_modi,cfields::fdecl list,
schirmer@12854
   348
                      methods::mdecl list,init::stmt,
schirmer@12854
   349
                      super::qtname,superIfs::qtname list,\<dots>::'a\<rparr>"
wenzelm@35431
   350
  (type) "cdecl" <= (type) "qtname \<times> class"
schirmer@12854
   351
haftmann@35416
   352
definition cbody :: "class \<Rightarrow> cbody" where
schirmer@12854
   353
  "cbody c \<equiv> \<lparr>access=access c, cfields=cfields c,methods=methods c,init=init c\<rparr>"
schirmer@12854
   354
schirmer@12854
   355
lemma access_cbody [simp]:"access (cbody c) = access c"
schirmer@12854
   356
by (simp add: cbody_def)
schirmer@12854
   357
schirmer@12854
   358
lemma cfields_cbody [simp]:"cfields (cbody c) = cfields c"
schirmer@12854
   359
by (simp add: cbody_def)
schirmer@12854
   360
schirmer@12854
   361
lemma methods_cbody [simp]:"methods (cbody c) = methods c"
schirmer@12854
   362
by (simp add: cbody_def)
schirmer@12854
   363
schirmer@12854
   364
lemma init_cbody [simp]:"init (cbody c) = init c"
schirmer@12854
   365
by (simp add: cbody_def)
schirmer@12854
   366
schirmer@12854
   367
schirmer@12854
   368
section "standard classes"
schirmer@12854
   369
schirmer@12854
   370
consts
schirmer@12854
   371
wenzelm@14674
   372
  Object_mdecls  ::  "mdecl list" --{* methods of Object *}
wenzelm@14674
   373
  SXcpt_mdecls   ::  "mdecl list" --{* methods of SXcpts *}
wenzelm@14674
   374
  ObjectC ::         "cdecl"      --{* declaration  of root      class   *}
schirmer@13688
   375
  SXcptC  ::"xname \<Rightarrow> cdecl"      --{* declarations of throwable classes *}
schirmer@12854
   376
schirmer@12854
   377
defs 
schirmer@12854
   378
schirmer@12854
   379
schirmer@12854
   380
ObjectC_def:"ObjectC  \<equiv> (Object,\<lparr>access=Public,cfields=[],methods=Object_mdecls,
haftmann@28524
   381
                                  init=Skip,super=undefined,superIfs=[]\<rparr>)"
schirmer@12854
   382
SXcptC_def:"SXcptC xn\<equiv> (SXcpt xn,\<lparr>access=Public,cfields=[],methods=SXcpt_mdecls,
schirmer@12854
   383
                                   init=Skip,
schirmer@12854
   384
                                   super=if xn = Throwable then Object 
schirmer@12854
   385
                                                           else SXcpt Throwable,
schirmer@12854
   386
                                   superIfs=[]\<rparr>)"
schirmer@12854
   387
schirmer@12854
   388
lemma ObjectC_neq_SXcptC [simp]: "ObjectC \<noteq> SXcptC xn"
schirmer@12854
   389
by (simp add: ObjectC_def SXcptC_def Object_def SXcpt_def)
schirmer@12854
   390
schirmer@12854
   391
lemma SXcptC_inject [simp]: "(SXcptC xn = SXcptC xm) = (xn = xm)"
nipkow@17778
   392
by (simp add: SXcptC_def)
schirmer@12854
   393
haftmann@35416
   394
definition standard_classes :: "cdecl list" where
schirmer@12854
   395
         "standard_classes \<equiv> [ObjectC, SXcptC Throwable,
wenzelm@32960
   396
                SXcptC NullPointer, SXcptC OutOfMemory, SXcptC ClassCast,
wenzelm@32960
   397
                SXcptC NegArrSize , SXcptC IndOutBound, SXcptC ArrStore]"
schirmer@12854
   398
schirmer@12854
   399
schirmer@12854
   400
section "programs"
schirmer@12854
   401
schirmer@12854
   402
record prog =
schirmer@12854
   403
        ifaces ::"idecl list"
schirmer@12854
   404
        "classes"::"cdecl list"
schirmer@12854
   405
schirmer@12854
   406
translations
wenzelm@35431
   407
     (type) "prog" <= (type) "\<lparr>ifaces::idecl list,classes::cdecl list\<rparr>"
wenzelm@35431
   408
     (type) "prog" <= (type) "\<lparr>ifaces::idecl list,classes::cdecl list,\<dots>::'a\<rparr>"
schirmer@12854
   409
wenzelm@35067
   410
abbreviation
wenzelm@35067
   411
  iface :: "prog  \<Rightarrow> (qtname, iface) table"
wenzelm@35067
   412
  where "iface G I == table_of (ifaces G) I"
wenzelm@35067
   413
wenzelm@35067
   414
abbreviation
wenzelm@35067
   415
  "class" :: "prog  \<Rightarrow> (qtname, class) table"
wenzelm@35067
   416
  where "class G C == table_of (classes G) C"
schirmer@12854
   417
wenzelm@35067
   418
abbreviation
wenzelm@35067
   419
  is_iface :: "prog  \<Rightarrow> qtname  \<Rightarrow> bool"
wenzelm@35067
   420
  where "is_iface G I == iface G I \<noteq> None"
wenzelm@35067
   421
wenzelm@35067
   422
abbreviation
wenzelm@35067
   423
  is_class :: "prog  \<Rightarrow> qtname  \<Rightarrow> bool"
wenzelm@35067
   424
  where "is_class G C == class G C \<noteq> None"
schirmer@12854
   425
schirmer@12854
   426
schirmer@12854
   427
section "is type"
schirmer@12854
   428
schirmer@12854
   429
consts
schirmer@12854
   430
  is_type :: "prog \<Rightarrow>     ty \<Rightarrow> bool"
schirmer@12854
   431
  isrtype :: "prog \<Rightarrow> ref_ty \<Rightarrow> bool"
schirmer@12854
   432
wenzelm@32960
   433
primrec "is_type G (PrimT pt)  = True"
wenzelm@32960
   434
        "is_type G (RefT  rt)  = isrtype G rt"
wenzelm@32960
   435
        "isrtype G (NullT    ) = True"
wenzelm@32960
   436
        "isrtype G (IfaceT tn) = is_iface G tn"
wenzelm@32960
   437
        "isrtype G (ClassT tn) = is_class G tn"
wenzelm@32960
   438
        "isrtype G (ArrayT T ) = is_type  G T"
schirmer@12854
   439
schirmer@12854
   440
lemma type_is_iface: "is_type G (Iface I) \<Longrightarrow> is_iface G I"
schirmer@12854
   441
by auto
schirmer@12854
   442
schirmer@12854
   443
lemma type_is_class: "is_type G (Class C) \<Longrightarrow>  is_class G C"
schirmer@12854
   444
by auto
schirmer@12854
   445
schirmer@12854
   446
schirmer@12854
   447
section "subinterface and subclass relation, in anticipation of TypeRel.thy"
schirmer@12854
   448
schirmer@12854
   449
consts 
schirmer@13688
   450
  subint1  :: "prog \<Rightarrow> (qtname \<times> qtname) set" --{* direct subinterface *}
schirmer@13688
   451
  subcls1  :: "prog \<Rightarrow> (qtname \<times> qtname) set" --{* direct subclass     *}
schirmer@12854
   452
schirmer@12854
   453
defs
schirmer@12854
   454
  subint1_def: "subint1 G \<equiv> {(I,J). \<exists>i\<in>iface G I: J\<in>set (isuperIfs i)}"
schirmer@12854
   455
  subcls1_def: "subcls1 G \<equiv> {(C,D). C\<noteq>Object \<and> (\<exists>c\<in>class G C: super c = D)}"
schirmer@12854
   456
wenzelm@35067
   457
abbreviation
wenzelm@35067
   458
  subcls1_syntax :: "prog => [qtname, qtname] => bool" ("_|-_<:C1_" [71,71,71] 70)
wenzelm@35067
   459
  where "G|-C <:C1 D == (C,D) \<in> subcls1 G"
wenzelm@35067
   460
wenzelm@35067
   461
abbreviation
wenzelm@35067
   462
  subclseq_syntax :: "prog => [qtname, qtname] => bool" ("_|-_<=:C _"[71,71,71] 70)
wenzelm@35067
   463
  where "G|-C <=:C D == (C,D) \<in>(subcls1 G)^*" (* cf. 8.1.3 *)
schirmer@12854
   464
wenzelm@35067
   465
abbreviation
wenzelm@35067
   466
  subcls_syntax :: "prog => [qtname, qtname] => bool" ("_|-_<:C _"[71,71,71] 70)
wenzelm@35067
   467
  where "G|-C <:C D == (C,D) \<in>(subcls1 G)^+"
schirmer@12854
   468
wenzelm@35067
   469
notation (xsymbols)
haftmann@35416
   470
  subcls1_syntax  ("_\<turnstile>_\<prec>\<^sub>C1_"  [71,71,71] 70) and
wenzelm@35067
   471
  subclseq_syntax  ("_\<turnstile>_\<preceq>\<^sub>C _"  [71,71,71] 70) and
wenzelm@35067
   472
  subcls_syntax  ("_\<turnstile>_\<prec>\<^sub>C _"  [71,71,71] 70)
schirmer@12854
   473
schirmer@12854
   474
lemma subint1I: "\<lbrakk>iface G I = Some i; J \<in> set (isuperIfs i)\<rbrakk> 
schirmer@12854
   475
                 \<Longrightarrow> (I,J) \<in> subint1 G" 
schirmer@12854
   476
apply (simp add: subint1_def)
schirmer@12854
   477
done
schirmer@12854
   478
schirmer@12854
   479
lemma subcls1I:"\<lbrakk>class G C = Some c; C \<noteq> Object\<rbrakk> \<Longrightarrow> (C,(super c)) \<in> subcls1 G"
schirmer@12854
   480
apply (simp add: subcls1_def)
schirmer@12854
   481
done
schirmer@12854
   482
schirmer@12854
   483
schirmer@12854
   484
lemma subint1D: "(I,J)\<in>subint1 G\<Longrightarrow> \<exists>i\<in>iface G I: J\<in>set (isuperIfs i)"
schirmer@12854
   485
by (simp add: subint1_def)
schirmer@12854
   486
schirmer@12854
   487
lemma subcls1D: 
schirmer@12854
   488
  "(C,D)\<in>subcls1 G \<Longrightarrow> C\<noteq>Object \<and> (\<exists>c. class G C = Some c \<and> (super c = D))"
schirmer@12854
   489
apply (simp add: subcls1_def)
schirmer@12854
   490
apply auto
schirmer@12854
   491
done
schirmer@12854
   492
schirmer@12854
   493
lemma subint1_def2:  
paulson@14952
   494
  "subint1 G = (SIGMA I: {I. is_iface G I}. set (isuperIfs (the (iface G I))))"
schirmer@12854
   495
apply (unfold subint1_def)
schirmer@12854
   496
apply auto
schirmer@12854
   497
done
schirmer@12854
   498
schirmer@12854
   499
lemma subcls1_def2: 
paulson@14952
   500
  "subcls1 G = 
paulson@14952
   501
     (SIGMA C: {C. is_class G C}. {D. C\<noteq>Object \<and> super (the(class G C))=D})"
schirmer@12854
   502
apply (unfold subcls1_def)
schirmer@12854
   503
apply auto
schirmer@12854
   504
done
schirmer@12854
   505
schirmer@12854
   506
lemma subcls_is_class:
schirmer@12854
   507
"\<lbrakk>G\<turnstile>C \<prec>\<^sub>C D\<rbrakk> \<Longrightarrow> \<exists> c. class G C = Some c"
schirmer@12854
   508
by (auto simp add: subcls1_def dest: tranclD)
schirmer@12854
   509
haftmann@35416
   510
lemma no_subcls1_Object:"G\<turnstile>Object\<prec>\<^sub>C1 D \<Longrightarrow> P"
schirmer@12854
   511
by (auto simp add: subcls1_def)
schirmer@12854
   512
schirmer@12854
   513
lemma no_subcls_Object: "G\<turnstile>Object\<prec>\<^sub>C D \<Longrightarrow> P"
schirmer@12854
   514
apply (erule trancl_induct)
schirmer@12854
   515
apply (auto intro: no_subcls1_Object)
schirmer@12854
   516
done
schirmer@12854
   517
schirmer@12854
   518
section "well-structured programs"
schirmer@12854
   519
haftmann@35416
   520
definition ws_idecl :: "prog \<Rightarrow> qtname \<Rightarrow> qtname list \<Rightarrow> bool" where
schirmer@12854
   521
 "ws_idecl G I si \<equiv> \<forall>J\<in>set si.  is_iface G J   \<and> (J,I)\<notin>(subint1 G)^+"
schirmer@12854
   522
  
haftmann@35416
   523
definition ws_cdecl :: "prog \<Rightarrow> qtname \<Rightarrow> qtname \<Rightarrow> bool" where
schirmer@12854
   524
 "ws_cdecl G C sc \<equiv> C\<noteq>Object \<longrightarrow> is_class G sc \<and> (sc,C)\<notin>(subcls1 G)^+"
schirmer@12854
   525
  
haftmann@35416
   526
definition ws_prog  :: "prog \<Rightarrow> bool" where
schirmer@12854
   527
 "ws_prog G \<equiv> (\<forall>(I,i)\<in>set (ifaces  G). ws_idecl G I (isuperIfs i)) \<and> 
wenzelm@32960
   528
              (\<forall>(C,c)\<in>set (classes G). ws_cdecl G C (super c))"
schirmer@12854
   529
schirmer@12854
   530
schirmer@12854
   531
lemma ws_progI: 
schirmer@12854
   532
"\<lbrakk>\<forall>(I,i)\<in>set (ifaces G). \<forall>J\<in>set (isuperIfs i). is_iface G J \<and> 
schirmer@12854
   533
                                                (J,I) \<notin> (subint1 G)^+; 
schirmer@12854
   534
  \<forall>(C,c)\<in>set (classes G). C\<noteq>Object \<longrightarrow> is_class G (super c) \<and> 
schirmer@12854
   535
                                        ((super c),C) \<notin> (subcls1 G)^+  
schirmer@12854
   536
 \<rbrakk> \<Longrightarrow> ws_prog G"
schirmer@12854
   537
apply (unfold ws_prog_def ws_idecl_def ws_cdecl_def)
schirmer@12854
   538
apply (erule_tac conjI)
schirmer@12854
   539
apply blast
schirmer@12854
   540
done
schirmer@12854
   541
schirmer@12854
   542
lemma ws_prog_ideclD: 
schirmer@12854
   543
"\<lbrakk>iface G I = Some i; J\<in>set (isuperIfs i); ws_prog G\<rbrakk> \<Longrightarrow>  
schirmer@12854
   544
  is_iface G J \<and> (J,I)\<notin>(subint1 G)^+"
schirmer@12854
   545
apply (unfold ws_prog_def ws_idecl_def)
schirmer@12854
   546
apply clarify
schirmer@12854
   547
apply (drule_tac map_of_SomeD)
schirmer@12854
   548
apply auto
schirmer@12854
   549
done
schirmer@12854
   550
schirmer@12854
   551
lemma ws_prog_cdeclD: 
schirmer@12854
   552
"\<lbrakk>class G C = Some c; C\<noteq>Object; ws_prog G\<rbrakk> \<Longrightarrow>  
schirmer@12854
   553
  is_class G (super c) \<and> (super c,C)\<notin>(subcls1 G)^+"
schirmer@12854
   554
apply (unfold ws_prog_def ws_cdecl_def)
schirmer@12854
   555
apply clarify
schirmer@12854
   556
apply (drule_tac map_of_SomeD)
schirmer@12854
   557
apply auto
schirmer@12854
   558
done
schirmer@12854
   559
schirmer@12854
   560
schirmer@12854
   561
section "well-foundedness"
schirmer@12854
   562
schirmer@12854
   563
lemma finite_is_iface: "finite {I. is_iface G I}"
schirmer@12854
   564
apply (fold dom_def)
schirmer@12854
   565
apply (rule_tac finite_dom_map_of)
schirmer@12854
   566
done
schirmer@12854
   567
schirmer@12854
   568
lemma finite_is_class: "finite {C. is_class G C}"
schirmer@12854
   569
apply (fold dom_def)
schirmer@12854
   570
apply (rule_tac finite_dom_map_of)
schirmer@12854
   571
done
schirmer@12854
   572
schirmer@12854
   573
lemma finite_subint1: "finite (subint1 G)"
schirmer@12854
   574
apply (subst subint1_def2)
schirmer@12854
   575
apply (rule finite_SigmaI)
schirmer@12854
   576
apply (rule finite_is_iface)
schirmer@12854
   577
apply (simp (no_asm))
schirmer@12854
   578
done
schirmer@12854
   579
schirmer@12854
   580
lemma finite_subcls1: "finite (subcls1 G)"
schirmer@12854
   581
apply (subst subcls1_def2)
schirmer@12854
   582
apply (rule finite_SigmaI)
schirmer@12854
   583
apply (rule finite_is_class)
schirmer@12854
   584
apply (rule_tac B = "{super (the (class G C))}" in finite_subset)
schirmer@12854
   585
apply  auto
schirmer@12854
   586
done
schirmer@12854
   587
schirmer@12854
   588
lemma subint1_irrefl_lemma1: 
schirmer@12854
   589
  "ws_prog G \<Longrightarrow> (subint1 G)^-1 \<inter> (subint1 G)^+ = {}"
schirmer@12854
   590
apply (force dest: subint1D ws_prog_ideclD conjunct2)
schirmer@12854
   591
done
schirmer@12854
   592
schirmer@12854
   593
lemma subcls1_irrefl_lemma1: 
schirmer@12854
   594
  "ws_prog G \<Longrightarrow> (subcls1 G)^-1 \<inter> (subcls1 G)^+ = {}"
schirmer@12854
   595
apply (force dest: subcls1D ws_prog_cdeclD conjunct2)
schirmer@12854
   596
done
schirmer@12854
   597
schirmer@12854
   598
lemmas subint1_irrefl_lemma2 = subint1_irrefl_lemma1 [THEN irrefl_tranclI']
schirmer@12854
   599
lemmas subcls1_irrefl_lemma2 = subcls1_irrefl_lemma1 [THEN irrefl_tranclI']
schirmer@12854
   600
schirmer@12854
   601
lemma subint1_irrefl: "\<lbrakk>(x, y) \<in> subint1 G; ws_prog G\<rbrakk> \<Longrightarrow> x \<noteq> y"
schirmer@12854
   602
apply (rule irrefl_trancl_rD)
schirmer@12854
   603
apply (rule subint1_irrefl_lemma2)
schirmer@12854
   604
apply auto
schirmer@12854
   605
done
schirmer@12854
   606
schirmer@12854
   607
lemma subcls1_irrefl: "\<lbrakk>(x, y) \<in> subcls1 G; ws_prog G\<rbrakk> \<Longrightarrow> x \<noteq> y"
schirmer@12854
   608
apply (rule irrefl_trancl_rD)
schirmer@12854
   609
apply (rule subcls1_irrefl_lemma2)
schirmer@12854
   610
apply auto
schirmer@12854
   611
done
schirmer@12854
   612
schirmer@12854
   613
lemmas subint1_acyclic = subint1_irrefl_lemma2 [THEN acyclicI, standard]
schirmer@12854
   614
lemmas subcls1_acyclic = subcls1_irrefl_lemma2 [THEN acyclicI, standard]
schirmer@12854
   615
schirmer@12854
   616
schirmer@12854
   617
lemma wf_subint1: "ws_prog G \<Longrightarrow> wf ((subint1 G)\<inverse>)"
schirmer@12854
   618
by (auto intro: finite_acyclic_wf_converse finite_subint1 subint1_acyclic)
schirmer@12854
   619
schirmer@12854
   620
lemma wf_subcls1: "ws_prog G \<Longrightarrow> wf ((subcls1 G)\<inverse>)"
schirmer@12854
   621
by (auto intro: finite_acyclic_wf_converse finite_subcls1 subcls1_acyclic)
schirmer@12854
   622
schirmer@12854
   623
schirmer@12854
   624
lemma subint1_induct: 
schirmer@12854
   625
  "\<lbrakk>ws_prog G; \<And>x. \<forall>y. (x, y) \<in> subint1 G \<longrightarrow> P y \<Longrightarrow> P x\<rbrakk> \<Longrightarrow> P a"
schirmer@12854
   626
apply (frule wf_subint1)
schirmer@12854
   627
apply (erule wf_induct)
schirmer@12854
   628
apply (simp (no_asm_use) only: converse_iff)
schirmer@12854
   629
apply blast
schirmer@12854
   630
done
schirmer@12854
   631
schirmer@12854
   632
lemma subcls1_induct [consumes 1]:
schirmer@12854
   633
  "\<lbrakk>ws_prog G; \<And>x. \<forall>y. (x, y) \<in> subcls1 G \<longrightarrow> P y \<Longrightarrow> P x\<rbrakk> \<Longrightarrow> P a"
schirmer@12854
   634
apply (frule wf_subcls1)
schirmer@12854
   635
apply (erule wf_induct)
schirmer@12854
   636
apply (simp (no_asm_use) only: converse_iff)
schirmer@12854
   637
apply blast
schirmer@12854
   638
done
schirmer@12854
   639
schirmer@12854
   640
lemma ws_subint1_induct: 
schirmer@12854
   641
 "\<lbrakk>is_iface G I; ws_prog G; \<And>I i. \<lbrakk>iface G I = Some i \<and> 
schirmer@12854
   642
   (\<forall>J \<in> set (isuperIfs i). (I,J)\<in>subint1 G \<and> P J \<and> is_iface G J)\<rbrakk> \<Longrightarrow> P I
schirmer@12854
   643
  \<rbrakk> \<Longrightarrow> P I"
wenzelm@24038
   644
apply (erule rev_mp)
schirmer@12854
   645
apply (rule subint1_induct)
schirmer@12854
   646
apply  assumption
paulson@18447
   647
apply (simp (no_asm)) 
schirmer@12854
   648
apply safe
paulson@18447
   649
apply (blast dest: subint1I ws_prog_ideclD)
schirmer@12854
   650
done
schirmer@12854
   651
schirmer@12854
   652
schirmer@12854
   653
lemma ws_subcls1_induct: "\<lbrakk>is_class G C; ws_prog G;  
schirmer@12854
   654
  \<And>C c. \<lbrakk>class G C = Some c;  
schirmer@12854
   655
 (C \<noteq> Object \<longrightarrow> (C,(super c))\<in>subcls1 G \<and> 
schirmer@12854
   656
                  P (super c) \<and> is_class G (super c))\<rbrakk> \<Longrightarrow> P C
schirmer@12854
   657
 \<rbrakk> \<Longrightarrow> P C"
wenzelm@24038
   658
apply (erule rev_mp)
schirmer@12854
   659
apply (rule subcls1_induct)
schirmer@12854
   660
apply  assumption
paulson@18447
   661
apply (simp (no_asm)) 
schirmer@12854
   662
apply safe
schirmer@12854
   663
apply (fast dest: subcls1I ws_prog_cdeclD)
schirmer@12854
   664
done
schirmer@12854
   665
schirmer@12854
   666
lemma ws_class_induct [consumes 2, case_names Object Subcls]:
schirmer@12854
   667
"\<lbrakk>class G C = Some c; ws_prog G; 
schirmer@12854
   668
  \<And> co. class G Object = Some co \<Longrightarrow> P Object; 
schirmer@12854
   669
  \<And>  C c. \<lbrakk>class G C = Some c; C \<noteq> Object; P (super c)\<rbrakk> \<Longrightarrow> P C
schirmer@12854
   670
 \<rbrakk> \<Longrightarrow> P C"
schirmer@12854
   671
proof -
schirmer@12854
   672
  assume clsC: "class G C = Some c"
schirmer@12854
   673
  and    init: "\<And> co. class G Object = Some co \<Longrightarrow> P Object"
schirmer@12854
   674
  and    step: "\<And>   C c. \<lbrakk>class G C = Some c; C \<noteq> Object; P (super c)\<rbrakk> \<Longrightarrow> P C"
schirmer@12854
   675
  assume ws: "ws_prog G"
schirmer@12854
   676
  then have "is_class G C \<Longrightarrow> P C"  
schirmer@12854
   677
  proof (induct rule: subcls1_induct)
schirmer@12854
   678
    fix C
haftmann@35416
   679
    assume   hyp:"\<forall> S. G\<turnstile>C \<prec>\<^sub>C1 S \<longrightarrow> is_class G S \<longrightarrow> P S"
schirmer@12854
   680
       and iscls:"is_class G C"
schirmer@12854
   681
    show "P C"
schirmer@12854
   682
    proof (cases "C=Object")
schirmer@12854
   683
      case True with iscls init show "P C" by auto
schirmer@12854
   684
    next
schirmer@12854
   685
      case False with ws step hyp iscls 
nipkow@18576
   686
      show "P C" by (auto dest: subcls1I ws_prog_cdeclD)
schirmer@12854
   687
    qed
schirmer@12854
   688
  qed
schirmer@12854
   689
  with clsC show ?thesis by simp
schirmer@12854
   690
qed
schirmer@12854
   691
schirmer@12854
   692
lemma ws_class_induct' [consumes 2, case_names Object Subcls]:
schirmer@12854
   693
"\<lbrakk>is_class G C; ws_prog G; 
schirmer@12854
   694
  \<And> co. class G Object = Some co \<Longrightarrow> P Object; 
schirmer@12854
   695
  \<And> C c. \<lbrakk>class G C = Some c; C \<noteq> Object; P (super c)\<rbrakk> \<Longrightarrow> P C
schirmer@12854
   696
 \<rbrakk> \<Longrightarrow> P C"
paulson@18447
   697
by (auto intro: ws_class_induct)
schirmer@12854
   698
schirmer@12854
   699
lemma ws_class_induct'' [consumes 2, case_names Object Subcls]:
schirmer@12854
   700
"\<lbrakk>class G C = Some c; ws_prog G; 
schirmer@12854
   701
  \<And> co. class G Object = Some co \<Longrightarrow> P Object co; 
schirmer@12854
   702
  \<And>  C c sc. \<lbrakk>class G C = Some c; class G (super c) = Some sc;
schirmer@12854
   703
            C \<noteq> Object; P (super c) sc\<rbrakk> \<Longrightarrow> P C c
schirmer@12854
   704
 \<rbrakk> \<Longrightarrow> P C c"
schirmer@12854
   705
proof -
schirmer@12854
   706
  assume clsC: "class G C = Some c"
schirmer@12854
   707
  and    init: "\<And> co. class G Object = Some co \<Longrightarrow> P Object co"
schirmer@12854
   708
  and    step: "\<And> C c sc . \<lbrakk>class G C = Some c; class G (super c) = Some sc;
schirmer@12854
   709
                             C \<noteq> Object; P (super c) sc\<rbrakk> \<Longrightarrow> P C c"
schirmer@12854
   710
  assume ws: "ws_prog G"
schirmer@12854
   711
  then have "\<And> c. class G C = Some c\<Longrightarrow> P C c"  
schirmer@12854
   712
  proof (induct rule: subcls1_induct)
schirmer@12854
   713
    fix C c
haftmann@35416
   714
    assume   hyp:"\<forall> S. G\<turnstile>C \<prec>\<^sub>C1 S \<longrightarrow> (\<forall> s. class G S = Some s \<longrightarrow> P S s)"
schirmer@12854
   715
       and iscls:"class G C = Some c"
schirmer@12854
   716
    show "P C c"
schirmer@12854
   717
    proof (cases "C=Object")
schirmer@12854
   718
      case True with iscls init show "P C c" by auto
schirmer@12854
   719
    next
schirmer@12854
   720
      case False
schirmer@12854
   721
      with ws iscls obtain sc where
wenzelm@32960
   722
        sc: "class G (super c) = Some sc"
wenzelm@32960
   723
        by (auto dest: ws_prog_cdeclD)
haftmann@35416
   724
      from iscls False have "G\<turnstile>C \<prec>\<^sub>C1 (super c)" by (rule subcls1I)
schirmer@12854
   725
      with False ws step hyp iscls sc
schirmer@12854
   726
      show "P C c" 
wenzelm@32960
   727
        by (auto)  
schirmer@12854
   728
    qed
schirmer@12854
   729
  qed
schirmer@12854
   730
  with clsC show "P C c" by auto
schirmer@12854
   731
qed
schirmer@12854
   732
schirmer@12854
   733
lemma ws_interface_induct [consumes 2, case_names Step]:
wenzelm@12937
   734
  assumes is_if_I: "is_iface G I" and 
schirmer@12854
   735
               ws: "ws_prog G" and
schirmer@12854
   736
          hyp_sub: "\<And>I i. \<lbrakk>iface G I = Some i; 
schirmer@12854
   737
                            \<forall> J \<in> set (isuperIfs i).
schirmer@12854
   738
                                 (I,J)\<in>subint1 G \<and> P J \<and> is_iface G J\<rbrakk> \<Longrightarrow> P I"
wenzelm@12937
   739
  shows "P I"
schirmer@12854
   740
proof -
schirmer@12854
   741
  from is_if_I ws 
schirmer@12854
   742
  show "P I"
schirmer@12854
   743
  proof (rule ws_subint1_induct)
schirmer@12854
   744
    fix I i
schirmer@12854
   745
    assume hyp: "iface G I = Some i \<and>
schirmer@12854
   746
                (\<forall>J\<in>set (isuperIfs i). (I,J) \<in>subint1 G \<and> P J \<and> is_iface G J)"
schirmer@12854
   747
    then have if_I: "iface G I = Some i"
schirmer@12854
   748
      by blast
schirmer@12854
   749
    show "P I"
schirmer@12854
   750
    proof (cases "isuperIfs i")
schirmer@12854
   751
      case Nil
schirmer@12854
   752
      with if_I hyp_sub 
schirmer@12854
   753
      show "P I" 
wenzelm@32960
   754
        by auto
schirmer@12854
   755
    next
schirmer@12854
   756
      case (Cons hd tl)
schirmer@12854
   757
      with hyp if_I hyp_sub 
schirmer@12854
   758
      show "P I" 
wenzelm@32960
   759
        by auto
schirmer@12854
   760
    qed
schirmer@12854
   761
  qed
schirmer@12854
   762
qed
schirmer@12854
   763
schirmer@12854
   764
section "general recursion operators for the interface and class hiearchies"
schirmer@12854
   765
krauss@35440
   766
function
krauss@35440
   767
  iface_rec  :: "prog \<Rightarrow> qtname \<Rightarrow>   \<spacespace>(qtname \<Rightarrow> iface \<Rightarrow> 'a set \<Rightarrow> 'a) \<Rightarrow> 'a"
krauss@35440
   768
where
krauss@35440
   769
[simp del]: "iface_rec G I f = 
krauss@35440
   770
  (case iface G I of 
haftmann@28524
   771
         None \<Rightarrow> undefined 
schirmer@12854
   772
       | Some i \<Rightarrow> if ws_prog G 
schirmer@12854
   773
                      then f I i 
krauss@35440
   774
                               ((\<lambda>J. iface_rec G J f)`set (isuperIfs i))
haftmann@28524
   775
                      else undefined)"
krauss@35440
   776
by auto
krauss@35440
   777
termination
krauss@35440
   778
by (relation "inv_image (same_fst ws_prog (\<lambda>G. (subint1 G)^-1)) (%(x,y,z). (x,y))")
krauss@35440
   779
 (auto simp: wf_subint1 subint1I wf_same_fst)
schirmer@12854
   780
schirmer@12854
   781
lemma iface_rec: 
schirmer@12854
   782
"\<lbrakk>iface G I = Some i; ws_prog G\<rbrakk> \<Longrightarrow> 
krauss@35440
   783
 iface_rec G I f = f I i ((\<lambda>J. iface_rec G J f)`set (isuperIfs i))"
schirmer@12854
   784
apply (subst iface_rec.simps)
schirmer@12854
   785
apply simp
schirmer@12854
   786
done
schirmer@12854
   787
krauss@35440
   788
krauss@35440
   789
function
krauss@35440
   790
  class_rec  :: "prog \<Rightarrow> qtname \<Rightarrow> 'a \<Rightarrow> (qtname \<Rightarrow> class \<Rightarrow> 'a     \<Rightarrow> 'a) \<Rightarrow> 'a"
krauss@35440
   791
where
krauss@35440
   792
[simp del]: "class_rec G C t f = 
krauss@35440
   793
  (case class G C of 
haftmann@28524
   794
           None \<Rightarrow> undefined 
schirmer@12854
   795
         | Some c \<Rightarrow> if ws_prog G 
schirmer@12854
   796
                        then f C c 
schirmer@12854
   797
                                 (if C = Object then t 
krauss@35440
   798
                                                else class_rec G (super c) t f)
haftmann@28524
   799
                        else undefined)"
krauss@35440
   800
krauss@35440
   801
by auto
krauss@35440
   802
termination
krauss@35440
   803
by (relation "inv_image (same_fst ws_prog (\<lambda>G. (subcls1 G)^-1)) (%(x,y,z,w). (x,y))")
krauss@35440
   804
 (auto simp: wf_subcls1 subcls1I wf_same_fst)
schirmer@12854
   805
schirmer@12854
   806
lemma class_rec: "\<lbrakk>class G C = Some c; ws_prog G\<rbrakk> \<Longrightarrow>  
krauss@35440
   807
 class_rec G C t f = 
krauss@35440
   808
   f C c (if C = Object then t else class_rec G (super c) t f)"
krauss@35440
   809
apply (subst class_rec.simps)
schirmer@12854
   810
apply simp
schirmer@12854
   811
done
schirmer@12854
   812
haftmann@35416
   813
definition imethds :: "prog \<Rightarrow> qtname \<Rightarrow> (sig,qtname \<times> mhead) tables" where
schirmer@13688
   814
  --{* methods of an interface, with overriding and inheritance, cf. 9.2 *}
schirmer@12854
   815
"imethds G I 
krauss@35440
   816
  \<equiv> iface_rec G I  
schirmer@12854
   817
              (\<lambda>I i ts. (Un_tables ts) \<oplus>\<oplus> 
nipkow@30235
   818
                        (Option.set \<circ> table_of (map (\<lambda>(s,m). (s,I,m)) (imethods i))))"
wenzelm@32960
   819
        
schirmer@12854
   820
schirmer@12854
   821
schirmer@12854
   822
end