author | nipkow |
Wed, 24 Apr 2013 12:25:56 +0200 | |
changeset 51754 | 39133c710fa3 |
parent 51749 | c27bb7994bd3 |
child 51783 | f4a00cdae743 |
permissions | -rw-r--r-- |
47613 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
3 |
theory Abs_Int0 |
|
4 |
imports Abs_Int_init |
|
5 |
begin |
|
6 |
||
7 |
subsection "Orderings" |
|
8 |
||
51625 | 9 |
text{* The basic type classes @{class order}, @{class semilattice_sup} and @{class top} are |
10 |
defined in @{theory Main}, more precisely in theories @{theory Orderings} and @{theory Lattices}. |
|
11 |
If you view this theory with jedit, just click on the names to get there. *} |
|
47613 | 12 |
|
51389 | 13 |
class semilattice = semilattice_sup + top |
47613 | 14 |
|
15 |
||
51389 | 16 |
instance "fun" :: (type, semilattice) semilattice .. |
47613 | 17 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
18 |
instantiation option :: (order)order |
47613 | 19 |
begin |
20 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
21 |
fun less_eq_option where |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
22 |
"Some x \<le> Some y = (x \<le> y)" | |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
23 |
"None \<le> y = True" | |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
24 |
"Some _ \<le> None = False" |
47613 | 25 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
26 |
definition less_option where "x < (y::'a option) = (x \<le> y \<and> \<not> y \<le> x)" |
47613 | 27 |
|
51628 | 28 |
lemma le_None[simp]: "(x \<le> None) = (x = None)" |
47613 | 29 |
by (cases x) simp_all |
30 |
||
51628 | 31 |
lemma Some_le[simp]: "(Some x \<le> u) = (\<exists>y. u = Some y \<and> x \<le> y)" |
47613 | 32 |
by (cases u) auto |
33 |
||
34 |
instance proof |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
35 |
case goal1 show ?case by(rule less_option_def) |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
36 |
next |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
37 |
case goal2 show ?case by(cases x, simp_all) |
47613 | 38 |
next |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
39 |
case goal3 thus ?case by(cases z, simp, cases y, simp, cases x, auto) |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
40 |
next |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
41 |
case goal4 thus ?case by(cases y, simp, cases x, auto) |
47613 | 42 |
qed |
43 |
||
44 |
end |
|
45 |
||
51389 | 46 |
instantiation option :: (sup)sup |
47613 | 47 |
begin |
48 |
||
51389 | 49 |
fun sup_option where |
47613 | 50 |
"Some x \<squnion> Some y = Some(x \<squnion> y)" | |
51 |
"None \<squnion> y = y" | |
|
52 |
"x \<squnion> None = x" |
|
53 |
||
51389 | 54 |
lemma sup_None2[simp]: "x \<squnion> None = x" |
47613 | 55 |
by (cases x) simp_all |
56 |
||
57 |
instance .. |
|
58 |
||
59 |
end |
|
60 |
||
49396 | 61 |
instantiation option :: (semilattice)semilattice |
47613 | 62 |
begin |
63 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
64 |
definition top_option where "\<top> = Some \<top>" |
47613 | 65 |
|
66 |
instance proof |
|
51389 | 67 |
case goal4 show ?case by(cases a, simp_all add: top_option_def) |
47613 | 68 |
next |
51389 | 69 |
case goal1 thus ?case by(cases x, simp, cases y, simp_all) |
47613 | 70 |
next |
51389 | 71 |
case goal2 thus ?case by(cases y, simp, cases x, simp_all) |
47613 | 72 |
next |
51389 | 73 |
case goal3 thus ?case by(cases z, simp, cases y, simp, cases x, simp_all) |
47613 | 74 |
qed |
75 |
||
76 |
end |
|
77 |
||
51390 | 78 |
lemma [simp]: "(Some x < Some y) = (x < y)" |
79 |
by(auto simp: less_le) |
|
80 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
81 |
instantiation option :: (order)bot |
47613 | 82 |
begin |
83 |
||
49396 | 84 |
definition bot_option :: "'a option" where |
47613 | 85 |
"\<bottom> = None" |
86 |
||
87 |
instance |
|
88 |
proof |
|
49396 | 89 |
case goal1 thus ?case by(auto simp: bot_option_def) |
47613 | 90 |
qed |
91 |
||
92 |
end |
|
93 |
||
94 |
||
95 |
definition bot :: "com \<Rightarrow> 'a option acom" where |
|
96 |
"bot c = anno None c" |
|
97 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
98 |
lemma bot_least: "strip C = c \<Longrightarrow> bot c \<le> C" |
47613 | 99 |
by(induct C arbitrary: c)(auto simp: bot_def) |
100 |
||
101 |
lemma strip_bot[simp]: "strip(bot c) = c" |
|
102 |
by(simp add: bot_def) |
|
103 |
||
104 |
||
51722 | 105 |
subsubsection "Pre-fixpoint iteration" |
47613 | 106 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
107 |
definition pfp :: "(('a::order) \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a option" where |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
108 |
"pfp f = while_option (\<lambda>x. \<not> f x \<le> x) f" |
47613 | 109 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
110 |
lemma pfp_pfp: assumes "pfp f x0 = Some x" shows "f x \<le> x" |
47613 | 111 |
using while_option_stop[OF assms[simplified pfp_def]] by simp |
112 |
||
49464 | 113 |
lemma while_least: |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
114 |
fixes q :: "'a::order" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
115 |
assumes "\<forall>x\<in>L.\<forall>y\<in>L. x \<le> y \<longrightarrow> f x \<le> f y" and "\<forall>x. x \<in> L \<longrightarrow> f x \<in> L" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
116 |
and "\<forall>x \<in> L. b \<le> x" and "b \<in> L" and "f q \<le> q" and "q \<in> L" |
49464 | 117 |
and "while_option P f b = Some p" |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
118 |
shows "p \<le> q" |
49464 | 119 |
using while_option_rule[OF _ assms(7)[unfolded pfp_def], |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
120 |
where P = "%x. x \<in> L \<and> x \<le> q"] |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
121 |
by (metis assms(1-6) order_trans) |
47613 | 122 |
|
51710 | 123 |
lemma pfp_bot_least: |
124 |
assumes "\<forall>x\<in>{C. strip C = c}.\<forall>y\<in>{C. strip C = c}. x \<le> y \<longrightarrow> f x \<le> f y" |
|
125 |
and "\<forall>C. C \<in> {C. strip C = c} \<longrightarrow> f C \<in> {C. strip C = c}" |
|
126 |
and "f C' \<le> C'" "strip C' = c" "pfp f (bot c) = Some C" |
|
127 |
shows "C \<le> C'" |
|
128 |
by(rule while_least[OF assms(1,2) _ _ assms(3) _ assms(5)[unfolded pfp_def]]) |
|
129 |
(simp_all add: assms(4) bot_least) |
|
130 |
||
49464 | 131 |
lemma pfp_inv: |
132 |
"pfp f x = Some y \<Longrightarrow> (\<And>x. P x \<Longrightarrow> P(f x)) \<Longrightarrow> P x \<Longrightarrow> P y" |
|
133 |
unfolding pfp_def by (metis (lifting) while_option_rule) |
|
47613 | 134 |
|
135 |
lemma strip_pfp: |
|
136 |
assumes "\<And>x. g(f x) = g x" and "pfp f x0 = Some x" shows "g x = g x0" |
|
49464 | 137 |
using pfp_inv[OF assms(2), where P = "%x. g x = g x0"] assms(1) by simp |
47613 | 138 |
|
139 |
||
140 |
subsection "Abstract Interpretation" |
|
141 |
||
142 |
definition \<gamma>_fun :: "('a \<Rightarrow> 'b set) \<Rightarrow> ('c \<Rightarrow> 'a) \<Rightarrow> ('c \<Rightarrow> 'b)set" where |
|
143 |
"\<gamma>_fun \<gamma> F = {f. \<forall>x. f x \<in> \<gamma>(F x)}" |
|
144 |
||
145 |
fun \<gamma>_option :: "('a \<Rightarrow> 'b set) \<Rightarrow> 'a option \<Rightarrow> 'b set" where |
|
146 |
"\<gamma>_option \<gamma> None = {}" | |
|
147 |
"\<gamma>_option \<gamma> (Some a) = \<gamma> a" |
|
148 |
||
149 |
text{* The interface for abstract values: *} |
|
150 |
||
151 |
locale Val_abs = |
|
49396 | 152 |
fixes \<gamma> :: "'av::semilattice \<Rightarrow> val set" |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
153 |
assumes mono_gamma: "a \<le> b \<Longrightarrow> \<gamma> a \<le> \<gamma> b" |
47613 | 154 |
and gamma_Top[simp]: "\<gamma> \<top> = UNIV" |
155 |
fixes num' :: "val \<Rightarrow> 'av" |
|
156 |
and plus' :: "'av \<Rightarrow> 'av \<Rightarrow> 'av" |
|
51036 | 157 |
assumes gamma_num': "i \<in> \<gamma>(num' i)" |
158 |
and gamma_plus': "i1 \<in> \<gamma> a1 \<Longrightarrow> i2 \<in> \<gamma> a2 \<Longrightarrow> i1+i2 \<in> \<gamma>(plus' a1 a2)" |
|
47613 | 159 |
|
160 |
type_synonym 'av st = "(vname \<Rightarrow> 'av)" |
|
161 |
||
51721 | 162 |
locale Abs_Int_fun = Val_abs \<gamma> for \<gamma> :: "'av::semilattice \<Rightarrow> val set" |
47613 | 163 |
begin |
164 |
||
165 |
fun aval' :: "aexp \<Rightarrow> 'av st \<Rightarrow> 'av" where |
|
50896 | 166 |
"aval' (N i) S = num' i" | |
47613 | 167 |
"aval' (V x) S = S x" | |
168 |
"aval' (Plus a1 a2) S = plus' (aval' a1 S) (aval' a2 S)" |
|
169 |
||
51694 | 170 |
definition "fa x e S = (case S of None \<Rightarrow> None | Some S \<Rightarrow> Some(S(x := aval' e S)))" |
171 |
||
172 |
definition "step' = Step fa (\<lambda>b S. S)" |
|
51389 | 173 |
|
51722 | 174 |
lemma strip_step'[simp]: "strip(step' S C) = strip C" |
175 |
by(simp add: step'_def) |
|
176 |
||
47613 | 177 |
definition AI :: "com \<Rightarrow> 'av st option acom option" where |
49464 | 178 |
"AI c = pfp (step' \<top>) (bot c)" |
47613 | 179 |
|
180 |
||
49497 | 181 |
abbreviation \<gamma>\<^isub>s :: "'av st \<Rightarrow> state set" |
182 |
where "\<gamma>\<^isub>s == \<gamma>_fun \<gamma>" |
|
47613 | 183 |
|
184 |
abbreviation \<gamma>\<^isub>o :: "'av st option \<Rightarrow> state set" |
|
49497 | 185 |
where "\<gamma>\<^isub>o == \<gamma>_option \<gamma>\<^isub>s" |
47613 | 186 |
|
187 |
abbreviation \<gamma>\<^isub>c :: "'av st option acom \<Rightarrow> state set acom" |
|
188 |
where "\<gamma>\<^isub>c == map_acom \<gamma>\<^isub>o" |
|
189 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
190 |
lemma gamma_s_Top[simp]: "\<gamma>\<^isub>s \<top> = UNIV" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
191 |
by(simp add: top_fun_def \<gamma>_fun_def) |
47613 | 192 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
193 |
lemma gamma_o_Top[simp]: "\<gamma>\<^isub>o \<top> = UNIV" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
194 |
by (simp add: top_option_def) |
47613 | 195 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
196 |
lemma mono_gamma_s: "f1 \<le> f2 \<Longrightarrow> \<gamma>\<^isub>s f1 \<subseteq> \<gamma>\<^isub>s f2" |
47613 | 197 |
by(auto simp: le_fun_def \<gamma>_fun_def dest: mono_gamma) |
198 |
||
199 |
lemma mono_gamma_o: |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
200 |
"S1 \<le> S2 \<Longrightarrow> \<gamma>\<^isub>o S1 \<subseteq> \<gamma>\<^isub>o S2" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
201 |
by(induction S1 S2 rule: less_eq_option.induct)(simp_all add: mono_gamma_s) |
47613 | 202 |
|
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
203 |
lemma mono_gamma_c: "C1 \<le> C2 \<Longrightarrow> \<gamma>\<^isub>c C1 \<le> \<gamma>\<^isub>c C2" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
204 |
by (induction C1 C2 rule: less_eq_acom.induct) (simp_all add:mono_gamma_o) |
47613 | 205 |
|
206 |
text{* Soundness: *} |
|
207 |
||
49497 | 208 |
lemma aval'_sound: "s : \<gamma>\<^isub>s S \<Longrightarrow> aval a s : \<gamma>(aval' a S)" |
47613 | 209 |
by (induct a) (auto simp: gamma_num' gamma_plus' \<gamma>_fun_def) |
210 |
||
51390 | 211 |
lemma in_gamma_update: "\<lbrakk> s : \<gamma>\<^isub>s S; i : \<gamma> a \<rbrakk> \<Longrightarrow> s(x := i) : \<gamma>\<^isub>s(S(x := a))" |
47613 | 212 |
by(simp add: \<gamma>_fun_def) |
213 |
||
51390 | 214 |
lemma gamma_Step_subcomm: |
215 |
assumes "!!x e S. f1 x e (\<gamma>\<^isub>o S) \<subseteq> \<gamma>\<^isub>o (f2 x e S)" "!!b S. g1 b (\<gamma>\<^isub>o S) \<subseteq> \<gamma>\<^isub>o (g2 b S)" |
|
216 |
shows "Step f1 g1 (\<gamma>\<^isub>o S) (\<gamma>\<^isub>c C) \<le> \<gamma>\<^isub>c (Step f2 g2 S C)" |
|
217 |
proof(induction C arbitrary: S) |
|
218 |
qed (auto simp: mono_gamma_o assms) |
|
219 |
||
50986 | 220 |
lemma step_step': "step (\<gamma>\<^isub>o S) (\<gamma>\<^isub>c C) \<le> \<gamma>\<^isub>c (step' S C)" |
51390 | 221 |
unfolding step_def step'_def |
51694 | 222 |
by(rule gamma_Step_subcomm) |
223 |
(auto simp: aval'_sound in_gamma_update fa_def split: option.splits) |
|
47613 | 224 |
|
225 |
lemma AI_sound: "AI c = Some C \<Longrightarrow> CS c \<le> \<gamma>\<^isub>c C" |
|
226 |
proof(simp add: CS_def AI_def) |
|
49464 | 227 |
assume 1: "pfp (step' \<top>) (bot c) = Some C" |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
228 |
have pfp': "step' \<top> C \<le> C" by(rule pfp_pfp[OF 1]) |
50986 | 229 |
have 2: "step (\<gamma>\<^isub>o \<top>) (\<gamma>\<^isub>c C) \<le> \<gamma>\<^isub>c C" --"transfer the pfp'" |
230 |
proof(rule order_trans) |
|
231 |
show "step (\<gamma>\<^isub>o \<top>) (\<gamma>\<^isub>c C) \<le> \<gamma>\<^isub>c (step' \<top> C)" by(rule step_step') |
|
232 |
show "... \<le> \<gamma>\<^isub>c C" by (metis mono_gamma_c[OF pfp']) |
|
47613 | 233 |
qed |
51390 | 234 |
have 3: "strip (\<gamma>\<^isub>c C) = c" by(simp add: strip_pfp[OF _ 1] step'_def) |
50986 | 235 |
have "lfp c (step (\<gamma>\<^isub>o \<top>)) \<le> \<gamma>\<^isub>c C" |
236 |
by(rule lfp_lowerbound[simplified,where f="step (\<gamma>\<^isub>o \<top>)", OF 3 2]) |
|
237 |
thus "lfp c (step UNIV) \<le> \<gamma>\<^isub>c C" by simp |
|
47613 | 238 |
qed |
239 |
||
240 |
end |
|
241 |
||
242 |
||
243 |
subsubsection "Monotonicity" |
|
244 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
245 |
lemma mono_post: "C1 \<le> C2 \<Longrightarrow> post C1 \<le> post C2" |
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
246 |
by(induction C1 C2 rule: less_eq_acom.induct) (auto) |
47613 | 247 |
|
51721 | 248 |
locale Abs_Int_fun_mono = Abs_Int_fun + |
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
249 |
assumes mono_plus': "a1 \<le> b1 \<Longrightarrow> a2 \<le> b2 \<Longrightarrow> plus' a1 a2 \<le> plus' b1 b2" |
47613 | 250 |
begin |
251 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
252 |
lemma mono_aval': "S \<le> S' \<Longrightarrow> aval' e S \<le> aval' e S'" |
47613 | 253 |
by(induction e)(auto simp: le_fun_def mono_plus') |
254 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
255 |
lemma mono_update: "a \<le> a' \<Longrightarrow> S \<le> S' \<Longrightarrow> S(x := a) \<le> S'(x := a')" |
47613 | 256 |
by(simp add: le_fun_def) |
257 |
||
51359
00b45c7e831f
major redesign: order instead of preorder, new definition of intervals as quotients
nipkow
parents:
51036
diff
changeset
|
258 |
lemma mono_step': "S1 \<le> S2 \<Longrightarrow> C1 \<le> C2 \<Longrightarrow> step' S1 C1 \<le> step' S2 C2" |
51390 | 259 |
unfolding step'_def |
51694 | 260 |
by(rule mono2_Step) |
261 |
(auto simp: mono_update mono_aval' fa_def split: option.split) |
|
47613 | 262 |
|
51722 | 263 |
lemma mono_step'_top: "C \<le> C' \<Longrightarrow> step' \<top> C \<le> step' \<top> C'" |
264 |
by (metis mono_step' order_refl) |
|
265 |
||
266 |
lemma AI_least_pfp: assumes "AI c = Some C" "step' \<top> C' \<le> C'" "strip C' = c" |
|
267 |
shows "C \<le> C'" |
|
268 |
by(rule pfp_bot_least[OF _ _ assms(2,3) assms(1)[unfolded AI_def]]) |
|
269 |
(simp_all add: mono_step'_top) |
|
270 |
||
271 |
end |
|
272 |
||
273 |
||
274 |
instantiation acom :: (type) vars |
|
275 |
begin |
|
276 |
||
277 |
definition "vars_acom = vars o strip" |
|
278 |
||
279 |
instance .. |
|
280 |
||
281 |
end |
|
282 |
||
283 |
lemma finite_Cvars: "finite(vars(C::'a acom))" |
|
284 |
by(simp add: vars_acom_def) |
|
285 |
||
286 |
||
287 |
subsubsection "Termination" |
|
288 |
||
289 |
lemma pfp_termination: |
|
290 |
fixes x0 :: "'a::order" and m :: "'a \<Rightarrow> nat" |
|
291 |
assumes mono: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y" |
|
292 |
and m: "\<And>x y. I x \<Longrightarrow> I y \<Longrightarrow> x < y \<Longrightarrow> m x > m y" |
|
293 |
and I: "\<And>x y. I x \<Longrightarrow> I(f x)" and "I x0" and "x0 \<le> f x0" |
|
294 |
shows "\<exists>x. pfp f x0 = Some x" |
|
295 |
proof(simp add: pfp_def, rule wf_while_option_Some[where P = "%x. I x & x \<le> f x"]) |
|
296 |
show "wf {(y,x). ((I x \<and> x \<le> f x) \<and> \<not> f x \<le> x) \<and> y = f x}" |
|
297 |
by(rule wf_subset[OF wf_measure[of m]]) (auto simp: m I) |
|
298 |
next |
|
299 |
show "I x0 \<and> x0 \<le> f x0" using `I x0` `x0 \<le> f x0` by blast |
|
300 |
next |
|
301 |
fix x assume "I x \<and> x \<le> f x" thus "I(f x) \<and> f x \<le> f(f x)" |
|
302 |
by (blast intro: I mono) |
|
303 |
qed |
|
304 |
||
305 |
||
306 |
locale Measure1_fun = |
|
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
307 |
fixes m :: "'av::top \<Rightarrow> nat" |
51722 | 308 |
fixes h :: "nat" |
309 |
assumes h: "m x \<le> h" |
|
310 |
begin |
|
311 |
||
312 |
definition m_s :: "vname set \<Rightarrow> 'av st \<Rightarrow> nat" ("m\<^isub>s") where |
|
313 |
"m_s X S = (\<Sum> x \<in> X. m(S x))" |
|
314 |
||
315 |
lemma m_s_h: "finite X \<Longrightarrow> m_s X S \<le> h * card X" |
|
316 |
by(simp add: m_s_def) (metis nat_mult_commute of_nat_id setsum_bounded[OF h]) |
|
317 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
318 |
fun m_o :: "vname set \<Rightarrow> 'av st option \<Rightarrow> nat" ("m\<^isub>o") where |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
319 |
"m_o X (Some S) = m_s X S" | |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
320 |
"m_o X None = h * card X + 1" |
51722 | 321 |
|
322 |
lemma m_o_h: "finite X \<Longrightarrow> m_o X opt \<le> (h*card X + 1)" |
|
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
323 |
by(cases opt)(auto simp add: m_s_h le_SucI dest: m_s_h) |
51722 | 324 |
|
325 |
definition m_c :: "'av st option acom \<Rightarrow> nat" ("m\<^isub>c") where |
|
326 |
"m_c C = (\<Sum>i<size(annos C). m_o (vars C) (annos C ! i))" |
|
327 |
||
328 |
text{* Upper complexity bound: *} |
|
329 |
lemma m_c_h: "m_c C \<le> size(annos C) * (h * card(vars C) + 1)" |
|
330 |
proof- |
|
331 |
let ?X = "vars C" let ?n = "card ?X" let ?a = "size(annos C)" |
|
332 |
have "m_c C = (\<Sum>i<?a. m_o ?X (annos C ! i))" by(simp add: m_c_def) |
|
333 |
also have "\<dots> \<le> (\<Sum>i<?a. h * ?n + 1)" |
|
334 |
apply(rule setsum_mono) using m_o_h[OF finite_Cvars] by simp |
|
335 |
also have "\<dots> = ?a * (h * ?n + 1)" by simp |
|
336 |
finally show ?thesis . |
|
337 |
qed |
|
338 |
||
339 |
end |
|
340 |
||
341 |
||
342 |
lemma le_iff_le_annos_zip: "C1 \<le> C2 \<longleftrightarrow> |
|
343 |
(\<forall> (a1,a2) \<in> set(zip (annos C1) (annos C2)). a1 \<le> a2) \<and> strip C1 = strip C2" |
|
344 |
by(induct C1 C2 rule: less_eq_acom.induct) (auto simp: size_annos_same2) |
|
345 |
||
346 |
lemma le_iff_le_annos: "C1 \<le> C2 \<longleftrightarrow> |
|
347 |
strip C1 = strip C2 \<and> (\<forall> i<size(annos C1). annos C1 ! i \<le> annos C2 ! i)" |
|
348 |
by(auto simp add: le_iff_le_annos_zip set_zip) (metis size_annos_same2) |
|
349 |
||
350 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
351 |
locale Measure_fun = Measure1_fun where m=m for m :: "'av::semilattice \<Rightarrow> nat" + |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
352 |
assumes m2: "x < y \<Longrightarrow> m x > m y" |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
353 |
begin |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
354 |
|
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
355 |
text{* The predicates @{text "top_on_ty X a"} that follow describe that any abstract |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
356 |
state in @{text a} maps all variables in @{text X} to @{term \<top>}. |
51722 | 357 |
This is an important invariant for the termination proof where we argue that only |
358 |
the finitely many variables in the program change. That the others do not change |
|
359 |
follows because they remain @{term \<top>}. *} |
|
360 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
361 |
fun top_on_st :: "vname set \<Rightarrow> 'av st \<Rightarrow> bool" ("top'_on\<^isub>s") where |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
362 |
"top_on_st X S = (\<forall>x\<in>X. S x = \<top>)" |
51722 | 363 |
|
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
364 |
fun top_on_opt :: "vname set \<Rightarrow> 'av st option \<Rightarrow> bool" ("top'_on\<^isub>o") where |
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
365 |
"top_on_opt X (Some S) = top_on_st X S" | |
51722 | 366 |
"top_on_opt X None = True" |
367 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
368 |
definition top_on_acom :: "vname set \<Rightarrow> 'av st option acom \<Rightarrow> bool" ("top'_on\<^isub>c") where |
51722 | 369 |
"top_on_acom X C = (\<forall>a \<in> set(annos C). top_on_opt X a)" |
370 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
371 |
lemma top_on_top: "top_on_opt X \<top>" |
51722 | 372 |
by(auto simp: top_option_def) |
373 |
||
374 |
lemma top_on_bot: "top_on_acom X (bot c)" |
|
375 |
by(auto simp add: top_on_acom_def bot_def) |
|
376 |
||
377 |
lemma top_on_post: "top_on_acom X C \<Longrightarrow> top_on_opt X (post C)" |
|
378 |
by(simp add: top_on_acom_def post_in_annos) |
|
379 |
||
380 |
lemma top_on_acom_simps: |
|
381 |
"top_on_acom X (SKIP {Q}) = top_on_opt X Q" |
|
382 |
"top_on_acom X (x ::= e {Q}) = top_on_opt X Q" |
|
383 |
"top_on_acom X (C1;C2) = (top_on_acom X C1 \<and> top_on_acom X C2)" |
|
384 |
"top_on_acom X (IF b THEN {P1} C1 ELSE {P2} C2 {Q}) = |
|
385 |
(top_on_opt X P1 \<and> top_on_acom X C1 \<and> top_on_opt X P2 \<and> top_on_acom X C2 \<and> top_on_opt X Q)" |
|
386 |
"top_on_acom X ({I} WHILE b DO {P} C {Q}) = |
|
387 |
(top_on_opt X I \<and> top_on_acom X C \<and> top_on_opt X P \<and> top_on_opt X Q)" |
|
388 |
by(auto simp add: top_on_acom_def) |
|
389 |
||
390 |
lemma top_on_sup: |
|
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
391 |
"top_on_opt X o1 \<Longrightarrow> top_on_opt X o2 \<Longrightarrow> top_on_opt X (o1 \<squnion> o2)" |
51722 | 392 |
apply(induction o1 o2 rule: sup_option.induct) |
393 |
apply(auto) |
|
394 |
done |
|
395 |
||
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
396 |
lemma top_on_Step: fixes C :: "'av st option acom" |
51722 | 397 |
assumes "!!x e S. \<lbrakk>top_on_opt X S; x \<notin> X; vars e \<subseteq> -X\<rbrakk> \<Longrightarrow> top_on_opt X (f x e S)" |
398 |
"!!b S. top_on_opt X S \<Longrightarrow> vars b \<subseteq> -X \<Longrightarrow> top_on_opt X (g b S)" |
|
399 |
shows "\<lbrakk> vars C \<subseteq> -X; top_on_opt X S; top_on_acom X C \<rbrakk> \<Longrightarrow> top_on_acom X (Step f g S C)" |
|
400 |
proof(induction C arbitrary: S) |
|
401 |
qed (auto simp: top_on_acom_simps vars_acom_def top_on_post top_on_sup assms) |
|
402 |
||
403 |
lemma m1: "x \<le> y \<Longrightarrow> m x \<ge> m y" |
|
404 |
by(auto simp: le_less m2) |
|
405 |
||
406 |
lemma m_s2_rep: assumes "finite(X)" and "S1 = S2 on -X" and "\<forall>x. S1 x \<le> S2 x" and "S1 \<noteq> S2" |
|
407 |
shows "(\<Sum>x\<in>X. m (S2 x)) < (\<Sum>x\<in>X. m (S1 x))" |
|
408 |
proof- |
|
409 |
from assms(3) have 1: "\<forall>x\<in>X. m(S1 x) \<ge> m(S2 x)" by (simp add: m1) |
|
410 |
from assms(2,3,4) have "EX x:X. S1 x < S2 x" |
|
411 |
by(simp add: fun_eq_iff) (metis Compl_iff le_neq_trans) |
|
412 |
hence 2: "\<exists>x\<in>X. m(S1 x) > m(S2 x)" by (metis m2) |
|
413 |
from setsum_strict_mono_ex1[OF `finite X` 1 2] |
|
414 |
show "(\<Sum>x\<in>X. m (S2 x)) < (\<Sum>x\<in>X. m (S1 x))" . |
|
415 |
qed |
|
416 |
||
417 |
lemma m_s2: "finite(X) \<Longrightarrow> S1 = S2 on -X \<Longrightarrow> S1 < S2 \<Longrightarrow> m_s X S1 > m_s X S2" |
|
418 |
apply(auto simp add: less_fun_def m_s_def) |
|
419 |
apply(simp add: m_s2_rep le_fun_def) |
|
420 |
done |
|
421 |
||
422 |
lemma m_o2: "finite X \<Longrightarrow> top_on_opt (-X) o1 \<Longrightarrow> top_on_opt (-X) o2 \<Longrightarrow> |
|
423 |
o1 < o2 \<Longrightarrow> m_o X o1 > m_o X o2" |
|
424 |
proof(induction o1 o2 rule: less_eq_option.induct) |
|
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
425 |
case 1 thus ?case by (auto simp: m_s2 less_option_def) |
51722 | 426 |
next |
51749
c27bb7994bd3
moved defs into locale to reduce unnecessary polymorphism; tuned
nipkow
parents:
51722
diff
changeset
|
427 |
case 2 thus ?case by(auto simp: less_option_def le_imp_less_Suc m_s_h) |
51722 | 428 |
next |
429 |
case 3 thus ?case by (auto simp: less_option_def) |
|
430 |
qed |
|
431 |
||
432 |
lemma m_o1: "finite X \<Longrightarrow> top_on_opt (-X) o1 \<Longrightarrow> top_on_opt (-X) o2 \<Longrightarrow> |
|
433 |
o1 \<le> o2 \<Longrightarrow> m_o X o1 \<ge> m_o X o2" |
|
434 |
by(auto simp: le_less m_o2) |
|
435 |
||
436 |
||
437 |
lemma m_c2: "top_on_acom (-vars C1) C1 \<Longrightarrow> top_on_acom (-vars C2) C2 \<Longrightarrow> |
|
438 |
C1 < C2 \<Longrightarrow> m_c C1 > m_c C2" |
|
439 |
proof(auto simp add: le_iff_le_annos m_c_def size_annos_same[of C1 C2] vars_acom_def less_acom_def) |
|
440 |
let ?X = "vars(strip C2)" |
|
441 |
assume top: "top_on_acom (- vars(strip C2)) C1" "top_on_acom (- vars(strip C2)) C2" |
|
442 |
and strip_eq: "strip C1 = strip C2" |
|
443 |
and 0: "\<forall>i<size(annos C2). annos C1 ! i \<le> annos C2 ! i" |
|
444 |
hence 1: "\<forall>i<size(annos C2). m_o ?X (annos C1 ! i) \<ge> m_o ?X (annos C2 ! i)" |
|
445 |
apply (auto simp: all_set_conv_all_nth vars_acom_def top_on_acom_def) |
|
446 |
by (metis (lifting, no_types) finite_cvars m_o1 size_annos_same2) |
|
447 |
fix i assume i: "i < size(annos C2)" "\<not> annos C2 ! i \<le> annos C1 ! i" |
|
448 |
have topo1: "top_on_opt (- ?X) (annos C1 ! i)" |
|
449 |
using i(1) top(1) by(simp add: top_on_acom_def size_annos_same[OF strip_eq]) |
|
450 |
have topo2: "top_on_opt (- ?X) (annos C2 ! i)" |
|
451 |
using i(1) top(2) by(simp add: top_on_acom_def size_annos_same[OF strip_eq]) |
|
452 |
from i have "m_o ?X (annos C1 ! i) > m_o ?X (annos C2 ! i)" (is "?P i") |
|
453 |
by (metis 0 less_option_def m_o2[OF finite_cvars topo1] topo2) |
|
454 |
hence 2: "\<exists>i < size(annos C2). ?P i" using `i < size(annos C2)` by blast |
|
455 |
show "(\<Sum>i<size(annos C2). m_o ?X (annos C2 ! i)) |
|
456 |
< (\<Sum>i<size(annos C2). m_o ?X (annos C1 ! i))" |
|
457 |
apply(rule setsum_strict_mono_ex1) using 1 2 by (auto) |
|
458 |
qed |
|
459 |
||
460 |
end |
|
461 |
||
462 |
||
463 |
locale Abs_Int_fun_measure = |
|
464 |
Abs_Int_fun_mono where \<gamma>=\<gamma> + Measure_fun where m=m |
|
465 |
for \<gamma> :: "'av::semilattice \<Rightarrow> val set" and m :: "'av \<Rightarrow> nat" |
|
466 |
begin |
|
467 |
||
51754 | 468 |
lemma top_on_step': "top_on_acom (-vars C) C \<Longrightarrow> top_on_acom (-vars C) (step' \<top> C)" |
51722 | 469 |
unfolding step'_def |
470 |
by(rule top_on_Step) |
|
471 |
(auto simp add: top_option_def fa_def split: option.splits) |
|
472 |
||
473 |
lemma AI_Some_measure: "\<exists>C. AI c = Some C" |
|
474 |
unfolding AI_def |
|
51754 | 475 |
apply(rule pfp_termination[where I = "\<lambda>C. top_on_acom (- vars C) C" and m="m_c"]) |
51722 | 476 |
apply(simp_all add: m_c2 mono_step'_top bot_least top_on_bot) |
51754 | 477 |
using top_on_step' apply(auto simp add: vars_acom_def) |
51722 | 478 |
done |
479 |
||
47613 | 480 |
end |
481 |
||
482 |
text{* Problem: not executable because of the comparison of abstract states, |
|
483 |
i.e. functions, in the post-fixedpoint computation. *} |
|
484 |
||
485 |
end |