(* Title: HOL/Bali/Term.thy Author: David von Oheimb
*)
subsection \<open>Java expressions and statements\<close>
theoryTermimportsValue Table begin
text\<open>
design issues: \begin{itemize} \item invocation frames for local variables could be reduced to special static
objects (one per method). This would reduce redundancy, but yield a rather
non-standard execution model more difficult to understand. \item method bodies separated from calls to handle assumptions in axiomat.
semantics
NB: Body is intended to be in the environment of the called method. \item class initialization is regarded as (auxiliary) statement
(required for AxSem) \item result expression of method return is handled by a special result variable
result variable is treated uniformly withlocal variables \begin{itemize} \item[+] welltypedness and existence of the result/return expression is
ensured without extra efford \end{itemize} \end{itemize}
simplifications: \begin{itemize} \item expression statement allowed for any expression \item This is modeled as a special non-assignable local variable \item Super is modeled as a general expression with the same value as This \item access to field x in current class via This.x \item NewA creates only one-dimensional arrays;
initialization of further subarrays may be simulated with nested NewAs \item The 'Lit' constructor is allowed to contain a reference value.
But this is assumed to be prohibited in the input language, which is enforced by the type-checking rules. \item a call of a static method via a type name may be simulated by a dummy
variable \item no nested blocks with inner local variables \item no synchronized statements \item no secondary forms of if, while (e.g. no for) (may be easily simulated) \item no switch (may be simulated with if) \item the \<open>try_catch_finally\<close> statement is divided into the \<open>try_catch\<close> statement and a finally statement, which may be considered as try..finallywith
empty catch \item the \<open>try_catch\<close> statement has exactly one catch clause;
multiple ones can be
simulated with instanceof \item the compiler is supposed to add the annotations {\<open>_\<close>} during
type-checking. This
transformation is left out as its result is checked by the type rules anyway \end{itemize} \<close>
datatype jump
= Break label \<comment> \<open>break\<close>
| Cont label \<comment> \<open>continue\<close>
| Ret \<comment> \<open>return from method\<close>
datatype xcpt \<comment> \<open>exception\<close>
= Loc loc \<comment> \<open>location of allocated execption object\<close>
| Std xname \<comment> \<open>intermediate standard exception, see Eval.thy\<close>
datatype error
= AccessViolation \<comment> \<open>Access to a member that isn't permitted\<close>
| CrossMethodJump \<comment> \<open>Method exits with a break or continue\<close>
datatype abrupt \<comment> \<open>abrupt completion\<close>
= Xcpt xcpt \<comment> \<open>exception\<close>
| Jump jump \<comment> \<open>break, continue, return\<close>
| Error error \<comment> \<open>runtime errors, we wan't to detect and proof absent in welltyped programms\<close> type_synonym
abopt = "abrupt option"
text\<open>Local variable store and exception.
Anticipation of State.thy used by smallstep semantics. For a method call,
we save the local variables of the caller in the term Callee to restore them
after method return. Also an exception must be restored after the finally
statement\<close>
record sig = \<comment> \<open>signature of a method, cf. 8.4.2\<close>
name ::"mname"\<comment> \<open>acutally belongs to Decl.thy\<close>
parTs::"ty list"
\<comment> \<open>function codes for binary operations\<close> datatype binop = Mul \<comment> \<open>{\tt *} multiplication\<close>
| Div \<comment> \<open>{\tt /} division\<close>
| Mod \<comment> \<open>{\tt \%} remainder\<close>
| Plus \<comment> \<open>{\tt +} addition\<close>
| Minus \<comment> \<open>{\tt -} subtraction\<close>
| LShift \<comment> \<open>{\tt <<} left shift\<close>
| RShift \<comment> \<open>{\tt >>} signed right shift\<close>
| RShiftU \<comment> \<open>{\tt >>>} unsigned right shift\<close>
| Less \<comment> \<open>{\tt <} less than\<close>
| Le \<comment> \<open>{\tt <=} less than or equal\<close>
| Greater \<comment> \<open>{\tt >} greater than\<close>
| Ge \<comment> \<open>{\tt >=} greater than or equal\<close>
| Eq \<comment> \<open>{\tt ==} equal\<close>
| Neq \<comment> \<open>{\tt !=} not equal\<close>
| BitAnd \<comment> \<open>{\tt \&} bitwise AND\<close>
| And\<comment> \<open>{\tt \&} boolean AND\<close>
| BitXor \<comment> \<open>{\texttt \^} bitwise Xor\<close>
| Xor \<comment> \<open>{\texttt \^} boolean Xor\<close>
| BitOr \<comment> \<open>{\tt |} bitwise Or\<close>
| Or \<comment> \<open>{\tt |} boolean Or\<close>
| CondAnd \<comment> \<open>{\tt \&\&} conditional And\<close>
| CondOr \<comment> \<open>{\tt ||} conditional Or\<close> text\<open>The boolean operators {\tt \&} and {\tt |} strictly evaluate both
of their arguments. The conditional operators {\tt \&\&} and {\tt ||} only
evaluate the second argument if the value of the whole expression isn't
allready determined by the first argument.
e.g.: {\tt false \&\& e} e is not evaluated;
{\tt true || e} e is not evaluated; \<close>
datatype var
= LVar lname \<comment> \<open>local variable (incl. parameters)\<close>
| FVar qtname qtname bool expr vname (\<open>{_,_,_}_.._\<close>[10,10,10,85,99]90) \<comment> \<open>class field\<close> \<comment> \<open>\<^term>\<open>{accC,statDeclC,stat}e..fn\<close>\<close> \<comment> \<open>\<open>accC\<close>: accessing class (static class were\<close> \<comment> \<open>the code is declared. Annotation only needed for\<close> \<comment> \<open>evaluation to check accessibility)\<close> \<comment> \<open>\<open>statDeclC\<close>: static declaration class of field\<close> \<comment> \<open>\<open>stat\<close>: static or instance field?\<close> \<comment> \<open>\<open>e\<close>: reference to object\<close> \<comment> \<open>\<open>fn\<close>: field name\<close>
| AVar expr expr (\<open>_.[_]\<close>[90,10 ]90) \<comment> \<open>array component\<close> \<comment> \<open>\<^term>\<open>e1.[e2]\<close>: e1 array reference; e2 index\<close>
| InsInitV stmt var \<comment> \<open>insertion of initialization before evaluation\<close> \<comment> \<open>of var (technical term for smallstep semantics.)\<close>
| Super \<comment> \<open>special Super keyword\<close>
| Acc var \<comment> \<open>variable access\<close>
| Ass var expr (\<open>_:=_\<close> [90,85 ]85) \<comment> \<open>variable assign\<close>
| Cond expr expr expr (\<open>_ ? _ : _\<close> [85,85,80]80) \<comment> \<open>conditional\<close>
| Call qtname ref_ty inv_mode expr mname "(ty list)""(expr list)"
(\<open>{_,_,_}_\<cdot>_'( {_}_')\<close>[10,10,10,85,99,10,10]85) \<comment> \<open>method call\<close> \<comment> \<open>\<^term>\<open>{accC,statT,mode}e\<cdot>mn({pTs}args)\<close> "\<close> \<comment> \<open>\<open>accC\<close>: accessing class (static class were\<close> \<comment> \<open>the call code is declared. Annotation only needed for\<close> \<comment> \<open>evaluation to check accessibility)\<close> \<comment> \<open>\<open>statT\<close>: static declaration class/interface of\<close> \<comment> \<open>method\<close> \<comment> \<open>\<open>mode\<close>: invocation mode\<close> \<comment> \<open>\<open>e\<close>: reference to object\<close> \<comment> \<open>\<open>mn\<close>: field name\<close> \<comment> \<open>\<open>pTs\<close>: types of parameters\<close> \<comment> \<open>\<open>args\<close>: the actual parameters/arguments\<close>
| Methd qtname sig \<comment> \<open>(folded) method (see below)\<close>
| Body qtname stmt \<comment> \<open>(unfolded) method body\<close>
| InsInitE stmt expr \<comment> \<open>insertion of initialization before\<close> \<comment> \<open>evaluation of expr (technical term for smallstep sem.)\<close>
| Callee locals expr \<comment> \<open>save callers locals in callee-Frame\<close> \<comment> \<open>(technical term for smallstep semantics)\<close> and stmt
= Skip \<comment> \<open>empty statement\<close>
| Expr expr \<comment> \<open>expression statement\<close>
| Lab jump stmt (\<open>_\<bullet> _\<close> [ 99,66]66) \<comment> \<open>labeled statement; handles break\<close>
| Comp stmt stmt (\<open>_;; _\<close> [ 66,65]65)
| If' expr stmt stmt (\If'(_') _ Else _\ [ 80,79,79]70)
| Loop label expr stmt (\<open>_\<bullet> While'(_') _\<close> [ 99,80,79]70)
| Jmp jump \<comment> \<open>break, continue, return\<close>
| Throw expr
| TryC stmt qtname vname stmt (\<open>Try _ Catch'(_ _') _\<close> [79,99,80,79]70) \<comment> \<open>\<^term>\<open>Try c1 Catch(C vn) c2\<close>\<close> \<comment> \<open>\<open>c1\<close>: block were exception may be thrown\<close> \<comment> \<open>\<open>C\<close>: execption class to catch\<close> \<comment> \<open>\<open>vn\<close>: local name for exception used in \<open>c2\<close>\<close> \<comment> \<open>\<open>c2\<close>: block to execute when exception is cateched\<close>
| Fin stmt stmt (\<open>_ Finally _\<close> [ 79,79]70)
| FinA abopt stmt \<comment> \<open>Save abruption of first statement\<close> \<comment> \<open>technical term for smallstep sem.)\<close>
| Init qtname \<comment> \<open>class initialization\<close>
datatype_compat var expr stmt
text\<open>
The expressions Methd and Body are artificial program constructs, in the
sense that they are not used to define a concrete Bali program. In the
operational semantic's they are "generated on the fly" to decompose the task to define the behaviour of the Call expression.
They are crucial for the axiomatic semantics to give a syntactic hook to insert
some assertions (cf. AxSem.thy, Eval.thy).
The Init statement (to initialize a class on its first use) is
inserted in various places by the semantics.
Callee, InsInitV, InsInitE,FinA are only needed as intermediate steps in the
smallstep (transition) semantics (cf. Trans.thy). Callee is used to save the local variables of the caller for method return. So ve avoid modelling a
frame stack.
The InsInitV/E terms are only used by the smallstep semantics to model the
intermediate steps of class-initialisation. \<close>
ML \<open>ML_Thms.bind_thms ("is_stmt_rews", sum3_instantiate \<^context> @{thm is_stmt_def})\<close>
declare is_stmt_rews [simp]
text\<open>
Here is some syntactic stuff to handle the injections of statements,
expressions, variables and expression lists into general terms. \<close>
text\<open>It seems to be more elegant to have an overloaded injection like the
following. \<close>
class inj_term = fixes inj_term:: "'a \ term" (\\_\\ 1000)
text\<open>How this overloaded injections work can be seen in the theory \<open>DefiniteAssignment\<close>. Other big inductive relations on
terms defined in theories \<open>WellType\<close>, \<open>Eval\<close>, \<open>Evaln\<close> and \<open>AxSem\<close> don't follow this convention right now, but introduce subtle
syntactic sugar in the relations themselves to make a distinction on
expressions, statements and so on. So unfortunately you will encounter a
mixture of dealing with these injections. The abbreviations above are used
as bridge between the different conventions. \<close>
lemma term_cases: " \<lbrakk>\<And> v. P \<langle>v\<rangle>\<^sub>v; \<And> e. P \<langle>e\<rangle>\<^sub>e;\<And> c. P \<langle>c\<rangle>\<^sub>s;\<And> l. P \<langle>l\<rangle>\<^sub>l\<rbrakk> \<Longrightarrow> P t" apply (cases t) apply (rename_tac a, case_tac a) apply auto done
subsubsection \<open>Evaluation of unary operations\<close> primrec eval_unop :: "unop \ val \ val" where "eval_unop UPlus v = Intg (the_Intg v)"
| "eval_unop UMinus v = Intg (- (the_Intg v))"
| "eval_unop UBitNot v = Intg 42"\<comment> \<open>FIXME: Not yet implemented\<close>
| "eval_unop UNot v = Bool (\ the_Bool v)"
subsubsection \<open>Evaluation of binary operations\<close> primrec eval_binop :: "binop \ val \ val \ val" where "eval_binop Mul v1 v2 = Intg ((the_Intg v1) * (the_Intg v2))"
| "eval_binop Div v1 v2 = Intg ((the_Intg v1) div (the_Intg v2))"
| "eval_binop Mod v1 v2 = Intg ((the_Intg v1) mod (the_Intg v2))"
| "eval_binop Plus v1 v2 = Intg ((the_Intg v1) + (the_Intg v2))"
| "eval_binop Minus v1 v2 = Intg ((the_Intg v1) - (the_Intg v2))"
\<comment> \<open>Be aware of the explicit coercion of the shift distance to nat\<close>
| "eval_binop LShift v1 v2 = Intg ((the_Intg v1) * (2^(nat (the_Intg v2))))"
| "eval_binop RShift v1 v2 = Intg ((the_Intg v1) div (2^(nat (the_Intg v2))))"
| "eval_binop RShiftU v1 v2 = Intg 42"\<comment> \<open>FIXME: Not yet implemented\<close>
definition
need_second_arg :: "binop \ val \ bool" where "need_second_arg binop v1 = (\ ((binop=CondAnd \ \ the_Bool v1) \
(binop=CondOr \<and> the_Bool v1)))" text\<open>\<^term>\<open>CondAnd\<close> and \<^term>\<open>CondOr\<close> only evalulate the second argument if the value isn't already determined by the first argument\
lemma need_second_arg_CondAnd [simp]: "need_second_arg CondAnd (Bool b) = b" by (simp add: need_second_arg_def)
lemma need_second_arg_CondOr [simp]: "need_second_arg CondOr (Bool b) = (\ b)" by (simp add: need_second_arg_def)
lemma need_second_arg_strict[simp]: "\binop\CondAnd; binop\CondOr\ \ need_second_arg binop b" by (cases binop)
(simp_all add: need_second_arg_def) end
¤ Dauer der Verarbeitung: 0.33 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.