src/HOL/Bali/Term.thy
author schirmer
Mon Jul 15 10:41:34 2002 +0200 (2002-07-15)
changeset 13358 c837ba4cfb62
parent 13345 bd611943cbc2
child 13384 a34e38154413
permissions -rw-r--r--
fix latex output
wenzelm@12857
     1
(*  Title:      HOL/Bali/Term.thy
schirmer@12854
     2
    ID:         $Id$
schirmer@12854
     3
    Author:     David von Oheimb
wenzelm@12858
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
schirmer@12854
     5
*)
schirmer@12854
     6
schirmer@12854
     7
header {* Java expressions and statements *}
schirmer@12854
     8
schirmer@13337
     9
theory Term = Value + Table:
schirmer@12854
    10
schirmer@12854
    11
text {*
schirmer@12854
    12
design issues:
schirmer@12854
    13
\begin{itemize}
schirmer@12854
    14
\item invocation frames for local variables could be reduced to special static
schirmer@12854
    15
  objects (one per method). This would reduce redundancy, but yield a rather
schirmer@12854
    16
  non-standard execution model more difficult to understand.
schirmer@12854
    17
\item method bodies separated from calls to handle assumptions in axiomat. 
schirmer@12854
    18
  semantics
schirmer@12854
    19
  NB: Body is intended to be in the environment of the called method.
schirmer@12854
    20
\item class initialization is regarded as (auxiliary) statement 
schirmer@12854
    21
      (required for AxSem)
schirmer@12854
    22
\item result expression of method return is handled by a special result variable
schirmer@12854
    23
  result variable is treated uniformly with local variables
schirmer@12854
    24
  \begin{itemize}
schirmer@12854
    25
  \item[+] welltypedness and existence of the result/return expression is 
schirmer@12854
    26
           ensured without extra efford
schirmer@12854
    27
  \end{itemize}
schirmer@12854
    28
\end{itemize}
schirmer@12854
    29
schirmer@12854
    30
simplifications:
schirmer@12854
    31
\begin{itemize}
schirmer@12854
    32
\item expression statement allowed for any expression
schirmer@12854
    33
\item no unary, binary, etc, operators
schirmer@12854
    34
\item This  is modeled as a special non-assignable local variable
schirmer@12854
    35
\item Super is modeled as a general expression with the same value as This
schirmer@12854
    36
\item access to field x in current class via This.x
schirmer@12854
    37
\item NewA creates only one-dimensional arrays;
schirmer@12854
    38
  initialization of further subarrays may be simulated with nested NewAs
schirmer@12854
    39
\item The 'Lit' constructor is allowed to contain a reference value.
schirmer@12854
    40
  But this is assumed to be prohibited in the input language, which is enforced
schirmer@12854
    41
  by the type-checking rules.
schirmer@12854
    42
\item a call of a static method via a type name may be simulated by a dummy 
schirmer@12854
    43
      variable
schirmer@12854
    44
\item no nested blocks with inner local variables
schirmer@12854
    45
\item no synchronized statements
schirmer@12854
    46
\item no secondary forms of if, while (e.g. no for) (may be easily simulated)
schirmer@12854
    47
\item no switch (may be simulated with if)
schirmer@12854
    48
\item the @{text try_catch_finally} statement is divided into the 
schirmer@12854
    49
      @{text try_catch} statement 
schirmer@12854
    50
      and a finally statement, which may be considered as try..finally with 
schirmer@12854
    51
      empty catch
schirmer@12854
    52
\item the @{text try_catch} statement has exactly one catch clause; 
schirmer@12854
    53
      multiple ones can be
schirmer@12854
    54
  simulated with instanceof
schirmer@12854
    55
\item the compiler is supposed to add the annotations {@{text _}} during 
schirmer@12854
    56
      type-checking. This
schirmer@12854
    57
  transformation is left out as its result is checked by the type rules anyway
schirmer@12854
    58
\end{itemize}
schirmer@12854
    59
*}
schirmer@12854
    60
schirmer@13337
    61
schirmer@13337
    62
schirmer@13337
    63
types locals = "(lname, val) table"  (* local variables *)
schirmer@13337
    64
schirmer@13337
    65
schirmer@13337
    66
datatype jump
schirmer@13337
    67
        = Break label (* break *)
schirmer@13337
    68
        | Cont label  (* continue *)
schirmer@13337
    69
        | Ret         (* return from method *)
schirmer@13337
    70
schirmer@13337
    71
datatype xcpt        (* exception *)
schirmer@13337
    72
	= Loc loc    (* location of allocated execption object *)
schirmer@13337
    73
	| Std xname  (* intermediate standard exception, see Eval.thy *)
schirmer@13337
    74
schirmer@13337
    75
datatype error
schirmer@13337
    76
       = AccessViolation (* Access to a member that isn't permitted *)
schirmer@13337
    77
schirmer@13337
    78
datatype abrupt      (* abrupt completion *) 
schirmer@13337
    79
        = Xcpt xcpt  (* exception *)
schirmer@13337
    80
        | Jump jump  (* break, continue, return *)
schirmer@13337
    81
        | Error error (* runtime errors, we wan't to detect and proof absent
schirmer@13337
    82
                         in welltyped programms *)
schirmer@13337
    83
types
schirmer@13337
    84
  abopt  = "abrupt option"
schirmer@13337
    85
schirmer@13337
    86
text {* Local variable store and exception. 
schirmer@13337
    87
Anticipation of State.thy used by smallstep semantics. For a method call, 
schirmer@13337
    88
we save the local variables of the caller in the term Callee to restore them 
schirmer@13337
    89
after method return. Also an exception must be restored after the finally
schirmer@13337
    90
statement *}
schirmer@13337
    91
schirmer@13337
    92
translations
schirmer@13337
    93
 "locals" <= (type) "(lname, val) table"
schirmer@13337
    94
schirmer@12854
    95
datatype inv_mode                  (* invocation mode for method calls *)
schirmer@12854
    96
	= Static                   (* static *)
schirmer@12854
    97
	| SuperM                   (* super  *)
schirmer@12854
    98
	| IntVir                   (* interface or virtual *)
schirmer@12854
    99
schirmer@12854
   100
record  sig =            (* signature of a method, cf. 8.4.2  *)
schirmer@12854
   101
	  name ::"mname"      (* acutally belongs to Decl.thy *)
schirmer@12854
   102
          parTs::"ty list"        
schirmer@12854
   103
schirmer@12854
   104
translations
schirmer@12854
   105
  "sig" <= (type) "\<lparr>name::mname,parTs::ty list\<rparr>"
schirmer@12854
   106
  "sig" <= (type) "\<lparr>name::mname,parTs::ty list,\<dots>::'a\<rparr>"
schirmer@12854
   107
schirmer@13337
   108
-- "function codes for unary operations"
schirmer@13358
   109
datatype unop =  UPlus    
schirmer@13358
   110
               | UMinus   
schirmer@13358
   111
               | UBitNot  
schirmer@13358
   112
               | UNot     
schirmer@13337
   113
schirmer@13337
   114
-- "function codes for binary operations"
schirmer@13358
   115
datatype binop = Mul     
schirmer@13358
   116
               | Div     
schirmer@13358
   117
               | Mod     
schirmer@13358
   118
               | Plus    
schirmer@13358
   119
               | Minus   
schirmer@13358
   120
               | LShift  
schirmer@13358
   121
               | RShift  
schirmer@13358
   122
               | RShiftU 
schirmer@13358
   123
               | Less    
schirmer@13358
   124
               | Le      
schirmer@13358
   125
               | Greater 
schirmer@13358
   126
               | Ge      
schirmer@13358
   127
               | Eq      
schirmer@13358
   128
               | Neq     
schirmer@13358
   129
               | BitAnd  
schirmer@13358
   130
               | And     
schirmer@13358
   131
               | BitXor  
schirmer@13358
   132
               | Xor     
schirmer@13358
   133
               | BitOr   
schirmer@13358
   134
               | Or      
schirmer@13358
   135
schirmer@12854
   136
datatype var
schirmer@12854
   137
	= LVar                  lname(* local variable (incl. parameters) *)
schirmer@12925
   138
        | FVar qtname qtname bool expr vname
schirmer@12925
   139
                                (*class field*)("{_,_,_}_.._"[10,10,10,85,99]90)
schirmer@13337
   140
	| AVar expr expr    (* array component *) ("_.[_]"[90,10   ]90)
schirmer@13337
   141
        | InsInitV stmt var (* insertion of initialization before
schirmer@13337
   142
                                   evaluation of var (for smallstep sem.) *)
schirmer@12854
   143
schirmer@12854
   144
and expr
schirmer@13337
   145
	= NewC qtname         (* class instance creation *)
schirmer@13337
   146
	| NewA ty expr        (* array creation *) ("New _[_]"[99,10   ]85)
schirmer@13337
   147
	| Cast ty expr        (* type cast  *)
schirmer@13337
   148
	| Inst expr ref_ty    (* instanceof *)     ("_ InstOf _"[85,99] 85)
schirmer@13337
   149
	| Lit  val            (* literal value, references not allowed *)
schirmer@13337
   150
        | UnOp unop expr        (* unary operation *)
schirmer@13337
   151
        | BinOp binop expr expr (* binary operation *)
schirmer@13337
   152
        
schirmer@13337
   153
	| Super               (* special Super keyword *)
schirmer@13337
   154
	| Acc  var            (* variable access *)
schirmer@13337
   155
	| Ass  var expr       (* variable assign *) ("_:=_"   [90,85   ]85)
schirmer@13337
   156
	| Cond expr expr expr (* conditional *)  ("_ ? _ : _" [85,85,80]80)
schirmer@13337
   157
        | Call qtname ref_ty inv_mode expr mname "(ty list)" 
schirmer@13337
   158
               "(expr list)"  (* method call *) 
schirmer@13337
   159
                              ("{_,_,_}_\<cdot>_'( {_}_')"[10,10,10,85,99,10,10]85)
schirmer@13337
   160
        | Methd qtname sig    (*   (folded) method (see below) *)
schirmer@13337
   161
        | Body qtname stmt    (* (unfolded) method body *)
schirmer@13337
   162
        | InsInitE stmt expr      (* insertion of initialization before
schirmer@13337
   163
                                   evaluation of expr (for smallstep sem.) *)
schirmer@13337
   164
        | Callee locals expr      (* save Callers locals in Callee-Frame
schirmer@13337
   165
                                     (for smallstep semantics) *)
schirmer@12854
   166
and  stmt
schirmer@13337
   167
	= Skip                  (* empty      statement *)
schirmer@13337
   168
	| Expr  expr            (* expression statement *)
schirmer@13337
   169
        | Lab   jump stmt       ("_\<bullet> _"(* labeled statement*)[      99,66]66)
schirmer@13337
   170
                                (* handles break *)
schirmer@13337
   171
	| Comp  stmt stmt       ("_;; _"                  [      66,65]65)
schirmer@13337
   172
	| If_   expr stmt stmt  ("If'(_') _ Else _"          [   80,79,79]70)
schirmer@13337
   173
	| Loop  label expr stmt ("_\<bullet> While'(_') _"           [   99,80,79]70)
schirmer@13337
   174
        | Do jump               (* break, continue, return *)
schirmer@12854
   175
	| Throw expr
schirmer@12854
   176
        | TryC  stmt
schirmer@13337
   177
	     qtname vname stmt ("Try _ Catch'(_ _') _"     [79,99,80,79]70)
schirmer@13337
   178
	| Fin  stmt  stmt        ("_ Finally _"               [      79,79]70)
schirmer@13337
   179
        | FinA abopt stmt        (* Save abruption of first statement 
schirmer@13337
   180
                                    (for smallstep sem.) *)
schirmer@12854
   181
	| Init  qtname              (* class initialization *)
schirmer@12854
   182
schirmer@12854
   183
schirmer@12854
   184
text {*
schirmer@12854
   185
The expressions Methd and Body are artificial program constructs, in the
schirmer@12854
   186
sense that they are not used to define a concrete Bali program. In the 
schirmer@13337
   187
operational semantic's they are "generated on the fly" 
schirmer@13337
   188
to decompose the task to define the behaviour of the Call expression. 
schirmer@13337
   189
They are crucial for the axiomatic semantics to give a syntactic hook to insert 
schirmer@13337
   190
some assertions (cf. AxSem.thy, Eval.thy). 
schirmer@13337
   191
The Init statement (to initialize a class on its first use) is 
schirmer@13337
   192
inserted in various places by the semantics. 
schirmer@13337
   193
Callee, InsInitV, InsInitE,FinA are only needed as intermediate steps in the
schirmer@13337
   194
smallstep (transition) semantics (cf. Trans.thy). Callee is used to save the 
schirmer@13337
   195
local variables of the caller for method return. So ve avoid modelling a 
schirmer@13337
   196
frame stack.
schirmer@13337
   197
The InsInitV/E terms are only used by the smallstep semantics to model the
schirmer@13337
   198
intermediate steps of class-initialisation.
schirmer@12854
   199
*}
schirmer@12854
   200
 
schirmer@13337
   201
types "term" = "(expr+stmt,var,expr list) sum3"
schirmer@12854
   202
translations
schirmer@12854
   203
  "sig"   <= (type) "mname \<times> ty list"
schirmer@12854
   204
  "var"   <= (type) "Term.var"
schirmer@12854
   205
  "expr"  <= (type) "Term.expr"
schirmer@12854
   206
  "stmt"  <= (type) "Term.stmt"
schirmer@13337
   207
  "term"  <= (type) "(expr+stmt,var,expr list) sum3"
schirmer@12854
   208
schirmer@12854
   209
syntax
schirmer@12854
   210
  
schirmer@12854
   211
  this    :: expr
schirmer@13337
   212
  LAcc    :: "vname \<Rightarrow> expr" ("!!")
schirmer@13337
   213
  LAss    :: "vname \<Rightarrow> expr \<Rightarrow>stmt" ("_:==_" [90,85] 85)
schirmer@12854
   214
  Return  :: "expr \<Rightarrow> stmt"
schirmer@12854
   215
  StatRef :: "ref_ty \<Rightarrow> expr"
schirmer@12854
   216
schirmer@12854
   217
translations
schirmer@12854
   218
  
schirmer@12854
   219
 "this"       == "Acc (LVar This)"
schirmer@12854
   220
 "!!v"        == "Acc (LVar (EName (VNam v)))"
schirmer@12854
   221
 "v:==e"      == "Expr (Ass (LVar (EName (VNam  v))) e)"
schirmer@12854
   222
 "Return e"   == "Expr (Ass (LVar (EName Res)) e);; Do Ret" 
schirmer@12854
   223
                                                   (* Res := e;; Do Ret *)
schirmer@12854
   224
 "StatRef rt" == "Cast (RefT rt) (Lit Null)"
schirmer@12854
   225
  
schirmer@12854
   226
constdefs
schirmer@12854
   227
schirmer@12854
   228
  is_stmt :: "term \<Rightarrow> bool"
schirmer@12854
   229
 "is_stmt t \<equiv> \<exists>c. t=In1r c"
schirmer@12854
   230
schirmer@12854
   231
ML {*
schirmer@12854
   232
bind_thms ("is_stmt_rews", sum3_instantiate (thm "is_stmt_def"));
schirmer@12854
   233
*}
schirmer@12854
   234
schirmer@12854
   235
declare is_stmt_rews [simp]
schirmer@13345
   236
schirmer@13345
   237
axclass inj_term < "type"
schirmer@13345
   238
consts inj_term:: "'a::inj_term \<Rightarrow> term" ("\<langle>_\<rangle>" 83)
schirmer@13345
   239
schirmer@13345
   240
instance stmt::inj_term ..
schirmer@13345
   241
schirmer@13345
   242
defs (overloaded)
schirmer@13345
   243
stmt_inj_term_def: "\<langle>c::stmt\<rangle> \<equiv> In1r c"
schirmer@13345
   244
schirmer@13345
   245
lemma stmt_inj_term_simp: "\<langle>c::stmt\<rangle> = In1r c"
schirmer@13345
   246
by (simp add: stmt_inj_term_def)
schirmer@13345
   247
schirmer@13345
   248
instance expr::inj_term ..
schirmer@13345
   249
schirmer@13345
   250
defs (overloaded)
schirmer@13345
   251
expr_inj_term_def: "\<langle>e::expr\<rangle> \<equiv> In1l e"
schirmer@13345
   252
schirmer@13345
   253
lemma expr_inj_term_simp: "\<langle>e::expr\<rangle> = In1l e"
schirmer@13345
   254
by (simp add: expr_inj_term_def)
schirmer@13345
   255
schirmer@13345
   256
instance var::inj_term ..
schirmer@13345
   257
schirmer@13345
   258
defs (overloaded)
schirmer@13345
   259
var_inj_term_def: "\<langle>v::var\<rangle> \<equiv> In2 v"
schirmer@13345
   260
schirmer@13345
   261
lemma var_inj_term_simp: "\<langle>v::var\<rangle> = In2 v"
schirmer@13345
   262
by (simp add: var_inj_term_def)
schirmer@13345
   263
schirmer@13345
   264
instance "list":: (type) inj_term ..
schirmer@13345
   265
schirmer@13345
   266
defs (overloaded)
schirmer@13345
   267
expr_list_inj_term_def: "\<langle>es::expr list\<rangle> \<equiv> In3 es"
schirmer@13345
   268
schirmer@13345
   269
lemma expr_list_inj_term_simp: "\<langle>es::expr list\<rangle> = In3 es"
schirmer@13345
   270
by (simp add: expr_list_inj_term_def)
schirmer@13345
   271
schirmer@12854
   272
end