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