src/HOL/MicroJava/BV/BVExample.thy
author kleing
Tue May 14 12:33:42 2002 +0200 (2002-05-14)
changeset 13148 fe118a977a6d
parent 13139 94648e0e4506
child 13187 e5434b822a96
permissions -rw-r--r--
numerals work again
     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   apply arith
   179   done
   180 
   181 lemma pc_end: "x \<in> [n,n) \<Longrightarrow> P x" 
   182   by (unfold intervall_def) arith
   183 
   184 
   185 section "Program structure"
   186 
   187 text {*
   188   The program is structurally wellformed:
   189 *}
   190 lemma wf_struct:
   191   "wf_prog (\<lambda>G C mb. True) E" (is "wf_prog ?mb E")
   192 proof -
   193   have "unique E" 
   194     by (simp add: system_defs E_def class_defs name_defs distinct_classes)
   195   moreover
   196   have "set SystemClasses \<subseteq> set E" by (simp add: system_defs E_def)
   197   hence "wf_syscls E" by (rule wf_syscls)
   198   moreover
   199   have "wf_cdecl ?mb E ObjectC" by (simp add: wf_cdecl_def ObjectC_def)
   200   moreover
   201   have "wf_cdecl ?mb E NullPointerC" 
   202     by (auto elim: notin_rtrancl 
   203             simp add: wf_cdecl_def name_defs NullPointerC_def subcls1)
   204   moreover
   205   have "wf_cdecl ?mb E ClassCastC" 
   206     by (auto elim: notin_rtrancl 
   207             simp add: wf_cdecl_def name_defs ClassCastC_def subcls1)
   208   moreover
   209   have "wf_cdecl ?mb E OutOfMemoryC" 
   210     by (auto elim: notin_rtrancl 
   211             simp add: wf_cdecl_def name_defs OutOfMemoryC_def subcls1)
   212   moreover
   213   have "wf_cdecl ?mb E (list_name, list_class)"
   214     apply (auto elim!: notin_rtrancl 
   215             simp add: wf_cdecl_def wf_fdecl_def list_class_def 
   216                       wf_mdecl_def wf_mhead_def subcls1)
   217     apply (auto simp add: name_defs distinct_classes distinct_fields)
   218     done    
   219   moreover
   220   have "wf_cdecl ?mb E (test_name, test_class)" 
   221     apply (auto elim!: notin_rtrancl 
   222             simp add: wf_cdecl_def wf_fdecl_def test_class_def 
   223                       wf_mdecl_def wf_mhead_def subcls1)
   224     apply (auto simp add: name_defs distinct_classes distinct_fields)
   225     done       
   226   ultimately
   227   show ?thesis by (simp add: wf_prog_def E_def SystemClasses_def)
   228 qed
   229 
   230 section "Welltypings"
   231 text {*
   232   We show welltypings of the methods @{term append_name} in class @{term list_name}, 
   233   and @{term makelist_name} in class @{term test_name}:
   234 *}
   235 lemmas eff_simps [simp] = eff_def norm_eff_def xcpt_eff_def
   236 declare appInvoke [simp del]
   237 
   238 constdefs
   239   phi_append :: method_type ("\<phi>\<^sub>a")
   240   "\<phi>\<^sub>a \<equiv> map (\<lambda>(x,y). Some (x, map OK y)) [ 
   241    (                                    [], [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]),
   244    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   245    ([NT, Class list_name, Class list_name], [Class list_name, Class list_name]),
   246    (                     [Class list_name], [Class list_name, Class list_name]),
   247    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   248    (                          [PrimT Void], [Class list_name, Class list_name]),
   249    (                        [Class Object], [Class list_name, Class list_name]),
   250    (                                    [], [Class list_name, Class list_name]),
   251    (                     [Class list_name], [Class list_name, Class list_name]),
   252    (    [Class list_name, Class list_name], [Class list_name, Class list_name]),
   253    (                                    [], [Class list_name, Class list_name]),
   254    (                          [PrimT Void], [Class list_name, Class list_name])]"
   255 
   256 lemma wt_append [simp]:
   257   "wt_method E list_name [Class list_name] (PrimT Void) 3 0 append_ins
   258              [(Suc 0, 2, 8, Xcpt NullPointer)] \<phi>\<^sub>a"
   259   apply (simp add: wt_method_def append_ins_def phi_append_def 
   260                    wt_start_def wt_instr_def)
   261   apply clarify
   262   apply (elim pc_end pc_next pc_0)
   263   apply simp
   264   apply (fastsimp simp add: match_exception_entry_def sup_state_conv subcls1)
   265   apply simp
   266   apply simp
   267   apply (fastsimp simp add: sup_state_conv subcls1)
   268   apply simp
   269   apply (simp add: app_def xcpt_app_def)
   270   apply simp
   271   apply simp
   272   apply simp
   273   apply (simp add: match_exception_entry_def)
   274   apply (simp add: match_exception_entry_def)
   275   apply simp
   276   apply simp
   277   done
   278 
   279 text {* Some abbreviations for readability *} 
   280 syntax
   281   list :: ty 
   282   test :: ty
   283 translations
   284   "list" == "Class list_name"
   285   "test" == "Class test_name"
   286 
   287 constdefs
   288   phi_makelist :: method_type ("\<phi>\<^sub>m")
   289   "\<phi>\<^sub>m \<equiv> map (\<lambda>(x,y). Some (x, y)) [ 
   290     (                                   [], [OK test, Err    , Err    ]),
   291     (                               [list], [OK test, Err    , Err    ]),
   292     (                         [list, list], [OK test, Err    , Err    ]),
   293     (                               [list], [OK list, Err    , Err    ]),
   294     (                [PrimT Integer, list], [OK list, Err    , Err    ]),
   295     (                                   [], [OK list, Err    , Err    ]),
   296     (                               [list], [OK list, Err    , Err    ]),
   297     (                         [list, list], [OK list, Err    , Err    ]),
   298     (                               [list], [OK list, OK list, Err    ]),
   299     (                [PrimT Integer, list], [OK list, OK list, Err    ]),
   300     (                                   [], [OK list, OK list, Err    ]),
   301     (                               [list], [OK list, OK list, Err    ]),
   302     (                         [list, list], [OK list, OK list, Err    ]),
   303     (                               [list], [OK list, OK list, OK list]),
   304     (                [PrimT Integer, list], [OK list, OK list, OK list]),
   305     (                                   [], [OK list, OK list, OK list]),
   306     (                               [list], [OK list, OK list, OK list]),
   307     (                         [list, list], [OK list, OK list, OK list]),
   308     (                         [PrimT Void], [OK list, OK list, OK list]),
   309     (                                   [], [OK list, OK list, OK list]),
   310     (                               [list], [OK list, OK list, OK list]),
   311     (                         [list, list], [OK list, OK list, OK list]),
   312     (                         [PrimT Void], [OK list, OK list, OK list])]"
   313 
   314 lemma wt_makelist [simp]:
   315   "wt_method E test_name [] (PrimT Void) 3 2 make_list_ins [] \<phi>\<^sub>m"
   316   apply (simp add: wt_method_def make_list_ins_def phi_makelist_def)
   317   apply (simp add: wt_start_def nat_number)
   318   apply (simp add: wt_instr_def)
   319   apply clarify
   320   apply (elim pc_end pc_next pc_0)
   321   apply (simp add: match_exception_entry_def)
   322   apply simp
   323   apply simp
   324   apply simp
   325   apply (simp add: match_exception_entry_def)
   326   apply (simp add: match_exception_entry_def) 
   327   apply simp
   328   apply simp
   329   apply simp
   330   apply (simp add: match_exception_entry_def)
   331   apply (simp add: match_exception_entry_def) 
   332   apply simp
   333   apply simp
   334   apply simp
   335   apply (simp add: match_exception_entry_def)
   336   apply (simp add: match_exception_entry_def) 
   337   apply simp
   338   apply (simp add: app_def xcpt_app_def)
   339   apply simp 
   340   apply simp
   341   apply simp
   342   apply (simp add: app_def xcpt_app_def) 
   343   apply simp
   344   done
   345 
   346 text {* The whole program is welltyped: *}
   347 constdefs 
   348   Phi :: prog_type ("\<Phi>")
   349   "\<Phi> C sg \<equiv> if C = test_name \<and> sg = (makelist_name, []) then \<phi>\<^sub>m else          
   350              if C = list_name \<and> sg = (append_name, [Class list_name]) then \<phi>\<^sub>a else []"
   351 
   352 lemma wf_prog:
   353   "wt_jvm_prog E \<Phi>" 
   354   apply (unfold wt_jvm_prog_def)
   355   apply (rule wf_mb'E [OF wf_struct])
   356   apply (simp add: E_def)
   357   apply clarify
   358   apply (fold E_def)
   359   apply (simp add: system_defs class_defs Phi_def) 
   360   apply auto
   361   done 
   362 
   363 
   364 section "Conformance"
   365 text {* Execution of the program will be typesafe, because its
   366   start state conforms to the welltyping: *}
   367 
   368 lemma "E,\<Phi> \<turnstile>JVM start_state E test_name makelist_name \<surd>"
   369   apply (rule BV_correct_initial)
   370     apply (rule wf_prog)
   371    apply simp
   372   apply simp
   373   done
   374 
   375 
   376 section "Example for code generation: inferring method types"
   377 
   378 constdefs
   379   test_kil :: "jvm_prog \<Rightarrow> cname \<Rightarrow> ty List.list \<Rightarrow> ty \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 
   380              exception_table \<Rightarrow> instr List.list \<Rightarrow> JVMType.state List.list"
   381   "test_kil G C pTs rT mxs mxl et instr ==
   382    (let first  = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
   383         start  = OK first#(replicate (size instr - 1) (OK None))
   384     in  kiljvm G mxs (1+size pTs+mxl) rT et instr start)"
   385 
   386 lemma [code]:
   387   "unstables r step ss = (UN p:{..size ss(}. if \<not>stable r step ss p then {p} else {})"
   388   apply (unfold unstables_def)
   389   apply (rule equalityI)
   390   apply (rule subsetI)
   391   apply (erule CollectE)
   392   apply (erule conjE)
   393   apply (rule UN_I)
   394   apply simp
   395   apply simp
   396   apply (rule subsetI)
   397   apply (erule UN_E)
   398   apply (case_tac "\<not> stable r step ss p")
   399   apply simp+
   400   done
   401 
   402 lemmas [code] = lessThan_0 lessThan_Suc
   403 
   404 constdefs
   405   some_elem :: "'a set \<Rightarrow> 'a"
   406   "some_elem == (%S. SOME x. x : S)"
   407 
   408 lemma [code]:
   409 "iter f step ss w =
   410  while (%(ss,w). w \<noteq> {})
   411        (%(ss,w). let p = some_elem w
   412                  in propa f (step p (ss!p)) ss (w-{p}))
   413        (ss,w)"
   414   by (unfold iter_def some_elem_def, rule refl)
   415 
   416 types_code
   417   set ("_ list")
   418 
   419 consts_code
   420   "{}"     ("[]")
   421   "insert" ("(_ ins _)")
   422   "op :"   ("(_ mem _)")
   423   "op Un"  ("(_ union _)")
   424   "image"  ("map")
   425   "UNION"  ("(fn A => fn f => flat (map f A))")
   426   "Bex"    ("(fn A => fn f => exists f A)")
   427   "Ball"   ("(fn A => fn f => forall f A)")
   428   "some_elem" ("hd")
   429   "op -" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  ("(_ \\ _)")
   430 
   431 lemma JVM_sup_unfold [code]:
   432  "JVMType.sup S m n = lift2 (Opt.sup
   433        (Product.sup (Listn.sup (JType.sup S))
   434          (\<lambda>x y. OK (map2 (lift2 (JType.sup S)) x y))))" 
   435   apply (unfold JVMType.sup_def JVMType.sl_def Opt.esl_def Err.sl_def
   436          stk_esl_def reg_sl_def Product.esl_def  
   437          Listn.sl_def upto_esl_def JType.esl_def Err.esl_def) 
   438   by simp
   439 
   440 lemmas [code] =
   441   meta_eq_to_obj_eq [OF JType.sup_def [unfolded exec_lub_def]]
   442   meta_eq_to_obj_eq [OF JVM_le_unfold]
   443 
   444 lemmas [code ind] = rtrancl_refl converse_rtrancl_into_rtrancl
   445 
   446 generate_code 
   447   test1 = "test_kil E list_name [Class list_name] (PrimT Void) 3 0
   448     [(Suc 0, 2, 8, Xcpt NullPointer)] append_ins"
   449   test2 = "test_kil E test_name [] (PrimT Void) 3 2 [] make_list_ins"
   450 
   451 ML test1
   452 ML test2
   453 
   454 end