src/HOL/IMP/Abs_Int_Den/Abs_Int_den0_fun.thy
author nipkow
Wed, 28 Sep 2011 09:55:11 +0200
changeset 45963 054a9ac0d7ef
parent 45962 src/HOL/IMP/Abs_Int_Den/Abs_Int0_fun.thy@305f83b6da54
child 46083 e87feee00a4c
permissions -rw-r--r--
Added Hoare-like Abstract Interpretation
     1 (* Author: Tobias Nipkow *)
     2 
     3 header "Denotational Abstract Interpretation"
     4 
     5 theory Abs_Int_den0_fun
     6 imports "~~/src/HOL/ex/Interpretation_with_Defs" Big_Step
     7 begin
     8 
     9 subsection "Orderings"
    10 
    11 class preord =
    12 fixes le :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<sqsubseteq>" 50)
    13 assumes le_refl[simp]: "x \<sqsubseteq> x"
    14 and le_trans: "x \<sqsubseteq> y \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> z"
    15 
    16 text{* Note: no antisymmetry. Allows implementations where some abstract
    17 element is implemented by two different values @{prop "x \<noteq> y"}
    18 such that @{prop"x \<sqsubseteq> y"} and @{prop"y \<sqsubseteq> x"}. Antisymmetry is not
    19 needed because we never compare elements for equality but only for @{text"\<sqsubseteq>"}.
    20 *}
    21 
    22 class SL_top = preord +
    23 fixes join :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65)
    24 fixes Top :: "'a"
    25 assumes join_ge1 [simp]: "x \<sqsubseteq> x \<squnion> y"
    26 and join_ge2 [simp]: "y \<sqsubseteq> x \<squnion> y"
    27 and join_least: "x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> x \<squnion> y \<sqsubseteq> z"
    28 and top[simp]: "x \<sqsubseteq> Top"
    29 begin
    30 
    31 lemma join_le_iff[simp]: "x \<squnion> y \<sqsubseteq> z \<longleftrightarrow> x \<sqsubseteq> z \<and> y \<sqsubseteq> z"
    32 by (metis join_ge1 join_ge2 join_least le_trans)
    33 
    34 fun iter :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
    35 "iter 0 f _ = Top" |
    36 "iter (Suc n) f x = (if f x \<sqsubseteq> x then x else iter n f (f x))"
    37 
    38 lemma iter_pfp: "f(iter n f x) \<sqsubseteq> iter n f x"
    39 apply (induction n arbitrary: x)
    40  apply (simp)
    41 apply (simp)
    42 done
    43 
    44 abbreviation iter' :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
    45 "iter' n f x0 == iter n (\<lambda>x. x0 \<squnion> f x) x0"
    46 
    47 lemma iter'_pfp_above:
    48   "f(iter' n f x0) \<sqsubseteq> iter' n f x0"  "x0 \<sqsubseteq> iter' n f x0"
    49 using iter_pfp[of "\<lambda>x. x0 \<squnion> f x"] by auto
    50 
    51 text{* So much for soundness. But how good an approximation of the post-fixed
    52 point does @{const iter} yield? *}
    53 
    54 lemma iter_funpow: "iter n f x \<noteq> Top \<Longrightarrow> \<exists>k. iter n f x = (f^^k) x"
    55 apply(induction n arbitrary: x)
    56  apply simp
    57 apply (auto)
    58  apply(metis funpow.simps(1) id_def)
    59 by (metis funpow.simps(2) funpow_swap1 o_apply)
    60 
    61 text{* For monotone @{text f}, @{term "iter f n x0"} yields the least
    62 post-fixed point above @{text x0}, unless it yields @{text Top}. *}
    63 
    64 lemma iter_least_pfp:
    65 assumes mono: "\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" and "iter n f x0 \<noteq> Top"
    66 and "f p \<sqsubseteq> p" and "x0 \<sqsubseteq> p" shows "iter n f x0 \<sqsubseteq> p"
    67 proof-
    68   obtain k where "iter n f x0 = (f^^k) x0"
    69     using iter_funpow[OF `iter n f x0 \<noteq> Top`] by blast
    70   moreover
    71   { fix n have "(f^^n) x0 \<sqsubseteq> p"
    72     proof(induction n)
    73       case 0 show ?case by(simp add: `x0 \<sqsubseteq> p`)
    74     next
    75       case (Suc n) thus ?case
    76         by (simp add: `x0 \<sqsubseteq> p`)(metis Suc assms(3) le_trans mono)
    77     qed
    78   } ultimately show ?thesis by simp
    79 qed
    80 
    81 end
    82 
    83 text{* The interface of abstract values: *}
    84 
    85 locale Rep =
    86 fixes rep :: "'a::SL_top \<Rightarrow> 'b set"
    87 assumes le_rep: "a \<sqsubseteq> b \<Longrightarrow> rep a \<subseteq> rep b"
    88 begin
    89 
    90 abbreviation in_rep (infix "<:" 50) where "x <: a == x : rep a"
    91 
    92 lemma in_rep_join: "x <: a1 \<or> x <: a2 \<Longrightarrow> x <: a1 \<squnion> a2"
    93 by (metis SL_top_class.join_ge1 SL_top_class.join_ge2 le_rep subsetD)
    94 
    95 end
    96 
    97 locale Val_abs = Rep rep
    98   for rep :: "'a::SL_top \<Rightarrow> val set" +
    99 fixes num' :: "val \<Rightarrow> 'a"
   100 and plus' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   101 assumes rep_num': "rep(num' n) = {n}"
   102 and rep_plus': "n1 <: a1 \<Longrightarrow> n2 <: a2 \<Longrightarrow> n1+n2 <: plus' a1 a2"
   103 
   104 
   105 instantiation "fun" :: (type, SL_top) SL_top
   106 begin
   107 
   108 definition "f \<sqsubseteq> g = (ALL x. f x \<sqsubseteq> g x)"
   109 definition "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)"
   110 definition "Top = (\<lambda>x. Top)"
   111 
   112 lemma join_apply[simp]:
   113   "(f \<squnion> g) x = f x \<squnion> g x"
   114 by (simp add: join_fun_def)
   115 
   116 instance
   117 proof
   118   case goal2 thus ?case by (metis le_fun_def preord_class.le_trans)
   119 qed (simp_all add: le_fun_def Top_fun_def)
   120 
   121 end
   122 
   123 subsection "Abstract Interpretation Abstractly"
   124 
   125 text{* Abstract interpretation over abstract values. Abstract states are
   126 simply functions. The post-fixed point finder is parameterized over. *}
   127 
   128 type_synonym 'a st = "name \<Rightarrow> 'a"
   129 
   130 locale Abs_Int_Fun = Val_abs +
   131 fixes pfp :: "('a st \<Rightarrow> 'a st) \<Rightarrow> 'a st \<Rightarrow> 'a st"
   132 assumes pfp: "f(pfp f x) \<sqsubseteq> pfp f x"
   133 assumes above: "x \<sqsubseteq> pfp f x"
   134 begin
   135 
   136 fun aval' :: "aexp \<Rightarrow> (name \<Rightarrow> 'a) \<Rightarrow> 'a" where
   137 "aval' (N n) _ = num' n" |
   138 "aval' (V x) S = S x" |
   139 "aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
   140 
   141 abbreviation fun_in_rep (infix "<:" 50) where
   142 "f <: F == \<forall>x. f x <: F x"
   143 
   144 lemma fun_in_rep_le: "(s::state) <: S \<Longrightarrow> S \<sqsubseteq> T \<Longrightarrow> s <: T"
   145 by (metis le_fun_def le_rep subsetD)
   146 
   147 lemma aval'_sound: "s <: S \<Longrightarrow> aval a s <: aval' a S"
   148 by (induct a) (auto simp: rep_num' rep_plus')
   149 
   150 fun AI :: "com \<Rightarrow> (name \<Rightarrow> 'a) \<Rightarrow> (name \<Rightarrow> 'a)" where
   151 "AI SKIP S = S" |
   152 "AI (x ::= a) S = S(x := aval' a S)" |
   153 "AI (c1;c2) S = AI c2 (AI c1 S)" |
   154 "AI (IF b THEN c1 ELSE c2) S = (AI c1 S) \<squnion> (AI c2 S)" |
   155 "AI (WHILE b DO c) S = pfp (AI c) S"
   156 
   157 lemma AI_sound: "(c,s) \<Rightarrow> t \<Longrightarrow> s <: S0 \<Longrightarrow> t <: AI c S0"
   158 proof(induction c arbitrary: s t S0)
   159   case SKIP thus ?case by fastforce
   160 next
   161   case Assign thus ?case by (auto simp: aval'_sound)
   162 next
   163   case Semi thus ?case by auto
   164 next
   165   case If thus ?case by(auto simp: in_rep_join)
   166 next
   167   case (While b c)
   168   let ?P = "pfp (AI c) S0"
   169   { fix s t have "(WHILE b DO c,s) \<Rightarrow> t \<Longrightarrow> s <: ?P \<Longrightarrow> t <: ?P"
   170     proof(induction "WHILE b DO c" s t rule: big_step_induct)
   171       case WhileFalse thus ?case by simp
   172     next
   173       case WhileTrue thus ?case by(metis While.IH pfp fun_in_rep_le)
   174     qed
   175   }
   176   with fun_in_rep_le[OF `s <: S0` above]
   177   show ?case by (metis While(2) AI.simps(5))
   178 qed
   179 
   180 end
   181 
   182 
   183 text{* Problem: not executable because of the comparison of abstract states,
   184 i.e. functions, in the post-fixedpoint computation. Need to implement
   185 abstract states concretely. *}
   186 
   187 end