author | wenzelm |
Tue, 17 May 2005 18:51:16 +0200 | |
changeset 15992 | cb02d70a2040 |
parent 15661 | 9ef583b08647 |
child 17956 | 369e2af8ee45 |
permissions | -rw-r--r-- |
13857 | 1 |
(* Title: HOL/Hoare/Hoare.ML |
2 |
ID: $Id$ |
|
3 |
Author: Leonor Prensa Nieto & Tobias Nipkow |
|
4 |
Copyright 1998 TUM |
|
5 |
||
6 |
Derivation of the proof rules and, most importantly, the VCG tactic. |
|
7 |
*) |
|
8 |
||
9 |
val SkipRule = thm"SkipRule"; |
|
10 |
val BasicRule = thm"BasicRule"; |
|
11 |
val AbortRule = thm"AbortRule"; |
|
12 |
val SeqRule = thm"SeqRule"; |
|
13 |
val CondRule = thm"CondRule"; |
|
14 |
val WhileRule = thm"WhileRule"; |
|
15 |
||
16 |
(*** The tactics ***) |
|
17 |
||
18 |
(*****************************************************************************) |
|
19 |
(** The function Mset makes the theorem **) |
|
20 |
(** "?Mset <= {(x1,...,xn). ?P (x1,...,xn)} ==> ?Mset <= {s. ?P s}", **) |
|
21 |
(** where (x1,...,xn) are the variables of the particular program we are **) |
|
22 |
(** working on at the moment of the call **) |
|
23 |
(*****************************************************************************) |
|
24 |
||
25 |
local open HOLogic in |
|
26 |
||
27 |
(** maps (%x1 ... xn. t) to [x1,...,xn] **) |
|
28 |
fun abs2list (Const ("split",_) $ (Abs(x,T,t))) = Free (x, T)::abs2list t |
|
29 |
| abs2list (Abs(x,T,t)) = [Free (x, T)] |
|
30 |
| abs2list _ = []; |
|
31 |
||
32 |
(** maps {(x1,...,xn). t} to [x1,...,xn] **) |
|
33 |
fun mk_vars (Const ("Collect",_) $ T) = abs2list T |
|
34 |
| mk_vars _ = []; |
|
35 |
||
36 |
(** abstraction of body over a tuple formed from a list of free variables. |
|
37 |
Types are also built **) |
|
38 |
fun mk_abstupleC [] body = absfree ("x", unitT, body) |
|
39 |
| mk_abstupleC (v::w) body = let val (n,T) = dest_Free v |
|
40 |
in if w=[] then absfree (n, T, body) |
|
41 |
else let val z = mk_abstupleC w body; |
|
42 |
val T2 = case z of Abs(_,T,_) => T |
|
43 |
| Const (_, Type (_,[_, Type (_,[T,_])])) $ _ => T; |
|
44 |
in Const ("split", (T --> T2 --> boolT) --> mk_prodT (T,T2) --> boolT) |
|
45 |
$ absfree (n, T, z) end end; |
|
46 |
||
47 |
(** maps [x1,...,xn] to (x1,...,xn) and types**) |
|
48 |
fun mk_bodyC [] = HOLogic.unit |
|
49 |
| mk_bodyC (x::xs) = if xs=[] then x |
|
50 |
else let val (n, T) = dest_Free x ; |
|
51 |
val z = mk_bodyC xs; |
|
52 |
val T2 = case z of Free(_, T) => T |
|
53 |
| Const ("Pair", Type ("fun", [_, Type |
|
54 |
("fun", [_, T])])) $ _ $ _ => T; |
|
55 |
in Const ("Pair", [T, T2] ---> mk_prodT (T, T2)) $ x $ z end; |
|
56 |
||
57 |
fun dest_Goal (Const ("Goal", _) $ P) = P; |
|
58 |
||
59 |
(** maps a goal of the form: |
|
60 |
1. [| P |] ==> VARS x1 ... xn {._.} _ {._.} or to [x1,...,xn]**) |
|
61 |
fun get_vars thm = let val c = dest_Goal (concl_of (thm)); |
|
62 |
val d = Logic.strip_assums_concl c; |
|
63 |
val Const _ $ pre $ _ $ _ = dest_Trueprop d; |
|
64 |
in mk_vars pre end; |
|
65 |
||
66 |
||
67 |
(** Makes Collect with type **) |
|
68 |
fun mk_CollectC trm = let val T as Type ("fun",[t,_]) = fastype_of trm |
|
69 |
in Collect_const t $ trm end; |
|
70 |
||
71 |
fun inclt ty = Const ("op <=", [ty,ty] ---> boolT); |
|
72 |
||
73 |
(** Makes "Mset <= t" **) |
|
74 |
fun Mset_incl t = let val MsetT = fastype_of t |
|
75 |
in mk_Trueprop ((inclt MsetT) $ Free ("Mset", MsetT) $ t) end; |
|
76 |
||
77 |
||
78 |
fun Mset thm = let val vars = get_vars(thm); |
|
79 |
val varsT = fastype_of (mk_bodyC vars); |
|
80 |
val big_Collect = mk_CollectC (mk_abstupleC vars |
|
81 |
(Free ("P",varsT --> boolT) $ mk_bodyC vars)); |
|
82 |
val small_Collect = mk_CollectC (Abs("x",varsT, |
|
83 |
Free ("P",varsT --> boolT) $ Bound 0)); |
|
84 |
val impl = implies $ (Mset_incl big_Collect) $ |
|
85 |
(Mset_incl small_Collect); |
|
86 |
in Tactic.prove (Thm.sign_of_thm thm) ["Mset", "P"] [] impl (K (CLASET' blast_tac 1)) end; |
|
87 |
||
88 |
end; |
|
89 |
||
90 |
||
91 |
(*****************************************************************************) |
|
92 |
(** Simplifying: **) |
|
15661
9ef583b08647
reverted renaming of Some/None in comments and strings;
wenzelm
parents:
15531
diff
changeset
|
93 |
(** Some useful lemmata, lists and simplification tactics to control which **) |
13857 | 94 |
(** theorems are used to simplify at each moment, so that the original **) |
95 |
(** input does not suffer any unexpected transformation **) |
|
96 |
(*****************************************************************************) |
|
97 |
||
98 |
Goal "-(Collect b) = {x. ~(b x)}"; |
|
99 |
by (Fast_tac 1); |
|
100 |
qed "Compl_Collect"; |
|
101 |
||
102 |
||
103 |
(**Simp_tacs**) |
|
104 |
||
105 |
val before_set2pred_simp_tac = |
|
106 |
(simp_tac (HOL_basic_ss addsimps [Collect_conj_eq RS sym,Compl_Collect])); |
|
107 |
||
108 |
val split_simp_tac = (simp_tac (HOL_basic_ss addsimps [split_conv])); |
|
109 |
||
110 |
(*****************************************************************************) |
|
111 |
(** set2pred transforms sets inclusion into predicates implication, **) |
|
112 |
(** maintaining the original variable names. **) |
|
113 |
(** Ex. "{x. x=0} <= {x. x <= 1}" -set2pred-> "x=0 --> x <= 1" **) |
|
114 |
(** Subgoals containing intersections (A Int B) or complement sets (-A) **) |
|
115 |
(** are first simplified by "before_set2pred_simp_tac", that returns only **) |
|
116 |
(** subgoals of the form "{x. P x} <= {x. Q x}", which are easily **) |
|
117 |
(** transformed. **) |
|
118 |
(** This transformation may solve very easy subgoals due to a ligth **) |
|
119 |
(** simplification done by (split_all_tac) **) |
|
120 |
(*****************************************************************************) |
|
121 |
||
122 |
fun set2pred i thm = let fun mk_string [] = "" |
|
123 |
| mk_string (x::xs) = x^" "^mk_string xs; |
|
124 |
val vars=get_vars(thm); |
|
125 |
val var_string = mk_string (map (fst o dest_Free) vars); |
|
126 |
in ((before_set2pred_simp_tac i) THEN_MAYBE |
|
127 |
(EVERY [rtac subsetI i, |
|
128 |
rtac CollectI i, |
|
129 |
dtac CollectD i, |
|
130 |
(TRY(split_all_tac i)) THEN_MAYBE |
|
131 |
((rename_tac var_string i) THEN |
|
132 |
(full_simp_tac (HOL_basic_ss addsimps [split_conv]) i)) ])) thm |
|
133 |
end; |
|
134 |
||
135 |
(*****************************************************************************) |
|
136 |
(** BasicSimpTac is called to simplify all verification conditions. It does **) |
|
137 |
(** a light simplification by applying "mem_Collect_eq", then it calls **) |
|
138 |
(** MaxSimpTac, which solves subgoals of the form "A <= A", **) |
|
139 |
(** and transforms any other into predicates, applying then **) |
|
140 |
(** the tactic chosen by the user, which may solve the subgoal completely. **) |
|
141 |
(*****************************************************************************) |
|
142 |
||
143 |
fun MaxSimpTac tac = FIRST'[rtac subset_refl, set2pred THEN_MAYBE' tac]; |
|
144 |
||
145 |
fun BasicSimpTac tac = |
|
146 |
simp_tac |
|
147 |
(HOL_basic_ss addsimps [mem_Collect_eq,split_conv] addsimprocs [record_simproc]) |
|
148 |
THEN_MAYBE' MaxSimpTac tac; |
|
149 |
||
150 |
(** HoareRuleTac **) |
|
151 |
||
152 |
fun WlpTac Mlem tac i = |
|
153 |
rtac SeqRule i THEN HoareRuleTac Mlem tac false (i+1) |
|
154 |
and HoareRuleTac Mlem tac pre_cond i st = st |> |
|
155 |
(*abstraction over st prevents looping*) |
|
156 |
( (WlpTac Mlem tac i THEN HoareRuleTac Mlem tac pre_cond i) |
|
157 |
ORELSE |
|
158 |
(FIRST[rtac SkipRule i, |
|
159 |
rtac AbortRule i, |
|
160 |
EVERY[rtac BasicRule i, |
|
161 |
rtac Mlem i, |
|
162 |
split_simp_tac i], |
|
163 |
EVERY[rtac CondRule i, |
|
164 |
HoareRuleTac Mlem tac false (i+2), |
|
165 |
HoareRuleTac Mlem tac false (i+1)], |
|
166 |
EVERY[rtac WhileRule i, |
|
167 |
BasicSimpTac tac (i+2), |
|
168 |
HoareRuleTac Mlem tac true (i+1)] ] |
|
169 |
THEN (if pre_cond then (BasicSimpTac tac i) else rtac subset_refl i) )); |
|
170 |
||
171 |
||
172 |
(** tac:(int -> tactic) is the tactic the user chooses to solve or simplify **) |
|
173 |
(** the final verification conditions **) |
|
174 |
||
175 |
fun hoare_tac tac i thm = |
|
176 |
let val Mlem = Mset(thm) |
|
177 |
in SELECT_GOAL(EVERY[HoareRuleTac Mlem tac true 1]) i thm end; |