src/HOL/MicroJava/BV/BVExample.thy
author nipkow
Thu May 30 10:12:52 2002 +0200 (2002-05-30)
changeset 13187 e5434b822a96
parent 13148 fe118a977a6d
child 13214 2aa33ed5f526
permissions -rw-r--r--
Modifications due to enhanced linear arithmetic.
     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 lemma wt_append [simp]:
   256   "wt_method E list_name [Class list_name] (PrimT Void) 3 0 append_ins
   257              [(Suc 0, 2, 8, Xcpt NullPointer)] \<phi>\<^sub>a"
   258   apply (simp add: wt_method_def append_ins_def phi_append_def 
   259                    wt_start_def wt_instr_def)
   260   apply clarify
   261   apply (elim pc_end pc_next pc_0)
   262   apply simp
   263   apply (fastsimp simp add: match_exception_entry_def sup_state_conv subcls1)
   264   apply simp
   265   apply simp
   266   apply (fastsimp simp add: sup_state_conv subcls1)
   267   apply simp
   268   apply (simp add: app_def xcpt_app_def)
   269   apply simp
   270   apply simp
   271   apply simp
   272   apply (simp add: match_exception_entry_def)
   273   apply (simp add: match_exception_entry_def)
   274   apply simp
   275   apply simp
   276   done
   277 
   278 text {* Some abbreviations for readability *} 
   279 syntax
   280   list :: ty 
   281   test :: ty
   282 translations
   283   "list" == "Class list_name"
   284   "test" == "Class test_name"
   285 
   286 constdefs
   287   phi_makelist :: method_type ("\<phi>\<^sub>m")
   288   "\<phi>\<^sub>m \<equiv> map (\<lambda>(x,y). Some (x, y)) [ 
   289     (                                   [], [OK test, Err    , Err    ]),
   290     (                               [list], [OK test, Err    , Err    ]),
   291     (                         [list, list], [OK test, Err    , Err    ]),
   292     (                               [list], [OK list, Err    , Err    ]),
   293     (                [PrimT Integer, list], [OK list, Err    , Err    ]),
   294     (                                   [], [OK list, Err    , Err    ]),
   295     (                               [list], [OK list, Err    , Err    ]),
   296     (                         [list, list], [OK list, Err    , Err    ]),
   297     (                               [list], [OK list, OK list, Err    ]),
   298     (                [PrimT Integer, list], [OK list, OK list, Err    ]),
   299     (                                   [], [OK list, OK list, Err    ]),
   300     (                               [list], [OK list, OK list, Err    ]),
   301     (                         [list, list], [OK list, OK list, Err    ]),
   302     (                               [list], [OK list, OK list, OK list]),
   303     (                [PrimT Integer, list], [OK list, OK list, OK list]),
   304     (                                   [], [OK list, OK list, OK list]),
   305     (                               [list], [OK list, OK list, OK list]),
   306     (                         [list, list], [OK list, OK list, OK list]),
   307     (                         [PrimT Void], [OK list, OK list, OK list]),
   308     (                                   [], [OK list, OK list, OK list]),
   309     (                               [list], [OK list, OK list, OK list]),
   310     (                         [list, list], [OK list, OK list, OK list]),
   311     (                         [PrimT Void], [OK list, OK list, OK list])]"
   312 
   313 lemma wt_makelist [simp]:
   314   "wt_method E test_name [] (PrimT Void) 3 2 make_list_ins [] \<phi>\<^sub>m"
   315   apply (simp add: wt_method_def make_list_ins_def phi_makelist_def)
   316   apply (simp add: wt_start_def nat_number)
   317   apply (simp add: wt_instr_def)
   318   apply clarify
   319   apply (elim pc_end pc_next pc_0)
   320   apply (simp add: match_exception_entry_def)
   321   apply simp
   322   apply simp
   323   apply simp
   324   apply (simp add: match_exception_entry_def)
   325   apply (simp add: match_exception_entry_def) 
   326   apply simp
   327   apply simp
   328   apply simp
   329   apply (simp add: match_exception_entry_def)
   330   apply (simp add: match_exception_entry_def) 
   331   apply simp
   332   apply simp
   333   apply simp
   334   apply (simp add: match_exception_entry_def)
   335   apply (simp add: match_exception_entry_def) 
   336   apply simp
   337   apply (simp add: app_def xcpt_app_def)
   338   apply simp 
   339   apply simp
   340   apply simp
   341   apply (simp add: app_def xcpt_app_def) 
   342   apply simp
   343   done
   344 
   345 text {* The whole program is welltyped: *}
   346 constdefs 
   347   Phi :: prog_type ("\<Phi>")
   348   "\<Phi> C sg \<equiv> if C = test_name \<and> sg = (makelist_name, []) then \<phi>\<^sub>m else          
   349              if C = list_name \<and> sg = (append_name, [Class list_name]) then \<phi>\<^sub>a else []"
   350 
   351 lemma wf_prog:
   352   "wt_jvm_prog E \<Phi>" 
   353   apply (unfold wt_jvm_prog_def)
   354   apply (rule wf_mb'E [OF wf_struct])
   355   apply (simp add: E_def)
   356   apply clarify
   357   apply (fold E_def)
   358   apply (simp add: system_defs class_defs Phi_def) 
   359   apply auto
   360   done 
   361 
   362 
   363 section "Conformance"
   364 text {* Execution of the program will be typesafe, because its
   365   start state conforms to the welltyping: *}
   366 
   367 lemma "E,\<Phi> \<turnstile>JVM start_state E test_name makelist_name \<surd>"
   368   apply (rule BV_correct_initial)
   369     apply (rule wf_prog)
   370    apply simp
   371   apply simp
   372   done
   373 
   374 
   375 section "Example for code generation: inferring method types"
   376 
   377 constdefs
   378   test_kil :: "jvm_prog \<Rightarrow> cname \<Rightarrow> ty List.list \<Rightarrow> ty \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 
   379              exception_table \<Rightarrow> instr List.list \<Rightarrow> JVMType.state List.list"
   380   "test_kil G C pTs rT mxs mxl et instr ==
   381    (let first  = Some ([],(OK (Class C))#((map OK pTs))@(replicate mxl Err));
   382         start  = OK first#(replicate (size instr - 1) (OK None))
   383     in  kiljvm G mxs (1+size pTs+mxl) rT et instr start)"
   384 
   385 lemma [code]:
   386   "unstables r step ss = (UN p:{..size ss(}. if \<not>stable r step ss p then {p} else {})"
   387   apply (unfold unstables_def)
   388   apply (rule equalityI)
   389   apply (rule subsetI)
   390   apply (erule CollectE)
   391   apply (erule conjE)
   392   apply (rule UN_I)
   393   apply simp
   394   apply simp
   395   apply (rule subsetI)
   396   apply (erule UN_E)
   397   apply (case_tac "\<not> stable r step ss p")
   398   apply simp+
   399   done
   400 
   401 lemmas [code] = lessThan_0 lessThan_Suc
   402 
   403 constdefs
   404   some_elem :: "'a set \<Rightarrow> 'a"
   405   "some_elem == (%S. SOME x. x : S)"
   406 
   407 lemma [code]:
   408 "iter f step ss w =
   409  while (%(ss,w). w \<noteq> {})
   410        (%(ss,w). let p = some_elem w
   411                  in propa f (step p (ss!p)) ss (w-{p}))
   412        (ss,w)"
   413   by (unfold iter_def some_elem_def, rule refl)
   414 
   415 types_code
   416   set ("_ list")
   417 
   418 consts_code
   419   "{}"     ("[]")
   420   "insert" ("(_ ins _)")
   421   "op :"   ("(_ mem _)")
   422   "op Un"  ("(_ union _)")
   423   "image"  ("map")
   424   "UNION"  ("(fn A => fn f => flat (map f A))")
   425   "Bex"    ("(fn A => fn f => exists f A)")
   426   "Ball"   ("(fn A => fn f => forall f A)")
   427   "some_elem" ("hd")
   428   "op -" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  ("(_ \\ _)")
   429 
   430 lemma JVM_sup_unfold [code]:
   431  "JVMType.sup S m n = lift2 (Opt.sup
   432        (Product.sup (Listn.sup (JType.sup S))
   433          (\<lambda>x y. OK (map2 (lift2 (JType.sup S)) x y))))" 
   434   apply (unfold JVMType.sup_def JVMType.sl_def Opt.esl_def Err.sl_def
   435          stk_esl_def reg_sl_def Product.esl_def  
   436          Listn.sl_def upto_esl_def JType.esl_def Err.esl_def) 
   437   by simp
   438 
   439 lemmas [code] =
   440   meta_eq_to_obj_eq [OF JType.sup_def [unfolded exec_lub_def]]
   441   meta_eq_to_obj_eq [OF JVM_le_unfold]
   442 
   443 lemmas [code ind] = rtrancl_refl converse_rtrancl_into_rtrancl
   444 
   445 generate_code 
   446   test1 = "test_kil E list_name [Class list_name] (PrimT Void) 3 0
   447     [(Suc 0, 2, 8, Xcpt NullPointer)] append_ins"
   448   test2 = "test_kil E test_name [] (PrimT Void) 3 2 [] make_list_ins"
   449 
   450 ML test1
   451 ML test2
   452 
   453 end