44656
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
header "Abstract Interpretation"
|
|
4 |
|
|
5 |
theory AbsInt0_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 bpfp where
|
|
35 |
"bpfp f 0 _ = Top" |
|
|
36 |
"bpfp f (Suc n) x = (if f x \<sqsubseteq> x then x else bpfp f n (f x))"
|
|
37 |
|
|
38 |
definition "pfp f = bpfp f 3"
|
|
39 |
|
|
40 |
lemma postfixedpoint: "f(bpfp f n x) \<sqsubseteq> bpfp f n x"
|
|
41 |
apply (induct n arbitrary: x)
|
|
42 |
apply (simp)
|
|
43 |
apply (simp)
|
|
44 |
done
|
|
45 |
|
|
46 |
lemma bpfp_funpow: "bpfp f n x \<noteq> Top \<Longrightarrow> EX k. bpfp f n x = (f^^k) x"
|
|
47 |
apply(induct n arbitrary: x)
|
|
48 |
apply simp
|
|
49 |
apply simp
|
|
50 |
apply (auto)
|
|
51 |
apply(rule_tac x=0 in exI)
|
|
52 |
apply simp
|
|
53 |
by (metis funpow.simps(2) funpow_swap1 o_apply)
|
|
54 |
|
|
55 |
lemma pfp_funpow: "pfp f x \<noteq> Top \<Longrightarrow> EX k. pfp f x = (f^^k) x"
|
|
56 |
by(simp add: pfp_def bpfp_funpow)
|
|
57 |
|
|
58 |
abbreviation (input) lift (infix "\<up>" 65) where "f\<up>x0 == (%x. x0 \<squnion> f x)"
|
|
59 |
|
|
60 |
definition "pfp_above f x0 = pfp (f\<up>x0) x0"
|
|
61 |
|
|
62 |
lemma pfp_above_pfp:
|
|
63 |
shows "f(pfp_above f x0) \<sqsubseteq> pfp_above f x0" and "x0 \<sqsubseteq> pfp_above f x0"
|
|
64 |
using postfixedpoint[of "lift f x0"]
|
|
65 |
by(auto simp add: pfp_above_def pfp_def)
|
|
66 |
|
|
67 |
lemma least_pfp:
|
|
68 |
assumes mono: "!!x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" and "pfp_above f x0 \<noteq> Top"
|
|
69 |
and "f p \<sqsubseteq> p" and "x0 \<sqsubseteq> p" shows "pfp_above f x0 \<sqsubseteq> p"
|
|
70 |
proof-
|
|
71 |
let ?F = "lift f x0"
|
|
72 |
obtain k where "pfp_above f x0 = (?F^^k) x0"
|
|
73 |
using pfp_funpow `pfp_above f x0 \<noteq> Top`
|
|
74 |
by(fastsimp simp add: pfp_above_def)
|
|
75 |
moreover
|
|
76 |
{ fix n have "(?F^^n) x0 \<sqsubseteq> p"
|
|
77 |
proof(induct n)
|
|
78 |
case 0 show ?case by(simp add: `x0 \<sqsubseteq> p`)
|
|
79 |
next
|
|
80 |
case (Suc n) thus ?case
|
|
81 |
by (simp add: `x0 \<sqsubseteq> p`)(metis Suc assms(3) le_trans mono)
|
|
82 |
qed
|
|
83 |
} ultimately show ?thesis by simp
|
|
84 |
qed
|
|
85 |
|
|
86 |
lemma chain: assumes "!!x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y"
|
|
87 |
shows "((f\<up>x0)^^i) x0 \<sqsubseteq> ((f\<up>x0)^^(i+1)) x0"
|
|
88 |
apply(induct i)
|
|
89 |
apply simp
|
|
90 |
apply simp
|
|
91 |
by (metis assms join_ge2 le_trans)
|
|
92 |
|
|
93 |
lemma pfp_almost_fp:
|
|
94 |
assumes mono: "!!x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" and "pfp_above f x0 \<noteq> Top"
|
|
95 |
shows "pfp_above f x0 \<sqsubseteq> x0 \<squnion> f(pfp_above f x0)"
|
|
96 |
proof-
|
|
97 |
let ?p = "pfp_above f x0"
|
|
98 |
obtain k where 1: "?p = ((f\<up>x0)^^k) x0"
|
|
99 |
using pfp_funpow `?p \<noteq> Top`
|
|
100 |
by(fastsimp simp add: pfp_above_def)
|
|
101 |
thus ?thesis using chain mono by simp
|
|
102 |
qed
|
|
103 |
|
|
104 |
end
|
|
105 |
|
|
106 |
text{* The interface of abstract values: *}
|
|
107 |
|
|
108 |
locale Rep =
|
|
109 |
fixes rep :: "'a::SL_top \<Rightarrow> 'b set"
|
|
110 |
assumes le_rep: "a \<sqsubseteq> b \<Longrightarrow> rep a \<subseteq> rep b"
|
|
111 |
begin
|
|
112 |
|
|
113 |
abbreviation in_rep (infix "<:" 50) where "x <: a == x : rep a"
|
|
114 |
|
|
115 |
lemma in_rep_join: "x <: a1 \<or> x <: a2 \<Longrightarrow> x <: a1 \<squnion> a2"
|
|
116 |
by (metis SL_top_class.join_ge1 SL_top_class.join_ge2 le_rep subsetD)
|
|
117 |
|
|
118 |
end
|
|
119 |
|
|
120 |
locale Val_abs = Rep rep
|
|
121 |
for rep :: "'a::SL_top \<Rightarrow> val set" +
|
|
122 |
fixes num' :: "val \<Rightarrow> 'a" ("num\<^isup>#")
|
|
123 |
and plus' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" ("plus\<^isup>#")
|
|
124 |
assumes rep_num': "rep(num' n) = {n}"
|
|
125 |
and rep_plus': "n1 <: a1 \<Longrightarrow> n2 <: a2 \<Longrightarrow> n1+n2 <: plus' a1 a2"
|
|
126 |
|
|
127 |
|
|
128 |
instantiation "fun" :: (type, SL_top) SL_top
|
|
129 |
begin
|
|
130 |
|
|
131 |
definition "f \<sqsubseteq> g = (ALL x. f x \<sqsubseteq> g x)"
|
|
132 |
definition "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)"
|
|
133 |
definition "Top = (\<lambda>x. Top)"
|
|
134 |
|
|
135 |
lemma join_apply[simp]:
|
|
136 |
"(f \<squnion> g) x = f x \<squnion> g x"
|
|
137 |
by (simp add: join_fun_def)
|
|
138 |
|
|
139 |
instance
|
|
140 |
proof
|
|
141 |
case goal2 thus ?case by (metis le_fun_def preord_class.le_trans)
|
|
142 |
qed (simp_all add: le_fun_def Top_fun_def)
|
|
143 |
|
|
144 |
end
|
|
145 |
|
|
146 |
subsection "Abstract Interpretation Abstractly"
|
|
147 |
|
|
148 |
text{* Abstract interpretation over abstract values.
|
|
149 |
Abstract states are simply functions. *}
|
|
150 |
|
|
151 |
locale Abs_Int_Fun = Val_abs
|
|
152 |
begin
|
|
153 |
|
|
154 |
fun aval' :: "aexp \<Rightarrow> (name \<Rightarrow> 'a) \<Rightarrow> 'a" ("aval\<^isup>#") where
|
|
155 |
"aval' (N n) _ = num' n" |
|
|
156 |
"aval' (V x) S = S x" |
|
|
157 |
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)"
|
|
158 |
|
|
159 |
abbreviation fun_in_rep (infix "<:" 50) where
|
|
160 |
"f <: F == ALL x. f x <: F x"
|
|
161 |
|
|
162 |
lemma fun_in_rep_le: "(s::state) <: S \<Longrightarrow> S \<sqsubseteq> T \<Longrightarrow> s <: T"
|
|
163 |
by (metis le_fun_def le_rep subsetD)
|
|
164 |
|
|
165 |
lemma aval'_sound: "s <: S \<Longrightarrow> aval a s <: aval' a S"
|
|
166 |
by (induct a) (auto simp: rep_num' rep_plus')
|
|
167 |
|
|
168 |
fun AI :: "com \<Rightarrow> (name \<Rightarrow> 'a) \<Rightarrow> (name \<Rightarrow> 'a)" where
|
|
169 |
"AI SKIP S = S" |
|
|
170 |
"AI (x ::= a) S = S(x := aval' a S)" |
|
|
171 |
"AI (c1;c2) S = AI c2 (AI c1 S)" |
|
|
172 |
"AI (IF b THEN c1 ELSE c2) S = (AI c1 S) \<squnion> (AI c2 S)" |
|
|
173 |
"AI (WHILE b DO c) S = pfp_above (AI c) S"
|
|
174 |
|
|
175 |
lemma AI_sound: "(c,s) \<Rightarrow> t \<Longrightarrow> s <: S0 \<Longrightarrow> t <: AI c S0"
|
|
176 |
proof(induct c arbitrary: s t S0)
|
|
177 |
case SKIP thus ?case by fastsimp
|
|
178 |
next
|
|
179 |
case Assign thus ?case by (auto simp: aval'_sound)
|
|
180 |
next
|
|
181 |
case Semi thus ?case by auto
|
|
182 |
next
|
|
183 |
case If thus ?case by(auto simp: in_rep_join)
|
|
184 |
next
|
|
185 |
case (While b c)
|
|
186 |
let ?P = "pfp_above (AI c) S0"
|
|
187 |
have pfp: "AI c ?P \<sqsubseteq> ?P" and "S0 \<sqsubseteq> ?P"
|
|
188 |
by(simp_all add: SL_top_class.pfp_above_pfp)
|
|
189 |
{ fix s t have "(WHILE b DO c,s) \<Rightarrow> t \<Longrightarrow> s <: ?P \<Longrightarrow> t <: ?P"
|
|
190 |
proof(induct "WHILE b DO c" s t rule: big_step_induct)
|
|
191 |
case WhileFalse thus ?case by simp
|
|
192 |
next
|
|
193 |
case WhileTrue thus ?case using While.hyps pfp fun_in_rep_le by metis
|
|
194 |
qed
|
|
195 |
}
|
|
196 |
with fun_in_rep_le[OF `s <: S0` `S0 \<sqsubseteq> ?P`]
|
|
197 |
show ?case by (metis While(2) AI.simps(5))
|
|
198 |
qed
|
|
199 |
|
|
200 |
end
|
|
201 |
|
|
202 |
|
|
203 |
text{* Problem: not executable because of the comparison of abstract states,
|
|
204 |
i.e. functions, in the post-fixedpoint computation. Need to implement
|
|
205 |
abstract states concretely. *}
|
|
206 |
|
|
207 |
|
|
208 |
(* Exercises: show that <= is a preorder if
|
|
209 |
1. v1 <= v2 = rep v1 <= rep v2
|
|
210 |
2. v1 <= v2 = ALL x. lookup v1 x <= lookup v2 x
|
|
211 |
*)
|
|
212 |
|
|
213 |
end
|