src/HOL/MicroJava/BV/BVExample.thy
author berghofe
Wed Nov 27 17:11:38 2002 +0100 (2002-11-27)
changeset 13727 4ab8d49ab981
parent 13214 2aa33ed5f526
child 14045 a34d89ce6097
permissions -rw-r--r--
Fixed bug in consts_code section.
     1 (*  Title:      HOL/MicroJava/BV/BVExample.thy
     2     ID:         $Id$
     3     Author:     Gerwin Klein
     4 *)
     5 
     6 header {* \isaheader{Example Welltypings}\label{sec:BVExample} *}
     7 
     8 theory BVExample = JVMListExample + BVSpecTypeSafe + JVM:
     9 
    10 text {*
    11   This theory shows type correctness of the example program in section 
    12   \ref{sec:JVMListExample} (p. \pageref{sec:JVMListExample}) by
    13   explicitly providing a welltyping. It also shows that the start
    14   state of the program conforms to the welltyping; hence type safe
    15   execution is guaranteed.
    16 *}
    17 
    18 section "Setup"
    19 text {*
    20   Since the types @{typ cnam}, @{text vnam}, and @{text mname} are 
    21   anonymous, we describe distinctness of names in the example by axioms:
    22 *}
    23 axioms 
    24   distinct_classes: "list_nam \<noteq> test_nam"
    25   distinct_fields:  "val_nam \<noteq> next_nam"
    26 
    27 text {* Abbreviations for definitions we will have to use often in the
    28 proofs below: *}
    29 lemmas name_defs   = list_name_def test_name_def val_name_def next_name_def 
    30 lemmas system_defs = SystemClasses_def ObjectC_def NullPointerC_def 
    31                      OutOfMemoryC_def ClassCastC_def
    32 lemmas class_defs  = list_class_def test_class_def
    33 
    34 text {* These auxiliary proofs are for efficiency: class lookup,
    35 subclass relation, method and field lookup are computed only once:
    36 *}
    37 lemma class_Object [simp]:
    38   "class E Object = Some (arbitrary, [],[])"
    39   by (simp add: class_def system_defs E_def)
    40 
    41 lemma class_NullPointer [simp]:
    42   "class E (Xcpt NullPointer) = Some (Object, [], [])"
    43   by (simp add: class_def system_defs E_def)
    44 
    45 lemma class_OutOfMemory [simp]:
    46   "class E (Xcpt OutOfMemory) = Some (Object, [], [])"
    47   by (simp add: class_def system_defs E_def)
    48 
    49 lemma class_ClassCast [simp]:
    50   "class E (Xcpt ClassCast) = Some (Object, [], [])"
    51   by (simp add: class_def system_defs E_def)
    52 
    53 lemma class_list [simp]:
    54   "class E list_name = Some list_class"
    55   by (simp add: class_def system_defs E_def name_defs distinct_classes [symmetric])
    56  
    57 lemma class_test [simp]:
    58   "class E test_name = Some test_class"
    59   by (simp add: class_def system_defs E_def name_defs distinct_classes [symmetric])
    60 
    61 lemma E_classes [simp]:
    62   "{C. is_class E C} = {list_name, test_name, Xcpt NullPointer, 
    63                         Xcpt ClassCast, Xcpt OutOfMemory, Object}"
    64   by (auto simp add: is_class_def class_def system_defs E_def name_defs class_defs)
    65 
    66 text {* The subclass releation spelled out: *}
    67 lemma subcls1:
    68   "subcls1 E = {(list_name,Object), (test_name,Object), (Xcpt NullPointer, Object),
    69                 (Xcpt ClassCast, Object), (Xcpt OutOfMemory, Object)}"
    70   apply (simp add: subcls1_def2)
    71   apply (simp add: name_defs class_defs system_defs E_def class_def)
    72   apply (auto split: split_if_asm)
    73   done
    74 
    75 text {* The subclass relation is acyclic; hence its converse is well founded: *}
    76 lemma notin_rtrancl:
    77   "(a,b) \<in> r\<^sup>* \<Longrightarrow> a \<noteq> b \<Longrightarrow> (\<And>y. (a,y) \<notin> r) \<Longrightarrow> False"
    78   by (auto elim: converse_rtranclE)  
    79 
    80 lemma acyclic_subcls1_E: "acyclic (subcls1 E)"
    81   apply (rule acyclicI)
    82   apply (simp add: subcls1)
    83   apply (auto dest!: tranclD)
    84   apply (auto elim!: notin_rtrancl simp add: name_defs distinct_classes)
    85   done
    86 
    87 lemma wf_subcls1_E: "wf ((subcls1 E)\<inverse>)"
    88   apply (rule finite_acyclic_wf_converse)
    89   apply (simp add: subcls1)
    90   apply (rule acyclic_subcls1_E)
    91   done  
    92 
    93 text {* Method and field lookup: *}
    94 lemma method_Object [simp]:
    95   "method (E, Object) = empty"
    96   by (simp add: method_rec_lemma [OF class_Object wf_subcls1_E])
    97 
    98 lemma method_append [simp]:
    99   "method (E, list_name) (append_name, [Class list_name]) =
   100   Some (list_name, PrimT Void, 3, 0, append_ins, [(1, 2, 8, Xcpt NullPointer)])"
   101   apply (insert class_list)
   102   apply (unfold list_class_def)
   103   apply (drule method_rec_lemma [OF _ wf_subcls1_E])
   104   apply simp
   105   done
   106 
   107 lemma method_makelist [simp]:
   108   "method (E, test_name) (makelist_name, []) = 
   109   Some (test_name, PrimT Void, 3, 2, make_list_ins, [])"
   110   apply (insert class_test)
   111   apply (unfold test_class_def)
   112   apply (drule method_rec_lemma [OF _ wf_subcls1_E])
   113   apply simp
   114   done
   115 
   116 lemma field_val [simp]:
   117   "field (E, list_name) val_name = Some (list_name, PrimT Integer)"
   118   apply (unfold field_def)
   119   apply (insert class_list)
   120   apply (unfold list_class_def)
   121   apply (drule fields_rec_lemma [OF _ wf_subcls1_E])
   122   apply simp
   123   done
   124 
   125 lemma field_next [simp]:
   126   "field (E, list_name) next_name = Some (list_name, Class list_name)"
   127   apply (unfold field_def)
   128   apply (insert class_list)
   129   apply (unfold list_class_def)
   130   apply (drule fields_rec_lemma [OF _ wf_subcls1_E])
   131   apply (simp add: name_defs distinct_fields [symmetric])
   132   done
   133 
   134 lemma [simp]: "fields (E, Object) = []"
   135    by (simp add: fields_rec_lemma [OF class_Object wf_subcls1_E])
   136  
   137 lemma [simp]: "fields (E, Xcpt NullPointer) = []"
   138   by (simp add: fields_rec_lemma [OF class_NullPointer wf_subcls1_E])
   139 
   140 lemma [simp]: "fields (E, Xcpt ClassCast) = []"
   141   by (simp add: fields_rec_lemma [OF class_ClassCast wf_subcls1_E])
   142 
   143 lemma [simp]: "fields (E, Xcpt OutOfMemory) = []"
   144   by (simp add: fields_rec_lemma [OF class_OutOfMemory wf_subcls1_E])
   145 
   146 lemma [simp]: "fields (E, test_name) = []"
   147   apply (insert class_test)
   148   apply (unfold test_class_def)
   149   apply (drule fields_rec_lemma [OF _ wf_subcls1_E])
   150   apply simp
   151   done
   152 
   153 lemmas [simp] = is_class_def
   154 
   155 text {*
   156   The next definition and three proof rules implement an algorithm to
   157   enumarate natural numbers. The command @{text "apply (elim pc_end pc_next pc_0"} 
   158   transforms a goal of the form
   159   @{prop [display] "pc < n \<Longrightarrow> P pc"} 
   160   into a series of goals
   161   @{prop [display] "P 0"} 
   162   @{prop [display] "P (Suc 0)"} 
   163 
   164   @{text "\<dots>"}
   165 
   166   @{prop [display] "P n"} 
   167 *}
   168 constdefs 
   169   intervall :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" ("_ \<in> [_, _')")
   170   "x \<in> [a, b) \<equiv> a \<le> x \<and> x < b"
   171 
   172 lemma pc_0: "x < n \<Longrightarrow> (x \<in> [0, n) \<Longrightarrow> P x) \<Longrightarrow> P x"
   173   by (simp add: intervall_def)
   174 
   175 lemma pc_next: "x \<in> [n0, n) \<Longrightarrow> P n0 \<Longrightarrow> (x \<in> [Suc n0, n) \<Longrightarrow> P x) \<Longrightarrow> P x"
   176   apply (cases "x=n0")
   177   apply (auto simp add: intervall_def)
   178   done
   179 
   180 lemma pc_end: "x \<in> [n,n) \<Longrightarrow> P x" 
   181   by (unfold intervall_def) arith
   182 
   183 
   184 section "Program structure"
   185 
   186 text {*
   187   The program is structurally wellformed:
   188 *}
   189 lemma wf_struct:
   190   "wf_prog (\<lambda>G C mb. True) E" (is "wf_prog ?mb E")
   191 proof -
   192   have "unique E" 
   193     by (simp add: system_defs E_def class_defs name_defs distinct_classes)
   194   moreover
   195   have "set SystemClasses \<subseteq> set E" by (simp add: system_defs E_def)
   196   hence "wf_syscls E" by (rule wf_syscls)
   197   moreover
   198   have "wf_cdecl ?mb E ObjectC" by (simp add: wf_cdecl_def ObjectC_def)
   199   moreover
   200   have "wf_cdecl ?mb E NullPointerC" 
   201     by (auto elim: notin_rtrancl 
   202             simp add: wf_cdecl_def name_defs NullPointerC_def subcls1)
   203   moreover
   204   have "wf_cdecl ?mb E ClassCastC" 
   205     by (auto elim: notin_rtrancl 
   206             simp add: wf_cdecl_def name_defs ClassCastC_def subcls1)
   207   moreover
   208   have "wf_cdecl ?mb E OutOfMemoryC" 
   209     by (auto elim: notin_rtrancl 
   210             simp add: wf_cdecl_def name_defs OutOfMemoryC_def subcls1)
   211   moreover
   212   have "wf_cdecl ?mb E (list_name, list_class)"
   213     apply (auto elim!: notin_rtrancl 
   214             simp add: wf_cdecl_def wf_fdecl_def list_class_def 
   215                       wf_mdecl_def wf_mhead_def subcls1)
   216     apply (auto simp add: name_defs distinct_classes distinct_fields)
   217     done    
   218   moreover
   219   have "wf_cdecl ?mb E (test_name, test_class)" 
   220     apply (auto elim!: notin_rtrancl 
   221             simp add: wf_cdecl_def wf_fdecl_def test_class_def 
   222                       wf_mdecl_def wf_mhead_def subcls1)
   223     apply (auto simp add: name_defs distinct_classes distinct_fields)
   224     done       
   225   ultimately
   226   show ?thesis by (simp add: wf_prog_def E_def SystemClasses_def)
   227 qed
   228 
   229 section "Welltypings"
   230 text {*
   231   We show welltypings of the methods @{term append_name} in class @{term list_name}, 
   232   and @{term makelist_name} in class @{term test_name}:
   233 *}
   234 lemmas eff_simps [simp] = eff_def norm_eff_def xcpt_eff_def
   235 declare appInvoke [simp del]
   236 
   237 constdefs
   238   phi_append :: method_type ("\<phi>\<^sub>a")
   239   "\<phi>\<^sub>a \<equiv> map (\<lambda>(x,y). Some (x, map OK y)) [ 
   240    (                                    [], [Class list_name, Class list_name]),
   241    (                     [Class list_name], [Class list_name, Class list_name]),
   242    (                     [Class list_name], [Class list_name, Class list_name]),
   243    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   244    ([NT, Class list_name, Class list_name], [Class list_name, Class list_name]),
   245    (                     [Class list_name], [Class list_name, Class list_name]),
   246    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   247    (                          [PrimT Void], [Class list_name, Class list_name]),
   248    (                        [Class Object], [Class list_name, Class list_name]),
   249    (                                    [], [Class list_name, Class list_name]),
   250    (                     [Class list_name], [Class list_name, Class list_name]),
   251    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   252    (                                    [], [Class list_name, Class list_name]),
   253    (                          [PrimT Void], [Class list_name, Class list_name])]"
   254 
   255 
   256 lemma bounded_append [simp]:
   257   "check_bounded append_ins [(Suc 0, 2, 8, Xcpt NullPointer)]"
   258   apply (simp add: check_bounded_def)
   259   apply (simp add: nat_number append_ins_def)
   260   apply (rule allI, rule impI)
   261   apply (elim pc_end pc_next pc_0)
   262   apply auto
   263   done
   264 
   265 lemma types_append [simp]: "check_types E 3 (Suc (Suc 0)) (map OK \<phi>\<^sub>a)"
   266   apply (auto simp add: check_types_def phi_append_def JVM_states_unfold)
   267   apply (unfold list_def)
   268   apply auto
   269   done
   270   
   271 lemma wt_append [simp]:
   272   "wt_method E list_name [Class list_name] (PrimT Void) 3 0 append_ins
   273              [(Suc 0, 2, 8, Xcpt NullPointer)] \<phi>\<^sub>a"
   274   apply (simp add: wt_method_def wt_start_def wt_instr_def)
   275   apply (simp add: phi_append_def append_ins_def)
   276   apply clarify
   277   apply (elim pc_end pc_next pc_0)
   278   apply simp
   279   apply (fastsimp simp add: match_exception_entry_def sup_state_conv subcls1)
   280   apply simp
   281   apply simp
   282   apply (fastsimp simp add: sup_state_conv subcls1)
   283   apply simp
   284   apply (simp add: app_def xcpt_app_def)
   285   apply simp
   286   apply simp
   287   apply simp
   288   apply (simp add: match_exception_entry_def)
   289   apply (simp add: match_exception_entry_def)
   290   apply simp
   291   apply simp
   292   done
   293 
   294 text {* Some abbreviations for readability *} 
   295 syntax
   296   Clist :: ty 
   297   Ctest :: ty
   298 translations
   299   "Clist" == "Class list_name"
   300   "Ctest" == "Class test_name"
   301 
   302 constdefs
   303   phi_makelist :: method_type ("\<phi>\<^sub>m")
   304   "\<phi>\<^sub>m \<equiv> map (\<lambda>(x,y). Some (x, y)) [ 
   305     (                                   [], [OK Ctest, Err     , Err     ]),
   306     (                              [Clist], [OK Ctest, Err     , Err     ]),
   307     (                       [Clist, Clist], [OK Ctest, Err     , Err     ]),
   308     (                              [Clist], [OK Clist, Err     , Err     ]),
   309     (               [PrimT Integer, Clist], [OK Clist, Err     , Err     ]),
   310     (                                   [], [OK Clist, Err     , Err     ]),
   311     (                              [Clist], [OK Clist, Err     , Err     ]),
   312     (                       [Clist, Clist], [OK Clist, Err     , Err     ]),
   313     (                              [Clist], [OK Clist, OK Clist, Err     ]),
   314     (               [PrimT Integer, Clist], [OK Clist, OK Clist, Err     ]),
   315     (                                   [], [OK Clist, OK Clist, Err     ]),
   316     (                              [Clist], [OK Clist, OK Clist, Err     ]),
   317     (                       [Clist, Clist], [OK Clist, OK Clist, Err     ]),
   318     (                              [Clist], [OK Clist, OK Clist, OK Clist]),
   319     (               [PrimT Integer, Clist], [OK Clist, OK Clist, OK Clist]),
   320     (                                   [], [OK Clist, OK Clist, OK Clist]),
   321     (                              [Clist], [OK Clist, OK Clist, OK Clist]),
   322     (                       [Clist, Clist], [OK Clist, OK Clist, OK Clist]),
   323     (                         [PrimT Void], [OK Clist, OK Clist, OK Clist]),
   324     (                                   [], [OK Clist, OK Clist, OK Clist]),
   325     (                              [Clist], [OK Clist, OK Clist, OK Clist]),
   326     (                       [Clist, Clist], [OK Clist, OK Clist, OK Clist]),
   327     (                         [PrimT Void], [OK Clist, OK Clist, OK Clist])]"
   328 
   329 lemma bounded_makelist [simp]: "check_bounded make_list_ins []"
   330   apply (simp add: check_bounded_def)
   331   apply (simp add: nat_number make_list_ins_def)
   332   apply (rule allI, rule impI)
   333   apply (elim pc_end pc_next pc_0)
   334   apply auto
   335   done
   336 
   337 lemma types_makelist [simp]: "check_types E 3 (Suc (Suc (Suc 0))) (map OK \<phi>\<^sub>m)"
   338   apply (auto simp add: check_types_def phi_makelist_def JVM_states_unfold)
   339   apply (unfold list_def)
   340   apply auto
   341   done
   342 
   343 lemma wt_makelist [simp]:
   344   "wt_method E test_name [] (PrimT Void) 3 2 make_list_ins [] \<phi>\<^sub>m"
   345   apply (simp add: wt_method_def)
   346   apply (simp add: make_list_ins_def phi_makelist_def)
   347   apply (simp add: wt_start_def nat_number)
   348   apply (simp add: wt_instr_def)
   349   apply clarify
   350   apply (elim pc_end pc_next pc_0)
   351   apply (simp add: match_exception_entry_def)
   352   apply simp
   353   apply simp
   354   apply simp
   355   apply (simp add: match_exception_entry_def)
   356   apply (simp add: match_exception_entry_def) 
   357   apply simp
   358   apply simp
   359   apply simp
   360   apply (simp add: match_exception_entry_def)
   361   apply (simp add: match_exception_entry_def) 
   362   apply simp
   363   apply simp
   364   apply simp
   365   apply (simp add: match_exception_entry_def)
   366   apply (simp add: match_exception_entry_def) 
   367   apply simp
   368   apply (simp add: app_def xcpt_app_def)
   369   apply simp 
   370   apply simp
   371   apply simp
   372   apply (simp add: app_def xcpt_app_def) 
   373   apply simp
   374   done
   375 
   376 text {* The whole program is welltyped: *}
   377 constdefs 
   378   Phi :: prog_type ("\<Phi>")
   379   "\<Phi> C sg \<equiv> if C = test_name \<and> sg = (makelist_name, []) then \<phi>\<^sub>m else          
   380              if C = list_name \<and> sg = (append_name, [Class list_name]) then \<phi>\<^sub>a else []"
   381 
   382 lemma wf_prog:
   383   "wt_jvm_prog E \<Phi>" 
   384   apply (unfold wt_jvm_prog_def)
   385   apply (rule wf_mb'E [OF wf_struct])
   386   apply (simp add: E_def)
   387   apply clarify
   388   apply (fold E_def)
   389   apply (simp add: system_defs class_defs Phi_def) 
   390   apply auto
   391   done 
   392 
   393 
   394 section "Conformance"
   395 text {* Execution of the program will be typesafe, because its
   396   start state conforms to the welltyping: *}
   397 
   398 lemma "E,\<Phi> \<turnstile>JVM start_state E test_name makelist_name \<surd>"
   399   apply (rule BV_correct_initial)
   400     apply (rule wf_prog)
   401    apply simp
   402   apply simp
   403   done
   404 
   405 
   406 section "Example for code generation: inferring method types"
   407 
   408 constdefs
   409   test_kil :: "jvm_prog \<Rightarrow> cname \<Rightarrow> ty list \<Rightarrow> ty \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 
   410              exception_table \<Rightarrow> instr list \<Rightarrow> JVMType.state list"
   411   "test_kil G C pTs rT mxs mxl et instr ==
   412    (let first  = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
   413         start  = OK first#(replicate (size instr - 1) (OK None))
   414     in  kiljvm G mxs (1+size pTs+mxl) rT et instr start)"
   415 
   416 lemma [code]:
   417   "unstables r step ss = (UN p:{..size ss(}. if \<not>stable r step ss p then {p} else {})"
   418   apply (unfold unstables_def)
   419   apply (rule equalityI)
   420   apply (rule subsetI)
   421   apply (erule CollectE)
   422   apply (erule conjE)
   423   apply (rule UN_I)
   424   apply simp
   425   apply simp
   426   apply (rule subsetI)
   427   apply (erule UN_E)
   428   apply (case_tac "\<not> stable r step ss p")
   429   apply simp+
   430   done
   431 
   432 lemmas [code] = lessThan_0 lessThan_Suc
   433 
   434 constdefs
   435   some_elem :: "'a set \<Rightarrow> 'a"
   436   "some_elem == (%S. SOME x. x : S)"
   437 
   438 lemma [code]:
   439 "iter f step ss w =
   440  while (%(ss,w). w \<noteq> {})
   441        (%(ss,w). let p = some_elem w
   442                  in propa f (step p (ss!p)) ss (w-{p}))
   443        (ss,w)"
   444   by (unfold iter_def some_elem_def, rule refl)
   445 
   446 types_code
   447   set ("_ list")
   448 
   449 consts_code
   450   "{}"     ("[]")
   451   "insert" ("(_ ins _)")
   452   "op :"   ("(_ mem _)")
   453   "op Un"  ("(_ union _)")
   454   "image"  ("map")
   455   "UNION"  ("(fn A => fn f => flat (map f A))")
   456   "Bex"    ("(fn A => fn f => exists f A)")
   457   "Ball"   ("(fn A => fn f => forall f A)")
   458   "some_elem" ("hd")
   459   "op -" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  ("(_ \\\\ _)")
   460 
   461 lemma JVM_sup_unfold [code]:
   462  "JVMType.sup S m n = lift2 (Opt.sup
   463        (Product.sup (Listn.sup (JType.sup S))
   464          (\<lambda>x y. OK (map2 (lift2 (JType.sup S)) x y))))" 
   465   apply (unfold JVMType.sup_def JVMType.sl_def Opt.esl_def Err.sl_def
   466          stk_esl_def reg_sl_def Product.esl_def  
   467          Listn.sl_def upto_esl_def JType.esl_def Err.esl_def) 
   468   by simp
   469 
   470 lemmas [code] =
   471   meta_eq_to_obj_eq [OF JType.sup_def [unfolded exec_lub_def]]
   472   meta_eq_to_obj_eq [OF JVM_le_unfold]
   473 
   474 lemmas [code ind] = rtrancl_refl converse_rtrancl_into_rtrancl
   475 
   476 generate_code 
   477   test1 = "test_kil E list_name [Class list_name] (PrimT Void) 3 0
   478     [(Suc 0, 2, 8, Xcpt NullPointer)] append_ins"
   479   test2 = "test_kil E test_name [] (PrimT Void) 3 2 [] make_list_ins"
   480 
   481 ML test1
   482 ML test2
   483 
   484 end