47613
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
theory Abs_Int2_ivl
|
|
4 |
imports Abs_Int2
|
|
5 |
begin
|
|
6 |
|
|
7 |
subsection "Interval Analysis"
|
|
8 |
|
51245
|
9 |
datatype lb = Minf | Lb int
|
|
10 |
datatype ub = Pinf | Ub int
|
|
11 |
|
|
12 |
datatype ivl = Ivl lb ub
|
47613
|
13 |
|
|
14 |
definition "\<gamma>_ivl i = (case i of
|
51245
|
15 |
Ivl (Lb l) (Ub h) \<Rightarrow> {l..h} |
|
|
16 |
Ivl (Lb l) Pinf \<Rightarrow> {l..} |
|
|
17 |
Ivl Minf (Ub h) \<Rightarrow> {..h} |
|
|
18 |
Ivl Minf Pinf \<Rightarrow> UNIV)"
|
47613
|
19 |
|
51245
|
20 |
abbreviation Ivl_Lb_Ub :: "int \<Rightarrow> int \<Rightarrow> ivl" ("{_\<dots>_}") where
|
|
21 |
"{lo\<dots>hi} == Ivl (Lb lo) (Ub hi)"
|
|
22 |
abbreviation Ivl_Lb_Pinf :: "int \<Rightarrow> ivl" ("{_\<dots>}") where
|
|
23 |
"{lo\<dots>} == Ivl (Lb lo) Pinf"
|
|
24 |
abbreviation Ivl_Minf_Ub :: "int \<Rightarrow> ivl" ("{\<dots>_}") where
|
|
25 |
"{\<dots>hi} == Ivl Minf (Ub hi)"
|
|
26 |
abbreviation Ivl_Minf_Pinf :: "ivl" ("{\<dots>}") where
|
|
27 |
"{\<dots>} == Ivl Minf Pinf"
|
|
28 |
|
|
29 |
lemmas lub_splits = lb.splits ub.splits
|
47613
|
30 |
|
|
31 |
definition "num_ivl n = {n\<dots>n}"
|
|
32 |
|
|
33 |
fun in_ivl :: "int \<Rightarrow> ivl \<Rightarrow> bool" where
|
51245
|
34 |
"in_ivl k (Ivl (Lb l) (Ub h)) \<longleftrightarrow> l \<le> k \<and> k \<le> h" |
|
|
35 |
"in_ivl k (Ivl (Lb l) Pinf) \<longleftrightarrow> l \<le> k" |
|
|
36 |
"in_ivl k (Ivl Minf (Ub h)) \<longleftrightarrow> k \<le> h" |
|
|
37 |
"in_ivl k (Ivl Minf Pinf) \<longleftrightarrow> True"
|
47613
|
38 |
|
51245
|
39 |
|
51261
|
40 |
instantiation lb :: linorder
|
47613
|
41 |
begin
|
|
42 |
|
51245
|
43 |
definition less_eq_lb where
|
|
44 |
"l1 \<le> l2 = (case l1 of Minf \<Rightarrow> True | Lb i1 \<Rightarrow> (case l2 of Minf \<Rightarrow> False | Lb i2 \<Rightarrow> i1 \<le> i2))"
|
|
45 |
|
|
46 |
definition less_lb :: "lb \<Rightarrow> lb \<Rightarrow> bool" where
|
|
47 |
"((l1::lb) < l2) = (l1 \<le> l2 & ~ l1 \<ge> l2)"
|
47613
|
48 |
|
51245
|
49 |
instance
|
|
50 |
proof
|
|
51 |
case goal1 show ?case by(rule less_lb_def)
|
|
52 |
next
|
|
53 |
case goal2 show ?case by(auto simp: less_eq_lb_def split:lub_splits)
|
|
54 |
next
|
|
55 |
case goal3 thus ?case by(auto simp: less_eq_lb_def split:lub_splits)
|
|
56 |
next
|
|
57 |
case goal4 thus ?case by(auto simp: less_eq_lb_def split:lub_splits)
|
51261
|
58 |
next
|
|
59 |
case goal5 thus ?case by(auto simp: less_eq_lb_def split:lub_splits)
|
51245
|
60 |
qed
|
47613
|
61 |
|
|
62 |
end
|
|
63 |
|
51261
|
64 |
instantiation ub :: linorder
|
51245
|
65 |
begin
|
|
66 |
|
|
67 |
definition less_eq_ub where
|
|
68 |
"u1 \<le> u2 = (case u2 of Pinf \<Rightarrow> True | Ub i2 \<Rightarrow> (case u1 of Pinf \<Rightarrow> False | Ub i1 \<Rightarrow> i1 \<le> i2))"
|
|
69 |
|
|
70 |
definition less_ub :: "ub \<Rightarrow> ub \<Rightarrow> bool" where
|
|
71 |
"((u1::ub) < u2) = (u1 \<le> u2 & ~ u1 \<ge> u2)"
|
|
72 |
|
|
73 |
instance
|
|
74 |
proof
|
|
75 |
case goal1 show ?case by(rule less_ub_def)
|
|
76 |
next
|
|
77 |
case goal2 show ?case by(auto simp: less_eq_ub_def split:lub_splits)
|
|
78 |
next
|
|
79 |
case goal3 thus ?case by(auto simp: less_eq_ub_def split:lub_splits)
|
|
80 |
next
|
|
81 |
case goal4 thus ?case by(auto simp: less_eq_ub_def split:lub_splits)
|
51261
|
82 |
next
|
|
83 |
case goal5 thus ?case by(auto simp: less_eq_ub_def split:lub_splits)
|
51245
|
84 |
qed
|
|
85 |
|
|
86 |
end
|
|
87 |
|
|
88 |
lemmas le_lub_defs = less_eq_lb_def less_eq_ub_def
|
|
89 |
|
51261
|
90 |
lemma le_lub_simps[simp]:
|
|
91 |
"Minf \<le> l" "Lb i \<le> Lb j = (i \<le> j)" "~ Lb i \<le> Minf"
|
|
92 |
"h \<le> Pinf" "Ub i \<le> Ub j = (i \<le> j)" "~ Pinf \<le> Ub j"
|
|
93 |
by(auto simp: le_lub_defs split: lub_splits)
|
|
94 |
|
47613
|
95 |
definition empty where "empty = {1\<dots>0}"
|
|
96 |
|
|
97 |
fun is_empty where
|
|
98 |
"is_empty {l\<dots>h} = (h<l)" |
|
|
99 |
"is_empty _ = False"
|
|
100 |
|
49579
|
101 |
lemma [simp]: "is_empty(Ivl l h) =
|
51245
|
102 |
(case l of Lb l \<Rightarrow> (case h of Ub h \<Rightarrow> h<l | Pinf \<Rightarrow> False) | Minf \<Rightarrow> False)"
|
|
103 |
by(auto split: lub_splits)
|
47613
|
104 |
|
|
105 |
lemma [simp]: "is_empty i \<Longrightarrow> \<gamma>_ivl i = {}"
|
51245
|
106 |
by(auto simp add: \<gamma>_ivl_def split: ivl.split lub_splits)
|
47613
|
107 |
|
|
108 |
|
49396
|
109 |
instantiation ivl :: semilattice
|
47613
|
110 |
begin
|
|
111 |
|
|
112 |
fun le_aux where
|
51245
|
113 |
"le_aux (Ivl l1 h1) (Ivl l2 h2) = (l2 \<le> l1 & h1 \<le> h2)"
|
47613
|
114 |
|
|
115 |
definition le_ivl where
|
|
116 |
"i1 \<sqsubseteq> i2 =
|
|
117 |
(if is_empty i1 then True else
|
|
118 |
if is_empty i2 then False else le_aux i1 i2)"
|
|
119 |
|
|
120 |
definition "i1 \<squnion> i2 =
|
|
121 |
(if is_empty i1 then i2 else if is_empty i2 then i1
|
51245
|
122 |
else case (i1,i2) of (Ivl l1 h1, Ivl l2 h2) \<Rightarrow> Ivl (min l1 l2) (max h1 h2))"
|
47613
|
123 |
|
|
124 |
definition "\<top> = {\<dots>}"
|
|
125 |
|
|
126 |
instance
|
|
127 |
proof
|
|
128 |
case goal1 thus ?case
|
51245
|
129 |
by(cases x, simp add: le_ivl_def)
|
47613
|
130 |
next
|
|
131 |
case goal2 thus ?case
|
51245
|
132 |
by(cases x, cases y, cases z, auto simp: le_ivl_def split: if_splits)
|
47613
|
133 |
next
|
|
134 |
case goal3 thus ?case
|
51245
|
135 |
by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_lub_defs min_def max_def split: lub_splits)
|
47613
|
136 |
next
|
|
137 |
case goal4 thus ?case
|
51245
|
138 |
by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_lub_defs min_def max_def split: lub_splits)
|
47613
|
139 |
next
|
|
140 |
case goal5 thus ?case
|
51245
|
141 |
by(cases x, cases y, cases z, auto simp add: le_ivl_def join_ivl_def le_lub_defs min_def max_def split: lub_splits if_splits)
|
47613
|
142 |
next
|
|
143 |
case goal6 thus ?case
|
51245
|
144 |
by(cases x, simp add: Top_ivl_def le_ivl_def le_lub_defs split: lub_splits)
|
47613
|
145 |
qed
|
|
146 |
|
|
147 |
end
|
|
148 |
|
|
149 |
|
49396
|
150 |
instantiation ivl :: lattice
|
47613
|
151 |
begin
|
|
152 |
|
|
153 |
definition "i1 \<sqinter> i2 = (if is_empty i1 \<or> is_empty i2 then empty else
|
51245
|
154 |
case (i1,i2) of (Ivl l1 h1, Ivl l2 h2) \<Rightarrow> Ivl (max l1 l2) (min h1 h2))"
|
47613
|
155 |
|
|
156 |
definition "\<bottom> = empty"
|
|
157 |
|
|
158 |
instance
|
|
159 |
proof
|
|
160 |
case goal2 thus ?case
|
51245
|
161 |
by (simp add:meet_ivl_def empty_def le_ivl_def le_lub_defs max_def min_def split: ivl.splits lub_splits)
|
47613
|
162 |
next
|
|
163 |
case goal3 thus ?case
|
51245
|
164 |
by (simp add: empty_def meet_ivl_def le_ivl_def le_lub_defs max_def min_def split: ivl.splits lub_splits)
|
47613
|
165 |
next
|
|
166 |
case goal4 thus ?case
|
51245
|
167 |
by (cases x, cases y, cases z, auto simp add: le_ivl_def meet_ivl_def empty_def le_lub_defs max_def min_def split: lub_splits if_splits)
|
47613
|
168 |
next
|
49396
|
169 |
case goal1 show ?case by(cases x, simp add: bot_ivl_def empty_def le_ivl_def)
|
47613
|
170 |
qed
|
|
171 |
|
|
172 |
end
|
|
173 |
|
51245
|
174 |
|
|
175 |
instantiation lb :: plus
|
47613
|
176 |
begin
|
|
177 |
|
51245
|
178 |
fun plus_lb where
|
|
179 |
"Lb x + Lb y = Lb(x+y)" |
|
|
180 |
"_ + _ = Minf"
|
|
181 |
|
|
182 |
instance ..
|
|
183 |
end
|
|
184 |
|
|
185 |
instantiation ub :: plus
|
|
186 |
begin
|
|
187 |
|
|
188 |
fun plus_ub where
|
|
189 |
"Ub x + Ub y = Ub(x+y)" |
|
|
190 |
"_ + _ = Pinf"
|
|
191 |
|
|
192 |
instance ..
|
|
193 |
end
|
|
194 |
|
|
195 |
instantiation ivl :: plus
|
|
196 |
begin
|
|
197 |
|
|
198 |
definition "i1+i2 = (if is_empty i1 | is_empty i2 then empty else
|
|
199 |
case (i1,i2) of (Ivl l1 h1, Ivl l2 h2) \<Rightarrow> Ivl (l1+l2) (h1+h2))"
|
47613
|
200 |
|
|
201 |
instance ..
|
51245
|
202 |
end
|
47613
|
203 |
|
51245
|
204 |
fun uminus_ub :: "ub \<Rightarrow> lb" where
|
|
205 |
"uminus_ub(Ub( x)) = Lb(-x)" |
|
|
206 |
"uminus_ub Pinf = Minf"
|
|
207 |
|
|
208 |
fun uminus_lb :: "lb \<Rightarrow> ub" where
|
|
209 |
"uminus_lb(Lb( x)) = Ub(-x)" |
|
|
210 |
"uminus_lb Minf = Pinf"
|
|
211 |
|
|
212 |
instantiation ivl :: uminus
|
|
213 |
begin
|
|
214 |
|
|
215 |
fun uminus_ivl where
|
|
216 |
"-(Ivl l h) = Ivl (uminus_ub h) (uminus_lb l)"
|
|
217 |
|
|
218 |
instance ..
|
|
219 |
end
|
|
220 |
|
51261
|
221 |
instantiation ivl :: minus
|
|
222 |
begin
|
|
223 |
|
|
224 |
definition minus_ivl :: "ivl \<Rightarrow> ivl \<Rightarrow> ivl" where
|
|
225 |
"(i1::ivl) - i2 = i1 + -i2"
|
|
226 |
|
|
227 |
instance ..
|
|
228 |
end
|
|
229 |
|
|
230 |
lemma minus_ivl_cases: "i1 - i2 = (if is_empty i1 | is_empty i2 then empty else
|
|
231 |
case (i1,i2) of (Ivl l1 h1, Ivl l2 h2) \<Rightarrow> Ivl (l1 + uminus_ub h2) (h1 + uminus_lb l2))"
|
51245
|
232 |
by(auto simp: plus_ivl_def minus_ivl_def split: ivl.split lub_splits)
|
47613
|
233 |
|
|
234 |
lemma gamma_minus_ivl:
|
51245
|
235 |
"n1 : \<gamma>_ivl i1 \<Longrightarrow> n2 : \<gamma>_ivl i2 \<Longrightarrow> n1-n2 : \<gamma>_ivl(i1 - i2)"
|
51261
|
236 |
by(auto simp add: minus_ivl_def plus_ivl_def \<gamma>_ivl_def split: ivl.splits lub_splits)
|
47613
|
237 |
|
|
238 |
definition "filter_plus_ivl i i1 i2 = ((*if is_empty i then empty else*)
|
51245
|
239 |
i1 \<sqinter> (i - i2), i2 \<sqinter> (i - i1))"
|
47613
|
240 |
|
|
241 |
fun filter_less_ivl :: "bool \<Rightarrow> ivl \<Rightarrow> ivl \<Rightarrow> ivl * ivl" where
|
49579
|
242 |
"filter_less_ivl res (Ivl l1 h1) (Ivl l2 h2) =
|
|
243 |
(if is_empty(Ivl l1 h1) \<or> is_empty(Ivl l2 h2) then (empty, empty) else
|
47613
|
244 |
if res
|
51245
|
245 |
then (Ivl l1 (min h1 (h2 + Ub -1)), Ivl (max (l1 + Lb 1) l2) h2)
|
|
246 |
else (Ivl (max l1 l2) h1, Ivl l2 (min h1 h2)))"
|
47613
|
247 |
|
|
248 |
interpretation Val_abs
|
51245
|
249 |
where \<gamma> = \<gamma>_ivl and num' = num_ivl and plus' = "op +"
|
47613
|
250 |
proof
|
|
251 |
case goal1 thus ?case
|
51245
|
252 |
by(auto simp: \<gamma>_ivl_def le_ivl_def le_lub_defs split: ivl.split lub_splits if_splits)
|
47613
|
253 |
next
|
|
254 |
case goal2 show ?case by(simp add: \<gamma>_ivl_def Top_ivl_def)
|
|
255 |
next
|
|
256 |
case goal3 thus ?case by(simp add: \<gamma>_ivl_def num_ivl_def)
|
|
257 |
next
|
|
258 |
case goal4 thus ?case
|
51245
|
259 |
by(auto simp add: \<gamma>_ivl_def plus_ivl_def split: ivl.split lub_splits)
|
47613
|
260 |
qed
|
|
261 |
|
|
262 |
interpretation Val_abs1_gamma
|
51245
|
263 |
where \<gamma> = \<gamma>_ivl and num' = num_ivl and plus' = "op +"
|
47613
|
264 |
defines aval_ivl is aval'
|
|
265 |
proof
|
|
266 |
case goal1 thus ?case
|
51245
|
267 |
by(auto simp add: \<gamma>_ivl_def meet_ivl_def empty_def min_def max_def split: ivl.split lub_splits)
|
47613
|
268 |
next
|
49396
|
269 |
case goal2 show ?case by(auto simp add: bot_ivl_def \<gamma>_ivl_def empty_def)
|
47613
|
270 |
qed
|
|
271 |
|
51245
|
272 |
lemma mono_minus_ivl: fixes i1 :: ivl
|
|
273 |
shows "i1 \<sqsubseteq> i1' \<Longrightarrow> i2 \<sqsubseteq> i2' \<Longrightarrow> i1 - i2 \<sqsubseteq> i1' - i2'"
|
51261
|
274 |
apply(auto simp add: minus_ivl_cases empty_def le_ivl_def le_lub_defs split: ivl.splits)
|
51245
|
275 |
apply(simp split: lub_splits)
|
|
276 |
apply(simp split: lub_splits)
|
|
277 |
apply(simp split: lub_splits)
|
47613
|
278 |
done
|
|
279 |
|
|
280 |
|
|
281 |
interpretation Val_abs1
|
51245
|
282 |
where \<gamma> = \<gamma>_ivl and num' = num_ivl and plus' = "op +"
|
47613
|
283 |
and test_num' = in_ivl
|
|
284 |
and filter_plus' = filter_plus_ivl and filter_less' = filter_less_ivl
|
|
285 |
proof
|
|
286 |
case goal1 thus ?case
|
51245
|
287 |
by (simp add: \<gamma>_ivl_def split: ivl.split lub_splits)
|
47613
|
288 |
next
|
|
289 |
case goal2 thus ?case
|
|
290 |
by(auto simp add: filter_plus_ivl_def)
|
|
291 |
(metis gamma_minus_ivl add_diff_cancel add_commute)+
|
|
292 |
next
|
|
293 |
case goal3 thus ?case
|
51245
|
294 |
by(cases a1, cases a2, auto simp: \<gamma>_ivl_def min_def max_def split: if_splits lub_splits)
|
47613
|
295 |
qed
|
|
296 |
|
|
297 |
interpretation Abs_Int1
|
51245
|
298 |
where \<gamma> = \<gamma>_ivl and num' = num_ivl and plus' = "op +"
|
47613
|
299 |
and test_num' = in_ivl
|
|
300 |
and filter_plus' = filter_plus_ivl and filter_less' = filter_less_ivl
|
|
301 |
defines afilter_ivl is afilter
|
|
302 |
and bfilter_ivl is bfilter
|
|
303 |
and step_ivl is step'
|
|
304 |
and AI_ivl is AI
|
|
305 |
and aval_ivl' is aval''
|
|
306 |
..
|
|
307 |
|
|
308 |
|
|
309 |
text{* Monotonicity: *}
|
|
310 |
|
|
311 |
interpretation Abs_Int1_mono
|
51245
|
312 |
where \<gamma> = \<gamma>_ivl and num' = num_ivl and plus' = "op +"
|
47613
|
313 |
and test_num' = in_ivl
|
|
314 |
and filter_plus' = filter_plus_ivl and filter_less' = filter_less_ivl
|
|
315 |
proof
|
|
316 |
case goal1 thus ?case
|
51245
|
317 |
by(auto simp: plus_ivl_def le_ivl_def le_lub_defs empty_def split: if_splits ivl.splits lub_splits)
|
47613
|
318 |
next
|
|
319 |
case goal2 thus ?case
|
|
320 |
by(auto simp: filter_plus_ivl_def le_prod_def mono_meet mono_minus_ivl)
|
|
321 |
next
|
|
322 |
case goal3 thus ?case
|
|
323 |
apply(cases a1, cases b1, cases a2, cases b2, auto simp: le_prod_def)
|
51245
|
324 |
by(auto simp add: empty_def le_ivl_def le_lub_defs min_def max_def split: lub_splits)
|
47613
|
325 |
qed
|
|
326 |
|
|
327 |
|
|
328 |
subsubsection "Tests"
|
|
329 |
|
51036
|
330 |
value "show_acom_opt (AI_ivl test1_ivl)"
|
47613
|
331 |
|
|
332 |
text{* Better than @{text AI_const}: *}
|
51036
|
333 |
value "show_acom_opt (AI_ivl test3_const)"
|
|
334 |
value "show_acom_opt (AI_ivl test4_const)"
|
|
335 |
value "show_acom_opt (AI_ivl test6_const)"
|
47613
|
336 |
|
51036
|
337 |
definition "steps c i = (step_ivl(top(vars c)) ^^ i) (bot c)"
|
47613
|
338 |
|
51036
|
339 |
value "show_acom_opt (AI_ivl test2_ivl)"
|
47613
|
340 |
value "show_acom (steps test2_ivl 0)"
|
|
341 |
value "show_acom (steps test2_ivl 1)"
|
|
342 |
value "show_acom (steps test2_ivl 2)"
|
49188
|
343 |
value "show_acom (steps test2_ivl 3)"
|
47613
|
344 |
|
51036
|
345 |
text{* Fixed point reached in 2 steps.
|
47613
|
346 |
Not so if the start value of x is known: *}
|
|
347 |
|
51036
|
348 |
value "show_acom_opt (AI_ivl test3_ivl)"
|
47613
|
349 |
value "show_acom (steps test3_ivl 0)"
|
|
350 |
value "show_acom (steps test3_ivl 1)"
|
|
351 |
value "show_acom (steps test3_ivl 2)"
|
|
352 |
value "show_acom (steps test3_ivl 3)"
|
|
353 |
value "show_acom (steps test3_ivl 4)"
|
49188
|
354 |
value "show_acom (steps test3_ivl 5)"
|
47613
|
355 |
|
|
356 |
text{* Takes as many iterations as the actual execution. Would diverge if
|
|
357 |
loop did not terminate. Worse still, as the following example shows: even if
|
|
358 |
the actual execution terminates, the analysis may not. The value of y keeps
|
|
359 |
decreasing as the analysis is iterated, no matter how long: *}
|
|
360 |
|
|
361 |
value "show_acom (steps test4_ivl 50)"
|
|
362 |
|
|
363 |
text{* Relationships between variables are NOT captured: *}
|
51036
|
364 |
value "show_acom_opt (AI_ivl test5_ivl)"
|
47613
|
365 |
|
|
366 |
text{* Again, the analysis would not terminate: *}
|
|
367 |
value "show_acom (steps test6_ivl 50)"
|
|
368 |
|
|
369 |
end
|