theory BVExample imports "../JVM/JVMListExample"
BVSpecTypeSafe
JVM begin
text\<open>
This theoryshows type correctness of the example program in section \ref{sec:JVMListExample} (p. \pageref{sec:JVMListExample}) by
explicitly providing a welltyping. It alsoshows that the start
state of the program conforms to the welltyping; hence type safe
execution is guaranteed. \<close>
subsection "Setup"
text\<open>Abbreviations for definitions we will have to use often in the
proofs below:\<close> lemmas name_defs = list_name_def test_name_def val_name_def next_name_def lemmas system_defs = SystemClasses_def ObjectC_def NullPointerC_def
OutOfMemoryC_def ClassCastC_def lemmas class_defs = list_class_def test_class_def
text\<open>These auxiliary proofs are for efficiency: class lookup, subclass relation, method and field lookup are computed only once: \<close> lemma class_Object [simp]: "class E Object = Some (undefined, [],[])" by (simp add: class_def system_defs E_def)
lemma class_NullPointer [simp]: "class E (Xcpt NullPointer) = Some (Object, [], [])" by (simp add: class_def system_defs E_def)
lemma class_OutOfMemory [simp]: "class E (Xcpt OutOfMemory) = Some (Object, [], [])" by (simp add: class_def system_defs E_def)
lemma class_ClassCast [simp]: "class E (Xcpt ClassCast) = Some (Object, [], [])" by (simp add: class_def system_defs E_def)
lemma class_list [simp]: "class E list_name = Some list_class" by (simp add: class_def system_defs E_def name_defs distinct_classes [symmetric])
lemma class_test [simp]: "class E test_name = Some test_class" by (simp add: class_def system_defs E_def name_defs distinct_classes [symmetric])
text\<open>The subclass relation is acyclic; hence its converse is well founded:\<close> lemma notin_rtrancl: "(a, b) \ r\<^sup>* \ a \ b \ (\y. (a, y) \ r) \ False" by (auto elim: converse_rtranclE)
text\<open>
The nextdefinitionand three proof rules implement an algorithm to
enumarate natural numbers. The command \<open>apply (elim pc_end pc_next pc_0\<close>
transforms a goal of the form
@{prop [display] "pc < n \ P pc"}
into a series of goals
@{prop [display] "P 0"}
@{prop [display] "P (Suc 0)"}
\<open>\<dots>\<close>
@{prop [display] "P n"} \<close> definition intervall :: "nat \ nat \ nat \ bool" (\_ \ [_, _')\) where "x \ [a, b) \ a \ x \ x < b"
lemma pc_0: "x < n \ (x \ [0, n) \ P x) \ P x" by (simp add: intervall_def)
lemma pc_next: "x \ [n0, n) \ P n0 \ (x \ [Suc n0, n) \ P x) \ P x" apply (cases "x=n0") apply (auto simp add: intervall_def) done
lemma pc_end: "x \ [n,n) \ P x" by (unfold intervall_def) arith
subsection "Program structure"
text\<open>
The program is structurally wellformed: \<close>
lemma wf_struct: "wf_prog (\G C mb. True) E" (is "wf_prog ?mb E") proof - have"unique E" by (simp add: system_defs E_def class_defs name_defs distinct_classes) moreover have"set SystemClasses \ set E" by (simp add: system_defs E_def) hence"wf_syscls E"by (rule wf_syscls) moreover have"wf_cdecl ?mb E ObjectC"by (simp add: wf_cdecl_def ObjectC_def) moreover have"wf_cdecl ?mb E NullPointerC" by (auto elim: notin_rtrancl
simp add: wf_cdecl_def name_defs NullPointerC_def subcls1) moreover have"wf_cdecl ?mb E ClassCastC" by (auto elim: notin_rtrancl
simp add: wf_cdecl_def name_defs ClassCastC_def subcls1) moreover have"wf_cdecl ?mb E OutOfMemoryC" by (auto elim: notin_rtrancl
simp add: wf_cdecl_def name_defs OutOfMemoryC_def subcls1) moreover have"wf_cdecl ?mb E (list_name, list_class)" apply (auto elim!: notin_rtrancl
simp add: wf_cdecl_def wf_fdecl_def list_class_def
wf_mdecl_def wf_mhead_def subcls1) apply (auto simp add: name_defs distinct_classes distinct_fields) done moreover have"wf_cdecl ?mb E (test_name, test_class)" apply (auto elim!: notin_rtrancl
simp add: wf_cdecl_def wf_fdecl_def test_class_def
wf_mdecl_def wf_mhead_def subcls1) apply (auto simp add: name_defs distinct_classes distinct_fields) done ultimately show ?thesis by (simp add: wf_prog_def ws_prog_def wf_cdecl_mrT_cdecl_mdecl E_def SystemClasses_def) qed
subsection "Welltypings" text\<open>
We show welltypings of the methods \<^term>\<open>append_name\<close> in class \<^term>\<open>list_name\<close>, and\<^term>\<open>makelist_name\<close> in class \<^term>\<open>test_name\<close>: \<close> lemmas eff_simps [simp] = eff_def norm_eff_def xcpt_eff_def declare appInvoke [simp del]
definition phi_append :: method_type (\<open>\<phi>\<^sub>a\<close>) where "\\<^sub>a \ map (\(x,y). Some (x, map OK y)) [
( [], [Class list_name, Class list_name]),
( [Class list_name], [Class list_name, Class list_name]),
( [Class list_name], [Class list_name, Class list_name]),
( [Class list_name, Class list_name], [Class list_name, Class list_name]),
([NT, Class list_name, Class list_name], [Class list_name, Class list_name]),
( [Class list_name], [Class list_name, Class list_name]),
( [Class list_name, Class list_name], [Class list_name, Class list_name]),
( [PrimT Void], [Class list_name, Class list_name]),
( [Class Object], [Class list_name, Class list_name]),
( [], [Class list_name, Class list_name]),
( [Class list_name], [Class list_name, Class list_name]),
( [Class list_name, Class list_name], [Class list_name, Class list_name]),
( [], [Class list_name, Class list_name]),
( [PrimT Void], [Class list_name, Class list_name])]"
text\<open>Some abbreviations for readability\<close> abbreviation Clist :: ty where"Clist == Class list_name" abbreviation Ctest :: ty where"Ctest == Class test_name"
definition phi_makelist :: method_type (\<open>\<phi>\<^sub>m\<close>) where "\\<^sub>m \ map (\(x,y). Some (x, y)) [
( [], [OK Ctest, Err , Err ]),
( [Clist], [OK Ctest, Err , Err ]),
( [Clist, Clist], [OK Ctest, Err , Err ]),
( [Clist], [OK Clist, Err , Err ]),
( [PrimT Integer, Clist], [OK Clist, Err , Err ]),
( [], [OK Clist, Err , Err ]),
( [Clist], [OK Clist, Err , Err ]),
( [Clist, Clist], [OK Clist, Err , Err ]),
( [Clist], [OK Clist, OK Clist, Err ]),
( [PrimT Integer, Clist], [OK Clist, OK Clist, Err ]),
( [], [OK Clist, OK Clist, Err ]),
( [Clist], [OK Clist, OK Clist, Err ]),
( [Clist, Clist], [OK Clist, OK Clist, Err ]),
( [Clist], [OK Clist, OK Clist, OK Clist]),
( [PrimT Integer, Clist], [OK Clist, OK Clist, OK Clist]),
( [], [OK Clist, OK Clist, OK Clist]),
( [Clist], [OK Clist, OK Clist, OK Clist]),
( [Clist, Clist], [OK Clist, OK Clist, OK Clist]),
( [PrimT Void], [OK Clist, OK Clist, OK Clist]),
( [], [OK Clist, OK Clist, OK Clist]),
( [Clist], [OK Clist, OK Clist, OK Clist]),
( [Clist, Clist], [OK Clist, OK Clist, OK Clist]),
( [PrimT Void], [OK Clist, OK Clist, OK Clist])]"
text\<open>The whole program is welltyped:\<close> definition Phi :: prog_type (\<open>\<Phi>\<close>) where "\ C sg \ if C = test_name \ sg = (makelist_name, []) then \\<^sub>m else if C = list_name \<and> sg = (append_name, [Class list_name]) then \<phi>\<^sub>a else []"
subsection "Example for code generation: inferring method types"
definition test_kil :: "jvm_prog \ cname \ ty list \ ty \ nat \ nat \
exception_table \<Rightarrow> instr list \<Rightarrow> JVMType.state list" where "test_kil G C pTs rT mxs mxl et instr =
(let first = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
start = OK first#(replicate (size instr - 1) (OK None)) in kiljvm G mxs (1+size pTs+mxl) rT et instr start)"
lemma [code]: "unstables r step ss =
fold (\<lambda>p A. if \<not>stable r step ss p then insert p A else A) [0..<size ss] {}" proof - have"unstables r step ss = (UN p:{..stable r step ss p then {p} else {})" apply (unfold unstables_def) apply (rule equalityI) apply (rule subsetI) apply (erule CollectE) apply (erule conjE) apply (rule UN_I) apply simp apply simp apply (rule subsetI) apply (erule UN_E) apply (case_tac "\ stable r step ss p") apply simp_all done alsohave"\f. (UN p:{..(set (map f [0.. alsonote Sup_set_fold alsonote fold_map alsohave"(\) \ (\p. if \ stable r step ss p then {p} else {}) =
(\<lambda>p A. if \<not>stable r step ss p then insert p A else A)" by(auto simp add: fun_eq_iff) finallyshow ?thesis . qed
definition some_elem :: "'a set \ 'a" where [code del]: "some_elem = (\S. SOME x. x \ S)"
code_printing
constant some_elem \<rightharpoonup> (SML) "(case/ _ of/ Set/ xs/ =>/ hd/ xs)"
text\<open>This code setup is just a demonstration and \emph{not} sound!\<close>
lemma False proof - have"some_elem (set [False, True]) = False" by eval moreoverhave"some_elem (set [True, False]) = True" by eval ultimatelyshow False by (simp add: some_elem_def) qed
lemma [code]: "iter f step ss w = while (\(ss, w). \ Set.is_empty w)
(\<lambda>(ss, w). let p = some_elem w in propa f (step p (ss ! p)) ss (w - {p}))
(ss, w)" by (simp add: iter_def some_elem_def)
lemma JVM_sup_unfold [code]: "JVMType.sup S m n = lift2 (Opt.sup
(Product.sup (Listn.sup (JType.sup S))
(\<lambda>x y. OK (map2 (lift2 (JType.sup S)) x y))))" apply (unfold JVMType.sup_def JVMType.sl_def Opt.esl_def Err.sl_def
stk_esl_def reg_sl_def Product.esl_def
Listn.sl_def upto_esl_def JType.esl_def Err.esl_def) by simp
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.