author | paulson |
Thu, 29 Jul 1999 12:44:57 +0200 | |
changeset 7127 | 48e235179ffb |
parent 6162 | 484adda70b65 |
child 8573 | fc22f59f5ae7 |
permissions | -rw-r--r-- |
1465 | 1 |
(* Title: HOL/Hoare/Hoare.ML |
1335 | 2 |
ID: $Id$ |
5646 | 3 |
Author: Leonor Prensa Nieto & Tobias Nipkow |
4 |
Copyright 1998 TUM |
|
1335 | 5 |
|
5646 | 6 |
Derivation of the proof rules and, most importantly, the VCG tactic. |
1335 | 7 |
*) |
8 |
||
5646 | 9 |
(*** The proof rules ***) |
1335 | 10 |
|
5646 | 11 |
Goalw [Valid_def] "p <= q ==> Valid p SKIP q"; |
6162 | 12 |
by (Auto_tac); |
5646 | 13 |
qed "SkipRule"; |
1335 | 14 |
|
5646 | 15 |
Goalw [Valid_def] "p <= {s. (f s):q} ==> Valid p (Basic f) q"; |
6162 | 16 |
by (Auto_tac); |
5646 | 17 |
qed "BasicRule"; |
1335 | 18 |
|
5646 | 19 |
Goalw [Valid_def] "[| Valid P c1 Q; Valid Q c2 R |] ==> Valid P (c1;c2) R"; |
6162 | 20 |
by (Asm_simp_tac 1); |
21 |
by (Blast_tac 1); |
|
5646 | 22 |
qed "SeqRule"; |
1335 | 23 |
|
5646 | 24 |
Goalw [Valid_def] |
25 |
"[| p <= {s. (s:b --> s:w) & (s~:b --> s:w')}; \ |
|
26 |
\ Valid w c1 q; Valid w' c2 q |] \ |
|
27 |
\ ==> Valid p (IF b THEN c1 ELSE c2 FI) q"; |
|
6162 | 28 |
by (Asm_simp_tac 1); |
29 |
by (Blast_tac 1); |
|
5646 | 30 |
qed "CondRule"; |
1335 | 31 |
|
5646 | 32 |
Goal "! s s'. Sem c s s' --> s : I Int b --> s' : I ==> \ |
33 |
\ ! s s'. s : I --> iter n b (Sem c) s s' --> s' : I & s' ~: b"; |
|
6162 | 34 |
by (induct_tac "n" 1); |
35 |
by (Asm_simp_tac 1); |
|
36 |
by (Simp_tac 1); |
|
37 |
by (Blast_tac 1); |
|
5646 | 38 |
val lemma = result() RS spec RS spec RS mp RS mp; |
1335 | 39 |
|
5646 | 40 |
Goalw [Valid_def] |
7127
48e235179ffb
added parentheses to cope with a possible reduction of the precedence of unary
paulson
parents:
6162
diff
changeset
|
41 |
"[| p <= i; Valid (i Int b) c i; i Int (-b) <= q |] \ |
5646 | 42 |
\ ==> Valid p (WHILE b INV {i} DO c OD) q"; |
6162 | 43 |
by (Asm_simp_tac 1); |
44 |
by (Clarify_tac 1); |
|
45 |
by (dtac lemma 1); |
|
46 |
by (assume_tac 2); |
|
47 |
by (Blast_tac 1); |
|
48 |
by (Blast_tac 1); |
|
5646 | 49 |
qed "WhileRule"; |
1335 | 50 |
|
5646 | 51 |
(*** The tactics ***) |
1335 | 52 |
|
5646 | 53 |
(*****************************************************************************) |
54 |
(** The function Mset makes the theorem **) |
|
55 |
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}", **) |
|
56 |
(** where (x1,...,xn) are the variables of the particular program we are **) |
|
57 |
(** working on at the moment of the call. For instance, (found,x,y) are **) |
|
58 |
(** the variables of the Zero Search program. **) |
|
59 |
(*****************************************************************************) |
|
1335 | 60 |
|
5646 | 61 |
local open HOLogic in |
1335 | 62 |
|
5646 | 63 |
(** maps (%x1 ... xn. t) to [x1,...,xn] **) |
64 |
fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t |
|
65 |
| abs2list (Abs(x,T,t)) = [Free (x, T)] |
|
66 |
| abs2list _ = []; |
|
67 |
||
68 |
(** maps {(x1,...,xn). t} to [x1,...,xn] **) |
|
69 |
fun mk_vars (Const ("Collect",_) $ T) = abs2list T |
|
70 |
| mk_vars _ = []; |
|
1335 | 71 |
|
5646 | 72 |
(** abstraction of body over a tuple formed from a list of free variables. |
73 |
Types are also built **) |
|
74 |
fun mk_abstupleC [] body = absfree ("x", unitT, body) |
|
75 |
| mk_abstupleC (v::w) body = let val (n,T) = dest_Free v |
|
76 |
in if w=[] then absfree (n, T, body) |
|
77 |
else let val z = mk_abstupleC w body; |
|
78 |
val T2 = case z of Abs(_,T,_) => T |
|
79 |
| Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T; |
|
80 |
in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT) |
|
81 |
$ absfree (n, T, z) end end; |
|
1335 | 82 |
|
5646 | 83 |
(** maps [x1,...,xn] to (x1,...,xn) and types**) |
84 |
fun mk_bodyC [] = Const ("()", unitT) |
|
85 |
| mk_bodyC (x::xs) = if xs=[] then x |
|
86 |
else let val (n, T) = dest_Free x ; |
|
87 |
val z = mk_bodyC xs; |
|
88 |
val T2 = case z of Free(_, T) => T |
|
89 |
| Const ("Pair", Type ("fun", [_, Type |
|
90 |
("fun", [_, T])])) $ _ $ _ => T; |
|
91 |
in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) $ x $ z end; |
|
1335 | 92 |
|
5646 | 93 |
fun dest_Goal (Const ("Goal", _) $ P) = P; |
1335 | 94 |
|
5646 | 95 |
(** maps a goal of the form: |
96 |
1. [| P |] ==> |- VARS x1 ... xn. {._.} _ {._.} or to [x1,...,xn]**) |
|
97 |
fun get_vars thm = let val c = dest_Goal (concl_of (thm)); |
|
98 |
val d = Logic.strip_assums_concl c; |
|
99 |
val Const _ $ pre $ _ $ _ = dest_Trueprop d; |
|
100 |
in mk_vars pre end; |
|
1335 | 101 |
|
102 |
||
5646 | 103 |
(** Makes Collect with type **) |
104 |
fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm |
|
105 |
in Collect_const t $ trm end; |
|
106 |
||
107 |
fun inclt ty = Const ("op <=", [ty,ty] ---> boolT); |
|
108 |
||
109 |
(** Makes "Mset <= t" **) |
|
110 |
fun Mset_incl t = let val MsetT = fastype_of t |
|
111 |
in mk_Trueprop ((inclt MsetT) $ Free ("Mset", MsetT) $ t) end; |
|
112 |
||
1335 | 113 |
|
5646 | 114 |
fun Mset thm = let val vars = get_vars(thm); |
115 |
val varsT = fastype_of (mk_bodyC vars); |
|
116 |
val big_Collect = mk_CollectC (mk_abstupleC vars |
|
117 |
(Free ("P",varsT --> boolT) $ mk_bodyC vars)); |
|
118 |
val small_Collect = mk_CollectC (Abs("x",varsT, |
|
119 |
Free ("P",varsT --> boolT) $ Bound 0)); |
|
120 |
val impl = implies $ (Mset_incl big_Collect) $ |
|
121 |
(Mset_incl small_Collect); |
|
122 |
val cimpl = cterm_of (#sign (rep_thm thm)) impl |
|
123 |
in prove_goalw_cterm [] cimpl (fn prems => |
|
124 |
[cut_facts_tac prems 1,Blast_tac 1]) end; |
|
1335 | 125 |
|
5646 | 126 |
end; |
3537 | 127 |
|
1335 | 128 |
|
5646 | 129 |
(*****************************************************************************) |
130 |
(** Simplifying: **) |
|
131 |
(** Some useful lemmata, lists and simplification tactics to control which **) |
|
132 |
(** theorems are used to simplify at each moment, so that the original **) |
|
133 |
(** input does not suffer any unexpected transformation **) |
|
134 |
(*****************************************************************************) |
|
135 |
||
136 |
val Compl_Collect = prove_goal thy "-(Collect b) = {x. ~(b x)}" |
|
137 |
(fn _ => [Fast_tac 1]); |
|
138 |
||
139 |
(**Simp_tacs**) |
|
1335 | 140 |
|
5646 | 141 |
val before_set2pred_simp_tac = |
142 |
(simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym,Compl_Collect])); |
|
143 |
||
144 |
val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split])); |
|
145 |
||
146 |
(*****************************************************************************) |
|
147 |
(** set2pred transforms sets inclusion into predicates implication, **) |
|
148 |
(** maintaining the original variable names. **) |
|
149 |
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1" **) |
|
150 |
(** Subgoals containing intersections (A Int B) or complement sets (-A) **) |
|
151 |
(** are first simplified by "before_set2pred_simp_tac", that returns only **) |
|
152 |
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily **) |
|
153 |
(** transformed. **) |
|
154 |
(** This transformation may solve very easy subgoals due to a ligth **) |
|
155 |
(** simplification done by (split_all_tac) **) |
|
156 |
(*****************************************************************************) |
|
1335 | 157 |
|
5646 | 158 |
fun set2pred i thm = let fun mk_string [] = "" |
159 |
| mk_string (x::xs) = x^" "^mk_string xs; |
|
160 |
val vars=get_vars(thm); |
|
161 |
val var_string = mk_string (map (fst o dest_Free) vars); |
|
162 |
in ((before_set2pred_simp_tac i) THEN_MAYBE |
|
163 |
(EVERY [rtac subsetI i, |
|
164 |
rtac CollectI i, |
|
165 |
dtac CollectD i, |
|
166 |
(TRY(split_all_tac i)) THEN_MAYBE |
|
167 |
((rename_tac var_string i) THEN |
|
168 |
(full_simp_tac (HOL_basic_ss addsimps [split]) i)) ])) thm |
|
169 |
end; |
|
170 |
||
171 |
(*****************************************************************************) |
|
172 |
(** BasicSimpTac is called to simplify all verification conditions. It does **) |
|
173 |
(** a light simplification by applying "mem_Collect_eq", then it calls **) |
|
174 |
(** MaxSimpTac, which solves subgoals of the form "A <= A", **) |
|
175 |
(** and transforms any other into predicates, applying then **) |
|
176 |
(** the tactic chosen by the user, which may solve the subgoal completely. **) |
|
177 |
(*****************************************************************************) |
|
178 |
||
179 |
fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac]; |
|
1335 | 180 |
|
5646 | 181 |
fun BasicSimpTac tac = |
182 |
simp_tac (HOL_basic_ss addsimps [mem_Collect_eq,split]) |
|
183 |
THEN_MAYBE' MaxSimpTac tac; |
|
184 |
||
185 |
(** HoareRuleTac **) |
|
186 |
||
187 |
fun WlpTac Mlem tac i = rtac SeqRule i THEN HoareRuleTac Mlem tac false (i+1) |
|
188 |
and HoareRuleTac Mlem tac pre_cond i st = st |> |
|
189 |
(*abstraction over st prevents looping*) |
|
190 |
( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i) |
|
191 |
ORELSE |
|
192 |
(FIRST[rtac SkipRule i, |
|
193 |
EVERY[rtac BasicRule i, |
|
194 |
rtac Mlem i, |
|
195 |
split_simp_tac i], |
|
196 |
EVERY[rtac CondRule i, |
|
197 |
HoareRuleTac Mlem tac false (i+2), |
|
198 |
HoareRuleTac Mlem tac false (i+1)], |
|
199 |
EVERY[rtac WhileRule i, |
|
200 |
BasicSimpTac tac (i+2), |
|
201 |
HoareRuleTac Mlem tac true (i+1)] ] |
|
202 |
THEN (if pre_cond then (BasicSimpTac tac i) else (rtac subset_refl i)) )); |
|
1335 | 203 |
|
204 |
||
5646 | 205 |
(** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **) |
206 |
(** the final verification conditions **) |
|
207 |
||
208 |
fun hoare_tac tac i thm = |
|
209 |
let val Mlem = Mset(thm) |
|
210 |
in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end; |