src/HOL/MicroJava/BV/JVMType.thy
author haftmann
Mon Mar 01 13:40:23 2010 +0100 (2010-03-01 ago)
changeset 35416 d8d7d1b785af
parent 33954 1bc3b688548c
child 35417 47ee18b6ae32
permissions -rw-r--r--
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
     1 (*  Title:      HOL/MicroJava/BV/JVM.thy
     2     Author:     Gerwin Klein
     3     Copyright   2000 TUM
     4 *)
     5 
     6 header {* \isaheader{The JVM Type System as Semilattice} *}
     7 
     8 theory JVMType
     9 imports JType
    10 begin
    11 
    12 types
    13   locvars_type = "ty err list"
    14   opstack_type = "ty list"
    15   state_type   = "opstack_type \<times> locvars_type"
    16   state        = "state_type option err"    -- "for Kildall"
    17   method_type  = "state_type option list"   -- "for BVSpec"
    18   class_type   = "sig \<Rightarrow> method_type"
    19   prog_type    = "cname \<Rightarrow> class_type"
    20 
    21 
    22 definition stk_esl :: "'c prog \<Rightarrow> nat \<Rightarrow> ty list esl" where
    23   "stk_esl S maxs == upto_esl maxs (JType.esl S)"
    24 
    25 definition reg_sl :: "'c prog \<Rightarrow> nat \<Rightarrow> ty err list sl" where
    26   "reg_sl S maxr == Listn.sl maxr (Err.sl (JType.esl S))"
    27 
    28 definition sl :: "'c prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state sl" where
    29   "sl S maxs maxr ==
    30   Err.sl(Opt.esl(Product.esl (stk_esl S maxs) (Err.esl(reg_sl S maxr))))"
    31 
    32 definition states :: "'c prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state set" where
    33   "states S maxs maxr == fst(sl S maxs maxr)"
    34 
    35 definition le :: "'c prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state ord" where
    36   "le S maxs maxr == fst(snd(sl S maxs maxr))"
    37 
    38 definition  sup :: "'c prog \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> state binop" where
    39   "sup S maxs maxr == snd(snd(sl S maxs maxr))"
    40 
    41 definition sup_ty_opt :: "['code prog,ty err,ty err] \<Rightarrow> bool"
    42                  ("_ |- _ <=o _" [71,71] 70) where 
    43   "sup_ty_opt G == Err.le (subtype G)"
    44 
    45 definition sup_loc :: "['code prog,locvars_type,locvars_type] \<Rightarrow> bool" 
    46               ("_ |- _ <=l _"  [71,71] 70) where
    47   "sup_loc G == Listn.le (sup_ty_opt G)"
    48 
    49 definition sup_state :: "['code prog,state_type,state_type] \<Rightarrow> bool"   
    50                ("_ |- _ <=s _"  [71,71] 70) where
    51   "sup_state G == Product.le (Listn.le (subtype G)) (sup_loc G)"
    52 
    53 definition sup_state_opt :: "['code prog,state_type option,state_type option] \<Rightarrow> bool" 
    54                    ("_ |- _ <=' _"  [71,71] 70) where
    55   "sup_state_opt G == Opt.le (sup_state G)"
    56 
    57 
    58 syntax (xsymbols)
    59   sup_ty_opt    :: "['code prog,ty err,ty err] \<Rightarrow> bool" 
    60                    ("_ \<turnstile> _ <=o _" [71,71] 70)
    61   sup_loc       :: "['code prog,locvars_type,locvars_type] \<Rightarrow> bool" 
    62                    ("_ \<turnstile> _ <=l _" [71,71] 70)
    63   sup_state     :: "['code prog,state_type,state_type] \<Rightarrow> bool" 
    64                    ("_ \<turnstile> _ <=s _" [71,71] 70)
    65   sup_state_opt :: "['code prog,state_type option,state_type option] \<Rightarrow> bool"
    66                    ("_ \<turnstile> _ <=' _" [71,71] 70)
    67                    
    68 
    69 lemma JVM_states_unfold: 
    70   "states S maxs maxr == err(opt((Union {list n (types S) |n. n <= maxs}) <*>
    71                                   list maxr (err(types S))))"
    72   apply (unfold states_def sl_def Opt.esl_def Err.sl_def
    73          stk_esl_def reg_sl_def Product.esl_def
    74          Listn.sl_def upto_esl_def JType.esl_def Err.esl_def)
    75   by simp
    76 
    77 
    78 lemma JVM_le_unfold:
    79  "le S m n == 
    80   Err.le(Opt.le(Product.le(Listn.le(subtype S))(Listn.le(Err.le(subtype S)))))" 
    81   apply (unfold le_def sl_def Opt.esl_def Err.sl_def
    82          stk_esl_def reg_sl_def Product.esl_def  
    83          Listn.sl_def upto_esl_def JType.esl_def Err.esl_def) 
    84   by simp
    85 
    86 lemma JVM_le_convert:
    87   "le G m n (OK t1) (OK t2) = G \<turnstile> t1 <=' t2"
    88   by (simp add: JVM_le_unfold Err.le_def lesub_def sup_state_opt_def 
    89                 sup_state_def sup_loc_def sup_ty_opt_def)
    90 
    91 lemma JVM_le_Err_conv:
    92   "le G m n = Err.le (sup_state_opt G)"
    93   by (unfold sup_state_opt_def sup_state_def sup_loc_def  
    94              sup_ty_opt_def JVM_le_unfold) simp
    95 
    96 lemma zip_map [rule_format]:
    97   "\<forall>a. length a = length b \<longrightarrow> 
    98   zip (map f a) (map g b) = map (\<lambda>(x,y). (f x, g y)) (zip a b)"
    99   apply (induct b) 
   100    apply simp
   101   apply clarsimp
   102   apply (case_tac aa)
   103   apply simp+
   104   done
   105 
   106 lemma [simp]: "Err.le r (OK a) (OK b) = r a b"
   107   by (simp add: Err.le_def lesub_def)
   108 
   109 lemma stk_convert:
   110   "Listn.le (subtype G) a b = G \<turnstile> map OK a <=l map OK b"
   111 proof 
   112   assume "Listn.le (subtype G) a b"
   113 
   114   hence le: "list_all2 (subtype G) a b"
   115     by (unfold Listn.le_def lesub_def)
   116   
   117   { fix x' y'
   118     assume "length a = length b"
   119            "(x',y') \<in> set (zip (map OK a) (map OK b))"
   120     then
   121     obtain x y where OK:
   122       "x' = OK x" "y' = OK y" "(x,y) \<in> set (zip a b)"
   123       by (auto simp add: zip_map)
   124     with le
   125     have "subtype G x y"
   126       by (simp add: list_all2_def Ball_def)
   127     with OK
   128     have "G \<turnstile> x' <=o y'"
   129       by (simp add: sup_ty_opt_def)
   130   }
   131   
   132   with le
   133   show "G \<turnstile> map OK a <=l map OK b"
   134     by (unfold sup_loc_def Listn.le_def lesub_def list_all2_def) auto
   135 next
   136   assume "G \<turnstile> map OK a <=l map OK b"
   137 
   138   thus "Listn.le (subtype G) a b"
   139     apply (unfold sup_loc_def list_all2_def Listn.le_def lesub_def)
   140     apply (clarsimp simp add: zip_map)
   141     apply (drule bspec, assumption)
   142     apply (auto simp add: sup_ty_opt_def subtype_def)
   143     done
   144 qed
   145 
   146 
   147 lemma sup_state_conv:
   148   "(G \<turnstile> s1 <=s s2) == 
   149   (G \<turnstile> map OK (fst s1) <=l map OK (fst s2)) \<and> (G \<turnstile> snd s1 <=l snd s2)"
   150   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def split_beta)
   151 
   152 
   153 lemma subtype_refl [simp]:
   154   "subtype G t t"
   155   by (simp add: subtype_def)
   156 
   157 theorem sup_ty_opt_refl [simp]:
   158   "G \<turnstile> t <=o t"
   159   by (simp add: sup_ty_opt_def Err.le_def lesub_def split: err.split)
   160 
   161 lemma le_list_refl2 [simp]: 
   162   "(\<And>xs. r xs xs) \<Longrightarrow> Listn.le r xs xs"
   163   by (induct xs, auto simp add: Listn.le_def lesub_def)
   164 
   165 theorem sup_loc_refl [simp]:
   166   "G \<turnstile> t <=l t"
   167   by (simp add: sup_loc_def)
   168 
   169 theorem sup_state_refl [simp]:
   170   "G \<turnstile> s <=s s"
   171   by (auto simp add: sup_state_def Product.le_def lesub_def)
   172 
   173 theorem sup_state_opt_refl [simp]:
   174   "G \<turnstile> s <=' s"
   175   by (simp add: sup_state_opt_def Opt.le_def lesub_def split: option.split)
   176   
   177 
   178 theorem anyConvErr [simp]:
   179   "(G \<turnstile> Err <=o any) = (any = Err)"
   180   by (simp add: sup_ty_opt_def Err.le_def split: err.split)
   181 
   182 theorem OKanyConvOK [simp]:
   183   "(G \<turnstile> (OK ty') <=o (OK ty)) = (G \<turnstile> ty' \<preceq> ty)"
   184   by (simp add: sup_ty_opt_def Err.le_def lesub_def subtype_def)
   185 
   186 theorem sup_ty_opt_OK:
   187   "G \<turnstile> a <=o (OK b) \<Longrightarrow> \<exists> x. a = OK x"
   188   by (clarsimp simp add: sup_ty_opt_def Err.le_def split: err.splits)
   189 
   190 lemma widen_PrimT_conv1 [simp]:
   191   "\<lbrakk> G \<turnstile> S \<preceq> T; S = PrimT x\<rbrakk> \<Longrightarrow> T = PrimT x"
   192   by (auto elim: widen.cases)
   193 
   194 theorem sup_PTS_eq:
   195   "(G \<turnstile> OK (PrimT p) <=o X) = (X=Err \<or> X = OK (PrimT p))"
   196   by (auto simp add: sup_ty_opt_def Err.le_def lesub_def subtype_def 
   197               split: err.splits)
   198 
   199 theorem sup_loc_Nil [iff]:
   200   "(G \<turnstile> [] <=l XT) = (XT=[])"
   201   by (simp add: sup_loc_def Listn.le_def)
   202 
   203 theorem sup_loc_Cons [iff]:
   204   "(G \<turnstile> (Y#YT) <=l XT) = (\<exists>X XT'. XT=X#XT' \<and> (G \<turnstile> Y <=o X) \<and> (G \<turnstile> YT <=l XT'))"
   205   by (simp add: sup_loc_def Listn.le_def lesub_def list_all2_Cons1)
   206 
   207 theorem sup_loc_Cons2:
   208   "(G \<turnstile> YT <=l (X#XT)) = (\<exists>Y YT'. YT=Y#YT' \<and> (G \<turnstile> Y <=o X) \<and> (G \<turnstile> YT' <=l XT))"
   209   by (simp add: sup_loc_def Listn.le_def lesub_def list_all2_Cons2)
   210 
   211 lemma sup_state_Cons:
   212   "(G \<turnstile> (x#xt, a) <=s (y#yt, b)) = 
   213    ((G \<turnstile> x \<preceq> y) \<and> (G \<turnstile> (xt,a) <=s (yt,b)))"
   214   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def)
   215 
   216 
   217 theorem sup_loc_length:
   218   "G \<turnstile> a <=l b \<Longrightarrow> length a = length b"
   219 proof -
   220   assume G: "G \<turnstile> a <=l b"
   221   have "\<forall>b. (G \<turnstile> a <=l b) \<longrightarrow> length a = length b"
   222     by (induct a, auto)
   223   with G
   224   show ?thesis by blast
   225 qed
   226 
   227 theorem sup_loc_nth:
   228   "\<lbrakk> G \<turnstile> a <=l b; n < length a \<rbrakk> \<Longrightarrow> G \<turnstile> (a!n) <=o (b!n)"
   229 proof -
   230   assume a: "G \<turnstile> a <=l b" "n < length a"
   231   have "\<forall> n b. (G \<turnstile> a <=l b) \<longrightarrow> n < length a \<longrightarrow> (G \<turnstile> (a!n) <=o (b!n))"
   232     (is "?P a")
   233   proof (induct a)
   234     show "?P []" by simp
   235     
   236     fix x xs assume IH: "?P xs"
   237 
   238     show "?P (x#xs)"
   239     proof (intro strip)
   240       fix n b
   241       assume "G \<turnstile> (x # xs) <=l b" "n < length (x # xs)"
   242       with IH
   243       show "G \<turnstile> ((x # xs) ! n) <=o (b ! n)"
   244         by - (cases n, auto)
   245     qed
   246   qed
   247   with a
   248   show ?thesis by blast
   249 qed
   250 
   251 theorem all_nth_sup_loc:
   252   "\<forall>b. length a = length b \<longrightarrow> (\<forall> n. n < length a \<longrightarrow> (G \<turnstile> (a!n) <=o (b!n))) 
   253   \<longrightarrow> (G \<turnstile> a <=l b)" (is "?P a")
   254 proof (induct a)
   255   show "?P []" by simp
   256 
   257   fix l ls assume IH: "?P ls"
   258   
   259   show "?P (l#ls)"
   260   proof (intro strip)
   261     fix b
   262     assume f: "\<forall>n. n < length (l # ls) \<longrightarrow> (G \<turnstile> ((l # ls) ! n) <=o (b ! n))"
   263     assume l: "length (l#ls) = length b"
   264     
   265     then obtain b' bs where b: "b = b'#bs"
   266       by - (cases b, simp, simp add: neq_Nil_conv, rule that)
   267 
   268     with f
   269     have "\<forall>n. n < length ls \<longrightarrow> (G \<turnstile> (ls!n) <=o (bs!n))"
   270       by auto
   271 
   272     with f b l IH
   273     show "G \<turnstile> (l # ls) <=l b"
   274       by auto
   275   qed
   276 qed
   277 
   278 
   279 theorem sup_loc_append:
   280   "length a = length b \<Longrightarrow> 
   281    (G \<turnstile> (a@x) <=l (b@y)) = ((G \<turnstile> a <=l b) \<and> (G \<turnstile> x <=l y))"
   282 proof -
   283   assume l: "length a = length b"
   284 
   285   have "\<forall>b. length a = length b \<longrightarrow> (G \<turnstile> (a@x) <=l (b@y)) = ((G \<turnstile> a <=l b) \<and> 
   286             (G \<turnstile> x <=l y))" (is "?P a") 
   287   proof (induct a)
   288     show "?P []" by simp
   289     
   290     fix l ls assume IH: "?P ls"    
   291     show "?P (l#ls)" 
   292     proof (intro strip)
   293       fix b
   294       assume "length (l#ls) = length (b::ty err list)"
   295       with IH
   296       show "(G \<turnstile> ((l#ls)@x) <=l (b@y)) = ((G \<turnstile> (l#ls) <=l b) \<and> (G \<turnstile> x <=l y))"
   297         by - (cases b, auto)
   298     qed
   299   qed
   300   with l
   301   show ?thesis by blast
   302 qed
   303 
   304 theorem sup_loc_rev [simp]:
   305   "(G \<turnstile> (rev a) <=l rev b) = (G \<turnstile> a <=l b)"
   306 proof -
   307   have "\<forall>b. (G \<turnstile> (rev a) <=l rev b) = (G \<turnstile> a <=l b)" (is "\<forall>b. ?Q a b" is "?P a")
   308   proof (induct a)
   309     show "?P []" by simp
   310 
   311     fix l ls assume IH: "?P ls"
   312 
   313     { 
   314       fix b
   315       have "?Q (l#ls) b"
   316       proof (cases b)
   317         case Nil
   318         thus ?thesis by (auto dest: sup_loc_length)
   319       next
   320         case (Cons a list)
   321         show ?thesis
   322         proof
   323           assume "G \<turnstile> (l # ls) <=l b"
   324           thus "G \<turnstile> rev (l # ls) <=l rev b"
   325             by (clarsimp simp add: Cons IH sup_loc_length sup_loc_append)
   326         next
   327           assume "G \<turnstile> rev (l # ls) <=l rev b"
   328           hence G: "G \<turnstile> (rev ls @ [l]) <=l (rev list @ [a])"
   329             by (simp add: Cons)          
   330           
   331           hence "length (rev ls) = length (rev list)"
   332             by (auto dest: sup_loc_length)
   333 
   334           from this G
   335           obtain "G \<turnstile> rev ls <=l rev list" "G \<turnstile> l <=o a"
   336             by (simp add: sup_loc_append)
   337 
   338           thus "G \<turnstile> (l # ls) <=l b"
   339             by (simp add: Cons IH)
   340         qed
   341       qed    
   342     }
   343     thus "?P (l#ls)" by blast
   344   qed
   345 
   346   thus ?thesis by blast
   347 qed
   348 
   349 
   350 theorem sup_loc_update [rule_format]:
   351   "\<forall> n y. (G \<turnstile> a <=o b) \<longrightarrow> n < length y \<longrightarrow> (G \<turnstile> x <=l y) \<longrightarrow> 
   352           (G \<turnstile> x[n := a] <=l y[n := b])" (is "?P x")
   353 proof (induct x)
   354   show "?P []" by simp
   355 
   356   fix l ls assume IH: "?P ls"
   357   show "?P (l#ls)"
   358   proof (intro strip)
   359     fix n y
   360     assume "G \<turnstile>a <=o b" "G \<turnstile> (l # ls) <=l y" "n < length y"
   361     with IH
   362     show "G \<turnstile> (l # ls)[n := a] <=l y[n := b]"
   363       by - (cases n, auto simp add: sup_loc_Cons2 list_all2_Cons1)
   364   qed
   365 qed
   366 
   367 
   368 theorem sup_state_length [simp]:
   369   "G \<turnstile> s2 <=s s1 \<Longrightarrow> 
   370    length (fst s2) = length (fst s1) \<and> length (snd s2) = length (snd s1)"
   371   by (auto dest: sup_loc_length simp add: sup_state_def stk_convert lesub_def Product.le_def);
   372 
   373 theorem sup_state_append_snd:
   374   "length a = length b \<Longrightarrow> 
   375   (G \<turnstile> (i,a@x) <=s (j,b@y)) = ((G \<turnstile> (i,a) <=s (j,b)) \<and> (G \<turnstile> (i,x) <=s (j,y)))"
   376   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def sup_loc_append)
   377 
   378 theorem sup_state_append_fst:
   379   "length a = length b \<Longrightarrow> 
   380   (G \<turnstile> (a@x,i) <=s (b@y,j)) = ((G \<turnstile> (a,i) <=s (b,j)) \<and> (G \<turnstile> (x,i) <=s (y,j)))"
   381   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def sup_loc_append)
   382 
   383 theorem sup_state_Cons1:
   384   "(G \<turnstile> (x#xt, a) <=s (yt, b)) = 
   385    (\<exists>y yt'. yt=y#yt' \<and> (G \<turnstile> x \<preceq> y) \<and> (G \<turnstile> (xt,a) <=s (yt',b)))"
   386   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def)
   387 
   388 theorem sup_state_Cons2:
   389   "(G \<turnstile> (xt, a) <=s (y#yt, b)) = 
   390    (\<exists>x xt'. xt=x#xt' \<and> (G \<turnstile> x \<preceq> y) \<and> (G \<turnstile> (xt',a) <=s (yt,b)))"
   391   by (auto simp add: sup_state_def stk_convert lesub_def Product.le_def sup_loc_Cons2)
   392 
   393 theorem sup_state_ignore_fst:  
   394   "G \<turnstile> (a, x) <=s (b, y) \<Longrightarrow> G \<turnstile> (c, x) <=s (c, y)"
   395   by (simp add: sup_state_def lesub_def Product.le_def)
   396 
   397 theorem sup_state_rev_fst:
   398   "(G \<turnstile> (rev a, x) <=s (rev b, y)) = (G \<turnstile> (a, x) <=s (b, y))"
   399 proof -
   400   have m: "\<And>f x. map f (rev x) = rev (map f x)" by (simp add: rev_map)
   401   show ?thesis by (simp add: m sup_state_def stk_convert lesub_def Product.le_def)
   402 qed
   403   
   404 
   405 lemma sup_state_opt_None_any [iff]:
   406   "(G \<turnstile> None <=' any) = True"
   407   by (simp add: sup_state_opt_def Opt.le_def split: option.split)
   408 
   409 lemma sup_state_opt_any_None [iff]:
   410   "(G \<turnstile> any <=' None) = (any = None)"
   411   by (simp add: sup_state_opt_def Opt.le_def split: option.split)
   412 
   413 lemma sup_state_opt_Some_Some [iff]:
   414   "(G \<turnstile> (Some a) <=' (Some b)) = (G \<turnstile> a <=s b)"
   415   by (simp add: sup_state_opt_def Opt.le_def lesub_def del: split_paired_Ex)
   416 
   417 lemma sup_state_opt_any_Some [iff]:
   418   "(G \<turnstile> (Some a) <=' any) = (\<exists>b. any = Some b \<and> G \<turnstile> a <=s b)"
   419   by (simp add: sup_state_opt_def Opt.le_def lesub_def split: option.split)
   420 
   421 lemma sup_state_opt_Some_any:
   422   "(G \<turnstile> any <=' (Some b)) = (any = None \<or> (\<exists>a. any = Some a \<and> G \<turnstile> a <=s b))"
   423   by (simp add: sup_state_opt_def Opt.le_def lesub_def split: option.split)
   424 
   425 
   426 theorem sup_ty_opt_trans [trans]:
   427   "\<lbrakk>G \<turnstile> a <=o b; G \<turnstile> b <=o c\<rbrakk> \<Longrightarrow> G \<turnstile> a <=o c"
   428   by (auto intro: widen_trans 
   429            simp add: sup_ty_opt_def Err.le_def lesub_def subtype_def 
   430            split: err.splits)
   431 
   432 theorem sup_loc_trans [trans]:
   433   "\<lbrakk>G \<turnstile> a <=l b; G \<turnstile> b <=l c\<rbrakk> \<Longrightarrow> G \<turnstile> a <=l c"
   434 proof -
   435   assume G: "G \<turnstile> a <=l b" "G \<turnstile> b <=l c"
   436  
   437   hence "\<forall> n. n < length a \<longrightarrow> (G \<turnstile> (a!n) <=o (c!n))"
   438   proof (intro strip)
   439     fix n 
   440     assume n: "n < length a"
   441     with G
   442     have "G \<turnstile> (a!n) <=o (b!n)"
   443       by - (rule sup_loc_nth)
   444     also 
   445     from n G
   446     have "G \<turnstile> \<dots> <=o (c!n)"
   447       by - (rule sup_loc_nth, auto dest: sup_loc_length)
   448     finally
   449     show "G \<turnstile> (a!n) <=o (c!n)" .
   450   qed
   451 
   452   with G
   453   show ?thesis 
   454     by (auto intro!: all_nth_sup_loc [rule_format] dest!: sup_loc_length) 
   455 qed
   456   
   457 
   458 theorem sup_state_trans [trans]:
   459   "\<lbrakk>G \<turnstile> a <=s b; G \<turnstile> b <=s c\<rbrakk> \<Longrightarrow> G \<turnstile> a <=s c"
   460   by (auto intro: sup_loc_trans simp add: sup_state_def stk_convert Product.le_def lesub_def)
   461 
   462 theorem sup_state_opt_trans [trans]:
   463   "\<lbrakk>G \<turnstile> a <=' b; G \<turnstile> b <=' c\<rbrakk> \<Longrightarrow> G \<turnstile> a <=' c"
   464   by (auto intro: sup_state_trans 
   465            simp add: sup_state_opt_def Opt.le_def lesub_def 
   466            split: option.splits)
   467 
   468 end