src/HOL/Bali/WellType.thy
author haftmann
Fri Jun 17 16:12:49 2005 +0200 (2005-06-17)
changeset 16417 9bc16273c2d4
parent 14981 e73f8140af78
child 17876 b9c92f384109
permissions -rw-r--r--
migrated theory headers to new format
wenzelm@12857
     1
(*  Title:      HOL/Bali/WellType.thy
schirmer@12854
     2
    ID:         $Id$
schirmer@12854
     3
    Author:     David von Oheimb
schirmer@12854
     4
*)
schirmer@12854
     5
header {* Well-typedness of Java programs *}
schirmer@12854
     6
haftmann@16417
     7
theory WellType imports DeclConcepts begin
schirmer@12854
     8
schirmer@12854
     9
text {*
schirmer@12854
    10
improvements over Java Specification 1.0:
schirmer@12854
    11
\begin{itemize}
schirmer@12854
    12
\item methods of Object can be called upon references of interface or array type
schirmer@12854
    13
\end{itemize}
schirmer@12854
    14
simplifications:
schirmer@12854
    15
\begin{itemize}
schirmer@12854
    16
\item the type rules include all static checks on statements and expressions, 
schirmer@12854
    17
      e.g. definedness of names (of parameters, locals, fields, methods)
schirmer@12854
    18
\end{itemize}
schirmer@12854
    19
design issues:
schirmer@12854
    20
\begin{itemize}
schirmer@12854
    21
\item unified type judgment for statements, variables, expressions, 
schirmer@12854
    22
      expression lists
schirmer@12854
    23
\item statements are typed like expressions with dummy type Void
schirmer@12854
    24
\item the typing rules take an extra argument that is capable of determining 
schirmer@12854
    25
  the dynamic type of objects. Therefore, they can be used for both 
schirmer@12854
    26
  checking static types and determining runtime types in transition semantics.
schirmer@12854
    27
\end{itemize}
schirmer@12854
    28
*}
schirmer@12854
    29
schirmer@12854
    30
types	lenv
schirmer@13688
    31
	= "(lname, ty) table"  --{* local variables, including This and Result*}
schirmer@12854
    32
schirmer@12854
    33
record env = 
schirmer@13688
    34
         prg:: "prog"    --{* program *}
schirmer@13688
    35
         cls:: "qtname"  --{* current package and class name *}
schirmer@13688
    36
         lcl:: "lenv"    --{* local environment *}     
schirmer@12854
    37
  
schirmer@12854
    38
translations
schirmer@12854
    39
  "lenv" <= (type) "(lname, ty) table"
schirmer@12854
    40
  "lenv" <= (type) "lname \<Rightarrow> ty option"
schirmer@12854
    41
  "env" <= (type) "\<lparr>prg::prog,cls::qtname,lcl::lenv\<rparr>"
schirmer@12854
    42
  "env" <= (type) "\<lparr>prg::prog,cls::qtname,lcl::lenv,\<dots>::'a\<rparr>"
schirmer@12854
    43
schirmer@12854
    44
schirmer@12854
    45
schirmer@12854
    46
syntax
schirmer@13688
    47
  pkg :: "env \<Rightarrow> pname" --{* select the current package from an environment *}
schirmer@13688
    48
translations 
schirmer@12854
    49
  "pkg e" == "pid (cls e)"
schirmer@12854
    50
schirmer@12854
    51
section "Static overloading: maximally specific methods "
schirmer@12854
    52
schirmer@12854
    53
types
schirmer@12854
    54
  emhead = "ref_ty \<times> mhead"
schirmer@12854
    55
schirmer@13688
    56
--{* Some mnemotic selectors for emhead *}
schirmer@12854
    57
constdefs 
schirmer@12854
    58
  "declrefT" :: "emhead \<Rightarrow> ref_ty"
schirmer@12854
    59
  "declrefT \<equiv> fst"
schirmer@12854
    60
schirmer@12854
    61
  "mhd"     :: "emhead \<Rightarrow> mhead"
schirmer@12854
    62
  "mhd \<equiv> snd"
schirmer@12854
    63
schirmer@12854
    64
lemma declrefT_simp[simp]:"declrefT (r,m) = r"
schirmer@12854
    65
by (simp add: declrefT_def)
schirmer@12854
    66
schirmer@12854
    67
lemma mhd_simp[simp]:"mhd (r,m) = m"
schirmer@12854
    68
by (simp add: mhd_def)
schirmer@12854
    69
schirmer@12854
    70
lemma static_mhd_simp[simp]: "static (mhd m) = is_static m"
schirmer@12854
    71
by (cases m) (simp add: member_is_static_simp mhd_def)
schirmer@12854
    72
schirmer@12854
    73
lemma mhd_resTy_simp [simp]: "resTy (mhd m) = resTy m"
schirmer@12854
    74
by (cases m) simp
schirmer@12854
    75
schirmer@12854
    76
lemma mhd_is_static_simp [simp]: "is_static (mhd m) = is_static m"
schirmer@12854
    77
by (cases m) simp
schirmer@12854
    78
schirmer@12854
    79
lemma mhd_accmodi_simp [simp]: "accmodi (mhd m) = accmodi m"
schirmer@12854
    80
by (cases m) simp
schirmer@12854
    81
schirmer@12854
    82
consts
schirmer@12854
    83
  cmheads        :: "prog \<Rightarrow> qtname \<Rightarrow> qtname \<Rightarrow> sig \<Rightarrow> emhead set"
schirmer@12854
    84
  Objectmheads   :: "prog \<Rightarrow> qtname           \<Rightarrow> sig \<Rightarrow> emhead set"
schirmer@12854
    85
  accObjectmheads:: "prog \<Rightarrow> qtname \<Rightarrow> ref_ty \<Rightarrow> sig \<Rightarrow> emhead set"
schirmer@12854
    86
  mheads         :: "prog \<Rightarrow> qtname \<Rightarrow> ref_ty \<Rightarrow> sig \<Rightarrow> emhead set"
schirmer@12854
    87
defs
schirmer@12854
    88
 cmheads_def:
schirmer@12854
    89
"cmheads G S C 
schirmer@12854
    90
  \<equiv> \<lambda>sig. (\<lambda>(Cls,mthd). (ClassT Cls,(mhead mthd))) ` o2s (accmethd G S C sig)"
schirmer@12854
    91
  Objectmheads_def:
schirmer@12854
    92
"Objectmheads G S  
schirmer@12854
    93
  \<equiv> \<lambda>sig. (\<lambda>(Cls,mthd). (ClassT Cls,(mhead mthd))) 
schirmer@12854
    94
    ` o2s (filter_tab (\<lambda>sig m. accmodi m \<noteq> Private) (accmethd G S Object) sig)"
schirmer@12854
    95
  accObjectmheads_def:
schirmer@12854
    96
"accObjectmheads G S T
schirmer@12854
    97
   \<equiv> if G\<turnstile>RefT T accessible_in (pid S)
schirmer@12854
    98
        then Objectmheads G S
schirmer@12854
    99
        else \<lambda>sig. {}"
schirmer@12854
   100
primrec
schirmer@12854
   101
"mheads G S  NullT     = (\<lambda>sig. {})"
schirmer@12854
   102
"mheads G S (IfaceT I) = (\<lambda>sig. (\<lambda>(I,h).(IfaceT I,h)) 
schirmer@12854
   103
                         ` accimethds G (pid S) I sig \<union> 
schirmer@12854
   104
                           accObjectmheads G S (IfaceT I) sig)"
schirmer@12854
   105
"mheads G S (ClassT C) = cmheads G S C"
schirmer@12854
   106
"mheads G S (ArrayT T) = accObjectmheads G S (ArrayT T)"
schirmer@12854
   107
schirmer@12854
   108
constdefs
schirmer@13688
   109
  --{* applicable methods, cf. 15.11.2.1 *}
schirmer@12854
   110
  appl_methds   :: "prog \<Rightarrow> qtname \<Rightarrow>  ref_ty \<Rightarrow> sig \<Rightarrow> (emhead \<times> ty list)\<spacespace> set"
schirmer@12854
   111
 "appl_methds G S rt \<equiv> \<lambda> sig. 
schirmer@12854
   112
      {(mh,pTs') |mh pTs'. mh \<in> mheads G S rt \<lparr>name=name sig,parTs=pTs'\<rparr> \<and> 
schirmer@12854
   113
                           G\<turnstile>(parTs sig)[\<preceq>]pTs'}"
schirmer@12854
   114
schirmer@13688
   115
  --{* more specific methods, cf. 15.11.2.2 *}
schirmer@12854
   116
  more_spec     :: "prog \<Rightarrow> emhead \<times> ty list \<Rightarrow> emhead \<times> ty list \<Rightarrow> bool"
schirmer@12854
   117
 "more_spec G \<equiv> \<lambda>(mh,pTs). \<lambda>(mh',pTs'). G\<turnstile>pTs[\<preceq>]pTs'"
schirmer@12854
   118
(*more_spec G \<equiv>\<lambda>((d,h),pTs). \<lambda>((d',h'),pTs'). G\<turnstile>RefT d\<preceq>RefT d'\<and>G\<turnstile>pTs[\<preceq>]pTs'*)
schirmer@12854
   119
schirmer@13688
   120
  --{* maximally specific methods, cf. 15.11.2.2 *}
schirmer@12854
   121
   max_spec     :: "prog \<Rightarrow> qtname \<Rightarrow> ref_ty \<Rightarrow> sig \<Rightarrow> (emhead \<times> ty list)\<spacespace> set"
schirmer@12854
   122
schirmer@12854
   123
 "max_spec G S rt sig \<equiv>{m. m \<in>appl_methds G S rt sig \<and>
schirmer@12854
   124
		      (\<forall>m'\<in>appl_methds G S rt sig. more_spec G m' m \<longrightarrow> m'=m)}"
schirmer@12854
   125
schirmer@12854
   126
schirmer@12854
   127
lemma max_spec2appl_meths: 
schirmer@12854
   128
  "x \<in> max_spec G S T sig \<Longrightarrow> x \<in> appl_methds G S T sig" 
schirmer@12854
   129
by (auto simp: max_spec_def)
schirmer@12854
   130
schirmer@12854
   131
lemma appl_methsD: "(mh,pTs')\<in>appl_methds G S T \<lparr>name=mn,parTs=pTs\<rparr> \<Longrightarrow>  
schirmer@12854
   132
   mh \<in> mheads G S T \<lparr>name=mn,parTs=pTs'\<rparr> \<and> G\<turnstile>pTs[\<preceq>]pTs'"
schirmer@12854
   133
by (auto simp: appl_methds_def)
schirmer@12854
   134
schirmer@12854
   135
lemma max_spec2mheads: 
schirmer@12854
   136
"max_spec G S rt \<lparr>name=mn,parTs=pTs\<rparr> = insert (mh, pTs') A 
schirmer@12854
   137
 \<Longrightarrow> mh \<in> mheads G S rt \<lparr>name=mn,parTs=pTs'\<rparr> \<and> G\<turnstile>pTs[\<preceq>]pTs'"
schirmer@12854
   138
apply (auto dest: equalityD2 subsetD max_spec2appl_meths appl_methsD)
schirmer@12854
   139
done
schirmer@12854
   140
schirmer@12854
   141
schirmer@12854
   142
constdefs
schirmer@12854
   143
  empty_dt :: "dyn_ty"
schirmer@12854
   144
 "empty_dt \<equiv> \<lambda>a. None"
schirmer@12854
   145
schirmer@12854
   146
  invmode :: "('a::type)member_scheme \<Rightarrow> expr \<Rightarrow> inv_mode"
schirmer@12925
   147
"invmode m e \<equiv> if is_static m 
schirmer@12925
   148
                  then Static 
schirmer@12925
   149
                  else if e=Super then SuperM else IntVir"
schirmer@12854
   150
schirmer@12854
   151
lemma invmode_nonstatic [simp]: 
schirmer@12854
   152
  "invmode \<lparr>access=a,static=False,\<dots>=x\<rparr> (Acc (LVar e)) = IntVir"
schirmer@12854
   153
apply (unfold invmode_def)
schirmer@12925
   154
apply (simp (no_asm) add: member_is_static_simp)
schirmer@12925
   155
done
schirmer@12925
   156
schirmer@12925
   157
schirmer@12925
   158
lemma invmode_Static_eq [simp]: "(invmode m e = Static) = is_static m"
schirmer@12925
   159
apply (unfold invmode_def)
schirmer@12854
   160
apply (simp (no_asm))
schirmer@12854
   161
done
schirmer@12854
   162
schirmer@12854
   163
schirmer@12925
   164
lemma invmode_IntVir_eq: "(invmode m e = IntVir) = (\<not>(is_static m) \<and> e\<noteq>Super)"
schirmer@12854
   165
apply (unfold invmode_def)
schirmer@12854
   166
apply (simp (no_asm))
schirmer@12854
   167
done
schirmer@12854
   168
schirmer@12854
   169
lemma Null_staticD: 
schirmer@12925
   170
  "a'=Null \<longrightarrow> (is_static m) \<Longrightarrow> invmode m e = IntVir \<longrightarrow> a' \<noteq> Null"
schirmer@12854
   171
apply (clarsimp simp add: invmode_IntVir_eq)
schirmer@12854
   172
done
schirmer@12854
   173
schirmer@13688
   174
section "Typing for unary operations"
schirmer@13688
   175
schirmer@13337
   176
consts unop_type :: "unop \<Rightarrow> prim_ty"
schirmer@13337
   177
primrec 
schirmer@13337
   178
"unop_type UPlus   = Integer"
schirmer@13337
   179
"unop_type UMinus  = Integer"
schirmer@13337
   180
"unop_type UBitNot = Integer"
schirmer@13337
   181
"unop_type UNot    = Boolean"    
schirmer@12854
   182
schirmer@13337
   183
consts wt_unop :: "unop \<Rightarrow> ty \<Rightarrow> bool"
schirmer@13337
   184
primrec
schirmer@13337
   185
"wt_unop UPlus   t = (t = PrimT Integer)"
schirmer@13337
   186
"wt_unop UMinus  t = (t = PrimT Integer)"
schirmer@13337
   187
"wt_unop UBitNot t = (t = PrimT Integer)"
schirmer@13337
   188
"wt_unop UNot    t = (t = PrimT Boolean)"
schirmer@13337
   189
schirmer@13688
   190
section "Typing for binary operations"
schirmer@13688
   191
schirmer@13337
   192
consts binop_type :: "binop \<Rightarrow> prim_ty"
schirmer@13337
   193
primrec
schirmer@13337
   194
"binop_type Mul      = Integer"     
schirmer@13337
   195
"binop_type Div      = Integer"
schirmer@13337
   196
"binop_type Mod      = Integer"
schirmer@13337
   197
"binop_type Plus     = Integer"
schirmer@13337
   198
"binop_type Minus    = Integer"
schirmer@13337
   199
"binop_type LShift   = Integer"
schirmer@13337
   200
"binop_type RShift   = Integer"
schirmer@13337
   201
"binop_type RShiftU  = Integer"
schirmer@13337
   202
"binop_type Less     = Boolean"
schirmer@13337
   203
"binop_type Le       = Boolean"
schirmer@13337
   204
"binop_type Greater  = Boolean"
schirmer@13337
   205
"binop_type Ge       = Boolean"
schirmer@13337
   206
"binop_type Eq       = Boolean"
schirmer@13337
   207
"binop_type Neq      = Boolean"
schirmer@13337
   208
"binop_type BitAnd   = Integer"
schirmer@13337
   209
"binop_type And      = Boolean"
schirmer@13337
   210
"binop_type BitXor   = Integer"
schirmer@13337
   211
"binop_type Xor      = Boolean"
schirmer@13337
   212
"binop_type BitOr    = Integer"
schirmer@13337
   213
"binop_type Or       = Boolean"
schirmer@13384
   214
"binop_type CondAnd  = Boolean"
schirmer@13384
   215
"binop_type CondOr   = Boolean"
schirmer@13337
   216
schirmer@13337
   217
consts wt_binop :: "prog \<Rightarrow> binop \<Rightarrow> ty \<Rightarrow> ty \<Rightarrow> bool"
schirmer@13337
   218
primrec
schirmer@13337
   219
"wt_binop G Mul     t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   220
"wt_binop G Div     t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   221
"wt_binop G Mod     t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   222
"wt_binop G Plus    t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   223
"wt_binop G Minus   t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   224
"wt_binop G LShift  t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   225
"wt_binop G RShift  t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   226
"wt_binop G RShiftU t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   227
"wt_binop G Less    t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   228
"wt_binop G Le      t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   229
"wt_binop G Greater t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   230
"wt_binop G Ge      t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   231
"wt_binop G Eq      t1 t2 = (G\<turnstile>t1\<preceq>t2 \<or> G\<turnstile>t2\<preceq>t1)" 
schirmer@13337
   232
"wt_binop G Neq     t1 t2 = (G\<turnstile>t1\<preceq>t2 \<or> G\<turnstile>t2\<preceq>t1)"
schirmer@13337
   233
"wt_binop G BitAnd  t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   234
"wt_binop G And     t1 t2 = ((t1 = PrimT Boolean) \<and> (t2 = PrimT Boolean))"
schirmer@13337
   235
"wt_binop G BitXor  t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   236
"wt_binop G Xor     t1 t2 = ((t1 = PrimT Boolean) \<and> (t2 = PrimT Boolean))"
schirmer@13337
   237
"wt_binop G BitOr   t1 t2 = ((t1 = PrimT Integer) \<and> (t2 = PrimT Integer))"
schirmer@13337
   238
"wt_binop G Or      t1 t2 = ((t1 = PrimT Boolean) \<and> (t2 = PrimT Boolean))"
schirmer@13384
   239
"wt_binop G CondAnd t1 t2 = ((t1 = PrimT Boolean) \<and> (t2 = PrimT Boolean))"
schirmer@13384
   240
"wt_binop G CondOr  t1 t2 = ((t1 = PrimT Boolean) \<and> (t2 = PrimT Boolean))"
schirmer@13337
   241
schirmer@13688
   242
section "Typing for terms"
schirmer@13384
   243
schirmer@12854
   244
types tys  =        "ty + ty list"
schirmer@12854
   245
translations
schirmer@12854
   246
  "tys"   <= (type) "ty + ty list"
schirmer@12854
   247
schirmer@12854
   248
consts
schirmer@12854
   249
  wt    :: "(env\<spacespace> \<times> dyn_ty\<spacespace> \<times>  term \<times> tys) set"
schirmer@12854
   250
(*wt    :: " env \<Rightarrow> dyn_ty \<Rightarrow> (term \<times> tys) set" not feasible because of 
schirmer@12854
   251
					      \<spacespace>  changing env in Try stmt *)
schirmer@12854
   252
schirmer@12854
   253
syntax
schirmer@12854
   254
wt      :: "env \<Rightarrow> dyn_ty \<Rightarrow> [term,tys]  \<Rightarrow> bool" ("_,_|=_::_" [51,51,51,51] 50)
schirmer@12854
   255
wt_stmt :: "env \<Rightarrow> dyn_ty \<Rightarrow>  stmt       \<Rightarrow> bool" ("_,_|=_:<>" [51,51,51   ] 50)
schirmer@12854
   256
ty_expr :: "env \<Rightarrow> dyn_ty \<Rightarrow> [expr ,ty ] \<Rightarrow> bool" ("_,_|=_:-_" [51,51,51,51] 50)
schirmer@12854
   257
ty_var  :: "env \<Rightarrow> dyn_ty \<Rightarrow> [var  ,ty ] \<Rightarrow> bool" ("_,_|=_:=_" [51,51,51,51] 50)
schirmer@12854
   258
ty_exprs:: "env \<Rightarrow> dyn_ty \<Rightarrow> [expr list,
schirmer@12854
   259
	         \<spacespace>        \<spacespace>  ty   list] \<Rightarrow> bool" ("_,_|=_:#_" [51,51,51,51] 50)
schirmer@12854
   260
schirmer@12854
   261
syntax (xsymbols)
schirmer@12854
   262
wt      :: "env \<Rightarrow> dyn_ty \<Rightarrow> [term,tys] \<Rightarrow> bool" ("_,_\<Turnstile>_\<Colon>_"  [51,51,51,51] 50)
schirmer@12854
   263
wt_stmt :: "env \<Rightarrow> dyn_ty \<Rightarrow>  stmt       \<Rightarrow> bool" ("_,_\<Turnstile>_\<Colon>\<surd>"  [51,51,51   ] 50)
schirmer@12854
   264
ty_expr :: "env \<Rightarrow> dyn_ty \<Rightarrow> [expr ,ty ] \<Rightarrow> bool" ("_,_\<Turnstile>_\<Colon>-_" [51,51,51,51] 50)
schirmer@12854
   265
ty_var  :: "env \<Rightarrow> dyn_ty \<Rightarrow> [var  ,ty ] \<Rightarrow> bool" ("_,_\<Turnstile>_\<Colon>=_" [51,51,51,51] 50)
schirmer@12854
   266
ty_exprs:: "env \<Rightarrow> dyn_ty \<Rightarrow> [expr list,
schirmer@12854
   267
		  \<spacespace>        \<spacespace>  ty   list] \<Rightarrow> bool"("_,_\<Turnstile>_\<Colon>\<doteq>_" [51,51,51,51] 50)
schirmer@12854
   268
schirmer@12854
   269
translations
schirmer@12854
   270
	"E,dt\<Turnstile>t\<Colon>T" == "(E,dt,t,T) \<in> wt"
schirmer@12854
   271
	"E,dt\<Turnstile>s\<Colon>\<surd>"  == "E,dt\<Turnstile>In1r s\<Colon>Inl (PrimT Void)"
schirmer@12854
   272
	"E,dt\<Turnstile>e\<Colon>-T" == "E,dt\<Turnstile>In1l e\<Colon>Inl T"
schirmer@12854
   273
	"E,dt\<Turnstile>e\<Colon>=T" == "E,dt\<Turnstile>In2  e\<Colon>Inl T"
schirmer@12854
   274
	"E,dt\<Turnstile>e\<Colon>\<doteq>T" == "E,dt\<Turnstile>In3  e\<Colon>Inr T"
schirmer@12854
   275
schirmer@12854
   276
syntax (* for purely static typing *)
schirmer@12854
   277
  wt_      :: "env \<Rightarrow> [term,tys] \<Rightarrow> bool" ("_|-_::_" [51,51,51] 50)
schirmer@12854
   278
  wt_stmt_ :: "env \<Rightarrow>  stmt       \<Rightarrow> bool" ("_|-_:<>" [51,51   ] 50)
schirmer@12854
   279
  ty_expr_ :: "env \<Rightarrow> [expr ,ty ] \<Rightarrow> bool" ("_|-_:-_" [51,51,51] 50)
schirmer@12854
   280
  ty_var_  :: "env \<Rightarrow> [var  ,ty ] \<Rightarrow> bool" ("_|-_:=_" [51,51,51] 50)
schirmer@12854
   281
  ty_exprs_:: "env \<Rightarrow> [expr list,
schirmer@12854
   282
		     \<spacespace> ty   list] \<Rightarrow> bool" ("_|-_:#_" [51,51,51] 50)
schirmer@12854
   283
schirmer@12854
   284
syntax (xsymbols)
schirmer@12854
   285
  wt_      :: "env \<Rightarrow> [term,tys] \<Rightarrow> bool" ("_\<turnstile>_\<Colon>_"  [51,51,51] 50)
schirmer@12854
   286
  wt_stmt_ ::  "env \<Rightarrow>  stmt       \<Rightarrow> bool" ("_\<turnstile>_\<Colon>\<surd>"  [51,51   ] 50)
schirmer@12854
   287
  ty_expr_ :: "env \<Rightarrow> [expr ,ty ] \<Rightarrow> bool" ("_\<turnstile>_\<Colon>-_" [51,51,51] 50)
schirmer@12854
   288
  ty_var_  :: "env \<Rightarrow> [var  ,ty ] \<Rightarrow> bool" ("_\<turnstile>_\<Colon>=_" [51,51,51] 50)
schirmer@12854
   289
  ty_exprs_ :: "env \<Rightarrow> [expr list,
schirmer@12854
   290
		    \<spacespace>  ty   list] \<Rightarrow> bool" ("_\<turnstile>_\<Colon>\<doteq>_" [51,51,51] 50)
schirmer@12854
   291
schirmer@12854
   292
translations
schirmer@12854
   293
	"E\<turnstile>t\<Colon> T" == "E,empty_dt\<Turnstile>t\<Colon> T"
schirmer@13688
   294
        "E\<turnstile>s\<Colon>\<surd>"  == "E\<turnstile>In1r s\<Colon>Inl (PrimT Void)"
schirmer@13688
   295
	"E\<turnstile>e\<Colon>-T" == "E\<turnstile>In1l e\<Colon>Inl T"
schirmer@13688
   296
	"E\<turnstile>e\<Colon>=T" == "E\<turnstile>In2  e\<Colon>Inl T"
schirmer@13688
   297
	"E\<turnstile>e\<Colon>\<doteq>T" == "E\<turnstile>In3  e\<Colon>Inr T"
schirmer@12854
   298
schirmer@13337
   299
schirmer@12854
   300
inductive wt intros 
schirmer@12854
   301
schirmer@12854
   302
schirmer@13688
   303
--{* well-typed statements *}
schirmer@12854
   304
schirmer@12854
   305
  Skip:					"E,dt\<Turnstile>Skip\<Colon>\<surd>"
schirmer@12854
   306
schirmer@12854
   307
  Expr:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-T\<rbrakk> \<Longrightarrow>
schirmer@12854
   308
					 E,dt\<Turnstile>Expr e\<Colon>\<surd>"
schirmer@13688
   309
  --{* cf. 14.6 *}
schirmer@12854
   310
  Lab:  "E,dt\<Turnstile>c\<Colon>\<surd> \<Longrightarrow>                   
schirmer@12854
   311
                                         E,dt\<Turnstile>l\<bullet> c\<Colon>\<surd>" 
schirmer@12854
   312
schirmer@12854
   313
  Comp:	"\<lbrakk>E,dt\<Turnstile>c1\<Colon>\<surd>; 
schirmer@12854
   314
	  E,dt\<Turnstile>c2\<Colon>\<surd>\<rbrakk> \<Longrightarrow>
schirmer@12854
   315
					 E,dt\<Turnstile>c1;; c2\<Colon>\<surd>"
schirmer@12854
   316
schirmer@13688
   317
  --{* cf. 14.8 *}
schirmer@12854
   318
  If:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-PrimT Boolean;
schirmer@12854
   319
	  E,dt\<Turnstile>c1\<Colon>\<surd>;
schirmer@12854
   320
	  E,dt\<Turnstile>c2\<Colon>\<surd>\<rbrakk> \<Longrightarrow>
schirmer@12854
   321
					 E,dt\<Turnstile>If(e) c1 Else c2\<Colon>\<surd>"
schirmer@12854
   322
schirmer@13688
   323
  --{* cf. 14.10 *}
schirmer@12854
   324
  Loop:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-PrimT Boolean;
schirmer@12854
   325
	  E,dt\<Turnstile>c\<Colon>\<surd>\<rbrakk> \<Longrightarrow>
schirmer@12854
   326
					 E,dt\<Turnstile>l\<bullet> While(e) c\<Colon>\<surd>"
schirmer@13688
   327
  --{* cf. 14.13, 14.15, 14.16 *}
schirmer@13688
   328
  Jmp:                                   "E,dt\<Turnstile>Jmp jump\<Colon>\<surd>"
schirmer@12854
   329
schirmer@13688
   330
  --{* cf. 14.16 *}
schirmer@12854
   331
  Throw: "\<lbrakk>E,dt\<Turnstile>e\<Colon>-Class tn;
schirmer@12854
   332
	  prg E\<turnstile>tn\<preceq>\<^sub>C SXcpt Throwable\<rbrakk> \<Longrightarrow>
schirmer@12854
   333
					 E,dt\<Turnstile>Throw e\<Colon>\<surd>"
schirmer@13688
   334
  --{* cf. 14.18 *}
schirmer@12854
   335
  Try:	"\<lbrakk>E,dt\<Turnstile>c1\<Colon>\<surd>; prg E\<turnstile>tn\<preceq>\<^sub>C SXcpt Throwable;
schirmer@12854
   336
	  lcl E (VName vn)=None; E \<lparr>lcl := lcl E(VName vn\<mapsto>Class tn)\<rparr>,dt\<Turnstile>c2\<Colon>\<surd>\<rbrakk>
schirmer@12854
   337
          \<Longrightarrow>
schirmer@12854
   338
					 E,dt\<Turnstile>Try c1 Catch(tn vn) c2\<Colon>\<surd>"
schirmer@12854
   339
schirmer@13688
   340
  --{* cf. 14.18 *}
schirmer@12854
   341
  Fin:	"\<lbrakk>E,dt\<Turnstile>c1\<Colon>\<surd>; E,dt\<Turnstile>c2\<Colon>\<surd>\<rbrakk> \<Longrightarrow>
schirmer@12854
   342
					 E,dt\<Turnstile>c1 Finally c2\<Colon>\<surd>"
schirmer@12854
   343
schirmer@12854
   344
  Init:	"\<lbrakk>is_class (prg E) C\<rbrakk> \<Longrightarrow>
schirmer@12854
   345
					 E,dt\<Turnstile>Init C\<Colon>\<surd>"
schirmer@13688
   346
  --{* @{term Init} is created on the fly during evaluation (see Eval.thy). 
schirmer@13688
   347
     The class isn't necessarily accessible from the points @{term Init} 
schirmer@13688
   348
     is called. Therefor we only demand @{term is_class} and not 
schirmer@13688
   349
     @{term is_acc_class} here. 
schirmer@13688
   350
   *}
schirmer@12854
   351
schirmer@13688
   352
--{* well-typed expressions *}
schirmer@12854
   353
schirmer@13688
   354
  --{* cf. 15.8 *}
schirmer@12854
   355
  NewC:	"\<lbrakk>is_acc_class (prg E) (pkg E) C\<rbrakk> \<Longrightarrow>
schirmer@12854
   356
					 E,dt\<Turnstile>NewC C\<Colon>-Class C"
schirmer@13688
   357
  --{* cf. 15.9 *}
schirmer@12854
   358
  NewA:	"\<lbrakk>is_acc_type (prg E) (pkg E) T;
schirmer@12854
   359
	  E,dt\<Turnstile>i\<Colon>-PrimT Integer\<rbrakk> \<Longrightarrow>
schirmer@12854
   360
					 E,dt\<Turnstile>New T[i]\<Colon>-T.[]"
schirmer@12854
   361
schirmer@13688
   362
  --{* cf. 15.15 *}
schirmer@12854
   363
  Cast:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-T; is_acc_type (prg E) (pkg E) T';
schirmer@12854
   364
	  prg E\<turnstile>T\<preceq>? T'\<rbrakk> \<Longrightarrow>
schirmer@12854
   365
					 E,dt\<Turnstile>Cast T' e\<Colon>-T'"
schirmer@12854
   366
schirmer@13688
   367
  --{* cf. 15.19.2 *}
schirmer@12854
   368
  Inst:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-RefT T; is_acc_type (prg E) (pkg E) (RefT T');
schirmer@12854
   369
	  prg E\<turnstile>RefT T\<preceq>? RefT T'\<rbrakk> \<Longrightarrow>
schirmer@12854
   370
					 E,dt\<Turnstile>e InstOf T'\<Colon>-PrimT Boolean"
schirmer@12854
   371
schirmer@13688
   372
  --{* cf. 15.7.1 *}
schirmer@12854
   373
  Lit:	"\<lbrakk>typeof dt x = Some T\<rbrakk> \<Longrightarrow>
schirmer@12854
   374
					 E,dt\<Turnstile>Lit x\<Colon>-T"
schirmer@12854
   375
schirmer@13337
   376
  UnOp: "\<lbrakk>E,dt\<Turnstile>e\<Colon>-Te; wt_unop unop Te; T=PrimT (unop_type unop)\<rbrakk> 
schirmer@13337
   377
          \<Longrightarrow>
schirmer@13337
   378
	  E,dt\<Turnstile>UnOp unop e\<Colon>-T"
schirmer@13337
   379
  
schirmer@13337
   380
  BinOp: "\<lbrakk>E,dt\<Turnstile>e1\<Colon>-T1; E,dt\<Turnstile>e2\<Colon>-T2; wt_binop (prg E) binop T1 T2; 
schirmer@13337
   381
           T=PrimT (binop_type binop)\<rbrakk> 
schirmer@13337
   382
           \<Longrightarrow>
schirmer@13337
   383
	   E,dt\<Turnstile>BinOp binop e1 e2\<Colon>-T"
schirmer@13337
   384
  
schirmer@13688
   385
  --{* cf. 15.10.2, 15.11.1 *}
schirmer@12854
   386
  Super: "\<lbrakk>lcl E This = Some (Class C); C \<noteq> Object;
schirmer@12854
   387
	  class (prg E) C = Some c\<rbrakk> \<Longrightarrow>
schirmer@12854
   388
					 E,dt\<Turnstile>Super\<Colon>-Class (super c)"
schirmer@12854
   389
schirmer@13688
   390
  --{* cf. 15.13.1, 15.10.1, 15.12 *}
schirmer@12854
   391
  Acc:	"\<lbrakk>E,dt\<Turnstile>va\<Colon>=T\<rbrakk> \<Longrightarrow>
schirmer@12854
   392
					 E,dt\<Turnstile>Acc va\<Colon>-T"
schirmer@12854
   393
schirmer@13688
   394
  --{* cf. 15.25, 15.25.1 *}
schirmer@12854
   395
  Ass:	"\<lbrakk>E,dt\<Turnstile>va\<Colon>=T; va \<noteq> LVar This;
schirmer@12854
   396
	  E,dt\<Turnstile>v \<Colon>-T';
schirmer@12854
   397
	  prg E\<turnstile>T'\<preceq>T\<rbrakk> \<Longrightarrow>
schirmer@12854
   398
					 E,dt\<Turnstile>va:=v\<Colon>-T'"
schirmer@12854
   399
schirmer@13688
   400
  --{* cf. 15.24 *}
schirmer@12854
   401
  Cond:	"\<lbrakk>E,dt\<Turnstile>e0\<Colon>-PrimT Boolean;
schirmer@12854
   402
	  E,dt\<Turnstile>e1\<Colon>-T1; E,dt\<Turnstile>e2\<Colon>-T2;
schirmer@12854
   403
	  prg E\<turnstile>T1\<preceq>T2 \<and> T = T2  \<or>  prg E\<turnstile>T2\<preceq>T1 \<and> T = T1\<rbrakk> \<Longrightarrow>
schirmer@12854
   404
					 E,dt\<Turnstile>e0 ? e1 : e2\<Colon>-T"
schirmer@12854
   405
schirmer@13688
   406
  --{* cf. 15.11.1, 15.11.2, 15.11.3 *}
schirmer@12854
   407
  Call:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-RefT statT;
schirmer@12854
   408
	  E,dt\<Turnstile>ps\<Colon>\<doteq>pTs;
schirmer@12854
   409
	  max_spec (prg E) (cls E) statT \<lparr>name=mn,parTs=pTs\<rparr> 
schirmer@12854
   410
            = {((statDeclT,m),pTs')}
schirmer@12854
   411
         \<rbrakk> \<Longrightarrow>
schirmer@12925
   412
		   E,dt\<Turnstile>{cls E,statT,invmode m e}e\<cdot>mn({pTs'}ps)\<Colon>-(resTy m)"
schirmer@12854
   413
schirmer@12854
   414
  Methd: "\<lbrakk>is_class (prg E) C;
schirmer@12854
   415
	  methd (prg E) C sig = Some m;
schirmer@12854
   416
	  E,dt\<Turnstile>Body (declclass m) (stmt (mbody (mthd m)))\<Colon>-T\<rbrakk> \<Longrightarrow>
schirmer@12854
   417
					 E,dt\<Turnstile>Methd C sig\<Colon>-T"
schirmer@13688
   418
 --{* The class @{term C} is the dynamic class of the method call 
schirmer@13688
   419
    (cf. Eval.thy). 
schirmer@13688
   420
    It hasn't got to be directly accessible from the current package 
schirmer@13688
   421
    @{term "(pkg E)"}. 
schirmer@13688
   422
    Only the static class must be accessible (enshured indirectly by 
schirmer@13688
   423
    @{term Call}). 
schirmer@13688
   424
    Note that l is just a dummy value. It is only used in the smallstep 
schirmer@13688
   425
    semantics. To proof typesafety directly for the smallstep semantics 
schirmer@13688
   426
    we would have to assume conformance of l here!
schirmer@13688
   427
  *}
schirmer@12854
   428
schirmer@12854
   429
  Body:	"\<lbrakk>is_class (prg E) D;
schirmer@12854
   430
	  E,dt\<Turnstile>blk\<Colon>\<surd>;
schirmer@12854
   431
	  (lcl E) Result = Some T;
schirmer@12854
   432
          is_type (prg E) T\<rbrakk> \<Longrightarrow>
schirmer@12854
   433
   					 E,dt\<Turnstile>Body D blk\<Colon>-T"
schirmer@13688
   434
--{* The class @{term D} implementing the method must not directly be 
schirmer@13688
   435
     accessible  from the current package @{term "(pkg E)"}, but can also 
schirmer@13688
   436
     be indirectly accessible due to inheritance (enshured in @{term Call})
schirmer@13688
   437
    The result type hasn't got to be accessible in Java! (If it is not 
schirmer@13688
   438
    accessible you can only assign it to Object).
schirmer@13688
   439
    For dummy value l see rule @{term Methd}. 
schirmer@13688
   440
   *}
schirmer@12854
   441
schirmer@13688
   442
--{* well-typed variables *}
schirmer@12854
   443
schirmer@13688
   444
  --{* cf. 15.13.1 *}
schirmer@12854
   445
  LVar:	"\<lbrakk>lcl E vn = Some T; is_acc_type (prg E) (pkg E) T\<rbrakk> \<Longrightarrow>
schirmer@12854
   446
					 E,dt\<Turnstile>LVar vn\<Colon>=T"
schirmer@13688
   447
  --{* cf. 15.10.1 *}
schirmer@12854
   448
  FVar:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-Class C; 
schirmer@12925
   449
	  accfield (prg E) (cls E) C fn = Some (statDeclC,f)\<rbrakk> \<Longrightarrow>
schirmer@12925
   450
			 E,dt\<Turnstile>{cls E,statDeclC,is_static f}e..fn\<Colon>=(type f)"
schirmer@13688
   451
  --{* cf. 15.12 *}
schirmer@12854
   452
  AVar:	"\<lbrakk>E,dt\<Turnstile>e\<Colon>-T.[]; 
schirmer@12854
   453
	  E,dt\<Turnstile>i\<Colon>-PrimT Integer\<rbrakk> \<Longrightarrow>
schirmer@12854
   454
					 E,dt\<Turnstile>e.[i]\<Colon>=T"
schirmer@12854
   455
schirmer@12854
   456
schirmer@13688
   457
--{* well-typed expression lists *}
schirmer@12854
   458
schirmer@13688
   459
  --{* cf. 15.11.??? *}
schirmer@12854
   460
  Nil:					"E,dt\<Turnstile>[]\<Colon>\<doteq>[]"
schirmer@12854
   461
schirmer@13688
   462
  --{* cf. 15.11.??? *}
schirmer@12854
   463
  Cons:	"\<lbrakk>E,dt\<Turnstile>e \<Colon>-T;
schirmer@12854
   464
	  E,dt\<Turnstile>es\<Colon>\<doteq>Ts\<rbrakk> \<Longrightarrow>
schirmer@12854
   465
					 E,dt\<Turnstile>e#es\<Colon>\<doteq>T#Ts"
schirmer@12854
   466
schirmer@12854
   467
schirmer@12854
   468
declare not_None_eq [simp del] 
schirmer@12854
   469
declare split_if [split del] split_if_asm [split del]
schirmer@12854
   470
declare split_paired_All [simp del] split_paired_Ex [simp del]
schirmer@12854
   471
ML_setup {*
schirmer@12854
   472
simpset_ref() := simpset() delloop "split_all_tac"
schirmer@12854
   473
*}
schirmer@13688
   474
schirmer@13688
   475
inductive_cases wt_elim_cases [cases set]:
schirmer@12854
   476
	"E,dt\<Turnstile>In2  (LVar vn)               \<Colon>T"
schirmer@12925
   477
	"E,dt\<Turnstile>In2  ({accC,statDeclC,s}e..fn)\<Colon>T"
schirmer@12854
   478
	"E,dt\<Turnstile>In2  (e.[i])                 \<Colon>T"
schirmer@12854
   479
	"E,dt\<Turnstile>In1l (NewC C)                \<Colon>T"
schirmer@12854
   480
	"E,dt\<Turnstile>In1l (New T'[i])             \<Colon>T"
schirmer@12854
   481
	"E,dt\<Turnstile>In1l (Cast T' e)             \<Colon>T"
schirmer@12854
   482
	"E,dt\<Turnstile>In1l (e InstOf T')           \<Colon>T"
schirmer@12854
   483
	"E,dt\<Turnstile>In1l (Lit x)                 \<Colon>T"
schirmer@13337
   484
        "E,dt\<Turnstile>In1l (UnOp unop e)           \<Colon>T"
schirmer@13337
   485
        "E,dt\<Turnstile>In1l (BinOp binop e1 e2)     \<Colon>T"
schirmer@12854
   486
	"E,dt\<Turnstile>In1l (Super)                 \<Colon>T"
schirmer@12854
   487
	"E,dt\<Turnstile>In1l (Acc va)                \<Colon>T"
schirmer@12854
   488
	"E,dt\<Turnstile>In1l (Ass va v)              \<Colon>T"
schirmer@12854
   489
	"E,dt\<Turnstile>In1l (e0 ? e1 : e2)          \<Colon>T"
schirmer@12925
   490
	"E,dt\<Turnstile>In1l ({accC,statT,mode}e\<cdot>mn({pT'}p))\<Colon>T"
schirmer@12854
   491
	"E,dt\<Turnstile>In1l (Methd C sig)           \<Colon>T"
schirmer@12854
   492
	"E,dt\<Turnstile>In1l (Body D blk)            \<Colon>T"
schirmer@12854
   493
	"E,dt\<Turnstile>In3  ([])                    \<Colon>Ts"
schirmer@12854
   494
	"E,dt\<Turnstile>In3  (e#es)                  \<Colon>Ts"
schirmer@12854
   495
	"E,dt\<Turnstile>In1r  Skip                   \<Colon>x"
schirmer@12854
   496
	"E,dt\<Turnstile>In1r (Expr e)                \<Colon>x"
schirmer@12854
   497
	"E,dt\<Turnstile>In1r (c1;; c2)               \<Colon>x"
schirmer@12854
   498
        "E,dt\<Turnstile>In1r (l\<bullet> c)                  \<Colon>x" 
schirmer@12854
   499
	"E,dt\<Turnstile>In1r (If(e) c1 Else c2)      \<Colon>x"
schirmer@12854
   500
	"E,dt\<Turnstile>In1r (l\<bullet> While(e) c)         \<Colon>x"
schirmer@13688
   501
        "E,dt\<Turnstile>In1r (Jmp jump)              \<Colon>x"
schirmer@12854
   502
	"E,dt\<Turnstile>In1r (Throw e)               \<Colon>x"
schirmer@12854
   503
	"E,dt\<Turnstile>In1r (Try c1 Catch(tn vn) c2)\<Colon>x"
schirmer@12854
   504
	"E,dt\<Turnstile>In1r (c1 Finally c2)         \<Colon>x"
schirmer@12854
   505
	"E,dt\<Turnstile>In1r (Init C)                \<Colon>x"
schirmer@12854
   506
declare not_None_eq [simp] 
schirmer@12854
   507
declare split_if [split] split_if_asm [split]
schirmer@12854
   508
declare split_paired_All [simp] split_paired_Ex [simp]
schirmer@12854
   509
ML_setup {*
schirmer@12854
   510
simpset_ref() := simpset() addloop ("split_all_tac", split_all_tac)
schirmer@12854
   511
*}
schirmer@12854
   512
schirmer@12854
   513
lemma is_acc_class_is_accessible: 
schirmer@12854
   514
  "is_acc_class G P C \<Longrightarrow> G\<turnstile>(Class C) accessible_in P"
schirmer@12854
   515
by (auto simp add: is_acc_class_def)
schirmer@12854
   516
schirmer@12854
   517
lemma is_acc_iface_is_iface: "is_acc_iface G P I \<Longrightarrow> is_iface G I"
schirmer@12854
   518
by (auto simp add: is_acc_iface_def)
schirmer@12854
   519
wenzelm@13524
   520
lemma is_acc_iface_Iface_is_accessible: 
schirmer@12854
   521
  "is_acc_iface G P I \<Longrightarrow> G\<turnstile>(Iface I) accessible_in P"
schirmer@12854
   522
by (auto simp add: is_acc_iface_def)
schirmer@12854
   523
schirmer@12854
   524
lemma is_acc_type_is_type: "is_acc_type G P T \<Longrightarrow> is_type G T"
schirmer@12854
   525
by (auto simp add: is_acc_type_def)
schirmer@12854
   526
wenzelm@13524
   527
lemma is_acc_iface_is_accessible:
wenzelm@13524
   528
  "is_acc_type G P T \<Longrightarrow> G\<turnstile>T accessible_in P"
schirmer@12854
   529
by (auto simp add: is_acc_type_def)
schirmer@12854
   530
schirmer@12854
   531
lemma wt_Methd_is_methd: 
schirmer@12854
   532
  "E\<turnstile>In1l (Methd C sig)\<Colon>T \<Longrightarrow> is_methd (prg E) C sig"
schirmer@12854
   533
apply (erule_tac wt_elim_cases)
schirmer@12854
   534
apply clarsimp
schirmer@12854
   535
apply (erule is_methdI, assumption)
schirmer@12854
   536
done
schirmer@12854
   537
schirmer@13688
   538
schirmer@13688
   539
text {* Special versions of some typing rules, better suited to pattern 
schirmer@13688
   540
        match the conclusion (no selectors in the conclusion)
schirmer@13688
   541
*}
schirmer@12854
   542
schirmer@12854
   543
lemma wt_Call: 
schirmer@12854
   544
"\<lbrakk>E,dt\<Turnstile>e\<Colon>-RefT statT; E,dt\<Turnstile>ps\<Colon>\<doteq>pTs;  
schirmer@12854
   545
  max_spec (prg E) (cls E) statT \<lparr>name=mn,parTs=pTs\<rparr> 
schirmer@12925
   546
    = {((statDeclC,m),pTs')};rT=(resTy m);accC=cls E;
schirmer@12925
   547
 mode = invmode m e\<rbrakk> \<Longrightarrow> E,dt\<Turnstile>{accC,statT,mode}e\<cdot>mn({pTs'}ps)\<Colon>-rT"
schirmer@12854
   548
by (auto elim: wt.Call)
schirmer@12854
   549
schirmer@12854
   550
schirmer@12854
   551
lemma invocationTypeExpr_noClassD: 
schirmer@12854
   552
"\<lbrakk> E\<turnstile>e\<Colon>-RefT statT\<rbrakk>
schirmer@12854
   553
 \<Longrightarrow> (\<forall> statC. statT \<noteq> ClassT statC) \<longrightarrow> invmode m e \<noteq> SuperM"
schirmer@12854
   554
proof -
schirmer@12854
   555
  assume wt: "E\<turnstile>e\<Colon>-RefT statT"
schirmer@12854
   556
  show ?thesis
schirmer@12854
   557
  proof (cases "e=Super")
schirmer@12854
   558
    case True
schirmer@12854
   559
    with wt obtain "C" where "statT = ClassT C" by (blast elim: wt_elim_cases)
schirmer@12854
   560
    then show ?thesis by blast
schirmer@12854
   561
  next 
schirmer@12854
   562
    case False then show ?thesis 
schirmer@12854
   563
      by (auto simp add: invmode_def split: split_if_asm)
schirmer@12854
   564
  qed
schirmer@12854
   565
qed
schirmer@12854
   566
schirmer@12854
   567
lemma wt_Super: 
schirmer@12854
   568
"\<lbrakk>lcl E This = Some (Class C); C \<noteq> Object; class (prg E) C = Some c; D=super c\<rbrakk> 
schirmer@12854
   569
\<Longrightarrow> E,dt\<Turnstile>Super\<Colon>-Class D"
schirmer@12854
   570
by (auto elim: wt.Super)
schirmer@12854
   571
schirmer@12925
   572
lemma wt_FVar:	
schirmer@12925
   573
"\<lbrakk>E,dt\<Turnstile>e\<Colon>-Class C; accfield (prg E) (cls E) C fn = Some (statDeclC,f);
schirmer@12925
   574
  sf=is_static f; fT=(type f); accC=cls E\<rbrakk> 
schirmer@12925
   575
\<Longrightarrow> E,dt\<Turnstile>{accC,statDeclC,sf}e..fn\<Colon>=fT"
schirmer@12925
   576
by (auto dest: wt.FVar)
schirmer@12854
   577
schirmer@12854
   578
schirmer@12854
   579
lemma wt_init [iff]: "E,dt\<Turnstile>Init C\<Colon>\<surd> = is_class (prg E) C"
schirmer@12854
   580
by (auto elim: wt_elim_cases intro: "wt.Init")
schirmer@12854
   581
schirmer@12854
   582
declare wt.Skip [iff]
schirmer@12854
   583
schirmer@12854
   584
lemma wt_StatRef: 
schirmer@12854
   585
  "is_acc_type (prg E) (pkg E) (RefT rt) \<Longrightarrow> E\<turnstile>StatRef rt\<Colon>-RefT rt"
schirmer@12854
   586
apply (rule wt.Cast)
schirmer@12854
   587
apply  (rule wt.Lit)
schirmer@12854
   588
apply   (simp (no_asm))
schirmer@12854
   589
apply  (simp (no_asm_simp))
schirmer@12854
   590
apply (rule cast.widen)
schirmer@12854
   591
apply (simp (no_asm))
schirmer@12854
   592
done
schirmer@12854
   593
schirmer@12854
   594
lemma wt_Inj_elim: 
schirmer@12854
   595
  "\<And>E. E,dt\<Turnstile>t\<Colon>U \<Longrightarrow> case t of 
schirmer@12854
   596
                       In1 ec \<Rightarrow> (case ec of 
schirmer@12854
   597
                                    Inl e \<Rightarrow> \<exists>T. U=Inl T  
schirmer@12854
   598
                                  | Inr s \<Rightarrow> U=Inl (PrimT Void))  
schirmer@12854
   599
                     | In2 e \<Rightarrow> (\<exists>T. U=Inl T) 
schirmer@12854
   600
                     | In3 e \<Rightarrow> (\<exists>T. U=Inr T)"
schirmer@12854
   601
apply (erule wt.induct)
schirmer@12854
   602
apply auto
schirmer@12854
   603
done
schirmer@12854
   604
schirmer@13688
   605
--{* In the special syntax to distinguish the typing judgements for expressions, 
schirmer@13688
   606
     statements, variables and expression lists the kind of term corresponds
schirmer@13688
   607
     to the kind of type in the end e.g. An statement (injection @{term In3} 
schirmer@13688
   608
    into terms, always has type void (injection @{term Inl} into the generalised
schirmer@13688
   609
    types. The following simplification procedures establish these kinds of
schirmer@13688
   610
    correlation. 
schirmer@13688
   611
 *}
schirmer@13688
   612
schirmer@12854
   613
ML {*
schirmer@12854
   614
fun wt_fun name inj rhs =
schirmer@12854
   615
let
schirmer@12854
   616
  val lhs = "E,dt\<Turnstile>" ^ inj ^ " t\<Colon>U"
schirmer@12854
   617
  val () = qed_goal name (the_context()) (lhs ^ " = (" ^ rhs ^ ")") 
schirmer@12854
   618
	(K [Auto_tac, ALLGOALS (ftac (thm "wt_Inj_elim")) THEN Auto_tac])
schirmer@12854
   619
  fun is_Inj (Const (inj,_) $ _) = true
schirmer@12854
   620
    | is_Inj _                   = false
schirmer@12854
   621
  fun pred (t as (_ $ (Const ("Pair",_) $
schirmer@12854
   622
     _ $ (Const ("Pair", _) $ _ $ (Const ("Pair", _) $ _ $
schirmer@12854
   623
       x))) $ _ )) = is_Inj x
schirmer@12854
   624
in
wenzelm@13462
   625
  cond_simproc name lhs pred (thm name)
schirmer@12854
   626
end
schirmer@12854
   627
schirmer@12854
   628
val wt_expr_proc  = wt_fun "wt_expr_eq"  "In1l" "\<exists>T.  U=Inl T  \<and> E,dt\<Turnstile>t\<Colon>-T"
schirmer@12854
   629
val wt_var_proc   = wt_fun "wt_var_eq"   "In2"  "\<exists>T.  U=Inl T  \<and> E,dt\<Turnstile>t\<Colon>=T"
schirmer@12854
   630
val wt_exprs_proc = wt_fun "wt_exprs_eq" "In3"  "\<exists>Ts. U=Inr Ts \<and> E,dt\<Turnstile>t\<Colon>\<doteq>Ts"
schirmer@12854
   631
val wt_stmt_proc  = wt_fun "wt_stmt_eq"  "In1r" "U=Inl(PrimT Void)\<and>E,dt\<Turnstile>t\<Colon>\<surd>"
schirmer@12854
   632
*}
schirmer@12854
   633
schirmer@12854
   634
ML {*
schirmer@12854
   635
Addsimprocs [wt_expr_proc,wt_var_proc,wt_exprs_proc,wt_stmt_proc]
schirmer@12854
   636
*}
schirmer@12854
   637
schirmer@13688
   638
lemma wt_elim_BinOp:
schirmer@13688
   639
  "\<lbrakk>E,dt\<Turnstile>In1l (BinOp binop e1 e2)\<Colon>T;
schirmer@13688
   640
    \<And>T1 T2 T3.
schirmer@13688
   641
       \<lbrakk>E,dt\<Turnstile>e1\<Colon>-T1; E,dt\<Turnstile>e2\<Colon>-T2; wt_binop (prg E) binop T1 T2;
schirmer@13688
   642
          E,dt\<Turnstile>(if b then In1l e2 else In1r Skip)\<Colon>T3;
schirmer@13688
   643
          T = Inl (PrimT (binop_type binop))\<rbrakk>
schirmer@13688
   644
       \<Longrightarrow> P\<rbrakk>
schirmer@13688
   645
\<Longrightarrow> P"
schirmer@13688
   646
apply (erule wt_elim_cases)
schirmer@13688
   647
apply (cases b)
schirmer@13688
   648
apply auto
schirmer@13688
   649
done
schirmer@13688
   650
schirmer@12854
   651
lemma Inj_eq_lemma [simp]: 
schirmer@12854
   652
  "(\<forall>T. (\<exists>T'. T = Inj T' \<and> P T') \<longrightarrow> Q T) = (\<forall>T'. P T' \<longrightarrow> Q (Inj T'))"
schirmer@12854
   653
by auto
schirmer@12854
   654
schirmer@12854
   655
(* unused *)
schirmer@12854
   656
lemma single_valued_tys_lemma [rule_format (no_asm)]: 
schirmer@12854
   657
  "\<forall>S T. G\<turnstile>S\<preceq>T \<longrightarrow> G\<turnstile>T\<preceq>S \<longrightarrow> S = T \<Longrightarrow> E,dt\<Turnstile>t\<Colon>T \<Longrightarrow>  
schirmer@12854
   658
     G = prg E \<longrightarrow> (\<forall>T'. E,dt\<Turnstile>t\<Colon>T' \<longrightarrow> T  = T')"
schirmer@12854
   659
apply (cases "E", erule wt.induct)
schirmer@12854
   660
apply (safe del: disjE)
schirmer@12854
   661
apply (simp_all (no_asm_use) split del: split_if_asm)
schirmer@12854
   662
apply (safe del: disjE)
schirmer@12854
   663
(* 17 subgoals *)
schirmer@13337
   664
apply (tactic {* ALLGOALS (fn i => if i = 11 then EVERY'[thin_tac "?E,dt\<Turnstile>e0\<Colon>-PrimT Boolean", thin_tac "?E,dt\<Turnstile>e1\<Colon>-?T1", thin_tac "?E,dt\<Turnstile>e2\<Colon>-?T2"] i else thin_tac "All ?P" i) *})
schirmer@12854
   665
(*apply (safe del: disjE elim!: wt_elim_cases)*)
schirmer@12854
   666
apply (tactic {*ALLGOALS (eresolve_tac (thms "wt_elim_cases"))*})
schirmer@12854
   667
apply (simp_all (no_asm_use) split del: split_if_asm)
schirmer@13337
   668
apply (erule_tac [12] V = "All ?P" in thin_rl) (* Call *)
schirmer@12854
   669
apply ((blast del: equalityCE dest: sym [THEN trans])+)
schirmer@12854
   670
done
schirmer@12854
   671
schirmer@12854
   672
(* unused *)
schirmer@12854
   673
lemma single_valued_tys: 
schirmer@12854
   674
"ws_prog (prg E) \<Longrightarrow> single_valued {(t,T). E,dt\<Turnstile>t\<Colon>T}"
schirmer@12854
   675
apply (unfold single_valued_def)
schirmer@12854
   676
apply clarsimp
schirmer@12854
   677
apply (rule single_valued_tys_lemma)
schirmer@12854
   678
apply (auto intro!: widen_antisym)
schirmer@12854
   679
done
schirmer@12854
   680
schirmer@12854
   681
lemma typeof_empty_is_type [rule_format (no_asm)]: 
schirmer@12854
   682
  "typeof (\<lambda>a. None) v = Some T \<longrightarrow> is_type G T"
schirmer@12854
   683
apply (rule val.induct)
schirmer@12854
   684
apply    	auto
schirmer@12854
   685
done
schirmer@12854
   686
schirmer@12854
   687
(* unused *)
schirmer@12854
   688
lemma typeof_is_type [rule_format (no_asm)]: 
schirmer@12854
   689
 "(\<forall>a. v \<noteq> Addr a) \<longrightarrow> (\<exists>T. typeof dt v = Some T \<and> is_type G T)"
schirmer@12854
   690
apply (rule val.induct)
schirmer@12854
   691
prefer 5 
schirmer@12854
   692
apply     fast
schirmer@12854
   693
apply  (simp_all (no_asm))
schirmer@12854
   694
done
schirmer@12854
   695
schirmer@12854
   696
end