src/HOL/Bali/Term.thy
author wenzelm
Mon, 25 Feb 2002 20:48:14 +0100
changeset 12937 0c4fd7529467
parent 12925 99131847fb93
child 13337 f75dfc606ac7
permissions -rw-r--r--
clarified syntax of ``long'' statements: fixes/assumes/shows;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12857
a4386cc9b1c3 tuned header;
wenzelm
parents: 12854
diff changeset
     1
(*  Title:      HOL/Bali/Term.thy
12854
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     2
    ID:         $Id$
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     3
    Author:     David von Oheimb
12858
wenzelm
parents: 12857
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
12854
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     5
*)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     6
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     7
header {* Java expressions and statements *}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     8
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
     9
theory Term = Value:
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    10
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    11
text {*
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    12
design issues:
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    13
\begin{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    14
\item invocation frames for local variables could be reduced to special static
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    15
  objects (one per method). This would reduce redundancy, but yield a rather
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    16
  non-standard execution model more difficult to understand.
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    17
\item method bodies separated from calls to handle assumptions in axiomat. 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    18
  semantics
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    19
  NB: Body is intended to be in the environment of the called method.
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    20
\item class initialization is regarded as (auxiliary) statement 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    21
      (required for AxSem)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    22
\item result expression of method return is handled by a special result variable
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    23
  result variable is treated uniformly with local variables
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    24
  \begin{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    25
  \item[+] welltypedness and existence of the result/return expression is 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    26
           ensured without extra efford
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    27
  \end{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    28
\end{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    29
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    30
simplifications:
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    31
\begin{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    32
\item expression statement allowed for any expression
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    33
\item no unary, binary, etc, operators
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    34
\item This  is modeled as a special non-assignable local variable
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    35
\item Super is modeled as a general expression with the same value as This
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    36
\item access to field x in current class via This.x
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    37
\item NewA creates only one-dimensional arrays;
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    38
  initialization of further subarrays may be simulated with nested NewAs
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    39
\item The 'Lit' constructor is allowed to contain a reference value.
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    40
  But this is assumed to be prohibited in the input language, which is enforced
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    41
  by the type-checking rules.
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    42
\item a call of a static method via a type name may be simulated by a dummy 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    43
      variable
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    44
\item no nested blocks with inner local variables
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    45
\item no synchronized statements
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    46
\item no secondary forms of if, while (e.g. no for) (may be easily simulated)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    47
\item no switch (may be simulated with if)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    48
\item the @{text try_catch_finally} statement is divided into the 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    49
      @{text try_catch} statement 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    50
      and a finally statement, which may be considered as try..finally with 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    51
      empty catch
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    52
\item the @{text try_catch} statement has exactly one catch clause; 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    53
      multiple ones can be
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    54
  simulated with instanceof
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    55
\item the compiler is supposed to add the annotations {@{text _}} during 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    56
      type-checking. This
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    57
  transformation is left out as its result is checked by the type rules anyway
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    58
\end{itemize}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    59
*}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    60
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    61
datatype inv_mode                  (* invocation mode for method calls *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    62
	= Static                   (* static *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    63
	| SuperM                   (* super  *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    64
	| IntVir                   (* interface or virtual *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    65
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    66
record  sig =            (* signature of a method, cf. 8.4.2  *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    67
	  name ::"mname"      (* acutally belongs to Decl.thy *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    68
          parTs::"ty list"        
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    69
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    70
translations
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    71
  "sig" <= (type) "\<lparr>name::mname,parTs::ty list\<rparr>"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    72
  "sig" <= (type) "\<lparr>name::mname,parTs::ty list,\<dots>::'a\<rparr>"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    73
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    74
datatype jump
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    75
        = Break label (* break *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    76
        | Cont label  (* continue *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    77
        | Ret         (* return from method *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    78
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    79
datatype var
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    80
	= LVar                  lname(* local variable (incl. parameters) *)
12925
99131847fb93 Added check for field/method access to operational semantics and proved the acesses valid.
schirmer
parents: 12858
diff changeset
    81
        | FVar qtname qtname bool expr vname
99131847fb93 Added check for field/method access to operational semantics and proved the acesses valid.
schirmer
parents: 12858
diff changeset
    82
                                (*class field*)("{_,_,_}_.._"[10,10,10,85,99]90)
12854
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    83
	| AVar        expr expr      (* array component *) ("_.[_]"[90,10   ]90)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    84
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    85
and expr
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    86
	= NewC qtname              (* class instance creation *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    87
	| NewA ty expr             (* array creation *) ("New _[_]"[99,10   ]85)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    88
	| Cast ty expr             (* type cast  *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    89
	| Inst expr ref_ty         (* instanceof *)     ("_ InstOf _"[85,99] 85)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    90
	| Lit  val                 (* literal value, references not allowed *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    91
	| Super                    (* special Super keyword *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    92
	| Acc  var                 (* variable access *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    93
	| Ass  var expr            (* variable assign *) ("_:=_"   [90,85   ]85)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    94
	| Cond expr expr expr      (* conditional *)  ("_ ? _ : _" [85,85,80]80)
12925
99131847fb93 Added check for field/method access to operational semantics and proved the acesses valid.
schirmer
parents: 12858
diff changeset
    95
        | Call qtname ref_ty inv_mode expr mname "(ty list)" (* method call *)
99131847fb93 Added check for field/method access to operational semantics and proved the acesses valid.
schirmer
parents: 12858
diff changeset
    96
                  "(expr list)" ("{_,_,_}_\<cdot>_'( {_}_')"[10,10,10,85,99,10,10]85)
12854
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    97
        | Methd qtname sig          (*   (folded) method (see below) *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    98
        | Body qtname stmt          (* (unfolded) method body *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
    99
and  stmt
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   100
	= Skip                     (* empty      statement *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   101
	| Expr  expr               (* expression statement *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   102
        | Lab   label stmt         ("_\<bullet> _"(* labeled statement*)[      99,66]66)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   103
                                   (* handles break *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   104
	| Comp  stmt stmt          ("_;; _"                     [      66,65]65)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   105
	| If_   expr stmt stmt     ("If'(_') _ Else _"          [   80,79,79]70)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   106
	| Loop  label expr stmt    ("_\<bullet> While'(_') _"           [   99,80,79]70)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   107
        | Do jump                  (* break, continue, return *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   108
	| Throw expr
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   109
        | TryC  stmt
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   110
	        qtname vname stmt   ("Try _ Catch'(_ _') _"     [79,99,80,79]70)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   111
	| Fin   stmt stmt          ("_ Finally _"               [      79,79]70)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   112
	| Init  qtname              (* class initialization *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   113
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   114
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   115
text {*
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   116
The expressions Methd and Body are artificial program constructs, in the
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   117
sense that they are not used to define a concrete Bali program. In the 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   118
evaluation semantic definition they are "generated on the fly" to decompose 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   119
the task to define the behaviour of the Call expression. They are crucial 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   120
for the axiomatic semantics to give a syntactic hook to insert 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   121
some assertions (cf. AxSem.thy, Eval.thy).
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   122
Also the Init statement (to initialize a class on its first use) is inserted 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   123
in various places by the evaluation semantics.   
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   124
*}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   125
 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   126
types "term" = "(expr+stmt, var, expr list) sum3"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   127
translations
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   128
  "sig"   <= (type) "mname \<times> ty list"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   129
  "var"   <= (type) "Term.var"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   130
  "expr"  <= (type) "Term.expr"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   131
  "stmt"  <= (type) "Term.stmt"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   132
  "term"  <= (type) "(expr+stmt, var, expr list) sum3"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   133
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   134
syntax
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   135
  
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   136
  this    :: expr
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   137
  LAcc    :: "vname \<Rightarrow>         expr" ("!!")
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   138
  LAss    :: "vname \<Rightarrow> expr \<Rightarrow> stmt" ("_:==_" [90,85] 85)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   139
  Return  :: "expr \<Rightarrow> stmt"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   140
  StatRef :: "ref_ty \<Rightarrow> expr"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   141
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   142
translations
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   143
  
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   144
 "this"       == "Acc (LVar This)"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   145
 "!!v"        == "Acc (LVar (EName (VNam v)))"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   146
 "v:==e"      == "Expr (Ass (LVar (EName (VNam  v))) e)"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   147
 "Return e"   == "Expr (Ass (LVar (EName Res)) e);; Do Ret" 
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   148
                                                   (* Res := e;; Do Ret *)
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   149
 "StatRef rt" == "Cast (RefT rt) (Lit Null)"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   150
  
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   151
constdefs
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   152
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   153
  is_stmt :: "term \<Rightarrow> bool"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   154
 "is_stmt t \<equiv> \<exists>c. t=In1r c"
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   155
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   156
ML {*
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   157
bind_thms ("is_stmt_rews", sum3_instantiate (thm "is_stmt_def"));
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   158
*}
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   159
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   160
declare is_stmt_rews [simp]
00d4a435777f Isabelle/Bali sources;
schirmer
parents:
diff changeset
   161
end