src/HOL/IMP/Abs_Int1.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 45200 1f1897ac7877
child 45623 f682f3f7b726
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
     1 (* Author: Tobias Nipkow *)
     2 
     3 theory Abs_Int1
     4 imports Abs_Int0_const
     5 begin
     6 
     7 instantiation prod :: (preord,preord) preord
     8 begin
     9 
    10 definition "le_prod p1 p2 = (fst p1 \<sqsubseteq> fst p2 \<and> snd p1 \<sqsubseteq> snd p2)"
    11 
    12 instance
    13 proof
    14   case goal1 show ?case by(simp add: le_prod_def)
    15 next
    16   case goal2 thus ?case unfolding le_prod_def by(metis le_trans)
    17 qed
    18 
    19 end
    20 
    21 
    22 subsection "Backward Analysis of Expressions"
    23 
    24 hide_const bot
    25 
    26 class L_top_bot = SL_top +
    27 fixes meet :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 65)
    28 and bot :: "'a" ("\<bottom>")
    29 assumes meet_le1 [simp]: "x \<sqinter> y \<sqsubseteq> x"
    30 and meet_le2 [simp]: "x \<sqinter> y \<sqsubseteq> y"
    31 and meet_greatest: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<sqinter> z"
    32 assumes bot[simp]: "\<bottom> \<sqsubseteq> x"
    33 begin
    34 
    35 lemma mono_meet: "x \<sqsubseteq> x' \<Longrightarrow> y \<sqsubseteq> y' \<Longrightarrow> x \<sqinter> y \<sqsubseteq> x' \<sqinter> y'"
    36 by (metis meet_greatest meet_le1 meet_le2 le_trans)
    37 
    38 end
    39 
    40 
    41 locale Val_abs1_rep =
    42   Val_abs rep num' plus'
    43   for rep :: "'a::L_top_bot \<Rightarrow> val set" and num' plus' +
    44 assumes inter_rep_subset_rep_meet:
    45   "rep a1 \<inter> rep a2 \<subseteq> rep(a1 \<sqinter> a2)"
    46 and rep_Bot: "rep \<bottom> = {}"
    47 begin
    48 
    49 lemma in_rep_meet: "x <: a1 \<Longrightarrow> x <: a2 \<Longrightarrow> x <: a1 \<sqinter> a2"
    50 by (metis IntI inter_rep_subset_rep_meet set_mp)
    51 
    52 lemma rep_meet[simp]: "rep(a1 \<sqinter> a2) = rep a1 \<inter> rep a2"
    53 by (metis equalityI inter_rep_subset_rep_meet le_inf_iff le_rep meet_le1 meet_le2)
    54 
    55 end
    56 
    57 
    58 locale Val_abs1 = Val_abs1_rep +
    59 fixes filter_plus' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a * 'a"
    60 and filter_less' :: "bool \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a * 'a"
    61 assumes filter_plus': "filter_plus' a a1 a2 = (a1',a2') \<Longrightarrow>
    62   n1 <: a1 \<Longrightarrow> n2 <: a2 \<Longrightarrow> n1+n2 <: a \<Longrightarrow> n1 <: a1' \<and> n2 <: a2'"
    63 and filter_less': "filter_less' (n1<n2) a1 a2 = (a1',a2') \<Longrightarrow>
    64   n1 <: a1 \<Longrightarrow> n2 <: a2 \<Longrightarrow> n1 <: a1' \<and> n2 <: a2'"
    65 
    66 
    67 locale Abs_Int1 = Val_abs1
    68 begin
    69 
    70 lemma in_rep_join_UpI: "s <:up S1 | s <:up S2 \<Longrightarrow> s <:up S1 \<squnion> S2"
    71 by (metis join_ge1 join_ge2 up_fun_in_rep_le)
    72 
    73 fun aval' :: "aexp \<Rightarrow> 'a st up \<Rightarrow> 'a" where
    74 "aval' _ Bot = \<bottom>" |
    75 "aval' (N n) _ = num' n" |
    76 "aval' (V x) (Up S) = lookup S x" |
    77 "aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
    78 
    79 lemma aval'_sound: "s <:up S \<Longrightarrow> aval a s <: aval' a S"
    80 by(induct a)(auto simp: rep_num' rep_plus' in_rep_up_iff lookup_def rep_st_def)
    81 
    82 subsubsection "Backward analysis"
    83 
    84 fun afilter :: "aexp \<Rightarrow> 'a \<Rightarrow> 'a st up \<Rightarrow> 'a st up" where
    85 "afilter (N n) a S = (if n <: a then S else Bot)" |
    86 "afilter (V x) a S = (case S of Bot \<Rightarrow> Bot | Up S \<Rightarrow>
    87   let a' = lookup S x \<sqinter> a in
    88   if a' \<sqsubseteq> \<bottom> then Bot else Up(update S x a'))" |
    89 "afilter (Plus e1 e2) a S =
    90  (let (a1,a2) = filter_plus' a (aval' e1 S) (aval' e2 S)
    91   in afilter e1 a1 (afilter e2 a2 S))"
    92 
    93 text{* The test for @{const Bot} in the @{const V}-case is important: @{const
    94 Bot} indicates that a variable has no possible values, i.e.\ that the current
    95 program point is unreachable. But then the abstract state should collapse to
    96 @{const bot}. Put differently, we maintain the invariant that in an abstract
    97 state all variables are mapped to non-@{const Bot} values. Otherwise the
    98 (pointwise) join of two abstract states, one of which contains @{const Bot}
    99 values, may produce too large a result, thus making the analysis less
   100 precise. *}
   101 
   102 
   103 fun bfilter :: "bexp \<Rightarrow> bool \<Rightarrow> 'a st up \<Rightarrow> 'a st up" where
   104 "bfilter (Bc v) res S = (if v=res then S else Bot)" |
   105 "bfilter (Not b) res S = bfilter b (\<not> res) S" |
   106 "bfilter (And b1 b2) res S =
   107   (if res then bfilter b1 True (bfilter b2 True S)
   108    else bfilter b1 False S \<squnion> bfilter b2 False S)" |
   109 "bfilter (Less e1 e2) res S =
   110   (let (res1,res2) = filter_less' res (aval' e1 S) (aval' e2 S)
   111    in afilter e1 res1 (afilter e2 res2 S))"
   112 
   113 lemma afilter_sound: "s <:up S \<Longrightarrow> aval e s <: a \<Longrightarrow> s <:up afilter e a S"
   114 proof(induction e arbitrary: a S)
   115   case N thus ?case by simp
   116 next
   117   case (V x)
   118   obtain S' where "S = Up S'" and "s <:f S'" using `s <:up S`
   119     by(auto simp: in_rep_up_iff)
   120   moreover hence "s x <: lookup S' x" by(simp add: rep_st_def)
   121   moreover have "s x <: a" using V by simp
   122   ultimately show ?case using V(1)
   123     by(simp add: lookup_update Let_def rep_st_def)
   124       (metis le_rep emptyE in_rep_meet rep_Bot subset_empty)
   125 next
   126   case (Plus e1 e2) thus ?case
   127     using filter_plus'[OF _ aval'_sound[OF Plus(3)] aval'_sound[OF Plus(3)]]
   128     by (auto split: prod.split)
   129 qed
   130 
   131 lemma bfilter_sound: "s <:up S \<Longrightarrow> bv = bval b s \<Longrightarrow> s <:up bfilter b bv S"
   132 proof(induction b arbitrary: S bv)
   133   case Bc thus ?case by simp
   134 next
   135   case (Not b) thus ?case by simp
   136 next
   137   case (And b1 b2) thus ?case by(fastforce simp: in_rep_join_UpI)
   138 next
   139   case (Less e1 e2) thus ?case
   140     by (auto split: prod.split)
   141        (metis afilter_sound filter_less' aval'_sound Less)
   142 qed
   143 
   144 
   145 fun step :: "'a st up \<Rightarrow> 'a st up acom \<Rightarrow> 'a st up acom"
   146  where
   147 "step S (SKIP {P}) = (SKIP {S})" |
   148 "step S (x ::= e {P}) =
   149   x ::= e {case S of Bot \<Rightarrow> Bot
   150            | Up S \<Rightarrow> Up(update S x (aval' e (Up S)))}" |
   151 "step S (c1; c2) = step S c1; step (post c1) c2" |
   152 "step S (IF b THEN c1 ELSE c2 {P}) =
   153   (let c1' = step (bfilter b True S) c1; c2' = step (bfilter b False S) c2
   154    in IF b THEN c1' ELSE c2' {post c1 \<squnion> post c2})" |
   155 "step S ({Inv} WHILE b DO c {P}) =
   156    {S \<squnion> post c}
   157    WHILE b DO step (bfilter b True Inv) c
   158    {bfilter b False Inv}"
   159 
   160 definition AI :: "com \<Rightarrow> 'a st up acom option" where
   161 "AI = lpfp\<^isub>c (step \<top>)"
   162 
   163 lemma strip_step[simp]: "strip(step S c) = strip c"
   164 by(induct c arbitrary: S) (simp_all add: Let_def)
   165 
   166 
   167 subsubsection "Soundness"
   168 
   169 lemma in_rep_update: "\<lbrakk> s <:f S; i <: a \<rbrakk> \<Longrightarrow> s(x := i) <:f update S x a"
   170 by(simp add: rep_st_def lookup_update)
   171 
   172 lemma While_final_False: "(WHILE b DO c, s) \<Rightarrow> t \<Longrightarrow> \<not> bval b t"
   173 by(induct "WHILE b DO c" s t rule: big_step_induct) simp_all
   174 
   175 lemma step_sound:
   176   "step S c \<sqsubseteq> c \<Longrightarrow> (strip c,s) \<Rightarrow> t \<Longrightarrow> s <:up S \<Longrightarrow> t <:up post c"
   177 proof(induction c arbitrary: S s t)
   178   case SKIP thus ?case
   179     by simp (metis skipE up_fun_in_rep_le)
   180 next
   181   case Assign thus ?case
   182     apply (auto simp del: fun_upd_apply split: up.splits)
   183     by (metis aval'_sound fun_in_rep_le in_rep_update rep_up.simps(2))
   184 next
   185   case Semi thus ?case by simp blast
   186 next
   187   case (If b c1 c2 S0)
   188   show ?case
   189   proof cases
   190     assume "bval b s"
   191     with If.prems have 1: "step (bfilter b True S) c1 \<sqsubseteq> c1"
   192       and 2: "(strip c1, s) \<Rightarrow> t" and 3: "post c1 \<sqsubseteq> S0" by(auto simp: Let_def)
   193     from If.IH(1)[OF 1 2 bfilter_sound[OF `s <:up S`]] `bval b s` 3
   194     show ?thesis by simp (metis up_fun_in_rep_le)
   195   next
   196     assume "\<not> bval b s"
   197     with If.prems have 1: "step (bfilter b False S) c2 \<sqsubseteq> c2"
   198       and 2: "(strip c2, s) \<Rightarrow> t" and 3: "post c2 \<sqsubseteq> S0" by(auto simp: Let_def)
   199     from If.IH(2)[OF 1 2 bfilter_sound[OF `s <:up S`]] `\<not> bval b s` 3
   200     show ?thesis by simp (metis up_fun_in_rep_le)
   201   qed
   202 next
   203   case (While Inv b c P)
   204   from While.prems have inv: "step (bfilter b True Inv) c \<sqsubseteq> c"
   205     and "post c \<sqsubseteq> Inv" and "S \<sqsubseteq> Inv" and "bfilter b False Inv \<sqsubseteq> P"
   206     by(auto simp: Let_def)
   207   { fix s t have "(WHILE b DO strip c,s) \<Rightarrow> t \<Longrightarrow> s <:up Inv \<Longrightarrow> t <:up Inv"
   208     proof(induction "WHILE b DO strip c" s t rule: big_step_induct)
   209       case WhileFalse thus ?case by simp
   210     next
   211       case (WhileTrue s1 s2 s3)
   212       from WhileTrue.hyps(5)[OF up_fun_in_rep_le[OF While.IH[OF inv `(strip c, s1) \<Rightarrow> s2` bfilter_sound[OF `s1 <:up Inv`]] `post c \<sqsubseteq> Inv`]] `bval b s1`
   213       show ?case by simp
   214     qed
   215   } note Inv = this
   216   from  While.prems(2) have "(WHILE b DO strip c, s) \<Rightarrow> t" and "\<not> bval b t"
   217     by(auto dest: While_final_False)
   218   from Inv[OF this(1) up_fun_in_rep_le[OF `s <:up S` `S \<sqsubseteq> Inv`]]
   219   have "t <:up Inv" .
   220   from up_fun_in_rep_le[OF bfilter_sound[OF this]  `bfilter b False Inv \<sqsubseteq> P`]
   221   show ?case using `\<not> bval b t` by simp
   222 qed
   223 
   224 lemma AI_sound: "\<lbrakk> AI c = Some c';  (c,s) \<Rightarrow> t \<rbrakk> \<Longrightarrow> t <:up post c'"
   225 unfolding AI_def
   226 by (metis in_rep_Top_up lpfpc_pfp step_sound strip_lpfpc strip_step)
   227 (*
   228 by(metis step_sound[of "\<top>" c' s t] strip_lpfp strip_step
   229   lpfp_pfp mono_def mono_step[OF le_refl] in_rep_Top_up)
   230 *)
   231 end
   232 
   233 
   234 subsubsection "Monotonicity"
   235 
   236 locale Abs_Int1_mono = Abs_Int1 +
   237 assumes mono_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> plus' a1 a2 \<sqsubseteq> plus' b1 b2"
   238 and mono_filter_plus': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow> r \<sqsubseteq> r' \<Longrightarrow>
   239   filter_plus' r a1 a2 \<sqsubseteq> filter_plus' r' b1 b2"
   240 and mono_filter_less': "a1 \<sqsubseteq> b1 \<Longrightarrow> a2 \<sqsubseteq> b2 \<Longrightarrow>
   241   filter_less' bv a1 a2 \<sqsubseteq> filter_less' bv b1 b2"
   242 begin
   243 
   244 lemma mono_aval': "S \<sqsubseteq> S' \<Longrightarrow> aval' e S \<sqsubseteq> aval' e S'"
   245 apply(cases S)
   246  apply simp
   247 apply(cases S')
   248  apply simp
   249 apply simp
   250 by(induction e) (auto simp: le_st_def lookup_def mono_plus')
   251 
   252 lemma mono_afilter: "r \<sqsubseteq> r' \<Longrightarrow> S \<sqsubseteq> S' \<Longrightarrow> afilter e r S \<sqsubseteq> afilter e r' S'"
   253 apply(induction e arbitrary: r r' S S')
   254 apply(auto simp: Let_def split: up.splits prod.splits)
   255 apply (metis le_rep subsetD)
   256 apply(drule_tac x = "list" in mono_lookup)
   257 apply (metis mono_meet le_trans)
   258 apply (metis mono_meet mono_lookup mono_update le_trans)
   259 apply(metis mono_aval' mono_filter_plus'[simplified le_prod_def] fst_conv snd_conv)
   260 done
   261 
   262 lemma mono_bfilter: "S \<sqsubseteq> S' \<Longrightarrow> bfilter b r S \<sqsubseteq> bfilter b r S'"
   263 apply(induction b arbitrary: r S S')
   264 apply(auto simp: le_trans[OF _ join_ge1] le_trans[OF _ join_ge2] split: prod.splits)
   265 apply(metis mono_aval' mono_afilter mono_filter_less'[simplified le_prod_def] fst_conv snd_conv)
   266 done
   267 
   268 
   269 lemma post_le_post: "c \<sqsubseteq> c' \<Longrightarrow> post c \<sqsubseteq> post c'"
   270 by (induction c c' rule: le_acom.induct) simp_all
   271 
   272 lemma mono_step: "S \<sqsubseteq> S' \<Longrightarrow> c \<sqsubseteq> c' \<Longrightarrow> step S c \<sqsubseteq> step S' c'"
   273 apply(induction c c' arbitrary: S S' rule: le_acom.induct)
   274 apply (auto simp: post_le_post Let_def mono_bfilter mono_update mono_aval' le_join_disj
   275   split: up.split)
   276 done
   277 
   278 end
   279 
   280 end