author | nipkow |
Mon, 01 Sep 2008 22:10:42 +0200 | |
changeset 28072 | a45e8c872dc1 |
parent 24472 | 943ef707396c |
child 28457 | 25669513fd4c |
permissions | -rw-r--r-- |
1476 | 1 |
(* Title: HOL/Hoare/Hoare.thy |
1335 | 2 |
ID: $Id$ |
5646 | 3 |
Author: Leonor Prensa Nieto & Tobias Nipkow |
4 |
Copyright 1998 TUM |
|
1335 | 5 |
|
6 |
Sugared semantic embedding of Hoare logic. |
|
5646 | 7 |
Strictly speaking a shallow embedding (as implemented by Norbert Galm |
8 |
following Mike Gordon) would suffice. Maybe the datatype com comes in useful |
|
9 |
later. |
|
1335 | 10 |
*) |
11 |
||
16417 | 12 |
theory Hoare imports Main |
24472
943ef707396c
added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
24470
diff
changeset
|
13 |
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
|
14 |
begin |
1335 | 15 |
|
16 |
types |
|
13682 | 17 |
'a bexp = "'a set" |
18 |
'a assn = "'a set" |
|
5646 | 19 |
|
20 |
datatype |
|
13682 | 21 |
'a com = Basic "'a \<Rightarrow> 'a" |
22 |
| Seq "'a com" "'a com" ("(_;/ _)" [61,60] 60) |
|
23 |
| Cond "'a bexp" "'a com" "'a com" ("(1IF _/ THEN _ / ELSE _/ FI)" [0,0,0] 61) |
|
24 |
| While "'a bexp" "'a assn" "'a com" ("(1WHILE _/ INV {_} //DO _ /OD)" [0,0,0] 61) |
|
5646 | 25 |
|
26 |
syntax |
|
13682 | 27 |
"@assign" :: "id => 'b => 'a com" ("(2_ :=/ _)" [70,65] 61) |
28 |
"@annskip" :: "'a com" ("SKIP") |
|
5646 | 29 |
|
30 |
translations |
|
31 |
"SKIP" == "Basic id" |
|
32 |
||
13682 | 33 |
types 'a sem = "'a => 'a => bool" |
5646 | 34 |
|
13682 | 35 |
consts iter :: "nat => 'a bexp => 'a sem => 'a sem" |
5646 | 36 |
primrec |
37 |
"iter 0 b S = (%s s'. s ~: b & (s=s'))" |
|
38 |
"iter (Suc n) b S = (%s s'. s : b & (? s''. S s s'' & iter n b S s'' s'))" |
|
39 |
||
13682 | 40 |
consts Sem :: "'a com => 'a sem" |
5646 | 41 |
primrec |
42 |
"Sem(Basic f) s s' = (s' = f s)" |
|
43 |
"Sem(c1;c2) s s' = (? s''. Sem c1 s s'' & Sem c2 s'' s')" |
|
44 |
"Sem(IF b THEN c1 ELSE c2 FI) s s' = ((s : b --> Sem c1 s s') & |
|
45 |
(s ~: b --> Sem c2 s s'))" |
|
46 |
"Sem(While b x c) s s' = (? n. iter n b (Sem c) s s')" |
|
47 |
||
13682 | 48 |
constdefs Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool" |
5646 | 49 |
"Valid p c q == !s s'. Sem c s s' --> s : p --> s' : q" |
5007 | 50 |
|
51 |
||
1335 | 52 |
syntax |
13764 | 53 |
"@hoare_vars" :: "[idts, 'a assn,'a com,'a assn] => bool" |
13737 | 54 |
("VARS _// {_} // _ // {_}" [0,0,55,0] 50) |
5646 | 55 |
syntax ("" output) |
13682 | 56 |
"@hoare" :: "['a assn,'a com,'a assn] => bool" |
13737 | 57 |
("{_} // _ // {_}" [0,55,0] 50) |
1335 | 58 |
|
5646 | 59 |
(** parse translations **) |
1335 | 60 |
|
13682 | 61 |
ML{* |
13764 | 62 |
|
63 |
local |
|
13857 | 64 |
|
13764 | 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 |
|
1335 | 69 |
|
13764 | 70 |
fun mk_abstuple [x] body = abs (x, body) |
71 |
| mk_abstuple (x::xs) body = |
|
72 |
Syntax.const "split" $ abs (x, mk_abstuple xs body); |
|
1335 | 73 |
|
13857 | 74 |
fun mk_fbody a e [x as (b,_)] = if a=b then e else Syntax.free b |
13764 | 75 |
| mk_fbody a e ((b,_)::xs) = |
13857 | 76 |
Syntax.const "Pair" $ (if a=b then e else Syntax.free b) $ mk_fbody a e xs; |
13764 | 77 |
|
78 |
fun mk_fexp a e xs = mk_abstuple xs (mk_fbody a e xs) |
|
79 |
end |
|
13682 | 80 |
*} |
1335 | 81 |
|
5646 | 82 |
(* bexp_tr & assn_tr *) |
9397 | 83 |
(*all meta-variables for bexp except for TRUE are translated as if they |
5646 | 84 |
were boolean expressions*) |
13682 | 85 |
ML{* |
5646 | 86 |
fun bexp_tr (Const ("TRUE", _)) xs = Syntax.const "TRUE" |
87 |
| bexp_tr b xs = Syntax.const "Collect" $ mk_abstuple xs b; |
|
88 |
||
89 |
fun assn_tr r xs = Syntax.const "Collect" $ mk_abstuple xs r; |
|
13682 | 90 |
*} |
5646 | 91 |
(* com_tr *) |
13682 | 92 |
ML{* |
13764 | 93 |
fun com_tr (Const("@assign",_) $ Free (a,_) $ e) xs = |
94 |
Syntax.const "Basic" $ mk_fexp a e xs |
|
5646 | 95 |
| com_tr (Const ("Basic",_) $ f) xs = Syntax.const "Basic" $ f |
13764 | 96 |
| com_tr (Const ("Seq",_) $ c1 $ c2) xs = |
97 |
Syntax.const "Seq" $ com_tr c1 xs $ com_tr c2 xs |
|
98 |
| com_tr (Const ("Cond",_) $ b $ c1 $ c2) xs = |
|
99 |
Syntax.const "Cond" $ bexp_tr b xs $ com_tr c1 xs $ com_tr c2 xs |
|
100 |
| com_tr (Const ("While",_) $ b $ I $ c) xs = |
|
101 |
Syntax.const "While" $ bexp_tr b xs $ assn_tr I xs $ com_tr c xs |
|
102 |
| com_tr t _ = t (* if t is just a Free/Var *) |
|
13682 | 103 |
*} |
5646 | 104 |
|
17781 | 105 |
(* triple_tr *) (* FIXME does not handle "_idtdummy" *) |
13682 | 106 |
ML{* |
13764 | 107 |
local |
108 |
||
109 |
fun var_tr(Free(a,_)) = (a,Bound 0) (* Bound 0 = dummy term *) |
|
110 |
| var_tr(Const ("_constrain", _) $ (Free (a,_)) $ T) = (a,T); |
|
5646 | 111 |
|
13764 | 112 |
fun vars_tr (Const ("_idts", _) $ idt $ vars) = var_tr idt :: vars_tr vars |
113 |
| vars_tr t = [var_tr t] |
|
114 |
||
115 |
in |
|
5646 | 116 |
fun hoare_vars_tr [vars, pre, prg, post] = |
117 |
let val xs = vars_tr vars |
|
118 |
in Syntax.const "Valid" $ |
|
13764 | 119 |
assn_tr pre xs $ com_tr prg xs $ assn_tr post xs |
5646 | 120 |
end |
121 |
| hoare_vars_tr ts = raise TERM ("hoare_vars_tr", ts); |
|
13764 | 122 |
end |
13682 | 123 |
*} |
5646 | 124 |
|
13682 | 125 |
parse_translation {* [("@hoare_vars", hoare_vars_tr)] *} |
1335 | 126 |
|
127 |
||
5646 | 128 |
(*****************************************************************************) |
129 |
||
130 |
(*** print translations ***) |
|
13682 | 131 |
ML{* |
5646 | 132 |
fun dest_abstuple (Const ("split",_) $ (Abs(v,_, body))) = |
133 |
subst_bound (Syntax.free v, dest_abstuple body) |
|
134 |
| dest_abstuple (Abs(v,_, body)) = subst_bound (Syntax.free v, body) |
|
135 |
| dest_abstuple trm = trm; |
|
1335 | 136 |
|
5646 | 137 |
fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t |
138 |
| abs2list (Abs(x,T,t)) = [Free (x, T)] |
|
139 |
| abs2list _ = []; |
|
1335 | 140 |
|
5646 | 141 |
fun mk_ts (Const ("split",_) $ (Abs(x,_,t))) = mk_ts t |
142 |
| mk_ts (Abs(x,_,t)) = mk_ts t |
|
143 |
| mk_ts (Const ("Pair",_) $ a $ b) = a::(mk_ts b) |
|
144 |
| mk_ts t = [t]; |
|
1335 | 145 |
|
5646 | 146 |
fun mk_vts (Const ("split",_) $ (Abs(x,_,t))) = |
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; |
|
150 |
||
151 |
fun find_ch [] i xs = (false, (Syntax.free "not_ch",Syntax.free "not_ch" )) |
|
152 |
| find_ch ((v,t)::vts) i xs = if t=(Bound i) then find_ch vts (i-1) xs |
|
153 |
else (true, (v, subst_bounds (xs,t))); |
|
154 |
||
155 |
fun is_f (Const ("split",_) $ (Abs(x,_,t))) = true |
|
156 |
| is_f (Abs(x,_,t)) = true |
|
157 |
| is_f t = false; |
|
13682 | 158 |
*} |
159 |
||
5646 | 160 |
(* assn_tr' & bexp_tr'*) |
13682 | 161 |
ML{* |
5646 | 162 |
fun assn_tr' (Const ("Collect",_) $ T) = dest_abstuple T |
163 |
| assn_tr' (Const ("op Int",_) $ (Const ("Collect",_) $ T1) $ |
|
164 |
(Const ("Collect",_) $ T2)) = |
|
165 |
Syntax.const "op Int" $ dest_abstuple T1 $ dest_abstuple T2 |
|
166 |
| assn_tr' t = t; |
|
1335 | 167 |
|
5646 | 168 |
fun bexp_tr' (Const ("Collect",_) $ T) = dest_abstuple T |
169 |
| bexp_tr' t = t; |
|
13682 | 170 |
*} |
5646 | 171 |
|
172 |
(*com_tr' *) |
|
13682 | 173 |
ML{* |
5646 | 174 |
fun mk_assign f = |
175 |
let val (vs, ts) = mk_vts f; |
|
176 |
val (ch, which) = find_ch (vs~~ts) ((length vs)-1) (rev vs) |
|
177 |
in if ch then Syntax.const "@assign" $ fst(which) $ snd(which) |
|
178 |
else Syntax.const "@skip" end; |
|
1335 | 179 |
|
5646 | 180 |
fun com_tr' (Const ("Basic",_) $ f) = if is_f f then mk_assign f |
181 |
else Syntax.const "Basic" $ f |
|
182 |
| com_tr' (Const ("Seq",_) $ c1 $ c2) = Syntax.const "Seq" $ |
|
183 |
com_tr' c1 $ com_tr' c2 |
|
184 |
| com_tr' (Const ("Cond",_) $ b $ c1 $ c2) = Syntax.const "Cond" $ |
|
185 |
bexp_tr' b $ com_tr' c1 $ com_tr' c2 |
|
186 |
| com_tr' (Const ("While",_) $ b $ I $ c) = Syntax.const "While" $ |
|
187 |
bexp_tr' b $ assn_tr' I $ com_tr' c |
|
188 |
| com_tr' t = t; |
|
1335 | 189 |
|
190 |
||
5646 | 191 |
fun spec_tr' [p, c, q] = |
192 |
Syntax.const "@hoare" $ assn_tr' p $ com_tr' c $ assn_tr' q |
|
13682 | 193 |
*} |
194 |
||
195 |
print_translation {* [("Valid", spec_tr')] *} |
|
196 |
||
13857 | 197 |
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q" |
198 |
by (auto simp:Valid_def) |
|
199 |
||
200 |
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q" |
|
201 |
by (auto simp:Valid_def) |
|
202 |
||
203 |
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R" |
|
204 |
by (auto simp:Valid_def) |
|
205 |
||
206 |
lemma CondRule: |
|
207 |
"p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')} |
|
208 |
\<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q" |
|
209 |
by (auto simp:Valid_def) |
|
210 |
||
211 |
lemma iter_aux: "! s s'. Sem c s s' --> s : I & s : b --> s' : I ==> |
|
212 |
(\<And>s s'. s : I \<Longrightarrow> iter n b (Sem c) s s' \<Longrightarrow> s' : I & s' ~: b)"; |
|
213 |
apply(induct n) |
|
214 |
apply clarsimp |
|
215 |
apply(simp (no_asm_use)) |
|
216 |
apply blast |
|
217 |
done |
|
218 |
||
219 |
lemma WhileRule: |
|
220 |
"p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q" |
|
221 |
apply (clarsimp simp:Valid_def) |
|
222 |
apply(drule iter_aux) |
|
223 |
prefer 2 apply assumption |
|
224 |
apply blast |
|
225 |
apply blast |
|
226 |
done |
|
227 |
||
228 |
||
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
|
229 |
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
|
230 |
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
|
231 |
|
24472
943ef707396c
added Hoare/hoare_tac.ML (code from Hoare/Hoare.thy, also required in Isar_examples/Hoare.thy);
wenzelm
parents:
24470
diff
changeset
|
232 |
use "hoare_tac.ML" |
13682 | 233 |
|
234 |
method_setup vcg = {* |
|
21588 | 235 |
Method.no_args (Method.SIMPLE_METHOD' (hoare_tac (K all_tac))) *} |
13682 | 236 |
"verification condition generator" |
237 |
||
238 |
method_setup vcg_simp = {* |
|
239 |
Method.ctxt_args (fn ctxt => |
|
21588 | 240 |
Method.SIMPLE_METHOD' (hoare_tac (asm_full_simp_tac (local_simpset_of ctxt)))) *} |
13682 | 241 |
"verification condition generator plus simplification" |
242 |
||
243 |
end |