44656
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
theory AbsInt1_ivl
|
|
4 |
imports AbsInt1
|
|
5 |
begin
|
|
6 |
|
|
7 |
subsection "Interval Analysis"
|
|
8 |
|
|
9 |
datatype ivl = I "int option" "int option"
|
|
10 |
|
44932
|
11 |
text{* We assume an important invariant: arithmetic operations are never
|
|
12 |
applied to empty intervals @{term"I (Some i) (Some j)"} with @{term"j <
|
|
13 |
i"}. This avoids special cases. Why can we assume this? Because an empty
|
|
14 |
interval of values for a variable means that the current program point is
|
|
15 |
unreachable. But this should actually translate into the bottom state, not a
|
|
16 |
state where some variables have empty intervals. *}
|
|
17 |
|
44656
|
18 |
definition "rep_ivl i =
|
|
19 |
(case i of I (Some l) (Some h) \<Rightarrow> {l..h} | I (Some l) None \<Rightarrow> {l..}
|
|
20 |
| I None (Some h) \<Rightarrow> {..h} | I None None \<Rightarrow> UNIV)"
|
|
21 |
|
44932
|
22 |
definition "num_ivl n = I (Some n) (Some n)"
|
|
23 |
|
44656
|
24 |
instantiation option :: (plus)plus
|
|
25 |
begin
|
|
26 |
|
|
27 |
fun plus_option where
|
|
28 |
"Some x + Some y = Some(x+y)" |
|
|
29 |
"_ + _ = None"
|
|
30 |
|
|
31 |
instance proof qed
|
|
32 |
|
|
33 |
end
|
|
34 |
|
|
35 |
definition empty where "empty = I (Some 1) (Some 0)"
|
|
36 |
|
|
37 |
fun is_empty where
|
|
38 |
"is_empty(I (Some l) (Some h)) = (h<l)" |
|
|
39 |
"is_empty _ = False"
|
|
40 |
|
|
41 |
lemma [simp]: "is_empty(I l h) =
|
|
42 |
(case l of Some l \<Rightarrow> (case h of Some h \<Rightarrow> h<l | None \<Rightarrow> False) | None \<Rightarrow> False)"
|
|
43 |
by(auto split:option.split)
|
|
44 |
|
|
45 |
lemma [simp]: "is_empty i \<Longrightarrow> rep_ivl i = {}"
|
|
46 |
by(auto simp add: rep_ivl_def split: ivl.split option.split)
|
|
47 |
|
44932
|
48 |
definition "plus_ivl i1 i2 = ((*if is_empty i1 | is_empty i2 then empty else*)
|
|
49 |
case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow> I (l1+l2) (h1+h2))"
|
44656
|
50 |
|
|
51 |
instantiation ivl :: SL_top
|
|
52 |
begin
|
|
53 |
|
|
54 |
definition le_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> bool" where
|
|
55 |
"le_option pos x y =
|
44932
|
56 |
(case x of (Some i) \<Rightarrow> (case y of Some j \<Rightarrow> i\<le>j | None \<Rightarrow> pos)
|
|
57 |
| None \<Rightarrow> (case y of Some j \<Rightarrow> \<not>pos | None \<Rightarrow> True))"
|
44656
|
58 |
|
|
59 |
fun le_aux where
|
|
60 |
"le_aux (I l1 h1) (I l2 h2) = (le_option False l2 l1 & le_option True h1 h2)"
|
|
61 |
|
|
62 |
definition le_ivl where
|
|
63 |
"i1 \<sqsubseteq> i2 =
|
|
64 |
(if is_empty i1 then True else
|
|
65 |
if is_empty i2 then False else le_aux i1 i2)"
|
|
66 |
|
|
67 |
definition min_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> int option" where
|
|
68 |
"min_option pos o1 o2 = (if le_option pos o1 o2 then o1 else o2)"
|
|
69 |
|
|
70 |
definition max_option :: "bool \<Rightarrow> int option \<Rightarrow> int option \<Rightarrow> int option" where
|
|
71 |
"max_option pos o1 o2 = (if le_option pos o1 o2 then o2 else o1)"
|
|
72 |
|
|
73 |
definition "i1 \<squnion> i2 =
|
|
74 |
(if is_empty i1 then i2 else if is_empty i2 then i1
|
|
75 |
else case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow>
|
|
76 |
I (min_option False l1 l2) (max_option True h1 h2))"
|
|
77 |
|
|
78 |
definition "Top = I None None"
|
|
79 |
|
|
80 |
instance
|
|
81 |
proof
|
|
82 |
case goal1 thus ?case
|
|
83 |
by(cases x, simp add: le_ivl_def le_option_def split: option.split)
|
|
84 |
next
|
|
85 |
case goal2 thus ?case
|
|
86 |
by(cases x, cases y, cases z, auto simp: le_ivl_def le_option_def split: option.splits if_splits)
|
|
87 |
next
|
|
88 |
case goal3 thus ?case
|
|
89 |
by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits)
|
|
90 |
next
|
|
91 |
case goal4 thus ?case
|
|
92 |
by(cases x, cases y, simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits)
|
|
93 |
next
|
|
94 |
case goal5 thus ?case
|
|
95 |
by(cases x, cases y, cases z, auto simp add: le_ivl_def join_ivl_def le_option_def min_option_def max_option_def split: option.splits if_splits)
|
|
96 |
next
|
|
97 |
case goal6 thus ?case
|
|
98 |
by(cases x, simp add: Top_ivl_def le_ivl_def le_option_def split: option.split)
|
|
99 |
qed
|
|
100 |
|
|
101 |
end
|
|
102 |
|
|
103 |
|
|
104 |
instantiation ivl :: L_top_bot
|
|
105 |
begin
|
|
106 |
|
44932
|
107 |
definition "i1 \<sqinter> i2 = (if is_empty i1 \<or> is_empty i2 then empty else
|
|
108 |
case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow>
|
|
109 |
I (max_option False l1 l2) (min_option True h1 h2))"
|
44656
|
110 |
|
|
111 |
definition "Bot = empty"
|
|
112 |
|
|
113 |
instance
|
|
114 |
proof
|
|
115 |
case goal1 thus ?case
|
|
116 |
by (simp add:meet_ivl_def empty_def meet_ivl_def le_ivl_def le_option_def max_option_def min_option_def split: ivl.splits option.splits)
|
|
117 |
next
|
|
118 |
case goal2 thus ?case
|
|
119 |
by (simp add:meet_ivl_def empty_def meet_ivl_def le_ivl_def le_option_def max_option_def min_option_def split: ivl.splits option.splits)
|
|
120 |
next
|
|
121 |
case goal3 thus ?case
|
|
122 |
by (cases x, cases y, cases z, auto simp add: le_ivl_def meet_ivl_def empty_def le_option_def max_option_def min_option_def split: option.splits if_splits)
|
|
123 |
next
|
|
124 |
case goal4 show ?case by(cases x, simp add: Bot_ivl_def empty_def le_ivl_def)
|
|
125 |
qed
|
|
126 |
|
|
127 |
end
|
|
128 |
|
|
129 |
instantiation option :: (minus)minus
|
|
130 |
begin
|
|
131 |
|
|
132 |
fun minus_option where
|
|
133 |
"Some x - Some y = Some(x-y)" |
|
|
134 |
"_ - _ = None"
|
|
135 |
|
|
136 |
instance proof qed
|
|
137 |
|
|
138 |
end
|
|
139 |
|
44932
|
140 |
definition "minus_ivl i1 i2 = ((*if is_empty i1 | is_empty i2 then empty else*)
|
|
141 |
case (i1,i2) of (I l1 h1, I l2 h2) \<Rightarrow> I (l1-h2) (h1-l2))"
|
44656
|
142 |
|
|
143 |
lemma rep_minus_ivl:
|
|
144 |
"n1 : rep_ivl i1 \<Longrightarrow> n2 : rep_ivl i2 \<Longrightarrow> n1-n2 : rep_ivl(minus_ivl i1 i2)"
|
|
145 |
by(auto simp add: minus_ivl_def rep_ivl_def split: ivl.splits option.splits)
|
|
146 |
|
|
147 |
|
44932
|
148 |
definition "inv_plus_ivl i1 i2 i = ((*if is_empty i then empty else*)
|
|
149 |
i1 \<sqinter> minus_ivl i i2, i2 \<sqinter> minus_ivl i i1)"
|
44656
|
150 |
|
|
151 |
fun inv_less_ivl :: "ivl \<Rightarrow> ivl \<Rightarrow> bool \<Rightarrow> ivl * ivl" where
|
|
152 |
"inv_less_ivl (I l1 h1) (I l2 h2) res =
|
45019
|
153 |
((*if is_empty(I l1 h1) \<or> is_empty(I l2 h2) then (empty, empty) else*)
|
|
154 |
if res
|
44932
|
155 |
then (I l1 (min_option True h1 (h2 - Some 1)),
|
|
156 |
I (max_option False (l1 + Some 1) l2) h2)
|
44656
|
157 |
else (I (max_option False l1 l2) h1, I l2 (min_option True h1 h2)))"
|
|
158 |
|
|
159 |
interpretation Rep rep_ivl
|
|
160 |
proof
|
|
161 |
case goal1 thus ?case
|
|
162 |
by(auto simp: rep_ivl_def le_ivl_def le_option_def split: ivl.split option.split if_splits)
|
|
163 |
qed
|
|
164 |
|
44932
|
165 |
interpretation Val_abs rep_ivl num_ivl plus_ivl
|
44656
|
166 |
proof
|
44932
|
167 |
case goal1 thus ?case by(simp add: rep_ivl_def num_ivl_def)
|
44656
|
168 |
next
|
|
169 |
case goal2 thus ?case
|
|
170 |
by(auto simp add: rep_ivl_def plus_ivl_def split: ivl.split option.splits)
|
|
171 |
qed
|
|
172 |
|
|
173 |
interpretation Rep1 rep_ivl
|
|
174 |
proof
|
|
175 |
case goal1 thus ?case
|
|
176 |
by(auto simp add: rep_ivl_def meet_ivl_def empty_def min_option_def max_option_def split: ivl.split option.split)
|
|
177 |
next
|
|
178 |
case goal2 show ?case by(auto simp add: Bot_ivl_def rep_ivl_def empty_def)
|
|
179 |
qed
|
|
180 |
|
44932
|
181 |
interpretation
|
|
182 |
Val_abs1 rep_ivl num_ivl plus_ivl inv_plus_ivl inv_less_ivl
|
44656
|
183 |
proof
|
|
184 |
case goal1 thus ?case
|
|
185 |
by(auto simp add: inv_plus_ivl_def)
|
|
186 |
(metis rep_minus_ivl add_diff_cancel add_commute)+
|
|
187 |
next
|
|
188 |
case goal2 thus ?case
|
|
189 |
by(cases a1, cases a2,
|
|
190 |
auto simp: rep_ivl_def min_option_def max_option_def le_option_def split: if_splits option.splits)
|
|
191 |
qed
|
|
192 |
|
44932
|
193 |
interpretation
|
44944
|
194 |
Abs_Int1 rep_ivl num_ivl plus_ivl inv_plus_ivl inv_less_ivl "(iter' 3)"
|
44656
|
195 |
defines afilter_ivl is afilter
|
|
196 |
and bfilter_ivl is bfilter
|
|
197 |
and AI_ivl is AI
|
|
198 |
and aval_ivl is aval'
|
44944
|
199 |
proof qed (auto simp: iter'_pfp_above)
|
44656
|
200 |
|
|
201 |
|
|
202 |
fun list_up where
|
|
203 |
"list_up bot = bot" |
|
|
204 |
"list_up (Up x) = Up(list x)"
|
|
205 |
|
|
206 |
value [code] "list_up(afilter_ivl (N 5) (I (Some 4) (Some 5)) Top)"
|
|
207 |
value [code] "list_up(afilter_ivl (N 5) (I (Some 4) (Some 4)) Top)"
|
|
208 |
value [code] "list_up(afilter_ivl (V ''x'') (I (Some 4) (Some 4))
|
|
209 |
(Up(FunDom(Top(''x'':=I (Some 5) (Some 6))) [''x''])))"
|
|
210 |
value [code] "list_up(afilter_ivl (V ''x'') (I (Some 4) (Some 5))
|
|
211 |
(Up(FunDom(Top(''x'':=I (Some 5) (Some 6))) [''x''])))"
|
|
212 |
value [code] "list_up(afilter_ivl (Plus (V ''x'') (V ''x'')) (I (Some 0) (Some 10))
|
|
213 |
(Up(FunDom(Top(''x'':= I (Some 0) (Some 100)))[''x''])))"
|
|
214 |
value [code] "list_up(afilter_ivl (Plus (V ''x'') (N 7)) (I (Some 0) (Some 10))
|
|
215 |
(Up(FunDom(Top(''x'':= I (Some 0) (Some 100)))[''x''])))"
|
|
216 |
|
|
217 |
value [code] "list_up(bfilter_ivl (Less (V ''x'') (V ''x'')) True
|
|
218 |
(Up(FunDom(Top(''x'':= I (Some 0) (Some 0)))[''x''])))"
|
|
219 |
value [code] "list_up(bfilter_ivl (Less (V ''x'') (V ''x'')) True
|
|
220 |
(Up(FunDom(Top(''x'':= I (Some 0) (Some 2)))[''x''])))"
|
|
221 |
value [code] "list_up(bfilter_ivl (Less (V ''x'') (Plus (N 10) (V ''y''))) True
|
|
222 |
(Up(FunDom(Top(''x'':= I (Some 15) (Some 20),''y'':= I (Some 5) (Some 7)))[''x'', ''y''])))"
|
|
223 |
|
|
224 |
definition "test_ivl1 =
|
|
225 |
''y'' ::= N 7;
|
|
226 |
IF Less (V ''x'') (V ''y'')
|
|
227 |
THEN ''y'' ::= Plus (V ''y'') (V ''x'')
|
|
228 |
ELSE ''x'' ::= Plus (V ''x'') (V ''y'')"
|
|
229 |
value [code] "list_up(AI_ivl test_ivl1 Top)"
|
|
230 |
|
|
231 |
value "list_up (AI_ivl test3_const Top)"
|
|
232 |
|
|
233 |
value "list_up (AI_ivl test5_const Top)"
|
|
234 |
|
|
235 |
value "list_up (AI_ivl test6_const Top)"
|
|
236 |
|
|
237 |
definition "test2_ivl =
|
|
238 |
''y'' ::= N 7;
|
|
239 |
WHILE Less (V ''x'') (N 100) DO ''y'' ::= Plus (V ''y'') (N 1)"
|
|
240 |
value [code] "list_up(AI_ivl test2_ivl Top)"
|
|
241 |
|
44932
|
242 |
definition "test3_ivl =
|
|
243 |
''x'' ::= N 0; ''y'' ::= N 100; ''z'' ::= Plus (V ''x'') (V ''y'');
|
|
244 |
WHILE Less (V ''x'') (N 11)
|
|
245 |
DO (''x'' ::= Plus (V ''x'') (N 1); ''y'' ::= Plus (V ''y'') (N -1))"
|
|
246 |
value [code] "list_up(AI_ivl test3_ivl Top)"
|
|
247 |
|
|
248 |
definition "test4_ivl =
|
|
249 |
''x'' ::= N 0; ''y'' ::= N 0;
|
|
250 |
WHILE Less (V ''x'') (N 1001)
|
|
251 |
DO (''y'' ::= V ''x''; ''x'' ::= Plus (V ''x'') (N 1))"
|
|
252 |
value [code] "list_up(AI_ivl test4_ivl Top)"
|
|
253 |
|
45020
|
254 |
text{* Nontermination not detected: *}
|
|
255 |
definition "test5_ivl =
|
|
256 |
''x'' ::= N 0;
|
|
257 |
WHILE Less (V ''x'') (N 1) DO ''x'' ::= Plus (V ''x'') (N -1)"
|
|
258 |
value [code] "list_up(AI_ivl test5_ivl Top)"
|
|
259 |
|
44656
|
260 |
end
|