author | haftmann |
Tue, 23 Feb 2010 10:11:49 +0100 | |
changeset 35320 | f80aee1ed475 |
parent 35113 | src/HOL/Hoare/HoareAbort.thy@1a0c129bb2e0 |
child 35352 | fa051b504c3f |
child 35416 | d8d7d1b785af |
permissions | -rw-r--r-- |
13857 | 1 |
(* Title: HOL/Hoare/HoareAbort.thy |
2 |
Author: Leonor Prensa Nieto & Tobias Nipkow |
|
3 |
Copyright 2003 TUM |
|
4 |
||
5 |
Like Hoare.thy, but with an Abort statement for modelling run time errors. |
|
6 |
*) |
|
7 |
||
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
8 |
theory Hoare_Logic_Abort |
28457
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents:
27330
diff
changeset
|
9 |
imports Main |
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents:
27330
diff
changeset
|
10 |
uses ("hoare_tac.ML") |
24470
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
11 |
begin |
13857 | 12 |
|
13 |
types |
|
14 |
'a bexp = "'a set" |
|
15 |
'a assn = "'a set" |
|
16 |
||
17 |
datatype |
|
18 |
'a com = Basic "'a \<Rightarrow> 'a" |
|
19 |
| Abort |
|
20 |
| Seq "'a com" "'a com" ("(_;/ _)" [61,60] 60) |
|
21 |
| Cond "'a bexp" "'a com" "'a com" ("(1IF _/ THEN _ / ELSE _/ FI)" [0,0,0] 61) |
|
22 |
| While "'a bexp" "'a assn" "'a com" ("(1WHILE _/ INV {_} //DO _ /OD)" [0,0,0] 61) |
|
35113 | 23 |
|
35054 | 24 |
abbreviation annskip ("SKIP") where "SKIP == Basic id" |
13857 | 25 |
|
26 |
types 'a sem = "'a option => 'a option => bool" |
|
27 |
||
28 |
consts iter :: "nat => 'a bexp => 'a sem => 'a sem" |
|
29 |
primrec |
|
13875 | 30 |
"iter 0 b S = (\<lambda>s s'. s \<notin> Some ` b \<and> s=s')" |
31 |
"iter (Suc n) b S = |
|
32 |
(\<lambda>s s'. s \<in> Some ` b \<and> (\<exists>s''. S s s'' \<and> iter n b S s'' s'))" |
|
13857 | 33 |
|
34 |
consts Sem :: "'a com => 'a sem" |
|
35 |
primrec |
|
36 |
"Sem(Basic f) s s' = (case s of None \<Rightarrow> s' = None | Some t \<Rightarrow> s' = Some(f t))" |
|
37 |
"Sem Abort s s' = (s' = None)" |
|
13875 | 38 |
"Sem(c1;c2) s s' = (\<exists>s''. Sem c1 s s'' \<and> Sem c2 s'' s')" |
13857 | 39 |
"Sem(IF b THEN c1 ELSE c2 FI) s s' = |
40 |
(case s of None \<Rightarrow> s' = None |
|
13875 | 41 |
| Some t \<Rightarrow> ((t \<in> b \<longrightarrow> Sem c1 s s') \<and> (t \<notin> b \<longrightarrow> Sem c2 s s')))" |
13857 | 42 |
"Sem(While b x c) s s' = |
13875 | 43 |
(if s = None then s' = None else \<exists>n. iter n b (Sem c) s s')" |
13857 | 44 |
|
45 |
constdefs Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool" |
|
46 |
"Valid p c q == \<forall>s s'. Sem c s s' \<longrightarrow> s : Some ` p \<longrightarrow> s' : Some ` q" |
|
47 |
||
48 |
||
49 |
||
50 |
(** parse translations **) |
|
51 |
||
35054 | 52 |
syntax |
53 |
"_assign" :: "id => 'b => 'a com" ("(2_ :=/ _)" [70,65] 61) |
|
54 |
||
55 |
syntax |
|
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
56 |
"_hoare_abort_vars" :: "[idts, 'a assn,'a com,'a assn] => bool" |
35054 | 57 |
("VARS _// {_} // _ // {_}" [0,0,55,0] 50) |
58 |
syntax ("" output) |
|
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
59 |
"_hoare_abort" :: "['a assn,'a com,'a assn] => bool" |
35054 | 60 |
("{_} // _ // {_}" [0,55,0] 50) |
61 |
ML {* |
|
13857 | 62 |
|
63 |
local |
|
64 |
fun free a = Free(a,dummyT) |
|
65 |
fun abs((a,T),body) = |
|
66 |
let val a = absfree(a, dummyT, body) |
|
67 |
in if T = Bound 0 then a else Const(Syntax.constrainAbsC,dummyT) $ a $ T end |
|
68 |
in |
|
69 |
||
70 |
fun mk_abstuple [x] body = abs (x, body) |
|
71 |
| mk_abstuple (x::xs) body = |
|
35113 | 72 |
Syntax.const @{const_syntax split} $ abs (x, mk_abstuple xs body); |
13857 | 73 |
|
74 |
fun mk_fbody a e [x as (b,_)] = if a=b then e else free b |
|
75 |
| mk_fbody a e ((b,_)::xs) = |
|
35113 | 76 |
Syntax.const @{const_syntax Pair} $ (if a=b then e else free b) $ mk_fbody a e xs; |
13857 | 77 |
|
78 |
fun mk_fexp a e xs = mk_abstuple xs (mk_fbody a e xs) |
|
79 |
end |
|
80 |
*} |
|
81 |
||
82 |
(* bexp_tr & assn_tr *) |
|
83 |
(*all meta-variables for bexp except for TRUE are translated as if they |
|
84 |
were boolean expressions*) |
|
85 |
ML{* |
|
35113 | 86 |
fun bexp_tr (Const ("TRUE", _)) xs = Syntax.const "TRUE" (* FIXME !? *) |
87 |
| bexp_tr b xs = Syntax.const @{const_syntax Collect} $ mk_abstuple xs b; |
|
88 |
||
89 |
fun assn_tr r xs = Syntax.const @{const_syntax Collect} $ mk_abstuple xs r; |
|
13857 | 90 |
*} |
91 |
(* com_tr *) |
|
92 |
ML{* |
|
35113 | 93 |
fun com_tr (Const (@{syntax_const "_assign"},_) $ Free (a,_) $ e) xs = |
94 |
Syntax.const @{const_syntax Basic} $ mk_fexp a e xs |
|
95 |
| com_tr (Const (@{const_syntax Basic},_) $ f) xs = Syntax.const @{const_syntax Basic} $ f |
|
96 |
| com_tr (Const (@{const_syntax Seq},_) $ c1 $ c2) xs = |
|
97 |
Syntax.const @{const_syntax Seq} $ com_tr c1 xs $ com_tr c2 xs |
|
98 |
| com_tr (Const (@{const_syntax Cond},_) $ b $ c1 $ c2) xs = |
|
99 |
Syntax.const @{const_syntax Cond} $ bexp_tr b xs $ com_tr c1 xs $ com_tr c2 xs |
|
100 |
| com_tr (Const (@{const_syntax While},_) $ b $ I $ c) xs = |
|
101 |
Syntax.const @{const_syntax While} $ bexp_tr b xs $ assn_tr I xs $ com_tr c xs |
|
13857 | 102 |
| com_tr t _ = t (* if t is just a Free/Var *) |
103 |
*} |
|
104 |
||
17781 | 105 |
(* triple_tr *) (* FIXME does not handle "_idtdummy" *) |
13857 | 106 |
ML{* |
107 |
local |
|
108 |
||
35113 | 109 |
fun var_tr (Free (a, _)) = (a, Bound 0) (* Bound 0 = dummy term *) |
110 |
| var_tr (Const (@{syntax_const "_constrain"}, _) $ Free (a, _) $ T) = (a, T); |
|
13857 | 111 |
|
35113 | 112 |
fun vars_tr (Const (@{syntax_const "_idts"}, _) $ idt $ vars) = var_tr idt :: vars_tr vars |
13857 | 113 |
| vars_tr t = [var_tr t] |
114 |
||
115 |
in |
|
116 |
fun hoare_vars_tr [vars, pre, prg, post] = |
|
117 |
let val xs = vars_tr vars |
|
35113 | 118 |
in Syntax.const @{const_syntax Valid} $ |
13857 | 119 |
assn_tr pre xs $ com_tr prg xs $ assn_tr post xs |
120 |
end |
|
121 |
| hoare_vars_tr ts = raise TERM ("hoare_vars_tr", ts); |
|
122 |
end |
|
123 |
*} |
|
124 |
||
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
125 |
parse_translation {* [(@{syntax_const "_hoare_abort_vars"}, hoare_vars_tr)] *} |
13857 | 126 |
|
127 |
||
128 |
(*****************************************************************************) |
|
129 |
||
130 |
(*** print translations ***) |
|
131 |
ML{* |
|
35113 | 132 |
fun dest_abstuple (Const (@{const_syntax split},_) $ (Abs(v,_, body))) = |
133 |
subst_bound (Syntax.free v, dest_abstuple body) |
|
13857 | 134 |
| dest_abstuple (Abs(v,_, body)) = subst_bound (Syntax.free v, body) |
135 |
| dest_abstuple trm = trm; |
|
136 |
||
35113 | 137 |
fun abs2list (Const (@{const_syntax split},_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t |
13857 | 138 |
| abs2list (Abs(x,T,t)) = [Free (x, T)] |
139 |
| abs2list _ = []; |
|
140 |
||
35113 | 141 |
fun mk_ts (Const (@{const_syntax split},_) $ (Abs(x,_,t))) = mk_ts t |
13857 | 142 |
| mk_ts (Abs(x,_,t)) = mk_ts t |
35113 | 143 |
| mk_ts (Const (@{const_syntax Pair},_) $ a $ b) = a::(mk_ts b) |
13857 | 144 |
| mk_ts t = [t]; |
145 |
||
35113 | 146 |
fun mk_vts (Const (@{const_syntax split},_) $ (Abs(x,_,t))) = |
13857 | 147 |
((Syntax.free x)::(abs2list t), mk_ts t) |
148 |
| mk_vts (Abs(x,_,t)) = ([Syntax.free x], [t]) |
|
149 |
| mk_vts t = raise Match; |
|
35113 | 150 |
|
151 |
fun find_ch [] i xs = (false, (Syntax.free "not_ch", Syntax.free "not_ch")) |
|
152 |
| find_ch ((v,t)::vts) i xs = |
|
153 |
if t = Bound i then find_ch vts (i-1) xs |
|
154 |
else (true, (v, subst_bounds (xs,t))); |
|
155 |
||
156 |
fun is_f (Const (@{const_syntax split},_) $ (Abs(x,_,t))) = true |
|
13857 | 157 |
| is_f (Abs(x,_,t)) = true |
158 |
| is_f t = false; |
|
159 |
*} |
|
160 |
||
161 |
(* assn_tr' & bexp_tr'*) |
|
35113 | 162 |
ML{* |
163 |
fun assn_tr' (Const (@{const_syntax Collect},_) $ T) = dest_abstuple T |
|
164 |
| assn_tr' (Const (@{const_syntax inter},_) $ (Const (@{const_syntax Collect},_) $ T1) $ |
|
165 |
(Const (@{const_syntax Collect},_) $ T2)) = |
|
166 |
Syntax.const @{const_syntax inter} $ dest_abstuple T1 $ dest_abstuple T2 |
|
13857 | 167 |
| assn_tr' t = t; |
168 |
||
35113 | 169 |
fun bexp_tr' (Const (@{const_syntax Collect},_) $ T) = dest_abstuple T |
13857 | 170 |
| bexp_tr' t = t; |
171 |
*} |
|
172 |
||
173 |
(*com_tr' *) |
|
174 |
ML{* |
|
175 |
fun mk_assign f = |
|
176 |
let val (vs, ts) = mk_vts f; |
|
177 |
val (ch, which) = find_ch (vs~~ts) ((length vs)-1) (rev vs) |
|
35113 | 178 |
in |
179 |
if ch then Syntax.const @{syntax_const "_assign"} $ fst which $ snd which |
|
180 |
else Syntax.const @{const_syntax annskip} |
|
181 |
end; |
|
13857 | 182 |
|
35113 | 183 |
fun com_tr' (Const (@{const_syntax Basic},_) $ f) = |
184 |
if is_f f then mk_assign f else Syntax.const @{const_syntax Basic} $ f |
|
185 |
| com_tr' (Const (@{const_syntax Seq},_) $ c1 $ c2) = |
|
186 |
Syntax.const @{const_syntax Seq} $ com_tr' c1 $ com_tr' c2 |
|
187 |
| com_tr' (Const (@{const_syntax Cond},_) $ b $ c1 $ c2) = |
|
188 |
Syntax.const @{const_syntax Cond} $ bexp_tr' b $ com_tr' c1 $ com_tr' c2 |
|
189 |
| com_tr' (Const (@{const_syntax While},_) $ b $ I $ c) = |
|
190 |
Syntax.const @{const_syntax While} $ bexp_tr' b $ assn_tr' I $ com_tr' c |
|
13857 | 191 |
| com_tr' t = t; |
192 |
||
193 |
fun spec_tr' [p, c, q] = |
|
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
194 |
Syntax.const @{syntax_const "_hoare_abort"} $ assn_tr' p $ com_tr' c $ assn_tr' q |
13857 | 195 |
*} |
196 |
||
35054 | 197 |
print_translation {* [(@{const_syntax Valid}, spec_tr')] *} |
13857 | 198 |
|
199 |
(*** The proof rules ***) |
|
200 |
||
201 |
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q" |
|
202 |
by (auto simp:Valid_def) |
|
203 |
||
204 |
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q" |
|
205 |
by (auto simp:Valid_def) |
|
206 |
||
207 |
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R" |
|
208 |
by (auto simp:Valid_def) |
|
209 |
||
210 |
lemma CondRule: |
|
211 |
"p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')} |
|
212 |
\<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q" |
|
213 |
by (fastsimp simp:Valid_def image_def) |
|
214 |
||
13875 | 215 |
lemma iter_aux: |
216 |
"! s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow> |
|
217 |
(\<And>s s'. s \<in> Some ` I \<Longrightarrow> iter n b (Sem c) s s' \<Longrightarrow> s' \<in> Some ` (I \<inter> -b))"; |
|
13857 | 218 |
apply(unfold image_def) |
219 |
apply(induct n) |
|
220 |
apply clarsimp |
|
221 |
apply(simp (no_asm_use)) |
|
222 |
apply blast |
|
223 |
done |
|
224 |
||
225 |
lemma WhileRule: |
|
226 |
"p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q" |
|
227 |
apply(simp add:Valid_def) |
|
228 |
apply(simp (no_asm) add:image_def) |
|
229 |
apply clarify |
|
230 |
apply(drule iter_aux) |
|
231 |
prefer 2 apply assumption |
|
232 |
apply blast |
|
233 |
apply blast |
|
234 |
done |
|
235 |
||
236 |
lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort q" |
|
237 |
by(auto simp:Valid_def) |
|
238 |
||
24470
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
239 |
|
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
240 |
subsection {* Derivation of the proof rules and, most importantly, the VCG tactic *} |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
241 |
|
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
242 |
lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}" |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
243 |
by blast |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
244 |
|
28457
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents:
27330
diff
changeset
|
245 |
use "hoare_tac.ML" |
13857 | 246 |
|
247 |
method_setup vcg = {* |
|
30549 | 248 |
Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *} |
13857 | 249 |
"verification condition generator" |
250 |
||
251 |
method_setup vcg_simp = {* |
|
30549 | 252 |
Scan.succeed (fn ctxt => |
32149
ef59550a55d3
renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents:
32134
diff
changeset
|
253 |
SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *} |
13857 | 254 |
"verification condition generator plus simplification" |
255 |
||
13875 | 256 |
(* Special syntax for guarded statements and guarded array updates: *) |
257 |
||
258 |
syntax |
|
259 |
guarded_com :: "bool \<Rightarrow> 'a com \<Rightarrow> 'a com" ("(2_ \<rightarrow>/ _)" 71) |
|
35054 | 260 |
array_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a com" ("(2_[_] :=/ _)" [70, 65] 61) |
13875 | 261 |
translations |
35101 | 262 |
"P \<rightarrow> c" == "IF P THEN c ELSE CONST Abort FI" |
34940 | 263 |
"a[i] := v" => "(i < CONST length a) \<rightarrow> (a := CONST list_update a i v)" |
13875 | 264 |
(* reverse translation not possible because of duplicate "a" *) |
265 |
||
266 |
text{* Note: there is no special syntax for guarded array access. Thus |
|
267 |
you must write @{text"j < length a \<rightarrow> a[i] := a!j"}. *} |
|
268 |
||
13857 | 269 |
end |